entry.go 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211
  1. // Copyright 2009 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. // DWARF debug information entry parser.
  5. // An entry is a sequence of data items of a given format.
  6. // The first word in the entry is an index into what DWARF
  7. // calls the ``abbreviation table.'' An abbreviation is really
  8. // just a type descriptor: it's an array of attribute tag/value format pairs.
  9. package dwarf
  10. import (
  11. "encoding/binary"
  12. "errors"
  13. "strconv"
  14. )
  15. // a single entry's description: a sequence of attributes
  16. type abbrev struct {
  17. tag Tag
  18. children bool
  19. field []afield
  20. }
  21. type afield struct {
  22. attr Attr
  23. fmt format
  24. class Class
  25. val int64 // for formImplicitConst
  26. }
  27. // a map from entry format ids to their descriptions
  28. type abbrevTable map[uint32]abbrev
  29. // ParseAbbrev returns the abbreviation table that starts at byte off
  30. // in the .debug_abbrev section.
  31. func (d *Data) parseAbbrev(off uint64, vers int) (abbrevTable, error) {
  32. if m, ok := d.abbrevCache[off]; ok {
  33. return m, nil
  34. }
  35. data := d.abbrev
  36. if off > uint64(len(data)) {
  37. data = nil
  38. } else {
  39. data = data[off:]
  40. }
  41. b := makeBuf(d, unknownFormat{}, "abbrev", 0, data)
  42. // Error handling is simplified by the buf getters
  43. // returning an endless stream of 0s after an error.
  44. m := make(abbrevTable)
  45. for {
  46. // Table ends with id == 0.
  47. id := uint32(b.uint())
  48. if id == 0 {
  49. break
  50. }
  51. // Walk over attributes, counting.
  52. n := 0
  53. b1 := b // Read from copy of b.
  54. b1.uint()
  55. b1.uint8()
  56. for {
  57. tag := b1.uint()
  58. fmt := b1.uint()
  59. if tag == 0 && fmt == 0 {
  60. break
  61. }
  62. if format(fmt) == formImplicitConst {
  63. b1.int()
  64. }
  65. n++
  66. }
  67. if b1.err != nil {
  68. return nil, b1.err
  69. }
  70. // Walk over attributes again, this time writing them down.
  71. var a abbrev
  72. a.tag = Tag(b.uint())
  73. a.children = b.uint8() != 0
  74. a.field = make([]afield, n)
  75. for i := range a.field {
  76. a.field[i].attr = Attr(b.uint())
  77. a.field[i].fmt = format(b.uint())
  78. a.field[i].class = formToClass(a.field[i].fmt, a.field[i].attr, vers, &b)
  79. if a.field[i].fmt == formImplicitConst {
  80. a.field[i].val = b.int()
  81. }
  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. // attrIsExprloc indicates attributes that allow exprloc values that
  94. // are encoded as block values in DWARF 2 and 3. See DWARF 4, Figure
  95. // 20.
  96. var attrIsExprloc = map[Attr]bool{
  97. AttrLocation: true,
  98. AttrByteSize: true,
  99. AttrBitOffset: true,
  100. AttrBitSize: true,
  101. AttrStringLength: true,
  102. AttrLowerBound: true,
  103. AttrReturnAddr: true,
  104. AttrStrideSize: true,
  105. AttrUpperBound: true,
  106. AttrCount: true,
  107. AttrDataMemberLoc: true,
  108. AttrFrameBase: true,
  109. AttrSegment: true,
  110. AttrStaticLink: true,
  111. AttrUseLocation: true,
  112. AttrVtableElemLoc: true,
  113. AttrAllocated: true,
  114. AttrAssociated: true,
  115. AttrDataLocation: true,
  116. AttrStride: true,
  117. }
  118. // attrPtrClass indicates the *ptr class of attributes that have
  119. // encoding formSecOffset in DWARF 4 or formData* in DWARF 2 and 3.
  120. var attrPtrClass = map[Attr]Class{
  121. AttrLocation: ClassLocListPtr,
  122. AttrStmtList: ClassLinePtr,
  123. AttrStringLength: ClassLocListPtr,
  124. AttrReturnAddr: ClassLocListPtr,
  125. AttrStartScope: ClassRangeListPtr,
  126. AttrDataMemberLoc: ClassLocListPtr,
  127. AttrFrameBase: ClassLocListPtr,
  128. AttrMacroInfo: ClassMacPtr,
  129. AttrSegment: ClassLocListPtr,
  130. AttrStaticLink: ClassLocListPtr,
  131. AttrUseLocation: ClassLocListPtr,
  132. AttrVtableElemLoc: ClassLocListPtr,
  133. AttrRanges: ClassRangeListPtr,
  134. // The following are new in DWARF 5.
  135. AttrStrOffsetsBase: ClassStrOffsetsPtr,
  136. AttrAddrBase: ClassAddrPtr,
  137. AttrRnglistsBase: ClassRngListsPtr,
  138. AttrLoclistsBase: ClassLocListPtr,
  139. }
  140. // formToClass returns the DWARF 4 Class for the given form. If the
  141. // DWARF version is less then 4, it will disambiguate some forms
  142. // depending on the attribute.
  143. func formToClass(form format, attr Attr, vers int, b *buf) Class {
  144. switch form {
  145. default:
  146. b.error("cannot determine class of unknown attribute form")
  147. return 0
  148. case formIndirect:
  149. return ClassUnknown
  150. case formAddr, formAddrx, formAddrx1, formAddrx2, formAddrx3, formAddrx4:
  151. return ClassAddress
  152. case formDwarfBlock1, formDwarfBlock2, formDwarfBlock4, formDwarfBlock:
  153. // In DWARF 2 and 3, ClassExprLoc was encoded as a
  154. // block. DWARF 4 distinguishes ClassBlock and
  155. // ClassExprLoc, but there are no attributes that can
  156. // be both, so we also promote ClassBlock values in
  157. // DWARF 4 that should be ClassExprLoc in case
  158. // producers get this wrong.
  159. if attrIsExprloc[attr] {
  160. return ClassExprLoc
  161. }
  162. return ClassBlock
  163. case formData1, formData2, formData4, formData8, formSdata, formUdata, formData16, formImplicitConst:
  164. // In DWARF 2 and 3, ClassPtr was encoded as a
  165. // constant. Unlike ClassExprLoc/ClassBlock, some
  166. // DWARF 4 attributes need to distinguish Class*Ptr
  167. // from ClassConstant, so we only do this promotion
  168. // for versions 2 and 3.
  169. if class, ok := attrPtrClass[attr]; vers < 4 && ok {
  170. return class
  171. }
  172. return ClassConstant
  173. case formFlag, formFlagPresent:
  174. return ClassFlag
  175. case formRefAddr, formRef1, formRef2, formRef4, formRef8, formRefUdata, formRefSup4, formRefSup8:
  176. return ClassReference
  177. case formRefSig8:
  178. return ClassReferenceSig
  179. case formString, formStrp, formStrx, formStrpSup, formLineStrp, formStrx1, formStrx2, formStrx3, formStrx4:
  180. return ClassString
  181. case formSecOffset:
  182. // DWARF 4 defines four *ptr classes, but doesn't
  183. // distinguish them in the encoding. Disambiguate
  184. // these classes using the attribute.
  185. if class, ok := attrPtrClass[attr]; ok {
  186. return class
  187. }
  188. return ClassUnknown
  189. case formExprloc:
  190. return ClassExprLoc
  191. case formGnuRefAlt:
  192. return ClassReferenceAlt
  193. case formGnuStrpAlt:
  194. return ClassStringAlt
  195. case formLoclistx:
  196. return ClassLocList
  197. case formRnglistx:
  198. return ClassRngList
  199. }
  200. }
  201. // An entry is a sequence of attribute/value pairs.
  202. type Entry struct {
  203. Offset Offset // offset of Entry in DWARF info
  204. Tag Tag // tag (kind of Entry)
  205. Children bool // whether Entry is followed by children
  206. Field []Field
  207. }
  208. // A Field is a single attribute/value pair in an Entry.
  209. //
  210. // A value can be one of several "attribute classes" defined by DWARF.
  211. // The Go types corresponding to each class are:
  212. //
  213. // DWARF class Go type Class
  214. // ----------- ------- -----
  215. // address uint64 ClassAddress
  216. // block []byte ClassBlock
  217. // constant int64 ClassConstant
  218. // flag bool ClassFlag
  219. // reference
  220. // to info dwarf.Offset ClassReference
  221. // to type unit uint64 ClassReferenceSig
  222. // string string ClassString
  223. // exprloc []byte ClassExprLoc
  224. // lineptr int64 ClassLinePtr
  225. // loclistptr int64 ClassLocListPtr
  226. // macptr int64 ClassMacPtr
  227. // rangelistptr int64 ClassRangeListPtr
  228. //
  229. // For unrecognized or vendor-defined attributes, Class may be
  230. // ClassUnknown.
  231. type Field struct {
  232. Attr Attr
  233. Val any
  234. Class Class
  235. }
  236. // A Class is the DWARF 4 class of an attribute value.
  237. //
  238. // In general, a given attribute's value may take on one of several
  239. // possible classes defined by DWARF, each of which leads to a
  240. // slightly different interpretation of the attribute.
  241. //
  242. // DWARF version 4 distinguishes attribute value classes more finely
  243. // than previous versions of DWARF. The reader will disambiguate
  244. // coarser classes from earlier versions of DWARF into the appropriate
  245. // DWARF 4 class. For example, DWARF 2 uses "constant" for constants
  246. // as well as all types of section offsets, but the reader will
  247. // canonicalize attributes in DWARF 2 files that refer to section
  248. // offsets to one of the Class*Ptr classes, even though these classes
  249. // were only defined in DWARF 3.
  250. type Class int
  251. const (
  252. // ClassUnknown represents values of unknown DWARF class.
  253. ClassUnknown Class = iota
  254. // ClassAddress represents values of type uint64 that are
  255. // addresses on the target machine.
  256. ClassAddress
  257. // ClassBlock represents values of type []byte whose
  258. // interpretation depends on the attribute.
  259. ClassBlock
  260. // ClassConstant represents values of type int64 that are
  261. // constants. The interpretation of this constant depends on
  262. // the attribute.
  263. ClassConstant
  264. // ClassExprLoc represents values of type []byte that contain
  265. // an encoded DWARF expression or location description.
  266. ClassExprLoc
  267. // ClassFlag represents values of type bool.
  268. ClassFlag
  269. // ClassLinePtr represents values that are an int64 offset
  270. // into the "line" section.
  271. ClassLinePtr
  272. // ClassLocListPtr represents values that are an int64 offset
  273. // into the "loclist" section.
  274. ClassLocListPtr
  275. // ClassMacPtr represents values that are an int64 offset into
  276. // the "mac" section.
  277. ClassMacPtr
  278. // ClassRangeListPtr represents values that are an int64 offset into
  279. // the "rangelist" section.
  280. ClassRangeListPtr
  281. // ClassReference represents values that are an Offset offset
  282. // of an Entry in the info section (for use with Reader.Seek).
  283. // The DWARF specification combines ClassReference and
  284. // ClassReferenceSig into class "reference".
  285. ClassReference
  286. // ClassReferenceSig represents values that are a uint64 type
  287. // signature referencing a type Entry.
  288. ClassReferenceSig
  289. // ClassString represents values that are strings. If the
  290. // compilation unit specifies the AttrUseUTF8 flag (strongly
  291. // recommended), the string value will be encoded in UTF-8.
  292. // Otherwise, the encoding is unspecified.
  293. ClassString
  294. // ClassReferenceAlt represents values of type int64 that are
  295. // an offset into the DWARF "info" section of an alternate
  296. // object file.
  297. ClassReferenceAlt
  298. // ClassStringAlt represents values of type int64 that are an
  299. // offset into the DWARF string section of an alternate object
  300. // file.
  301. ClassStringAlt
  302. // ClassAddrPtr represents values that are an int64 offset
  303. // into the "addr" section.
  304. ClassAddrPtr
  305. // ClassLocList represents values that are an int64 offset
  306. // into the "loclists" section.
  307. ClassLocList
  308. // ClassRngList represents values that are a uint64 offset
  309. // from the base of the "rnglists" section.
  310. ClassRngList
  311. // ClassRngListsPtr represents values that are an int64 offset
  312. // into the "rnglists" section. These are used as the base for
  313. // ClassRngList values.
  314. ClassRngListsPtr
  315. // ClassStrOffsetsPtr represents values that are an int64
  316. // offset into the "str_offsets" section.
  317. ClassStrOffsetsPtr
  318. )
  319. //go:generate stringer -type=Class
  320. func (i Class) GoString() string {
  321. return "dwarf." + i.String()
  322. }
  323. // Val returns the value associated with attribute Attr in Entry,
  324. // or nil if there is no such attribute.
  325. //
  326. // A common idiom is to merge the check for nil return with
  327. // the check that the value has the expected dynamic type, as in:
  328. // v, ok := e.Val(AttrSibling).(int64)
  329. //
  330. func (e *Entry) Val(a Attr) any {
  331. if f := e.AttrField(a); f != nil {
  332. return f.Val
  333. }
  334. return nil
  335. }
  336. // AttrField returns the Field associated with attribute Attr in
  337. // Entry, or nil if there is no such attribute.
  338. func (e *Entry) AttrField(a Attr) *Field {
  339. for i, f := range e.Field {
  340. if f.Attr == a {
  341. return &e.Field[i]
  342. }
  343. }
  344. return nil
  345. }
  346. // An Offset represents the location of an Entry within the DWARF info.
  347. // (See Reader.Seek.)
  348. type Offset uint32
  349. // Entry reads a single entry from buf, decoding
  350. // according to the given abbreviation table.
  351. func (b *buf) entry(cu *Entry, atab abbrevTable, ubase Offset, vers int) *Entry {
  352. off := b.off
  353. id := uint32(b.uint())
  354. if id == 0 {
  355. return &Entry{}
  356. }
  357. a, ok := atab[id]
  358. if !ok {
  359. b.error("unknown abbreviation table index")
  360. return nil
  361. }
  362. e := &Entry{
  363. Offset: off,
  364. Tag: a.tag,
  365. Children: a.children,
  366. Field: make([]Field, len(a.field)),
  367. }
  368. // If we are currently parsing the compilation unit,
  369. // we can't evaluate Addrx or Strx until we've seen the
  370. // relevant base entry.
  371. type delayed struct {
  372. idx int
  373. off uint64
  374. fmt format
  375. }
  376. var delay []delayed
  377. resolveStrx := func(strBase, off uint64) string {
  378. off += strBase
  379. if uint64(int(off)) != off {
  380. b.error("DW_FORM_strx offset out of range")
  381. }
  382. b1 := makeBuf(b.dwarf, b.format, "str_offsets", 0, b.dwarf.strOffsets)
  383. b1.skip(int(off))
  384. is64, _ := b.format.dwarf64()
  385. if is64 {
  386. off = b1.uint64()
  387. } else {
  388. off = uint64(b1.uint32())
  389. }
  390. if b1.err != nil {
  391. b.err = b1.err
  392. return ""
  393. }
  394. if uint64(int(off)) != off {
  395. b.error("DW_FORM_strx indirect offset out of range")
  396. }
  397. b1 = makeBuf(b.dwarf, b.format, "str", 0, b.dwarf.str)
  398. b1.skip(int(off))
  399. val := b1.string()
  400. if b1.err != nil {
  401. b.err = b1.err
  402. }
  403. return val
  404. }
  405. resolveRnglistx := func(rnglistsBase, off uint64) uint64 {
  406. is64, _ := b.format.dwarf64()
  407. if is64 {
  408. off *= 8
  409. } else {
  410. off *= 4
  411. }
  412. off += rnglistsBase
  413. if uint64(int(off)) != off {
  414. b.error("DW_FORM_rnglistx offset out of range")
  415. }
  416. b1 := makeBuf(b.dwarf, b.format, "rnglists", 0, b.dwarf.rngLists)
  417. b1.skip(int(off))
  418. if is64 {
  419. off = b1.uint64()
  420. } else {
  421. off = uint64(b1.uint32())
  422. }
  423. if b1.err != nil {
  424. b.err = b1.err
  425. return 0
  426. }
  427. if uint64(int(off)) != off {
  428. b.error("DW_FORM_rnglistx indirect offset out of range")
  429. }
  430. return rnglistsBase + off
  431. }
  432. for i := range e.Field {
  433. e.Field[i].Attr = a.field[i].attr
  434. e.Field[i].Class = a.field[i].class
  435. fmt := a.field[i].fmt
  436. if fmt == formIndirect {
  437. fmt = format(b.uint())
  438. e.Field[i].Class = formToClass(fmt, a.field[i].attr, vers, b)
  439. }
  440. var val any
  441. switch fmt {
  442. default:
  443. b.error("unknown entry attr format 0x" + strconv.FormatInt(int64(fmt), 16))
  444. // address
  445. case formAddr:
  446. val = b.addr()
  447. case formAddrx, formAddrx1, formAddrx2, formAddrx3, formAddrx4:
  448. var off uint64
  449. switch fmt {
  450. case formAddrx:
  451. off = b.uint()
  452. case formAddrx1:
  453. off = uint64(b.uint8())
  454. case formAddrx2:
  455. off = uint64(b.uint16())
  456. case formAddrx3:
  457. off = uint64(b.uint24())
  458. case formAddrx4:
  459. off = uint64(b.uint32())
  460. }
  461. if b.dwarf.addr == nil {
  462. b.error("DW_FORM_addrx with no .debug_addr section")
  463. }
  464. if b.err != nil {
  465. return nil
  466. }
  467. // We have to adjust by the offset of the
  468. // compilation unit. This won't work if the
  469. // program uses Reader.Seek to skip over the
  470. // unit. Not much we can do about that.
  471. var addrBase int64
  472. if cu != nil {
  473. addrBase, _ = cu.Val(AttrAddrBase).(int64)
  474. } else if a.tag == TagCompileUnit {
  475. delay = append(delay, delayed{i, off, formAddrx})
  476. break
  477. }
  478. var err error
  479. val, err = b.dwarf.debugAddr(b.format, uint64(addrBase), off)
  480. if err != nil {
  481. if b.err == nil {
  482. b.err = err
  483. }
  484. return nil
  485. }
  486. // block
  487. case formDwarfBlock1:
  488. val = b.bytes(int(b.uint8()))
  489. case formDwarfBlock2:
  490. val = b.bytes(int(b.uint16()))
  491. case formDwarfBlock4:
  492. val = b.bytes(int(b.uint32()))
  493. case formDwarfBlock:
  494. val = b.bytes(int(b.uint()))
  495. // constant
  496. case formData1:
  497. val = int64(b.uint8())
  498. case formData2:
  499. val = int64(b.uint16())
  500. case formData4:
  501. val = int64(b.uint32())
  502. case formData8:
  503. val = int64(b.uint64())
  504. case formData16:
  505. val = b.bytes(16)
  506. case formSdata:
  507. val = int64(b.int())
  508. case formUdata:
  509. val = int64(b.uint())
  510. case formImplicitConst:
  511. val = a.field[i].val
  512. // flag
  513. case formFlag:
  514. val = b.uint8() == 1
  515. // New in DWARF 4.
  516. case formFlagPresent:
  517. // The attribute is implicitly indicated as present, and no value is
  518. // encoded in the debugging information entry itself.
  519. val = true
  520. // reference to other entry
  521. case formRefAddr:
  522. vers := b.format.version()
  523. if vers == 0 {
  524. b.error("unknown version for DW_FORM_ref_addr")
  525. } else if vers == 2 {
  526. val = Offset(b.addr())
  527. } else {
  528. is64, known := b.format.dwarf64()
  529. if !known {
  530. b.error("unknown size for DW_FORM_ref_addr")
  531. } else if is64 {
  532. val = Offset(b.uint64())
  533. } else {
  534. val = Offset(b.uint32())
  535. }
  536. }
  537. case formRef1:
  538. val = Offset(b.uint8()) + ubase
  539. case formRef2:
  540. val = Offset(b.uint16()) + ubase
  541. case formRef4:
  542. val = Offset(b.uint32()) + ubase
  543. case formRef8:
  544. val = Offset(b.uint64()) + ubase
  545. case formRefUdata:
  546. val = Offset(b.uint()) + ubase
  547. // string
  548. case formString:
  549. val = b.string()
  550. case formStrp, formLineStrp:
  551. var off uint64 // offset into .debug_str
  552. is64, known := b.format.dwarf64()
  553. if !known {
  554. b.error("unknown size for DW_FORM_strp/line_strp")
  555. } else if is64 {
  556. off = b.uint64()
  557. } else {
  558. off = uint64(b.uint32())
  559. }
  560. if uint64(int(off)) != off {
  561. b.error("DW_FORM_strp/line_strp offset out of range")
  562. }
  563. if b.err != nil {
  564. return nil
  565. }
  566. var b1 buf
  567. if fmt == formStrp {
  568. b1 = makeBuf(b.dwarf, b.format, "str", 0, b.dwarf.str)
  569. } else {
  570. if len(b.dwarf.lineStr) == 0 {
  571. b.error("DW_FORM_line_strp with no .debug_line_str section")
  572. return nil
  573. }
  574. b1 = makeBuf(b.dwarf, b.format, "line_str", 0, b.dwarf.lineStr)
  575. }
  576. b1.skip(int(off))
  577. val = b1.string()
  578. if b1.err != nil {
  579. b.err = b1.err
  580. return nil
  581. }
  582. case formStrx, formStrx1, formStrx2, formStrx3, formStrx4:
  583. var off uint64
  584. switch fmt {
  585. case formStrx:
  586. off = b.uint()
  587. case formStrx1:
  588. off = uint64(b.uint8())
  589. case formStrx2:
  590. off = uint64(b.uint16())
  591. case formStrx3:
  592. off = uint64(b.uint24())
  593. case formStrx4:
  594. off = uint64(b.uint32())
  595. }
  596. if len(b.dwarf.strOffsets) == 0 {
  597. b.error("DW_FORM_strx with no .debug_str_offsets section")
  598. }
  599. is64, known := b.format.dwarf64()
  600. if !known {
  601. b.error("unknown offset size for DW_FORM_strx")
  602. }
  603. if b.err != nil {
  604. return nil
  605. }
  606. if is64 {
  607. off *= 8
  608. } else {
  609. off *= 4
  610. }
  611. // We have to adjust by the offset of the
  612. // compilation unit. This won't work if the
  613. // program uses Reader.Seek to skip over the
  614. // unit. Not much we can do about that.
  615. var strBase int64
  616. if cu != nil {
  617. strBase, _ = cu.Val(AttrStrOffsetsBase).(int64)
  618. } else if a.tag == TagCompileUnit {
  619. delay = append(delay, delayed{i, off, formStrx})
  620. break
  621. }
  622. val = resolveStrx(uint64(strBase), off)
  623. case formStrpSup:
  624. is64, known := b.format.dwarf64()
  625. if !known {
  626. b.error("unknown size for DW_FORM_strp_sup")
  627. } else if is64 {
  628. val = b.uint64()
  629. } else {
  630. val = b.uint32()
  631. }
  632. // lineptr, loclistptr, macptr, rangelistptr
  633. // New in DWARF 4, but clang can generate them with -gdwarf-2.
  634. // Section reference, replacing use of formData4 and formData8.
  635. case formSecOffset, formGnuRefAlt, formGnuStrpAlt:
  636. is64, known := b.format.dwarf64()
  637. if !known {
  638. b.error("unknown size for form 0x" + strconv.FormatInt(int64(fmt), 16))
  639. } else if is64 {
  640. val = int64(b.uint64())
  641. } else {
  642. val = int64(b.uint32())
  643. }
  644. // exprloc
  645. // New in DWARF 4.
  646. case formExprloc:
  647. val = b.bytes(int(b.uint()))
  648. // reference
  649. // New in DWARF 4.
  650. case formRefSig8:
  651. // 64-bit type signature.
  652. val = b.uint64()
  653. case formRefSup4:
  654. val = b.uint32()
  655. case formRefSup8:
  656. val = b.uint64()
  657. // loclist
  658. case formLoclistx:
  659. val = b.uint()
  660. // rnglist
  661. case formRnglistx:
  662. off := b.uint()
  663. // We have to adjust by the rnglists_base of
  664. // the compilation unit. This won't work if
  665. // the program uses Reader.Seek to skip over
  666. // the unit. Not much we can do about that.
  667. var rnglistsBase int64
  668. if cu != nil {
  669. rnglistsBase, _ = cu.Val(AttrRnglistsBase).(int64)
  670. } else if a.tag == TagCompileUnit {
  671. delay = append(delay, delayed{i, off, formRnglistx})
  672. break
  673. }
  674. val = resolveRnglistx(uint64(rnglistsBase), off)
  675. }
  676. e.Field[i].Val = val
  677. }
  678. if b.err != nil {
  679. return nil
  680. }
  681. for _, del := range delay {
  682. switch del.fmt {
  683. case formAddrx:
  684. addrBase, _ := e.Val(AttrAddrBase).(int64)
  685. val, err := b.dwarf.debugAddr(b.format, uint64(addrBase), del.off)
  686. if err != nil {
  687. b.err = err
  688. return nil
  689. }
  690. e.Field[del.idx].Val = val
  691. case formStrx:
  692. strBase, _ := e.Val(AttrStrOffsetsBase).(int64)
  693. e.Field[del.idx].Val = resolveStrx(uint64(strBase), del.off)
  694. if b.err != nil {
  695. return nil
  696. }
  697. case formRnglistx:
  698. rnglistsBase, _ := e.Val(AttrRnglistsBase).(int64)
  699. e.Field[del.idx].Val = resolveRnglistx(uint64(rnglistsBase), del.off)
  700. if b.err != nil {
  701. return nil
  702. }
  703. }
  704. }
  705. return e
  706. }
  707. // A Reader allows reading Entry structures from a DWARF ``info'' section.
  708. // The Entry structures are arranged in a tree. The Reader's Next function
  709. // return successive entries from a pre-order traversal of the tree.
  710. // If an entry has children, its Children field will be true, and the children
  711. // follow, terminated by an Entry with Tag 0.
  712. type Reader struct {
  713. b buf
  714. d *Data
  715. err error
  716. unit int
  717. lastUnit bool // set if last entry returned by Next is TagCompileUnit/TagPartialUnit
  718. lastChildren bool // .Children of last entry returned by Next
  719. lastSibling Offset // .Val(AttrSibling) of last entry returned by Next
  720. cu *Entry // current compilation unit
  721. }
  722. // Reader returns a new Reader for Data.
  723. // The reader is positioned at byte offset 0 in the DWARF ``info'' section.
  724. func (d *Data) Reader() *Reader {
  725. r := &Reader{d: d}
  726. r.Seek(0)
  727. return r
  728. }
  729. // AddressSize returns the size in bytes of addresses in the current compilation
  730. // unit.
  731. func (r *Reader) AddressSize() int {
  732. return r.d.unit[r.unit].asize
  733. }
  734. // ByteOrder returns the byte order in the current compilation unit.
  735. func (r *Reader) ByteOrder() binary.ByteOrder {
  736. return r.b.order
  737. }
  738. // Seek positions the Reader at offset off in the encoded entry stream.
  739. // Offset 0 can be used to denote the first entry.
  740. func (r *Reader) Seek(off Offset) {
  741. d := r.d
  742. r.err = nil
  743. r.lastChildren = false
  744. if off == 0 {
  745. if len(d.unit) == 0 {
  746. return
  747. }
  748. u := &d.unit[0]
  749. r.unit = 0
  750. r.b = makeBuf(r.d, u, "info", u.off, u.data)
  751. r.cu = nil
  752. return
  753. }
  754. i := d.offsetToUnit(off)
  755. if i == -1 {
  756. r.err = errors.New("offset out of range")
  757. return
  758. }
  759. if i != r.unit {
  760. r.cu = nil
  761. }
  762. u := &d.unit[i]
  763. r.unit = i
  764. r.b = makeBuf(r.d, u, "info", off, u.data[off-u.off:])
  765. }
  766. // maybeNextUnit advances to the next unit if this one is finished.
  767. func (r *Reader) maybeNextUnit() {
  768. for len(r.b.data) == 0 && r.unit+1 < len(r.d.unit) {
  769. r.nextUnit()
  770. }
  771. }
  772. // nextUnit advances to the next unit.
  773. func (r *Reader) nextUnit() {
  774. r.unit++
  775. u := &r.d.unit[r.unit]
  776. r.b = makeBuf(r.d, u, "info", u.off, u.data)
  777. r.cu = nil
  778. }
  779. // Next reads the next entry from the encoded entry stream.
  780. // It returns nil, nil when it reaches the end of the section.
  781. // It returns an error if the current offset is invalid or the data at the
  782. // offset cannot be decoded as a valid Entry.
  783. func (r *Reader) Next() (*Entry, error) {
  784. if r.err != nil {
  785. return nil, r.err
  786. }
  787. r.maybeNextUnit()
  788. if len(r.b.data) == 0 {
  789. return nil, nil
  790. }
  791. u := &r.d.unit[r.unit]
  792. e := r.b.entry(r.cu, u.atable, u.base, u.vers)
  793. if r.b.err != nil {
  794. r.err = r.b.err
  795. return nil, r.err
  796. }
  797. r.lastUnit = false
  798. if e != nil {
  799. r.lastChildren = e.Children
  800. if r.lastChildren {
  801. r.lastSibling, _ = e.Val(AttrSibling).(Offset)
  802. }
  803. if e.Tag == TagCompileUnit || e.Tag == TagPartialUnit {
  804. r.lastUnit = true
  805. r.cu = e
  806. }
  807. } else {
  808. r.lastChildren = false
  809. }
  810. return e, nil
  811. }
  812. // SkipChildren skips over the child entries associated with
  813. // the last Entry returned by Next. If that Entry did not have
  814. // children or Next has not been called, SkipChildren is a no-op.
  815. func (r *Reader) SkipChildren() {
  816. if r.err != nil || !r.lastChildren {
  817. return
  818. }
  819. // If the last entry had a sibling attribute,
  820. // that attribute gives the offset of the next
  821. // sibling, so we can avoid decoding the
  822. // child subtrees.
  823. if r.lastSibling >= r.b.off {
  824. r.Seek(r.lastSibling)
  825. return
  826. }
  827. if r.lastUnit && r.unit+1 < len(r.d.unit) {
  828. r.nextUnit()
  829. return
  830. }
  831. for {
  832. e, err := r.Next()
  833. if err != nil || e == nil || e.Tag == 0 {
  834. break
  835. }
  836. if e.Children {
  837. r.SkipChildren()
  838. }
  839. }
  840. }
  841. // clone returns a copy of the reader. This is used by the typeReader
  842. // interface.
  843. func (r *Reader) clone() typeReader {
  844. return r.d.Reader()
  845. }
  846. // offset returns the current buffer offset. This is used by the
  847. // typeReader interface.
  848. func (r *Reader) offset() Offset {
  849. return r.b.off
  850. }
  851. // SeekPC returns the Entry for the compilation unit that includes pc,
  852. // and positions the reader to read the children of that unit. If pc
  853. // is not covered by any unit, SeekPC returns ErrUnknownPC and the
  854. // position of the reader is undefined.
  855. //
  856. // Because compilation units can describe multiple regions of the
  857. // executable, in the worst case SeekPC must search through all the
  858. // ranges in all the compilation units. Each call to SeekPC starts the
  859. // search at the compilation unit of the last call, so in general
  860. // looking up a series of PCs will be faster if they are sorted. If
  861. // the caller wishes to do repeated fast PC lookups, it should build
  862. // an appropriate index using the Ranges method.
  863. func (r *Reader) SeekPC(pc uint64) (*Entry, error) {
  864. unit := r.unit
  865. for i := 0; i < len(r.d.unit); i++ {
  866. if unit >= len(r.d.unit) {
  867. unit = 0
  868. }
  869. r.err = nil
  870. r.lastChildren = false
  871. r.unit = unit
  872. r.cu = nil
  873. u := &r.d.unit[unit]
  874. r.b = makeBuf(r.d, u, "info", u.off, u.data)
  875. e, err := r.Next()
  876. if err != nil {
  877. return nil, err
  878. }
  879. ranges, err := r.d.Ranges(e)
  880. if err != nil {
  881. return nil, err
  882. }
  883. for _, pcs := range ranges {
  884. if pcs[0] <= pc && pc < pcs[1] {
  885. return e, nil
  886. }
  887. }
  888. unit++
  889. }
  890. return nil, ErrUnknownPC
  891. }
  892. // Ranges returns the PC ranges covered by e, a slice of [low,high) pairs.
  893. // Only some entry types, such as TagCompileUnit or TagSubprogram, have PC
  894. // ranges; for others, this will return nil with no error.
  895. func (d *Data) Ranges(e *Entry) ([][2]uint64, error) {
  896. var ret [][2]uint64
  897. low, lowOK := e.Val(AttrLowpc).(uint64)
  898. var high uint64
  899. var highOK bool
  900. highField := e.AttrField(AttrHighpc)
  901. if highField != nil {
  902. switch highField.Class {
  903. case ClassAddress:
  904. high, highOK = highField.Val.(uint64)
  905. case ClassConstant:
  906. off, ok := highField.Val.(int64)
  907. if ok {
  908. high = low + uint64(off)
  909. highOK = true
  910. }
  911. }
  912. }
  913. if lowOK && highOK {
  914. ret = append(ret, [2]uint64{low, high})
  915. }
  916. var u *unit
  917. if uidx := d.offsetToUnit(e.Offset); uidx >= 0 && uidx < len(d.unit) {
  918. u = &d.unit[uidx]
  919. }
  920. if u != nil && u.vers >= 5 && d.rngLists != nil {
  921. // DWARF version 5 and later
  922. field := e.AttrField(AttrRanges)
  923. if field == nil {
  924. return ret, nil
  925. }
  926. switch field.Class {
  927. case ClassRangeListPtr:
  928. ranges, rangesOK := field.Val.(int64)
  929. if !rangesOK {
  930. return ret, nil
  931. }
  932. cu, base, err := d.baseAddressForEntry(e)
  933. if err != nil {
  934. return nil, err
  935. }
  936. return d.dwarf5Ranges(u, cu, base, ranges, ret)
  937. case ClassRngList:
  938. rnglist, ok := field.Val.(uint64)
  939. if !ok {
  940. return ret, nil
  941. }
  942. cu, base, err := d.baseAddressForEntry(e)
  943. if err != nil {
  944. return nil, err
  945. }
  946. return d.dwarf5Ranges(u, cu, base, int64(rnglist), ret)
  947. default:
  948. return ret, nil
  949. }
  950. }
  951. // DWARF version 2 through 4
  952. ranges, rangesOK := e.Val(AttrRanges).(int64)
  953. if rangesOK && d.ranges != nil {
  954. _, base, err := d.baseAddressForEntry(e)
  955. if err != nil {
  956. return nil, err
  957. }
  958. return d.dwarf2Ranges(u, base, ranges, ret)
  959. }
  960. return ret, nil
  961. }
  962. // baseAddressForEntry returns the initial base address to be used when
  963. // looking up the range list of entry e.
  964. // DWARF specifies that this should be the lowpc attribute of the enclosing
  965. // compilation unit, however comments in gdb/dwarf2read.c say that some
  966. // versions of GCC use the entrypc attribute, so we check that too.
  967. func (d *Data) baseAddressForEntry(e *Entry) (*Entry, uint64, error) {
  968. var cu *Entry
  969. if e.Tag == TagCompileUnit {
  970. cu = e
  971. } else {
  972. i := d.offsetToUnit(e.Offset)
  973. if i == -1 {
  974. return nil, 0, errors.New("no unit for entry")
  975. }
  976. u := &d.unit[i]
  977. b := makeBuf(d, u, "info", u.off, u.data)
  978. cu = b.entry(nil, u.atable, u.base, u.vers)
  979. if b.err != nil {
  980. return nil, 0, b.err
  981. }
  982. }
  983. if cuEntry, cuEntryOK := cu.Val(AttrEntrypc).(uint64); cuEntryOK {
  984. return cu, cuEntry, nil
  985. } else if cuLow, cuLowOK := cu.Val(AttrLowpc).(uint64); cuLowOK {
  986. return cu, cuLow, nil
  987. }
  988. return cu, 0, nil
  989. }
  990. func (d *Data) dwarf2Ranges(u *unit, base uint64, ranges int64, ret [][2]uint64) ([][2]uint64, error) {
  991. buf := makeBuf(d, u, "ranges", Offset(ranges), d.ranges[ranges:])
  992. for len(buf.data) > 0 {
  993. low := buf.addr()
  994. high := buf.addr()
  995. if low == 0 && high == 0 {
  996. break
  997. }
  998. if low == ^uint64(0)>>uint((8-u.addrsize())*8) {
  999. base = high
  1000. } else {
  1001. ret = append(ret, [2]uint64{base + low, base + high})
  1002. }
  1003. }
  1004. return ret, nil
  1005. }
  1006. // dwarf5Ranges interpets a debug_rnglists sequence, see DWARFv5 section
  1007. // 2.17.3 (page 53).
  1008. func (d *Data) dwarf5Ranges(u *unit, cu *Entry, base uint64, ranges int64, ret [][2]uint64) ([][2]uint64, error) {
  1009. var addrBase int64
  1010. if cu != nil {
  1011. addrBase, _ = cu.Val(AttrAddrBase).(int64)
  1012. }
  1013. buf := makeBuf(d, u, "rnglists", 0, d.rngLists)
  1014. buf.skip(int(ranges))
  1015. for {
  1016. opcode := buf.uint8()
  1017. switch opcode {
  1018. case rleEndOfList:
  1019. if buf.err != nil {
  1020. return nil, buf.err
  1021. }
  1022. return ret, nil
  1023. case rleBaseAddressx:
  1024. baseIdx := buf.uint()
  1025. var err error
  1026. base, err = d.debugAddr(u, uint64(addrBase), baseIdx)
  1027. if err != nil {
  1028. return nil, err
  1029. }
  1030. case rleStartxEndx:
  1031. startIdx := buf.uint()
  1032. endIdx := buf.uint()
  1033. start, err := d.debugAddr(u, uint64(addrBase), startIdx)
  1034. if err != nil {
  1035. return nil, err
  1036. }
  1037. end, err := d.debugAddr(u, uint64(addrBase), endIdx)
  1038. if err != nil {
  1039. return nil, err
  1040. }
  1041. ret = append(ret, [2]uint64{start, end})
  1042. case rleStartxLength:
  1043. startIdx := buf.uint()
  1044. len := buf.uint()
  1045. start, err := d.debugAddr(u, uint64(addrBase), startIdx)
  1046. if err != nil {
  1047. return nil, err
  1048. }
  1049. ret = append(ret, [2]uint64{start, start + len})
  1050. case rleOffsetPair:
  1051. off1 := buf.uint()
  1052. off2 := buf.uint()
  1053. ret = append(ret, [2]uint64{base + off1, base + off2})
  1054. case rleBaseAddress:
  1055. base = buf.addr()
  1056. case rleStartEnd:
  1057. start := buf.addr()
  1058. end := buf.addr()
  1059. ret = append(ret, [2]uint64{start, end})
  1060. case rleStartLength:
  1061. start := buf.addr()
  1062. len := buf.uint()
  1063. ret = append(ret, [2]uint64{start, start + len})
  1064. }
  1065. }
  1066. }
  1067. // debugAddr returns the address at idx in debug_addr
  1068. func (d *Data) debugAddr(format dataFormat, addrBase, idx uint64) (uint64, error) {
  1069. off := idx*uint64(format.addrsize()) + addrBase
  1070. if uint64(int(off)) != off {
  1071. return 0, errors.New("offset out of range")
  1072. }
  1073. b := makeBuf(d, format, "addr", 0, d.addr)
  1074. b.skip(int(off))
  1075. val := b.addr()
  1076. if b.err != nil {
  1077. return 0, b.err
  1078. }
  1079. return val, nil
  1080. }