]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/link: emit LC_BUILD_VERSION on Mach-O
authorCherry Zhang <cherryyz@google.com>
Wed, 21 Apr 2021 23:12:38 +0000 (19:12 -0400)
committerCherry Zhang <cherryyz@google.com>
Thu, 22 Apr 2021 14:40:40 +0000 (14:40 +0000)
LC_VERSION_MIN_MACOSX seems deprecated. Emit LC_BUILD_VERSION
instead. Also emit it on darwin/arm64, where it was not emitted
before.

Fixes #45091.

Change-Id: I18fb80d571f681da3bd258e53beb520e68f354bd
Reviewed-on: https://go-review.googlesource.com/c/go/+/312550
Trust: Cherry Zhang <cherryyz@google.com>
Run-TryBot: Cherry Zhang <cherryyz@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Than McIntosh <thanm@google.com>
src/cmd/link/internal/ld/macho.go
src/cmd/link/link_test.go

index 1c88c05dd4af7a70ac1830a2dec809ad66a76dd5..85269b30d04e37228012d90f5f4ff1eb4fb69f01 100644 (file)
@@ -472,24 +472,20 @@ func (ctxt *Link) domacho() {
                if buildcfg.GOOS == "ios" {
                        machoPlatform = PLATFORM_IOS
                }
-               switch ctxt.Arch.Family {
-               default:
-                       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.
-                               //
+               if ctxt.LinkMode == LinkInternal && machoPlatform == PLATFORM_MACOS {
+                       var version uint32
+                       switch ctxt.Arch.Family {
+                       case sys.AMD64:
                                // The version must be at least 10.9; see golang.org/issues/30488.
-                               ml := newMachoLoad(ctxt.Arch, LC_VERSION_MIN_MACOSX, 2)
-                               ml.data[0] = 10<<16 | 9<<8 | 0<<0 // OS X version 10.9.0
-                               ml.data[1] = 10<<16 | 9<<8 | 0<<0 // SDK 10.9.0
+                               version = 10<<16 | 9<<8 | 0<<0 // 10.9.0
+                       case sys.ARM64:
+                               version = 11<<16 | 0<<8 | 0<<0 // 11.0.0
                        }
-               case sys.ARM64:
+                       ml := newMachoLoad(ctxt.Arch, LC_BUILD_VERSION, 4)
+                       ml.data[0] = uint32(machoPlatform)
+                       ml.data[1] = version // OS version
+                       ml.data[2] = version // SDK version
+                       ml.data[3] = 0       // ntools
                }
        }
 
index 9369e550f40b38ff0a5fffee73e1e053f20ede25..985aed49e2bb2c07e5bd621896e67859f3762191 100644 (file)
@@ -333,12 +333,12 @@ func TestXFlag(t *testing.T) {
        }
 }
 
-var testMacOSVersionSrc = `
+var testMachOBuildVersionSrc = `
 package main
 func main() { }
 `
 
-func TestMacOSVersion(t *testing.T) {
+func TestMachOBuildVersion(t *testing.T) {
        testenv.MustHaveGoBuild(t)
 
        t.Parallel()
@@ -346,7 +346,7 @@ func TestMacOSVersion(t *testing.T) {
        tmpdir := t.TempDir()
 
        src := filepath.Join(tmpdir, "main.go")
-       err := ioutil.WriteFile(src, []byte(testMacOSVersionSrc), 0666)
+       err := ioutil.WriteFile(src, []byte(testMachOBuildVersionSrc), 0666)
        if err != nil {
                t.Fatal(err)
        }
@@ -371,28 +371,28 @@ func TestMacOSVersion(t *testing.T) {
                t.Fatal(err)
        }
        found := false
-       const LC_VERSION_MIN_MACOSX = 0x24
+       const LC_BUILD_VERSION = 0x32
        checkMin := func(ver uint32) {
                major, minor := (ver>>16)&0xff, (ver>>8)&0xff
                if major != 10 || minor < 9 {
-                       t.Errorf("LC_VERSION_MIN_MACOSX version %d.%d < 10.9", major, minor)
+                       t.Errorf("LC_BUILD_VERSION version %d.%d < 10.9", major, minor)
                }
        }
        for _, cmd := range exem.Loads {
                raw := cmd.Raw()
                type_ := exem.ByteOrder.Uint32(raw)
-               if type_ != LC_VERSION_MIN_MACOSX {
+               if type_ != LC_BUILD_VERSION {
                        continue
                }
-               osVer := exem.ByteOrder.Uint32(raw[8:])
+               osVer := exem.ByteOrder.Uint32(raw[12:])
                checkMin(osVer)
-               sdkVer := exem.ByteOrder.Uint32(raw[12:])
+               sdkVer := exem.ByteOrder.Uint32(raw[16:])
                checkMin(sdkVer)
                found = true
                break
        }
        if !found {
-               t.Errorf("no LC_VERSION_MIN_MACOSX load command found")
+               t.Errorf("no LC_BUILD_VERSION load command found")
        }
 }