Fix missing SCNuMAX defines in inttypes.h on hpux11.[01]*
[official-gcc.git] / libbacktrace / dwarf.c
blob006c818162297b964d7e92a0687b4db160d20dee
1 /* dwarf.c -- Get file/line information from DWARF for backtraces.
2 Copyright (C) 2012-2020 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;
95 /* The attribute value, for DW_FORM_implicit_const. */
96 int64_t val;
99 /* A single DWARF abbreviation. */
101 struct abbrev
103 /* The abbrev code--the number used to refer to the abbrev. */
104 uint64_t code;
105 /* The entry tag. */
106 enum dwarf_tag tag;
107 /* Non-zero if this abbrev has child entries. */
108 int has_children;
109 /* The number of attributes. */
110 size_t num_attrs;
111 /* The attributes. */
112 struct attr *attrs;
115 /* The DWARF abbreviations for a compilation unit. This structure
116 only exists while reading the compilation unit. Most DWARF readers
117 seem to a hash table to map abbrev ID's to abbrev entries.
118 However, we primarily care about GCC, and GCC simply issues ID's in
119 numerical order starting at 1. So we simply keep a sorted vector,
120 and try to just look up the code. */
122 struct abbrevs
124 /* The number of abbrevs in the vector. */
125 size_t num_abbrevs;
126 /* The abbrevs, sorted by the code field. */
127 struct abbrev *abbrevs;
130 /* The different kinds of attribute values. */
132 enum attr_val_encoding
134 /* No attribute value. */
135 ATTR_VAL_NONE,
136 /* An address. */
137 ATTR_VAL_ADDRESS,
138 /* An index into the .debug_addr section, whose value is relative to
139 * the DW_AT_addr_base attribute of the compilation unit. */
140 ATTR_VAL_ADDRESS_INDEX,
141 /* A unsigned integer. */
142 ATTR_VAL_UINT,
143 /* A sigd integer. */
144 ATTR_VAL_SINT,
145 /* A string. */
146 ATTR_VAL_STRING,
147 /* An index into the .debug_str_offsets section. */
148 ATTR_VAL_STRING_INDEX,
149 /* An offset to other data in the containing unit. */
150 ATTR_VAL_REF_UNIT,
151 /* An offset to other data within the .debug_info section. */
152 ATTR_VAL_REF_INFO,
153 /* An offset to other data within the alt .debug_info section. */
154 ATTR_VAL_REF_ALT_INFO,
155 /* An offset to data in some other section. */
156 ATTR_VAL_REF_SECTION,
157 /* A type signature. */
158 ATTR_VAL_REF_TYPE,
159 /* An index into the .debug_rnglists section. */
160 ATTR_VAL_RNGLISTS_INDEX,
161 /* A block of data (not represented). */
162 ATTR_VAL_BLOCK,
163 /* An expression (not represented). */
164 ATTR_VAL_EXPR,
167 /* An attribute value. */
169 struct attr_val
171 /* How the value is stored in the field u. */
172 enum attr_val_encoding encoding;
173 union
175 /* ATTR_VAL_ADDRESS*, ATTR_VAL_UINT, ATTR_VAL_REF*. */
176 uint64_t uint;
177 /* ATTR_VAL_SINT. */
178 int64_t sint;
179 /* ATTR_VAL_STRING. */
180 const char *string;
181 /* ATTR_VAL_BLOCK not stored. */
182 } u;
185 /* The line number program header. */
187 struct line_header
189 /* The version of the line number information. */
190 int version;
191 /* Address size. */
192 int addrsize;
193 /* The minimum instruction length. */
194 unsigned int min_insn_len;
195 /* The maximum number of ops per instruction. */
196 unsigned int max_ops_per_insn;
197 /* The line base for special opcodes. */
198 int line_base;
199 /* The line range for special opcodes. */
200 unsigned int line_range;
201 /* The opcode base--the first special opcode. */
202 unsigned int opcode_base;
203 /* Opcode lengths, indexed by opcode - 1. */
204 const unsigned char *opcode_lengths;
205 /* The number of directory entries. */
206 size_t dirs_count;
207 /* The directory entries. */
208 const char **dirs;
209 /* The number of filenames. */
210 size_t filenames_count;
211 /* The filenames. */
212 const char **filenames;
215 /* A format description from a line header. */
217 struct line_header_format
219 int lnct; /* LNCT code. */
220 enum dwarf_form form; /* Form of entry data. */
223 /* Map a single PC value to a file/line. We will keep a vector of
224 these sorted by PC value. Each file/line will be correct from the
225 PC up to the PC of the next entry if there is one. We allocate one
226 extra entry at the end so that we can use bsearch. */
228 struct line
230 /* PC. */
231 uintptr_t pc;
232 /* File name. Many entries in the array are expected to point to
233 the same file name. */
234 const char *filename;
235 /* Line number. */
236 int lineno;
237 /* Index of the object in the original array read from the DWARF
238 section, before it has been sorted. The index makes it possible
239 to use Quicksort and maintain stability. */
240 int idx;
243 /* A growable vector of line number information. This is used while
244 reading the line numbers. */
246 struct line_vector
248 /* Memory. This is an array of struct line. */
249 struct backtrace_vector vec;
250 /* Number of valid mappings. */
251 size_t count;
254 /* A function described in the debug info. */
256 struct function
258 /* The name of the function. */
259 const char *name;
260 /* If this is an inlined function, the filename of the call
261 site. */
262 const char *caller_filename;
263 /* If this is an inlined function, the line number of the call
264 site. */
265 int caller_lineno;
266 /* Map PC ranges to inlined functions. */
267 struct function_addrs *function_addrs;
268 size_t function_addrs_count;
271 /* An address range for a function. This maps a PC value to a
272 specific function. */
274 struct function_addrs
276 /* Range is LOW <= PC < HIGH. */
277 uint64_t low;
278 uint64_t high;
279 /* Function for this address range. */
280 struct function *function;
283 /* A growable vector of function address ranges. */
285 struct function_vector
287 /* Memory. This is an array of struct function_addrs. */
288 struct backtrace_vector vec;
289 /* Number of address ranges present. */
290 size_t count;
293 /* A DWARF compilation unit. This only holds the information we need
294 to map a PC to a file and line. */
296 struct unit
298 /* The first entry for this compilation unit. */
299 const unsigned char *unit_data;
300 /* The length of the data for this compilation unit. */
301 size_t unit_data_len;
302 /* The offset of UNIT_DATA from the start of the information for
303 this compilation unit. */
304 size_t unit_data_offset;
305 /* Offset of the start of the compilation unit from the start of the
306 .debug_info section. */
307 size_t low_offset;
308 /* Offset of the end of the compilation unit from the start of the
309 .debug_info section. */
310 size_t high_offset;
311 /* DWARF version. */
312 int version;
313 /* Whether unit is DWARF64. */
314 int is_dwarf64;
315 /* Address size. */
316 int addrsize;
317 /* Offset into line number information. */
318 off_t lineoff;
319 /* Offset of compilation unit in .debug_str_offsets. */
320 uint64_t str_offsets_base;
321 /* Offset of compilation unit in .debug_addr. */
322 uint64_t addr_base;
323 /* Offset of compilation unit in .debug_rnglists. */
324 uint64_t rnglists_base;
325 /* Primary source file. */
326 const char *filename;
327 /* Compilation command working directory. */
328 const char *comp_dir;
329 /* Absolute file name, only set if needed. */
330 const char *abs_filename;
331 /* The abbreviations for this unit. */
332 struct abbrevs abbrevs;
334 /* The fields above this point are read in during initialization and
335 may be accessed freely. The fields below this point are read in
336 as needed, and therefore require care, as different threads may
337 try to initialize them simultaneously. */
339 /* PC to line number mapping. This is NULL if the values have not
340 been read. This is (struct line *) -1 if there was an error
341 reading the values. */
342 struct line *lines;
343 /* Number of entries in lines. */
344 size_t lines_count;
345 /* PC ranges to function. */
346 struct function_addrs *function_addrs;
347 size_t function_addrs_count;
350 /* An address range for a compilation unit. This maps a PC value to a
351 specific compilation unit. Note that we invert the representation
352 in DWARF: instead of listing the units and attaching a list of
353 ranges, we list the ranges and have each one point to the unit.
354 This lets us do a binary search to find the unit. */
356 struct unit_addrs
358 /* Range is LOW <= PC < HIGH. */
359 uint64_t low;
360 uint64_t high;
361 /* Compilation unit for this address range. */
362 struct unit *u;
365 /* A growable vector of compilation unit address ranges. */
367 struct unit_addrs_vector
369 /* Memory. This is an array of struct unit_addrs. */
370 struct backtrace_vector vec;
371 /* Number of address ranges present. */
372 size_t count;
375 /* A growable vector of compilation unit pointer. */
377 struct unit_vector
379 struct backtrace_vector vec;
380 size_t count;
383 /* The information we need to map a PC to a file and line. */
385 struct dwarf_data
387 /* The data for the next file we know about. */
388 struct dwarf_data *next;
389 /* The data for .gnu_debugaltlink. */
390 struct dwarf_data *altlink;
391 /* The base address for this file. */
392 uintptr_t base_address;
393 /* A sorted list of address ranges. */
394 struct unit_addrs *addrs;
395 /* Number of address ranges in list. */
396 size_t addrs_count;
397 /* A sorted list of units. */
398 struct unit **units;
399 /* Number of units in the list. */
400 size_t units_count;
401 /* The unparsed DWARF debug data. */
402 struct dwarf_sections dwarf_sections;
403 /* Whether the data is big-endian or not. */
404 int is_bigendian;
405 /* A vector used for function addresses. We keep this here so that
406 we can grow the vector as we read more functions. */
407 struct function_vector fvec;
410 /* Report an error for a DWARF buffer. */
412 static void
413 dwarf_buf_error (struct dwarf_buf *buf, const char *msg)
415 char b[200];
417 snprintf (b, sizeof b, "%s in %s at %d",
418 msg, buf->name, (int) (buf->buf - buf->start));
419 buf->error_callback (buf->data, b, 0);
422 /* Require at least COUNT bytes in BUF. Return 1 if all is well, 0 on
423 error. */
425 static int
426 require (struct dwarf_buf *buf, size_t count)
428 if (buf->left >= count)
429 return 1;
431 if (!buf->reported_underflow)
433 dwarf_buf_error (buf, "DWARF underflow");
434 buf->reported_underflow = 1;
437 return 0;
440 /* Advance COUNT bytes in BUF. Return 1 if all is well, 0 on
441 error. */
443 static int
444 advance (struct dwarf_buf *buf, size_t count)
446 if (!require (buf, count))
447 return 0;
448 buf->buf += count;
449 buf->left -= count;
450 return 1;
453 /* Read one zero-terminated string from BUF and advance past the string. */
455 static const char *
456 read_string (struct dwarf_buf *buf)
458 const char *p = (const char *)buf->buf;
459 size_t len = strnlen (p, buf->left);
461 /* - If len == left, we ran out of buffer before finding the zero terminator.
462 Generate an error by advancing len + 1.
463 - If len < left, advance by len + 1 to skip past the zero terminator. */
464 size_t count = len + 1;
466 if (!advance (buf, count))
467 return NULL;
469 return p;
472 /* Read one byte from BUF and advance 1 byte. */
474 static unsigned char
475 read_byte (struct dwarf_buf *buf)
477 const unsigned char *p = buf->buf;
479 if (!advance (buf, 1))
480 return 0;
481 return p[0];
484 /* Read a signed char from BUF and advance 1 byte. */
486 static signed char
487 read_sbyte (struct dwarf_buf *buf)
489 const unsigned char *p = buf->buf;
491 if (!advance (buf, 1))
492 return 0;
493 return (*p ^ 0x80) - 0x80;
496 /* Read a uint16 from BUF and advance 2 bytes. */
498 static uint16_t
499 read_uint16 (struct dwarf_buf *buf)
501 const unsigned char *p = buf->buf;
503 if (!advance (buf, 2))
504 return 0;
505 if (buf->is_bigendian)
506 return ((uint16_t) p[0] << 8) | (uint16_t) p[1];
507 else
508 return ((uint16_t) p[1] << 8) | (uint16_t) p[0];
511 /* Read a 24 bit value from BUF and advance 3 bytes. */
513 static uint32_t
514 read_uint24 (struct dwarf_buf *buf)
516 const unsigned char *p = buf->buf;
518 if (!advance (buf, 3))
519 return 0;
520 if (buf->is_bigendian)
521 return (((uint32_t) p[0] << 16) | ((uint32_t) p[1] << 8)
522 | (uint32_t) p[2]);
523 else
524 return (((uint32_t) p[2] << 16) | ((uint32_t) p[1] << 8)
525 | (uint32_t) p[0]);
528 /* Read a uint32 from BUF and advance 4 bytes. */
530 static uint32_t
531 read_uint32 (struct dwarf_buf *buf)
533 const unsigned char *p = buf->buf;
535 if (!advance (buf, 4))
536 return 0;
537 if (buf->is_bigendian)
538 return (((uint32_t) p[0] << 24) | ((uint32_t) p[1] << 16)
539 | ((uint32_t) p[2] << 8) | (uint32_t) p[3]);
540 else
541 return (((uint32_t) p[3] << 24) | ((uint32_t) p[2] << 16)
542 | ((uint32_t) p[1] << 8) | (uint32_t) p[0]);
545 /* Read a uint64 from BUF and advance 8 bytes. */
547 static uint64_t
548 read_uint64 (struct dwarf_buf *buf)
550 const unsigned char *p = buf->buf;
552 if (!advance (buf, 8))
553 return 0;
554 if (buf->is_bigendian)
555 return (((uint64_t) p[0] << 56) | ((uint64_t) p[1] << 48)
556 | ((uint64_t) p[2] << 40) | ((uint64_t) p[3] << 32)
557 | ((uint64_t) p[4] << 24) | ((uint64_t) p[5] << 16)
558 | ((uint64_t) p[6] << 8) | (uint64_t) p[7]);
559 else
560 return (((uint64_t) p[7] << 56) | ((uint64_t) p[6] << 48)
561 | ((uint64_t) p[5] << 40) | ((uint64_t) p[4] << 32)
562 | ((uint64_t) p[3] << 24) | ((uint64_t) p[2] << 16)
563 | ((uint64_t) p[1] << 8) | (uint64_t) p[0]);
566 /* Read an offset from BUF and advance the appropriate number of
567 bytes. */
569 static uint64_t
570 read_offset (struct dwarf_buf *buf, int is_dwarf64)
572 if (is_dwarf64)
573 return read_uint64 (buf);
574 else
575 return read_uint32 (buf);
578 /* Read an address from BUF and advance the appropriate number of
579 bytes. */
581 static uint64_t
582 read_address (struct dwarf_buf *buf, int addrsize)
584 switch (addrsize)
586 case 1:
587 return read_byte (buf);
588 case 2:
589 return read_uint16 (buf);
590 case 4:
591 return read_uint32 (buf);
592 case 8:
593 return read_uint64 (buf);
594 default:
595 dwarf_buf_error (buf, "unrecognized address size");
596 return 0;
600 /* Return whether a value is the highest possible address, given the
601 address size. */
603 static int
604 is_highest_address (uint64_t address, int addrsize)
606 switch (addrsize)
608 case 1:
609 return address == (unsigned char) -1;
610 case 2:
611 return address == (uint16_t) -1;
612 case 4:
613 return address == (uint32_t) -1;
614 case 8:
615 return address == (uint64_t) -1;
616 default:
617 return 0;
621 /* Read an unsigned LEB128 number. */
623 static uint64_t
624 read_uleb128 (struct dwarf_buf *buf)
626 uint64_t ret;
627 unsigned int shift;
628 int overflow;
629 unsigned char b;
631 ret = 0;
632 shift = 0;
633 overflow = 0;
636 const unsigned char *p;
638 p = buf->buf;
639 if (!advance (buf, 1))
640 return 0;
641 b = *p;
642 if (shift < 64)
643 ret |= ((uint64_t) (b & 0x7f)) << shift;
644 else if (!overflow)
646 dwarf_buf_error (buf, "LEB128 overflows uint64_t");
647 overflow = 1;
649 shift += 7;
651 while ((b & 0x80) != 0);
653 return ret;
656 /* Read a signed LEB128 number. */
658 static int64_t
659 read_sleb128 (struct dwarf_buf *buf)
661 uint64_t val;
662 unsigned int shift;
663 int overflow;
664 unsigned char b;
666 val = 0;
667 shift = 0;
668 overflow = 0;
671 const unsigned char *p;
673 p = buf->buf;
674 if (!advance (buf, 1))
675 return 0;
676 b = *p;
677 if (shift < 64)
678 val |= ((uint64_t) (b & 0x7f)) << shift;
679 else if (!overflow)
681 dwarf_buf_error (buf, "signed LEB128 overflows uint64_t");
682 overflow = 1;
684 shift += 7;
686 while ((b & 0x80) != 0);
688 if ((b & 0x40) != 0 && shift < 64)
689 val |= ((uint64_t) -1) << shift;
691 return (int64_t) val;
694 /* Return the length of an LEB128 number. */
696 static size_t
697 leb128_len (const unsigned char *p)
699 size_t ret;
701 ret = 1;
702 while ((*p & 0x80) != 0)
704 ++p;
705 ++ret;
707 return ret;
710 /* Read initial_length from BUF and advance the appropriate number of bytes. */
712 static uint64_t
713 read_initial_length (struct dwarf_buf *buf, int *is_dwarf64)
715 uint64_t len;
717 len = read_uint32 (buf);
718 if (len == 0xffffffff)
720 len = read_uint64 (buf);
721 *is_dwarf64 = 1;
723 else
724 *is_dwarf64 = 0;
726 return len;
729 /* Free an abbreviations structure. */
731 static void
732 free_abbrevs (struct backtrace_state *state, struct abbrevs *abbrevs,
733 backtrace_error_callback error_callback, void *data)
735 size_t i;
737 for (i = 0; i < abbrevs->num_abbrevs; ++i)
738 backtrace_free (state, abbrevs->abbrevs[i].attrs,
739 abbrevs->abbrevs[i].num_attrs * sizeof (struct attr),
740 error_callback, data);
741 backtrace_free (state, abbrevs->abbrevs,
742 abbrevs->num_abbrevs * sizeof (struct abbrev),
743 error_callback, data);
744 abbrevs->num_abbrevs = 0;
745 abbrevs->abbrevs = NULL;
748 /* Read an attribute value. Returns 1 on success, 0 on failure. If
749 the value can be represented as a uint64_t, sets *VAL and sets
750 *IS_VALID to 1. We don't try to store the value of other attribute
751 forms, because we don't care about them. */
753 static int
754 read_attribute (enum dwarf_form form, uint64_t implicit_val,
755 struct dwarf_buf *buf, int is_dwarf64, int version,
756 int addrsize, const struct dwarf_sections *dwarf_sections,
757 struct dwarf_data *altlink, struct attr_val *val)
759 /* Avoid warnings about val.u.FIELD may be used uninitialized if
760 this function is inlined. The warnings aren't valid but can
761 occur because the different fields are set and used
762 conditionally. */
763 memset (val, 0, sizeof *val);
765 switch (form)
767 case DW_FORM_addr:
768 val->encoding = ATTR_VAL_ADDRESS;
769 val->u.uint = read_address (buf, addrsize);
770 return 1;
771 case DW_FORM_block2:
772 val->encoding = ATTR_VAL_BLOCK;
773 return advance (buf, read_uint16 (buf));
774 case DW_FORM_block4:
775 val->encoding = ATTR_VAL_BLOCK;
776 return advance (buf, read_uint32 (buf));
777 case DW_FORM_data2:
778 val->encoding = ATTR_VAL_UINT;
779 val->u.uint = read_uint16 (buf);
780 return 1;
781 case DW_FORM_data4:
782 val->encoding = ATTR_VAL_UINT;
783 val->u.uint = read_uint32 (buf);
784 return 1;
785 case DW_FORM_data8:
786 val->encoding = ATTR_VAL_UINT;
787 val->u.uint = read_uint64 (buf);
788 return 1;
789 case DW_FORM_data16:
790 val->encoding = ATTR_VAL_BLOCK;
791 return advance (buf, 16);
792 case DW_FORM_string:
793 val->encoding = ATTR_VAL_STRING;
794 val->u.string = read_string (buf);
795 return val->u.string == NULL ? 0 : 1;
796 case DW_FORM_block:
797 val->encoding = ATTR_VAL_BLOCK;
798 return advance (buf, read_uleb128 (buf));
799 case DW_FORM_block1:
800 val->encoding = ATTR_VAL_BLOCK;
801 return advance (buf, read_byte (buf));
802 case DW_FORM_data1:
803 val->encoding = ATTR_VAL_UINT;
804 val->u.uint = read_byte (buf);
805 return 1;
806 case DW_FORM_flag:
807 val->encoding = ATTR_VAL_UINT;
808 val->u.uint = read_byte (buf);
809 return 1;
810 case DW_FORM_sdata:
811 val->encoding = ATTR_VAL_SINT;
812 val->u.sint = read_sleb128 (buf);
813 return 1;
814 case DW_FORM_strp:
816 uint64_t offset;
818 offset = read_offset (buf, is_dwarf64);
819 if (offset >= dwarf_sections->size[DEBUG_STR])
821 dwarf_buf_error (buf, "DW_FORM_strp out of range");
822 return 0;
824 val->encoding = ATTR_VAL_STRING;
825 val->u.string =
826 (const char *) dwarf_sections->data[DEBUG_STR] + offset;
827 return 1;
829 case DW_FORM_line_strp:
831 uint64_t offset;
833 offset = read_offset (buf, is_dwarf64);
834 if (offset >= dwarf_sections->size[DEBUG_LINE_STR])
836 dwarf_buf_error (buf, "DW_FORM_line_strp out of range");
837 return 0;
839 val->encoding = ATTR_VAL_STRING;
840 val->u.string =
841 (const char *) dwarf_sections->data[DEBUG_LINE_STR] + offset;
842 return 1;
844 case DW_FORM_udata:
845 val->encoding = ATTR_VAL_UINT;
846 val->u.uint = read_uleb128 (buf);
847 return 1;
848 case DW_FORM_ref_addr:
849 val->encoding = ATTR_VAL_REF_INFO;
850 if (version == 2)
851 val->u.uint = read_address (buf, addrsize);
852 else
853 val->u.uint = read_offset (buf, is_dwarf64);
854 return 1;
855 case DW_FORM_ref1:
856 val->encoding = ATTR_VAL_REF_UNIT;
857 val->u.uint = read_byte (buf);
858 return 1;
859 case DW_FORM_ref2:
860 val->encoding = ATTR_VAL_REF_UNIT;
861 val->u.uint = read_uint16 (buf);
862 return 1;
863 case DW_FORM_ref4:
864 val->encoding = ATTR_VAL_REF_UNIT;
865 val->u.uint = read_uint32 (buf);
866 return 1;
867 case DW_FORM_ref8:
868 val->encoding = ATTR_VAL_REF_UNIT;
869 val->u.uint = read_uint64 (buf);
870 return 1;
871 case DW_FORM_ref_udata:
872 val->encoding = ATTR_VAL_REF_UNIT;
873 val->u.uint = read_uleb128 (buf);
874 return 1;
875 case DW_FORM_indirect:
877 uint64_t form;
879 form = read_uleb128 (buf);
880 if (form == DW_FORM_implicit_const)
882 dwarf_buf_error (buf,
883 "DW_FORM_indirect to DW_FORM_implicit_const");
884 return 0;
886 return read_attribute ((enum dwarf_form) form, 0, buf, is_dwarf64,
887 version, addrsize, dwarf_sections, altlink,
888 val);
890 case DW_FORM_sec_offset:
891 val->encoding = ATTR_VAL_REF_SECTION;
892 val->u.uint = read_offset (buf, is_dwarf64);
893 return 1;
894 case DW_FORM_exprloc:
895 val->encoding = ATTR_VAL_EXPR;
896 return advance (buf, read_uleb128 (buf));
897 case DW_FORM_flag_present:
898 val->encoding = ATTR_VAL_UINT;
899 val->u.uint = 1;
900 return 1;
901 case DW_FORM_ref_sig8:
902 val->encoding = ATTR_VAL_REF_TYPE;
903 val->u.uint = read_uint64 (buf);
904 return 1;
905 case DW_FORM_strx: case DW_FORM_strx1: case DW_FORM_strx2:
906 case DW_FORM_strx3: case DW_FORM_strx4:
908 uint64_t offset;
910 switch (form)
912 case DW_FORM_strx:
913 offset = read_uleb128 (buf);
914 break;
915 case DW_FORM_strx1:
916 offset = read_byte (buf);
917 break;
918 case DW_FORM_strx2:
919 offset = read_uint16 (buf);
920 break;
921 case DW_FORM_strx3:
922 offset = read_uint24 (buf);
923 break;
924 case DW_FORM_strx4:
925 offset = read_uint32 (buf);
926 break;
927 default:
928 /* This case can't happen. */
929 return 0;
931 val->encoding = ATTR_VAL_STRING_INDEX;
932 val->u.uint = offset;
933 return 1;
935 case DW_FORM_addrx: case DW_FORM_addrx1: case DW_FORM_addrx2:
936 case DW_FORM_addrx3: case DW_FORM_addrx4:
938 uint64_t offset;
940 switch (form)
942 case DW_FORM_addrx:
943 offset = read_uleb128 (buf);
944 break;
945 case DW_FORM_addrx1:
946 offset = read_byte (buf);
947 break;
948 case DW_FORM_addrx2:
949 offset = read_uint16 (buf);
950 break;
951 case DW_FORM_addrx3:
952 offset = read_uint24 (buf);
953 break;
954 case DW_FORM_addrx4:
955 offset = read_uint32 (buf);
956 break;
957 default:
958 /* This case can't happen. */
959 return 0;
961 val->encoding = ATTR_VAL_ADDRESS_INDEX;
962 val->u.uint = offset;
963 return 1;
965 case DW_FORM_ref_sup4:
966 val->encoding = ATTR_VAL_REF_SECTION;
967 val->u.uint = read_uint32 (buf);
968 return 1;
969 case DW_FORM_ref_sup8:
970 val->encoding = ATTR_VAL_REF_SECTION;
971 val->u.uint = read_uint64 (buf);
972 return 1;
973 case DW_FORM_implicit_const:
974 val->encoding = ATTR_VAL_UINT;
975 val->u.uint = implicit_val;
976 return 1;
977 case DW_FORM_loclistx:
978 /* We don't distinguish this from DW_FORM_sec_offset. It
979 * shouldn't matter since we don't care about loclists. */
980 val->encoding = ATTR_VAL_REF_SECTION;
981 val->u.uint = read_uleb128 (buf);
982 return 1;
983 case DW_FORM_rnglistx:
984 val->encoding = ATTR_VAL_RNGLISTS_INDEX;
985 val->u.uint = read_uleb128 (buf);
986 return 1;
987 case DW_FORM_GNU_addr_index:
988 val->encoding = ATTR_VAL_REF_SECTION;
989 val->u.uint = read_uleb128 (buf);
990 return 1;
991 case DW_FORM_GNU_str_index:
992 val->encoding = ATTR_VAL_REF_SECTION;
993 val->u.uint = read_uleb128 (buf);
994 return 1;
995 case DW_FORM_GNU_ref_alt:
996 val->u.uint = read_offset (buf, is_dwarf64);
997 if (altlink == NULL)
999 val->encoding = ATTR_VAL_NONE;
1000 return 1;
1002 val->encoding = ATTR_VAL_REF_ALT_INFO;
1003 return 1;
1004 case DW_FORM_strp_sup: case DW_FORM_GNU_strp_alt:
1006 uint64_t offset;
1008 offset = read_offset (buf, is_dwarf64);
1009 if (altlink == NULL)
1011 val->encoding = ATTR_VAL_NONE;
1012 return 1;
1014 if (offset >= altlink->dwarf_sections.size[DEBUG_STR])
1016 dwarf_buf_error (buf, "DW_FORM_strp_sup out of range");
1017 return 0;
1019 val->encoding = ATTR_VAL_STRING;
1020 val->u.string =
1021 (const char *) altlink->dwarf_sections.data[DEBUG_STR] + offset;
1022 return 1;
1024 default:
1025 dwarf_buf_error (buf, "unrecognized DWARF form");
1026 return 0;
1030 /* If we can determine the value of a string attribute, set *STRING to
1031 point to the string. Return 1 on success, 0 on error. If we don't
1032 know the value, we consider that a success, and we don't change
1033 *STRING. An error is only reported for some sort of out of range
1034 offset. */
1036 static int
1037 resolve_string (const struct dwarf_sections *dwarf_sections, int is_dwarf64,
1038 int is_bigendian, uint64_t str_offsets_base,
1039 const struct attr_val *val,
1040 backtrace_error_callback error_callback, void *data,
1041 const char **string)
1043 switch (val->encoding)
1045 case ATTR_VAL_STRING:
1046 *string = val->u.string;
1047 return 1;
1049 case ATTR_VAL_STRING_INDEX:
1051 uint64_t offset;
1052 struct dwarf_buf offset_buf;
1054 offset = val->u.uint * (is_dwarf64 ? 8 : 4) + str_offsets_base;
1055 if (offset + (is_dwarf64 ? 8 : 4)
1056 >= dwarf_sections->size[DEBUG_STR_OFFSETS])
1058 error_callback (data, "DW_FORM_strx value out of range", 0);
1059 return 0;
1062 offset_buf.name = ".debug_str_offsets";
1063 offset_buf.start = dwarf_sections->data[DEBUG_STR_OFFSETS];
1064 offset_buf.buf = dwarf_sections->data[DEBUG_STR_OFFSETS] + offset;
1065 offset_buf.left = dwarf_sections->size[DEBUG_STR_OFFSETS] - offset;
1066 offset_buf.is_bigendian = is_bigendian;
1067 offset_buf.error_callback = error_callback;
1068 offset_buf.data = data;
1069 offset_buf.reported_underflow = 0;
1071 offset = read_offset (&offset_buf, is_dwarf64);
1072 if (offset >= dwarf_sections->size[DEBUG_STR])
1074 dwarf_buf_error (&offset_buf, "DW_FORM_strx offset out of range");
1075 return 0;
1077 *string = (const char *) dwarf_sections->data[DEBUG_STR] + offset;
1078 return 1;
1081 default:
1082 return 1;
1086 /* Set *ADDRESS to the real address for a ATTR_VAL_ADDRESS_INDEX.
1087 Return 1 on success, 0 on error. */
1089 static int
1090 resolve_addr_index (const struct dwarf_sections *dwarf_sections,
1091 uint64_t addr_base, int addrsize, int is_bigendian,
1092 uint64_t addr_index,
1093 backtrace_error_callback error_callback, void *data,
1094 uint64_t *address)
1096 uint64_t offset;
1097 struct dwarf_buf addr_buf;
1099 offset = addr_index * addrsize + addr_base;
1100 if (offset + addrsize >= dwarf_sections->size[DEBUG_ADDR])
1102 error_callback (data, "DW_FORM_addrx value out of range", 0);
1103 return 0;
1106 addr_buf.name = ".debug_addr";
1107 addr_buf.start = dwarf_sections->data[DEBUG_ADDR];
1108 addr_buf.buf = dwarf_sections->data[DEBUG_ADDR] + offset;
1109 addr_buf.left = dwarf_sections->size[DEBUG_ADDR] - offset;
1110 addr_buf.is_bigendian = is_bigendian;
1111 addr_buf.error_callback = error_callback;
1112 addr_buf.data = data;
1113 addr_buf.reported_underflow = 0;
1115 *address = read_address (&addr_buf, addrsize);
1116 return 1;
1119 /* Compare a unit offset against a unit for bsearch. */
1121 static int
1122 units_search (const void *vkey, const void *ventry)
1124 const size_t *key = (const size_t *) vkey;
1125 const struct unit *entry = *((const struct unit *const *) ventry);
1126 size_t offset;
1128 offset = *key;
1129 if (offset < entry->low_offset)
1130 return -1;
1131 else if (offset >= entry->high_offset)
1132 return 1;
1133 else
1134 return 0;
1137 /* Find a unit in PU containing OFFSET. */
1139 static struct unit *
1140 find_unit (struct unit **pu, size_t units_count, size_t offset)
1142 struct unit **u;
1143 u = bsearch (&offset, pu, units_count, sizeof (struct unit *), units_search);
1144 return u == NULL ? NULL : *u;
1147 /* Compare function_addrs for qsort. When ranges are nested, make the
1148 smallest one sort last. */
1150 static int
1151 function_addrs_compare (const void *v1, const void *v2)
1153 const struct function_addrs *a1 = (const struct function_addrs *) v1;
1154 const struct function_addrs *a2 = (const struct function_addrs *) v2;
1156 if (a1->low < a2->low)
1157 return -1;
1158 if (a1->low > a2->low)
1159 return 1;
1160 if (a1->high < a2->high)
1161 return 1;
1162 if (a1->high > a2->high)
1163 return -1;
1164 return strcmp (a1->function->name, a2->function->name);
1167 /* Compare a PC against a function_addrs for bsearch. Note that if
1168 there are multiple ranges containing PC, which one will be returned
1169 is unpredictable. We compensate for that in dwarf_fileline. */
1171 static int
1172 function_addrs_search (const void *vkey, const void *ventry)
1174 const uintptr_t *key = (const uintptr_t *) vkey;
1175 const struct function_addrs *entry = (const struct function_addrs *) ventry;
1176 uintptr_t pc;
1178 pc = *key;
1179 if (pc < entry->low)
1180 return -1;
1181 else if (pc >= entry->high)
1182 return 1;
1183 else
1184 return 0;
1187 /* Add a new compilation unit address range to a vector. This is
1188 called via add_ranges. Returns 1 on success, 0 on failure. */
1190 static int
1191 add_unit_addr (struct backtrace_state *state, void *rdata,
1192 uint64_t lowpc, uint64_t highpc,
1193 backtrace_error_callback error_callback, void *data,
1194 void *pvec)
1196 struct unit *u = (struct unit *) rdata;
1197 struct unit_addrs_vector *vec = (struct unit_addrs_vector *) pvec;
1198 struct unit_addrs *p;
1200 /* Try to merge with the last entry. */
1201 if (vec->count > 0)
1203 p = (struct unit_addrs *) vec->vec.base + (vec->count - 1);
1204 if ((lowpc == p->high || lowpc == p->high + 1)
1205 && u == p->u)
1207 if (highpc > p->high)
1208 p->high = highpc;
1209 return 1;
1213 p = ((struct unit_addrs *)
1214 backtrace_vector_grow (state, sizeof (struct unit_addrs),
1215 error_callback, data, &vec->vec));
1216 if (p == NULL)
1217 return 0;
1219 p->low = lowpc;
1220 p->high = highpc;
1221 p->u = u;
1223 ++vec->count;
1225 return 1;
1228 /* Compare unit_addrs for qsort. When ranges are nested, make the
1229 smallest one sort last. */
1231 static int
1232 unit_addrs_compare (const void *v1, const void *v2)
1234 const struct unit_addrs *a1 = (const struct unit_addrs *) v1;
1235 const struct unit_addrs *a2 = (const struct unit_addrs *) v2;
1237 if (a1->low < a2->low)
1238 return -1;
1239 if (a1->low > a2->low)
1240 return 1;
1241 if (a1->high < a2->high)
1242 return 1;
1243 if (a1->high > a2->high)
1244 return -1;
1245 if (a1->u->lineoff < a2->u->lineoff)
1246 return -1;
1247 if (a1->u->lineoff > a2->u->lineoff)
1248 return 1;
1249 return 0;
1252 /* Compare a PC against a unit_addrs for bsearch. Note that if there
1253 are multiple ranges containing PC, which one will be returned is
1254 unpredictable. We compensate for that in dwarf_fileline. */
1256 static int
1257 unit_addrs_search (const void *vkey, const void *ventry)
1259 const uintptr_t *key = (const uintptr_t *) vkey;
1260 const struct unit_addrs *entry = (const struct unit_addrs *) ventry;
1261 uintptr_t pc;
1263 pc = *key;
1264 if (pc < entry->low)
1265 return -1;
1266 else if (pc >= entry->high)
1267 return 1;
1268 else
1269 return 0;
1272 /* Sort the line vector by PC. We want a stable sort here to maintain
1273 the order of lines for the same PC values. Since the sequence is
1274 being sorted in place, their addresses cannot be relied on to
1275 maintain stability. That is the purpose of the index member. */
1277 static int
1278 line_compare (const void *v1, const void *v2)
1280 const struct line *ln1 = (const struct line *) v1;
1281 const struct line *ln2 = (const struct line *) v2;
1283 if (ln1->pc < ln2->pc)
1284 return -1;
1285 else if (ln1->pc > ln2->pc)
1286 return 1;
1287 else if (ln1->idx < ln2->idx)
1288 return -1;
1289 else if (ln1->idx > ln2->idx)
1290 return 1;
1291 else
1292 return 0;
1295 /* Find a PC in a line vector. We always allocate an extra entry at
1296 the end of the lines vector, so that this routine can safely look
1297 at the next entry. Note that when there are multiple mappings for
1298 the same PC value, this will return the last one. */
1300 static int
1301 line_search (const void *vkey, const void *ventry)
1303 const uintptr_t *key = (const uintptr_t *) vkey;
1304 const struct line *entry = (const struct line *) ventry;
1305 uintptr_t pc;
1307 pc = *key;
1308 if (pc < entry->pc)
1309 return -1;
1310 else if (pc >= (entry + 1)->pc)
1311 return 1;
1312 else
1313 return 0;
1316 /* Sort the abbrevs by the abbrev code. This function is passed to
1317 both qsort and bsearch. */
1319 static int
1320 abbrev_compare (const void *v1, const void *v2)
1322 const struct abbrev *a1 = (const struct abbrev *) v1;
1323 const struct abbrev *a2 = (const struct abbrev *) v2;
1325 if (a1->code < a2->code)
1326 return -1;
1327 else if (a1->code > a2->code)
1328 return 1;
1329 else
1331 /* This really shouldn't happen. It means there are two
1332 different abbrevs with the same code, and that means we don't
1333 know which one lookup_abbrev should return. */
1334 return 0;
1338 /* Read the abbreviation table for a compilation unit. Returns 1 on
1339 success, 0 on failure. */
1341 static int
1342 read_abbrevs (struct backtrace_state *state, uint64_t abbrev_offset,
1343 const unsigned char *dwarf_abbrev, size_t dwarf_abbrev_size,
1344 int is_bigendian, backtrace_error_callback error_callback,
1345 void *data, struct abbrevs *abbrevs)
1347 struct dwarf_buf abbrev_buf;
1348 struct dwarf_buf count_buf;
1349 size_t num_abbrevs;
1351 abbrevs->num_abbrevs = 0;
1352 abbrevs->abbrevs = NULL;
1354 if (abbrev_offset >= dwarf_abbrev_size)
1356 error_callback (data, "abbrev offset out of range", 0);
1357 return 0;
1360 abbrev_buf.name = ".debug_abbrev";
1361 abbrev_buf.start = dwarf_abbrev;
1362 abbrev_buf.buf = dwarf_abbrev + abbrev_offset;
1363 abbrev_buf.left = dwarf_abbrev_size - abbrev_offset;
1364 abbrev_buf.is_bigendian = is_bigendian;
1365 abbrev_buf.error_callback = error_callback;
1366 abbrev_buf.data = data;
1367 abbrev_buf.reported_underflow = 0;
1369 /* Count the number of abbrevs in this list. */
1371 count_buf = abbrev_buf;
1372 num_abbrevs = 0;
1373 while (read_uleb128 (&count_buf) != 0)
1375 if (count_buf.reported_underflow)
1376 return 0;
1377 ++num_abbrevs;
1378 // Skip tag.
1379 read_uleb128 (&count_buf);
1380 // Skip has_children.
1381 read_byte (&count_buf);
1382 // Skip attributes.
1383 while (read_uleb128 (&count_buf) != 0)
1385 uint64_t form;
1387 form = read_uleb128 (&count_buf);
1388 if ((enum dwarf_form) form == DW_FORM_implicit_const)
1389 read_sleb128 (&count_buf);
1391 // Skip form of last attribute.
1392 read_uleb128 (&count_buf);
1395 if (count_buf.reported_underflow)
1396 return 0;
1398 if (num_abbrevs == 0)
1399 return 1;
1401 abbrevs->abbrevs = ((struct abbrev *)
1402 backtrace_alloc (state,
1403 num_abbrevs * sizeof (struct abbrev),
1404 error_callback, data));
1405 if (abbrevs->abbrevs == NULL)
1406 return 0;
1407 abbrevs->num_abbrevs = num_abbrevs;
1408 memset (abbrevs->abbrevs, 0, num_abbrevs * sizeof (struct abbrev));
1410 num_abbrevs = 0;
1411 while (1)
1413 uint64_t code;
1414 struct abbrev a;
1415 size_t num_attrs;
1416 struct attr *attrs;
1418 if (abbrev_buf.reported_underflow)
1419 goto fail;
1421 code = read_uleb128 (&abbrev_buf);
1422 if (code == 0)
1423 break;
1425 a.code = code;
1426 a.tag = (enum dwarf_tag) read_uleb128 (&abbrev_buf);
1427 a.has_children = read_byte (&abbrev_buf);
1429 count_buf = abbrev_buf;
1430 num_attrs = 0;
1431 while (read_uleb128 (&count_buf) != 0)
1433 uint64_t form;
1435 ++num_attrs;
1436 form = read_uleb128 (&count_buf);
1437 if ((enum dwarf_form) form == DW_FORM_implicit_const)
1438 read_sleb128 (&count_buf);
1441 if (num_attrs == 0)
1443 attrs = NULL;
1444 read_uleb128 (&abbrev_buf);
1445 read_uleb128 (&abbrev_buf);
1447 else
1449 attrs = ((struct attr *)
1450 backtrace_alloc (state, num_attrs * sizeof *attrs,
1451 error_callback, data));
1452 if (attrs == NULL)
1453 goto fail;
1454 num_attrs = 0;
1455 while (1)
1457 uint64_t name;
1458 uint64_t form;
1460 name = read_uleb128 (&abbrev_buf);
1461 form = read_uleb128 (&abbrev_buf);
1462 if (name == 0)
1463 break;
1464 attrs[num_attrs].name = (enum dwarf_attribute) name;
1465 attrs[num_attrs].form = (enum dwarf_form) form;
1466 if ((enum dwarf_form) form == DW_FORM_implicit_const)
1467 attrs[num_attrs].val = read_sleb128 (&abbrev_buf);
1468 else
1469 attrs[num_attrs].val = 0;
1470 ++num_attrs;
1474 a.num_attrs = num_attrs;
1475 a.attrs = attrs;
1477 abbrevs->abbrevs[num_abbrevs] = a;
1478 ++num_abbrevs;
1481 backtrace_qsort (abbrevs->abbrevs, abbrevs->num_abbrevs,
1482 sizeof (struct abbrev), abbrev_compare);
1484 return 1;
1486 fail:
1487 free_abbrevs (state, abbrevs, error_callback, data);
1488 return 0;
1491 /* Return the abbrev information for an abbrev code. */
1493 static const struct abbrev *
1494 lookup_abbrev (struct abbrevs *abbrevs, uint64_t code,
1495 backtrace_error_callback error_callback, void *data)
1497 struct abbrev key;
1498 void *p;
1500 /* With GCC, where abbrevs are simply numbered in order, we should
1501 be able to just look up the entry. */
1502 if (code - 1 < abbrevs->num_abbrevs
1503 && abbrevs->abbrevs[code - 1].code == code)
1504 return &abbrevs->abbrevs[code - 1];
1506 /* Otherwise we have to search. */
1507 memset (&key, 0, sizeof key);
1508 key.code = code;
1509 p = bsearch (&key, abbrevs->abbrevs, abbrevs->num_abbrevs,
1510 sizeof (struct abbrev), abbrev_compare);
1511 if (p == NULL)
1513 error_callback (data, "invalid abbreviation code", 0);
1514 return NULL;
1516 return (const struct abbrev *) p;
1519 /* This struct is used to gather address range information while
1520 reading attributes. We use this while building a mapping from
1521 address ranges to compilation units and then again while mapping
1522 from address ranges to function entries. Normally either
1523 lowpc/highpc is set or ranges is set. */
1525 struct pcrange {
1526 uint64_t lowpc; /* The low PC value. */
1527 int have_lowpc; /* Whether a low PC value was found. */
1528 int lowpc_is_addr_index; /* Whether lowpc is in .debug_addr. */
1529 uint64_t highpc; /* The high PC value. */
1530 int have_highpc; /* Whether a high PC value was found. */
1531 int highpc_is_relative; /* Whether highpc is relative to lowpc. */
1532 int highpc_is_addr_index; /* Whether highpc is in .debug_addr. */
1533 uint64_t ranges; /* Offset in ranges section. */
1534 int have_ranges; /* Whether ranges is valid. */
1535 int ranges_is_index; /* Whether ranges is DW_FORM_rnglistx. */
1538 /* Update PCRANGE from an attribute value. */
1540 static void
1541 update_pcrange (const struct attr* attr, const struct attr_val* val,
1542 struct pcrange *pcrange)
1544 switch (attr->name)
1546 case DW_AT_low_pc:
1547 if (val->encoding == ATTR_VAL_ADDRESS)
1549 pcrange->lowpc = val->u.uint;
1550 pcrange->have_lowpc = 1;
1552 else if (val->encoding == ATTR_VAL_ADDRESS_INDEX)
1554 pcrange->lowpc = val->u.uint;
1555 pcrange->have_lowpc = 1;
1556 pcrange->lowpc_is_addr_index = 1;
1558 break;
1560 case DW_AT_high_pc:
1561 if (val->encoding == ATTR_VAL_ADDRESS)
1563 pcrange->highpc = val->u.uint;
1564 pcrange->have_highpc = 1;
1566 else if (val->encoding == ATTR_VAL_UINT)
1568 pcrange->highpc = val->u.uint;
1569 pcrange->have_highpc = 1;
1570 pcrange->highpc_is_relative = 1;
1572 else if (val->encoding == ATTR_VAL_ADDRESS_INDEX)
1574 pcrange->highpc = val->u.uint;
1575 pcrange->have_highpc = 1;
1576 pcrange->highpc_is_addr_index = 1;
1578 break;
1580 case DW_AT_ranges:
1581 if (val->encoding == ATTR_VAL_UINT
1582 || val->encoding == ATTR_VAL_REF_SECTION)
1584 pcrange->ranges = val->u.uint;
1585 pcrange->have_ranges = 1;
1587 else if (val->encoding == ATTR_VAL_RNGLISTS_INDEX)
1589 pcrange->ranges = val->u.uint;
1590 pcrange->have_ranges = 1;
1591 pcrange->ranges_is_index = 1;
1593 break;
1595 default:
1596 break;
1600 /* Call ADD_RANGE for a low/high PC pair. Returns 1 on success, 0 on
1601 error. */
1603 static int
1604 add_low_high_range (struct backtrace_state *state,
1605 const struct dwarf_sections *dwarf_sections,
1606 uintptr_t base_address, int is_bigendian,
1607 struct unit *u, const struct pcrange *pcrange,
1608 int (*add_range) (struct backtrace_state *state,
1609 void *rdata, uint64_t lowpc,
1610 uint64_t highpc,
1611 backtrace_error_callback error_callback,
1612 void *data, void *vec),
1613 void *rdata,
1614 backtrace_error_callback error_callback, void *data,
1615 void *vec)
1617 uint64_t lowpc;
1618 uint64_t highpc;
1620 lowpc = pcrange->lowpc;
1621 if (pcrange->lowpc_is_addr_index)
1623 if (!resolve_addr_index (dwarf_sections, u->addr_base, u->addrsize,
1624 is_bigendian, lowpc, error_callback, data,
1625 &lowpc))
1626 return 0;
1629 highpc = pcrange->highpc;
1630 if (pcrange->highpc_is_addr_index)
1632 if (!resolve_addr_index (dwarf_sections, u->addr_base, u->addrsize,
1633 is_bigendian, highpc, error_callback, data,
1634 &highpc))
1635 return 0;
1637 if (pcrange->highpc_is_relative)
1638 highpc += lowpc;
1640 /* Add in the base address of the module when recording PC values,
1641 so that we can look up the PC directly. */
1642 lowpc += base_address;
1643 highpc += base_address;
1645 return add_range (state, rdata, lowpc, highpc, error_callback, data, vec);
1648 /* Call ADD_RANGE for each range read from .debug_ranges, as used in
1649 DWARF versions 2 through 4. */
1651 static int
1652 add_ranges_from_ranges (
1653 struct backtrace_state *state,
1654 const struct dwarf_sections *dwarf_sections,
1655 uintptr_t base_address, int is_bigendian,
1656 struct unit *u, uint64_t base,
1657 const struct pcrange *pcrange,
1658 int (*add_range) (struct backtrace_state *state, void *rdata,
1659 uint64_t lowpc, uint64_t highpc,
1660 backtrace_error_callback error_callback, void *data,
1661 void *vec),
1662 void *rdata,
1663 backtrace_error_callback error_callback, void *data,
1664 void *vec)
1666 struct dwarf_buf ranges_buf;
1668 if (pcrange->ranges >= dwarf_sections->size[DEBUG_RANGES])
1670 error_callback (data, "ranges offset out of range", 0);
1671 return 0;
1674 ranges_buf.name = ".debug_ranges";
1675 ranges_buf.start = dwarf_sections->data[DEBUG_RANGES];
1676 ranges_buf.buf = dwarf_sections->data[DEBUG_RANGES] + pcrange->ranges;
1677 ranges_buf.left = dwarf_sections->size[DEBUG_RANGES] - pcrange->ranges;
1678 ranges_buf.is_bigendian = is_bigendian;
1679 ranges_buf.error_callback = error_callback;
1680 ranges_buf.data = data;
1681 ranges_buf.reported_underflow = 0;
1683 while (1)
1685 uint64_t low;
1686 uint64_t high;
1688 if (ranges_buf.reported_underflow)
1689 return 0;
1691 low = read_address (&ranges_buf, u->addrsize);
1692 high = read_address (&ranges_buf, u->addrsize);
1694 if (low == 0 && high == 0)
1695 break;
1697 if (is_highest_address (low, u->addrsize))
1698 base = high;
1699 else
1701 if (!add_range (state, rdata,
1702 low + base + base_address,
1703 high + base + base_address,
1704 error_callback, data, vec))
1705 return 0;
1709 if (ranges_buf.reported_underflow)
1710 return 0;
1712 return 1;
1715 /* Call ADD_RANGE for each range read from .debug_rnglists, as used in
1716 DWARF version 5. */
1718 static int
1719 add_ranges_from_rnglists (
1720 struct backtrace_state *state,
1721 const struct dwarf_sections *dwarf_sections,
1722 uintptr_t base_address, int is_bigendian,
1723 struct unit *u, uint64_t base,
1724 const struct pcrange *pcrange,
1725 int (*add_range) (struct backtrace_state *state, void *rdata,
1726 uint64_t lowpc, uint64_t highpc,
1727 backtrace_error_callback error_callback, void *data,
1728 void *vec),
1729 void *rdata,
1730 backtrace_error_callback error_callback, void *data,
1731 void *vec)
1733 uint64_t offset;
1734 struct dwarf_buf rnglists_buf;
1736 if (!pcrange->ranges_is_index)
1737 offset = pcrange->ranges;
1738 else
1739 offset = u->rnglists_base + pcrange->ranges * (u->is_dwarf64 ? 8 : 4);
1740 if (offset >= dwarf_sections->size[DEBUG_RNGLISTS])
1742 error_callback (data, "rnglists offset out of range", 0);
1743 return 0;
1746 rnglists_buf.name = ".debug_rnglists";
1747 rnglists_buf.start = dwarf_sections->data[DEBUG_RNGLISTS];
1748 rnglists_buf.buf = dwarf_sections->data[DEBUG_RNGLISTS] + offset;
1749 rnglists_buf.left = dwarf_sections->size[DEBUG_RNGLISTS] - offset;
1750 rnglists_buf.is_bigendian = is_bigendian;
1751 rnglists_buf.error_callback = error_callback;
1752 rnglists_buf.data = data;
1753 rnglists_buf.reported_underflow = 0;
1755 if (pcrange->ranges_is_index)
1757 offset = read_offset (&rnglists_buf, u->is_dwarf64);
1758 offset += u->rnglists_base;
1759 if (offset >= dwarf_sections->size[DEBUG_RNGLISTS])
1761 error_callback (data, "rnglists index offset out of range", 0);
1762 return 0;
1764 rnglists_buf.buf = dwarf_sections->data[DEBUG_RNGLISTS] + offset;
1765 rnglists_buf.left = dwarf_sections->size[DEBUG_RNGLISTS] - offset;
1768 while (1)
1770 unsigned char rle;
1772 rle = read_byte (&rnglists_buf);
1773 if (rle == DW_RLE_end_of_list)
1774 break;
1775 switch (rle)
1777 case DW_RLE_base_addressx:
1779 uint64_t index;
1781 index = read_uleb128 (&rnglists_buf);
1782 if (!resolve_addr_index (dwarf_sections, u->addr_base,
1783 u->addrsize, is_bigendian, index,
1784 error_callback, data, &base))
1785 return 0;
1787 break;
1789 case DW_RLE_startx_endx:
1791 uint64_t index;
1792 uint64_t low;
1793 uint64_t high;
1795 index = read_uleb128 (&rnglists_buf);
1796 if (!resolve_addr_index (dwarf_sections, u->addr_base,
1797 u->addrsize, is_bigendian, index,
1798 error_callback, data, &low))
1799 return 0;
1800 index = read_uleb128 (&rnglists_buf);
1801 if (!resolve_addr_index (dwarf_sections, u->addr_base,
1802 u->addrsize, is_bigendian, index,
1803 error_callback, data, &high))
1804 return 0;
1805 if (!add_range (state, rdata, low + base_address,
1806 high + base_address, error_callback, data,
1807 vec))
1808 return 0;
1810 break;
1812 case DW_RLE_startx_length:
1814 uint64_t index;
1815 uint64_t low;
1816 uint64_t length;
1818 index = read_uleb128 (&rnglists_buf);
1819 if (!resolve_addr_index (dwarf_sections, u->addr_base,
1820 u->addrsize, is_bigendian, index,
1821 error_callback, data, &low))
1822 return 0;
1823 length = read_uleb128 (&rnglists_buf);
1824 low += base_address;
1825 if (!add_range (state, rdata, low, low + length,
1826 error_callback, data, vec))
1827 return 0;
1829 break;
1831 case DW_RLE_offset_pair:
1833 uint64_t low;
1834 uint64_t high;
1836 low = read_uleb128 (&rnglists_buf);
1837 high = read_uleb128 (&rnglists_buf);
1838 if (!add_range (state, rdata, low + base + base_address,
1839 high + base + base_address,
1840 error_callback, data, vec))
1841 return 0;
1843 break;
1845 case DW_RLE_base_address:
1846 base = read_address (&rnglists_buf, u->addrsize);
1847 break;
1849 case DW_RLE_start_end:
1851 uint64_t low;
1852 uint64_t high;
1854 low = read_address (&rnglists_buf, u->addrsize);
1855 high = read_address (&rnglists_buf, u->addrsize);
1856 if (!add_range (state, rdata, low + base_address,
1857 high + base_address, error_callback, data,
1858 vec))
1859 return 0;
1861 break;
1863 case DW_RLE_start_length:
1865 uint64_t low;
1866 uint64_t length;
1868 low = read_address (&rnglists_buf, u->addrsize);
1869 length = read_uleb128 (&rnglists_buf);
1870 low += base_address;
1871 if (!add_range (state, rdata, low, low + length,
1872 error_callback, data, vec))
1873 return 0;
1875 break;
1877 default:
1878 dwarf_buf_error (&rnglists_buf, "unrecognized DW_RLE value");
1879 return 0;
1883 if (rnglists_buf.reported_underflow)
1884 return 0;
1886 return 1;
1889 /* Call ADD_RANGE for each lowpc/highpc pair in PCRANGE. RDATA is
1890 passed to ADD_RANGE, and is either a struct unit * or a struct
1891 function *. VEC is the vector we are adding ranges to, and is
1892 either a struct unit_addrs_vector * or a struct function_vector *.
1893 Returns 1 on success, 0 on error. */
1895 static int
1896 add_ranges (struct backtrace_state *state,
1897 const struct dwarf_sections *dwarf_sections,
1898 uintptr_t base_address, int is_bigendian,
1899 struct unit *u, uint64_t base, const struct pcrange *pcrange,
1900 int (*add_range) (struct backtrace_state *state, void *rdata,
1901 uint64_t lowpc, uint64_t highpc,
1902 backtrace_error_callback error_callback,
1903 void *data, void *vec),
1904 void *rdata,
1905 backtrace_error_callback error_callback, void *data,
1906 void *vec)
1908 if (pcrange->have_lowpc && pcrange->have_highpc)
1909 return add_low_high_range (state, dwarf_sections, base_address,
1910 is_bigendian, u, pcrange, add_range, rdata,
1911 error_callback, data, vec);
1913 if (!pcrange->have_ranges)
1915 /* Did not find any address ranges to add. */
1916 return 1;
1919 if (u->version < 5)
1920 return add_ranges_from_ranges (state, dwarf_sections, base_address,
1921 is_bigendian, u, base, pcrange, add_range,
1922 rdata, error_callback, data, vec);
1923 else
1924 return add_ranges_from_rnglists (state, dwarf_sections, base_address,
1925 is_bigendian, u, base, pcrange, add_range,
1926 rdata, error_callback, data, vec);
1929 /* Find the address range covered by a compilation unit, reading from
1930 UNIT_BUF and adding values to U. Returns 1 if all data could be
1931 read, 0 if there is some error. */
1933 static int
1934 find_address_ranges (struct backtrace_state *state, uintptr_t base_address,
1935 struct dwarf_buf *unit_buf,
1936 const struct dwarf_sections *dwarf_sections,
1937 int is_bigendian, struct dwarf_data *altlink,
1938 backtrace_error_callback error_callback, void *data,
1939 struct unit *u, struct unit_addrs_vector *addrs,
1940 enum dwarf_tag *unit_tag)
1942 while (unit_buf->left > 0)
1944 uint64_t code;
1945 const struct abbrev *abbrev;
1946 struct pcrange pcrange;
1947 struct attr_val name_val;
1948 int have_name_val;
1949 struct attr_val comp_dir_val;
1950 int have_comp_dir_val;
1951 size_t i;
1953 code = read_uleb128 (unit_buf);
1954 if (code == 0)
1955 return 1;
1957 abbrev = lookup_abbrev (&u->abbrevs, code, error_callback, data);
1958 if (abbrev == NULL)
1959 return 0;
1961 if (unit_tag != NULL)
1962 *unit_tag = abbrev->tag;
1964 memset (&pcrange, 0, sizeof pcrange);
1965 memset (&name_val, 0, sizeof name_val);
1966 have_name_val = 0;
1967 memset (&comp_dir_val, 0, sizeof comp_dir_val);
1968 have_comp_dir_val = 0;
1969 for (i = 0; i < abbrev->num_attrs; ++i)
1971 struct attr_val val;
1973 if (!read_attribute (abbrev->attrs[i].form, abbrev->attrs[i].val,
1974 unit_buf, u->is_dwarf64, u->version,
1975 u->addrsize, dwarf_sections, altlink, &val))
1976 return 0;
1978 switch (abbrev->attrs[i].name)
1980 case DW_AT_low_pc: case DW_AT_high_pc: case DW_AT_ranges:
1981 update_pcrange (&abbrev->attrs[i], &val, &pcrange);
1982 break;
1984 case DW_AT_stmt_list:
1985 if (abbrev->tag == DW_TAG_compile_unit
1986 && (val.encoding == ATTR_VAL_UINT
1987 || val.encoding == ATTR_VAL_REF_SECTION))
1988 u->lineoff = val.u.uint;
1989 break;
1991 case DW_AT_name:
1992 if (abbrev->tag == DW_TAG_compile_unit)
1994 name_val = val;
1995 have_name_val = 1;
1997 break;
1999 case DW_AT_comp_dir:
2000 if (abbrev->tag == DW_TAG_compile_unit)
2002 comp_dir_val = val;
2003 have_comp_dir_val = 1;
2005 break;
2007 case DW_AT_str_offsets_base:
2008 if (abbrev->tag == DW_TAG_compile_unit
2009 && val.encoding == ATTR_VAL_REF_SECTION)
2010 u->str_offsets_base = val.u.uint;
2011 break;
2013 case DW_AT_addr_base:
2014 if (abbrev->tag == DW_TAG_compile_unit
2015 && val.encoding == ATTR_VAL_REF_SECTION)
2016 u->addr_base = val.u.uint;
2017 break;
2019 case DW_AT_rnglists_base:
2020 if (abbrev->tag == DW_TAG_compile_unit
2021 && val.encoding == ATTR_VAL_REF_SECTION)
2022 u->rnglists_base = val.u.uint;
2023 break;
2025 default:
2026 break;
2030 // Resolve strings after we're sure that we have seen
2031 // DW_AT_str_offsets_base.
2032 if (have_name_val)
2034 if (!resolve_string (dwarf_sections, u->is_dwarf64, is_bigendian,
2035 u->str_offsets_base, &name_val,
2036 error_callback, data, &u->filename))
2037 return 0;
2039 if (have_comp_dir_val)
2041 if (!resolve_string (dwarf_sections, u->is_dwarf64, is_bigendian,
2042 u->str_offsets_base, &comp_dir_val,
2043 error_callback, data, &u->comp_dir))
2044 return 0;
2047 if (abbrev->tag == DW_TAG_compile_unit
2048 || abbrev->tag == DW_TAG_subprogram)
2050 if (!add_ranges (state, dwarf_sections, base_address,
2051 is_bigendian, u, pcrange.lowpc, &pcrange,
2052 add_unit_addr, (void *) u, error_callback, data,
2053 (void *) addrs))
2054 return 0;
2056 /* If we found the PC range in the DW_TAG_compile_unit, we
2057 can stop now. */
2058 if (abbrev->tag == DW_TAG_compile_unit
2059 && (pcrange.have_ranges
2060 || (pcrange.have_lowpc && pcrange.have_highpc)))
2061 return 1;
2064 if (abbrev->has_children)
2066 if (!find_address_ranges (state, base_address, unit_buf,
2067 dwarf_sections, is_bigendian, altlink,
2068 error_callback, data, u, addrs, NULL))
2069 return 0;
2073 return 1;
2076 /* Build a mapping from address ranges to the compilation units where
2077 the line number information for that range can be found. Returns 1
2078 on success, 0 on failure. */
2080 static int
2081 build_address_map (struct backtrace_state *state, uintptr_t base_address,
2082 const struct dwarf_sections *dwarf_sections,
2083 int is_bigendian, struct dwarf_data *altlink,
2084 backtrace_error_callback error_callback, void *data,
2085 struct unit_addrs_vector *addrs,
2086 struct unit_vector *unit_vec)
2088 struct dwarf_buf info;
2089 struct backtrace_vector units;
2090 size_t units_count;
2091 size_t i;
2092 struct unit **pu;
2093 size_t unit_offset = 0;
2095 memset (&addrs->vec, 0, sizeof addrs->vec);
2096 memset (&unit_vec->vec, 0, sizeof unit_vec->vec);
2097 addrs->count = 0;
2098 unit_vec->count = 0;
2100 /* Read through the .debug_info section. FIXME: Should we use the
2101 .debug_aranges section? gdb and addr2line don't use it, but I'm
2102 not sure why. */
2104 info.name = ".debug_info";
2105 info.start = dwarf_sections->data[DEBUG_INFO];
2106 info.buf = info.start;
2107 info.left = dwarf_sections->size[DEBUG_INFO];
2108 info.is_bigendian = is_bigendian;
2109 info.error_callback = error_callback;
2110 info.data = data;
2111 info.reported_underflow = 0;
2113 memset (&units, 0, sizeof units);
2114 units_count = 0;
2116 while (info.left > 0)
2118 const unsigned char *unit_data_start;
2119 uint64_t len;
2120 int is_dwarf64;
2121 struct dwarf_buf unit_buf;
2122 int version;
2123 int unit_type;
2124 uint64_t abbrev_offset;
2125 int addrsize;
2126 struct unit *u;
2127 enum dwarf_tag unit_tag;
2129 if (info.reported_underflow)
2130 goto fail;
2132 unit_data_start = info.buf;
2134 len = read_initial_length (&info, &is_dwarf64);
2135 unit_buf = info;
2136 unit_buf.left = len;
2138 if (!advance (&info, len))
2139 goto fail;
2141 version = read_uint16 (&unit_buf);
2142 if (version < 2 || version > 5)
2144 dwarf_buf_error (&unit_buf, "unrecognized DWARF version");
2145 goto fail;
2148 if (version < 5)
2149 unit_type = 0;
2150 else
2152 unit_type = read_byte (&unit_buf);
2153 if (unit_type == DW_UT_type || unit_type == DW_UT_split_type)
2155 /* This unit doesn't have anything we need. */
2156 continue;
2160 pu = ((struct unit **)
2161 backtrace_vector_grow (state, sizeof (struct unit *),
2162 error_callback, data, &units));
2163 if (pu == NULL)
2164 goto fail;
2166 u = ((struct unit *)
2167 backtrace_alloc (state, sizeof *u, error_callback, data));
2168 if (u == NULL)
2169 goto fail;
2171 *pu = u;
2172 ++units_count;
2174 if (version < 5)
2175 addrsize = 0; /* Set below. */
2176 else
2177 addrsize = read_byte (&unit_buf);
2179 memset (&u->abbrevs, 0, sizeof u->abbrevs);
2180 abbrev_offset = read_offset (&unit_buf, is_dwarf64);
2181 if (!read_abbrevs (state, abbrev_offset,
2182 dwarf_sections->data[DEBUG_ABBREV],
2183 dwarf_sections->size[DEBUG_ABBREV],
2184 is_bigendian, error_callback, data, &u->abbrevs))
2185 goto fail;
2187 if (version < 5)
2188 addrsize = read_byte (&unit_buf);
2190 switch (unit_type)
2192 case 0:
2193 break;
2194 case DW_UT_compile: case DW_UT_partial:
2195 break;
2196 case DW_UT_skeleton: case DW_UT_split_compile:
2197 read_uint64 (&unit_buf); /* dwo_id */
2198 break;
2199 default:
2200 break;
2203 u->low_offset = unit_offset;
2204 unit_offset += len + (is_dwarf64 ? 12 : 4);
2205 u->high_offset = unit_offset;
2206 u->unit_data = unit_buf.buf;
2207 u->unit_data_len = unit_buf.left;
2208 u->unit_data_offset = unit_buf.buf - unit_data_start;
2209 u->version = version;
2210 u->is_dwarf64 = is_dwarf64;
2211 u->addrsize = addrsize;
2212 u->filename = NULL;
2213 u->comp_dir = NULL;
2214 u->abs_filename = NULL;
2215 u->lineoff = 0;
2217 /* The actual line number mappings will be read as needed. */
2218 u->lines = NULL;
2219 u->lines_count = 0;
2220 u->function_addrs = NULL;
2221 u->function_addrs_count = 0;
2223 if (!find_address_ranges (state, base_address, &unit_buf, dwarf_sections,
2224 is_bigendian, altlink, error_callback, data,
2225 u, addrs, &unit_tag))
2226 goto fail;
2228 if (unit_buf.reported_underflow)
2229 goto fail;
2231 if (info.reported_underflow)
2232 goto fail;
2234 unit_vec->vec = units;
2235 unit_vec->count = units_count;
2236 return 1;
2238 fail:
2239 if (units_count > 0)
2241 pu = (struct unit **) units.base;
2242 for (i = 0; i < units_count; i++)
2244 free_abbrevs (state, &pu[i]->abbrevs, error_callback, data);
2245 backtrace_free (state, pu[i], sizeof **pu, error_callback, data);
2247 backtrace_vector_free (state, &units, error_callback, data);
2249 if (addrs->count > 0)
2251 backtrace_vector_free (state, &addrs->vec, error_callback, data);
2252 addrs->count = 0;
2254 return 0;
2257 /* Add a new mapping to the vector of line mappings that we are
2258 building. Returns 1 on success, 0 on failure. */
2260 static int
2261 add_line (struct backtrace_state *state, struct dwarf_data *ddata,
2262 uintptr_t pc, const char *filename, int lineno,
2263 backtrace_error_callback error_callback, void *data,
2264 struct line_vector *vec)
2266 struct line *ln;
2268 /* If we are adding the same mapping, ignore it. This can happen
2269 when using discriminators. */
2270 if (vec->count > 0)
2272 ln = (struct line *) vec->vec.base + (vec->count - 1);
2273 if (pc == ln->pc && filename == ln->filename && lineno == ln->lineno)
2274 return 1;
2277 ln = ((struct line *)
2278 backtrace_vector_grow (state, sizeof (struct line), error_callback,
2279 data, &vec->vec));
2280 if (ln == NULL)
2281 return 0;
2283 /* Add in the base address here, so that we can look up the PC
2284 directly. */
2285 ln->pc = pc + ddata->base_address;
2287 ln->filename = filename;
2288 ln->lineno = lineno;
2289 ln->idx = vec->count;
2291 ++vec->count;
2293 return 1;
2296 /* Free the line header information. */
2298 static void
2299 free_line_header (struct backtrace_state *state, struct line_header *hdr,
2300 backtrace_error_callback error_callback, void *data)
2302 if (hdr->dirs_count != 0)
2303 backtrace_free (state, hdr->dirs, hdr->dirs_count * sizeof (const char *),
2304 error_callback, data);
2305 backtrace_free (state, hdr->filenames,
2306 hdr->filenames_count * sizeof (char *),
2307 error_callback, data);
2310 /* Read the directories and file names for a line header for version
2311 2, setting fields in HDR. Return 1 on success, 0 on failure. */
2313 static int
2314 read_v2_paths (struct backtrace_state *state, struct unit *u,
2315 struct dwarf_buf *hdr_buf, struct line_header *hdr)
2317 const unsigned char *p;
2318 const unsigned char *pend;
2319 size_t i;
2321 /* Count the number of directory entries. */
2322 hdr->dirs_count = 0;
2323 p = hdr_buf->buf;
2324 pend = p + hdr_buf->left;
2325 while (p < pend && *p != '\0')
2327 p += strnlen((const char *) p, pend - p) + 1;
2328 ++hdr->dirs_count;
2331 hdr->dirs = NULL;
2332 if (hdr->dirs_count != 0)
2334 hdr->dirs = ((const char **)
2335 backtrace_alloc (state,
2336 hdr->dirs_count * sizeof (const char *),
2337 hdr_buf->error_callback,
2338 hdr_buf->data));
2339 if (hdr->dirs == NULL)
2340 return 0;
2343 i = 0;
2344 while (*hdr_buf->buf != '\0')
2346 if (hdr_buf->reported_underflow)
2347 return 0;
2349 hdr->dirs[i] = read_string (hdr_buf);
2350 if (hdr->dirs[i] == NULL)
2351 return 0;
2352 ++i;
2354 if (!advance (hdr_buf, 1))
2355 return 0;
2357 /* Count the number of file entries. */
2358 hdr->filenames_count = 0;
2359 p = hdr_buf->buf;
2360 pend = p + hdr_buf->left;
2361 while (p < pend && *p != '\0')
2363 p += strnlen ((const char *) p, pend - p) + 1;
2364 p += leb128_len (p);
2365 p += leb128_len (p);
2366 p += leb128_len (p);
2367 ++hdr->filenames_count;
2370 hdr->filenames = ((const char **)
2371 backtrace_alloc (state,
2372 hdr->filenames_count * sizeof (char *),
2373 hdr_buf->error_callback,
2374 hdr_buf->data));
2375 if (hdr->filenames == NULL)
2376 return 0;
2377 i = 0;
2378 while (*hdr_buf->buf != '\0')
2380 const char *filename;
2381 uint64_t dir_index;
2383 if (hdr_buf->reported_underflow)
2384 return 0;
2386 filename = read_string (hdr_buf);
2387 if (filename == NULL)
2388 return 0;
2389 dir_index = read_uleb128 (hdr_buf);
2390 if (IS_ABSOLUTE_PATH (filename)
2391 || (dir_index == 0 && u->comp_dir == NULL))
2392 hdr->filenames[i] = filename;
2393 else
2395 const char *dir;
2396 size_t dir_len;
2397 size_t filename_len;
2398 char *s;
2400 if (dir_index == 0)
2401 dir = u->comp_dir;
2402 else if (dir_index - 1 < hdr->dirs_count)
2403 dir = hdr->dirs[dir_index - 1];
2404 else
2406 dwarf_buf_error (hdr_buf,
2407 ("invalid directory index in "
2408 "line number program header"));
2409 return 0;
2411 dir_len = strlen (dir);
2412 filename_len = strlen (filename);
2413 s = ((char *) backtrace_alloc (state, dir_len + filename_len + 2,
2414 hdr_buf->error_callback,
2415 hdr_buf->data));
2416 if (s == NULL)
2417 return 0;
2418 memcpy (s, dir, dir_len);
2419 /* FIXME: If we are on a DOS-based file system, and the
2420 directory or the file name use backslashes, then we
2421 should use a backslash here. */
2422 s[dir_len] = '/';
2423 memcpy (s + dir_len + 1, filename, filename_len + 1);
2424 hdr->filenames[i] = s;
2427 /* Ignore the modification time and size. */
2428 read_uleb128 (hdr_buf);
2429 read_uleb128 (hdr_buf);
2431 ++i;
2434 return 1;
2437 /* Read a single version 5 LNCT entry for a directory or file name in a
2438 line header. Sets *STRING to the resulting name, ignoring other
2439 data. Return 1 on success, 0 on failure. */
2441 static int
2442 read_lnct (struct backtrace_state *state, struct dwarf_data *ddata,
2443 struct unit *u, struct dwarf_buf *hdr_buf,
2444 const struct line_header *hdr, size_t formats_count,
2445 const struct line_header_format *formats, const char **string)
2447 size_t i;
2448 const char *dir;
2449 const char *path;
2451 dir = NULL;
2452 path = NULL;
2453 for (i = 0; i < formats_count; i++)
2455 struct attr_val val;
2457 if (!read_attribute (formats[i].form, 0, hdr_buf, u->is_dwarf64,
2458 u->version, hdr->addrsize, &ddata->dwarf_sections,
2459 ddata->altlink, &val))
2460 return 0;
2461 switch (formats[i].lnct)
2463 case DW_LNCT_path:
2464 if (!resolve_string (&ddata->dwarf_sections, u->is_dwarf64,
2465 ddata->is_bigendian, u->str_offsets_base,
2466 &val, hdr_buf->error_callback, hdr_buf->data,
2467 &path))
2468 return 0;
2469 break;
2470 case DW_LNCT_directory_index:
2471 if (val.encoding == ATTR_VAL_UINT)
2473 if (val.u.uint >= hdr->dirs_count)
2475 dwarf_buf_error (hdr_buf,
2476 ("invalid directory index in "
2477 "line number program header"));
2478 return 0;
2480 dir = hdr->dirs[val.u.uint];
2482 break;
2483 default:
2484 /* We don't care about timestamps or sizes or hashes. */
2485 break;
2489 if (path == NULL)
2491 dwarf_buf_error (hdr_buf,
2492 "missing file name in line number program header");
2493 return 0;
2496 if (dir == NULL)
2497 *string = path;
2498 else
2500 size_t dir_len;
2501 size_t path_len;
2502 char *s;
2504 dir_len = strlen (dir);
2505 path_len = strlen (path);
2506 s = (char *) backtrace_alloc (state, dir_len + path_len + 2,
2507 hdr_buf->error_callback, hdr_buf->data);
2508 if (s == NULL)
2509 return 0;
2510 memcpy (s, dir, dir_len);
2511 /* FIXME: If we are on a DOS-based file system, and the
2512 directory or the path name use backslashes, then we should
2513 use a backslash here. */
2514 s[dir_len] = '/';
2515 memcpy (s + dir_len + 1, path, path_len + 1);
2516 *string = s;
2519 return 1;
2522 /* Read a set of DWARF 5 line header format entries, setting *PCOUNT
2523 and *PPATHS. Return 1 on success, 0 on failure. */
2525 static int
2526 read_line_header_format_entries (struct backtrace_state *state,
2527 struct dwarf_data *ddata,
2528 struct unit *u,
2529 struct dwarf_buf *hdr_buf,
2530 struct line_header *hdr,
2531 size_t *pcount,
2532 const char ***ppaths)
2534 size_t formats_count;
2535 struct line_header_format *formats;
2536 size_t paths_count;
2537 const char **paths;
2538 size_t i;
2539 int ret;
2541 formats_count = read_byte (hdr_buf);
2542 if (formats_count == 0)
2543 formats = NULL;
2544 else
2546 formats = ((struct line_header_format *)
2547 backtrace_alloc (state,
2548 (formats_count
2549 * sizeof (struct line_header_format)),
2550 hdr_buf->error_callback,
2551 hdr_buf->data));
2552 if (formats == NULL)
2553 return 0;
2555 for (i = 0; i < formats_count; i++)
2557 formats[i].lnct = (int) read_uleb128(hdr_buf);
2558 formats[i].form = (enum dwarf_form) read_uleb128 (hdr_buf);
2562 paths_count = read_uleb128 (hdr_buf);
2563 if (paths_count == 0)
2565 *pcount = 0;
2566 *ppaths = NULL;
2567 ret = 1;
2568 goto exit;
2571 paths = ((const char **)
2572 backtrace_alloc (state, paths_count * sizeof (const char *),
2573 hdr_buf->error_callback, hdr_buf->data));
2574 if (paths == NULL)
2576 ret = 0;
2577 goto exit;
2579 for (i = 0; i < paths_count; i++)
2581 if (!read_lnct (state, ddata, u, hdr_buf, hdr, formats_count,
2582 formats, &paths[i]))
2584 backtrace_free (state, paths,
2585 paths_count * sizeof (const char *),
2586 hdr_buf->error_callback, hdr_buf->data);
2587 ret = 0;
2588 goto exit;
2592 *pcount = paths_count;
2593 *ppaths = paths;
2595 ret = 1;
2597 exit:
2598 if (formats != NULL)
2599 backtrace_free (state, formats,
2600 formats_count * sizeof (struct line_header_format),
2601 hdr_buf->error_callback, hdr_buf->data);
2603 return ret;
2606 /* Read the line header. Return 1 on success, 0 on failure. */
2608 static int
2609 read_line_header (struct backtrace_state *state, struct dwarf_data *ddata,
2610 struct unit *u, int is_dwarf64, struct dwarf_buf *line_buf,
2611 struct line_header *hdr)
2613 uint64_t hdrlen;
2614 struct dwarf_buf hdr_buf;
2616 hdr->version = read_uint16 (line_buf);
2617 if (hdr->version < 2 || hdr->version > 5)
2619 dwarf_buf_error (line_buf, "unsupported line number version");
2620 return 0;
2623 if (hdr->version < 5)
2624 hdr->addrsize = u->addrsize;
2625 else
2627 hdr->addrsize = read_byte (line_buf);
2628 /* We could support a non-zero segment_selector_size but I doubt
2629 we'll ever see it. */
2630 if (read_byte (line_buf) != 0)
2632 dwarf_buf_error (line_buf,
2633 "non-zero segment_selector_size not supported");
2634 return 0;
2638 hdrlen = read_offset (line_buf, is_dwarf64);
2640 hdr_buf = *line_buf;
2641 hdr_buf.left = hdrlen;
2643 if (!advance (line_buf, hdrlen))
2644 return 0;
2646 hdr->min_insn_len = read_byte (&hdr_buf);
2647 if (hdr->version < 4)
2648 hdr->max_ops_per_insn = 1;
2649 else
2650 hdr->max_ops_per_insn = read_byte (&hdr_buf);
2652 /* We don't care about default_is_stmt. */
2653 read_byte (&hdr_buf);
2655 hdr->line_base = read_sbyte (&hdr_buf);
2656 hdr->line_range = read_byte (&hdr_buf);
2658 hdr->opcode_base = read_byte (&hdr_buf);
2659 hdr->opcode_lengths = hdr_buf.buf;
2660 if (!advance (&hdr_buf, hdr->opcode_base - 1))
2661 return 0;
2663 if (hdr->version < 5)
2665 if (!read_v2_paths (state, u, &hdr_buf, hdr))
2666 return 0;
2668 else
2670 if (!read_line_header_format_entries (state, ddata, u, &hdr_buf, hdr,
2671 &hdr->dirs_count,
2672 &hdr->dirs))
2673 return 0;
2674 if (!read_line_header_format_entries (state, ddata, u, &hdr_buf, hdr,
2675 &hdr->filenames_count,
2676 &hdr->filenames))
2677 return 0;
2680 if (hdr_buf.reported_underflow)
2681 return 0;
2683 return 1;
2686 /* Read the line program, adding line mappings to VEC. Return 1 on
2687 success, 0 on failure. */
2689 static int
2690 read_line_program (struct backtrace_state *state, struct dwarf_data *ddata,
2691 struct unit *u, const struct line_header *hdr,
2692 struct dwarf_buf *line_buf, struct line_vector *vec)
2694 uint64_t address;
2695 unsigned int op_index;
2696 const char *reset_filename;
2697 const char *filename;
2698 int lineno;
2700 address = 0;
2701 op_index = 0;
2702 if (hdr->filenames_count > 0)
2703 reset_filename = hdr->filenames[0];
2704 else
2705 reset_filename = "";
2706 filename = reset_filename;
2707 lineno = 1;
2708 while (line_buf->left > 0)
2710 unsigned int op;
2712 op = read_byte (line_buf);
2713 if (op >= hdr->opcode_base)
2715 unsigned int advance;
2717 /* Special opcode. */
2718 op -= hdr->opcode_base;
2719 advance = op / hdr->line_range;
2720 address += (hdr->min_insn_len * (op_index + advance)
2721 / hdr->max_ops_per_insn);
2722 op_index = (op_index + advance) % hdr->max_ops_per_insn;
2723 lineno += hdr->line_base + (int) (op % hdr->line_range);
2724 add_line (state, ddata, address, filename, lineno,
2725 line_buf->error_callback, line_buf->data, vec);
2727 else if (op == DW_LNS_extended_op)
2729 uint64_t len;
2731 len = read_uleb128 (line_buf);
2732 op = read_byte (line_buf);
2733 switch (op)
2735 case DW_LNE_end_sequence:
2736 /* FIXME: Should we mark the high PC here? It seems
2737 that we already have that information from the
2738 compilation unit. */
2739 address = 0;
2740 op_index = 0;
2741 filename = reset_filename;
2742 lineno = 1;
2743 break;
2744 case DW_LNE_set_address:
2745 address = read_address (line_buf, hdr->addrsize);
2746 break;
2747 case DW_LNE_define_file:
2749 const char *f;
2750 unsigned int dir_index;
2752 f = read_string (line_buf);
2753 if (f == NULL)
2754 return 0;
2755 dir_index = read_uleb128 (line_buf);
2756 /* Ignore that time and length. */
2757 read_uleb128 (line_buf);
2758 read_uleb128 (line_buf);
2759 if (IS_ABSOLUTE_PATH (f))
2760 filename = f;
2761 else
2763 const char *dir;
2764 size_t dir_len;
2765 size_t f_len;
2766 char *p;
2768 if (dir_index == 0 && hdr->version < 5)
2769 dir = u->comp_dir;
2770 else if (dir_index - 1 < hdr->dirs_count)
2771 dir = hdr->dirs[dir_index - 1];
2772 else
2774 dwarf_buf_error (line_buf,
2775 ("invalid directory index "
2776 "in line number program"));
2777 return 0;
2779 dir_len = strlen (dir);
2780 f_len = strlen (f);
2781 p = ((char *)
2782 backtrace_alloc (state, dir_len + f_len + 2,
2783 line_buf->error_callback,
2784 line_buf->data));
2785 if (p == NULL)
2786 return 0;
2787 memcpy (p, dir, dir_len);
2788 /* FIXME: If we are on a DOS-based file system,
2789 and the directory or the file name use
2790 backslashes, then we should use a backslash
2791 here. */
2792 p[dir_len] = '/';
2793 memcpy (p + dir_len + 1, f, f_len + 1);
2794 filename = p;
2797 break;
2798 case DW_LNE_set_discriminator:
2799 /* We don't care about discriminators. */
2800 read_uleb128 (line_buf);
2801 break;
2802 default:
2803 if (!advance (line_buf, len - 1))
2804 return 0;
2805 break;
2808 else
2810 switch (op)
2812 case DW_LNS_copy:
2813 add_line (state, ddata, address, filename, lineno,
2814 line_buf->error_callback, line_buf->data, vec);
2815 break;
2816 case DW_LNS_advance_pc:
2818 uint64_t advance;
2820 advance = read_uleb128 (line_buf);
2821 address += (hdr->min_insn_len * (op_index + advance)
2822 / hdr->max_ops_per_insn);
2823 op_index = (op_index + advance) % hdr->max_ops_per_insn;
2825 break;
2826 case DW_LNS_advance_line:
2827 lineno += (int) read_sleb128 (line_buf);
2828 break;
2829 case DW_LNS_set_file:
2831 uint64_t fileno;
2833 fileno = read_uleb128 (line_buf);
2834 if (fileno == 0)
2835 filename = "";
2836 else
2838 if (fileno - 1 >= hdr->filenames_count)
2840 dwarf_buf_error (line_buf,
2841 ("invalid file number in "
2842 "line number program"));
2843 return 0;
2845 filename = hdr->filenames[fileno - 1];
2848 break;
2849 case DW_LNS_set_column:
2850 read_uleb128 (line_buf);
2851 break;
2852 case DW_LNS_negate_stmt:
2853 break;
2854 case DW_LNS_set_basic_block:
2855 break;
2856 case DW_LNS_const_add_pc:
2858 unsigned int advance;
2860 op = 255 - hdr->opcode_base;
2861 advance = op / hdr->line_range;
2862 address += (hdr->min_insn_len * (op_index + advance)
2863 / hdr->max_ops_per_insn);
2864 op_index = (op_index + advance) % hdr->max_ops_per_insn;
2866 break;
2867 case DW_LNS_fixed_advance_pc:
2868 address += read_uint16 (line_buf);
2869 op_index = 0;
2870 break;
2871 case DW_LNS_set_prologue_end:
2872 break;
2873 case DW_LNS_set_epilogue_begin:
2874 break;
2875 case DW_LNS_set_isa:
2876 read_uleb128 (line_buf);
2877 break;
2878 default:
2880 unsigned int i;
2882 for (i = hdr->opcode_lengths[op - 1]; i > 0; --i)
2883 read_uleb128 (line_buf);
2885 break;
2890 return 1;
2893 /* Read the line number information for a compilation unit. Returns 1
2894 on success, 0 on failure. */
2896 static int
2897 read_line_info (struct backtrace_state *state, struct dwarf_data *ddata,
2898 backtrace_error_callback error_callback, void *data,
2899 struct unit *u, struct line_header *hdr, struct line **lines,
2900 size_t *lines_count)
2902 struct line_vector vec;
2903 struct dwarf_buf line_buf;
2904 uint64_t len;
2905 int is_dwarf64;
2906 struct line *ln;
2908 memset (&vec.vec, 0, sizeof vec.vec);
2909 vec.count = 0;
2911 memset (hdr, 0, sizeof *hdr);
2913 if (u->lineoff != (off_t) (size_t) u->lineoff
2914 || (size_t) u->lineoff >= ddata->dwarf_sections.size[DEBUG_LINE])
2916 error_callback (data, "unit line offset out of range", 0);
2917 goto fail;
2920 line_buf.name = ".debug_line";
2921 line_buf.start = ddata->dwarf_sections.data[DEBUG_LINE];
2922 line_buf.buf = ddata->dwarf_sections.data[DEBUG_LINE] + u->lineoff;
2923 line_buf.left = ddata->dwarf_sections.size[DEBUG_LINE] - u->lineoff;
2924 line_buf.is_bigendian = ddata->is_bigendian;
2925 line_buf.error_callback = error_callback;
2926 line_buf.data = data;
2927 line_buf.reported_underflow = 0;
2929 len = read_initial_length (&line_buf, &is_dwarf64);
2930 line_buf.left = len;
2932 if (!read_line_header (state, ddata, u, is_dwarf64, &line_buf, hdr))
2933 goto fail;
2935 if (!read_line_program (state, ddata, u, hdr, &line_buf, &vec))
2936 goto fail;
2938 if (line_buf.reported_underflow)
2939 goto fail;
2941 if (vec.count == 0)
2943 /* This is not a failure in the sense of a generating an error,
2944 but it is a failure in that sense that we have no useful
2945 information. */
2946 goto fail;
2949 /* Allocate one extra entry at the end. */
2950 ln = ((struct line *)
2951 backtrace_vector_grow (state, sizeof (struct line), error_callback,
2952 data, &vec.vec));
2953 if (ln == NULL)
2954 goto fail;
2955 ln->pc = (uintptr_t) -1;
2956 ln->filename = NULL;
2957 ln->lineno = 0;
2958 ln->idx = 0;
2960 if (!backtrace_vector_release (state, &vec.vec, error_callback, data))
2961 goto fail;
2963 ln = (struct line *) vec.vec.base;
2964 backtrace_qsort (ln, vec.count, sizeof (struct line), line_compare);
2966 *lines = ln;
2967 *lines_count = vec.count;
2969 return 1;
2971 fail:
2972 backtrace_vector_free (state, &vec.vec, error_callback, data);
2973 free_line_header (state, hdr, error_callback, data);
2974 *lines = (struct line *) (uintptr_t) -1;
2975 *lines_count = 0;
2976 return 0;
2979 static const char *read_referenced_name (struct dwarf_data *, struct unit *,
2980 uint64_t, backtrace_error_callback,
2981 void *);
2983 /* Read the name of a function from a DIE referenced by ATTR with VAL. */
2985 static const char *
2986 read_referenced_name_from_attr (struct dwarf_data *ddata, struct unit *u,
2987 struct attr *attr, struct attr_val *val,
2988 backtrace_error_callback error_callback,
2989 void *data)
2991 switch (attr->name)
2993 case DW_AT_abstract_origin:
2994 case DW_AT_specification:
2995 break;
2996 default:
2997 return NULL;
3000 if (attr->form == DW_FORM_ref_sig8)
3001 return NULL;
3003 if (val->encoding == ATTR_VAL_REF_INFO)
3005 struct unit *unit
3006 = find_unit (ddata->units, ddata->units_count,
3007 val->u.uint);
3008 if (unit == NULL)
3009 return NULL;
3011 uint64_t offset = val->u.uint - unit->low_offset;
3012 return read_referenced_name (ddata, unit, offset, error_callback, data);
3015 if (val->encoding == ATTR_VAL_UINT
3016 || val->encoding == ATTR_VAL_REF_UNIT)
3017 return read_referenced_name (ddata, u, val->u.uint, error_callback, data);
3019 if (val->encoding == ATTR_VAL_REF_ALT_INFO)
3021 struct unit *alt_unit
3022 = find_unit (ddata->altlink->units, ddata->altlink->units_count,
3023 val->u.uint);
3024 if (alt_unit == NULL)
3025 return NULL;
3027 uint64_t offset = val->u.uint - alt_unit->low_offset;
3028 return read_referenced_name (ddata->altlink, alt_unit, offset,
3029 error_callback, data);
3032 return NULL;
3035 /* Read the name of a function from a DIE referenced by a
3036 DW_AT_abstract_origin or DW_AT_specification tag. OFFSET is within
3037 the same compilation unit. */
3039 static const char *
3040 read_referenced_name (struct dwarf_data *ddata, struct unit *u,
3041 uint64_t offset, backtrace_error_callback error_callback,
3042 void *data)
3044 struct dwarf_buf unit_buf;
3045 uint64_t code;
3046 const struct abbrev *abbrev;
3047 const char *ret;
3048 size_t i;
3050 /* OFFSET is from the start of the data for this compilation unit.
3051 U->unit_data is the data, but it starts U->unit_data_offset bytes
3052 from the beginning. */
3054 if (offset < u->unit_data_offset
3055 || offset - u->unit_data_offset >= u->unit_data_len)
3057 error_callback (data,
3058 "abstract origin or specification out of range",
3060 return NULL;
3063 offset -= u->unit_data_offset;
3065 unit_buf.name = ".debug_info";
3066 unit_buf.start = ddata->dwarf_sections.data[DEBUG_INFO];
3067 unit_buf.buf = u->unit_data + offset;
3068 unit_buf.left = u->unit_data_len - offset;
3069 unit_buf.is_bigendian = ddata->is_bigendian;
3070 unit_buf.error_callback = error_callback;
3071 unit_buf.data = data;
3072 unit_buf.reported_underflow = 0;
3074 code = read_uleb128 (&unit_buf);
3075 if (code == 0)
3077 dwarf_buf_error (&unit_buf, "invalid abstract origin or specification");
3078 return NULL;
3081 abbrev = lookup_abbrev (&u->abbrevs, code, error_callback, data);
3082 if (abbrev == NULL)
3083 return NULL;
3085 ret = NULL;
3086 for (i = 0; i < abbrev->num_attrs; ++i)
3088 struct attr_val val;
3090 if (!read_attribute (abbrev->attrs[i].form, abbrev->attrs[i].val,
3091 &unit_buf, u->is_dwarf64, u->version, u->addrsize,
3092 &ddata->dwarf_sections, ddata->altlink, &val))
3093 return NULL;
3095 switch (abbrev->attrs[i].name)
3097 case DW_AT_name:
3098 /* Third name preference: don't override. A name we found in some
3099 other way, will normally be more useful -- e.g., this name is
3100 normally not mangled. */
3101 if (ret != NULL)
3102 break;
3103 if (!resolve_string (&ddata->dwarf_sections, u->is_dwarf64,
3104 ddata->is_bigendian, u->str_offsets_base,
3105 &val, error_callback, data, &ret))
3106 return NULL;
3107 break;
3109 case DW_AT_linkage_name:
3110 case DW_AT_MIPS_linkage_name:
3111 /* First name preference: override all. */
3113 const char *s;
3115 s = NULL;
3116 if (!resolve_string (&ddata->dwarf_sections, u->is_dwarf64,
3117 ddata->is_bigendian, u->str_offsets_base,
3118 &val, error_callback, data, &s))
3119 return NULL;
3120 if (s != NULL)
3121 return s;
3123 break;
3125 case DW_AT_specification:
3126 /* Second name preference: override DW_AT_name, don't override
3127 DW_AT_linkage_name. */
3129 const char *name;
3131 name = read_referenced_name_from_attr (ddata, u, &abbrev->attrs[i],
3132 &val, error_callback, data);
3133 if (name != NULL)
3134 ret = name;
3136 break;
3138 default:
3139 break;
3143 return ret;
3146 /* Add a range to a unit that maps to a function. This is called via
3147 add_ranges. Returns 1 on success, 0 on error. */
3149 static int
3150 add_function_range (struct backtrace_state *state, void *rdata,
3151 uint64_t lowpc, uint64_t highpc,
3152 backtrace_error_callback error_callback, void *data,
3153 void *pvec)
3155 struct function *function = (struct function *) rdata;
3156 struct function_vector *vec = (struct function_vector *) pvec;
3157 struct function_addrs *p;
3159 if (vec->count > 0)
3161 p = (struct function_addrs *) vec->vec.base + (vec->count - 1);
3162 if ((lowpc == p->high || lowpc == p->high + 1)
3163 && function == p->function)
3165 if (highpc > p->high)
3166 p->high = highpc;
3167 return 1;
3171 p = ((struct function_addrs *)
3172 backtrace_vector_grow (state, sizeof (struct function_addrs),
3173 error_callback, data, &vec->vec));
3174 if (p == NULL)
3175 return 0;
3177 p->low = lowpc;
3178 p->high = highpc;
3179 p->function = function;
3181 ++vec->count;
3183 return 1;
3186 /* Read one entry plus all its children. Add function addresses to
3187 VEC. Returns 1 on success, 0 on error. */
3189 static int
3190 read_function_entry (struct backtrace_state *state, struct dwarf_data *ddata,
3191 struct unit *u, uint64_t base, struct dwarf_buf *unit_buf,
3192 const struct line_header *lhdr,
3193 backtrace_error_callback error_callback, void *data,
3194 struct function_vector *vec_function,
3195 struct function_vector *vec_inlined)
3197 while (unit_buf->left > 0)
3199 uint64_t code;
3200 const struct abbrev *abbrev;
3201 int is_function;
3202 struct function *function;
3203 struct function_vector *vec;
3204 size_t i;
3205 struct pcrange pcrange;
3206 int have_linkage_name;
3208 code = read_uleb128 (unit_buf);
3209 if (code == 0)
3210 return 1;
3212 abbrev = lookup_abbrev (&u->abbrevs, code, error_callback, data);
3213 if (abbrev == NULL)
3214 return 0;
3216 is_function = (abbrev->tag == DW_TAG_subprogram
3217 || abbrev->tag == DW_TAG_entry_point
3218 || abbrev->tag == DW_TAG_inlined_subroutine);
3220 if (abbrev->tag == DW_TAG_inlined_subroutine)
3221 vec = vec_inlined;
3222 else
3223 vec = vec_function;
3225 function = NULL;
3226 if (is_function)
3228 function = ((struct function *)
3229 backtrace_alloc (state, sizeof *function,
3230 error_callback, data));
3231 if (function == NULL)
3232 return 0;
3233 memset (function, 0, sizeof *function);
3236 memset (&pcrange, 0, sizeof pcrange);
3237 have_linkage_name = 0;
3238 for (i = 0; i < abbrev->num_attrs; ++i)
3240 struct attr_val val;
3242 if (!read_attribute (abbrev->attrs[i].form, abbrev->attrs[i].val,
3243 unit_buf, u->is_dwarf64, u->version,
3244 u->addrsize, &ddata->dwarf_sections,
3245 ddata->altlink, &val))
3246 return 0;
3248 /* The compile unit sets the base address for any address
3249 ranges in the function entries. */
3250 if (abbrev->tag == DW_TAG_compile_unit
3251 && abbrev->attrs[i].name == DW_AT_low_pc)
3253 if (val.encoding == ATTR_VAL_ADDRESS)
3254 base = val.u.uint;
3255 else if (val.encoding == ATTR_VAL_ADDRESS_INDEX)
3257 if (!resolve_addr_index (&ddata->dwarf_sections,
3258 u->addr_base, u->addrsize,
3259 ddata->is_bigendian, val.u.uint,
3260 error_callback, data, &base))
3261 return 0;
3265 if (is_function)
3267 switch (abbrev->attrs[i].name)
3269 case DW_AT_call_file:
3270 if (val.encoding == ATTR_VAL_UINT)
3272 if (val.u.uint == 0)
3273 function->caller_filename = "";
3274 else
3276 if (val.u.uint - 1 >= lhdr->filenames_count)
3278 dwarf_buf_error (unit_buf,
3279 ("invalid file number in "
3280 "DW_AT_call_file attribute"));
3281 return 0;
3283 function->caller_filename =
3284 lhdr->filenames[val.u.uint - 1];
3287 break;
3289 case DW_AT_call_line:
3290 if (val.encoding == ATTR_VAL_UINT)
3291 function->caller_lineno = val.u.uint;
3292 break;
3294 case DW_AT_abstract_origin:
3295 case DW_AT_specification:
3296 /* Second name preference: override DW_AT_name, don't override
3297 DW_AT_linkage_name. */
3298 if (have_linkage_name)
3299 break;
3301 const char *name;
3303 name
3304 = read_referenced_name_from_attr (ddata, u,
3305 &abbrev->attrs[i], &val,
3306 error_callback, data);
3307 if (name != NULL)
3308 function->name = name;
3310 break;
3312 case DW_AT_name:
3313 /* Third name preference: don't override. */
3314 if (function->name != NULL)
3315 break;
3316 if (!resolve_string (&ddata->dwarf_sections, u->is_dwarf64,
3317 ddata->is_bigendian,
3318 u->str_offsets_base, &val,
3319 error_callback, data, &function->name))
3320 return 0;
3321 break;
3323 case DW_AT_linkage_name:
3324 case DW_AT_MIPS_linkage_name:
3325 /* First name preference: override all. */
3327 const char *s;
3329 s = NULL;
3330 if (!resolve_string (&ddata->dwarf_sections, u->is_dwarf64,
3331 ddata->is_bigendian,
3332 u->str_offsets_base, &val,
3333 error_callback, data, &s))
3334 return 0;
3335 if (s != NULL)
3337 function->name = s;
3338 have_linkage_name = 1;
3341 break;
3343 case DW_AT_low_pc: case DW_AT_high_pc: case DW_AT_ranges:
3344 update_pcrange (&abbrev->attrs[i], &val, &pcrange);
3345 break;
3347 default:
3348 break;
3353 /* If we couldn't find a name for the function, we have no use
3354 for it. */
3355 if (is_function && function->name == NULL)
3357 backtrace_free (state, function, sizeof *function,
3358 error_callback, data);
3359 is_function = 0;
3362 if (is_function)
3364 if (pcrange.have_ranges
3365 || (pcrange.have_lowpc && pcrange.have_highpc))
3367 if (!add_ranges (state, &ddata->dwarf_sections,
3368 ddata->base_address, ddata->is_bigendian,
3369 u, base, &pcrange, add_function_range,
3370 (void *) function, error_callback, data,
3371 (void *) vec))
3372 return 0;
3374 else
3376 backtrace_free (state, function, sizeof *function,
3377 error_callback, data);
3378 is_function = 0;
3382 if (abbrev->has_children)
3384 if (!is_function)
3386 if (!read_function_entry (state, ddata, u, base, unit_buf, lhdr,
3387 error_callback, data, vec_function,
3388 vec_inlined))
3389 return 0;
3391 else
3393 struct function_vector fvec;
3395 /* Gather any information for inlined functions in
3396 FVEC. */
3398 memset (&fvec, 0, sizeof fvec);
3400 if (!read_function_entry (state, ddata, u, base, unit_buf, lhdr,
3401 error_callback, data, vec_function,
3402 &fvec))
3403 return 0;
3405 if (fvec.count > 0)
3407 struct function_addrs *faddrs;
3409 if (!backtrace_vector_release (state, &fvec.vec,
3410 error_callback, data))
3411 return 0;
3413 faddrs = (struct function_addrs *) fvec.vec.base;
3414 backtrace_qsort (faddrs, fvec.count,
3415 sizeof (struct function_addrs),
3416 function_addrs_compare);
3418 function->function_addrs = faddrs;
3419 function->function_addrs_count = fvec.count;
3425 return 1;
3428 /* Read function name information for a compilation unit. We look
3429 through the whole unit looking for function tags. */
3431 static void
3432 read_function_info (struct backtrace_state *state, struct dwarf_data *ddata,
3433 const struct line_header *lhdr,
3434 backtrace_error_callback error_callback, void *data,
3435 struct unit *u, struct function_vector *fvec,
3436 struct function_addrs **ret_addrs,
3437 size_t *ret_addrs_count)
3439 struct function_vector lvec;
3440 struct function_vector *pfvec;
3441 struct dwarf_buf unit_buf;
3442 struct function_addrs *addrs;
3443 size_t addrs_count;
3445 /* Use FVEC if it is not NULL. Otherwise use our own vector. */
3446 if (fvec != NULL)
3447 pfvec = fvec;
3448 else
3450 memset (&lvec, 0, sizeof lvec);
3451 pfvec = &lvec;
3454 unit_buf.name = ".debug_info";
3455 unit_buf.start = ddata->dwarf_sections.data[DEBUG_INFO];
3456 unit_buf.buf = u->unit_data;
3457 unit_buf.left = u->unit_data_len;
3458 unit_buf.is_bigendian = ddata->is_bigendian;
3459 unit_buf.error_callback = error_callback;
3460 unit_buf.data = data;
3461 unit_buf.reported_underflow = 0;
3463 while (unit_buf.left > 0)
3465 if (!read_function_entry (state, ddata, u, 0, &unit_buf, lhdr,
3466 error_callback, data, pfvec, pfvec))
3467 return;
3470 if (pfvec->count == 0)
3471 return;
3473 addrs_count = pfvec->count;
3475 if (fvec == NULL)
3477 if (!backtrace_vector_release (state, &lvec.vec, error_callback, data))
3478 return;
3479 addrs = (struct function_addrs *) pfvec->vec.base;
3481 else
3483 /* Finish this list of addresses, but leave the remaining space in
3484 the vector available for the next function unit. */
3485 addrs = ((struct function_addrs *)
3486 backtrace_vector_finish (state, &fvec->vec,
3487 error_callback, data));
3488 if (addrs == NULL)
3489 return;
3490 fvec->count = 0;
3493 backtrace_qsort (addrs, addrs_count, sizeof (struct function_addrs),
3494 function_addrs_compare);
3496 *ret_addrs = addrs;
3497 *ret_addrs_count = addrs_count;
3500 /* See if PC is inlined in FUNCTION. If it is, print out the inlined
3501 information, and update FILENAME and LINENO for the caller.
3502 Returns whatever CALLBACK returns, or 0 to keep going. */
3504 static int
3505 report_inlined_functions (uintptr_t pc, struct function *function,
3506 backtrace_full_callback callback, void *data,
3507 const char **filename, int *lineno)
3509 struct function_addrs *function_addrs;
3510 struct function *inlined;
3511 int ret;
3513 if (function->function_addrs_count == 0)
3514 return 0;
3516 function_addrs = ((struct function_addrs *)
3517 bsearch (&pc, function->function_addrs,
3518 function->function_addrs_count,
3519 sizeof (struct function_addrs),
3520 function_addrs_search));
3521 if (function_addrs == NULL)
3522 return 0;
3524 while (((size_t) (function_addrs - function->function_addrs) + 1
3525 < function->function_addrs_count)
3526 && pc >= (function_addrs + 1)->low
3527 && pc < (function_addrs + 1)->high)
3528 ++function_addrs;
3530 /* We found an inlined call. */
3532 inlined = function_addrs->function;
3534 /* Report any calls inlined into this one. */
3535 ret = report_inlined_functions (pc, inlined, callback, data,
3536 filename, lineno);
3537 if (ret != 0)
3538 return ret;
3540 /* Report this inlined call. */
3541 ret = callback (data, pc, *filename, *lineno, inlined->name);
3542 if (ret != 0)
3543 return ret;
3545 /* Our caller will report the caller of the inlined function; tell
3546 it the appropriate filename and line number. */
3547 *filename = inlined->caller_filename;
3548 *lineno = inlined->caller_lineno;
3550 return 0;
3553 /* Look for a PC in the DWARF mapping for one module. On success,
3554 call CALLBACK and return whatever it returns. On error, call
3555 ERROR_CALLBACK and return 0. Sets *FOUND to 1 if the PC is found,
3556 0 if not. */
3558 static int
3559 dwarf_lookup_pc (struct backtrace_state *state, struct dwarf_data *ddata,
3560 uintptr_t pc, backtrace_full_callback callback,
3561 backtrace_error_callback error_callback, void *data,
3562 int *found)
3564 struct unit_addrs *entry;
3565 struct unit *u;
3566 int new_data;
3567 struct line *lines;
3568 struct line *ln;
3569 struct function_addrs *function_addrs;
3570 struct function *function;
3571 const char *filename;
3572 int lineno;
3573 int ret;
3575 *found = 1;
3577 /* Find an address range that includes PC. */
3578 entry = (ddata->addrs_count == 0
3579 ? NULL
3580 : bsearch (&pc, ddata->addrs, ddata->addrs_count,
3581 sizeof (struct unit_addrs), unit_addrs_search));
3583 if (entry == NULL)
3585 *found = 0;
3586 return 0;
3589 /* If there are multiple ranges that contain PC, use the last one,
3590 in order to produce predictable results. If we assume that all
3591 ranges are properly nested, then the last range will be the
3592 smallest one. */
3593 while ((size_t) (entry - ddata->addrs) + 1 < ddata->addrs_count
3594 && pc >= (entry + 1)->low
3595 && pc < (entry + 1)->high)
3596 ++entry;
3598 /* We need the lines, lines_count, function_addrs,
3599 function_addrs_count fields of u. If they are not set, we need
3600 to set them. When running in threaded mode, we need to allow for
3601 the possibility that some other thread is setting them
3602 simultaneously. */
3604 u = entry->u;
3605 lines = u->lines;
3607 /* Skip units with no useful line number information by walking
3608 backward. Useless line number information is marked by setting
3609 lines == -1. */
3610 while (entry > ddata->addrs
3611 && pc >= (entry - 1)->low
3612 && pc < (entry - 1)->high)
3614 if (state->threaded)
3615 lines = (struct line *) backtrace_atomic_load_pointer (&u->lines);
3617 if (lines != (struct line *) (uintptr_t) -1)
3618 break;
3620 --entry;
3622 u = entry->u;
3623 lines = u->lines;
3626 if (state->threaded)
3627 lines = backtrace_atomic_load_pointer (&u->lines);
3629 new_data = 0;
3630 if (lines == NULL)
3632 size_t function_addrs_count;
3633 struct line_header lhdr;
3634 size_t count;
3636 /* We have never read the line information for this unit. Read
3637 it now. */
3639 function_addrs = NULL;
3640 function_addrs_count = 0;
3641 if (read_line_info (state, ddata, error_callback, data, entry->u, &lhdr,
3642 &lines, &count))
3644 struct function_vector *pfvec;
3646 /* If not threaded, reuse DDATA->FVEC for better memory
3647 consumption. */
3648 if (state->threaded)
3649 pfvec = NULL;
3650 else
3651 pfvec = &ddata->fvec;
3652 read_function_info (state, ddata, &lhdr, error_callback, data,
3653 entry->u, pfvec, &function_addrs,
3654 &function_addrs_count);
3655 free_line_header (state, &lhdr, error_callback, data);
3656 new_data = 1;
3659 /* Atomically store the information we just read into the unit.
3660 If another thread is simultaneously writing, it presumably
3661 read the same information, and we don't care which one we
3662 wind up with; we just leak the other one. We do have to
3663 write the lines field last, so that the acquire-loads above
3664 ensure that the other fields are set. */
3666 if (!state->threaded)
3668 u->lines_count = count;
3669 u->function_addrs = function_addrs;
3670 u->function_addrs_count = function_addrs_count;
3671 u->lines = lines;
3673 else
3675 backtrace_atomic_store_size_t (&u->lines_count, count);
3676 backtrace_atomic_store_pointer (&u->function_addrs, function_addrs);
3677 backtrace_atomic_store_size_t (&u->function_addrs_count,
3678 function_addrs_count);
3679 backtrace_atomic_store_pointer (&u->lines, lines);
3683 /* Now all fields of U have been initialized. */
3685 if (lines == (struct line *) (uintptr_t) -1)
3687 /* If reading the line number information failed in some way,
3688 try again to see if there is a better compilation unit for
3689 this PC. */
3690 if (new_data)
3691 return dwarf_lookup_pc (state, ddata, pc, callback, error_callback,
3692 data, found);
3693 return callback (data, pc, NULL, 0, NULL);
3696 /* Search for PC within this unit. */
3698 ln = (struct line *) bsearch (&pc, lines, entry->u->lines_count,
3699 sizeof (struct line), line_search);
3700 if (ln == NULL)
3702 /* The PC is between the low_pc and high_pc attributes of the
3703 compilation unit, but no entry in the line table covers it.
3704 This implies that the start of the compilation unit has no
3705 line number information. */
3707 if (entry->u->abs_filename == NULL)
3709 const char *filename;
3711 filename = entry->u->filename;
3712 if (filename != NULL
3713 && !IS_ABSOLUTE_PATH (filename)
3714 && entry->u->comp_dir != NULL)
3716 size_t filename_len;
3717 const char *dir;
3718 size_t dir_len;
3719 char *s;
3721 filename_len = strlen (filename);
3722 dir = entry->u->comp_dir;
3723 dir_len = strlen (dir);
3724 s = (char *) backtrace_alloc (state, dir_len + filename_len + 2,
3725 error_callback, data);
3726 if (s == NULL)
3728 *found = 0;
3729 return 0;
3731 memcpy (s, dir, dir_len);
3732 /* FIXME: Should use backslash if DOS file system. */
3733 s[dir_len] = '/';
3734 memcpy (s + dir_len + 1, filename, filename_len + 1);
3735 filename = s;
3737 entry->u->abs_filename = filename;
3740 return callback (data, pc, entry->u->abs_filename, 0, NULL);
3743 /* Search for function name within this unit. */
3745 if (entry->u->function_addrs_count == 0)
3746 return callback (data, pc, ln->filename, ln->lineno, NULL);
3748 function_addrs = ((struct function_addrs *)
3749 bsearch (&pc, entry->u->function_addrs,
3750 entry->u->function_addrs_count,
3751 sizeof (struct function_addrs),
3752 function_addrs_search));
3753 if (function_addrs == NULL)
3754 return callback (data, pc, ln->filename, ln->lineno, NULL);
3756 /* If there are multiple function ranges that contain PC, use the
3757 last one, in order to produce predictable results. */
3759 while (((size_t) (function_addrs - entry->u->function_addrs + 1)
3760 < entry->u->function_addrs_count)
3761 && pc >= (function_addrs + 1)->low
3762 && pc < (function_addrs + 1)->high)
3763 ++function_addrs;
3765 function = function_addrs->function;
3767 filename = ln->filename;
3768 lineno = ln->lineno;
3770 ret = report_inlined_functions (pc, function, callback, data,
3771 &filename, &lineno);
3772 if (ret != 0)
3773 return ret;
3775 return callback (data, pc, filename, lineno, function->name);
3779 /* Return the file/line information for a PC using the DWARF mapping
3780 we built earlier. */
3782 static int
3783 dwarf_fileline (struct backtrace_state *state, uintptr_t pc,
3784 backtrace_full_callback callback,
3785 backtrace_error_callback error_callback, void *data)
3787 struct dwarf_data *ddata;
3788 int found;
3789 int ret;
3791 if (!state->threaded)
3793 for (ddata = (struct dwarf_data *) state->fileline_data;
3794 ddata != NULL;
3795 ddata = ddata->next)
3797 ret = dwarf_lookup_pc (state, ddata, pc, callback, error_callback,
3798 data, &found);
3799 if (ret != 0 || found)
3800 return ret;
3803 else
3805 struct dwarf_data **pp;
3807 pp = (struct dwarf_data **) (void *) &state->fileline_data;
3808 while (1)
3810 ddata = backtrace_atomic_load_pointer (pp);
3811 if (ddata == NULL)
3812 break;
3814 ret = dwarf_lookup_pc (state, ddata, pc, callback, error_callback,
3815 data, &found);
3816 if (ret != 0 || found)
3817 return ret;
3819 pp = &ddata->next;
3823 /* FIXME: See if any libraries have been dlopen'ed. */
3825 return callback (data, pc, NULL, 0, NULL);
3828 /* Initialize our data structures from the DWARF debug info for a
3829 file. Return NULL on failure. */
3831 static struct dwarf_data *
3832 build_dwarf_data (struct backtrace_state *state,
3833 uintptr_t base_address,
3834 const struct dwarf_sections *dwarf_sections,
3835 int is_bigendian,
3836 struct dwarf_data *altlink,
3837 backtrace_error_callback error_callback,
3838 void *data)
3840 struct unit_addrs_vector addrs_vec;
3841 struct unit_addrs *addrs;
3842 size_t addrs_count;
3843 struct unit_vector units_vec;
3844 struct unit **units;
3845 size_t units_count;
3846 struct dwarf_data *fdata;
3848 if (!build_address_map (state, base_address, dwarf_sections, is_bigendian,
3849 altlink, error_callback, data, &addrs_vec,
3850 &units_vec))
3851 return NULL;
3853 if (!backtrace_vector_release (state, &addrs_vec.vec, error_callback, data))
3854 return NULL;
3855 if (!backtrace_vector_release (state, &units_vec.vec, error_callback, data))
3856 return NULL;
3857 addrs = (struct unit_addrs *) addrs_vec.vec.base;
3858 units = (struct unit **) units_vec.vec.base;
3859 addrs_count = addrs_vec.count;
3860 units_count = units_vec.count;
3861 backtrace_qsort (addrs, addrs_count, sizeof (struct unit_addrs),
3862 unit_addrs_compare);
3863 /* No qsort for units required, already sorted. */
3865 fdata = ((struct dwarf_data *)
3866 backtrace_alloc (state, sizeof (struct dwarf_data),
3867 error_callback, data));
3868 if (fdata == NULL)
3869 return NULL;
3871 fdata->next = NULL;
3872 fdata->altlink = altlink;
3873 fdata->base_address = base_address;
3874 fdata->addrs = addrs;
3875 fdata->addrs_count = addrs_count;
3876 fdata->units = units;
3877 fdata->units_count = units_count;
3878 fdata->dwarf_sections = *dwarf_sections;
3879 fdata->is_bigendian = is_bigendian;
3880 memset (&fdata->fvec, 0, sizeof fdata->fvec);
3882 return fdata;
3885 /* Build our data structures from the DWARF sections for a module.
3886 Set FILELINE_FN and STATE->FILELINE_DATA. Return 1 on success, 0
3887 on failure. */
3890 backtrace_dwarf_add (struct backtrace_state *state,
3891 uintptr_t base_address,
3892 const struct dwarf_sections *dwarf_sections,
3893 int is_bigendian,
3894 struct dwarf_data *fileline_altlink,
3895 backtrace_error_callback error_callback,
3896 void *data, fileline *fileline_fn,
3897 struct dwarf_data **fileline_entry)
3899 struct dwarf_data *fdata;
3901 fdata = build_dwarf_data (state, base_address, dwarf_sections, is_bigendian,
3902 fileline_altlink, error_callback, data);
3903 if (fdata == NULL)
3904 return 0;
3906 if (fileline_entry != NULL)
3907 *fileline_entry = fdata;
3909 if (!state->threaded)
3911 struct dwarf_data **pp;
3913 for (pp = (struct dwarf_data **) (void *) &state->fileline_data;
3914 *pp != NULL;
3915 pp = &(*pp)->next)
3917 *pp = fdata;
3919 else
3921 while (1)
3923 struct dwarf_data **pp;
3925 pp = (struct dwarf_data **) (void *) &state->fileline_data;
3927 while (1)
3929 struct dwarf_data *p;
3931 p = backtrace_atomic_load_pointer (pp);
3933 if (p == NULL)
3934 break;
3936 pp = &p->next;
3939 if (__sync_bool_compare_and_swap (pp, NULL, fdata))
3940 break;
3944 *fileline_fn = dwarf_fileline;
3946 return 1;