From: Cherry Mui Date: Fri, 5 May 2023 21:04:45 +0000 (-0400) Subject: cmd/link: suppress symbol table on Mach-O when -s X-Git-Tag: go1.22rc1~1553 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=1d84c89bec2ad5a4a186afe40ef1910e33376a43;p=gostls13.git cmd/link: suppress symbol table on Mach-O when -s Currently, on Mach-O, we don't strip the symbol table even the -s flag is set. This CL makes it suppress the symbol table, as documented. On Mach-O, even with -s, we still need to keep symbols that are dynamically exported or referenced symbol. Otherwise the dynamic linker cannot resolve them and the binary doesn't run. (Interestingly, for a PIE binary it is okay to strip the symbol table entirely. We keep the dynamic symbols for consistency. And this is also in consistent with what the system "strip" command does.) Change-Id: I39c572553fe0215ae3bdf5349bf2bab7205fbdc9 Reviewed-on: https://go-review.googlesource.com/c/go/+/492744 Reviewed-by: Than McIntosh Run-TryBot: Cherry Mui TryBot-Result: Gopher Robot --- diff --git a/src/cmd/link/internal/ld/macho.go b/src/cmd/link/internal/ld/macho.go index e43a1aa002..81ebfb6c7a 100644 --- a/src/cmd/link/internal/ld/macho.go +++ b/src/cmd/link/internal/ld/macho.go @@ -894,30 +894,39 @@ func collectmachosyms(ctxt *Link) { nkind[symkind(ldr, s)]++ } - // Add special runtime.text and runtime.etext symbols. + // On Mach-O, even with -s, we still need to keep dynamically exported and + // referenced symbols. We can strip defined local text and data symbols. + // So *FlagS is applied based on symbol type. + + // Add special runtime.text and runtime.etext symbols (which are local). // We've already included this symbol in Textp on darwin if ctxt.DynlinkingGo(). // See data.go:/textaddress - if !ctxt.DynlinkingGo() { - s := ldr.Lookup("runtime.text", 0) - if ldr.SymType(s) == sym.STEXT { - addsym(s) - } - for n := range Segtext.Sections[1:] { - s := ldr.Lookup(fmt.Sprintf("runtime.text.%d", n+1), 0) - if s != 0 { + if !*FlagS { + if !ctxt.DynlinkingGo() { + s := ldr.Lookup("runtime.text", 0) + if ldr.SymType(s) == sym.STEXT { + addsym(s) + } + for n := range Segtext.Sections[1:] { + s := ldr.Lookup(fmt.Sprintf("runtime.text.%d", n+1), 0) + if s != 0 { + addsym(s) + } else { + break + } + } + s = ldr.Lookup("runtime.etext", 0) + if ldr.SymType(s) == sym.STEXT { addsym(s) - } else { - break } - } - s = ldr.Lookup("runtime.etext", 0) - if ldr.SymType(s) == sym.STEXT { - addsym(s) } } // Add text symbols. for _, s := range ctxt.Textp { + if *FlagS && !ldr.AttrCgoExportDynamic(s) { + continue + } addsym(s) } @@ -946,11 +955,16 @@ func collectmachosyms(ctxt *Link) { if !shouldBeInSymbolTable(s) { continue } + if *FlagS && !ldr.AttrCgoExportDynamic(s) { + continue + } addsym(s) + continue } switch t { case sym.SDYNIMPORT, sym.SHOSTOBJ, sym.SUNDEFEXT: + // Keep dynamic symbol references even if *FlagS. addsym(s) }