PR c++/67184 (again)
[official-gcc.git] / libbacktrace / dwarf.c
blobf338489fe4496fa8c550722936eb0fb49b11bc7b
1 /* dwarf.c -- Get file/line information from DWARF for backtraces.
2 Copyright (C) 2012-2019 Free Software Foundation, Inc.
3 Written by Ian Lance Taylor, Google.
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are
7 met:
9 (1) Redistributions of source code must retain the above copyright
10 notice, this list of conditions and the following disclaimer.
12 (2) Redistributions in binary form must reproduce the above copyright
13 notice, this list of conditions and the following disclaimer in
14 the documentation and/or other materials provided with the
15 distribution.
17 (3) The name of the author may not be used to
18 endorse or promote products derived from this software without
19 specific prior written permission.
21 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
25 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
29 STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
30 IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 POSSIBILITY OF SUCH DAMAGE. */
33 #include "config.h"
35 #include <errno.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <sys/types.h>
40 #include "dwarf2.h"
41 #include "filenames.h"
43 #include "backtrace.h"
44 #include "internal.h"
46 #if !defined(HAVE_DECL_STRNLEN) || !HAVE_DECL_STRNLEN
48 /* If strnlen is not declared, provide our own version. */
50 static size_t
51 xstrnlen (const char *s, size_t maxlen)
53 size_t i;
55 for (i = 0; i < maxlen; ++i)
56 if (s[i] == '\0')
57 break;
58 return i;
61 #define strnlen xstrnlen
63 #endif
65 /* A buffer to read DWARF info. */
67 struct dwarf_buf
69 /* Buffer name for error messages. */
70 const char *name;
71 /* Start of the buffer. */
72 const unsigned char *start;
73 /* Next byte to read. */
74 const unsigned char *buf;
75 /* The number of bytes remaining. */
76 size_t left;
77 /* Whether the data is big-endian. */
78 int is_bigendian;
79 /* Error callback routine. */
80 backtrace_error_callback error_callback;
81 /* Data for error_callback. */
82 void *data;
83 /* Non-zero if we've reported an underflow error. */
84 int reported_underflow;
87 /* A single attribute in a DWARF abbreviation. */
89 struct attr
91 /* The attribute name. */
92 enum dwarf_attribute name;
93 /* The attribute form. */
94 enum dwarf_form form;
97 /* A single DWARF abbreviation. */
99 struct abbrev
101 /* The abbrev code--the number used to refer to the abbrev. */
102 uint64_t code;
103 /* The entry tag. */
104 enum dwarf_tag tag;
105 /* Non-zero if this abbrev has child entries. */
106 int has_children;
107 /* The number of attributes. */
108 size_t num_attrs;
109 /* The attributes. */
110 struct attr *attrs;
113 /* The DWARF abbreviations for a compilation unit. This structure
114 only exists while reading the compilation unit. Most DWARF readers
115 seem to a hash table to map abbrev ID's to abbrev entries.
116 However, we primarily care about GCC, and GCC simply issues ID's in
117 numerical order starting at 1. So we simply keep a sorted vector,
118 and try to just look up the code. */
120 struct abbrevs
122 /* The number of abbrevs in the vector. */
123 size_t num_abbrevs;
124 /* The abbrevs, sorted by the code field. */
125 struct abbrev *abbrevs;
128 /* The different kinds of attribute values. */
130 enum attr_val_encoding
132 /* No attribute value. */
133 ATTR_VAL_NONE,
134 /* An address. */
135 ATTR_VAL_ADDRESS,
136 /* A unsigned integer. */
137 ATTR_VAL_UINT,
138 /* A sigd integer. */
139 ATTR_VAL_SINT,
140 /* A string. */
141 ATTR_VAL_STRING,
142 /* An offset to other data in the containing unit. */
143 ATTR_VAL_REF_UNIT,
144 /* An offset to other data within the .dwarf_info section. */
145 ATTR_VAL_REF_INFO,
146 /* An offset to other data within the alt .dwarf_info section. */
147 ATTR_VAL_REF_ALT_INFO,
148 /* An offset to data in some other section. */
149 ATTR_VAL_REF_SECTION,
150 /* A type signature. */
151 ATTR_VAL_REF_TYPE,
152 /* A block of data (not represented). */
153 ATTR_VAL_BLOCK,
154 /* An expression (not represented). */
155 ATTR_VAL_EXPR,
158 /* An attribute value. */
160 struct attr_val
162 /* How the value is stored in the field u. */
163 enum attr_val_encoding encoding;
164 union
166 /* ATTR_VAL_ADDRESS, ATTR_VAL_UINT, ATTR_VAL_REF*. */
167 uint64_t uint;
168 /* ATTR_VAL_SINT. */
169 int64_t sint;
170 /* ATTR_VAL_STRING. */
171 const char *string;
172 /* ATTR_VAL_BLOCK not stored. */
173 } u;
176 /* The line number program header. */
178 struct line_header
180 /* The version of the line number information. */
181 int version;
182 /* The minimum instruction length. */
183 unsigned int min_insn_len;
184 /* The maximum number of ops per instruction. */
185 unsigned int max_ops_per_insn;
186 /* The line base for special opcodes. */
187 int line_base;
188 /* The line range for special opcodes. */
189 unsigned int line_range;
190 /* The opcode base--the first special opcode. */
191 unsigned int opcode_base;
192 /* Opcode lengths, indexed by opcode - 1. */
193 const unsigned char *opcode_lengths;
194 /* The number of directory entries. */
195 size_t dirs_count;
196 /* The directory entries. */
197 const char **dirs;
198 /* The number of filenames. */
199 size_t filenames_count;
200 /* The filenames. */
201 const char **filenames;
204 /* Map a single PC value to a file/line. We will keep a vector of
205 these sorted by PC value. Each file/line will be correct from the
206 PC up to the PC of the next entry if there is one. We allocate one
207 extra entry at the end so that we can use bsearch. */
209 struct line
211 /* PC. */
212 uintptr_t pc;
213 /* File name. Many entries in the array are expected to point to
214 the same file name. */
215 const char *filename;
216 /* Line number. */
217 int lineno;
218 /* Index of the object in the original array read from the DWARF
219 section, before it has been sorted. The index makes it possible
220 to use Quicksort and maintain stability. */
221 int idx;
224 /* A growable vector of line number information. This is used while
225 reading the line numbers. */
227 struct line_vector
229 /* Memory. This is an array of struct line. */
230 struct backtrace_vector vec;
231 /* Number of valid mappings. */
232 size_t count;
235 /* A function described in the debug info. */
237 struct function
239 /* The name of the function. */
240 const char *name;
241 /* If this is an inlined function, the filename of the call
242 site. */
243 const char *caller_filename;
244 /* If this is an inlined function, the line number of the call
245 site. */
246 int caller_lineno;
247 /* Map PC ranges to inlined functions. */
248 struct function_addrs *function_addrs;
249 size_t function_addrs_count;
252 /* An address range for a function. This maps a PC value to a
253 specific function. */
255 struct function_addrs
257 /* Range is LOW <= PC < HIGH. */
258 uint64_t low;
259 uint64_t high;
260 /* Function for this address range. */
261 struct function *function;
264 /* A growable vector of function address ranges. */
266 struct function_vector
268 /* Memory. This is an array of struct function_addrs. */
269 struct backtrace_vector vec;
270 /* Number of address ranges present. */
271 size_t count;
274 /* A DWARF compilation unit. This only holds the information we need
275 to map a PC to a file and line. */
277 struct unit
279 /* The first entry for this compilation unit. */
280 const unsigned char *unit_data;
281 /* The length of the data for this compilation unit. */
282 size_t unit_data_len;
283 /* The offset of UNIT_DATA from the start of the information for
284 this compilation unit. */
285 size_t unit_data_offset;
286 /* Offset of the start of the compilation unit from the start of the
287 .debug_info section. */
288 size_t low_offset;
289 /* Offset of the end of the compilation unit from the start of the
290 .debug_info section. */
291 size_t high_offset;
292 /* DWARF version. */
293 int version;
294 /* Whether unit is DWARF64. */
295 int is_dwarf64;
296 /* Address size. */
297 int addrsize;
298 /* Offset into line number information. */
299 off_t lineoff;
300 /* Primary source file. */
301 const char *filename;
302 /* Compilation command working directory. */
303 const char *comp_dir;
304 /* Absolute file name, only set if needed. */
305 const char *abs_filename;
306 /* The abbreviations for this unit. */
307 struct abbrevs abbrevs;
309 /* The fields above this point are read in during initialization and
310 may be accessed freely. The fields below this point are read in
311 as needed, and therefore require care, as different threads may
312 try to initialize them simultaneously. */
314 /* PC to line number mapping. This is NULL if the values have not
315 been read. This is (struct line *) -1 if there was an error
316 reading the values. */
317 struct line *lines;
318 /* Number of entries in lines. */
319 size_t lines_count;
320 /* PC ranges to function. */
321 struct function_addrs *function_addrs;
322 size_t function_addrs_count;
325 /* An address range for a compilation unit. This maps a PC value to a
326 specific compilation unit. Note that we invert the representation
327 in DWARF: instead of listing the units and attaching a list of
328 ranges, we list the ranges and have each one point to the unit.
329 This lets us do a binary search to find the unit. */
331 struct unit_addrs
333 /* Range is LOW <= PC < HIGH. */
334 uint64_t low;
335 uint64_t high;
336 /* Compilation unit for this address range. */
337 struct unit *u;
340 /* A growable vector of compilation unit address ranges. */
342 struct unit_addrs_vector
344 /* Memory. This is an array of struct unit_addrs. */
345 struct backtrace_vector vec;
346 /* Number of address ranges present. */
347 size_t count;
350 /* A growable vector of compilation unit pointer. */
352 struct unit_vector
354 struct backtrace_vector vec;
355 size_t count;
358 /* The information we need to map a PC to a file and line. */
360 struct dwarf_data
362 /* The data for the next file we know about. */
363 struct dwarf_data *next;
364 /* The data for .gnu_debugaltlink. */
365 struct dwarf_data *altlink;
366 /* The base address for this file. */
367 uintptr_t base_address;
368 /* A sorted list of address ranges. */
369 struct unit_addrs *addrs;
370 /* Number of address ranges in list. */
371 size_t addrs_count;
372 /* A sorted list of units. */
373 struct unit **units;
374 /* Number of units in the list. */
375 size_t units_count;
376 /* The unparsed .debug_info section. */
377 const unsigned char *dwarf_info;
378 size_t dwarf_info_size;
379 /* The unparsed .debug_line section. */
380 const unsigned char *dwarf_line;
381 size_t dwarf_line_size;
382 /* The unparsed .debug_ranges section. */
383 const unsigned char *dwarf_ranges;
384 size_t dwarf_ranges_size;
385 /* The unparsed .debug_str section. */
386 const unsigned char *dwarf_str;
387 size_t dwarf_str_size;
388 /* Whether the data is big-endian or not. */
389 int is_bigendian;
390 /* A vector used for function addresses. We keep this here so that
391 we can grow the vector as we read more functions. */
392 struct function_vector fvec;
395 /* Report an error for a DWARF buffer. */
397 static void
398 dwarf_buf_error (struct dwarf_buf *buf, const char *msg)
400 char b[200];
402 snprintf (b, sizeof b, "%s in %s at %d",
403 msg, buf->name, (int) (buf->buf - buf->start));
404 buf->error_callback (buf->data, b, 0);
407 /* Require at least COUNT bytes in BUF. Return 1 if all is well, 0 on
408 error. */
410 static int
411 require (struct dwarf_buf *buf, size_t count)
413 if (buf->left >= count)
414 return 1;
416 if (!buf->reported_underflow)
418 dwarf_buf_error (buf, "DWARF underflow");
419 buf->reported_underflow = 1;
422 return 0;
425 /* Advance COUNT bytes in BUF. Return 1 if all is well, 0 on
426 error. */
428 static int
429 advance (struct dwarf_buf *buf, size_t count)
431 if (!require (buf, count))
432 return 0;
433 buf->buf += count;
434 buf->left -= count;
435 return 1;
438 /* Read one zero-terminated string from BUF and advance past the string. */
440 static const char *
441 read_string (struct dwarf_buf *buf)
443 const char *p = (const char *)buf->buf;
444 size_t len = strnlen (p, buf->left);
446 /* - If len == left, we ran out of buffer before finding the zero terminator.
447 Generate an error by advancing len + 1.
448 - If len < left, advance by len + 1 to skip past the zero terminator. */
449 size_t count = len + 1;
451 if (!advance (buf, count))
452 return NULL;
454 return p;
457 /* Read one byte from BUF and advance 1 byte. */
459 static unsigned char
460 read_byte (struct dwarf_buf *buf)
462 const unsigned char *p = buf->buf;
464 if (!advance (buf, 1))
465 return 0;
466 return p[0];
469 /* Read a signed char from BUF and advance 1 byte. */
471 static signed char
472 read_sbyte (struct dwarf_buf *buf)
474 const unsigned char *p = buf->buf;
476 if (!advance (buf, 1))
477 return 0;
478 return (*p ^ 0x80) - 0x80;
481 /* Read a uint16 from BUF and advance 2 bytes. */
483 static uint16_t
484 read_uint16 (struct dwarf_buf *buf)
486 const unsigned char *p = buf->buf;
488 if (!advance (buf, 2))
489 return 0;
490 if (buf->is_bigendian)
491 return ((uint16_t) p[0] << 8) | (uint16_t) p[1];
492 else
493 return ((uint16_t) p[1] << 8) | (uint16_t) p[0];
496 /* Read a uint32 from BUF and advance 4 bytes. */
498 static uint32_t
499 read_uint32 (struct dwarf_buf *buf)
501 const unsigned char *p = buf->buf;
503 if (!advance (buf, 4))
504 return 0;
505 if (buf->is_bigendian)
506 return (((uint32_t) p[0] << 24) | ((uint32_t) p[1] << 16)
507 | ((uint32_t) p[2] << 8) | (uint32_t) p[3]);
508 else
509 return (((uint32_t) p[3] << 24) | ((uint32_t) p[2] << 16)
510 | ((uint32_t) p[1] << 8) | (uint32_t) p[0]);
513 /* Read a uint64 from BUF and advance 8 bytes. */
515 static uint64_t
516 read_uint64 (struct dwarf_buf *buf)
518 const unsigned char *p = buf->buf;
520 if (!advance (buf, 8))
521 return 0;
522 if (buf->is_bigendian)
523 return (((uint64_t) p[0] << 56) | ((uint64_t) p[1] << 48)
524 | ((uint64_t) p[2] << 40) | ((uint64_t) p[3] << 32)
525 | ((uint64_t) p[4] << 24) | ((uint64_t) p[5] << 16)
526 | ((uint64_t) p[6] << 8) | (uint64_t) p[7]);
527 else
528 return (((uint64_t) p[7] << 56) | ((uint64_t) p[6] << 48)
529 | ((uint64_t) p[5] << 40) | ((uint64_t) p[4] << 32)
530 | ((uint64_t) p[3] << 24) | ((uint64_t) p[2] << 16)
531 | ((uint64_t) p[1] << 8) | (uint64_t) p[0]);
534 /* Read an offset from BUF and advance the appropriate number of
535 bytes. */
537 static uint64_t
538 read_offset (struct dwarf_buf *buf, int is_dwarf64)
540 if (is_dwarf64)
541 return read_uint64 (buf);
542 else
543 return read_uint32 (buf);
546 /* Read an address from BUF and advance the appropriate number of
547 bytes. */
549 static uint64_t
550 read_address (struct dwarf_buf *buf, int addrsize)
552 switch (addrsize)
554 case 1:
555 return read_byte (buf);
556 case 2:
557 return read_uint16 (buf);
558 case 4:
559 return read_uint32 (buf);
560 case 8:
561 return read_uint64 (buf);
562 default:
563 dwarf_buf_error (buf, "unrecognized address size");
564 return 0;
568 /* Return whether a value is the highest possible address, given the
569 address size. */
571 static int
572 is_highest_address (uint64_t address, int addrsize)
574 switch (addrsize)
576 case 1:
577 return address == (unsigned char) -1;
578 case 2:
579 return address == (uint16_t) -1;
580 case 4:
581 return address == (uint32_t) -1;
582 case 8:
583 return address == (uint64_t) -1;
584 default:
585 return 0;
589 /* Read an unsigned LEB128 number. */
591 static uint64_t
592 read_uleb128 (struct dwarf_buf *buf)
594 uint64_t ret;
595 unsigned int shift;
596 int overflow;
597 unsigned char b;
599 ret = 0;
600 shift = 0;
601 overflow = 0;
604 const unsigned char *p;
606 p = buf->buf;
607 if (!advance (buf, 1))
608 return 0;
609 b = *p;
610 if (shift < 64)
611 ret |= ((uint64_t) (b & 0x7f)) << shift;
612 else if (!overflow)
614 dwarf_buf_error (buf, "LEB128 overflows uint64_t");
615 overflow = 1;
617 shift += 7;
619 while ((b & 0x80) != 0);
621 return ret;
624 /* Read a signed LEB128 number. */
626 static int64_t
627 read_sleb128 (struct dwarf_buf *buf)
629 uint64_t val;
630 unsigned int shift;
631 int overflow;
632 unsigned char b;
634 val = 0;
635 shift = 0;
636 overflow = 0;
639 const unsigned char *p;
641 p = buf->buf;
642 if (!advance (buf, 1))
643 return 0;
644 b = *p;
645 if (shift < 64)
646 val |= ((uint64_t) (b & 0x7f)) << shift;
647 else if (!overflow)
649 dwarf_buf_error (buf, "signed LEB128 overflows uint64_t");
650 overflow = 1;
652 shift += 7;
654 while ((b & 0x80) != 0);
656 if ((b & 0x40) != 0 && shift < 64)
657 val |= ((uint64_t) -1) << shift;
659 return (int64_t) val;
662 /* Return the length of an LEB128 number. */
664 static size_t
665 leb128_len (const unsigned char *p)
667 size_t ret;
669 ret = 1;
670 while ((*p & 0x80) != 0)
672 ++p;
673 ++ret;
675 return ret;
678 /* Read initial_length from BUF and advance the appropriate number of bytes. */
680 static uint64_t
681 read_initial_length (struct dwarf_buf *buf, int *is_dwarf64)
683 uint64_t len;
685 len = read_uint32 (buf);
686 if (len == 0xffffffff)
688 len = read_uint64 (buf);
689 *is_dwarf64 = 1;
691 else
692 *is_dwarf64 = 0;
694 return len;
697 /* Free an abbreviations structure. */
699 static void
700 free_abbrevs (struct backtrace_state *state, struct abbrevs *abbrevs,
701 backtrace_error_callback error_callback, void *data)
703 size_t i;
705 for (i = 0; i < abbrevs->num_abbrevs; ++i)
706 backtrace_free (state, abbrevs->abbrevs[i].attrs,
707 abbrevs->abbrevs[i].num_attrs * sizeof (struct attr),
708 error_callback, data);
709 backtrace_free (state, abbrevs->abbrevs,
710 abbrevs->num_abbrevs * sizeof (struct abbrev),
711 error_callback, data);
712 abbrevs->num_abbrevs = 0;
713 abbrevs->abbrevs = NULL;
716 /* Read an attribute value. Returns 1 on success, 0 on failure. If
717 the value can be represented as a uint64_t, sets *VAL and sets
718 *IS_VALID to 1. We don't try to store the value of other attribute
719 forms, because we don't care about them. */
721 static int
722 read_attribute (enum dwarf_form form, struct dwarf_buf *buf,
723 int is_dwarf64, int version, int addrsize,
724 const unsigned char *dwarf_str, size_t dwarf_str_size,
725 struct dwarf_data *altlink, struct attr_val *val)
727 /* Avoid warnings about val.u.FIELD may be used uninitialized if
728 this function is inlined. The warnings aren't valid but can
729 occur because the different fields are set and used
730 conditionally. */
731 memset (val, 0, sizeof *val);
733 switch (form)
735 case DW_FORM_addr:
736 val->encoding = ATTR_VAL_ADDRESS;
737 val->u.uint = read_address (buf, addrsize);
738 return 1;
739 case DW_FORM_block2:
740 val->encoding = ATTR_VAL_BLOCK;
741 return advance (buf, read_uint16 (buf));
742 case DW_FORM_block4:
743 val->encoding = ATTR_VAL_BLOCK;
744 return advance (buf, read_uint32 (buf));
745 case DW_FORM_data2:
746 val->encoding = ATTR_VAL_UINT;
747 val->u.uint = read_uint16 (buf);
748 return 1;
749 case DW_FORM_data4:
750 val->encoding = ATTR_VAL_UINT;
751 val->u.uint = read_uint32 (buf);
752 return 1;
753 case DW_FORM_data8:
754 val->encoding = ATTR_VAL_UINT;
755 val->u.uint = read_uint64 (buf);
756 return 1;
757 case DW_FORM_string:
758 val->encoding = ATTR_VAL_STRING;
759 val->u.string = read_string (buf);
760 return val->u.string == NULL ? 0 : 1;
761 case DW_FORM_block:
762 val->encoding = ATTR_VAL_BLOCK;
763 return advance (buf, read_uleb128 (buf));
764 case DW_FORM_block1:
765 val->encoding = ATTR_VAL_BLOCK;
766 return advance (buf, read_byte (buf));
767 case DW_FORM_data1:
768 val->encoding = ATTR_VAL_UINT;
769 val->u.uint = read_byte (buf);
770 return 1;
771 case DW_FORM_flag:
772 val->encoding = ATTR_VAL_UINT;
773 val->u.uint = read_byte (buf);
774 return 1;
775 case DW_FORM_sdata:
776 val->encoding = ATTR_VAL_SINT;
777 val->u.sint = read_sleb128 (buf);
778 return 1;
779 case DW_FORM_strp:
781 uint64_t offset;
783 offset = read_offset (buf, is_dwarf64);
784 if (offset >= dwarf_str_size)
786 dwarf_buf_error (buf, "DW_FORM_strp out of range");
787 return 0;
789 val->encoding = ATTR_VAL_STRING;
790 val->u.string = (const char *) dwarf_str + offset;
791 return 1;
793 case DW_FORM_udata:
794 val->encoding = ATTR_VAL_UINT;
795 val->u.uint = read_uleb128 (buf);
796 return 1;
797 case DW_FORM_ref_addr:
798 val->encoding = ATTR_VAL_REF_INFO;
799 if (version == 2)
800 val->u.uint = read_address (buf, addrsize);
801 else
802 val->u.uint = read_offset (buf, is_dwarf64);
803 return 1;
804 case DW_FORM_ref1:
805 val->encoding = ATTR_VAL_REF_UNIT;
806 val->u.uint = read_byte (buf);
807 return 1;
808 case DW_FORM_ref2:
809 val->encoding = ATTR_VAL_REF_UNIT;
810 val->u.uint = read_uint16 (buf);
811 return 1;
812 case DW_FORM_ref4:
813 val->encoding = ATTR_VAL_REF_UNIT;
814 val->u.uint = read_uint32 (buf);
815 return 1;
816 case DW_FORM_ref8:
817 val->encoding = ATTR_VAL_REF_UNIT;
818 val->u.uint = read_uint64 (buf);
819 return 1;
820 case DW_FORM_ref_udata:
821 val->encoding = ATTR_VAL_REF_UNIT;
822 val->u.uint = read_uleb128 (buf);
823 return 1;
824 case DW_FORM_indirect:
826 uint64_t form;
828 form = read_uleb128 (buf);
829 return read_attribute ((enum dwarf_form) form, buf, is_dwarf64,
830 version, addrsize, dwarf_str, dwarf_str_size,
831 altlink, val);
833 case DW_FORM_sec_offset:
834 val->encoding = ATTR_VAL_REF_SECTION;
835 val->u.uint = read_offset (buf, is_dwarf64);
836 return 1;
837 case DW_FORM_exprloc:
838 val->encoding = ATTR_VAL_EXPR;
839 return advance (buf, read_uleb128 (buf));
840 case DW_FORM_flag_present:
841 val->encoding = ATTR_VAL_UINT;
842 val->u.uint = 1;
843 return 1;
844 case DW_FORM_ref_sig8:
845 val->encoding = ATTR_VAL_REF_TYPE;
846 val->u.uint = read_uint64 (buf);
847 return 1;
848 case DW_FORM_GNU_addr_index:
849 val->encoding = ATTR_VAL_REF_SECTION;
850 val->u.uint = read_uleb128 (buf);
851 return 1;
852 case DW_FORM_GNU_str_index:
853 val->encoding = ATTR_VAL_REF_SECTION;
854 val->u.uint = read_uleb128 (buf);
855 return 1;
856 case DW_FORM_GNU_ref_alt:
857 val->u.uint = read_offset (buf, is_dwarf64);
858 if (altlink == NULL)
860 val->encoding = ATTR_VAL_NONE;
861 return 1;
863 val->encoding = ATTR_VAL_REF_ALT_INFO;
864 return 1;
865 case DW_FORM_GNU_strp_alt:
867 uint64_t offset;
868 offset = read_offset (buf, is_dwarf64);
869 if (altlink == NULL)
871 val->encoding = ATTR_VAL_NONE;
872 return 1;
874 if (offset >= altlink->dwarf_str_size)
876 dwarf_buf_error (buf, "DW_FORM_GNU_strp_alt out of range");
877 return 0;
879 val->encoding = ATTR_VAL_STRING;
880 val->u.string = (const char *) altlink->dwarf_str + offset;
881 return 1;
883 default:
884 dwarf_buf_error (buf, "unrecognized DWARF form");
885 return 0;
889 /* Compare a unit offset against a unit for bsearch. */
891 static int
892 units_search (const void *vkey, const void *ventry)
894 const size_t *key = (const size_t *) vkey;
895 const struct unit *entry = *((const struct unit *const *) ventry);
896 size_t offset;
898 offset = *key;
899 if (offset < entry->low_offset)
900 return -1;
901 else if (offset >= entry->high_offset)
902 return 1;
903 else
904 return 0;
907 /* Find a unit in PU containing OFFSET. */
909 static struct unit *
910 find_unit (struct unit **pu, size_t units_count, size_t offset)
912 struct unit **u;
913 u = bsearch (&offset, pu, units_count, sizeof (struct unit *), units_search);
914 return u == NULL ? NULL : *u;
917 /* Compare function_addrs for qsort. When ranges are nested, make the
918 smallest one sort last. */
920 static int
921 function_addrs_compare (const void *v1, const void *v2)
923 const struct function_addrs *a1 = (const struct function_addrs *) v1;
924 const struct function_addrs *a2 = (const struct function_addrs *) v2;
926 if (a1->low < a2->low)
927 return -1;
928 if (a1->low > a2->low)
929 return 1;
930 if (a1->high < a2->high)
931 return 1;
932 if (a1->high > a2->high)
933 return -1;
934 return strcmp (a1->function->name, a2->function->name);
937 /* Compare a PC against a function_addrs for bsearch. Note that if
938 there are multiple ranges containing PC, which one will be returned
939 is unpredictable. We compensate for that in dwarf_fileline. */
941 static int
942 function_addrs_search (const void *vkey, const void *ventry)
944 const uintptr_t *key = (const uintptr_t *) vkey;
945 const struct function_addrs *entry = (const struct function_addrs *) ventry;
946 uintptr_t pc;
948 pc = *key;
949 if (pc < entry->low)
950 return -1;
951 else if (pc >= entry->high)
952 return 1;
953 else
954 return 0;
957 /* Add a new compilation unit address range to a vector. Returns 1 on
958 success, 0 on failure. */
960 static int
961 add_unit_addr (struct backtrace_state *state, uintptr_t base_address,
962 struct unit_addrs addrs,
963 backtrace_error_callback error_callback, void *data,
964 struct unit_addrs_vector *vec)
966 struct unit_addrs *p;
968 /* Add in the base address of the module here, so that we can look
969 up the PC directly. */
970 addrs.low += base_address;
971 addrs.high += base_address;
973 /* Try to merge with the last entry. */
974 if (vec->count > 0)
976 p = (struct unit_addrs *) vec->vec.base + (vec->count - 1);
977 if ((addrs.low == p->high || addrs.low == p->high + 1)
978 && addrs.u == p->u)
980 if (addrs.high > p->high)
981 p->high = addrs.high;
982 return 1;
986 p = ((struct unit_addrs *)
987 backtrace_vector_grow (state, sizeof (struct unit_addrs),
988 error_callback, data, &vec->vec));
989 if (p == NULL)
990 return 0;
992 *p = addrs;
993 ++vec->count;
994 return 1;
997 /* Compare unit_addrs for qsort. When ranges are nested, make the
998 smallest one sort last. */
1000 static int
1001 unit_addrs_compare (const void *v1, const void *v2)
1003 const struct unit_addrs *a1 = (const struct unit_addrs *) v1;
1004 const struct unit_addrs *a2 = (const struct unit_addrs *) v2;
1006 if (a1->low < a2->low)
1007 return -1;
1008 if (a1->low > a2->low)
1009 return 1;
1010 if (a1->high < a2->high)
1011 return 1;
1012 if (a1->high > a2->high)
1013 return -1;
1014 if (a1->u->lineoff < a2->u->lineoff)
1015 return -1;
1016 if (a1->u->lineoff > a2->u->lineoff)
1017 return 1;
1018 return 0;
1021 /* Compare a PC against a unit_addrs for bsearch. Note that if there
1022 are multiple ranges containing PC, which one will be returned is
1023 unpredictable. We compensate for that in dwarf_fileline. */
1025 static int
1026 unit_addrs_search (const void *vkey, const void *ventry)
1028 const uintptr_t *key = (const uintptr_t *) vkey;
1029 const struct unit_addrs *entry = (const struct unit_addrs *) ventry;
1030 uintptr_t pc;
1032 pc = *key;
1033 if (pc < entry->low)
1034 return -1;
1035 else if (pc >= entry->high)
1036 return 1;
1037 else
1038 return 0;
1041 /* Sort the line vector by PC. We want a stable sort here to maintain
1042 the order of lines for the same PC values. Since the sequence is
1043 being sorted in place, their addresses cannot be relied on to
1044 maintain stability. That is the purpose of the index member. */
1046 static int
1047 line_compare (const void *v1, const void *v2)
1049 const struct line *ln1 = (const struct line *) v1;
1050 const struct line *ln2 = (const struct line *) v2;
1052 if (ln1->pc < ln2->pc)
1053 return -1;
1054 else if (ln1->pc > ln2->pc)
1055 return 1;
1056 else if (ln1->idx < ln2->idx)
1057 return -1;
1058 else if (ln1->idx > ln2->idx)
1059 return 1;
1060 else
1061 return 0;
1064 /* Find a PC in a line vector. We always allocate an extra entry at
1065 the end of the lines vector, so that this routine can safely look
1066 at the next entry. Note that when there are multiple mappings for
1067 the same PC value, this will return the last one. */
1069 static int
1070 line_search (const void *vkey, const void *ventry)
1072 const uintptr_t *key = (const uintptr_t *) vkey;
1073 const struct line *entry = (const struct line *) ventry;
1074 uintptr_t pc;
1076 pc = *key;
1077 if (pc < entry->pc)
1078 return -1;
1079 else if (pc >= (entry + 1)->pc)
1080 return 1;
1081 else
1082 return 0;
1085 /* Sort the abbrevs by the abbrev code. This function is passed to
1086 both qsort and bsearch. */
1088 static int
1089 abbrev_compare (const void *v1, const void *v2)
1091 const struct abbrev *a1 = (const struct abbrev *) v1;
1092 const struct abbrev *a2 = (const struct abbrev *) v2;
1094 if (a1->code < a2->code)
1095 return -1;
1096 else if (a1->code > a2->code)
1097 return 1;
1098 else
1100 /* This really shouldn't happen. It means there are two
1101 different abbrevs with the same code, and that means we don't
1102 know which one lookup_abbrev should return. */
1103 return 0;
1107 /* Read the abbreviation table for a compilation unit. Returns 1 on
1108 success, 0 on failure. */
1110 static int
1111 read_abbrevs (struct backtrace_state *state, uint64_t abbrev_offset,
1112 const unsigned char *dwarf_abbrev, size_t dwarf_abbrev_size,
1113 int is_bigendian, backtrace_error_callback error_callback,
1114 void *data, struct abbrevs *abbrevs)
1116 struct dwarf_buf abbrev_buf;
1117 struct dwarf_buf count_buf;
1118 size_t num_abbrevs;
1120 abbrevs->num_abbrevs = 0;
1121 abbrevs->abbrevs = NULL;
1123 if (abbrev_offset >= dwarf_abbrev_size)
1125 error_callback (data, "abbrev offset out of range", 0);
1126 return 0;
1129 abbrev_buf.name = ".debug_abbrev";
1130 abbrev_buf.start = dwarf_abbrev;
1131 abbrev_buf.buf = dwarf_abbrev + abbrev_offset;
1132 abbrev_buf.left = dwarf_abbrev_size - abbrev_offset;
1133 abbrev_buf.is_bigendian = is_bigendian;
1134 abbrev_buf.error_callback = error_callback;
1135 abbrev_buf.data = data;
1136 abbrev_buf.reported_underflow = 0;
1138 /* Count the number of abbrevs in this list. */
1140 count_buf = abbrev_buf;
1141 num_abbrevs = 0;
1142 while (read_uleb128 (&count_buf) != 0)
1144 if (count_buf.reported_underflow)
1145 return 0;
1146 ++num_abbrevs;
1147 // Skip tag.
1148 read_uleb128 (&count_buf);
1149 // Skip has_children.
1150 read_byte (&count_buf);
1151 // Skip attributes.
1152 while (read_uleb128 (&count_buf) != 0)
1153 read_uleb128 (&count_buf);
1154 // Skip form of last attribute.
1155 read_uleb128 (&count_buf);
1158 if (count_buf.reported_underflow)
1159 return 0;
1161 if (num_abbrevs == 0)
1162 return 1;
1164 abbrevs->abbrevs = ((struct abbrev *)
1165 backtrace_alloc (state,
1166 num_abbrevs * sizeof (struct abbrev),
1167 error_callback, data));
1168 if (abbrevs->abbrevs == NULL)
1169 return 0;
1170 abbrevs->num_abbrevs = num_abbrevs;
1171 memset (abbrevs->abbrevs, 0, num_abbrevs * sizeof (struct abbrev));
1173 num_abbrevs = 0;
1174 while (1)
1176 uint64_t code;
1177 struct abbrev a;
1178 size_t num_attrs;
1179 struct attr *attrs;
1181 if (abbrev_buf.reported_underflow)
1182 goto fail;
1184 code = read_uleb128 (&abbrev_buf);
1185 if (code == 0)
1186 break;
1188 a.code = code;
1189 a.tag = (enum dwarf_tag) read_uleb128 (&abbrev_buf);
1190 a.has_children = read_byte (&abbrev_buf);
1192 count_buf = abbrev_buf;
1193 num_attrs = 0;
1194 while (read_uleb128 (&count_buf) != 0)
1196 ++num_attrs;
1197 read_uleb128 (&count_buf);
1200 if (num_attrs == 0)
1202 attrs = NULL;
1203 read_uleb128 (&abbrev_buf);
1204 read_uleb128 (&abbrev_buf);
1206 else
1208 attrs = ((struct attr *)
1209 backtrace_alloc (state, num_attrs * sizeof *attrs,
1210 error_callback, data));
1211 if (attrs == NULL)
1212 goto fail;
1213 num_attrs = 0;
1214 while (1)
1216 uint64_t name;
1217 uint64_t form;
1219 name = read_uleb128 (&abbrev_buf);
1220 form = read_uleb128 (&abbrev_buf);
1221 if (name == 0)
1222 break;
1223 attrs[num_attrs].name = (enum dwarf_attribute) name;
1224 attrs[num_attrs].form = (enum dwarf_form) form;
1225 ++num_attrs;
1229 a.num_attrs = num_attrs;
1230 a.attrs = attrs;
1232 abbrevs->abbrevs[num_abbrevs] = a;
1233 ++num_abbrevs;
1236 backtrace_qsort (abbrevs->abbrevs, abbrevs->num_abbrevs,
1237 sizeof (struct abbrev), abbrev_compare);
1239 return 1;
1241 fail:
1242 free_abbrevs (state, abbrevs, error_callback, data);
1243 return 0;
1246 /* Return the abbrev information for an abbrev code. */
1248 static const struct abbrev *
1249 lookup_abbrev (struct abbrevs *abbrevs, uint64_t code,
1250 backtrace_error_callback error_callback, void *data)
1252 struct abbrev key;
1253 void *p;
1255 /* With GCC, where abbrevs are simply numbered in order, we should
1256 be able to just look up the entry. */
1257 if (code - 1 < abbrevs->num_abbrevs
1258 && abbrevs->abbrevs[code - 1].code == code)
1259 return &abbrevs->abbrevs[code - 1];
1261 /* Otherwise we have to search. */
1262 memset (&key, 0, sizeof key);
1263 key.code = code;
1264 p = bsearch (&key, abbrevs->abbrevs, abbrevs->num_abbrevs,
1265 sizeof (struct abbrev), abbrev_compare);
1266 if (p == NULL)
1268 error_callback (data, "invalid abbreviation code", 0);
1269 return NULL;
1271 return (const struct abbrev *) p;
1274 /* Add non-contiguous address ranges for a compilation unit. Returns
1275 1 on success, 0 on failure. */
1277 static int
1278 add_unit_ranges (struct backtrace_state *state, uintptr_t base_address,
1279 struct unit *u, uint64_t ranges, uint64_t base,
1280 int is_bigendian, const unsigned char *dwarf_ranges,
1281 size_t dwarf_ranges_size,
1282 backtrace_error_callback error_callback, void *data,
1283 struct unit_addrs_vector *addrs)
1285 struct dwarf_buf ranges_buf;
1287 if (ranges >= dwarf_ranges_size)
1289 error_callback (data, "ranges offset out of range", 0);
1290 return 0;
1293 ranges_buf.name = ".debug_ranges";
1294 ranges_buf.start = dwarf_ranges;
1295 ranges_buf.buf = dwarf_ranges + ranges;
1296 ranges_buf.left = dwarf_ranges_size - ranges;
1297 ranges_buf.is_bigendian = is_bigendian;
1298 ranges_buf.error_callback = error_callback;
1299 ranges_buf.data = data;
1300 ranges_buf.reported_underflow = 0;
1302 while (1)
1304 uint64_t low;
1305 uint64_t high;
1307 if (ranges_buf.reported_underflow)
1308 return 0;
1310 low = read_address (&ranges_buf, u->addrsize);
1311 high = read_address (&ranges_buf, u->addrsize);
1313 if (low == 0 && high == 0)
1314 break;
1316 if (is_highest_address (low, u->addrsize))
1317 base = high;
1318 else
1320 struct unit_addrs a;
1322 a.low = low + base;
1323 a.high = high + base;
1324 a.u = u;
1325 if (!add_unit_addr (state, base_address, a, error_callback, data,
1326 addrs))
1327 return 0;
1331 if (ranges_buf.reported_underflow)
1332 return 0;
1334 return 1;
1337 /* Find the address range covered by a compilation unit, reading from
1338 UNIT_BUF and adding values to U. Returns 1 if all data could be
1339 read, 0 if there is some error. */
1341 static int
1342 find_address_ranges (struct backtrace_state *state, uintptr_t base_address,
1343 struct dwarf_buf *unit_buf,
1344 const unsigned char *dwarf_str, size_t dwarf_str_size,
1345 const unsigned char *dwarf_ranges,
1346 size_t dwarf_ranges_size,
1347 int is_bigendian, struct dwarf_data *altlink,
1348 backtrace_error_callback error_callback, void *data,
1349 struct unit *u, struct unit_addrs_vector *addrs,
1350 enum dwarf_tag *unit_tag)
1352 while (unit_buf->left > 0)
1354 uint64_t code;
1355 const struct abbrev *abbrev;
1356 uint64_t lowpc;
1357 int have_lowpc;
1358 uint64_t highpc;
1359 int have_highpc;
1360 int highpc_is_relative;
1361 uint64_t ranges;
1362 int have_ranges;
1363 size_t i;
1365 code = read_uleb128 (unit_buf);
1366 if (code == 0)
1367 return 1;
1369 abbrev = lookup_abbrev (&u->abbrevs, code, error_callback, data);
1370 if (abbrev == NULL)
1371 return 0;
1373 if (unit_tag != NULL)
1374 *unit_tag = abbrev->tag;
1376 lowpc = 0;
1377 have_lowpc = 0;
1378 highpc = 0;
1379 have_highpc = 0;
1380 highpc_is_relative = 0;
1381 ranges = 0;
1382 have_ranges = 0;
1383 for (i = 0; i < abbrev->num_attrs; ++i)
1385 struct attr_val val;
1387 if (!read_attribute (abbrev->attrs[i].form, unit_buf,
1388 u->is_dwarf64, u->version, u->addrsize,
1389 dwarf_str, dwarf_str_size, altlink, &val))
1390 return 0;
1392 switch (abbrev->attrs[i].name)
1394 case DW_AT_low_pc:
1395 if (val.encoding == ATTR_VAL_ADDRESS)
1397 lowpc = val.u.uint;
1398 have_lowpc = 1;
1400 break;
1402 case DW_AT_high_pc:
1403 if (val.encoding == ATTR_VAL_ADDRESS)
1405 highpc = val.u.uint;
1406 have_highpc = 1;
1408 else if (val.encoding == ATTR_VAL_UINT)
1410 highpc = val.u.uint;
1411 have_highpc = 1;
1412 highpc_is_relative = 1;
1414 break;
1416 case DW_AT_ranges:
1417 if (val.encoding == ATTR_VAL_UINT
1418 || val.encoding == ATTR_VAL_REF_SECTION)
1420 ranges = val.u.uint;
1421 have_ranges = 1;
1423 break;
1425 case DW_AT_stmt_list:
1426 if (abbrev->tag == DW_TAG_compile_unit
1427 && (val.encoding == ATTR_VAL_UINT
1428 || val.encoding == ATTR_VAL_REF_SECTION))
1429 u->lineoff = val.u.uint;
1430 break;
1432 case DW_AT_name:
1433 if (abbrev->tag == DW_TAG_compile_unit
1434 && val.encoding == ATTR_VAL_STRING)
1435 u->filename = val.u.string;
1436 break;
1438 case DW_AT_comp_dir:
1439 if (abbrev->tag == DW_TAG_compile_unit
1440 && val.encoding == ATTR_VAL_STRING)
1441 u->comp_dir = val.u.string;
1442 break;
1444 default:
1445 break;
1449 if (abbrev->tag == DW_TAG_compile_unit
1450 || abbrev->tag == DW_TAG_subprogram)
1452 if (have_ranges)
1454 if (!add_unit_ranges (state, base_address, u, ranges, lowpc,
1455 is_bigendian, dwarf_ranges,
1456 dwarf_ranges_size, error_callback,
1457 data, addrs))
1458 return 0;
1460 else if (have_lowpc && have_highpc)
1462 struct unit_addrs a;
1464 if (highpc_is_relative)
1465 highpc += lowpc;
1466 a.low = lowpc;
1467 a.high = highpc;
1468 a.u = u;
1470 if (!add_unit_addr (state, base_address, a, error_callback, data,
1471 addrs))
1472 return 0;
1475 /* If we found the PC range in the DW_TAG_compile_unit, we
1476 can stop now. */
1477 if (abbrev->tag == DW_TAG_compile_unit
1478 && (have_ranges || (have_lowpc && have_highpc)))
1479 return 1;
1482 if (abbrev->has_children)
1484 if (!find_address_ranges (state, base_address, unit_buf,
1485 dwarf_str, dwarf_str_size,
1486 dwarf_ranges, dwarf_ranges_size,
1487 is_bigendian, altlink, error_callback, data,
1488 u, addrs, NULL))
1489 return 0;
1493 return 1;
1496 /* Build a mapping from address ranges to the compilation units where
1497 the line number information for that range can be found. Returns 1
1498 on success, 0 on failure. */
1500 static int
1501 build_address_map (struct backtrace_state *state, uintptr_t base_address,
1502 const unsigned char *dwarf_info, size_t dwarf_info_size,
1503 const unsigned char *dwarf_abbrev, size_t dwarf_abbrev_size,
1504 const unsigned char *dwarf_ranges, size_t dwarf_ranges_size,
1505 const unsigned char *dwarf_str, size_t dwarf_str_size,
1506 int is_bigendian, struct dwarf_data *altlink,
1507 backtrace_error_callback error_callback, void *data,
1508 struct unit_addrs_vector *addrs,
1509 struct unit_vector *unit_vec)
1511 struct dwarf_buf info;
1512 struct backtrace_vector units;
1513 size_t units_count;
1514 size_t i;
1515 struct unit **pu;
1516 size_t unit_offset = 0;
1518 memset (&addrs->vec, 0, sizeof addrs->vec);
1519 memset (&unit_vec->vec, 0, sizeof unit_vec->vec);
1520 addrs->count = 0;
1521 unit_vec->count = 0;
1523 /* Read through the .debug_info section. FIXME: Should we use the
1524 .debug_aranges section? gdb and addr2line don't use it, but I'm
1525 not sure why. */
1527 info.name = ".debug_info";
1528 info.start = dwarf_info;
1529 info.buf = dwarf_info;
1530 info.left = dwarf_info_size;
1531 info.is_bigendian = is_bigendian;
1532 info.error_callback = error_callback;
1533 info.data = data;
1534 info.reported_underflow = 0;
1536 memset (&units, 0, sizeof units);
1537 units_count = 0;
1539 while (info.left > 0)
1541 const unsigned char *unit_data_start;
1542 uint64_t len;
1543 int is_dwarf64;
1544 struct dwarf_buf unit_buf;
1545 int version;
1546 uint64_t abbrev_offset;
1547 int addrsize;
1548 struct unit *u;
1549 enum dwarf_tag unit_tag;
1551 if (info.reported_underflow)
1552 goto fail;
1554 unit_data_start = info.buf;
1556 len = read_initial_length (&info, &is_dwarf64);
1557 unit_buf = info;
1558 unit_buf.left = len;
1560 if (!advance (&info, len))
1561 goto fail;
1563 version = read_uint16 (&unit_buf);
1564 if (version < 2 || version > 4)
1566 dwarf_buf_error (&unit_buf, "unrecognized DWARF version");
1567 goto fail;
1570 pu = ((struct unit **)
1571 backtrace_vector_grow (state, sizeof (struct unit *),
1572 error_callback, data, &units));
1573 if (pu == NULL)
1574 goto fail;
1576 u = ((struct unit *)
1577 backtrace_alloc (state, sizeof *u, error_callback, data));
1578 if (u == NULL)
1579 goto fail;
1581 *pu = u;
1582 ++units_count;
1584 memset (&u->abbrevs, 0, sizeof u->abbrevs);
1585 abbrev_offset = read_offset (&unit_buf, is_dwarf64);
1586 if (!read_abbrevs (state, abbrev_offset, dwarf_abbrev, dwarf_abbrev_size,
1587 is_bigendian, error_callback, data, &u->abbrevs))
1588 goto fail;
1590 addrsize = read_byte (&unit_buf);
1592 u->low_offset = unit_offset;
1593 unit_offset += len + (is_dwarf64 ? 12 : 4);
1594 u->high_offset = unit_offset;
1595 u->unit_data = unit_buf.buf;
1596 u->unit_data_len = unit_buf.left;
1597 u->unit_data_offset = unit_buf.buf - unit_data_start;
1598 u->version = version;
1599 u->is_dwarf64 = is_dwarf64;
1600 u->addrsize = addrsize;
1601 u->filename = NULL;
1602 u->comp_dir = NULL;
1603 u->abs_filename = NULL;
1604 u->lineoff = 0;
1606 /* The actual line number mappings will be read as needed. */
1607 u->lines = NULL;
1608 u->lines_count = 0;
1609 u->function_addrs = NULL;
1610 u->function_addrs_count = 0;
1612 if (!find_address_ranges (state, base_address, &unit_buf,
1613 dwarf_str, dwarf_str_size,
1614 dwarf_ranges, dwarf_ranges_size,
1615 is_bigendian, altlink, error_callback, data,
1616 u, addrs, &unit_tag))
1617 goto fail;
1619 if (unit_buf.reported_underflow)
1620 goto fail;
1622 if (info.reported_underflow)
1623 goto fail;
1625 unit_vec->vec = units;
1626 unit_vec->count = units_count;
1627 return 1;
1629 fail:
1630 if (units_count > 0)
1632 pu = (struct unit **) units.base;
1633 for (i = 0; i < units_count; i++)
1635 free_abbrevs (state, &pu[i]->abbrevs, error_callback, data);
1636 backtrace_free (state, pu[i], sizeof **pu, error_callback, data);
1638 backtrace_vector_free (state, &units, error_callback, data);
1640 if (addrs->count > 0)
1642 backtrace_vector_free (state, &addrs->vec, error_callback, data);
1643 addrs->count = 0;
1645 return 0;
1648 /* Add a new mapping to the vector of line mappings that we are
1649 building. Returns 1 on success, 0 on failure. */
1651 static int
1652 add_line (struct backtrace_state *state, struct dwarf_data *ddata,
1653 uintptr_t pc, const char *filename, int lineno,
1654 backtrace_error_callback error_callback, void *data,
1655 struct line_vector *vec)
1657 struct line *ln;
1659 /* If we are adding the same mapping, ignore it. This can happen
1660 when using discriminators. */
1661 if (vec->count > 0)
1663 ln = (struct line *) vec->vec.base + (vec->count - 1);
1664 if (pc == ln->pc && filename == ln->filename && lineno == ln->lineno)
1665 return 1;
1668 ln = ((struct line *)
1669 backtrace_vector_grow (state, sizeof (struct line), error_callback,
1670 data, &vec->vec));
1671 if (ln == NULL)
1672 return 0;
1674 /* Add in the base address here, so that we can look up the PC
1675 directly. */
1676 ln->pc = pc + ddata->base_address;
1678 ln->filename = filename;
1679 ln->lineno = lineno;
1680 ln->idx = vec->count;
1682 ++vec->count;
1684 return 1;
1687 /* Free the line header information. */
1689 static void
1690 free_line_header (struct backtrace_state *state, struct line_header *hdr,
1691 backtrace_error_callback error_callback, void *data)
1693 if (hdr->dirs_count != 0)
1694 backtrace_free (state, hdr->dirs, hdr->dirs_count * sizeof (const char *),
1695 error_callback, data);
1696 backtrace_free (state, hdr->filenames,
1697 hdr->filenames_count * sizeof (char *),
1698 error_callback, data);
1701 /* Read the line header. Return 1 on success, 0 on failure. */
1703 static int
1704 read_line_header (struct backtrace_state *state, struct unit *u,
1705 int is_dwarf64, struct dwarf_buf *line_buf,
1706 struct line_header *hdr)
1708 uint64_t hdrlen;
1709 struct dwarf_buf hdr_buf;
1710 const unsigned char *p;
1711 const unsigned char *pend;
1712 size_t i;
1714 hdr->version = read_uint16 (line_buf);
1715 if (hdr->version < 2 || hdr->version > 4)
1717 dwarf_buf_error (line_buf, "unsupported line number version");
1718 return 0;
1721 hdrlen = read_offset (line_buf, is_dwarf64);
1723 hdr_buf = *line_buf;
1724 hdr_buf.left = hdrlen;
1726 if (!advance (line_buf, hdrlen))
1727 return 0;
1729 hdr->min_insn_len = read_byte (&hdr_buf);
1730 if (hdr->version < 4)
1731 hdr->max_ops_per_insn = 1;
1732 else
1733 hdr->max_ops_per_insn = read_byte (&hdr_buf);
1735 /* We don't care about default_is_stmt. */
1736 read_byte (&hdr_buf);
1738 hdr->line_base = read_sbyte (&hdr_buf);
1739 hdr->line_range = read_byte (&hdr_buf);
1741 hdr->opcode_base = read_byte (&hdr_buf);
1742 hdr->opcode_lengths = hdr_buf.buf;
1743 if (!advance (&hdr_buf, hdr->opcode_base - 1))
1744 return 0;
1746 /* Count the number of directory entries. */
1747 hdr->dirs_count = 0;
1748 p = hdr_buf.buf;
1749 pend = p + hdr_buf.left;
1750 while (p < pend && *p != '\0')
1752 p += strnlen((const char *) p, pend - p) + 1;
1753 ++hdr->dirs_count;
1756 hdr->dirs = NULL;
1757 if (hdr->dirs_count != 0)
1759 hdr->dirs = ((const char **)
1760 backtrace_alloc (state,
1761 hdr->dirs_count * sizeof (const char *),
1762 line_buf->error_callback, line_buf->data));
1763 if (hdr->dirs == NULL)
1764 return 0;
1767 i = 0;
1768 while (*hdr_buf.buf != '\0')
1770 if (hdr_buf.reported_underflow)
1771 return 0;
1773 hdr->dirs[i] = read_string (&hdr_buf);
1774 if (hdr->dirs[i] == NULL)
1775 return 0;
1776 ++i;
1778 if (!advance (&hdr_buf, 1))
1779 return 0;
1781 /* Count the number of file entries. */
1782 hdr->filenames_count = 0;
1783 p = hdr_buf.buf;
1784 pend = p + hdr_buf.left;
1785 while (p < pend && *p != '\0')
1787 p += strnlen ((const char *) p, pend - p) + 1;
1788 p += leb128_len (p);
1789 p += leb128_len (p);
1790 p += leb128_len (p);
1791 ++hdr->filenames_count;
1794 hdr->filenames = ((const char **)
1795 backtrace_alloc (state,
1796 hdr->filenames_count * sizeof (char *),
1797 line_buf->error_callback,
1798 line_buf->data));
1799 if (hdr->filenames == NULL)
1800 return 0;
1801 i = 0;
1802 while (*hdr_buf.buf != '\0')
1804 const char *filename;
1805 uint64_t dir_index;
1807 if (hdr_buf.reported_underflow)
1808 return 0;
1810 filename = read_string (&hdr_buf);
1811 if (filename == NULL)
1812 return 0;
1813 dir_index = read_uleb128 (&hdr_buf);
1814 if (IS_ABSOLUTE_PATH (filename)
1815 || (dir_index == 0 && u->comp_dir == NULL))
1816 hdr->filenames[i] = filename;
1817 else
1819 const char *dir;
1820 size_t dir_len;
1821 size_t filename_len;
1822 char *s;
1824 if (dir_index == 0)
1825 dir = u->comp_dir;
1826 else if (dir_index - 1 < hdr->dirs_count)
1827 dir = hdr->dirs[dir_index - 1];
1828 else
1830 dwarf_buf_error (line_buf,
1831 ("invalid directory index in "
1832 "line number program header"));
1833 return 0;
1835 dir_len = strlen (dir);
1836 filename_len = strlen (filename);
1837 s = ((char *)
1838 backtrace_alloc (state, dir_len + filename_len + 2,
1839 line_buf->error_callback, line_buf->data));
1840 if (s == NULL)
1841 return 0;
1842 memcpy (s, dir, dir_len);
1843 /* FIXME: If we are on a DOS-based file system, and the
1844 directory or the file name use backslashes, then we
1845 should use a backslash here. */
1846 s[dir_len] = '/';
1847 memcpy (s + dir_len + 1, filename, filename_len + 1);
1848 hdr->filenames[i] = s;
1851 /* Ignore the modification time and size. */
1852 read_uleb128 (&hdr_buf);
1853 read_uleb128 (&hdr_buf);
1855 ++i;
1858 if (hdr_buf.reported_underflow)
1859 return 0;
1861 return 1;
1864 /* Read the line program, adding line mappings to VEC. Return 1 on
1865 success, 0 on failure. */
1867 static int
1868 read_line_program (struct backtrace_state *state, struct dwarf_data *ddata,
1869 struct unit *u, const struct line_header *hdr,
1870 struct dwarf_buf *line_buf, struct line_vector *vec)
1872 uint64_t address;
1873 unsigned int op_index;
1874 const char *reset_filename;
1875 const char *filename;
1876 int lineno;
1878 address = 0;
1879 op_index = 0;
1880 if (hdr->filenames_count > 0)
1881 reset_filename = hdr->filenames[0];
1882 else
1883 reset_filename = "";
1884 filename = reset_filename;
1885 lineno = 1;
1886 while (line_buf->left > 0)
1888 unsigned int op;
1890 op = read_byte (line_buf);
1891 if (op >= hdr->opcode_base)
1893 unsigned int advance;
1895 /* Special opcode. */
1896 op -= hdr->opcode_base;
1897 advance = op / hdr->line_range;
1898 address += (hdr->min_insn_len * (op_index + advance)
1899 / hdr->max_ops_per_insn);
1900 op_index = (op_index + advance) % hdr->max_ops_per_insn;
1901 lineno += hdr->line_base + (int) (op % hdr->line_range);
1902 add_line (state, ddata, address, filename, lineno,
1903 line_buf->error_callback, line_buf->data, vec);
1905 else if (op == DW_LNS_extended_op)
1907 uint64_t len;
1909 len = read_uleb128 (line_buf);
1910 op = read_byte (line_buf);
1911 switch (op)
1913 case DW_LNE_end_sequence:
1914 /* FIXME: Should we mark the high PC here? It seems
1915 that we already have that information from the
1916 compilation unit. */
1917 address = 0;
1918 op_index = 0;
1919 filename = reset_filename;
1920 lineno = 1;
1921 break;
1922 case DW_LNE_set_address:
1923 address = read_address (line_buf, u->addrsize);
1924 break;
1925 case DW_LNE_define_file:
1927 const char *f;
1928 unsigned int dir_index;
1930 f = read_string (line_buf);
1931 if (f == NULL)
1932 return 0;
1933 dir_index = read_uleb128 (line_buf);
1934 /* Ignore that time and length. */
1935 read_uleb128 (line_buf);
1936 read_uleb128 (line_buf);
1937 if (IS_ABSOLUTE_PATH (f))
1938 filename = f;
1939 else
1941 const char *dir;
1942 size_t dir_len;
1943 size_t f_len;
1944 char *p;
1946 if (dir_index == 0)
1947 dir = u->comp_dir;
1948 else if (dir_index - 1 < hdr->dirs_count)
1949 dir = hdr->dirs[dir_index - 1];
1950 else
1952 dwarf_buf_error (line_buf,
1953 ("invalid directory index "
1954 "in line number program"));
1955 return 0;
1957 dir_len = strlen (dir);
1958 f_len = strlen (f);
1959 p = ((char *)
1960 backtrace_alloc (state, dir_len + f_len + 2,
1961 line_buf->error_callback,
1962 line_buf->data));
1963 if (p == NULL)
1964 return 0;
1965 memcpy (p, dir, dir_len);
1966 /* FIXME: If we are on a DOS-based file system,
1967 and the directory or the file name use
1968 backslashes, then we should use a backslash
1969 here. */
1970 p[dir_len] = '/';
1971 memcpy (p + dir_len + 1, f, f_len + 1);
1972 filename = p;
1975 break;
1976 case DW_LNE_set_discriminator:
1977 /* We don't care about discriminators. */
1978 read_uleb128 (line_buf);
1979 break;
1980 default:
1981 if (!advance (line_buf, len - 1))
1982 return 0;
1983 break;
1986 else
1988 switch (op)
1990 case DW_LNS_copy:
1991 add_line (state, ddata, address, filename, lineno,
1992 line_buf->error_callback, line_buf->data, vec);
1993 break;
1994 case DW_LNS_advance_pc:
1996 uint64_t advance;
1998 advance = read_uleb128 (line_buf);
1999 address += (hdr->min_insn_len * (op_index + advance)
2000 / hdr->max_ops_per_insn);
2001 op_index = (op_index + advance) % hdr->max_ops_per_insn;
2003 break;
2004 case DW_LNS_advance_line:
2005 lineno += (int) read_sleb128 (line_buf);
2006 break;
2007 case DW_LNS_set_file:
2009 uint64_t fileno;
2011 fileno = read_uleb128 (line_buf);
2012 if (fileno == 0)
2013 filename = "";
2014 else
2016 if (fileno - 1 >= hdr->filenames_count)
2018 dwarf_buf_error (line_buf,
2019 ("invalid file number in "
2020 "line number program"));
2021 return 0;
2023 filename = hdr->filenames[fileno - 1];
2026 break;
2027 case DW_LNS_set_column:
2028 read_uleb128 (line_buf);
2029 break;
2030 case DW_LNS_negate_stmt:
2031 break;
2032 case DW_LNS_set_basic_block:
2033 break;
2034 case DW_LNS_const_add_pc:
2036 unsigned int advance;
2038 op = 255 - hdr->opcode_base;
2039 advance = op / hdr->line_range;
2040 address += (hdr->min_insn_len * (op_index + advance)
2041 / hdr->max_ops_per_insn);
2042 op_index = (op_index + advance) % hdr->max_ops_per_insn;
2044 break;
2045 case DW_LNS_fixed_advance_pc:
2046 address += read_uint16 (line_buf);
2047 op_index = 0;
2048 break;
2049 case DW_LNS_set_prologue_end:
2050 break;
2051 case DW_LNS_set_epilogue_begin:
2052 break;
2053 case DW_LNS_set_isa:
2054 read_uleb128 (line_buf);
2055 break;
2056 default:
2058 unsigned int i;
2060 for (i = hdr->opcode_lengths[op - 1]; i > 0; --i)
2061 read_uleb128 (line_buf);
2063 break;
2068 return 1;
2071 /* Read the line number information for a compilation unit. Returns 1
2072 on success, 0 on failure. */
2074 static int
2075 read_line_info (struct backtrace_state *state, struct dwarf_data *ddata,
2076 backtrace_error_callback error_callback, void *data,
2077 struct unit *u, struct line_header *hdr, struct line **lines,
2078 size_t *lines_count)
2080 struct line_vector vec;
2081 struct dwarf_buf line_buf;
2082 uint64_t len;
2083 int is_dwarf64;
2084 struct line *ln;
2086 memset (&vec.vec, 0, sizeof vec.vec);
2087 vec.count = 0;
2089 memset (hdr, 0, sizeof *hdr);
2091 if (u->lineoff != (off_t) (size_t) u->lineoff
2092 || (size_t) u->lineoff >= ddata->dwarf_line_size)
2094 error_callback (data, "unit line offset out of range", 0);
2095 goto fail;
2098 line_buf.name = ".debug_line";
2099 line_buf.start = ddata->dwarf_line;
2100 line_buf.buf = ddata->dwarf_line + u->lineoff;
2101 line_buf.left = ddata->dwarf_line_size - u->lineoff;
2102 line_buf.is_bigendian = ddata->is_bigendian;
2103 line_buf.error_callback = error_callback;
2104 line_buf.data = data;
2105 line_buf.reported_underflow = 0;
2107 len = read_initial_length (&line_buf, &is_dwarf64);
2108 line_buf.left = len;
2110 if (!read_line_header (state, u, is_dwarf64, &line_buf, hdr))
2111 goto fail;
2113 if (!read_line_program (state, ddata, u, hdr, &line_buf, &vec))
2114 goto fail;
2116 if (line_buf.reported_underflow)
2117 goto fail;
2119 if (vec.count == 0)
2121 /* This is not a failure in the sense of a generating an error,
2122 but it is a failure in that sense that we have no useful
2123 information. */
2124 goto fail;
2127 /* Allocate one extra entry at the end. */
2128 ln = ((struct line *)
2129 backtrace_vector_grow (state, sizeof (struct line), error_callback,
2130 data, &vec.vec));
2131 if (ln == NULL)
2132 goto fail;
2133 ln->pc = (uintptr_t) -1;
2134 ln->filename = NULL;
2135 ln->lineno = 0;
2136 ln->idx = 0;
2138 if (!backtrace_vector_release (state, &vec.vec, error_callback, data))
2139 goto fail;
2141 ln = (struct line *) vec.vec.base;
2142 backtrace_qsort (ln, vec.count, sizeof (struct line), line_compare);
2144 *lines = ln;
2145 *lines_count = vec.count;
2147 return 1;
2149 fail:
2150 backtrace_vector_free (state, &vec.vec, error_callback, data);
2151 free_line_header (state, hdr, error_callback, data);
2152 *lines = (struct line *) (uintptr_t) -1;
2153 *lines_count = 0;
2154 return 0;
2157 static const char *read_referenced_name (struct dwarf_data *, struct unit *,
2158 uint64_t, backtrace_error_callback,
2159 void *);
2161 /* Read the name of a function from a DIE referenced by ATTR with VAL. */
2163 static const char *
2164 read_referenced_name_from_attr (struct dwarf_data *ddata, struct unit *u,
2165 struct attr *attr, struct attr_val *val,
2166 backtrace_error_callback error_callback,
2167 void *data)
2169 switch (attr->name)
2171 case DW_AT_abstract_origin:
2172 case DW_AT_specification:
2173 break;
2174 default:
2175 return NULL;
2178 if (attr->form == DW_FORM_ref_sig8)
2179 return NULL;
2181 if (val->encoding == ATTR_VAL_REF_INFO)
2183 struct unit *unit
2184 = find_unit (ddata->units, ddata->units_count,
2185 val->u.uint);
2186 if (unit == NULL)
2187 return NULL;
2189 uint64_t offset = val->u.uint - unit->low_offset;
2190 return read_referenced_name (ddata, unit, offset, error_callback, data);
2193 if (val->encoding == ATTR_VAL_UINT
2194 || val->encoding == ATTR_VAL_REF_UNIT)
2195 return read_referenced_name (ddata, u, val->u.uint, error_callback, data);
2197 if (val->encoding == ATTR_VAL_REF_ALT_INFO)
2199 struct unit *alt_unit
2200 = find_unit (ddata->altlink->units, ddata->altlink->units_count,
2201 val->u.uint);
2202 if (alt_unit == NULL)
2203 return NULL;
2205 uint64_t offset = val->u.uint - alt_unit->low_offset;
2206 return read_referenced_name (ddata->altlink, alt_unit, offset,
2207 error_callback, data);
2210 return NULL;
2213 /* Read the name of a function from a DIE referenced by a
2214 DW_AT_abstract_origin or DW_AT_specification tag. OFFSET is within
2215 the same compilation unit. */
2217 static const char *
2218 read_referenced_name (struct dwarf_data *ddata, struct unit *u,
2219 uint64_t offset, backtrace_error_callback error_callback,
2220 void *data)
2222 struct dwarf_buf unit_buf;
2223 uint64_t code;
2224 const struct abbrev *abbrev;
2225 const char *ret;
2226 size_t i;
2228 /* OFFSET is from the start of the data for this compilation unit.
2229 U->unit_data is the data, but it starts U->unit_data_offset bytes
2230 from the beginning. */
2232 if (offset < u->unit_data_offset
2233 || offset - u->unit_data_offset >= u->unit_data_len)
2235 error_callback (data,
2236 "abstract origin or specification out of range",
2238 return NULL;
2241 offset -= u->unit_data_offset;
2243 unit_buf.name = ".debug_info";
2244 unit_buf.start = ddata->dwarf_info;
2245 unit_buf.buf = u->unit_data + offset;
2246 unit_buf.left = u->unit_data_len - offset;
2247 unit_buf.is_bigendian = ddata->is_bigendian;
2248 unit_buf.error_callback = error_callback;
2249 unit_buf.data = data;
2250 unit_buf.reported_underflow = 0;
2252 code = read_uleb128 (&unit_buf);
2253 if (code == 0)
2255 dwarf_buf_error (&unit_buf, "invalid abstract origin or specification");
2256 return NULL;
2259 abbrev = lookup_abbrev (&u->abbrevs, code, error_callback, data);
2260 if (abbrev == NULL)
2261 return NULL;
2263 ret = NULL;
2264 for (i = 0; i < abbrev->num_attrs; ++i)
2266 struct attr_val val;
2268 if (!read_attribute (abbrev->attrs[i].form, &unit_buf,
2269 u->is_dwarf64, u->version, u->addrsize,
2270 ddata->dwarf_str, ddata->dwarf_str_size,
2271 ddata->altlink, &val))
2272 return NULL;
2274 switch (abbrev->attrs[i].name)
2276 case DW_AT_name:
2277 /* Third name preference: don't override. A name we found in some
2278 other way, will normally be more useful -- e.g., this name is
2279 normally not mangled. */
2280 if (ret != NULL)
2281 break;
2282 if (val.encoding == ATTR_VAL_STRING)
2283 ret = val.u.string;
2284 break;
2286 case DW_AT_linkage_name:
2287 case DW_AT_MIPS_linkage_name:
2288 /* First name preference: override all. */
2289 if (val.encoding == ATTR_VAL_STRING)
2290 return val.u.string;
2291 break;
2293 case DW_AT_specification:
2294 /* Second name preference: override DW_AT_name, don't override
2295 DW_AT_linkage_name. */
2297 const char *name;
2299 name = read_referenced_name_from_attr (ddata, u, &abbrev->attrs[i],
2300 &val, error_callback, data);
2301 if (name != NULL)
2302 ret = name;
2304 break;
2306 default:
2307 break;
2311 return ret;
2314 /* Add a single range to U that maps to function. Returns 1 on
2315 success, 0 on error. */
2317 static int
2318 add_function_range (struct backtrace_state *state, struct dwarf_data *ddata,
2319 struct function *function, uint64_t lowpc, uint64_t highpc,
2320 backtrace_error_callback error_callback,
2321 void *data, struct function_vector *vec)
2323 struct function_addrs *p;
2325 /* Add in the base address here, so that we can look up the PC
2326 directly. */
2327 lowpc += ddata->base_address;
2328 highpc += ddata->base_address;
2330 if (vec->count > 0)
2332 p = (struct function_addrs *) vec->vec.base + vec->count - 1;
2333 if ((lowpc == p->high || lowpc == p->high + 1)
2334 && function == p->function)
2336 if (highpc > p->high)
2337 p->high = highpc;
2338 return 1;
2342 p = ((struct function_addrs *)
2343 backtrace_vector_grow (state, sizeof (struct function_addrs),
2344 error_callback, data, &vec->vec));
2345 if (p == NULL)
2346 return 0;
2348 p->low = lowpc;
2349 p->high = highpc;
2350 p->function = function;
2351 ++vec->count;
2352 return 1;
2355 /* Add PC ranges to U that map to FUNCTION. Returns 1 on success, 0
2356 on error. */
2358 static int
2359 add_function_ranges (struct backtrace_state *state, struct dwarf_data *ddata,
2360 struct unit *u, struct function *function,
2361 uint64_t ranges, uint64_t base,
2362 backtrace_error_callback error_callback, void *data,
2363 struct function_vector *vec)
2365 struct dwarf_buf ranges_buf;
2367 if (ranges >= ddata->dwarf_ranges_size)
2369 error_callback (data, "function ranges offset out of range", 0);
2370 return 0;
2373 ranges_buf.name = ".debug_ranges";
2374 ranges_buf.start = ddata->dwarf_ranges;
2375 ranges_buf.buf = ddata->dwarf_ranges + ranges;
2376 ranges_buf.left = ddata->dwarf_ranges_size - ranges;
2377 ranges_buf.is_bigendian = ddata->is_bigendian;
2378 ranges_buf.error_callback = error_callback;
2379 ranges_buf.data = data;
2380 ranges_buf.reported_underflow = 0;
2382 while (1)
2384 uint64_t low;
2385 uint64_t high;
2387 if (ranges_buf.reported_underflow)
2388 return 0;
2390 low = read_address (&ranges_buf, u->addrsize);
2391 high = read_address (&ranges_buf, u->addrsize);
2393 if (low == 0 && high == 0)
2394 break;
2396 if (is_highest_address (low, u->addrsize))
2397 base = high;
2398 else
2400 if (!add_function_range (state, ddata, function, low + base,
2401 high + base, error_callback, data, vec))
2402 return 0;
2406 if (ranges_buf.reported_underflow)
2407 return 0;
2409 return 1;
2412 /* Read one entry plus all its children. Add function addresses to
2413 VEC. Returns 1 on success, 0 on error. */
2415 static int
2416 read_function_entry (struct backtrace_state *state, struct dwarf_data *ddata,
2417 struct unit *u, uint64_t base, struct dwarf_buf *unit_buf,
2418 const struct line_header *lhdr,
2419 backtrace_error_callback error_callback, void *data,
2420 struct function_vector *vec_function,
2421 struct function_vector *vec_inlined)
2423 while (unit_buf->left > 0)
2425 uint64_t code;
2426 const struct abbrev *abbrev;
2427 int is_function;
2428 struct function *function;
2429 struct function_vector *vec;
2430 size_t i;
2431 uint64_t lowpc;
2432 int have_lowpc;
2433 uint64_t highpc;
2434 int have_highpc;
2435 int highpc_is_relative;
2436 uint64_t ranges;
2437 int have_ranges;
2438 int have_linkage_name;
2440 code = read_uleb128 (unit_buf);
2441 if (code == 0)
2442 return 1;
2444 abbrev = lookup_abbrev (&u->abbrevs, code, error_callback, data);
2445 if (abbrev == NULL)
2446 return 0;
2448 is_function = (abbrev->tag == DW_TAG_subprogram
2449 || abbrev->tag == DW_TAG_entry_point
2450 || abbrev->tag == DW_TAG_inlined_subroutine);
2452 if (abbrev->tag == DW_TAG_inlined_subroutine)
2453 vec = vec_inlined;
2454 else
2455 vec = vec_function;
2457 function = NULL;
2458 if (is_function)
2460 function = ((struct function *)
2461 backtrace_alloc (state, sizeof *function,
2462 error_callback, data));
2463 if (function == NULL)
2464 return 0;
2465 memset (function, 0, sizeof *function);
2468 lowpc = 0;
2469 have_lowpc = 0;
2470 highpc = 0;
2471 have_highpc = 0;
2472 highpc_is_relative = 0;
2473 ranges = 0;
2474 have_ranges = 0;
2475 have_linkage_name = 0;
2476 for (i = 0; i < abbrev->num_attrs; ++i)
2478 struct attr_val val;
2480 if (!read_attribute (abbrev->attrs[i].form, unit_buf,
2481 u->is_dwarf64, u->version, u->addrsize,
2482 ddata->dwarf_str, ddata->dwarf_str_size,
2483 ddata->altlink, &val))
2484 return 0;
2486 /* The compile unit sets the base address for any address
2487 ranges in the function entries. */
2488 if (abbrev->tag == DW_TAG_compile_unit
2489 && abbrev->attrs[i].name == DW_AT_low_pc
2490 && val.encoding == ATTR_VAL_ADDRESS)
2491 base = val.u.uint;
2493 if (is_function)
2495 switch (abbrev->attrs[i].name)
2497 case DW_AT_call_file:
2498 if (val.encoding == ATTR_VAL_UINT)
2500 if (val.u.uint == 0)
2501 function->caller_filename = "";
2502 else
2504 if (val.u.uint - 1 >= lhdr->filenames_count)
2506 dwarf_buf_error (unit_buf,
2507 ("invalid file number in "
2508 "DW_AT_call_file attribute"));
2509 return 0;
2511 function->caller_filename =
2512 lhdr->filenames[val.u.uint - 1];
2515 break;
2517 case DW_AT_call_line:
2518 if (val.encoding == ATTR_VAL_UINT)
2519 function->caller_lineno = val.u.uint;
2520 break;
2522 case DW_AT_abstract_origin:
2523 case DW_AT_specification:
2524 /* Second name preference: override DW_AT_name, don't override
2525 DW_AT_linkage_name. */
2526 if (have_linkage_name)
2527 break;
2529 const char *name;
2531 name
2532 = read_referenced_name_from_attr (ddata, u,
2533 &abbrev->attrs[i], &val,
2534 error_callback, data);
2535 if (name != NULL)
2536 function->name = name;
2538 break;
2540 case DW_AT_name:
2541 /* Third name preference: don't override. */
2542 if (function->name != NULL)
2543 break;
2544 if (val.encoding == ATTR_VAL_STRING)
2545 function->name = val.u.string;
2546 break;
2548 case DW_AT_linkage_name:
2549 case DW_AT_MIPS_linkage_name:
2550 /* First name preference: override all. */
2551 if (val.encoding == ATTR_VAL_STRING)
2553 function->name = val.u.string;
2554 have_linkage_name = 1;
2556 break;
2558 case DW_AT_low_pc:
2559 if (val.encoding == ATTR_VAL_ADDRESS)
2561 lowpc = val.u.uint;
2562 have_lowpc = 1;
2564 break;
2566 case DW_AT_high_pc:
2567 if (val.encoding == ATTR_VAL_ADDRESS)
2569 highpc = val.u.uint;
2570 have_highpc = 1;
2572 else if (val.encoding == ATTR_VAL_UINT)
2574 highpc = val.u.uint;
2575 have_highpc = 1;
2576 highpc_is_relative = 1;
2578 break;
2580 case DW_AT_ranges:
2581 if (val.encoding == ATTR_VAL_UINT
2582 || val.encoding == ATTR_VAL_REF_SECTION)
2584 ranges = val.u.uint;
2585 have_ranges = 1;
2587 break;
2589 default:
2590 break;
2595 /* If we couldn't find a name for the function, we have no use
2596 for it. */
2597 if (is_function && function->name == NULL)
2599 backtrace_free (state, function, sizeof *function,
2600 error_callback, data);
2601 is_function = 0;
2604 if (is_function)
2606 if (have_ranges)
2608 if (!add_function_ranges (state, ddata, u, function, ranges,
2609 base, error_callback, data, vec))
2610 return 0;
2612 else if (have_lowpc && have_highpc)
2614 if (highpc_is_relative)
2615 highpc += lowpc;
2616 if (!add_function_range (state, ddata, function, lowpc, highpc,
2617 error_callback, data, vec))
2618 return 0;
2620 else
2622 backtrace_free (state, function, sizeof *function,
2623 error_callback, data);
2624 is_function = 0;
2628 if (abbrev->has_children)
2630 if (!is_function)
2632 if (!read_function_entry (state, ddata, u, base, unit_buf, lhdr,
2633 error_callback, data, vec_function,
2634 vec_inlined))
2635 return 0;
2637 else
2639 struct function_vector fvec;
2641 /* Gather any information for inlined functions in
2642 FVEC. */
2644 memset (&fvec, 0, sizeof fvec);
2646 if (!read_function_entry (state, ddata, u, base, unit_buf, lhdr,
2647 error_callback, data, vec_function,
2648 &fvec))
2649 return 0;
2651 if (fvec.count > 0)
2653 struct function_addrs *faddrs;
2655 if (!backtrace_vector_release (state, &fvec.vec,
2656 error_callback, data))
2657 return 0;
2659 faddrs = (struct function_addrs *) fvec.vec.base;
2660 backtrace_qsort (faddrs, fvec.count,
2661 sizeof (struct function_addrs),
2662 function_addrs_compare);
2664 function->function_addrs = faddrs;
2665 function->function_addrs_count = fvec.count;
2671 return 1;
2674 /* Read function name information for a compilation unit. We look
2675 through the whole unit looking for function tags. */
2677 static void
2678 read_function_info (struct backtrace_state *state, struct dwarf_data *ddata,
2679 const struct line_header *lhdr,
2680 backtrace_error_callback error_callback, void *data,
2681 struct unit *u, struct function_vector *fvec,
2682 struct function_addrs **ret_addrs,
2683 size_t *ret_addrs_count)
2685 struct function_vector lvec;
2686 struct function_vector *pfvec;
2687 struct dwarf_buf unit_buf;
2688 struct function_addrs *addrs;
2689 size_t addrs_count;
2691 /* Use FVEC if it is not NULL. Otherwise use our own vector. */
2692 if (fvec != NULL)
2693 pfvec = fvec;
2694 else
2696 memset (&lvec, 0, sizeof lvec);
2697 pfvec = &lvec;
2700 unit_buf.name = ".debug_info";
2701 unit_buf.start = ddata->dwarf_info;
2702 unit_buf.buf = u->unit_data;
2703 unit_buf.left = u->unit_data_len;
2704 unit_buf.is_bigendian = ddata->is_bigendian;
2705 unit_buf.error_callback = error_callback;
2706 unit_buf.data = data;
2707 unit_buf.reported_underflow = 0;
2709 while (unit_buf.left > 0)
2711 if (!read_function_entry (state, ddata, u, 0, &unit_buf, lhdr,
2712 error_callback, data, pfvec, pfvec))
2713 return;
2716 if (pfvec->count == 0)
2717 return;
2719 addrs_count = pfvec->count;
2721 if (fvec == NULL)
2723 if (!backtrace_vector_release (state, &lvec.vec, error_callback, data))
2724 return;
2725 addrs = (struct function_addrs *) pfvec->vec.base;
2727 else
2729 /* Finish this list of addresses, but leave the remaining space in
2730 the vector available for the next function unit. */
2731 addrs = ((struct function_addrs *)
2732 backtrace_vector_finish (state, &fvec->vec,
2733 error_callback, data));
2734 if (addrs == NULL)
2735 return;
2736 fvec->count = 0;
2739 backtrace_qsort (addrs, addrs_count, sizeof (struct function_addrs),
2740 function_addrs_compare);
2742 *ret_addrs = addrs;
2743 *ret_addrs_count = addrs_count;
2746 /* See if PC is inlined in FUNCTION. If it is, print out the inlined
2747 information, and update FILENAME and LINENO for the caller.
2748 Returns whatever CALLBACK returns, or 0 to keep going. */
2750 static int
2751 report_inlined_functions (uintptr_t pc, struct function *function,
2752 backtrace_full_callback callback, void *data,
2753 const char **filename, int *lineno)
2755 struct function_addrs *function_addrs;
2756 struct function *inlined;
2757 int ret;
2759 if (function->function_addrs_count == 0)
2760 return 0;
2762 function_addrs = ((struct function_addrs *)
2763 bsearch (&pc, function->function_addrs,
2764 function->function_addrs_count,
2765 sizeof (struct function_addrs),
2766 function_addrs_search));
2767 if (function_addrs == NULL)
2768 return 0;
2770 while (((size_t) (function_addrs - function->function_addrs) + 1
2771 < function->function_addrs_count)
2772 && pc >= (function_addrs + 1)->low
2773 && pc < (function_addrs + 1)->high)
2774 ++function_addrs;
2776 /* We found an inlined call. */
2778 inlined = function_addrs->function;
2780 /* Report any calls inlined into this one. */
2781 ret = report_inlined_functions (pc, inlined, callback, data,
2782 filename, lineno);
2783 if (ret != 0)
2784 return ret;
2786 /* Report this inlined call. */
2787 ret = callback (data, pc, *filename, *lineno, inlined->name);
2788 if (ret != 0)
2789 return ret;
2791 /* Our caller will report the caller of the inlined function; tell
2792 it the appropriate filename and line number. */
2793 *filename = inlined->caller_filename;
2794 *lineno = inlined->caller_lineno;
2796 return 0;
2799 /* Look for a PC in the DWARF mapping for one module. On success,
2800 call CALLBACK and return whatever it returns. On error, call
2801 ERROR_CALLBACK and return 0. Sets *FOUND to 1 if the PC is found,
2802 0 if not. */
2804 static int
2805 dwarf_lookup_pc (struct backtrace_state *state, struct dwarf_data *ddata,
2806 uintptr_t pc, backtrace_full_callback callback,
2807 backtrace_error_callback error_callback, void *data,
2808 int *found)
2810 struct unit_addrs *entry;
2811 struct unit *u;
2812 int new_data;
2813 struct line *lines;
2814 struct line *ln;
2815 struct function_addrs *function_addrs;
2816 struct function *function;
2817 const char *filename;
2818 int lineno;
2819 int ret;
2821 *found = 1;
2823 /* Find an address range that includes PC. */
2824 entry = (ddata->addrs_count == 0
2825 ? NULL
2826 : bsearch (&pc, ddata->addrs, ddata->addrs_count,
2827 sizeof (struct unit_addrs), unit_addrs_search));
2829 if (entry == NULL)
2831 *found = 0;
2832 return 0;
2835 /* If there are multiple ranges that contain PC, use the last one,
2836 in order to produce predictable results. If we assume that all
2837 ranges are properly nested, then the last range will be the
2838 smallest one. */
2839 while ((size_t) (entry - ddata->addrs) + 1 < ddata->addrs_count
2840 && pc >= (entry + 1)->low
2841 && pc < (entry + 1)->high)
2842 ++entry;
2844 /* We need the lines, lines_count, function_addrs,
2845 function_addrs_count fields of u. If they are not set, we need
2846 to set them. When running in threaded mode, we need to allow for
2847 the possibility that some other thread is setting them
2848 simultaneously. */
2850 u = entry->u;
2851 lines = u->lines;
2853 /* Skip units with no useful line number information by walking
2854 backward. Useless line number information is marked by setting
2855 lines == -1. */
2856 while (entry > ddata->addrs
2857 && pc >= (entry - 1)->low
2858 && pc < (entry - 1)->high)
2860 if (state->threaded)
2861 lines = (struct line *) backtrace_atomic_load_pointer (&u->lines);
2863 if (lines != (struct line *) (uintptr_t) -1)
2864 break;
2866 --entry;
2868 u = entry->u;
2869 lines = u->lines;
2872 if (state->threaded)
2873 lines = backtrace_atomic_load_pointer (&u->lines);
2875 new_data = 0;
2876 if (lines == NULL)
2878 size_t function_addrs_count;
2879 struct line_header lhdr;
2880 size_t count;
2882 /* We have never read the line information for this unit. Read
2883 it now. */
2885 function_addrs = NULL;
2886 function_addrs_count = 0;
2887 if (read_line_info (state, ddata, error_callback, data, entry->u, &lhdr,
2888 &lines, &count))
2890 struct function_vector *pfvec;
2892 /* If not threaded, reuse DDATA->FVEC for better memory
2893 consumption. */
2894 if (state->threaded)
2895 pfvec = NULL;
2896 else
2897 pfvec = &ddata->fvec;
2898 read_function_info (state, ddata, &lhdr, error_callback, data,
2899 entry->u, pfvec, &function_addrs,
2900 &function_addrs_count);
2901 free_line_header (state, &lhdr, error_callback, data);
2902 new_data = 1;
2905 /* Atomically store the information we just read into the unit.
2906 If another thread is simultaneously writing, it presumably
2907 read the same information, and we don't care which one we
2908 wind up with; we just leak the other one. We do have to
2909 write the lines field last, so that the acquire-loads above
2910 ensure that the other fields are set. */
2912 if (!state->threaded)
2914 u->lines_count = count;
2915 u->function_addrs = function_addrs;
2916 u->function_addrs_count = function_addrs_count;
2917 u->lines = lines;
2919 else
2921 backtrace_atomic_store_size_t (&u->lines_count, count);
2922 backtrace_atomic_store_pointer (&u->function_addrs, function_addrs);
2923 backtrace_atomic_store_size_t (&u->function_addrs_count,
2924 function_addrs_count);
2925 backtrace_atomic_store_pointer (&u->lines, lines);
2929 /* Now all fields of U have been initialized. */
2931 if (lines == (struct line *) (uintptr_t) -1)
2933 /* If reading the line number information failed in some way,
2934 try again to see if there is a better compilation unit for
2935 this PC. */
2936 if (new_data)
2937 return dwarf_lookup_pc (state, ddata, pc, callback, error_callback,
2938 data, found);
2939 return callback (data, pc, NULL, 0, NULL);
2942 /* Search for PC within this unit. */
2944 ln = (struct line *) bsearch (&pc, lines, entry->u->lines_count,
2945 sizeof (struct line), line_search);
2946 if (ln == NULL)
2948 /* The PC is between the low_pc and high_pc attributes of the
2949 compilation unit, but no entry in the line table covers it.
2950 This implies that the start of the compilation unit has no
2951 line number information. */
2953 if (entry->u->abs_filename == NULL)
2955 const char *filename;
2957 filename = entry->u->filename;
2958 if (filename != NULL
2959 && !IS_ABSOLUTE_PATH (filename)
2960 && entry->u->comp_dir != NULL)
2962 size_t filename_len;
2963 const char *dir;
2964 size_t dir_len;
2965 char *s;
2967 filename_len = strlen (filename);
2968 dir = entry->u->comp_dir;
2969 dir_len = strlen (dir);
2970 s = (char *) backtrace_alloc (state, dir_len + filename_len + 2,
2971 error_callback, data);
2972 if (s == NULL)
2974 *found = 0;
2975 return 0;
2977 memcpy (s, dir, dir_len);
2978 /* FIXME: Should use backslash if DOS file system. */
2979 s[dir_len] = '/';
2980 memcpy (s + dir_len + 1, filename, filename_len + 1);
2981 filename = s;
2983 entry->u->abs_filename = filename;
2986 return callback (data, pc, entry->u->abs_filename, 0, NULL);
2989 /* Search for function name within this unit. */
2991 if (entry->u->function_addrs_count == 0)
2992 return callback (data, pc, ln->filename, ln->lineno, NULL);
2994 function_addrs = ((struct function_addrs *)
2995 bsearch (&pc, entry->u->function_addrs,
2996 entry->u->function_addrs_count,
2997 sizeof (struct function_addrs),
2998 function_addrs_search));
2999 if (function_addrs == NULL)
3000 return callback (data, pc, ln->filename, ln->lineno, NULL);
3002 /* If there are multiple function ranges that contain PC, use the
3003 last one, in order to produce predictable results. */
3005 while (((size_t) (function_addrs - entry->u->function_addrs + 1)
3006 < entry->u->function_addrs_count)
3007 && pc >= (function_addrs + 1)->low
3008 && pc < (function_addrs + 1)->high)
3009 ++function_addrs;
3011 function = function_addrs->function;
3013 filename = ln->filename;
3014 lineno = ln->lineno;
3016 ret = report_inlined_functions (pc, function, callback, data,
3017 &filename, &lineno);
3018 if (ret != 0)
3019 return ret;
3021 return callback (data, pc, filename, lineno, function->name);
3025 /* Return the file/line information for a PC using the DWARF mapping
3026 we built earlier. */
3028 static int
3029 dwarf_fileline (struct backtrace_state *state, uintptr_t pc,
3030 backtrace_full_callback callback,
3031 backtrace_error_callback error_callback, void *data)
3033 struct dwarf_data *ddata;
3034 int found;
3035 int ret;
3037 if (!state->threaded)
3039 for (ddata = (struct dwarf_data *) state->fileline_data;
3040 ddata != NULL;
3041 ddata = ddata->next)
3043 ret = dwarf_lookup_pc (state, ddata, pc, callback, error_callback,
3044 data, &found);
3045 if (ret != 0 || found)
3046 return ret;
3049 else
3051 struct dwarf_data **pp;
3053 pp = (struct dwarf_data **) (void *) &state->fileline_data;
3054 while (1)
3056 ddata = backtrace_atomic_load_pointer (pp);
3057 if (ddata == NULL)
3058 break;
3060 ret = dwarf_lookup_pc (state, ddata, pc, callback, error_callback,
3061 data, &found);
3062 if (ret != 0 || found)
3063 return ret;
3065 pp = &ddata->next;
3069 /* FIXME: See if any libraries have been dlopen'ed. */
3071 return callback (data, pc, NULL, 0, NULL);
3074 /* Initialize our data structures from the DWARF debug info for a
3075 file. Return NULL on failure. */
3077 static struct dwarf_data *
3078 build_dwarf_data (struct backtrace_state *state,
3079 uintptr_t base_address,
3080 const unsigned char *dwarf_info,
3081 size_t dwarf_info_size,
3082 const unsigned char *dwarf_line,
3083 size_t dwarf_line_size,
3084 const unsigned char *dwarf_abbrev,
3085 size_t dwarf_abbrev_size,
3086 const unsigned char *dwarf_ranges,
3087 size_t dwarf_ranges_size,
3088 const unsigned char *dwarf_str,
3089 size_t dwarf_str_size,
3090 int is_bigendian,
3091 struct dwarf_data *altlink,
3092 backtrace_error_callback error_callback,
3093 void *data)
3095 struct unit_addrs_vector addrs_vec;
3096 struct unit_addrs *addrs;
3097 size_t addrs_count;
3098 struct unit_vector units_vec;
3099 struct unit **units;
3100 size_t units_count;
3101 struct dwarf_data *fdata;
3103 if (!build_address_map (state, base_address, dwarf_info, dwarf_info_size,
3104 dwarf_abbrev, dwarf_abbrev_size, dwarf_ranges,
3105 dwarf_ranges_size, dwarf_str, dwarf_str_size,
3106 is_bigendian, altlink, error_callback, data,
3107 &addrs_vec, &units_vec))
3108 return NULL;
3110 if (!backtrace_vector_release (state, &addrs_vec.vec, error_callback, data))
3111 return NULL;
3112 if (!backtrace_vector_release (state, &units_vec.vec, error_callback, data))
3113 return NULL;
3114 addrs = (struct unit_addrs *) addrs_vec.vec.base;
3115 units = (struct unit **) units_vec.vec.base;
3116 addrs_count = addrs_vec.count;
3117 units_count = units_vec.count;
3118 backtrace_qsort (addrs, addrs_count, sizeof (struct unit_addrs),
3119 unit_addrs_compare);
3120 /* No qsort for units required, already sorted. */
3122 fdata = ((struct dwarf_data *)
3123 backtrace_alloc (state, sizeof (struct dwarf_data),
3124 error_callback, data));
3125 if (fdata == NULL)
3126 return NULL;
3128 fdata->next = NULL;
3129 fdata->altlink = altlink;
3130 fdata->base_address = base_address;
3131 fdata->addrs = addrs;
3132 fdata->addrs_count = addrs_count;
3133 fdata->units = units;
3134 fdata->units_count = units_count;
3135 fdata->dwarf_info = dwarf_info;
3136 fdata->dwarf_info_size = dwarf_info_size;
3137 fdata->dwarf_line = dwarf_line;
3138 fdata->dwarf_line_size = dwarf_line_size;
3139 fdata->dwarf_ranges = dwarf_ranges;
3140 fdata->dwarf_ranges_size = dwarf_ranges_size;
3141 fdata->dwarf_str = dwarf_str;
3142 fdata->dwarf_str_size = dwarf_str_size;
3143 fdata->is_bigendian = is_bigendian;
3144 memset (&fdata->fvec, 0, sizeof fdata->fvec);
3146 return fdata;
3149 /* Build our data structures from the DWARF sections for a module.
3150 Set FILELINE_FN and STATE->FILELINE_DATA. Return 1 on success, 0
3151 on failure. */
3154 backtrace_dwarf_add (struct backtrace_state *state,
3155 uintptr_t base_address,
3156 const unsigned char *dwarf_info,
3157 size_t dwarf_info_size,
3158 const unsigned char *dwarf_line,
3159 size_t dwarf_line_size,
3160 const unsigned char *dwarf_abbrev,
3161 size_t dwarf_abbrev_size,
3162 const unsigned char *dwarf_ranges,
3163 size_t dwarf_ranges_size,
3164 const unsigned char *dwarf_str,
3165 size_t dwarf_str_size,
3166 int is_bigendian,
3167 struct dwarf_data *fileline_altlink,
3168 backtrace_error_callback error_callback,
3169 void *data, fileline *fileline_fn,
3170 struct dwarf_data **fileline_entry)
3172 struct dwarf_data *fdata;
3174 fdata = build_dwarf_data (state, base_address, dwarf_info, dwarf_info_size,
3175 dwarf_line, dwarf_line_size, dwarf_abbrev,
3176 dwarf_abbrev_size, dwarf_ranges, dwarf_ranges_size,
3177 dwarf_str, dwarf_str_size, is_bigendian,
3178 fileline_altlink, error_callback, data);
3179 if (fdata == NULL)
3180 return 0;
3182 if (fileline_entry != NULL)
3183 *fileline_entry = fdata;
3185 if (!state->threaded)
3187 struct dwarf_data **pp;
3189 for (pp = (struct dwarf_data **) (void *) &state->fileline_data;
3190 *pp != NULL;
3191 pp = &(*pp)->next)
3193 *pp = fdata;
3195 else
3197 while (1)
3199 struct dwarf_data **pp;
3201 pp = (struct dwarf_data **) (void *) &state->fileline_data;
3203 while (1)
3205 struct dwarf_data *p;
3207 p = backtrace_atomic_load_pointer (pp);
3209 if (p == NULL)
3210 break;
3212 pp = &p->next;
3215 if (__sync_bool_compare_and_swap (pp, NULL, fdata))
3216 break;
3220 *fileline_fn = dwarf_fileline;
3222 return 1;