cmd/link: remove name expansion logic

Now both the compiler and the assembler require the -p flag and
emit full package path in symbol names, we no longer need to do
the name expansion in the linker. Delete it.

Change-Id: I771d4d97987a0a17414881b52806d600ef4cc351
Reviewed-on: https://go-review.googlesource.com/c/go/+/404300
Reviewed-by: Than McIntosh <thanm@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Run-TryBot: Cherry Mui <cherryyz@google.com>
This commit is contained in:
Cherry Mui 2022-05-06 13:52:23 -04:00
parent c4c9c80e4f
commit e3f7816f0e
8 changed files with 14 additions and 50 deletions

View file

@ -873,6 +873,5 @@ func (r *Reader) Flags() uint32 {
}
func (r *Reader) Shared() bool { return r.Flags()&ObjFlagShared != 0 }
func (r *Reader) NeedNameExpansion() bool { return false } // TODO: delete
func (r *Reader) FromAssembly() bool { return r.Flags()&ObjFlagFromAssembly != 0 }
func (r *Reader) Unlinkable() bool { return r.Flags()&ObjFlagUnlinkable != 0 }

View file

@ -26,11 +26,6 @@ import (
// go-specific code shared across loaders (5l, 6l, 8l).
// replace all "". with pkg.
func expandpkg(t0 string, pkg string) string {
return strings.Replace(t0, `"".`, pkg+".", -1)
}
// TODO:
// generate debugging section in binary.
// once the dust settles, try to move some code to
@ -146,7 +141,6 @@ func setCgoAttr(ctxt *Link, file string, pkg string, directives [][]string, host
continue
}
local = expandpkg(local, pkg)
q := ""
if i := strings.Index(remote, "#"); i >= 0 {
remote, q = remote[:i], remote[i+1:]
@ -193,7 +187,6 @@ func setCgoAttr(ctxt *Link, file string, pkg string, directives [][]string, host
if len(f) > 2 {
remote = f[2]
}
local = expandpkg(local, pkg)
// The compiler adds a fourth argument giving
// the definition ABI of function symbols.
abi := obj.ABI0

View file

@ -920,7 +920,7 @@ func collectmachosyms(ctxt *Link) {
if ldr.AttrNotInSymbolTable(s) {
return false
}
name := ldr.RawSymName(s) // TODO: try not to read the name
name := ldr.SymName(s) // TODO: try not to read the name
if name == "" || name[0] == '.' {
return false
}
@ -1019,7 +1019,7 @@ func machoShouldExport(ctxt *Link, ldr *loader.Loader, s loader.Sym) bool {
if ctxt.BuildMode == BuildModePlugin && strings.HasPrefix(ldr.SymExtname(s), objabi.PathToPrefix(*flagPluginPath)) {
return true
}
name := ldr.RawSymName(s)
name := ldr.SymName(s)
if strings.HasPrefix(name, "go.itab.") {
return true
}

View file

@ -780,7 +780,7 @@ func (f *peFile) writeSymbols(ctxt *Link) {
if ldr.AttrNotInSymbolTable(s) {
return false
}
name := ldr.RawSymName(s) // TODO: try not to read the name
name := ldr.SymName(s) // TODO: try not to read the name
if name == "" || name[0] == '.' {
return false
}

View file

@ -329,7 +329,7 @@ func asmbPlan9Sym(ctxt *Link) {
if ldr.AttrNotInSymbolTable(s) {
return false
}
name := ldr.RawSymName(s) // TODO: try not to read the name
name := ldr.SymName(s) // TODO: try not to read the name
if name == "" || name[0] == '.' {
return false
}

View file

@ -358,9 +358,6 @@ func (l *Loader) addObj(pkg string, r *oReader) Sym {
i := Sym(len(l.objSyms))
l.start[r] = i
l.objs = append(l.objs, objIdx{r, i})
if r.NeedNameExpansion() && !r.FromAssembly() {
panic("object compiled without -p")
}
return i
}
@ -749,17 +746,7 @@ func (l *Loader) NReachableSym() int {
return l.attrReachable.Count()
}
// Returns the raw (unpatched) name of the i-th symbol.
func (l *Loader) RawSymName(i Sym) string {
if l.IsExternal(i) {
pp := l.getPayload(i)
return pp.name
}
r, li := l.toLocal(i)
return r.Sym(li).Name(r.Reader)
}
// Returns the (patched) name of the i-th symbol.
// Returns the name of the i-th symbol.
func (l *Loader) SymName(i Sym) string {
if l.IsExternal(i) {
pp := l.getPayload(i)
@ -769,11 +756,7 @@ func (l *Loader) SymName(i Sym) string {
if r == nil {
return "?"
}
name := r.Sym(li).Name(r.Reader)
if !r.NeedNameExpansion() {
return name
}
return strings.Replace(name, "\"\".", r.pkgprefix, -1)
return r.Sym(li).Name(r.Reader)
}
// Returns the version of the i-th symbol.
@ -1012,7 +995,7 @@ func (l *Loader) AttrExternal(i Sym) bool {
// symbol (see AttrExternal).
func (l *Loader) SetAttrExternal(i Sym, v bool) {
if !l.IsExternal(i) {
panic(fmt.Sprintf("tried to set external attr on non-external symbol %q", l.RawSymName(i)))
panic(fmt.Sprintf("tried to set external attr on non-external symbol %q", l.SymName(i)))
}
if v {
l.attrExternal.Set(l.extIndex(i))
@ -2131,7 +2114,6 @@ func (st *loadState) preloadSyms(r *oReader, kind int) {
panic("preloadSyms: bad kind")
}
l.growAttrBitmaps(len(l.objSyms) + int(end-start))
needNameExpansion := r.NeedNameExpansion()
loadingRuntimePkg := r.unit.Lib.Pkg == "runtime"
for i := start; i < end; i++ {
osym := r.Sym(i)
@ -2139,9 +2121,6 @@ func (st *loadState) preloadSyms(r *oReader, kind int) {
var v int
if kind != hashed64Def && kind != hashedDef { // we don't need the name, etc. for hashed symbols
name = osym.Name(r.Reader)
if needNameExpansion {
name = strings.Replace(name, "\"\".", r.pkgprefix, -1)
}
v = abiToVer(osym.ABI(), r.version)
}
gi := st.addSym(name, v, r, i, kind, osym)
@ -2205,13 +2184,9 @@ func (l *Loader) LoadSyms(arch *sys.Arch) {
func loadObjRefs(l *Loader, r *oReader, arch *sys.Arch) {
// load non-package refs
ndef := uint32(r.NAlldef())
needNameExpansion := r.NeedNameExpansion()
for i, n := uint32(0), uint32(r.NNonpkgref()); i < n; i++ {
osym := r.Sym(ndef + i)
name := osym.Name(r.Reader)
if needNameExpansion {
name = strings.Replace(name, "\"\".", r.pkgprefix, -1)
}
v := abiToVer(osym.ABI(), r.version)
r.syms[ndef+i] = l.LookupOrCreateSym(name, v)
gi := r.syms[ndef+i]
@ -2264,7 +2239,7 @@ func abiToVer(abi uint16, localSymVersion int) int {
// anonymous aux or sub-symbol containing some sub-part or payload of
// another symbol.
func (l *Loader) TopLevelSym(s Sym) bool {
return topLevelSym(l.RawSymName(s), l.SymType(s))
return topLevelSym(l.SymName(s), l.SymType(s))
}
// topLevelSym tests a symbol name and kind to determine whether
@ -2299,9 +2274,6 @@ func (l *Loader) cloneToExternal(symIdx Sym) {
r, li := l.toLocal(symIdx)
osym := r.Sym(li)
sname := osym.Name(r.Reader)
if r.NeedNameExpansion() {
sname = strings.Replace(sname, "\"\".", r.pkgprefix, -1)
}
sver := abiToVer(osym.ABI(), r.version)
skind := sym.AbiSymKindToSymKind[objabi.SymKind(osym.Type())]
@ -2432,7 +2404,7 @@ func (l *Loader) UndefinedRelocTargets(limit int) []Sym {
for ri := 0; ri < relocs.Count(); ri++ {
r := relocs.At(ri)
rs := r.Sym()
if rs != 0 && l.SymType(rs) == sym.SXREF && l.RawSymName(rs) != ".got" {
if rs != 0 && l.SymType(rs) == sym.SXREF && l.SymName(rs) != ".got" {
result = append(result, rs)
if limit != -1 && len(result) >= limit {
break

View file

@ -198,7 +198,7 @@ func TestAddMaterializedSymbol(t *testing.T) {
}
// Nameless symbol should still be nameless.
es3name := ldr.RawSymName(es3)
es3name := ldr.SymName(es3)
if "" != es3name {
t.Errorf("expected es3 name of '', got '%s'", es3name)
}

View file

@ -686,7 +686,7 @@ func Load(l *loader.Loader, arch *sys.Arch, localSymVersion int, f *bio.Reader,
textp = append(textp, s)
for s1 := bld.Sub(); s1 != 0; s1 = l.SubSym(s1) {
if l.AttrOnList(s1) {
return errorf("symbol %s listed multiple times", l.RawSymName(s1))
return errorf("symbol %s listed multiple times", l.SymName(s1))
}
l.SetAttrOnList(s1, true)
textp = append(textp, s1)