Tempfile document updated.
[ruby.git] / addr2line.c
blob02a3e617a6b80fda987fb22388bf41968ce81773
1 /**********************************************************************
3 addr2line.c -
5 $Author$
7 Copyright (C) 2010 Shinichiro Hamaji
9 **********************************************************************/
11 #if defined(__clang__) && defined(__has_warning)
12 #if __has_warning("-Wgnu-empty-initializer")
13 #pragma clang diagnostic ignored "-Wgnu-empty-initializer"
14 #endif
15 #if __has_warning("-Wgcc-compat")
16 #pragma clang diagnostic ignored "-Wgcc-compat"
17 #endif
18 #endif
20 #include "ruby/internal/config.h"
21 #include "ruby/defines.h"
22 #include "ruby/missing.h"
23 #include "addr2line.h"
25 #include <stdio.h>
26 #include <errno.h>
28 #ifdef HAVE_LIBPROC_H
29 #include <libproc.h>
30 #endif
32 #include "ruby/internal/stdbool.h"
34 #if defined(USE_ELF) || defined(HAVE_MACH_O_LOADER_H)
36 #include <fcntl.h>
37 #include <limits.h>
38 #include <stdio.h>
39 #include <stdint.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <sys/mman.h>
43 #include <sys/types.h>
44 #include <sys/stat.h>
45 #include <unistd.h>
47 /* Make alloca work the best possible way. */
48 #ifdef __GNUC__
49 # ifndef alloca
50 # define alloca __builtin_alloca
51 # endif
52 #else
53 # ifdef HAVE_ALLOCA_H
54 # include <alloca.h>
55 # else
56 # ifdef _AIX
57 #pragma alloca
58 # else
59 # ifndef alloca /* predefined by HP cc +Olibcalls */
60 void *alloca();
61 # endif
62 # endif /* AIX */
63 # endif /* HAVE_ALLOCA_H */
64 # ifndef UNREACHABLE
65 # define UNREACHABLE __builtin_unreachable()
66 # endif
67 # ifndef UNREACHABLE_RETURN
68 # define UNREACHABLE_RETURN(_) __builtin_unreachable()
69 # endif
70 #endif /* __GNUC__ */
72 #ifndef UNREACHABLE
73 # define UNREACHABLE abort()
74 #endif
75 #ifndef UNREACHABLE_RETURN
76 # define UNREACHABLE_RETURN(_) return (abort(), (_))
77 #endif
79 #ifdef HAVE_DLADDR
80 # include <dlfcn.h>
81 #endif
83 #ifdef HAVE_MACH_O_LOADER_H
84 # include <crt_externs.h>
85 # include <mach-o/fat.h>
86 # include <mach-o/loader.h>
87 # include <mach-o/nlist.h>
88 # include <mach-o/stab.h>
89 #endif
91 #ifdef USE_ELF
92 # ifdef __OpenBSD__
93 # include <elf_abi.h>
94 # else
95 # include <elf.h>
96 # endif
98 #ifndef ElfW
99 # if SIZEOF_VOIDP == 8
100 # define ElfW(x) Elf64##_##x
101 # else
102 # define ElfW(x) Elf32##_##x
103 # endif
104 #endif
105 #ifndef ELF_ST_TYPE
106 # if SIZEOF_VOIDP == 8
107 # define ELF_ST_TYPE ELF64_ST_TYPE
108 # else
109 # define ELF_ST_TYPE ELF32_ST_TYPE
110 # endif
111 #endif
112 #endif
114 #ifdef SHF_COMPRESSED
115 # if defined(ELFCOMPRESS_ZLIB) && defined(HAVE_LIBZ)
116 /* FreeBSD 11.0 lacks ELFCOMPRESS_ZLIB */
117 # include <zlib.h>
118 # define SUPPORT_COMPRESSED_DEBUG_LINE
119 # endif
120 #else /* compatibility with glibc < 2.22 */
121 # define SHF_COMPRESSED 0
122 #endif
124 #ifndef PATH_MAX
125 #define PATH_MAX 4096
126 #endif
128 #define DW_LNS_copy 0x01
129 #define DW_LNS_advance_pc 0x02
130 #define DW_LNS_advance_line 0x03
131 #define DW_LNS_set_file 0x04
132 #define DW_LNS_set_column 0x05
133 #define DW_LNS_negate_stmt 0x06
134 #define DW_LNS_set_basic_block 0x07
135 #define DW_LNS_const_add_pc 0x08
136 #define DW_LNS_fixed_advance_pc 0x09
137 #define DW_LNS_set_prologue_end 0x0a /* DWARF3 */
138 #define DW_LNS_set_epilogue_begin 0x0b /* DWARF3 */
139 #define DW_LNS_set_isa 0x0c /* DWARF3 */
141 /* Line number extended opcode name. */
142 #define DW_LNE_end_sequence 0x01
143 #define DW_LNE_set_address 0x02
144 #define DW_LNE_define_file 0x03
145 #define DW_LNE_set_discriminator 0x04 /* DWARF4 */
147 #define kprintf(...) fprintf(errout, "" __VA_ARGS__)
149 typedef struct line_info {
150 const char *dirname;
151 const char *filename;
152 const char *path; /* object path */
153 int line;
155 uintptr_t base_addr;
156 uintptr_t saddr;
157 const char *sname; /* function name */
159 struct line_info *next;
160 } line_info_t;
162 struct dwarf_section {
163 char *ptr;
164 size_t size;
165 uint64_t flags;
168 typedef struct obj_info {
169 const char *path; /* object path */
170 char *mapped;
171 size_t mapped_size;
172 void *uncompressed;
173 uintptr_t base_addr;
174 uintptr_t vmaddr;
175 struct dwarf_section debug_abbrev;
176 struct dwarf_section debug_info;
177 struct dwarf_section debug_line;
178 struct dwarf_section debug_ranges;
179 struct dwarf_section debug_str_offsets;
180 struct dwarf_section debug_addr;
181 struct dwarf_section debug_rnglists;
182 struct dwarf_section debug_str;
183 struct dwarf_section debug_line_str;
184 struct obj_info *next;
185 } obj_info_t;
187 #define DWARF_SECTION_COUNT 9
189 static struct dwarf_section *
190 obj_dwarf_section_at(obj_info_t *obj, int n)
192 struct dwarf_section *ary[] = {
193 &obj->debug_abbrev,
194 &obj->debug_info,
195 &obj->debug_line,
196 &obj->debug_ranges,
197 &obj->debug_str_offsets,
198 &obj->debug_addr,
199 &obj->debug_rnglists,
200 &obj->debug_str,
201 &obj->debug_line_str
203 if (n < 0 || DWARF_SECTION_COUNT <= n) {
204 UNREACHABLE_RETURN(0);
206 return ary[n];
209 struct debug_section_definition {
210 const char *name;
211 struct dwarf_section *dwarf;
214 /* Avoid consuming stack as this module may be used from signal handler */
215 static char binary_filename[PATH_MAX + 1];
217 static unsigned long
218 uleb128(const char **p)
220 unsigned long r = 0;
221 int s = 0;
222 for (;;) {
223 unsigned char b = (unsigned char)*(*p)++;
224 if (b < 0x80) {
225 r += (unsigned long)b << s;
226 break;
228 r += (b & 0x7f) << s;
229 s += 7;
231 return r;
234 static long
235 sleb128(const char **p)
237 long r = 0;
238 int s = 0;
239 for (;;) {
240 unsigned char b = (unsigned char)*(*p)++;
241 if (b < 0x80) {
242 if (b & 0x40) {
243 r -= (0x80 - b) << s;
245 else {
246 r += (b & 0x3f) << s;
248 break;
250 r += (b & 0x7f) << s;
251 s += 7;
253 return r;
256 static const char *
257 get_nth_dirname(unsigned long dir, const char *p, FILE *errout)
259 if (!dir--) {
260 return "";
262 while (dir--) {
263 while (*p) p++;
264 p++;
265 if (!*p) {
266 kprintf("Unexpected directory number %lu in %s\n",
267 dir, binary_filename);
268 return "";
271 return p;
274 static const char *parse_ver5_debug_line_header(
275 const char *p, int idx, uint8_t format,
276 obj_info_t *obj, const char **out_path,
277 uint64_t *out_directory_index, FILE *errout);
279 static void
280 fill_filename(int file, uint8_t format, uint16_t version, const char *include_directories,
281 const char *filenames, line_info_t *line, obj_info_t *obj, FILE *errout)
283 int i;
284 const char *p = filenames;
285 const char *filename;
286 unsigned long dir;
287 if (version >= 5) {
288 const char *path;
289 uint64_t directory_index = -1;
290 parse_ver5_debug_line_header(filenames, file, format, obj, &path, &directory_index, errout);
291 line->filename = path;
292 parse_ver5_debug_line_header(include_directories, (int)directory_index, format, obj, &path, NULL, errout);
293 line->dirname = path;
295 else {
296 for (i = 1; i <= file; i++) {
297 filename = p;
298 if (!*p) {
299 /* Need to output binary file name? */
300 kprintf("Unexpected file number %d in %s at %tx\n",
301 file, binary_filename, filenames - obj->mapped);
302 return;
304 while (*p) p++;
305 p++;
306 dir = uleb128(&p);
307 /* last modified. */
308 uleb128(&p);
309 /* size of the file. */
310 uleb128(&p);
312 if (i == file) {
313 line->filename = filename;
314 line->dirname = get_nth_dirname(dir, include_directories, errout);
320 static void
321 fill_line(int num_traces, void **traces, uintptr_t addr, int file, int line,
322 uint8_t format, uint16_t version, const char *include_directories, const char *filenames,
323 obj_info_t *obj, line_info_t *lines, int offset, FILE *errout)
325 int i;
326 addr += obj->base_addr - obj->vmaddr;
327 for (i = offset; i < num_traces; i++) {
328 uintptr_t a = (uintptr_t)traces[i];
329 /* We assume one line code doesn't result >100 bytes of native code.
330 We may want more reliable way eventually... */
331 if (addr < a && a < addr + 100) {
332 fill_filename(file, format, version, include_directories, filenames, &lines[i], obj, errout);
333 lines[i].line = line;
338 struct LineNumberProgramHeader {
339 uint64_t unit_length;
340 uint16_t version;
341 uint8_t format; /* 4 or 8 */
342 uint64_t header_length;
343 uint8_t minimum_instruction_length;
344 uint8_t maximum_operations_per_instruction;
345 uint8_t default_is_stmt;
346 int8_t line_base;
347 uint8_t line_range;
348 uint8_t opcode_base;
349 /* uint8_t standard_opcode_lengths[opcode_base-1]; */
350 const char *include_directories;
351 const char *filenames;
352 const char *cu_start;
353 const char *cu_end;
356 static int
357 parse_debug_line_header(obj_info_t *obj, const char **pp, struct LineNumberProgramHeader *header, FILE *errout)
359 const char *p = *pp;
360 header->unit_length = *(uint32_t *)p;
361 p += sizeof(uint32_t);
363 header->format = 4;
364 if (header->unit_length == 0xffffffff) {
365 header->unit_length = *(uint64_t *)p;
366 p += sizeof(uint64_t);
367 header->format = 8;
370 header->cu_end = p + header->unit_length;
372 header->version = *(uint16_t *)p;
373 p += sizeof(uint16_t);
374 if (header->version > 5) return -1;
376 if (header->version >= 5) {
377 /* address_size = *(uint8_t *)p++; */
378 /* segment_selector_size = *(uint8_t *)p++; */
379 p += 2;
382 header->header_length = header->format == 4 ? *(uint32_t *)p : *(uint64_t *)p;
383 p += header->format;
384 header->cu_start = p + header->header_length;
386 header->minimum_instruction_length = *(uint8_t *)p++;
388 if (header->version >= 4) {
389 /* maximum_operations_per_instruction = *(uint8_t *)p; */
390 if (*p != 1) return -1; /* For non-VLIW architectures, this field is 1 */
391 p++;
394 header->default_is_stmt = *(uint8_t *)p++;
395 header->line_base = *(int8_t *)p++;
396 header->line_range = *(uint8_t *)p++;
397 header->opcode_base = *(uint8_t *)p++;
398 /* header->standard_opcode_lengths = (uint8_t *)p - 1; */
399 p += header->opcode_base - 1;
401 if (header->version >= 5) {
402 header->include_directories = p;
403 p = parse_ver5_debug_line_header(p, -1, header->format, obj, NULL, NULL, errout);
404 header->filenames = p;
406 else {
407 header->include_directories = p;
409 /* temporary measure for compress-debug-sections */
410 if (p >= header->cu_end) return -1;
412 /* skip include directories */
413 while (*p) {
414 p = memchr(p, '\0', header->cu_end - p);
415 if (!p) return -1;
416 p++;
418 p++;
420 header->filenames = p;
423 *pp = header->cu_start;
425 return 0;
428 static int
429 parse_debug_line_cu(int num_traces, void **traces, const char **debug_line,
430 obj_info_t *obj, line_info_t *lines, int offset, FILE *errout)
432 const char *p = (const char *)*debug_line;
433 struct LineNumberProgramHeader header;
435 /* The registers. */
436 unsigned long addr = 0;
437 unsigned int file = 1;
438 unsigned int line = 1;
439 /* unsigned int column = 0; */
440 int is_stmt;
441 /* int basic_block = 0; */
442 /* int end_sequence = 0; */
443 /* int prologue_end = 0; */
444 /* int epilogue_begin = 0; */
445 /* unsigned int isa = 0; */
447 if (parse_debug_line_header(obj, &p, &header, errout))
448 return -1;
449 is_stmt = header.default_is_stmt;
451 #define FILL_LINE() \
452 do { \
453 fill_line(num_traces, traces, addr, file, line, \
454 header.format, \
455 header.version, \
456 header.include_directories, \
457 header.filenames, \
458 obj, lines, offset, errout); \
459 /*basic_block = prologue_end = epilogue_begin = 0;*/ \
460 } while (0)
462 while (p < header.cu_end) {
463 unsigned long a;
464 unsigned char op = *p++;
465 switch (op) {
466 case DW_LNS_copy:
467 FILL_LINE();
468 break;
469 case DW_LNS_advance_pc:
470 a = uleb128(&p) * header.minimum_instruction_length;
471 addr += a;
472 break;
473 case DW_LNS_advance_line: {
474 long a = sleb128(&p);
475 line += a;
476 break;
478 case DW_LNS_set_file:
479 file = (unsigned int)uleb128(&p);
480 break;
481 case DW_LNS_set_column:
482 /*column = (unsigned int)*/(void)uleb128(&p);
483 break;
484 case DW_LNS_negate_stmt:
485 is_stmt = !is_stmt;
486 break;
487 case DW_LNS_set_basic_block:
488 /*basic_block = 1; */
489 break;
490 case DW_LNS_const_add_pc:
491 a = ((255UL - header.opcode_base) / header.line_range) *
492 header.minimum_instruction_length;
493 addr += a;
494 break;
495 case DW_LNS_fixed_advance_pc:
496 a = *(uint16_t *)p;
497 p += sizeof(uint16_t);
498 addr += a;
499 break;
500 case DW_LNS_set_prologue_end:
501 /* prologue_end = 1; */
502 break;
503 case DW_LNS_set_epilogue_begin:
504 /* epilogue_begin = 1; */
505 break;
506 case DW_LNS_set_isa:
507 /* isa = (unsigned int)*/(void)uleb128(&p);
508 break;
509 case 0:
510 a = uleb128(&p);
511 op = *p++;
512 switch (op) {
513 case DW_LNE_end_sequence:
514 /* end_sequence = 1; */
515 FILL_LINE();
516 addr = 0;
517 file = 1;
518 line = 1;
519 /* column = 0; */
520 is_stmt = header.default_is_stmt;
521 /* end_sequence = 0; */
522 /* isa = 0; */
523 break;
524 case DW_LNE_set_address:
525 addr = *(unsigned long *)p;
526 p += sizeof(unsigned long);
527 break;
528 case DW_LNE_define_file:
529 kprintf("Unsupported operation in %s\n",
530 binary_filename);
531 break;
532 case DW_LNE_set_discriminator:
533 /* TODO:currently ignore */
534 uleb128(&p);
535 break;
536 default:
537 kprintf("Unknown extended opcode: %d in %s\n",
538 op, binary_filename);
540 break;
541 default: {
542 uint8_t adjusted_opcode = op - header.opcode_base;
543 uint8_t operation_advance = adjusted_opcode / header.line_range;
544 /* NOTE: this code doesn't support VLIW */
545 addr += operation_advance * header.minimum_instruction_length;
546 line += header.line_base + (adjusted_opcode % header.line_range);
547 FILL_LINE();
551 *debug_line = (char *)p;
552 return 0;
555 static int
556 parse_debug_line(int num_traces, void **traces,
557 const char *debug_line, unsigned long size,
558 obj_info_t *obj, line_info_t *lines, int offset, FILE *errout)
560 const char *debug_line_end = debug_line + size;
561 while (debug_line < debug_line_end) {
562 if (parse_debug_line_cu(num_traces, traces, &debug_line, obj, lines, offset, errout))
563 return -1;
565 if (debug_line != debug_line_end) {
566 kprintf("Unexpected size of .debug_line in %s\n",
567 binary_filename);
569 return 0;
572 /* read file and fill lines */
573 static uintptr_t
574 fill_lines(int num_traces, void **traces, int check_debuglink,
575 obj_info_t **objp, line_info_t *lines, int offset, FILE *errout);
577 static void
578 append_obj(obj_info_t **objp)
580 obj_info_t *newobj = calloc(1, sizeof(obj_info_t));
581 if (*objp) (*objp)->next = newobj;
582 *objp = newobj;
585 #ifdef USE_ELF
586 /* Ideally we should check 4 paths to follow gnu_debuglink:
588 * - /usr/lib/debug/.build-id/ab/cdef1234.debug
589 * - /usr/bin/ruby.debug
590 * - /usr/bin/.debug/ruby.debug
591 * - /usr/lib/debug/usr/bin/ruby.debug.
593 * but we handle only two cases for now as the two formats are
594 * used by some linux distributions.
596 * See GDB's info for detail.
597 * https://sourceware.org/gdb/onlinedocs/gdb/Separate-Debug-Files.html
600 // check the path pattern of "/usr/lib/debug/usr/bin/ruby.debug"
601 static void
602 follow_debuglink(const char *debuglink, int num_traces, void **traces,
603 obj_info_t **objp, line_info_t *lines, int offset, FILE *errout)
605 static const char global_debug_dir[] = "/usr/lib/debug";
606 const size_t global_debug_dir_len = sizeof(global_debug_dir) - 1;
607 char *p;
608 obj_info_t *o1 = *objp, *o2;
609 size_t len;
611 p = strrchr(binary_filename, '/');
612 if (!p) {
613 return;
615 p[1] = '\0';
617 len = strlen(binary_filename);
618 if (len >= PATH_MAX - global_debug_dir_len)
619 len = PATH_MAX - global_debug_dir_len - 1;
620 memmove(binary_filename + global_debug_dir_len, binary_filename, len);
621 memcpy(binary_filename, global_debug_dir, global_debug_dir_len);
622 len += global_debug_dir_len;
623 strlcpy(binary_filename + len, debuglink, PATH_MAX - len);
625 append_obj(objp);
626 o2 = *objp;
627 o2->base_addr = o1->base_addr;
628 o2->path = o1->path;
629 fill_lines(num_traces, traces, 0, objp, lines, offset, errout);
632 // check the path pattern of "/usr/lib/debug/.build-id/ab/cdef1234.debug"
633 static void
634 follow_debuglink_build_id(const char *build_id, size_t build_id_size, int num_traces, void **traces,
635 obj_info_t **objp, line_info_t *lines, int offset, FILE *errout)
637 static const char global_debug_dir[] = "/usr/lib/debug/.build-id/";
638 const size_t global_debug_dir_len = sizeof(global_debug_dir) - 1;
639 char *p;
640 obj_info_t *o1 = *objp, *o2;
641 size_t i;
643 if (PATH_MAX < global_debug_dir_len + 1 + build_id_size * 2 + 6) return;
645 memcpy(binary_filename, global_debug_dir, global_debug_dir_len);
646 p = binary_filename + global_debug_dir_len;
647 for (i = 0; i < build_id_size; i++) {
648 static const char tbl[] = "0123456789abcdef";
649 unsigned char n = build_id[i];
650 *p++ = tbl[n / 16];
651 *p++ = tbl[n % 16];
652 if (i == 0) *p++ = '/';
654 strcpy(p, ".debug");
656 append_obj(objp);
657 o2 = *objp;
658 o2->base_addr = o1->base_addr;
659 o2->path = o1->path;
660 fill_lines(num_traces, traces, 0, objp, lines, offset, errout);
662 #endif
664 enum
666 DW_TAG_compile_unit = 0x11,
667 DW_TAG_inlined_subroutine = 0x1d,
668 DW_TAG_subprogram = 0x2e,
671 /* Attributes encodings */
672 enum
674 DW_AT_sibling = 0x01,
675 DW_AT_location = 0x02,
676 DW_AT_name = 0x03,
677 /* Reserved 0x04 */
678 /* Reserved 0x05 */
679 /* Reserved 0x06 */
680 /* Reserved 0x07 */
681 /* Reserved 0x08 */
682 DW_AT_ordering = 0x09,
683 /* Reserved 0x0a */
684 DW_AT_byte_size = 0x0b,
685 /* Reserved 0x0c */
686 DW_AT_bit_size = 0x0d,
687 /* Reserved 0x0e */
688 /* Reserved 0x0f */
689 DW_AT_stmt_list = 0x10,
690 DW_AT_low_pc = 0x11,
691 DW_AT_high_pc = 0x12,
692 DW_AT_language = 0x13,
693 /* Reserved 0x14 */
694 DW_AT_discr = 0x15,
695 DW_AT_discr_value = 0x16,
696 DW_AT_visibility = 0x17,
697 DW_AT_import = 0x18,
698 DW_AT_string_length = 0x19,
699 DW_AT_common_reference = 0x1a,
700 DW_AT_comp_dir = 0x1b,
701 DW_AT_const_value = 0x1c,
702 DW_AT_containing_type = 0x1d,
703 DW_AT_default_value = 0x1e,
704 /* Reserved 0x1f */
705 DW_AT_inline = 0x20,
706 DW_AT_is_optional = 0x21,
707 DW_AT_lower_bound = 0x22,
708 /* Reserved 0x23 */
709 /* Reserved 0x24 */
710 DW_AT_producer = 0x25,
711 /* Reserved 0x26 */
712 DW_AT_prototyped = 0x27,
713 /* Reserved 0x28 */
714 /* Reserved 0x29 */
715 DW_AT_return_addr = 0x2a,
716 /* Reserved 0x2b */
717 DW_AT_start_scope = 0x2c,
718 /* Reserved 0x2d */
719 DW_AT_bit_stride = 0x2e,
720 DW_AT_upper_bound = 0x2f,
721 /* Reserved 0x30 */
722 DW_AT_abstract_origin = 0x31,
723 DW_AT_accessibility = 0x32,
724 DW_AT_address_class = 0x33,
725 DW_AT_artificial = 0x34,
726 DW_AT_base_types = 0x35,
727 DW_AT_calling_convention = 0x36,
728 DW_AT_count = 0x37,
729 DW_AT_data_member_location = 0x38,
730 DW_AT_decl_column = 0x39,
731 DW_AT_decl_file = 0x3a,
732 DW_AT_decl_line = 0x3b,
733 DW_AT_declaration = 0x3c,
734 DW_AT_discr_list = 0x3d,
735 DW_AT_encoding = 0x3e,
736 DW_AT_external = 0x3f,
737 DW_AT_frame_base = 0x40,
738 DW_AT_friend = 0x41,
739 DW_AT_identifier_case = 0x42,
740 /* Reserved 0x43 */
741 DW_AT_namelist_item = 0x44,
742 DW_AT_priority = 0x45,
743 DW_AT_segment = 0x46,
744 DW_AT_specification = 0x47,
745 DW_AT_static_link = 0x48,
746 DW_AT_type = 0x49,
747 DW_AT_use_location = 0x4a,
748 DW_AT_variable_parameter = 0x4b,
749 DW_AT_virtuality = 0x4c,
750 DW_AT_vtable_elem_location = 0x4d,
751 DW_AT_allocated = 0x4e,
752 DW_AT_associated = 0x4f,
753 DW_AT_data_location = 0x50,
754 DW_AT_byte_stride = 0x51,
755 DW_AT_entry_pc = 0x52,
756 DW_AT_use_UTF8 = 0x53,
757 DW_AT_extension = 0x54,
758 DW_AT_ranges = 0x55,
759 DW_AT_trampoline = 0x56,
760 DW_AT_call_column = 0x57,
761 DW_AT_call_file = 0x58,
762 DW_AT_call_line = 0x59,
763 DW_AT_description = 0x5a,
764 DW_AT_binary_scale = 0x5b,
765 DW_AT_decimal_scale = 0x5c,
766 DW_AT_small = 0x5d,
767 DW_AT_decimal_sign = 0x5e,
768 DW_AT_digit_count = 0x5f,
769 DW_AT_picture_string = 0x60,
770 DW_AT_mutable = 0x61,
771 DW_AT_threads_scaled = 0x62,
772 DW_AT_explicit = 0x63,
773 DW_AT_object_pointer = 0x64,
774 DW_AT_endianity = 0x65,
775 DW_AT_elemental = 0x66,
776 DW_AT_pure = 0x67,
777 DW_AT_recursive = 0x68,
778 DW_AT_signature = 0x69,
779 DW_AT_main_subprogram = 0x6a,
780 DW_AT_data_bit_offset = 0x6b,
781 DW_AT_const_expr = 0x6c,
782 DW_AT_enum_class = 0x6d,
783 DW_AT_linkage_name = 0x6e,
784 DW_AT_string_length_bit_size = 0x6f,
785 DW_AT_string_length_byte_size = 0x70,
786 DW_AT_rank = 0x71,
787 DW_AT_str_offsets_base = 0x72,
788 DW_AT_addr_base = 0x73,
789 DW_AT_rnglists_base = 0x74,
790 /* Reserved 0x75 */
791 DW_AT_dwo_name = 0x76,
792 DW_AT_reference = 0x77,
793 DW_AT_rvalue_reference = 0x78,
794 DW_AT_macros = 0x79,
795 DW_AT_call_all_calls = 0x7a,
796 DW_AT_call_all_source_calls = 0x7b,
797 DW_AT_call_all_tail_calls = 0x7c,
798 DW_AT_call_return_pc = 0x7d,
799 DW_AT_call_value = 0x7e,
800 DW_AT_call_origin = 0x7f,
801 DW_AT_call_parameter = 0x80,
802 DW_AT_call_pc = 0x81,
803 DW_AT_call_tail_call = 0x82,
804 DW_AT_call_target = 0x83,
805 DW_AT_call_target_clobbered = 0x84,
806 DW_AT_call_data_location = 0x85,
807 DW_AT_call_data_value = 0x86,
808 DW_AT_noreturn = 0x87,
809 DW_AT_alignment = 0x88,
810 DW_AT_export_symbols = 0x89,
811 DW_AT_deleted = 0x8a,
812 DW_AT_defaulted = 0x8b,
813 DW_AT_loclists_base = 0x8c,
814 DW_AT_lo_user = 0x2000,
815 DW_AT_hi_user = 0x3fff
818 /* Attribute form encodings */
819 enum
821 DW_FORM_addr = 0x01,
822 /* Reserved 0x02 */
823 DW_FORM_block2 = 0x03,
824 DW_FORM_block4 = 0x04,
825 DW_FORM_data2 = 0x05,
826 DW_FORM_data4 = 0x06,
827 DW_FORM_data8 = 0x07,
828 DW_FORM_string = 0x08,
829 DW_FORM_block = 0x09,
830 DW_FORM_block1 = 0x0a,
831 DW_FORM_data1 = 0x0b,
832 DW_FORM_flag = 0x0c,
833 DW_FORM_sdata = 0x0d,
834 DW_FORM_strp = 0x0e,
835 DW_FORM_udata = 0x0f,
836 DW_FORM_ref_addr = 0x10,
837 DW_FORM_ref1 = 0x11,
838 DW_FORM_ref2 = 0x12,
839 DW_FORM_ref4 = 0x13,
840 DW_FORM_ref8 = 0x14,
841 DW_FORM_ref_udata = 0x15,
842 DW_FORM_indirect = 0x16,
843 DW_FORM_sec_offset = 0x17,
844 DW_FORM_exprloc = 0x18,
845 DW_FORM_flag_present = 0x19,
846 DW_FORM_strx = 0x1a,
847 DW_FORM_addrx = 0x1b,
848 DW_FORM_ref_sup4 = 0x1c,
849 DW_FORM_strp_sup = 0x1d,
850 DW_FORM_data16 = 0x1e,
851 DW_FORM_line_strp = 0x1f,
852 DW_FORM_ref_sig8 = 0x20,
853 DW_FORM_implicit_const = 0x21,
854 DW_FORM_loclistx = 0x22,
855 DW_FORM_rnglistx = 0x23,
856 DW_FORM_ref_sup8 = 0x24,
857 DW_FORM_strx1 = 0x25,
858 DW_FORM_strx2 = 0x26,
859 DW_FORM_strx3 = 0x27,
860 DW_FORM_strx4 = 0x28,
861 DW_FORM_addrx1 = 0x29,
862 DW_FORM_addrx2 = 0x2a,
863 DW_FORM_addrx3 = 0x2b,
864 DW_FORM_addrx4 = 0x2c,
866 /* GNU extensions for referring to .gnu_debugaltlink dwz-compressed info */
867 DW_FORM_GNU_ref_alt = 0x1f20,
868 DW_FORM_GNU_strp_alt = 0x1f21
871 /* Range list entry encodings */
872 enum {
873 DW_RLE_end_of_list = 0x00,
874 DW_RLE_base_addressx = 0x01,
875 DW_RLE_startx_endx = 0x02,
876 DW_RLE_startx_length = 0x03,
877 DW_RLE_offset_pair = 0x04,
878 DW_RLE_base_address = 0x05,
879 DW_RLE_start_end = 0x06,
880 DW_RLE_start_length = 0x07
883 enum {
884 VAL_none = 0,
885 VAL_cstr = 1,
886 VAL_data = 2,
887 VAL_uint = 3,
888 VAL_int = 4,
889 VAL_addr = 5
892 # define ABBREV_TABLE_SIZE 256
893 typedef struct {
894 obj_info_t *obj;
895 const char *file;
896 uint8_t current_version;
897 const char *current_cu;
898 uint64_t current_low_pc;
899 uint64_t current_str_offsets_base;
900 uint64_t current_addr_base;
901 uint64_t current_rnglists_base;
902 const char *debug_line_cu_end;
903 uint8_t debug_line_format;
904 uint16_t debug_line_version;
905 const char *debug_line_files;
906 const char *debug_line_directories;
907 const char *p;
908 const char *cu_end;
909 const char *pend;
910 const char *q0;
911 const char *q;
912 int format; // 4 or 8
913 uint8_t address_size;
914 int level;
915 const char *abbrev_table[ABBREV_TABLE_SIZE];
916 } DebugInfoReader;
918 typedef struct {
919 ptrdiff_t pos;
920 int tag;
921 int has_children;
922 } DIE;
924 typedef struct {
925 union {
926 const char *ptr;
927 uint64_t uint64;
928 int64_t int64;
929 uint64_t addr_idx;
930 } as;
931 uint64_t off;
932 uint64_t at;
933 uint64_t form;
934 size_t size;
935 int type;
936 } DebugInfoValue;
938 #if defined(WORDS_BIGENDIAN)
939 #define MERGE_2INTS(a,b,sz) (((uint64_t)(a)<<sz)|(b))
940 #else
941 #define MERGE_2INTS(a,b,sz) (((uint64_t)(b)<<sz)|(a))
942 #endif
944 static uint16_t
945 get_uint16(const uint8_t *p)
947 return (uint16_t)MERGE_2INTS(p[0],p[1],8);
950 static uint32_t
951 get_uint32(const uint8_t *p)
953 return (uint32_t)MERGE_2INTS(get_uint16(p),get_uint16(p+2),16);
956 static uint64_t
957 get_uint64(const uint8_t *p)
959 return MERGE_2INTS(get_uint32(p),get_uint32(p+4),32);
962 static uint8_t
963 read_uint8(const char **ptr)
965 const char *p = *ptr;
966 *ptr = (p + 1);
967 return (uint8_t)*p;
970 static uint16_t
971 read_uint16(const char **ptr)
973 const char *p = *ptr;
974 *ptr = (p + 2);
975 return get_uint16((const uint8_t *)p);
978 static uint32_t
979 read_uint24(const char **ptr)
981 const char *p = *ptr;
982 *ptr = (p + 3);
983 return ((uint8_t)*p << 16) | get_uint16((const uint8_t *)p+1);
986 static uint32_t
987 read_uint32(const char **ptr)
989 const char *p = *ptr;
990 *ptr = (p + 4);
991 return get_uint32((const uint8_t *)p);
994 static uint64_t
995 read_uint64(const char **ptr)
997 const unsigned char *p = (const unsigned char *)*ptr;
998 *ptr = (char *)(p + 8);
999 return get_uint64(p);
1002 static uintptr_t
1003 read_uintptr(const char **ptr)
1005 const unsigned char *p = (const unsigned char *)*ptr;
1006 *ptr = (char *)(p + SIZEOF_VOIDP);
1007 #if SIZEOF_VOIDP == 8
1008 return get_uint64(p);
1009 #else
1010 return get_uint32(p);
1011 #endif
1014 static uint64_t
1015 read_uint(DebugInfoReader *reader)
1017 if (reader->format == 4) {
1018 return read_uint32(&reader->p);
1019 } else { /* 64 bit */
1020 return read_uint64(&reader->p);
1024 static uint64_t
1025 read_uleb128(DebugInfoReader *reader)
1027 return uleb128(&reader->p);
1030 static int64_t
1031 read_sleb128(DebugInfoReader *reader)
1033 return sleb128(&reader->p);
1036 static void
1037 debug_info_reader_init(DebugInfoReader *reader, obj_info_t *obj)
1039 reader->file = obj->mapped;
1040 reader->obj = obj;
1041 reader->p = obj->debug_info.ptr;
1042 reader->pend = obj->debug_info.ptr + obj->debug_info.size;
1043 reader->debug_line_cu_end = obj->debug_line.ptr;
1044 reader->current_low_pc = 0;
1045 reader->current_str_offsets_base = 0;
1046 reader->current_addr_base = 0;
1047 reader->current_rnglists_base = 0;
1050 static void
1051 di_skip_die_attributes(const char **p)
1053 for (;;) {
1054 uint64_t at = uleb128(p);
1055 uint64_t form = uleb128(p);
1056 if (!at && !form) break;
1057 switch (form) {
1058 default:
1059 break;
1060 case DW_FORM_implicit_const:
1061 sleb128(p);
1062 break;
1067 static void
1068 di_read_debug_abbrev_cu(DebugInfoReader *reader)
1070 uint64_t prev = 0;
1071 const char *p = reader->q0;
1072 for (;;) {
1073 uint64_t abbrev_number = uleb128(&p);
1074 if (abbrev_number <= prev) break;
1075 if (abbrev_number < ABBREV_TABLE_SIZE) {
1076 reader->abbrev_table[abbrev_number] = p;
1078 prev = abbrev_number;
1079 uleb128(&p); /* tag */
1080 p++; /* has_children */
1081 di_skip_die_attributes(&p);
1085 static int
1086 di_read_debug_line_cu(DebugInfoReader *reader, FILE *errout)
1088 const char *p;
1089 struct LineNumberProgramHeader header;
1091 p = (const char *)reader->debug_line_cu_end;
1092 if (parse_debug_line_header(reader->obj, &p, &header, errout))
1093 return -1;
1095 reader->debug_line_cu_end = (char *)header.cu_end;
1096 reader->debug_line_format = header.format;
1097 reader->debug_line_version = header.version;
1098 reader->debug_line_directories = (char *)header.include_directories;
1099 reader->debug_line_files = (char *)header.filenames;
1101 return 0;
1104 static void
1105 set_addr_idx_value(DebugInfoValue *v, uint64_t n)
1107 v->as.addr_idx = n;
1108 v->type = VAL_addr;
1111 static void
1112 set_uint_value(DebugInfoValue *v, uint64_t n)
1114 v->as.uint64 = n;
1115 v->type = VAL_uint;
1118 static void
1119 set_int_value(DebugInfoValue *v, int64_t n)
1121 v->as.int64 = n;
1122 v->type = VAL_int;
1125 static void
1126 set_cstr_value(DebugInfoValue *v, const char *s)
1128 v->as.ptr = s;
1129 v->off = 0;
1130 v->type = VAL_cstr;
1133 static void
1134 set_cstrp_value(DebugInfoValue *v, const char *s, uint64_t off)
1136 v->as.ptr = s;
1137 v->off = off;
1138 v->type = VAL_cstr;
1141 static void
1142 set_data_value(DebugInfoValue *v, const char *s)
1144 v->as.ptr = s;
1145 v->type = VAL_data;
1148 static const char *
1149 get_cstr_value(DebugInfoValue *v)
1151 if (v->as.ptr) {
1152 return v->as.ptr + v->off;
1153 } else {
1154 return NULL;
1158 static const char *
1159 resolve_strx(DebugInfoReader *reader, uint64_t idx)
1161 const char *p = reader->obj->debug_str_offsets.ptr + reader->current_str_offsets_base;
1162 uint64_t off;
1163 if (reader->format == 4) {
1164 off = ((uint32_t *)p)[idx];
1166 else {
1167 off = ((uint64_t *)p)[idx];
1169 return reader->obj->debug_str.ptr + off;
1172 static bool
1173 debug_info_reader_read_addr_value_member(DebugInfoReader *reader, DebugInfoValue *v, int size)
1175 if (size == 4) {
1176 set_uint_value(v, read_uint32(&reader->p));
1177 } else if (size == 8) {
1178 set_uint_value(v, read_uint64(&reader->p));
1179 } else {
1180 return false;
1182 return true;
1185 #define debug_info_reader_read_addr_value(reader, v, mem) \
1186 if (!debug_info_reader_read_addr_value_member((reader), (v), (reader)->mem)) { \
1187 kprintf("unknown " #mem ":%d", (reader)->mem); \
1188 return false; \
1192 static bool
1193 debug_info_reader_read_value(DebugInfoReader *reader, uint64_t form, DebugInfoValue *v, FILE *errout)
1195 switch (form) {
1196 case DW_FORM_addr:
1197 debug_info_reader_read_addr_value(reader, v, address_size);
1198 break;
1199 case DW_FORM_block2:
1200 v->size = read_uint16(&reader->p);
1201 set_data_value(v, reader->p);
1202 reader->p += v->size;
1203 break;
1204 case DW_FORM_block4:
1205 v->size = read_uint32(&reader->p);
1206 set_data_value(v, reader->p);
1207 reader->p += v->size;
1208 break;
1209 case DW_FORM_data2:
1210 set_uint_value(v, read_uint16(&reader->p));
1211 break;
1212 case DW_FORM_data4:
1213 set_uint_value(v, read_uint32(&reader->p));
1214 break;
1215 case DW_FORM_data8:
1216 set_uint_value(v, read_uint64(&reader->p));
1217 break;
1218 case DW_FORM_string:
1219 v->size = strlen(reader->p);
1220 set_cstr_value(v, reader->p);
1221 reader->p += v->size + 1;
1222 break;
1223 case DW_FORM_block:
1224 v->size = uleb128(&reader->p);
1225 set_data_value(v, reader->p);
1226 reader->p += v->size;
1227 break;
1228 case DW_FORM_block1:
1229 v->size = read_uint8(&reader->p);
1230 set_data_value(v, reader->p);
1231 reader->p += v->size;
1232 break;
1233 case DW_FORM_data1:
1234 set_uint_value(v, read_uint8(&reader->p));
1235 break;
1236 case DW_FORM_flag:
1237 set_uint_value(v, read_uint8(&reader->p));
1238 break;
1239 case DW_FORM_sdata:
1240 set_int_value(v, read_sleb128(reader));
1241 break;
1242 case DW_FORM_strp:
1243 set_cstrp_value(v, reader->obj->debug_str.ptr, read_uint(reader));
1244 break;
1245 case DW_FORM_udata:
1246 set_uint_value(v, read_uleb128(reader));
1247 break;
1248 case DW_FORM_ref_addr:
1249 if (reader->current_version <= 2) {
1250 // DWARF Version 2 specifies that references have
1251 // the same size as an address on the target system
1252 debug_info_reader_read_addr_value(reader, v, address_size);
1253 } else {
1254 debug_info_reader_read_addr_value(reader, v, format);
1256 break;
1257 case DW_FORM_ref1:
1258 set_uint_value(v, read_uint8(&reader->p));
1259 break;
1260 case DW_FORM_ref2:
1261 set_uint_value(v, read_uint16(&reader->p));
1262 break;
1263 case DW_FORM_ref4:
1264 set_uint_value(v, read_uint32(&reader->p));
1265 break;
1266 case DW_FORM_ref8:
1267 set_uint_value(v, read_uint64(&reader->p));
1268 break;
1269 case DW_FORM_ref_udata:
1270 set_uint_value(v, uleb128(&reader->p));
1271 break;
1272 case DW_FORM_indirect:
1273 /* TODO: read the referred value */
1274 set_uint_value(v, uleb128(&reader->p));
1275 break;
1276 case DW_FORM_sec_offset:
1277 set_uint_value(v, read_uint(reader)); /* offset */
1278 /* addrptr: debug_addr */
1279 /* lineptr: debug_line */
1280 /* loclist: debug_loclists */
1281 /* loclistptr: debug_loclists */
1282 /* macptr: debug_macro */
1283 /* rnglist: debug_rnglists */
1284 /* rnglistptr: debug_rnglists */
1285 /* stroffsetsptr: debug_str_offsets */
1286 break;
1287 case DW_FORM_exprloc:
1288 v->size = (size_t)read_uleb128(reader);
1289 set_data_value(v, reader->p);
1290 reader->p += v->size;
1291 break;
1292 case DW_FORM_flag_present:
1293 set_uint_value(v, 1);
1294 break;
1295 case DW_FORM_strx:
1296 set_cstr_value(v, resolve_strx(reader, uleb128(&reader->p)));
1297 break;
1298 case DW_FORM_addrx:
1299 set_addr_idx_value(v, uleb128(&reader->p));
1300 break;
1301 case DW_FORM_ref_sup4:
1302 set_uint_value(v, read_uint32(&reader->p));
1303 break;
1304 case DW_FORM_strp_sup:
1305 set_uint_value(v, read_uint(reader));
1306 /* *p = reader->sup_file + reader->sup_str->sh_offset + ret; */
1307 break;
1308 case DW_FORM_data16:
1309 v->size = 16;
1310 set_data_value(v, reader->p);
1311 reader->p += v->size;
1312 break;
1313 case DW_FORM_line_strp:
1314 set_cstrp_value(v, reader->obj->debug_line_str.ptr, read_uint(reader));
1315 break;
1316 case DW_FORM_ref_sig8:
1317 set_uint_value(v, read_uint64(&reader->p));
1318 break;
1319 case DW_FORM_implicit_const:
1320 set_int_value(v, sleb128(&reader->q));
1321 break;
1322 case DW_FORM_loclistx:
1323 set_uint_value(v, read_uleb128(reader));
1324 break;
1325 case DW_FORM_rnglistx:
1326 set_uint_value(v, read_uleb128(reader));
1327 break;
1328 case DW_FORM_ref_sup8:
1329 set_uint_value(v, read_uint64(&reader->p));
1330 break;
1331 case DW_FORM_strx1:
1332 set_cstr_value(v, resolve_strx(reader, read_uint8(&reader->p)));
1333 break;
1334 case DW_FORM_strx2:
1335 set_cstr_value(v, resolve_strx(reader, read_uint16(&reader->p)));
1336 break;
1337 case DW_FORM_strx3:
1338 set_cstr_value(v, resolve_strx(reader, read_uint24(&reader->p)));
1339 break;
1340 case DW_FORM_strx4:
1341 set_cstr_value(v, resolve_strx(reader, read_uint32(&reader->p)));
1342 break;
1343 case DW_FORM_addrx1:
1344 set_addr_idx_value(v, read_uint8(&reader->p));
1345 break;
1346 case DW_FORM_addrx2:
1347 set_addr_idx_value(v, read_uint16(&reader->p));
1348 break;
1349 case DW_FORM_addrx3:
1350 set_addr_idx_value(v, read_uint24(&reader->p));
1351 break;
1352 case DW_FORM_addrx4:
1353 set_addr_idx_value(v, read_uint32(&reader->p));
1354 break;
1355 /* we have no support for actually reading the real values of these refs out
1356 * of the .gnu_debugaltlink dwz-compressed debuginfo at the moment, but "read"
1357 * them anyway so that we advance the reader by the right amount. */
1358 case DW_FORM_GNU_ref_alt:
1359 case DW_FORM_GNU_strp_alt:
1360 read_uint(reader);
1361 set_uint_value(v, 0);
1362 break;
1363 case 0:
1364 goto fail;
1365 break;
1367 return true;
1369 fail:
1370 kprintf("%d: unsupported form: %#"PRIx64"\n", __LINE__, form);
1371 return false;
1374 /* find abbrev in current compilation unit */
1375 static const char *
1376 di_find_abbrev(DebugInfoReader *reader, uint64_t abbrev_number, FILE *errout)
1378 const char *p;
1379 if (abbrev_number < ABBREV_TABLE_SIZE) {
1380 return reader->abbrev_table[abbrev_number];
1382 p = reader->abbrev_table[ABBREV_TABLE_SIZE-1];
1383 /* skip 255th record */
1384 uleb128(&p); /* tag */
1385 p++; /* has_children */
1386 di_skip_die_attributes(&p);
1387 for (uint64_t n = uleb128(&p); abbrev_number != n; n = uleb128(&p)) {
1388 if (n == 0) {
1389 kprintf("%d: Abbrev Number %"PRId64" not found\n",__LINE__, abbrev_number);
1390 return NULL;
1392 uleb128(&p); /* tag */
1393 p++; /* has_children */
1394 di_skip_die_attributes(&p);
1396 return p;
1399 #if 0
1400 static void
1401 hexdump0(const unsigned char *p, size_t n, FILE *errout)
1403 size_t i;
1404 kprintf(" 0 1 2 3 4 5 6 7 8 9 A B C D E F\n");
1405 for (i=0; i < n; i++){
1406 switch (i & 15) {
1407 case 0:
1408 kprintf("%02" PRIdSIZE ": %02X ", i/16, p[i]);
1409 break;
1410 case 15:
1411 kprintf("%02X\n", p[i]);
1412 break;
1413 default:
1414 kprintf("%02X ", p[i]);
1415 break;
1418 if ((i & 15) != 15) {
1419 kprintf("\n");
1422 #define hexdump(p,n,e) hexdump0((const unsigned char *)p, n, e)
1424 static void
1425 div_inspect(DebugInfoValue *v, FILE *errout)
1427 switch (v->type) {
1428 case VAL_uint:
1429 kprintf("%d: type:%d size:%" PRIxSIZE " v:%"PRIx64"\n",__LINE__,v->type,v->size,v->as.uint64);
1430 break;
1431 case VAL_int:
1432 kprintf("%d: type:%d size:%" PRIxSIZE " v:%"PRId64"\n",__LINE__,v->type,v->size,(int64_t)v->as.uint64);
1433 break;
1434 case VAL_cstr:
1435 kprintf("%d: type:%d size:%" PRIxSIZE " v:'%s'\n",__LINE__,v->type,v->size,v->as.ptr);
1436 break;
1437 case VAL_data:
1438 kprintf("%d: type:%d size:%" PRIxSIZE " v:\n",__LINE__,v->type,v->size);
1439 hexdump(v->as.ptr, 16, errout);
1440 break;
1443 #endif
1445 static DIE *
1446 di_read_die(DebugInfoReader *reader, DIE *die, FILE *errout)
1448 uint64_t abbrev_number = uleb128(&reader->p);
1449 if (abbrev_number == 0) {
1450 reader->level--;
1451 return NULL;
1454 if (!(reader->q = di_find_abbrev(reader, abbrev_number, errout))) return NULL;
1456 die->pos = reader->p - reader->obj->debug_info.ptr - 1;
1457 die->tag = (int)uleb128(&reader->q); /* tag */
1458 die->has_children = *reader->q++; /* has_children */
1459 if (die->has_children) {
1460 reader->level++;
1462 return die;
1465 static DebugInfoValue *
1466 di_read_record(DebugInfoReader *reader, DebugInfoValue *vp, FILE *errout)
1468 uint64_t at = uleb128(&reader->q);
1469 uint64_t form = uleb128(&reader->q);
1470 if (!at || !form) return NULL;
1471 vp->at = at;
1472 vp->form = form;
1473 if (!debug_info_reader_read_value(reader, form, vp, errout)) return NULL;
1474 return vp;
1477 static bool
1478 di_skip_records(DebugInfoReader *reader, FILE *errout)
1480 for (;;) {
1481 DebugInfoValue v = {{0}};
1482 uint64_t at = uleb128(&reader->q);
1483 uint64_t form = uleb128(&reader->q);
1484 if (!at || !form) return true;
1485 if (!debug_info_reader_read_value(reader, form, &v, errout)) return false;
1489 typedef struct addr_header {
1490 const char *ptr;
1491 uint64_t unit_length;
1492 uint8_t format;
1493 uint8_t address_size;
1494 /* uint8_t segment_selector_size; */
1495 } addr_header_t;
1497 static bool
1498 addr_header_init(obj_info_t *obj, addr_header_t *header, FILE *errout)
1500 const char *p = obj->debug_addr.ptr;
1502 header->ptr = p;
1504 if (!p) return true;
1506 header->unit_length = *(uint32_t *)p;
1507 p += sizeof(uint32_t);
1509 header->format = 4;
1510 if (header->unit_length == 0xffffffff) {
1511 header->unit_length = *(uint64_t *)p;
1512 p += sizeof(uint64_t);
1513 header->format = 8;
1516 p += 2; /* version */
1517 header->address_size = *p++;
1518 if (header->address_size != 4 && header->address_size != 8) {
1519 kprintf("unknown address_size:%d", header->address_size);
1520 return false;
1522 p++; /* segment_selector_size */
1523 return true;
1526 static uint64_t
1527 read_addr(addr_header_t *header, uint64_t addr_base, uint64_t idx) {
1528 if (header->address_size == 4) {
1529 return ((uint32_t*)(header->ptr + addr_base))[idx];
1531 else {
1532 return ((uint64_t*)(header->ptr + addr_base))[idx];
1536 typedef struct rnglists_header {
1537 uint64_t unit_length;
1538 uint8_t format;
1539 uint8_t address_size;
1540 uint32_t offset_entry_count;
1541 } rnglists_header_t;
1543 static bool
1544 rnglists_header_init(obj_info_t *obj, rnglists_header_t *header, FILE *errout)
1546 const char *p = obj->debug_rnglists.ptr;
1548 if (!p) return true;
1550 header->unit_length = *(uint32_t *)p;
1551 p += sizeof(uint32_t);
1553 header->format = 4;
1554 if (header->unit_length == 0xffffffff) {
1555 header->unit_length = *(uint64_t *)p;
1556 p += sizeof(uint64_t);
1557 header->format = 8;
1560 p += 2; /* version */
1561 header->address_size = *p++;
1562 if (header->address_size != 4 && header->address_size != 8) {
1563 kprintf("unknown address_size:%d", header->address_size);
1564 return false;
1566 p++; /* segment_selector_size */
1567 header->offset_entry_count = *(uint32_t *)p;
1568 return true;
1571 typedef struct {
1572 uint64_t low_pc;
1573 uint64_t high_pc;
1574 uint64_t ranges;
1575 bool low_pc_set;
1576 bool high_pc_set;
1577 bool ranges_set;
1578 } ranges_t;
1580 static void
1581 ranges_set(ranges_t *ptr, DebugInfoValue *v, addr_header_t *addr_header, uint64_t addr_base)
1583 uint64_t n = 0;
1584 if (v->type == VAL_uint) {
1585 n = v->as.uint64;
1587 else if (v->type == VAL_addr) {
1588 n = read_addr(addr_header, addr_base, v->as.addr_idx);
1590 switch (v->at) {
1591 case DW_AT_low_pc:
1592 ptr->low_pc = n;
1593 ptr->low_pc_set = true;
1594 break;
1595 case DW_AT_high_pc:
1596 if (v->form == DW_FORM_addr) {
1597 ptr->high_pc = n;
1599 else {
1600 ptr->high_pc = ptr->low_pc + n;
1602 ptr->high_pc_set = true;
1603 break;
1604 case DW_AT_ranges:
1605 ptr->ranges = n;
1606 ptr->ranges_set = true;
1607 break;
1611 static uint64_t
1612 read_dw_form_addr(DebugInfoReader *reader, const char **ptr, FILE *errout)
1614 const char *p = *ptr;
1615 *ptr = p + reader->address_size;
1616 if (reader->address_size == 4) {
1617 return read_uint32(&p);
1618 } else {
1619 return read_uint64(&p);
1623 static uintptr_t
1624 ranges_include(DebugInfoReader *reader, ranges_t *ptr, uint64_t addr, rnglists_header_t *rnglists_header, FILE *errout)
1626 if (ptr->high_pc_set) {
1627 if (ptr->ranges_set || !ptr->low_pc_set) {
1628 return UINTPTR_MAX;
1630 if (ptr->low_pc <= addr && addr <= ptr->high_pc) {
1631 return (uintptr_t)ptr->low_pc;
1634 else if (ptr->ranges_set) {
1635 /* TODO: support base address selection entry */
1636 const char *p;
1637 uint64_t base = ptr->low_pc_set ? ptr->low_pc : reader->current_low_pc;
1638 bool base_valid = true;
1639 if (reader->current_version >= 5) {
1640 if (rnglists_header->offset_entry_count == 0) {
1641 // DW_FORM_sec_offset
1642 p = reader->obj->debug_rnglists.ptr + ptr->ranges + reader->current_rnglists_base;
1644 else {
1645 // DW_FORM_rnglistx
1646 const char *offset_array = reader->obj->debug_rnglists.ptr + reader->current_rnglists_base;
1647 if (rnglists_header->format == 4) {
1648 p = offset_array + ((uint32_t *)offset_array)[ptr->ranges];
1650 else {
1651 p = offset_array + ((uint64_t *)offset_array)[ptr->ranges];
1654 for (;;) {
1655 uint8_t rle = read_uint8(&p);
1656 uintptr_t from = 0, to = 0;
1657 if (rle == DW_RLE_end_of_list) break;
1658 switch (rle) {
1659 case DW_RLE_base_addressx:
1660 uleb128(&p);
1661 base_valid = false; /* not supported yet */
1662 break;
1663 case DW_RLE_startx_endx:
1664 uleb128(&p);
1665 uleb128(&p);
1666 break;
1667 case DW_RLE_startx_length:
1668 uleb128(&p);
1669 uleb128(&p);
1670 break;
1671 case DW_RLE_offset_pair:
1672 if (!base_valid) break;
1673 from = (uintptr_t)base + uleb128(&p);
1674 to = (uintptr_t)base + uleb128(&p);
1675 break;
1676 case DW_RLE_base_address:
1677 base = read_dw_form_addr(reader, &p, errout);
1678 base_valid = true;
1679 break;
1680 case DW_RLE_start_end:
1681 from = (uintptr_t)read_dw_form_addr(reader, &p, errout);
1682 to = (uintptr_t)read_dw_form_addr(reader, &p, errout);
1683 break;
1684 case DW_RLE_start_length:
1685 from = (uintptr_t)read_dw_form_addr(reader, &p, errout);
1686 to = from + uleb128(&p);
1687 break;
1689 if (from <= addr && addr < to) {
1690 return from;
1693 return 0;
1695 p = reader->obj->debug_ranges.ptr + ptr->ranges;
1696 for (;;) {
1697 uintptr_t from = read_uintptr(&p);
1698 uintptr_t to = read_uintptr(&p);
1699 if (!from && !to) break;
1700 if (from == UINTPTR_MAX) {
1701 /* base address selection entry */
1702 base = to;
1704 else if (base + from <= addr && addr < base + to) {
1705 return (uintptr_t)base + from;
1709 else if (ptr->low_pc_set) {
1710 if (ptr->low_pc == addr) {
1711 return (uintptr_t)ptr->low_pc;
1714 return 0;
1717 #if 0
1718 static void
1719 ranges_inspect(DebugInfoReader *reader, ranges_t *ptr, FILE *errout)
1721 if (ptr->high_pc_set) {
1722 if (ptr->ranges_set || !ptr->low_pc_set) {
1723 kprintf("low_pc_set:%d high_pc_set:%d ranges_set:%d\n",ptr->low_pc_set,ptr->high_pc_set,ptr->ranges_set);
1724 return;
1726 kprintf("low_pc:%"PRIx64" high_pc:%"PRIx64"\n",ptr->low_pc,ptr->high_pc);
1728 else if (ptr->ranges_set) {
1729 char *p = reader->obj->debug_ranges.ptr + ptr->ranges;
1730 kprintf("low_pc:%"PRIx64" ranges:%"PRIx64" %lx ",ptr->low_pc,ptr->ranges, p-reader->obj->mapped);
1731 for (;;) {
1732 uintptr_t from = read_uintptr(&p);
1733 uintptr_t to = read_uintptr(&p);
1734 if (!from && !to) break;
1735 kprintf("%"PRIx64"-%"PRIx64" ",ptr->low_pc+from,ptr->low_pc+to);
1737 kprintf("\n");
1739 else if (ptr->low_pc_set) {
1740 kprintf("low_pc:%"PRIx64"\n",ptr->low_pc);
1742 else {
1743 kprintf("empty\n");
1746 #endif
1748 static int
1749 di_read_cu(DebugInfoReader *reader, FILE *errout)
1751 uint64_t unit_length;
1752 uint16_t version;
1753 uint64_t debug_abbrev_offset;
1754 reader->format = 4;
1755 reader->current_cu = reader->p;
1756 unit_length = read_uint32(&reader->p);
1757 if (unit_length == 0xffffffff) {
1758 unit_length = read_uint64(&reader->p);
1759 reader->format = 8;
1761 reader->cu_end = reader->p + unit_length;
1762 version = read_uint16(&reader->p);
1763 reader->current_version = version;
1764 if (version > 5) {
1765 return -1;
1767 else if (version == 5) {
1768 /* unit_type = */ read_uint8(&reader->p);
1769 reader->address_size = read_uint8(&reader->p);
1770 debug_abbrev_offset = read_uint(reader);
1772 else {
1773 debug_abbrev_offset = read_uint(reader);
1774 reader->address_size = read_uint8(&reader->p);
1776 if (reader->address_size != 4 && reader->address_size != 8) {
1777 kprintf("unknown address_size:%d", reader->address_size);
1778 return -1;
1780 reader->q0 = reader->obj->debug_abbrev.ptr + debug_abbrev_offset;
1782 reader->level = 0;
1783 di_read_debug_abbrev_cu(reader);
1784 if (di_read_debug_line_cu(reader, errout)) return -1;
1786 do {
1787 DIE die;
1789 if (!di_read_die(reader, &die, errout)) continue;
1791 if (die.tag != DW_TAG_compile_unit) {
1792 if (!di_skip_records(reader, errout)) return -1;
1793 break;
1796 reader->current_str_offsets_base = 0;
1797 reader->current_addr_base = 0;
1798 reader->current_rnglists_base = 0;
1800 DebugInfoValue low_pc = {{0}};
1801 /* enumerate abbrev */
1802 for (;;) {
1803 DebugInfoValue v = {{0}};
1804 if (!di_read_record(reader, &v, errout)) break;
1805 switch (v.at) {
1806 case DW_AT_low_pc:
1807 // clang may output DW_AT_addr_base after DW_AT_low_pc.
1808 // We need to resolve the DW_FORM_addr* after DW_AT_addr_base is parsed.
1809 low_pc = v;
1810 break;
1811 case DW_AT_str_offsets_base:
1812 reader->current_str_offsets_base = v.as.uint64;
1813 break;
1814 case DW_AT_addr_base:
1815 reader->current_addr_base = v.as.uint64;
1816 break;
1817 case DW_AT_rnglists_base:
1818 reader->current_rnglists_base = v.as.uint64;
1819 break;
1822 // Resolve the DW_FORM_addr of DW_AT_low_pc
1823 switch (low_pc.type) {
1824 case VAL_uint:
1825 reader->current_low_pc = low_pc.as.uint64;
1826 break;
1827 case VAL_addr:
1829 addr_header_t header = {0};
1830 if (!addr_header_init(reader->obj, &header, errout)) return -1;
1831 reader->current_low_pc = read_addr(&header, reader->current_addr_base, low_pc.as.addr_idx);
1833 break;
1835 } while (0);
1837 return 0;
1840 static void
1841 read_abstract_origin(DebugInfoReader *reader, uint64_t form, uint64_t abstract_origin, line_info_t *line, FILE *errout)
1843 const char *p = reader->p;
1844 const char *q = reader->q;
1845 int level = reader->level;
1846 DIE die;
1848 switch (form) {
1849 case DW_FORM_ref1:
1850 case DW_FORM_ref2:
1851 case DW_FORM_ref4:
1852 case DW_FORM_ref8:
1853 case DW_FORM_ref_udata:
1854 reader->p = reader->current_cu + abstract_origin;
1855 break;
1856 case DW_FORM_ref_addr:
1857 goto finish; /* not supported yet */
1858 case DW_FORM_ref_sig8:
1859 goto finish; /* not supported yet */
1860 case DW_FORM_ref_sup4:
1861 case DW_FORM_ref_sup8:
1862 goto finish; /* not supported yet */
1863 default:
1864 goto finish;
1866 if (!di_read_die(reader, &die, errout)) goto finish;
1868 /* enumerate abbrev */
1869 for (;;) {
1870 DebugInfoValue v = {{0}};
1871 if (!di_read_record(reader, &v, errout)) break;
1872 switch (v.at) {
1873 case DW_AT_name:
1874 line->sname = get_cstr_value(&v);
1875 break;
1879 finish:
1880 reader->p = p;
1881 reader->q = q;
1882 reader->level = level;
1885 static bool
1886 debug_info_read(DebugInfoReader *reader, int num_traces, void **traces,
1887 line_info_t *lines, int offset, FILE *errout)
1890 addr_header_t addr_header = {0};
1891 if (!addr_header_init(reader->obj, &addr_header, errout)) return false;
1893 rnglists_header_t rnglists_header = {0};
1894 if (!rnglists_header_init(reader->obj, &rnglists_header, errout)) return false;
1896 while (reader->p < reader->cu_end) {
1897 DIE die;
1898 ranges_t ranges = {0};
1899 line_info_t line = {0};
1901 if (!di_read_die(reader, &die, errout)) continue;
1902 /* kprintf("%d:%tx: <%d>\n",__LINE__,die.pos,reader->level,die.tag); */
1904 if (die.tag != DW_TAG_subprogram && die.tag != DW_TAG_inlined_subroutine) {
1905 skip_die:
1906 if (!di_skip_records(reader, errout)) return false;
1907 continue;
1910 /* enumerate abbrev */
1911 for (;;) {
1912 DebugInfoValue v = {{0}};
1913 /* ptrdiff_t pos = reader->p - reader->p0; */
1914 if (!di_read_record(reader, &v, errout)) break;
1915 /* kprintf("\n%d:%tx: AT:%lx FORM:%lx\n",__LINE__,pos,v.at,v.form); */
1916 /* div_inspect(&v, errout); */
1917 switch (v.at) {
1918 case DW_AT_name:
1919 line.sname = get_cstr_value(&v);
1920 break;
1921 case DW_AT_call_file:
1922 fill_filename((int)v.as.uint64, reader->debug_line_format, reader->debug_line_version, reader->debug_line_directories, reader->debug_line_files, &line, reader->obj, errout);
1923 break;
1924 case DW_AT_call_line:
1925 line.line = (int)v.as.uint64;
1926 break;
1927 case DW_AT_low_pc:
1928 case DW_AT_high_pc:
1929 case DW_AT_ranges:
1930 ranges_set(&ranges, &v, &addr_header, reader->current_addr_base);
1931 break;
1932 case DW_AT_declaration:
1933 goto skip_die;
1934 case DW_AT_inline:
1935 /* 1 or 3 */
1936 break; /* goto skip_die; */
1937 case DW_AT_abstract_origin:
1938 read_abstract_origin(reader, v.form, v.as.uint64, &line, errout);
1939 break; /* goto skip_die; */
1942 /* ranges_inspect(reader, &ranges, errout); */
1943 /* kprintf("%d:%tx: %x ",__LINE__,diepos,die.tag); */
1944 for (int i=offset; i < num_traces; i++) {
1945 uintptr_t addr = (uintptr_t)traces[i];
1946 uintptr_t offset = addr - reader->obj->base_addr + reader->obj->vmaddr;
1947 uintptr_t saddr = ranges_include(reader, &ranges, offset, &rnglists_header, errout);
1948 if (saddr == UINTPTR_MAX) return false;
1949 if (saddr) {
1950 /* kprintf("%d:%tx: %d %lx->%lx %x %s: %s/%s %d %s %s %s\n",__LINE__,die.pos, i,addr,offset, die.tag,line.sname,line.dirname,line.filename,line.line,reader->obj->path,line.sname,lines[i].sname); */
1951 if (lines[i].sname) {
1952 line_info_t *lp = malloc(sizeof(line_info_t));
1953 memcpy(lp, &lines[i], sizeof(line_info_t));
1954 lines[i].next = lp;
1955 lp->dirname = line.dirname;
1956 lp->filename = line.filename;
1957 lp->line = line.line;
1958 lp->saddr = 0;
1960 lines[i].path = reader->obj->path;
1961 lines[i].base_addr = line.base_addr;
1962 lines[i].sname = line.sname;
1963 lines[i].saddr = saddr + reader->obj->base_addr - reader->obj->vmaddr;
1967 return true;
1970 // This function parses the following attributes of Line Number Program Header in DWARF 5:
1972 // * directory_entry_format_count
1973 // * directory_entry_format
1974 // * directories_count
1975 // * directories
1977 // or
1979 // * file_name_entry_format_count
1980 // * file_name_entry_format
1981 // * file_names_count
1982 // * file_names
1984 // It records DW_LNCT_path and DW_LNCT_directory_index at the index "idx".
1985 static const char *
1986 parse_ver5_debug_line_header(const char *p, int idx, uint8_t format,
1987 obj_info_t *obj, const char **out_path,
1988 uint64_t *out_directory_index, FILE *errout)
1990 int i, j;
1991 int entry_format_count = *(uint8_t *)p++;
1992 const char *entry_format = p;
1994 /* skip the part of entry_format */
1995 for (i = 0; i < entry_format_count * 2; i++) uleb128(&p);
1997 int entry_count = (int)uleb128(&p);
1999 DebugInfoReader reader = {0};
2000 debug_info_reader_init(&reader, obj);
2001 reader.format = format;
2002 reader.p = p;
2003 for (j = 0; j < entry_count; j++) {
2004 const char *format = entry_format;
2005 for (i = 0; i < entry_format_count; i++) {
2006 DebugInfoValue v = {{0}};
2007 unsigned long dw_lnct = uleb128(&format);
2008 unsigned long dw_form = uleb128(&format);
2009 if (!debug_info_reader_read_value(&reader, dw_form, &v, errout)) return 0;
2010 if (dw_lnct == 1 /* DW_LNCT_path */ && v.type == VAL_cstr && out_path)
2011 *out_path = v.as.ptr + v.off;
2012 if (dw_lnct == 2 /* DW_LNCT_directory_index */ && v.type == VAL_uint && out_directory_index)
2013 *out_directory_index = v.as.uint64;
2015 if (j == idx) return 0;
2018 return reader.p;
2021 #ifdef USE_ELF
2022 static unsigned long
2023 uncompress_debug_section(ElfW(Shdr) *shdr, char *file, char **ptr)
2025 *ptr = NULL;
2026 #ifdef SUPPORT_COMPRESSED_DEBUG_LINE
2027 ElfW(Chdr) *chdr = (ElfW(Chdr) *)(file + shdr->sh_offset);
2028 unsigned long destsize = chdr->ch_size;
2029 int ret = 0;
2031 if (chdr->ch_type != ELFCOMPRESS_ZLIB) {
2032 /* unsupported compression type */
2033 return 0;
2036 *ptr = malloc(destsize);
2037 if (!*ptr) return 0;
2038 ret = uncompress((Bytef *)*ptr, &destsize,
2039 (const Bytef*)chdr + sizeof(ElfW(Chdr)),
2040 shdr->sh_size - sizeof(ElfW(Chdr)));
2041 if (ret != Z_OK) goto fail;
2042 return destsize;
2044 fail:
2045 free(*ptr);
2046 *ptr = NULL;
2047 #endif
2048 return 0;
2051 /* read file and fill lines */
2052 static uintptr_t
2053 fill_lines(int num_traces, void **traces, int check_debuglink,
2054 obj_info_t **objp, line_info_t *lines, int offset, FILE *errout)
2056 int i, j;
2057 char *shstr;
2058 ElfW(Ehdr) *ehdr;
2059 ElfW(Shdr) *shdr, *shstr_shdr;
2060 ElfW(Shdr) *gnu_debuglink_shdr = NULL;
2061 ElfW(Shdr) *note_gnu_build_id = NULL;
2062 int fd;
2063 off_t filesize;
2064 char *file;
2065 ElfW(Shdr) *symtab_shdr = NULL, *strtab_shdr = NULL;
2066 ElfW(Shdr) *dynsym_shdr = NULL, *dynstr_shdr = NULL;
2067 obj_info_t *obj = *objp;
2068 uintptr_t dladdr_fbase = 0;
2070 fd = open(binary_filename, O_RDONLY);
2071 if (fd < 0) {
2072 goto fail;
2074 filesize = lseek(fd, 0, SEEK_END);
2075 if (filesize < 0) {
2076 int e = errno;
2077 close(fd);
2078 kprintf("lseek: %s\n", strerror(e));
2079 goto fail;
2081 #if SIZEOF_OFF_T > SIZEOF_SIZE_T
2082 if (filesize > (off_t)SIZE_MAX) {
2083 close(fd);
2084 kprintf("Too large file %s\n", binary_filename);
2085 goto fail;
2087 #endif
2088 lseek(fd, 0, SEEK_SET);
2089 /* async-signal unsafe */
2090 file = (char *)mmap(NULL, (size_t)filesize, PROT_READ, MAP_SHARED, fd, 0);
2091 if (file == MAP_FAILED) {
2092 int e = errno;
2093 close(fd);
2094 kprintf("mmap: %s\n", strerror(e));
2095 goto fail;
2097 close(fd);
2099 ehdr = (ElfW(Ehdr) *)file;
2100 if (memcmp(ehdr->e_ident, "\177ELF", 4) != 0) {
2102 * Huh? Maybe filename was overridden by setproctitle() and
2103 * it match non-elf file.
2105 goto fail;
2107 obj->mapped = file;
2108 obj->mapped_size = (size_t)filesize;
2110 shdr = (ElfW(Shdr) *)(file + ehdr->e_shoff);
2112 shstr_shdr = shdr + ehdr->e_shstrndx;
2113 shstr = file + shstr_shdr->sh_offset;
2115 for (i = 0; i < ehdr->e_shnum; i++) {
2116 char *section_name = shstr + shdr[i].sh_name;
2117 switch (shdr[i].sh_type) {
2118 case SHT_STRTAB:
2119 if (!strcmp(section_name, ".strtab")) {
2120 strtab_shdr = shdr + i;
2122 else if (!strcmp(section_name, ".dynstr")) {
2123 dynstr_shdr = shdr + i;
2125 break;
2126 case SHT_SYMTAB:
2127 /* if (!strcmp(section_name, ".symtab")) */
2128 symtab_shdr = shdr + i;
2129 break;
2130 case SHT_DYNSYM:
2131 /* if (!strcmp(section_name, ".dynsym")) */
2132 dynsym_shdr = shdr + i;
2133 break;
2134 case SHT_NOTE:
2135 if (!strcmp(section_name, ".note.gnu.build-id")) {
2136 note_gnu_build_id = shdr + i;
2138 break;
2139 case SHT_PROGBITS:
2140 if (!strcmp(section_name, ".gnu_debuglink")) {
2141 gnu_debuglink_shdr = shdr + i;
2143 else {
2144 const char *debug_section_names[] = {
2145 ".debug_abbrev",
2146 ".debug_info",
2147 ".debug_line",
2148 ".debug_ranges",
2149 ".debug_str_offsets",
2150 ".debug_addr",
2151 ".debug_rnglists",
2152 ".debug_str",
2153 ".debug_line_str"
2156 for (j=0; j < DWARF_SECTION_COUNT; j++) {
2157 struct dwarf_section *s = obj_dwarf_section_at(obj, j);
2159 if (strcmp(section_name, debug_section_names[j]) != 0)
2160 continue;
2162 s->ptr = file + shdr[i].sh_offset;
2163 s->size = shdr[i].sh_size;
2164 s->flags = shdr[i].sh_flags;
2165 if (s->flags & SHF_COMPRESSED) {
2166 s->size = uncompress_debug_section(&shdr[i], file, &s->ptr);
2167 if (!s->size) goto fail;
2169 break;
2172 break;
2176 if (offset == -1) {
2177 /* main executable */
2178 offset = 0;
2179 if (dynsym_shdr && dynstr_shdr) {
2180 char *strtab = file + dynstr_shdr->sh_offset;
2181 ElfW(Sym) *symtab = (ElfW(Sym) *)(file + dynsym_shdr->sh_offset);
2182 int symtab_count = (int)(dynsym_shdr->sh_size / sizeof(ElfW(Sym)));
2183 void *handle = dlopen(NULL, RTLD_NOW|RTLD_LOCAL);
2184 if (handle) {
2185 for (j = 0; j < symtab_count; j++) {
2186 ElfW(Sym) *sym = &symtab[j];
2187 Dl_info info;
2188 void *s;
2189 if (ELF_ST_TYPE(sym->st_info) != STT_FUNC || sym->st_size == 0) continue;
2190 s = dlsym(handle, strtab + sym->st_name);
2191 if (s && dladdr(s, &info)) {
2192 obj->base_addr = dladdr_fbase;
2193 dladdr_fbase = (uintptr_t)info.dli_fbase;
2194 break;
2197 dlclose(handle);
2199 if (ehdr->e_type == ET_EXEC) {
2200 obj->base_addr = 0;
2202 else {
2203 /* PIE (position-independent executable) */
2204 obj->base_addr = dladdr_fbase;
2209 if (obj->debug_info.ptr && obj->debug_abbrev.ptr) {
2210 DebugInfoReader reader;
2211 debug_info_reader_init(&reader, obj);
2212 i = 0;
2213 while (reader.p < reader.pend) {
2214 /* kprintf("%d:%tx: CU[%d]\n", __LINE__, reader.p - reader.obj->debug_info.ptr, i++); */
2215 if (di_read_cu(&reader, errout)) goto use_symtab;
2216 if (!debug_info_read(&reader, num_traces, traces, lines, offset, errout))
2217 goto use_symtab;
2220 else {
2221 /* This file doesn't have dwarf, use symtab or dynsym */
2222 use_symtab:
2223 if (!symtab_shdr) {
2224 /* This file doesn't have symtab, use dynsym instead */
2225 symtab_shdr = dynsym_shdr;
2226 strtab_shdr = dynstr_shdr;
2229 if (symtab_shdr && strtab_shdr) {
2230 char *strtab = file + strtab_shdr->sh_offset;
2231 ElfW(Sym) *symtab = (ElfW(Sym) *)(file + symtab_shdr->sh_offset);
2232 int symtab_count = (int)(symtab_shdr->sh_size / sizeof(ElfW(Sym)));
2233 for (j = 0; j < symtab_count; j++) {
2234 ElfW(Sym) *sym = &symtab[j];
2235 uintptr_t saddr = (uintptr_t)sym->st_value + obj->base_addr;
2236 if (ELF_ST_TYPE(sym->st_info) != STT_FUNC) continue;
2237 for (i = offset; i < num_traces; i++) {
2238 uintptr_t d = (uintptr_t)traces[i] - saddr;
2239 if (lines[i].line > 0 || d > (uintptr_t)sym->st_size)
2240 continue;
2241 /* fill symbol name and addr from .symtab */
2242 if (!lines[i].sname) lines[i].sname = strtab + sym->st_name;
2243 lines[i].saddr = saddr;
2244 lines[i].path = obj->path;
2245 lines[i].base_addr = obj->base_addr;
2251 if (!obj->debug_line.ptr) {
2252 /* This file doesn't have .debug_line section,
2253 let's check .gnu_debuglink section instead. */
2254 if (gnu_debuglink_shdr && check_debuglink) {
2255 follow_debuglink(file + gnu_debuglink_shdr->sh_offset,
2256 num_traces, traces,
2257 objp, lines, offset, errout);
2259 if (note_gnu_build_id && check_debuglink) {
2260 ElfW(Nhdr) *nhdr = (ElfW(Nhdr)*) (file + note_gnu_build_id->sh_offset);
2261 const char *build_id = (char *)(nhdr + 1) + nhdr->n_namesz;
2262 follow_debuglink_build_id(build_id, nhdr->n_descsz,
2263 num_traces, traces,
2264 objp, lines, offset, errout);
2266 goto finish;
2269 if (parse_debug_line(num_traces, traces,
2270 obj->debug_line.ptr,
2271 obj->debug_line.size,
2272 obj, lines, offset, errout) == -1)
2273 goto fail;
2275 finish:
2276 return dladdr_fbase;
2277 fail:
2278 return (uintptr_t)-1;
2280 #else /* Mach-O */
2281 /* read file and fill lines */
2282 static uintptr_t
2283 fill_lines(int num_traces, void **traces, int check_debuglink,
2284 obj_info_t **objp, line_info_t *lines, int offset, FILE *errout)
2286 # ifdef __LP64__
2287 # define LP(x) x##_64
2288 # else
2289 # define LP(x) x
2290 # endif
2291 int fd;
2292 off_t filesize;
2293 char *file, *p = NULL;
2294 obj_info_t *obj = *objp;
2295 struct LP(mach_header) *header;
2296 uintptr_t dladdr_fbase = 0;
2299 char *s = binary_filename;
2300 char *base = strrchr(binary_filename, '/')+1;
2301 size_t max = PATH_MAX;
2302 size_t size = strlen(binary_filename);
2303 size_t basesize = size - (base - binary_filename);
2304 s += size;
2305 max -= size;
2306 p = s;
2307 size = strlcpy(s, ".dSYM/Contents/Resources/DWARF/", max);
2308 if (size == 0) goto fail;
2309 s += size;
2310 max -= size;
2311 if (max <= basesize) goto fail;
2312 memcpy(s, base, basesize);
2313 s[basesize] = 0;
2315 fd = open(binary_filename, O_RDONLY);
2316 if (fd < 0) {
2317 *p = 0; /* binary_filename becomes original file name */
2318 fd = open(binary_filename, O_RDONLY);
2319 if (fd < 0) {
2320 goto fail;
2325 filesize = lseek(fd, 0, SEEK_END);
2326 if (filesize < 0) {
2327 int e = errno;
2328 close(fd);
2329 kprintf("lseek: %s\n", strerror(e));
2330 goto fail;
2332 #if SIZEOF_OFF_T > SIZEOF_SIZE_T
2333 if (filesize > (off_t)SIZE_MAX) {
2334 close(fd);
2335 kprintf("Too large file %s\n", binary_filename);
2336 goto fail;
2338 #endif
2339 lseek(fd, 0, SEEK_SET);
2340 /* async-signal unsafe */
2341 file = (char *)mmap(NULL, (size_t)filesize, PROT_READ, MAP_SHARED, fd, 0);
2342 if (file == MAP_FAILED) {
2343 int e = errno;
2344 close(fd);
2345 kprintf("mmap: %s\n", strerror(e));
2346 goto fail;
2348 close(fd);
2350 obj->mapped = file;
2351 obj->mapped_size = (size_t)filesize;
2353 header = (struct LP(mach_header) *)file;
2354 if (header->magic == LP(MH_MAGIC)) {
2355 /* non universal binary */
2356 p = file;
2358 else if (header->magic == FAT_CIGAM) {
2359 struct LP(mach_header) *mhp = _NSGetMachExecuteHeader();
2360 struct fat_header *fat = (struct fat_header *)file;
2361 char *q = file + sizeof(*fat);
2362 uint32_t nfat_arch = __builtin_bswap32(fat->nfat_arch);
2363 /* kprintf("%d: fat:%s %d\n",__LINE__, binary_filename,nfat_arch); */
2364 for (uint32_t i = 0; i < nfat_arch; i++) {
2365 struct fat_arch *arch = (struct fat_arch *)q;
2366 cpu_type_t cputype = __builtin_bswap32(arch->cputype);
2367 cpu_subtype_t cpusubtype = __builtin_bswap32(arch->cpusubtype);
2368 uint32_t offset = __builtin_bswap32(arch->offset);
2369 /* kprintf("%d: fat %d %x/%x %x/%x\n",__LINE__, i, mhp->cputype,mhp->cpusubtype, cputype,cpusubtype); */
2370 if (mhp->cputype == cputype &&
2371 (cpu_subtype_t)(mhp->cpusubtype & ~CPU_SUBTYPE_MASK) == cpusubtype) {
2372 p = file + offset;
2373 file = p;
2374 header = (struct LP(mach_header) *)p;
2375 if (header->magic == LP(MH_MAGIC)) {
2376 goto found_mach_header;
2378 break;
2380 q += sizeof(*arch);
2382 kprintf("'%s' is not a Mach-O universal binary file!\n",binary_filename);
2383 close(fd);
2384 goto fail;
2386 else {
2387 # ifdef __LP64__
2388 # define bitsize "64"
2389 # else
2390 # define bitsize "32"
2391 # endif
2392 kprintf("'%s' is not a " bitsize
2393 "-bit Mach-O file!\n",binary_filename);
2394 # undef bitsize
2395 close(fd);
2396 goto fail;
2398 found_mach_header:
2399 p += sizeof(*header);
2401 for (uint32_t i = 0; i < (uint32_t)header->ncmds; i++) {
2402 struct load_command *lcmd = (struct load_command *)p;
2403 switch (lcmd->cmd) {
2404 case LP(LC_SEGMENT):
2406 static const char *debug_section_names[] = {
2407 "__debug_abbrev",
2408 "__debug_info",
2409 "__debug_line",
2410 "__debug_ranges",
2411 "__debug_str_offsets",
2412 "__debug_addr",
2413 "__debug_rnglists",
2414 "__debug_str",
2415 "__debug_line_str",
2417 struct LP(segment_command) *scmd = (struct LP(segment_command) *)lcmd;
2418 if (strcmp(scmd->segname, "__TEXT") == 0) {
2419 obj->vmaddr = scmd->vmaddr;
2421 else if (strcmp(scmd->segname, "__DWARF") == 0) {
2422 p += sizeof(struct LP(segment_command));
2423 for (uint64_t i = 0; i < scmd->nsects; i++) {
2424 struct LP(section) *sect = (struct LP(section) *)p;
2425 p += sizeof(struct LP(section));
2426 for (int j=0; j < DWARF_SECTION_COUNT; j++) {
2427 struct dwarf_section *s = obj_dwarf_section_at(obj, j);
2429 if (strcmp(sect->sectname, debug_section_names[j]) != 0)
2430 continue;
2432 s->ptr = file + sect->offset;
2433 s->size = sect->size;
2434 s->flags = sect->flags;
2435 if (s->flags & SHF_COMPRESSED) {
2436 goto fail;
2438 break;
2443 break;
2445 case LC_SYMTAB:
2447 struct symtab_command *cmd = (struct symtab_command *)lcmd;
2448 struct LP(nlist) *nl = (struct LP(nlist) *)(file + cmd->symoff);
2449 char *strtab = file + cmd->stroff, *sname = 0;
2450 uint32_t j;
2451 uintptr_t saddr = 0;
2452 /* kprintf("[%2d]: %x/symtab %p\n", i, cmd->cmd, (void *)p); */
2453 for (j = 0; j < cmd->nsyms; j++) {
2454 uintptr_t symsize, d;
2455 struct LP(nlist) *e = &nl[j];
2456 /* kprintf("[%2d][%4d]: %02x/%x/%x: %s %llx\n", i, j, e->n_type,e->n_sect,e->n_desc,strtab+e->n_un.n_strx,e->n_value); */
2457 if (e->n_type != N_FUN) continue;
2458 if (e->n_sect) {
2459 saddr = (uintptr_t)e->n_value + obj->base_addr - obj->vmaddr;
2460 sname = strtab + e->n_un.n_strx;
2461 /* kprintf("[%2d][%4d]: %02x/%x/%x: %s %llx\n", i, j, e->n_type,e->n_sect,e->n_desc,strtab+e->n_un.n_strx,e->n_value); */
2462 continue;
2464 for (int k = offset; k < num_traces; k++) {
2465 d = (uintptr_t)traces[k] - saddr;
2466 symsize = e->n_value;
2467 /* kprintf("%lx %lx %lx\n",saddr,symsize,traces[k]); */
2468 if (lines[k].line > 0 || d > (uintptr_t)symsize)
2469 continue;
2470 /* fill symbol name and addr from .symtab */
2471 if (!lines[k].sname) lines[k].sname = sname;
2472 lines[k].saddr = saddr;
2473 lines[k].path = obj->path;
2474 lines[k].base_addr = obj->base_addr;
2479 p += lcmd->cmdsize;
2482 if (obj->debug_info.ptr && obj->debug_abbrev.ptr) {
2483 DebugInfoReader reader;
2484 debug_info_reader_init(&reader, obj);
2485 while (reader.p < reader.pend) {
2486 if (di_read_cu(&reader, errout)) goto fail;
2487 if (!debug_info_read(&reader, num_traces, traces, lines, offset, errout))
2488 goto fail;
2492 if (parse_debug_line(num_traces, traces,
2493 obj->debug_line.ptr,
2494 obj->debug_line.size,
2495 obj, lines, offset, errout) == -1)
2496 goto fail;
2498 return dladdr_fbase;
2499 fail:
2500 return (uintptr_t)-1;
2502 #endif
2504 #define HAVE_MAIN_EXE_PATH
2505 #if defined(__FreeBSD__) || defined(__DragonFly__)
2506 # include <sys/sysctl.h>
2507 #endif
2508 /* ssize_t main_exe_path(FILE *errout)
2510 * store the path of the main executable to `binary_filename`,
2511 * and returns strlen(binary_filename).
2512 * it is NUL terminated.
2514 #if defined(__linux__) || defined(__NetBSD__)
2515 static ssize_t
2516 main_exe_path(FILE *errout)
2518 # if defined(__linux__)
2519 # define PROC_SELF_EXE "/proc/self/exe"
2520 # elif defined(__NetBSD__)
2521 # define PROC_SELF_EXE "/proc/curproc/exe"
2522 # endif
2523 ssize_t len = readlink(PROC_SELF_EXE, binary_filename, PATH_MAX);
2524 if (len < 0) return 0;
2525 binary_filename[len] = 0;
2526 return len;
2528 #elif defined(__FreeBSD__) || defined(__DragonFly__)
2529 static ssize_t
2530 main_exe_path(FILE *errout)
2532 int mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1};
2533 size_t len = PATH_MAX;
2534 int err = sysctl(mib, 4, binary_filename, &len, NULL, 0);
2535 if (err) {
2536 kprintf("Can't get the path of ruby");
2537 return -1;
2539 len--; /* sysctl sets strlen+1 */
2540 return len;
2542 #elif defined(HAVE_LIBPROC_H)
2543 static ssize_t
2544 main_exe_path(FILE *errout)
2546 int len = proc_pidpath(getpid(), binary_filename, PATH_MAX);
2547 if (len == 0) return 0;
2548 binary_filename[len] = 0;
2549 return len;
2551 #else
2552 #undef HAVE_MAIN_EXE_PATH
2553 #endif
2555 static void
2556 print_line0(line_info_t *line, void *address, FILE *errout)
2558 uintptr_t addr = (uintptr_t)address;
2559 uintptr_t d = addr - line->saddr;
2560 if (!address) {
2561 /* inlined */
2562 if (line->dirname && line->dirname[0]) {
2563 kprintf("%s(%s) %s/%s:%d\n", line->path, line->sname, line->dirname, line->filename, line->line);
2565 else {
2566 kprintf("%s(%s) %s:%d\n", line->path, line->sname, line->filename, line->line);
2569 else if (!line->path) {
2570 kprintf("[0x%"PRIxPTR"]\n", addr);
2572 else if (!line->sname) {
2573 kprintf("%s(0x%"PRIxPTR") [0x%"PRIxPTR"]\n", line->path, addr-line->base_addr, addr);
2575 else if (!line->saddr) {
2576 kprintf("%s(%s) [0x%"PRIxPTR"]\n", line->path, line->sname, addr);
2578 else if (line->line <= 0) {
2579 kprintf("%s(%s+0x%"PRIxPTR") [0x%"PRIxPTR"]\n", line->path, line->sname,
2580 d, addr);
2582 else if (!line->filename) {
2583 kprintf("%s(%s+0x%"PRIxPTR") [0x%"PRIxPTR"] ???:%d\n", line->path, line->sname,
2584 d, addr, line->line);
2586 else if (line->dirname && line->dirname[0]) {
2587 kprintf("%s(%s+0x%"PRIxPTR") [0x%"PRIxPTR"] %s/%s:%d\n", line->path, line->sname,
2588 d, addr, line->dirname, line->filename, line->line);
2590 else {
2591 kprintf("%s(%s+0x%"PRIxPTR") [0x%"PRIxPTR"] %s:%d\n", line->path, line->sname,
2592 d, addr, line->filename, line->line);
2596 static void
2597 print_line(line_info_t *line, void *address, FILE *errout)
2599 print_line0(line, address, errout);
2600 if (line->next) {
2601 print_line(line->next, NULL, errout);
2605 void
2606 rb_dump_backtrace_with_lines(int num_traces, void **traces, FILE *errout)
2608 int i;
2609 /* async-signal unsafe */
2610 line_info_t *lines = (line_info_t *)calloc(num_traces, sizeof(line_info_t));
2611 obj_info_t *obj = NULL;
2612 /* 2 is NULL + main executable */
2613 void **dladdr_fbases = (void **)calloc(num_traces+2, sizeof(void *));
2615 #ifdef HAVE_MAIN_EXE_PATH
2616 char *main_path = NULL; /* used on printing backtrace */
2617 ssize_t len;
2618 if ((len = main_exe_path(errout)) > 0) {
2619 main_path = (char *)alloca(len + 1);
2620 if (main_path) {
2621 uintptr_t addr;
2622 memcpy(main_path, binary_filename, len+1);
2623 append_obj(&obj);
2624 obj->path = main_path;
2625 addr = fill_lines(num_traces, traces, 1, &obj, lines, -1, errout);
2626 if (addr != (uintptr_t)-1) {
2627 dladdr_fbases[0] = (void *)addr;
2631 #endif
2633 /* fill source lines by reading dwarf */
2634 for (i = 0; i < num_traces; i++) {
2635 Dl_info info;
2636 if (lines[i].line) continue;
2637 if (dladdr(traces[i], &info)) {
2638 const char *path;
2639 void **p;
2641 /* skip symbols which is in already checked objects */
2642 /* if the binary is strip-ed, this may effect */
2643 for (p=dladdr_fbases; *p; p++) {
2644 if (*p == info.dli_fbase) {
2645 if (info.dli_fname) lines[i].path = info.dli_fname;
2646 if (info.dli_sname) lines[i].sname = info.dli_sname;
2647 goto next_line;
2650 *p = info.dli_fbase;
2652 append_obj(&obj);
2653 obj->base_addr = (uintptr_t)info.dli_fbase;
2654 path = info.dli_fname;
2655 obj->path = path;
2656 if (path) lines[i].path = path;
2657 if (info.dli_sname) {
2658 lines[i].sname = info.dli_sname;
2659 lines[i].saddr = (uintptr_t)info.dli_saddr;
2661 strlcpy(binary_filename, path, PATH_MAX);
2662 if (fill_lines(num_traces, traces, 1, &obj, lines, i, errout) == (uintptr_t)-1)
2663 break;
2665 next_line:
2666 continue;
2669 /* output */
2670 for (i = 0; i < num_traces; i++) {
2671 print_line(&lines[i], traces[i], errout);
2673 /* FreeBSD's backtrace may show _start and so on */
2674 if (lines[i].sname && strcmp("main", lines[i].sname) == 0)
2675 break;
2678 /* free */
2679 while (obj) {
2680 obj_info_t *o = obj;
2681 for (i=0; i < DWARF_SECTION_COUNT; i++) {
2682 struct dwarf_section *s = obj_dwarf_section_at(obj, i);
2683 if (s->flags & SHF_COMPRESSED) {
2684 free(s->ptr);
2687 if (obj->mapped_size) {
2688 munmap(obj->mapped, obj->mapped_size);
2690 obj = o->next;
2691 free(o);
2693 for (i = 0; i < num_traces; i++) {
2694 line_info_t *line = lines[i].next;
2695 while (line) {
2696 line_info_t *l = line;
2697 line = line->next;
2698 free(l);
2701 free(lines);
2702 free(dladdr_fbases);
2705 #undef kprintf
2707 #else /* defined(USE_ELF) */
2708 #error not supported
2709 #endif