Debian Codegraph @

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

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