Skip to content

Commit

Permalink
cmd/link: rationalize -s and -w flags with Mach-O external linking
Browse files Browse the repository at this point in the history
Currently, on Mach-O in external linking mode, the handling of -s
and -w flags are a bit mixed: neither flag disables the symbol
table, and both flags disable DWARF.

This CL makes it do what is documented: -s disables symbol table,
and -w disables DWARF. For the Darwin system linker, the -s flag
(strip symbol table) is obsolete. So we strip it afterwards. We
already use the strip command to strip the debug STAB symbols if
we need to combine DWARF. With this CL we'll use an additional
flag to strip more symbols. And we now also use strip if -s is
specified and we don't need to combine DWARF.

Change-Id: I9bed24fd388f2bd5b0ffa4ec2db46a4a2f6b1016
Reviewed-on: https://go-review.googlesource.com/c/go/+/493136
Reviewed-by: Than McIntosh <[email protected]>
Run-TryBot: Cherry Mui <[email protected]>
TryBot-Result: Gopher Robot <[email protected]>
  • Loading branch information
cherrymui committed Jul 21, 2023
1 parent 1d84c89 commit 4d21b3e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
20 changes: 17 additions & 3 deletions src/cmd/link/internal/ld/lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -1390,15 +1390,15 @@ func (ctxt *Link) hostlink() {
if ctxt.HeadType == objabi.Hdarwin {
// Recent versions of macOS print
// ld: warning: option -s is obsolete and being ignored
// so do not pass any arguments.
// so do not pass any arguments (but we strip symbols below).
} else {
argv = append(argv, "-s")
}
}

// On darwin, whether to combine DWARF into executable.
// Only macOS supports unmapped segments such as our __DWARF segment.
combineDwarf := ctxt.IsDarwin() && !*FlagS && !*FlagW && !debug_s && machoPlatform == PLATFORM_MACOS
combineDwarf := ctxt.IsDarwin() && !*FlagW && machoPlatform == PLATFORM_MACOS

switch ctxt.HeadType {
case objabi.Hdarwin:
Expand All @@ -1417,6 +1417,12 @@ func (ctxt *Link) hostlink() {
}
if !combineDwarf {
argv = append(argv, "-Wl,-S") // suppress STAB (symbolic debugging) symbols
if debug_s {
// We are generating a binary with symbol table suppressed.
// Suppress local symbols. We need to keep dynamically exported
// and referenced symbols so the dynamic linker can resolve them.
argv = append(argv, "-Wl,-x")
}
}
case objabi.Hopenbsd:
argv = append(argv, "-Wl,-nopie")
Expand Down Expand Up @@ -1929,7 +1935,15 @@ func (ctxt *Link) hostlink() {
}
// Remove STAB (symbolic debugging) symbols after we are done with them (by dsymutil).
// They contain temporary file paths and make the build not reproducible.
if out, err := exec.Command(stripCmd, "-S", *flagOutfile).CombinedOutput(); err != nil {
var stripArgs = []string{"-S"}
if debug_s {
// We are generating a binary with symbol table suppressed.
// Suppress local symbols. We need to keep dynamically exported
// and referenced symbols so the dynamic linker can resolve them.
stripArgs = append(stripArgs, "-x")
}
stripArgs = append(stripArgs, *flagOutfile)
if out, err := exec.Command(stripCmd, stripArgs...).CombinedOutput(); err != nil {
Exitf("%s: running strip failed: %v\n%s", os.Args[0], err, out)
}
// Skip combining if `dsymutil` didn't generate a file. See #11994.
Expand Down
2 changes: 1 addition & 1 deletion src/cmd/link/internal/ld/macho.go
Original file line number Diff line number Diff line change
Expand Up @@ -665,7 +665,7 @@ func machoshbits(ctxt *Link, mseg *MachoSeg, sect *sym.Section, segname string)

func asmbMacho(ctxt *Link) {
machlink := doMachoLink(ctxt)
if !*FlagS && ctxt.IsExternal() {
if ctxt.IsExternal() {
symo := int64(Segdwarf.Fileoff + uint64(Rnd(int64(Segdwarf.Filelen), int64(*FlagRound))) + uint64(machlink))
ctxt.Out.SeekSet(symo)
machoEmitReloc(ctxt)
Expand Down

0 comments on commit 4d21b3e

Please sign in to comment.