Skip to content

Commit e8f9185

Browse files
committed
cmd/link: strip STAB (symbolic debugging) symbols on darwin
On darwin, with external linking, the system linker produces STAB (symbolic debugging) symbols in the binary's symbol table. These include paths of the intermediate object files, like <tmpdir>/go.o, which changes from run to run, making the build non-reproducible. Since we run dsymutil to produce debug info and combine them back into the binary, we don't need those STAB symbols anymore. Strip them after running dsymutil. If DWARF is not enabled, we don't run dsymutil. We can pass "-Wl,-S" to let the system linker not generate those symbols. While here, also make it more consistent about DWARF combining. Currently we only do DWARF combining on macOS/AMD64, when DWARF is enabled. On ARM64, we run dsymutil, but then throw the result away. This CL changes it to not run dsymutil (and strip) on ARM64. TODO: add a test. We don't do it here as it fails on some (non-darwin) platforms. Fixes #40979. Change-Id: If770f7828cdb858857d6079e0585bf067f8f7a92 Reviewed-on: https://go-review.googlesource.com/c/go/+/250944 Run-TryBot: Cherry Zhang <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]> Reviewed-by: Than McIntosh <[email protected]>
1 parent 565ad13 commit e8f9185

File tree

1 file changed

+19
-10
lines changed
  • src/cmd/link/internal/ld

1 file changed

+19
-10
lines changed

src/cmd/link/internal/ld/lib.go

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1238,6 +1238,10 @@ func (ctxt *Link) hostlink() {
12381238
}
12391239
}
12401240

1241+
// On darwin, whether to combine DWARF into executable.
1242+
// Only macOS supports unmapped segments such as our __DWARF segment.
1243+
combineDwarf := ctxt.IsDarwin() && !*FlagS && !*FlagW && !debug_s && machoPlatform == PLATFORM_MACOS && ctxt.IsAMD64()
1244+
12411245
switch ctxt.HeadType {
12421246
case objabi.Hdarwin:
12431247
if machoPlatform == PLATFORM_MACOS && ctxt.IsAMD64() {
@@ -1248,6 +1252,9 @@ func (ctxt *Link) hostlink() {
12481252
if ctxt.DynlinkingGo() && !ctxt.Arch.InFamily(sys.ARM, sys.ARM64) {
12491253
argv = append(argv, "-Wl,-flat_namespace")
12501254
}
1255+
if !combineDwarf {
1256+
argv = append(argv, "-Wl,-S") // suppress STAB (symbolic debugging) symbols
1257+
}
12511258
case objabi.Hopenbsd:
12521259
argv = append(argv, "-Wl,-nopie")
12531260
case objabi.Hwindows:
@@ -1587,11 +1594,16 @@ func (ctxt *Link) hostlink() {
15871594
ctxt.Logf("%s", out)
15881595
}
15891596

1590-
if !*FlagS && !*FlagW && !debug_s && ctxt.HeadType == objabi.Hdarwin {
1597+
if combineDwarf {
15911598
dsym := filepath.Join(*flagTmpdir, "go.dwarf")
15921599
if out, err := exec.Command("dsymutil", "-f", *flagOutfile, "-o", dsym).CombinedOutput(); err != nil {
15931600
Exitf("%s: running dsymutil failed: %v\n%s", os.Args[0], err, out)
15941601
}
1602+
// Remove STAB (symbolic debugging) symbols after we are done with them (by dsymutil).
1603+
// They contain temporary file paths and make the build not reproducible.
1604+
if out, err := exec.Command("strip", "-S", *flagOutfile).CombinedOutput(); err != nil {
1605+
Exitf("%s: running strip failed: %v\n%s", os.Args[0], err, out)
1606+
}
15951607
// Skip combining if `dsymutil` didn't generate a file. See #11994.
15961608
if _, err := os.Stat(dsym); os.IsNotExist(err) {
15971609
return
@@ -1607,15 +1619,12 @@ func (ctxt *Link) hostlink() {
16071619
if err != nil {
16081620
Exitf("%s: parsing Mach-O header failed: %v", os.Args[0], err)
16091621
}
1610-
// Only macOS supports unmapped segments such as our __DWARF segment.
1611-
if machoPlatform == PLATFORM_MACOS && ctxt.IsAMD64() {
1612-
if err := machoCombineDwarf(ctxt, exef, exem, dsym, combinedOutput); err != nil {
1613-
Exitf("%s: combining dwarf failed: %v", os.Args[0], err)
1614-
}
1615-
os.Remove(*flagOutfile)
1616-
if err := os.Rename(combinedOutput, *flagOutfile); err != nil {
1617-
Exitf("%s: %v", os.Args[0], err)
1618-
}
1622+
if err := machoCombineDwarf(ctxt, exef, exem, dsym, combinedOutput); err != nil {
1623+
Exitf("%s: combining dwarf failed: %v", os.Args[0], err)
1624+
}
1625+
os.Remove(*flagOutfile)
1626+
if err := os.Rename(combinedOutput, *flagOutfile); err != nil {
1627+
Exitf("%s: %v", os.Args[0], err)
16191628
}
16201629
}
16211630
}

0 commit comments

Comments
 (0)