dwarf.scm 58 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853
  1. ;;; Guile DWARF reader and writer
  2. ;; Copyright (C) 2012, 2013 Free Software Foundation, Inc.
  3. ;; Parts of this file were derived from sysdeps/generic/dwarf2.h, from
  4. ;; the GNU C Library. That file is available under the LGPL version 2
  5. ;; or later, and is copyright:
  6. ;;
  7. ;; Copyright (C) 1992, 1993, 1995, 1996, 1997, 2000, 2011
  8. ;; Free Software Foundation, Inc.
  9. ;; Contributed by Gary Funck (gary@intrepid.com). Derived from the
  10. ;; DWARF 1 implementation written by Ron Guilmette (rfg@monkeys.com).
  11. ;;;; This library is free software; you can redistribute it and/or
  12. ;;;; modify it under the terms of the GNU Lesser General Public
  13. ;;;; License as published by the Free Software Foundation; either
  14. ;;;; version 3 of the License, or (at your option) any later version.
  15. ;;;;
  16. ;;;; This library is distributed in the hope that it will be useful,
  17. ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  19. ;;;; Lesser General Public License for more details.
  20. ;;;;
  21. ;;;; You should have received a copy of the GNU Lesser General Public
  22. ;;;; License along with this library; if not, write to the Free Software
  23. ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  24. ;;; Commentary:
  25. ;;
  26. ;; DWARF is a flexible format for describing compiled programs. It is
  27. ;; used by Guile to record source positions, describe local variables,
  28. ;; function arities, and other function metadata.
  29. ;;
  30. ;; Structurally, DWARF describes a tree of data. Each node in the tree
  31. ;; is a debugging information entry ("DIE"). Each DIE has a "tag",
  32. ;; possible a set of attributes, and possibly some child DIE nodes.
  33. ;; That's basically it!
  34. ;;
  35. ;; The DIE nodes are contained in the .debug_info section of an ELF
  36. ;; file. Attributes within the DIE nodes link them to mapped ranges of
  37. ;; the ELF file (.rtl-text, .data, etc.).
  38. ;;
  39. ;; A .debug_info section logically contains a series of debugging
  40. ;; "contributions", one for each compilation unit. Each contribution is
  41. ;; prefixed by a header and contains a single DIE element whose tag is
  42. ;; "compilation-unit". That node usually contains child nodes, for
  43. ;; example of type "subprogram".
  44. ;;
  45. ;; Since usually one will end up producing many DIE nodes with the same
  46. ;; tag and attribute types, DIE nodes are defined by referencing a known
  47. ;; shape, and then filling in the values. The shapes are defined in the
  48. ;; form of "abbrev" entries, which specify a specific combination of a
  49. ;; tag and an ordered set of attributes, with corresponding attribute
  50. ;; representations ("forms"). Abbrevs are written out to a separate
  51. ;; section, .debug_abbrev. Abbrev nodes also specify whether the
  52. ;; corresponding DIE node has children or not. When a DIE is written
  53. ;; into the .debug_info section, it references one of the abbrevs in
  54. ;; .debug_abbrev. You need the abbrev in order to parse the DIE.
  55. ;;
  56. ;; For completeness, the other sections that DWARF uses are .debug_str,
  57. ;; .debug_loc, .debug_pubnames, .debug_aranges, .debug_frame, and
  58. ;; .debug_line. These are described in section 6 of the DWARF 3.0
  59. ;; specification, at http://dwarfstd.org/.
  60. ;;
  61. ;; This DWARF module is currently capable of parsing all of DWARF 2.0
  62. ;; and parts of DWARF 3.0. For Guile's purposes, we also use DWARF as
  63. ;; the format for our own debugging information. The DWARF generator is
  64. ;; fairly minimal, and is not intended to be complete.
  65. ;;
  66. ;;; Code:
  67. (define-module (system vm dwarf)
  68. #:use-module (rnrs bytevectors)
  69. #:use-module (system foreign)
  70. #:use-module (system base target)
  71. #:use-module (system vm elf)
  72. #:use-module ((srfi srfi-1) #:select (fold))
  73. #:use-module (srfi srfi-9)
  74. #:use-module (srfi srfi-9 gnu)
  75. #:use-module (srfi srfi-11)
  76. #:export (elf->dwarf-context
  77. read-die-roots
  78. fold-pubnames fold-aranges
  79. access-name->code
  80. address-name->code
  81. attribute-name->code
  82. call-frame-address-name->code
  83. children-name->code
  84. convention-name->code
  85. discriminant-name->code
  86. form-name->code
  87. inline-name->code
  88. language-name->code
  89. macro-name->code
  90. ordering-name->code
  91. sensitivity-name->code
  92. tag-name->code
  93. virtuality-name->code
  94. visibility-name->code
  95. abbrev? abbrev-code
  96. abbrev-tag abbrev-has-children? abbrev-attrs abbrev-forms
  97. die? die-ctx die-offset die-abbrev die-vals die-children
  98. die-tag die-attrs die-forms die-ref
  99. die-name die-specification die-qname die-low-pc die-high-pc
  100. ctx-parent ctx-die ctx-start ctx-end ctx-children ctx-language
  101. die-line-prog line-prog-advance line-prog-scan-to-pc
  102. find-die-context find-die-by-offset find-die find-die-by-pc
  103. read-die fold-die-list
  104. fold-die-children die->tree))
  105. ;;;
  106. ;;; First, define a number of constants. The figures numbers refer to
  107. ;;; the DWARF 2.0 draft specification available on http://dwarfstd.org/.
  108. ;;; Extra codes not defined in that document are taken from the dwarf2
  109. ;;; header in glibc.
  110. ;;;
  111. (define-syntax-rule (define-enumeration code->name name->code
  112. (tag value) ...)
  113. (begin
  114. (define code->name
  115. (let ((table (make-hash-table)))
  116. (hashv-set! table value 'tag)
  117. ...
  118. (lambda (v)
  119. (hashv-ref table v v))))
  120. (define name->code
  121. (let ((table (make-hash-table)))
  122. (hashv-set! table 'tag value)
  123. ...
  124. (lambda (v)
  125. (hashv-ref table v v))))))
  126. ;; Figures 14 and 15: Tag names and codes.
  127. ;;
  128. (define-enumeration tag-code->name tag-name->code
  129. (padding #x00)
  130. (array-type #x01)
  131. (class-type #x02)
  132. (entry-point #x03)
  133. (enumeration-type #x04)
  134. (formal-parameter #x05)
  135. (imported-declaration #x08)
  136. (label #x0a)
  137. (lexical-block #x0b)
  138. (member #x0d)
  139. (pointer-type #x0f)
  140. (reference-type #x10)
  141. (compile-unit #x11)
  142. (string-type #x12)
  143. (structure-type #x13)
  144. (subroutine-type #x15)
  145. (typedef #x16)
  146. (union-type #x17)
  147. (unspecified-parameters #x18)
  148. (variant #x19)
  149. (common-block #x1a)
  150. (common-inclusion #x1b)
  151. (inheritance #x1c)
  152. (inlined-subroutine #x1d)
  153. (module #x1e)
  154. (ptr-to-member-type #x1f)
  155. (set-type #x20)
  156. (subrange-type #x21)
  157. (with-stmt #x22)
  158. (access-declaration #x23)
  159. (base-type #x24)
  160. (catch-block #x25)
  161. (const-type #x26)
  162. (constant #x27)
  163. (enumerator #x28)
  164. (file-type #x29)
  165. (friend #x2a)
  166. (namelist #x2b)
  167. (namelist-item #x2c)
  168. (packed-type #x2d)
  169. (subprogram #x2e)
  170. (template-type-param #x2f)
  171. (template-value-param #x30)
  172. (thrown-type #x31)
  173. (try-block #x32)
  174. (variant-part #x33)
  175. (variable #x34)
  176. (volatile-type #x35)
  177. ;; DWARF 3.
  178. (dwarf-procedure #x36)
  179. (restrict-type #x37)
  180. (interface-type #x38)
  181. (namespace #x39)
  182. (imported-module #x3a)
  183. (unspecified-type #x3b)
  184. (partial-unit #x3c)
  185. (imported-unit #x3d)
  186. (condition #x3f)
  187. (shared-type #x40)
  188. ;; Extensions.
  189. (format-label #x4101)
  190. (function-template #x4102)
  191. (class-template #x4103)
  192. (GNU-BINCL #x4104)
  193. (GNU-EINCL #x4105)
  194. (lo-user #x4080)
  195. (hi-user #xffff))
  196. ;; Figure 16: Flag that tells whether entry has a child or not.
  197. ;;
  198. (define-enumeration children-code->name children-name->code
  199. (no 0)
  200. (yes 1))
  201. ;; Figures 17 and 18: Attribute names and codes.
  202. ;;
  203. (define-enumeration attribute-code->name attribute-name->code
  204. (sibling #x01)
  205. (location #x02)
  206. (name #x03)
  207. (ordering #x09)
  208. (subscr-data #x0a)
  209. (byte-size #x0b)
  210. (bit-offset #x0c)
  211. (bit-size #x0d)
  212. (element-list #x0f)
  213. (stmt-list #x10)
  214. (low-pc #x11)
  215. (high-pc #x12)
  216. (language #x13)
  217. (member #x14)
  218. (discr #x15)
  219. (discr-value #x16)
  220. (visibility #x17)
  221. (import #x18)
  222. (string-length #x19)
  223. (common-reference #x1a)
  224. (comp-dir #x1b)
  225. (const-value #x1c)
  226. (containing-type #x1d)
  227. (default-value #x1e)
  228. (inline #x20)
  229. (is-optional #x21)
  230. (lower-bound #x22)
  231. (producer #x25)
  232. (prototyped #x27)
  233. (return-addr #x2a)
  234. (start-scope #x2c)
  235. (stride-size #x2e)
  236. (upper-bound #x2f)
  237. (abstract-origin #x31)
  238. (accessibility #x32)
  239. (address-class #x33)
  240. (artificial #x34)
  241. (base-types #x35)
  242. (calling-convention #x36)
  243. (count #x37)
  244. (data-member-location #x38)
  245. (decl-column #x39)
  246. (decl-file #x3a)
  247. (decl-line #x3b)
  248. (declaration #x3c)
  249. (discr-list #x3d)
  250. (encoding #x3e)
  251. (external #x3f)
  252. (frame-base #x40)
  253. (friend #x41)
  254. (identifier-case #x42)
  255. (macro-info #x43)
  256. (namelist-items #x44)
  257. (priority #x45)
  258. (segment #x46)
  259. (specification #x47)
  260. (static-link #x48)
  261. (type #x49)
  262. (use-location #x4a)
  263. (variable-parameter #x4b)
  264. (virtuality #x4c)
  265. (vtable-elem-location #x4d)
  266. ;; DWARF 3.
  267. (associated #x4f)
  268. (data-location #x50)
  269. (byte-stride #x51)
  270. (entry-pc #x52)
  271. (use-UTF8 #x53)
  272. (extension #x54)
  273. (ranges #x55)
  274. (trampoline #x56)
  275. (call-column #x57)
  276. (call-file #x58)
  277. (call-line #x59)
  278. (description #x5a)
  279. (binary-scale #x5b)
  280. (decimal-scale #x5c)
  281. (small #x5d)
  282. (decimal-sign #x5e)
  283. (digit-count #x5f)
  284. (picture-string #x60)
  285. (mutable #x61)
  286. (threads-scaled #x62)
  287. (explicit #x63)
  288. (object-pointer #x64)
  289. (endianity #x65)
  290. (elemental #x66)
  291. (pure #x67)
  292. (recursive #x68)
  293. ;; Extensions.
  294. (linkage-name #x2007)
  295. (sf-names #x2101)
  296. (src-info #x2102)
  297. (mac-info #x2103)
  298. (src-coords #x2104)
  299. (body-begin #x2105)
  300. (body-end #x2106)
  301. (lo-user #x2000)
  302. (hi-user #x3fff))
  303. ;; Figure 19: Form names and codes.
  304. ;;
  305. (define-enumeration form-code->name form-name->code
  306. (addr #x01)
  307. (block2 #x03)
  308. (block4 #x04)
  309. (data2 #x05)
  310. (data4 #x06)
  311. (data8 #x07)
  312. (string #x08)
  313. (block #x09)
  314. (block1 #x0a)
  315. (data1 #x0b)
  316. (flag #x0c)
  317. (sdata #x0d)
  318. (strp #x0e)
  319. (udata #x0f)
  320. (ref-addr #x10)
  321. (ref1 #x11)
  322. (ref2 #x12)
  323. (ref4 #x13)
  324. (ref8 #x14)
  325. (ref-udata #x15)
  326. (indirect #x16)
  327. (sec-offset #x17)
  328. (exprloc #x18)
  329. (flag-present #x19)
  330. (ref-sig8 #x20))
  331. ;; Figures 22 and 23: Location atom names and codes.
  332. ;;
  333. (define-enumeration location-op->name location-name->op
  334. (addr #x03)
  335. (deref #x06)
  336. (const1u #x08)
  337. (const1s #x09)
  338. (const2u #x0a)
  339. (const2s #x0b)
  340. (const4u #x0c)
  341. (const4s #x0d)
  342. (const8u #x0e)
  343. (const8s #x0f)
  344. (constu #x10)
  345. (consts #x11)
  346. (dup #x12)
  347. (drop #x13)
  348. (over #x14)
  349. (pick #x15)
  350. (swap #x16)
  351. (rot #x17)
  352. (xderef #x18)
  353. (abs #x19)
  354. (and #x1a)
  355. (div #x1b)
  356. (minus #x1c)
  357. (mod #x1d)
  358. (mul #x1e)
  359. (neg #x1f)
  360. (not #x20)
  361. (or #x21)
  362. (plus #x22)
  363. (plus-uconst #x23)
  364. (shl #x24)
  365. (shr #x25)
  366. (shra #x26)
  367. (xor #x27)
  368. (bra #x28)
  369. (eq #x29)
  370. (ge #x2a)
  371. (gt #x2b)
  372. (le #x2c)
  373. (lt #x2d)
  374. (ne #x2e)
  375. (skip #x2f)
  376. (lit0 #x30)
  377. (lit1 #x31)
  378. (lit2 #x32)
  379. (lit3 #x33)
  380. (lit4 #x34)
  381. (lit5 #x35)
  382. (lit6 #x36)
  383. (lit7 #x37)
  384. (lit8 #x38)
  385. (lit9 #x39)
  386. (lit10 #x3a)
  387. (lit11 #x3b)
  388. (lit12 #x3c)
  389. (lit13 #x3d)
  390. (lit14 #x3e)
  391. (lit15 #x3f)
  392. (lit16 #x40)
  393. (lit17 #x41)
  394. (lit18 #x42)
  395. (lit19 #x43)
  396. (lit20 #x44)
  397. (lit21 #x45)
  398. (lit22 #x46)
  399. (lit23 #x47)
  400. (lit24 #x48)
  401. (lit25 #x49)
  402. (lit26 #x4a)
  403. (lit27 #x4b)
  404. (lit28 #x4c)
  405. (lit29 #x4d)
  406. (lit30 #x4e)
  407. (lit31 #x4f)
  408. (reg0 #x50)
  409. (reg1 #x51)
  410. (reg2 #x52)
  411. (reg3 #x53)
  412. (reg4 #x54)
  413. (reg5 #x55)
  414. (reg6 #x56)
  415. (reg7 #x57)
  416. (reg8 #x58)
  417. (reg9 #x59)
  418. (reg10 #x5a)
  419. (reg11 #x5b)
  420. (reg12 #x5c)
  421. (reg13 #x5d)
  422. (reg14 #x5e)
  423. (reg15 #x5f)
  424. (reg16 #x60)
  425. (reg17 #x61)
  426. (reg18 #x62)
  427. (reg19 #x63)
  428. (reg20 #x64)
  429. (reg21 #x65)
  430. (reg22 #x66)
  431. (reg23 #x67)
  432. (reg24 #x68)
  433. (reg25 #x69)
  434. (reg26 #x6a)
  435. (reg27 #x6b)
  436. (reg28 #x6c)
  437. (reg29 #x6d)
  438. (reg30 #x6e)
  439. (reg31 #x6f)
  440. (breg0 #x70)
  441. (breg1 #x71)
  442. (breg2 #x72)
  443. (breg3 #x73)
  444. (breg4 #x74)
  445. (breg5 #x75)
  446. (breg6 #x76)
  447. (breg7 #x77)
  448. (breg8 #x78)
  449. (breg9 #x79)
  450. (breg10 #x7a)
  451. (breg11 #x7b)
  452. (breg12 #x7c)
  453. (breg13 #x7d)
  454. (breg14 #x7e)
  455. (breg15 #x7f)
  456. (breg16 #x80)
  457. (breg17 #x81)
  458. (breg18 #x82)
  459. (breg19 #x83)
  460. (breg20 #x84)
  461. (breg21 #x85)
  462. (breg22 #x86)
  463. (breg23 #x87)
  464. (breg24 #x88)
  465. (breg25 #x89)
  466. (breg26 #x8a)
  467. (breg27 #x8b)
  468. (breg28 #x8c)
  469. (breg29 #x8d)
  470. (breg30 #x8e)
  471. (breg31 #x8f)
  472. (regx #x90)
  473. (fbreg #x91)
  474. (bregx #x92)
  475. (piece #x93)
  476. (deref-size #x94)
  477. (xderef-size #x95)
  478. (nop #x96)
  479. ;; DWARF 3.
  480. (push-object-address #x97)
  481. (call2 #x98)
  482. (call4 #x99)
  483. (call-ref #x9a)
  484. (form-tls-address #x9b)
  485. (call-frame-cfa #x9c)
  486. (bit-piece #x9d)
  487. (lo-user #x80)
  488. (hi-user #xff))
  489. ;; Figure 24: Type encodings.
  490. ;;
  491. (define-enumeration type-encoding->name type-name->encoding
  492. (void #x0)
  493. (address #x1)
  494. (boolean #x2)
  495. (complex-float #x3)
  496. (float #x4)
  497. (signed #x5)
  498. (signed-char #x6)
  499. (unsigned #x7)
  500. (unsigned-char #x8)
  501. ;; DWARF 3.
  502. (imaginary-float #x09)
  503. (packed-decimal #x0a)
  504. (numeric-string #x0b)
  505. (edited #x0c)
  506. (signed-fixed #x0d)
  507. (unsigned-fixed #x0e)
  508. (decimal-float #x0f)
  509. (lo-user #x80)
  510. (hi-user #xff))
  511. ;; Figure 25: Access attribute.
  512. ;;
  513. (define-enumeration access-code->name access-name->code
  514. (public 1)
  515. (protected 2)
  516. (private 3))
  517. ;; Figure 26: Visibility.
  518. ;;
  519. (define-enumeration visibility-code->name visibility-name->code
  520. (local 1)
  521. (exported 2)
  522. (qualified 3))
  523. ;; Figure 27: Virtuality.
  524. ;;
  525. (define-enumeration virtuality-code->name virtuality-name->code
  526. (none 0)
  527. (virtual 1)
  528. (pure-virtual 2))
  529. ;; Figure 28: Source language names and codes.
  530. ;;
  531. (define-enumeration language-code->name language-name->code
  532. (c89 #x0001)
  533. (c #x0002)
  534. (ada83 #x0003)
  535. (c++ #x0004)
  536. (cobol74 #x0005)
  537. (cobol85 #x0006)
  538. (fortran77 #x0007)
  539. (fortran90 #x0008)
  540. (pascal83 #x0009)
  541. (modula2 #x000a)
  542. (java #x000b)
  543. (c99 #x000c)
  544. (ada95 #x000d)
  545. (fortran95 #x000e)
  546. (pli #x000f)
  547. (objc #x0010)
  548. (objc++ #x0011)
  549. (upc #x0012)
  550. (d #x0013)
  551. (python #x0014)
  552. (mips-assembler #x8001)
  553. (lo-user #x8000)
  554. ;; FIXME: Ask for proper codes for these.
  555. (scheme #xaf33)
  556. (emacs-lisp #xaf34)
  557. (ecmascript #xaf35)
  558. (lua #xaf36)
  559. (brainfuck #xaf37)
  560. (hi-user #xffff))
  561. ;; Figure 29: Case sensitivity.
  562. ;;
  563. (define-enumeration case-sensitivity-code->name case-sensitivity-name->code
  564. (case-sensitive 0)
  565. (up-case 1)
  566. (down-case 2)
  567. (case-insensitive 3))
  568. ;; Figure 30: Calling convention.
  569. ;;
  570. (define-enumeration calling-convention-code->name calling-convention-name->code
  571. (normal #x1)
  572. (program #x2)
  573. (nocall #x3)
  574. (lo-user #x40)
  575. (hi-user #xff))
  576. ;; Figure 31: Inline attribute.
  577. ;;
  578. (define-enumeration inline-code->name inline-name->code
  579. (not-inlined 0)
  580. (inlined 1)
  581. (declared-not-inlined 2)
  582. (declared-inlined 3))
  583. ;; Figure 32: Array ordering names and codes.
  584. (define-enumeration ordering-code->name ordering-name->code
  585. (row-major 0)
  586. (col-major 1))
  587. ;; Figure 33: Discriminant lists.
  588. ;;
  589. (define-enumeration discriminant-code->name discriminant-name->code
  590. (label 0)
  591. (range 1))
  592. ;; Figure 34: "Standard" line number opcodes.
  593. ;;
  594. (define-enumeration standard-line-opcode->name standard-line-name->opcode
  595. (extended-op 0)
  596. (copy 1)
  597. (advance-pc 2)
  598. (advance-line 3)
  599. (set-file 4)
  600. (set-column 5)
  601. (negate-stmt 6)
  602. (set-basic-block 7)
  603. (const-add-pc 8)
  604. (fixed-advance-pc 9)
  605. ;; DWARF 3.
  606. (set-prologue-end #x0a)
  607. (set-epilogue-begin #x0b)
  608. (set-isa #x0c))
  609. ;; Figure 35: "Extended" line number opcodes.
  610. ;;
  611. (define-enumeration extended-line-opcode->name extended-line-name->opcode
  612. (end-sequence 1)
  613. (set-address 2)
  614. (define-file 3)
  615. ;; DWARF 3.
  616. (lo-user #x80)
  617. (hi-user #xff))
  618. ;; Figure 36: Names and codes for macro information.
  619. ;;
  620. (define-enumeration macro-code->name macro-name->code
  621. (define 1)
  622. (undef 2)
  623. (start-file 3)
  624. (end-file 4)
  625. (vendor-ext 255))
  626. ;; Figure 37: Call frame information.
  627. ;;
  628. (define-enumeration call-frame-address-code->name call-frame-address-code->name
  629. (advance-loc #x40)
  630. (offset #x80)
  631. (restore #xc0)
  632. (nop #x00)
  633. (set-loc #x01)
  634. (advance-loc1 #x02)
  635. (advance-loc2 #x03)
  636. (advance-loc4 #x04)
  637. (offset-extended #x05)
  638. (restore-extended #x06)
  639. (undefined #x07)
  640. (same-value #x08)
  641. (register #x09)
  642. (remember-state #x0a)
  643. (restore-state #x0b)
  644. (def-cfa #x0c)
  645. (def-cfa-register #x0d)
  646. (def-cfa-offset #x0e)
  647. ;; DWARF 3.
  648. (def-cfa-expression #x0f)
  649. (expression #x10)
  650. (offset-extended-sf #x11)
  651. (def-cfa-sf #x12)
  652. (def-cfa-offset-sf #x13)
  653. (val-offset #x14)
  654. (val-offset-sf #x15)
  655. (val-expression #x16)
  656. (GNU-window-save #x2d)
  657. (GNU-args-size #x2e)
  658. (GNU-negative-offset-extended #x2f)
  659. (extended 0)
  660. (low-user #x1c)
  661. (high-user #x3f))
  662. ;(define CIE-ID #xffffffff)
  663. ;(define CIE-VERSION 1)
  664. ;(define ADDR-none 0)
  665. ;;;
  666. ;;; A general configuration object.
  667. ;;;
  668. (define-record-type <dwarf-meta>
  669. (make-dwarf-meta addr-size
  670. vaddr memsz
  671. path lib-path
  672. info-start info-end
  673. abbrevs-start abbrevs-end
  674. strtab-start strtab-end
  675. loc-start loc-end
  676. line-start line-end
  677. pubnames-start pubnames-end
  678. aranges-start aranges-end)
  679. dwarf-meta?
  680. (addr-size meta-addr-size)
  681. (vaddr meta-vaddr)
  682. (memsz meta-memsz)
  683. (path meta-path)
  684. (lib-path meta-lib-path)
  685. (info-start meta-info-start)
  686. (info-end meta-info-end)
  687. (abbrevs-start meta-abbrevs-start)
  688. (abbrevs-end meta-abbrevs-end)
  689. (strtab-start meta-strtab-start)
  690. (strtab-end meta-strtab-end)
  691. (loc-start meta-loc-start)
  692. (loc-end meta-loc-end)
  693. (line-start meta-line-start)
  694. (line-end meta-line-end)
  695. (pubnames-start meta-pubnames-start)
  696. (pubnames-end meta-pubnames-end)
  697. (aranges-start meta-aranges-start)
  698. (aranges-end meta-aranges-end))
  699. ;; A context represents a namespace. The root context is the
  700. ;; compilation unit. DIE nodes of type class-type, structure-type, or
  701. ;; namespace may form child contexts.
  702. ;;
  703. (define-record-type <dwarf-context>
  704. (make-dwarf-context bv offset-size endianness meta
  705. abbrevs
  706. parent die start end children)
  707. dwarf-context?
  708. (bv ctx-bv)
  709. (offset-size ctx-offset-size)
  710. (endianness ctx-endianness)
  711. (meta ctx-meta)
  712. (abbrevs ctx-abbrevs)
  713. (parent ctx-parent)
  714. (die ctx-die)
  715. (start ctx-start)
  716. (end ctx-end)
  717. (children ctx-children set-children!))
  718. (set-record-type-printer! <dwarf-context>
  719. (lambda (x port)
  720. (format port "<dwarf-context ~a>"
  721. (number->string (object-address x) 16))))
  722. (define-inlinable (ctx-addr-size ctx)
  723. (meta-addr-size (ctx-meta ctx)))
  724. ;;;
  725. ;;; Procedures for reading DWARF data.
  726. ;;;
  727. (define (read-u8 ctx pos)
  728. (values (bytevector-u8-ref (ctx-bv ctx) pos)
  729. (1+ pos)))
  730. (define (read-s8 ctx pos)
  731. (values (bytevector-s8-ref (ctx-bv ctx) pos)
  732. (1+ pos)))
  733. (define (skip-8 ctx pos)
  734. (+ pos 1))
  735. (define (read-u16 ctx pos)
  736. (values (bytevector-u16-ref (ctx-bv ctx) pos (ctx-endianness ctx))
  737. (+ pos 2)))
  738. (define (skip-16 ctx pos)
  739. (+ pos 2))
  740. (define (read-u32 ctx pos)
  741. (values (bytevector-u32-ref (ctx-bv ctx) pos (ctx-endianness ctx))
  742. (+ pos 4)))
  743. (define (skip-32 ctx pos)
  744. (+ pos 4))
  745. (define (read-u64 ctx pos)
  746. (values (bytevector-u64-ref (ctx-bv ctx) pos (ctx-endianness ctx))
  747. (+ pos 8)))
  748. (define (skip-64 ctx pos)
  749. (+ pos 8))
  750. (define (read-addr ctx pos)
  751. (case (ctx-addr-size ctx)
  752. ((4) (read-u32 ctx pos))
  753. ((8) (read-u64 ctx pos))
  754. (else (error "unsupported word size" ctx))))
  755. (define (skip-addr ctx pos)
  756. (+ pos (ctx-addr-size ctx)))
  757. (define (%read-uleb128 bv pos)
  758. ;; Unrolled by one.
  759. (let ((b (bytevector-u8-ref bv pos)))
  760. (if (zero? (logand b #x80))
  761. (values b
  762. (1+ pos))
  763. (let lp ((n (logxor #x80 b)) (pos (1+ pos)) (shift 7))
  764. (let ((b (bytevector-u8-ref bv pos)))
  765. (if (zero? (logand b #x80))
  766. (values (logior (ash b shift) n)
  767. (1+ pos))
  768. (lp (logior (ash (logxor #x80 b) shift) n)
  769. (1+ pos)
  770. (+ shift 7))))))))
  771. (define (%read-sleb128 bv pos)
  772. (let lp ((n 0) (pos pos) (shift 0))
  773. (let ((b (bytevector-u8-ref bv pos)))
  774. (if (zero? (logand b #x80))
  775. (values (logior (ash b shift) n
  776. (if (zero? (logand #x40 b))
  777. 0
  778. (- (ash 1 (+ shift 7)))))
  779. (1+ pos))
  780. (lp (logior (ash (logxor #x80 b) shift) n)
  781. (1+ pos)
  782. (+ shift 7))))))
  783. (define (read-uleb128 ctx pos)
  784. (%read-uleb128 (ctx-bv ctx) pos))
  785. (define (read-sleb128 ctx pos)
  786. (%read-sleb128 (ctx-bv ctx) pos))
  787. (define (skip-leb128 ctx pos)
  788. (let ((bv (ctx-bv ctx)))
  789. (let lp ((pos pos))
  790. (let ((b (bytevector-u8-ref bv pos)))
  791. (if (zero? (logand b #x80))
  792. (1+ pos)
  793. (lp (1+ pos)))))))
  794. (define (read-initial-length ctx pos)
  795. (let ((len (bytevector-u32-ref (ctx-bv ctx) pos (ctx-endianness ctx))))
  796. (cond
  797. ((= len #xffffffff)
  798. (values (bytevector-u32-ref (ctx-bv ctx) (+ pos 4) (ctx-endianness ctx))
  799. (+ pos 12)
  800. 8))
  801. ((>= len #xfffffff0)
  802. (error "bad initial length value" len))
  803. (else
  804. (values len
  805. (+ pos 4)
  806. 4)))))
  807. (define* (read-offset ctx pos #:optional (offset-size (ctx-offset-size ctx)))
  808. (case offset-size
  809. ((4) (values (read-u32 ctx pos) (+ pos 4)))
  810. ((8) (values (read-u64 ctx pos) (+ pos 8)))
  811. (else (error "bad word size" offset-size))))
  812. (define* (skip-offset ctx pos #:optional (offset-size (ctx-offset-size ctx)))
  813. (+ pos offset-size))
  814. (define (read-block ctx pos len)
  815. (let ((bv (make-bytevector len)))
  816. (bytevector-copy! (ctx-bv ctx) pos bv 0 len)
  817. (values bv
  818. (+ pos len))))
  819. (define (read-string ctx pos)
  820. (let ((bv (ctx-bv ctx)))
  821. (let lp ((end pos))
  822. (if (zero? (bytevector-u8-ref bv end))
  823. (let ((out (make-bytevector (- end pos))))
  824. (bytevector-copy! bv pos out 0 (- end pos))
  825. (values (utf8->string out)
  826. (1+ end)))
  827. (lp (1+ end))))))
  828. (define (skip-string ctx pos)
  829. (let ((bv (ctx-bv ctx)))
  830. (let lp ((end pos))
  831. (if (zero? (bytevector-u8-ref bv end))
  832. (1+ end)
  833. (lp (1+ end))))))
  834. (define (read-string-seq ctx pos)
  835. (let ((bv (ctx-bv ctx)))
  836. (let lp ((pos pos) (strs '()))
  837. (if (zero? (bytevector-u8-ref bv pos))
  838. (values (list->vector (reverse strs)) (1+ pos))
  839. (let-values (((str pos) (read-string ctx pos)))
  840. (lp pos (cons str strs)))))))
  841. (define-record-type <abbrev>
  842. (make-abbrev code tag has-children? attrs forms)
  843. abbrev?
  844. (code abbrev-code)
  845. (tag abbrev-tag)
  846. (has-children? abbrev-has-children?)
  847. (attrs abbrev-attrs)
  848. (forms abbrev-forms))
  849. (define (read-abbrev ctx pos)
  850. (let*-values (((code pos) (read-uleb128 ctx pos))
  851. ((tag pos) (read-uleb128 ctx pos))
  852. ((children pos) (read-u8 ctx pos)))
  853. (let lp ((attrs '()) (forms '()) (pos pos))
  854. (let*-values (((attr pos) (read-uleb128 ctx pos))
  855. ((form pos) (read-uleb128 ctx pos)))
  856. (if (and (zero? attr) (zero? form))
  857. (values (make-abbrev code
  858. (tag-code->name tag)
  859. (eq? (children-code->name children) 'yes)
  860. (reverse attrs)
  861. (reverse forms))
  862. pos)
  863. (lp (cons (attribute-code->name attr) attrs)
  864. (cons (form-code->name form) forms)
  865. pos))))))
  866. (define* (read-abbrevs ctx pos
  867. #:optional (start (meta-abbrevs-start
  868. (ctx-meta ctx)))
  869. (end (meta-abbrevs-end
  870. (ctx-meta ctx))))
  871. (let lp ((abbrevs '()) (pos (+ start pos)) (max-code -1))
  872. (if (zero? (read-u8 ctx pos))
  873. (if (< pos end)
  874. (let ((av (make-vector (1+ max-code) #f)))
  875. (for-each (lambda (a)
  876. (vector-set! av (abbrev-code a) a))
  877. abbrevs)
  878. av)
  879. (error "Unexpected length" abbrevs pos start end))
  880. (let-values (((abbrev pos) (read-abbrev ctx pos)))
  881. (lp (cons abbrev abbrevs)
  882. pos
  883. (max (abbrev-code abbrev) max-code))))))
  884. (define (ctx-compile-unit-start ctx)
  885. (if (ctx-die ctx)
  886. (ctx-compile-unit-start (ctx-parent ctx))
  887. (ctx-start ctx)))
  888. ;; Values.
  889. ;;
  890. (define *readers* (make-hash-table))
  891. (define *scanners* (make-hash-table))
  892. (define-syntax define-value-reader
  893. (syntax-rules ()
  894. ((_ form reader scanner)
  895. (begin
  896. (hashq-set! *readers* 'form reader)
  897. (hashq-set! *scanners* 'form scanner)))))
  898. (define-value-reader addr read-addr skip-addr)
  899. (define-value-reader block
  900. (lambda (ctx pos)
  901. (let-values (((len pos) (read-uleb128 ctx pos)))
  902. (read-block ctx pos len)))
  903. (lambda (ctx pos)
  904. (let-values (((len pos) (read-uleb128 ctx pos)))
  905. (+ pos len))))
  906. (define-value-reader block1
  907. (lambda (ctx pos)
  908. (let-values (((len pos) (read-u8 ctx pos)))
  909. (read-block ctx pos len)))
  910. (lambda (ctx pos)
  911. (+ pos 1 (bytevector-u8-ref (ctx-bv ctx) pos))))
  912. (define-value-reader block2
  913. (lambda (ctx pos)
  914. (let-values (((len pos) (read-u16 ctx pos)))
  915. (read-block ctx pos len)))
  916. (lambda (ctx pos)
  917. (+ pos 2 (bytevector-u16-ref (ctx-bv ctx) pos (ctx-endianness ctx)))))
  918. (define-value-reader block4
  919. (lambda (ctx pos)
  920. (let-values (((len pos) (read-u32 ctx pos)))
  921. (read-block ctx pos len)))
  922. (lambda (ctx pos)
  923. (+ pos 4 (bytevector-u32-ref (ctx-bv ctx) pos (ctx-endianness ctx)))))
  924. (define-value-reader data1 read-u8 skip-8)
  925. (define-value-reader data2 read-u16 skip-16)
  926. (define-value-reader data4 read-u32 skip-32)
  927. (define-value-reader data8 read-u64 skip-64)
  928. (define-value-reader udata read-uleb128 skip-leb128)
  929. (define-value-reader sdata read-sleb128 skip-leb128)
  930. (define-value-reader flag
  931. (lambda (ctx pos)
  932. (values (not (zero? (bytevector-u8-ref (ctx-bv ctx) pos)))
  933. (1+ pos)))
  934. skip-8)
  935. (define-value-reader string
  936. read-string
  937. skip-string)
  938. (define-value-reader strp
  939. (lambda (ctx pos)
  940. (let ((strtab (meta-strtab-start (ctx-meta ctx))))
  941. (unless strtab
  942. (error "expected a string table" ctx))
  943. (let-values (((offset pos) (read-offset ctx pos)))
  944. (values (read-string ctx (+ strtab offset))
  945. pos))))
  946. skip-32)
  947. (define-value-reader ref-addr
  948. (lambda (ctx pos)
  949. (let-values (((addr pos) (read-addr ctx pos)))
  950. (values (+ addr (meta-info-start (ctx-meta ctx)))
  951. pos)))
  952. skip-addr)
  953. (define-value-reader ref1
  954. (lambda (ctx pos)
  955. (let-values (((addr pos) (read-u8 ctx pos)))
  956. (values (+ addr (ctx-compile-unit-start ctx))
  957. pos)))
  958. skip-8)
  959. (define-value-reader ref2
  960. (lambda (ctx pos)
  961. (let-values (((addr pos) (read-u16 ctx pos)))
  962. (values (+ addr (ctx-compile-unit-start ctx))
  963. pos)))
  964. skip-16)
  965. (define-value-reader ref4
  966. (lambda (ctx pos)
  967. (let-values (((addr pos) (read-u32 ctx pos)))
  968. (values (+ addr (ctx-compile-unit-start ctx))
  969. pos)))
  970. skip-32)
  971. (define-value-reader ref8
  972. (lambda (ctx pos)
  973. (let-values (((addr pos) (read-u64 ctx pos)))
  974. (values (+ addr (ctx-compile-unit-start ctx))
  975. pos)))
  976. skip-64)
  977. (define-value-reader ref
  978. (lambda (udata ctx pos)
  979. (let-values (((addr pos) (read-uleb128 ctx pos)))
  980. (values (+ addr (ctx-compile-unit-start ctx))
  981. pos)))
  982. skip-leb128)
  983. (define-value-reader indirect
  984. (lambda (ctx pos)
  985. (let*-values (((form pos) (read-uleb128 ctx pos))
  986. ((val pos) (read-value ctx pos (form-code->name form))))
  987. (values (cons form val)
  988. pos)))
  989. (lambda (ctx pos)
  990. (let*-values (((form pos) (read-uleb128 ctx pos)))
  991. (skip-value ctx pos (form-code->name form)))))
  992. (define-value-reader sec-offset
  993. read-offset
  994. skip-offset)
  995. (define-value-reader exprloc
  996. (lambda (ctx pos)
  997. (let-values (((len pos) (read-uleb128 ctx pos)))
  998. (read-block ctx pos len)))
  999. (lambda (ctx pos)
  1000. (let-values (((len pos) (read-uleb128 ctx pos)))
  1001. (+ pos len))))
  1002. (define-value-reader flag-present
  1003. (lambda (ctx pos)
  1004. (values #t pos))
  1005. (lambda (ctx pos)
  1006. pos))
  1007. (define-value-reader ref-sig8
  1008. read-u64
  1009. skip-64)
  1010. (define (read-value ctx pos form)
  1011. ((or (hashq-ref *readers* form)
  1012. (error "unrecognized form" form))
  1013. ctx pos))
  1014. (define (skip-value ctx pos form)
  1015. ((or (hashq-ref *scanners* form)
  1016. (error "unrecognized form" form))
  1017. ctx pos))
  1018. ;; Parsers for particular attributes.
  1019. ;;
  1020. (define (parse-location-list ctx offset)
  1021. (let lp ((pos (+ (meta-loc-start (ctx-meta ctx)) offset))
  1022. (out '()))
  1023. (let*-values (((start pos) (read-addr ctx pos))
  1024. ((end pos) (read-addr ctx pos)))
  1025. (if (and (zero? start) (zero? end))
  1026. (reverse out)
  1027. (let*-values (((len pos) (read-u16 ctx pos))
  1028. ((block pos) (read-block ctx pos len)))
  1029. (lp pos
  1030. (cons (list start end (parse-location ctx block)) out)))))))
  1031. (define (parse-location ctx loc)
  1032. (cond
  1033. ((bytevector? loc)
  1034. (let ((len (bytevector-length loc))
  1035. (addr-size (ctx-addr-size ctx))
  1036. (endianness (ctx-endianness ctx)))
  1037. (define (u8-ref pos) (bytevector-u8-ref loc pos))
  1038. (define (s8-ref pos) (bytevector-s8-ref loc pos))
  1039. (define (u16-ref pos) (bytevector-u16-ref loc pos endianness))
  1040. (define (s16-ref pos) (bytevector-s16-ref loc pos endianness))
  1041. (define (u32-ref pos) (bytevector-u32-ref loc pos endianness))
  1042. (define (s32-ref pos) (bytevector-s32-ref loc pos endianness))
  1043. (define (u64-ref pos) (bytevector-u64-ref loc pos endianness))
  1044. (define (s64-ref pos) (bytevector-s64-ref loc pos endianness))
  1045. (let lp ((pos 0) (out '()))
  1046. (if (= pos len)
  1047. (reverse out)
  1048. (let ((op (location-op->name (u8-ref pos))))
  1049. (case op
  1050. ((addr)
  1051. (case addr-size
  1052. ((4) (lp (+ pos 5) (cons (list op (u32-ref (1+ pos))) out)))
  1053. ((8) (lp (+ pos 9) (cons (list op (u64-ref (1+ pos))) out)))
  1054. (else (error "what!"))))
  1055. ((call-ref)
  1056. (case addr-size
  1057. ((4) (lp (+ pos 5)
  1058. (cons (list op (+ (meta-info-start (ctx-meta ctx))
  1059. (u32-ref (1+ pos))))
  1060. out)))
  1061. ((8) (lp (+ pos 9)
  1062. (cons (list op (+ (meta-info-start (ctx-meta ctx))
  1063. (u64-ref (1+ pos))))
  1064. out)))
  1065. (else (error "what!"))))
  1066. ((const1u pick deref-size xderef-size)
  1067. (lp (+ pos 2) (cons (list op (u8-ref (1+ pos))) out)))
  1068. ((const1s)
  1069. (lp (+ pos 2) (cons (list op (s8-ref (1+ pos))) out)))
  1070. ((const2u)
  1071. (lp (+ pos 3) (cons (list op (u16-ref (1+ pos))) out)))
  1072. ((call2)
  1073. (lp (+ pos 3) (cons (list op (+ (ctx-compile-unit-start ctx)
  1074. (u16-ref (1+ pos))))
  1075. out)))
  1076. ((const2s skip bra)
  1077. (lp (+ pos 3) (cons (list op (s16-ref (1+ pos))) out)))
  1078. ((const4u)
  1079. (lp (+ pos 5) (cons (list op (u32-ref (1+ pos))) out)))
  1080. ((call4)
  1081. (lp (+ pos 5) (cons (list op (+ (ctx-compile-unit-start ctx)
  1082. (u32-ref (1+ pos))))
  1083. out)))
  1084. ((const4s)
  1085. (lp (+ pos 5) (cons (list op (s32-ref (1+ pos))) out)))
  1086. ((const8u)
  1087. (lp (+ pos 9) (cons (list op (u64-ref (1+ pos))) out)))
  1088. ((const8s)
  1089. (lp (+ pos 9) (cons (list op (s64-ref (1+ pos))) out)))
  1090. ((plus-uconst regx piece)
  1091. (let-values (((val pos) (%read-uleb128 loc (1+ pos))))
  1092. (lp pos (cons (list op val) out))))
  1093. ((bit-piece)
  1094. (let*-values (((bit-len pos) (%read-uleb128 loc (1+ pos)))
  1095. ((bit-offset pos) (%read-uleb128 loc pos)))
  1096. (lp pos (cons (list op bit-len bit-offset) out))))
  1097. ((breg0 breg1 breg2 breg3 breg4 breg5 breg6 breg7 breg8 breg9
  1098. breg10 breg11 breg12 breg13 breg14 breg15 breg16 breg17
  1099. breg18 breg19 breg20 breg21 breg22 breg23 breg24 breg25
  1100. breg26 breg27 breg28 breg29 breg30 breg31 fbreg)
  1101. (let-values (((val pos) (%read-sleb128 loc (1+ pos))))
  1102. (lp pos (cons (list op val) out))))
  1103. (else
  1104. (if (number? op)
  1105. ;; We failed to parse this opcode; we have to give
  1106. ;; up
  1107. loc
  1108. (lp (1+ pos) (cons (list op) out))))))))))
  1109. (else
  1110. (parse-location-list ctx loc))))
  1111. ;; Statement programs.
  1112. (define-record-type <lregs>
  1113. (make-lregs pos pc file line column)
  1114. lregs?
  1115. (pos lregs-pos set-lregs-pos!)
  1116. (pc lregs-pc set-lregs-pc!)
  1117. (file lregs-file set-lregs-file!)
  1118. (line lregs-line set-lregs-line!)
  1119. (column lregs-column set-lregs-column!))
  1120. (define-record-type <line-prog>
  1121. (%make-line-prog ctx version
  1122. header-offset program-offset end
  1123. min-insn-length max-insn-ops default-stmt?
  1124. line-base line-range opcode-base
  1125. standard-opcode-lengths
  1126. include-directories file-names
  1127. regs)
  1128. line-prog?
  1129. (ctx line-prog-ctx)
  1130. (version line-prog-version)
  1131. (header-offset line-prog-header-offset)
  1132. (program-offset line-prog-program-offset)
  1133. (end line-prog-end)
  1134. (min-insn-length line-prog-min-insn-length)
  1135. (max-insn-ops line-prog-max-insn-ops)
  1136. (default-stmt? line-prog-default-stmt?)
  1137. (line-base line-prog-line-base)
  1138. (line-range line-prog-line-range)
  1139. (opcode-base line-prog-opcode-base)
  1140. (standard-opcode-lengths line-prog-standard-opcode-lengths)
  1141. (include-directories line-prog-include-directories)
  1142. (file-names line-prog-file-names)
  1143. (regs line-prog-regs))
  1144. (define (make-line-prog ctx header-pos end)
  1145. (unless (> end (+ header-pos 12))
  1146. (error "statement program header too short"))
  1147. (let-values (((len pos offset-size) (read-initial-length ctx header-pos)))
  1148. (unless (<= (+ pos len) end)
  1149. (error (".debug_line too short")))
  1150. (let*-values (((version pos) (read-u16 ctx pos))
  1151. ((prologue-len prologue-pos) (read-u32 ctx pos))
  1152. ((min-insn-len pos) (read-u8 ctx prologue-pos))
  1153. ;; The maximum_operations_per_instruction field is
  1154. ;; only present in DWARFv4.
  1155. ((max-insn-ops pos) (if (< version 4)
  1156. (values 1 pos)
  1157. (read-u8 ctx pos)))
  1158. ((default-stmt pos) (read-u8 ctx pos))
  1159. ((line-base pos) (read-s8 ctx pos))
  1160. ((line-range pos) (read-u8 ctx pos))
  1161. ((opcode-base pos) (read-u8 ctx pos))
  1162. ((opcode-lens pos) (read-block ctx pos (1- opcode-base)))
  1163. ((include-directories pos) (read-string-seq ctx pos))
  1164. ((file-names pos)
  1165. (let lp ((pos pos) (strs '()))
  1166. (if (zero? (bytevector-u8-ref (ctx-bv ctx) pos))
  1167. (values (reverse strs) (1+ pos))
  1168. (let-values (((str pos) (read-string ctx pos)))
  1169. (let* ((pos (skip-leb128 ctx pos)) ; skip dir
  1170. (pos (skip-leb128 ctx pos)) ; skip mtime
  1171. (pos (skip-leb128 ctx pos))) ; skip len
  1172. (lp pos (cons str strs))))))))
  1173. (unless (= pos (+ prologue-pos prologue-len))
  1174. (error "unexpected prologue length"))
  1175. (%make-line-prog ctx version header-pos pos end
  1176. min-insn-len max-insn-ops (not (zero? default-stmt))
  1177. line-base line-range opcode-base opcode-lens
  1178. include-directories file-names
  1179. ;; Initial state: file=1, line=1, col=0
  1180. (make-lregs pos 0 1 1 0)))))
  1181. (define (line-prog-next-row prog pos pc file line col)
  1182. (let ((ctx (line-prog-ctx prog))
  1183. (end (line-prog-end prog))
  1184. (min-insn-len (line-prog-min-insn-length prog))
  1185. (line-base (line-prog-line-base prog))
  1186. (line-range (line-prog-line-range prog))
  1187. (opcode-base (line-prog-opcode-base prog))
  1188. (opcode-lens (line-prog-standard-opcode-lengths prog)))
  1189. (let lp ((pos pos) (pc pc) (file file) (line line) (col col))
  1190. (cond
  1191. ((>= pos end)
  1192. (values #f #f #f #f #f))
  1193. (else
  1194. (let-values (((op pos) (read-u8 ctx pos)))
  1195. (cond
  1196. ((zero? op) ; extended opcodes
  1197. (let*-values (((len pos*) (read-uleb128 ctx pos))
  1198. ((op pos) (read-u8 ctx pos*)))
  1199. (case op
  1200. ((1) ; end-sequence
  1201. (values pos pc file line col))
  1202. ((2) ; set-address
  1203. (let-values (((addr pos) (read-addr ctx pos)))
  1204. (unless (>= addr pc)
  1205. (error "pc not advancing"))
  1206. (lp pos addr file line col)))
  1207. ((3) ; define-file
  1208. (warn "define-file unimplemented")
  1209. (lp (+ pos* len) pc file line col))
  1210. ((4) ; set-discriminator; ignore.
  1211. (lp (+ pos* len) pc file line col))
  1212. (else
  1213. (warn "unknown extended op" op)
  1214. (lp (+ pos* len) pc file line col)))))
  1215. ((< op opcode-base) ; standard opcodes
  1216. (case op
  1217. ((1) ; copy
  1218. (values pos pc file line col))
  1219. ((2) ; advance-pc
  1220. (let-values (((advance pos) (read-uleb128 ctx pos)))
  1221. (lp pos (+ pc (* advance min-insn-len)) file line col)))
  1222. ((3) ; advance-line
  1223. (let-values (((diff pos) (read-sleb128 ctx pos)))
  1224. (lp pos pc file (+ line diff) col)))
  1225. ((4) ; set-file
  1226. (let-values (((file pos) (read-uleb128 ctx pos)))
  1227. (lp pos pc file line col)))
  1228. ((5) ; set-column
  1229. (let-values (((col pos) (read-uleb128 ctx pos)))
  1230. (lp pos pc file line col)))
  1231. ((6) ; negate-line
  1232. (lp pos pc file line col))
  1233. ((7) ; set-basic-block
  1234. (lp pos pc file line col))
  1235. ((8) ; const-add-pc
  1236. (let ((advance (floor/ (- 255 opcode-base) line-range)))
  1237. (lp pos (+ pc (* advance min-insn-len)) file line col)))
  1238. ((9) ; fixed-advance-pc
  1239. (let-values (((advance pos) (read-u16 ctx pos)))
  1240. (lp pos (+ pc (* advance min-insn-len)) file line col)))
  1241. (else
  1242. ;; fixme: read args and move on
  1243. (error "unknown extended op" op))))
  1244. (else ; special opcodes
  1245. (let-values (((quo rem) (floor/ (- op opcode-base) line-range)))
  1246. (values pos (+ pc (* quo min-insn-len))
  1247. file (+ line (+ rem line-base)) col))))))))))
  1248. (define (line-prog-advance prog)
  1249. (let ((regs (line-prog-regs prog)))
  1250. (call-with-values (lambda ()
  1251. (line-prog-next-row prog
  1252. (lregs-pos regs)
  1253. (lregs-pc regs)
  1254. (lregs-file regs)
  1255. (lregs-line regs)
  1256. (lregs-column regs)))
  1257. (lambda (pos pc file line col)
  1258. (cond
  1259. ((not pos)
  1260. (values #f #f #f #f))
  1261. (else
  1262. (set-lregs-pos! regs pos)
  1263. (set-lregs-pc! regs pc)
  1264. (set-lregs-file! regs file)
  1265. (set-lregs-line! regs line)
  1266. (set-lregs-column! regs col)
  1267. ;; Return DWARF-numbered lines and columns (1-based).
  1268. (values pc
  1269. (if (zero? file)
  1270. #f
  1271. (list-ref (line-prog-file-names prog) (1- file)))
  1272. (if (zero? line) #f line)
  1273. (if (zero? col) #f col))))))))
  1274. (define (line-prog-scan-to-pc prog target-pc)
  1275. (let ((regs (line-prog-regs prog)))
  1276. (define (finish pos pc file line col)
  1277. (set-lregs-pos! regs pos)
  1278. (set-lregs-pc! regs pc)
  1279. (set-lregs-file! regs file)
  1280. (set-lregs-line! regs line)
  1281. (set-lregs-column! regs col)
  1282. ;; Return DWARF-numbered lines and columns (1-based).
  1283. (values pc
  1284. (if (zero? file)
  1285. #f
  1286. (list-ref (line-prog-file-names prog) (1- file)))
  1287. (if (zero? line) #f line)
  1288. (if (zero? col) #f col)))
  1289. (define (scan pos pc file line col)
  1290. (call-with-values (lambda ()
  1291. (line-prog-next-row prog pos pc file line col))
  1292. (lambda (pos* pc* file* line* col*)
  1293. (cond
  1294. ((not pos*)
  1295. (values #f #f #f #f))
  1296. ((< pc* target-pc)
  1297. (scan pos* pc* file* line* col*))
  1298. ((= pc* target-pc)
  1299. (finish pos* pc* file* line* col*))
  1300. ((zero? pc)
  1301. ;; We scanned from the beginning didn't find any info.
  1302. (values #f #f #f #f))
  1303. (else
  1304. (finish pos pc file line col))))))
  1305. (let ((pos (lregs-pos regs))
  1306. (pc (lregs-pc regs))
  1307. (file (lregs-file regs))
  1308. (line (lregs-line regs))
  1309. (col (lregs-column regs)))
  1310. (if (< pc target-pc)
  1311. (scan pos pc file line col)
  1312. (scan (line-prog-program-offset prog) 0 1 1 0)))))
  1313. (define-syntax-rule (define-attribute-parsers parse (name parser) ...)
  1314. (define parse
  1315. (let ((parsers (make-hash-table)))
  1316. (hashq-set! parsers 'name parser)
  1317. ...
  1318. (lambda (ctx attr val)
  1319. (cond
  1320. ((hashq-ref parsers attr) => (lambda (p) (p ctx val)))
  1321. (else val))))))
  1322. (define-attribute-parsers parse-attribute
  1323. (encoding (lambda (ctx val) (type-encoding->name val)))
  1324. (accessibility (lambda (ctx val) (access-code->name val)))
  1325. (visibility (lambda (ctx val) (visibility-code->name val)))
  1326. (virtuality (lambda (ctx val) (virtuality-code->name val)))
  1327. (language (lambda (ctx val) (language-code->name val)))
  1328. (location parse-location)
  1329. (data-member-location parse-location)
  1330. (case-sensitive (lambda (ctx val) (case-sensitivity-code->name val)))
  1331. (calling-convention (lambda (ctx val) (calling-convention-code->name val)))
  1332. (inline (lambda (ctx val) (inline-code->name val)))
  1333. (ordering (lambda (ctx val) (ordering-code->name val)))
  1334. (discr-value (lambda (ctx val) (discriminant-code->name val))))
  1335. ;; "Debugging Information Entries": DIEs.
  1336. ;;
  1337. (define-record-type <die>
  1338. (make-die ctx offset abbrev vals)
  1339. die?
  1340. (ctx die-ctx)
  1341. (offset die-offset)
  1342. (abbrev die-abbrev)
  1343. (vals %die-vals %set-die-vals!))
  1344. (define (die-tag die)
  1345. (abbrev-tag (die-abbrev die)))
  1346. (define (die-attrs die)
  1347. (abbrev-attrs (die-abbrev die)))
  1348. (define (die-forms die)
  1349. (abbrev-forms (die-abbrev die)))
  1350. (define (die-vals die)
  1351. (let ((vals (%die-vals die)))
  1352. (or vals
  1353. (begin
  1354. (%set-die-vals! die (read-values (die-ctx die) (skip-leb128 (die-ctx die) (die-offset die)) (die-abbrev die)))
  1355. (die-vals die)))))
  1356. (define* (die-next-offset die #:optional offset-vals)
  1357. (let ((ctx (die-ctx die)))
  1358. (skip-values ctx (or offset-vals (skip-leb128 ctx (die-offset die)))
  1359. (die-abbrev die))))
  1360. (define* (die-ref die attr #:optional default)
  1361. (cond
  1362. ((list-index (die-attrs die) attr)
  1363. => (lambda (n) (list-ref (die-vals die) n)))
  1364. (else default)))
  1365. (define (die-specification die)
  1366. (and=> (die-ref die 'specification)
  1367. (lambda (offset) (find-die-by-offset (die-ctx die) offset))))
  1368. (define (die-name die)
  1369. (or (die-ref die 'name)
  1370. (and=> (die-specification die) die-name)))
  1371. (define (die-qname die)
  1372. (cond
  1373. ((eq? (die-tag die) 'compile-unit) "")
  1374. ((die-ref die 'name)
  1375. => (lambda (name)
  1376. (if (eq? (die-tag (ctx-die (die-ctx die))) 'compile-unit)
  1377. name ; short cut
  1378. (string-append (die-qname (ctx-die (die-ctx die))) "::" name))))
  1379. ((die-specification die)
  1380. => die-qname)
  1381. (else #f)))
  1382. (define (die-line-prog die)
  1383. (let ((stmt-list (die-ref die 'stmt-list)))
  1384. (and stmt-list
  1385. (let* ((ctx (die-ctx die))
  1386. (meta (ctx-meta ctx)))
  1387. (make-line-prog ctx
  1388. (+ (meta-line-start meta) stmt-list)
  1389. (meta-line-end meta))))))
  1390. (define (read-values ctx offset abbrev)
  1391. (let lp ((attrs (abbrev-attrs abbrev))
  1392. (forms (abbrev-forms abbrev))
  1393. (vals '())
  1394. (pos offset))
  1395. (if (null? forms)
  1396. (values (reverse vals) pos)
  1397. (let-values (((val pos) (read-value ctx pos (car forms))))
  1398. (lp (cdr attrs) (cdr forms)
  1399. (cons (parse-attribute ctx (car attrs) val) vals)
  1400. pos)))))
  1401. (define (skip-values ctx offset abbrev)
  1402. (let lp ((forms (abbrev-forms abbrev))
  1403. (pos offset))
  1404. (if (null? forms)
  1405. pos
  1406. (lp (cdr forms) (skip-value ctx pos (car forms))))))
  1407. (define (read-die-abbrev ctx offset)
  1408. (let*-values (((code pos) (read-uleb128 ctx offset)))
  1409. (values (cond ((zero? code) #f)
  1410. ((vector-ref (ctx-abbrevs ctx) code))
  1411. (else (error "unknown abbrev" ctx code)))
  1412. pos)))
  1413. (define (read-die ctx offset)
  1414. (let*-values (((abbrev pos) (read-die-abbrev ctx offset)))
  1415. (if abbrev
  1416. (values (make-die ctx offset abbrev #f)
  1417. (skip-values ctx pos abbrev))
  1418. (values #f pos))))
  1419. (define* (die-sibling ctx abbrev offset #:optional offset-vals offset-end)
  1420. (cond
  1421. ((not (abbrev-has-children? abbrev))
  1422. (or offset-end
  1423. (skip-values ctx
  1424. (or offset-vals (skip-leb128 ctx offset))
  1425. abbrev)))
  1426. ((memq 'sibling (abbrev-attrs abbrev))
  1427. (let lp ((offset (or offset-vals (skip-leb128 ctx offset)))
  1428. (attrs (abbrev-attrs abbrev))
  1429. (forms (abbrev-forms abbrev)))
  1430. (if (eq? (car attrs) 'sibling)
  1431. (read-value ctx offset (car forms))
  1432. (lp (skip-value ctx offset (car forms))
  1433. (cdr attrs) (cdr forms)))))
  1434. (else
  1435. (call-with-values
  1436. (lambda ()
  1437. (fold-die-list ctx
  1438. (or offset-end
  1439. (skip-values ctx
  1440. (or offset-vals
  1441. (skip-leb128 ctx offset))
  1442. abbrev))
  1443. (lambda (ctx offset abbrev) #t)
  1444. error
  1445. #f))
  1446. (lambda (seed pos)
  1447. pos)))))
  1448. (define (find-die-context ctx offset)
  1449. (define (not-found)
  1450. (error "failed to find DIE by context" offset))
  1451. (define (in-context? ctx)
  1452. (and (<= (ctx-start ctx) offset)
  1453. (< offset (ctx-end ctx))))
  1454. (define (find-root ctx)
  1455. (if (in-context? ctx)
  1456. ctx
  1457. (find-root (or (ctx-parent ctx) (not-found)))))
  1458. (define (find-leaf ctx)
  1459. (let lp ((kids (ctx-children ctx)))
  1460. (if (null? kids)
  1461. ctx
  1462. (if (in-context? (car kids))
  1463. (find-leaf (car kids))
  1464. (lp (cdr kids))))))
  1465. (find-leaf (find-root ctx)))
  1466. (define (find-die-by-offset ctx offset)
  1467. (or (read-die (find-die-context ctx offset) offset)
  1468. (error "Failed to read DIE at offset" offset)))
  1469. (define-syntax-rule (let/ec k e e* ...)
  1470. (let ((tag (make-prompt-tag)))
  1471. (call-with-prompt
  1472. tag
  1473. (lambda ()
  1474. (let ((k (lambda args (apply abort-to-prompt tag args))))
  1475. e e* ...))
  1476. (lambda (_ res) res))))
  1477. (define* (find-die roots pred #:key
  1478. (skip? (lambda (ctx offset abbrev) #f))
  1479. (recurse? (lambda (die) #t)))
  1480. (let/ec k
  1481. (define (visit-die die)
  1482. (cond
  1483. ((pred die)
  1484. (k die))
  1485. ((recurse? die)
  1486. (fold-die-children die (lambda (die seed) (visit-die die)) #f
  1487. #:skip? skip?))
  1488. (else #f)))
  1489. (for-each visit-die roots)
  1490. #f))
  1491. (define (die-low-pc die)
  1492. (die-ref die 'low-pc))
  1493. (define (die-high-pc die)
  1494. (let ((val (die-ref die 'high-pc)))
  1495. (and val
  1496. (let ((idx (list-index (die-attrs die) 'high-pc)))
  1497. (case (list-ref (die-forms die) idx)
  1498. ((addr) val)
  1499. (else (+ val (die-low-pc die))))))))
  1500. (define (find-die-by-pc roots pc)
  1501. ;; The result will be a subprogram.
  1502. (define (skip? ctx offset abbrev)
  1503. (case (abbrev-tag abbrev)
  1504. ((subprogram compile-unit) #f)
  1505. (else #t)))
  1506. (define (recurse? die)
  1507. (case (die-tag die)
  1508. ((compile-unit)
  1509. (not (or (and=> (die-low-pc die)
  1510. (lambda (low) (< pc low)))
  1511. (and=> (die-high-pc die)
  1512. (lambda (high) (<= high pc))))))
  1513. (else #f)))
  1514. (find-die roots
  1515. (lambda (die)
  1516. (and (eq? (die-tag die) 'subprogram)
  1517. (equal? (die-low-pc die) pc)))
  1518. #:skip? skip? #:recurse? recurse?))
  1519. (define (fold-die-list ctx offset skip? proc seed)
  1520. (let ((ctx (find-die-context ctx offset)))
  1521. (let lp ((offset offset) (seed seed))
  1522. (let-values (((abbrev pos) (read-die-abbrev ctx offset)))
  1523. (cond
  1524. ((not abbrev) (values seed pos))
  1525. ((skip? ctx offset abbrev)
  1526. (lp (die-sibling ctx abbrev offset pos) seed))
  1527. (else
  1528. (let-values (((vals pos) (read-values ctx pos abbrev)))
  1529. (let* ((die (make-die ctx offset abbrev vals))
  1530. (seed (proc die seed)))
  1531. (lp (die-sibling ctx abbrev offset #f pos) seed)))))))))
  1532. (define* (fold-die-children die proc seed #:key
  1533. (skip? (lambda (ctx offset abbrev) #f)))
  1534. (if (abbrev-has-children? (die-abbrev die))
  1535. (values (fold-die-list (die-ctx die) (die-next-offset die)
  1536. skip? proc seed))
  1537. seed))
  1538. (define (die-children die)
  1539. (reverse (fold-die-children die cons '())))
  1540. (define (add-to-parent! ctx)
  1541. (let ((parent (ctx-parent ctx)))
  1542. (set-children! parent
  1543. (append (ctx-children parent) (list ctx)))
  1544. ctx))
  1545. (define (make-compilation-unit-context ctx offset-size addr-size
  1546. abbrevs start len)
  1547. (unless (= addr-size (ctx-addr-size ctx))
  1548. (error "ELF word size not equal to compilation unit addrsize"))
  1549. (add-to-parent!
  1550. (make-dwarf-context (ctx-bv ctx)
  1551. offset-size (ctx-endianness ctx)
  1552. (ctx-meta ctx)
  1553. abbrevs ctx #f start (+ start 4 len) '())))
  1554. (define (make-child-context die)
  1555. (let ((ctx (die-ctx die)))
  1556. (add-to-parent!
  1557. (make-dwarf-context (ctx-bv ctx)
  1558. (ctx-offset-size ctx) (ctx-endianness ctx)
  1559. (ctx-meta ctx)
  1560. (ctx-abbrevs ctx)
  1561. ctx die
  1562. (die-next-offset die)
  1563. (die-sibling ctx (die-abbrev die) (die-offset die))
  1564. '()))))
  1565. (define (ctx-language ctx)
  1566. (or (and=> (ctx-die ctx) (lambda (x) (die-ref x 'language)))
  1567. (and=> (ctx-parent ctx) ctx-language)))
  1568. (define (populate-context-tree! die)
  1569. (define (skip? ctx offset abbrev)
  1570. (case (abbrev-tag abbrev)
  1571. ((class-type structure-type namespace) #f)
  1572. (else #t)))
  1573. (case (die-tag die)
  1574. ((compile-unit class-type structure-type namespace)
  1575. (let ((ctx (make-child-context die)))
  1576. ;; For C++, descend into classes and structures so that we
  1577. ;; populate the context tree. Note that for compile-unit, we
  1578. ;; still need to call `make-child-context' for its side effect of
  1579. ;; adding to the context tree.
  1580. (when (eq? (ctx-language ctx) 'c++)
  1581. (fold-die-children die
  1582. (lambda (die seed) (populate-context-tree! die))
  1583. #f
  1584. #:skip? skip?))))))
  1585. (define (read-compilation-unit ctx pos)
  1586. (let*-values (((start) pos)
  1587. ((len pos offset-size) (read-initial-length ctx pos))
  1588. ((version pos) (read-u16 ctx pos))
  1589. ((abbrevs-offset pos) (read-offset ctx pos offset-size))
  1590. ((av) (read-abbrevs ctx abbrevs-offset))
  1591. ((addrsize pos) (read-u8 ctx pos))
  1592. ((ctx) (make-compilation-unit-context ctx offset-size addrsize
  1593. av start len))
  1594. ((die pos) (read-die ctx pos)))
  1595. (populate-context-tree! die)
  1596. (values die (ctx-end ctx))))
  1597. (define (read-die-roots ctx)
  1598. (let lp ((dies '()) (pos (meta-info-start (ctx-meta ctx))))
  1599. (if (< pos (meta-info-end (ctx-meta ctx)))
  1600. (let-values (((die pos) (read-compilation-unit ctx pos)))
  1601. (if die
  1602. (lp (cons die dies) pos)
  1603. (reverse dies)))
  1604. (reverse dies))))
  1605. (define (fold-pubname-set ctx pos folder seed)
  1606. (let*-values (((len pos offset-size) (read-initial-length ctx pos))
  1607. ((version pos) (read-u16 ctx pos))
  1608. ((info-offset pos) (read-offset ctx pos offset-size))
  1609. ((info-offset) (+ info-offset
  1610. (meta-info-start (ctx-meta ctx))))
  1611. ((info-len pos) (read-offset ctx pos offset-size)))
  1612. (let lp ((pos pos) (seed seed))
  1613. (let-values (((offset pos) (read-offset ctx pos offset-size)))
  1614. (if (zero? offset)
  1615. (values seed pos)
  1616. (let-values (((str pos) (read-string ctx pos)))
  1617. (lp pos
  1618. (folder str (+ offset info-offset) seed))))))))
  1619. (define (fold-pubnames ctx folder seed)
  1620. (let ((end (meta-pubnames-end (ctx-meta ctx))))
  1621. (if end
  1622. (let lp ((pos (meta-pubnames-start (ctx-meta ctx))) (seed seed))
  1623. (if (< pos end)
  1624. (let-values (((seed pos) (fold-pubname-set ctx pos folder seed)))
  1625. (lp pos seed))
  1626. seed))
  1627. seed)))
  1628. (define (align address alignment)
  1629. (+ address
  1630. (modulo (- alignment (modulo address alignment)) alignment)))
  1631. (define (fold-arange-set ctx pos folder seed)
  1632. (let*-values (((len pos offset-size) (read-initial-length ctx pos))
  1633. ((version pos) (read-u16 ctx pos))
  1634. ((info-offset pos) (read-offset ctx pos offset-size))
  1635. ((info-offset) (+ info-offset
  1636. (meta-info-start (ctx-meta ctx))))
  1637. ((addr-size pos) (read-u8 ctx pos))
  1638. ((segment-size pos) (read-u8 ctx pos)))
  1639. (let lp ((pos (align pos (* 2 (ctx-addr-size ctx)))) (seed seed))
  1640. (let*-values (((addr pos) (read-addr ctx pos))
  1641. ((len pos) (read-addr ctx pos)))
  1642. (if (and (zero? addr) (zero? len))
  1643. (values seed pos)
  1644. (lp pos
  1645. (folder info-offset addr len seed)))))))
  1646. (define (fold-aranges ctx folder seed)
  1647. (let ((end (meta-aranges-end (ctx-meta ctx))))
  1648. (if end
  1649. (let lp ((pos (meta-aranges-start (ctx-meta ctx))) (seed seed))
  1650. (if (< pos end)
  1651. (let-values (((seed pos) (fold-arange-set ctx pos folder seed)))
  1652. (lp pos seed))
  1653. seed))
  1654. seed)))
  1655. (define* (elf->dwarf-context elf #:key (vaddr 0) (memsz 0)
  1656. (path #f) (lib-path path))
  1657. (let* ((sections (elf-sections-by-name elf))
  1658. (info (assoc-ref sections ".debug_info"))
  1659. (abbrevs (assoc-ref sections ".debug_abbrev"))
  1660. (strtab (assoc-ref sections ".debug_str"))
  1661. (loc (assoc-ref sections ".debug_loc"))
  1662. (line (assoc-ref sections ".debug_line"))
  1663. (pubnames (assoc-ref sections ".debug_pubnames"))
  1664. (aranges (assoc-ref sections ".debug_aranges")))
  1665. (make-dwarf-context (elf-bytes elf)
  1666. 4 ;; initial offset size
  1667. (elf-byte-order elf)
  1668. (make-dwarf-meta
  1669. (elf-word-size elf)
  1670. vaddr memsz
  1671. path lib-path
  1672. (elf-section-offset info)
  1673. (+ (elf-section-offset info)
  1674. (elf-section-size info))
  1675. (elf-section-offset abbrevs)
  1676. (+ (elf-section-offset abbrevs)
  1677. (elf-section-size abbrevs))
  1678. (elf-section-offset strtab)
  1679. (+ (elf-section-offset strtab)
  1680. (elf-section-size strtab))
  1681. (elf-section-offset loc)
  1682. (+ (elf-section-offset loc)
  1683. (elf-section-size loc))
  1684. (and line
  1685. (elf-section-offset line))
  1686. (and line
  1687. (+ (elf-section-offset line)
  1688. (elf-section-size line)))
  1689. (and pubnames
  1690. (elf-section-offset pubnames))
  1691. (and pubnames
  1692. (+ (elf-section-offset pubnames)
  1693. (elf-section-size pubnames)))
  1694. (and aranges
  1695. (elf-section-offset aranges))
  1696. (and aranges
  1697. (+ (elf-section-offset aranges)
  1698. (elf-section-size aranges))))
  1699. #() #f #f
  1700. (elf-section-offset info)
  1701. (+ (elf-section-offset info)
  1702. (elf-section-size info))
  1703. '())))
  1704. (define (die->tree die)
  1705. (cons* (die-tag die)
  1706. (cons 'offset (die-offset die))
  1707. (reverse! (fold-die-children
  1708. die
  1709. (lambda (die seed)
  1710. (cons (die->tree die) seed))
  1711. (fold acons '() (die-attrs die) (die-vals die))))))