forked from ruby/ruby
-
Notifications
You must be signed in to change notification settings - Fork 0
/
addr2line.c
2818 lines (2625 loc) · 75.6 KB
/
addr2line.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
/**********************************************************************
addr2line.c -
$Author$
Copyright (C) 2010 Shinichiro Hamaji
**********************************************************************/
#if defined(__clang__)
#pragma clang diagnostic ignored "-Wgnu-empty-initializer"
#pragma clang diagnostic ignored "-Wgcc-compat"
#endif
#include "ruby/internal/config.h"
#include "ruby/defines.h"
#include "ruby/missing.h"
#include "addr2line.h"
#include <stdio.h>
#include <errno.h>
#ifdef HAVE_LIBPROC_H
#include <libproc.h>
#endif
#include "ruby/internal/stdbool.h"
#if defined(USE_ELF) || defined(HAVE_MACH_O_LOADER_H)
#include <fcntl.h>
#include <limits.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
/* Make alloca work the best possible way. */
#ifdef __GNUC__
# ifndef alloca
# define alloca __builtin_alloca
# endif
#else
# ifdef HAVE_ALLOCA_H
# include <alloca.h>
# else
# ifdef _AIX
#pragma alloca
# else
# ifndef alloca /* predefined by HP cc +Olibcalls */
void *alloca();
# endif
# endif /* AIX */
# endif /* HAVE_ALLOCA_H */
#endif /* __GNUC__ */
#ifdef HAVE_DLADDR
# include <dlfcn.h>
#endif
#ifdef HAVE_MACH_O_LOADER_H
# include <crt_externs.h>
# include <mach-o/fat.h>
# include <mach-o/loader.h>
# include <mach-o/nlist.h>
# include <mach-o/stab.h>
#endif
#ifdef USE_ELF
# ifdef __OpenBSD__
# include <elf_abi.h>
# else
# include <elf.h>
# endif
#ifndef ElfW
# if SIZEOF_VOIDP == 8
# define ElfW(x) Elf64##_##x
# else
# define ElfW(x) Elf32##_##x
# endif
#endif
#ifndef ELF_ST_TYPE
# if SIZEOF_VOIDP == 8
# define ELF_ST_TYPE ELF64_ST_TYPE
# else
# define ELF_ST_TYPE ELF32_ST_TYPE
# endif
#endif
#endif
#ifdef SHF_COMPRESSED
# if defined(ELFCOMPRESS_ZLIB) && defined(HAVE_LIBZ)
/* FreeBSD 11.0 lacks ELFCOMPRESS_ZLIB */
# include <zlib.h>
# define SUPPORT_COMPRESSED_DEBUG_LINE
# endif
#else /* compatibility with glibc < 2.22 */
# define SHF_COMPRESSED 0
#endif
#ifndef PATH_MAX
#define PATH_MAX 4096
#endif
#define DW_LNS_copy 0x01
#define DW_LNS_advance_pc 0x02
#define DW_LNS_advance_line 0x03
#define DW_LNS_set_file 0x04
#define DW_LNS_set_column 0x05
#define DW_LNS_negate_stmt 0x06
#define DW_LNS_set_basic_block 0x07
#define DW_LNS_const_add_pc 0x08
#define DW_LNS_fixed_advance_pc 0x09
#define DW_LNS_set_prologue_end 0x0a /* DWARF3 */
#define DW_LNS_set_epilogue_begin 0x0b /* DWARF3 */
#define DW_LNS_set_isa 0x0c /* DWARF3 */
/* Line number extended opcode name. */
#define DW_LNE_end_sequence 0x01
#define DW_LNE_set_address 0x02
#define DW_LNE_define_file 0x03
#define DW_LNE_set_discriminator 0x04 /* DWARF4 */
PRINTF_ARGS(static int kprintf(const char *fmt, ...), 1, 2);
typedef struct line_info {
const char *dirname;
const char *filename;
const char *path; /* object path */
int line;
uintptr_t base_addr;
uintptr_t saddr;
const char *sname; /* function name */
struct line_info *next;
} line_info_t;
struct dwarf_section {
char *ptr;
size_t size;
uint64_t flags;
};
typedef struct obj_info {
const char *path; /* object path */
char *mapped;
size_t mapped_size;
void *uncompressed;
uintptr_t base_addr;
uintptr_t vmaddr;
struct dwarf_section debug_abbrev;
struct dwarf_section debug_info;
struct dwarf_section debug_line;
struct dwarf_section debug_ranges;
struct dwarf_section debug_rnglists;
struct dwarf_section debug_str;
struct obj_info *next;
} obj_info_t;
#define DWARF_SECTION_COUNT 6
static struct dwarf_section *
obj_dwarf_section_at(obj_info_t *obj, int n)
{
struct dwarf_section *ary[] = {
&obj->debug_abbrev,
&obj->debug_info,
&obj->debug_line,
&obj->debug_ranges,
&obj->debug_rnglists,
&obj->debug_str
};
if (n < 0 || DWARF_SECTION_COUNT <= n) {
abort();
}
return ary[n];
}
struct debug_section_definition {
const char *name;
struct dwarf_section *dwarf;
};
/* Avoid consuming stack as this module may be used from signal handler */
static char binary_filename[PATH_MAX + 1];
static unsigned long
uleb128(const char **p)
{
unsigned long r = 0;
int s = 0;
for (;;) {
unsigned char b = (unsigned char)*(*p)++;
if (b < 0x80) {
r += (unsigned long)b << s;
break;
}
r += (b & 0x7f) << s;
s += 7;
}
return r;
}
static long
sleb128(const char **p)
{
long r = 0;
int s = 0;
for (;;) {
unsigned char b = (unsigned char)*(*p)++;
if (b < 0x80) {
if (b & 0x40) {
r -= (0x80 - b) << s;
}
else {
r += (b & 0x3f) << s;
}
break;
}
r += (b & 0x7f) << s;
s += 7;
}
return r;
}
static const char *
get_nth_dirname(unsigned long dir, const char *p)
{
if (!dir--) {
return "";
}
while (dir--) {
while (*p) p++;
p++;
if (!*p) {
kprintf("Unexpected directory number %lu in %s\n",
dir, binary_filename);
return "";
}
}
return p;
}
static void
fill_filename(int file, const char *include_directories, const char *filenames, line_info_t *line, obj_info_t *obj)
{
int i;
const char *p = filenames;
const char *filename;
unsigned long dir;
for (i = 1; i <= file; i++) {
filename = p;
if (!*p) {
/* Need to output binary file name? */
kprintf("Unexpected file number %d in %s at %tx\n",
file, binary_filename, filenames - obj->mapped);
return;
}
while (*p) p++;
p++;
dir = uleb128(&p);
/* last modified. */
uleb128(&p);
/* size of the file. */
uleb128(&p);
if (i == file) {
line->filename = filename;
line->dirname = get_nth_dirname(dir, include_directories);
}
}
}
static void
fill_line(int num_traces, void **traces, uintptr_t addr, int file, int line,
const char *include_directories, const char *filenames,
obj_info_t *obj, line_info_t *lines, int offset)
{
int i;
addr += obj->base_addr - obj->vmaddr;
for (i = offset; i < num_traces; i++) {
uintptr_t a = (uintptr_t)traces[i];
/* We assume one line code doesn't result >100 bytes of native code.
We may want more reliable way eventually... */
if (addr < a && a < addr + 100) {
fill_filename(file, include_directories, filenames, &lines[i], obj);
lines[i].line = line;
}
}
}
struct LineNumberProgramHeader {
uint64_t unit_length;
uint16_t version;
uint8_t format; /* 4 or 8 */
uint64_t header_length;
uint8_t minimum_instruction_length;
uint8_t maximum_operations_per_instruction;
uint8_t default_is_stmt;
int8_t line_base;
uint8_t line_range;
uint8_t opcode_base;
/* uint8_t standard_opcode_lengths[opcode_base-1]; */
const char *include_directories;
const char *filenames;
const char *cu_start;
const char *cu_end;
};
static int
parse_debug_line_header(const char **pp, struct LineNumberProgramHeader *header)
{
const char *p = *pp;
header->unit_length = *(uint32_t *)p;
p += sizeof(uint32_t);
header->format = 4;
if (header->unit_length == 0xffffffff) {
header->unit_length = *(uint64_t *)p;
p += sizeof(uint64_t);
header->format = 8;
}
header->cu_end = p + header->unit_length;
header->version = *(uint16_t *)p;
p += sizeof(uint16_t);
if (header->version > 4) return -1;
header->header_length = header->format == 4 ? *(uint32_t *)p : *(uint64_t *)p;
p += header->format;
header->cu_start = p + header->header_length;
header->minimum_instruction_length = *(uint8_t *)p++;
if (header->version >= 4) {
/* maximum_operations_per_instruction = *(uint8_t *)p; */
if (*p != 1) return -1; /* For non-VLIW architectures, this field is 1 */
p++;
}
header->default_is_stmt = *(uint8_t *)p++;
header->line_base = *(int8_t *)p++;
header->line_range = *(uint8_t *)p++;
header->opcode_base = *(uint8_t *)p++;
/* header->standard_opcode_lengths = (uint8_t *)p - 1; */
p += header->opcode_base - 1;
header->include_directories = p;
/* temporary measure for compress-debug-sections */
if (p >= header->cu_end) return -1;
/* skip include directories */
while (*p) {
p = memchr(p, '\0', header->cu_end - p);
if (!p) return -1;
p++;
}
p++;
header->filenames = p;
*pp = header->cu_start;
return 0;
}
static int
parse_debug_line_cu(int num_traces, void **traces, const char **debug_line,
obj_info_t *obj, line_info_t *lines, int offset)
{
const char *p = (const char *)*debug_line;
struct LineNumberProgramHeader header;
/* The registers. */
unsigned long addr = 0;
unsigned int file = 1;
unsigned int line = 1;
/* unsigned int column = 0; */
int is_stmt;
/* int basic_block = 0; */
/* int end_sequence = 0; */
/* int prologue_end = 0; */
/* int epilogue_begin = 0; */
/* unsigned int isa = 0; */
if (parse_debug_line_header(&p, &header))
return -1;
is_stmt = header.default_is_stmt;
#define FILL_LINE() \
do { \
fill_line(num_traces, traces, addr, file, line, \
header.include_directories, \
header.filenames, \
obj, lines, offset); \
/*basic_block = prologue_end = epilogue_begin = 0;*/ \
} while (0)
while (p < header.cu_end) {
unsigned long a;
unsigned char op = *p++;
switch (op) {
case DW_LNS_copy:
FILL_LINE();
break;
case DW_LNS_advance_pc:
a = uleb128(&p) * header.minimum_instruction_length;
addr += a;
break;
case DW_LNS_advance_line: {
long a = sleb128(&p);
line += a;
break;
}
case DW_LNS_set_file:
file = (unsigned int)uleb128(&p);
break;
case DW_LNS_set_column:
/*column = (unsigned int)*/(void)uleb128(&p);
break;
case DW_LNS_negate_stmt:
is_stmt = !is_stmt;
break;
case DW_LNS_set_basic_block:
/*basic_block = 1; */
break;
case DW_LNS_const_add_pc:
a = ((255UL - header.opcode_base) / header.line_range) *
header.minimum_instruction_length;
addr += a;
break;
case DW_LNS_fixed_advance_pc:
a = *(uint16_t *)p;
p += sizeof(uint16_t);
addr += a;
break;
case DW_LNS_set_prologue_end:
/* prologue_end = 1; */
break;
case DW_LNS_set_epilogue_begin:
/* epilogue_begin = 1; */
break;
case DW_LNS_set_isa:
/* isa = (unsigned int)*/(void)uleb128(&p);
break;
case 0:
a = uleb128(&p);
op = *p++;
switch (op) {
case DW_LNE_end_sequence:
/* end_sequence = 1; */
FILL_LINE();
addr = 0;
file = 1;
line = 1;
/* column = 0; */
is_stmt = header.default_is_stmt;
/* end_sequence = 0; */
/* isa = 0; */
break;
case DW_LNE_set_address:
addr = *(unsigned long *)p;
p += sizeof(unsigned long);
break;
case DW_LNE_define_file:
kprintf("Unsupported operation in %s\n",
binary_filename);
break;
case DW_LNE_set_discriminator:
/* TODO:currently ignore */
uleb128(&p);
break;
default:
kprintf("Unknown extended opcode: %d in %s\n",
op, binary_filename);
}
break;
default: {
uint8_t adjusted_opcode = op - header.opcode_base;
uint8_t operation_advance = adjusted_opcode / header.line_range;
/* NOTE: this code doesn't support VLIW */
addr += operation_advance * header.minimum_instruction_length;
line += header.line_base + (adjusted_opcode % header.line_range);
FILL_LINE();
}
}
}
*debug_line = (char *)p;
return 0;
}
static int
parse_debug_line(int num_traces, void **traces,
const char *debug_line, unsigned long size,
obj_info_t *obj, line_info_t *lines, int offset)
{
const char *debug_line_end = debug_line + size;
while (debug_line < debug_line_end) {
if (parse_debug_line_cu(num_traces, traces, &debug_line, obj, lines, offset))
return -1;
}
if (debug_line != debug_line_end) {
kprintf("Unexpected size of .debug_line in %s\n",
binary_filename);
}
return 0;
}
/* read file and fill lines */
static uintptr_t
fill_lines(int num_traces, void **traces, int check_debuglink,
obj_info_t **objp, line_info_t *lines, int offset);
static void
append_obj(obj_info_t **objp)
{
obj_info_t *newobj = calloc(1, sizeof(obj_info_t));
if (*objp) (*objp)->next = newobj;
*objp = newobj;
}
#ifdef USE_ELF
/* Ideally we should check 4 paths to follow gnu_debuglink:
*
* - /usr/lib/debug/.build-id/ab/cdef1234.debug
* - /usr/bin/ruby.debug
* - /usr/bin/.debug/ruby.debug
* - /usr/lib/debug/usr/bin/ruby.debug.
*
* but we handle only two cases for now as the two formats are
* used by some linux distributions.
*
* See GDB's info for detail.
* https://sourceware.org/gdb/onlinedocs/gdb/Separate-Debug-Files.html
*/
// check the path pattern of "/usr/lib/debug/usr/bin/ruby.debug"
static void
follow_debuglink(const char *debuglink, int num_traces, void **traces,
obj_info_t **objp, line_info_t *lines, int offset)
{
static const char global_debug_dir[] = "/usr/lib/debug";
const size_t global_debug_dir_len = sizeof(global_debug_dir) - 1;
char *p;
obj_info_t *o1 = *objp, *o2;
size_t len;
p = strrchr(binary_filename, '/');
if (!p) {
return;
}
p[1] = '\0';
len = strlen(binary_filename);
if (len >= PATH_MAX - global_debug_dir_len)
len = PATH_MAX - global_debug_dir_len - 1;
memmove(binary_filename + global_debug_dir_len, binary_filename, len);
memcpy(binary_filename, global_debug_dir, global_debug_dir_len);
len += global_debug_dir_len;
strlcpy(binary_filename + len, debuglink, PATH_MAX - len);
append_obj(objp);
o2 = *objp;
o2->base_addr = o1->base_addr;
o2->path = o1->path;
fill_lines(num_traces, traces, 0, objp, lines, offset);
}
// check the path pattern of "/usr/lib/debug/.build-id/ab/cdef1234.debug"
static void
follow_debuglink_build_id(const char *build_id, size_t build_id_size, int num_traces, void **traces,
obj_info_t **objp, line_info_t *lines, int offset)
{
static const char global_debug_dir[] = "/usr/lib/debug/.build-id/";
const size_t global_debug_dir_len = sizeof(global_debug_dir) - 1;
char *p;
obj_info_t *o1 = *objp, *o2;
size_t i;
if (PATH_MAX < global_debug_dir_len + 1 + build_id_size * 2 + 6) return;
memcpy(binary_filename, global_debug_dir, global_debug_dir_len);
p = binary_filename + global_debug_dir_len;
for (i = 0; i < build_id_size; i++) {
static const char tbl[] = "0123456789abcdef";
unsigned char n = build_id[i];
*p++ = tbl[n / 16];
*p++ = tbl[n % 16];
if (i == 0) *p++ = '/';
}
strcpy(p, ".debug");
append_obj(objp);
o2 = *objp;
o2->base_addr = o1->base_addr;
o2->path = o1->path;
fill_lines(num_traces, traces, 0, objp, lines, offset);
}
#endif
enum
{
DW_TAG_compile_unit = 0x11,
DW_TAG_inlined_subroutine = 0x1d,
DW_TAG_subprogram = 0x2e,
};
/* Attributes encodings */
enum
{
DW_AT_sibling = 0x01,
DW_AT_location = 0x02,
DW_AT_name = 0x03,
/* Reserved 0x04 */
/* Reserved 0x05 */
/* Reserved 0x06 */
/* Reserved 0x07 */
/* Reserved 0x08 */
DW_AT_ordering = 0x09,
/* Reserved 0x0a */
DW_AT_byte_size = 0x0b,
/* Reserved 0x0c */
DW_AT_bit_size = 0x0d,
/* Reserved 0x0e */
/* Reserved 0x0f */
DW_AT_stmt_list = 0x10,
DW_AT_low_pc = 0x11,
DW_AT_high_pc = 0x12,
DW_AT_language = 0x13,
/* Reserved 0x14 */
DW_AT_discr = 0x15,
DW_AT_discr_value = 0x16,
DW_AT_visibility = 0x17,
DW_AT_import = 0x18,
DW_AT_string_length = 0x19,
DW_AT_common_reference = 0x1a,
DW_AT_comp_dir = 0x1b,
DW_AT_const_value = 0x1c,
DW_AT_containing_type = 0x1d,
DW_AT_default_value = 0x1e,
/* Reserved 0x1f */
DW_AT_inline = 0x20,
DW_AT_is_optional = 0x21,
DW_AT_lower_bound = 0x22,
/* Reserved 0x23 */
/* Reserved 0x24 */
DW_AT_producer = 0x25,
/* Reserved 0x26 */
DW_AT_prototyped = 0x27,
/* Reserved 0x28 */
/* Reserved 0x29 */
DW_AT_return_addr = 0x2a,
/* Reserved 0x2b */
DW_AT_start_scope = 0x2c,
/* Reserved 0x2d */
DW_AT_bit_stride = 0x2e,
DW_AT_upper_bound = 0x2f,
/* Reserved 0x30 */
DW_AT_abstract_origin = 0x31,
DW_AT_accessibility = 0x32,
DW_AT_address_class = 0x33,
DW_AT_artificial = 0x34,
DW_AT_base_types = 0x35,
DW_AT_calling_convention = 0x36,
DW_AT_count = 0x37,
DW_AT_data_member_location = 0x38,
DW_AT_decl_column = 0x39,
DW_AT_decl_file = 0x3a,
DW_AT_decl_line = 0x3b,
DW_AT_declaration = 0x3c,
DW_AT_discr_list = 0x3d,
DW_AT_encoding = 0x3e,
DW_AT_external = 0x3f,
DW_AT_frame_base = 0x40,
DW_AT_friend = 0x41,
DW_AT_identifier_case = 0x42,
/* Reserved 0x43 */
DW_AT_namelist_item = 0x44,
DW_AT_priority = 0x45,
DW_AT_segment = 0x46,
DW_AT_specification = 0x47,
DW_AT_static_link = 0x48,
DW_AT_type = 0x49,
DW_AT_use_location = 0x4a,
DW_AT_variable_parameter = 0x4b,
DW_AT_virtuality = 0x4c,
DW_AT_vtable_elem_location = 0x4d,
DW_AT_allocated = 0x4e,
DW_AT_associated = 0x4f,
DW_AT_data_location = 0x50,
DW_AT_byte_stride = 0x51,
DW_AT_entry_pc = 0x52,
DW_AT_use_UTF8 = 0x53,
DW_AT_extension = 0x54,
DW_AT_ranges = 0x55,
DW_AT_trampoline = 0x56,
DW_AT_call_column = 0x57,
DW_AT_call_file = 0x58,
DW_AT_call_line = 0x59,
DW_AT_description = 0x5a,
DW_AT_binary_scale = 0x5b,
DW_AT_decimal_scale = 0x5c,
DW_AT_small = 0x5d,
DW_AT_decimal_sign = 0x5e,
DW_AT_digit_count = 0x5f,
DW_AT_picture_string = 0x60,
DW_AT_mutable = 0x61,
DW_AT_threads_scaled = 0x62,
DW_AT_explicit = 0x63,
DW_AT_object_pointer = 0x64,
DW_AT_endianity = 0x65,
DW_AT_elemental = 0x66,
DW_AT_pure = 0x67,
DW_AT_recursive = 0x68,
DW_AT_signature = 0x69,
DW_AT_main_subprogram = 0x6a,
DW_AT_data_bit_offset = 0x6b,
DW_AT_const_expr = 0x6c,
DW_AT_enum_class = 0x6d,
DW_AT_linkage_name = 0x6e,
DW_AT_string_length_bit_size = 0x6f,
DW_AT_string_length_byte_size = 0x70,
DW_AT_rank = 0x71,
DW_AT_str_offsets_base = 0x72,
DW_AT_addr_base = 0x73,
DW_AT_rnglists_base = 0x74,
/* Reserved 0x75 */
DW_AT_dwo_name = 0x76,
DW_AT_reference = 0x77,
DW_AT_rvalue_reference = 0x78,
DW_AT_macros = 0x79,
DW_AT_call_all_calls = 0x7a,
DW_AT_call_all_source_calls = 0x7b,
DW_AT_call_all_tail_calls = 0x7c,
DW_AT_call_return_pc = 0x7d,
DW_AT_call_value = 0x7e,
DW_AT_call_origin = 0x7f,
DW_AT_call_parameter = 0x80,
DW_AT_call_pc = 0x81,
DW_AT_call_tail_call = 0x82,
DW_AT_call_target = 0x83,
DW_AT_call_target_clobbered = 0x84,
DW_AT_call_data_location = 0x85,
DW_AT_call_data_value = 0x86,
DW_AT_noreturn = 0x87,
DW_AT_alignment = 0x88,
DW_AT_export_symbols = 0x89,
DW_AT_deleted = 0x8a,
DW_AT_defaulted = 0x8b,
DW_AT_loclists_base = 0x8c,
DW_AT_lo_user = 0x2000,
DW_AT_hi_user = 0x3fff
};
/* Attribute form encodings */
enum
{
DW_FORM_addr = 0x01,
/* Reserved 0x02 */
DW_FORM_block2 = 0x03,
DW_FORM_block4 = 0x04,
DW_FORM_data2 = 0x05,
DW_FORM_data4 = 0x06,
DW_FORM_data8 = 0x07,
DW_FORM_string = 0x08,
DW_FORM_block = 0x09,
DW_FORM_block1 = 0x0a,
DW_FORM_data1 = 0x0b,
DW_FORM_flag = 0x0c,
DW_FORM_sdata = 0x0d,
DW_FORM_strp = 0x0e,
DW_FORM_udata = 0x0f,
DW_FORM_ref_addr = 0x10,
DW_FORM_ref1 = 0x11,
DW_FORM_ref2 = 0x12,
DW_FORM_ref4 = 0x13,
DW_FORM_ref8 = 0x14,
DW_FORM_ref_udata = 0x15,
DW_FORM_indirect = 0x16,
DW_FORM_sec_offset = 0x17,
DW_FORM_exprloc = 0x18,
DW_FORM_flag_present = 0x19,
DW_FORM_strx = 0x1a,
DW_FORM_addrx = 0x1b,
DW_FORM_ref_sup4 = 0x1c,
DW_FORM_strp_sup = 0x1d,
DW_FORM_data16 = 0x1e,
DW_FORM_line_strp = 0x1f,
DW_FORM_ref_sig8 = 0x20,
DW_FORM_implicit_const = 0x21,
DW_FORM_loclistx = 0x22,
DW_FORM_rnglistx = 0x23,
DW_FORM_ref_sup8 = 0x24,
DW_FORM_strx1 = 0x25,
DW_FORM_strx2 = 0x26,
DW_FORM_strx3 = 0x27,
DW_FORM_strx4 = 0x28,
DW_FORM_addrx1 = 0x29,
DW_FORM_addrx2 = 0x2a,
DW_FORM_addrx3 = 0x2b,
DW_FORM_addrx4 = 0x2c
};
/* Range list entry encodings */
enum {
DW_RLE_end_of_list = 0x00,
DW_RLE_base_addressx = 0x01,
DW_RLE_startx_endx = 0x02,
DW_RLE_startx_length = 0x03,
DW_RLE_offset_pair = 0x04,
DW_RLE_base_address = 0x05,
DW_RLE_start_end = 0x06,
DW_RLE_start_length = 0x07
};
enum {
VAL_none = 0,
VAL_cstr = 1,
VAL_data = 2,
VAL_uint = 3,
VAL_int = 4
};
# define ABBREV_TABLE_SIZE 256
typedef struct {
obj_info_t *obj;
const char *file;
const char *current_cu;
uint64_t current_low_pc;
const char *debug_line_cu_end;
const char *debug_line_files;
const char *debug_line_directories;
const char *p;
const char *cu_end;
const char *pend;
const char *q0;
const char *q;
int format; // 4 or 8
uint8_t address_size;
int level;
const char *abbrev_table[ABBREV_TABLE_SIZE];
} DebugInfoReader;
typedef struct {
ptrdiff_t pos;
int tag;
int has_children;
} DIE;
typedef struct {
union {
const char *ptr;
uint64_t uint64;
int64_t int64;
} as;
uint64_t off;
uint64_t at;
uint64_t form;
size_t size;
int type;
} DebugInfoValue;
/* TODO: Big Endian */
#define MERGE_2INTS(a,b,sz) (((uint64_t)(b)<<sz)|(a))
static uint16_t
get_uint16(const uint8_t *p)
{
return (uint16_t)MERGE_2INTS(p[0],p[1],8);
}
static uint32_t
get_uint32(const uint8_t *p)
{
return (uint32_t)MERGE_2INTS(get_uint16(p),get_uint16(p+2),16);
}
static uint64_t
get_uint64(const uint8_t *p)
{
return MERGE_2INTS(get_uint32(p),get_uint32(p+4),32);
}
static uint8_t
read_uint8(const char **ptr)
{
const char *p = *ptr;
*ptr = (p + 1);
return (uint8_t)*p;
}
static uint16_t
read_uint16(const char **ptr)
{
const char *p = *ptr;
*ptr = (p + 2);
return get_uint16((const uint8_t *)p);
}
static uint32_t
read_uint24(const char **ptr)
{
const char *p = *ptr;
*ptr = (p + 3);
return ((uint8_t)*p << 16) | get_uint16((const uint8_t *)p+1);
}
static uint32_t
read_uint32(const char **ptr)
{
const char *p = *ptr;
*ptr = (p + 4);
return get_uint32((const uint8_t *)p);
}
static uint64_t
read_uint64(const char **ptr)
{
const unsigned char *p = (const unsigned char *)*ptr;
*ptr = (char *)(p + 8);
return get_uint64(p);
}
static uintptr_t
read_uintptr(const char **ptr)
{
const unsigned char *p = (const unsigned char *)*ptr;
*ptr = (char *)(p + SIZEOF_VOIDP);
#if SIZEOF_VOIDP == 8
return get_uint64(p);
#else
return get_uint32(p);
#endif
}
static uint64_t
read_uint(DebugInfoReader *reader)
{
if (reader->format == 4) {
return read_uint32(&reader->p);
} else { /* 64 bit */
return read_uint64(&reader->p);
}
}
static uint64_t
read_uleb128(DebugInfoReader *reader)
{
return uleb128(&reader->p);
}
static int64_t
read_sleb128(DebugInfoReader *reader)
{
return sleb128(&reader->p);
}
static void
debug_info_reader_init(DebugInfoReader *reader, obj_info_t *obj)
{
reader->file = obj->mapped;
reader->obj = obj;
reader->p = obj->debug_info.ptr;
reader->pend = obj->debug_info.ptr + obj->debug_info.size;
reader->debug_line_cu_end = obj->debug_line.ptr;
reader->current_low_pc = 0;
}
static void
di_skip_die_attributes(const char **p)
{
for (;;) {
uint64_t at = uleb128(p);
uint64_t form = uleb128(p);
if (!at && !form) break;
switch (form) {
default:
break;
case DW_FORM_implicit_const:
sleb128(p);
break;
}
}
}
static void
di_read_debug_abbrev_cu(DebugInfoReader *reader)
{
uint64_t prev = 0;
const char *p = reader->q0;
for (;;) {