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

Change-Id: If861409a5cc4e398496199a89498a141f106f44f
Reviewed-on: https://go-review.googlesource.com/c/go/+/227762
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-04-08 19:46:00 -04:00
parent fd7666881b
commit a35b322709
4 changed files with 39 additions and 18 deletions

View file

@ -118,3 +118,14 @@ func decodetypeStructFieldOffsAnon2(ldr *loader.Loader, arch *sys.Arch, symIdx l
data := ldr.Data(symIdx) data := ldr.Data(symIdx)
return int64(decodeInuxi(arch, data[off+2*arch.PtrSize:], arch.PtrSize)) return int64(decodeInuxi(arch, data[off+2*arch.PtrSize:], arch.PtrSize))
} }
// decodetypeStr2 returns the contents of an rtype's str field (a nameOff).
func decodetypeStr2(ldr *loader.Loader, arch *sys.Arch, symIdx loader.Sym) string {
relocs := ldr.Relocs(symIdx)
str := decodetypeName2(ldr, symIdx, &relocs, 4*arch.PtrSize+8)
data := ldr.Data(symIdx)
if data[2*arch.PtrSize+4]&tflagExtraStar != 0 {
return str[1:]
}
return str
}

View file

@ -290,14 +290,14 @@ func Main(arch *sys.Arch, theArch Arch) {
bench.Start("textaddress") bench.Start("textaddress")
ctxt.textaddress() ctxt.textaddress()
bench.Start("typelink")
ctxt.typelink()
bench.Start("loadlibfull") bench.Start("loadlibfull")
ctxt.loadlibfull() // XXX do it here for now ctxt.loadlibfull() // XXX do it here for now
bench.Start("pclntab") bench.Start("pclntab")
ctxt.pclntab() ctxt.pclntab()
bench.Start("findfunctab") bench.Start("findfunctab")
ctxt.findfunctab() ctxt.findfunctab()
bench.Start("typelink")
ctxt.typelink()
bench.Start("symtab") bench.Start("symtab")
ctxt.symtab() ctxt.symtab()
bench.Start("buildinfo") bench.Start("buildinfo")

View file

@ -6,6 +6,7 @@ package ld
import ( import (
"cmd/internal/objabi" "cmd/internal/objabi"
"cmd/link/internal/loader"
"cmd/link/internal/sym" "cmd/link/internal/sym"
"sort" "sort"
) )
@ -14,7 +15,7 @@ type byTypeStr []typelinkSortKey
type typelinkSortKey struct { type typelinkSortKey struct {
TypeStr string TypeStr string
Type *sym.Symbol Type loader.Sym
} }
func (s byTypeStr) Less(i, j int) bool { return s[i].TypeStr < s[j].TypeStr } func (s byTypeStr) Less(i, j int) bool { return s[i].TypeStr < s[j].TypeStr }
@ -25,25 +26,27 @@ func (s byTypeStr) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
// Types that should be added to the typelinks table are marked with the // Types that should be added to the typelinks table are marked with the
// MakeTypelink attribute by the compiler. // MakeTypelink attribute by the compiler.
func (ctxt *Link) typelink() { func (ctxt *Link) typelink() {
ldr := ctxt.loader
typelinks := byTypeStr{} typelinks := byTypeStr{}
for _, s := range ctxt.Syms.Allsym { for s := loader.Sym(1); s < loader.Sym(ldr.NSym()); s++ {
if s.Attr.Reachable() && s.Attr.MakeTypelink() { if ldr.AttrReachable(s) && ldr.IsTypelink(s) {
typelinks = append(typelinks, typelinkSortKey{decodetypeStr(ctxt.Arch, s), s}) typelinks = append(typelinks, typelinkSortKey{decodetypeStr2(ldr, ctxt.Arch, s), s})
} }
} }
sort.Sort(typelinks) sort.Sort(typelinks)
tl := ctxt.Syms.Lookup("runtime.typelink", 0) tl := ldr.CreateSymForUpdate("runtime.typelink", 0)
tl.Type = sym.STYPELINK tl.SetType(sym.STYPELINK)
tl.Attr |= sym.AttrReachable | sym.AttrLocal ldr.SetAttrReachable(tl.Sym(), true)
tl.Size = int64(4 * len(typelinks)) ldr.SetAttrLocal(tl.Sym(), true)
tl.P = make([]byte, tl.Size) tl.SetSize(int64(4 * len(typelinks)))
tl.R = make([]sym.Reloc, len(typelinks)) tl.Grow(tl.Size())
relocs := tl.AddRelocs(len(typelinks))
for i, s := range typelinks { for i, s := range typelinks {
r := &tl.R[i] r := relocs.At2(i)
r.Sym = s.Type r.SetSym(s.Type)
r.Off = int32(i * 4) r.SetOff(int32(i * 4))
r.Siz = 4 r.SetSiz(4)
r.Type = objabi.R_ADDROFF r.SetType(objabi.R_ADDROFF)
} }
} }

View file

@ -659,7 +659,9 @@ func (l *Loader) SymType(i Sym) sym.SymKind {
// Returns the attributes of the i-th symbol. // Returns the attributes of the i-th symbol.
func (l *Loader) SymAttr(i Sym) uint8 { func (l *Loader) SymAttr(i Sym) uint8 {
if l.IsExternal(i) { if l.IsExternal(i) {
// TODO: do something? External symbols have different representation of attributes. For now, ReflectMethod is the only thing matters and it cannot be set by external symbol. // TODO: do something? External symbols have different representation of attributes.
// For now, ReflectMethod, NoSplit, GoType, and Typelink are used and they cannot be
// set by external symbol.
return 0 return 0
} }
r, li := l.toLocal(i) r, li := l.toLocal(i)
@ -982,6 +984,11 @@ func (l *Loader) IsGoType(i Sym) bool {
return l.SymAttr(i)&goobj2.SymFlagGoType != 0 return l.SymAttr(i)&goobj2.SymFlagGoType != 0
} }
// Returns whether this symbol should be included in typelink.
func (l *Loader) IsTypelink(i Sym) bool {
return l.SymAttr(i)&goobj2.SymFlagTypelink != 0
}
// Returns whether this is a "go.itablink.*" symbol. // Returns whether this is a "go.itablink.*" symbol.
func (l *Loader) IsItabLink(i Sym) bool { func (l *Loader) IsItabLink(i Sym) bool {
if _, ok := l.itablink[i]; ok { if _, ok := l.itablink[i]; ok {