]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/link/internal/ld: consolidate macho platform setup
authorElias Naur <mail@eliasnaur.com>
Sat, 27 Apr 2019 09:28:49 +0000 (11:28 +0200)
committerElias Naur <mail@eliasnaur.com>
Sat, 27 Apr 2019 21:29:58 +0000 (21:29 +0000)
Determine the macho platform once and use that the two places that
need it. This makes it easier to add a third platform check for a
follow-up change.

Updates #31447

Change-Id: I522a5fface647ab8e608f816c5832d531534df7a
Reviewed-on: https://go-review.googlesource.com/c/go/+/174198
Run-TryBot: Elias Naur <mail@eliasnaur.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Keith Randall <khr@golang.org>
src/cmd/link/internal/ld/lib.go
src/cmd/link/internal/ld/macho.go

index c4748781911aa4ba83c7cf700908e540f7c1f80c..d6555a56338327a69d16965929aac0d3a9ea1322 100644 (file)
@@ -1482,12 +1482,8 @@ func (ctxt *Link) hostlink() {
                if err != nil {
                        Exitf("%s: parsing Mach-O header failed: %v", os.Args[0], err)
                }
-               load, err := peekMachoPlatform(exem)
-               if err != nil {
-                       Exitf("%s: failed to parse Mach-O load commands: %v", os.Args[0], err)
-               }
                // Only macOS supports unmapped segments such as our __DWARF segment.
-               if load == nil || load.platform == PLATFORM_MACOS {
+               if machoPlatform == PLATFORM_MACOS {
                        if err := machoCombineDwarf(ctxt, exef, exem, dsym, combinedOutput); err != nil {
                                Exitf("%s: combining dwarf failed: %v", os.Args[0], err)
                        }
index f577ed1fc3ca8b0d0aad8d6fa9658c0cb2bfc4dc..8500b61db7c31c3048533eaf3702d60250321928 100644 (file)
@@ -197,6 +197,8 @@ var machohdr MachoHdr
 
 var load []MachoLoad
 
+var machoPlatform MachoPlatform
+
 var seg [16]MachoSeg
 
 var nseg int
@@ -388,6 +390,36 @@ func (ctxt *Link) domacho() {
                return
        }
 
+       // Copy platform load command.
+       for _, h := range hostobj {
+               load, err := hostobjMachoPlatform(&h)
+               if err != nil {
+                       Exitf("%v", err)
+               }
+               if load != nil {
+                       machoPlatform = load.platform
+                       ml := newMachoLoad(ctxt.Arch, load.cmd.type_, uint32(len(load.cmd.data)))
+                       copy(ml.data, load.cmd.data)
+                       break
+               }
+       }
+       if machoPlatform == 0 {
+               machoPlatform = PLATFORM_MACOS
+               if ctxt.LinkMode == LinkInternal {
+                       // For lldb, must say LC_VERSION_MIN_MACOSX or else
+                       // it won't know that this Mach-O binary is from OS X
+                       // (could be iOS or WatchOS instead).
+                       // Go on iOS uses linkmode=external, and linkmode=external
+                       // adds this itself. So we only need this code for linkmode=internal
+                       // and we can assume OS X.
+                       //
+                       // See golang.org/issues/12941.
+                       ml := newMachoLoad(ctxt.Arch, LC_VERSION_MIN_MACOSX, 2)
+                       ml.data[0] = 10<<16 | 7<<8 | 0<<0 // OS X version 10.7.0
+                       ml.data[1] = 10<<16 | 7<<8 | 0<<0 // SDK 10.7.0
+               }
+       }
+
        // empirically, string table must begin with " \x00".
        s := ctxt.Syms.Lookup(".machosymstr", 0)
 
@@ -690,32 +722,6 @@ func Asmbmacho(ctxt *Link) {
                        }
                }
        }
-       foundLoad := false
-       for _, h := range hostobj {
-               load, err := hostobjMachoPlatform(&h)
-               if err != nil {
-                       Exitf("%v", err)
-               }
-               if load != nil {
-                       ml := newMachoLoad(ctxt.Arch, load.cmd.type_, uint32(len(load.cmd.data)))
-                       copy(ml.data, load.cmd.data)
-                       foundLoad = true
-                       break
-               }
-       }
-       if !foundLoad && ctxt.LinkMode == LinkInternal {
-               // For lldb, must say LC_VERSION_MIN_MACOSX or else
-               // it won't know that this Mach-O binary is from OS X
-               // (could be iOS or WatchOS instead).
-               // Go on iOS uses linkmode=external, and linkmode=external
-               // adds this itself. So we only need this code for linkmode=internal
-               // and we can assume OS X.
-               //
-               // See golang.org/issues/12941.
-               ml := newMachoLoad(ctxt.Arch, LC_VERSION_MIN_MACOSX, 2)
-               ml.data[0] = 10<<16 | 7<<8 | 0<<0 // OS X version 10.7.0
-               ml.data[1] = 10<<16 | 7<<8 | 0<<0 // SDK 10.7.0
-       }
 
        a := machowrite(ctxt.Arch, ctxt.Out, ctxt.LinkMode)
        if int32(a) > HEADR {