Ruby: addr2line.c Source File

Ruby 3.4.8p72 (2025-12-17 revision 995b59f66677d44767ce9faac6957e5543617ff9)

addr2line.c
1/**********************************************************************
2
3 addr2line.c -
4
5 $Author$
6
7 Copyright (C) 2010 Shinichiro Hamaji
8
9**********************************************************************/
10
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
19
20#include "ruby/internal/config.h"
21#include "ruby/defines.h"
22#include "ruby/missing.h"
23#include "addr2line.h"
24
25#include <stdio.h>
26#include <errno.h>
27
28#ifdef HAVE_LIBPROC_H
29#include <libproc.h>
30#endif
31
32#include "ruby/internal/stdbool.h"
33
34#if defined(USE_ELF) || defined(HAVE_MACH_O_LOADER_H)
35
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>
46
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 */
60void *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__ */
71
72#ifndef UNREACHABLE
73# define UNREACHABLE abort()
74#endif
75#ifndef UNREACHABLE_RETURN
76# define UNREACHABLE_RETURN(_) return (abort(), (_))
77#endif
78
79#ifdef HAVE_DLADDR
80# include <dlfcn.h>
81#endif
82
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
90
91#ifdef USE_ELF
92# ifdef __OpenBSD__
93# include <elf_abi.h>
94# else
95# include <elf.h>
96# endif
97
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
113
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
123
124#ifndef PATH_MAX
125#define PATH_MAX 4096
126#endif
127
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 */
140
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 */
146
147#define kprintf(...) fprintf(errout, "" __VA_ARGS__)
148
149typedef struct line_info {
150 const char *dirname;
151 const char *filename;
152 const char *path; /* object path */
153 int line;
154
155 uintptr_t base_addr;
156 uintptr_t saddr;
157 const char *sname; /* function name */
158
159 struct line_info *next;
160} line_info_t;
161
162struct dwarf_section {
163 char *ptr;
164 size_t size;
165 uint64_t flags;
166};
167
168typedef 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;
186
187#define DWARF_SECTION_COUNT 9
188
189static struct dwarf_section *
190obj_dwarf_section_at(obj_info_t *obj, int n)
191{
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
202 };
203 if (n < 0 || DWARF_SECTION_COUNT <= n) {
204 UNREACHABLE_RETURN(0);
205 }
206 return ary[n];
207}
208
209struct debug_section_definition {
210 const char *name;
211 struct dwarf_section *dwarf;
212};
213
214/* Avoid consuming stack as this module may be used from signal handler */
215static char binary_filename[PATH_MAX + 1];
216
217static unsigned long
218uleb128(const char **p)
219{
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;
227 }
228 r += (b & 0x7f) << s;
229 s += 7;
230 }
231 return r;
232}
233
234static long
235sleb128(const char **p)
236{
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;
244 }
245 else {
246 r += (b & 0x3f) << s;
247 }
248 break;
249 }
250 r += (b & 0x7f) << s;
251 s += 7;
252 }
253 return r;
254}
255
256static const char *
257get_nth_dirname(unsigned long dir, const char *p, FILE *errout)
258{
259 if (!dir--) {
260 return "";
261 }
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 "";
269 }
270 }
271 return p;
272}
273
274static 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);
278
279static void
280fill_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)
282{
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;
294 }
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;
303 }
304 while (*p) p++;
305 p++;
306 dir = uleb128(&p);
307 /* last modified. */
308 uleb128(&p);
309 /* size of the file. */
310 uleb128(&p);
311
312 if (i == file) {
313 line->filename = filename;
314 line->dirname = get_nth_dirname(dir, include_directories, errout);
315 }
316 }
317 }
318}
319
320static void
321fill_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)
324{
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;
334 }
335 }
336}
337
338struct 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;
354};
355
356static int
357parse_debug_line_header(obj_info_t *obj, const char **pp, struct LineNumberProgramHeader *header, FILE *errout)
358{
359 const char *p = *pp;
360 header->unit_length = *(uint32_t *)p;
361 p += sizeof(uint32_t);
362
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;
368 }
369
370 header->cu_end = p + header->unit_length;
371
372 header->version = *(uint16_t *)p;
373 p += sizeof(uint16_t);
374 if (header->version > 5) return -1;
375
376 if (header->version >= 5) {
377 /* address_size = *(uint8_t *)p++; */
378 /* segment_selector_size = *(uint8_t *)p++; */
379 p += 2;
380 }
381
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;
385
386 header->minimum_instruction_length = *(uint8_t *)p++;
387
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++;
392 }
393
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;
400
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;
405 }
406 else {
407 header->include_directories = p;
408
409 /* temporary measure for compress-debug-sections */
410 if (p >= header->cu_end) return -1;
411
412 /* skip include directories */
413 while (*p) {
414 p = memchr(p, '\0', header->cu_end - p);
415 if (!p) return -1;
416 p++;
417 }
418 p++;
419
420 header->filenames = p;
421 }
422
423 *pp = header->cu_start;
424
425 return 0;
426}
427
428static int
429parse_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)
431{
432 const char *p = (const char *)*debug_line;
433 struct LineNumberProgramHeader header;
434
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; */
446
447 if (parse_debug_line_header(obj, &p, &header, errout))
448 return -1;
449 is_stmt = header.default_is_stmt;
450
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)
461
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;
477 }
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);
539 }
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();
548 }
549 }
550 }
551 *debug_line = (char *)p;
552 return 0;
553}
554
555static int
556parse_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)
559{
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;
564 }
565 if (debug_line != debug_line_end) {
566 kprintf("Unexpected size of .debug_line in %s\n",
567 binary_filename);
568 }
569 return 0;
570}
571
572/* read file and fill lines */
573static uintptr_t
574fill_lines(int num_traces, void **traces, int check_debuglink,
575 obj_info_t **objp, line_info_t *lines, int offset, FILE *errout);
576
577static void
578append_obj(obj_info_t **objp)
579{
580 obj_info_t *newobj = calloc(1, sizeof(obj_info_t));
581 if (*objp) (*objp)->next = newobj;
582 *objp = newobj;
583}
584
585#ifdef USE_ELF
586/* Ideally we should check 4 paths to follow gnu_debuglink:
587 *
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.
592 *
593 * but we handle only two cases for now as the two formats are
594 * used by some linux distributions.
595 *
596 * See GDB's info for detail.
597 * https://sourceware.org/gdb/onlinedocs/gdb/Separate-Debug-Files.html
598 */
599
600// check the path pattern of "/usr/lib/debug/usr/bin/ruby.debug"
601static void
602follow_debuglink(const char *debuglink, int num_traces, void **traces,
603 obj_info_t **objp, line_info_t *lines, int offset, FILE *errout)
604{
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;
610
611 p = strrchr(binary_filename, '/');
612 if (!p) {
613 return;
614 }
615 p[1] = '\0';
616
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);
624
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);
630}
631
632// check the path pattern of "/usr/lib/debug/.build-id/ab/cdef1234.debug"
633static void
634follow_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)
636{
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;
642
643 if (PATH_MAX < global_debug_dir_len + 1 + build_id_size * 2 + 6) return;
644
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++ = '/';
653 }
654 strcpy(p, ".debug");
655
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);
661}
662#endif
663
664enum
665{
666 DW_TAG_compile_unit = 0x11,
667 DW_TAG_inlined_subroutine = 0x1d,
668 DW_TAG_subprogram = 0x2e,
669};
670
671/* Attributes encodings */
672enum
673{
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
816};
817
818/* Attribute form encodings */
819enum
820{
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,
865
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
869};
870
871/* Range list entry encodings */
872enum {
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
881};
882
883enum {
884 VAL_none = 0,
885 VAL_cstr = 1,
886 VAL_data = 2,
887 VAL_uint = 3,
888 VAL_int = 4,
889 VAL_addr = 5
890};
891
892# define ABBREV_TABLE_SIZE 256
893typedef 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;
917
918typedef struct {
919 ptrdiff_t pos;
920 int tag;
921 int has_children;
922} DIE;
923
924typedef 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;
937
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
943
944static uint16_t
945get_uint16(const uint8_t *p)
946{
947 return (uint16_t)MERGE_2INTS(p[0],p[1],8);
948}
949
950static uint32_t
951get_uint32(const uint8_t *p)
952{
953 return (uint32_t)MERGE_2INTS(get_uint16(p),get_uint16(p+2),16);
954}
955
956static uint64_t
957get_uint64(const uint8_t *p)
958{
959 return MERGE_2INTS(get_uint32(p),get_uint32(p+4),32);
960}
961
962static uint8_t
963read_uint8(const char **ptr)
964{
965 const char *p = *ptr;
966 *ptr = (p + 1);
967 return (uint8_t)*p;
968}
969
970static uint16_t
971read_uint16(const char **ptr)
972{
973 const char *p = *ptr;
974 *ptr = (p + 2);
975 return get_uint16((const uint8_t *)p);
976}
977
978static uint32_t
979read_uint24(const char **ptr)
980{
981 const char *p = *ptr;
982 *ptr = (p + 3);
983 return ((uint8_t)*p << 16) | get_uint16((const uint8_t *)p+1);
984}
985
986static uint32_t
987read_uint32(const char **ptr)
988{
989 const char *p = *ptr;
990 *ptr = (p + 4);
991 return get_uint32((const uint8_t *)p);
992}
993
994static uint64_t
995read_uint64(const char **ptr)
996{
997 const unsigned char *p = (const unsigned char *)*ptr;
998 *ptr = (char *)(p + 8);
999 return get_uint64(p);
1000}
1001
1002static uintptr_t
1003read_uintptr(const char **ptr)
1004{
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
1012}
1013
1014static uint64_t
1015read_uint(DebugInfoReader *reader)
1016{
1017 if (reader->format == 4) {
1018 return read_uint32(&reader->p);
1019 } else { /* 64 bit */
1020 return read_uint64(&reader->p);
1021 }
1022}
1023
1024static uint64_t
1025read_uleb128(DebugInfoReader *reader)
1026{
1027 return uleb128(&reader->p);
1028}
1029
1030static int64_t
1031read_sleb128(DebugInfoReader *reader)
1032{
1033 return sleb128(&reader->p);
1034}
1035
1036static void
1037debug_info_reader_init(DebugInfoReader *reader, obj_info_t *obj)
1038{
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;
1048}
1049
1050static void
1051di_skip_die_attributes(const char **p)
1052{
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;
1063 }
1064 }
1065}
1066
1067static void
1068di_read_debug_abbrev_cu(DebugInfoReader *reader)
1069{
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;
1077 }
1078 prev = abbrev_number;
1079 uleb128(&p); /* tag */
1080 p++; /* has_children */
1081 di_skip_die_attributes(&p);
1082 }
1083}
1084
1085static int
1086di_read_debug_line_cu(DebugInfoReader *reader, FILE *errout)
1087{
1088 const char *p;
1089 struct LineNumberProgramHeader header;
1090
1091 p = (const char *)reader->debug_line_cu_end;
1092 if (parse_debug_line_header(reader->obj, &p, &header, errout))
1093 return -1;
1094
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;
1100
1101 return 0;
1102}
1103
1104static void
1105set_addr_idx_value(DebugInfoValue *v, uint64_t n)
1106{
1107 v->as.addr_idx = n;
1108 v->type = VAL_addr;
1109}
1110
1111static void
1112set_uint_value(DebugInfoValue *v, uint64_t n)
1113{
1114 v->as.uint64 = n;
1115 v->type = VAL_uint;
1116}
1117
1118static void
1119set_int_value(DebugInfoValue *v, int64_t n)
1120{
1121 v->as.int64 = n;
1122 v->type = VAL_int;
1123}
1124
1125static void
1126set_cstr_value(DebugInfoValue *v, const char *s)
1127{
1128 v->as.ptr = s;
1129 v->off = 0;
1130 v->type = VAL_cstr;
1131}
1132
1133static void
1134set_cstrp_value(DebugInfoValue *v, const char *s, uint64_t off)
1135{
1136 v->as.ptr = s;
1137 v->off = off;
1138 v->type = VAL_cstr;
1139}
1140
1141static void
1142set_data_value(DebugInfoValue *v, const char *s)
1143{
1144 v->as.ptr = s;
1145 v->type = VAL_data;
1146}
1147
1148static const char *
1149get_cstr_value(DebugInfoValue *v)
1150{
1151 if (v->as.ptr) {
1152 return v->as.ptr + v->off;
1153 } else {
1154 return NULL;
1155 }
1156}
1157
1158static const char *
1159resolve_strx(DebugInfoReader *reader, uint64_t idx)
1160{
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];
1165 }
1166 else {
1167 off = ((uint64_t *)p)[idx];
1168 }
1169 return reader->obj->debug_str.ptr + off;
1170}
1171
1172static bool
1173debug_info_reader_read_addr_value_member(DebugInfoReader *reader, DebugInfoValue *v, int size)
1174{
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;
1181 }
1182 return true;
1183}
1184
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; \
1189 }
1190
1191
1192static bool
1193debug_info_reader_read_value(DebugInfoReader *reader, uint64_t form, DebugInfoValue *v, FILE *errout)
1194{
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);
1255 }
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;
1366 }
1367 return true;
1368
1369 fail:
1370 kprintf("%d: unsupported form: %#"PRIx64"\n", __LINE__, form);
1371 return false;
1372}
1373
1374/* find abbrev in current compilation unit */
1375static const char *
1376di_find_abbrev(DebugInfoReader *reader, uint64_t abbrev_number, FILE *errout)
1377{
1378 const char *p;
1379 if (abbrev_number < ABBREV_TABLE_SIZE) {
1380 return reader->abbrev_table[abbrev_number];
1381 }
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;
1391 }
1392 uleb128(&p); /* tag */
1393 p++; /* has_children */
1394 di_skip_die_attributes(&p);
1395 }
1396 return p;
1397}
1398
1399#if 0
1400static void
1401hexdump0(const unsigned char *p, size_t n, FILE *errout)
1402{
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;
1416 }
1417 }
1418 if ((i & 15) != 15) {
1419 kprintf("\n");
1420 }
1421}
1422#define hexdump(p,n,e) hexdump0((const unsigned char *)p, n, e)
1423
1424static void
1425div_inspect(DebugInfoValue *v, FILE *errout)
1426{
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;
1441 }
1442}
1443#endif
1444
1445static DIE *
1446di_read_die(DebugInfoReader *reader, DIE *die, FILE *errout)
1447{
1448 uint64_t abbrev_number = uleb128(&reader->p);
1449 if (abbrev_number == 0) {
1450 reader->level--;
1451 return NULL;
1452 }
1453
1454 if (!(reader->q = di_find_abbrev(reader, abbrev_number, errout))) return NULL;
1455
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++;
1461 }
1462 return die;
1463}
1464
1465static DebugInfoValue *
1466di_read_record(DebugInfoReader *reader, DebugInfoValue *vp, FILE *errout)
1467{
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;
1475}
1476
1477static bool
1478di_skip_records(DebugInfoReader *reader, FILE *errout)
1479{
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;
1486 }
1487}
1488
1489typedef 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;
1496
1497static bool
1498addr_header_init(obj_info_t *obj, addr_header_t *header, FILE *errout)
1499{
1500 const char *p = obj->debug_addr.ptr;
1501
1502 header->ptr = p;
1503
1504 if (!p) return true;
1505
1506 header->unit_length = *(uint32_t *)p;
1507 p += sizeof(uint32_t);
1508
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;
1514 }
1515
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;
1521 }
1522 p++; /* segment_selector_size */
1523 return true;
1524}
1525
1526static uint64_t
1527read_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];
1530 }
1531 else {
1532 return ((uint64_t*)(header->ptr + addr_base))[idx];
1533 }
1534}
1535
1536typedef 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;
1542
1543static bool
1544rnglists_header_init(obj_info_t *obj, rnglists_header_t *header, FILE *errout)
1545{
1546 const char *p = obj->debug_rnglists.ptr;
1547
1548 if (!p) return true;
1549
1550 header->unit_length = *(uint32_t *)p;
1551 p += sizeof(uint32_t);
1552
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;
1558 }
1559
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;
1565 }
1566 p++; /* segment_selector_size */
1567 header->offset_entry_count = *(uint32_t *)p;
1568 return true;
1569}
1570
1571typedef 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;
1579
1580static void
1581ranges_set(ranges_t *ptr, DebugInfoValue *v, addr_header_t *addr_header, uint64_t addr_base)
1582{
1583 uint64_t n = 0;
1584 if (v->type == VAL_uint) {
1585 n = v->as.uint64;
1586 }
1587 else if (v->type == VAL_addr) {
1588 n = read_addr(addr_header, addr_base, v->as.addr_idx);
1589 }
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;
1598 }
1599 else {
1600 ptr->high_pc = ptr->low_pc + n;
1601 }
1602 ptr->high_pc_set = true;
1603 break;
1604 case DW_AT_ranges:
1605 ptr->ranges = n;
1606 ptr->ranges_set = true;
1607 break;
1608 }
1609}
1610
1611static uint64_t
1612read_dw_form_addr(DebugInfoReader *reader, const char **ptr, FILE *errout)
1613{
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);
1620 }
1621}
1622
1623static uintptr_t
1624ranges_include(DebugInfoReader *reader, ranges_t *ptr, uint64_t addr, rnglists_header_t *rnglists_header, FILE *errout)
1625{
1626 if (ptr->high_pc_set) {
1627 if (ptr->ranges_set || !ptr->low_pc_set) {
1628 return UINTPTR_MAX;
1629 }
1630 if (ptr->low_pc <= addr && addr <= ptr->high_pc) {
1631 return (uintptr_t)ptr->low_pc;
1632 }
1633 }
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;
1643 }
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];
1649 }
1650 else {
1651 p = offset_array + ((uint64_t *)offset_array)[ptr->ranges];
1652 }
1653 }
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;
1688 }
1689 if (from <= addr && addr < to) {
1690 return from;
1691 }
1692 }
1693 return 0;
1694 }
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;
1703 }
1704 else if (base + from <= addr && addr < base + to) {
1705 return (uintptr_t)base + from;
1706 }
1707 }
1708 }
1709 else if (ptr->low_pc_set) {
1710 if (ptr->low_pc == addr) {
1711 return (uintptr_t)ptr->low_pc;
1712 }
1713 }
1714 return 0;
1715}
1716
1717#if 0
1718static void
1719ranges_inspect(DebugInfoReader *reader, ranges_t *ptr, FILE *errout)
1720{
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;
1725 }
1726 kprintf("low_pc:%"PRIx64" high_pc:%"PRIx64"\n",ptr->low_pc,ptr->high_pc);
1727 }
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);
1736 }
1737 kprintf("\n");
1738 }
1739 else if (ptr->low_pc_set) {
1740 kprintf("low_pc:%"PRIx64"\n",ptr->low_pc);
1741 }
1742 else {
1743 kprintf("empty\n");
1744 }
1745}
1746#endif
1747
1748static int
1749di_read_cu(DebugInfoReader *reader, FILE *errout)
1750{
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;
1760 }
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;
1766 }
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);
1771 }
1772 else {
1773 debug_abbrev_offset = read_uint(reader);
1774 reader->address_size = read_uint8(&reader->p);
1775 }
1776 if (reader->address_size != 4 && reader->address_size != 8) {
1777 kprintf("unknown address_size:%d", reader->address_size);
1778 return -1;
1779 }
1780 reader->q0 = reader->obj->debug_abbrev.ptr + debug_abbrev_offset;
1781
1782 reader->level = 0;
1783 di_read_debug_abbrev_cu(reader);
1784 if (di_read_debug_line_cu(reader, errout)) return -1;
1785
1786 do {
1787 DIE die;
1788
1789 if (!di_read_die(reader, &die, errout)) continue;
1790
1791 if (die.tag != DW_TAG_compile_unit) {
1792 if (!di_skip_records(reader, errout)) return -1;
1793 break;
1794 }
1795
1796 reader->current_str_offsets_base = 0;
1797 reader->current_addr_base = 0;
1798 reader->current_rnglists_base = 0;
1799
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;
1820 }
1821 }
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:
1828 {
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);
1832 }
1833 break;
1834 }
1835 } while (0);
1836
1837 return 0;
1838}
1839
1840static void
1841read_abstract_origin(DebugInfoReader *reader, uint64_t form, uint64_t abstract_origin, line_info_t *line, FILE *errout)
1842{
1843 const char *p = reader->p;
1844 const char *q = reader->q;
1845 int level = reader->level;
1846 DIE die;
1847
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;
1865 }
1866 if (!di_read_die(reader, &die, errout)) goto finish;
1867
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;
1876 }
1877 }
1878
1879 finish:
1880 reader->p = p;
1881 reader->q = q;
1882 reader->level = level;
1883}
1884
1885static bool
1886debug_info_read(DebugInfoReader *reader, int num_traces, void **traces,
1887 line_info_t *lines, int offset, FILE *errout)
1888{
1889
1890 addr_header_t addr_header = {0};
1891 if (!addr_header_init(reader->obj, &addr_header, errout)) return false;
1892
1893 rnglists_header_t rnglists_header = {0};
1894 if (!rnglists_header_init(reader->obj, &rnglists_header, errout)) return false;
1895
1896 while (reader->p < reader->cu_end) {
1897 DIE die;
1898 ranges_t ranges = {0};
1899 line_info_t line = {0};
1900
1901 if (!di_read_die(reader, &die, errout)) continue;
1902 /* kprintf("%d:%tx: <%d>\n",__LINE__,die.pos,reader->level,die.tag); */
1903
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;
1908 }
1909
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; */
1940 }
1941 }
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;
1959 }
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;
1964 }
1965 }
1966 }
1967 return true;
1968}
1969
1970// This function parses the following attributes of Line Number Program Header in DWARF 5:
1971//
1972// * directory_entry_format_count
1973// * directory_entry_format
1974// * directories_count
1975// * directories
1976//
1977// or
1978//
1979// * file_name_entry_format_count
1980// * file_name_entry_format
1981// * file_names_count
1982// * file_names
1983//
1984// It records DW_LNCT_path and DW_LNCT_directory_index at the index "idx".
1985static const char *
1986parse_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)
1989{
1990 int i, j;
1991 int entry_format_count = *(uint8_t *)p++;
1992 const char *entry_format = p;
1993
1994 /* skip the part of entry_format */
1995 for (i = 0; i < entry_format_count * 2; i++) uleb128(&p);
1996
1997 int entry_count = (int)uleb128(&p);
1998
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;
2014 }
2015 if (j == idx) return 0;
2016 }
2017
2018 return reader.p;
2019}
2020
2021#ifdef USE_ELF
2022static unsigned long
2023uncompress_debug_section(ElfW(Shdr) *shdr, char *file, char **ptr)
2024{
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;
2030
2031 if (chdr->ch_type != ELFCOMPRESS_ZLIB) {
2032 /* unsupported compression type */
2033 return 0;
2034 }
2035
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;
2043
2044fail:
2045 free(*ptr);
2046 *ptr = NULL;
2047#endif
2048 return 0;
2049}
2050
2051/* read file and fill lines */
2052static uintptr_t
2053fill_lines(int num_traces, void **traces, int check_debuglink,
2054 obj_info_t **objp, line_info_t *lines, int offset, FILE *errout)
2055{
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;
2069
2070 fd = open(binary_filename, O_RDONLY);
2071 if (fd < 0) {
2072 goto fail;
2073 }
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;
2080 }
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;
2086 }
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;
2096 }
2097 close(fd);
2098
2099 ehdr = (ElfW(Ehdr) *)file;
2100 if (memcmp(ehdr->e_ident, "\177ELF", 4) != 0) {
2101 /*
2102 * Huh? Maybe filename was overridden by setproctitle() and
2103 * it match non-elf file.
2104 */
2105 goto fail;
2106 }
2107 obj->mapped = file;
2108 obj->mapped_size = (size_t)filesize;
2109
2110 shdr = (ElfW(Shdr) *)(file + ehdr->e_shoff);
2111
2112 shstr_shdr = shdr + ehdr->e_shstrndx;
2113 shstr = file + shstr_shdr->sh_offset;
2114
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;
2121 }
2122 else if (!strcmp(section_name, ".dynstr")) {
2123 dynstr_shdr = shdr + i;
2124 }
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;
2137 }
2138 break;
2139 case SHT_PROGBITS:
2140 if (!strcmp(section_name, ".gnu_debuglink")) {
2141 gnu_debuglink_shdr = shdr + i;
2142 }
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"
2154 };
2155
2156 for (j=0; j < DWARF_SECTION_COUNT; j++) {
2157 struct dwarf_section *s = obj_dwarf_section_at(obj, j);
2158
2159 if (strcmp(section_name, debug_section_names[j]) != 0)
2160 continue;
2161
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;
2168 }
2169 break;
2170 }
2171 }
2172 break;
2173 }
2174 }
2175
2176 if (offset == 0) {
2177 /* main executable */
2178 if (dynsym_shdr && dynstr_shdr) {
2179 char *strtab = file + dynstr_shdr->sh_offset;
2180 ElfW(Sym) *symtab = (ElfW(Sym) *)(file + dynsym_shdr->sh_offset);
2181 int symtab_count = (int)(dynsym_shdr->sh_size / sizeof(ElfW(Sym)));
2182 void *handle = dlopen(NULL, RTLD_NOW|RTLD_LOCAL);
2183 if (handle) {
2184 for (j = 0; j < symtab_count; j++) {
2185 ElfW(Sym) *sym = &symtab[j];
2186 Dl_info info;
2187 void *s;
2188 if (ELF_ST_TYPE(sym->st_info) != STT_FUNC || sym->st_size == 0) continue;
2189 s = dlsym(handle, strtab + sym->st_name);
2190 if (s && dladdr(s, &info)) {
2191 obj->base_addr = dladdr_fbase;
2192 dladdr_fbase = (uintptr_t)info.dli_fbase;
2193 break;
2194 }
2195 }
2196 dlclose(handle);
2197 }
2198 if (ehdr->e_type == ET_EXEC) {
2199 obj->base_addr = 0;
2200 }
2201 else {
2202 /* PIE (position-independent executable) */
2203 obj->base_addr = dladdr_fbase;
2204 }
2205 }
2206 }
2207
2208 if (obj->debug_info.ptr && obj->debug_abbrev.ptr) {
2209 DebugInfoReader reader;
2210 debug_info_reader_init(&reader, obj);
2211 i = 0;
2212 while (reader.p < reader.pend) {
2213 /* kprintf("%d:%tx: CU[%d]\n", __LINE__, reader.p - reader.obj->debug_info.ptr, i++); */
2214 if (di_read_cu(&reader, errout)) goto use_symtab;
2215 if (!debug_info_read(&reader, num_traces, traces, lines, offset, errout))
2216 goto use_symtab;
2217 }
2218 }
2219 else {
2220 /* This file doesn't have dwarf, use symtab or dynsym */
2221use_symtab:
2222 if (!symtab_shdr) {
2223 /* This file doesn't have symtab, use dynsym instead */
2224 symtab_shdr = dynsym_shdr;
2225 strtab_shdr = dynstr_shdr;
2226 }
2227
2228 if (symtab_shdr && strtab_shdr) {
2229 char *strtab = file + strtab_shdr->sh_offset;
2230 ElfW(Sym) *symtab = (ElfW(Sym) *)(file + symtab_shdr->sh_offset);
2231 int symtab_count = (int)(symtab_shdr->sh_size / sizeof(ElfW(Sym)));
2232 for (j = 0; j < symtab_count; j++) {
2233 ElfW(Sym) *sym = &symtab[j];
2234 uintptr_t saddr = (uintptr_t)sym->st_value + obj->base_addr;
2235 if (ELF_ST_TYPE(sym->st_info) != STT_FUNC) continue;
2236 for (i = offset; i < num_traces; i++) {
2237 uintptr_t d = (uintptr_t)traces[i] - saddr;
2238 if (lines[i].line > 0 || d > (uintptr_t)sym->st_size)
2239 continue;
2240 /* fill symbol name and addr from .symtab */
2241 if (!lines[i].sname) lines[i].sname = strtab + sym->st_name;
2242 lines[i].saddr = saddr;
2243 lines[i].path = obj->path;
2244 lines[i].base_addr = obj->base_addr;
2245 }
2246 }
2247 }
2248 }
2249
2250 if (!obj->debug_line.ptr) {
2251 /* This file doesn't have .debug_line section,
2252 let's check .gnu_debuglink section instead. */
2253 if (gnu_debuglink_shdr && check_debuglink) {
2254 follow_debuglink(file + gnu_debuglink_shdr->sh_offset,
2255 num_traces, traces,
2256 objp, lines, offset, errout);
2257 }
2258 if (note_gnu_build_id && check_debuglink) {
2259 ElfW(Nhdr) *nhdr = (ElfW(Nhdr)*) (file + note_gnu_build_id->sh_offset);
2260 const char *build_id = (char *)(nhdr + 1) + nhdr->n_namesz;
2261 follow_debuglink_build_id(build_id, nhdr->n_descsz,
2262 num_traces, traces,
2263 objp, lines, offset, errout);
2264 }
2265 goto finish;
2266 }
2267
2268 if (parse_debug_line(num_traces, traces,
2269 obj->debug_line.ptr,
2270 obj->debug_line.size,
2271 obj, lines, offset, errout) == -1)
2272 goto fail;
2273
2274finish:
2275 return dladdr_fbase;
2276fail:
2277 return (uintptr_t)-1;
2278}
2279#else /* Mach-O */
2280/* read file and fill lines */
2281static uintptr_t
2282fill_lines(int num_traces, void **traces, int check_debuglink,
2283 obj_info_t **objp, line_info_t *lines, int offset, FILE *errout)
2284{
2285# ifdef __LP64__
2286# define LP(x) x##_64
2287# else
2288# define LP(x) x
2289# endif
2290 int fd;
2291 off_t filesize;
2292 char *file, *p = NULL;
2293 obj_info_t *obj = *objp;
2294 struct LP(mach_header) *header;
2295 uintptr_t dladdr_fbase = 0;
2296
2297 {
2298 char *s = binary_filename;
2299 char *base = strrchr(binary_filename, '/')+1;
2300 size_t max = PATH_MAX;
2301 size_t size = strlen(binary_filename);
2302 size_t basesize = size - (base - binary_filename);
2303 s += size;
2304 max -= size;
2305 p = s;
2306 size = strlcpy(s, ".dSYM/Contents/Resources/DWARF/", max);
2307 if (size == 0) goto fail;
2308 s += size;
2309 max -= size;
2310 if (max <= basesize) goto fail;
2311 memcpy(s, base, basesize);
2312 s[basesize] = 0;
2313
2314 fd = open(binary_filename, O_RDONLY);
2315 if (fd < 0) {
2316 *p = 0; /* binary_filename becomes original file name */
2317 fd = open(binary_filename, O_RDONLY);
2318 if (fd < 0) {
2319 goto fail;
2320 }
2321 }
2322 }
2323
2324 filesize = lseek(fd, 0, SEEK_END);
2325 if (filesize < 0) {
2326 int e = errno;
2327 close(fd);
2328 kprintf("lseek: %s\n", strerror(e));
2329 goto fail;
2330 }
2331#if SIZEOF_OFF_T > SIZEOF_SIZE_T
2332 if (filesize > (off_t)SIZE_MAX) {
2333 close(fd);
2334 kprintf("Too large file %s\n", binary_filename);
2335 goto fail;
2336 }
2337#endif
2338 lseek(fd, 0, SEEK_SET);
2339 /* async-signal unsafe */
2340 file = (char *)mmap(NULL, (size_t)filesize, PROT_READ, MAP_SHARED, fd, 0);
2341 if (file == MAP_FAILED) {
2342 int e = errno;
2343 close(fd);
2344 kprintf("mmap: %s\n", strerror(e));
2345 goto fail;
2346 }
2347 close(fd);
2348
2349 obj->mapped = file;
2350 obj->mapped_size = (size_t)filesize;
2351
2352 header = (struct LP(mach_header) *)file;
2353 if (header->magic == LP(MH_MAGIC)) {
2354 /* non universal binary */
2355 p = file;
2356 }
2357 else if (header->magic == FAT_CIGAM) {
2358 struct LP(mach_header) *mhp = _NSGetMachExecuteHeader();
2359 struct fat_header *fat = (struct fat_header *)file;
2360 char *q = file + sizeof(*fat);
2361 uint32_t nfat_arch = __builtin_bswap32(fat->nfat_arch);
2362 /* kprintf("%d: fat:%s %d\n",__LINE__, binary_filename,nfat_arch); */
2363 for (uint32_t i = 0; i < nfat_arch; i++) {
2364 struct fat_arch *arch = (struct fat_arch *)q;
2365 cpu_type_t cputype = __builtin_bswap32(arch->cputype);
2366 cpu_subtype_t cpusubtype = __builtin_bswap32(arch->cpusubtype);
2367 uint32_t offset = __builtin_bswap32(arch->offset);
2368 /* kprintf("%d: fat %d %x/%x %x/%x\n",__LINE__, i, mhp->cputype,mhp->cpusubtype, cputype,cpusubtype); */
2369 if (mhp->cputype == cputype &&
2370 (cpu_subtype_t)(mhp->cpusubtype & ~CPU_SUBTYPE_MASK) == cpusubtype) {
2371 p = file + offset;
2372 file = p;
2373 header = (struct LP(mach_header) *)p;
2374 if (header->magic == LP(MH_MAGIC)) {
2375 goto found_mach_header;
2376 }
2377 break;
2378 }
2379 q += sizeof(*arch);
2380 }
2381 kprintf("'%s' is not a Mach-O universal binary file!\n",binary_filename);
2382 close(fd);
2383 goto fail;
2384 }
2385 else {
2386# ifdef __LP64__
2387# define bitsize "64"
2388# else
2389# define bitsize "32"
2390# endif
2391 kprintf("'%s' is not a " bitsize
2392 "-bit Mach-O file!\n",binary_filename);
2393# undef bitsize
2394 close(fd);
2395 goto fail;
2396 }
2397found_mach_header:
2398 p += sizeof(*header);
2399
2400 for (uint32_t i = 0; i < (uint32_t)header->ncmds; i++) {
2401 struct load_command *lcmd = (struct load_command *)p;
2402 switch (lcmd->cmd) {
2403 case LP(LC_SEGMENT):
2404 {
2405 static const char *debug_section_names[] = {
2406 "__debug_abbrev",
2407 "__debug_info",
2408 "__debug_line",
2409 "__debug_ranges",
2410 "__debug_str_offsets",
2411 "__debug_addr",
2412 "__debug_rnglists",
2413 "__debug_str",
2414 "__debug_line_str",
2415 };
2416 struct LP(segment_command) *scmd = (struct LP(segment_command) *)lcmd;
2417 if (strcmp(scmd->segname, "__TEXT") == 0) {
2418 obj->vmaddr = scmd->vmaddr;
2419 }
2420 else if (strcmp(scmd->segname, "__DWARF") == 0) {
2421 p += sizeof(struct LP(segment_command));
2422 for (uint64_t i = 0; i < scmd->nsects; i++) {
2423 struct LP(section) *sect = (struct LP(section) *)p;
2424 p += sizeof(struct LP(section));
2425 for (int j=0; j < DWARF_SECTION_COUNT; j++) {
2426 struct dwarf_section *s = obj_dwarf_section_at(obj, j);
2427
2428 if (strcmp(sect->sectname, debug_section_names[j]) != 0
2429#ifdef __APPLE__
2430 /* macOS clang 16 generates DWARF5, which have Mach-O
2431 * section names that are limited to 16 characters,
2432 * which causes sections with long names to be truncated
2433 * and not match above.
2434 * See: https://wiki.dwarfstd.org/Best_Practices.md#Mach-2d-O
2435 */
2436 && strncmp(sect->sectname, debug_section_names[j], 16) != 0
2437#endif
2438 )
2439 continue;
2440
2441 s->ptr = file + sect->offset;
2442 s->size = sect->size;
2443 s->flags = sect->flags;
2444 if (s->flags & SHF_COMPRESSED) {
2445 goto fail;
2446 }
2447 break;
2448 }
2449 }
2450 }
2451 }
2452 break;
2453
2454 case LC_SYMTAB:
2455 {
2456 struct symtab_command *cmd = (struct symtab_command *)lcmd;
2457 struct LP(nlist) *nl = (struct LP(nlist) *)(file + cmd->symoff);
2458 char *strtab = file + cmd->stroff, *sname = 0;
2459 uint32_t j;
2460 uintptr_t saddr = 0;
2461 /* kprintf("[%2d]: %x/symtab %p\n", i, cmd->cmd, (void *)p); */
2462 for (j = 0; j < cmd->nsyms; j++) {
2463 uintptr_t symsize, d;
2464 struct LP(nlist) *e = &nl[j];
2465 /* 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); */
2466 if (e->n_type != N_FUN) continue;
2467 if (e->n_sect) {
2468 saddr = (uintptr_t)e->n_value + obj->base_addr - obj->vmaddr;
2469 sname = strtab + e->n_un.n_strx;
2470 /* 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); */
2471 continue;
2472 }
2473 for (int k = offset; k < num_traces; k++) {
2474 d = (uintptr_t)traces[k] - saddr;
2475 symsize = e->n_value;
2476 /* kprintf("%lx %lx %lx\n",saddr,symsize,traces[k]); */
2477 if (lines[k].line > 0 || d > (uintptr_t)symsize)
2478 continue;
2479 /* fill symbol name and addr from .symtab */
2480 if (!lines[k].sname) lines[k].sname = sname;
2481 lines[k].saddr = saddr;
2482 lines[k].path = obj->path;
2483 lines[k].base_addr = obj->base_addr;
2484 }
2485 }
2486 }
2487 }
2488 p += lcmd->cmdsize;
2489 }
2490
2491 if (obj->debug_info.ptr && obj->debug_abbrev.ptr) {
2492 DebugInfoReader reader;
2493 debug_info_reader_init(&reader, obj);
2494 while (reader.p < reader.pend) {
2495 if (di_read_cu(&reader, errout)) goto fail;
2496 if (!debug_info_read(&reader, num_traces, traces, lines, offset, errout))
2497 goto fail;
2498 }
2499 }
2500
2501 if (parse_debug_line(num_traces, traces,
2502 obj->debug_line.ptr,
2503 obj->debug_line.size,
2504 obj, lines, offset, errout) == -1)
2505 goto fail;
2506
2507 return dladdr_fbase;
2508fail:
2509 return (uintptr_t)-1;
2510}
2511#endif
2512
2513#define HAVE_MAIN_EXE_PATH
2514#if defined(__FreeBSD__) || defined(__DragonFly__)
2515# include <sys/sysctl.h>
2516#endif
2517/* ssize_t main_exe_path(FILE *errout)
2518 *
2519 * store the path of the main executable to `binary_filename`,
2520 * and returns strlen(binary_filename).
2521 * it is NUL terminated.
2522 */
2523#if defined(__linux__) || defined(__NetBSD__)
2524static ssize_t
2525main_exe_path(FILE *errout)
2526{
2527# if defined(__linux__)
2528# define PROC_SELF_EXE "/proc/self/exe"
2529# elif defined(__NetBSD__)
2530# define PROC_SELF_EXE "/proc/curproc/exe"
2531# endif
2532 ssize_t len = readlink(PROC_SELF_EXE, binary_filename, PATH_MAX);
2533 if (len < 0) return 0;
2534 binary_filename[len] = 0;
2535 return len;
2536}
2537#elif defined(__FreeBSD__) || defined(__DragonFly__)
2538static ssize_t
2539main_exe_path(FILE *errout)
2540{
2541 int mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1};
2542 size_t len = PATH_MAX;
2543 int err = sysctl(mib, 4, binary_filename, &len, NULL, 0);
2544 if (err) {
2545 kprintf("Can't get the path of ruby");
2546 return -1;
2547 }
2548 len--; /* sysctl sets strlen+1 */
2549 return len;
2550}
2551#elif defined(HAVE_LIBPROC_H)
2552static ssize_t
2553main_exe_path(FILE *errout)
2554{
2555 int len = proc_pidpath(getpid(), binary_filename, PATH_MAX);
2556 if (len == 0) return 0;
2557 binary_filename[len] = 0;
2558 return len;
2559}
2560#else
2561#undef HAVE_MAIN_EXE_PATH
2562#endif
2563
2564static void
2565print_line0(line_info_t *line, void *address, FILE *errout)
2566{
2567 uintptr_t addr = (uintptr_t)address;
2568 uintptr_t d = addr - line->saddr;
2569 if (!address) {
2570 /* inlined */
2571 if (line->dirname && line->dirname[0]) {
2572 kprintf("%s(%s) %s/%s:%d\n", line->path, line->sname, line->dirname, line->filename, line->line);
2573 }
2574 else {
2575 kprintf("%s(%s) %s:%d\n", line->path, line->sname, line->filename, line->line);
2576 }
2577 }
2578 else if (!line->path) {
2579 kprintf("[0x%"PRIxPTR"]\n", addr);
2580 }
2581 else if (!line->sname) {
2582 kprintf("%s(0x%"PRIxPTR") [0x%"PRIxPTR"]\n", line->path, addr-line->base_addr, addr);
2583 }
2584 else if (!line->saddr) {
2585 kprintf("%s(%s) [0x%"PRIxPTR"]\n", line->path, line->sname, addr);
2586 }
2587 else if (line->line <= 0) {
2588 kprintf("%s(%s+0x%"PRIxPTR") [0x%"PRIxPTR"]\n", line->path, line->sname,
2589 d, addr);
2590 }
2591 else if (!line->filename) {
2592 kprintf("%s(%s+0x%"PRIxPTR") [0x%"PRIxPTR"] ???:%d\n", line->path, line->sname,
2593 d, addr, line->line);
2594 }
2595 else if (line->dirname && line->dirname[0]) {
2596 kprintf("%s(%s+0x%"PRIxPTR") [0x%"PRIxPTR"] %s/%s:%d\n", line->path, line->sname,
2597 d, addr, line->dirname, line->filename, line->line);
2598 }
2599 else {
2600 kprintf("%s(%s+0x%"PRIxPTR") [0x%"PRIxPTR"] %s:%d\n", line->path, line->sname,
2601 d, addr, line->filename, line->line);
2602 }
2603}
2604
2605static void
2606print_line(line_info_t *line, void *address, FILE *errout)
2607{
2608 print_line0(line, address, errout);
2609 if (line->next) {
2610 print_line(line->next, NULL, errout);
2611 }
2612}
2613
2614void
2615rb_dump_backtrace_with_lines(int num_traces, void **traces, FILE *errout)
2616{
2617 int i;
2618 /* async-signal unsafe */
2619 line_info_t *lines = (line_info_t *)calloc(num_traces, sizeof(line_info_t));
2620 obj_info_t *obj = NULL;
2621 /* 2 is NULL + main executable */
2622 void **dladdr_fbases = (void **)calloc(num_traces+2, sizeof(void *));
2623
2624#ifdef HAVE_MAIN_EXE_PATH
2625 char *main_path = NULL; /* used on printing backtrace */
2626 ssize_t len;
2627 if ((len = main_exe_path(errout)) > 0) {
2628 main_path = (char *)alloca(len + 1);
2629 if (main_path) {
2630 uintptr_t addr;
2631 memcpy(main_path, binary_filename, len+1);
2632 append_obj(&obj);
2633 obj->path = main_path;
2634 addr = fill_lines(num_traces, traces, 1, &obj, lines, 0, errout);
2635 if (addr != (uintptr_t)-1) {
2636 dladdr_fbases[0] = (void *)addr;
2637 }
2638 }
2639 }
2640#endif
2641
2642 /* fill source lines by reading dwarf */
2643 for (i = 0; i < num_traces; i++) {
2644 Dl_info info;
2645 if (lines[i].line) continue;
2646 if (dladdr(traces[i], &info)) {
2647 const char *path;
2648 void **p;
2649
2650 /* skip symbols which is in already checked objects */
2651 /* if the binary is strip-ed, this may effect */
2652 for (p=dladdr_fbases; *p; p++) {
2653 if (*p == info.dli_fbase) {
2654 if (info.dli_fname) lines[i].path = info.dli_fname;
2655 if (info.dli_sname) lines[i].sname = info.dli_sname;
2656 goto next_line;
2657 }
2658 }
2659 *p = info.dli_fbase;
2660
2661 append_obj(&obj);
2662 obj->base_addr = (uintptr_t)info.dli_fbase;
2663 path = info.dli_fname;
2664 obj->path = path;
2665 if (path) lines[i].path = path;
2666 if (info.dli_sname) {
2667 lines[i].sname = info.dli_sname;
2668 lines[i].saddr = (uintptr_t)info.dli_saddr;
2669 }
2670 strlcpy(binary_filename, path, PATH_MAX);
2671 if (fill_lines(num_traces, traces, 1, &obj, lines, i, errout) == (uintptr_t)-1)
2672 break;
2673 }
2674next_line:
2675 continue;
2676 }
2677
2678 /* output */
2679 for (i = 0; i < num_traces; i++) {
2680 print_line(&lines[i], traces[i], errout);
2681
2682 /* FreeBSD's backtrace may show _start and so on */
2683 if (lines[i].sname && strcmp("main", lines[i].sname) == 0)
2684 break;
2685 }
2686
2687 /* free */
2688 while (obj) {
2689 obj_info_t *o = obj;
2690 for (i=0; i < DWARF_SECTION_COUNT; i++) {
2691 struct dwarf_section *s = obj_dwarf_section_at(obj, i);
2692 if (s->flags & SHF_COMPRESSED) {
2693 free(s->ptr);
2694 }
2695 }
2696 if (obj->mapped_size) {
2697 munmap(obj->mapped, obj->mapped_size);
2698 }
2699 obj = o->next;
2700 free(o);
2701 }
2702 for (i = 0; i < num_traces; i++) {
2703 line_info_t *line = lines[i].next;
2704 while (line) {
2705 line_info_t *l = line;
2706 line = line->next;
2707 free(l);
2708 }
2709 }
2710 free(lines);
2711 free(dladdr_fbases);
2712}
2713
2714#undef kprintf
2715
2716#else /* defined(USE_ELF) */
2717#error not supported
2718#endif
UNREACHABLE_RETURN
#define UNREACHABLE_RETURN
Old name of RBIMPL_UNREACHABLE_RETURN.
Definition assume.h:29
defines.h
off
int off
Offset inside of ptr.
Definition io.h:5
len
int len
Length of the buffer.
Definition io.h:8
ruby::backward::cxxanyargs::type
VALUE type(ANYARGS)
ANYARGS-ed function type.
Definition cxxanyargs.hpp:56
errno
#define errno
Ractor-aware version of errno.
Definition ruby.h:388
stdbool.h
C99 shim for <stdbool.h>
__sFILE
Definition vsnprintf.c:160