Skip to content
Snippets Groups Projects
loader.go 78.2 KiB
Newer Older
  • Learn to ignore specific revisions
  • 		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 >= Sym(len(l.objSyms)) || 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 >= Sym(len(l.objSyms)) || 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 >= Sym(len(l.objSyms)) || i == 0 {
    
    		panic("bad symbol index in SetExtname")
    	}
    	if value == "" {
    		delete(l.extname, i)
    	} else {
    		l.extname[i] = value
    	}
    }
    
    
    // SymElfType returns the previously recorded ELF type for a symbol
    // (used only for symbols read from shared libraries by ldshlibsyms).
    // It is not set for symbols defined by the packages being linked or
    // by symbols read by ldelf (and so is left as elf.STT_NOTYPE).
    func (l *Loader) SymElfType(i Sym) elf.SymType {
    	if et, ok := l.elfType[i]; ok {
    		return et
    	}
    	return elf.STT_NOTYPE
    }
    
    
    // SetSymElfType sets the elf type attribute for a symbol.
    
    func (l *Loader) SetSymElfType(i Sym, et elf.SymType) {
    	// reject bad symbols
    
    	if i >= Sym(len(l.objSyms)) || i == 0 {
    
    		panic("bad symbol index in SetSymElfType")
    	}
    	if et == elf.STT_NOTYPE {
    		delete(l.elfType, i)
    	} else {
    		l.elfType[i] = et
    	}
    }
    
    
    // SetPlt sets the plt value for pe symbols.
    func (l *Loader) SetPlt(i Sym, v int32) {
    
    	if i >= Sym(len(l.objSyms)) || i == 0 {
    
    		panic("bad symbol for SetPlt")
    	}
    	if v == 0 {
    		delete(l.plt, i)
    	} else {
    		l.plt[i] = v
    	}
    }
    
    // SetGot sets the got value for pe symbols.
    func (l *Loader) SetGot(i Sym, v int32) {
    
    	if i >= Sym(len(l.objSyms)) || i == 0 {
    
    		panic("bad symbol for SetPlt")
    	}
    	if v == 0 {
    		delete(l.got, i)
    	} else {
    		l.got[i] = v
    	}
    }
    
    
    // SymGoType returns the 'Gotype' property for a given symbol (set by
    // the Go compiler for variable symbols). This version relies on
    // reading aux symbols for the target sym -- it could be that a faster
    // approach would be to check for gotype during preload and copy the
    // results in to a map (might want to try this at some point and see
    // if it helps speed things up).
    func (l *Loader) SymGoType(i Sym) Sym {
    	if l.IsExternal(i) {
    		pp := l.getPayload(i)
    		return pp.gotype
    	}
    	r, li := l.toLocal(i)
    	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:
    			return l.resolve(r, a.Sym)
    		}
    	}
    	return 0
    }
    
    // SymUnit returns the compilation unit for a given symbol (which will
    // typically be nil for external or linker-manufactured symbols).
    func (l *Loader) SymUnit(i Sym) *sym.CompilationUnit {
    	if l.IsExternal(i) {
    		pp := l.getPayload(i)
    		if pp.objidx != 0 {
    			r := l.objs[pp.objidx].r
    			return r.unit
    		}
    		return nil
    	}
    	r, _ := l.toLocal(i)
    	return r.unit
    }
    
    
    // SymFile returns the file for a symbol, which is normally the
    // package the symbol came from (for regular compiler-generated Go
    // symbols), but in the case of building with "-linkshared" (when a
    // symbol is read from a a shared library), will hold the library
    // name.
    func (l *Loader) SymFile(i Sym) string {
    	if l.IsExternal(i) {
    		if f, ok := l.symFile[i]; ok {
    			return f
    		}
    		pp := l.getPayload(i)
    		if pp.objidx != 0 {
    			r := l.objs[pp.objidx].r
    			return r.unit.Lib.File
    		}
    		return ""
    	}
    	r, _ := l.toLocal(i)
    	return r.unit.Lib.File
    }
    
    // SetSymFile sets the file attribute for a symbol. This is
    // needed mainly for external symbols, specifically those imported
    // from shared libraries.
    func (l *Loader) SetSymFile(i Sym, file string) {
    	// reject bad symbols
    
    	if i >= Sym(len(l.objSyms)) || i == 0 {
    
    		panic("bad symbol index in SetSymFile")
    	}
    	if !l.IsExternal(i) {
    		panic("can't set file for non-external sym")
    	}
    	l.symFile[i] = file
    }
    
    
    // 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 >= Sym(len(l.objSyms)) || 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))
    
    // GetFuncDwarfAuxSyms collects and returns the auxiliary DWARF
    // symbols associated with a given function symbol.  Prior to the
    // introduction of the loader, this was done purely using name
    // lookups, e.f. for function with name XYZ we would then look up
    // go.info.XYZ, etc.
    // FIXME: once all of dwarfgen is converted over to the loader,
    // it would save some space to make these aux symbols nameless.
    func (l *Loader) GetFuncDwarfAuxSyms(fnSymIdx Sym) (auxDwarfInfo, auxDwarfLoc, auxDwarfRanges, auxDwarfLines Sym) {
    	if l.SymType(fnSymIdx) != sym.STEXT {
    		log.Fatalf("error: non-function sym %d/%s t=%s passed to GetFuncDwarfAuxSyms", fnSymIdx, l.SymName(fnSymIdx), l.SymType(fnSymIdx).String())
    	}
    	if l.IsExternal(fnSymIdx) {
    		// Current expectation is that any external function will
    		// not have auxsyms.
    		return
    	}
    	naux := l.NAux(fnSymIdx)
    	if naux == 0 {
    		return
    	}
    	r, li := l.toLocal(fnSymIdx)
    	for i := 0; i < naux; i++ {
    		a := goobj2.Aux{}
    		a.Read(r.Reader, r.AuxOff(li, i))
    		switch a.Type {
    		case goobj2.AuxDwarfInfo:
    			auxDwarfInfo = l.resolve(r, a.Sym)
    			if l.SymType(auxDwarfInfo) != sym.SDWARFINFO {
    				panic("aux dwarf info sym with wrong type")
    			}
    		case goobj2.AuxDwarfLoc:
    			auxDwarfLoc = l.resolve(r, a.Sym)
    			if l.SymType(auxDwarfLoc) != sym.SDWARFLOC {
    				panic("aux dwarf loc sym with wrong type")
    			}
    		case goobj2.AuxDwarfRanges:
    			auxDwarfRanges = l.resolve(r, a.Sym)
    			if l.SymType(auxDwarfRanges) != sym.SDWARFRANGE {
    				panic("aux dwarf ranges sym with wrong type")
    			}
    		case goobj2.AuxDwarfLines:
    			auxDwarfLines = l.resolve(r, a.Sym)
    			if l.SymType(auxDwarfLines) != sym.SDWARFLINES {
    				panic("aux dwarf lines sym with wrong type")
    			}
    		}
    	}
    	return
    }
    
    
    // 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)
    
    		a.ReadSym(r.Reader, r.AuxOff(li, i))
    
    // 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) {
    	// 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 {
    
    	// 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 {
    
    	// 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)
    
    		l.attrLocal = growBitmap(reqLen, l.attrLocal)
    
    		l.attrNotInSymbolTable = growBitmap(reqLen, l.attrNotInSymbolTable)
    
    	l.growExtAttrBitmaps()
    }
    
    func (l *Loader) growExtAttrBitmaps() {
    	// These are indexed by external symbol index (e.g. l.extIndex(i))
    	extReqLen := len(l.payloads)
    
    	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 relocs.l.isExtReader(relocs.r) {
    		pp := relocs.l.payloads[relocs.li]
    
    	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 {
    
    	return relocs.readAll(dst, false)
    }
    
    // ReadSyms method reads all relocation target symbols and reloc types
    // for a symbol into the specified slice. It is like ReadAll but only
    // fill in the Sym and Type fields.
    func (relocs *Relocs) ReadSyms(dst []Reloc) []Reloc {
    	return relocs.readAll(dst, true)
    }
    
    func (relocs *Relocs) readAll(dst []Reloc, onlySymType bool) []Reloc {
    
    	}
    
    	if cap(dst) < relocs.Count {
    		dst = make([]Reloc, relocs.Count)
    	}
    	dst = dst[:0]
    
    
    	if relocs.l.isExtReader(relocs.r) {
    		pp := relocs.l.payloads[relocs.li]
    
    	off := relocs.r.RelocOff(relocs.li, 0)
    
    		if onlySymType {
    			rel.ReadSymType(relocs.r.Reader, off)
    		} else {
    			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 {
    
    	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 {
    
    	var n int
    	if l.isExtReader(r) {
    		pp := l.payloads[li]
    		n = len(pp.relocs)
    	} else {
    		n = r.NReloc(li)
    	}
    
    // 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 }
    
    
    // FuncInfo provides hooks to access goobj2.FuncInfo in the objects.
    type FuncInfo struct {
    	l    *Loader
    	r    *oReader
    	data []byte
    }
    
    func (fi *FuncInfo) Valid() bool { return fi.r != nil }
    
    func (fi *FuncInfo) Locals() int {
    	return int((*goobj2.FuncInfo)(nil).ReadLocals(fi.data))
    }
    
    func (fi *FuncInfo) Pcsp() []byte {
    	pcsp, end := (*goobj2.FuncInfo)(nil).ReadPcsp(fi.data)
    	return fi.r.BytesAt(fi.r.PcdataBase()+pcsp, int(end-pcsp))
    }
    
    // TODO: more accessors.
    
    func (l *Loader) FuncInfo(i Sym) FuncInfo {
    	if l.IsExternal(i) {
    		return FuncInfo{}
    	}
    	r, li := l.toLocal(i)
    	n := r.NAux(li)
    	for j := 0; j < n; j++ {
    		a := goobj2.Aux{}
    		a.Read(r.Reader, r.AuxOff(li, j))
    		if a.Type == goobj2.AuxFuncInfo {
    			b := r.Data(int(a.Sym.SymIdx))
    			return FuncInfo{l, r, b}
    		}
    	}
    	return FuncInfo{}
    }
    
    
    // Preload a package: add autolibs, add defined package symbols to the symbol table.
    // Does not add non-package symbols yet, which will be done in LoadNonpkgSyms.
    // Does not read symbol data.
    
    func (l *Loader) Preload(syms *sym.Symbols, f *bio.Reader, lib *sym.Library, unit *sym.CompilationUnit, length int64, 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) + "."
    
    	ndef := r.NSym()
    	nnonpkgdef := r.NNonpkgdef()
    
    	or := &oReader{r, unit, localSymVersion, r.Flags(), pkgprefix, make([]Sym, ndef+nnonpkgdef+r.NNonpkgref()), ndef, uint32(len(l.objs))}
    
    	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)
    	}
    
    
    	l.addObj(lib.Pkg, or)
    	l.preloadSyms(or, pkgDef)
    
    	// The caller expects us consuming all the data
    	f.MustSeek(length, os.SEEK_CUR)
    }
    
    // Preload symbols of given kind from an object.
    func (l *Loader) preloadSyms(r *oReader, kind int) {
    	ndef := r.NSym()
    	nnonpkgdef := r.NNonpkgdef()
    	var start, end int
    	switch kind {
    	case pkgDef:
    		start = 0
    		end = ndef
    	case nonPkgDef:
    		start = ndef
    		end = ndef + nnonpkgdef
    	default:
    		panic("preloadSyms: bad kind")
    	}
    	l.growSyms(len(l.objSyms) + end - start)
    	l.growAttrBitmaps(len(l.objSyms) + end - start)
    	for i := start; i < end; i++ {
    
    		osym.Read(r.Reader, r.SymOff(i))
    		name := strings.Replace(osym.Name, "\"\".", r.pkgprefix, -1)
    		v := abiToVer(osym.ABI, r.version)
    
    		gi, added := l.AddSym(name, v, r, i, kind, dupok, sym.AbiSymKindToSymKind[objabi.SymKind(osym.Type)])
    		r.syms[i] = gi
    
    		if osym.TopFrame() {
    			l.SetAttrTopFrame(gi, true)
    		}
    
    		if strings.HasPrefix(name, "go.itablink.") {
    
    			l.itablink[gi] = struct{}{}
    
    		if strings.HasPrefix(name, "runtime.") {
    
    			if bi := goobj2.BuiltinIdx(name, v); bi != -1 {
    				// This is a definition of a builtin symbol. Record where it is.
    
    		if strings.HasPrefix(name, "go.string.") ||
    
    			strings.HasPrefix(name, "gclocals·") ||
    
    			strings.HasPrefix(name, "runtime.gcbits.") {
    
    			l.SetAttrNotInSymbolTable(gi, true)
    
    // Add non-package symbols and references to external symbols (which are always
    // named).
    
    func (l *Loader) LoadNonpkgSyms(syms *sym.Symbols) {
    
    	for _, o := range l.objs[1:] {
    		l.preloadSyms(o.r, nonPkgDef)
    	}
    
    func loadObjRefs(l *Loader, r *oReader, 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)
    
    		r.syms[ndef+i] = l.AddExtSym(name, v)
    
    	}
    }
    
    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, len(l.payloads))
    	for _, i := range l.extReader.syms {
    
    		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. Note that once we do this,
    		// we'll need to get at the payload for a symbol with direct
    		// reference to l.payloads[] as opposed to calling l.getPayload().
    
    		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[l.extIndex(i)]
    
    		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]
    		}
    
    		s.Value = l.values[i]
    		if f, ok := l.symFile[i]; ok {
    			s.File = f
    		} else if pp.objidx != 0 {
    			s.File = l.objs[pp.objidx].r.unit.Lib.File
    		}
    
    
    		// 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, false)
    
    		// Transfer over attributes.
    		l.migrateAttributes(i, s)
    
    		// Preprocess symbol. May set 'AttrLocal'.
    
    	// 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 := range l.extReader.syms {
    
    		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
    				}
    			}
    		}
    	}
    
    // PropagateSymbolChangesBackToLoader is a temporary shim function
    // that copies over a given sym.Symbol into the equivalent representation
    // in the loader world. The intent is to enable converting a given
    // linker phase/pass from dealing with sym.Symbol's to a modernized
    // pass that works with loader.Sym, in cases where the "loader.Sym
    // wavefront" has not yet reached the pass in question. For such work
    // the recipe is to first call PropagateSymbolChangesBackToLoader(),
    // then exexute the pass working with the loader, then call
    // PropagateLoaderChangesToSymbols to copy the changes made by the
    // pass back to the sym.Symbol world.
    func (l *Loader) PropagateSymbolChangesBackToLoader() {
    
    	// For the moment we only copy symbol values, and we don't touch
    	// any new sym.Symbols created since loadlibfull() was run. This
    	// seems to be what's needed for DWARF gen.
    	for i := Sym(1); i < Sym(len(l.objSyms)); i++ {
    		s := l.Syms[i]
    		if s != nil {
    			if s.Value != l.SymValue(i) {
    				l.SetSymValue(i, s.Value)
    			}
    		}
    	}
    }
    
    // PropagateLoaderChangesToSymbols is a temporary shim function that
    // takes a list of loader.Sym symbols and works to copy their contents
    // and attributes over to a corresponding sym.Symbol. See the
    // PropagateSymbolChangesBackToLoader header comment for more info.
    //
    // WARNING: this function is brittle and depends heavily on loader
    // implementation. A key problem with doing this is that as things
    // stand at the moment, some sym.Symbol contents/attributes are
    // populated only when converting from loader.Sym to sym.Symbol
    // in loadlibfull, meaning if we may wipe out some information
    // when copying back.
    
    func (l *Loader) PropagateLoaderChangesToSymbols(toconvert []Sym, syms *sym.Symbols) []*sym.Symbol {
    
    	result := []*sym.Symbol{}
    	relocfixup := []Sym{}
    
    	// Note: this loop needs to allow for the possibility that we may
    	// see "new" symbols on the 'toconvert' list that come from object
    	// files (for example, DWARF location lists), as opposed to just
    	// newly manufactured symbols (ex: DWARF section symbols such as
    	// ".debug_info").  This means that we have to be careful not to
    	// stomp on sym.Symbol attributes/content that was set up in
    	// in loadlibfull().
    
    	// Also note that in order for the relocation fixup to work, we
    	// have to do this in two passes -- one pass to create the symbols,
    	// and then a second fix up the relocations once all necessary
    	// sym.Symbols are created.
    
    	// First pass, symbol creation and symbol data fixup.
    	anonVerReplacement := syms.IncVersion()
    	rslice := []Reloc{}
    	for _, cand := range toconvert {
    
    		sn := l.SymName(cand)
    		sv := l.SymVersion(cand)
    
    		if sv < 0 {
    			sv = anonVerReplacement
    		}
    
    		s := l.Syms[cand]
    
    		isnew := false
    		if sn == "" {
    			// Don't install anonymous symbols in the lookup tab.
    			if s == nil {
    
    				l.installSym(cand, s)
    			}
    			isnew = true
    		} else {
    			if s != nil {
    				// Already have a symbol for this -- it must be
    				// something that was previously processed by
    				// loadObjFull. Note that the symbol in question may
    				// or may not be in the name lookup map.
    			} else {
    				isnew = true
    				s = syms.Lookup(sn, sv)
    			}
    		}
    		result = append(result, s)
    
    		// Always copy these from new to old.
    		s.Value = l.SymValue(cand)
    
    
    		// If the data for a symbol has increased in size, make sure
    		// we bring the new content across.
    		relfix := isnew
    		if isnew || len(l.Data(cand)) > len(s.P) {
    			s.P = l.Data(cand)
    			s.Size = int64(len(s.P))
    			relfix = true
    		}
    
    		// For 'new' symbols, copy other content (such as Gotype,
    		// sym file, relocations, etc).
    		if isnew {
    			if gt := l.SymGoType(cand); gt != 0 {
    				s.Gotype = l.Syms[gt]
    			}
    			if f, ok := l.symFile[cand]; ok {
    				s.File = f
    			} else {
    				r, _ := l.toLocal(cand)
    				if r != nil && r != l.extReader {
    					s.File = l.SymFile(cand)
    				}
    			}
    		}
    
    		// If this symbol has any DWARF file relocations, we need to
    		// make sure that the relocations are copied back over, since
    
    		// DWARF-gen alters the offset values for these relocs. Also:
    		// if this is an info symbol and it refers to a previously
    		// unseen range/loc symbol, we'll need to fix up relocations
    		// for it as well.
    
    		relocs := l.Relocs(cand)
    		rslice = relocs.ReadSyms(rslice)
    		for ri := range rslice {
    			if rslice[ri].Type == objabi.R_DWARFFILEREF {
    				relfix = true
    				break
    			}
    
    			if st != sym.SDWARFINFO {
    				continue
    			}
    			rst := l.SymType(rslice[ri].Sym)
    			if rst == sym.SDWARFRANGE || rst == sym.SDWARFLOC {
    				relfix = true
    				break
    			}
    
    		}
    
    		if relfix {
    			relocfixup = append(relocfixup, cand)
    		}
    
    		// If new symbol, call a helper to migrate attributes.
    		// Otherwise touch only not-in-symbol-table, since there are
    		// some attrs that are only set up at the point where we
    		// convert loader.Sym to sym.Symbol.
    		if isnew {
    			l.migrateAttributes(cand, s)
    		} else {
    			if l.AttrNotInSymbolTable(cand) {
    				s.Attr.Set(sym.AttrNotInSymbolTable, true)
    			}
    		}
    	}
    
    	// Second pass to fix up relocations.
    	for _, cand := range relocfixup {
    		s := l.Syms[cand]
    		relocs := l.Relocs(cand)
    		rslice = relocs.ReadAll(rslice)
    		s.R = make([]sym.Reloc, len(rslice))
    		l.convertRelocations(rslice, s, true)
    	}
    
    	return result
    }
    
    
    // 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, rp map[*sym.Symbol]*sym.Symbol) {
    
    	// 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()
    
    		syms.Allsym = append(syms.Allsym, s) // XXX still add to Allsym for now, as there are code looping through Allsym
    
    		if s.Version < 0 {
    			s.Version = int16(anonVerReplacement)
    		}
    
    
    	for i, s := range l.Reachparent {
    		if i == 0 {
    			continue
    		}
    		rp[l.Syms[i]] = l.Syms[s]
    	}
    
    
    	// Provide lookup functions for sym.Symbols.
    	syms.Lookup = func(name string, ver int) *sym.Symbol {
    		i := l.LookupOrCreateSym(name, ver)
    		if s := l.Syms[i]; s != nil {