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

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

View as plain text