dwarf.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <stdint.h>
  4. #include "dwarf.h"
  5. #include "../sti.h"
  6. // LEB128: "Little-Endian Base 128"
  7. int uleb128_encode(uint64_t n, uint8_t* out) {
  8. uint8_t chunks[9];
  9. uint8_t a;
  10. int i = 0;
  11. do {
  12. a = n & 0x7f;
  13. n >>= 7;
  14. if(n != 0) a |= 0x80;
  15. out[i++] = a;
  16. } while(a != 0);
  17. return i;
  18. }
  19. uint64_t uleb128_decode(uint8_t* in, uint8_t** end) {
  20. uint64_t out = 0;
  21. int i;
  22. for(i = 0; 1; i++) {
  23. uint8_t a = in[i] & 0x7f;
  24. out |= a << (i * 7);
  25. if((in[i] & 0x80) == 0) break;
  26. }
  27. if(end) *end = in + i + 1;
  28. return out;
  29. }
  30. int leb128_encode(int64_t n, uint8_t* out) {
  31. int i = 0;
  32. int neg = n < 0;
  33. // C does not specify if signed right shifts are arithmetic or logical
  34. uint64_t u = *((uint64_t*)&n);
  35. while(1) {
  36. uint8_t a = u & 0x7f;
  37. u >>= 7;
  38. if(neg) u |= -(1l<< 57l);
  39. out[i++] = a | 0x80;
  40. if(u == 0 && ((a & 0x40) == 0)) break;
  41. if(u == -1 && ((a & 0x40) == 0x40)) break;
  42. }
  43. // last byte has high bit cleared
  44. out[i - 1] &= 0x7f;
  45. return i;
  46. }
  47. int64_t leb128_decode(uint8_t* in, uint8_t** end) {
  48. int64_t sout;
  49. uint64_t out = 0;
  50. uint8_t a;
  51. int i;
  52. int sh = 0;
  53. for(i = 0; 1; i++) {
  54. a = in[i] & 0x7f;
  55. out |= a << sh;
  56. sh += 7;
  57. if((in[i] & 0x80) == 0) break;
  58. }
  59. // sign extend an originally-negative number
  60. if((i < 8) && (a & 0x40) == 0x40) {
  61. out |= -(1l << sh);
  62. }
  63. sout = *((int64_t*)&out);
  64. if(end) *end = in + i + 1;
  65. return sout;
  66. }
  67. #define PEEK_u8(p) (*((uint8_t*)(p)))
  68. #define PEEK_u16(p) (*((uint16_t*)(p)))
  69. #define PEEK_u32(p) (*((uint32_t*)(p)))
  70. #define PEEK_u64(p) (*((uint64_t*)(p)))
  71. #define PEEK_s8(p) (*((int8_t*)(p)))
  72. #define PEEK_s16(p) (*((int16_t*)(p)))
  73. #define PEEK_s32(p) (*((int32_t*)(p)))
  74. #define PEEK_s64(p) (*((int64_t*)(p)))
  75. struct fileinfo {
  76. char* name;
  77. uint64_t dir_index;
  78. uint64_t mtime;
  79. uint64_t size;
  80. };
  81. struct liheader {
  82. uint8_t* raw;
  83. size_t* raw_len;
  84. uint8_t header_bits; // 32 or 64
  85. uint8_t* header_part2; // len of this first part with version info
  86. uint64_t initial_length;
  87. uint16_t version;
  88. uint64_t header_length;
  89. /* ---- header_part2 ------- */
  90. uint8_t address_size;
  91. uint8_t segment_selector_size;
  92. uint8_t min_instr_length;
  93. uint8_t max_ops_per_instr;
  94. uint8_t default_is_stmt;
  95. int8_t line_base; // min. added to line for special opcode
  96. uint8_t line_range; // max. plus above - 1
  97. uint8_t opcode_base; // number assigned to first special opcode
  98. uint8_t* std_op_lengths; /*[opcode_base-1]*/
  99. // uint8_t dir_entry_format_cnt;
  100. // uint64_t* dir_entry_format; /*[dir_entry_format_cnt]*/
  101. // uint64_t dir_count;
  102. // /*dir_entry_format*/ char* directories; /*[dir_count];*/
  103. // uint64_t file_name_entry_fmt_cnt;
  104. // uint64_t* file_name_entry_fmt; /*[file_name_entry_fmt_cnt];*/
  105. // uint64_t file_names_cnt;
  106. /*file_name_entry_fmt*/ char* file_names; /*[file_names_cnt];*/
  107. VEC(char*) dir_names;
  108. VEC(struct fileinfo) files;
  109. } ;
  110. void* line_num_machine_v2(struct liheader* h) {
  111. uint8_t* raw = h->raw;
  112. uint8_t* r = h->header_part2;
  113. size_t len = h->raw_len;
  114. h->header_length = PEEK_u32(r);
  115. r += 4;
  116. h->min_instr_length = PEEK_u8(r);
  117. r++;
  118. h->default_is_stmt = PEEK_u8(r);
  119. r++;
  120. h->line_base = PEEK_s8(r);
  121. r++;
  122. h->line_range = PEEK_u8(r);
  123. r++;
  124. h->opcode_base = PEEK_u8(r);
  125. r++;
  126. printf("section len %d\n", len);
  127. printf("header_length %d\n", h->header_length);
  128. printf("min_instr_length %d\n", h->min_instr_length);
  129. printf("default_is_stmt %d\n", h->default_is_stmt);
  130. printf("line_base %d\n", h->line_base);
  131. printf("line_range %d\n", h->line_range);
  132. printf("opcode_base %d\n", h->opcode_base);
  133. h->std_op_lengths = malloc(sizeof(*h->std_op_lengths) * h->opcode_base);
  134. for(int i = 0; i < h->opcode_base-1; i++) {
  135. h->std_op_lengths[i] = PEEK_u8(r);
  136. r++;
  137. printf("oplen %d: %ld\n", i, h->std_op_lengths[i]);
  138. }
  139. printf("foo\n");
  140. // include_directories
  141. // null-terminated string list, followed by a null
  142. while(*r) {
  143. char* f;
  144. f = strdup(r);
  145. r += strlen(f) + 1;
  146. printf("dir: %s\n", f);
  147. VEC_PUSH(&h->dir_names, f);
  148. }
  149. r++;
  150. // r = memmem(r, h->initial_length, "\0", 2) + 2;
  151. // file_names, 1-based
  152. // null string, uleb=dir_index, uleb=mtime, uleb=flen
  153. // final null byte
  154. // dir-index is 1-based; 0 = cur dir
  155. while(*r) {
  156. struct fileinfo f;
  157. f.name = strdup(r);
  158. r += strlen(f.name) + 1;
  159. printf("r %p\n", r);
  160. f.dir_index = uleb128_decode(r, &r);
  161. printf("r %p\n", r);
  162. f.mtime = uleb128_decode(r, &r);
  163. printf("r %p\n", r);
  164. f.size = uleb128_decode(r, &r);
  165. printf("r %p\n", r);
  166. printf("file: %s\n", f.name);
  167. VEC_PUSH(&h->files, f);
  168. }
  169. r++;
  170. // program start
  171. printf("prog start\n");
  172. #define RESET_REGS() \
  173. address = 0; \
  174. op_index = 0; \
  175. column = 0; \
  176. line = 1; \
  177. file = 1; \
  178. is_stmt = h->default_is_stmt; \
  179. basic_block = 0; \
  180. end_sequence = 0; \
  181. prologue_end = 0; \
  182. epilogue_begin = 0; \
  183. isa = 0; \
  184. discriminator = 0;
  185. size_t address = 0;
  186. size_t op_index = 0;
  187. size_t column = 0;
  188. size_t line = 1;
  189. size_t file = 1;
  190. int is_stmt = h->default_is_stmt; // need to look in the header
  191. int basic_block = 0;
  192. int end_sequence = 0;
  193. int prologue_end = 0;
  194. int epilogue_begin = 0;
  195. size_t isa = 0;
  196. size_t discriminator = 0;
  197. size_t out_i = 0;
  198. #define APPEND_CURRENT() printf(" row %ld addr:%lx, file:%ld, line:%ld, col:%ld\n", out_i++, address, file, line, column);
  199. uint8_t* start_prog = h->header_part2 + h->header_length + 4;
  200. uint8_t* prog = start_prog;
  201. int f = 0;
  202. while(prog <= raw + len) {
  203. uint8_t op = *prog;
  204. prog++;
  205. printf("\nloop:%d op=%d prog=%ld \n", f++, op, prog - start_prog);
  206. if(op >= h->opcode_base) {
  207. printf(" special op\n");
  208. // special op decoding:
  209. uint8_t adj_op = op - h->opcode_base;
  210. uint64_t op_adv = adj_op / h->line_range;
  211. address += h->min_instr_length * op_adv;
  212. // op_index = (op_index + op_adv) % h->max_ops_per_instr;
  213. line += h->line_base + (adj_op % h->line_range);
  214. APPEND_CURRENT()
  215. basic_block = 0;
  216. continue;
  217. }
  218. if(op == 0) {
  219. // extended opcode
  220. uint64_t l = uleb128_decode(prog, &prog);
  221. uint8_t* orig_prog = prog;
  222. op = *prog;
  223. prog++;
  224. printf("extended op: %d %ld\n", op, l);
  225. // extended opcodes
  226. switch(op) {
  227. case DW_LNE_end_sequence:
  228. end_sequence = 1;
  229. APPEND_CURRENT();
  230. RESET_REGS();
  231. return NULL;
  232. break;
  233. case DW_LNE_set_address:
  234. // 1 machine address (64bit) > address
  235. address = *((uint64_t*)prog);
  236. prog += 8;
  237. op_index = 0;
  238. break;
  239. case DW_LNE_set_discriminator:
  240. // 1 uleb > discriminator
  241. discriminator = uleb128_decode(prog, &prog);
  242. break;
  243. case DW_LNE_define_file:
  244. printf("define file ext. opcode\n");
  245. printf(" '%s' \n", prog);
  246. // null string w/ file name
  247. // uleb dir index
  248. // uleb mtime
  249. // uleb fsize
  250. exit(1);
  251. break;
  252. }
  253. prog = orig_prog + l;
  254. continue;
  255. }
  256. switch(op) {
  257. case DW_LNS_copy:
  258. // no operands
  259. APPEND_CURRENT();
  260. discriminator = 0;
  261. basic_block = 0;
  262. prologue_end = 0;
  263. epilogue_begin = 0;
  264. break;
  265. case DW_LNS_advance_pc: {
  266. // 1 uleb operand
  267. uint64_t op_adv = uleb128_decode(prog, &prog);
  268. address += h->min_instr_length * op_adv;
  269. break;
  270. }
  271. case DW_LNS_advance_line:
  272. // 1 leb operand
  273. line += leb128_decode(prog, &prog);
  274. break;
  275. case DW_LNS_set_file:
  276. // 1 uleb operand > file
  277. file = uleb128_decode(prog, &prog);
  278. break;
  279. case DW_LNS_set_column:
  280. // 1 uleb operand > column
  281. column = uleb128_decode(prog, &prog);
  282. break;
  283. case DW_LNS_negate_stmt:
  284. is_stmt = !is_stmt;
  285. break;
  286. case DW_LNS_set_basic_block:
  287. basic_block = 1;
  288. break;
  289. case DW_LNS_const_add_pc: {
  290. // adv. address and op_index per sp.op. #255
  291. int adv = 255 - h->opcode_base;
  292. address += h->min_instr_length * adv;
  293. // op_index = (op_index + adv) % h->max_ops_per_instr;
  294. break;
  295. }
  296. case DW_LNS_fixed_advance_pc:
  297. // 1 uhalf op (unencoded) += address
  298. address += *((uint16_t*)prog);
  299. prog += 2;
  300. // op_index = 0;
  301. break;
  302. case DW_LNS_set_prologue_end:
  303. prologue_end = 1;
  304. break;
  305. case DW_LNS_set_epilogue_begin:
  306. epilogue_begin = 1;
  307. break;
  308. case DW_LNS_set_isa:
  309. // 1 uleb > isa
  310. isa = uleb128_decode(prog, &prog);
  311. break;
  312. default:
  313. // read oplen, then skip forward
  314. printf("unknown op: %d\n", op);
  315. /*
  316. for(int i = 0; i < oplens[op]; i++) {
  317. uleb128_decode(prog, &prog);
  318. }
  319. break;
  320. */
  321. }
  322. }
  323. }
  324. void* line_num_machine_v4(struct liheader* h) {
  325. uint8_t* raw = h->raw;
  326. size_t len = h->raw_len;
  327. /*
  328. if(h.header_bits == 32) {
  329. h.header_length = PEEK_u32(r);
  330. r += 4;
  331. }
  332. else {
  333. h.header_length = PEEK_u64(r);
  334. r += 8;
  335. }*/
  336. // printf("initial length: %d\n", h->initial_length);
  337. // printf("header length: %d\n", h->header_length);
  338. // printf("opcode_base: %d\n", h->opcode_base);
  339. // translate the standard opcode operand counts to a sane number format (because this is /totally/ worth saving 27 bytes in a /debug build/ executable
  340. int64_t* oplens = malloc(sizeof(oplens) * h->opcode_base);
  341. uint8_t* hp = raw + sizeof(*h);
  342. for(int i = 0; i < h->opcode_base; i++) {
  343. oplens[i] = leb128_decode(hp, &hp);
  344. }
  345. int dir_entry_format_cnt = *hp;
  346. hp++;
  347. for(int i = 0; i < dir_entry_format_cnt; i++) {
  348. uleb128_decode(hp, &hp); // content type code
  349. uleb128_decode(hp, &hp); // form code
  350. }
  351. uint64_t dir_cnt = uleb128_decode(hp, &hp); // these 2 bytes saved allowed the executable to fit on my 6TB disk.
  352. // for(int i = 0; i < dir_cnt; i++) { // TODO: complicated variable format
  353. // uleb128_decode(hp, &hp); // content type code
  354. // uleb128_decode(hp, &hp); // form code
  355. // }
  356. //.. can't do any more until the complicated format is done
  357. #define RESET_REGS() \
  358. address = 0; \
  359. op_index = 0; \
  360. column = 0; \
  361. line = 1; \
  362. file = 1; \
  363. is_stmt = h->default_is_stmt; \
  364. basic_block = 0; \
  365. end_sequence = 0; \
  366. prologue_end = 0; \
  367. epilogue_begin = 0; \
  368. isa = 0; \
  369. discriminator = 0;
  370. size_t address = 0;
  371. size_t op_index = 0;
  372. size_t column = 0;
  373. size_t line = 1;
  374. size_t file = 1;
  375. int is_stmt = h->default_is_stmt; // need to look in the header
  376. int basic_block = 0;
  377. int end_sequence = 0;
  378. int prologue_end = 0;
  379. int epilogue_begin = 0;
  380. size_t isa = 0;
  381. size_t discriminator = 0;
  382. size_t out_i = 0;
  383. #define APPEND_CURRENT() printf(" row %ld addr:%lx, file:%ld, line:%ld, col:%ld\n", out_i++, address, file, line, column);
  384. uint8_t* start_prog = &h->header_length + h->header_length;
  385. uint8_t* prog = &h->header_length + h->header_length;
  386. int f = 0;
  387. while(prog <= raw + len) {
  388. uint8_t op = *prog;
  389. prog++;
  390. printf("\nloop:%d op=%d prog=%ld \n", f++, op, prog-start_prog);
  391. if(op >= h->opcode_base) {
  392. printf(" special op\n");
  393. // special op decoding:
  394. uint8_t adj_op = op - h->opcode_base;
  395. uint64_t op_adv = adj_op / h->line_range;
  396. address += h->min_instr_length + ((op_index + op_adv) / h->max_ops_per_instr);
  397. op_index = (op_index + op_adv) % h->max_ops_per_instr;
  398. line += h->line_base + (adj_op % h->line_range);
  399. APPEND_CURRENT()
  400. basic_block = 0;
  401. prologue_end = 0;
  402. epilogue_begin = 0;
  403. discriminator = 0;
  404. continue;
  405. }
  406. if(op == 0) {
  407. // extended opcode
  408. uint64_t l = uleb128_decode(prog, &prog);
  409. uint8_t orig_prog = prog;
  410. op = *prog;
  411. // extended opcodes
  412. switch(op) {
  413. case DW_LNE_end_sequence:
  414. end_sequence = 1;
  415. APPEND_CURRENT();
  416. RESET_REGS();
  417. return NULL;
  418. break;
  419. case DW_LNE_set_address:
  420. // 1 machine address (64bit) > address
  421. address = *((uint64_t*)prog);
  422. prog += 8;
  423. op_index = 0;
  424. break;
  425. case DW_LNE_set_discriminator:
  426. // 1 uleb > discriminator
  427. discriminator = uleb128_decode(prog, &prog);
  428. break;
  429. /*
  430. case DW_LNS_define_file:
  431. // deprecated as of DWARF v5
  432. printf("DW_LNS_define_file is deprecated.\n");
  433. break;
  434. */
  435. }
  436. prog = orig_prog + l;
  437. continue;
  438. }
  439. switch(op) {
  440. case DW_LNS_copy:
  441. // no operands
  442. APPEND_CURRENT();
  443. discriminator = 0;
  444. basic_block = 0;
  445. prologue_end = 0;
  446. epilogue_begin = 0;
  447. break;
  448. case DW_LNS_advance_pc: {
  449. // 1 uleb operand
  450. uint64_t adv = uleb128_decode(prog, &prog);
  451. // mod address and op_index per rules on p160
  452. address += h->min_instr_length + ((op_index + adv) / h->max_ops_per_instr);
  453. op_index = (op_index + adv) % h->max_ops_per_instr;
  454. break;
  455. }
  456. case DW_LNS_advance_line:
  457. // 1 leb operand
  458. line += leb128_decode(prog, &prog);
  459. break;
  460. case DW_LNS_set_file:
  461. // 1 uleb operand > file
  462. file = uleb128_decode(prog, &prog);
  463. break;
  464. case DW_LNS_set_column:
  465. // 1 uleb operand > column
  466. column = uleb128_decode(prog, &prog);
  467. break;
  468. case DW_LNS_negate_stmt:
  469. is_stmt = !is_stmt;
  470. break;
  471. case DW_LNS_set_basic_block:
  472. basic_block = 1;
  473. break;
  474. case DW_LNS_const_add_pc: {
  475. // adv. address and op_index per sp.op. #255
  476. int adv = 255 - h->opcode_base;
  477. address += h->min_instr_length + ((op_index + adv) / h->max_ops_per_instr);
  478. op_index = (op_index + adv) % h->max_ops_per_instr;
  479. break;
  480. }
  481. case DW_LNS_fixed_advance_pc:
  482. // 1 uhalf op (unencoded) += address
  483. address += *((uint16_t*)prog);
  484. prog += 2;
  485. op_index = 0;
  486. break;
  487. case DW_LNS_set_prologue_end:
  488. prologue_end = 1;
  489. break;
  490. case DW_LNS_set_epilogue_begin:
  491. epilogue_begin = 1;
  492. break;
  493. case DW_LNS_set_isa:
  494. // 1 uleb > isa
  495. isa = uleb128_decode(prog, &prog);
  496. break;
  497. default:
  498. // read oplen, then skip forward
  499. printf("unknown op: %d\n", op);
  500. for(int i = 0; i < oplens[op]; i++) {
  501. uleb128_decode(prog, &prog);
  502. }
  503. break;
  504. }
  505. }
  506. }
  507. void* line_num_machine(uint8_t* raw, size_t len) {
  508. struct liheader h = {0};
  509. uint8_t* r = raw;
  510. h.raw = raw;
  511. h.raw_len = len;
  512. h.initial_length = PEEK_u32(r);
  513. r += 4;
  514. if(h.initial_length >= 0xfffffff0) {
  515. // 64 bit header
  516. h.header_bits = 64;
  517. h.initial_length = PEEK_u64(r);
  518. r += 8;
  519. }
  520. else {
  521. // 32 bit header
  522. h.header_bits = 32;
  523. }
  524. h.version = PEEK_u16(r);
  525. r += 2;
  526. h.header_part2 = r; // versioned functions pick up here
  527. // printf("line number version: %d\n", h.version);
  528. //
  529. switch(h.version) {
  530. case 2: return line_num_machine_v2(&h);
  531. // case 4: return line_num_machine_v4(&h);
  532. default:
  533. printf("Unsupported DWARF Line Info version: %d\n", h.version);
  534. return NULL;
  535. }
  536. }
  537. void* debug_info_parse(uint8_t* raw, size_t raw_len) {
  538. int filebits;
  539. uint8_t* r = raw;
  540. uint64_t debug_abbrev_offset;
  541. uint8_t address_bytes;
  542. // compilation unit header
  543. // h.raw = raw;
  544. // h.raw_len = len;
  545. uint64_t initial_length = PEEK_u32(r);
  546. r += 4;
  547. if(initial_length >= 0xfffffff0) {
  548. // 64 bit header
  549. h.filebits = 64;
  550. // h.initial_length = PEEK_u64(r);
  551. r += 8;
  552. }
  553. else {
  554. // 32 bit header
  555. filebits = 32;
  556. }
  557. uint16_t version = PEEK_u16(r);
  558. r += 2;
  559. printf("debug info version: %d \n", version);
  560. if(version != 4) {
  561. printf("unsupported debug info version: %d\n", version);
  562. return NULL;
  563. }
  564. if(filebits == 64) {
  565. debug_abbrev_offset = PEEK_u64(r);
  566. r += 8;
  567. }
  568. else {
  569. debug_abbrev_offset = PEEK_u32(r);
  570. r += 4;
  571. }
  572. address_bytes = PEEK_u8(r);
  573. r++;
  574. // debug info records
  575. uint64_t abbrcode = uleb128_decode(r, &r);
  576. // 0 is invalid and should be skipped
  577. // attributes
  578. }
  579. void read_abbr(uint8_t* raw, size_t raw_len) {
  580. uint8_t* r = raw;
  581. uint64_t abbr_code = uleb128_decode(r, &r);
  582. uint64_t tag = uleb128_decode(r, &r);
  583. uint8_t has_childen = PEEK_u8(r)
  584. r++;
  585. // DW_CHILDREN_yes = next DIE entry is a child
  586. // DW_CHILDREN_no = next DIE entry is a sibling
  587. // sibling chains are terminated by a null entry
  588. // attributes
  589. while(1) {
  590. uint64_t name = uleb128_decode(r, &r);
  591. uint64_t form = uleb128_decode(r, &r);
  592. // DW_FORM_indirect = each attr instance defines its own form inline
  593. if(name == 0 && form == 0) break; // null terminated
  594. }
  595. // abbrev table ends with 0 for abbr_code
  596. }
  597. void* debug_types_parse(uint8_t* raw, size_t raw_len) {
  598. int filebits;
  599. uint8_t* r = raw;
  600. uint64_t debug_abbrev_offset;
  601. uint8_t address_bytes;
  602. // type header
  603. // h.raw = raw;
  604. // h.raw_len = len;
  605. uint64_t initial_length = PEEK_u32(r);
  606. r += 4;
  607. if(initial_length >= 0xfffffff0) {
  608. // 64 bit header
  609. h.filebits = 64;
  610. // h.initial_length = PEEK_u64(r);
  611. r += 8;
  612. }
  613. else {
  614. // 32 bit header
  615. filebits = 32;
  616. }
  617. uint16_t version = PEEK_u16(r);
  618. r += 2;
  619. printf("debug type version: %d \n", version);
  620. if(version != 4) {
  621. printf("unsupported debug type version: %d\n", version);
  622. return NULL;
  623. }
  624. if(filebits == 64) {
  625. debug_abbrev_offset = PEEK_u64(r);
  626. r += 8;
  627. }
  628. else {
  629. debug_abbrev_offset = PEEK_u32(r);
  630. r += 4;
  631. }
  632. address_bytes = PEEK_u8(r);
  633. r++;
  634. uint64_t type_sig = PEEK_u64(r);
  635. r += 8;
  636. uint64_t type_offset;
  637. if(filebits == 64) {
  638. type_offset = PEEK_u64(r);
  639. r += 8;
  640. }
  641. else {
  642. type_offset = PEEK_u32(r);
  643. r += 4;
  644. }
  645. // type info
  646. }