[dev.link] cmd/link: convert mangleTypeSym to new style

Use symbol's Extname, instead of symbol renaming, for the mangled
names.

The old symbol Rename has an interesting logic of "merging"
symbols, when a symbol is renamed to the name of an existing
symbol. It turns out that this is needed for linking against
shared libraries, where the Go object has a reference to a symbol
with the original name, and the shared libary provides a symbol
under the mangled name. Implement this logic with the loader.

Change-Id: Ib95d7a9c93a52f8e02f4a51ac67240d6ebfc1c6a
Reviewed-on: https://go-review.googlesource.com/c/go/+/224939
Run-TryBot: Cherry Zhang <cherryyz@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Than McIntosh <thanm@google.com>
This commit is contained in:
Cherry Zhang 2020-03-21 13:50:46 -04:00
parent b328ab1d1e
commit ed7a491940
4 changed files with 51 additions and 7 deletions

View file

@ -794,10 +794,29 @@ func (ctxt *Link) mangleTypeSym() {
return
}
for _, s := range ctxt.Syms.Allsym {
newName := typeSymbolMangle(s.Name)
if newName != s.Name {
ctxt.Syms.Rename(s.Name, newName, int(s.Version))
ldr := ctxt.loader
for s := loader.Sym(1); s < loader.Sym(ldr.NSym()); s++ {
if !ldr.AttrReachable(s) {
continue
}
name := ldr.SymName(s)
newName := typeSymbolMangle(name)
if newName != name {
ldr.SetSymExtname(s, newName)
// When linking against a shared library, the Go object file may
// have reference to the original symbol name whereas the shared
// library provides a symbol with the mangled name. We need to
// copy the payload of mangled to original.
// XXX maybe there is a better way to do this.
dup := ldr.Lookup(newName, ldr.SymVersion(s))
if dup != 0 {
st := ldr.SymType(s)
dt := ldr.SymType(dup)
if st == sym.Sxxx && dt != sym.Sxxx {
ldr.CopySym(dup, s)
}
}
}
}
}