Ruby 3.2.4p170 (2024-04-23 revision af471c0e0127eea0cafa6f308c0425bbfab0acf5)
addr2line.c
1/**********************************************************************
2
3 addr2line.c -
4
5 $Author$
6
7 Copyright (C) 2010 Shinichiro Hamaji
8
9**********************************************************************/
10
11#if defined(__clang__)
12#pragma clang diagnostic ignored "-Wgnu-empty-initializer"
13#pragma clang diagnostic ignored "-Wgcc-compat"
14#endif
15
16#include "ruby/internal/config.h"
17#include "ruby/defines.h"
18#include "ruby/missing.h"
19#include "addr2line.h"
20
21#include <stdio.h>
22#include <errno.h>
23
24#ifdef HAVE_LIBPROC_H
25#include <libproc.h>
26#endif
27
29
30#if defined(USE_ELF) || defined(HAVE_MACH_O_LOADER_H)
31
32#include <fcntl.h>
33#include <limits.h>
34#include <stdio.h>
35#include <stdint.h>
36#include <stdlib.h>
37#include <string.h>
38#include <sys/mman.h>
39#include <sys/types.h>
40#include <sys/stat.h>
41#include <unistd.h>
42
43/* Make alloca work the best possible way. */
44#ifdef __GNUC__
45# ifndef alloca
46# define alloca __builtin_alloca
47# endif
48#else
49# ifdef HAVE_ALLOCA_H
50# include <alloca.h>
51# else
52# ifdef _AIX
53#pragma alloca
54# else
55# ifndef alloca /* predefined by HP cc +Olibcalls */
56void *alloca();
57# endif
58# endif /* AIX */
59# endif /* HAVE_ALLOCA_H */
60#endif /* __GNUC__ */
61
62#ifdef HAVE_DLADDR
63# include <dlfcn.h>
64#endif
65
66#ifdef HAVE_MACH_O_LOADER_H
67# include <crt_externs.h>
68# include <mach-o/fat.h>
69# include <mach-o/loader.h>
70# include <mach-o/nlist.h>
71# include <mach-o/stab.h>
72#endif
73
74#ifdef USE_ELF
75# ifdef __OpenBSD__
76# include <elf_abi.h>
77# else
78# include <elf.h>
79# endif
80
81#ifndef ElfW
82# if SIZEOF_VOIDP == 8
83# define ElfW(x) Elf64##_##x
84# else
85# define ElfW(x) Elf32##_##x
86# endif
87#endif
88#ifndef ELF_ST_TYPE
89# if SIZEOF_VOIDP == 8
90# define ELF_ST_TYPE ELF64_ST_TYPE
91# else
92# define ELF_ST_TYPE ELF32_ST_TYPE
93# endif
94#endif
95#endif
96
97#ifdef SHF_COMPRESSED
98# if defined(ELFCOMPRESS_ZLIB) && defined(HAVE_LIBZ)
99 /* FreeBSD 11.0 lacks ELFCOMPRESS_ZLIB */
100# include <zlib.h>
101# define SUPPORT_COMPRESSED_DEBUG_LINE
102# endif
103#else /* compatibility with glibc < 2.22 */
104# define SHF_COMPRESSED 0
105#endif
106
107#ifndef PATH_MAX
108#define PATH_MAX 4096
109#endif
110
111#define DW_LNS_copy 0x01
112#define DW_LNS_advance_pc 0x02
113#define DW_LNS_advance_line 0x03
114#define DW_LNS_set_file 0x04
115#define DW_LNS_set_column 0x05
116#define DW_LNS_negate_stmt 0x06
117#define DW_LNS_set_basic_block 0x07
118#define DW_LNS_const_add_pc 0x08
119#define DW_LNS_fixed_advance_pc 0x09
120#define DW_LNS_set_prologue_end 0x0a /* DWARF3 */
121#define DW_LNS_set_epilogue_begin 0x0b /* DWARF3 */
122#define DW_LNS_set_isa 0x0c /* DWARF3 */
123
124/* Line number extended opcode name. */
125#define DW_LNE_end_sequence 0x01
126#define DW_LNE_set_address 0x02
127#define DW_LNE_define_file 0x03
128#define DW_LNE_set_discriminator 0x04 /* DWARF4 */
129
130PRINTF_ARGS(static int kprintf(const char *fmt, ...), 1, 2);
131
132typedef struct line_info {
133 const char *dirname;
134 const char *filename;
135 const char *path; /* object path */
136 int line;
137
138 uintptr_t base_addr;
139 uintptr_t saddr;
140 const char *sname; /* function name */
141
142 struct line_info *next;
143} line_info_t;
144
145struct dwarf_section {
146 char *ptr;
147 size_t size;
148 uint64_t flags;
149};
150
151typedef struct obj_info {
152 const char *path; /* object path */
153 char *mapped;
154 size_t mapped_size;
155 void *uncompressed;
156 uintptr_t base_addr;
157 uintptr_t vmaddr;
158 struct dwarf_section debug_abbrev;
159 struct dwarf_section debug_info;
160 struct dwarf_section debug_line;
161 struct dwarf_section debug_ranges;
162 struct dwarf_section debug_str_offsets;
163 struct dwarf_section debug_addr;
164 struct dwarf_section debug_rnglists;
165 struct dwarf_section debug_str;
166 struct dwarf_section debug_line_str;
167 struct obj_info *next;
168} obj_info_t;
169
170#define DWARF_SECTION_COUNT 9
171
172static struct dwarf_section *
173obj_dwarf_section_at(obj_info_t *obj, int n)
174{
175 struct dwarf_section *ary[] = {
176 &obj->debug_abbrev,
177 &obj->debug_info,
178 &obj->debug_line,
179 &obj->debug_ranges,
180 &obj->debug_str_offsets,
181 &obj->debug_addr,
182 &obj->debug_rnglists,
183 &obj->debug_str,
184 &obj->debug_line_str
185 };
186 if (n < 0 || DWARF_SECTION_COUNT <= n) {
187 abort();
188 }
189 return ary[n];
190}
191
192struct debug_section_definition {
193 const char *name;
194 struct dwarf_section *dwarf;
195};
196
197/* Avoid consuming stack as this module may be used from signal handler */
198static char binary_filename[PATH_MAX + 1];
199
200static unsigned long
201uleb128(const char **p)
202{
203 unsigned long r = 0;
204 int s = 0;
205 for (;;) {
206 unsigned char b = (unsigned char)*(*p)++;
207 if (b < 0x80) {
208 r += (unsigned long)b << s;
209 break;
210 }
211 r += (b & 0x7f) << s;
212 s += 7;
213 }
214 return r;
215}
216
217static long
218sleb128(const char **p)
219{
220 long r = 0;
221 int s = 0;
222 for (;;) {
223 unsigned char b = (unsigned char)*(*p)++;
224 if (b < 0x80) {
225 if (b & 0x40) {
226 r -= (0x80 - b) << s;
227 }
228 else {
229 r += (b & 0x3f) << s;
230 }
231 break;
232 }
233 r += (b & 0x7f) << s;
234 s += 7;
235 }
236 return r;
237}
238
239static const char *
240get_nth_dirname(unsigned long dir, const char *p)
241{
242 if (!dir--) {
243 return "";
244 }
245 while (dir--) {
246 while (*p) p++;
247 p++;
248 if (!*p) {
249 kprintf("Unexpected directory number %lu in %s\n",
250 dir, binary_filename);
251 return "";
252 }
253 }
254 return p;
255}
256
257static const char *parse_ver5_debug_line_header(const char *p, int idx, uint8_t format, obj_info_t *obj, const char **out_path, uint64_t *out_directory_index);
258
259static void
260fill_filename(int file, uint8_t format, uint16_t version, const char *include_directories, const char *filenames, line_info_t *line, obj_info_t *obj)
261{
262 int i;
263 const char *p = filenames;
264 const char *filename;
265 unsigned long dir;
266 if (version >= 5) {
267 const char *path;
268 uint64_t directory_index = -1;
269 parse_ver5_debug_line_header(filenames, file, format, obj, &path, &directory_index);
270 line->filename = path;
271 parse_ver5_debug_line_header(include_directories, (int)directory_index, format, obj, &path, NULL);
272 line->dirname = path;
273 }
274 else {
275 for (i = 1; i <= file; i++) {
276 filename = p;
277 if (!*p) {
278 /* Need to output binary file name? */
279 kprintf("Unexpected file number %d in %s at %tx\n",
280 file, binary_filename, filenames - obj->mapped);
281 return;
282 }
283 while (*p) p++;
284 p++;
285 dir = uleb128(&p);
286 /* last modified. */
287 uleb128(&p);
288 /* size of the file. */
289 uleb128(&p);
290
291 if (i == file) {
292 line->filename = filename;
293 line->dirname = get_nth_dirname(dir, include_directories);
294 }
295 }
296 }
297}
298
299static void
300fill_line(int num_traces, void **traces, uintptr_t addr, int file, int line,
301 uint8_t format, uint16_t version, const char *include_directories, const char *filenames,
302 obj_info_t *obj, line_info_t *lines, int offset)
303{
304 int i;
305 addr += obj->base_addr - obj->vmaddr;
306 for (i = offset; i < num_traces; i++) {
307 uintptr_t a = (uintptr_t)traces[i];
308 /* We assume one line code doesn't result >100 bytes of native code.
309 We may want more reliable way eventually... */
310 if (addr < a && a < addr + 100) {
311 fill_filename(file, format, version, include_directories, filenames, &lines[i], obj);
312 lines[i].line = line;
313 }
314 }
315}
316
317struct LineNumberProgramHeader {
318 uint64_t unit_length;
319 uint16_t version;
320 uint8_t format; /* 4 or 8 */
321 uint64_t header_length;
322 uint8_t minimum_instruction_length;
323 uint8_t maximum_operations_per_instruction;
324 uint8_t default_is_stmt;
325 int8_t line_base;
326 uint8_t line_range;
327 uint8_t opcode_base;
328 /* uint8_t standard_opcode_lengths[opcode_base-1]; */
329 const char *include_directories;
330 const char *filenames;
331 const char *cu_start;
332 const char *cu_end;
333};
334
335static int
336parse_debug_line_header(obj_info_t *obj, const char **pp, struct LineNumberProgramHeader *header)
337{
338 const char *p = *pp;
339 header->unit_length = *(uint32_t *)p;
340 p += sizeof(uint32_t);
341
342 header->format = 4;
343 if (header->unit_length == 0xffffffff) {
344 header->unit_length = *(uint64_t *)p;
345 p += sizeof(uint64_t);
346 header->format = 8;
347 }
348
349 header->cu_end = p + header->unit_length;
350
351 header->version = *(uint16_t *)p;
352 p += sizeof(uint16_t);
353 if (header->version > 5) return -1;
354
355 if (header->version >= 5) {
356 /* address_size = *(uint8_t *)p++; */
357 /* segment_selector_size = *(uint8_t *)p++; */
358 p += 2;
359 }
360
361 header->header_length = header->format == 4 ? *(uint32_t *)p : *(uint64_t *)p;
362 p += header->format;
363 header->cu_start = p + header->header_length;
364
365 header->minimum_instruction_length = *(uint8_t *)p++;
366
367 if (header->version >= 4) {
368 /* maximum_operations_per_instruction = *(uint8_t *)p; */
369 if (*p != 1) return -1; /* For non-VLIW architectures, this field is 1 */
370 p++;
371 }
372
373 header->default_is_stmt = *(uint8_t *)p++;
374 header->line_base = *(int8_t *)p++;
375 header->line_range = *(uint8_t *)p++;
376 header->opcode_base = *(uint8_t *)p++;
377 /* header->standard_opcode_lengths = (uint8_t *)p - 1; */
378 p += header->opcode_base - 1;
379
380 if (header->version >= 5) {
381 header->include_directories = p;
382 p = parse_ver5_debug_line_header(p, -1, header->format, obj, NULL, NULL);
383 header->filenames = p;
384 }
385 else {
386 header->include_directories = p;
387
388 /* temporary measure for compress-debug-sections */
389 if (p >= header->cu_end) return -1;
390
391 /* skip include directories */
392 while (*p) {
393 p = memchr(p, '\0', header->cu_end - p);
394 if (!p) return -1;
395 p++;
396 }
397 p++;
398
399 header->filenames = p;
400 }
401
402 *pp = header->cu_start;
403
404 return 0;
405}
406
407static int
408parse_debug_line_cu(int num_traces, void **traces, const char **debug_line,
409 obj_info_t *obj, line_info_t *lines, int offset)
410{
411 const char *p = (const char *)*debug_line;
412 struct LineNumberProgramHeader header;
413
414 /* The registers. */
415 unsigned long addr = 0;
416 unsigned int file = 1;
417 unsigned int line = 1;
418 /* unsigned int column = 0; */
419 int is_stmt;
420 /* int basic_block = 0; */
421 /* int end_sequence = 0; */
422 /* int prologue_end = 0; */
423 /* int epilogue_begin = 0; */
424 /* unsigned int isa = 0; */
425
426 if (parse_debug_line_header(obj, &p, &header))
427 return -1;
428 is_stmt = header.default_is_stmt;
429
430#define FILL_LINE() \
431 do { \
432 fill_line(num_traces, traces, addr, file, line, \
433 header.format, \
434 header.version, \
435 header.include_directories, \
436 header.filenames, \
437 obj, lines, offset); \
438 /*basic_block = prologue_end = epilogue_begin = 0;*/ \
439 } while (0)
440
441 while (p < header.cu_end) {
442 unsigned long a;
443 unsigned char op = *p++;
444 switch (op) {
445 case DW_LNS_copy:
446 FILL_LINE();
447 break;
448 case DW_LNS_advance_pc:
449 a = uleb128(&p) * header.minimum_instruction_length;
450 addr += a;
451 break;
452 case DW_LNS_advance_line: {
453 long a = sleb128(&p);
454 line += a;
455 break;
456 }
457 case DW_LNS_set_file:
458 file = (unsigned int)uleb128(&p);
459 break;
460 case DW_LNS_set_column:
461 /*column = (unsigned int)*/(void)uleb128(&p);
462 break;
463 case DW_LNS_negate_stmt:
464 is_stmt = !is_stmt;
465 break;
466 case DW_LNS_set_basic_block:
467 /*basic_block = 1; */
468 break;
469 case DW_LNS_const_add_pc:
470 a = ((255UL - header.opcode_base) / header.line_range) *
471 header.minimum_instruction_length;
472 addr += a;
473 break;
474 case DW_LNS_fixed_advance_pc:
475 a = *(uint16_t *)p;
476 p += sizeof(uint16_t);
477 addr += a;
478 break;
479 case DW_LNS_set_prologue_end:
480 /* prologue_end = 1; */
481 break;
482 case DW_LNS_set_epilogue_begin:
483 /* epilogue_begin = 1; */
484 break;
485 case DW_LNS_set_isa:
486 /* isa = (unsigned int)*/(void)uleb128(&p);
487 break;
488 case 0:
489 a = uleb128(&p);
490 op = *p++;
491 switch (op) {
492 case DW_LNE_end_sequence:
493 /* end_sequence = 1; */
494 FILL_LINE();
495 addr = 0;
496 file = 1;
497 line = 1;
498 /* column = 0; */
499 is_stmt = header.default_is_stmt;
500 /* end_sequence = 0; */
501 /* isa = 0; */
502 break;
503 case DW_LNE_set_address:
504 addr = *(unsigned long *)p;
505 p += sizeof(unsigned long);
506 break;
507 case DW_LNE_define_file:
508 kprintf("Unsupported operation in %s\n",
509 binary_filename);
510 break;
511 case DW_LNE_set_discriminator:
512 /* TODO:currently ignore */
513 uleb128(&p);
514 break;
515 default:
516 kprintf("Unknown extended opcode: %d in %s\n",
517 op, binary_filename);
518 }
519 break;
520 default: {
521 uint8_t adjusted_opcode = op - header.opcode_base;
522 uint8_t operation_advance = adjusted_opcode / header.line_range;
523 /* NOTE: this code doesn't support VLIW */
524 addr += operation_advance * header.minimum_instruction_length;
525 line += header.line_base + (adjusted_opcode % header.line_range);
526 FILL_LINE();
527 }
528 }
529 }
530 *debug_line = (char *)p;
531 return 0;
532}
533
534static int
535parse_debug_line(int num_traces, void **traces,
536 const char *debug_line, unsigned long size,
537 obj_info_t *obj, line_info_t *lines, int offset)
538{
539 const char *debug_line_end = debug_line + size;
540 while (debug_line < debug_line_end) {
541 if (parse_debug_line_cu(num_traces, traces, &debug_line, obj, lines, offset))
542 return -1;
543 }
544 if (debug_line != debug_line_end) {
545 kprintf("Unexpected size of .debug_line in %s\n",
546 binary_filename);
547 }
548 return 0;
549}
550
551/* read file and fill lines */
552static uintptr_t
553fill_lines(int num_traces, void **traces, int check_debuglink,
554 obj_info_t **objp, line_info_t *lines, int offset);
555
556static void
557append_obj(obj_info_t **objp)
558{
559 obj_info_t *newobj = calloc(1, sizeof(obj_info_t));
560 if (*objp) (*objp)->next = newobj;
561 *objp = newobj;
562}
563
564#ifdef USE_ELF
565/* Ideally we should check 4 paths to follow gnu_debuglink:
566 *
567 * - /usr/lib/debug/.build-id/ab/cdef1234.debug
568 * - /usr/bin/ruby.debug
569 * - /usr/bin/.debug/ruby.debug
570 * - /usr/lib/debug/usr/bin/ruby.debug.
571 *
572 * but we handle only two cases for now as the two formats are
573 * used by some linux distributions.
574 *
575 * See GDB's info for detail.
576 * https://sourceware.org/gdb/onlinedocs/gdb/Separate-Debug-Files.html
577 */
578
579// check the path pattern of "/usr/lib/debug/usr/bin/ruby.debug"
580static void
581follow_debuglink(const char *debuglink, int num_traces, void **traces,
582 obj_info_t **objp, line_info_t *lines, int offset)
583{
584 static const char global_debug_dir[] = "/usr/lib/debug";
585 const size_t global_debug_dir_len = sizeof(global_debug_dir) - 1;
586 char *p;
587 obj_info_t *o1 = *objp, *o2;
588 size_t len;
589
590 p = strrchr(binary_filename, '/');
591 if (!p) {
592 return;
593 }
594 p[1] = '\0';
595
596 len = strlen(binary_filename);
597 if (len >= PATH_MAX - global_debug_dir_len)
598 len = PATH_MAX - global_debug_dir_len - 1;
599 memmove(binary_filename + global_debug_dir_len, binary_filename, len);
600 memcpy(binary_filename, global_debug_dir, global_debug_dir_len);
601 len += global_debug_dir_len;
602 strlcpy(binary_filename + len, debuglink, PATH_MAX - len);
603
604 append_obj(objp);
605 o2 = *objp;
606 o2->base_addr = o1->base_addr;
607 o2->path = o1->path;
608 fill_lines(num_traces, traces, 0, objp, lines, offset);
609}
610
611// check the path pattern of "/usr/lib/debug/.build-id/ab/cdef1234.debug"
612static void
613follow_debuglink_build_id(const char *build_id, size_t build_id_size, int num_traces, void **traces,
614 obj_info_t **objp, line_info_t *lines, int offset)
615{
616 static const char global_debug_dir[] = "/usr/lib/debug/.build-id/";
617 const size_t global_debug_dir_len = sizeof(global_debug_dir) - 1;
618 char *p;
619 obj_info_t *o1 = *objp, *o2;
620 size_t i;
621
622 if (PATH_MAX < global_debug_dir_len + 1 + build_id_size * 2 + 6) return;
623
624 memcpy(binary_filename, global_debug_dir, global_debug_dir_len);
625 p = binary_filename + global_debug_dir_len;
626 for (i = 0; i < build_id_size; i++) {
627 static const char tbl[] = "0123456789abcdef";
628 unsigned char n = build_id[i];
629 *p++ = tbl[n / 16];
630 *p++ = tbl[n % 16];
631 if (i == 0) *p++ = '/';
632 }
633 strcpy(p, ".debug");
634
635 append_obj(objp);
636 o2 = *objp;
637 o2->base_addr = o1->base_addr;
638 o2->path = o1->path;
639 fill_lines(num_traces, traces, 0, objp, lines, offset);
640}
641#endif
642
643enum
644{
645 DW_TAG_compile_unit = 0x11,
646 DW_TAG_inlined_subroutine = 0x1d,
647 DW_TAG_subprogram = 0x2e,
648};
649
650/* Attributes encodings */
651enum
652{
653 DW_AT_sibling = 0x01,
654 DW_AT_location = 0x02,
655 DW_AT_name = 0x03,
656 /* Reserved 0x04 */
657 /* Reserved 0x05 */
658 /* Reserved 0x06 */
659 /* Reserved 0x07 */
660 /* Reserved 0x08 */
661 DW_AT_ordering = 0x09,
662 /* Reserved 0x0a */
663 DW_AT_byte_size = 0x0b,
664 /* Reserved 0x0c */
665 DW_AT_bit_size = 0x0d,
666 /* Reserved 0x0e */
667 /* Reserved 0x0f */
668 DW_AT_stmt_list = 0x10,
669 DW_AT_low_pc = 0x11,
670 DW_AT_high_pc = 0x12,
671 DW_AT_language = 0x13,
672 /* Reserved 0x14 */
673 DW_AT_discr = 0x15,
674 DW_AT_discr_value = 0x16,
675 DW_AT_visibility = 0x17,
676 DW_AT_import = 0x18,
677 DW_AT_string_length = 0x19,
678 DW_AT_common_reference = 0x1a,
679 DW_AT_comp_dir = 0x1b,
680 DW_AT_const_value = 0x1c,
681 DW_AT_containing_type = 0x1d,
682 DW_AT_default_value = 0x1e,
683 /* Reserved 0x1f */
684 DW_AT_inline = 0x20,
685 DW_AT_is_optional = 0x21,
686 DW_AT_lower_bound = 0x22,
687 /* Reserved 0x23 */
688 /* Reserved 0x24 */
689 DW_AT_producer = 0x25,
690 /* Reserved 0x26 */
691 DW_AT_prototyped = 0x27,
692 /* Reserved 0x28 */
693 /* Reserved 0x29 */
694 DW_AT_return_addr = 0x2a,
695 /* Reserved 0x2b */
696 DW_AT_start_scope = 0x2c,
697 /* Reserved 0x2d */
698 DW_AT_bit_stride = 0x2e,
699 DW_AT_upper_bound = 0x2f,
700 /* Reserved 0x30 */
701 DW_AT_abstract_origin = 0x31,
702 DW_AT_accessibility = 0x32,
703 DW_AT_address_class = 0x33,
704 DW_AT_artificial = 0x34,
705 DW_AT_base_types = 0x35,
706 DW_AT_calling_convention = 0x36,
707 DW_AT_count = 0x37,
708 DW_AT_data_member_location = 0x38,
709 DW_AT_decl_column = 0x39,
710 DW_AT_decl_file = 0x3a,
711 DW_AT_decl_line = 0x3b,
712 DW_AT_declaration = 0x3c,
713 DW_AT_discr_list = 0x3d,
714 DW_AT_encoding = 0x3e,
715 DW_AT_external = 0x3f,
716 DW_AT_frame_base = 0x40,
717 DW_AT_friend = 0x41,
718 DW_AT_identifier_case = 0x42,
719 /* Reserved 0x43 */
720 DW_AT_namelist_item = 0x44,
721 DW_AT_priority = 0x45,
722 DW_AT_segment = 0x46,
723 DW_AT_specification = 0x47,
724 DW_AT_static_link = 0x48,
725 DW_AT_type = 0x49,
726 DW_AT_use_location = 0x4a,
727 DW_AT_variable_parameter = 0x4b,
728 DW_AT_virtuality = 0x4c,
729 DW_AT_vtable_elem_location = 0x4d,
730 DW_AT_allocated = 0x4e,
731 DW_AT_associated = 0x4f,
732 DW_AT_data_location = 0x50,
733 DW_AT_byte_stride = 0x51,
734 DW_AT_entry_pc = 0x52,
735 DW_AT_use_UTF8 = 0x53,
736 DW_AT_extension = 0x54,
737 DW_AT_ranges = 0x55,
738 DW_AT_trampoline = 0x56,
739 DW_AT_call_column = 0x57,
740 DW_AT_call_file = 0x58,
741 DW_AT_call_line = 0x59,
742 DW_AT_description = 0x5a,
743 DW_AT_binary_scale = 0x5b,
744 DW_AT_decimal_scale = 0x5c,
745 DW_AT_small = 0x5d,
746 DW_AT_decimal_sign = 0x5e,
747 DW_AT_digit_count = 0x5f,
748 DW_AT_picture_string = 0x60,
749 DW_AT_mutable = 0x61,
750 DW_AT_threads_scaled = 0x62,
751 DW_AT_explicit = 0x63,
752 DW_AT_object_pointer = 0x64,
753 DW_AT_endianity = 0x65,
754 DW_AT_elemental = 0x66,
755 DW_AT_pure = 0x67,
756 DW_AT_recursive = 0x68,
757 DW_AT_signature = 0x69,
758 DW_AT_main_subprogram = 0x6a,
759 DW_AT_data_bit_offset = 0x6b,
760 DW_AT_const_expr = 0x6c,
761 DW_AT_enum_class = 0x6d,
762 DW_AT_linkage_name = 0x6e,
763 DW_AT_string_length_bit_size = 0x6f,
764 DW_AT_string_length_byte_size = 0x70,
765 DW_AT_rank = 0x71,
766 DW_AT_str_offsets_base = 0x72,
767 DW_AT_addr_base = 0x73,
768 DW_AT_rnglists_base = 0x74,
769 /* Reserved 0x75 */
770 DW_AT_dwo_name = 0x76,
771 DW_AT_reference = 0x77,
772 DW_AT_rvalue_reference = 0x78,
773 DW_AT_macros = 0x79,
774 DW_AT_call_all_calls = 0x7a,
775 DW_AT_call_all_source_calls = 0x7b,
776 DW_AT_call_all_tail_calls = 0x7c,
777 DW_AT_call_return_pc = 0x7d,
778 DW_AT_call_value = 0x7e,
779 DW_AT_call_origin = 0x7f,
780 DW_AT_call_parameter = 0x80,
781 DW_AT_call_pc = 0x81,
782 DW_AT_call_tail_call = 0x82,
783 DW_AT_call_target = 0x83,
784 DW_AT_call_target_clobbered = 0x84,
785 DW_AT_call_data_location = 0x85,
786 DW_AT_call_data_value = 0x86,
787 DW_AT_noreturn = 0x87,
788 DW_AT_alignment = 0x88,
789 DW_AT_export_symbols = 0x89,
790 DW_AT_deleted = 0x8a,
791 DW_AT_defaulted = 0x8b,
792 DW_AT_loclists_base = 0x8c,
793 DW_AT_lo_user = 0x2000,
794 DW_AT_hi_user = 0x3fff
795};
796
797/* Attribute form encodings */
798enum
799{
800 DW_FORM_addr = 0x01,
801 /* Reserved 0x02 */
802 DW_FORM_block2 = 0x03,
803 DW_FORM_block4 = 0x04,
804 DW_FORM_data2 = 0x05,
805 DW_FORM_data4 = 0x06,
806 DW_FORM_data8 = 0x07,
807 DW_FORM_string = 0x08,
808 DW_FORM_block = 0x09,
809 DW_FORM_block1 = 0x0a,
810 DW_FORM_data1 = 0x0b,
811 DW_FORM_flag = 0x0c,
812 DW_FORM_sdata = 0x0d,
813 DW_FORM_strp = 0x0e,
814 DW_FORM_udata = 0x0f,
815 DW_FORM_ref_addr = 0x10,
816 DW_FORM_ref1 = 0x11,
817 DW_FORM_ref2 = 0x12,
818 DW_FORM_ref4 = 0x13,
819 DW_FORM_ref8 = 0x14,
820 DW_FORM_ref_udata = 0x15,
821 DW_FORM_indirect = 0x16,
822 DW_FORM_sec_offset = 0x17,
823 DW_FORM_exprloc = 0x18,
824 DW_FORM_flag_present = 0x19,
825 DW_FORM_strx = 0x1a,
826 DW_FORM_addrx = 0x1b,
827 DW_FORM_ref_sup4 = 0x1c,
828 DW_FORM_strp_sup = 0x1d,
829 DW_FORM_data16 = 0x1e,
830 DW_FORM_line_strp = 0x1f,
831 DW_FORM_ref_sig8 = 0x20,
832 DW_FORM_implicit_const = 0x21,
833 DW_FORM_loclistx = 0x22,
834 DW_FORM_rnglistx = 0x23,
835 DW_FORM_ref_sup8 = 0x24,
836 DW_FORM_strx1 = 0x25,
837 DW_FORM_strx2 = 0x26,
838 DW_FORM_strx3 = 0x27,
839 DW_FORM_strx4 = 0x28,
840 DW_FORM_addrx1 = 0x29,
841 DW_FORM_addrx2 = 0x2a,
842 DW_FORM_addrx3 = 0x2b,
843 DW_FORM_addrx4 = 0x2c
844};
845
846/* Range list entry encodings */
847enum {
848 DW_RLE_end_of_list = 0x00,
849 DW_RLE_base_addressx = 0x01,
850 DW_RLE_startx_endx = 0x02,
851 DW_RLE_startx_length = 0x03,
852 DW_RLE_offset_pair = 0x04,
853 DW_RLE_base_address = 0x05,
854 DW_RLE_start_end = 0x06,
855 DW_RLE_start_length = 0x07
856};
857
858enum {
859 VAL_none = 0,
860 VAL_cstr = 1,
861 VAL_data = 2,
862 VAL_uint = 3,
863 VAL_int = 4,
864 VAL_addr = 5
865};
866
867# define ABBREV_TABLE_SIZE 256
868typedef struct {
869 obj_info_t *obj;
870 const char *file;
871 uint8_t current_version;
872 const char *current_cu;
873 uint64_t current_low_pc;
874 uint64_t current_str_offsets_base;
875 uint64_t current_addr_base;
876 uint64_t current_rnglists_base;
877 const char *debug_line_cu_end;
878 uint8_t debug_line_format;
879 uint16_t debug_line_version;
880 const char *debug_line_files;
881 const char *debug_line_directories;
882 const char *p;
883 const char *cu_end;
884 const char *pend;
885 const char *q0;
886 const char *q;
887 int format; // 4 or 8
888 uint8_t address_size;
889 int level;
890 const char *abbrev_table[ABBREV_TABLE_SIZE];
891} DebugInfoReader;
892
893typedef struct {
894 ptrdiff_t pos;
895 int tag;
896 int has_children;
897} DIE;
898
899typedef struct {
900 union {
901 const char *ptr;
902 uint64_t uint64;
903 int64_t int64;
904 uint64_t addr_idx;
905 } as;
906 uint64_t off;
907 uint64_t at;
908 uint64_t form;
909 size_t size;
910 int type;
911} DebugInfoValue;
912
913#if defined(WORDS_BIGENDIAN)
914#define MERGE_2INTS(a,b,sz) (((uint64_t)(a)<<sz)|(b))
915#else
916#define MERGE_2INTS(a,b,sz) (((uint64_t)(b)<<sz)|(a))
917#endif
918
919static uint16_t
920get_uint16(const uint8_t *p)
921{
922 return (uint16_t)MERGE_2INTS(p[0],p[1],8);
923}
924
925static uint32_t
926get_uint32(const uint8_t *p)
927{
928 return (uint32_t)MERGE_2INTS(get_uint16(p),get_uint16(p+2),16);
929}
930
931static uint64_t
932get_uint64(const uint8_t *p)
933{
934 return MERGE_2INTS(get_uint32(p),get_uint32(p+4),32);
935}
936
937static uint8_t
938read_uint8(const char **ptr)
939{
940 const char *p = *ptr;
941 *ptr = (p + 1);
942 return (uint8_t)*p;
943}
944
945static uint16_t
946read_uint16(const char **ptr)
947{
948 const char *p = *ptr;
949 *ptr = (p + 2);
950 return get_uint16((const uint8_t *)p);
951}
952
953static uint32_t
954read_uint24(const char **ptr)
955{
956 const char *p = *ptr;
957 *ptr = (p + 3);
958 return ((uint8_t)*p << 16) | get_uint16((const uint8_t *)p+1);
959}
960
961static uint32_t
962read_uint32(const char **ptr)
963{
964 const char *p = *ptr;
965 *ptr = (p + 4);
966 return get_uint32((const uint8_t *)p);
967}
968
969static uint64_t
970read_uint64(const char **ptr)
971{
972 const unsigned char *p = (const unsigned char *)*ptr;
973 *ptr = (char *)(p + 8);
974 return get_uint64(p);
975}
976
977static uintptr_t
978read_uintptr(const char **ptr)
979{
980 const unsigned char *p = (const unsigned char *)*ptr;
981 *ptr = (char *)(p + SIZEOF_VOIDP);
982#if SIZEOF_VOIDP == 8
983 return get_uint64(p);
984#else
985 return get_uint32(p);
986#endif
987}
988
989static uint64_t
990read_uint(DebugInfoReader *reader)
991{
992 if (reader->format == 4) {
993 return read_uint32(&reader->p);
994 } else { /* 64 bit */
995 return read_uint64(&reader->p);
996 }
997}
998
999static uint64_t
1000read_uleb128(DebugInfoReader *reader)
1001{
1002 return uleb128(&reader->p);
1003}
1004
1005static int64_t
1006read_sleb128(DebugInfoReader *reader)
1007{
1008 return sleb128(&reader->p);
1009}
1010
1011static void
1012debug_info_reader_init(DebugInfoReader *reader, obj_info_t *obj)
1013{
1014 reader->file = obj->mapped;
1015 reader->obj = obj;
1016 reader->p = obj->debug_info.ptr;
1017 reader->pend = obj->debug_info.ptr + obj->debug_info.size;
1018 reader->debug_line_cu_end = obj->debug_line.ptr;
1019 reader->current_low_pc = 0;
1020 reader->current_str_offsets_base = 0;
1021 reader->current_addr_base = 0;
1022 reader->current_rnglists_base = 0;
1023}
1024
1025static void
1026di_skip_die_attributes(const char **p)
1027{
1028 for (;;) {
1029 uint64_t at = uleb128(p);
1030 uint64_t form = uleb128(p);
1031 if (!at && !form) break;
1032 switch (form) {
1033 default:
1034 break;
1035 case DW_FORM_implicit_const:
1036 sleb128(p);
1037 break;
1038 }
1039 }
1040}
1041
1042static void
1043di_read_debug_abbrev_cu(DebugInfoReader *reader)
1044{
1045 uint64_t prev = 0;
1046 const char *p = reader->q0;
1047 for (;;) {
1048 uint64_t abbrev_number = uleb128(&p);
1049 if (abbrev_number <= prev) break;
1050 if (abbrev_number < ABBREV_TABLE_SIZE) {
1051 reader->abbrev_table[abbrev_number] = p;
1052 }
1053 prev = abbrev_number;
1054 uleb128(&p); /* tag */
1055 p++; /* has_children */
1056 di_skip_die_attributes(&p);
1057 }
1058}
1059
1060static int
1061di_read_debug_line_cu(DebugInfoReader *reader)
1062{
1063 const char *p;
1064 struct LineNumberProgramHeader header;
1065
1066 p = (const char *)reader->debug_line_cu_end;
1067 if (parse_debug_line_header(reader->obj, &p, &header))
1068 return -1;
1069
1070 reader->debug_line_cu_end = (char *)header.cu_end;
1071 reader->debug_line_format = header.format;
1072 reader->debug_line_version = header.version;
1073 reader->debug_line_directories = (char *)header.include_directories;
1074 reader->debug_line_files = (char *)header.filenames;
1075
1076 return 0;
1077}
1078
1079static void
1080set_addr_idx_value(DebugInfoValue *v, uint64_t n)
1081{
1082 v->as.addr_idx = n;
1083 v->type = VAL_addr;
1084}
1085
1086static void
1087set_uint_value(DebugInfoValue *v, uint64_t n)
1088{
1089 v->as.uint64 = n;
1090 v->type = VAL_uint;
1091}
1092
1093static void
1094set_int_value(DebugInfoValue *v, int64_t n)
1095{
1096 v->as.int64 = n;
1097 v->type = VAL_int;
1098}
1099
1100static void
1101set_cstr_value(DebugInfoValue *v, const char *s)
1102{
1103 v->as.ptr = s;
1104 v->off = 0;
1105 v->type = VAL_cstr;
1106}
1107
1108static void
1109set_cstrp_value(DebugInfoValue *v, const char *s, uint64_t off)
1110{
1111 v->as.ptr = s;
1112 v->off = off;
1113 v->type = VAL_cstr;
1114}
1115
1116static void
1117set_data_value(DebugInfoValue *v, const char *s)
1118{
1119 v->as.ptr = s;
1120 v->type = VAL_data;
1121}
1122
1123static const char *
1124get_cstr_value(DebugInfoValue *v)
1125{
1126 if (v->as.ptr) {
1127 return v->as.ptr + v->off;
1128 } else {
1129 return NULL;
1130 }
1131}
1132
1133static const char *
1134resolve_strx(DebugInfoReader *reader, uint64_t idx)
1135{
1136 const char *p = reader->obj->debug_str_offsets.ptr + reader->current_str_offsets_base;
1137 uint64_t off;
1138 if (reader->format == 4) {
1139 off = ((uint32_t *)p)[idx];
1140 }
1141 else {
1142 off = ((uint64_t *)p)[idx];
1143 }
1144 return reader->obj->debug_str.ptr + off;
1145}
1146
1147static void
1148debug_info_reader_read_addr_value(DebugInfoReader *reader, DebugInfoValue *v)
1149{
1150 if (reader->address_size == 4) {
1151 set_uint_value(v, read_uint32(&reader->p));
1152 } else if (reader->address_size == 8) {
1153 set_uint_value(v, read_uint64(&reader->p));
1154 } else {
1155 fprintf(stderr,"unknown address_size:%d", reader->address_size);
1156 abort();
1157 }
1158}
1159
1160static void
1161debug_info_reader_read_value(DebugInfoReader *reader, uint64_t form, DebugInfoValue *v)
1162{
1163 switch (form) {
1164 case DW_FORM_addr:
1165 debug_info_reader_read_addr_value(reader, v);
1166 break;
1167 case DW_FORM_block2:
1168 v->size = read_uint16(&reader->p);
1169 set_data_value(v, reader->p);
1170 reader->p += v->size;
1171 break;
1172 case DW_FORM_block4:
1173 v->size = read_uint32(&reader->p);
1174 set_data_value(v, reader->p);
1175 reader->p += v->size;
1176 break;
1177 case DW_FORM_data2:
1178 set_uint_value(v, read_uint16(&reader->p));
1179 break;
1180 case DW_FORM_data4:
1181 set_uint_value(v, read_uint32(&reader->p));
1182 break;
1183 case DW_FORM_data8:
1184 set_uint_value(v, read_uint64(&reader->p));
1185 break;
1186 case DW_FORM_string:
1187 v->size = strlen(reader->p);
1188 set_cstr_value(v, reader->p);
1189 reader->p += v->size + 1;
1190 break;
1191 case DW_FORM_block:
1192 v->size = uleb128(&reader->p);
1193 set_data_value(v, reader->p);
1194 reader->p += v->size;
1195 break;
1196 case DW_FORM_block1:
1197 v->size = read_uint8(&reader->p);
1198 set_data_value(v, reader->p);
1199 reader->p += v->size;
1200 break;
1201 case DW_FORM_data1:
1202 set_uint_value(v, read_uint8(&reader->p));
1203 break;
1204 case DW_FORM_flag:
1205 set_uint_value(v, read_uint8(&reader->p));
1206 break;
1207 case DW_FORM_sdata:
1208 set_int_value(v, read_sleb128(reader));
1209 break;
1210 case DW_FORM_strp:
1211 set_cstrp_value(v, reader->obj->debug_str.ptr, read_uint(reader));
1212 break;
1213 case DW_FORM_udata:
1214 set_uint_value(v, read_uleb128(reader));
1215 break;
1216 case DW_FORM_ref_addr:
1217 if (reader->current_version <= 2) {
1218 // DWARF Version 2 specifies that references have
1219 // the same size as an address on the target system
1220 debug_info_reader_read_addr_value(reader, v);
1221 } else {
1222 if (reader->format == 4) {
1223 set_uint_value(v, read_uint32(&reader->p));
1224 } else if (reader->format == 8) {
1225 set_uint_value(v, read_uint64(&reader->p));
1226 } else {
1227 fprintf(stderr,"unknown format:%d", reader->format);
1228 abort();
1229 }
1230 }
1231 break;
1232 case DW_FORM_ref1:
1233 set_uint_value(v, read_uint8(&reader->p));
1234 break;
1235 case DW_FORM_ref2:
1236 set_uint_value(v, read_uint16(&reader->p));
1237 break;
1238 case DW_FORM_ref4:
1239 set_uint_value(v, read_uint32(&reader->p));
1240 break;
1241 case DW_FORM_ref8:
1242 set_uint_value(v, read_uint64(&reader->p));
1243 break;
1244 case DW_FORM_ref_udata:
1245 set_uint_value(v, uleb128(&reader->p));
1246 break;
1247 case DW_FORM_indirect:
1248 /* TODO: read the referred value */
1249 set_uint_value(v, uleb128(&reader->p));
1250 break;
1251 case DW_FORM_sec_offset:
1252 set_uint_value(v, read_uint(reader)); /* offset */
1253 /* addrptr: debug_addr */
1254 /* lineptr: debug_line */
1255 /* loclist: debug_loclists */
1256 /* loclistptr: debug_loclists */
1257 /* macptr: debug_macro */
1258 /* rnglist: debug_rnglists */
1259 /* rnglistptr: debug_rnglists */
1260 /* stroffsetsptr: debug_str_offsets */
1261 break;
1262 case DW_FORM_exprloc:
1263 v->size = (size_t)read_uleb128(reader);
1264 set_data_value(v, reader->p);
1265 reader->p += v->size;
1266 break;
1267 case DW_FORM_flag_present:
1268 set_uint_value(v, 1);
1269 break;
1270 case DW_FORM_strx:
1271 set_cstr_value(v, resolve_strx(reader, uleb128(&reader->p)));
1272 break;
1273 case DW_FORM_addrx:
1274 set_addr_idx_value(v, uleb128(&reader->p));
1275 break;
1276 case DW_FORM_ref_sup4:
1277 set_uint_value(v, read_uint32(&reader->p));
1278 break;
1279 case DW_FORM_strp_sup:
1280 set_uint_value(v, read_uint(reader));
1281 /* *p = reader->sup_file + reader->sup_str->sh_offset + ret; */
1282 break;
1283 case DW_FORM_data16:
1284 v->size = 16;
1285 set_data_value(v, reader->p);
1286 reader->p += v->size;
1287 break;
1288 case DW_FORM_line_strp:
1289 set_cstrp_value(v, reader->obj->debug_line_str.ptr, read_uint(reader));
1290 break;
1291 case DW_FORM_ref_sig8:
1292 set_uint_value(v, read_uint64(&reader->p));
1293 break;
1294 case DW_FORM_implicit_const:
1295 set_int_value(v, sleb128(&reader->q));
1296 break;
1297 case DW_FORM_loclistx:
1298 set_uint_value(v, read_uleb128(reader));
1299 break;
1300 case DW_FORM_rnglistx:
1301 set_uint_value(v, read_uleb128(reader));
1302 break;
1303 case DW_FORM_ref_sup8:
1304 set_uint_value(v, read_uint64(&reader->p));
1305 break;
1306 case DW_FORM_strx1:
1307 set_cstr_value(v, resolve_strx(reader, read_uint8(&reader->p)));
1308 break;
1309 case DW_FORM_strx2:
1310 set_cstr_value(v, resolve_strx(reader, read_uint16(&reader->p)));
1311 break;
1312 case DW_FORM_strx3:
1313 set_cstr_value(v, resolve_strx(reader, read_uint24(&reader->p)));
1314 break;
1315 case DW_FORM_strx4:
1316 set_cstr_value(v, resolve_strx(reader, read_uint32(&reader->p)));
1317 break;
1318 case DW_FORM_addrx1:
1319 set_addr_idx_value(v, read_uint8(&reader->p));
1320 break;
1321 case DW_FORM_addrx2:
1322 set_addr_idx_value(v, read_uint16(&reader->p));
1323 break;
1324 case DW_FORM_addrx3:
1325 set_addr_idx_value(v, read_uint24(&reader->p));
1326 break;
1327 case DW_FORM_addrx4:
1328 set_addr_idx_value(v, read_uint32(&reader->p));
1329 break;
1330 case 0:
1331 goto fail;
1332 break;
1333 }
1334 return;
1335
1336 fail:
1337 fprintf(stderr, "%d: unsupported form: %#"PRIx64"\n", __LINE__, form);
1338 exit(1);
1339}
1340
1341/* find abbrev in current compilation unit */
1342static const char *
1343di_find_abbrev(DebugInfoReader *reader, uint64_t abbrev_number)
1344{
1345 const char *p;
1346 if (abbrev_number < ABBREV_TABLE_SIZE) {
1347 return reader->abbrev_table[abbrev_number];
1348 }
1349 p = reader->abbrev_table[ABBREV_TABLE_SIZE-1];
1350 /* skip 255th record */
1351 uleb128(&p); /* tag */
1352 p++; /* has_children */
1353 di_skip_die_attributes(&p);
1354 for (uint64_t n = uleb128(&p); abbrev_number != n; n = uleb128(&p)) {
1355 if (n == 0) {
1356 fprintf(stderr,"%d: Abbrev Number %"PRId64" not found\n",__LINE__, abbrev_number);
1357 exit(1);
1358 }
1359 uleb128(&p); /* tag */
1360 p++; /* has_children */
1361 di_skip_die_attributes(&p);
1362 }
1363 return p;
1364}
1365
1366#if 0
1367static void
1368hexdump0(const unsigned char *p, size_t n)
1369{
1370 size_t i;
1371 fprintf(stderr, " 0 1 2 3 4 5 6 7 8 9 A B C D E F\n");
1372 for (i=0; i < n; i++){
1373 switch (i & 15) {
1374 case 0:
1375 fprintf(stderr, "%02" PRIdSIZE ": %02X ", i/16, p[i]);
1376 break;
1377 case 15:
1378 fprintf(stderr, "%02X\n", p[i]);
1379 break;
1380 default:
1381 fprintf(stderr, "%02X ", p[i]);
1382 break;
1383 }
1384 }
1385 if ((i & 15) != 15) {
1386 fprintf(stderr, "\n");
1387 }
1388}
1389#define hexdump(p,n) hexdump0((const unsigned char *)p, n)
1390
1391static void
1392div_inspect(DebugInfoValue *v)
1393{
1394 switch (v->type) {
1395 case VAL_uint:
1396 fprintf(stderr,"%d: type:%d size:%" PRIxSIZE " v:%"PRIx64"\n",__LINE__,v->type,v->size,v->as.uint64);
1397 break;
1398 case VAL_int:
1399 fprintf(stderr,"%d: type:%d size:%" PRIxSIZE " v:%"PRId64"\n",__LINE__,v->type,v->size,(int64_t)v->as.uint64);
1400 break;
1401 case VAL_cstr:
1402 fprintf(stderr,"%d: type:%d size:%" PRIxSIZE " v:'%s'\n",__LINE__,v->type,v->size,v->as.ptr);
1403 break;
1404 case VAL_data:
1405 fprintf(stderr,"%d: type:%d size:%" PRIxSIZE " v:\n",__LINE__,v->type,v->size);
1406 hexdump(v->as.ptr, 16);
1407 break;
1408 }
1409}
1410#endif
1411
1412static DIE *
1413di_read_die(DebugInfoReader *reader, DIE *die)
1414{
1415 uint64_t abbrev_number = uleb128(&reader->p);
1416 if (abbrev_number == 0) {
1417 reader->level--;
1418 return NULL;
1419 }
1420
1421 reader->q = di_find_abbrev(reader, abbrev_number);
1422
1423 die->pos = reader->p - reader->obj->debug_info.ptr - 1;
1424 die->tag = (int)uleb128(&reader->q); /* tag */
1425 die->has_children = *reader->q++; /* has_children */
1426 if (die->has_children) {
1427 reader->level++;
1428 }
1429 return die;
1430}
1431
1432static DebugInfoValue *
1433di_read_record(DebugInfoReader *reader, DebugInfoValue *vp)
1434{
1435 uint64_t at = uleb128(&reader->q);
1436 uint64_t form = uleb128(&reader->q);
1437 if (!at || !form) return NULL;
1438 vp->at = at;
1439 vp->form = form;
1440 debug_info_reader_read_value(reader, form, vp);
1441 return vp;
1442}
1443
1444static void
1445di_skip_records(DebugInfoReader *reader)
1446{
1447 for (;;) {
1448 DebugInfoValue v = {{}};
1449 uint64_t at = uleb128(&reader->q);
1450 uint64_t form = uleb128(&reader->q);
1451 if (!at || !form) return;
1452 debug_info_reader_read_value(reader, form, &v);
1453 }
1454}
1455
1456typedef struct addr_header {
1457 const char *ptr;
1458 uint64_t unit_length;
1459 uint8_t format;
1460 uint8_t address_size;
1461 /* uint8_t segment_selector_size; */
1462} addr_header_t;
1463
1464static void
1465addr_header_init(obj_info_t *obj, addr_header_t *header) {
1466 const char *p = obj->debug_addr.ptr;
1467
1468 header->ptr = p;
1469
1470 if (!p) return;
1471
1472 header->unit_length = *(uint32_t *)p;
1473 p += sizeof(uint32_t);
1474
1475 header->format = 4;
1476 if (header->unit_length == 0xffffffff) {
1477 header->unit_length = *(uint64_t *)p;
1478 p += sizeof(uint64_t);
1479 header->format = 8;
1480 }
1481
1482 p += 2; /* version */
1483 header->address_size = *p++;
1484 p++; /* segment_selector_size */
1485}
1486
1487static uint64_t
1488read_addr(addr_header_t *header, uint64_t addr_base, uint64_t idx) {
1489 if (header->address_size == 4) {
1490 return ((uint32_t*)(header->ptr + addr_base))[idx];
1491 }
1492 else {
1493 return ((uint64_t*)(header->ptr + addr_base))[idx];
1494 }
1495}
1496
1497typedef struct rnglists_header {
1498 uint64_t unit_length;
1499 uint8_t format;
1500 uint8_t address_size;
1501 uint32_t offset_entry_count;
1502} rnglists_header_t;
1503
1504static void
1505rnglists_header_init(obj_info_t *obj, rnglists_header_t *header) {
1506 const char *p = obj->debug_rnglists.ptr;
1507
1508 if (!p) return;
1509
1510 header->unit_length = *(uint32_t *)p;
1511 p += sizeof(uint32_t);
1512
1513 header->format = 4;
1514 if (header->unit_length == 0xffffffff) {
1515 header->unit_length = *(uint64_t *)p;
1516 p += sizeof(uint64_t);
1517 header->format = 8;
1518 }
1519
1520 p += 2; /* version */
1521 header->address_size = *p++;
1522 p++; /* segment_selector_size */
1523 header->offset_entry_count = *(uint32_t *)p;
1524}
1525
1526typedef struct {
1527 uint64_t low_pc;
1528 uint64_t high_pc;
1529 uint64_t ranges;
1530 bool low_pc_set;
1531 bool high_pc_set;
1532 bool ranges_set;
1533} ranges_t;
1534
1535static void
1536ranges_set(ranges_t *ptr, DebugInfoValue *v, addr_header_t *addr_header, uint64_t addr_base)
1537{
1538 uint64_t n = 0;
1539 if (v->type == VAL_uint) {
1540 n = v->as.uint64;
1541 }
1542 else if (v->type == VAL_addr) {
1543 n = read_addr(addr_header, addr_base, v->as.addr_idx);
1544 }
1545 switch (v->at) {
1546 case DW_AT_low_pc:
1547 ptr->low_pc = n;
1548 ptr->low_pc_set = true;
1549 break;
1550 case DW_AT_high_pc:
1551 if (v->form == DW_FORM_addr) {
1552 ptr->high_pc = n;
1553 }
1554 else {
1555 ptr->high_pc = ptr->low_pc + n;
1556 }
1557 ptr->high_pc_set = true;
1558 break;
1559 case DW_AT_ranges:
1560 ptr->ranges = n;
1561 ptr->ranges_set = true;
1562 break;
1563 }
1564}
1565
1566static uint64_t
1567read_dw_form_addr(DebugInfoReader *reader, const char **ptr)
1568{
1569 const char *p = *ptr;
1570 *ptr = p + reader->address_size;
1571 if (reader->address_size == 4) {
1572 return read_uint32(&p);
1573 } else if (reader->address_size == 8) {
1574 return read_uint64(&p);
1575 } else {
1576 fprintf(stderr,"unknown address_size:%d", reader->address_size);
1577 abort();
1578 }
1579}
1580
1581static uintptr_t
1582ranges_include(DebugInfoReader *reader, ranges_t *ptr, uint64_t addr, rnglists_header_t *rnglists_header)
1583{
1584 if (ptr->high_pc_set) {
1585 if (ptr->ranges_set || !ptr->low_pc_set) {
1586 exit(1);
1587 }
1588 if (ptr->low_pc <= addr && addr <= ptr->high_pc) {
1589 return (uintptr_t)ptr->low_pc;
1590 }
1591 }
1592 else if (ptr->ranges_set) {
1593 /* TODO: support base address selection entry */
1594 const char *p;
1595 uint64_t base = ptr->low_pc_set ? ptr->low_pc : reader->current_low_pc;
1596 bool base_valid = true;
1597 if (reader->current_version >= 5) {
1598 if (rnglists_header->offset_entry_count == 0) {
1599 // DW_FORM_sec_offset
1600 p = reader->obj->debug_rnglists.ptr + ptr->ranges + reader->current_rnglists_base;
1601 }
1602 else {
1603 // DW_FORM_rnglistx
1604 const char *offset_array = reader->obj->debug_rnglists.ptr + reader->current_rnglists_base;
1605 if (rnglists_header->format == 4) {
1606 p = offset_array + ((uint32_t *)offset_array)[ptr->ranges];
1607 }
1608 else {
1609 p = offset_array + ((uint64_t *)offset_array)[ptr->ranges];
1610 }
1611 }
1612 for (;;) {
1613 uint8_t rle = read_uint8(&p);
1614 uintptr_t from = 0, to = 0;
1615 if (rle == DW_RLE_end_of_list) break;
1616 switch (rle) {
1617 case DW_RLE_base_addressx:
1618 uleb128(&p);
1619 base_valid = false; /* not supported yet */
1620 break;
1621 case DW_RLE_startx_endx:
1622 uleb128(&p);
1623 uleb128(&p);
1624 break;
1625 case DW_RLE_startx_length:
1626 uleb128(&p);
1627 uleb128(&p);
1628 break;
1629 case DW_RLE_offset_pair:
1630 if (!base_valid) break;
1631 from = (uintptr_t)base + uleb128(&p);
1632 to = (uintptr_t)base + uleb128(&p);
1633 break;
1634 case DW_RLE_base_address:
1635 base = read_dw_form_addr(reader, &p);
1636 base_valid = true;
1637 break;
1638 case DW_RLE_start_end:
1639 from = (uintptr_t)read_dw_form_addr(reader, &p);
1640 to = (uintptr_t)read_dw_form_addr(reader, &p);
1641 break;
1642 case DW_RLE_start_length:
1643 from = (uintptr_t)read_dw_form_addr(reader, &p);
1644 to = from + uleb128(&p);
1645 break;
1646 }
1647 if (from <= addr && addr < to) {
1648 return from;
1649 }
1650 }
1651 return false;
1652 }
1653 p = reader->obj->debug_ranges.ptr + ptr->ranges;
1654 for (;;) {
1655 uintptr_t from = read_uintptr(&p);
1656 uintptr_t to = read_uintptr(&p);
1657 if (!from && !to) break;
1658 if (from == UINTPTR_MAX) {
1659 /* base address selection entry */
1660 base = to;
1661 }
1662 else if (base + from <= addr && addr < base + to) {
1663 return (uintptr_t)base + from;
1664 }
1665 }
1666 }
1667 else if (ptr->low_pc_set) {
1668 if (ptr->low_pc == addr) {
1669 return (uintptr_t)ptr->low_pc;
1670 }
1671 }
1672 return false;
1673}
1674
1675#if 0
1676static void
1677ranges_inspect(DebugInfoReader *reader, ranges_t *ptr)
1678{
1679 if (ptr->high_pc_set) {
1680 if (ptr->ranges_set || !ptr->low_pc_set) {
1681 fprintf(stderr,"low_pc_set:%d high_pc_set:%d ranges_set:%d\n",ptr->low_pc_set,ptr->high_pc_set,ptr->ranges_set);
1682 exit(1);
1683 }
1684 fprintf(stderr,"low_pc:%"PRIx64" high_pc:%"PRIx64"\n",ptr->low_pc,ptr->high_pc);
1685 }
1686 else if (ptr->ranges_set) {
1687 char *p = reader->obj->debug_ranges.ptr + ptr->ranges;
1688 fprintf(stderr,"low_pc:%"PRIx64" ranges:%"PRIx64" %lx ",ptr->low_pc,ptr->ranges, p-reader->obj->mapped);
1689 for (;;) {
1690 uintptr_t from = read_uintptr(&p);
1691 uintptr_t to = read_uintptr(&p);
1692 if (!from && !to) break;
1693 fprintf(stderr,"%"PRIx64"-%"PRIx64" ",ptr->low_pc+from,ptr->low_pc+to);
1694 }
1695 fprintf(stderr,"\n");
1696 }
1697 else if (ptr->low_pc_set) {
1698 fprintf(stderr,"low_pc:%"PRIx64"\n",ptr->low_pc);
1699 }
1700 else {
1701 fprintf(stderr,"empty\n");
1702 }
1703}
1704#endif
1705
1706static int
1707di_read_cu(DebugInfoReader *reader)
1708{
1709 uint64_t unit_length;
1710 uint16_t version;
1711 uint64_t debug_abbrev_offset;
1712 reader->format = 4;
1713 reader->current_cu = reader->p;
1714 unit_length = read_uint32(&reader->p);
1715 if (unit_length == 0xffffffff) {
1716 unit_length = read_uint64(&reader->p);
1717 reader->format = 8;
1718 }
1719 reader->cu_end = reader->p + unit_length;
1720 version = read_uint16(&reader->p);
1721 reader->current_version = version;
1722 if (version > 5) {
1723 return -1;
1724 }
1725 else if (version == 5) {
1726 /* unit_type = */ read_uint8(&reader->p);
1727 reader->address_size = read_uint8(&reader->p);
1728 debug_abbrev_offset = read_uint(reader);
1729 }
1730 else {
1731 debug_abbrev_offset = read_uint(reader);
1732 reader->address_size = read_uint8(&reader->p);
1733 }
1734 reader->q0 = reader->obj->debug_abbrev.ptr + debug_abbrev_offset;
1735
1736 reader->level = 0;
1737 di_read_debug_abbrev_cu(reader);
1738 if (di_read_debug_line_cu(reader)) return -1;
1739
1740#if defined(__GNUC__) && !defined(__clang__) && !defined(__INTEL_COMPILER_BUILD_DATE)
1741 /* Though DWARF specifies "the applicable base address defaults to the base
1742 address of the compilation unit", but GCC seems to use zero as default */
1743#else
1744 do {
1745 DIE die;
1746
1747 if (!di_read_die(reader, &die)) continue;
1748
1749 if (die.tag != DW_TAG_compile_unit) {
1750 di_skip_records(reader);
1751 break;
1752 }
1753
1754 reader->current_str_offsets_base = 0;
1755 reader->current_addr_base = 0;
1756 reader->current_rnglists_base = 0;
1757
1758 DebugInfoValue low_pc = {{}};
1759 /* enumerate abbrev */
1760 for (;;) {
1761 DebugInfoValue v = {{}};
1762 if (!di_read_record(reader, &v)) break;
1763 switch (v.at) {
1764 case DW_AT_low_pc:
1765 // clang may output DW_AT_addr_base after DW_AT_low_pc.
1766 // We need to resolve the DW_FORM_addr* after DW_AT_addr_base is parsed.
1767 low_pc = v;
1768 break;
1769 case DW_AT_str_offsets_base:
1770 reader->current_str_offsets_base = v.as.uint64;
1771 break;
1772 case DW_AT_addr_base:
1773 reader->current_addr_base = v.as.uint64;
1774 break;
1775 case DW_AT_rnglists_base:
1776 reader->current_rnglists_base = v.as.uint64;
1777 break;
1778 }
1779 }
1780 // Resolve the DW_FORM_addr of DW_AT_low_pc
1781 switch (low_pc.type) {
1782 case VAL_uint:
1783 reader->current_low_pc = low_pc.as.uint64;
1784 break;
1785 case VAL_addr:
1786 {
1787 addr_header_t header;
1788 addr_header_init(reader->obj, &header);
1789 reader->current_low_pc = read_addr(&header, reader->current_addr_base, low_pc.as.addr_idx);
1790 }
1791 break;
1792 }
1793 } while (0);
1794#endif
1795 return 0;
1796}
1797
1798static void
1799read_abstract_origin(DebugInfoReader *reader, uint64_t form, uint64_t abstract_origin, line_info_t *line)
1800{
1801 const char *p = reader->p;
1802 const char *q = reader->q;
1803 int level = reader->level;
1804 DIE die;
1805
1806 switch (form) {
1807 case DW_FORM_ref1:
1808 case DW_FORM_ref2:
1809 case DW_FORM_ref4:
1810 case DW_FORM_ref8:
1811 case DW_FORM_ref_udata:
1812 reader->p = reader->current_cu + abstract_origin;
1813 break;
1814 case DW_FORM_ref_addr:
1815 goto finish; /* not supported yet */
1816 case DW_FORM_ref_sig8:
1817 goto finish; /* not supported yet */
1818 case DW_FORM_ref_sup4:
1819 case DW_FORM_ref_sup8:
1820 goto finish; /* not supported yet */
1821 default:
1822 goto finish;
1823 }
1824 if (!di_read_die(reader, &die)) goto finish;
1825
1826 /* enumerate abbrev */
1827 for (;;) {
1828 DebugInfoValue v = {{}};
1829 if (!di_read_record(reader, &v)) break;
1830 switch (v.at) {
1831 case DW_AT_name:
1832 line->sname = get_cstr_value(&v);
1833 break;
1834 }
1835 }
1836
1837 finish:
1838 reader->p = p;
1839 reader->q = q;
1840 reader->level = level;
1841}
1842
1843static void
1844debug_info_read(DebugInfoReader *reader, int num_traces, void **traces,
1845 line_info_t *lines, int offset) {
1846
1847 addr_header_t addr_header = {};
1848 addr_header_init(reader->obj, &addr_header);
1849
1850 rnglists_header_t rnglists_header = {};
1851 rnglists_header_init(reader->obj, &rnglists_header);
1852
1853 while (reader->p < reader->cu_end) {
1854 DIE die;
1855 ranges_t ranges = {};
1856 line_info_t line = {};
1857
1858 if (!di_read_die(reader, &die)) continue;
1859 /* fprintf(stderr,"%d:%tx: <%d>\n",__LINE__,die.pos,reader->level,die.tag); */
1860
1861 if (die.tag != DW_TAG_subprogram && die.tag != DW_TAG_inlined_subroutine) {
1862 skip_die:
1863 di_skip_records(reader);
1864 continue;
1865 }
1866
1867 /* enumerate abbrev */
1868 for (;;) {
1869 DebugInfoValue v = {{}};
1870 /* ptrdiff_t pos = reader->p - reader->p0; */
1871 if (!di_read_record(reader, &v)) break;
1872 /* fprintf(stderr,"\n%d:%tx: AT:%lx FORM:%lx\n",__LINE__,pos,v.at,v.form); */
1873 /* div_inspect(&v); */
1874 switch (v.at) {
1875 case DW_AT_name:
1876 line.sname = get_cstr_value(&v);
1877 break;
1878 case DW_AT_call_file:
1879 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);
1880 break;
1881 case DW_AT_call_line:
1882 line.line = (int)v.as.uint64;
1883 break;
1884 case DW_AT_low_pc:
1885 case DW_AT_high_pc:
1886 case DW_AT_ranges:
1887 ranges_set(&ranges, &v, &addr_header, reader->current_addr_base);
1888 break;
1889 case DW_AT_declaration:
1890 goto skip_die;
1891 case DW_AT_inline:
1892 /* 1 or 3 */
1893 break; /* goto skip_die; */
1894 case DW_AT_abstract_origin:
1895 read_abstract_origin(reader, v.form, v.as.uint64, &line);
1896 break; /* goto skip_die; */
1897 }
1898 }
1899 /* ranges_inspect(reader, &ranges); */
1900 /* fprintf(stderr,"%d:%tx: %x ",__LINE__,diepos,die.tag); */
1901 for (int i=offset; i < num_traces; i++) {
1902 uintptr_t addr = (uintptr_t)traces[i];
1903 uintptr_t offset = addr - reader->obj->base_addr + reader->obj->vmaddr;
1904 uintptr_t saddr = ranges_include(reader, &ranges, offset, &rnglists_header);
1905 if (saddr) {
1906 /* fprintf(stdout, "%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); */
1907 if (lines[i].sname) {
1908 line_info_t *lp = malloc(sizeof(line_info_t));
1909 memcpy(lp, &lines[i], sizeof(line_info_t));
1910 lines[i].next = lp;
1911 lp->dirname = line.dirname;
1912 lp->filename = line.filename;
1913 lp->line = line.line;
1914 lp->saddr = 0;
1915 }
1916 lines[i].path = reader->obj->path;
1917 lines[i].base_addr = line.base_addr;
1918 lines[i].sname = line.sname;
1919 lines[i].saddr = saddr + reader->obj->base_addr - reader->obj->vmaddr;
1920 }
1921 }
1922 }
1923}
1924
1925// This function parses the following attributes of Line Number Program Header in DWARF 5:
1926//
1927// * directory_entry_format_count
1928// * directory_entry_format
1929// * directories_count
1930// * directories
1931//
1932// or
1933//
1934// * file_name_entry_format_count
1935// * file_name_entry_format
1936// * file_names_count
1937// * file_names
1938//
1939// It records DW_LNCT_path and DW_LNCT_directory_index at the index "idx".
1940static const char *
1941parse_ver5_debug_line_header(const char *p, int idx, uint8_t format, obj_info_t *obj, const char **out_path, uint64_t *out_directory_index) {
1942 int i, j;
1943 int entry_format_count = *(uint8_t *)p++;
1944 const char *entry_format = p;
1945
1946 /* skip the part of entry_format */
1947 for (i = 0; i < entry_format_count * 2; i++) uleb128(&p);
1948
1949 int entry_count = (int)uleb128(&p);
1950
1951 DebugInfoReader reader;
1952 debug_info_reader_init(&reader, obj);
1953 reader.format = format;
1954 reader.p = p;
1955 for (j = 0; j < entry_count; j++) {
1956 const char *format = entry_format;
1957 for (i = 0; i < entry_format_count; i++) {
1958 DebugInfoValue v = {{}};
1959 unsigned long dw_lnct = uleb128(&format);
1960 unsigned long dw_form = uleb128(&format);
1961 debug_info_reader_read_value(&reader, dw_form, &v);
1962 if (dw_lnct == 1 /* DW_LNCT_path */ && v.type == VAL_cstr && out_path)
1963 *out_path = v.as.ptr + v.off;
1964 if (dw_lnct == 2 /* DW_LNCT_directory_index */ && v.type == VAL_uint && out_directory_index)
1965 *out_directory_index = v.as.uint64;
1966 }
1967 if (j == idx) return 0;
1968 }
1969
1970 return reader.p;
1971}
1972
1973#ifdef USE_ELF
1974static unsigned long
1975uncompress_debug_section(ElfW(Shdr) *shdr, char *file, char **ptr)
1976{
1977 *ptr = NULL;
1978#ifdef SUPPORT_COMPRESSED_DEBUG_LINE
1979 ElfW(Chdr) *chdr = (ElfW(Chdr) *)(file + shdr->sh_offset);
1980 unsigned long destsize = chdr->ch_size;
1981 int ret = 0;
1982
1983 if (chdr->ch_type != ELFCOMPRESS_ZLIB) {
1984 /* unsupported compression type */
1985 return 0;
1986 }
1987
1988 *ptr = malloc(destsize);
1989 if (!*ptr) return 0;
1990 ret = uncompress((Bytef *)*ptr, &destsize,
1991 (const Bytef*)chdr + sizeof(ElfW(Chdr)),
1992 shdr->sh_size - sizeof(ElfW(Chdr)));
1993 if (ret != Z_OK) goto fail;
1994 return destsize;
1995
1996fail:
1997 free(*ptr);
1998 *ptr = NULL;
1999#endif
2000 return 0;
2001}
2002
2003/* read file and fill lines */
2004static uintptr_t
2005fill_lines(int num_traces, void **traces, int check_debuglink,
2006 obj_info_t **objp, line_info_t *lines, int offset)
2007{
2008 int i, j;
2009 char *shstr;
2010 ElfW(Ehdr) *ehdr;
2011 ElfW(Shdr) *shdr, *shstr_shdr;
2012 ElfW(Shdr) *gnu_debuglink_shdr = NULL;
2013 ElfW(Shdr) *note_gnu_build_id = NULL;
2014 int fd;
2015 off_t filesize;
2016 char *file;
2017 ElfW(Shdr) *symtab_shdr = NULL, *strtab_shdr = NULL;
2018 ElfW(Shdr) *dynsym_shdr = NULL, *dynstr_shdr = NULL;
2019 obj_info_t *obj = *objp;
2020 uintptr_t dladdr_fbase = 0;
2021
2022 fd = open(binary_filename, O_RDONLY);
2023 if (fd < 0) {
2024 goto fail;
2025 }
2026 filesize = lseek(fd, 0, SEEK_END);
2027 if (filesize < 0) {
2028 int e = errno;
2029 close(fd);
2030 kprintf("lseek: %s\n", strerror(e));
2031 goto fail;
2032 }
2033#if SIZEOF_OFF_T > SIZEOF_SIZE_T
2034 if (filesize > (off_t)SIZE_MAX) {
2035 close(fd);
2036 kprintf("Too large file %s\n", binary_filename);
2037 goto fail;
2038 }
2039#endif
2040 lseek(fd, 0, SEEK_SET);
2041 /* async-signal unsafe */
2042 file = (char *)mmap(NULL, (size_t)filesize, PROT_READ, MAP_SHARED, fd, 0);
2043 if (file == MAP_FAILED) {
2044 int e = errno;
2045 close(fd);
2046 kprintf("mmap: %s\n", strerror(e));
2047 goto fail;
2048 }
2049 close(fd);
2050
2051 ehdr = (ElfW(Ehdr) *)file;
2052 if (memcmp(ehdr->e_ident, "\177ELF", 4) != 0) {
2053 /*
2054 * Huh? Maybe filename was overridden by setproctitle() and
2055 * it match non-elf file.
2056 */
2057 goto fail;
2058 }
2059 obj->mapped = file;
2060 obj->mapped_size = (size_t)filesize;
2061
2062 shdr = (ElfW(Shdr) *)(file + ehdr->e_shoff);
2063
2064 shstr_shdr = shdr + ehdr->e_shstrndx;
2065 shstr = file + shstr_shdr->sh_offset;
2066
2067 for (i = 0; i < ehdr->e_shnum; i++) {
2068 char *section_name = shstr + shdr[i].sh_name;
2069 switch (shdr[i].sh_type) {
2070 case SHT_STRTAB:
2071 if (!strcmp(section_name, ".strtab")) {
2072 strtab_shdr = shdr + i;
2073 }
2074 else if (!strcmp(section_name, ".dynstr")) {
2075 dynstr_shdr = shdr + i;
2076 }
2077 break;
2078 case SHT_SYMTAB:
2079 /* if (!strcmp(section_name, ".symtab")) */
2080 symtab_shdr = shdr + i;
2081 break;
2082 case SHT_DYNSYM:
2083 /* if (!strcmp(section_name, ".dynsym")) */
2084 dynsym_shdr = shdr + i;
2085 break;
2086 case SHT_NOTE:
2087 if (!strcmp(section_name, ".note.gnu.build-id")) {
2088 note_gnu_build_id = shdr + i;
2089 }
2090 break;
2091 case SHT_PROGBITS:
2092 if (!strcmp(section_name, ".gnu_debuglink")) {
2093 gnu_debuglink_shdr = shdr + i;
2094 }
2095 else {
2096 const char *debug_section_names[] = {
2097 ".debug_abbrev",
2098 ".debug_info",
2099 ".debug_line",
2100 ".debug_ranges",
2101 ".debug_str_offsets",
2102 ".debug_addr",
2103 ".debug_rnglists",
2104 ".debug_str",
2105 ".debug_line_str"
2106 };
2107
2108 for (j=0; j < DWARF_SECTION_COUNT; j++) {
2109 struct dwarf_section *s = obj_dwarf_section_at(obj, j);
2110
2111 if (strcmp(section_name, debug_section_names[j]) != 0)
2112 continue;
2113
2114 s->ptr = file + shdr[i].sh_offset;
2115 s->size = shdr[i].sh_size;
2116 s->flags = shdr[i].sh_flags;
2117 if (s->flags & SHF_COMPRESSED) {
2118 s->size = uncompress_debug_section(&shdr[i], file, &s->ptr);
2119 if (!s->size) goto fail;
2120 }
2121 break;
2122 }
2123 }
2124 break;
2125 }
2126 }
2127
2128 if (offset == -1) {
2129 /* main executable */
2130 offset = 0;
2131 if (dynsym_shdr && dynstr_shdr) {
2132 char *strtab = file + dynstr_shdr->sh_offset;
2133 ElfW(Sym) *symtab = (ElfW(Sym) *)(file + dynsym_shdr->sh_offset);
2134 int symtab_count = (int)(dynsym_shdr->sh_size / sizeof(ElfW(Sym)));
2135 void *handle = dlopen(NULL, RTLD_NOW|RTLD_LOCAL);
2136 if (handle) {
2137 for (j = 0; j < symtab_count; j++) {
2138 ElfW(Sym) *sym = &symtab[j];
2139 Dl_info info;
2140 void *s;
2141 if (ELF_ST_TYPE(sym->st_info) != STT_FUNC || sym->st_size == 0) continue;
2142 s = dlsym(handle, strtab + sym->st_name);
2143 if (s && dladdr(s, &info)) {
2144 obj->base_addr = dladdr_fbase;
2145 dladdr_fbase = (uintptr_t)info.dli_fbase;
2146 break;
2147 }
2148 }
2149 dlclose(handle);
2150 }
2151 if (ehdr->e_type == ET_EXEC) {
2152 obj->base_addr = 0;
2153 }
2154 else {
2155 /* PIE (position-independent executable) */
2156 obj->base_addr = dladdr_fbase;
2157 }
2158 }
2159 }
2160
2161 if (obj->debug_info.ptr && obj->debug_abbrev.ptr) {
2162 DebugInfoReader reader;
2163 debug_info_reader_init(&reader, obj);
2164 i = 0;
2165 while (reader.p < reader.pend) {
2166 /* fprintf(stderr, "%d:%tx: CU[%d]\n", __LINE__, reader.p - reader.obj->debug_info.ptr, i++); */
2167 if (di_read_cu(&reader)) goto use_symtab;
2168 debug_info_read(&reader, num_traces, traces, lines, offset);
2169 }
2170 }
2171 else {
2172 /* This file doesn't have dwarf, use symtab or dynsym */
2173use_symtab:
2174 if (!symtab_shdr) {
2175 /* This file doesn't have symtab, use dynsym instead */
2176 symtab_shdr = dynsym_shdr;
2177 strtab_shdr = dynstr_shdr;
2178 }
2179
2180 if (symtab_shdr && strtab_shdr) {
2181 char *strtab = file + strtab_shdr->sh_offset;
2182 ElfW(Sym) *symtab = (ElfW(Sym) *)(file + symtab_shdr->sh_offset);
2183 int symtab_count = (int)(symtab_shdr->sh_size / sizeof(ElfW(Sym)));
2184 for (j = 0; j < symtab_count; j++) {
2185 ElfW(Sym) *sym = &symtab[j];
2186 uintptr_t saddr = (uintptr_t)sym->st_value + obj->base_addr;
2187 if (ELF_ST_TYPE(sym->st_info) != STT_FUNC) continue;
2188 for (i = offset; i < num_traces; i++) {
2189 uintptr_t d = (uintptr_t)traces[i] - saddr;
2190 if (lines[i].line > 0 || d > (uintptr_t)sym->st_size)
2191 continue;
2192 /* fill symbol name and addr from .symtab */
2193 if (!lines[i].sname) lines[i].sname = strtab + sym->st_name;
2194 lines[i].saddr = saddr;
2195 lines[i].path = obj->path;
2196 lines[i].base_addr = obj->base_addr;
2197 }
2198 }
2199 }
2200 }
2201
2202 if (!obj->debug_line.ptr) {
2203 /* This file doesn't have .debug_line section,
2204 let's check .gnu_debuglink section instead. */
2205 if (gnu_debuglink_shdr && check_debuglink) {
2206 follow_debuglink(file + gnu_debuglink_shdr->sh_offset,
2207 num_traces, traces,
2208 objp, lines, offset);
2209 }
2210 if (note_gnu_build_id && check_debuglink) {
2211 ElfW(Nhdr) *nhdr = (ElfW(Nhdr)*) (file + note_gnu_build_id->sh_offset);
2212 const char *build_id = (char *)(nhdr + 1) + nhdr->n_namesz;
2213 follow_debuglink_build_id(build_id, nhdr->n_descsz,
2214 num_traces, traces,
2215 objp, lines, offset);
2216 }
2217 goto finish;
2218 }
2219
2220 if (parse_debug_line(num_traces, traces,
2221 obj->debug_line.ptr,
2222 obj->debug_line.size,
2223 obj, lines, offset) == -1)
2224 goto fail;
2225
2226finish:
2227 return dladdr_fbase;
2228fail:
2229 return (uintptr_t)-1;
2230}
2231#else /* Mach-O */
2232/* read file and fill lines */
2233static uintptr_t
2234fill_lines(int num_traces, void **traces, int check_debuglink,
2235 obj_info_t **objp, line_info_t *lines, int offset)
2236{
2237# ifdef __LP64__
2238# define LP(x) x##_64
2239# else
2240# define LP(x) x
2241# endif
2242 int fd;
2243 off_t filesize;
2244 char *file, *p = NULL;
2245 obj_info_t *obj = *objp;
2246 struct LP(mach_header) *header;
2247 uintptr_t dladdr_fbase = 0;
2248
2249 {
2250 char *s = binary_filename;
2251 char *base = strrchr(binary_filename, '/')+1;
2252 size_t max = PATH_MAX;
2253 size_t size = strlen(binary_filename);
2254 size_t basesize = size - (base - binary_filename);
2255 s += size;
2256 max -= size;
2257 p = s;
2258 size = strlcpy(s, ".dSYM/Contents/Resources/DWARF/", max);
2259 if (size == 0) goto fail;
2260 s += size;
2261 max -= size;
2262 if (max <= basesize) goto fail;
2263 memcpy(s, base, basesize);
2264 s[basesize] = 0;
2265
2266 fd = open(binary_filename, O_RDONLY);
2267 if (fd < 0) {
2268 *p = 0; /* binary_filename becomes original file name */
2269 fd = open(binary_filename, O_RDONLY);
2270 if (fd < 0) {
2271 goto fail;
2272 }
2273 }
2274 }
2275
2276 filesize = lseek(fd, 0, SEEK_END);
2277 if (filesize < 0) {
2278 int e = errno;
2279 close(fd);
2280 kprintf("lseek: %s\n", strerror(e));
2281 goto fail;
2282 }
2283#if SIZEOF_OFF_T > SIZEOF_SIZE_T
2284 if (filesize > (off_t)SIZE_MAX) {
2285 close(fd);
2286 kprintf("Too large file %s\n", binary_filename);
2287 goto fail;
2288 }
2289#endif
2290 lseek(fd, 0, SEEK_SET);
2291 /* async-signal unsafe */
2292 file = (char *)mmap(NULL, (size_t)filesize, PROT_READ, MAP_SHARED, fd, 0);
2293 if (file == MAP_FAILED) {
2294 int e = errno;
2295 close(fd);
2296 kprintf("mmap: %s\n", strerror(e));
2297 goto fail;
2298 }
2299 close(fd);
2300
2301 obj->mapped = file;
2302 obj->mapped_size = (size_t)filesize;
2303
2304 header = (struct LP(mach_header) *)file;
2305 if (header->magic == LP(MH_MAGIC)) {
2306 /* non universal binary */
2307 p = file;
2308 }
2309 else if (header->magic == FAT_CIGAM) {
2310 struct LP(mach_header) *mhp = _NSGetMachExecuteHeader();
2311 struct fat_header *fat = (struct fat_header *)file;
2312 char *q = file + sizeof(*fat);
2313 uint32_t nfat_arch = __builtin_bswap32(fat->nfat_arch);
2314 /* fprintf(stderr,"%d: fat:%s %d\n",__LINE__, binary_filename,nfat_arch); */
2315 for (uint32_t i = 0; i < nfat_arch; i++) {
2316 struct fat_arch *arch = (struct fat_arch *)q;
2317 cpu_type_t cputype = __builtin_bswap32(arch->cputype);
2318 cpu_subtype_t cpusubtype = __builtin_bswap32(arch->cpusubtype);
2319 uint32_t offset = __builtin_bswap32(arch->offset);
2320 /* fprintf(stderr,"%d: fat %d %x/%x %x/%x\n",__LINE__, i, mhp->cputype,mhp->cpusubtype, cputype,cpusubtype); */
2321 if (mhp->cputype == cputype &&
2322 (cpu_subtype_t)(mhp->cpusubtype & ~CPU_SUBTYPE_MASK) == cpusubtype) {
2323 p = file + offset;
2324 file = p;
2325 header = (struct LP(mach_header) *)p;
2326 if (header->magic == LP(MH_MAGIC)) {
2327 goto found_mach_header;
2328 }
2329 break;
2330 }
2331 q += sizeof(*arch);
2332 }
2333 kprintf("'%s' is not a Mach-O universal binary file!\n",binary_filename);
2334 close(fd);
2335 goto fail;
2336 }
2337 else {
2338 kprintf("'%s' is not a "
2339# ifdef __LP64__
2340 "64"
2341# else
2342 "32"
2343# endif
2344 "-bit Mach-O file!\n",binary_filename);
2345 close(fd);
2346 goto fail;
2347 }
2348found_mach_header:
2349 p += sizeof(*header);
2350
2351 for (uint32_t i = 0; i < (uint32_t)header->ncmds; i++) {
2352 struct load_command *lcmd = (struct load_command *)p;
2353 switch (lcmd->cmd) {
2354 case LP(LC_SEGMENT):
2355 {
2356 static const char *debug_section_names[] = {
2357 "__debug_abbrev",
2358 "__debug_info",
2359 "__debug_line",
2360 "__debug_ranges",
2361 "__debug_str_offsets",
2362 "__debug_addr",
2363 "__debug_rnglists",
2364 "__debug_str",
2365 "__debug_line_str",
2366 };
2367 struct LP(segment_command) *scmd = (struct LP(segment_command) *)lcmd;
2368 if (strcmp(scmd->segname, "__TEXT") == 0) {
2369 obj->vmaddr = scmd->vmaddr;
2370 }
2371 else if (strcmp(scmd->segname, "__DWARF") == 0) {
2372 p += sizeof(struct LP(segment_command));
2373 for (uint64_t i = 0; i < scmd->nsects; i++) {
2374 struct LP(section) *sect = (struct LP(section) *)p;
2375 p += sizeof(struct LP(section));
2376 for (int j=0; j < DWARF_SECTION_COUNT; j++) {
2377 struct dwarf_section *s = obj_dwarf_section_at(obj, j);
2378
2379 if (strcmp(sect->sectname, debug_section_names[j]) != 0)
2380 continue;
2381
2382 s->ptr = file + sect->offset;
2383 s->size = sect->size;
2384 s->flags = sect->flags;
2385 if (s->flags & SHF_COMPRESSED) {
2386 goto fail;
2387 }
2388 break;
2389 }
2390 }
2391 }
2392 }
2393 break;
2394
2395 case LC_SYMTAB:
2396 {
2397 struct symtab_command *cmd = (struct symtab_command *)lcmd;
2398 struct LP(nlist) *nl = (struct LP(nlist) *)(file + cmd->symoff);
2399 char *strtab = file + cmd->stroff, *sname = 0;
2400 uint32_t j;
2401 uintptr_t saddr = 0;
2402 /* kprintf("[%2d]: %x/symtab %p\n", i, cmd->cmd, (void *)p); */
2403 for (j = 0; j < cmd->nsyms; j++) {
2404 uintptr_t symsize, d;
2405 struct LP(nlist) *e = &nl[j];
2406 /* 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); */
2407 if (e->n_type != N_FUN) continue;
2408 if (e->n_sect) {
2409 saddr = (uintptr_t)e->n_value + obj->base_addr - obj->vmaddr;
2410 sname = strtab + e->n_un.n_strx;
2411 /* 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); */
2412 continue;
2413 }
2414 for (int k = offset; k < num_traces; k++) {
2415 d = (uintptr_t)traces[k] - saddr;
2416 symsize = e->n_value;
2417 /* kprintf("%lx %lx %lx\n",saddr,symsize,traces[k]); */
2418 if (lines[k].line > 0 || d > (uintptr_t)symsize)
2419 continue;
2420 /* fill symbol name and addr from .symtab */
2421 if (!lines[k].sname) lines[k].sname = sname;
2422 lines[k].saddr = saddr;
2423 lines[k].path = obj->path;
2424 lines[k].base_addr = obj->base_addr;
2425 }
2426 }
2427 }
2428 }
2429 p += lcmd->cmdsize;
2430 }
2431
2432 if (obj->debug_info.ptr && obj->debug_abbrev.ptr) {
2433 DebugInfoReader reader;
2434 debug_info_reader_init(&reader, obj);
2435 while (reader.p < reader.pend) {
2436 if (di_read_cu(&reader)) goto fail;
2437 debug_info_read(&reader, num_traces, traces, lines, offset);
2438 }
2439 }
2440
2441 if (parse_debug_line(num_traces, traces,
2442 obj->debug_line.ptr,
2443 obj->debug_line.size,
2444 obj, lines, offset) == -1)
2445 goto fail;
2446
2447 return dladdr_fbase;
2448fail:
2449 return (uintptr_t)-1;
2450}
2451#endif
2452
2453#define HAVE_MAIN_EXE_PATH
2454#if defined(__FreeBSD__) || defined(__DragonFly__)
2455# include <sys/sysctl.h>
2456#endif
2457/* ssize_t main_exe_path(void)
2458 *
2459 * store the path of the main executable to `binary_filename`,
2460 * and returns strlen(binary_filename).
2461 * it is NUL terminated.
2462 */
2463#if defined(__linux__) || defined(__NetBSD__)
2464static ssize_t
2465main_exe_path(void)
2466{
2467# if defined(__linux__)
2468# define PROC_SELF_EXE "/proc/self/exe"
2469# elif defined(__NetBSD__)
2470# define PROC_SELF_EXE "/proc/curproc/exe"
2471# endif
2472 ssize_t len = readlink(PROC_SELF_EXE, binary_filename, PATH_MAX);
2473 if (len < 0) return 0;
2474 binary_filename[len] = 0;
2475 return len;
2476}
2477#elif defined(__FreeBSD__) || defined(__DragonFly__)
2478static ssize_t
2479main_exe_path(void)
2480{
2481 int mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1};
2482 size_t len = PATH_MAX;
2483 int err = sysctl(mib, 4, binary_filename, &len, NULL, 0);
2484 if (err) {
2485 kprintf("Can't get the path of ruby");
2486 return -1;
2487 }
2488 len--; /* sysctl sets strlen+1 */
2489 return len;
2490}
2491#elif defined(HAVE_LIBPROC_H)
2492static ssize_t
2493main_exe_path(void)
2494{
2495 int len = proc_pidpath(getpid(), binary_filename, PATH_MAX);
2496 if (len == 0) return 0;
2497 binary_filename[len] = 0;
2498 return len;
2499}
2500#else
2501#undef HAVE_MAIN_EXE_PATH
2502#endif
2503
2504static void
2505print_line0(line_info_t *line, void *address)
2506{
2507 uintptr_t addr = (uintptr_t)address;
2508 uintptr_t d = addr - line->saddr;
2509 if (!address) {
2510 /* inlined */
2511 if (line->dirname && line->dirname[0]) {
2512 kprintf("%s(%s) %s/%s:%d\n", line->path, line->sname, line->dirname, line->filename, line->line);
2513 }
2514 else {
2515 kprintf("%s(%s) %s:%d\n", line->path, line->sname, line->filename, line->line);
2516 }
2517 }
2518 else if (!line->path) {
2519 kprintf("[0x%"PRIxPTR"]\n", addr);
2520 }
2521 else if (!line->sname) {
2522 kprintf("%s(0x%"PRIxPTR") [0x%"PRIxPTR"]\n", line->path, addr-line->base_addr, addr);
2523 }
2524 else if (!line->saddr) {
2525 kprintf("%s(%s) [0x%"PRIxPTR"]\n", line->path, line->sname, addr);
2526 }
2527 else if (line->line <= 0) {
2528 kprintf("%s(%s+0x%"PRIxPTR") [0x%"PRIxPTR"]\n", line->path, line->sname,
2529 d, addr);
2530 }
2531 else if (!line->filename) {
2532 kprintf("%s(%s+0x%"PRIxPTR") [0x%"PRIxPTR"] ???:%d\n", line->path, line->sname,
2533 d, addr, line->line);
2534 }
2535 else if (line->dirname && line->dirname[0]) {
2536 kprintf("%s(%s+0x%"PRIxPTR") [0x%"PRIxPTR"] %s/%s:%d\n", line->path, line->sname,
2537 d, addr, line->dirname, line->filename, line->line);
2538 }
2539 else {
2540 kprintf("%s(%s+0x%"PRIxPTR") [0x%"PRIxPTR"] %s:%d\n", line->path, line->sname,
2541 d, addr, line->filename, line->line);
2542 }
2543}
2544
2545static void
2546print_line(line_info_t *line, void *address)
2547{
2548 print_line0(line, address);
2549 if (line->next) {
2550 print_line(line->next, NULL);
2551 }
2552}
2553
2554void
2555rb_dump_backtrace_with_lines(int num_traces, void **traces)
2556{
2557 int i;
2558 /* async-signal unsafe */
2559 line_info_t *lines = (line_info_t *)calloc(num_traces, sizeof(line_info_t));
2560 obj_info_t *obj = NULL;
2561 /* 2 is NULL + main executable */
2562 void **dladdr_fbases = (void **)calloc(num_traces+2, sizeof(void *));
2563
2564#ifdef HAVE_MAIN_EXE_PATH
2565 char *main_path = NULL; /* used on printing backtrace */
2566 ssize_t len;
2567 if ((len = main_exe_path()) > 0) {
2568 main_path = (char *)alloca(len + 1);
2569 if (main_path) {
2570 uintptr_t addr;
2571 memcpy(main_path, binary_filename, len+1);
2572 append_obj(&obj);
2573 obj->path = main_path;
2574 addr = fill_lines(num_traces, traces, 1, &obj, lines, -1);
2575 if (addr != (uintptr_t)-1) {
2576 dladdr_fbases[0] = (void *)addr;
2577 }
2578 }
2579 }
2580#endif
2581
2582 /* fill source lines by reading dwarf */
2583 for (i = 0; i < num_traces; i++) {
2584 Dl_info info;
2585 if (lines[i].line) continue;
2586 if (dladdr(traces[i], &info)) {
2587 const char *path;
2588 void **p;
2589
2590 /* skip symbols which is in already checked objects */
2591 /* if the binary is strip-ed, this may effect */
2592 for (p=dladdr_fbases; *p; p++) {
2593 if (*p == info.dli_fbase) {
2594 if (info.dli_fname) lines[i].path = info.dli_fname;
2595 if (info.dli_sname) lines[i].sname = info.dli_sname;
2596 goto next_line;
2597 }
2598 }
2599 *p = info.dli_fbase;
2600
2601 append_obj(&obj);
2602 obj->base_addr = (uintptr_t)info.dli_fbase;
2603 path = info.dli_fname;
2604 obj->path = path;
2605 if (path) lines[i].path = path;
2606 if (info.dli_sname) {
2607 lines[i].sname = info.dli_sname;
2608 lines[i].saddr = (uintptr_t)info.dli_saddr;
2609 }
2610 strlcpy(binary_filename, path, PATH_MAX);
2611 if (fill_lines(num_traces, traces, 1, &obj, lines, i) == (uintptr_t)-1)
2612 break;
2613 }
2614next_line:
2615 continue;
2616 }
2617
2618 /* output */
2619 for (i = 0; i < num_traces; i++) {
2620 print_line(&lines[i], traces[i]);
2621
2622 /* FreeBSD's backtrace may show _start and so on */
2623 if (lines[i].sname && strcmp("main", lines[i].sname) == 0)
2624 break;
2625 }
2626
2627 /* free */
2628 while (obj) {
2629 obj_info_t *o = obj;
2630 for (i=0; i < DWARF_SECTION_COUNT; i++) {
2631 struct dwarf_section *s = obj_dwarf_section_at(obj, i);
2632 if (s->flags & SHF_COMPRESSED) {
2633 free(s->ptr);
2634 }
2635 }
2636 if (obj->mapped_size) {
2637 munmap(obj->mapped, obj->mapped_size);
2638 }
2639 obj = o->next;
2640 free(o);
2641 }
2642 for (i = 0; i < num_traces; i++) {
2643 line_info_t *line = lines[i].next;
2644 while (line) {
2645 line_info_t *l = line;
2646 line = line->next;
2647 free(l);
2648 }
2649 }
2650 free(lines);
2651 free(dladdr_fbases);
2652}
2653
2654/* From FreeBSD's lib/libstand/printf.c */
2655/*-
2656 * Copyright (c) 1986, 1988, 1991, 1993
2657 * The Regents of the University of California. All rights reserved.
2658 * (c) UNIX System Laboratories, Inc.
2659 * All or some portions of this file are derived from material licensed
2660 * to the University of California by American Telephone and Telegraph
2661 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
2662 * the permission of UNIX System Laboratories, Inc.
2663 *
2664 * Redistribution and use in source and binary forms, with or without
2665 * modification, are permitted provided that the following conditions
2666 * are met:
2667 * 1. Redistributions of source code must retain the above copyright
2668 * notice, this list of conditions and the following disclaimer.
2669 * 2. Redistributions in binary form must reproduce the above copyright
2670 * notice, this list of conditions and the following disclaimer in the
2671 * documentation and/or other materials provided with the distribution.
2672 * 4. Neither the name of the University nor the names of its contributors
2673 * may be used to endorse or promote products derived from this software
2674 * without specific prior written permission.
2675 *
2676 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2677 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2678 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2679 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2680 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2681 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2682 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2683 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2684 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2685 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2686 * SUCH DAMAGE.
2687 *
2688 * @(#)subr_prf.c 8.3 (Berkeley) 1/21/94
2689 */
2690
2691#include <stdarg.h>
2692#define MAXNBUF (sizeof(intmax_t) * CHAR_BIT + 1)
2693static inline int toupper(int c) { return ('A' <= c && c <= 'Z') ? (c&0x5f) : c; }
2694#define hex2ascii(hex) (hex2ascii_data[hex])
2695static const char hex2ascii_data[] = "0123456789abcdefghijklmnopqrstuvwxyz";
2696static inline int imax(int a, int b) { return (a > b ? a : b); }
2697static int kvprintf(char const *fmt, void (*func)(int), void *arg, int radix, va_list ap);
2698
2699static void putce(int c)
2700{
2701 char s[1];
2702 ssize_t ret;
2703
2704 s[0] = (char)c;
2705 ret = write(2, s, 1);
2706 (void)ret;
2707}
2708
2709static int
2710kprintf(const char *fmt, ...)
2711{
2712 va_list ap;
2713 int retval;
2714
2715 va_start(ap, fmt);
2716 retval = kvprintf(fmt, putce, NULL, 10, ap);
2717 va_end(ap);
2718 return retval;
2719}
2720
2721/*
2722 * Put a NUL-terminated ASCII number (base <= 36) in a buffer in reverse
2723 * order; return an optional length and a pointer to the last character
2724 * written in the buffer (i.e., the first character of the string).
2725 * The buffer pointed to by `nbuf' must have length >= MAXNBUF.
2726 */
2727static char *
2728ksprintn(char *nbuf, uintmax_t num, int base, int *lenp, int upper)
2729{
2730 char *p, c;
2731
2732 p = nbuf;
2733 *p = '\0';
2734 do {
2735 c = hex2ascii(num % base);
2736 *++p = upper ? toupper(c) : c;
2737 } while (num /= base);
2738 if (lenp)
2739 *lenp = (int)(p - nbuf);
2740 return (p);
2741}
2742
2743/*
2744 * Scaled down version of printf(3).
2745 *
2746 * Two additional formats:
2747 *
2748 * The format %b is supported to decode error registers.
2749 * Its usage is:
2750 *
2751 * printf("reg=%b\n", regval, "<base><arg>*");
2752 *
2753 * where <base> is the output base expressed as a control character, e.g.
2754 * \10 gives octal; \20 gives hex. Each arg is a sequence of characters,
2755 * the first of which gives the bit number to be inspected (origin 1), and
2756 * the next characters (up to a control character, i.e. a character <= 32),
2757 * give the name of the register. Thus:
2758 *
2759 * kvprintf("reg=%b\n", 3, "\10\2BITTWO\1BITONE\n");
2760 *
2761 * would produce output:
2762 *
2763 * reg=3<BITTWO,BITONE>
2764 *
2765 * XXX: %D -- Hexdump, takes pointer and separator string:
2766 * ("%6D", ptr, ":") -> XX:XX:XX:XX:XX:XX
2767 * ("%*D", len, ptr, " " -> XX XX XX XX ...
2768 */
2769static int
2770kvprintf(char const *fmt, void (*func)(int), void *arg, int radix, va_list ap)
2771{
2772#define PCHAR(c) {int cc=(c); if (func) (*func)(cc); else *d++ = cc; retval++; }
2773 char nbuf[MAXNBUF];
2774 char *d;
2775 const char *p, *percent, *q;
2776 unsigned char *up;
2777 int ch, n;
2778 uintmax_t num;
2779 int base, lflag, qflag, tmp, width, ladjust, sharpflag, neg, sign, dot;
2780 int cflag, hflag, jflag, tflag, zflag;
2781 int dwidth, upper;
2782 char padc;
2783 int stop = 0, retval = 0;
2784
2785 num = 0;
2786 if (!func)
2787 d = (char *) arg;
2788 else
2789 d = NULL;
2790
2791 if (fmt == NULL)
2792 fmt = "(fmt null)\n";
2793
2794 if (radix < 2 || radix > 36)
2795 radix = 10;
2796
2797 for (;;) {
2798 padc = ' ';
2799 width = 0;
2800 while ((ch = (unsigned char)*fmt++) != '%' || stop) {
2801 if (ch == '\0')
2802 return (retval);
2803 PCHAR(ch);
2804 }
2805 percent = fmt - 1;
2806 qflag = 0; lflag = 0; ladjust = 0; sharpflag = 0; neg = 0;
2807 sign = 0; dot = 0; dwidth = 0; upper = 0;
2808 cflag = 0; hflag = 0; jflag = 0; tflag = 0; zflag = 0;
2809reswitch: switch (ch = (unsigned char)*fmt++) {
2810 case '.':
2811 dot = 1;
2812 goto reswitch;
2813 case '#':
2814 sharpflag = 1;
2815 goto reswitch;
2816 case '+':
2817 sign = 1;
2818 goto reswitch;
2819 case '-':
2820 ladjust = 1;
2821 goto reswitch;
2822 case '%':
2823 PCHAR(ch);
2824 break;
2825 case '*':
2826 if (!dot) {
2827 width = va_arg(ap, int);
2828 if (width < 0) {
2829 ladjust = !ladjust;
2830 width = -width;
2831 }
2832 } else {
2833 dwidth = va_arg(ap, int);
2834 }
2835 goto reswitch;
2836 case '0':
2837 if (!dot) {
2838 padc = '0';
2839 goto reswitch;
2840 }
2841 case '1': case '2': case '3': case '4':
2842 case '5': case '6': case '7': case '8': case '9':
2843 for (n = 0;; ++fmt) {
2844 n = n * 10 + ch - '0';
2845 ch = *fmt;
2846 if (ch < '0' || ch > '9')
2847 break;
2848 }
2849 if (dot)
2850 dwidth = n;
2851 else
2852 width = n;
2853 goto reswitch;
2854 case 'b':
2855 num = (unsigned int)va_arg(ap, int);
2856 p = va_arg(ap, char *);
2857 for (q = ksprintn(nbuf, num, *p++, NULL, 0); *q;)
2858 PCHAR(*q--);
2859
2860 if (num == 0)
2861 break;
2862
2863 for (tmp = 0; *p;) {
2864 n = *p++;
2865 if (num & (1 << (n - 1))) {
2866 PCHAR(tmp ? ',' : '<');
2867 for (; (n = *p) > ' '; ++p)
2868 PCHAR(n);
2869 tmp = 1;
2870 } else
2871 for (; *p > ' '; ++p)
2872 continue;
2873 }
2874 if (tmp)
2875 PCHAR('>');
2876 break;
2877 case 'c':
2878 PCHAR(va_arg(ap, int));
2879 break;
2880 case 'D':
2881 up = va_arg(ap, unsigned char *);
2882 p = va_arg(ap, char *);
2883 if (!width)
2884 width = 16;
2885 while(width--) {
2886 PCHAR(hex2ascii(*up >> 4));
2887 PCHAR(hex2ascii(*up & 0x0f));
2888 up++;
2889 if (width)
2890 for (q=p;*q;q++)
2891 PCHAR(*q);
2892 }
2893 break;
2894 case 'd':
2895 case 'i':
2896 base = 10;
2897 sign = 1;
2898 goto handle_sign;
2899 case 'h':
2900 if (hflag) {
2901 hflag = 0;
2902 cflag = 1;
2903 } else
2904 hflag = 1;
2905 goto reswitch;
2906 case 'j':
2907 jflag = 1;
2908 goto reswitch;
2909 case 'l':
2910 if (lflag) {
2911 lflag = 0;
2912 qflag = 1;
2913 } else
2914 lflag = 1;
2915 goto reswitch;
2916 case 'n':
2917 if (jflag)
2918 *(va_arg(ap, intmax_t *)) = retval;
2919 else if (qflag)
2920 *(va_arg(ap, int64_t *)) = retval;
2921 else if (lflag)
2922 *(va_arg(ap, long *)) = retval;
2923 else if (zflag)
2924 *(va_arg(ap, size_t *)) = retval;
2925 else if (hflag)
2926 *(va_arg(ap, short *)) = retval;
2927 else if (cflag)
2928 *(va_arg(ap, char *)) = retval;
2929 else
2930 *(va_arg(ap, int *)) = retval;
2931 break;
2932 case 'o':
2933 base = 8;
2934 goto handle_nosign;
2935 case 'p':
2936 base = 16;
2937 sharpflag = (width == 0);
2938 sign = 0;
2939 num = (uintptr_t)va_arg(ap, void *);
2940 goto number;
2941 case 'q':
2942 qflag = 1;
2943 goto reswitch;
2944 case 'r':
2945 base = radix;
2946 if (sign)
2947 goto handle_sign;
2948 goto handle_nosign;
2949 case 's':
2950 p = va_arg(ap, char *);
2951 if (p == NULL)
2952 p = "(null)";
2953 if (!dot)
2954 n = (int)strlen (p);
2955 else
2956 for (n = 0; n < dwidth && p[n]; n++)
2957 continue;
2958
2959 width -= n;
2960
2961 if (!ladjust && width > 0)
2962 while (width--)
2963 PCHAR(padc);
2964 while (n--)
2965 PCHAR(*p++);
2966 if (ladjust && width > 0)
2967 while (width--)
2968 PCHAR(padc);
2969 break;
2970 case 't':
2971 tflag = 1;
2972 goto reswitch;
2973 case 'u':
2974 base = 10;
2975 goto handle_nosign;
2976 case 'X':
2977 upper = 1;
2978 case 'x':
2979 base = 16;
2980 goto handle_nosign;
2981 case 'y':
2982 base = 16;
2983 sign = 1;
2984 goto handle_sign;
2985 case 'z':
2986 zflag = 1;
2987 goto reswitch;
2988handle_nosign:
2989 sign = 0;
2990 if (jflag)
2991 num = va_arg(ap, uintmax_t);
2992 else if (qflag)
2993 num = va_arg(ap, uint64_t);
2994 else if (tflag)
2995 num = va_arg(ap, ptrdiff_t);
2996 else if (lflag)
2997 num = va_arg(ap, unsigned long);
2998 else if (zflag)
2999 num = va_arg(ap, size_t);
3000 else if (hflag)
3001 num = (unsigned short)va_arg(ap, int);
3002 else if (cflag)
3003 num = (unsigned char)va_arg(ap, int);
3004 else
3005 num = va_arg(ap, unsigned int);
3006 goto number;
3007handle_sign:
3008 if (jflag)
3009 num = va_arg(ap, intmax_t);
3010 else if (qflag)
3011 num = va_arg(ap, int64_t);
3012 else if (tflag)
3013 num = va_arg(ap, ptrdiff_t);
3014 else if (lflag)
3015 num = va_arg(ap, long);
3016 else if (zflag)
3017 num = va_arg(ap, ssize_t);
3018 else if (hflag)
3019 num = (short)va_arg(ap, int);
3020 else if (cflag)
3021 num = (char)va_arg(ap, int);
3022 else
3023 num = va_arg(ap, int);
3024number:
3025 if (sign && (intmax_t)num < 0) {
3026 neg = 1;
3027 num = -(intmax_t)num;
3028 }
3029 p = ksprintn(nbuf, num, base, &n, upper);
3030 tmp = 0;
3031 if (sharpflag && num != 0) {
3032 if (base == 8)
3033 tmp++;
3034 else if (base == 16)
3035 tmp += 2;
3036 }
3037 if (neg)
3038 tmp++;
3039
3040 if (!ladjust && padc == '0')
3041 dwidth = width - tmp;
3042 width -= tmp + imax(dwidth, n);
3043 dwidth -= n;
3044 if (!ladjust)
3045 while (width-- > 0)
3046 PCHAR(' ');
3047 if (neg)
3048 PCHAR('-');
3049 if (sharpflag && num != 0) {
3050 if (base == 8) {
3051 PCHAR('0');
3052 } else if (base == 16) {
3053 PCHAR('0');
3054 PCHAR('x');
3055 }
3056 }
3057 while (dwidth-- > 0)
3058 PCHAR('0');
3059
3060 while (*p)
3061 PCHAR(*p--);
3062
3063 if (ladjust)
3064 while (width-- > 0)
3065 PCHAR(' ');
3066
3067 break;
3068 default:
3069 while (percent < fmt)
3070 PCHAR(*percent++);
3071 /*
3072 * Since we ignore an formatting argument it is no
3073 * longer safe to obey the remaining formatting
3074 * arguments as the arguments will no longer match
3075 * the format specs.
3076 */
3077 stop = 1;
3078 break;
3079 }
3080 }
3081#undef PCHAR
3082}
3083#else /* defined(USE_ELF) */
3084#error not supported
3085#endif
VALUE type(ANYARGS)
ANYARGS-ed function type.
Defines old _.
C99 shim for <stdbool.h>