]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/link: suppress symbol table on Mach-O when -s
authorCherry Mui <cherryyz@google.com>
Fri, 5 May 2023 21:04:45 +0000 (17:04 -0400)
committerCherry Mui <cherryyz@google.com>
Fri, 21 Jul 2023 15:42:40 +0000 (15:42 +0000)
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 <thanm@google.com>
Run-TryBot: Cherry Mui <cherryyz@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>

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

index e43a1aa002eaff29be4e33472d35fe1113a1a0b5..81ebfb6c7ac8b5cd05de1aacd35cc0f76e790e6f 100644 (file)
@@ -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)
                }