From ea93ddfaeb2a797717ae6230d1166d842bc44655 Mon Sep 17 00:00:00 2001 From: Cherry Zhang Date: Sat, 21 Mar 2020 16:06:57 -0400 Subject: [PATCH] [dev.link] cmd/link: use Extname in doxcoff On AIX, when external linking, we need to change the function names to start with a dot and make function descriptors with the names without the dot. Currently this is done through symbol renaming, which is not friendly for switching to the loader. In this CL we use symbol's external name for this. This allows us to get rid of symbol renaming. Change-Id: If72602d17e96f0339fdac2e2321f1edfb292b5f9 Reviewed-on: https://go-review.googlesource.com/c/go/+/224940 Run-TryBot: Cherry Zhang TryBot-Result: Gobot Gobot Reviewed-by: Than McIntosh --- src/cmd/link/internal/ld/xcoff.go | 22 +++++++++++----- src/cmd/link/internal/loader/loader.go | 35 +++++--------------------- src/cmd/link/internal/sym/symbols.go | 6 +++-- 3 files changed, 26 insertions(+), 37 deletions(-) diff --git a/src/cmd/link/internal/ld/xcoff.go b/src/cmd/link/internal/ld/xcoff.go index 74889b3833..5d36622bad 100644 --- a/src/cmd/link/internal/ld/xcoff.go +++ b/src/cmd/link/internal/ld/xcoff.go @@ -1186,9 +1186,9 @@ func (ctxt *Link) doxcoff() { if ctxt.LinkMode == LinkExternal { // Change rt0_go name to match name in runtime/cgo:main(). rt0 := ctxt.Syms.ROLookup("runtime.rt0_go", 0) - ctxt.Syms.Rename(rt0.Name, "runtime_rt0_go", 0) + rt0.SetExtname("runtime_rt0_go") - for _, s := range ctxt.Syms.Allsym { + for _, s := range ctxt.Textp { if !s.Attr.CgoExport() { continue } @@ -1198,9 +1198,19 @@ func (ctxt *Link) doxcoff() { // On AIX, a exported function must have two symbols: // - a .text symbol which must start with a ".". // - a .data symbol which is a function descriptor. - ctxt.Syms.Rename(s.Name, "."+name, 0) - - desc := ctxt.Syms.Lookup(name, 0) + // + // XXX the old code was quite confusing -- it always + // rename a version 0 symbol, even if s.Version is not + // 0, but the descriptor still points to s. + // And in xcoffCreateExportFile, it seems to expect a + // name before the renaming. + // I guess this happens to work as the ABIALIAS symbol + // and the TEXT symbol have the same address. + // (Do the same here for now, but using Extname.) + s0 := ctxt.Syms.ROLookup(s.Name, 0) + s0.SetExtname("." + name) + + desc := ctxt.Syms.Newsym(name, 0) desc.Type = sym.SNOPTRDATA desc.AddAddr(ctxt.Arch, s) desc.AddAddr(ctxt.Arch, toc) @@ -1662,7 +1672,7 @@ func xcoffCreateExportFile(ctxt *Link) (fname string) { if !s.Attr.CgoExport() { continue } - if !strings.HasPrefix(s.String(), "_cgoexp_") { + if !strings.HasPrefix(s.Extname(), "_cgoexp_") { continue } diff --git a/src/cmd/link/internal/loader/loader.go b/src/cmd/link/internal/loader/loader.go index b06e92214e..2c180af0a4 100644 --- a/src/cmd/link/internal/loader/loader.go +++ b/src/cmd/link/internal/loader/loader.go @@ -2060,35 +2060,12 @@ func (l *Loader) ExtractSymbols(syms *sym.Symbols, rp map[*sym.Symbol]*sym.Symbo i := l.Lookup(name, ver) return l.Syms[i] } - syms.Rename = func(old, new string, ver int) { - // annoying... maybe there is a better way to do this - if ver >= 2 { - panic("cannot rename static symbol") - } - i := l.Lookup(old, ver) - s := l.Syms[i] - s.Name = new - if s.Extname() == old { - s.SetExtname(new) - } - delete(l.symsByName[ver], old) - - // This mirrors the old code. But I'm not sure if the logic of - // handling dup in the old code actually works, or necessary. - dupi := l.symsByName[ver][new] - dup := l.Syms[dupi] - if dup == nil { - l.symsByName[ver][new] = i - } else { - if s.Type == 0 { - dup.Attr |= s.Attr - *s = *dup - } else if dup.Type == 0 { - s.Attr |= dup.Attr - *dup = *s - l.symsByName[ver][new] = i - } - } + syms.Newsym = func(name string, ver int) *sym.Symbol { + i := l.newExtSym(name, ver) + s := l.allocSym(name, ver) + l.installSym(i, s) + syms.Allsym = append(syms.Allsym, s) // XXX see above + return s } } diff --git a/src/cmd/link/internal/sym/symbols.go b/src/cmd/link/internal/sym/symbols.go index 10e4ac5f54..d36be11ee8 100644 --- a/src/cmd/link/internal/sym/symbols.go +++ b/src/cmd/link/internal/sym/symbols.go @@ -46,8 +46,10 @@ type Symbols struct { // if it is not found. ROLookup func(name string, v int) *Symbol - // Rename renames a symbol. - Rename func(old, new string, v int) + // Create a symbol with the given name and version. The new symbol + // is not added to the lookup table and is not dedup'd with existing + // symbols (if any). + Newsym func(name string, v int) *Symbol } func NewSymbols() *Symbols { -- 2.48.1