[dev.link] cmd/link: export loader's symbol bitmap

Export loader's bitmap. As more things converted to new style,
I think it may be not uncommon to have bitmaps for symbols used
outside of the loader.

Change-Id: Ida68dac87988957a29985143121d781296820fcf
Reviewed-on: https://go-review.googlesource.com/c/go/+/220980
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-02-25 15:15:11 -05:00
parent 416561c9e2
commit 23c52de5fc

View file

@ -83,40 +83,40 @@ type nameVer struct {
v int v int
} }
type bitmap []uint32 type Bitmap []uint32
// set the i-th bit. // set the i-th bit.
func (bm bitmap) set(i Sym) { func (bm Bitmap) Set(i Sym) {
n, r := uint(i)/32, uint(i)%32 n, r := uint(i)/32, uint(i)%32
bm[n] |= 1 << r bm[n] |= 1 << r
} }
// unset the i-th bit. // unset the i-th bit.
func (bm bitmap) unset(i Sym) { func (bm Bitmap) Unset(i Sym) {
n, r := uint(i)/32, uint(i)%32 n, r := uint(i)/32, uint(i)%32
bm[n] &^= (1 << r) bm[n] &^= (1 << r)
} }
// whether the i-th bit is set. // whether the i-th bit is set.
func (bm bitmap) has(i Sym) bool { func (bm Bitmap) Has(i Sym) bool {
n, r := uint(i)/32, uint(i)%32 n, r := uint(i)/32, uint(i)%32
return bm[n]&(1<<r) != 0 return bm[n]&(1<<r) != 0
} }
// return current length of bitmap in bits. // return current length of bitmap in bits.
func (bm bitmap) len() int { func (bm Bitmap) Len() int {
return len(bm) * 32 return len(bm) * 32
} }
func makeBitmap(n int) bitmap { func MakeBitmap(n int) Bitmap {
return make(bitmap, (n+31)/32) return make(Bitmap, (n+31)/32)
} }
// growBitmap insures that the specified bitmap has enough capacity, // growBitmap insures that the specified bitmap has enough capacity,
// reallocating (doubling the size) if needed. // reallocating (doubling the size) if needed.
func growBitmap(reqLen int, b bitmap) bitmap { func growBitmap(reqLen int, b Bitmap) Bitmap {
curLen := b.len() curLen := b.Len()
if reqLen > curLen { if reqLen > curLen {
b = append(b, makeBitmap(reqLen+1-curLen)...) b = append(b, MakeBitmap(reqLen+1-curLen)...)
} }
return b return b
} }
@ -178,14 +178,14 @@ type Loader struct {
// corresponding loader "AttrXXX" and "SetAttrXXX" methods. Please // corresponding loader "AttrXXX" and "SetAttrXXX" methods. Please
// visit the comments on these methods for more details on the // visit the comments on these methods for more details on the
// semantics / interpretation of the specific flags or attribute. // semantics / interpretation of the specific flags or attribute.
attrReachable bitmap // reachable symbols, indexed by global index attrReachable Bitmap // reachable symbols, indexed by global index
attrOnList bitmap // "on list" symbols, indexed by global index attrOnList Bitmap // "on list" symbols, indexed by global index
attrLocal bitmap // "local" symbols, indexed by global index attrLocal Bitmap // "local" symbols, indexed by global index
attrNotInSymbolTable bitmap // "not in symtab" symbols, indexed by glob idx attrNotInSymbolTable Bitmap // "not in symtab" symbols, indexed by glob idx
attrVisibilityHidden bitmap // hidden symbols, indexed by ext sym index attrVisibilityHidden Bitmap // hidden symbols, indexed by ext sym index
attrDuplicateOK bitmap // dupOK symbols, indexed by ext sym index attrDuplicateOK Bitmap // dupOK symbols, indexed by ext sym index
attrShared bitmap // shared symbols, indexed by ext sym index attrShared Bitmap // shared symbols, indexed by ext sym index
attrExternal bitmap // external symbols, indexed by ext sym index attrExternal Bitmap // external symbols, indexed by ext sym index
attrReadOnly map[Sym]bool // readonly data for this sym attrReadOnly map[Sym]bool // readonly data for this sym
attrTopFrame map[Sym]struct{} // top frame symbols attrTopFrame map[Sym]struct{} // top frame symbols
@ -654,16 +654,16 @@ func (l *Loader) SymAttr(i Sym) uint8 {
// referenced from the entry points. Unreachable symbols are not // referenced from the entry points. Unreachable symbols are not
// written to the output. // written to the output.
func (l *Loader) AttrReachable(i Sym) bool { func (l *Loader) AttrReachable(i Sym) bool {
return l.attrReachable.has(i) return l.attrReachable.Has(i)
} }
// SetAttrReachable sets the reachability property for a symbol (see // SetAttrReachable sets the reachability property for a symbol (see
// AttrReachable). // AttrReachable).
func (l *Loader) SetAttrReachable(i Sym, v bool) { func (l *Loader) SetAttrReachable(i Sym, v bool) {
if v { if v {
l.attrReachable.set(i) l.attrReachable.Set(i)
} else { } else {
l.attrReachable.unset(i) l.attrReachable.Unset(i)
} }
} }
@ -672,16 +672,16 @@ func (l *Loader) SetAttrReachable(i Sym, v bool) {
// and is consulted to avoid bugs where a symbol is put on a list // and is consulted to avoid bugs where a symbol is put on a list
// twice. // twice.
func (l *Loader) AttrOnList(i Sym) bool { func (l *Loader) AttrOnList(i Sym) bool {
return l.attrOnList.has(i) return l.attrOnList.Has(i)
} }
// SetAttrOnList sets the "on list" property for a symbol (see // SetAttrOnList sets the "on list" property for a symbol (see
// AttrOnList). // AttrOnList).
func (l *Loader) SetAttrOnList(i Sym, v bool) { func (l *Loader) SetAttrOnList(i Sym, v bool) {
if v { if v {
l.attrOnList.set(i) l.attrOnList.Set(i)
} else { } else {
l.attrOnList.unset(i) l.attrOnList.Unset(i)
} }
} }
@ -689,31 +689,31 @@ func (l *Loader) SetAttrOnList(i Sym, v bool) {
// module (executable or shared library) being linked. This attribute // module (executable or shared library) being linked. This attribute
// is applied to thunks and certain other linker-generated symbols. // is applied to thunks and certain other linker-generated symbols.
func (l *Loader) AttrLocal(i Sym) bool { func (l *Loader) AttrLocal(i Sym) bool {
return l.attrLocal.has(i) return l.attrLocal.Has(i)
} }
// SetAttrLocal the "local" property for a symbol (see AttrLocal above). // SetAttrLocal the "local" property for a symbol (see AttrLocal above).
func (l *Loader) SetAttrLocal(i Sym, v bool) { func (l *Loader) SetAttrLocal(i Sym, v bool) {
if v { if v {
l.attrLocal.set(i) l.attrLocal.Set(i)
} else { } else {
l.attrLocal.unset(i) l.attrLocal.Unset(i)
} }
} }
// AttrNotInSymbolTable returns true for symbols that should not be // AttrNotInSymbolTable returns true for symbols that should not be
// added to the symbol table of the final generated load module. // added to the symbol table of the final generated load module.
func (l *Loader) AttrNotInSymbolTable(i Sym) bool { func (l *Loader) AttrNotInSymbolTable(i Sym) bool {
return l.attrNotInSymbolTable.has(i) return l.attrNotInSymbolTable.Has(i)
} }
// SetAttrNotInSymbolTable the "not in symtab" property for a symbol // SetAttrNotInSymbolTable the "not in symtab" property for a symbol
// (see AttrNotInSymbolTable above). // (see AttrNotInSymbolTable above).
func (l *Loader) SetAttrNotInSymbolTable(i Sym, v bool) { func (l *Loader) SetAttrNotInSymbolTable(i Sym, v bool) {
if v { if v {
l.attrNotInSymbolTable.set(i) l.attrNotInSymbolTable.Set(i)
} else { } else {
l.attrNotInSymbolTable.unset(i) l.attrNotInSymbolTable.Unset(i)
} }
} }
@ -725,7 +725,7 @@ func (l *Loader) AttrVisibilityHidden(i Sym) bool {
if !l.IsExternal(i) { if !l.IsExternal(i) {
return false return false
} }
return l.attrVisibilityHidden.has(l.extIndex(i)) return l.attrVisibilityHidden.Has(l.extIndex(i))
} }
// SetAttrVisibilityHidden sets the "hidden visibility" property for a // SetAttrVisibilityHidden sets the "hidden visibility" property for a
@ -735,9 +735,9 @@ func (l *Loader) SetAttrVisibilityHidden(i Sym, v bool) {
panic("tried to set visibility attr on non-external symbol") panic("tried to set visibility attr on non-external symbol")
} }
if v { if v {
l.attrVisibilityHidden.set(l.extIndex(i)) l.attrVisibilityHidden.Set(l.extIndex(i))
} else { } else {
l.attrVisibilityHidden.unset(l.extIndex(i)) l.attrVisibilityHidden.Unset(l.extIndex(i))
} }
} }
@ -753,7 +753,7 @@ func (l *Loader) AttrDuplicateOK(i Sym) bool {
osym.ReadFlag(r.Reader, r.SymOff(li)) osym.ReadFlag(r.Reader, r.SymOff(li))
return osym.Dupok() return osym.Dupok()
} }
return l.attrDuplicateOK.has(l.extIndex(i)) return l.attrDuplicateOK.Has(l.extIndex(i))
} }
// SetAttrDuplicateOK sets the "duplicate OK" property for an external // SetAttrDuplicateOK sets the "duplicate OK" property for an external
@ -763,9 +763,9 @@ func (l *Loader) SetAttrDuplicateOK(i Sym, v bool) {
panic("tried to set dupok attr on non-external symbol") panic("tried to set dupok attr on non-external symbol")
} }
if v { if v {
l.attrDuplicateOK.set(l.extIndex(i)) l.attrDuplicateOK.Set(l.extIndex(i))
} else { } else {
l.attrDuplicateOK.unset(l.extIndex(i)) l.attrDuplicateOK.Unset(l.extIndex(i))
} }
} }
@ -778,7 +778,7 @@ func (l *Loader) AttrShared(i Sym) bool {
r, _ := l.toLocal(i) r, _ := l.toLocal(i)
return (r.Flags() & goobj2.ObjFlagShared) != 0 return (r.Flags() & goobj2.ObjFlagShared) != 0
} }
return l.attrShared.has(l.extIndex(i)) return l.attrShared.Has(l.extIndex(i))
} }
// SetAttrShared sets the "shared" property for an external // SetAttrShared sets the "shared" property for an external
@ -788,9 +788,9 @@ func (l *Loader) SetAttrShared(i Sym, v bool) {
panic("tried to set shared attr on non-external symbol") panic("tried to set shared attr on non-external symbol")
} }
if v { if v {
l.attrShared.set(l.extIndex(i)) l.attrShared.Set(l.extIndex(i))
} else { } else {
l.attrShared.unset(l.extIndex(i)) l.attrShared.Unset(l.extIndex(i))
} }
} }
@ -800,7 +800,7 @@ func (l *Loader) AttrExternal(i Sym) bool {
if !l.IsExternal(i) { if !l.IsExternal(i) {
return false return false
} }
return l.attrExternal.has(l.extIndex(i)) return l.attrExternal.Has(l.extIndex(i))
} }
// SetAttrExternal sets the "external" property for an host object // SetAttrExternal sets the "external" property for an host object
@ -810,9 +810,9 @@ func (l *Loader) SetAttrExternal(i Sym, v bool) {
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.RawSymName(i)))
} }
if v { if v {
l.attrExternal.set(l.extIndex(i)) l.attrExternal.Set(l.extIndex(i))
} else { } else {
l.attrExternal.unset(l.extIndex(i)) l.attrExternal.Unset(l.extIndex(i))
} }
} }
@ -1403,7 +1403,7 @@ func (l *Loader) SortSub(s Sym) Sym {
// Insure that reachable bitmap and its siblings have enough size. // Insure that reachable bitmap and its siblings have enough size.
func (l *Loader) growAttrBitmaps(reqLen int) { func (l *Loader) growAttrBitmaps(reqLen int) {
if reqLen > l.attrReachable.len() { if reqLen > l.attrReachable.Len() {
// These are indexed by global symbol // These are indexed by global symbol
l.attrReachable = growBitmap(reqLen, l.attrReachable) l.attrReachable = growBitmap(reqLen, l.attrReachable)
l.attrOnList = growBitmap(reqLen, l.attrOnList) l.attrOnList = growBitmap(reqLen, l.attrOnList)
@ -1416,7 +1416,7 @@ func (l *Loader) growAttrBitmaps(reqLen int) {
func (l *Loader) growExtAttrBitmaps() { func (l *Loader) growExtAttrBitmaps() {
// These are indexed by external symbol index (e.g. l.extIndex(i)) // These are indexed by external symbol index (e.g. l.extIndex(i))
extReqLen := len(l.payloads) extReqLen := len(l.payloads)
if extReqLen > l.attrVisibilityHidden.len() { if extReqLen > l.attrVisibilityHidden.Len() {
l.attrVisibilityHidden = growBitmap(extReqLen, l.attrVisibilityHidden) l.attrVisibilityHidden = growBitmap(extReqLen, l.attrVisibilityHidden)
l.attrDuplicateOK = growBitmap(extReqLen, l.attrDuplicateOK) l.attrDuplicateOK = growBitmap(extReqLen, l.attrDuplicateOK)
l.attrShared = growBitmap(extReqLen, l.attrShared) l.attrShared = growBitmap(extReqLen, l.attrShared)
@ -1719,7 +1719,7 @@ func (l *Loader) LoadFull(arch *sys.Arch, syms *sym.Symbols) {
toConvert := make([]Sym, 0, len(l.payloads)) toConvert := make([]Sym, 0, len(l.payloads))
for _, i := range l.extReader.syms { for _, i := range l.extReader.syms {
sname := l.RawSymName(i) sname := l.RawSymName(i)
if !l.attrReachable.has(i) && !strings.HasPrefix(sname, "gofile..") { // XXX file symbols are used but not marked if !l.attrReachable.Has(i) && !strings.HasPrefix(sname, "gofile..") { // XXX file symbols are used but not marked
continue continue
} }
pp := l.getPayload(i) pp := l.getPayload(i)
@ -1900,7 +1900,7 @@ func loadObjSyms(l *Loader, syms *sym.Symbols, r *oReader) int {
if t == 0 { if t == 0 {
log.Fatalf("missing type for %s in %s", name, r.unit.Lib) log.Fatalf("missing type for %s in %s", name, r.unit.Lib)
} }
if !l.attrReachable.has(gi) && !(t == sym.SRODATA && strings.HasPrefix(name, "type.")) && name != "runtime.addmoduledata" && name != "runtime.lastmoduledatap" { if !l.attrReachable.Has(gi) && !(t == sym.SRODATA && strings.HasPrefix(name, "type.")) && name != "runtime.addmoduledata" && name != "runtime.lastmoduledatap" {
// No need to load unreachable symbols. // No need to load unreachable symbols.
// XXX some type symbol's content may be needed in DWARF code, but they are not marked. // XXX some type symbol's content may be needed in DWARF code, but they are not marked.
// XXX reference to runtime.addmoduledata may be generated later by the linker in plugin mode. // XXX reference to runtime.addmoduledata may be generated later by the linker in plugin mode.
@ -2105,7 +2105,7 @@ func loadObjFull(l *Loader, r *oReader) {
osym.ReadWithoutName(r.Reader, r.SymOff(i)) osym.ReadWithoutName(r.Reader, r.SymOff(i))
dupok := osym.Dupok() dupok := osym.Dupok()
if dupok && isdup { if dupok && isdup {
if l.attrReachable.has(gi) { if l.attrReachable.Has(gi) {
// A dupok symbol is resolved to another package. We still need // A dupok symbol is resolved to another package. We still need
// to record its presence in the current package, as the trampoline // to record its presence in the current package, as the trampoline
// pass expects packages are laid out in dependency order. // pass expects packages are laid out in dependency order.
@ -2323,14 +2323,14 @@ func (l *Loader) convertRelocations(src []Reloc, dst *sym.Symbol) {
sz := r.Size sz := r.Size
rt := r.Type rt := r.Type
if rt == objabi.R_METHODOFF { if rt == objabi.R_METHODOFF {
if l.attrReachable.has(rs) { if l.attrReachable.Has(rs) {
rt = objabi.R_ADDROFF rt = objabi.R_ADDROFF
} else { } else {
sz = 0 sz = 0
rs = 0 rs = 0
} }
} }
if rt == objabi.R_WEAKADDROFF && !l.attrReachable.has(rs) { if rt == objabi.R_WEAKADDROFF && !l.attrReachable.Has(rs) {
rs = 0 rs = 0
sz = 0 sz = 0
} }
@ -2432,7 +2432,7 @@ func (l *Loader) AssignTextSymbolOrder(libs []*sym.Library, intlibs []bool) {
// clear for the later assignment of the sym.Symbol to a unit. // clear for the later assignment of the sym.Symbol to a unit.
// NB: we can convert to using onList once we no longer have to // NB: we can convert to using onList once we no longer have to
// call the regular addToTextp. // call the regular addToTextp.
assignedToUnit := makeBitmap(l.NSym() + 1) assignedToUnit := MakeBitmap(l.NSym() + 1)
// Walk through all text symbols from Go object files and append // Walk through all text symbols from Go object files and append
// them to their corresponding library's textp2 list. // them to their corresponding library's textp2 list.
@ -2449,7 +2449,7 @@ func (l *Loader) AssignTextSymbolOrder(libs []*sym.Library, intlibs []bool) {
} }
// check for dupok // check for dupok
if r2, i2 := l.toLocal(gi); r2 != r || i2 != i { if r2, i2 := l.toLocal(gi); r2 != r || i2 != i {
if l.attrReachable.has(gi) { if l.attrReachable.Has(gi) {
// A dupok symbol is resolved to another package. // A dupok symbol is resolved to another package.
// We still need to record its presence in the // We still need to record its presence in the
// current package, as the trampoline pass expects // current package, as the trampoline pass expects
@ -2474,12 +2474,12 @@ func (l *Loader) AssignTextSymbolOrder(libs []*sym.Library, intlibs []bool) {
for _, textp2 := range tpls { for _, textp2 := range tpls {
for _, s := range textp2 { for _, s := range textp2 {
sym := Sym(s) sym := Sym(s)
if l.attrReachable.has(sym) && !assignedToUnit.has(sym) { if l.attrReachable.Has(sym) && !assignedToUnit.Has(sym) {
libtextp2 = append(libtextp2, s) libtextp2 = append(libtextp2, s)
unit := l.SymUnit(sym) unit := l.SymUnit(sym)
if unit != nil { if unit != nil {
unit.Textp2 = append(unit.Textp2, s) unit.Textp2 = append(unit.Textp2, s)
assignedToUnit.set(sym) assignedToUnit.Set(sym)
} }
} }
} }