Skip to content
Snippets Groups Projects
loader.go 50.5 KiB
Newer Older
  • Learn to ignore specific revisions
  • 	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,
    	}
    }
    
    
    // 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)
    
    	// allocate a single large slab of relocations for all live symbols
    	l.relocBatch = make([]sym.Reloc, nr)
    
    
    	// external symbols
    	for i := l.extStart; i <= l.max; i++ {
    
    			s.Attr.Set(sym.AttrReachable, l.attrReachable.has(i))
    
    			continue // already loaded from external object
    		}
    
    		sname := l.payloads[i-l.extStart].name
    		sver := l.payloads[i-l.extStart].ver
    
    		if l.attrReachable.has(i) || strings.HasPrefix(sname, "gofile..") { // XXX file symbols are used but not marked
    
    			s := l.allocSym(sname, sver)
    			pp := l.getPayload(i)
    			if pp != nil {
    				if pp.kind != sym.Sxxx || len(pp.relocs) != 0 || len(pp.data) != 0 {
    					// Unpack payload into sym. Currently there is nothing
    					// to do here, but eventually we'll need a real
    					// implementation.
    					panic("need to handle this")
    				}
    			}
    
    			s.Attr.Set(sym.AttrReachable, l.attrReachable.has(i))
    
    
    	// 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")
    
    	}
    	if l.IsExternal(i) {
    		// temporary sanity check: make sure that the payload
    		// is empty, e.g. nobody has added symbol content already.
    		pp := l.getPayload(i)
    		if pp != nil && (len(pp.relocs) != 0 || len(pp.data) != 0) {
    			panic("expected empty payload")
    		}
    	}
    	l.Syms[i] = s
    }
    
    
    // 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)
    
    // 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 {
    	return l.newExtSym(name, sym.SymVerABI0)
    }
    
    
    // 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)
    
    			if dupsym := l.symsByName[ver][name]; dupsym != istart+Sym(i) {
    
    				if l.attrReachable.has(dupsym) {
    
    					// A dupok symbol is resolved to another package. We still need
    					// to record its presence in the current package, as the trampoline
    					// pass expects packages are laid out in dependency order.
    					s := l.Syms[dupsym]
    					if s.Type == sym.STEXT {
    						lib.DupTextSyms = append(lib.DupTextSyms, s)
    					}
    
    		if s.Name != name { // Sanity check. We can remove it in the final version.
    			fmt.Println("name mismatch:", lib, i, s.Name, name)
    			panic("name mismatch")
    		}
    
    
    		local := osym.Local()
    		makeTypelink := osym.Typelink()
    
    		size := osym.Siz
    
    		// Symbol data
    		s.P = r.Data(i)
    		s.Attr.Set(sym.AttrReadOnly, r.ReadOnly())
    
    		batch := l.relocBatch
    		s.R = batch[:relocs.Count:relocs.Count]
    		l.relocBatch = batch[relocs.Count:]
    
    			if rt == objabi.R_METHODOFF {
    
    					rt = objabi.R_ADDROFF
    				} else {
    					sz = 0
    					rs = 0
    				}
    			}
    
    			if rt == objabi.R_WEAKADDROFF && !l.attrReachable.has(rs) {
    
    			if rs != 0 && l.Syms[rs] != nil && l.Syms[rs].Type == sym.SABIALIAS {
    
    				rsrelocs := l.Relocs(rs)
    				rs = rsrelocs.At(0).Sym
    
    				Sym:  l.Syms[rs],
    
    		naux := r.NAux(i)
    		for j := 0; j < naux; j++ {
    			a := goobj2.Aux{}
    
    			switch a.Type {
    			case goobj2.AuxGotype:
    				typ := resolveSymRef(a.Sym)
    				if typ != nil {
    					s.Gotype = typ
    				}
    
    				fdsyms = append(fdsyms, resolveSymRef(a.Sym))
    
    			case goobj2.AuxFuncInfo:
    				if a.Sym.PkgIdx != goobj2.PkgIdxSelf {
    					panic("funcinfo symbol not defined in current package")
    				}
    				isym = int(a.Sym.SymIdx)
    
    			case goobj2.AuxDwarfInfo, goobj2.AuxDwarfLoc, goobj2.AuxDwarfRanges, goobj2.AuxDwarfLines:
    				// ignored for now
    
    		s.File = r.pkgprefix[:len(r.pkgprefix)-1]
    
    		if dupok {
    			s.Attr |= sym.AttrDuplicateOK
    		}
    		if s.Size < int64(size) {
    			s.Size = int64(size)
    		}
    		s.Attr.Set(sym.AttrLocal, local)
    		s.Attr.Set(sym.AttrMakeTypelink, makeTypelink)
    
    
    		if s.Type == sym.SDWARFINFO {
    			// For DWARF symbols, replace `"".` to actual package prefix
    			// in the symbol content.
    			// TODO: maybe we should do this in the compiler and get rid
    			// of this.
    			patchDWARFName(s, r)
    		}
    
    
    		if s.Type != sym.STEXT {
    			continue
    		}
    
    		if isym == -1 {
    			continue
    		}
    
    
    		// Record function sym and associated info for additional
    		// processing in the loop below.
    		fwis := funcInfoSym{s: s, isym: isym, osym: osym}
    		funcs = append(funcs, fwis)
    
    		// Read the goobj2.FuncInfo for this text symbol so that we can
    		// collect allocation counts. We'll read it again in the loop
    		// below.
    		b := r.Data(isym)
    		info := goobj2.FuncInfo{}
    		info.Read(b)
    		funcAllocCounts.symPtr += uint32(len(info.File))
    		funcAllocCounts.pcData += uint32(len(info.Pcdata))
    		funcAllocCounts.inlCall += uint32(len(info.InlTree))
    		funcAllocCounts.fdOff += uint32(len(info.Funcdataoff))
    	}
    
    	// At this point we can do batch allocation of the sym.FuncInfo's,
    	// along with the slices of sub-objects they use.
    	fiBatch := make([]sym.FuncInfo, len(funcs))
    	inlCallBatch := make([]sym.InlinedCall, funcAllocCounts.inlCall)
    	symPtrBatch := make([]*sym.Symbol, funcAllocCounts.symPtr)
    	pcDataBatch := make([]sym.Pcdata, funcAllocCounts.pcData)
    	fdOffBatch := make([]int64, funcAllocCounts.fdOff)
    
    	// Populate FuncInfo contents for func symbols.
    	for fi := 0; fi < len(funcs); fi++ {
    		s := funcs[fi].s
    		isym := funcs[fi].isym
    		osym := funcs[fi].osym
    
    		s.FuncInfo = &fiBatch[0]
    		fiBatch = fiBatch[1:]
    
    
    		info := goobj2.FuncInfo{}
    		info.Read(b)
    
    		if info.NoSplit != 0 {
    			s.Attr |= sym.AttrNoSplit
    		}
    
    			s.Attr |= sym.AttrReflectMethod
    		}
    
    		if r.Flags()&goobj2.ObjFlagShared != 0 {
    
    			s.Attr |= sym.AttrShared
    		}
    
    
    		if len(info.Funcdataoff) != 0 {
    			nfd := len(info.Funcdataoff)
    			pc.Funcdata = fdsyms[:nfd:nfd]
    			fdsyms = fdsyms[nfd:]
    
    
    		info.Pcdata = append(info.Pcdata, info.PcdataEnd) // for the ease of knowing where it ends
    
    		pc.Args = int32(info.Args)
    		pc.Locals = int32(info.Locals)
    
    
    		npc := len(info.Pcdata) - 1 // -1 as we appended one above
    		pc.Pcdata = pcDataBatch[:npc:npc]
    		pcDataBatch = pcDataBatch[npc:]
    
    		nfd := len(info.Funcdataoff)
    		pc.Funcdataoff = fdOffBatch[:nfd:nfd]
    		fdOffBatch = fdOffBatch[nfd:]
    
    		nsp := len(info.File)
    		pc.File = symPtrBatch[:nsp:nsp]
    		symPtrBatch = symPtrBatch[nsp:]
    
    		nic := len(info.InlTree)
    		pc.InlTree = inlCallBatch[:nic:nic]
    		inlCallBatch = inlCallBatch[nic:]
    
    
    		pc.Pcsp.P = r.BytesAt(pcdataBase+info.Pcsp, int(info.Pcfile-info.Pcsp))
    		pc.Pcfile.P = r.BytesAt(pcdataBase+info.Pcfile, int(info.Pcline-info.Pcfile))
    		pc.Pcline.P = r.BytesAt(pcdataBase+info.Pcline, int(info.Pcinline-info.Pcline))
    		pc.Pcinline.P = r.BytesAt(pcdataBase+info.Pcinline, int(info.Pcdata[0]-info.Pcinline))
    		for k := range pc.Pcdata {
    			pc.Pcdata[k].P = r.BytesAt(pcdataBase+info.Pcdata[k], int(info.Pcdata[k+1]-info.Pcdata[k]))
    		}
    
    			pc.Funcdataoff[k] = int64(info.Funcdataoff[k])
    		}
    		for k := range pc.File {
    			pc.File[k] = resolveSymRef(info.File[k])
    		}
    
    		for k := range pc.InlTree {
    			inl := &info.InlTree[k]
    			pc.InlTree[k] = sym.InlinedCall{
    				Parent:   inl.Parent,
    				File:     resolveSymRef(inl.File),
    				Line:     inl.Line,
    
    				Func:     l.SymName(l.resolve(r, inl.Func)),
    
    		if !dupok {
    			if s.Attr.OnList() {
    				log.Fatalf("symbol %s listed multiple times", s.Name)
    			}
    			s.Attr.Set(sym.AttrOnList, true)
    			lib.Textp = append(lib.Textp, s)
    		} else {
    
    			// there may be a dup in another package
    
    			// put into a temp list and add to text later
    			lib.DupTextSyms = append(lib.DupTextSyms, s)
    		}
    
    func patchDWARFName1(p []byte, r *oReader) ([]byte, int) {
    
    	// This is kind of ugly. Really the package name should not
    	// even be included here.
    
    	if len(p) < 1 || p[0] != dwarf.DW_ABRV_FUNCTION {
    		return p, -1
    
    	if !bytes.Contains(p[:e], emptyPkg) {
    		return p, -1
    
    	patched := bytes.Replace(p[:e], emptyPkg, pkgprefix, -1)
    	return append(patched, p[e:]...), e
    }
    
    func patchDWARFName(s *sym.Symbol, r *oReader) {
    	patched, e := patchDWARFName1(s.P, r)
    	if e == -1 {
    		return
    	}
    	s.P = patched
    
    	s.Attr.Set(sym.AttrReadOnly, false)
    	delta := int64(len(s.P)) - s.Size
    	s.Size = int64(len(s.P))
    	for i := range s.R {
    		r := &s.R[i]
    		if r.Off > int32(e) {
    			r.Off += int32(delta)
    		}
    	}
    }
    
    
    // For debugging.
    func (l *Loader) Dump() {
    	fmt.Println("objs")
    	for _, obj := range l.objs {
    		if obj.r != nil {
    			fmt.Println(obj.i, obj.r.unit.Lib)
    		}
    	}
    
    	fmt.Println("syms")
    	for i, s := range l.Syms {
    		if i == 0 {
    			continue
    		}
    		if s != nil {
    			fmt.Println(i, s, s.Type)
    		} else {
    			fmt.Println(i, l.SymName(Sym(i)), "<not loaded>")
    		}
    	}
    	fmt.Println("overwrite:", l.overwrite)
    	fmt.Println("symsByName")
    
    	for name, i := range l.symsByName[0] {
    		fmt.Println(i, name, 0)
    	}
    	for name, i := range l.symsByName[1] {
    		fmt.Println(i, name, 1)