Skip to content
Snippets Groups Projects
loader.go 66.4 KiB
Newer Older
  • Learn to ignore specific revisions
  • func (l *Loader) IsGoType(i Sym) bool {
    	return l.SymAttr(i)&goobj2.SymFlagGoType != 0
    }
    
    
    // Returns whether this is a "go.itablink.*" symbol.
    func (l *Loader) IsItabLink(i Sym) bool {
    	if _, ok := l.itablink[i]; ok {
    		return true
    	}
    	return false
    }
    
    
    // growValues grows the slice used to store symbol values.
    func (l *Loader) growValues(reqLen int) {
    	curLen := len(l.values)
    	if reqLen > curLen {
    		l.values = append(l.values, make([]int64, reqLen+1-curLen)...)
    	}
    }
    
    // SymValue returns the value of the i-th symbol. i is global index.
    func (l *Loader) SymValue(i Sym) int64 {
    	return l.values[i]
    }
    
    // SetSymValue sets the value of the i-th symbol. i is global index.
    func (l *Loader) SetSymValue(i Sym, val int64) {
    	l.values[i] = val
    }
    
    
    // Returns the symbol content of the i-th symbol. i is global index.
    func (l *Loader) Data(i Sym) []byte {
    
    // SymAlign returns the alignment for a symbol.
    func (l *Loader) SymAlign(i Sym) int32 {
    	// If an alignment has been recorded, return that.
    	if align, ok := l.align[i]; ok {
    		return align
    	}
    	// TODO: would it make sense to return an arch-specific
    	// alignment depending on section type? E.g. STEXT => 32,
    	// SDATA => 1, etc?
    	return 0
    }
    
    // SetSymAlign sets the alignment for a symbol.
    func (l *Loader) SetSymAlign(i Sym, align int32) {
    	// reject bad synbols
    	if i > l.max || i == 0 {
    		panic("bad symbol index in SetSymAlign")
    	}
    	// Reject nonsense alignments.
    	// TODO: do we need this?
    	if align < 0 {
    		panic("bad alignment value")
    	}
    	if align == 0 {
    		delete(l.align, i)
    	} else {
    		// Alignment should be a power of 2.
    		if bits.OnesCount32(uint32(align)) != 1 {
    			panic("bad alignment value")
    		}
    		l.align[i] = align
    	}
    }
    
    
    // SymDynImplib returns the "dynimplib" attribute for the specified
    // symbol, making up a portion of the info for a symbol specified
    // on a "cgo_import_dynamic" compiler directive.
    func (l *Loader) SymDynimplib(i Sym) string {
    	return l.dynimplib[i]
    }
    
    // SetSymDynimplib sets the "dynimplib" attribute for a symbol.
    func (l *Loader) SetSymDynimplib(i Sym, value string) {
    	// reject bad symbols
    	if i > l.max || i == 0 {
    		panic("bad symbol index in SetDynimplib")
    	}
    	if value == "" {
    		delete(l.dynimplib, i)
    	} else {
    		l.dynimplib[i] = value
    	}
    }
    
    // SymDynimpvers returns the "dynimpvers" attribute for the specified
    // symbol, making up a portion of the info for a symbol specified
    // on a "cgo_import_dynamic" compiler directive.
    func (l *Loader) SymDynimpvers(i Sym) string {
    	return l.dynimpvers[i]
    }
    
    // SetSymDynimpvers sets the "dynimpvers" attribute for a symbol.
    func (l *Loader) SetSymDynimpvers(i Sym, value string) {
    	// reject bad symbols
    	if i > l.max || i == 0 {
    		panic("bad symbol index in SetDynimpvers")
    	}
    	if value == "" {
    		delete(l.dynimpvers, i)
    	} else {
    		l.dynimpvers[i] = value
    	}
    }
    
    // SymExtname returns the "extname" value for the specified
    // symbol.
    func (l *Loader) SymExtname(i Sym) string {
    	return l.extname[i]
    }
    
    // SetSymExtname sets the  "extname" attribute for a symbol.
    func (l *Loader) SetSymExtname(i Sym, value string) {
    	// reject bad symbols
    	if i > l.max || i == 0 {
    		panic("bad symbol index in SetExtname")
    	}
    	if value == "" {
    		delete(l.extname, i)
    	} else {
    		l.extname[i] = value
    	}
    }
    
    // SymLocalentry returns the "local entry" value for the specified
    // symbol.
    func (l *Loader) SymLocalentry(i Sym) uint8 {
    	return l.localentry[i]
    }
    
    // SetSymExtname sets the "extname" attribute for a symbol.
    func (l *Loader) SetSymLocalentry(i Sym, value uint8) {
    	// reject bad symbols
    	if i > l.max || i == 0 {
    		panic("bad symbol index in SetExtname")
    	}
    	if value == 0 {
    		delete(l.localentry, i)
    	} else {
    		l.localentry[i] = value
    	}
    }
    
    
    // Returns the number of aux symbols given a global index.
    func (l *Loader) NAux(i Sym) int {
    
    	return r.NAux(li)
    }
    
    // Returns the referred symbol of the j-th aux symbol of the i-th
    // symbol.
    func (l *Loader) AuxSym(i Sym, j int) Sym {
    
    	a := goobj2.Aux{}
    	a.Read(r.Reader, r.AuxOff(li, j))
    
    // ReadAuxSyms reads the aux symbol ids for the specified symbol into the
    // slice passed as a parameter. If the slice capacity is not large enough, a new
    // larger slice will be allocated. Final slice is returned.
    func (l *Loader) ReadAuxSyms(symIdx Sym, dst []Sym) []Sym {
    
    	if l.IsExternal(symIdx) {
    
    		return dst[:0]
    	}
    	naux := l.NAux(symIdx)
    	if naux == 0 {
    		return dst[:0]
    	}
    
    	if cap(dst) < naux {
    		dst = make([]Sym, naux)
    	}
    	dst = dst[:0]
    
    	r, li := l.toLocal(symIdx)
    	for i := 0; i < naux; i++ {
    		a := goobj2.Aux{}
    		a.Read(r.Reader, r.AuxOff(li, i))
    		dst = append(dst, l.resolve(r, a.Sym))
    	}
    
    	return dst
    }
    
    
    // PrependSub prepends 'sub' onto the sub list for outer symbol 'outer'.
    // Will panic if 'sub' already has an outer sym or sub sym.
    // FIXME: should this be instead a method on SymbolBuilder?
    func (l *Loader) PrependSub(outer Sym, sub Sym) {
    	if l.Syms[outer] != nil {
    		panic("not implemented for sym.Symbol based syms")
    	}
    	// NB: this presupposes that an outer sym can't be a sub symbol of
    	// some other outer-outer sym (I'm assuming this is true, but I
    	// haven't tested exhaustively).
    	if l.OuterSym(outer) != 0 {
    		panic("outer has outer itself")
    	}
    	if l.SubSym(sub) != 0 {
    		panic("sub set for subsym")
    	}
    	if l.OuterSym(sub) != 0 {
    		panic("outer already set for subsym")
    	}
    	l.sub[sub] = l.sub[outer]
    	l.sub[outer] = sub
    	l.outer[sub] = outer
    }
    
    
    // OuterSym gets the outer symbol for host object loaded symbols.
    func (l *Loader) OuterSym(i Sym) Sym {
    	sym := l.Syms[i]
    	if sym != nil && sym.Outer != nil {
    		outer := sym.Outer
    		return l.Lookup(outer.Name, int(outer.Version))
    	}
    
    	// FIXME: add check for isExternal?
    	return l.outer[i]
    
    // SubSym gets the subsymbol for host object loaded symbols.
    
    func (l *Loader) SubSym(i Sym) Sym {
    	sym := l.Syms[i]
    	if sym != nil && sym.Sub != nil {
    		sub := sym.Sub
    		return l.Lookup(sub.Name, int(sub.Version))
    	}
    
    	// NB: note -- no check for l.isExternal(), since I am pretty sure
    	// that later phases in the linker set subsym for "type." syms
    	return l.sub[i]
    
    // Initialize Reachable bitmap and its siblings for running deadcode pass.
    
    func (l *Loader) InitReachable() {
    
    	l.growAttrBitmaps(l.NSym() + 1)
    
    type symWithVal struct {
    	s Sym
    	v int64
    }
    type bySymValue []symWithVal
    
    func (s bySymValue) Len() int           { return len(s) }
    func (s bySymValue) Swap(i, j int)      { s[i], s[j] = s[j], s[i] }
    func (s bySymValue) Less(i, j int) bool { return s[i].v < s[j].v }
    
    // SortSub walks through the sub-symbols for 's' and sorts them
    // in place by increasing value. Return value is the new
    // sub symbol for the specified outer symbol.
    func (l *Loader) SortSub(s Sym) Sym {
    
    	if s == 0 || l.sub[s] == 0 {
    		return s
    	}
    
    	// Sort symbols using a slice first. Use a stable sort on the off
    	// chance that there's more than once symbol with the same value,
    	// so as to preserve reproducible builds.
    	sl := []symWithVal{}
    	for ss := l.sub[s]; ss != 0; ss = l.sub[ss] {
    		sl = append(sl, symWithVal{s: ss, v: l.SymValue(ss)})
    	}
    	sort.Stable(bySymValue(sl))
    
    	// Then apply any changes needed to the sub map.
    	ns := Sym(0)
    	for i := len(sl) - 1; i >= 0; i-- {
    		s := sl[i].s
    		l.sub[s] = ns
    		ns = s
    	}
    
    	// Update sub for outer symbol, then return
    	l.sub[s] = sl[0].s
    	return sl[0].s
    }
    
    
    // Insure that reachable bitmap and its siblings have enough size.
    func (l *Loader) growAttrBitmaps(reqLen int) {
    	if reqLen > l.attrReachable.len() {
    		// These are indexed by global symbol
    		l.attrReachable = growBitmap(reqLen, l.attrReachable)
    
    		l.attrOnList = growBitmap(reqLen, l.attrOnList)
    
    	}
    	// These are indexed by external symbol offset (e.g. i - l.extStart)
    	if l.extStart == 0 {
    		return
    	}
    	extReqLen := reqLen - int(l.extStart)
    	if extReqLen > l.attrVisibilityHidden.len() {
    		l.attrVisibilityHidden = growBitmap(extReqLen, l.attrVisibilityHidden)
    		l.attrDuplicateOK = growBitmap(extReqLen, l.attrDuplicateOK)
    		l.attrShared = growBitmap(extReqLen, l.attrShared)
    		l.attrExternal = growBitmap(extReqLen, l.attrExternal)
    
    // At method returns the j-th reloc for a global symbol.
    func (relocs *Relocs) At(j int) Reloc {
    
    	if s := relocs.l.Syms[relocs.extIdx]; s != nil {
    		rel := s.R[j]
    
    		return Reloc{
    			Off:  rel.Off,
    			Size: rel.Siz,
    			Type: rel.Type,
    			Add:  rel.Add,
    			Sym:  relocs.l.Lookup(rel.Sym.Name, int(rel.Sym.Version)),
    		}
    	}
    
    	if relocs.extIdx != 0 {
    		pp := relocs.l.getPayload(relocs.extIdx)
    		return pp.relocs[j]
    	}
    
    	rel := goobj2.Reloc{}
    	rel.Read(relocs.r.Reader, relocs.r.RelocOff(relocs.li, j))
    
    	target := relocs.l.resolve(relocs.r, rel.Sym)
    
    	return Reloc{
    		Off:  rel.Off,
    		Size: rel.Siz,
    		Type: objabi.RelocType(rel.Type),
    		Add:  rel.Add,
    		Sym:  target,
    	}
    }
    
    
    // ReadAll method reads all relocations for a symbol into the
    // specified slice. If the slice capacity is not large enough, a new
    // larger slice will be allocated. Final slice is returned.
    func (relocs *Relocs) ReadAll(dst []Reloc) []Reloc {
    	if relocs.Count == 0 {
    
    	}
    
    	if cap(dst) < relocs.Count {
    		dst = make([]Reloc, relocs.Count)
    	}
    	dst = dst[:0]
    
    
    	if s := relocs.l.Syms[relocs.extIdx]; s != nil {
    
    			rel := Reloc{
    				Off:  erel.Off,
    				Size: erel.Siz,
    				Type: erel.Type,
    				Add:  erel.Add,
    				Sym:  relocs.l.Lookup(erel.Sym.Name, int(erel.Sym.Version)),
    			}
    			dst = append(dst, rel)
    		}
    		return dst
    	}
    
    
    	if relocs.extIdx != 0 {
    		pp := relocs.l.getPayload(relocs.extIdx)
    		dst = append(dst, pp.relocs...)
    		return dst
    	}
    
    
    	off := relocs.r.RelocOff(relocs.li, 0)
    	for i := 0; i < relocs.Count; i++ {
    		rel := goobj2.Reloc{}
    		rel.Read(relocs.r.Reader, off)
    		off += uint32(rel.Size())
    		target := relocs.l.resolve(relocs.r, rel.Sym)
    		dst = append(dst, Reloc{
    			Off:  rel.Off,
    			Size: rel.Siz,
    			Type: objabi.RelocType(rel.Type),
    			Add:  rel.Add,
    			Sym:  target,
    		})
    	}
    	return dst
    }
    
    
    // Relocs returns a Relocs object for the given global sym.
    func (l *Loader) Relocs(i Sym) Relocs {
    
    			return Relocs{Count: len(s.R), l: l, extIdx: i}
    		}
    		pp := l.getPayload(i)
    		if pp != nil {
    			return Relocs{Count: len(pp.relocs), l: l, extIdx: i}
    
    	if r == nil {
    		panic(fmt.Sprintf("trying to get oreader for invalid sym %d\n\n", i))
    	}
    
    	return l.relocs(r, li)
    }
    
    // Relocs returns a Relocs object given a local sym index and reader.
    func (l *Loader) relocs(r *oReader, li int) Relocs {
    	return Relocs{
    		Count: r.NReloc(li),
    		li:    li,
    		r:     r,
    		l:     l,
    	}
    }
    
    
    // RelocByOff implements sort.Interface for sorting relocations by offset.
    
    type RelocByOff []Reloc
    
    func (x RelocByOff) Len() int           { return len(x) }
    func (x RelocByOff) Swap(i, j int)      { x[i], x[j] = x[j], x[i] }
    func (x RelocByOff) Less(i, j int) bool { return x[i].Off < x[j].Off }
    
    
    // Preload a package: add autolibs, add symbols to the symbol table.
    // Does not read symbol data yet.
    
    func (l *Loader) Preload(arch *sys.Arch, syms *sym.Symbols, f *bio.Reader, lib *sym.Library, unit *sym.CompilationUnit, length int64, pn string, flags int) {
    
    	roObject, readonly, err := f.Slice(uint64(length))
    	if err != nil {
    		log.Fatal("cannot read object file:", err)
    	}
    	r := goobj2.NewReaderFromBytes(roObject, readonly)
    
    	if r == nil {
    		panic("cannot read object file")
    	}
    	localSymVersion := syms.IncVersion()
    	pkgprefix := objabi.PathToPrefix(lib.Pkg) + "."
    
    	or := &oReader{r, unit, localSymVersion, r.Flags(), pkgprefix, nil}
    
    	lib.ImportStrings = append(lib.ImportStrings, r.Autolib()...)
    
    	// DWARF file table
    	nfile := r.NDwarfFile()
    	unit.DWARFFileTable = make([]string, nfile)
    	for i := range unit.DWARFFileTable {
    		unit.DWARFFileTable[i] = r.DwarfFile(i)
    	}
    
    
    
    	ndef := r.NSym()
    	nnonpkgdef := r.NNonpkgdef()
    	for i, n := 0, ndef+nnonpkgdef; i < n; i++ {
    		osym := goobj2.Sym{}
    		osym.Read(r, r.SymOff(i))
    		name := strings.Replace(osym.Name, "\"\".", pkgprefix, -1)
    		if name == "" {
    			continue // don't add unnamed aux symbol
    		}
    		v := abiToVer(osym.ABI, localSymVersion)
    
    		added := l.AddSym(name, v, istart+Sym(i), or, dupok, sym.AbiSymKindToSymKind[objabi.SymKind(osym.Type)])
    		if added && strings.HasPrefix(name, "go.itablink.") {
    			l.itablink[istart+Sym(i)] = struct{}{}
    		}
    
    		if added && strings.HasPrefix(name, "runtime.") {
    			if bi := goobj2.BuiltinIdx(name, v); bi != -1 {
    				// This is a definition of a builtin symbol. Record where it is.
    				l.builtinSyms[bi] = istart + Sym(i)
    			}
    		}
    
    	}
    
    	// The caller expects us consuming all the data
    	f.MustSeek(length, os.SEEK_CUR)
    }
    
    // Make sure referenced symbols are added. Most of them should already be added.
    // This should only be needed for referenced external symbols.
    
    func (l *Loader) LoadRefs(arch *sys.Arch, syms *sym.Symbols) {
    
    	for _, o := range l.objs[1:] {
    		loadObjRefs(l, o.r, arch, syms)
    	}
    }
    
    func loadObjRefs(l *Loader, r *oReader, arch *sys.Arch, syms *sym.Symbols) {
    
    	ndef := r.NSym() + r.NNonpkgdef()
    	for i, n := 0, r.NNonpkgref(); i < n; i++ {
    		osym := goobj2.Sym{}
    
    		name := strings.Replace(osym.Name, "\"\".", r.pkgprefix, -1)
    
    	}
    }
    
    func abiToVer(abi uint16, localSymVersion int) int {
    	var v int
    	if abi == goobj2.SymABIstatic {
    		// Static
    		v = localSymVersion
    	} else if abiver := sym.ABIToVersion(obj.ABI(abi)); abiver != -1 {
    		// Note that data symbols are "ABI0", which maps to version 0.
    		v = abiver
    	} else {
    		log.Fatalf("invalid symbol ABI: %d", abi)
    	}
    	return v
    }
    
    func preprocess(arch *sys.Arch, s *sym.Symbol) {
    	if s.Name != "" && s.Name[0] == '$' && len(s.Name) > 5 && s.Type == 0 && len(s.P) == 0 {
    		x, err := strconv.ParseUint(s.Name[5:], 16, 64)
    		if err != nil {
    			log.Panicf("failed to parse $-symbol %s: %v", s.Name, err)
    		}
    		s.Type = sym.SRODATA
    		s.Attr |= sym.AttrLocal
    		switch s.Name[:5] {
    		case "$f32.":
    			if uint64(uint32(x)) != x {
    				log.Panicf("$-symbol %s too large: %d", s.Name, x)
    			}
    			s.AddUint32(arch, uint32(x))
    		case "$f64.", "$i64.":
    			s.AddUint64(arch, x)
    		default:
    			log.Panicf("unrecognized $-symbol: %s", s.Name)
    		}
    	}
    }
    
    
    func (l *Loader) LoadFull(arch *sys.Arch, syms *sym.Symbols) {
    
    
    	nr := 0 // total number of sym.Reloc's we'll need
    
    		nr += loadObjSyms(l, syms, o.r)
    
    	// Make a first pass through the external symbols, making
    	// sure that each external symbol has a non-nil entry in
    	// l.Syms (note that relocations and symbol content will
    	// be copied in a later loop).
    	toConvert := make([]Sym, 0, l.max-l.extStart+1)
    
    	for i := l.extStart; i <= l.max; i++ {
    
    			s.Attr.Set(sym.AttrReachable, l.attrReachable.has(i))
    
    		if i != l.getOverwrite(i) {
    			continue
    		}
    
    		sname := l.RawSymName(i)
    		if !l.attrReachable.has(i) && !strings.HasPrefix(sname, "gofile..") { // XXX file symbols are used but not marked
    			continue
    		}
    		pp := l.getPayload(i)
    		nr += len(pp.relocs)
    		// create and install the sym.Symbol here so that l.Syms will
    		// be fully populated when we do relocation processing and
    		// outer/sub processing below.
    		s := l.allocSym(sname, 0)
    		l.installSym(i, s)
    		toConvert = append(toConvert, i)
    	}
    
    	// allocate a single large slab of relocations for all live symbols
    	l.relocBatch = make([]sym.Reloc, nr)
    
    	// convert payload-based external symbols into sym.Symbol-based
    	for _, i := range toConvert {
    
    		// Copy kind/size/value etc.
    		pp := &l.payloads[i-l.extStart]
    		s := l.Syms[i]
    		s.Version = int16(pp.ver)
    		s.Type = pp.kind
    		s.Size = pp.size
    		s.Value = l.SymValue(i)
    
    		if pp.gotype != 0 {
    			s.Gotype = l.Syms[pp.gotype]
    		}
    
    
    		// Copy relocations
    		batch := l.relocBatch
    		s.R = batch[:len(pp.relocs):len(pp.relocs)]
    		l.relocBatch = batch[len(pp.relocs):]
    		l.convertRelocations(pp.relocs, s)
    
    		// Copy data
    		s.P = pp.data
    
    		// Convert outer/sub relationships
    		if outer, ok := l.outer[i]; ok {
    			s.Outer = l.Syms[outer]
    		}
    		if sub, ok := l.sub[i]; ok {
    			s.Sub = l.Syms[sub]
    		}
    
    		// Preprocess symbol and set reachability and onlist.
    		preprocess(arch, s)
    		s.Attr.Set(sym.AttrReachable, l.attrReachable.has(i))
    		s.Attr.Set(sym.AttrOnList, l.attrOnList.has(i))
    
    		// Set sub-symbol attribute. FIXME: would be better
    		// to do away with this and just use l.OuterSymbol() != 0
    		// elsewhere within the linker.
    		s.Attr.Set(sym.AttrSubSymbol, s.Outer != nil)
    
    		// Copy over dynimplib, dynimpvers, extname.
    		if l.SymExtname(i) != "" {
    			s.SetExtname(l.SymExtname(i))
    		}
    		if l.SymDynimplib(i) != "" {
    			s.SetDynimplib(l.SymDynimplib(i))
    		}
    		if l.SymDynimpvers(i) != "" {
    			s.SetDynimpvers(l.SymDynimpvers(i))
    
    	// Note: resolution of ABI aliases is now also handled in
    	// loader.convertRelocations, so once the host object loaders move
    	// completely to loader.Sym, we can remove the code below.
    
    
    	// Resolve ABI aliases for external symbols. This is only
    	// needed for internal cgo linking.
    	// (The old code does this in deadcode, but deadcode2 doesn't
    	// do this.)
    	for i := l.extStart; i <= l.max; i++ {
    		if s := l.Syms[i]; s != nil && s.Attr.Reachable() {
    			for ri := range s.R {
    				r := &s.R[ri]
    				if r.Sym != nil && r.Sym.Type == sym.SABIALIAS {
    					r.Sym = r.Sym.R[0].Sym
    				}
    			}
    		}
    	}
    
    // ExtractSymbols grabs the symbols out of the loader for work that hasn't been
    // ported to the new symbol type.
    func (l *Loader) ExtractSymbols(syms *sym.Symbols) {
    	// Nil out overwritten symbols.
    	// Overwritten Go symbols aren't a problem (as they're lazy loaded), but
    	// symbols loaded from host object loaders are fully loaded, and we might
    	// have multiple symbols with the same name. This loop nils them out.
    	for oldI := range l.overwrite {
    		l.Syms[oldI] = nil
    	}
    
    
    	// Add symbols to the ctxt.Syms lookup table. This explicitly skips things
    	// created via loader.Create (marked with versions less than zero), since
    	// if we tried to add these we'd wind up with collisions. We do, however,
    	// add these symbols to the list of global symbols so that other future
    	// steps (like pclntab generation) can find these symbols if neceassary.
    	// Along the way, update the version from the negative anon version to
    	// something larger than sym.SymVerStatic (needed so that
    	// sym.symbol.IsFileLocal() works properly).
    
    	anonVerReplacement := syms.IncVersion()
    
    		if s == nil {
    			continue
    		}
    		if s.Name != "" && s.Version >= 0 {
    
    		} else {
    			syms.Allsym = append(syms.Allsym, s)
    
    		if s.Version < 0 {
    			s.Version = int16(anonVerReplacement)
    		}
    
    // allocSym allocates a new symbol backing.
    func (l *Loader) allocSym(name string, version int) *sym.Symbol {
    	batch := l.symBatch
    	if len(batch) == 0 {
    		batch = make([]sym.Symbol, 1000)
    	}
    	s := &batch[0]
    	l.symBatch = batch[1:]
    
    	s.Dynid = -1
    	s.Name = name
    	s.Version = int16(version)
    
    	return s
    }
    
    
    // installSym sets the underlying sym.Symbol for the specified sym index.
    func (l *Loader) installSym(i Sym, s *sym.Symbol) {
    	if s == nil {
    		panic("installSym nil symbol")
    	}
    	if l.Syms[i] != nil {
    
    		panic("sym already present in installSym")
    
    // addNewSym adds a new sym.Symbol to the i-th index in the list of symbols.
    
    func (l *Loader) addNewSym(i Sym, name string, ver int, unit *sym.CompilationUnit, t sym.SymKind) *sym.Symbol {
    	s := l.allocSym(name, ver)
    
    	if s.Type != 0 && s.Type != sym.SXREF {
    		fmt.Println("symbol already processed:", unit.Lib, i, s)
    		panic("symbol already processed")
    	}
    	if t == sym.SBSS && (s.Type == sym.SRODATA || s.Type == sym.SNOPTRBSS) {
    		t = s.Type
    	}
    	s.Type = t
    	s.Unit = unit
    
    // loadObjSyms creates sym.Symbol objects for the live Syms in the
    // object corresponding to object reader "r". Return value is the
    // number of sym.Reloc entries required for all the new symbols.
    func loadObjSyms(l *Loader, syms *sym.Symbols, r *oReader) int {
    
    
    	for i, n := 0, r.NSym()+r.NNonpkgdef(); i < n; i++ {
    
    		// If it's been previously loaded in host object loading, we don't need to do it again.
    		if s := l.Syms[istart+Sym(i)]; s != nil {
    			// Mark symbol as reachable as it wasn't marked as such before.
    
    			s.Attr.Set(sym.AttrReachable, l.attrReachable.has(istart+Sym(i)))
    
    		name := strings.Replace(osym.Name, "\"\".", r.pkgprefix, -1)
    		if name == "" {
    			continue
    
    		ver := abiToVer(osym.ABI, r.version)
    
    		if osym.ABI != goobj2.SymABIstatic && l.symsByName[ver][name] != istart+Sym(i) {
    
    		t := sym.AbiSymKindToSymKind[objabi.SymKind(osym.Type)]
    		if t == sym.SXREF {
    			log.Fatalf("bad sxref")
    		}
    		if t == 0 {
    
    			log.Fatalf("missing type for %s in %s", name, r.unit.Lib)
    
    		if !l.attrReachable.has(istart+Sym(i)) && !(t == sym.SRODATA && strings.HasPrefix(name, "type.")) && name != "runtime.addmoduledata" && name != "runtime.lastmoduledatap" {
    
    			// No need to load unreachable symbols.
    
    			// XXX some type symbol's content may be needed in DWARF code, but they are not marked.
    
    			// XXX reference to runtime.addmoduledata may be generated later by the linker in plugin mode.
    
    		s := l.addNewSym(istart+Sym(i), name, ver, r.unit, t)
    
    		s.Attr.Set(sym.AttrReachable, l.attrReachable.has(istart+Sym(i)))
    
    	return nr
    }
    
    // funcInfoSym records the sym.Symbol for a function, along with a copy
    // of the corresponding goobj2.Sym and the index of its FuncInfo aux sym.
    // We use this to delay populating FuncInfo until we can batch-allocate
    // slices for their sub-objects.
    type funcInfoSym struct {
    	s    *sym.Symbol // sym.Symbol for a live function
    	osym goobj2.Sym  // object file symbol data for that function
    	isym int         // global symbol index of FuncInfo aux sym for func
    }
    
    // funcAllocInfo records totals/counts for all functions in an objfile;
    // used to help with bulk allocation of sym.Symbol sub-objects.
    type funcAllocInfo struct {
    	symPtr  uint32 // number of *sym.Symbol's needed in file slices
    	inlCall uint32 // number of sym.InlinedCall's needed in inltree slices
    	pcData  uint32 // number of sym.Pcdata's needed in pdata slices
    	fdOff   uint32 // number of int64's needed in all Funcdataoff slices
    
    // loadSymbol loads a single symbol by name.
    
    // NB: This function does NOT set the symbol as reachable.
    
    func (l *Loader) loadSymbol(name string, version int) *sym.Symbol {
    
    	global := l.Lookup(name, version)
    
    	// If we're already loaded, bail.
    
    	if global != 0 && int(global) < len(l.Syms) && l.Syms[global] != nil {
    
    		return l.Syms[global]
    	}
    
    	// Read the symbol.
    	r, i := l.toLocal(global)
    	istart := l.startIndex(r)
    
    	osym := goobj2.Sym{}
    	osym.Read(r.Reader, r.SymOff(int(i)))
    	if l.symsByName[version][name] != istart+Sym(i) {
    		return nil
    	}
    
    
    	return l.addNewSym(istart+Sym(i), name, version, r.unit, sym.AbiSymKindToSymKind[objabi.SymKind(osym.Type)])
    
    // LookupOrCreate looks up a symbol by name, and creates one if not found.
    // Either way, it will also create a sym.Symbol for it, if not already.
    // This should only be called when interacting with parts of the linker
    // that still works on sym.Symbols (i.e. internal cgo linking, for now).
    
    func (l *Loader) LookupOrCreate(name string, version int) *sym.Symbol {
    
    	i := l.Lookup(name, version)
    	if i != 0 {
    		// symbol exists
    		if int(i) < len(l.Syms) && l.Syms[i] != nil {
    
    		}
    		if l.IsExternal(i) {
    			panic("Can't load an external symbol.")
    		}
    
    		return l.loadSymbol(name, version)
    
    	s := l.allocSym(name, version)
    
    // cloneToExternal takes the existing object file symbol (symIdx)
    // and creates a new external symbol that is a clone with respect
    // to name, version, type, relocations, etc. The idea here is that
    // if the linker decides it wants to update the contents of a
    // symbol originally discovered as part of an object file, it's
    // easier to do this if we make the updates to a new and similarly
    // named external copy of that symbol.
    func (l *Loader) cloneToExternal(symIdx Sym) Sym {
    	if l.IsExternal(symIdx) {
    		panic("sym is already external, no need for clone")
    	}
    
    	// Read the particulars from object.
    	osym := goobj2.Sym{}
    	r, li := l.toLocal(symIdx)
    	osym.Read(r.Reader, r.SymOff(li))
    	sname := strings.Replace(osym.Name, "\"\".", r.pkgprefix, -1)
    	sver := abiToVer(osym.ABI, r.version)
    	skind := sym.AbiSymKindToSymKind[objabi.SymKind(osym.Type)]
    
    	// Create new symbol, update version and kind.
    	ns := l.newExtSym(sname, sver)
    	pp := &l.payloads[ns-l.extStart]
    	pp.kind = skind
    	pp.ver = sver
    	pp.size = int64(osym.Siz)
    
    	// If this is a def, then copy the guts. We expect this case
    	// to be very rare (one case it may come up is with -X).
    	if li < (r.NSym() + r.NNonpkgdef()) {
    
    		// Copy relocations
    		relocs := l.Relocs(symIdx)
    		pp.relocs = relocs.ReadAll(nil)
    
    		// Copy data
    		pp.data = r.Data(li)
    
    		// Copy read-only attr
    		if r.ReadOnly() {
    			l.attrReadOnly[ns] = true
    		}
    	}
    
    
    	// If we're overriding a data symbol, collect the associated
    	// Gotype, so as to propagate it to the new symbol.
    	naux := r.NAux(li)
    	for j := 0; j < naux; j++ {
    		a := goobj2.Aux{}
    		a.Read(r.Reader, r.AuxOff(li, j))
    		switch a.Type {
    		case goobj2.AuxGotype:
    			pp.gotype = l.resolve(r, a.Sym)
    		default:
    			log.Fatalf("internal error: cloneToExternal applied to %s symbol %s with non-gotype aux data %d", skind.String(), sname, a.Type)
    		}
    	}
    
    
    	// Fix up the lookup tables if the symbol in question was
    	// present in the lookup tables. At the moment it only makes
    	// sense to do this sort of clone/update for symbols that are
    	// in the symbol table (as opposed to anonymous symbols);
    	// issue an error if we can't look up the original symbol.
    	if sver >= sym.SymVerStatic {
    		s, ok := l.extStaticSyms[nameVer{sname, sver}]
    		if !ok || s != symIdx {
    			panic("lookup failed for clone of non-external static symbol")
    		}
    		l.extStaticSyms[nameVer{sname, sver}] = ns
    	} else {
    		s, ok := l.symsByName[sver][sname]
    		if !ok || s != symIdx {
    			panic("lookup failed for clone of non-external symbol")
    		}
    		l.symsByName[sver][sname] = ns
    	}
    
    	// Add an overwrite entry (in case there are relocations against
    	// the old symbol).
    	l.overwrite[symIdx] = ns
    
    	// FIXME: copy other attributes? reachable is the main one, and we
    	// don't expect it to be set at this point.
    
    	return ns
    }
    
    
    // CreateExtSym creates a new external symbol with the specified name
    // without adding it to any lookup tables, returning a Sym index for it.
    func (l *Loader) CreateExtSym(name string) Sym {
    
    	// Assign a new unique negative version -- this is to mark the
    	// symbol so that it can be skipped when ExtractSymbols is adding
    	// ext syms to the sym.Symbols hash.
    	l.anonVersion--
    	return l.newExtSym(name, l.anonVersion)
    
    // Create creates a symbol with the specified name, returning a
    // sym.Symbol object for it. This method is intended for static/hidden
    // symbols discovered while loading host objects. We can see more than
    // one instance of a given static symbol with the same name/version,
    // so we can't add them to the lookup tables "as is". Instead assign
    // them fictitious (unique) versions, starting at -1 and decreasing by
    // one for each newly created symbol, and record them in the
    // extStaticSyms hash.
    
    func (l *Loader) Create(name string) *sym.Symbol {
    
    	i := l.max + 1
    	l.max++
    	if l.extStart == 0 {
    		l.extStart = i
    	}
    
    
    	// Assign a new unique negative version -- this is to mark the
    	// symbol so that it can be skipped when ExtractSymbols is adding
    	// ext syms to the sym.Symbols hash.
    	l.anonVersion--
    	ver := l.anonVersion
    
    	s := l.allocSym(name, ver)
    
    	l.extStaticSyms[nameVer{name, ver}] = i
    
    
    func loadObjFull(l *Loader, r *oReader) {
    	lib := r.unit.Lib
    
    
    	resolveSymRef := func(s goobj2.SymRef) *sym.Symbol {
    
    	funcs := []funcInfoSym{}
    	fdsyms := []*sym.Symbol{}
    	var funcAllocCounts funcAllocInfo
    
    	for i, n := 0, r.NSym()+r.NNonpkgdef(); i < n; i++ {
    		osym := goobj2.Sym{}
    		osym.Read(r.Reader, r.SymOff(i))
    		name := strings.Replace(osym.Name, "\"\".", r.pkgprefix, -1)
    
    		if name == "" {
    			continue
    		}
    		ver := abiToVer(osym.ABI, r.version)