Skip to content

Commit

Permalink
cmd/link: suppress symbol table on Mach-O when -s
Browse files Browse the repository at this point in the history
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 <[email protected]>
Run-TryBot: Cherry Mui <[email protected]>
TryBot-Result: Gopher Robot <[email protected]>
  • Loading branch information
cherrymui committed Jul 21, 2023
1 parent 3437ff0 commit 1d84c89
Showing 1 changed file with 29 additions and 15 deletions.
44 changes: 29 additions & 15 deletions src/cmd/link/internal/ld/macho.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down Expand Up @@ -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)
}

Expand Down

0 comments on commit 1d84c89

Please sign in to comment.