objfile.go 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871
  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. // This package defines the Go object file format, and provide "low-level" functions
  5. // for reading and writing object files.
  6. // The object file is understood by the compiler, assembler, linker, and tools. They
  7. // have "high level" code that operates on object files, handling application-specific
  8. // logics, and use this package for the actual reading and writing. Specifically, the
  9. // code below:
  10. //
  11. // - cmd/internal/obj/objfile.go (used by cmd/asm and cmd/compile)
  12. // - cmd/internal/objfile/goobj.go (used cmd/nm, cmd/objdump)
  13. // - cmd/link/internal/loader package (used by cmd/link)
  14. //
  15. // If the object file format changes, they may (or may not) need to change.
  16. package goobj
  17. import (
  18. "bytes"
  19. "github.com/twitchyliquid64/golang-asm/bio"
  20. "crypto/sha1"
  21. "encoding/binary"
  22. "errors"
  23. "fmt"
  24. "github.com/twitchyliquid64/golang-asm/unsafeheader"
  25. "io"
  26. "unsafe"
  27. )
  28. // New object file format.
  29. //
  30. // Header struct {
  31. // Magic [...]byte // "\x00go116ld"
  32. // Fingerprint [8]byte
  33. // Flags uint32
  34. // Offsets [...]uint32 // byte offset of each block below
  35. // }
  36. //
  37. // Strings [...]struct {
  38. // Data [...]byte
  39. // }
  40. //
  41. // Autolib [...]struct { // imported packages (for file loading)
  42. // Pkg string
  43. // Fingerprint [8]byte
  44. // }
  45. //
  46. // PkgIndex [...]string // referenced packages by index
  47. //
  48. // Files [...]string
  49. //
  50. // SymbolDefs [...]struct {
  51. // Name string
  52. // ABI uint16
  53. // Type uint8
  54. // Flag uint8
  55. // Flag2 uint8
  56. // Size uint32
  57. // }
  58. // Hashed64Defs [...]struct { // short hashed (content-addressable) symbol definitions
  59. // ... // same as SymbolDefs
  60. // }
  61. // HashedDefs [...]struct { // hashed (content-addressable) symbol definitions
  62. // ... // same as SymbolDefs
  63. // }
  64. // NonPkgDefs [...]struct { // non-pkg symbol definitions
  65. // ... // same as SymbolDefs
  66. // }
  67. // NonPkgRefs [...]struct { // non-pkg symbol references
  68. // ... // same as SymbolDefs
  69. // }
  70. //
  71. // RefFlags [...]struct { // referenced symbol flags
  72. // Sym symRef
  73. // Flag uint8
  74. // Flag2 uint8
  75. // }
  76. //
  77. // Hash64 [...][8]byte
  78. // Hash [...][N]byte
  79. //
  80. // RelocIndex [...]uint32 // index to Relocs
  81. // AuxIndex [...]uint32 // index to Aux
  82. // DataIndex [...]uint32 // offset to Data
  83. //
  84. // Relocs [...]struct {
  85. // Off int32
  86. // Size uint8
  87. // Type uint8
  88. // Add int64
  89. // Sym symRef
  90. // }
  91. //
  92. // Aux [...]struct {
  93. // Type uint8
  94. // Sym symRef
  95. // }
  96. //
  97. // Data [...]byte
  98. // Pcdata [...]byte
  99. //
  100. // // blocks only used by tools (objdump, nm)
  101. //
  102. // RefNames [...]struct { // referenced symbol names
  103. // Sym symRef
  104. // Name string
  105. // // TODO: include ABI version as well?
  106. // }
  107. //
  108. // string is encoded as is a uint32 length followed by a uint32 offset
  109. // that points to the corresponding string bytes.
  110. //
  111. // symRef is struct { PkgIdx, SymIdx uint32 }.
  112. //
  113. // Slice type (e.g. []symRef) is encoded as a length prefix (uint32)
  114. // followed by that number of elements.
  115. //
  116. // The types below correspond to the encoded data structure in the
  117. // object file.
  118. // Symbol indexing.
  119. //
  120. // Each symbol is referenced with a pair of indices, { PkgIdx, SymIdx },
  121. // as the symRef struct above.
  122. //
  123. // PkgIdx is either a predeclared index (see PkgIdxNone below) or
  124. // an index of an imported package. For the latter case, PkgIdx is the
  125. // index of the package in the PkgIndex array. 0 is an invalid index.
  126. //
  127. // SymIdx is the index of the symbol in the given package.
  128. // - If PkgIdx is PkgIdxSelf, SymIdx is the index of the symbol in the
  129. // SymbolDefs array.
  130. // - If PkgIdx is PkgIdxHashed64, SymIdx is the index of the symbol in the
  131. // Hashed64Defs array.
  132. // - If PkgIdx is PkgIdxHashed, SymIdx is the index of the symbol in the
  133. // HashedDefs array.
  134. // - If PkgIdx is PkgIdxNone, SymIdx is the index of the symbol in the
  135. // NonPkgDefs array (could natually overflow to NonPkgRefs array).
  136. // - Otherwise, SymIdx is the index of the symbol in some other package's
  137. // SymbolDefs array.
  138. //
  139. // {0, 0} represents a nil symbol. Otherwise PkgIdx should not be 0.
  140. //
  141. // Hash contains the content hashes of content-addressable symbols, of
  142. // which PkgIdx is PkgIdxHashed, in the same order of HashedDefs array.
  143. // Hash64 is similar, for PkgIdxHashed64 symbols.
  144. //
  145. // RelocIndex, AuxIndex, and DataIndex contains indices/offsets to
  146. // Relocs/Aux/Data blocks, one element per symbol, first for all the
  147. // defined symbols, then all the defined hashed and non-package symbols,
  148. // in the same order of SymbolDefs/Hashed64Defs/HashedDefs/NonPkgDefs
  149. // arrays. For N total defined symbols, the array is of length N+1. The
  150. // last element is the total number of relocations (aux symbols, data
  151. // blocks, etc.).
  152. //
  153. // They can be accessed by index. For the i-th symbol, its relocations
  154. // are the RelocIndex[i]-th (inclusive) to RelocIndex[i+1]-th (exclusive)
  155. // elements in the Relocs array. Aux/Data are likewise. (The index is
  156. // 0-based.)
  157. // Auxiliary symbols.
  158. //
  159. // Each symbol may (or may not) be associated with a number of auxiliary
  160. // symbols. They are described in the Aux block. See Aux struct below.
  161. // Currently a symbol's Gotype, FuncInfo, and associated DWARF symbols
  162. // are auxiliary symbols.
  163. const stringRefSize = 8 // two uint32s
  164. type FingerprintType [8]byte
  165. func (fp FingerprintType) IsZero() bool { return fp == FingerprintType{} }
  166. // Package Index.
  167. const (
  168. PkgIdxNone = (1<<31 - 1) - iota // Non-package symbols
  169. PkgIdxHashed64 // Short hashed (content-addressable) symbols
  170. PkgIdxHashed // Hashed (content-addressable) symbols
  171. PkgIdxBuiltin // Predefined runtime symbols (ex: runtime.newobject)
  172. PkgIdxSelf // Symbols defined in the current package
  173. PkgIdxInvalid = 0
  174. // The index of other referenced packages starts from 1.
  175. )
  176. // Blocks
  177. const (
  178. BlkAutolib = iota
  179. BlkPkgIdx
  180. BlkFile
  181. BlkSymdef
  182. BlkHashed64def
  183. BlkHasheddef
  184. BlkNonpkgdef
  185. BlkNonpkgref
  186. BlkRefFlags
  187. BlkHash64
  188. BlkHash
  189. BlkRelocIdx
  190. BlkAuxIdx
  191. BlkDataIdx
  192. BlkReloc
  193. BlkAux
  194. BlkData
  195. BlkPcdata
  196. BlkRefName
  197. BlkEnd
  198. NBlk
  199. )
  200. // File header.
  201. // TODO: probably no need to export this.
  202. type Header struct {
  203. Magic string
  204. Fingerprint FingerprintType
  205. Flags uint32
  206. Offsets [NBlk]uint32
  207. }
  208. const Magic = "\x00go116ld"
  209. func (h *Header) Write(w *Writer) {
  210. w.RawString(h.Magic)
  211. w.Bytes(h.Fingerprint[:])
  212. w.Uint32(h.Flags)
  213. for _, x := range h.Offsets {
  214. w.Uint32(x)
  215. }
  216. }
  217. func (h *Header) Read(r *Reader) error {
  218. b := r.BytesAt(0, len(Magic))
  219. h.Magic = string(b)
  220. if h.Magic != Magic {
  221. return errors.New("wrong magic, not a Go object file")
  222. }
  223. off := uint32(len(h.Magic))
  224. copy(h.Fingerprint[:], r.BytesAt(off, len(h.Fingerprint)))
  225. off += 8
  226. h.Flags = r.uint32At(off)
  227. off += 4
  228. for i := range h.Offsets {
  229. h.Offsets[i] = r.uint32At(off)
  230. off += 4
  231. }
  232. return nil
  233. }
  234. func (h *Header) Size() int {
  235. return len(h.Magic) + 4 + 4*len(h.Offsets)
  236. }
  237. // Autolib
  238. type ImportedPkg struct {
  239. Pkg string
  240. Fingerprint FingerprintType
  241. }
  242. const importedPkgSize = stringRefSize + 8
  243. func (p *ImportedPkg) Write(w *Writer) {
  244. w.StringRef(p.Pkg)
  245. w.Bytes(p.Fingerprint[:])
  246. }
  247. // Symbol definition.
  248. //
  249. // Serialized format:
  250. // Sym struct {
  251. // Name string
  252. // ABI uint16
  253. // Type uint8
  254. // Flag uint8
  255. // Flag2 uint8
  256. // Siz uint32
  257. // Align uint32
  258. // }
  259. type Sym [SymSize]byte
  260. const SymSize = stringRefSize + 2 + 1 + 1 + 1 + 4 + 4
  261. const SymABIstatic = ^uint16(0)
  262. const (
  263. ObjFlagShared = 1 << iota // this object is built with -shared
  264. ObjFlagNeedNameExpansion // the linker needs to expand `"".` to package path in symbol names
  265. ObjFlagFromAssembly // object is from asm src, not go
  266. )
  267. // Sym.Flag
  268. const (
  269. SymFlagDupok = 1 << iota
  270. SymFlagLocal
  271. SymFlagTypelink
  272. SymFlagLeaf
  273. SymFlagNoSplit
  274. SymFlagReflectMethod
  275. SymFlagGoType
  276. SymFlagTopFrame
  277. )
  278. // Sym.Flag2
  279. const (
  280. SymFlagUsedInIface = 1 << iota
  281. SymFlagItab
  282. )
  283. // Returns the length of the name of the symbol.
  284. func (s *Sym) NameLen(r *Reader) int {
  285. return int(binary.LittleEndian.Uint32(s[:]))
  286. }
  287. func (s *Sym) Name(r *Reader) string {
  288. len := binary.LittleEndian.Uint32(s[:])
  289. off := binary.LittleEndian.Uint32(s[4:])
  290. return r.StringAt(off, len)
  291. }
  292. func (s *Sym) ABI() uint16 { return binary.LittleEndian.Uint16(s[8:]) }
  293. func (s *Sym) Type() uint8 { return s[10] }
  294. func (s *Sym) Flag() uint8 { return s[11] }
  295. func (s *Sym) Flag2() uint8 { return s[12] }
  296. func (s *Sym) Siz() uint32 { return binary.LittleEndian.Uint32(s[13:]) }
  297. func (s *Sym) Align() uint32 { return binary.LittleEndian.Uint32(s[17:]) }
  298. func (s *Sym) Dupok() bool { return s.Flag()&SymFlagDupok != 0 }
  299. func (s *Sym) Local() bool { return s.Flag()&SymFlagLocal != 0 }
  300. func (s *Sym) Typelink() bool { return s.Flag()&SymFlagTypelink != 0 }
  301. func (s *Sym) Leaf() bool { return s.Flag()&SymFlagLeaf != 0 }
  302. func (s *Sym) NoSplit() bool { return s.Flag()&SymFlagNoSplit != 0 }
  303. func (s *Sym) ReflectMethod() bool { return s.Flag()&SymFlagReflectMethod != 0 }
  304. func (s *Sym) IsGoType() bool { return s.Flag()&SymFlagGoType != 0 }
  305. func (s *Sym) TopFrame() bool { return s.Flag()&SymFlagTopFrame != 0 }
  306. func (s *Sym) UsedInIface() bool { return s.Flag2()&SymFlagUsedInIface != 0 }
  307. func (s *Sym) IsItab() bool { return s.Flag2()&SymFlagItab != 0 }
  308. func (s *Sym) SetName(x string, w *Writer) {
  309. binary.LittleEndian.PutUint32(s[:], uint32(len(x)))
  310. binary.LittleEndian.PutUint32(s[4:], w.stringOff(x))
  311. }
  312. func (s *Sym) SetABI(x uint16) { binary.LittleEndian.PutUint16(s[8:], x) }
  313. func (s *Sym) SetType(x uint8) { s[10] = x }
  314. func (s *Sym) SetFlag(x uint8) { s[11] = x }
  315. func (s *Sym) SetFlag2(x uint8) { s[12] = x }
  316. func (s *Sym) SetSiz(x uint32) { binary.LittleEndian.PutUint32(s[13:], x) }
  317. func (s *Sym) SetAlign(x uint32) { binary.LittleEndian.PutUint32(s[17:], x) }
  318. func (s *Sym) Write(w *Writer) { w.Bytes(s[:]) }
  319. // for testing
  320. func (s *Sym) fromBytes(b []byte) { copy(s[:], b) }
  321. // Symbol reference.
  322. type SymRef struct {
  323. PkgIdx uint32
  324. SymIdx uint32
  325. }
  326. // Hash64
  327. type Hash64Type [Hash64Size]byte
  328. const Hash64Size = 8
  329. // Hash
  330. type HashType [HashSize]byte
  331. const HashSize = sha1.Size
  332. // Relocation.
  333. //
  334. // Serialized format:
  335. // Reloc struct {
  336. // Off int32
  337. // Siz uint8
  338. // Type uint8
  339. // Add int64
  340. // Sym SymRef
  341. // }
  342. type Reloc [RelocSize]byte
  343. const RelocSize = 4 + 1 + 1 + 8 + 8
  344. func (r *Reloc) Off() int32 { return int32(binary.LittleEndian.Uint32(r[:])) }
  345. func (r *Reloc) Siz() uint8 { return r[4] }
  346. func (r *Reloc) Type() uint8 { return r[5] }
  347. func (r *Reloc) Add() int64 { return int64(binary.LittleEndian.Uint64(r[6:])) }
  348. func (r *Reloc) Sym() SymRef {
  349. return SymRef{binary.LittleEndian.Uint32(r[14:]), binary.LittleEndian.Uint32(r[18:])}
  350. }
  351. func (r *Reloc) SetOff(x int32) { binary.LittleEndian.PutUint32(r[:], uint32(x)) }
  352. func (r *Reloc) SetSiz(x uint8) { r[4] = x }
  353. func (r *Reloc) SetType(x uint8) { r[5] = x }
  354. func (r *Reloc) SetAdd(x int64) { binary.LittleEndian.PutUint64(r[6:], uint64(x)) }
  355. func (r *Reloc) SetSym(x SymRef) {
  356. binary.LittleEndian.PutUint32(r[14:], x.PkgIdx)
  357. binary.LittleEndian.PutUint32(r[18:], x.SymIdx)
  358. }
  359. func (r *Reloc) Set(off int32, size uint8, typ uint8, add int64, sym SymRef) {
  360. r.SetOff(off)
  361. r.SetSiz(size)
  362. r.SetType(typ)
  363. r.SetAdd(add)
  364. r.SetSym(sym)
  365. }
  366. func (r *Reloc) Write(w *Writer) { w.Bytes(r[:]) }
  367. // for testing
  368. func (r *Reloc) fromBytes(b []byte) { copy(r[:], b) }
  369. // Aux symbol info.
  370. //
  371. // Serialized format:
  372. // Aux struct {
  373. // Type uint8
  374. // Sym SymRef
  375. // }
  376. type Aux [AuxSize]byte
  377. const AuxSize = 1 + 8
  378. // Aux Type
  379. const (
  380. AuxGotype = iota
  381. AuxFuncInfo
  382. AuxFuncdata
  383. AuxDwarfInfo
  384. AuxDwarfLoc
  385. AuxDwarfRanges
  386. AuxDwarfLines
  387. // TODO: more. Pcdata?
  388. )
  389. func (a *Aux) Type() uint8 { return a[0] }
  390. func (a *Aux) Sym() SymRef {
  391. return SymRef{binary.LittleEndian.Uint32(a[1:]), binary.LittleEndian.Uint32(a[5:])}
  392. }
  393. func (a *Aux) SetType(x uint8) { a[0] = x }
  394. func (a *Aux) SetSym(x SymRef) {
  395. binary.LittleEndian.PutUint32(a[1:], x.PkgIdx)
  396. binary.LittleEndian.PutUint32(a[5:], x.SymIdx)
  397. }
  398. func (a *Aux) Write(w *Writer) { w.Bytes(a[:]) }
  399. // for testing
  400. func (a *Aux) fromBytes(b []byte) { copy(a[:], b) }
  401. // Referenced symbol flags.
  402. //
  403. // Serialized format:
  404. // RefFlags struct {
  405. // Sym symRef
  406. // Flag uint8
  407. // Flag2 uint8
  408. // }
  409. type RefFlags [RefFlagsSize]byte
  410. const RefFlagsSize = 8 + 1 + 1
  411. func (r *RefFlags) Sym() SymRef {
  412. return SymRef{binary.LittleEndian.Uint32(r[:]), binary.LittleEndian.Uint32(r[4:])}
  413. }
  414. func (r *RefFlags) Flag() uint8 { return r[8] }
  415. func (r *RefFlags) Flag2() uint8 { return r[9] }
  416. func (r *RefFlags) SetSym(x SymRef) {
  417. binary.LittleEndian.PutUint32(r[:], x.PkgIdx)
  418. binary.LittleEndian.PutUint32(r[4:], x.SymIdx)
  419. }
  420. func (r *RefFlags) SetFlag(x uint8) { r[8] = x }
  421. func (r *RefFlags) SetFlag2(x uint8) { r[9] = x }
  422. func (r *RefFlags) Write(w *Writer) { w.Bytes(r[:]) }
  423. // Referenced symbol name.
  424. //
  425. // Serialized format:
  426. // RefName struct {
  427. // Sym symRef
  428. // Name string
  429. // }
  430. type RefName [RefNameSize]byte
  431. const RefNameSize = 8 + stringRefSize
  432. func (n *RefName) Sym() SymRef {
  433. return SymRef{binary.LittleEndian.Uint32(n[:]), binary.LittleEndian.Uint32(n[4:])}
  434. }
  435. func (n *RefName) Name(r *Reader) string {
  436. len := binary.LittleEndian.Uint32(n[8:])
  437. off := binary.LittleEndian.Uint32(n[12:])
  438. return r.StringAt(off, len)
  439. }
  440. func (n *RefName) SetSym(x SymRef) {
  441. binary.LittleEndian.PutUint32(n[:], x.PkgIdx)
  442. binary.LittleEndian.PutUint32(n[4:], x.SymIdx)
  443. }
  444. func (n *RefName) SetName(x string, w *Writer) {
  445. binary.LittleEndian.PutUint32(n[8:], uint32(len(x)))
  446. binary.LittleEndian.PutUint32(n[12:], w.stringOff(x))
  447. }
  448. func (n *RefName) Write(w *Writer) { w.Bytes(n[:]) }
  449. type Writer struct {
  450. wr *bio.Writer
  451. stringMap map[string]uint32
  452. off uint32 // running offset
  453. }
  454. func NewWriter(wr *bio.Writer) *Writer {
  455. return &Writer{wr: wr, stringMap: make(map[string]uint32)}
  456. }
  457. func (w *Writer) AddString(s string) {
  458. if _, ok := w.stringMap[s]; ok {
  459. return
  460. }
  461. w.stringMap[s] = w.off
  462. w.RawString(s)
  463. }
  464. func (w *Writer) stringOff(s string) uint32 {
  465. off, ok := w.stringMap[s]
  466. if !ok {
  467. panic(fmt.Sprintf("writeStringRef: string not added: %q", s))
  468. }
  469. return off
  470. }
  471. func (w *Writer) StringRef(s string) {
  472. w.Uint32(uint32(len(s)))
  473. w.Uint32(w.stringOff(s))
  474. }
  475. func (w *Writer) RawString(s string) {
  476. w.wr.WriteString(s)
  477. w.off += uint32(len(s))
  478. }
  479. func (w *Writer) Bytes(s []byte) {
  480. w.wr.Write(s)
  481. w.off += uint32(len(s))
  482. }
  483. func (w *Writer) Uint64(x uint64) {
  484. var b [8]byte
  485. binary.LittleEndian.PutUint64(b[:], x)
  486. w.wr.Write(b[:])
  487. w.off += 8
  488. }
  489. func (w *Writer) Uint32(x uint32) {
  490. var b [4]byte
  491. binary.LittleEndian.PutUint32(b[:], x)
  492. w.wr.Write(b[:])
  493. w.off += 4
  494. }
  495. func (w *Writer) Uint16(x uint16) {
  496. var b [2]byte
  497. binary.LittleEndian.PutUint16(b[:], x)
  498. w.wr.Write(b[:])
  499. w.off += 2
  500. }
  501. func (w *Writer) Uint8(x uint8) {
  502. w.wr.WriteByte(x)
  503. w.off++
  504. }
  505. func (w *Writer) Offset() uint32 {
  506. return w.off
  507. }
  508. type Reader struct {
  509. b []byte // mmapped bytes, if not nil
  510. readonly bool // whether b is backed with read-only memory
  511. rd io.ReaderAt
  512. start uint32
  513. h Header // keep block offsets
  514. }
  515. func NewReaderFromBytes(b []byte, readonly bool) *Reader {
  516. r := &Reader{b: b, readonly: readonly, rd: bytes.NewReader(b), start: 0}
  517. err := r.h.Read(r)
  518. if err != nil {
  519. return nil
  520. }
  521. return r
  522. }
  523. func (r *Reader) BytesAt(off uint32, len int) []byte {
  524. if len == 0 {
  525. return nil
  526. }
  527. end := int(off) + len
  528. return r.b[int(off):end:end]
  529. }
  530. func (r *Reader) uint64At(off uint32) uint64 {
  531. b := r.BytesAt(off, 8)
  532. return binary.LittleEndian.Uint64(b)
  533. }
  534. func (r *Reader) int64At(off uint32) int64 {
  535. return int64(r.uint64At(off))
  536. }
  537. func (r *Reader) uint32At(off uint32) uint32 {
  538. b := r.BytesAt(off, 4)
  539. return binary.LittleEndian.Uint32(b)
  540. }
  541. func (r *Reader) int32At(off uint32) int32 {
  542. return int32(r.uint32At(off))
  543. }
  544. func (r *Reader) uint16At(off uint32) uint16 {
  545. b := r.BytesAt(off, 2)
  546. return binary.LittleEndian.Uint16(b)
  547. }
  548. func (r *Reader) uint8At(off uint32) uint8 {
  549. b := r.BytesAt(off, 1)
  550. return b[0]
  551. }
  552. func (r *Reader) StringAt(off uint32, len uint32) string {
  553. b := r.b[off : off+len]
  554. if r.readonly {
  555. return toString(b) // backed by RO memory, ok to make unsafe string
  556. }
  557. return string(b)
  558. }
  559. func toString(b []byte) string {
  560. if len(b) == 0 {
  561. return ""
  562. }
  563. var s string
  564. hdr := (*unsafeheader.String)(unsafe.Pointer(&s))
  565. hdr.Data = unsafe.Pointer(&b[0])
  566. hdr.Len = len(b)
  567. return s
  568. }
  569. func (r *Reader) StringRef(off uint32) string {
  570. l := r.uint32At(off)
  571. return r.StringAt(r.uint32At(off+4), l)
  572. }
  573. func (r *Reader) Fingerprint() FingerprintType {
  574. return r.h.Fingerprint
  575. }
  576. func (r *Reader) Autolib() []ImportedPkg {
  577. n := (r.h.Offsets[BlkAutolib+1] - r.h.Offsets[BlkAutolib]) / importedPkgSize
  578. s := make([]ImportedPkg, n)
  579. off := r.h.Offsets[BlkAutolib]
  580. for i := range s {
  581. s[i].Pkg = r.StringRef(off)
  582. copy(s[i].Fingerprint[:], r.BytesAt(off+stringRefSize, len(s[i].Fingerprint)))
  583. off += importedPkgSize
  584. }
  585. return s
  586. }
  587. func (r *Reader) Pkglist() []string {
  588. n := (r.h.Offsets[BlkPkgIdx+1] - r.h.Offsets[BlkPkgIdx]) / stringRefSize
  589. s := make([]string, n)
  590. off := r.h.Offsets[BlkPkgIdx]
  591. for i := range s {
  592. s[i] = r.StringRef(off)
  593. off += stringRefSize
  594. }
  595. return s
  596. }
  597. func (r *Reader) NPkg() int {
  598. return int(r.h.Offsets[BlkPkgIdx+1]-r.h.Offsets[BlkPkgIdx]) / stringRefSize
  599. }
  600. func (r *Reader) Pkg(i int) string {
  601. off := r.h.Offsets[BlkPkgIdx] + uint32(i)*stringRefSize
  602. return r.StringRef(off)
  603. }
  604. func (r *Reader) NFile() int {
  605. return int(r.h.Offsets[BlkFile+1]-r.h.Offsets[BlkFile]) / stringRefSize
  606. }
  607. func (r *Reader) File(i int) string {
  608. off := r.h.Offsets[BlkFile] + uint32(i)*stringRefSize
  609. return r.StringRef(off)
  610. }
  611. func (r *Reader) NSym() int {
  612. return int(r.h.Offsets[BlkSymdef+1]-r.h.Offsets[BlkSymdef]) / SymSize
  613. }
  614. func (r *Reader) NHashed64def() int {
  615. return int(r.h.Offsets[BlkHashed64def+1]-r.h.Offsets[BlkHashed64def]) / SymSize
  616. }
  617. func (r *Reader) NHasheddef() int {
  618. return int(r.h.Offsets[BlkHasheddef+1]-r.h.Offsets[BlkHasheddef]) / SymSize
  619. }
  620. func (r *Reader) NNonpkgdef() int {
  621. return int(r.h.Offsets[BlkNonpkgdef+1]-r.h.Offsets[BlkNonpkgdef]) / SymSize
  622. }
  623. func (r *Reader) NNonpkgref() int {
  624. return int(r.h.Offsets[BlkNonpkgref+1]-r.h.Offsets[BlkNonpkgref]) / SymSize
  625. }
  626. // SymOff returns the offset of the i-th symbol.
  627. func (r *Reader) SymOff(i uint32) uint32 {
  628. return r.h.Offsets[BlkSymdef] + uint32(i*SymSize)
  629. }
  630. // Sym returns a pointer to the i-th symbol.
  631. func (r *Reader) Sym(i uint32) *Sym {
  632. off := r.SymOff(i)
  633. return (*Sym)(unsafe.Pointer(&r.b[off]))
  634. }
  635. // NRefFlags returns the number of referenced symbol flags.
  636. func (r *Reader) NRefFlags() int {
  637. return int(r.h.Offsets[BlkRefFlags+1]-r.h.Offsets[BlkRefFlags]) / RefFlagsSize
  638. }
  639. // RefFlags returns a pointer to the i-th referenced symbol flags.
  640. // Note: here i is not a local symbol index, just a counter.
  641. func (r *Reader) RefFlags(i int) *RefFlags {
  642. off := r.h.Offsets[BlkRefFlags] + uint32(i*RefFlagsSize)
  643. return (*RefFlags)(unsafe.Pointer(&r.b[off]))
  644. }
  645. // Hash64 returns the i-th short hashed symbol's hash.
  646. // Note: here i is the index of short hashed symbols, not all symbols
  647. // (unlike other accessors).
  648. func (r *Reader) Hash64(i uint32) uint64 {
  649. off := r.h.Offsets[BlkHash64] + uint32(i*Hash64Size)
  650. return r.uint64At(off)
  651. }
  652. // Hash returns a pointer to the i-th hashed symbol's hash.
  653. // Note: here i is the index of hashed symbols, not all symbols
  654. // (unlike other accessors).
  655. func (r *Reader) Hash(i uint32) *HashType {
  656. off := r.h.Offsets[BlkHash] + uint32(i*HashSize)
  657. return (*HashType)(unsafe.Pointer(&r.b[off]))
  658. }
  659. // NReloc returns the number of relocations of the i-th symbol.
  660. func (r *Reader) NReloc(i uint32) int {
  661. relocIdxOff := r.h.Offsets[BlkRelocIdx] + uint32(i*4)
  662. return int(r.uint32At(relocIdxOff+4) - r.uint32At(relocIdxOff))
  663. }
  664. // RelocOff returns the offset of the j-th relocation of the i-th symbol.
  665. func (r *Reader) RelocOff(i uint32, j int) uint32 {
  666. relocIdxOff := r.h.Offsets[BlkRelocIdx] + uint32(i*4)
  667. relocIdx := r.uint32At(relocIdxOff)
  668. return r.h.Offsets[BlkReloc] + (relocIdx+uint32(j))*uint32(RelocSize)
  669. }
  670. // Reloc returns a pointer to the j-th relocation of the i-th symbol.
  671. func (r *Reader) Reloc(i uint32, j int) *Reloc {
  672. off := r.RelocOff(i, j)
  673. return (*Reloc)(unsafe.Pointer(&r.b[off]))
  674. }
  675. // Relocs returns a pointer to the relocations of the i-th symbol.
  676. func (r *Reader) Relocs(i uint32) []Reloc {
  677. off := r.RelocOff(i, 0)
  678. n := r.NReloc(i)
  679. return (*[1 << 20]Reloc)(unsafe.Pointer(&r.b[off]))[:n:n]
  680. }
  681. // NAux returns the number of aux symbols of the i-th symbol.
  682. func (r *Reader) NAux(i uint32) int {
  683. auxIdxOff := r.h.Offsets[BlkAuxIdx] + i*4
  684. return int(r.uint32At(auxIdxOff+4) - r.uint32At(auxIdxOff))
  685. }
  686. // AuxOff returns the offset of the j-th aux symbol of the i-th symbol.
  687. func (r *Reader) AuxOff(i uint32, j int) uint32 {
  688. auxIdxOff := r.h.Offsets[BlkAuxIdx] + i*4
  689. auxIdx := r.uint32At(auxIdxOff)
  690. return r.h.Offsets[BlkAux] + (auxIdx+uint32(j))*uint32(AuxSize)
  691. }
  692. // Aux returns a pointer to the j-th aux symbol of the i-th symbol.
  693. func (r *Reader) Aux(i uint32, j int) *Aux {
  694. off := r.AuxOff(i, j)
  695. return (*Aux)(unsafe.Pointer(&r.b[off]))
  696. }
  697. // Auxs returns the aux symbols of the i-th symbol.
  698. func (r *Reader) Auxs(i uint32) []Aux {
  699. off := r.AuxOff(i, 0)
  700. n := r.NAux(i)
  701. return (*[1 << 20]Aux)(unsafe.Pointer(&r.b[off]))[:n:n]
  702. }
  703. // DataOff returns the offset of the i-th symbol's data.
  704. func (r *Reader) DataOff(i uint32) uint32 {
  705. dataIdxOff := r.h.Offsets[BlkDataIdx] + i*4
  706. return r.h.Offsets[BlkData] + r.uint32At(dataIdxOff)
  707. }
  708. // DataSize returns the size of the i-th symbol's data.
  709. func (r *Reader) DataSize(i uint32) int {
  710. dataIdxOff := r.h.Offsets[BlkDataIdx] + i*4
  711. return int(r.uint32At(dataIdxOff+4) - r.uint32At(dataIdxOff))
  712. }
  713. // Data returns the i-th symbol's data.
  714. func (r *Reader) Data(i uint32) []byte {
  715. dataIdxOff := r.h.Offsets[BlkDataIdx] + i*4
  716. base := r.h.Offsets[BlkData]
  717. off := r.uint32At(dataIdxOff)
  718. end := r.uint32At(dataIdxOff + 4)
  719. return r.BytesAt(base+off, int(end-off))
  720. }
  721. // AuxDataBase returns the base offset of the aux data block.
  722. func (r *Reader) PcdataBase() uint32 {
  723. return r.h.Offsets[BlkPcdata]
  724. }
  725. // NRefName returns the number of referenced symbol names.
  726. func (r *Reader) NRefName() int {
  727. return int(r.h.Offsets[BlkRefName+1]-r.h.Offsets[BlkRefName]) / RefNameSize
  728. }
  729. // RefName returns a pointer to the i-th referenced symbol name.
  730. // Note: here i is not a local symbol index, just a counter.
  731. func (r *Reader) RefName(i int) *RefName {
  732. off := r.h.Offsets[BlkRefName] + uint32(i*RefNameSize)
  733. return (*RefName)(unsafe.Pointer(&r.b[off]))
  734. }
  735. // ReadOnly returns whether r.BytesAt returns read-only bytes.
  736. func (r *Reader) ReadOnly() bool {
  737. return r.readonly
  738. }
  739. // Flags returns the flag bits read from the object file header.
  740. func (r *Reader) Flags() uint32 {
  741. return r.h.Flags
  742. }
  743. func (r *Reader) Shared() bool { return r.Flags()&ObjFlagShared != 0 }
  744. func (r *Reader) NeedNameExpansion() bool { return r.Flags()&ObjFlagNeedNameExpansion != 0 }
  745. func (r *Reader) FromAssembly() bool { return r.Flags()&ObjFlagFromAssembly != 0 }