1
0
mirror of https://github.com/golang/go synced 2024-11-22 22:00:02 -07:00

cmd/link: rationalize -s and -w flags with Mach-O external linking

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 <thanm@google.com>
Run-TryBot: Cherry Mui <cherryyz@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
This commit is contained in:
Cherry Mui 2023-05-05 18:52:39 -04:00
parent 1d84c89bec
commit 4d21b3ec2d
2 changed files with 18 additions and 4 deletions

View File

@ -1390,7 +1390,7 @@ 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")
}
@ -1398,7 +1398,7 @@ func (ctxt *Link) hostlink() {
// 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:
@ -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")
@ -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.

View File

@ -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)