...

Source file src/cmd/link/internal/loader/loader.go

Documentation: cmd/link/internal/loader

     1  // Copyright 2019 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package loader
     6  
     7  import (
     8  	"bytes"
     9  	"cmd/internal/bio"
    10  	"cmd/internal/goobj"
    11  	"cmd/internal/obj"
    12  	"cmd/internal/objabi"
    13  	"cmd/internal/sys"
    14  	"cmd/link/internal/sym"
    15  	"debug/elf"
    16  	"fmt"
    17  	"internal/abi"
    18  	"io"
    19  	"log"
    20  	"math/bits"
    21  	"os"
    22  	"sort"
    23  	"strings"
    24  )
    25  
    26  var _ = fmt.Print
    27  
    28  // Sym encapsulates a global symbol index, used to identify a specific
    29  // Go symbol. The 0-valued Sym is corresponds to an invalid symbol.
    30  type Sym = sym.LoaderSym
    31  
    32  // Relocs encapsulates the set of relocations on a given symbol; an
    33  // instance of this type is returned by the Loader Relocs() method.
    34  type Relocs struct {
    35  	rs []goobj.Reloc
    36  
    37  	li uint32   // local index of symbol whose relocs we're examining
    38  	r  *oReader // object reader for containing package
    39  	l  *Loader  // loader
    40  }
    41  
    42  // ExtReloc contains the payload for an external relocation.
    43  type ExtReloc struct {
    44  	Xsym Sym
    45  	Xadd int64
    46  	Type objabi.RelocType
    47  	Size uint8
    48  }
    49  
    50  // Reloc holds a "handle" to access a relocation record from an
    51  // object file.
    52  type Reloc struct {
    53  	*goobj.Reloc
    54  	r *oReader
    55  	l *Loader
    56  }
    57  
    58  func (rel Reloc) Type() objabi.RelocType     { return objabi.RelocType(rel.Reloc.Type()) &^ objabi.R_WEAK }
    59  func (rel Reloc) Weak() bool                 { return objabi.RelocType(rel.Reloc.Type())&objabi.R_WEAK != 0 }
    60  func (rel Reloc) SetType(t objabi.RelocType) { rel.Reloc.SetType(uint16(t)) }
    61  func (rel Reloc) Sym() Sym                   { return rel.l.resolve(rel.r, rel.Reloc.Sym()) }
    62  func (rel Reloc) SetSym(s Sym)               { rel.Reloc.SetSym(goobj.SymRef{PkgIdx: 0, SymIdx: uint32(s)}) }
    63  func (rel Reloc) IsMarker() bool             { return rel.Siz() == 0 }
    64  
    65  // Aux holds a "handle" to access an aux symbol record from an
    66  // object file.
    67  type Aux struct {
    68  	*goobj.Aux
    69  	r *oReader
    70  	l *Loader
    71  }
    72  
    73  func (a Aux) Sym() Sym { return a.l.resolve(a.r, a.Aux.Sym()) }
    74  
    75  // oReader is a wrapper type of obj.Reader, along with some
    76  // extra information.
    77  type oReader struct {
    78  	*goobj.Reader
    79  	unit         *sym.CompilationUnit
    80  	version      int // version of static symbol
    81  	pkgprefix    string
    82  	syms         []Sym    // Sym's global index, indexed by local index
    83  	pkg          []uint32 // indices of referenced package by PkgIdx (index into loader.objs array)
    84  	ndef         int      // cache goobj.Reader.NSym()
    85  	nhashed64def int      // cache goobj.Reader.NHashed64Def()
    86  	nhasheddef   int      // cache goobj.Reader.NHashedDef()
    87  	objidx       uint32   // index of this reader in the objs slice
    88  }
    89  
    90  // Total number of defined symbols (package symbols, hashed symbols, and
    91  // non-package symbols).
    92  func (r *oReader) NAlldef() int { return r.ndef + r.nhashed64def + r.nhasheddef + r.NNonpkgdef() }
    93  
    94  // objSym represents a symbol in an object file. It is a tuple of
    95  // the object and the symbol's local index.
    96  // For external symbols, objidx is the index of l.extReader (extObj),
    97  // s is its index into the payload array.
    98  // {0, 0} represents the nil symbol.
    99  type objSym struct {
   100  	objidx uint32 // index of the object (in l.objs array)
   101  	s      uint32 // local index
   102  }
   103  
   104  type nameVer struct {
   105  	name string
   106  	v    int
   107  }
   108  
   109  type Bitmap []uint32
   110  
   111  // set the i-th bit.
   112  func (bm Bitmap) Set(i Sym) {
   113  	n, r := uint(i)/32, uint(i)%32
   114  	bm[n] |= 1 << r
   115  }
   116  
   117  // unset the i-th bit.
   118  func (bm Bitmap) Unset(i Sym) {
   119  	n, r := uint(i)/32, uint(i)%32
   120  	bm[n] &^= (1 << r)
   121  }
   122  
   123  // whether the i-th bit is set.
   124  func (bm Bitmap) Has(i Sym) bool {
   125  	n, r := uint(i)/32, uint(i)%32
   126  	return bm[n]&(1<<r) != 0
   127  }
   128  
   129  // return current length of bitmap in bits.
   130  func (bm Bitmap) Len() int {
   131  	return len(bm) * 32
   132  }
   133  
   134  // return the number of bits set.
   135  func (bm Bitmap) Count() int {
   136  	s := 0
   137  	for _, x := range bm {
   138  		s += bits.OnesCount32(x)
   139  	}
   140  	return s
   141  }
   142  
   143  func MakeBitmap(n int) Bitmap {
   144  	return make(Bitmap, (n+31)/32)
   145  }
   146  
   147  // growBitmap insures that the specified bitmap has enough capacity,
   148  // reallocating (doubling the size) if needed.
   149  func growBitmap(reqLen int, b Bitmap) Bitmap {
   150  	curLen := b.Len()
   151  	if reqLen > curLen {
   152  		b = append(b, MakeBitmap(reqLen+1-curLen)...)
   153  	}
   154  	return b
   155  }
   156  
   157  type symAndSize struct {
   158  	sym  Sym
   159  	size uint32
   160  }
   161  
   162  // A Loader loads new object files and resolves indexed symbol references.
   163  //
   164  // Notes on the layout of global symbol index space:
   165  //
   166  //   - Go object files are read before host object files; each Go object
   167  //     read adds its defined package symbols to the global index space.
   168  //     Nonpackage symbols are not yet added.
   169  //
   170  //   - In loader.LoadNonpkgSyms, add non-package defined symbols and
   171  //     references in all object files to the global index space.
   172  //
   173  //   - Host object file loading happens; the host object loader does a
   174  //     name/version lookup for each symbol it finds; this can wind up
   175  //     extending the external symbol index space range. The host object
   176  //     loader stores symbol payloads in loader.payloads using SymbolBuilder.
   177  //
   178  //   - Each symbol gets a unique global index. For duplicated and
   179  //     overwriting/overwritten symbols, the second (or later) appearance
   180  //     of the symbol gets the same global index as the first appearance.
   181  type Loader struct {
   182  	objs        []*oReader
   183  	extStart    Sym   // from this index on, the symbols are externally defined
   184  	builtinSyms []Sym // global index of builtin symbols
   185  
   186  	objSyms []objSym // global index mapping to local index
   187  
   188  	symsByName    [2]map[string]Sym // map symbol name to index, two maps are for ABI0 and ABIInternal
   189  	extStaticSyms map[nameVer]Sym   // externally defined static symbols, keyed by name
   190  
   191  	extReader    *oReader // a dummy oReader, for external symbols
   192  	payloadBatch []extSymPayload
   193  	payloads     []*extSymPayload // contents of linker-materialized external syms
   194  	values       []int64          // symbol values, indexed by global sym index
   195  
   196  	sects    []*sym.Section // sections
   197  	symSects []uint16       // symbol's section, index to sects array
   198  
   199  	align []uint8 // symbol 2^N alignment, indexed by global index
   200  
   201  	deferReturnTramp map[Sym]bool // whether the symbol is a trampoline of a deferreturn call
   202  
   203  	objByPkg map[string]uint32 // map package path to the index of its Go object reader
   204  
   205  	anonVersion int // most recently assigned ext static sym pseudo-version
   206  
   207  	// Bitmaps and other side structures used to store data used to store
   208  	// symbol flags/attributes; these are to be accessed via the
   209  	// corresponding loader "AttrXXX" and "SetAttrXXX" methods. Please
   210  	// visit the comments on these methods for more details on the
   211  	// semantics / interpretation of the specific flags or attribute.
   212  	attrReachable        Bitmap // reachable symbols, indexed by global index
   213  	attrOnList           Bitmap // "on list" symbols, indexed by global index
   214  	attrLocal            Bitmap // "local" symbols, indexed by global index
   215  	attrNotInSymbolTable Bitmap // "not in symtab" symbols, indexed by global idx
   216  	attrUsedInIface      Bitmap // "used in interface" symbols, indexed by global idx
   217  	attrSpecial          Bitmap // "special" frame symbols, indexed by global idx
   218  	attrVisibilityHidden Bitmap // hidden symbols, indexed by ext sym index
   219  	attrDuplicateOK      Bitmap // dupOK symbols, indexed by ext sym index
   220  	attrShared           Bitmap // shared symbols, indexed by ext sym index
   221  	attrExternal         Bitmap // external symbols, indexed by ext sym index
   222  	generatedSyms        Bitmap // symbols that generate their content, indexed by ext sym idx
   223  
   224  	attrReadOnly         map[Sym]bool     // readonly data for this sym
   225  	attrCgoExportDynamic map[Sym]struct{} // "cgo_export_dynamic" symbols
   226  	attrCgoExportStatic  map[Sym]struct{} // "cgo_export_static" symbols
   227  
   228  	// Outer and Sub relations for symbols.
   229  	outer []Sym // indexed by global index
   230  	sub   map[Sym]Sym
   231  
   232  	dynimplib   map[Sym]string      // stores Dynimplib symbol attribute
   233  	dynimpvers  map[Sym]string      // stores Dynimpvers symbol attribute
   234  	localentry  map[Sym]uint8       // stores Localentry symbol attribute
   235  	extname     map[Sym]string      // stores Extname symbol attribute
   236  	elfType     map[Sym]elf.SymType // stores elf type symbol property
   237  	elfSym      map[Sym]int32       // stores elf sym symbol property
   238  	localElfSym map[Sym]int32       // stores "local" elf sym symbol property
   239  	symPkg      map[Sym]string      // stores package for symbol, or library for shlib-derived syms
   240  	plt         map[Sym]int32       // stores dynimport for pe objects
   241  	got         map[Sym]int32       // stores got for pe objects
   242  	dynid       map[Sym]int32       // stores Dynid for symbol
   243  
   244  	relocVariant map[relocId]sym.RelocVariant // stores variant relocs
   245  
   246  	// Used to implement field tracking; created during deadcode if
   247  	// field tracking is enabled. Reachparent[K] contains the index of
   248  	// the symbol that triggered the marking of symbol K as live.
   249  	Reachparent []Sym
   250  
   251  	// CgoExports records cgo-exported symbols by SymName.
   252  	CgoExports map[string]Sym
   253  
   254  	WasmExports []Sym
   255  
   256  	flags uint32
   257  
   258  	strictDupMsgs int // number of strict-dup warning/errors, when FlagStrictDups is enabled
   259  
   260  	errorReporter *ErrorReporter
   261  
   262  	npkgsyms    int // number of package symbols, for accounting
   263  	nhashedsyms int // number of hashed symbols, for accounting
   264  }
   265  
   266  const (
   267  	pkgDef = iota
   268  	hashed64Def
   269  	hashedDef
   270  	nonPkgDef
   271  	nonPkgRef
   272  )
   273  
   274  // objidx
   275  const (
   276  	nilObj = iota
   277  	extObj
   278  	goObjStart
   279  )
   280  
   281  // extSymPayload holds the payload (data + relocations) for linker-synthesized
   282  // external symbols (note that symbol value is stored in a separate slice).
   283  type extSymPayload struct {
   284  	name   string // TODO: would this be better as offset into str table?
   285  	size   int64
   286  	ver    int
   287  	kind   sym.SymKind
   288  	objidx uint32 // index of original object if sym made by cloneToExternal
   289  	relocs []goobj.Reloc
   290  	data   []byte
   291  	auxs   []goobj.Aux
   292  }
   293  
   294  const (
   295  	// Loader.flags
   296  	FlagStrictDups = 1 << iota
   297  	FlagCheckLinkname
   298  )
   299  
   300  func NewLoader(flags uint32, reporter *ErrorReporter) *Loader {
   301  	nbuiltin := goobj.NBuiltin()
   302  	extReader := &oReader{objidx: extObj}
   303  	ldr := &Loader{
   304  		objs:                 []*oReader{nil, extReader}, // reserve index 0 for nil symbol, 1 for external symbols
   305  		objSyms:              make([]objSym, 1, 1),       // This will get overwritten later.
   306  		extReader:            extReader,
   307  		symsByName:           [2]map[string]Sym{make(map[string]Sym, 80000), make(map[string]Sym, 50000)}, // preallocate ~2MB for ABI0 and ~1MB for ABI1 symbols
   308  		objByPkg:             make(map[string]uint32),
   309  		sub:                  make(map[Sym]Sym),
   310  		dynimplib:            make(map[Sym]string),
   311  		dynimpvers:           make(map[Sym]string),
   312  		localentry:           make(map[Sym]uint8),
   313  		extname:              make(map[Sym]string),
   314  		attrReadOnly:         make(map[Sym]bool),
   315  		elfType:              make(map[Sym]elf.SymType),
   316  		elfSym:               make(map[Sym]int32),
   317  		localElfSym:          make(map[Sym]int32),
   318  		symPkg:               make(map[Sym]string),
   319  		plt:                  make(map[Sym]int32),
   320  		got:                  make(map[Sym]int32),
   321  		dynid:                make(map[Sym]int32),
   322  		attrCgoExportDynamic: make(map[Sym]struct{}),
   323  		attrCgoExportStatic:  make(map[Sym]struct{}),
   324  		deferReturnTramp:     make(map[Sym]bool),
   325  		extStaticSyms:        make(map[nameVer]Sym),
   326  		builtinSyms:          make([]Sym, nbuiltin),
   327  		flags:                flags,
   328  		errorReporter:        reporter,
   329  		sects:                []*sym.Section{nil}, // reserve index 0 for nil section
   330  	}
   331  	reporter.ldr = ldr
   332  	return ldr
   333  }
   334  
   335  // Add object file r
   336  func (l *Loader) addObj(pkg string, r *oReader) {
   337  	pkg = objabi.PathToPrefix(pkg) // the object file contains escaped package path
   338  	if _, ok := l.objByPkg[pkg]; !ok {
   339  		l.objByPkg[pkg] = r.objidx
   340  	}
   341  	l.objs = append(l.objs, r)
   342  }
   343  
   344  // Add a symbol from an object file, return the global index.
   345  // If the symbol already exist, it returns the index of that symbol.
   346  func (st *loadState) addSym(name string, ver int, r *oReader, li uint32, kind int, osym *goobj.Sym) Sym {
   347  	l := st.l
   348  	if l.extStart != 0 {
   349  		panic("addSym called after external symbol is created")
   350  	}
   351  	i := Sym(len(l.objSyms))
   352  	if int(i) != len(l.objSyms) { // overflow
   353  		panic("too many symbols")
   354  	}
   355  	addToGlobal := func() {
   356  		l.objSyms = append(l.objSyms, objSym{r.objidx, li})
   357  	}
   358  	if name == "" && kind != hashed64Def && kind != hashedDef {
   359  		addToGlobal()
   360  		return i // unnamed aux symbol
   361  	}
   362  	if ver == r.version {
   363  		// Static symbol. Add its global index but don't
   364  		// add to name lookup table, as it cannot be
   365  		// referenced by name.
   366  		addToGlobal()
   367  		return i
   368  	}
   369  	switch kind {
   370  	case pkgDef:
   371  		// Defined package symbols cannot be dup to each other.
   372  		// We load all the package symbols first, so we don't need
   373  		// to check dup here.
   374  		// We still add it to the lookup table, as it may still be
   375  		// referenced by name (e.g. through linkname).
   376  		l.symsByName[ver][name] = i
   377  		addToGlobal()
   378  		return i
   379  	case hashed64Def, hashedDef:
   380  		// Hashed (content-addressable) symbol. Check the hash
   381  		// but don't add to name lookup table, as they are not
   382  		// referenced by name. Also no need to do overwriting
   383  		// check, as same hash indicates same content.
   384  		var checkHash func() (symAndSize, bool)
   385  		var addToHashMap func(symAndSize)
   386  		var h64 uint64        // only used for hashed64Def
   387  		var h *goobj.HashType // only used for hashedDef
   388  		if kind == hashed64Def {
   389  			checkHash = func() (symAndSize, bool) {
   390  				h64 = r.Hash64(li - uint32(r.ndef))
   391  				s, existed := st.hashed64Syms[h64]
   392  				return s, existed
   393  			}
   394  			addToHashMap = func(ss symAndSize) { st.hashed64Syms[h64] = ss }
   395  		} else {
   396  			checkHash = func() (symAndSize, bool) {
   397  				h = r.Hash(li - uint32(r.ndef+r.nhashed64def))
   398  				s, existed := st.hashedSyms[*h]
   399  				return s, existed
   400  			}
   401  			addToHashMap = func(ss symAndSize) { st.hashedSyms[*h] = ss }
   402  		}
   403  		siz := osym.Siz()
   404  		if s, existed := checkHash(); existed {
   405  			// The content hash is built from symbol data and relocations. In the
   406  			// object file, the symbol data may not always contain trailing zeros,
   407  			// e.g. for [5]int{1,2,3} and [100]int{1,2,3}, the data is same
   408  			// (although the size is different).
   409  			// Also, for short symbols, the content hash is the identity function of
   410  			// the 8 bytes, and trailing zeros doesn't change the hash value, e.g.
   411  			// hash("A") == hash("A\0\0\0").
   412  			// So when two symbols have the same hash, we need to use the one with
   413  			// larger size.
   414  			if siz > s.size {
   415  				// New symbol has larger size, use the new one. Rewrite the index mapping.
   416  				l.objSyms[s.sym] = objSym{r.objidx, li}
   417  				addToHashMap(symAndSize{s.sym, siz})
   418  			}
   419  			return s.sym
   420  		}
   421  		addToHashMap(symAndSize{i, siz})
   422  		addToGlobal()
   423  		return i
   424  	}
   425  
   426  	// Non-package (named) symbol.
   427  	// Check if it already exists.
   428  	oldi, existed := l.symsByName[ver][name]
   429  	if !existed {
   430  		l.symsByName[ver][name] = i
   431  		addToGlobal()
   432  		return i
   433  	}
   434  	// symbol already exists
   435  	// Fix for issue #47185 -- given two dupok or BSS symbols with
   436  	// different sizes, favor symbol with larger size. See also
   437  	// issue #46653 and #72032.
   438  	oldsz := l.SymSize(oldi)
   439  	sz := int64(r.Sym(li).Siz())
   440  	if osym.Dupok() {
   441  		if l.flags&FlagStrictDups != 0 {
   442  			l.checkdup(name, r, li, oldi)
   443  		}
   444  		if oldsz < sz {
   445  			// new symbol overwrites old symbol.
   446  			l.objSyms[oldi] = objSym{r.objidx, li}
   447  		}
   448  		return oldi
   449  	}
   450  	oldr, oldli := l.toLocal(oldi)
   451  	oldsym := oldr.Sym(oldli)
   452  	if oldsym.Dupok() {
   453  		return oldi
   454  	}
   455  	// If one is a DATA symbol (i.e. has content, DataSize != 0,
   456  	// including RODATA) and the other is BSS, the one with content wins.
   457  	// If both are BSS, the one with larger size wins.
   458  	//
   459  	// For a special case, we allow a TEXT symbol overwrites a BSS symbol
   460  	// even if the BSS symbol has larger size. This is because there is
   461  	// code like below to take the address of a function
   462  	//
   463  	//	//go:linkname fn
   464  	//	var fn uintptr
   465  	//	var fnAddr = uintptr(unsafe.Pointer(&fn))
   466  	//
   467  	// TODO: maybe limit this case to just pointer sized variable?
   468  	//
   469  	// In summary, the "overwrite" variable and the final result are
   470  	//
   471  	// new sym       old sym       result
   472  	// ---------------------------------------------
   473  	// TEXT          BSS           new wins
   474  	// DATA          DATA          ERROR
   475  	// DATA lg/eq    BSS  sm/eq    new wins
   476  	// DATA small    BSS  large    ERROR
   477  	// BSS  large    DATA small    ERROR
   478  	// BSS  large    BSS  small    new wins
   479  	// BSS  sm/eq    D/B  lg/eq    old wins
   480  	// BSS           TEXT          old wins
   481  	oldtyp := sym.AbiSymKindToSymKind[objabi.SymKind(oldsym.Type())]
   482  	newtyp := sym.AbiSymKindToSymKind[objabi.SymKind(osym.Type())]
   483  	oldIsText := oldtyp.IsText()
   484  	newIsText := newtyp.IsText()
   485  	oldHasContent := oldr.DataSize(oldli) != 0
   486  	newHasContent := r.DataSize(li) != 0
   487  	oldIsBSS := oldtyp.IsData() && !oldHasContent
   488  	newIsBSS := newtyp.IsData() && !newHasContent
   489  	switch {
   490  	case newIsText && oldIsBSS,
   491  		newHasContent && oldIsBSS && sz >= oldsz,
   492  		newIsBSS && oldIsBSS && sz > oldsz:
   493  		// new symbol overwrites old symbol.
   494  		l.objSyms[oldi] = objSym{r.objidx, li}
   495  	case newIsBSS && (oldsz >= sz || oldIsText):
   496  		// old win, just ignore the new symbol.
   497  	default:
   498  		log.Fatalf("duplicated definition of symbol %s, from %s (type %s size %d) and %s (type %s size %d)", name, r.unit.Lib.Pkg, newtyp, sz, oldr.unit.Lib.Pkg, oldtyp, oldsz)
   499  	}
   500  	return oldi
   501  }
   502  
   503  // newExtSym creates a new external sym with the specified
   504  // name/version.
   505  func (l *Loader) newExtSym(name string, ver int) Sym {
   506  	i := Sym(len(l.objSyms))
   507  	if int(i) != len(l.objSyms) { // overflow
   508  		panic("too many symbols")
   509  	}
   510  	if l.extStart == 0 {
   511  		l.extStart = i
   512  	}
   513  	l.growValues(int(i) + 1)
   514  	l.growOuter(int(i) + 1)
   515  	l.growAttrBitmaps(int(i) + 1)
   516  	pi := l.newPayload(name, ver)
   517  	l.objSyms = append(l.objSyms, objSym{l.extReader.objidx, uint32(pi)})
   518  	l.extReader.syms = append(l.extReader.syms, i)
   519  	return i
   520  }
   521  
   522  // LookupOrCreateSym looks up the symbol with the specified name/version,
   523  // returning its Sym index if found. If the lookup fails, a new external
   524  // Sym will be created, entered into the lookup tables, and returned.
   525  func (l *Loader) LookupOrCreateSym(name string, ver int) Sym {
   526  	i := l.Lookup(name, ver)
   527  	if i != 0 {
   528  		return i
   529  	}
   530  	i = l.newExtSym(name, ver)
   531  	static := ver >= sym.SymVerStatic || ver < 0
   532  	if static {
   533  		l.extStaticSyms[nameVer{name, ver}] = i
   534  	} else {
   535  		l.symsByName[ver][name] = i
   536  	}
   537  	return i
   538  }
   539  
   540  // AddCgoExport records a cgo-exported symbol in l.CgoExports.
   541  // This table is used to identify the correct Go symbol ABI to use
   542  // to resolve references from host objects (which don't have ABIs).
   543  func (l *Loader) AddCgoExport(s Sym) {
   544  	if l.CgoExports == nil {
   545  		l.CgoExports = make(map[string]Sym)
   546  	}
   547  	l.CgoExports[l.SymName(s)] = s
   548  }
   549  
   550  // LookupOrCreateCgoExport is like LookupOrCreateSym, but if ver
   551  // indicates a global symbol, it uses the CgoExport table to determine
   552  // the appropriate symbol version (ABI) to use. ver must be either 0
   553  // or a static symbol version.
   554  func (l *Loader) LookupOrCreateCgoExport(name string, ver int) Sym {
   555  	if ver >= sym.SymVerStatic {
   556  		return l.LookupOrCreateSym(name, ver)
   557  	}
   558  	if ver != 0 {
   559  		panic("ver must be 0 or a static version")
   560  	}
   561  	// Look for a cgo-exported symbol from Go.
   562  	if s, ok := l.CgoExports[name]; ok {
   563  		return s
   564  	}
   565  	// Otherwise, this must just be a symbol in the host object.
   566  	// Create a version 0 symbol for it.
   567  	return l.LookupOrCreateSym(name, 0)
   568  }
   569  
   570  func (l *Loader) IsExternal(i Sym) bool {
   571  	r, _ := l.toLocal(i)
   572  	return l.isExtReader(r)
   573  }
   574  
   575  func (l *Loader) isExtReader(r *oReader) bool {
   576  	return r == l.extReader
   577  }
   578  
   579  // For external symbol, return its index in the payloads array.
   580  // XXX result is actually not a global index. We (ab)use the Sym type
   581  // so we don't need conversion for accessing bitmaps.
   582  func (l *Loader) extIndex(i Sym) Sym {
   583  	_, li := l.toLocal(i)
   584  	return Sym(li)
   585  }
   586  
   587  // Get a new payload for external symbol, return its index in
   588  // the payloads array.
   589  func (l *Loader) newPayload(name string, ver int) int {
   590  	pi := len(l.payloads)
   591  	pp := l.allocPayload()
   592  	pp.name = name
   593  	pp.ver = ver
   594  	l.payloads = append(l.payloads, pp)
   595  	l.growExtAttrBitmaps()
   596  	return pi
   597  }
   598  
   599  // getPayload returns a pointer to the extSymPayload struct for an
   600  // external symbol if the symbol has a payload. Will panic if the
   601  // symbol in question is bogus (zero or not an external sym).
   602  func (l *Loader) getPayload(i Sym) *extSymPayload {
   603  	if !l.IsExternal(i) {
   604  		panic(fmt.Sprintf("bogus symbol index %d in getPayload", i))
   605  	}
   606  	pi := l.extIndex(i)
   607  	return l.payloads[pi]
   608  }
   609  
   610  // allocPayload allocates a new payload.
   611  func (l *Loader) allocPayload() *extSymPayload {
   612  	batch := l.payloadBatch
   613  	if len(batch) == 0 {
   614  		batch = make([]extSymPayload, 1000)
   615  	}
   616  	p := &batch[0]
   617  	l.payloadBatch = batch[1:]
   618  	return p
   619  }
   620  
   621  func (ms *extSymPayload) Grow(siz int64) {
   622  	if int64(int(siz)) != siz {
   623  		log.Fatalf("symgrow size %d too long", siz)
   624  	}
   625  	if int64(len(ms.data)) >= siz {
   626  		return
   627  	}
   628  	if cap(ms.data) < int(siz) {
   629  		cl := len(ms.data)
   630  		ms.data = append(ms.data, make([]byte, int(siz)+1-cl)...)
   631  		ms.data = ms.data[0:cl]
   632  	}
   633  	ms.data = ms.data[:siz]
   634  }
   635  
   636  // Convert a local index to a global index.
   637  func (l *Loader) toGlobal(r *oReader, i uint32) Sym {
   638  	return r.syms[i]
   639  }
   640  
   641  // Convert a global index to a local index.
   642  func (l *Loader) toLocal(i Sym) (*oReader, uint32) {
   643  	return l.objs[l.objSyms[i].objidx], l.objSyms[i].s
   644  }
   645  
   646  // Resolve a local symbol reference. Return global index.
   647  func (l *Loader) resolve(r *oReader, s goobj.SymRef) Sym {
   648  	var rr *oReader
   649  	switch p := s.PkgIdx; p {
   650  	case goobj.PkgIdxInvalid:
   651  		// {0, X} with non-zero X is never a valid sym reference from a Go object.
   652  		// We steal this space for symbol references from external objects.
   653  		// In this case, X is just the global index.
   654  		if l.isExtReader(r) {
   655  			return Sym(s.SymIdx)
   656  		}
   657  		if s.SymIdx != 0 {
   658  			panic("bad sym ref")
   659  		}
   660  		return 0
   661  	case goobj.PkgIdxHashed64:
   662  		i := int(s.SymIdx) + r.ndef
   663  		return r.syms[i]
   664  	case goobj.PkgIdxHashed:
   665  		i := int(s.SymIdx) + r.ndef + r.nhashed64def
   666  		return r.syms[i]
   667  	case goobj.PkgIdxNone:
   668  		i := int(s.SymIdx) + r.ndef + r.nhashed64def + r.nhasheddef
   669  		return r.syms[i]
   670  	case goobj.PkgIdxBuiltin:
   671  		if bi := l.builtinSyms[s.SymIdx]; bi != 0 {
   672  			return bi
   673  		}
   674  		l.reportMissingBuiltin(int(s.SymIdx), r.unit.Lib.Pkg)
   675  		return 0
   676  	case goobj.PkgIdxSelf:
   677  		rr = r
   678  	default:
   679  		rr = l.objs[r.pkg[p]]
   680  	}
   681  	return l.toGlobal(rr, s.SymIdx)
   682  }
   683  
   684  // reportMissingBuiltin issues an error in the case where we have a
   685  // relocation against a runtime builtin whose definition is not found
   686  // when the runtime package is built. The canonical example is
   687  // "runtime.racefuncenter" -- currently if you do something like
   688  //
   689  //	go build -gcflags=-race myprogram.go
   690  //
   691  // the compiler will insert calls to the builtin runtime.racefuncenter,
   692  // but the version of the runtime used for linkage won't actually contain
   693  // definitions of that symbol. See issue #42396 for details.
   694  //
   695  // As currently implemented, this is a fatal error. This has drawbacks
   696  // in that if there are multiple missing builtins, the error will only
   697  // cite the first one. On the plus side, terminating the link here has
   698  // advantages in that we won't run the risk of panics or crashes later
   699  // on in the linker due to R_CALL relocations with 0-valued target
   700  // symbols.
   701  func (l *Loader) reportMissingBuiltin(bsym int, reflib string) {
   702  	bname, _ := goobj.BuiltinName(bsym)
   703  	log.Fatalf("reference to undefined builtin %q from package %q",
   704  		bname, reflib)
   705  }
   706  
   707  // Look up a symbol by name, return global index, or 0 if not found.
   708  // This is more like Syms.ROLookup than Lookup -- it doesn't create
   709  // new symbol.
   710  func (l *Loader) Lookup(name string, ver int) Sym {
   711  	if ver >= sym.SymVerStatic || ver < 0 {
   712  		return l.extStaticSyms[nameVer{name, ver}]
   713  	}
   714  	return l.symsByName[ver][name]
   715  }
   716  
   717  // Check that duplicate symbols have same contents.
   718  func (l *Loader) checkdup(name string, r *oReader, li uint32, dup Sym) {
   719  	p := r.Data(li)
   720  	rdup, ldup := l.toLocal(dup)
   721  	pdup := rdup.Data(ldup)
   722  	reason := "same length but different contents"
   723  	if len(p) != len(pdup) {
   724  		reason = fmt.Sprintf("new length %d != old length %d", len(p), len(pdup))
   725  	} else if bytes.Equal(p, pdup) {
   726  		// For BSS symbols, we need to check size as well, see issue 46653.
   727  		szdup := l.SymSize(dup)
   728  		sz := int64(r.Sym(li).Siz())
   729  		if szdup == sz {
   730  			return
   731  		}
   732  		reason = fmt.Sprintf("different sizes: new size %d != old size %d",
   733  			sz, szdup)
   734  	}
   735  	fmt.Fprintf(os.Stderr, "cmd/link: while reading object for '%v': duplicate symbol '%s', previous def at '%v', with mismatched payload: %s\n", r.unit.Lib, name, rdup.unit.Lib, reason)
   736  
   737  	// For the moment, allow DWARF subprogram DIEs for
   738  	// auto-generated wrapper functions. What seems to happen
   739  	// here is that we get different line numbers on formal
   740  	// params; I am guessing that the pos is being inherited
   741  	// from the spot where the wrapper is needed.
   742  	allowed := strings.HasPrefix(name, "go:info.go.interface") ||
   743  		strings.HasPrefix(name, "go:info.go.builtin") ||
   744  		strings.HasPrefix(name, "go:debuglines")
   745  	if !allowed {
   746  		l.strictDupMsgs++
   747  	}
   748  }
   749  
   750  func (l *Loader) NStrictDupMsgs() int { return l.strictDupMsgs }
   751  
   752  // Number of total symbols.
   753  func (l *Loader) NSym() int {
   754  	return len(l.objSyms)
   755  }
   756  
   757  // Number of defined Go symbols.
   758  func (l *Loader) NDef() int {
   759  	return int(l.extStart)
   760  }
   761  
   762  // Number of reachable symbols.
   763  func (l *Loader) NReachableSym() int {
   764  	return l.attrReachable.Count()
   765  }
   766  
   767  // Returns the name of the i-th symbol.
   768  func (l *Loader) SymName(i Sym) string {
   769  	if l.IsExternal(i) {
   770  		pp := l.getPayload(i)
   771  		return pp.name
   772  	}
   773  	r, li := l.toLocal(i)
   774  	if r == nil {
   775  		return "?"
   776  	}
   777  	return r.Sym(li).Name(r.Reader)
   778  }
   779  
   780  // Returns the version of the i-th symbol.
   781  func (l *Loader) SymVersion(i Sym) int {
   782  	if l.IsExternal(i) {
   783  		pp := l.getPayload(i)
   784  		return pp.ver
   785  	}
   786  	r, li := l.toLocal(i)
   787  	return int(abiToVer(r.Sym(li).ABI(), r.version))
   788  }
   789  
   790  func (l *Loader) IsFileLocal(i Sym) bool {
   791  	return l.SymVersion(i) >= sym.SymVerStatic
   792  }
   793  
   794  // IsFromAssembly returns true if this symbol is derived from an
   795  // object file generated by the Go assembler.
   796  func (l *Loader) IsFromAssembly(i Sym) bool {
   797  	if l.IsExternal(i) {
   798  		pp := l.getPayload(i)
   799  		if pp.objidx != 0 {
   800  			r := l.objs[pp.objidx]
   801  			return r.FromAssembly()
   802  		}
   803  		return false
   804  	}
   805  	r, _ := l.toLocal(i)
   806  	return r.FromAssembly()
   807  }
   808  
   809  // Returns the type of the i-th symbol.
   810  func (l *Loader) SymType(i Sym) sym.SymKind {
   811  	if l.IsExternal(i) {
   812  		pp := l.getPayload(i)
   813  		if pp != nil {
   814  			return pp.kind
   815  		}
   816  		return 0
   817  	}
   818  	r, li := l.toLocal(i)
   819  	return sym.AbiSymKindToSymKind[objabi.SymKind(r.Sym(li).Type())]
   820  }
   821  
   822  // Returns the attributes of the i-th symbol.
   823  func (l *Loader) SymAttr(i Sym) uint8 {
   824  	if l.IsExternal(i) {
   825  		// TODO: do something? External symbols have different representation of attributes.
   826  		// For now, ReflectMethod, NoSplit, GoType, and Typelink are used and they cannot be
   827  		// set by external symbol.
   828  		return 0
   829  	}
   830  	r, li := l.toLocal(i)
   831  	return r.Sym(li).Flag()
   832  }
   833  
   834  // Returns the size of the i-th symbol.
   835  func (l *Loader) SymSize(i Sym) int64 {
   836  	if l.IsExternal(i) {
   837  		pp := l.getPayload(i)
   838  		return pp.size
   839  	}
   840  	r, li := l.toLocal(i)
   841  	return int64(r.Sym(li).Siz())
   842  }
   843  
   844  // AttrReachable returns true for symbols that are transitively
   845  // referenced from the entry points. Unreachable symbols are not
   846  // written to the output.
   847  func (l *Loader) AttrReachable(i Sym) bool {
   848  	return l.attrReachable.Has(i)
   849  }
   850  
   851  // SetAttrReachable sets the reachability property for a symbol (see
   852  // AttrReachable).
   853  func (l *Loader) SetAttrReachable(i Sym, v bool) {
   854  	if v {
   855  		l.attrReachable.Set(i)
   856  	} else {
   857  		l.attrReachable.Unset(i)
   858  	}
   859  }
   860  
   861  // AttrOnList returns true for symbols that are on some list (such as
   862  // the list of all text symbols, or one of the lists of data symbols)
   863  // and is consulted to avoid bugs where a symbol is put on a list
   864  // twice.
   865  func (l *Loader) AttrOnList(i Sym) bool {
   866  	return l.attrOnList.Has(i)
   867  }
   868  
   869  // SetAttrOnList sets the "on list" property for a symbol (see
   870  // AttrOnList).
   871  func (l *Loader) SetAttrOnList(i Sym, v bool) {
   872  	if v {
   873  		l.attrOnList.Set(i)
   874  	} else {
   875  		l.attrOnList.Unset(i)
   876  	}
   877  }
   878  
   879  // AttrLocal returns true for symbols that are only visible within the
   880  // module (executable or shared library) being linked. This attribute
   881  // is applied to thunks and certain other linker-generated symbols.
   882  func (l *Loader) AttrLocal(i Sym) bool {
   883  	return l.attrLocal.Has(i)
   884  }
   885  
   886  // SetAttrLocal the "local" property for a symbol (see AttrLocal above).
   887  func (l *Loader) SetAttrLocal(i Sym, v bool) {
   888  	if v {
   889  		l.attrLocal.Set(i)
   890  	} else {
   891  		l.attrLocal.Unset(i)
   892  	}
   893  }
   894  
   895  // AttrUsedInIface returns true for a type symbol that is used in
   896  // an interface.
   897  func (l *Loader) AttrUsedInIface(i Sym) bool {
   898  	return l.attrUsedInIface.Has(i)
   899  }
   900  
   901  func (l *Loader) SetAttrUsedInIface(i Sym, v bool) {
   902  	if v {
   903  		l.attrUsedInIface.Set(i)
   904  	} else {
   905  		l.attrUsedInIface.Unset(i)
   906  	}
   907  }
   908  
   909  // SymAddr checks that a symbol is reachable, and returns its value.
   910  func (l *Loader) SymAddr(i Sym) int64 {
   911  	if !l.AttrReachable(i) {
   912  		panic("unreachable symbol in symaddr")
   913  	}
   914  	return l.values[i]
   915  }
   916  
   917  // AttrNotInSymbolTable returns true for symbols that should not be
   918  // added to the symbol table of the final generated load module.
   919  func (l *Loader) AttrNotInSymbolTable(i Sym) bool {
   920  	return l.attrNotInSymbolTable.Has(i)
   921  }
   922  
   923  // SetAttrNotInSymbolTable the "not in symtab" property for a symbol
   924  // (see AttrNotInSymbolTable above).
   925  func (l *Loader) SetAttrNotInSymbolTable(i Sym, v bool) {
   926  	if v {
   927  		l.attrNotInSymbolTable.Set(i)
   928  	} else {
   929  		l.attrNotInSymbolTable.Unset(i)
   930  	}
   931  }
   932  
   933  // AttrVisibilityHidden symbols returns true for ELF symbols with
   934  // visibility set to STV_HIDDEN. They become local symbols in
   935  // the final executable. Only relevant when internally linking
   936  // on an ELF platform.
   937  func (l *Loader) AttrVisibilityHidden(i Sym) bool {
   938  	if !l.IsExternal(i) {
   939  		return false
   940  	}
   941  	return l.attrVisibilityHidden.Has(l.extIndex(i))
   942  }
   943  
   944  // SetAttrVisibilityHidden sets the "hidden visibility" property for a
   945  // symbol (see AttrVisibilityHidden).
   946  func (l *Loader) SetAttrVisibilityHidden(i Sym, v bool) {
   947  	if !l.IsExternal(i) {
   948  		panic("tried to set visibility attr on non-external symbol")
   949  	}
   950  	if v {
   951  		l.attrVisibilityHidden.Set(l.extIndex(i))
   952  	} else {
   953  		l.attrVisibilityHidden.Unset(l.extIndex(i))
   954  	}
   955  }
   956  
   957  // AttrDuplicateOK returns true for a symbol that can be present in
   958  // multiple object files.
   959  func (l *Loader) AttrDuplicateOK(i Sym) bool {
   960  	if !l.IsExternal(i) {
   961  		// TODO: if this path winds up being taken frequently, it
   962  		// might make more sense to copy the flag value out of the object
   963  		// into a larger bitmap during preload.
   964  		r, li := l.toLocal(i)
   965  		return r.Sym(li).Dupok()
   966  	}
   967  	return l.attrDuplicateOK.Has(l.extIndex(i))
   968  }
   969  
   970  // SetAttrDuplicateOK sets the "duplicate OK" property for an external
   971  // symbol (see AttrDuplicateOK).
   972  func (l *Loader) SetAttrDuplicateOK(i Sym, v bool) {
   973  	if !l.IsExternal(i) {
   974  		panic("tried to set dupok attr on non-external symbol")
   975  	}
   976  	if v {
   977  		l.attrDuplicateOK.Set(l.extIndex(i))
   978  	} else {
   979  		l.attrDuplicateOK.Unset(l.extIndex(i))
   980  	}
   981  }
   982  
   983  // AttrShared returns true for symbols compiled with the -shared option.
   984  func (l *Loader) AttrShared(i Sym) bool {
   985  	if !l.IsExternal(i) {
   986  		// TODO: if this path winds up being taken frequently, it
   987  		// might make more sense to copy the flag value out of the
   988  		// object into a larger bitmap during preload.
   989  		r, _ := l.toLocal(i)
   990  		return r.Shared()
   991  	}
   992  	return l.attrShared.Has(l.extIndex(i))
   993  }
   994  
   995  // SetAttrShared sets the "shared" property for an external
   996  // symbol (see AttrShared).
   997  func (l *Loader) SetAttrShared(i Sym, v bool) {
   998  	if !l.IsExternal(i) {
   999  		panic(fmt.Sprintf("tried to set shared attr on non-external symbol %d %s", i, l.SymName(i)))
  1000  	}
  1001  	if v {
  1002  		l.attrShared.Set(l.extIndex(i))
  1003  	} else {
  1004  		l.attrShared.Unset(l.extIndex(i))
  1005  	}
  1006  }
  1007  
  1008  // AttrExternal returns true for function symbols loaded from host
  1009  // object files.
  1010  func (l *Loader) AttrExternal(i Sym) bool {
  1011  	if !l.IsExternal(i) {
  1012  		return false
  1013  	}
  1014  	return l.attrExternal.Has(l.extIndex(i))
  1015  }
  1016  
  1017  // SetAttrExternal sets the "external" property for a host object
  1018  // symbol (see AttrExternal).
  1019  func (l *Loader) SetAttrExternal(i Sym, v bool) {
  1020  	if !l.IsExternal(i) {
  1021  		panic(fmt.Sprintf("tried to set external attr on non-external symbol %q", l.SymName(i)))
  1022  	}
  1023  	if v {
  1024  		l.attrExternal.Set(l.extIndex(i))
  1025  	} else {
  1026  		l.attrExternal.Unset(l.extIndex(i))
  1027  	}
  1028  }
  1029  
  1030  // AttrSpecial returns true for a symbols that do not have their
  1031  // address (i.e. Value) computed by the usual mechanism of
  1032  // data.go:dodata() & data.go:address().
  1033  func (l *Loader) AttrSpecial(i Sym) bool {
  1034  	return l.attrSpecial.Has(i)
  1035  }
  1036  
  1037  // SetAttrSpecial sets the "special" property for a symbol (see
  1038  // AttrSpecial).
  1039  func (l *Loader) SetAttrSpecial(i Sym, v bool) {
  1040  	if v {
  1041  		l.attrSpecial.Set(i)
  1042  	} else {
  1043  		l.attrSpecial.Unset(i)
  1044  	}
  1045  }
  1046  
  1047  // AttrCgoExportDynamic returns true for a symbol that has been
  1048  // specially marked via the "cgo_export_dynamic" compiler directive
  1049  // written by cgo (in response to //export directives in the source).
  1050  func (l *Loader) AttrCgoExportDynamic(i Sym) bool {
  1051  	_, ok := l.attrCgoExportDynamic[i]
  1052  	return ok
  1053  }
  1054  
  1055  // SetAttrCgoExportDynamic sets the "cgo_export_dynamic" for a symbol
  1056  // (see AttrCgoExportDynamic).
  1057  func (l *Loader) SetAttrCgoExportDynamic(i Sym, v bool) {
  1058  	if v {
  1059  		l.attrCgoExportDynamic[i] = struct{}{}
  1060  	} else {
  1061  		delete(l.attrCgoExportDynamic, i)
  1062  	}
  1063  }
  1064  
  1065  // ForAllCgoExportDynamic calls f for every symbol that has been
  1066  // marked with the "cgo_export_dynamic" compiler directive.
  1067  func (l *Loader) ForAllCgoExportDynamic(f func(Sym)) {
  1068  	for s := range l.attrCgoExportDynamic {
  1069  		f(s)
  1070  	}
  1071  }
  1072  
  1073  // AttrCgoExportStatic returns true for a symbol that has been
  1074  // specially marked via the "cgo_export_static" directive
  1075  // written by cgo.
  1076  func (l *Loader) AttrCgoExportStatic(i Sym) bool {
  1077  	_, ok := l.attrCgoExportStatic[i]
  1078  	return ok
  1079  }
  1080  
  1081  // SetAttrCgoExportStatic sets the "cgo_export_static" for a symbol
  1082  // (see AttrCgoExportStatic).
  1083  func (l *Loader) SetAttrCgoExportStatic(i Sym, v bool) {
  1084  	if v {
  1085  		l.attrCgoExportStatic[i] = struct{}{}
  1086  	} else {
  1087  		delete(l.attrCgoExportStatic, i)
  1088  	}
  1089  }
  1090  
  1091  // IsGeneratedSym returns true if a symbol's been previously marked as a
  1092  // generator symbol through the SetIsGeneratedSym. The functions for generator
  1093  // symbols are kept in the Link context.
  1094  func (l *Loader) IsGeneratedSym(i Sym) bool {
  1095  	if !l.IsExternal(i) {
  1096  		return false
  1097  	}
  1098  	return l.generatedSyms.Has(l.extIndex(i))
  1099  }
  1100  
  1101  // SetIsGeneratedSym marks symbols as generated symbols. Data shouldn't be
  1102  // stored in generated symbols, and a function is registered and called for
  1103  // each of these symbols.
  1104  func (l *Loader) SetIsGeneratedSym(i Sym, v bool) {
  1105  	if !l.IsExternal(i) {
  1106  		panic("only external symbols can be generated")
  1107  	}
  1108  	if v {
  1109  		l.generatedSyms.Set(l.extIndex(i))
  1110  	} else {
  1111  		l.generatedSyms.Unset(l.extIndex(i))
  1112  	}
  1113  }
  1114  
  1115  func (l *Loader) AttrCgoExport(i Sym) bool {
  1116  	return l.AttrCgoExportDynamic(i) || l.AttrCgoExportStatic(i)
  1117  }
  1118  
  1119  // AttrReadOnly returns true for a symbol whose underlying data
  1120  // is stored via a read-only mmap.
  1121  func (l *Loader) AttrReadOnly(i Sym) bool {
  1122  	if v, ok := l.attrReadOnly[i]; ok {
  1123  		return v
  1124  	}
  1125  	if l.IsExternal(i) {
  1126  		pp := l.getPayload(i)
  1127  		if pp.objidx != 0 {
  1128  			return l.objs[pp.objidx].ReadOnly()
  1129  		}
  1130  		return false
  1131  	}
  1132  	r, _ := l.toLocal(i)
  1133  	return r.ReadOnly()
  1134  }
  1135  
  1136  // SetAttrReadOnly sets the "data is read only" property for a symbol
  1137  // (see AttrReadOnly).
  1138  func (l *Loader) SetAttrReadOnly(i Sym, v bool) {
  1139  	l.attrReadOnly[i] = v
  1140  }
  1141  
  1142  // AttrSubSymbol returns true for symbols that are listed as a
  1143  // sub-symbol of some other outer symbol. The sub/outer mechanism is
  1144  // used when loading host objects (sections from the host object
  1145  // become regular linker symbols and symbols go on the Sub list of
  1146  // their section) and for constructing the global offset table when
  1147  // internally linking a dynamic executable.
  1148  //
  1149  // Note that in later stages of the linker, we set Outer(S) to some
  1150  // container symbol C, but don't set Sub(C). Thus we have two
  1151  // distinct scenarios:
  1152  //
  1153  // - Outer symbol covers the address ranges of its sub-symbols.
  1154  //   Outer.Sub is set in this case.
  1155  // - Outer symbol doesn't cover the address ranges. It is zero-sized
  1156  //   and doesn't have sub-symbols. In the case, the inner symbol is
  1157  //   not actually a "SubSymbol". (Tricky!)
  1158  //
  1159  // This method returns TRUE only for sub-symbols in the first scenario.
  1160  //
  1161  // FIXME: would be better to do away with this and have a better way
  1162  // to represent container symbols.
  1163  
  1164  func (l *Loader) AttrSubSymbol(i Sym) bool {
  1165  	// we don't explicitly store this attribute any more -- return
  1166  	// a value based on the sub-symbol setting.
  1167  	o := l.OuterSym(i)
  1168  	if o == 0 {
  1169  		return false
  1170  	}
  1171  	return l.SubSym(o) != 0
  1172  }
  1173  
  1174  // Note that we don't have a 'SetAttrSubSymbol' method in the loader;
  1175  // clients should instead use the AddInteriorSym method to establish
  1176  // containment relationships for host object symbols.
  1177  
  1178  // Returns whether the i-th symbol has ReflectMethod attribute set.
  1179  func (l *Loader) IsReflectMethod(i Sym) bool {
  1180  	return l.SymAttr(i)&goobj.SymFlagReflectMethod != 0
  1181  }
  1182  
  1183  // Returns whether the i-th symbol is nosplit.
  1184  func (l *Loader) IsNoSplit(i Sym) bool {
  1185  	return l.SymAttr(i)&goobj.SymFlagNoSplit != 0
  1186  }
  1187  
  1188  // Returns whether this is a Go type symbol.
  1189  func (l *Loader) IsGoType(i Sym) bool {
  1190  	return l.SymAttr(i)&goobj.SymFlagGoType != 0
  1191  }
  1192  
  1193  // Returns whether this symbol should be included in typelink.
  1194  func (l *Loader) IsTypelink(i Sym) bool {
  1195  	return l.SymAttr(i)&goobj.SymFlagTypelink != 0
  1196  }
  1197  
  1198  // Returns whether this symbol is an itab symbol.
  1199  func (l *Loader) IsItab(i Sym) bool {
  1200  	if l.IsExternal(i) {
  1201  		return false
  1202  	}
  1203  	r, li := l.toLocal(i)
  1204  	return r.Sym(li).IsItab()
  1205  }
  1206  
  1207  // Returns whether this symbol is a dictionary symbol.
  1208  func (l *Loader) IsDict(i Sym) bool {
  1209  	if l.IsExternal(i) {
  1210  		return false
  1211  	}
  1212  	r, li := l.toLocal(i)
  1213  	return r.Sym(li).IsDict()
  1214  }
  1215  
  1216  // Returns whether this symbol is a compiler-generated package init func.
  1217  func (l *Loader) IsPkgInit(i Sym) bool {
  1218  	if l.IsExternal(i) {
  1219  		return false
  1220  	}
  1221  	r, li := l.toLocal(i)
  1222  	return r.Sym(li).IsPkgInit()
  1223  }
  1224  
  1225  // Return whether this is a trampoline of a deferreturn call.
  1226  func (l *Loader) IsDeferReturnTramp(i Sym) bool {
  1227  	return l.deferReturnTramp[i]
  1228  }
  1229  
  1230  // Set that i is a trampoline of a deferreturn call.
  1231  func (l *Loader) SetIsDeferReturnTramp(i Sym, v bool) {
  1232  	l.deferReturnTramp[i] = v
  1233  }
  1234  
  1235  // growValues grows the slice used to store symbol values.
  1236  func (l *Loader) growValues(reqLen int) {
  1237  	curLen := len(l.values)
  1238  	if reqLen > curLen {
  1239  		l.values = append(l.values, make([]int64, reqLen+1-curLen)...)
  1240  	}
  1241  }
  1242  
  1243  // SymValue returns the value of the i-th symbol. i is global index.
  1244  func (l *Loader) SymValue(i Sym) int64 {
  1245  	return l.values[i]
  1246  }
  1247  
  1248  // SetSymValue sets the value of the i-th symbol. i is global index.
  1249  func (l *Loader) SetSymValue(i Sym, val int64) {
  1250  	l.values[i] = val
  1251  }
  1252  
  1253  // AddToSymValue adds to the value of the i-th symbol. i is the global index.
  1254  func (l *Loader) AddToSymValue(i Sym, val int64) {
  1255  	l.values[i] += val
  1256  }
  1257  
  1258  // Returns the symbol content of the i-th symbol. i is global index.
  1259  func (l *Loader) Data(i Sym) []byte {
  1260  	if l.IsExternal(i) {
  1261  		pp := l.getPayload(i)
  1262  		if pp != nil {
  1263  			return pp.data
  1264  		}
  1265  		return nil
  1266  	}
  1267  	r, li := l.toLocal(i)
  1268  	return r.Data(li)
  1269  }
  1270  
  1271  // Returns the symbol content of the i-th symbol as a string. i is global index.
  1272  func (l *Loader) DataString(i Sym) string {
  1273  	if l.IsExternal(i) {
  1274  		pp := l.getPayload(i)
  1275  		return string(pp.data)
  1276  	}
  1277  	r, li := l.toLocal(i)
  1278  	return r.DataString(li)
  1279  }
  1280  
  1281  // FreeData clears the symbol data of an external symbol, allowing the memory
  1282  // to be freed earlier. No-op for non-external symbols.
  1283  // i is global index.
  1284  func (l *Loader) FreeData(i Sym) {
  1285  	if l.IsExternal(i) {
  1286  		pp := l.getPayload(i)
  1287  		if pp != nil {
  1288  			pp.data = nil
  1289  		}
  1290  	}
  1291  }
  1292  
  1293  // SymAlign returns the alignment for a symbol.
  1294  func (l *Loader) SymAlign(i Sym) int32 {
  1295  	if int(i) >= len(l.align) {
  1296  		// align is extended lazily -- it the sym in question is
  1297  		// outside the range of the existing slice, then we assume its
  1298  		// alignment has not yet been set.
  1299  		return 0
  1300  	}
  1301  	// TODO: would it make sense to return an arch-specific
  1302  	// alignment depending on section type? E.g. STEXT => 32,
  1303  	// SDATA => 1, etc?
  1304  	abits := l.align[i]
  1305  	if abits == 0 {
  1306  		return 0
  1307  	}
  1308  	return int32(1 << (abits - 1))
  1309  }
  1310  
  1311  // SetSymAlign sets the alignment for a symbol.
  1312  func (l *Loader) SetSymAlign(i Sym, align int32) {
  1313  	// Reject nonsense alignments.
  1314  	if align < 0 || align&(align-1) != 0 {
  1315  		panic("bad alignment value")
  1316  	}
  1317  	if int(i) >= len(l.align) {
  1318  		l.align = append(l.align, make([]uint8, l.NSym()-len(l.align))...)
  1319  	}
  1320  	if align == 0 {
  1321  		l.align[i] = 0
  1322  	}
  1323  	l.align[i] = uint8(bits.Len32(uint32(align)))
  1324  }
  1325  
  1326  // SymSect returns the section of the i-th symbol. i is global index.
  1327  func (l *Loader) SymSect(i Sym) *sym.Section {
  1328  	if int(i) >= len(l.symSects) {
  1329  		// symSects is extended lazily -- it the sym in question is
  1330  		// outside the range of the existing slice, then we assume its
  1331  		// section has not yet been set.
  1332  		return nil
  1333  	}
  1334  	return l.sects[l.symSects[i]]
  1335  }
  1336  
  1337  // SetSymSect sets the section of the i-th symbol. i is global index.
  1338  func (l *Loader) SetSymSect(i Sym, sect *sym.Section) {
  1339  	if int(i) >= len(l.symSects) {
  1340  		l.symSects = append(l.symSects, make([]uint16, l.NSym()-len(l.symSects))...)
  1341  	}
  1342  	l.symSects[i] = sect.Index
  1343  }
  1344  
  1345  // NewSection creates a new (output) section.
  1346  func (l *Loader) NewSection() *sym.Section {
  1347  	sect := new(sym.Section)
  1348  	idx := len(l.sects)
  1349  	if idx != int(uint16(idx)) {
  1350  		panic("too many sections created")
  1351  	}
  1352  	sect.Index = uint16(idx)
  1353  	l.sects = append(l.sects, sect)
  1354  	return sect
  1355  }
  1356  
  1357  // SymDynimplib returns the "dynimplib" attribute for the specified
  1358  // symbol, making up a portion of the info for a symbol specified
  1359  // on a "cgo_import_dynamic" compiler directive.
  1360  func (l *Loader) SymDynimplib(i Sym) string {
  1361  	return l.dynimplib[i]
  1362  }
  1363  
  1364  // SetSymDynimplib sets the "dynimplib" attribute for a symbol.
  1365  func (l *Loader) SetSymDynimplib(i Sym, value string) {
  1366  	// reject bad symbols
  1367  	if i >= Sym(len(l.objSyms)) || i == 0 {
  1368  		panic("bad symbol index in SetDynimplib")
  1369  	}
  1370  	if value == "" {
  1371  		delete(l.dynimplib, i)
  1372  	} else {
  1373  		l.dynimplib[i] = value
  1374  	}
  1375  }
  1376  
  1377  // SymDynimpvers returns the "dynimpvers" attribute for the specified
  1378  // symbol, making up a portion of the info for a symbol specified
  1379  // on a "cgo_import_dynamic" compiler directive.
  1380  func (l *Loader) SymDynimpvers(i Sym) string {
  1381  	return l.dynimpvers[i]
  1382  }
  1383  
  1384  // SetSymDynimpvers sets the "dynimpvers" attribute for a symbol.
  1385  func (l *Loader) SetSymDynimpvers(i Sym, value string) {
  1386  	// reject bad symbols
  1387  	if i >= Sym(len(l.objSyms)) || i == 0 {
  1388  		panic("bad symbol index in SetDynimpvers")
  1389  	}
  1390  	if value == "" {
  1391  		delete(l.dynimpvers, i)
  1392  	} else {
  1393  		l.dynimpvers[i] = value
  1394  	}
  1395  }
  1396  
  1397  // SymExtname returns the "extname" value for the specified
  1398  // symbol.
  1399  func (l *Loader) SymExtname(i Sym) string {
  1400  	if s, ok := l.extname[i]; ok {
  1401  		return s
  1402  	}
  1403  	return l.SymName(i)
  1404  }
  1405  
  1406  // SetSymExtname sets the  "extname" attribute for a symbol.
  1407  func (l *Loader) SetSymExtname(i Sym, value string) {
  1408  	// reject bad symbols
  1409  	if i >= Sym(len(l.objSyms)) || i == 0 {
  1410  		panic("bad symbol index in SetExtname")
  1411  	}
  1412  	if value == "" {
  1413  		delete(l.extname, i)
  1414  	} else {
  1415  		l.extname[i] = value
  1416  	}
  1417  }
  1418  
  1419  // SymElfType returns the previously recorded ELF type for a symbol
  1420  // (used only for symbols read from shared libraries by ldshlibsyms).
  1421  // It is not set for symbols defined by the packages being linked or
  1422  // by symbols read by ldelf (and so is left as elf.STT_NOTYPE).
  1423  func (l *Loader) SymElfType(i Sym) elf.SymType {
  1424  	if et, ok := l.elfType[i]; ok {
  1425  		return et
  1426  	}
  1427  	return elf.STT_NOTYPE
  1428  }
  1429  
  1430  // SetSymElfType sets the elf type attribute for a symbol.
  1431  func (l *Loader) SetSymElfType(i Sym, et elf.SymType) {
  1432  	// reject bad symbols
  1433  	if i >= Sym(len(l.objSyms)) || i == 0 {
  1434  		panic("bad symbol index in SetSymElfType")
  1435  	}
  1436  	if et == elf.STT_NOTYPE {
  1437  		delete(l.elfType, i)
  1438  	} else {
  1439  		l.elfType[i] = et
  1440  	}
  1441  }
  1442  
  1443  // SymElfSym returns the ELF symbol index for a given loader
  1444  // symbol, assigned during ELF symtab generation.
  1445  func (l *Loader) SymElfSym(i Sym) int32 {
  1446  	return l.elfSym[i]
  1447  }
  1448  
  1449  // SetSymElfSym sets the elf symbol index for a symbol.
  1450  func (l *Loader) SetSymElfSym(i Sym, es int32) {
  1451  	if i == 0 {
  1452  		panic("bad sym index")
  1453  	}
  1454  	if es == 0 {
  1455  		delete(l.elfSym, i)
  1456  	} else {
  1457  		l.elfSym[i] = es
  1458  	}
  1459  }
  1460  
  1461  // SymLocalElfSym returns the "local" ELF symbol index for a given loader
  1462  // symbol, assigned during ELF symtab generation.
  1463  func (l *Loader) SymLocalElfSym(i Sym) int32 {
  1464  	return l.localElfSym[i]
  1465  }
  1466  
  1467  // SetSymLocalElfSym sets the "local" elf symbol index for a symbol.
  1468  func (l *Loader) SetSymLocalElfSym(i Sym, es int32) {
  1469  	if i == 0 {
  1470  		panic("bad sym index")
  1471  	}
  1472  	if es == 0 {
  1473  		delete(l.localElfSym, i)
  1474  	} else {
  1475  		l.localElfSym[i] = es
  1476  	}
  1477  }
  1478  
  1479  // SymPlt returns the PLT offset of symbol s.
  1480  func (l *Loader) SymPlt(s Sym) int32 {
  1481  	if v, ok := l.plt[s]; ok {
  1482  		return v
  1483  	}
  1484  	return -1
  1485  }
  1486  
  1487  // SetPlt sets the PLT offset of symbol i.
  1488  func (l *Loader) SetPlt(i Sym, v int32) {
  1489  	if i >= Sym(len(l.objSyms)) || i == 0 {
  1490  		panic("bad symbol for SetPlt")
  1491  	}
  1492  	if v == -1 {
  1493  		delete(l.plt, i)
  1494  	} else {
  1495  		l.plt[i] = v
  1496  	}
  1497  }
  1498  
  1499  // SymGot returns the GOT offset of symbol s.
  1500  func (l *Loader) SymGot(s Sym) int32 {
  1501  	if v, ok := l.got[s]; ok {
  1502  		return v
  1503  	}
  1504  	return -1
  1505  }
  1506  
  1507  // SetGot sets the GOT offset of symbol i.
  1508  func (l *Loader) SetGot(i Sym, v int32) {
  1509  	if i >= Sym(len(l.objSyms)) || i == 0 {
  1510  		panic("bad symbol for SetGot")
  1511  	}
  1512  	if v == -1 {
  1513  		delete(l.got, i)
  1514  	} else {
  1515  		l.got[i] = v
  1516  	}
  1517  }
  1518  
  1519  // SymDynid returns the "dynid" property for the specified symbol.
  1520  func (l *Loader) SymDynid(i Sym) int32 {
  1521  	if s, ok := l.dynid[i]; ok {
  1522  		return s
  1523  	}
  1524  	return -1
  1525  }
  1526  
  1527  // SetSymDynid sets the "dynid" property for a symbol.
  1528  func (l *Loader) SetSymDynid(i Sym, val int32) {
  1529  	// reject bad symbols
  1530  	if i >= Sym(len(l.objSyms)) || i == 0 {
  1531  		panic("bad symbol index in SetSymDynid")
  1532  	}
  1533  	if val == -1 {
  1534  		delete(l.dynid, i)
  1535  	} else {
  1536  		l.dynid[i] = val
  1537  	}
  1538  }
  1539  
  1540  // DynidSyms returns the set of symbols for which dynID is set to an
  1541  // interesting (non-default) value. This is expected to be a fairly
  1542  // small set.
  1543  func (l *Loader) DynidSyms() []Sym {
  1544  	sl := make([]Sym, 0, len(l.dynid))
  1545  	for s := range l.dynid {
  1546  		sl = append(sl, s)
  1547  	}
  1548  	sort.Slice(sl, func(i, j int) bool { return sl[i] < sl[j] })
  1549  	return sl
  1550  }
  1551  
  1552  // SymGoType returns the 'Gotype' property for a given symbol (set by
  1553  // the Go compiler for variable symbols). This version relies on
  1554  // reading aux symbols for the target sym -- it could be that a faster
  1555  // approach would be to check for gotype during preload and copy the
  1556  // results in to a map (might want to try this at some point and see
  1557  // if it helps speed things up).
  1558  func (l *Loader) SymGoType(i Sym) Sym { return l.aux1(i, goobj.AuxGotype) }
  1559  
  1560  // SymUnit returns the compilation unit for a given symbol (which will
  1561  // typically be nil for external or linker-manufactured symbols).
  1562  func (l *Loader) SymUnit(i Sym) *sym.CompilationUnit {
  1563  	if l.IsExternal(i) {
  1564  		pp := l.getPayload(i)
  1565  		if pp.objidx != 0 {
  1566  			r := l.objs[pp.objidx]
  1567  			return r.unit
  1568  		}
  1569  		return nil
  1570  	}
  1571  	r, _ := l.toLocal(i)
  1572  	return r.unit
  1573  }
  1574  
  1575  // SymPkg returns the package where the symbol came from (for
  1576  // regular compiler-generated Go symbols), but in the case of
  1577  // building with "-linkshared" (when a symbol is read from a
  1578  // shared library), will hold the library name.
  1579  // NOTE: this corresponds to sym.Symbol.File field.
  1580  func (l *Loader) SymPkg(i Sym) string {
  1581  	if f, ok := l.symPkg[i]; ok {
  1582  		return f
  1583  	}
  1584  	if l.IsExternal(i) {
  1585  		pp := l.getPayload(i)
  1586  		if pp.objidx != 0 {
  1587  			r := l.objs[pp.objidx]
  1588  			return r.unit.Lib.Pkg
  1589  		}
  1590  		return ""
  1591  	}
  1592  	r, _ := l.toLocal(i)
  1593  	return r.unit.Lib.Pkg
  1594  }
  1595  
  1596  // SetSymPkg sets the package/library for a symbol. This is
  1597  // needed mainly for external symbols, specifically those imported
  1598  // from shared libraries.
  1599  func (l *Loader) SetSymPkg(i Sym, pkg string) {
  1600  	// reject bad symbols
  1601  	if i >= Sym(len(l.objSyms)) || i == 0 {
  1602  		panic("bad symbol index in SetSymPkg")
  1603  	}
  1604  	l.symPkg[i] = pkg
  1605  }
  1606  
  1607  // SymLocalentry returns an offset in bytes of the "local entry" of a symbol.
  1608  //
  1609  // On PPC64, a value of 1 indicates the symbol does not use or preserve a TOC
  1610  // pointer in R2, nor does it have a distinct local entry.
  1611  func (l *Loader) SymLocalentry(i Sym) uint8 {
  1612  	return l.localentry[i]
  1613  }
  1614  
  1615  // SetSymLocalentry sets the "local entry" offset attribute for a symbol.
  1616  func (l *Loader) SetSymLocalentry(i Sym, value uint8) {
  1617  	// reject bad symbols
  1618  	if i >= Sym(len(l.objSyms)) || i == 0 {
  1619  		panic("bad symbol index in SetSymLocalentry")
  1620  	}
  1621  	if value == 0 {
  1622  		delete(l.localentry, i)
  1623  	} else {
  1624  		l.localentry[i] = value
  1625  	}
  1626  }
  1627  
  1628  // Returns the number of aux symbols given a global index.
  1629  func (l *Loader) NAux(i Sym) int {
  1630  	if l.IsExternal(i) {
  1631  		return 0
  1632  	}
  1633  	r, li := l.toLocal(i)
  1634  	return r.NAux(li)
  1635  }
  1636  
  1637  // Returns the "handle" to the j-th aux symbol of the i-th symbol.
  1638  func (l *Loader) Aux(i Sym, j int) Aux {
  1639  	if l.IsExternal(i) {
  1640  		return Aux{}
  1641  	}
  1642  	r, li := l.toLocal(i)
  1643  	if j >= r.NAux(li) {
  1644  		return Aux{}
  1645  	}
  1646  	return Aux{r.Aux(li, j), r, l}
  1647  }
  1648  
  1649  // WasmImportSym returns the auxiliary WebAssembly import symbol associated with
  1650  // a given function symbol. The aux sym only exists for Go function stubs that
  1651  // have been annotated with the //go:wasmimport directive.  The aux sym
  1652  // contains the information necessary for the linker to add a WebAssembly
  1653  // import statement.
  1654  // (https://webassembly.github.io/spec/core/syntax/modules.html#imports)
  1655  func (l *Loader) WasmImportSym(fnSymIdx Sym) Sym {
  1656  	if !l.SymType(fnSymIdx).IsText() {
  1657  		log.Fatalf("error: non-function sym %d/%s t=%s passed to WasmImportSym", fnSymIdx, l.SymName(fnSymIdx), l.SymType(fnSymIdx).String())
  1658  	}
  1659  	return l.aux1(fnSymIdx, goobj.AuxWasmImport)
  1660  }
  1661  
  1662  func (l *Loader) WasmTypeSym(s Sym) Sym {
  1663  	return l.aux1(s, goobj.AuxWasmType)
  1664  }
  1665  
  1666  // SEHUnwindSym returns the auxiliary SEH unwind symbol associated with
  1667  // a given function symbol.
  1668  func (l *Loader) SEHUnwindSym(fnSymIdx Sym) Sym {
  1669  	if !l.SymType(fnSymIdx).IsText() {
  1670  		log.Fatalf("error: non-function sym %d/%s t=%s passed to SEHUnwindSym", fnSymIdx, l.SymName(fnSymIdx), l.SymType(fnSymIdx).String())
  1671  	}
  1672  
  1673  	return l.aux1(fnSymIdx, goobj.AuxSehUnwindInfo)
  1674  }
  1675  
  1676  // GetFuncDwarfAuxSyms collects and returns the auxiliary DWARF
  1677  // symbols associated with a given function symbol.  Prior to the
  1678  // introduction of the loader, this was done purely using name
  1679  // lookups, e.f. for function with name XYZ we would then look up
  1680  // go.info.XYZ, etc.
  1681  func (l *Loader) GetFuncDwarfAuxSyms(fnSymIdx Sym) (auxDwarfInfo, auxDwarfLoc, auxDwarfRanges, auxDwarfLines Sym) {
  1682  	if !l.SymType(fnSymIdx).IsText() {
  1683  		log.Fatalf("error: non-function sym %d/%s t=%s passed to GetFuncDwarfAuxSyms", fnSymIdx, l.SymName(fnSymIdx), l.SymType(fnSymIdx).String())
  1684  	}
  1685  	r, auxs := l.auxs(fnSymIdx)
  1686  
  1687  	for i := range auxs {
  1688  		a := &auxs[i]
  1689  		switch a.Type() {
  1690  		case goobj.AuxDwarfInfo:
  1691  			auxDwarfInfo = l.resolve(r, a.Sym())
  1692  			if l.SymType(auxDwarfInfo) != sym.SDWARFFCN {
  1693  				panic("aux dwarf info sym with wrong type")
  1694  			}
  1695  		case goobj.AuxDwarfLoc:
  1696  			auxDwarfLoc = l.resolve(r, a.Sym())
  1697  			if l.SymType(auxDwarfLoc) != sym.SDWARFLOC {
  1698  				panic("aux dwarf loc sym with wrong type")
  1699  			}
  1700  		case goobj.AuxDwarfRanges:
  1701  			auxDwarfRanges = l.resolve(r, a.Sym())
  1702  			if l.SymType(auxDwarfRanges) != sym.SDWARFRANGE {
  1703  				panic("aux dwarf ranges sym with wrong type")
  1704  			}
  1705  		case goobj.AuxDwarfLines:
  1706  			auxDwarfLines = l.resolve(r, a.Sym())
  1707  			if l.SymType(auxDwarfLines) != sym.SDWARFLINES {
  1708  				panic("aux dwarf lines sym with wrong type")
  1709  			}
  1710  		}
  1711  	}
  1712  	return
  1713  }
  1714  
  1715  func (l *Loader) GetVarDwarfAuxSym(i Sym) Sym {
  1716  	aux := l.aux1(i, goobj.AuxDwarfInfo)
  1717  	if aux != 0 && l.SymType(aux) != sym.SDWARFVAR {
  1718  		fmt.Println(l.SymName(i), l.SymType(i), l.SymType(aux), sym.SDWARFVAR)
  1719  		panic("aux dwarf info sym with wrong type")
  1720  	}
  1721  	return aux
  1722  }
  1723  
  1724  // AddInteriorSym sets up 'interior' as an interior symbol of
  1725  // container/payload symbol 'container'. An interior symbol does not
  1726  // itself have data, but gives a name to a subrange of the data in its
  1727  // container symbol. The container itself may or may not have a name.
  1728  // This method is intended primarily for use in the host object
  1729  // loaders, to capture the semantics of symbols and sections in an
  1730  // object file. When reading a host object file, we'll typically
  1731  // encounter a static section symbol (ex: ".text") containing content
  1732  // for a collection of functions, then a series of ELF (or macho, etc)
  1733  // symbol table entries each of which points into a sub-section
  1734  // (offset and length) of its corresponding container symbol. Within
  1735  // the go linker we create a loader.Sym for the container (which is
  1736  // expected to have the actual content/payload) and then a set of
  1737  // interior loader.Sym's that point into a portion of the container.
  1738  func (l *Loader) AddInteriorSym(container Sym, interior Sym) {
  1739  	// Container symbols are expected to have content/data.
  1740  	// NB: this restriction may turn out to be too strict (it's possible
  1741  	// to imagine a zero-sized container with an interior symbol pointing
  1742  	// into it); it's ok to relax or remove it if we counter an
  1743  	// oddball host object that triggers this.
  1744  	if l.SymSize(container) == 0 && len(l.Data(container)) == 0 {
  1745  		panic("unexpected empty container symbol")
  1746  	}
  1747  	// The interior symbols for a container are not expected to have
  1748  	// content/data or relocations.
  1749  	if len(l.Data(interior)) != 0 {
  1750  		panic("unexpected non-empty interior symbol")
  1751  	}
  1752  	// Interior symbol is expected to be in the symbol table.
  1753  	if l.AttrNotInSymbolTable(interior) {
  1754  		panic("interior symbol must be in symtab")
  1755  	}
  1756  	// Only a single level of containment is allowed.
  1757  	if l.OuterSym(container) != 0 {
  1758  		panic("outer has outer itself")
  1759  	}
  1760  	// Interior sym should not already have a sibling.
  1761  	if l.SubSym(interior) != 0 {
  1762  		panic("sub set for subsym")
  1763  	}
  1764  	// Interior sym should not already point at a container.
  1765  	if l.OuterSym(interior) != 0 {
  1766  		panic("outer already set for subsym")
  1767  	}
  1768  	l.sub[interior] = l.sub[container]
  1769  	l.sub[container] = interior
  1770  	l.outer[interior] = container
  1771  }
  1772  
  1773  // OuterSym gets the outer/container symbol.
  1774  func (l *Loader) OuterSym(i Sym) Sym {
  1775  	return l.outer[i]
  1776  }
  1777  
  1778  // SubSym gets the subsymbol for host object loaded symbols.
  1779  func (l *Loader) SubSym(i Sym) Sym {
  1780  	return l.sub[i]
  1781  }
  1782  
  1783  // growOuter grows the slice used to store outer symbol.
  1784  func (l *Loader) growOuter(reqLen int) {
  1785  	curLen := len(l.outer)
  1786  	if reqLen > curLen {
  1787  		l.outer = append(l.outer, make([]Sym, reqLen-curLen)...)
  1788  	}
  1789  }
  1790  
  1791  // SetCarrierSym declares that 'c' is the carrier or container symbol
  1792  // for 's'. Carrier symbols are used in the linker to as a container
  1793  // for a collection of sub-symbols where the content of the
  1794  // sub-symbols is effectively concatenated to form the content of the
  1795  // carrier. The carrier is given a name in the output symbol table
  1796  // while the sub-symbol names are not. For example, the Go compiler
  1797  // emits named string symbols (type SGOSTRING) when compiling a
  1798  // package; after being deduplicated, these symbols are collected into
  1799  // a single unit by assigning them a new carrier symbol named
  1800  // "go:string.*" (which appears in the final symbol table for the
  1801  // output load module).
  1802  func (l *Loader) SetCarrierSym(s Sym, c Sym) {
  1803  	if c == 0 {
  1804  		panic("invalid carrier in SetCarrierSym")
  1805  	}
  1806  	if s == 0 {
  1807  		panic("invalid sub-symbol in SetCarrierSym")
  1808  	}
  1809  	// Carrier symbols are not expected to have content/data. It is
  1810  	// ok for them to have non-zero size (to allow for use of generator
  1811  	// symbols).
  1812  	if len(l.Data(c)) != 0 {
  1813  		panic("unexpected non-empty carrier symbol")
  1814  	}
  1815  	l.outer[s] = c
  1816  	// relocsym's foldSubSymbolOffset requires that we only
  1817  	// have a single level of containment-- enforce here.
  1818  	if l.outer[c] != 0 {
  1819  		panic("invalid nested carrier sym")
  1820  	}
  1821  }
  1822  
  1823  // Initialize Reachable bitmap and its siblings for running deadcode pass.
  1824  func (l *Loader) InitReachable() {
  1825  	l.growAttrBitmaps(l.NSym() + 1)
  1826  }
  1827  
  1828  type symWithVal struct {
  1829  	s Sym
  1830  	v int64
  1831  }
  1832  type bySymValue []symWithVal
  1833  
  1834  func (s bySymValue) Len() int           { return len(s) }
  1835  func (s bySymValue) Swap(i, j int)      { s[i], s[j] = s[j], s[i] }
  1836  func (s bySymValue) Less(i, j int) bool { return s[i].v < s[j].v }
  1837  
  1838  // SortSub walks through the sub-symbols for 's' and sorts them
  1839  // in place by increasing value. Return value is the new
  1840  // sub symbol for the specified outer symbol.
  1841  func (l *Loader) SortSub(s Sym) Sym {
  1842  
  1843  	if s == 0 || l.sub[s] == 0 {
  1844  		return s
  1845  	}
  1846  
  1847  	// Sort symbols using a slice first. Use a stable sort on the off
  1848  	// chance that there's more than once symbol with the same value,
  1849  	// so as to preserve reproducible builds.
  1850  	sl := []symWithVal{}
  1851  	for ss := l.sub[s]; ss != 0; ss = l.sub[ss] {
  1852  		sl = append(sl, symWithVal{s: ss, v: l.SymValue(ss)})
  1853  	}
  1854  	sort.Stable(bySymValue(sl))
  1855  
  1856  	// Then apply any changes needed to the sub map.
  1857  	ns := Sym(0)
  1858  	for i := len(sl) - 1; i >= 0; i-- {
  1859  		s := sl[i].s
  1860  		l.sub[s] = ns
  1861  		ns = s
  1862  	}
  1863  
  1864  	// Update sub for outer symbol, then return
  1865  	l.sub[s] = sl[0].s
  1866  	return sl[0].s
  1867  }
  1868  
  1869  // SortSyms sorts a list of symbols by their value.
  1870  func (l *Loader) SortSyms(ss []Sym) {
  1871  	sort.SliceStable(ss, func(i, j int) bool { return l.SymValue(ss[i]) < l.SymValue(ss[j]) })
  1872  }
  1873  
  1874  // Insure that reachable bitmap and its siblings have enough size.
  1875  func (l *Loader) growAttrBitmaps(reqLen int) {
  1876  	if reqLen > l.attrReachable.Len() {
  1877  		// These are indexed by global symbol
  1878  		l.attrReachable = growBitmap(reqLen, l.attrReachable)
  1879  		l.attrOnList = growBitmap(reqLen, l.attrOnList)
  1880  		l.attrLocal = growBitmap(reqLen, l.attrLocal)
  1881  		l.attrNotInSymbolTable = growBitmap(reqLen, l.attrNotInSymbolTable)
  1882  		l.attrUsedInIface = growBitmap(reqLen, l.attrUsedInIface)
  1883  		l.attrSpecial = growBitmap(reqLen, l.attrSpecial)
  1884  	}
  1885  	l.growExtAttrBitmaps()
  1886  }
  1887  
  1888  func (l *Loader) growExtAttrBitmaps() {
  1889  	// These are indexed by external symbol index (e.g. l.extIndex(i))
  1890  	extReqLen := len(l.payloads)
  1891  	if extReqLen > l.attrVisibilityHidden.Len() {
  1892  		l.attrVisibilityHidden = growBitmap(extReqLen, l.attrVisibilityHidden)
  1893  		l.attrDuplicateOK = growBitmap(extReqLen, l.attrDuplicateOK)
  1894  		l.attrShared = growBitmap(extReqLen, l.attrShared)
  1895  		l.attrExternal = growBitmap(extReqLen, l.attrExternal)
  1896  		l.generatedSyms = growBitmap(extReqLen, l.generatedSyms)
  1897  	}
  1898  }
  1899  
  1900  func (relocs *Relocs) Count() int { return len(relocs.rs) }
  1901  
  1902  // At returns the j-th reloc for a global symbol.
  1903  func (relocs *Relocs) At(j int) Reloc {
  1904  	if relocs.l.isExtReader(relocs.r) {
  1905  		return Reloc{&relocs.rs[j], relocs.r, relocs.l}
  1906  	}
  1907  	return Reloc{&relocs.rs[j], relocs.r, relocs.l}
  1908  }
  1909  
  1910  // Relocs returns a Relocs object for the given global sym.
  1911  func (l *Loader) Relocs(i Sym) Relocs {
  1912  	r, li := l.toLocal(i)
  1913  	if r == nil {
  1914  		panic(fmt.Sprintf("trying to get oreader for invalid sym %d\n\n", i))
  1915  	}
  1916  	return l.relocs(r, li)
  1917  }
  1918  
  1919  // relocs returns a Relocs object given a local sym index and reader.
  1920  func (l *Loader) relocs(r *oReader, li uint32) Relocs {
  1921  	var rs []goobj.Reloc
  1922  	if l.isExtReader(r) {
  1923  		pp := l.payloads[li]
  1924  		rs = pp.relocs
  1925  	} else {
  1926  		rs = r.Relocs(li)
  1927  	}
  1928  	return Relocs{
  1929  		rs: rs,
  1930  		li: li,
  1931  		r:  r,
  1932  		l:  l,
  1933  	}
  1934  }
  1935  
  1936  func (l *Loader) auxs(i Sym) (*oReader, []goobj.Aux) {
  1937  	if l.IsExternal(i) {
  1938  		pp := l.getPayload(i)
  1939  		return l.objs[pp.objidx], pp.auxs
  1940  	} else {
  1941  		r, li := l.toLocal(i)
  1942  		return r, r.Auxs(li)
  1943  	}
  1944  }
  1945  
  1946  // Returns a specific aux symbol of type t for symbol i.
  1947  func (l *Loader) aux1(i Sym, t uint8) Sym {
  1948  	r, auxs := l.auxs(i)
  1949  	for j := range auxs {
  1950  		a := &auxs[j]
  1951  		if a.Type() == t {
  1952  			return l.resolve(r, a.Sym())
  1953  		}
  1954  	}
  1955  	return 0
  1956  }
  1957  
  1958  func (l *Loader) Pcsp(i Sym) Sym { return l.aux1(i, goobj.AuxPcsp) }
  1959  
  1960  // Returns all aux symbols of per-PC data for symbol i.
  1961  // tmp is a scratch space for the pcdata slice.
  1962  func (l *Loader) PcdataAuxs(i Sym, tmp []Sym) (pcsp, pcfile, pcline, pcinline Sym, pcdata []Sym) {
  1963  	pcdata = tmp[:0]
  1964  	r, auxs := l.auxs(i)
  1965  	for j := range auxs {
  1966  		a := &auxs[j]
  1967  		switch a.Type() {
  1968  		case goobj.AuxPcsp:
  1969  			pcsp = l.resolve(r, a.Sym())
  1970  		case goobj.AuxPcline:
  1971  			pcline = l.resolve(r, a.Sym())
  1972  		case goobj.AuxPcfile:
  1973  			pcfile = l.resolve(r, a.Sym())
  1974  		case goobj.AuxPcinline:
  1975  			pcinline = l.resolve(r, a.Sym())
  1976  		case goobj.AuxPcdata:
  1977  			pcdata = append(pcdata, l.resolve(r, a.Sym()))
  1978  		}
  1979  	}
  1980  	return
  1981  }
  1982  
  1983  // Returns the number of pcdata for symbol i.
  1984  func (l *Loader) NumPcdata(i Sym) int {
  1985  	n := 0
  1986  	_, auxs := l.auxs(i)
  1987  	for j := range auxs {
  1988  		a := &auxs[j]
  1989  		if a.Type() == goobj.AuxPcdata {
  1990  			n++
  1991  		}
  1992  	}
  1993  	return n
  1994  }
  1995  
  1996  // Returns all funcdata symbols of symbol i.
  1997  // tmp is a scratch space.
  1998  func (l *Loader) Funcdata(i Sym, tmp []Sym) []Sym {
  1999  	fd := tmp[:0]
  2000  	r, auxs := l.auxs(i)
  2001  	for j := range auxs {
  2002  		a := &auxs[j]
  2003  		if a.Type() == goobj.AuxFuncdata {
  2004  			fd = append(fd, l.resolve(r, a.Sym()))
  2005  		}
  2006  	}
  2007  	return fd
  2008  }
  2009  
  2010  // Returns the number of funcdata for symbol i.
  2011  func (l *Loader) NumFuncdata(i Sym) int {
  2012  	n := 0
  2013  	_, auxs := l.auxs(i)
  2014  	for j := range auxs {
  2015  		a := &auxs[j]
  2016  		if a.Type() == goobj.AuxFuncdata {
  2017  			n++
  2018  		}
  2019  	}
  2020  	return n
  2021  }
  2022  
  2023  // FuncInfo provides hooks to access goobj.FuncInfo in the objects.
  2024  type FuncInfo struct {
  2025  	l       *Loader
  2026  	r       *oReader
  2027  	data    []byte
  2028  	lengths goobj.FuncInfoLengths
  2029  }
  2030  
  2031  func (fi *FuncInfo) Valid() bool { return fi.r != nil }
  2032  
  2033  func (fi *FuncInfo) Args() int {
  2034  	return int((*goobj.FuncInfo)(nil).ReadArgs(fi.data))
  2035  }
  2036  
  2037  func (fi *FuncInfo) Locals() int {
  2038  	return int((*goobj.FuncInfo)(nil).ReadLocals(fi.data))
  2039  }
  2040  
  2041  func (fi *FuncInfo) FuncID() abi.FuncID {
  2042  	return (*goobj.FuncInfo)(nil).ReadFuncID(fi.data)
  2043  }
  2044  
  2045  func (fi *FuncInfo) FuncFlag() abi.FuncFlag {
  2046  	return (*goobj.FuncInfo)(nil).ReadFuncFlag(fi.data)
  2047  }
  2048  
  2049  func (fi *FuncInfo) StartLine() int32 {
  2050  	return (*goobj.FuncInfo)(nil).ReadStartLine(fi.data)
  2051  }
  2052  
  2053  // Preload has to be called prior to invoking the various methods
  2054  // below related to pcdata, funcdataoff, files, and inltree nodes.
  2055  func (fi *FuncInfo) Preload() {
  2056  	fi.lengths = (*goobj.FuncInfo)(nil).ReadFuncInfoLengths(fi.data)
  2057  }
  2058  
  2059  func (fi *FuncInfo) NumFile() uint32 {
  2060  	if !fi.lengths.Initialized {
  2061  		panic("need to call Preload first")
  2062  	}
  2063  	return fi.lengths.NumFile
  2064  }
  2065  
  2066  func (fi *FuncInfo) File(k int) goobj.CUFileIndex {
  2067  	if !fi.lengths.Initialized {
  2068  		panic("need to call Preload first")
  2069  	}
  2070  	return (*goobj.FuncInfo)(nil).ReadFile(fi.data, fi.lengths.FileOff, uint32(k))
  2071  }
  2072  
  2073  // TopFrame returns true if the function associated with this FuncInfo
  2074  // is an entry point, meaning that unwinders should stop when they hit
  2075  // this function.
  2076  func (fi *FuncInfo) TopFrame() bool {
  2077  	return (fi.FuncFlag() & abi.FuncFlagTopFrame) != 0
  2078  }
  2079  
  2080  type InlTreeNode struct {
  2081  	Parent   int32
  2082  	File     goobj.CUFileIndex
  2083  	Line     int32
  2084  	Func     Sym
  2085  	ParentPC int32
  2086  }
  2087  
  2088  func (fi *FuncInfo) NumInlTree() uint32 {
  2089  	if !fi.lengths.Initialized {
  2090  		panic("need to call Preload first")
  2091  	}
  2092  	return fi.lengths.NumInlTree
  2093  }
  2094  
  2095  func (fi *FuncInfo) InlTree(k int) InlTreeNode {
  2096  	if !fi.lengths.Initialized {
  2097  		panic("need to call Preload first")
  2098  	}
  2099  	node := (*goobj.FuncInfo)(nil).ReadInlTree(fi.data, fi.lengths.InlTreeOff, uint32(k))
  2100  	return InlTreeNode{
  2101  		Parent:   node.Parent,
  2102  		File:     node.File,
  2103  		Line:     node.Line,
  2104  		Func:     fi.l.resolve(fi.r, node.Func),
  2105  		ParentPC: node.ParentPC,
  2106  	}
  2107  }
  2108  
  2109  func (l *Loader) FuncInfo(i Sym) FuncInfo {
  2110  	r, auxs := l.auxs(i)
  2111  	for j := range auxs {
  2112  		a := &auxs[j]
  2113  		if a.Type() == goobj.AuxFuncInfo {
  2114  			b := r.Data(a.Sym().SymIdx)
  2115  			return FuncInfo{l, r, b, goobj.FuncInfoLengths{}}
  2116  		}
  2117  	}
  2118  	return FuncInfo{}
  2119  }
  2120  
  2121  // Preload a package: adds autolib.
  2122  // Does not add defined package or non-packaged symbols to the symbol table.
  2123  // These are done in LoadSyms.
  2124  // Does not read symbol data.
  2125  // Returns the fingerprint of the object.
  2126  func (l *Loader) Preload(localSymVersion int, f *bio.Reader, lib *sym.Library, unit *sym.CompilationUnit, length int64) goobj.FingerprintType {
  2127  	roObject, readonly, err := f.Slice(uint64(length)) // TODO: no need to map blocks that are for tools only (e.g. RefName)
  2128  	if err != nil {
  2129  		log.Fatal("cannot read object file:", err)
  2130  	}
  2131  	r := goobj.NewReaderFromBytes(roObject, readonly)
  2132  	if r == nil {
  2133  		if len(roObject) >= 8 && bytes.Equal(roObject[:8], []byte("\x00go114ld")) {
  2134  			log.Fatalf("found object file %s in old format", f.File().Name())
  2135  		}
  2136  		panic("cannot read object file")
  2137  	}
  2138  	pkgprefix := objabi.PathToPrefix(lib.Pkg) + "."
  2139  	ndef := r.NSym()
  2140  	nhashed64def := r.NHashed64def()
  2141  	nhasheddef := r.NHasheddef()
  2142  	or := &oReader{
  2143  		Reader:       r,
  2144  		unit:         unit,
  2145  		version:      localSymVersion,
  2146  		pkgprefix:    pkgprefix,
  2147  		syms:         make([]Sym, ndef+nhashed64def+nhasheddef+r.NNonpkgdef()+r.NNonpkgref()),
  2148  		ndef:         ndef,
  2149  		nhasheddef:   nhasheddef,
  2150  		nhashed64def: nhashed64def,
  2151  		objidx:       uint32(len(l.objs)),
  2152  	}
  2153  
  2154  	if r.Unlinkable() {
  2155  		log.Fatalf("link: unlinkable object (from package %s) - compiler requires -p flag", lib.Pkg)
  2156  	}
  2157  
  2158  	// Autolib
  2159  	lib.Autolib = append(lib.Autolib, r.Autolib()...)
  2160  
  2161  	// DWARF file table
  2162  	nfile := r.NFile()
  2163  	unit.FileTable = make([]string, nfile)
  2164  	for i := range unit.FileTable {
  2165  		unit.FileTable[i] = r.File(i)
  2166  	}
  2167  
  2168  	l.addObj(lib.Pkg, or)
  2169  
  2170  	// The caller expects us consuming all the data
  2171  	f.MustSeek(length, io.SeekCurrent)
  2172  
  2173  	return r.Fingerprint()
  2174  }
  2175  
  2176  // Holds the loader along with temporary states for loading symbols.
  2177  type loadState struct {
  2178  	l            *Loader
  2179  	hashed64Syms map[uint64]symAndSize         // short hashed (content-addressable) symbols, keyed by content hash
  2180  	hashedSyms   map[goobj.HashType]symAndSize // hashed (content-addressable) symbols, keyed by content hash
  2181  
  2182  	linknameVarRefs []linknameVarRef // linknamed var refererces
  2183  }
  2184  
  2185  type linknameVarRef struct {
  2186  	pkg  string // package of reference (not definition)
  2187  	name string
  2188  	sym  Sym
  2189  }
  2190  
  2191  // Preload symbols of given kind from an object.
  2192  func (st *loadState) preloadSyms(r *oReader, kind int) {
  2193  	l := st.l
  2194  	var start, end uint32
  2195  	switch kind {
  2196  	case pkgDef:
  2197  		start = 0
  2198  		end = uint32(r.ndef)
  2199  	case hashed64Def:
  2200  		start = uint32(r.ndef)
  2201  		end = uint32(r.ndef + r.nhashed64def)
  2202  	case hashedDef:
  2203  		start = uint32(r.ndef + r.nhashed64def)
  2204  		end = uint32(r.ndef + r.nhashed64def + r.nhasheddef)
  2205  	case nonPkgDef:
  2206  		start = uint32(r.ndef + r.nhashed64def + r.nhasheddef)
  2207  		end = uint32(r.ndef + r.nhashed64def + r.nhasheddef + r.NNonpkgdef())
  2208  	default:
  2209  		panic("preloadSyms: bad kind")
  2210  	}
  2211  	l.growAttrBitmaps(len(l.objSyms) + int(end-start))
  2212  	loadingRuntimePkg := r.unit.Lib.Pkg == "runtime"
  2213  	for i := start; i < end; i++ {
  2214  		osym := r.Sym(i)
  2215  		var name string
  2216  		var v int
  2217  		if kind != hashed64Def && kind != hashedDef { // we don't need the name, etc. for hashed symbols
  2218  			name = osym.Name(r.Reader)
  2219  			v = abiToVer(osym.ABI(), r.version)
  2220  		}
  2221  		gi := st.addSym(name, v, r, i, kind, osym)
  2222  		r.syms[i] = gi
  2223  		if kind == nonPkgDef && osym.IsLinkname() && r.DataSize(i) == 0 && strings.Contains(name, ".") {
  2224  			// This is a linknamed "var" "reference" (var x T with no data and //go:linkname x).
  2225  			// We want to check if a linkname reference is allowed. Here we haven't loaded all
  2226  			// symbol definitions, so we don't yet know all the push linknames. So we add to a
  2227  			// list and check later after all symbol defs are loaded. Linknamed vars are rare,
  2228  			// so this list won't be long.
  2229  			// Only check references (pull), not definitions (push, with non-zero size),
  2230  			// so push is always allowed.
  2231  			// This use of linkname is usually for referencing C symbols, so allow symbols
  2232  			// with no "." in its name (not a regular Go symbol).
  2233  			// Linkname is always a non-package reference.
  2234  			st.linknameVarRefs = append(st.linknameVarRefs, linknameVarRef{r.unit.Lib.Pkg, name, gi})
  2235  		}
  2236  		if osym.Local() {
  2237  			l.SetAttrLocal(gi, true)
  2238  		}
  2239  		if osym.UsedInIface() {
  2240  			l.SetAttrUsedInIface(gi, true)
  2241  		}
  2242  		if strings.HasPrefix(name, "runtime.") ||
  2243  			(loadingRuntimePkg && strings.HasPrefix(name, "type:")) {
  2244  			if bi := goobj.BuiltinIdx(name, int(osym.ABI())); bi != -1 {
  2245  				// This is a definition of a builtin symbol. Record where it is.
  2246  				l.builtinSyms[bi] = gi
  2247  			}
  2248  		}
  2249  		if a := int32(osym.Align()); a != 0 && a > l.SymAlign(gi) {
  2250  			l.SetSymAlign(gi, a)
  2251  		}
  2252  		if osym.WasmExport() {
  2253  			l.WasmExports = append(l.WasmExports, gi)
  2254  		}
  2255  	}
  2256  }
  2257  
  2258  // Add syms, hashed (content-addressable) symbols, non-package symbols, and
  2259  // references to external symbols (which are always named).
  2260  func (l *Loader) LoadSyms(arch *sys.Arch) {
  2261  	// Allocate space for symbols, making a guess as to how much space we need.
  2262  	// This function was determined empirically by looking at the cmd/compile on
  2263  	// Darwin, and picking factors for hashed and hashed64 syms.
  2264  	var symSize, hashedSize, hashed64Size int
  2265  	for _, r := range l.objs[goObjStart:] {
  2266  		symSize += r.ndef + r.nhasheddef/2 + r.nhashed64def/2 + r.NNonpkgdef()
  2267  		hashedSize += r.nhasheddef / 2
  2268  		hashed64Size += r.nhashed64def / 2
  2269  	}
  2270  	// Index 0 is invalid for symbols.
  2271  	l.objSyms = make([]objSym, 1, symSize)
  2272  
  2273  	st := loadState{
  2274  		l:            l,
  2275  		hashed64Syms: make(map[uint64]symAndSize, hashed64Size),
  2276  		hashedSyms:   make(map[goobj.HashType]symAndSize, hashedSize),
  2277  	}
  2278  
  2279  	for _, r := range l.objs[goObjStart:] {
  2280  		st.preloadSyms(r, pkgDef)
  2281  	}
  2282  	l.npkgsyms = l.NSym()
  2283  	for _, r := range l.objs[goObjStart:] {
  2284  		st.preloadSyms(r, hashed64Def)
  2285  		st.preloadSyms(r, hashedDef)
  2286  		st.preloadSyms(r, nonPkgDef)
  2287  	}
  2288  	for _, vr := range st.linknameVarRefs {
  2289  		l.checkLinkname(vr.pkg, vr.name, vr.sym)
  2290  	}
  2291  	l.nhashedsyms = len(st.hashed64Syms) + len(st.hashedSyms)
  2292  	for _, r := range l.objs[goObjStart:] {
  2293  		loadObjRefs(l, r, arch)
  2294  	}
  2295  	l.values = make([]int64, l.NSym(), l.NSym()+1000) // +1000 make some room for external symbols
  2296  	l.outer = make([]Sym, l.NSym(), l.NSym()+1000)
  2297  }
  2298  
  2299  func loadObjRefs(l *Loader, r *oReader, arch *sys.Arch) {
  2300  	// load non-package refs
  2301  	ndef := uint32(r.NAlldef())
  2302  	for i, n := uint32(0), uint32(r.NNonpkgref()); i < n; i++ {
  2303  		osym := r.Sym(ndef + i)
  2304  		name := osym.Name(r.Reader)
  2305  		v := abiToVer(osym.ABI(), r.version)
  2306  		gi := l.LookupOrCreateSym(name, v)
  2307  		r.syms[ndef+i] = gi
  2308  		if osym.IsLinkname() {
  2309  			// Check if a linkname reference is allowed.
  2310  			// Only check references (pull), not definitions (push),
  2311  			// so push is always allowed.
  2312  			// Linkname is always a non-package reference.
  2313  			l.checkLinkname(r.unit.Lib.Pkg, name, gi)
  2314  		}
  2315  		if osym.Local() {
  2316  			l.SetAttrLocal(gi, true)
  2317  		}
  2318  		if osym.UsedInIface() {
  2319  			l.SetAttrUsedInIface(gi, true)
  2320  		}
  2321  	}
  2322  
  2323  	// referenced packages
  2324  	npkg := r.NPkg()
  2325  	r.pkg = make([]uint32, npkg)
  2326  	for i := 1; i < npkg; i++ { // PkgIdx 0 is a dummy invalid package
  2327  		pkg := r.Pkg(i)
  2328  		objidx, ok := l.objByPkg[pkg]
  2329  		if !ok {
  2330  			log.Fatalf("%v: reference to nonexistent package %s", r.unit.Lib, pkg)
  2331  		}
  2332  		r.pkg[i] = objidx
  2333  	}
  2334  
  2335  	// load flags of package refs
  2336  	for i, n := 0, r.NRefFlags(); i < n; i++ {
  2337  		rf := r.RefFlags(i)
  2338  		gi := l.resolve(r, rf.Sym())
  2339  		if rf.Flag2()&goobj.SymFlagUsedInIface != 0 {
  2340  			l.SetAttrUsedInIface(gi, true)
  2341  		}
  2342  	}
  2343  }
  2344  
  2345  func abiToVer(abi uint16, localSymVersion int) int {
  2346  	var v int
  2347  	if abi == goobj.SymABIstatic {
  2348  		// Static
  2349  		v = localSymVersion
  2350  	} else if abiver := sym.ABIToVersion(obj.ABI(abi)); abiver != -1 {
  2351  		// Note that data symbols are "ABI0", which maps to version 0.
  2352  		v = abiver
  2353  	} else {
  2354  		log.Fatalf("invalid symbol ABI: %d", abi)
  2355  	}
  2356  	return v
  2357  }
  2358  
  2359  // A list of blocked linknames. Some linknames are allowed only
  2360  // in specific packages. This maps symbol names to allowed packages.
  2361  // If a name is not in this map, it is allowed iff the definition
  2362  // has a linkname (push).
  2363  // If a name is in this map, it is allowed only in listed packages,
  2364  // even if it has a linknamed definition.
  2365  var blockedLinknames = map[string][]string{
  2366  	// coroutines
  2367  	"runtime.coroswitch": {"iter"},
  2368  	"runtime.newcoro":    {"iter"},
  2369  	// fips info
  2370  	"go:fipsinfo": {"crypto/internal/fips140/check"},
  2371  	// New internal linknames in Go 1.24
  2372  	// Pushed from runtime
  2373  	"crypto/internal/fips140.fatal":         {"crypto/internal/fips140"},
  2374  	"crypto/internal/fips140.getIndicator":  {"crypto/internal/fips140"},
  2375  	"crypto/internal/fips140.setIndicator":  {"crypto/internal/fips140"},
  2376  	"crypto/internal/sysrand.fatal":         {"crypto/internal/sysrand"},
  2377  	"crypto/rand.fatal":                     {"crypto/rand"},
  2378  	"internal/runtime/maps.errNilAssign":    {"internal/runtime/maps"},
  2379  	"internal/runtime/maps.fatal":           {"internal/runtime/maps"},
  2380  	"internal/runtime/maps.mapKeyError":     {"internal/runtime/maps"},
  2381  	"internal/runtime/maps.newarray":        {"internal/runtime/maps"},
  2382  	"internal/runtime/maps.newobject":       {"internal/runtime/maps"},
  2383  	"internal/runtime/maps.typedmemclr":     {"internal/runtime/maps"},
  2384  	"internal/runtime/maps.typedmemmove":    {"internal/runtime/maps"},
  2385  	"internal/sync.fatal":                   {"internal/sync"},
  2386  	"internal/sync.runtime_canSpin":         {"internal/sync"},
  2387  	"internal/sync.runtime_doSpin":          {"internal/sync"},
  2388  	"internal/sync.runtime_nanotime":        {"internal/sync"},
  2389  	"internal/sync.runtime_Semrelease":      {"internal/sync"},
  2390  	"internal/sync.runtime_SemacquireMutex": {"internal/sync"},
  2391  	"internal/sync.throw":                   {"internal/sync"},
  2392  	"internal/synctest.Run":                 {"internal/synctest"},
  2393  	"internal/synctest.Wait":                {"internal/synctest"},
  2394  	"internal/synctest.acquire":             {"internal/synctest"},
  2395  	"internal/synctest.release":             {"internal/synctest"},
  2396  	"internal/synctest.inBubble":            {"internal/synctest"},
  2397  	"runtime.getStaticuint64s":              {"reflect"},
  2398  	"sync.runtime_SemacquireWaitGroup":      {"sync"},
  2399  	"time.runtimeNow":                       {"time"},
  2400  	"time.runtimeNano":                      {"time"},
  2401  	// Pushed to runtime from internal/runtime/maps
  2402  	// (other map functions are already linknamed in Go 1.23)
  2403  	"runtime.mapaccess1":         {"runtime"},
  2404  	"runtime.mapaccess1_fast32":  {"runtime"},
  2405  	"runtime.mapaccess1_fast64":  {"runtime"},
  2406  	"runtime.mapaccess1_faststr": {"runtime"},
  2407  	"runtime.mapdelete_fast32":   {"runtime"},
  2408  	"runtime.mapdelete_fast64":   {"runtime"},
  2409  	"runtime.mapdelete_faststr":  {"runtime"},
  2410  }
  2411  
  2412  // check if a linkname reference to symbol s from pkg is allowed
  2413  func (l *Loader) checkLinkname(pkg, name string, s Sym) {
  2414  	if l.flags&FlagCheckLinkname == 0 {
  2415  		return
  2416  	}
  2417  
  2418  	error := func() {
  2419  		log.Fatalf("%s: invalid reference to %s", pkg, name)
  2420  	}
  2421  	pkgs, ok := blockedLinknames[name]
  2422  	if ok {
  2423  		for _, p := range pkgs {
  2424  			if pkg == p {
  2425  				return // pkg is allowed
  2426  			}
  2427  			// crypto/internal/fips140/vX.Y.Z/... is the frozen version of
  2428  			// crypto/internal/fips140/... and is similarly allowed.
  2429  			if strings.HasPrefix(pkg, "crypto/internal/fips140/v") {
  2430  				parts := strings.Split(pkg, "/")
  2431  				parts = append(parts[:3], parts[4:]...)
  2432  				pkg := strings.Join(parts, "/")
  2433  				if pkg == p {
  2434  					return
  2435  				}
  2436  			}
  2437  		}
  2438  		error()
  2439  	}
  2440  	r, li := l.toLocal(s)
  2441  	if r == l.extReader { // referencing external symbol is okay
  2442  		return
  2443  	}
  2444  	if !r.Std() { // For now, only check for symbols defined in std
  2445  		return
  2446  	}
  2447  	if r.unit.Lib.Pkg == pkg { // assembly reference from same package
  2448  		return
  2449  	}
  2450  	osym := r.Sym(li)
  2451  	if osym.IsLinkname() || osym.ABIWrapper() {
  2452  		// Allow if the def has a linkname (push).
  2453  		// ABI wrapper usually wraps an assembly symbol, a linknamed symbol,
  2454  		// or an external symbol, or provide access of a Go symbol to assembly.
  2455  		// For now, allow ABI wrappers.
  2456  		// TODO: check the wrapped symbol?
  2457  		return
  2458  	}
  2459  	error()
  2460  }
  2461  
  2462  // TopLevelSym tests a symbol (by name and kind) to determine whether
  2463  // the symbol first class sym (participating in the link) or is an
  2464  // anonymous aux or sub-symbol containing some sub-part or payload of
  2465  // another symbol.
  2466  func (l *Loader) TopLevelSym(s Sym) bool {
  2467  	return topLevelSym(l.SymName(s), l.SymType(s))
  2468  }
  2469  
  2470  // topLevelSym tests a symbol name and kind to determine whether
  2471  // the symbol first class sym (participating in the link) or is an
  2472  // anonymous aux or sub-symbol containing some sub-part or payload of
  2473  // another symbol.
  2474  func topLevelSym(sname string, skind sym.SymKind) bool {
  2475  	if sname != "" {
  2476  		return true
  2477  	}
  2478  	switch skind {
  2479  	case sym.SDWARFFCN, sym.SDWARFABSFCN, sym.SDWARFTYPE, sym.SDWARFCONST, sym.SDWARFCUINFO, sym.SDWARFRANGE, sym.SDWARFLOC, sym.SDWARFLINES, sym.SGOFUNC:
  2480  		return true
  2481  	default:
  2482  		return false
  2483  	}
  2484  }
  2485  
  2486  // cloneToExternal takes the existing object file symbol (symIdx)
  2487  // and creates a new external symbol payload that is a clone with
  2488  // respect to name, version, type, relocations, etc. The idea here
  2489  // is that if the linker decides it wants to update the contents of
  2490  // a symbol originally discovered as part of an object file, it's
  2491  // easier to do this if we make the updates to an external symbol
  2492  // payload.
  2493  func (l *Loader) cloneToExternal(symIdx Sym) {
  2494  	if l.IsExternal(symIdx) {
  2495  		panic("sym is already external, no need for clone")
  2496  	}
  2497  
  2498  	// Read the particulars from object.
  2499  	r, li := l.toLocal(symIdx)
  2500  	osym := r.Sym(li)
  2501  	sname := osym.Name(r.Reader)
  2502  	sver := abiToVer(osym.ABI(), r.version)
  2503  	skind := sym.AbiSymKindToSymKind[objabi.SymKind(osym.Type())]
  2504  
  2505  	// Create new symbol, update version and kind.
  2506  	pi := l.newPayload(sname, sver)
  2507  	pp := l.payloads[pi]
  2508  	pp.kind = skind
  2509  	pp.ver = sver
  2510  	pp.size = int64(osym.Siz())
  2511  	pp.objidx = r.objidx
  2512  
  2513  	// If this is a def, then copy the guts. We expect this case
  2514  	// to be very rare (one case it may come up is with -X).
  2515  	if li < uint32(r.NAlldef()) {
  2516  
  2517  		// Copy relocations
  2518  		relocs := l.Relocs(symIdx)
  2519  		pp.relocs = make([]goobj.Reloc, relocs.Count())
  2520  		for i := range pp.relocs {
  2521  			// Copy the relocs slice.
  2522  			// Convert local reference to global reference.
  2523  			rel := relocs.At(i)
  2524  			pp.relocs[i].Set(rel.Off(), rel.Siz(), uint16(rel.Type()), rel.Add(), goobj.SymRef{PkgIdx: 0, SymIdx: uint32(rel.Sym())})
  2525  		}
  2526  
  2527  		// Copy data
  2528  		pp.data = r.Data(li)
  2529  	}
  2530  
  2531  	// If we're overriding a data symbol, collect the associated
  2532  	// Gotype, so as to propagate it to the new symbol.
  2533  	auxs := r.Auxs(li)
  2534  	pp.auxs = auxs
  2535  
  2536  	// Install new payload to global index space.
  2537  	// (This needs to happen at the end, as the accessors above
  2538  	// need to access the old symbol content.)
  2539  	l.objSyms[symIdx] = objSym{l.extReader.objidx, uint32(pi)}
  2540  	l.extReader.syms = append(l.extReader.syms, symIdx)
  2541  
  2542  	// Some attributes were encoded in the object file. Copy them over.
  2543  	l.SetAttrDuplicateOK(symIdx, r.Sym(li).Dupok())
  2544  	l.SetAttrShared(symIdx, r.Shared())
  2545  }
  2546  
  2547  // Copy the payload of symbol src to dst. Both src and dst must be external
  2548  // symbols.
  2549  // The intended use case is that when building/linking against a shared library,
  2550  // where we do symbol name mangling, the Go object file may have reference to
  2551  // the original symbol name whereas the shared library provides a symbol with
  2552  // the mangled name. When we do mangling, we copy payload of mangled to original.
  2553  func (l *Loader) CopySym(src, dst Sym) {
  2554  	if !l.IsExternal(dst) {
  2555  		panic("dst is not external") //l.newExtSym(l.SymName(dst), l.SymVersion(dst))
  2556  	}
  2557  	if !l.IsExternal(src) {
  2558  		panic("src is not external") //l.cloneToExternal(src)
  2559  	}
  2560  	l.payloads[l.extIndex(dst)] = l.payloads[l.extIndex(src)]
  2561  	l.SetSymPkg(dst, l.SymPkg(src))
  2562  	// TODO: other attributes?
  2563  }
  2564  
  2565  // CreateExtSym creates a new external symbol with the specified name
  2566  // without adding it to any lookup tables, returning a Sym index for it.
  2567  func (l *Loader) CreateExtSym(name string, ver int) Sym {
  2568  	return l.newExtSym(name, ver)
  2569  }
  2570  
  2571  // CreateStaticSym creates a new static symbol with the specified name
  2572  // without adding it to any lookup tables, returning a Sym index for it.
  2573  func (l *Loader) CreateStaticSym(name string) Sym {
  2574  	// Assign a new unique negative version -- this is to mark the
  2575  	// symbol so that it is not included in the name lookup table.
  2576  	l.anonVersion--
  2577  	return l.newExtSym(name, l.anonVersion)
  2578  }
  2579  
  2580  func (l *Loader) FreeSym(i Sym) {
  2581  	if l.IsExternal(i) {
  2582  		pp := l.getPayload(i)
  2583  		*pp = extSymPayload{}
  2584  	}
  2585  }
  2586  
  2587  // relocId is essentially a <S,R> tuple identifying the Rth
  2588  // relocation of symbol S.
  2589  type relocId struct {
  2590  	sym  Sym
  2591  	ridx int
  2592  }
  2593  
  2594  // SetRelocVariant sets the 'variant' property of a relocation on
  2595  // some specific symbol.
  2596  func (l *Loader) SetRelocVariant(s Sym, ri int, v sym.RelocVariant) {
  2597  	// sanity check
  2598  	if relocs := l.Relocs(s); ri >= relocs.Count() {
  2599  		panic("invalid relocation ID")
  2600  	}
  2601  	if l.relocVariant == nil {
  2602  		l.relocVariant = make(map[relocId]sym.RelocVariant)
  2603  	}
  2604  	if v != 0 {
  2605  		l.relocVariant[relocId{s, ri}] = v
  2606  	} else {
  2607  		delete(l.relocVariant, relocId{s, ri})
  2608  	}
  2609  }
  2610  
  2611  // RelocVariant returns the 'variant' property of a relocation on
  2612  // some specific symbol.
  2613  func (l *Loader) RelocVariant(s Sym, ri int) sym.RelocVariant {
  2614  	return l.relocVariant[relocId{s, ri}]
  2615  }
  2616  
  2617  // UndefinedRelocTargets iterates through the global symbol index
  2618  // space, looking for symbols with relocations targeting undefined
  2619  // references. The linker's loadlib method uses this to determine if
  2620  // there are unresolved references to functions in system libraries
  2621  // (for example, libgcc.a), presumably due to CGO code. Return value
  2622  // is a pair of lists of loader.Sym's. First list corresponds to the
  2623  // corresponding to the undefined symbols themselves, the second list
  2624  // is the symbol that is making a reference to the undef. The "limit"
  2625  // param controls the maximum number of results returned; if "limit"
  2626  // is -1, then all undefs are returned.
  2627  func (l *Loader) UndefinedRelocTargets(limit int) ([]Sym, []Sym) {
  2628  	result, fromr := []Sym{}, []Sym{}
  2629  outerloop:
  2630  	for si := Sym(1); si < Sym(len(l.objSyms)); si++ {
  2631  		relocs := l.Relocs(si)
  2632  		for ri := 0; ri < relocs.Count(); ri++ {
  2633  			r := relocs.At(ri)
  2634  			rs := r.Sym()
  2635  			if rs != 0 && l.SymType(rs) == sym.SXREF && l.SymName(rs) != ".got" {
  2636  				result = append(result, rs)
  2637  				fromr = append(fromr, si)
  2638  				if limit != -1 && len(result) >= limit {
  2639  					break outerloop
  2640  				}
  2641  			}
  2642  		}
  2643  	}
  2644  	return result, fromr
  2645  }
  2646  
  2647  // AssignTextSymbolOrder populates the Textp slices within each
  2648  // library and compilation unit, insuring that packages are laid down
  2649  // in dependency order (internal first, then everything else). Return value
  2650  // is a slice of all text syms.
  2651  func (l *Loader) AssignTextSymbolOrder(libs []*sym.Library, intlibs []bool, extsyms []Sym) []Sym {
  2652  
  2653  	// Library Textp lists should be empty at this point.
  2654  	for _, lib := range libs {
  2655  		if len(lib.Textp) != 0 {
  2656  			panic("expected empty Textp slice for library")
  2657  		}
  2658  		if len(lib.DupTextSyms) != 0 {
  2659  			panic("expected empty DupTextSyms slice for library")
  2660  		}
  2661  	}
  2662  
  2663  	// Used to record which dupok symbol we've assigned to a unit.
  2664  	// Can't use the onlist attribute here because it will need to
  2665  	// clear for the later assignment of the sym.Symbol to a unit.
  2666  	// NB: we can convert to using onList once we no longer have to
  2667  	// call the regular addToTextp.
  2668  	assignedToUnit := MakeBitmap(l.NSym() + 1)
  2669  
  2670  	// Start off textp with reachable external syms.
  2671  	textp := []Sym{}
  2672  	for _, sym := range extsyms {
  2673  		if !l.attrReachable.Has(sym) {
  2674  			continue
  2675  		}
  2676  		textp = append(textp, sym)
  2677  	}
  2678  
  2679  	// Walk through all text symbols from Go object files and append
  2680  	// them to their corresponding library's textp list.
  2681  	for _, r := range l.objs[goObjStart:] {
  2682  		lib := r.unit.Lib
  2683  		for i, n := uint32(0), uint32(r.NAlldef()); i < n; i++ {
  2684  			gi := l.toGlobal(r, i)
  2685  			if !l.attrReachable.Has(gi) {
  2686  				continue
  2687  			}
  2688  			osym := r.Sym(i)
  2689  			st := sym.AbiSymKindToSymKind[objabi.SymKind(osym.Type())]
  2690  			if !st.IsText() {
  2691  				continue
  2692  			}
  2693  			dupok := osym.Dupok()
  2694  			if r2, i2 := l.toLocal(gi); r2 != r || i2 != i {
  2695  				// A dupok text symbol is resolved to another package.
  2696  				// We still need to record its presence in the current
  2697  				// package, as the trampoline pass expects packages
  2698  				// are laid out in dependency order.
  2699  				lib.DupTextSyms = append(lib.DupTextSyms, sym.LoaderSym(gi))
  2700  				continue // symbol in different object
  2701  			}
  2702  			if dupok {
  2703  				lib.DupTextSyms = append(lib.DupTextSyms, sym.LoaderSym(gi))
  2704  				continue
  2705  			}
  2706  
  2707  			lib.Textp = append(lib.Textp, sym.LoaderSym(gi))
  2708  		}
  2709  	}
  2710  
  2711  	// Now assemble global textp, and assign text symbols to units.
  2712  	for _, doInternal := range [2]bool{true, false} {
  2713  		for idx, lib := range libs {
  2714  			if intlibs[idx] != doInternal {
  2715  				continue
  2716  			}
  2717  			lists := [2][]sym.LoaderSym{lib.Textp, lib.DupTextSyms}
  2718  			for i, list := range lists {
  2719  				for _, s := range list {
  2720  					sym := Sym(s)
  2721  					if !assignedToUnit.Has(sym) {
  2722  						textp = append(textp, sym)
  2723  						unit := l.SymUnit(sym)
  2724  						if unit != nil {
  2725  							unit.Textp = append(unit.Textp, s)
  2726  							assignedToUnit.Set(sym)
  2727  						}
  2728  						// Dupok symbols may be defined in multiple packages; the
  2729  						// associated package for a dupok sym is chosen sort of
  2730  						// arbitrarily (the first containing package that the linker
  2731  						// loads). Canonicalizes its Pkg to the package with which
  2732  						// it will be laid down in text.
  2733  						if i == 1 /* DupTextSyms2 */ && l.SymPkg(sym) != lib.Pkg {
  2734  							l.SetSymPkg(sym, lib.Pkg)
  2735  						}
  2736  					}
  2737  				}
  2738  			}
  2739  			lib.Textp = nil
  2740  			lib.DupTextSyms = nil
  2741  		}
  2742  	}
  2743  
  2744  	return textp
  2745  }
  2746  
  2747  // ErrorReporter is a helper class for reporting errors.
  2748  type ErrorReporter struct {
  2749  	ldr              *Loader
  2750  	AfterErrorAction func()
  2751  }
  2752  
  2753  // Errorf method logs an error message.
  2754  //
  2755  // After each error, the error actions function will be invoked; this
  2756  // will either terminate the link immediately (if -h option given)
  2757  // or it will keep a count and exit if more than 20 errors have been printed.
  2758  //
  2759  // Logging an error means that on exit cmd/link will delete any
  2760  // output file and return a non-zero error code.
  2761  func (reporter *ErrorReporter) Errorf(s Sym, format string, args ...interface{}) {
  2762  	if s != 0 && reporter.ldr.SymName(s) != "" {
  2763  		// Note: Replace is needed here because symbol names might have % in them,
  2764  		// due to the use of LinkString for names of instantiating types.
  2765  		format = strings.Replace(reporter.ldr.SymName(s), "%", "%%", -1) + ": " + format
  2766  	} else {
  2767  		format = fmt.Sprintf("sym %d: %s", s, format)
  2768  	}
  2769  	format += "\n"
  2770  	fmt.Fprintf(os.Stderr, format, args...)
  2771  	reporter.AfterErrorAction()
  2772  }
  2773  
  2774  // GetErrorReporter returns the loader's associated error reporter.
  2775  func (l *Loader) GetErrorReporter() *ErrorReporter {
  2776  	return l.errorReporter
  2777  }
  2778  
  2779  // Errorf method logs an error message. See ErrorReporter.Errorf for details.
  2780  func (l *Loader) Errorf(s Sym, format string, args ...interface{}) {
  2781  	l.errorReporter.Errorf(s, format, args...)
  2782  }
  2783  
  2784  // Symbol statistics.
  2785  func (l *Loader) Stat() string {
  2786  	s := fmt.Sprintf("%d symbols, %d reachable\n", l.NSym(), l.NReachableSym())
  2787  	s += fmt.Sprintf("\t%d package symbols, %d hashed symbols, %d non-package symbols, %d external symbols\n",
  2788  		l.npkgsyms, l.nhashedsyms, int(l.extStart)-l.npkgsyms-l.nhashedsyms, l.NSym()-int(l.extStart))
  2789  	return s
  2790  }
  2791  
  2792  // For debugging.
  2793  func (l *Loader) Dump() {
  2794  	fmt.Println("objs")
  2795  	for _, r := range l.objs[goObjStart:] {
  2796  		if r != nil {
  2797  			fmt.Println(r.unit.Lib)
  2798  		}
  2799  	}
  2800  	fmt.Println("extStart:", l.extStart)
  2801  	fmt.Println("Nsyms:", len(l.objSyms))
  2802  	fmt.Println("syms")
  2803  	for i := Sym(1); i < Sym(len(l.objSyms)); i++ {
  2804  		pi := ""
  2805  		if l.IsExternal(i) {
  2806  			pi = fmt.Sprintf("<ext %d>", l.extIndex(i))
  2807  		}
  2808  		sect := ""
  2809  		if l.SymSect(i) != nil {
  2810  			sect = l.SymSect(i).Name
  2811  		}
  2812  		fmt.Printf("%v %v %v %v %x %v\n", i, l.SymName(i), l.SymType(i), pi, l.SymValue(i), sect)
  2813  	}
  2814  	fmt.Println("symsByName")
  2815  	for name, i := range l.symsByName[0] {
  2816  		fmt.Println(i, name, 0)
  2817  	}
  2818  	for name, i := range l.symsByName[1] {
  2819  		fmt.Println(i, name, 1)
  2820  	}
  2821  	fmt.Println("payloads:")
  2822  	for i := range l.payloads {
  2823  		pp := l.payloads[i]
  2824  		fmt.Println(i, pp.name, pp.ver, pp.kind)
  2825  	}
  2826  }
  2827  

View as plain text