You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

418 lines
9.8 KiB

  1. // Copyright 2018 Google Inc. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. // DWARF debug information entry parser.
  15. // An entry is a sequence of data items of a given format.
  16. // The first word in the entry is an index into what DWARF
  17. // calls the ``abbreviation table.'' An abbreviation is really
  18. // just a type descriptor: it's an array of attribute tag/value format pairs.
  19. package dwarf
  20. import (
  21. "errors"
  22. "strconv"
  23. )
  24. // a single entry's description: a sequence of attributes
  25. type abbrev struct {
  26. tag Tag
  27. children bool
  28. field []afield
  29. }
  30. type afield struct {
  31. attr Attr
  32. fmt format
  33. }
  34. // a map from entry format ids to their descriptions
  35. type abbrevTable map[uint32]abbrev
  36. // ParseAbbrev returns the abbreviation table that starts at byte off
  37. // in the .debug_abbrev section.
  38. func (d *Data) parseAbbrev(off uint32) (abbrevTable, error) {
  39. if m, ok := d.abbrevCache[off]; ok {
  40. return m, nil
  41. }
  42. data := d.abbrev
  43. if off > uint32(len(data)) {
  44. data = nil
  45. } else {
  46. data = data[off:]
  47. }
  48. b := makeBuf(d, unknownFormat{}, "abbrev", 0, data)
  49. // Error handling is simplified by the buf getters
  50. // returning an endless stream of 0s after an error.
  51. m := make(abbrevTable)
  52. for {
  53. // Table ends with id == 0.
  54. id := uint32(b.uint())
  55. if id == 0 {
  56. break
  57. }
  58. // Walk over attributes, counting.
  59. n := 0
  60. b1 := b // Read from copy of b.
  61. b1.uint()
  62. b1.uint8()
  63. for {
  64. tag := b1.uint()
  65. fmt := b1.uint()
  66. if tag == 0 && fmt == 0 {
  67. break
  68. }
  69. n++
  70. }
  71. if b1.err != nil {
  72. return nil, b1.err
  73. }
  74. // Walk over attributes again, this time writing them down.
  75. var a abbrev
  76. a.tag = Tag(b.uint())
  77. a.children = b.uint8() != 0
  78. a.field = make([]afield, n)
  79. for i := range a.field {
  80. a.field[i].attr = Attr(b.uint())
  81. a.field[i].fmt = format(b.uint())
  82. }
  83. b.uint()
  84. b.uint()
  85. m[id] = a
  86. }
  87. if b.err != nil {
  88. return nil, b.err
  89. }
  90. d.abbrevCache[off] = m
  91. return m, nil
  92. }
  93. // An entry is a sequence of attribute/value pairs.
  94. type Entry struct {
  95. Offset Offset // offset of Entry in DWARF info
  96. Tag Tag // tag (kind of Entry)
  97. Children bool // whether Entry is followed by children
  98. Field []Field
  99. }
  100. // A Field is a single attribute/value pair in an Entry.
  101. type Field struct {
  102. Attr Attr
  103. Val interface{}
  104. }
  105. // Val returns the value associated with attribute Attr in Entry,
  106. // or nil if there is no such attribute.
  107. //
  108. // A common idiom is to merge the check for nil return with
  109. // the check that the value has the expected dynamic type, as in:
  110. // v, ok := e.Val(AttrSibling).(int64);
  111. //
  112. func (e *Entry) Val(a Attr) interface{} {
  113. for _, f := range e.Field {
  114. if f.Attr == a {
  115. return f.Val
  116. }
  117. }
  118. return nil
  119. }
  120. // An Offset represents the location of an Entry within the DWARF info.
  121. // (See Reader.Seek.)
  122. type Offset uint32
  123. // Entry reads a single entry from buf, decoding
  124. // according to the given abbreviation table.
  125. func (b *buf) entry(atab abbrevTable, ubase Offset) *Entry {
  126. off := b.off
  127. id := uint32(b.uint())
  128. if id == 0 {
  129. return &Entry{}
  130. }
  131. a, ok := atab[id]
  132. if !ok {
  133. b.error("unknown abbreviation table index")
  134. return nil
  135. }
  136. e := &Entry{
  137. Offset: off,
  138. Tag: a.tag,
  139. Children: a.children,
  140. Field: make([]Field, len(a.field)),
  141. }
  142. for i := range e.Field {
  143. e.Field[i].Attr = a.field[i].attr
  144. fmt := a.field[i].fmt
  145. if fmt == formIndirect {
  146. fmt = format(b.uint())
  147. }
  148. var val interface{}
  149. switch fmt {
  150. default:
  151. b.error("unknown entry attr format 0x" + strconv.FormatInt(int64(fmt), 16))
  152. // address
  153. case formAddr:
  154. val = b.addr()
  155. // block
  156. case formDwarfBlock1:
  157. val = b.bytes(int(b.uint8()))
  158. case formDwarfBlock2:
  159. val = b.bytes(int(b.uint16()))
  160. case formDwarfBlock4:
  161. val = b.bytes(int(b.uint32()))
  162. case formDwarfBlock:
  163. val = b.bytes(int(b.uint()))
  164. // constant
  165. case formData1:
  166. val = int64(b.uint8())
  167. case formData2:
  168. val = int64(b.uint16())
  169. case formData4:
  170. val = int64(b.uint32())
  171. case formData8:
  172. val = int64(b.uint64())
  173. case formSdata:
  174. val = int64(b.int())
  175. case formUdata:
  176. val = int64(b.uint())
  177. // flag
  178. case formFlag:
  179. val = b.uint8() == 1
  180. // New in DWARF 4.
  181. case formFlagPresent:
  182. // The attribute is implicitly indicated as present, and no value is
  183. // encoded in the debugging information entry itself.
  184. val = true
  185. // reference to other entry
  186. case formRefAddr:
  187. vers := b.format.version()
  188. if vers == 0 {
  189. b.error("unknown version for DW_FORM_ref_addr")
  190. } else if vers == 2 {
  191. val = Offset(b.addr())
  192. } else {
  193. is64, known := b.format.dwarf64()
  194. if !known {
  195. b.error("unknown size for DW_FORM_ref_addr")
  196. } else if is64 {
  197. val = Offset(b.uint64())
  198. } else {
  199. val = Offset(b.uint32())
  200. }
  201. }
  202. case formRef1:
  203. val = Offset(b.uint8()) + ubase
  204. case formRef2:
  205. val = Offset(b.uint16()) + ubase
  206. case formRef4:
  207. val = Offset(b.uint32()) + ubase
  208. case formRef8:
  209. val = Offset(b.uint64()) + ubase
  210. case formRefUdata:
  211. val = Offset(b.uint()) + ubase
  212. // string
  213. case formString:
  214. val = b.string()
  215. case formStrp:
  216. off := b.uint32() // offset into .debug_str
  217. if b.err != nil {
  218. return nil
  219. }
  220. b1 := makeBuf(b.dwarf, unknownFormat{}, "str", 0, b.dwarf.str)
  221. b1.skip(int(off))
  222. val = b1.string()
  223. if b1.err != nil {
  224. b.err = b1.err
  225. return nil
  226. }
  227. // lineptr, loclistptr, macptr, rangelistptr
  228. // New in DWARF 4, but clang can generate them with -gdwarf-2.
  229. // Section reference, replacing use of formData4 and formData8.
  230. case formSecOffset, formGnuRefAlt, formGnuStrpAlt:
  231. is64, known := b.format.dwarf64()
  232. if !known {
  233. b.error("unknown size for form 0x" + strconv.FormatInt(int64(fmt), 16))
  234. } else if is64 {
  235. val = int64(b.uint64())
  236. } else {
  237. val = int64(b.uint32())
  238. }
  239. // exprloc
  240. // New in DWARF 4.
  241. case formExprloc:
  242. val = b.bytes(int(b.uint()))
  243. // reference
  244. // New in DWARF 4.
  245. case formRefSig8:
  246. // 64-bit type signature.
  247. val = b.uint64()
  248. }
  249. e.Field[i].Val = val
  250. }
  251. if b.err != nil {
  252. return nil
  253. }
  254. return e
  255. }
  256. // A Reader allows reading Entry structures from a DWARF ``info'' section.
  257. // The Entry structures are arranged in a tree. The Reader's Next function
  258. // return successive entries from a pre-order traversal of the tree.
  259. // If an entry has children, its Children field will be true, and the children
  260. // follow, terminated by an Entry with Tag 0.
  261. type Reader struct {
  262. b buf
  263. d *Data
  264. err error
  265. unit int
  266. lastChildren bool // .Children of last entry returned by Next
  267. lastSibling Offset // .Val(AttrSibling) of last entry returned by Next
  268. }
  269. // Reader returns a new Reader for Data.
  270. // The reader is positioned at byte offset 0 in the DWARF ``info'' section.
  271. func (d *Data) Reader() *Reader {
  272. r := &Reader{d: d}
  273. r.Seek(0)
  274. return r
  275. }
  276. // AddressSize returns the size in bytes of addresses in the current compilation
  277. // unit.
  278. func (r *Reader) AddressSize() int {
  279. return r.d.unit[r.unit].asize
  280. }
  281. // Seek positions the Reader at offset off in the encoded entry stream.
  282. // Offset 0 can be used to denote the first entry.
  283. func (r *Reader) Seek(off Offset) {
  284. d := r.d
  285. r.err = nil
  286. r.lastChildren = false
  287. if off == 0 {
  288. if len(d.unit) == 0 {
  289. return
  290. }
  291. u := &d.unit[0]
  292. r.unit = 0
  293. r.b = makeBuf(r.d, u, "info", u.off, u.data)
  294. return
  295. }
  296. // TODO(rsc): binary search (maybe a new package)
  297. var i int
  298. var u *unit
  299. for i = range d.unit {
  300. u = &d.unit[i]
  301. if u.off <= off && off < u.off+Offset(len(u.data)) {
  302. r.unit = i
  303. r.b = makeBuf(r.d, u, "info", off, u.data[off-u.off:])
  304. return
  305. }
  306. }
  307. r.err = errors.New("offset out of range")
  308. }
  309. // maybeNextUnit advances to the next unit if this one is finished.
  310. func (r *Reader) maybeNextUnit() {
  311. for len(r.b.data) == 0 && r.unit+1 < len(r.d.unit) {
  312. r.unit++
  313. u := &r.d.unit[r.unit]
  314. r.b = makeBuf(r.d, u, "info", u.off, u.data)
  315. }
  316. }
  317. // Next reads the next entry from the encoded entry stream.
  318. // It returns nil, nil when it reaches the end of the section.
  319. // It returns an error if the current offset is invalid or the data at the
  320. // offset cannot be decoded as a valid Entry.
  321. func (r *Reader) Next() (*Entry, error) {
  322. if r.err != nil {
  323. return nil, r.err
  324. }
  325. r.maybeNextUnit()
  326. if len(r.b.data) == 0 {
  327. return nil, nil
  328. }
  329. u := &r.d.unit[r.unit]
  330. e := r.b.entry(u.atable, u.base)
  331. if r.b.err != nil {
  332. r.err = r.b.err
  333. return nil, r.err
  334. }
  335. if e != nil {
  336. r.lastChildren = e.Children
  337. if r.lastChildren {
  338. r.lastSibling, _ = e.Val(AttrSibling).(Offset)
  339. }
  340. } else {
  341. r.lastChildren = false
  342. }
  343. return e, nil
  344. }
  345. // SkipChildren skips over the child entries associated with
  346. // the last Entry returned by Next. If that Entry did not have
  347. // children or Next has not been called, SkipChildren is a no-op.
  348. func (r *Reader) SkipChildren() {
  349. if r.err != nil || !r.lastChildren {
  350. return
  351. }
  352. // If the last entry had a sibling attribute,
  353. // that attribute gives the offset of the next
  354. // sibling, so we can avoid decoding the
  355. // child subtrees.
  356. if r.lastSibling >= r.b.off {
  357. r.Seek(r.lastSibling)
  358. return
  359. }
  360. for {
  361. e, err := r.Next()
  362. if err != nil || e == nil || e.Tag == 0 {
  363. break
  364. }
  365. if e.Children {
  366. r.SkipChildren()
  367. }
  368. }
  369. }
  370. // clone returns a copy of the reader. This is used by the typeReader
  371. // interface.
  372. func (r *Reader) clone() typeReader {
  373. return r.d.Reader()
  374. }
  375. // offset returns the current buffer offset. This is used by the
  376. // typeReader interface.
  377. func (r *Reader) offset() Offset {
  378. return r.b.off
  379. }