From 4d21b3ec2d63163962feb0cfaceaa33c6b98e6a5 Mon Sep 17 00:00:00 2001 From: Cherry Mui Date: Fri, 5 May 2023 18:52:39 -0400 Subject: [PATCH] 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 Run-TryBot: Cherry Mui TryBot-Result: Gopher Robot --- src/cmd/link/internal/ld/lib.go | 20 +++++++++++++++++--- src/cmd/link/internal/ld/macho.go | 2 +- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/src/cmd/link/internal/ld/lib.go b/src/cmd/link/internal/ld/lib.go index 6c03072160..595e656e5b 100644 --- a/src/cmd/link/internal/ld/lib.go +++ b/src/cmd/link/internal/ld/lib.go @@ -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. diff --git a/src/cmd/link/internal/ld/macho.go b/src/cmd/link/internal/ld/macho.go index 81ebfb6c7a..52ff85ddef 100644 --- a/src/cmd/link/internal/ld/macho.go +++ b/src/cmd/link/internal/ld/macho.go @@ -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) -- 2.50.0