-
Notifications
You must be signed in to change notification settings - Fork 5
/
dwarf-read.c
2320 lines (2045 loc) · 58.5 KB
/
dwarf-read.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2009-2012 Message Systems, Inc. All rights reserved
* For licensing information, see:
* https://bitbucket.org/wez/gimli/src/tip/LICENSE
*/
#include "impl.h"
#include "gimli_dwarf.h"
static gimli_addr_t calc_reloc(gimli_mapped_object_t f);
struct dw_die_arange {
uint64_t addr;
uint64_t len;
uint64_t di_offset;
};
uint64_t dw_read_uleb128(const uint8_t **ptr, const uint8_t *end)
{
uint64_t res = 0;
int shift = 0;
const uint8_t *cur = *ptr;
while (cur < end) {
uint8_t b = *(uint8_t*)cur;
cur++;
res |= (b & 0x7f) << shift;
if ((b & 0x80) == 0) break;
shift += 7;
}
*ptr = cur;
return res;
}
int64_t dw_read_leb128(const uint8_t **ptr, const uint8_t *end)
{
int64_t res = 0;
int shift = 0;
int sign;
const uint8_t *cur = *ptr;
while (cur < end) {
uint8_t b = *(uint8_t*)cur;
cur++;
res |= (b & 0x7f) << shift;
shift += 7;
sign = (b & 0x40);
if ((b & 0x80) == 0) break;
}
if ((shift < sizeof(res) * 8) && (sign)) {
/* sign extend */
res |= - (1 << shift);
}
*ptr = cur;
return res;
}
int dw_read_encptr(gimli_proc_t proc,
uint8_t enc, const uint8_t **ptr, const uint8_t *end,
uint64_t pc, uint64_t *output)
{
const uint8_t *cur = *ptr;
uint64_t res = 0;
uint64_t base = 0;
if ((enc & DW_EH_PE_indirect) == DW_EH_PE_indirect) {
/* the issue here is that we need to adjust for the load address of
* the target. */
printf("DW_EH_PE_indirect is not supported correctly at this time\n");
return 0;
if (sizeof(void*) == 8) {
if (gimli_read_mem(proc, res, &res, sizeof(res))
!= sizeof(*output)) {
return 0;
}
} else {
uint32_t r;
if (gimli_read_mem(proc, res, &r, sizeof(r)) != sizeof(r)) {
return 0;
}
res = r;
}
}
switch (enc & DW_EH_PE_APPL_MASK) {
case DW_EH_PE_absptr:
base = 0;
break;
case DW_EH_PE_pcrel:
base = (uint64_t)pc;
break;
case DW_EH_PE_datarel:
default:
fprintf(stderr, "DWARF: unhandled pointer application value: %02x at %p\n", enc & DW_EH_PE_APPL_MASK, (void*)pc);
return 0;
}
if ((enc & 0x07) == 0x00) {
if (sizeof(void*) == 4) {
enc |= DW_EH_PE_udata4;
} else {
enc |= DW_EH_PE_udata8;
}
}
switch (enc & 0x0f) {
case DW_EH_PE_uleb128:
res = base + dw_read_uleb128(ptr, end);
break;
case DW_EH_PE_udata2:
{
uint16_t d;
memcpy(&d, cur, sizeof(d));
*ptr = cur + sizeof(d);
res = base + d;
}
break;
case DW_EH_PE_udata4:
{
uint32_t d;
memcpy(&d, cur, sizeof(d));
*ptr = cur + sizeof(d);
res = base + d;
}
break;
case DW_EH_PE_udata8:
{
uint64_t d;
memcpy(&d, cur, sizeof(d));
*ptr = cur + sizeof(d);
res = base + d;
}
break;
case DW_EH_PE_sleb128:
res = base + dw_read_leb128(ptr, end);
break;
case DW_EH_PE_sdata2:
{
int16_t d;
memcpy(&d, cur, sizeof(d));
*ptr = cur + sizeof(d);
res = base + d;
}
break;
case DW_EH_PE_sdata4:
{
int32_t d;
memcpy(&d, cur, sizeof(d));
*ptr = cur + sizeof(d);
res = base + d;
}
break;
case DW_EH_PE_sdata8:
{
int64_t d;
memcpy(&d, cur, sizeof(d));
*ptr = cur + sizeof(d);
res = base + d;
}
break;
default:
fprintf(stderr, "DWARF: unhandled DW_EH_PE value: 0x%02x (masked to 0x%02x) at %p\n", enc, enc & 0x0f, (void*)pc);
return 0;
}
*output = res;
return 1;
}
static int sort_by_addr(const void *A, const void *B)
{
struct gimli_line_info *a = (struct gimli_line_info*)A;
struct gimli_line_info *b = (struct gimli_line_info*)B;
return a->addr - b->addr;
}
static int search_compare_line(const void *addrp, const void *L)
{
struct gimli_line_info *line = (struct gimli_line_info*)L;
gimli_addr_t pc = *(gimli_addr_t*)addrp;
if (pc < line->addr) {
return -1;
}
if (pc < line->end) {
return 0;
}
return 1;
}
static int process_line_numbers(gimli_mapped_object_t f);
/* read dwarf info to determine the source/line information for a given
* address */
int gimli_determine_source_line_number(gimli_proc_t proc,
gimli_addr_t pc, char *src, int srclen,
uint64_t *lineno)
{
struct gimli_object_mapping *m;
gimli_mapped_object_t f;
struct gimli_line_info *linfo;
m = gimli_mapping_for_addr(proc, pc);
if (!m) {
return 0;
}
f = m->objfile;
if (!f->elf) {
/* can happen if the original file has been removed from disk */
return 0;
}
if (!f->lines) {
process_line_numbers(f);
if (!f->lines) {
return 0;
}
}
#ifdef __MACH__
pc -= f->base_addr;
#else
if (!gimli_object_is_executable(f->elf)) {
pc -= calc_reloc(f);
}
#endif
linfo = bsearch(&pc, f->lines, f->linecount, sizeof(*linfo),
search_compare_line);
if (linfo) {
snprintf(src, srclen, "%s", linfo->filename);
*lineno = linfo->lineno;
return 1;
}
return 0;
}
static int process_line_numbers(gimli_mapped_object_t f)
{
struct gimli_section_data *s = NULL;
struct {
char *address;
uint64_t file;
uint64_t line;
uint64_t column;
uint8_t is_stmt; /* recommended breakpoint location */
uint8_t basic_block; /* is start of a basic block */
uint8_t end_sequence;
uint8_t prologue_end;
uint8_t epilogue_begin;
uint64_t isa;
} regs;
const uint8_t *data, *end;
uint32_t initlen;
uint64_t len;
int is_64 = 0;
uint16_t ver;
struct {
uint8_t min_insn_len;
uint8_t def_is_stmt;
int8_t line_base;
uint8_t line_range;
uint8_t opcode_base;
} hdr_1;
uint64_t *opcode_lengths = NULL;
int i;
const char *filenames[1024];
uint8_t op;
struct gimli_line_info *linfo;
int debugline = debug && 0;
if (f->aux_elf) {
s = gimli_get_section_by_name(f->aux_elf, ".debug_line");
if (s) {
data = s->data;
}
}
if (!s) {
s = gimli_get_section_by_name(f->elf, ".debug_line");
if (s) {
data = s->data;
}
}
if (!s) {
return 0;
}
if (debugline) fprintf(stderr, "\nGot debug_line info\n");
end = data + s->size;
while (data < end) {
const uint8_t *cuend;
void *prior;
memset(®s, 0, sizeof(regs));
regs.file = 1;
regs.line = 1;
prior = NULL;
/* read the initial length, this tells us which dwarf version and format
* we're dealing with */
memcpy(&initlen, data, sizeof(initlen));
data += sizeof(initlen);
if (initlen == 0xffffffff) {
/* this is a 64-bit dwarf */
is_64 = 1;
memcpy(&len, data, sizeof(len));
data += sizeof(len);
} else {
len = initlen;
}
cuend = data + len;
memcpy(&ver, data, sizeof(ver));
data += sizeof(ver);
if (debugline) {
fprintf(stderr, "initlen is 0x%" PRIx64 " (%d bit) ver=%u\n",
len, is_64 ? 64 : 32, ver);
}
if (is_64) {
memcpy(&len, data, sizeof(len));
data += sizeof(len);
} else {
memcpy(&initlen, data, sizeof(initlen));
data += sizeof(initlen);
len = initlen;
}
memcpy(&hdr_1, data, sizeof(hdr_1));
data += sizeof(hdr_1);
regs.is_stmt = hdr_1.def_is_stmt;
if (debugline) {
fprintf(stderr,
"headerlen is %" PRIu64 ", min_insn_len=%u line_base=%d line_range=%u\n"
"opcode_base=%u\n",
len, hdr_1.min_insn_len, hdr_1.line_base, hdr_1.line_range,
hdr_1.opcode_base);
}
if (opcode_lengths) free(opcode_lengths);
opcode_lengths = calloc(hdr_1.opcode_base, sizeof(uint64_t));
for (i = 1; i < hdr_1.opcode_base; i++) {
opcode_lengths[i-1] = dw_read_uleb128(&data, cuend);
if (debugline) {
fprintf(stderr, "op len [%d] = %" PRIu64 "\n", i, opcode_lengths[i-1]);
}
}
/* include_directories */
while (*data && data < cuend) {
if (debugline) fprintf(stderr, "inc_dir: %s\n", data);
data += strlen((char*)data) + 1;
}
data++;
/* files */
i = 1;
memset(filenames, 0, sizeof(filenames));
while (*data && data < cuend) {
if (i >= sizeof(filenames)/sizeof(filenames[0])) {
fprintf(stderr, "DWARF: too many files for line number info reader\n");
return 0;
}
if (debugline) fprintf(stderr, "file[%d] = %s\n", i, data);
filenames[i] = (char*)data;
data += strlen((char*)data) + 1;
/* ignore additional data about the file */
dw_read_uleb128(&data, cuend);
dw_read_uleb128(&data, cuend);
dw_read_uleb128(&data, cuend);
i++;
}
data++;
/* opcodes */
while (data < cuend) {
memcpy(&op, data, sizeof(op));
data += sizeof(op);
prior = regs.address;
if (op == 0) {
/* extended */
const uint8_t *next;
initlen = dw_read_uleb128(&data, cuend);
memcpy(&op, data, sizeof(op));
next = data + initlen;
data += sizeof(op);
switch (op) {
case DW_LNE_set_address:
{
void *addr;
memcpy(&addr, data, sizeof(addr));
if (debugline) fprintf(stderr, "set_address %p\n", addr);
regs.address = addr;
break;
}
case DW_LNE_end_sequence:
{
if (debugline) fprintf(stderr, "end_sequence\n");
memset(®s, 0, sizeof(regs));
regs.file = 1;
regs.line = 1;
break;
}
case DW_LNE_define_file:
{
const char *fname = (char*)data;
uint64_t fno;
data += strlen(fname)+1;
fno = dw_read_uleb128(&data, cuend);
filenames[fno] = fname;
if (debugline) fprintf(stderr, "define_files[%" PRIu64 "] = %s\n", fno, fname);
break;
}
default:
// fprintf(stderr,
// "DWARF: line nos.: unhandled extended op=%02x, len=%" PRIu32 "\n",
// op, initlen);
;
}
data = next;
} else if (op < hdr_1.opcode_base) {
/* standard opcode */
switch (op) {
case DW_LNS_copy:
if (debugline) fprintf(stderr, "copy\n");
regs.basic_block = 0;
regs.prologue_end = 0;
regs.epilogue_begin = 0;
break;
case DW_LNS_advance_line:
{
int64_t d = dw_read_leb128(&data, cuend);
if (debugline) {
fprintf(stderr, "advance_line from %" PRId64 " to %" PRId64 "\n",
regs.line, regs.line + d);
}
regs.line += d;
break;
}
case DW_LNS_advance_pc:
{
uint64_t u = dw_read_uleb128(&data, cuend);
regs.address += u * hdr_1.min_insn_len;
if (debugline) {
fprintf(stderr, "advance_pc: addr=0x%" PRIx64 "\n", (uintptr_t)regs.address);
}
break;
}
case DW_LNS_set_file:
{
uint64_t u = dw_read_uleb128(&data, cuend);
regs.file = u;
if (debugline) fprintf(stderr, "set_file: %" PRIu64 "\n", regs.file);
break;
}
case DW_LNS_set_column:
{
uint64_t u = dw_read_uleb128(&data, cuend);
regs.column = u;
if (debugline) fprintf(stderr, "set_column: %" PRIu64 "\n", regs.column);
break;
}
case DW_LNS_negate_stmt:
if (debugline) fprintf(stderr, "negate_stmt\n");
regs.is_stmt = !regs.is_stmt;
break;
case DW_LNS_set_basic_block:
if (debugline) fprintf(stderr, "set_basic_block\n");
regs.basic_block = 1;
break;
case DW_LNS_const_add_pc:
regs.address += ((255 - hdr_1.opcode_base) /
hdr_1.line_range) * hdr_1.min_insn_len;
if (debugline) {
fprintf(stderr, "const_add_pc: addr=0x%" PRIx64 "\n", (uintptr_t)regs.address);
}
break;
case DW_LNS_fixed_advance_pc:
{
uint16_t u;
memcpy(&u, data, sizeof(u));
data += sizeof(u);
regs.address += u;
if (debugline) {
fprintf(stderr, "fixed_advance_pc: 0x%" PRIx64 "\n", (uintptr_t)regs.address);
}
break;
}
case DW_LNS_set_prologue_end:
if (debugline) {
fprintf(stderr, "set_prologue_end\n");
}
regs.prologue_end = 1;
break;
case DW_LNS_set_epilogue_begin:
if (debugline) {
fprintf(stderr, "set_epilogue_begin\n");
}
regs.epilogue_begin = 1;
break;
case DW_LNS_set_isa:
regs.isa = dw_read_uleb128(&data, cuend);
if (debugline) {
fprintf(stderr, "set_isa: 0x%" PRIx64 "\n", regs.isa);
}
break;
default:
fprintf(stderr, "DWARF: line nos: unhandled op: %02x\n", op);
/* consume unknown/unhandled args */
for (i = 0; i < opcode_lengths[i]; i++) {
dw_read_uleb128(&data, cuend);
}
}
} else {
/* special opcode */
op -= hdr_1.opcode_base;
if (debugline) {
fprintf(stderr, "special before: addr = %p, line = %" PRId64 "\n",
regs.address, regs.line);
fprintf(stderr, "line_base = %d, line_range = %d\n",
hdr_1.line_base, hdr_1.line_range);
}
regs.address += (op / hdr_1.line_range) * hdr_1.min_insn_len;
regs.line += hdr_1.line_base + (op % hdr_1.line_range);
if (debugline) {
fprintf(stderr, "special: addr = %p, line = %" PRId64 "\n",
regs.address, regs.line);
}
}
if (regs.address && filenames[regs.file]) {
if (f->linecount + 1 >= f->linealloc) {
f->linealloc = f->linealloc ? f->linealloc * 2 : 1024;
f->lines = realloc(f->lines, f->linealloc * sizeof(*linfo));
}
linfo = &f->lines[f->linecount++];
linfo->filename = filenames[regs.file];
linfo->lineno = regs.line;
linfo->addr = (gimli_addr_t)regs.address;
}
}
}
qsort(f->lines, f->linecount, sizeof(struct gimli_line_info), sort_by_addr);
//printf("sorting %d lines in %s\n", f->linecount, f->objname);
free(opcode_lengths);
/* make a pass to fill in the end member to make it easier to find
* an approx match */
if (f->linecount) {
for (i = 0; i < f->linecount - 1; i++) {
f->lines[i].end = f->lines[i+1].addr;
}
}
return 0;
}
static int get_sect_data(gimli_mapped_object_t f, const char *name,
const uint8_t **startptr, const uint8_t **endptr, gimli_object_file_t *elf)
{
const uint8_t *data = NULL, *end = NULL;
struct gimli_section_data *s;
if (elf && *elf) {
s = gimli_get_section_by_name(*elf, name);
} else {
s = gimli_get_section_by_name(f->elf, name);
if (!s || s->size <= sizeof(void*)) {
if (f->aux_elf) {
s = gimli_get_section_by_name(f->aux_elf, name);
} else {
s = NULL;
}
}
}
if (s) {
data = s->data;
if (elf) *elf = s->container;
} else {
data = NULL;
if (elf) *elf = NULL;
}
if (data == NULL) {
return 0;
}
end = data + s->size;
*startptr = data;
*endptr = end;
return 1;
}
/* given a location list offset, determine the location in question */
int dw_calc_location(struct gimli_unwind_cursor *cur,
uint64_t compilation_unit_base_addr,
struct gimli_object_mapping *m, uint64_t offset, uint64_t *res,
gimli_object_file_t elf, int *is_stack)
{
const uint8_t *data, *end;
void *rstart = NULL, *rend = NULL;
uint16_t len;
uint64_t off = compilation_unit_base_addr;
if (!get_sect_data(m->objfile, ".debug_loc", &data, &end, &elf)) {
printf("Couldn't find a .debug_loc\n");
return 0;
}
// printf("Using offset %d into .debug_loc\n", offset);
data += offset;
while (data < end) {
// printf("populating rstart with %d bytes\n", sizeof(rstart));
memcpy(&rstart, data, sizeof(rstart));
data += sizeof(rstart);
memcpy(&rend, data, sizeof(rend));
data += sizeof(rend);
if (rstart == 0 && rend == 0) {
/* end of list */
// printf("rstart = %p, so ending list\n", rstart);
break;
}
if (rstart == (void*)-1) {
/* base selection */
off = (uint64_t)(intptr_t)rend;
printf("got base selection: %p\n", rend);
continue;
}
rstart += off;
rend += off;
memcpy(&len, data, sizeof(len));
data += sizeof(len);
// printf("This section is %d bytes in length\n", len);
// printf("%p - %p\n", rstart, rend);
if (cur->st.pc >= rstart && cur->st.pc < rend) {
// printf("Found the range I was looking for, data=%p, len=%d\n", data, len);
return dw_eval_expr(cur, (uint8_t*)data, len, 0, res, NULL, is_stack);
}
data += len;
}
return 0;
}
static const uint8_t *find_abbr(gimli_mapped_object_t file,
uint64_t da_offset,
uint64_t fcode)
{
uint64_t code;
uint64_t tag;
uint64_t key;
const uint8_t *abbr;
int slow_mode = 0;
if (!file->abbr.map) {
if (!get_sect_data(file, ".debug_abbrev",
&file->abbr.start, &file->abbr.end, &file->abbr.elf)) {
printf("could not get abbrev data for %s\n", file->objname);
return 0;
}
/* observed approx 11-13 per abbrev, err on the side of avoiding
* rebuckets */
file->abbr.map = gimli_hash_new_size(NULL, GIMLI_HASH_U64_KEYS,
(file->abbr.end - file->abbr.start) / 10);
}
/* NOTE: even though DWARF allows for 64-bit offsets, we're making the assumption
* that they are not practical or possible for the next few years.
* Doing so allows us to cheaply record both the da_offset and code
* into a u64 key.
* I've observed approx 17% collisions with a max chain length of 7
* in a collided bucket. It's not perfect but it is effective.
* */
if (da_offset > UINT32_MAX || fcode > UINT32_MAX) {
// Allow correct, albeit slow, operation when we overflow this assumption
slow_mode = 1;
}
if (!slow_mode) {
key = (da_offset << 32) | (fcode & 0xffffffff);
if (gimli_hash_find_u64(file->abbr.map, key, (void**)&abbr)) {
return abbr;
}
}
abbr = file->abbr.start + da_offset;
while (abbr < file->abbr.end) {
code = dw_read_uleb128(&abbr, file->abbr.end);
if (code == 0) continue;
// printf("find_abbr: %lld (looking for %lld)\n", code, fcode);
if (fcode == code) {
//printf("find_abbr: %" PRIx64 " -> %p\n", fcode, abbr);
if (!slow_mode &&
!gimli_hash_insert_u64(file->abbr.map, key, (void*)abbr)) {
void *ptr = NULL;
gimli_hash_find_u64(file->abbr.map, key, &ptr);
if (ptr != abbr) {
printf("find_abbr: %" PRIx64 " (key=%" PRIx64 ") collided with %p and %p\n",
fcode, key, abbr, ptr);
}
}
return abbr;
}
tag = dw_read_uleb128(&abbr, file->abbr.end);
abbr += sizeof(uint8_t);
while (abbr < file->abbr.end) {
dw_read_uleb128(&abbr, file->abbr.end);
code = dw_read_uleb128(&abbr, file->abbr.end);
if (code == 0) {
break;
}
}
}
return NULL;
}
static uint64_t get_value(uint64_t form, uint64_t addr_size, int is_64,
const uint8_t **datap, const uint8_t *end,
uint64_t *vptr, const uint8_t **byteptr,
gimli_object_file_t elf)
{
uint64_t u64;
int64_t s64;
uint32_t u32;
uint16_t u16;
uint8_t u8;
const uint8_t *data = *datap;
const uint8_t *strp, *send;
*byteptr = NULL;
switch (form) {
case DW_FORM_addr:
case DW_FORM_ref_addr:
switch (addr_size) {
case 1:
memcpy(&u8, data, sizeof(u8));
*vptr = u8;
break;
case 2:
memcpy(&u16, data, sizeof(u16));
*vptr = u16;
break;
case 4:
memcpy(&u32, data, sizeof(u32));
*vptr = u32;
break;
case 8:
memcpy(vptr, data, sizeof(u64));
break;
}
data += addr_size;
break;
case DW_FORM_data1:
case DW_FORM_ref1:
memcpy(&u8, data, sizeof(u8));
data += sizeof(u8);
*vptr = u8;
break;
case DW_FORM_data2:
case DW_FORM_ref2:
memcpy(&u16, data, sizeof(u16));
data += sizeof(u16);
*vptr = u16;
break;
case DW_FORM_data4:
case DW_FORM_ref4:
memcpy(&u32, data, sizeof(u32));
data += sizeof(u32);
*vptr = u32;
break;
case DW_FORM_data8:
case DW_FORM_ref8:
memcpy(&u64, data, sizeof(u64));
data += sizeof(u64);
*vptr = u64;
break;
case DW_FORM_udata:
case DW_FORM_ref_udata:
*vptr = dw_read_uleb128(&data, end);
break;
case DW_FORM_sdata:
s64 = dw_read_leb128(&data, end);
*vptr = (uint64_t)s64;
break;
case DW_FORM_flag:
memcpy(&u8, data, sizeof(u8));
data += sizeof(u8);
*vptr = u8;
break;
/* for blocks, store length in vptr and set byteptr to start of data */
case DW_FORM_block1:
memcpy(&u8, data, sizeof(u8));
*vptr = u8;
data += sizeof(u8);
*byteptr = data;
data += *vptr;
break;
case DW_FORM_block2:
memcpy(&u16, data, sizeof(u16));
*vptr = u16;
data += sizeof(u16);
*byteptr = data;
data += *vptr;
break;
case DW_FORM_block4:
memcpy(&u32, data, sizeof(u32));
*vptr = u32;
data += sizeof(u32);
*byteptr = data;
data += *vptr;
break;
case DW_FORM_block:
*vptr = dw_read_uleb128(&data, end);
*byteptr = data;
data += *vptr;
break;
case DW_FORM_strp:
if (is_64) {
memcpy(vptr, data, sizeof(*vptr));
data += sizeof(*vptr);
} else {
memcpy(&u32, data, sizeof(u32));
data += sizeof(u32);
*vptr = u32;
}
if (get_sect_data(NULL, ".debug_str", &strp, &send, &elf)) {
const uint8_t *str = strp + *vptr;
*vptr = strlen((char*)str);
form = DW_FORM_string;
*byteptr = str;
}
break;
case DW_FORM_string:
*byteptr = data;
*vptr = strlen((char*)data);
data += 1 + *vptr;
break;
case DW_FORM_indirect:
form = dw_read_uleb128(datap, end);
if (form == DW_FORM_indirect) {
printf(
"DWARF: can't have an indirect FORM reference an indirect FORM\n");
return 0;
}
return get_value(form, addr_size, is_64, datap, end, vptr, byteptr, elf);
default:
printf("DWARF: unhandled FORM: 0x%" PRIx64 "\n", form);
return 0;
}
*datap = data;
/* normalize the form */
switch (form) {
case DW_FORM_string:
case DW_FORM_flag:
case DW_FORM_addr:
case DW_FORM_udata:
case DW_FORM_sdata:
case DW_FORM_ref_addr:
break;
case DW_FORM_block:
case DW_FORM_block1:
case DW_FORM_block2:
case DW_FORM_block4:
form = DW_FORM_block;
break;
case DW_FORM_data1:
case DW_FORM_data2:
case DW_FORM_data4:
case DW_FORM_data8:
form = DW_FORM_data8;
break;
case DW_FORM_ref1:
case DW_FORM_ref2:
case DW_FORM_ref4:
case DW_FORM_ref8:
case DW_FORM_ref_udata:
form = DW_FORM_ref_udata;
break;
}
if (debug && 0) {
printf("value normalized to form 0x%" PRIx64 " val=0x%" PRIx64 " bytep=%p %s\n",
form, *vptr, *byteptr, form == DW_FORM_string ? (char*)*byteptr : "");
}
return form;
}
static struct gimli_dwarf_die *process_die(
gimli_mapped_object_t file,
struct gimli_dwarf_cu *cu,
const uint8_t *custart,
const uint8_t **datap, const uint8_t *end,
uint64_t da_offset,
int is_64, uint8_t addr_size
)
{
const uint8_t *data = *datap;
uint64_t abbr_code;
uint64_t tag;
uint8_t has_children;
uint64_t atype, aform;
const uint8_t *abbr;
struct gimli_dwarf_die *die = NULL, *kid = NULL;
struct gimli_dwarf_attr *attr = NULL;
uint64_t offset;
offset = data - file->debug_info.start;
abbr_code = dw_read_uleb128(&data, end);
if (abbr_code == 0) {
// Skip over NUL entry
// printf("found a NUL entry @ 0x%" PRIx64 "\n", offset);
*datap = data;
return NULL;
}
abbr = find_abbr(file, da_offset, abbr_code);
if (!abbr) {
printf("Couldn't locate abbrev code %" PRId64 "\n", abbr_code);
*datap = data;
return NULL;
}
/* what kind of entry is this? */
tag = dw_read_uleb128(&abbr, file->abbr.end);
memcpy(&has_children, abbr, sizeof(has_children));
abbr += sizeof(has_children);
if (has_children != 0 && has_children != 1) {
printf("invalid value for has_children! %d\n", has_children);
abort();
}
die = gimli_slab_alloc(&file->dieslab);
memset(die, 0, sizeof(*die));
die->offset = offset;
die->tag = tag;
STAILQ_INIT(&die->kids);
while (data < end && abbr < file->abbr.end) {
atype = dw_read_uleb128(&abbr, file->abbr.end);
aform = dw_read_uleb128(&abbr, file->abbr.end);
if (atype == 0) {
break;
}
attr = gimli_slab_alloc(&file->attrslab);
memset(attr, 0, sizeof(*attr));
attr->attr = atype;
attr->form = get_value(aform, addr_size, is_64, &data, end,
&attr->code, &attr->ptr, file->debug_info.elf);
if (attr->form == 0) {
printf("Failed to resolve value for attribute\n");
break;
}
if (attr->form == DW_FORM_addr) {
attr->code += file->debug_info.reloc;
} else if (attr->form == DW_FORM_ref_udata) {
/* offset from start of its respective CU */
// printf("ref CU, code is %" PRIx64, attr->code);
attr->code += (int64_t)(custart - file->debug_info.start);
// printf(" fixed up to %" PRIx64 "\n", attr->code);
attr->ptr = (const uint8_t*)cu;
attr->form = DW_FORM_data8;
}
attr->next = die->attrs;
die->attrs = attr;
attr = NULL;
}