Skip to content
Snippets Groups Projects
loader.go 86.4 KiB
Newer Older
  • Learn to ignore specific revisions
  • // Copyright 2019 The Go Authors. All rights reserved.
    // Use of this source code is governed by a BSD-style
    // license that can be found in the LICENSE file.
    
    
    	"cmd/internal/bio"
    	"cmd/internal/goobj2"
    	"cmd/internal/obj"
    	"cmd/internal/objabi"
    	"cmd/internal/sys"
    	"cmd/link/internal/sym"
    
    	"os"
    	"sort"
    	"strconv"
    	"strings"
    )
    
    var _ = fmt.Print
    
    
    // Sym encapsulates a global symbol index, used to identify a specific
    // Go symbol. The 0-valued Sym is corresponds to an invalid symbol.
    type Sym int
    
    
    // Relocs encapsulates the set of relocations on a given symbol; an
    // instance of this type is returned by the Loader Relocs() method.
    type Relocs struct {
    
    	rs []goobj2.Reloc
    
    
    	li int      // local index of symbol whose relocs we're examining
    	r  *oReader // object reader for containing package
    	l  *Loader  // loader
    }
    
    // Reloc contains the payload for a specific relocation.
    // TODO: replace this with sym.Reloc, once we change the
    // relocation target from "*sym.Symbol" to "loader.Sym" in sym.Reloc.
    type Reloc struct {
    	Off  int32            // offset to rewrite
    	Size uint8            // number of bytes to rewrite: 0, 1, 2, or 4
    	Type objabi.RelocType // the relocation type
    	Add  int64            // addend
    	Sym  Sym              // global index of symbol the reloc addresses
    }
    
    
    // ExtReloc contains the payload for an external relocation.
    type ExtReloc struct {
    
    	Idx  int // index of the original relocation
    
    // Reloc2 holds a "handle" to access a relocation record from an
    // object file.
    type Reloc2 struct {
    
    
    	// External reloc types may not fit into a uint8 which the Go object file uses.
    	// Store it here, instead of in the byte of goobj2.Reloc2.
    
    	// For Go symbols this will always be zero.
    
    	// goobj2.Reloc2.Type() + typ is always the right type, for both Go and external
    	// symbols.
    	typ objabi.RelocType
    
    func (rel Reloc2) Type() objabi.RelocType { return objabi.RelocType(rel.Reloc.Type()) + rel.typ }
    func (rel Reloc2) Sym() Sym               { return rel.l.resolve(rel.r, rel.Reloc.Sym()) }
    func (rel Reloc2) SetSym(s Sym)           { rel.Reloc.SetSym(goobj2.SymRef{PkgIdx: 0, SymIdx: uint32(s)}) }
    
    
    func (rel Reloc2) SetType(t objabi.RelocType) {
    	if t != objabi.RelocType(uint8(t)) {
    		panic("SetType: type doesn't fit into Reloc2")
    	}
    
    	rel.Reloc.SetType(uint8(t))
    
    	if rel.typ != 0 {
    		// should use SymbolBuilder.SetRelocType
    		panic("wrong method to set reloc type")
    	}
    
    // Aux2 holds a "handle" to access an aux symbol record from an
    // object file.
    type Aux2 struct {
    
    func (a Aux2) Sym() Sym { return a.l.resolve(a.r, a.Aux.Sym()) }
    
    // oReader is a wrapper type of obj.Reader, along with some
    // extra information.
    // TODO: rename to objReader once the old one is gone?
    type oReader struct {
    	*goobj2.Reader
    	unit      *sym.CompilationUnit
    
    	version   int    // version of static symbol
    	flags     uint32 // read from object file
    
    	syms      []Sym  // Sym's global index, indexed by local index
    	ndef      int    // cache goobj2.Reader.NSym()
    	objidx    uint32 // index of this reader in the objs slice
    
    // objSym represents a symbol in an object file. It is a tuple of
    // the object and the symbol's local index.
    // For external symbols, r is l.extReader, s is its index into the
    // payload array.
    // {nil, 0} represents the nil symbol.
    type objSym struct {
    	r *oReader
    	s int // local index
    }
    
    
    type nameVer struct {
    	name string
    	v    int
    }
    
    
    type Bitmap []uint32
    
    
    // set the i-th bit.
    
    func (bm Bitmap) Set(i Sym) {
    
    	n, r := uint(i)/32, uint(i)%32
    	bm[n] |= 1 << r
    }
    
    
    func (bm Bitmap) Unset(i Sym) {
    
    	n, r := uint(i)/32, uint(i)%32
    	bm[n] &^= (1 << r)
    }
    
    
    // whether the i-th bit is set.
    
    func (bm Bitmap) Has(i Sym) bool {
    
    	n, r := uint(i)/32, uint(i)%32
    	return bm[n]&(1<<r) != 0
    }
    
    
    // return current length of bitmap in bits.
    
    func (bm Bitmap) Len() int {
    
    
    // return the number of bits set.
    func (bm Bitmap) Count() int {
    	s := 0
    	for _, x := range bm {
    		s += bits.OnesCount32(x)
    	}
    	return s
    }
    
    
    func MakeBitmap(n int) Bitmap {
    	return make(Bitmap, (n+31)/32)
    
    // growBitmap insures that the specified bitmap has enough capacity,
    // reallocating (doubling the size) if needed.
    
    func growBitmap(reqLen int, b Bitmap) Bitmap {
    	curLen := b.Len()
    
    		b = append(b, MakeBitmap(reqLen+1-curLen)...)
    
    // A Loader loads new object files and resolves indexed symbol references.
    
    //
    // Notes on the layout of global symbol index space:
    //
    // - Go object files are read before host object files; each Go object
    
    //   read adds its defined package symbols to the global index space.
    //   Nonpackage symbols are not yet added.
    
    // - In loader.LoadNonpkgSyms, add non-package defined symbols and
    //   references in all object files to the global index space.
    
    //
    // - Host object file loading happens; the host object loader does a
    //   name/version lookup for each symbol it finds; this can wind up
    //   extending the external symbol index space range. The host object
    
    //   loader stores symbol payloads in loader.payloads using SymbolBuilder.
    
    // - For now, in loader.LoadFull we convert all symbols (Go + external)
    //   to sym.Symbols.
    
    //
    // - At some point (when the wayfront is pushed through all of the
    //   linker), all external symbols will be payload-based, and we can
    //   get rid of the loader.Syms array.
    //
    
    // - Each symbol gets a unique global index. For duplicated and
    //   overwriting/overwritten symbols, the second (or later) appearance
    //   of the symbol gets the same global index as the first appearance.
    
    	start       map[*oReader]Sym // map from object file to its start index
    	objs        []objIdx         // sorted by start index (i.e. objIdx.i)
    	extStart    Sym              // from this index on, the symbols are externally defined
    	builtinSyms []Sym            // global index of builtin symbols
    
    	objSyms []objSym // global index mapping to local index
    
    
    	symsByName    [2]map[string]Sym // map symbol name to index, two maps are for ABI0 and ABIInternal
    	extStaticSyms map[nameVer]Sym   // externally defined static symbols, keyed by name
    
    	extReader    *oReader // a dummy oReader, for external symbols
    
    	payloadBatch []extSymPayload
    	payloads     []*extSymPayload // contents of linker-materialized external syms
    	values       []int64          // symbol values, indexed by global sym index
    
    
    	sects    []*sym.Section // sections
    	symSects []uint16       // symbol's section, index to sects array
    
    	outdata   [][]byte     // symbol's data in the output buffer
    	extRelocs [][]ExtReloc // symbol's external relocations
    
    	itablink map[Sym]struct{} // itablink[j] defined if j is go.itablink.*
    
    
    	objByPkg map[string]*oReader // map package path to its Go object reader
    
    
    	Syms     []*sym.Symbol // indexed symbols. XXX we still make sym.Symbol for now.
    	symBatch []sym.Symbol  // batch of symbols.
    
    	anonVersion int // most recently assigned ext static sym pseudo-version
    
    
    	// Bitmaps and other side structures used to store data used to store
    	// symbol flags/attributes; these are to be accessed via the
    	// corresponding loader "AttrXXX" and "SetAttrXXX" methods. Please
    	// visit the comments on these methods for more details on the
    	// semantics / interpretation of the specific flags or attribute.
    
    	attrReachable        Bitmap // reachable symbols, indexed by global index
    	attrOnList           Bitmap // "on list" symbols, indexed by global index
    	attrLocal            Bitmap // "local" symbols, indexed by global index
    	attrNotInSymbolTable Bitmap // "not in symtab" symbols, indexed by glob idx
    	attrVisibilityHidden Bitmap // hidden symbols, indexed by ext sym index
    	attrDuplicateOK      Bitmap // dupOK symbols, indexed by ext sym index
    	attrShared           Bitmap // shared symbols, indexed by ext sym index
    	attrExternal         Bitmap // external symbols, indexed by ext sym index
    
    	attrReadOnly         map[Sym]bool     // readonly data for this sym
    
    	attrTopFrame         map[Sym]struct{} // top frame symbols
    	attrSpecial          map[Sym]struct{} // "special" frame symbols
    	attrCgoExportDynamic map[Sym]struct{} // "cgo_export_dynamic" symbols
    	attrCgoExportStatic  map[Sym]struct{} // "cgo_export_static" symbols
    
    	// Outer and Sub relations for symbols.
    	// TODO: figure out whether it's more efficient to just have these
    	// as fields on extSymPayload (note that this won't be a viable
    	// strategy if somewhere in the linker we set sub/outer for a
    	// non-external sym).
    	outer map[Sym]Sym
    	sub   map[Sym]Sym
    
    
    	align map[Sym]int32 // stores alignment for symbols
    
    
    	dynimplib   map[Sym]string      // stores Dynimplib symbol attribute
    	dynimpvers  map[Sym]string      // stores Dynimpvers symbol attribute
    	localentry  map[Sym]uint8       // stores Localentry symbol attribute
    	extname     map[Sym]string      // stores Extname symbol attribute
    	elfType     map[Sym]elf.SymType // stores elf type symbol property
    	elfSym      map[Sym]int32       // stores elf sym symbol property
    	localElfSym map[Sym]int32       // stores "local" elf sym symbol property
    	symPkg      map[Sym]string      // stores package for symbol, or library for shlib-derived syms
    	plt         map[Sym]int32       // stores dynimport for pe objects
    	got         map[Sym]int32       // stores got for pe objects
    	dynid       map[Sym]int32       // stores Dynid for symbol
    
    	relocVariant map[relocId]sym.RelocVariant // stores variant relocs
    
    
    	// Used to implement field tracking; created during deadcode if
    	// field tracking is enabled. Reachparent[K] contains the index of
    	// the symbol that triggered the marking of symbol K as live.
    	Reachparent []Sym
    
    
    	relocBatch []sym.Reloc // for bulk allocation of relocations
    
    
    	flags uint32
    
    	strictDupMsgs int // number of strict-dup warning/errors, when FlagStrictDups is enabled
    
    
    	elfsetstring elfsetstringFunc
    
    	SymLookup func(name string, ver int) *sym.Symbol
    
    type elfsetstringFunc func(s *sym.Symbol, str string, off int)
    
    
    // extSymPayload holds the payload (data + relocations) for linker-synthesized
    
    // external symbols (note that symbol value is stored in a separate slice).
    
    	name     string // TODO: would this be better as offset into str table?
    	size     int64
    	ver      int
    	kind     sym.SymKind
    	objidx   uint32 // index of original object if sym made by cloneToExternal
    	gotype   Sym    // Gotype (0 if not present)
    
    	relocs   []goobj2.Reloc
    
    	reltypes []objabi.RelocType // relocation types
    	data     []byte
    
    	auxs     []goobj2.Aux
    
    const (
    	// Loader.flags
    	FlagStrictDups = 1 << iota
    )
    
    
    func NewLoader(flags uint32, elfsetstring elfsetstringFunc, reporter *ErrorReporter) *Loader {
    
    		start:                make(map[*oReader]Sym),
    
    		objs:                 []objIdx{{}}, // reserve index 0 for nil symbol
    		objSyms:              []objSym{{}}, // reserve index 0 for nil symbol
    		extReader:            &oReader{},
    
    		symsByName:           [2]map[string]Sym{make(map[string]Sym, 100000), make(map[string]Sym, 50000)}, // preallocate ~2MB for ABI0 and ~1MB for ABI1 symbols
    
    		objByPkg:             make(map[string]*oReader),
    		outer:                make(map[Sym]Sym),
    		sub:                  make(map[Sym]Sym),
    		align:                make(map[Sym]int32),
    		dynimplib:            make(map[Sym]string),
    		dynimpvers:           make(map[Sym]string),
    		localentry:           make(map[Sym]uint8),
    		extname:              make(map[Sym]string),
    
    		attrReadOnly:         make(map[Sym]bool),
    
    		elfSym:               make(map[Sym]int32),
    		localElfSym:          make(map[Sym]int32),
    
    		symPkg:               make(map[Sym]string),
    
    		plt:                  make(map[Sym]int32),
    		got:                  make(map[Sym]int32),
    
    		attrTopFrame:         make(map[Sym]struct{}),
    		attrSpecial:          make(map[Sym]struct{}),
    		attrCgoExportDynamic: make(map[Sym]struct{}),
    		attrCgoExportStatic:  make(map[Sym]struct{}),
    		itablink:             make(map[Sym]struct{}),
    		extStaticSyms:        make(map[nameVer]Sym),
    		builtinSyms:          make([]Sym, nbuiltin),
    		flags:                flags,
    
    		elfsetstring:         elfsetstring,
    
    		sects:                []*sym.Section{nil}, // reserve index 0 for nil section
    
    }
    
    // Add object file r, return the start index.
    
    func (l *Loader) addObj(pkg string, r *oReader) Sym {
    
    	if _, ok := l.start[r]; ok {
    		panic("already added")
    	}
    
    	pkg = objabi.PathToPrefix(pkg) // the object file contains escaped package path
    
    	if _, ok := l.objByPkg[pkg]; !ok {
    		l.objByPkg[pkg] = r
    	}
    
    	i := Sym(len(l.objSyms))
    
    	l.objs = append(l.objs, objIdx{r, i})
    
    // Add a symbol from an object file, return the global index and whether it is added.
    
    // If the symbol already exist, it returns the index of that symbol.
    
    func (l *Loader) AddSym(name string, ver int, r *oReader, li int, kind int, dupok bool, typ sym.SymKind) (Sym, bool) {
    
    	if l.extStart != 0 {
    
    		panic("AddSym called after external symbol is created")
    
    	i := Sym(len(l.objSyms))
    	addToGlobal := func() {
    		l.objSyms = append(l.objSyms, objSym{r, li})
    
    		return i, true // unnamed aux symbol
    	}
    
    	if ver == r.version {
    		// Static symbol. Add its global index but don't
    		// add to name lookup table, as it cannot be
    		// referenced by name.
    
    	if kind == pkgDef {
    		// Defined package symbols cannot be dup to each other.
    		// We load all the package symbols first, so we don't need
    		// to check dup here.
    		// We still add it to the lookup table, as it may still be
    		// referenced by name (e.g. through linkname).
    		l.symsByName[ver][name] = i
    		addToGlobal()
    		return i, true
    	}
    
    	// Non-package (named) symbol. Check if it already exists.
    
    	oldi, existed := l.symsByName[ver][name]
    	if !existed {
    		l.symsByName[ver][name] = i
    		addToGlobal()
    		return i, true
    	}
    	// symbol already exists
    	if dupok {
    		if l.flags&FlagStrictDups != 0 {
    			l.checkdup(name, r, li, oldi)
    		}
    		return oldi, false
    	}
    	oldr, oldli := l.toLocal(oldi)
    
    	oldsym := oldr.Sym(oldli)
    
    	if oldsym.Dupok() {
    		return oldi, false
    	}
    	overwrite := r.DataSize(li) != 0
    	if overwrite {
    		// new symbol overwrites old symbol.
    
    		oldtyp := sym.AbiSymKindToSymKind[objabi.SymKind(oldsym.Type())]
    
    		if !(oldtyp.IsData() && oldr.DataSize(oldli) == 0) {
    			log.Fatalf("duplicated definition of symbol " + name)
    		}
    		l.objSyms[oldi] = objSym{r, li}
    	} else {
    		// old symbol overwrites new symbol.
    		if !typ.IsData() { // only allow overwriting data symbol
    			log.Fatalf("duplicated definition of symbol " + name)
    
    // newExtSym creates a new external sym with the specified
    // name/version.
    func (l *Loader) newExtSym(name string, ver int) Sym {
    
    	i := Sym(len(l.objSyms))
    
    	if l.extStart == 0 {
    		l.extStart = i
    	}
    	l.growSyms(int(i))
    
    	pi := l.newPayload(name, ver)
    	l.objSyms = append(l.objSyms, objSym{l.extReader, int(pi)})
    	l.extReader.syms = append(l.extReader.syms, i)
    
    	return i
    }
    
    // LookupOrCreateSym looks up the symbol with the specified name/version,
    // returning its Sym index if found. If the lookup fails, a new external
    // Sym will be created, entered into the lookup tables, and returned.
    func (l *Loader) LookupOrCreateSym(name string, ver int) Sym {
    	i := l.Lookup(name, ver)
    	if i != 0 {
    		return i
    	}
    	i = l.newExtSym(name, ver)
    	static := ver >= sym.SymVerStatic || ver < 0
    
    	if static {
    		l.extStaticSyms[nameVer{name, ver}] = i
    	} else {
    		l.symsByName[ver][name] = i
    	}
    
    func (l *Loader) IsExternal(i Sym) bool {
    
    	return l.isExtReader(r)
    }
    
    func (l *Loader) isExtReader(r *oReader) bool {
    
    	return r == l.extReader
    }
    
    // For external symbol, return its index in the payloads array.
    // XXX result is actually not a global index. We (ab)use the Sym type
    // so we don't need conversion for accessing bitmaps.
    func (l *Loader) extIndex(i Sym) Sym {
    	_, li := l.toLocal(i)
    	return Sym(li)
    }
    
    // Get a new payload for external symbol, return its index in
    // the payloads array.
    func (l *Loader) newPayload(name string, ver int) int {
    	pi := len(l.payloads)
    	pp := l.allocPayload()
    	pp.name = name
    	pp.ver = ver
    	l.payloads = append(l.payloads, pp)
    	l.growExtAttrBitmaps()
    	return pi
    
    // getPayload returns a pointer to the extSymPayload struct for an
    
    // external symbol if the symbol has a payload. Will panic if the
    // symbol in question is bogus (zero or not an external sym).
    
    func (l *Loader) getPayload(i Sym) *extSymPayload {
    
    		panic(fmt.Sprintf("bogus symbol index %d in getPayload", i))
    	}
    
    	return l.payloads[pi]
    }
    
    // allocPayload allocates a new payload.
    func (l *Loader) allocPayload() *extSymPayload {
    	batch := l.payloadBatch
    	if len(batch) == 0 {
    		batch = make([]extSymPayload, 1000)
    	}
    	p := &batch[0]
    	l.payloadBatch = batch[1:]
    	return p
    
    func (ms *extSymPayload) Grow(siz int64) {
    	if int64(int(siz)) != siz {
    		log.Fatalf("symgrow size %d too long", siz)
    	}
    	if int64(len(ms.data)) >= siz {
    		return
    	}
    	if cap(ms.data) < int(siz) {
    		cl := len(ms.data)
    		ms.data = append(ms.data, make([]byte, int(siz)+1-cl)...)
    		ms.data = ms.data[0:cl]
    	}
    	ms.data = ms.data[:siz]
    }
    
    
    // Ensure Syms slice has enough space.
    
    func (l *Loader) growSyms(i int) {
    	n := len(l.Syms)
    	if n > i {
    		return
    	}
    	l.Syms = append(l.Syms, make([]*sym.Symbol, i+1-n)...)
    
    	l.growAttrBitmaps(int(i) + 1)
    
    // Convert a local index to a global index.
    
    func (l *Loader) toGlobal(r *oReader, i int) Sym {
    
    // Convert a global index to a local index.
    
    func (l *Loader) toLocal(i Sym) (*oReader, int) {
    
    	return l.objSyms[i].r, int(l.objSyms[i].s)
    
    // Resolve a local symbol reference. Return global index.
    
    func (l *Loader) resolve(r *oReader, s goobj2.SymRef) Sym {
    
    	var rr *oReader
    	switch p := s.PkgIdx; p {
    	case goobj2.PkgIdxInvalid:
    
    		// {0, X} with non-zero X is never a valid sym reference from a Go object.
    		// We steal this space for symbol references from external objects.
    		// In this case, X is just the global index.
    		if l.isExtReader(r) {
    			return Sym(s.SymIdx)
    		}
    
    		if s.SymIdx != 0 {
    			panic("bad sym ref")
    		}
    		return 0
    	case goobj2.PkgIdxNone:
    
    		return l.builtinSyms[s.SymIdx]
    
    	case goobj2.PkgIdxSelf:
    		rr = r
    	default:
    		pkg := r.Pkg(int(p))
    
    		var ok bool
    		rr, ok = l.objByPkg[pkg]
    		if !ok {
    			log.Fatalf("reference of nonexisted package %s, from %v", pkg, r.unit.Lib)
    		}
    
    	return l.toGlobal(rr, int(s.SymIdx))
    
    // Look up a symbol by name, return global index, or 0 if not found.
    // This is more like Syms.ROLookup than Lookup -- it doesn't create
    // new symbol.
    
    func (l *Loader) Lookup(name string, ver int) Sym {
    
    	if ver >= sym.SymVerStatic || ver < 0 {
    
    		return l.extStaticSyms[nameVer{name, ver}]
    	}
    	return l.symsByName[ver][name]
    
    // Check that duplicate symbols have same contents.
    
    func (l *Loader) checkdup(name string, r *oReader, li int, dup Sym) {
    
    	p := r.Data(li)
    	rdup, ldup := l.toLocal(dup)
    	pdup := rdup.Data(ldup)
    	if bytes.Equal(p, pdup) {
    		return
    	}
    	reason := "same length but different contents"
    	if len(p) != len(pdup) {
    		reason = fmt.Sprintf("new length %d != old length %d", len(p), len(pdup))
    	}
    	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)
    
    	// For the moment, whitelist DWARF subprogram DIEs for
    	// auto-generated wrapper functions. What seems to happen
    	// here is that we get different line numbers on formal
    	// params; I am guessing that the pos is being inherited
    	// from the spot where the wrapper is needed.
    	whitelist := strings.HasPrefix(name, "go.info.go.interface") ||
    		strings.HasPrefix(name, "go.info.go.builtin") ||
    		strings.HasPrefix(name, "go.debuglines")
    	if !whitelist {
    		l.strictDupMsgs++
    	}
    }
    
    func (l *Loader) NStrictDupMsgs() int { return l.strictDupMsgs }
    
    
    // Number of total symbols.
    func (l *Loader) NSym() int {
    
    	return len(l.objSyms)
    
    // Number of defined Go symbols.
    func (l *Loader) NDef() int {
    	return int(l.extStart)
    }
    
    
    // Number of reachable symbols.
    func (l *Loader) NReachableSym() int {
    	return l.attrReachable.Count()
    }
    
    
    // Returns the raw (unpatched) name of the i-th symbol.
    func (l *Loader) RawSymName(i Sym) string {
    
    	return r.Sym(li).Name(r.Reader)
    
    }
    
    // Returns the (patched) name of the i-th symbol.
    func (l *Loader) SymName(i Sym) string {
    
    	return strings.Replace(r.Sym(li).Name(r.Reader), "\"\".", r.pkgprefix, -1)
    
    // Returns the version of the i-th symbol.
    func (l *Loader) SymVersion(i Sym) int {
    	if l.IsExternal(i) {
    		pp := l.getPayload(i)
    		return pp.ver
    	}
    	r, li := l.toLocal(i)
    
    	return int(abiToVer(r.Sym(li).ABI(), r.version))
    
    // Returns the type of the i-th symbol.
    func (l *Loader) SymType(i Sym) sym.SymKind {
    
    		pp := l.getPayload(i)
    		if pp != nil {
    			return pp.kind
    		}
    
    	return sym.AbiSymKindToSymKind[objabi.SymKind(r.Sym(li).Type())]
    
    // Returns the attributes of the i-th symbol.
    func (l *Loader) SymAttr(i Sym) uint8 {
    
    		// TODO: do something? External symbols have different representation of attributes.
    		// For now, ReflectMethod, NoSplit, GoType, and Typelink are used and they cannot be
    		// set by external symbol.
    
    	return r.Sym(li).Flag()
    
    // Returns the size of the i-th symbol.
    func (l *Loader) SymSize(i Sym) int64 {
    	if l.IsExternal(i) {
    		pp := l.getPayload(i)
    		return pp.size
    	}
    	r, li := l.toLocal(i)
    
    	return int64(r.Sym(li).Siz())
    
    // AttrReachable returns true for symbols that are transitively
    // referenced from the entry points. Unreachable symbols are not
    // written to the output.
    func (l *Loader) AttrReachable(i Sym) bool {
    
    	return l.attrReachable.Has(i)
    
    }
    
    // SetAttrReachable sets the reachability property for a symbol (see
    // AttrReachable).
    func (l *Loader) SetAttrReachable(i Sym, v bool) {
    	if v {
    
    		l.attrReachable.Set(i)
    
    		l.attrReachable.Unset(i)
    
    	}
    }
    
    // AttrOnList returns true for symbols that are on some list (such as
    // the list of all text symbols, or one of the lists of data symbols)
    // and is consulted to avoid bugs where a symbol is put on a list
    // twice.
    func (l *Loader) AttrOnList(i Sym) bool {
    
    	return l.attrOnList.Has(i)
    
    }
    
    // SetAttrOnList sets the "on list" property for a symbol (see
    // AttrOnList).
    func (l *Loader) SetAttrOnList(i Sym, v bool) {
    	if v {
    
    		l.attrOnList.Unset(i)
    
    // AttrLocal returns true for symbols that are only visible within the
    // module (executable or shared library) being linked. This attribute
    // is applied to thunks and certain other linker-generated symbols.
    func (l *Loader) AttrLocal(i Sym) bool {
    
    	return l.attrLocal.Has(i)
    
    }
    
    // SetAttrLocal the "local" property for a symbol (see AttrLocal above).
    func (l *Loader) SetAttrLocal(i Sym, v bool) {
    	if v {
    
    		l.attrLocal.Unset(i)
    
    // SymAddr checks that a symbol is reachable, and returns its value.
    func (l *Loader) SymAddr(i Sym) int64 {
    	if !l.AttrReachable(i) {
    		panic("unreachable symbol in symaddr")
    	}
    	return l.values[i]
    }
    
    
    // AttrNotInSymbolTable returns true for symbols that should not be
    // added to the symbol table of the final generated load module.
    func (l *Loader) AttrNotInSymbolTable(i Sym) bool {
    
    	return l.attrNotInSymbolTable.Has(i)
    
    }
    
    // SetAttrNotInSymbolTable the "not in symtab" property for a symbol
    // (see AttrNotInSymbolTable above).
    func (l *Loader) SetAttrNotInSymbolTable(i Sym, v bool) {
    	if v {
    
    		l.attrNotInSymbolTable.Set(i)
    
    		l.attrNotInSymbolTable.Unset(i)
    
    // AttrVisibilityHidden symbols returns true for ELF symbols with
    // visibility set to STV_HIDDEN. They become local symbols in
    // the final executable. Only relevant when internally linking
    // on an ELF platform.
    func (l *Loader) AttrVisibilityHidden(i Sym) bool {
    
    	return l.attrVisibilityHidden.Has(l.extIndex(i))
    
    }
    
    // SetAttrVisibilityHidden sets the "hidden visibility" property for a
    // symbol (see AttrVisibilityHidden).
    func (l *Loader) SetAttrVisibilityHidden(i Sym, v bool) {
    
    		panic("tried to set visibility attr on non-external symbol")
    	}
    	if v {
    
    		l.attrVisibilityHidden.Set(l.extIndex(i))
    
    		l.attrVisibilityHidden.Unset(l.extIndex(i))
    
    	}
    }
    
    // AttrDuplicateOK returns true for a symbol that can be present in
    // multiple object files.
    func (l *Loader) AttrDuplicateOK(i Sym) bool {
    
    		// TODO: if this path winds up being taken frequently, it
    		// might make more sense to copy the flag value out of the object
    		// into a larger bitmap during preload.
    		r, li := l.toLocal(i)
    
    		return r.Sym(li).Dupok()
    
    	return l.attrDuplicateOK.Has(l.extIndex(i))
    
    }
    
    // SetAttrDuplicateOK sets the "duplicate OK" property for an external
    // symbol (see AttrDuplicateOK).
    func (l *Loader) SetAttrDuplicateOK(i Sym, v bool) {
    
    		panic("tried to set dupok attr on non-external symbol")
    	}
    	if v {
    
    		l.attrDuplicateOK.Set(l.extIndex(i))
    
    		l.attrDuplicateOK.Unset(l.extIndex(i))
    
    	}
    }
    
    // AttrShared returns true for symbols compiled with the -shared option.
    func (l *Loader) AttrShared(i Sym) bool {
    
    		// TODO: if this path winds up being taken frequently, it
    		// might make more sense to copy the flag value out of the
    		// object into a larger bitmap during preload.
    		r, _ := l.toLocal(i)
    		return (r.Flags() & goobj2.ObjFlagShared) != 0
    	}
    
    	return l.attrShared.Has(l.extIndex(i))
    
    // SetAttrShared sets the "shared" property for an external
    // symbol (see AttrShared).
    func (l *Loader) SetAttrShared(i Sym, v bool) {
    	if !l.IsExternal(i) {
    		panic(fmt.Sprintf("tried to set shared attr on non-external symbol %d %s", i, l.SymName(i)))
    	}
    	if v {
    		l.attrShared.Set(l.extIndex(i))
    	} else {
    		l.attrShared.Unset(l.extIndex(i))
    	}
    }
    
    
    // AttrExternal returns true for function symbols loaded from host
    // object files.
    func (l *Loader) AttrExternal(i Sym) bool {
    
    	return l.attrExternal.Has(l.extIndex(i))
    
    }
    
    // SetAttrExternal sets the "external" property for an host object
    // symbol (see AttrExternal).
    func (l *Loader) SetAttrExternal(i Sym, v bool) {
    
    		panic(fmt.Sprintf("tried to set external attr on non-external symbol %q", l.RawSymName(i)))
    
    		l.attrExternal.Set(l.extIndex(i))
    
    		l.attrExternal.Unset(l.extIndex(i))
    
    	}
    }
    
    // AttrTopFrame returns true for a function symbol that is an entry
    // point, meaning that unwinders should stop when they hit this
    // function.
    func (l *Loader) AttrTopFrame(i Sym) bool {
    	_, ok := l.attrTopFrame[i]
    	return ok
    }
    
    // SetAttrTopFrame sets the "top frame" property for a symbol (see
    // AttrTopFrame).
    func (l *Loader) SetAttrTopFrame(i Sym, v bool) {
    	if v {
    		l.attrTopFrame[i] = struct{}{}
    	} else {
    		delete(l.attrTopFrame, i)
    	}
    }
    
    // AttrSpecial returns true for a symbols that do not have their
    // address (i.e. Value) computed by the usual mechanism of
    // data.go:dodata() & data.go:address().
    func (l *Loader) AttrSpecial(i Sym) bool {
    	_, ok := l.attrSpecial[i]
    	return ok
    }
    
    // SetAttrSpecial sets the "special" property for a symbol (see
    // AttrSpecial).
    func (l *Loader) SetAttrSpecial(i Sym, v bool) {
    	if v {
    		l.attrSpecial[i] = struct{}{}
    	} else {
    		delete(l.attrSpecial, i)
    	}
    }
    
    // AttrCgoExportDynamic returns true for a symbol that has been
    // specially marked via the "cgo_export_dynamic" compiler directive
    // written by cgo (in response to //export directives in the source).
    func (l *Loader) AttrCgoExportDynamic(i Sym) bool {
    	_, ok := l.attrCgoExportDynamic[i]
    	return ok
    }
    
    // SetAttrCgoExportDynamic sets the "cgo_export_dynamic" for a symbol
    // (see AttrCgoExportDynamic).
    func (l *Loader) SetAttrCgoExportDynamic(i Sym, v bool) {
    	if v {
    		l.attrCgoExportDynamic[i] = struct{}{}
    	} else {
    		delete(l.attrCgoExportDynamic, i)
    	}
    }
    
    // AttrCgoExportStatic returns true for a symbol that has been
    // specially marked via the "cgo_export_static" directive
    // written by cgo.
    func (l *Loader) AttrCgoExportStatic(i Sym) bool {
    	_, ok := l.attrCgoExportStatic[i]
    	return ok
    }
    
    
    // SetAttrCgoExportStatic sets the "cgo_export_static" for a symbol
    
    // (see AttrCgoExportStatic).
    func (l *Loader) SetAttrCgoExportStatic(i Sym, v bool) {
    	if v {
    		l.attrCgoExportStatic[i] = struct{}{}
    	} else {
    		delete(l.attrCgoExportStatic, i)
    	}
    }
    
    
    func (l *Loader) AttrCgoExport(i Sym) bool {
    	return l.AttrCgoExportDynamic(i) || l.AttrCgoExportStatic(i)
    }
    
    
    // AttrReadOnly returns true for a symbol whose underlying data
    // is stored via a read-only mmap.
    func (l *Loader) AttrReadOnly(i Sym) bool {
    	if v, ok := l.attrReadOnly[i]; ok {
    		return v
    	}
    
    		pp := l.getPayload(i)
    		if pp.objidx != 0 {
    			return l.objs[pp.objidx].r.ReadOnly()
    		}
    
    		return false
    	}
    	r, _ := l.toLocal(i)
    	return r.ReadOnly()
    }
    
    
    // SetAttrReadOnly sets the "data is read only" property for a symbol
    
    // (see AttrReadOnly).
    func (l *Loader) SetAttrReadOnly(i Sym, v bool) {
    	l.attrReadOnly[i] = v
    }
    
    
    // AttrSubSymbol returns true for symbols that are listed as a
    // sub-symbol of some other outer symbol. The sub/outer mechanism is
    // used when loading host objects (sections from the host object
    // become regular linker symbols and symbols go on the Sub list of
    // their section) and for constructing the global offset table when
    // internally linking a dynamic executable.
    
    //
    // Note that in later stages of the linker, we set Outer(S) to some