cmd/link: remove objIdx structure

This indirection appears to be unnecessary for linking or linker debugging, and therefore hinders readability.

Since all *oReaders are added to loader.objs *only* via the Preload -> addObj path, before any symbols are examined, there is no possible way the "i" member of this structure is still useful; and is likely a remnant of an earlier design.

Change-Id: Icd880f40bf3299bf1aa0a14cf217268e49ee90c5
GitHub-Last-Rev: dd2d512cbe
GitHub-Pull-Request: golang/go#57460
Reviewed-on: https://go-review.googlesource.com/c/go/+/459456
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: qiulaidongfeng <2645477756@qq.com>
Reviewed-by: Than McIntosh <thanm@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Cherry Mui <cherryyz@google.com>
Auto-Submit: Ian Lance Taylor <iant@golang.org>
This commit is contained in:
Jeremy Quirke 2023-12-02 10:03:13 +00:00 committed by Gopher Robot
parent 83a6c13e73
commit c2c4a32f9e

View file

@ -91,11 +91,6 @@ type oReader struct {
// non-package symbols). // non-package symbols).
func (r *oReader) NAlldef() int { return r.ndef + r.nhashed64def + r.nhasheddef + r.NNonpkgdef() } func (r *oReader) NAlldef() int { return r.ndef + r.nhashed64def + r.nhasheddef + r.NNonpkgdef() }
type objIdx struct {
r *oReader
i Sym // start index
}
// objSym represents a symbol in an object file. It is a tuple of // objSym represents a symbol in an object file. It is a tuple of
// the object and the symbol's local index. // the object and the symbol's local index.
// For external symbols, objidx is the index of l.extReader (extObj), // For external symbols, objidx is the index of l.extReader (extObj),
@ -184,10 +179,9 @@ type symAndSize struct {
// overwriting/overwritten symbols, the second (or later) appearance // overwriting/overwritten symbols, the second (or later) appearance
// of the symbol gets the same global index as the first appearance. // of the symbol gets the same global index as the first appearance.
type Loader struct { type Loader struct {
start map[*oReader]Sym // map from object file to its start index objs []*oReader
objs []objIdx // sorted by start index (i.e. objIdx.i) extStart Sym // from this index on, the symbols are externally defined
extStart Sym // from this index on, the symbols are externally defined builtinSyms []Sym // global index of builtin symbols
builtinSyms []Sym // global index of builtin symbols
objSyms []objSym // global index mapping to local index objSyms []objSym // global index mapping to local index
@ -304,9 +298,8 @@ func NewLoader(flags uint32, reporter *ErrorReporter) *Loader {
nbuiltin := goobj.NBuiltin() nbuiltin := goobj.NBuiltin()
extReader := &oReader{objidx: extObj} extReader := &oReader{objidx: extObj}
ldr := &Loader{ ldr := &Loader{
start: make(map[*oReader]Sym), objs: []*oReader{nil, extReader}, // reserve index 0 for nil symbol, 1 for external symbols
objs: []objIdx{{}, {extReader, 0}}, // reserve index 0 for nil symbol, 1 for external symbols objSyms: make([]objSym, 1, 1), // This will get overwritten later.
objSyms: make([]objSym, 1, 1), // This will get overwritten later.
extReader: extReader, extReader: extReader,
symsByName: [2]map[string]Sym{make(map[string]Sym, 80000), make(map[string]Sym, 50000)}, // preallocate ~2MB for ABI0 and ~1MB for ABI1 symbols symsByName: [2]map[string]Sym{make(map[string]Sym, 80000), make(map[string]Sym, 50000)}, // preallocate ~2MB for ABI0 and ~1MB for ABI1 symbols
objByPkg: make(map[string]uint32), objByPkg: make(map[string]uint32),
@ -336,19 +329,13 @@ func NewLoader(flags uint32, reporter *ErrorReporter) *Loader {
return ldr return ldr
} }
// Add object file r, return the start index. // Add object file r
func (l *Loader) addObj(pkg string, r *oReader) Sym { func (l *Loader) addObj(pkg string, r *oReader) {
if _, ok := l.start[r]; ok {
panic("already added")
}
pkg = objabi.PathToPrefix(pkg) // the object file contains escaped package path pkg = objabi.PathToPrefix(pkg) // the object file contains escaped package path
if _, ok := l.objByPkg[pkg]; !ok { if _, ok := l.objByPkg[pkg]; !ok {
l.objByPkg[pkg] = r.objidx l.objByPkg[pkg] = r.objidx
} }
i := Sym(len(l.objSyms)) l.objs = append(l.objs, r)
l.start[r] = i
l.objs = append(l.objs, objIdx{r, i})
return i
} }
// Add a symbol from an object file, return the global index. // Add a symbol from an object file, return the global index.
@ -619,7 +606,7 @@ func (l *Loader) toGlobal(r *oReader, i uint32) Sym {
// Convert a global index to a local index. // Convert a global index to a local index.
func (l *Loader) toLocal(i Sym) (*oReader, uint32) { func (l *Loader) toLocal(i Sym) (*oReader, uint32) {
return l.objs[l.objSyms[i].objidx].r, l.objSyms[i].s return l.objs[l.objSyms[i].objidx], l.objSyms[i].s
} }
// Resolve a local symbol reference. Return global index. // Resolve a local symbol reference. Return global index.
@ -655,7 +642,7 @@ func (l *Loader) resolve(r *oReader, s goobj.SymRef) Sym {
case goobj.PkgIdxSelf: case goobj.PkgIdxSelf:
rr = r rr = r
default: default:
rr = l.objs[r.pkg[p]].r rr = l.objs[r.pkg[p]]
} }
return l.toGlobal(rr, s.SymIdx) return l.toGlobal(rr, s.SymIdx)
} }
@ -1099,7 +1086,7 @@ func (l *Loader) AttrReadOnly(i Sym) bool {
if l.IsExternal(i) { if l.IsExternal(i) {
pp := l.getPayload(i) pp := l.getPayload(i)
if pp.objidx != 0 { if pp.objidx != 0 {
return l.objs[pp.objidx].r.ReadOnly() return l.objs[pp.objidx].ReadOnly()
} }
return false return false
} }
@ -1537,7 +1524,7 @@ func (l *Loader) SymUnit(i Sym) *sym.CompilationUnit {
if l.IsExternal(i) { if l.IsExternal(i) {
pp := l.getPayload(i) pp := l.getPayload(i)
if pp.objidx != 0 { if pp.objidx != 0 {
r := l.objs[pp.objidx].r r := l.objs[pp.objidx]
return r.unit return r.unit
} }
return nil return nil
@ -1558,7 +1545,7 @@ func (l *Loader) SymPkg(i Sym) string {
if l.IsExternal(i) { if l.IsExternal(i) {
pp := l.getPayload(i) pp := l.getPayload(i)
if pp.objidx != 0 { if pp.objidx != 0 {
r := l.objs[pp.objidx].r r := l.objs[pp.objidx]
return r.unit.Lib.Pkg return r.unit.Lib.Pkg
} }
return "" return ""
@ -1916,7 +1903,7 @@ func (l *Loader) relocs(r *oReader, li uint32) Relocs {
func (l *Loader) auxs(i Sym) (*oReader, []goobj.Aux) { func (l *Loader) auxs(i Sym) (*oReader, []goobj.Aux) {
if l.IsExternal(i) { if l.IsExternal(i) {
pp := l.getPayload(i) pp := l.getPayload(i)
return l.objs[pp.objidx].r, pp.auxs return l.objs[pp.objidx], pp.auxs
} else { } else {
r, li := l.toLocal(i) r, li := l.toLocal(i)
return r, r.Auxs(li) return r, r.Auxs(li)
@ -2218,10 +2205,10 @@ func (l *Loader) LoadSyms(arch *sys.Arch) {
// This function was determined empirically by looking at the cmd/compile on // This function was determined empirically by looking at the cmd/compile on
// Darwin, and picking factors for hashed and hashed64 syms. // Darwin, and picking factors for hashed and hashed64 syms.
var symSize, hashedSize, hashed64Size int var symSize, hashedSize, hashed64Size int
for _, o := range l.objs[goObjStart:] { for _, r := range l.objs[goObjStart:] {
symSize += o.r.ndef + o.r.nhasheddef/2 + o.r.nhashed64def/2 + o.r.NNonpkgdef() symSize += r.ndef + r.nhasheddef/2 + r.nhashed64def/2 + r.NNonpkgdef()
hashedSize += o.r.nhasheddef / 2 hashedSize += r.nhasheddef / 2
hashed64Size += o.r.nhashed64def / 2 hashed64Size += r.nhashed64def / 2
} }
// Index 0 is invalid for symbols. // Index 0 is invalid for symbols.
l.objSyms = make([]objSym, 1, symSize) l.objSyms = make([]objSym, 1, symSize)
@ -2232,18 +2219,18 @@ func (l *Loader) LoadSyms(arch *sys.Arch) {
hashedSyms: make(map[goobj.HashType]symAndSize, hashedSize), hashedSyms: make(map[goobj.HashType]symAndSize, hashedSize),
} }
for _, o := range l.objs[goObjStart:] { for _, r := range l.objs[goObjStart:] {
st.preloadSyms(o.r, pkgDef) st.preloadSyms(r, pkgDef)
} }
l.npkgsyms = l.NSym() l.npkgsyms = l.NSym()
for _, o := range l.objs[goObjStart:] { for _, r := range l.objs[goObjStart:] {
st.preloadSyms(o.r, hashed64Def) st.preloadSyms(r, hashed64Def)
st.preloadSyms(o.r, hashedDef) st.preloadSyms(r, hashedDef)
st.preloadSyms(o.r, nonPkgDef) st.preloadSyms(r, nonPkgDef)
} }
l.nhashedsyms = len(st.hashed64Syms) + len(st.hashedSyms) l.nhashedsyms = len(st.hashed64Syms) + len(st.hashedSyms)
for _, o := range l.objs[goObjStart:] { for _, r := range l.objs[goObjStart:] {
loadObjRefs(l, o.r, arch) loadObjRefs(l, r, arch)
} }
l.values = make([]int64, l.NSym(), l.NSym()+1000) // +1000 make some room for external symbols l.values = make([]int64, l.NSym(), l.NSym()+1000) // +1000 make some room for external symbols
l.outer = make([]Sym, l.NSym(), l.NSym()+1000) l.outer = make([]Sym, l.NSym(), l.NSym()+1000)
@ -2521,8 +2508,7 @@ func (l *Loader) AssignTextSymbolOrder(libs []*sym.Library, intlibs []bool, exts
// 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 textp list. // them to their corresponding library's textp list.
for _, o := range l.objs[goObjStart:] { for _, r := range l.objs[goObjStart:] {
r := o.r
lib := r.unit.Lib lib := r.unit.Lib
for i, n := uint32(0), uint32(r.NAlldef()); i < n; i++ { for i, n := uint32(0), uint32(r.NAlldef()); i < n; i++ {
gi := l.toGlobal(r, i) gi := l.toGlobal(r, i)
@ -2636,9 +2622,9 @@ func (l *Loader) Stat() string {
// For debugging. // For debugging.
func (l *Loader) Dump() { func (l *Loader) Dump() {
fmt.Println("objs") fmt.Println("objs")
for _, obj := range l.objs[goObjStart:] { for _, r := range l.objs[goObjStart:] {
if obj.r != nil { if r != nil {
fmt.Println(obj.i, obj.r.unit.Lib) fmt.Println(r.unit.Lib)
} }
} }
fmt.Println("extStart:", l.extStart) fmt.Println("extStart:", l.extStart)