* cp-demangle.c (d_unqualified_name): Handle abi tags here.
[official-gcc.git] / libbacktrace / dwarf.c
blobb198dea38dc8ddfcda8b782a981dc31830491bc0
1 /* dwarf.c -- Get file/line information from DWARF for backtraces.
2 Copyright (C) 2012 Free Software Foundation, Inc.
3 Written by Ian Lance Taylor, Google.
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are
7 met:
9 (1) Redistributions of source code must retain the above copyright
10 notice, this list of conditions and the following disclaimer.
12 (2) Redistributions in binary form must reproduce the above copyright
13 notice, this list of conditions and the following disclaimer in
14 the documentation and/or other materials provided with the
15 distribution.
17 (3) The name of the author may not be used to
18 endorse or promote products derived from this software without
19 specific prior written permission.
21 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
25 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
29 STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
30 IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 POSSIBILITY OF SUCH DAMAGE. */
33 #include "config.h"
35 #include <errno.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <sys/types.h>
40 #include "dwarf2.h"
41 #include "filenames.h"
43 #include "backtrace.h"
44 #include "internal.h"
46 #if !defined(HAVE_DECL_STRNLEN) || !HAVE_DECL_STRNLEN
48 /* If strnlen is not declared, provide our own version. */
50 static size_t
51 xstrnlen (const char *s, size_t maxlen)
53 size_t i;
55 for (i = 0; i < maxlen; ++i)
56 if (s[i] == '\0')
57 break;
58 return i;
61 #define strnlen xstrnlen
63 #endif
65 /* A buffer to read DWARF info. */
67 struct dwarf_buf
69 /* Buffer name for error messages. */
70 const char *name;
71 /* Start of the buffer. */
72 const unsigned char *start;
73 /* Next byte to read. */
74 const unsigned char *buf;
75 /* The number of bytes remaining. */
76 size_t left;
77 /* Whether the data is big-endian. */
78 int is_bigendian;
79 /* Error callback routine. */
80 backtrace_error_callback error_callback;
81 /* Data for error_callback. */
82 void *data;
83 /* Non-zero if we've reported an underflow error. */
84 int reported_underflow;
87 /* A single attribute in a DWARF abbreviation. */
89 struct attr
91 /* The attribute name. */
92 enum dwarf_attribute name;
93 /* The attribute form. */
94 enum dwarf_form form;
97 /* A single DWARF abbreviation. */
99 struct abbrev
101 /* The abbrev code--the number used to refer to the abbrev. */
102 uint64_t code;
103 /* The entry tag. */
104 enum dwarf_tag tag;
105 /* Non-zero if this abbrev has child entries. */
106 int has_children;
107 /* The number of attributes. */
108 size_t num_attrs;
109 /* The attributes. */
110 struct attr *attrs;
113 /* The DWARF abbreviations for a compilation unit. This structure
114 only exists while reading the compilation unit. Most DWARF readers
115 seem to a hash table to map abbrev ID's to abbrev entries.
116 However, we primarily care about GCC, and GCC simply issues ID's in
117 numerical order starting at 1. So we simply keep a sorted vector,
118 and try to just look up the code. */
120 struct abbrevs
122 /* The number of abbrevs in the vector. */
123 size_t num_abbrevs;
124 /* The abbrevs, sorted by the code field. */
125 struct abbrev *abbrevs;
128 /* The different kinds of attribute values. */
130 enum attr_val_encoding
132 /* An address. */
133 ATTR_VAL_ADDRESS,
134 /* A unsigned integer. */
135 ATTR_VAL_UINT,
136 /* A sigd integer. */
137 ATTR_VAL_SINT,
138 /* A string. */
139 ATTR_VAL_STRING,
140 /* An offset to other data in the containing unit. */
141 ATTR_VAL_REF_UNIT,
142 /* An offset to other data within the .dwarf_info section. */
143 ATTR_VAL_REF_INFO,
144 /* An offset to data in some other section. */
145 ATTR_VAL_REF_SECTION,
146 /* A type signature. */
147 ATTR_VAL_REF_TYPE,
148 /* A block of data (not represented). */
149 ATTR_VAL_BLOCK,
150 /* An expression (not represented). */
151 ATTR_VAL_EXPR,
154 /* An attribute value. */
156 struct attr_val
158 /* How the value is stored in the field u. */
159 enum attr_val_encoding encoding;
160 union
162 /* ATTR_VAL_ADDRESS, ATTR_VAL_UINT, ATTR_VAL_REF*. */
163 uint64_t uint;
164 /* ATTR_VAL_SINT. */
165 int64_t sint;
166 /* ATTR_VAL_STRING. */
167 const char *string;
168 /* ATTR_VAL_BLOCK not stored. */
169 } u;
172 /* The line number program header. */
174 struct line_header
176 /* The version of the line number information. */
177 int version;
178 /* The minimum instruction length. */
179 unsigned int min_insn_len;
180 /* The maximum number of ops per instruction. */
181 unsigned int max_ops_per_insn;
182 /* The line base for special opcodes. */
183 int line_base;
184 /* The line range for special opcodes. */
185 unsigned int line_range;
186 /* The opcode base--the first special opcode. */
187 unsigned int opcode_base;
188 /* Opcode lengths, indexed by opcode - 1. */
189 const unsigned char *opcode_lengths;
190 /* The number of directory entries. */
191 size_t dirs_count;
192 /* The directory entries. */
193 const char **dirs;
194 /* The number of filenames. */
195 size_t filenames_count;
196 /* The filenames. */
197 const char **filenames;
200 /* Map a single PC value to a file/line. We will keep a vector of
201 these sorted by PC value. Each file/line will be correct from the
202 PC up to the PC of the next entry if there is one. We allocate one
203 extra entry at the end so that we can use bsearch. */
205 struct line
207 /* PC. */
208 uintptr_t pc;
209 /* File name. Many entries in the array are expected to point to
210 the same file name. */
211 const char *filename;
212 /* Line number. */
213 int lineno;
216 /* A growable vector of line number information. This is used while
217 reading the line numbers. */
219 struct line_vector
221 /* Memory. This is an array of struct line. */
222 struct backtrace_vector vec;
223 /* Number of valid mappings. */
224 size_t count;
227 /* A function described in the debug info. */
229 struct function
231 /* The name of the function. */
232 const char *name;
233 /* If this is an inlined function, the filename of the call
234 site. */
235 const char *caller_filename;
236 /* If this is an inlined function, the line number of the call
237 site. */
238 int caller_lineno;
239 /* Map PC ranges to inlined functions. */
240 struct function_addrs *function_addrs;
241 size_t function_addrs_count;
244 /* An address range for a function. This maps a PC value to a
245 specific function. */
247 struct function_addrs
249 /* Range is LOW <= PC < HIGH. */
250 uint64_t low;
251 uint64_t high;
252 /* Function for this address range. */
253 struct function *function;
256 /* A growable vector of function address ranges. */
258 struct function_vector
260 /* Memory. This is an array of struct function_addrs. */
261 struct backtrace_vector vec;
262 /* Number of address ranges present. */
263 size_t count;
266 /* A DWARF compilation unit. This only holds the information we need
267 to map a PC to a file and line. */
269 struct unit
271 /* The first entry for this compilation unit. */
272 const unsigned char *unit_data;
273 /* The length of the data for this compilation unit. */
274 size_t unit_data_len;
275 /* The offset of UNIT_DATA from the start of the information for
276 this compilation unit. */
277 size_t unit_data_offset;
278 /* DWARF version. */
279 int version;
280 /* Whether unit is DWARF64. */
281 int is_dwarf64;
282 /* Address size. */
283 int addrsize;
284 /* Offset into line number information. */
285 off_t lineoff;
286 /* Compilation command working directory. */
287 const char *comp_dir;
288 /* The abbreviations for this unit. */
289 struct abbrevs abbrevs;
291 /* The fields above this point are read in during initialization and
292 may be accessed freely. The fields below this point are read in
293 as needed, and therefore require care, as different threads may
294 try to initialize them simultaneously. */
296 /* PC to line number mapping. This is NULL if the values have not
297 been read. This is (struct line *) -1 if there was an error
298 reading the values. */
299 struct line *lines;
300 /* Number of entries in lines. */
301 size_t lines_count;
302 /* PC ranges to function. */
303 struct function_addrs *function_addrs;
304 size_t function_addrs_count;
307 /* An address range for a compilation unit. This maps a PC value to a
308 specific compilation unit. Note that we invert the representation
309 in DWARF: instead of listing the units and attaching a list of
310 ranges, we list the ranges and have each one point to the unit.
311 This lets us do a binary search to find the unit. */
313 struct unit_addrs
315 /* Range is LOW <= PC < HIGH. */
316 uint64_t low;
317 uint64_t high;
318 /* Compilation unit for this address range. */
319 struct unit *u;
322 /* A growable vector of compilation unit address ranges. */
324 struct unit_addrs_vector
326 /* Memory. This is an array of struct unit_addrs. */
327 struct backtrace_vector vec;
328 /* Number of address ranges present. */
329 size_t count;
332 /* The information we need to map a PC to a file and line. */
334 struct dwarf_data
336 /* The data for the next file we know about. */
337 struct dwarf_data *next;
338 /* The base address for this file. */
339 uintptr_t base_address;
340 /* A sorted list of address ranges. */
341 struct unit_addrs *addrs;
342 /* Number of address ranges in list. */
343 size_t addrs_count;
344 /* The unparsed .debug_info section. */
345 const unsigned char *dwarf_info;
346 size_t dwarf_info_size;
347 /* The unparsed .debug_line section. */
348 const unsigned char *dwarf_line;
349 size_t dwarf_line_size;
350 /* The unparsed .debug_ranges section. */
351 const unsigned char *dwarf_ranges;
352 size_t dwarf_ranges_size;
353 /* The unparsed .debug_str section. */
354 const unsigned char *dwarf_str;
355 size_t dwarf_str_size;
356 /* Whether the data is big-endian or not. */
357 int is_bigendian;
358 /* A vector used for function addresses. We keep this here so that
359 we can grow the vector as we read more functions. */
360 struct function_vector fvec;
363 /* Report an error for a DWARF buffer. */
365 static void
366 dwarf_buf_error (struct dwarf_buf *buf, const char *msg)
368 char b[200];
370 snprintf (b, sizeof b, "%s in %s at %d",
371 msg, buf->name, (int) (buf->buf - buf->start));
372 buf->error_callback (buf->data, b, 0);
375 /* Require at least COUNT bytes in BUF. Return 1 if all is well, 0 on
376 error. */
378 static int
379 require (struct dwarf_buf *buf, size_t count)
381 if (buf->left >= count)
382 return 1;
384 if (!buf->reported_underflow)
386 dwarf_buf_error (buf, "DWARF underflow");
387 buf->reported_underflow = 1;
390 return 0;
393 /* Advance COUNT bytes in BUF. Return 1 if all is well, 0 on
394 error. */
396 static int
397 advance (struct dwarf_buf *buf, size_t count)
399 if (!require (buf, count))
400 return 0;
401 buf->buf += count;
402 buf->left -= count;
403 return 1;
406 /* Read one byte from BUF and advance 1 byte. */
408 static unsigned char
409 read_byte (struct dwarf_buf *buf)
411 const unsigned char *p = buf->buf;
413 if (!advance (buf, 1))
414 return 0;
415 return p[0];
418 /* Read a signed char from BUF and advance 1 byte. */
420 static signed char
421 read_sbyte (struct dwarf_buf *buf)
423 const unsigned char *p = buf->buf;
425 if (!advance (buf, 1))
426 return 0;
427 return (*p ^ 0x80) - 0x80;
430 /* Read a uint16 from BUF and advance 2 bytes. */
432 static uint16_t
433 read_uint16 (struct dwarf_buf *buf)
435 const unsigned char *p = buf->buf;
437 if (!advance (buf, 2))
438 return 0;
439 if (buf->is_bigendian)
440 return ((uint16_t) p[0] << 8) | (uint16_t) p[1];
441 else
442 return ((uint16_t) p[1] << 8) | (uint16_t) p[0];
445 /* Read a uint32 from BUF and advance 4 bytes. */
447 static uint32_t
448 read_uint32 (struct dwarf_buf *buf)
450 const unsigned char *p = buf->buf;
452 if (!advance (buf, 4))
453 return 0;
454 if (buf->is_bigendian)
455 return (((uint32_t) p[0] << 24) | ((uint32_t) p[1] << 16)
456 | ((uint32_t) p[2] << 8) | (uint32_t) p[3]);
457 else
458 return (((uint32_t) p[3] << 24) | ((uint32_t) p[2] << 16)
459 | ((uint32_t) p[1] << 8) | (uint32_t) p[0]);
462 /* Read a uint64 from BUF and advance 8 bytes. */
464 static uint64_t
465 read_uint64 (struct dwarf_buf *buf)
467 const unsigned char *p = buf->buf;
469 if (!advance (buf, 8))
470 return 0;
471 if (buf->is_bigendian)
472 return (((uint64_t) p[0] << 56) | ((uint64_t) p[1] << 48)
473 | ((uint64_t) p[2] << 40) | ((uint64_t) p[3] << 32)
474 | ((uint64_t) p[4] << 24) | ((uint64_t) p[5] << 16)
475 | ((uint64_t) p[6] << 8) | (uint64_t) p[7]);
476 else
477 return (((uint64_t) p[7] << 56) | ((uint64_t) p[6] << 48)
478 | ((uint64_t) p[5] << 40) | ((uint64_t) p[4] << 32)
479 | ((uint64_t) p[3] << 24) | ((uint64_t) p[2] << 16)
480 | ((uint64_t) p[1] << 8) | (uint64_t) p[0]);
483 /* Read an offset from BUF and advance the appropriate number of
484 bytes. */
486 static uint64_t
487 read_offset (struct dwarf_buf *buf, int is_dwarf64)
489 if (is_dwarf64)
490 return read_uint64 (buf);
491 else
492 return read_uint32 (buf);
495 /* Read an address from BUF and advance the appropriate number of
496 bytes. */
498 static uint64_t
499 read_address (struct dwarf_buf *buf, int addrsize)
501 switch (addrsize)
503 case 1:
504 return read_byte (buf);
505 case 2:
506 return read_uint16 (buf);
507 case 4:
508 return read_uint32 (buf);
509 case 8:
510 return read_uint64 (buf);
511 default:
512 dwarf_buf_error (buf, "unrecognized address size");
513 return 0;
517 /* Return whether a value is the highest possible address, given the
518 address size. */
520 static int
521 is_highest_address (uint64_t address, int addrsize)
523 switch (addrsize)
525 case 1:
526 return address == (unsigned char) -1;
527 case 2:
528 return address == (uint16_t) -1;
529 case 4:
530 return address == (uint32_t) -1;
531 case 8:
532 return address == (uint64_t) -1;
533 default:
534 return 0;
538 /* Read an unsigned LEB128 number. */
540 static uint64_t
541 read_uleb128 (struct dwarf_buf *buf)
543 uint64_t ret;
544 unsigned int shift;
545 int overflow;
546 unsigned char b;
548 ret = 0;
549 shift = 0;
550 overflow = 0;
553 const unsigned char *p;
555 p = buf->buf;
556 if (!advance (buf, 1))
557 return 0;
558 b = *p;
559 if (shift < 64)
560 ret |= ((uint64_t) (b & 0x7f)) << shift;
561 else if (!overflow)
563 dwarf_buf_error (buf, "LEB128 overflows uint64_t");
564 overflow = 1;
566 shift += 7;
568 while ((b & 0x80) != 0);
570 return ret;
573 /* Read a signed LEB128 number. */
575 static int64_t
576 read_sleb128 (struct dwarf_buf *buf)
578 uint64_t val;
579 unsigned int shift;
580 int overflow;
581 unsigned char b;
583 val = 0;
584 shift = 0;
585 overflow = 0;
588 const unsigned char *p;
590 p = buf->buf;
591 if (!advance (buf, 1))
592 return 0;
593 b = *p;
594 if (shift < 64)
595 val |= ((uint64_t) (b & 0x7f)) << shift;
596 else if (!overflow)
598 dwarf_buf_error (buf, "signed LEB128 overflows uint64_t");
599 overflow = 1;
601 shift += 7;
603 while ((b & 0x80) != 0);
605 if ((b & 0x40) != 0 && shift < 64)
606 val |= ((uint64_t) -1) << shift;
608 return (int64_t) val;
611 /* Return the length of an LEB128 number. */
613 static size_t
614 leb128_len (const unsigned char *p)
616 size_t ret;
618 ret = 1;
619 while ((*p & 0x80) != 0)
621 ++p;
622 ++ret;
624 return ret;
627 /* Free an abbreviations structure. */
629 static void
630 free_abbrevs (struct backtrace_state *state, struct abbrevs *abbrevs,
631 backtrace_error_callback error_callback, void *data)
633 size_t i;
635 for (i = 0; i < abbrevs->num_abbrevs; ++i)
636 backtrace_free (state, abbrevs->abbrevs[i].attrs,
637 abbrevs->abbrevs[i].num_attrs * sizeof (struct attr),
638 error_callback, data);
639 backtrace_free (state, abbrevs->abbrevs,
640 abbrevs->num_abbrevs * sizeof (struct abbrev),
641 error_callback, data);
642 abbrevs->num_abbrevs = 0;
643 abbrevs->abbrevs = NULL;
646 /* Read an attribute value. Returns 1 on success, 0 on failure. If
647 the value can be represented as a uint64_t, sets *VAL and sets
648 *IS_VALID to 1. We don't try to store the value of other attribute
649 forms, because we don't care about them. */
651 static int
652 read_attribute (enum dwarf_form form, struct dwarf_buf *buf,
653 int is_dwarf64, int version, int addrsize,
654 const unsigned char *dwarf_str, size_t dwarf_str_size,
655 struct attr_val *val)
657 switch (form)
659 case DW_FORM_addr:
660 val->encoding = ATTR_VAL_ADDRESS;
661 val->u.uint = read_address (buf, addrsize);
662 return 1;
663 case DW_FORM_block2:
664 val->encoding = ATTR_VAL_BLOCK;
665 return advance (buf, read_uint16 (buf));
666 case DW_FORM_block4:
667 val->encoding = ATTR_VAL_BLOCK;
668 return advance (buf, read_uint32 (buf));
669 case DW_FORM_data2:
670 val->encoding = ATTR_VAL_UINT;
671 val->u.uint = read_uint16 (buf);
672 return 1;
673 case DW_FORM_data4:
674 val->encoding = ATTR_VAL_UINT;
675 val->u.uint = read_uint32 (buf);
676 return 1;
677 case DW_FORM_data8:
678 val->encoding = ATTR_VAL_UINT;
679 val->u.uint = read_uint64 (buf);
680 return 1;
681 case DW_FORM_string:
682 val->encoding = ATTR_VAL_STRING;
683 val->u.string = (const char *) buf->buf;
684 return advance (buf, strnlen ((const char *) buf->buf, buf->left) + 1);
685 case DW_FORM_block:
686 val->encoding = ATTR_VAL_BLOCK;
687 return advance (buf, read_uleb128 (buf));
688 case DW_FORM_block1:
689 val->encoding = ATTR_VAL_BLOCK;
690 return advance (buf, read_byte (buf));
691 case DW_FORM_data1:
692 val->encoding = ATTR_VAL_UINT;
693 val->u.uint = read_byte (buf);
694 return 1;
695 case DW_FORM_flag:
696 val->encoding = ATTR_VAL_UINT;
697 val->u.uint = read_byte (buf);
698 return 1;
699 case DW_FORM_sdata:
700 val->encoding = ATTR_VAL_SINT;
701 val->u.sint = read_sleb128 (buf);
702 return 1;
703 case DW_FORM_strp:
705 uint64_t offset;
707 offset = read_offset (buf, is_dwarf64);
708 if (offset >= dwarf_str_size)
710 dwarf_buf_error (buf, "DW_FORM_strp out of range");
711 return 0;
713 val->encoding = ATTR_VAL_STRING;
714 val->u.string = (const char *) dwarf_str + offset;
715 return 1;
717 case DW_FORM_udata:
718 val->encoding = ATTR_VAL_UINT;
719 val->u.uint = read_uleb128 (buf);
720 return 1;
721 case DW_FORM_ref_addr:
722 val->encoding = ATTR_VAL_REF_INFO;
723 if (version == 2)
724 val->u.uint = read_address (buf, addrsize);
725 else
726 val->u.uint = read_offset (buf, is_dwarf64);
727 return 1;
728 case DW_FORM_ref1:
729 val->encoding = ATTR_VAL_REF_UNIT;
730 val->u.uint = read_byte (buf);
731 return 1;
732 case DW_FORM_ref2:
733 val->encoding = ATTR_VAL_REF_UNIT;
734 val->u.uint = read_uint16 (buf);
735 return 1;
736 case DW_FORM_ref4:
737 val->encoding = ATTR_VAL_REF_UNIT;
738 val->u.uint = read_uint32 (buf);
739 return 1;
740 case DW_FORM_ref8:
741 val->encoding = ATTR_VAL_REF_UNIT;
742 val->u.uint = read_uint64 (buf);
743 return 1;
744 case DW_FORM_ref_udata:
745 val->encoding = ATTR_VAL_REF_UNIT;
746 val->u.uint = read_uleb128 (buf);
747 return 1;
748 case DW_FORM_indirect:
750 uint64_t form;
752 form = read_uleb128 (buf);
753 return read_attribute ((enum dwarf_form) form, buf, is_dwarf64,
754 version, addrsize, dwarf_str, dwarf_str_size,
755 val);
757 case DW_FORM_sec_offset:
758 val->encoding = ATTR_VAL_REF_SECTION;
759 val->u.uint = read_offset (buf, is_dwarf64);
760 return 1;
761 case DW_FORM_exprloc:
762 val->encoding = ATTR_VAL_EXPR;
763 return advance (buf, read_uleb128 (buf));
764 case DW_FORM_flag_present:
765 val->encoding = ATTR_VAL_UINT;
766 val->u.uint = 1;
767 return 1;
768 case DW_FORM_ref_sig8:
769 val->encoding = ATTR_VAL_REF_TYPE;
770 val->u.uint = read_uint64 (buf);
771 return 1;
772 case DW_FORM_GNU_addr_index:
773 val->encoding = ATTR_VAL_REF_SECTION;
774 val->u.uint = read_uleb128 (buf);
775 return 1;
776 case DW_FORM_GNU_str_index:
777 val->encoding = ATTR_VAL_REF_SECTION;
778 val->u.uint = read_uleb128 (buf);
779 return 1;
780 case DW_FORM_GNU_ref_alt:
781 val->encoding = ATTR_VAL_REF_SECTION;
782 val->u.uint = read_offset (buf, is_dwarf64);
783 return 1;
784 case DW_FORM_GNU_strp_alt:
785 val->encoding = ATTR_VAL_REF_SECTION;
786 val->u.uint = read_offset (buf, is_dwarf64);
787 return 1;
788 default:
789 dwarf_buf_error (buf, "unrecognized DWARF form");
790 return 0;
794 /* Compare function_addrs for qsort. When ranges are nested, make the
795 smallest one sort last. */
797 static int
798 function_addrs_compare (const void *v1, const void *v2)
800 const struct function_addrs *a1 = (const struct function_addrs *) v1;
801 const struct function_addrs *a2 = (const struct function_addrs *) v2;
803 if (a1->low < a2->low)
804 return -1;
805 if (a1->low > a2->low)
806 return 1;
807 if (a1->high < a2->high)
808 return 1;
809 if (a1->high > a2->high)
810 return -1;
811 return strcmp (a1->function->name, a2->function->name);
814 /* Compare a PC against a function_addrs for bsearch. Note that if
815 there are multiple ranges containing PC, which one will be returned
816 is unpredictable. We compensate for that in dwarf_fileline. */
818 static int
819 function_addrs_search (const void *vkey, const void *ventry)
821 const uintptr_t *key = (const uintptr_t *) vkey;
822 const struct function_addrs *entry = (const struct function_addrs *) ventry;
823 uintptr_t pc;
825 pc = *key;
826 if (pc < entry->low)
827 return -1;
828 else if (pc >= entry->high)
829 return 1;
830 else
831 return 0;
834 /* Add a new compilation unit address range to a vector. Returns 1 on
835 success, 0 on failure. */
837 static int
838 add_unit_addr (struct backtrace_state *state, uintptr_t base_address,
839 struct unit_addrs addrs,
840 backtrace_error_callback error_callback, void *data,
841 struct unit_addrs_vector *vec)
843 struct unit_addrs *p;
845 /* Add in the base address of the module here, so that we can look
846 up the PC directly. */
847 addrs.low += base_address;
848 addrs.high += base_address;
850 /* Try to merge with the last entry. */
851 if (vec->count > 0)
853 p = (struct unit_addrs *) vec->vec.base + (vec->count - 1);
854 if ((addrs.low == p->high || addrs.low == p->high + 1)
855 && addrs.u == p->u)
857 if (addrs.high > p->high)
858 p->high = addrs.high;
859 return 1;
863 p = ((struct unit_addrs *)
864 backtrace_vector_grow (state, sizeof (struct unit_addrs),
865 error_callback, data, &vec->vec));
866 if (p == NULL)
867 return 0;
869 *p = addrs;
870 ++vec->count;
871 return 1;
874 /* Free a unit address vector. */
876 static void
877 free_unit_addrs_vector (struct backtrace_state *state,
878 struct unit_addrs_vector *vec,
879 backtrace_error_callback error_callback, void *data)
881 struct unit_addrs *addrs;
882 size_t i;
884 addrs = (struct unit_addrs *) vec->vec.base;
885 for (i = 0; i < vec->count; ++i)
886 free_abbrevs (state, &addrs[i].u->abbrevs, error_callback, data);
889 /* Compare unit_addrs for qsort. When ranges are nested, make the
890 smallest one sort last. */
892 static int
893 unit_addrs_compare (const void *v1, const void *v2)
895 const struct unit_addrs *a1 = (const struct unit_addrs *) v1;
896 const struct unit_addrs *a2 = (const struct unit_addrs *) v2;
898 if (a1->low < a2->low)
899 return -1;
900 if (a1->low > a2->low)
901 return 1;
902 if (a1->high < a2->high)
903 return 1;
904 if (a1->high > a2->high)
905 return -1;
906 if (a1->u->lineoff < a2->u->lineoff)
907 return -1;
908 if (a1->u->lineoff > a2->u->lineoff)
909 return 1;
910 return 0;
913 /* Compare a PC against a unit_addrs for bsearch. Note that if there
914 are multiple ranges containing PC, which one will be returned is
915 unpredictable. We compensate for that in dwarf_fileline. */
917 static int
918 unit_addrs_search (const void *vkey, const void *ventry)
920 const uintptr_t *key = (const uintptr_t *) vkey;
921 const struct unit_addrs *entry = (const struct unit_addrs *) ventry;
922 uintptr_t pc;
924 pc = *key;
925 if (pc < entry->low)
926 return -1;
927 else if (pc >= entry->high)
928 return 1;
929 else
930 return 0;
933 /* Sort the line vector by PC. We want a stable sort here. We know
934 that the pointers are into the same array, so it is safe to compare
935 them directly. */
937 static int
938 line_compare (const void *v1, const void *v2)
940 const struct line *ln1 = (const struct line *) v1;
941 const struct line *ln2 = (const struct line *) v2;
943 if (ln1->pc < ln2->pc)
944 return -1;
945 else if (ln1->pc > ln2->pc)
946 return 1;
947 else if (ln1 < ln2)
948 return -1;
949 else if (ln1 > ln2)
950 return 1;
951 else
952 return 0;
955 /* Find a PC in a line vector. We always allocate an extra entry at
956 the end of the lines vector, so that this routine can safely look
957 at the next entry. Note that when there are multiple mappings for
958 the same PC value, this will return the last one. */
960 static int
961 line_search (const void *vkey, const void *ventry)
963 const uintptr_t *key = (const uintptr_t *) vkey;
964 const struct line *entry = (const struct line *) ventry;
965 uintptr_t pc;
967 pc = *key;
968 if (pc < entry->pc)
969 return -1;
970 else if (pc >= (entry + 1)->pc)
971 return 1;
972 else
973 return 0;
976 /* Sort the abbrevs by the abbrev code. This function is passed to
977 both qsort and bsearch. */
979 static int
980 abbrev_compare (const void *v1, const void *v2)
982 const struct abbrev *a1 = (const struct abbrev *) v1;
983 const struct abbrev *a2 = (const struct abbrev *) v2;
985 if (a1->code < a2->code)
986 return -1;
987 else if (a1->code > a2->code)
988 return 1;
989 else
991 /* This really shouldn't happen. It means there are two
992 different abbrevs with the same code, and that means we don't
993 know which one lookup_abbrev should return. */
994 return 0;
998 /* Read the abbreviation table for a compilation unit. Returns 1 on
999 success, 0 on failure. */
1001 static int
1002 read_abbrevs (struct backtrace_state *state, uint64_t abbrev_offset,
1003 const unsigned char *dwarf_abbrev, size_t dwarf_abbrev_size,
1004 int is_bigendian, backtrace_error_callback error_callback,
1005 void *data, struct abbrevs *abbrevs)
1007 struct dwarf_buf abbrev_buf;
1008 struct dwarf_buf count_buf;
1009 size_t num_abbrevs;
1011 abbrevs->num_abbrevs = 0;
1012 abbrevs->abbrevs = NULL;
1014 if (abbrev_offset >= dwarf_abbrev_size)
1016 error_callback (data, "abbrev offset out of range", 0);
1017 return 0;
1020 abbrev_buf.name = ".debug_abbrev";
1021 abbrev_buf.start = dwarf_abbrev;
1022 abbrev_buf.buf = dwarf_abbrev + abbrev_offset;
1023 abbrev_buf.left = dwarf_abbrev_size - abbrev_offset;
1024 abbrev_buf.is_bigendian = is_bigendian;
1025 abbrev_buf.error_callback = error_callback;
1026 abbrev_buf.data = data;
1027 abbrev_buf.reported_underflow = 0;
1029 /* Count the number of abbrevs in this list. */
1031 count_buf = abbrev_buf;
1032 num_abbrevs = 0;
1033 while (read_uleb128 (&count_buf) != 0)
1035 if (count_buf.reported_underflow)
1036 return 0;
1037 ++num_abbrevs;
1038 // Skip tag.
1039 read_uleb128 (&count_buf);
1040 // Skip has_children.
1041 read_byte (&count_buf);
1042 // Skip attributes.
1043 while (read_uleb128 (&count_buf) != 0)
1044 read_uleb128 (&count_buf);
1045 // Skip form of last attribute.
1046 read_uleb128 (&count_buf);
1049 if (count_buf.reported_underflow)
1050 return 0;
1052 if (num_abbrevs == 0)
1053 return 1;
1055 abbrevs->num_abbrevs = num_abbrevs;
1056 abbrevs->abbrevs = ((struct abbrev *)
1057 backtrace_alloc (state,
1058 num_abbrevs * sizeof (struct abbrev),
1059 error_callback, data));
1060 if (abbrevs->abbrevs == NULL)
1061 return 0;
1062 memset (abbrevs->abbrevs, 0, num_abbrevs * sizeof (struct abbrev));
1064 num_abbrevs = 0;
1065 while (1)
1067 uint64_t code;
1068 struct abbrev a;
1069 size_t num_attrs;
1070 struct attr *attrs;
1072 if (abbrev_buf.reported_underflow)
1073 goto fail;
1075 code = read_uleb128 (&abbrev_buf);
1076 if (code == 0)
1077 break;
1079 a.code = code;
1080 a.tag = (enum dwarf_tag) read_uleb128 (&abbrev_buf);
1081 a.has_children = read_byte (&abbrev_buf);
1083 count_buf = abbrev_buf;
1084 num_attrs = 0;
1085 while (read_uleb128 (&count_buf) != 0)
1087 ++num_attrs;
1088 read_uleb128 (&count_buf);
1091 if (num_attrs == 0)
1093 attrs = NULL;
1094 read_uleb128 (&abbrev_buf);
1095 read_uleb128 (&abbrev_buf);
1097 else
1099 attrs = ((struct attr *)
1100 backtrace_alloc (state, num_attrs * sizeof *attrs,
1101 error_callback, data));
1102 if (attrs == NULL)
1103 goto fail;
1104 num_attrs = 0;
1105 while (1)
1107 uint64_t name;
1108 uint64_t form;
1110 name = read_uleb128 (&abbrev_buf);
1111 form = read_uleb128 (&abbrev_buf);
1112 if (name == 0)
1113 break;
1114 attrs[num_attrs].name = (enum dwarf_attribute) name;
1115 attrs[num_attrs].form = (enum dwarf_form) form;
1116 ++num_attrs;
1120 a.num_attrs = num_attrs;
1121 a.attrs = attrs;
1123 abbrevs->abbrevs[num_abbrevs] = a;
1124 ++num_abbrevs;
1127 qsort (abbrevs->abbrevs, abbrevs->num_abbrevs, sizeof (struct abbrev),
1128 abbrev_compare);
1130 return 1;
1132 fail:
1133 free_abbrevs (state, abbrevs, error_callback, data);
1134 return 0;
1137 /* Return the abbrev information for an abbrev code. */
1139 static const struct abbrev *
1140 lookup_abbrev (struct abbrevs *abbrevs, uint64_t code,
1141 backtrace_error_callback error_callback, void *data)
1143 struct abbrev key;
1144 void *p;
1146 /* With GCC, where abbrevs are simply numbered in order, we should
1147 be able to just look up the entry. */
1148 if (code - 1 < abbrevs->num_abbrevs
1149 && abbrevs->abbrevs[code - 1].code == code)
1150 return &abbrevs->abbrevs[code - 1];
1152 /* Otherwise we have to search. */
1153 memset (&key, 0, sizeof key);
1154 key.code = code;
1155 p = bsearch (&key, abbrevs->abbrevs, abbrevs->num_abbrevs,
1156 sizeof (struct abbrev), abbrev_compare);
1157 if (p == NULL)
1159 error_callback (data, "invalid abbreviation code", 0);
1160 return NULL;
1162 return (const struct abbrev *) p;
1165 /* Add non-contiguous address ranges for a compilation unit. Returns
1166 1 on success, 0 on failure. */
1168 static int
1169 add_unit_ranges (struct backtrace_state *state, uintptr_t base_address,
1170 struct unit *u, uint64_t ranges, uint64_t base,
1171 int is_bigendian, const unsigned char *dwarf_ranges,
1172 size_t dwarf_ranges_size,
1173 backtrace_error_callback error_callback, void *data,
1174 struct unit_addrs_vector *addrs)
1176 struct dwarf_buf ranges_buf;
1178 if (ranges >= dwarf_ranges_size)
1180 error_callback (data, "ranges offset out of range", 0);
1181 return 0;
1184 ranges_buf.name = ".debug_ranges";
1185 ranges_buf.start = dwarf_ranges;
1186 ranges_buf.buf = dwarf_ranges + ranges;
1187 ranges_buf.left = dwarf_ranges_size - ranges;
1188 ranges_buf.is_bigendian = is_bigendian;
1189 ranges_buf.error_callback = error_callback;
1190 ranges_buf.data = data;
1191 ranges_buf.reported_underflow = 0;
1193 while (1)
1195 uint64_t low;
1196 uint64_t high;
1198 if (ranges_buf.reported_underflow)
1199 return 0;
1201 low = read_address (&ranges_buf, u->addrsize);
1202 high = read_address (&ranges_buf, u->addrsize);
1204 if (low == 0 && high == 0)
1205 break;
1207 if (is_highest_address (low, u->addrsize))
1208 base = high;
1209 else
1211 struct unit_addrs a;
1213 a.low = low + base;
1214 a.high = high + base;
1215 a.u = u;
1216 if (!add_unit_addr (state, base_address, a, error_callback, data,
1217 addrs))
1218 return 0;
1222 if (ranges_buf.reported_underflow)
1223 return 0;
1225 return 1;
1228 /* Build a mapping from address ranges to the compilation units where
1229 the line number information for that range can be found. Returns 1
1230 on success, 0 on failure. */
1232 static int
1233 build_address_map (struct backtrace_state *state, uintptr_t base_address,
1234 const unsigned char *dwarf_info, size_t dwarf_info_size,
1235 const unsigned char *dwarf_abbrev, size_t dwarf_abbrev_size,
1236 const unsigned char *dwarf_ranges, size_t dwarf_ranges_size,
1237 const unsigned char *dwarf_str, size_t dwarf_str_size,
1238 int is_bigendian, backtrace_error_callback error_callback,
1239 void *data, struct unit_addrs_vector *addrs)
1241 struct dwarf_buf info;
1242 struct abbrevs abbrevs;
1244 memset (&addrs->vec, 0, sizeof addrs->vec);
1245 addrs->count = 0;
1247 /* Read through the .debug_info section. FIXME: Should we use the
1248 .debug_aranges section? gdb and addr2line don't use it, but I'm
1249 not sure why. */
1251 info.name = ".debug_info";
1252 info.start = dwarf_info;
1253 info.buf = dwarf_info;
1254 info.left = dwarf_info_size;
1255 info.is_bigendian = is_bigendian;
1256 info.error_callback = error_callback;
1257 info.data = data;
1258 info.reported_underflow = 0;
1260 memset (&abbrevs, 0, sizeof abbrevs);
1261 while (info.left > 0)
1263 const unsigned char *unit_data_start;
1264 uint64_t len;
1265 int is_dwarf64;
1266 struct dwarf_buf unit_buf;
1267 int version;
1268 uint64_t abbrev_offset;
1269 const struct abbrev *abbrev;
1270 int addrsize;
1271 const unsigned char *unit_data;
1272 size_t unit_data_len;
1273 size_t unit_data_offset;
1274 uint64_t code;
1275 size_t i;
1276 uint64_t lowpc;
1277 int have_lowpc;
1278 uint64_t highpc;
1279 int have_highpc;
1280 int highpc_is_relative;
1281 uint64_t ranges;
1282 int have_ranges;
1283 uint64_t lineoff;
1284 int have_lineoff;
1285 const char *comp_dir;
1287 if (info.reported_underflow)
1288 goto fail;
1290 unit_data_start = info.buf;
1292 is_dwarf64 = 0;
1293 len = read_uint32 (&info);
1294 if (len == 0xffffffff)
1296 len = read_uint64 (&info);
1297 is_dwarf64 = 1;
1300 unit_buf = info;
1301 unit_buf.left = len;
1303 if (!advance (&info, len))
1304 goto fail;
1306 version = read_uint16 (&unit_buf);
1307 if (version < 2 || version > 4)
1309 dwarf_buf_error (&unit_buf, "unrecognized DWARF version");
1310 goto fail;
1313 abbrev_offset = read_offset (&unit_buf, is_dwarf64);
1314 if (!read_abbrevs (state, abbrev_offset, dwarf_abbrev, dwarf_abbrev_size,
1315 is_bigendian, error_callback, data, &abbrevs))
1316 goto fail;
1318 addrsize = read_byte (&unit_buf);
1320 unit_data = unit_buf.buf;
1321 unit_data_len = unit_buf.left;
1322 unit_data_offset = unit_buf.buf - unit_data_start;
1324 /* We only look at the first attribute in the compilation unit.
1325 In practice this will be a DW_TAG_compile_unit which will
1326 tell us the PC range and where to find the line number
1327 information. */
1329 code = read_uleb128 (&unit_buf);
1330 abbrev = lookup_abbrev (&abbrevs, code, error_callback, data);
1331 if (abbrev == NULL)
1332 goto fail;
1334 lowpc = 0;
1335 have_lowpc = 0;
1336 highpc = 0;
1337 have_highpc = 0;
1338 highpc_is_relative = 0;
1339 ranges = 0;
1340 have_ranges = 0;
1341 lineoff = 0;
1342 have_lineoff = 0;
1343 comp_dir = NULL;
1344 for (i = 0; i < abbrev->num_attrs; ++i)
1346 struct attr_val val;
1348 if (!read_attribute (abbrev->attrs[i].form, &unit_buf, is_dwarf64,
1349 version, addrsize, dwarf_str, dwarf_str_size,
1350 &val))
1351 goto fail;
1353 switch (abbrev->attrs[i].name)
1355 case DW_AT_low_pc:
1356 if (val.encoding == ATTR_VAL_ADDRESS)
1358 lowpc = val.u.uint;
1359 have_lowpc = 1;
1361 break;
1362 case DW_AT_high_pc:
1363 if (val.encoding == ATTR_VAL_ADDRESS)
1365 highpc = val.u.uint;
1366 have_highpc = 1;
1368 else if (val.encoding == ATTR_VAL_UINT)
1370 highpc = val.u.uint;
1371 have_highpc = 1;
1372 highpc_is_relative = 1;
1374 break;
1375 case DW_AT_ranges:
1376 if (val.encoding == ATTR_VAL_UINT
1377 || val.encoding == ATTR_VAL_REF_SECTION)
1379 ranges = val.u.uint;
1380 have_ranges = 1;
1382 break;
1383 case DW_AT_stmt_list:
1384 if (val.encoding == ATTR_VAL_UINT
1385 || val.encoding == ATTR_VAL_REF_SECTION)
1387 lineoff = val.u.uint;
1388 have_lineoff = 1;
1390 break;
1391 case DW_AT_comp_dir:
1392 if (val.encoding == ATTR_VAL_STRING)
1393 comp_dir = val.u.string;
1394 break;
1395 default:
1396 break;
1400 if (unit_buf.reported_underflow)
1401 goto fail;
1403 if (((have_lowpc && have_highpc) || have_ranges) && have_lineoff)
1405 struct unit *u;
1406 struct unit_addrs a;
1408 u = ((struct unit *)
1409 backtrace_alloc (state, sizeof *u, error_callback, data));
1410 if (u == NULL)
1411 goto fail;
1412 u->unit_data = unit_data;
1413 u->unit_data_len = unit_data_len;
1414 u->unit_data_offset = unit_data_offset;
1415 u->version = version;
1416 u->is_dwarf64 = is_dwarf64;
1417 u->addrsize = addrsize;
1418 u->comp_dir = comp_dir;
1419 u->lineoff = lineoff;
1420 u->abbrevs = abbrevs;
1421 memset (&abbrevs, 0, sizeof abbrevs);
1423 /* The actual line number mappings will be read as
1424 needed. */
1425 u->lines = NULL;
1426 u->lines_count = 0;
1427 u->function_addrs = NULL;
1428 u->function_addrs_count = 0;
1430 if (have_ranges)
1432 if (!add_unit_ranges (state, base_address, u, ranges, lowpc,
1433 is_bigendian, dwarf_ranges,
1434 dwarf_ranges_size, error_callback, data,
1435 addrs))
1437 free_abbrevs (state, &u->abbrevs, error_callback, data);
1438 backtrace_free (state, u, sizeof *u, error_callback, data);
1439 goto fail;
1442 else
1444 if (highpc_is_relative)
1445 highpc += lowpc;
1446 a.low = lowpc;
1447 a.high = highpc;
1448 a.u = u;
1450 if (!add_unit_addr (state, base_address, a, error_callback, data,
1451 addrs))
1453 free_abbrevs (state, &u->abbrevs, error_callback, data);
1454 backtrace_free (state, u, sizeof *u, error_callback, data);
1455 goto fail;
1459 else
1461 free_abbrevs (state, &abbrevs, error_callback, data);
1462 memset (&abbrevs, 0, sizeof abbrevs);
1465 if (info.reported_underflow)
1466 goto fail;
1468 return 1;
1470 fail:
1471 free_abbrevs (state, &abbrevs, error_callback, data);
1472 free_unit_addrs_vector (state, addrs, error_callback, data);
1473 return 0;
1476 /* Add a new mapping to the vector of line mappings that we are
1477 building. Returns 1 on success, 0 on failure. */
1479 static int
1480 add_line (struct backtrace_state *state, struct dwarf_data *ddata,
1481 uintptr_t pc, const char *filename, int lineno,
1482 backtrace_error_callback error_callback, void *data,
1483 struct line_vector *vec)
1485 struct line *ln;
1487 /* If we are adding the same mapping, ignore it. This can happen
1488 when using discriminators. */
1489 if (vec->count > 0)
1491 ln = (struct line *) vec->vec.base + (vec->count - 1);
1492 if (pc == ln->pc && filename == ln->filename && lineno == ln->lineno)
1493 return 1;
1496 ln = ((struct line *)
1497 backtrace_vector_grow (state, sizeof (struct line), error_callback,
1498 data, &vec->vec));
1499 if (ln == NULL)
1500 return 0;
1502 /* Add in the base address here, so that we can look up the PC
1503 directly. */
1504 ln->pc = pc + ddata->base_address;
1506 ln->filename = filename;
1507 ln->lineno = lineno;
1509 ++vec->count;
1511 return 1;
1514 /* Free the line header information. If FREE_FILENAMES is true we
1515 free the file names themselves, otherwise we leave them, as there
1516 may be line structures pointing to them. */
1518 static void
1519 free_line_header (struct backtrace_state *state, struct line_header *hdr,
1520 backtrace_error_callback error_callback, void *data)
1522 backtrace_free (state, hdr->dirs, hdr->dirs_count * sizeof (const char *),
1523 error_callback, data);
1524 backtrace_free (state, hdr->filenames,
1525 hdr->filenames_count * sizeof (char *),
1526 error_callback, data);
1529 /* Read the line header. Return 1 on success, 0 on failure. */
1531 static int
1532 read_line_header (struct backtrace_state *state, struct unit *u,
1533 int is_dwarf64, struct dwarf_buf *line_buf,
1534 struct line_header *hdr)
1536 uint64_t hdrlen;
1537 struct dwarf_buf hdr_buf;
1538 const unsigned char *p;
1539 const unsigned char *pend;
1540 size_t i;
1542 hdr->version = read_uint16 (line_buf);
1543 if (hdr->version < 2 || hdr->version > 4)
1545 dwarf_buf_error (line_buf, "unsupported line number version");
1546 return 0;
1549 hdrlen = read_offset (line_buf, is_dwarf64);
1551 hdr_buf = *line_buf;
1552 hdr_buf.left = hdrlen;
1554 if (!advance (line_buf, hdrlen))
1555 return 0;
1557 hdr->min_insn_len = read_byte (&hdr_buf);
1558 if (hdr->version < 4)
1559 hdr->max_ops_per_insn = 1;
1560 else
1561 hdr->max_ops_per_insn = read_byte (&hdr_buf);
1563 /* We don't care about default_is_stmt. */
1564 read_byte (&hdr_buf);
1566 hdr->line_base = read_sbyte (&hdr_buf);
1567 hdr->line_range = read_byte (&hdr_buf);
1569 hdr->opcode_base = read_byte (&hdr_buf);
1570 hdr->opcode_lengths = hdr_buf.buf;
1571 if (!advance (&hdr_buf, hdr->opcode_base - 1))
1572 return 0;
1574 /* Count the number of directory entries. */
1575 hdr->dirs_count = 0;
1576 p = hdr_buf.buf;
1577 pend = p + hdr_buf.left;
1578 while (p < pend && *p != '\0')
1580 p += strnlen((const char *) p, pend - p) + 1;
1581 ++hdr->dirs_count;
1584 hdr->dirs = ((const char **)
1585 backtrace_alloc (state,
1586 hdr->dirs_count * sizeof (const char *),
1587 line_buf->error_callback, line_buf->data));
1588 if (hdr->dirs == NULL)
1589 return 0;
1591 i = 0;
1592 while (*hdr_buf.buf != '\0')
1594 if (hdr_buf.reported_underflow)
1595 return 0;
1597 hdr->dirs[i] = (const char *) hdr_buf.buf;
1598 ++i;
1599 if (!advance (&hdr_buf,
1600 strnlen ((const char *) hdr_buf.buf, hdr_buf.left) + 1))
1601 return 0;
1603 if (!advance (&hdr_buf, 1))
1604 return 0;
1606 /* Count the number of file entries. */
1607 hdr->filenames_count = 0;
1608 p = hdr_buf.buf;
1609 pend = p + hdr_buf.left;
1610 while (p < pend && *p != '\0')
1612 p += strnlen ((const char *) p, pend - p) + 1;
1613 p += leb128_len (p);
1614 p += leb128_len (p);
1615 p += leb128_len (p);
1616 ++hdr->filenames_count;
1619 hdr->filenames = ((const char **)
1620 backtrace_alloc (state,
1621 hdr->filenames_count * sizeof (char *),
1622 line_buf->error_callback,
1623 line_buf->data));
1624 if (hdr->filenames == NULL)
1625 return 0;
1626 i = 0;
1627 while (*hdr_buf.buf != '\0')
1629 const char *filename;
1630 uint64_t dir_index;
1632 if (hdr_buf.reported_underflow)
1633 return 0;
1635 filename = (const char *) hdr_buf.buf;
1636 if (!advance (&hdr_buf,
1637 strnlen ((const char *) hdr_buf.buf, hdr_buf.left) + 1))
1638 return 0;
1639 dir_index = read_uleb128 (&hdr_buf);
1640 if (IS_ABSOLUTE_PATH (filename))
1641 hdr->filenames[i] = filename;
1642 else
1644 const char *dir;
1645 size_t dir_len;
1646 size_t filename_len;
1647 char *s;
1649 if (dir_index == 0)
1650 dir = u->comp_dir;
1651 else if (dir_index - 1 < hdr->dirs_count)
1652 dir = hdr->dirs[dir_index - 1];
1653 else
1655 dwarf_buf_error (line_buf,
1656 ("invalid directory index in "
1657 "line number program header"));
1658 return 0;
1660 dir_len = strlen (dir);
1661 filename_len = strlen (filename);
1662 s = ((char *)
1663 backtrace_alloc (state, dir_len + filename_len + 2,
1664 line_buf->error_callback, line_buf->data));
1665 if (s == NULL)
1666 return 0;
1667 memcpy (s, dir, dir_len);
1668 /* FIXME: If we are on a DOS-based file system, and the
1669 directory or the file name use backslashes, then we
1670 should use a backslash here. */
1671 s[dir_len] = '/';
1672 memcpy (s + dir_len + 1, filename, filename_len + 1);
1673 hdr->filenames[i] = s;
1676 /* Ignore the modification time and size. */
1677 read_uleb128 (&hdr_buf);
1678 read_uleb128 (&hdr_buf);
1680 ++i;
1683 if (hdr_buf.reported_underflow)
1684 return 0;
1686 return 1;
1689 /* Read the line program, adding line mappings to VEC. Return 1 on
1690 success, 0 on failure. */
1692 static int
1693 read_line_program (struct backtrace_state *state, struct dwarf_data *ddata,
1694 struct unit *u, const struct line_header *hdr,
1695 struct dwarf_buf *line_buf, struct line_vector *vec)
1697 uint64_t address;
1698 unsigned int op_index;
1699 const char *reset_filename;
1700 const char *filename;
1701 int lineno;
1703 address = 0;
1704 op_index = 0;
1705 if (hdr->filenames_count > 0)
1706 reset_filename = hdr->filenames[0];
1707 else
1708 reset_filename = "";
1709 filename = reset_filename;
1710 lineno = 1;
1711 while (line_buf->left > 0)
1713 unsigned int op;
1715 op = read_byte (line_buf);
1716 if (op >= hdr->opcode_base)
1718 unsigned int advance;
1720 /* Special opcode. */
1721 op -= hdr->opcode_base;
1722 advance = op / hdr->line_range;
1723 address += (hdr->min_insn_len * (op_index + advance)
1724 / hdr->max_ops_per_insn);
1725 op_index = (op_index + advance) % hdr->max_ops_per_insn;
1726 lineno += hdr->line_base + (int) (op % hdr->line_range);
1727 add_line (state, ddata, address, filename, lineno,
1728 line_buf->error_callback, line_buf->data, vec);
1730 else if (op == DW_LNS_extended_op)
1732 uint64_t len;
1734 len = read_uleb128 (line_buf);
1735 op = read_byte (line_buf);
1736 switch (op)
1738 case DW_LNE_end_sequence:
1739 /* FIXME: Should we mark the high PC here? It seems
1740 that we already have that information from the
1741 compilation unit. */
1742 address = 0;
1743 op_index = 0;
1744 filename = reset_filename;
1745 lineno = 1;
1746 break;
1747 case DW_LNE_set_address:
1748 address = read_address (line_buf, u->addrsize);
1749 break;
1750 case DW_LNE_define_file:
1752 const char *f;
1753 unsigned int dir_index;
1755 f = (const char *) line_buf->buf;
1756 if (!advance (line_buf, strnlen (f, line_buf->left) + 1))
1757 return 0;
1758 dir_index = read_uleb128 (line_buf);
1759 /* Ignore that time and length. */
1760 read_uleb128 (line_buf);
1761 read_uleb128 (line_buf);
1762 if (IS_ABSOLUTE_PATH (f))
1763 filename = f;
1764 else
1766 const char *dir;
1767 size_t dir_len;
1768 size_t f_len;
1769 char *p;
1771 if (dir_index == 0)
1772 dir = u->comp_dir;
1773 else if (dir_index - 1 < hdr->dirs_count)
1774 dir = hdr->dirs[dir_index - 1];
1775 else
1777 dwarf_buf_error (line_buf,
1778 ("invalid directory index "
1779 "in line number program"));
1780 return 0;
1782 dir_len = strlen (dir);
1783 f_len = strlen (f);
1784 p = ((char *)
1785 backtrace_alloc (state, dir_len + f_len + 2,
1786 line_buf->error_callback,
1787 line_buf->data));
1788 if (p == NULL)
1789 return 0;
1790 memcpy (p, dir, dir_len);
1791 /* FIXME: If we are on a DOS-based file system,
1792 and the directory or the file name use
1793 backslashes, then we should use a backslash
1794 here. */
1795 p[dir_len] = '/';
1796 memcpy (p + dir_len + 1, f, f_len + 1);
1797 filename = p;
1800 break;
1801 case DW_LNE_set_discriminator:
1802 /* We don't care about discriminators. */
1803 read_uleb128 (line_buf);
1804 break;
1805 default:
1806 if (!advance (line_buf, len - 1))
1807 return 0;
1808 break;
1811 else
1813 switch (op)
1815 case DW_LNS_copy:
1816 add_line (state, ddata, address, filename, lineno,
1817 line_buf->error_callback, line_buf->data, vec);
1818 break;
1819 case DW_LNS_advance_pc:
1821 uint64_t advance;
1823 advance = read_uleb128 (line_buf);
1824 address += (hdr->min_insn_len * (op_index + advance)
1825 / hdr->max_ops_per_insn);
1826 op_index = (op_index + advance) % hdr->max_ops_per_insn;
1828 break;
1829 case DW_LNS_advance_line:
1830 lineno += (int) read_sleb128 (line_buf);
1831 break;
1832 case DW_LNS_set_file:
1834 uint64_t fileno;
1836 fileno = read_uleb128 (line_buf);
1837 if (fileno == 0)
1838 filename = "";
1839 else
1841 if (fileno - 1 >= hdr->filenames_count)
1843 dwarf_buf_error (line_buf,
1844 ("invalid file number in "
1845 "line number program"));
1846 return 0;
1848 filename = hdr->filenames[fileno - 1];
1851 break;
1852 case DW_LNS_set_column:
1853 read_uleb128 (line_buf);
1854 break;
1855 case DW_LNS_negate_stmt:
1856 break;
1857 case DW_LNS_set_basic_block:
1858 break;
1859 case DW_LNS_const_add_pc:
1861 unsigned int advance;
1863 op = 255 - hdr->opcode_base;
1864 advance = op / hdr->line_range;
1865 address += (hdr->min_insn_len * (op_index + advance)
1866 / hdr->max_ops_per_insn);
1867 op_index = (op_index + advance) % hdr->max_ops_per_insn;
1869 break;
1870 case DW_LNS_fixed_advance_pc:
1871 address += read_uint16 (line_buf);
1872 op_index = 0;
1873 break;
1874 case DW_LNS_set_prologue_end:
1875 break;
1876 case DW_LNS_set_epilogue_begin:
1877 break;
1878 case DW_LNS_set_isa:
1879 read_uleb128 (line_buf);
1880 break;
1881 default:
1883 unsigned int i;
1885 for (i = hdr->opcode_lengths[op - 1]; i > 0; --i)
1886 read_uleb128 (line_buf);
1888 break;
1893 return 1;
1896 /* Read the line number information for a compilation unit. Returns 1
1897 on success, 0 on failure. */
1899 static int
1900 read_line_info (struct backtrace_state *state, struct dwarf_data *ddata,
1901 backtrace_error_callback error_callback, void *data,
1902 struct unit *u, struct line_header *hdr, struct line **lines,
1903 size_t *lines_count)
1905 struct line_vector vec;
1906 struct dwarf_buf line_buf;
1907 uint64_t len;
1908 int is_dwarf64;
1909 struct line *ln;
1911 memset (&vec.vec, 0, sizeof vec.vec);
1912 vec.count = 0;
1914 memset (hdr, 0, sizeof *hdr);
1916 if (u->lineoff != (off_t) (size_t) u->lineoff
1917 || (size_t) u->lineoff >= ddata->dwarf_line_size)
1919 error_callback (data, "unit line offset out of range", 0);
1920 goto fail;
1923 line_buf.name = ".debug_line";
1924 line_buf.start = ddata->dwarf_line;
1925 line_buf.buf = ddata->dwarf_line + u->lineoff;
1926 line_buf.left = ddata->dwarf_line_size - u->lineoff;
1927 line_buf.is_bigendian = ddata->is_bigendian;
1928 line_buf.error_callback = error_callback;
1929 line_buf.data = data;
1930 line_buf.reported_underflow = 0;
1932 is_dwarf64 = 0;
1933 len = read_uint32 (&line_buf);
1934 if (len == 0xffffffff)
1936 len = read_uint64 (&line_buf);
1937 is_dwarf64 = 1;
1939 line_buf.left = len;
1941 if (!read_line_header (state, u, is_dwarf64, &line_buf, hdr))
1942 goto fail;
1944 if (!read_line_program (state, ddata, u, hdr, &line_buf, &vec))
1945 goto fail;
1947 if (line_buf.reported_underflow)
1948 goto fail;
1950 if (vec.count == 0)
1952 /* This is not a failure in the sense of a generating an error,
1953 but it is a failure in that sense that we have no useful
1954 information. */
1955 goto fail;
1958 /* Allocate one extra entry at the end. */
1959 ln = ((struct line *)
1960 backtrace_vector_grow (state, sizeof (struct line), error_callback,
1961 data, &vec.vec));
1962 if (ln == NULL)
1963 goto fail;
1964 ln->pc = (uintptr_t) -1;
1965 ln->filename = NULL;
1966 ln->lineno = 0;
1968 if (!backtrace_vector_release (state, &vec.vec, error_callback, data))
1969 goto fail;
1971 ln = (struct line *) vec.vec.base;
1972 qsort (ln, vec.count, sizeof (struct line), line_compare);
1974 *lines = ln;
1975 *lines_count = vec.count;
1977 return 1;
1979 fail:
1980 vec.vec.alc += vec.vec.size;
1981 vec.vec.size = 0;
1982 backtrace_vector_release (state, &vec.vec, error_callback, data);
1983 free_line_header (state, hdr, error_callback, data);
1984 *lines = (struct line *) (uintptr_t) -1;
1985 *lines_count = 0;
1986 return 0;
1989 /* Read the name of a function from a DIE referenced by a
1990 DW_AT_abstract_origin or DW_AT_specification tag. OFFSET is within
1991 the same compilation unit. */
1993 static const char *
1994 read_referenced_name (struct dwarf_data *ddata, struct unit *u,
1995 uint64_t offset, backtrace_error_callback error_callback,
1996 void *data)
1998 struct dwarf_buf unit_buf;
1999 uint64_t code;
2000 const struct abbrev *abbrev;
2001 const char *ret;
2002 size_t i;
2004 /* OFFSET is from the start of the data for this compilation unit.
2005 U->unit_data is the data, but it starts U->unit_data_offset bytes
2006 from the beginning. */
2008 if (offset < u->unit_data_offset
2009 || offset - u->unit_data_offset >= u->unit_data_len)
2011 error_callback (data,
2012 "abstract origin or specification out of range",
2014 return NULL;
2017 offset -= u->unit_data_offset;
2019 unit_buf.name = ".debug_info";
2020 unit_buf.start = ddata->dwarf_info;
2021 unit_buf.buf = u->unit_data + offset;
2022 unit_buf.left = u->unit_data_len - offset;
2023 unit_buf.is_bigendian = ddata->is_bigendian;
2024 unit_buf.error_callback = error_callback;
2025 unit_buf.data = data;
2026 unit_buf.reported_underflow = 0;
2028 code = read_uleb128 (&unit_buf);
2029 if (code == 0)
2031 dwarf_buf_error (&unit_buf, "invalid abstract origin or specification");
2032 return NULL;
2035 abbrev = lookup_abbrev (&u->abbrevs, code, error_callback, data);
2036 if (abbrev == NULL)
2037 return NULL;
2039 ret = NULL;
2040 for (i = 0; i < abbrev->num_attrs; ++i)
2042 struct attr_val val;
2044 if (!read_attribute (abbrev->attrs[i].form, &unit_buf,
2045 u->is_dwarf64, u->version, u->addrsize,
2046 ddata->dwarf_str, ddata->dwarf_str_size,
2047 &val))
2048 return NULL;
2050 switch (abbrev->attrs[i].name)
2052 case DW_AT_name:
2053 /* We prefer the linkage name if get one. */
2054 if (val.encoding == ATTR_VAL_STRING)
2055 ret = val.u.string;
2056 break;
2058 case DW_AT_linkage_name:
2059 case DW_AT_MIPS_linkage_name:
2060 if (val.encoding == ATTR_VAL_STRING)
2061 return val.u.string;
2062 break;
2064 case DW_AT_specification:
2065 if (abbrev->attrs[i].form == DW_FORM_ref_addr
2066 || abbrev->attrs[i].form == DW_FORM_ref_sig8)
2068 /* This refers to a specification defined in some other
2069 compilation unit. We can handle this case if we
2070 must, but it's harder. */
2071 break;
2073 if (val.encoding == ATTR_VAL_UINT
2074 || val.encoding == ATTR_VAL_REF_UNIT)
2076 const char *name;
2078 name = read_referenced_name (ddata, u, val.u.uint,
2079 error_callback, data);
2080 if (name != NULL)
2081 ret = name;
2083 break;
2085 default:
2086 break;
2090 return ret;
2093 /* Add a single range to U that maps to function. Returns 1 on
2094 success, 0 on error. */
2096 static int
2097 add_function_range (struct backtrace_state *state, struct dwarf_data *ddata,
2098 struct function *function, uint64_t lowpc, uint64_t highpc,
2099 backtrace_error_callback error_callback,
2100 void *data, struct function_vector *vec)
2102 struct function_addrs *p;
2104 /* Add in the base address here, so that we can look up the PC
2105 directly. */
2106 lowpc += ddata->base_address;
2107 highpc += ddata->base_address;
2109 if (vec->count > 0)
2111 p = (struct function_addrs *) vec->vec.base + vec->count - 1;
2112 if ((lowpc == p->high || lowpc == p->high + 1)
2113 && function == p->function)
2115 if (highpc > p->high)
2116 p->high = highpc;
2117 return 1;
2121 p = ((struct function_addrs *)
2122 backtrace_vector_grow (state, sizeof (struct function_addrs),
2123 error_callback, data, &vec->vec));
2124 if (p == NULL)
2125 return 0;
2127 p->low = lowpc;
2128 p->high = highpc;
2129 p->function = function;
2130 ++vec->count;
2131 return 1;
2134 /* Add PC ranges to U that map to FUNCTION. Returns 1 on success, 0
2135 on error. */
2137 static int
2138 add_function_ranges (struct backtrace_state *state, struct dwarf_data *ddata,
2139 struct unit *u, struct function *function,
2140 uint64_t ranges, uint64_t base,
2141 backtrace_error_callback error_callback, void *data,
2142 struct function_vector *vec)
2144 struct dwarf_buf ranges_buf;
2146 if (ranges >= ddata->dwarf_ranges_size)
2148 error_callback (data, "function ranges offset out of range", 0);
2149 return 0;
2152 ranges_buf.name = ".debug_ranges";
2153 ranges_buf.start = ddata->dwarf_ranges;
2154 ranges_buf.buf = ddata->dwarf_ranges + ranges;
2155 ranges_buf.left = ddata->dwarf_ranges_size - ranges;
2156 ranges_buf.is_bigendian = ddata->is_bigendian;
2157 ranges_buf.error_callback = error_callback;
2158 ranges_buf.data = data;
2159 ranges_buf.reported_underflow = 0;
2161 while (1)
2163 uint64_t low;
2164 uint64_t high;
2166 if (ranges_buf.reported_underflow)
2167 return 0;
2169 low = read_address (&ranges_buf, u->addrsize);
2170 high = read_address (&ranges_buf, u->addrsize);
2172 if (low == 0 && high == 0)
2173 break;
2175 if (is_highest_address (low, u->addrsize))
2176 base = high;
2177 else
2179 if (!add_function_range (state, ddata, function, low + base,
2180 high + base, error_callback, data, vec))
2181 return 0;
2185 if (ranges_buf.reported_underflow)
2186 return 0;
2188 return 1;
2191 /* Read one entry plus all its children. Add function addresses to
2192 VEC. Returns 1 on success, 0 on error. */
2194 static int
2195 read_function_entry (struct backtrace_state *state, struct dwarf_data *ddata,
2196 struct unit *u, uint64_t base, struct dwarf_buf *unit_buf,
2197 const struct line_header *lhdr,
2198 backtrace_error_callback error_callback, void *data,
2199 struct function_vector *vec)
2201 while (unit_buf->left > 0)
2203 uint64_t code;
2204 const struct abbrev *abbrev;
2205 int is_function;
2206 struct function *function;
2207 size_t i;
2208 uint64_t lowpc;
2209 int have_lowpc;
2210 uint64_t highpc;
2211 int have_highpc;
2212 int highpc_is_relative;
2213 uint64_t ranges;
2214 int have_ranges;
2216 code = read_uleb128 (unit_buf);
2217 if (code == 0)
2218 return 1;
2220 abbrev = lookup_abbrev (&u->abbrevs, code, error_callback, data);
2221 if (abbrev == NULL)
2222 return 0;
2224 is_function = (abbrev->tag == DW_TAG_subprogram
2225 || abbrev->tag == DW_TAG_entry_point
2226 || abbrev->tag == DW_TAG_inlined_subroutine);
2228 function = NULL;
2229 if (is_function)
2231 function = ((struct function *)
2232 backtrace_alloc (state, sizeof *function,
2233 error_callback, data));
2234 if (function == NULL)
2235 return 0;
2236 memset (function, 0, sizeof *function);
2239 lowpc = 0;
2240 have_lowpc = 0;
2241 highpc = 0;
2242 have_highpc = 0;
2243 highpc_is_relative = 0;
2244 ranges = 0;
2245 have_ranges = 0;
2246 for (i = 0; i < abbrev->num_attrs; ++i)
2248 struct attr_val val;
2250 if (!read_attribute (abbrev->attrs[i].form, unit_buf,
2251 u->is_dwarf64, u->version, u->addrsize,
2252 ddata->dwarf_str, ddata->dwarf_str_size,
2253 &val))
2254 return 0;
2256 /* The compile unit sets the base address for any address
2257 ranges in the function entries. */
2258 if (abbrev->tag == DW_TAG_compile_unit
2259 && abbrev->attrs[i].name == DW_AT_low_pc
2260 && val.encoding == ATTR_VAL_ADDRESS)
2261 base = val.u.uint;
2263 if (is_function)
2265 switch (abbrev->attrs[i].name)
2267 case DW_AT_call_file:
2268 if (val.encoding == ATTR_VAL_UINT)
2270 if (val.u.uint == 0)
2271 function->caller_filename = "";
2272 else
2274 if (val.u.uint - 1 >= lhdr->filenames_count)
2276 dwarf_buf_error (unit_buf,
2277 ("invalid file number in "
2278 "DW_AT_call_file attribute"));
2279 return 0;
2281 function->caller_filename =
2282 lhdr->filenames[val.u.uint - 1];
2285 break;
2287 case DW_AT_call_line:
2288 if (val.encoding == ATTR_VAL_UINT)
2289 function->caller_lineno = val.u.uint;
2290 break;
2292 case DW_AT_abstract_origin:
2293 case DW_AT_specification:
2294 if (abbrev->attrs[i].form == DW_FORM_ref_addr
2295 || abbrev->attrs[i].form == DW_FORM_ref_sig8)
2297 /* This refers to an abstract origin defined in
2298 some other compilation unit. We can handle
2299 this case if we must, but it's harder. */
2300 break;
2302 if (val.encoding == ATTR_VAL_UINT
2303 || val.encoding == ATTR_VAL_REF_UNIT)
2305 const char *name;
2307 name = read_referenced_name (ddata, u, val.u.uint,
2308 error_callback, data);
2309 if (name != NULL)
2310 function->name = name;
2312 break;
2314 case DW_AT_name:
2315 if (val.encoding == ATTR_VAL_STRING)
2317 /* Don't override a name we found in some other
2318 way, as it will normally be more
2319 useful--e.g., this name is normally not
2320 mangled. */
2321 if (function->name == NULL)
2322 function->name = val.u.string;
2324 break;
2326 case DW_AT_linkage_name:
2327 case DW_AT_MIPS_linkage_name:
2328 if (val.encoding == ATTR_VAL_STRING)
2329 function->name = val.u.string;
2330 break;
2332 case DW_AT_low_pc:
2333 if (val.encoding == ATTR_VAL_ADDRESS)
2335 lowpc = val.u.uint;
2336 have_lowpc = 1;
2338 break;
2340 case DW_AT_high_pc:
2341 if (val.encoding == ATTR_VAL_ADDRESS)
2343 highpc = val.u.uint;
2344 have_highpc = 1;
2346 else if (val.encoding == ATTR_VAL_UINT)
2348 highpc = val.u.uint;
2349 have_highpc = 1;
2350 highpc_is_relative = 1;
2352 break;
2354 case DW_AT_ranges:
2355 if (val.encoding == ATTR_VAL_UINT
2356 || val.encoding == ATTR_VAL_REF_SECTION)
2358 ranges = val.u.uint;
2359 have_ranges = 1;
2361 break;
2363 default:
2364 break;
2369 /* If we couldn't find a name for the function, we have no use
2370 for it. */
2371 if (is_function && function->name == NULL)
2373 backtrace_free (state, function, sizeof *function,
2374 error_callback, data);
2375 is_function = 0;
2378 if (is_function)
2380 if (have_ranges)
2382 if (!add_function_ranges (state, ddata, u, function, ranges,
2383 base, error_callback, data, vec))
2384 return 0;
2386 else if (have_lowpc && have_highpc)
2388 if (highpc_is_relative)
2389 highpc += lowpc;
2390 if (!add_function_range (state, ddata, function, lowpc, highpc,
2391 error_callback, data, vec))
2392 return 0;
2394 else
2396 backtrace_free (state, function, sizeof *function,
2397 error_callback, data);
2398 is_function = 0;
2402 if (abbrev->has_children)
2404 if (!is_function)
2406 if (!read_function_entry (state, ddata, u, base, unit_buf, lhdr,
2407 error_callback, data, vec))
2408 return 0;
2410 else
2412 struct function_vector fvec;
2414 /* Gather any information for inlined functions in
2415 FVEC. */
2417 memset (&fvec, 0, sizeof fvec);
2419 if (!read_function_entry (state, ddata, u, base, unit_buf, lhdr,
2420 error_callback, data, &fvec))
2421 return 0;
2423 if (fvec.count > 0)
2425 struct function_addrs *faddrs;
2427 if (!backtrace_vector_release (state, &fvec.vec,
2428 error_callback, data))
2429 return 0;
2431 faddrs = (struct function_addrs *) fvec.vec.base;
2432 qsort (faddrs, fvec.count,
2433 sizeof (struct function_addrs),
2434 function_addrs_compare);
2436 function->function_addrs = faddrs;
2437 function->function_addrs_count = fvec.count;
2443 return 1;
2446 /* Read function name information for a compilation unit. We look
2447 through the whole unit looking for function tags. */
2449 static void
2450 read_function_info (struct backtrace_state *state, struct dwarf_data *ddata,
2451 const struct line_header *lhdr,
2452 backtrace_error_callback error_callback, void *data,
2453 struct unit *u, struct function_vector *fvec,
2454 struct function_addrs **ret_addrs,
2455 size_t *ret_addrs_count)
2457 struct dwarf_buf unit_buf;
2458 struct function_addrs *addrs;
2459 size_t addrs_count;
2461 unit_buf.name = ".debug_info";
2462 unit_buf.start = ddata->dwarf_info;
2463 unit_buf.buf = u->unit_data;
2464 unit_buf.left = u->unit_data_len;
2465 unit_buf.is_bigendian = ddata->is_bigendian;
2466 unit_buf.error_callback = error_callback;
2467 unit_buf.data = data;
2468 unit_buf.reported_underflow = 0;
2470 while (unit_buf.left > 0)
2472 if (!read_function_entry (state, ddata, u, 0, &unit_buf, lhdr,
2473 error_callback, data, fvec))
2474 return;
2477 if (fvec->count == 0)
2478 return;
2480 addrs = (struct function_addrs *) fvec->vec.base;
2481 addrs_count = fvec->count;
2483 /* Finish this list of addresses, but leave the remaining space in
2484 the vector available for the next function unit. */
2485 backtrace_vector_finish (state, &fvec->vec);
2486 fvec->count = 0;
2488 qsort (addrs, addrs_count, sizeof (struct function_addrs),
2489 function_addrs_compare);
2491 *ret_addrs = addrs;
2492 *ret_addrs_count = addrs_count;
2495 /* See if PC is inlined in FUNCTION. If it is, print out the inlined
2496 information, and update FILENAME and LINENO for the caller.
2497 Returns whatever CALLBACK returns, or 0 to keep going. */
2499 static int
2500 report_inlined_functions (uintptr_t pc, struct function *function,
2501 backtrace_full_callback callback, void *data,
2502 const char **filename, int *lineno)
2504 struct function_addrs *function_addrs;
2505 struct function *inlined;
2506 int ret;
2508 if (function->function_addrs_count == 0)
2509 return 0;
2511 function_addrs = ((struct function_addrs *)
2512 bsearch (&pc, function->function_addrs,
2513 function->function_addrs_count,
2514 sizeof (struct function_addrs),
2515 function_addrs_search));
2516 if (function_addrs == NULL)
2517 return 0;
2519 while (((size_t) (function_addrs - function->function_addrs) + 1
2520 < function->function_addrs_count)
2521 && pc >= (function_addrs + 1)->low
2522 && pc < (function_addrs + 1)->high)
2523 ++function_addrs;
2525 /* We found an inlined call. */
2527 inlined = function_addrs->function;
2529 /* Report any calls inlined into this one. */
2530 ret = report_inlined_functions (pc, inlined, callback, data,
2531 filename, lineno);
2532 if (ret != 0)
2533 return ret;
2535 /* Report this inlined call. */
2536 ret = callback (data, pc, *filename, *lineno, inlined->name);
2537 if (ret != 0)
2538 return ret;
2540 /* Our caller will report the caller of the inlined function; tell
2541 it the appropriate filename and line number. */
2542 *filename = inlined->caller_filename;
2543 *lineno = inlined->caller_lineno;
2545 return 0;
2548 /* Look for a PC in the DWARF mapping for one module. On success,
2549 call CALLBACK and return whatever it returns. On error, call
2550 ERROR_CALLBACK and return 0. Sets *FOUND to 1 if the PC is found,
2551 0 if not. */
2553 static int
2554 dwarf_lookup_pc (struct backtrace_state *state, struct dwarf_data *ddata,
2555 uintptr_t pc, backtrace_full_callback callback,
2556 backtrace_error_callback error_callback, void *data,
2557 int *found)
2559 struct unit_addrs *entry;
2560 struct unit *u;
2561 int new_data;
2562 struct line *lines;
2563 struct line *ln;
2564 struct function_addrs *function_addrs;
2565 struct function *function;
2566 const char *filename;
2567 int lineno;
2568 int ret;
2570 *found = 1;
2572 /* Find an address range that includes PC. */
2573 entry = bsearch (&pc, ddata->addrs, ddata->addrs_count,
2574 sizeof (struct unit_addrs), unit_addrs_search);
2576 if (entry == NULL)
2578 *found = 0;
2579 return 0;
2582 /* If there are multiple ranges that contain PC, use the last one,
2583 in order to produce predictable results. If we assume that all
2584 ranges are properly nested, then the last range will be the
2585 smallest one. */
2586 while ((size_t) (entry - ddata->addrs) + 1 < ddata->addrs_count
2587 && pc >= (entry + 1)->low
2588 && pc < (entry + 1)->high)
2589 ++entry;
2591 /* We need the lines, lines_count, function_addrs,
2592 function_addrs_count fields of u. If they are not set, we need
2593 to set them. When running in threaded mode, we need to allow for
2594 the possibility that some other thread is setting them
2595 simultaneously. */
2597 u = entry->u;
2598 lines = u->lines;
2600 /* Skip units with no useful line number information by walking
2601 backward. Useless line number information is marked by setting
2602 lines == -1. */
2603 while (entry > ddata->addrs
2604 && pc >= (entry - 1)->low
2605 && pc < (entry - 1)->high)
2607 if (state->threaded)
2609 /* Use __sync_bool_compare_and_swap to do a
2610 load-acquire. */
2611 while (!__sync_bool_compare_and_swap (&u->lines, lines, lines))
2612 lines = u->lines;
2615 if (lines != (struct line *) (uintptr_t) -1)
2616 break;
2618 --entry;
2620 u = entry->u;
2621 lines = u->lines;
2624 /* Do a load-acquire of u->lines. */
2625 if (state->threaded)
2627 /* Use __sync_bool_compare_and_swap to do an atomic load. */
2628 while (!__sync_bool_compare_and_swap (&u->lines, lines, lines))
2629 lines = u->lines;
2632 new_data = 0;
2633 if (lines == NULL)
2635 size_t function_addrs_count;
2636 struct line_header lhdr;
2637 size_t count;
2639 /* We have never read the line information for this unit. Read
2640 it now. */
2642 function_addrs = NULL;
2643 function_addrs_count = 0;
2644 if (read_line_info (state, ddata, error_callback, data, entry->u, &lhdr,
2645 &lines, &count))
2647 read_function_info (state, ddata, &lhdr, error_callback, data,
2648 entry->u, &ddata->fvec, &function_addrs,
2649 &function_addrs_count);
2650 free_line_header (state, &lhdr, error_callback, data);
2651 new_data = 1;
2654 /* Atomically store the information we just read into the unit.
2655 If another thread is simultaneously writing, it presumably
2656 read the same information, and we don't care which one we
2657 wind up with; we just leak the other one. We do have to
2658 write the lines field last, so that the acquire-loads above
2659 ensure that the other fields are set. */
2661 if (!state->threaded)
2663 u->lines_count = count;
2664 u->function_addrs = function_addrs;
2665 u->function_addrs_count = function_addrs_count;
2666 u->lines = lines;
2668 else
2670 __sync_bool_compare_and_swap (&u->lines_count, 0, count);
2671 __sync_bool_compare_and_swap (&u->function_addrs, NULL,
2672 function_addrs);
2673 __sync_bool_compare_and_swap (&u->function_addrs_count, 0,
2674 function_addrs_count);
2675 __sync_bool_compare_and_swap (&u->lines, NULL, lines);
2679 /* Now all fields of U have been initialized. */
2681 if (lines == (struct line *) (uintptr_t) -1)
2683 /* If reading the line number information failed in some way,
2684 try again to see if there is a better compilation unit for
2685 this PC. */
2686 if (new_data)
2687 return dwarf_lookup_pc (state, ddata, pc, callback, error_callback,
2688 data, found);
2689 return callback (data, pc, NULL, 0, NULL);
2692 /* Search for PC within this unit. */
2694 ln = (struct line *) bsearch (&pc, lines, entry->u->lines_count,
2695 sizeof (struct line), line_search);
2696 if (ln == NULL)
2698 error_callback (data, "inconsistent DWARF line number info", 0);
2699 return 0;
2702 /* Search for function name within this unit. */
2704 if (entry->u->function_addrs_count == 0)
2705 return callback (data, pc, ln->filename, ln->lineno, NULL);
2707 function_addrs = ((struct function_addrs *)
2708 bsearch (&pc, entry->u->function_addrs,
2709 entry->u->function_addrs_count,
2710 sizeof (struct function_addrs),
2711 function_addrs_search));
2712 if (function_addrs == NULL)
2713 return callback (data, pc, ln->filename, ln->lineno, NULL);
2715 /* If there are multiple function ranges that contain PC, use the
2716 last one, in order to produce predictable results. */
2718 while (((size_t) (function_addrs - entry->u->function_addrs + 1)
2719 < entry->u->function_addrs_count)
2720 && pc >= (function_addrs + 1)->low
2721 && pc < (function_addrs + 1)->high)
2722 ++function_addrs;
2724 function = function_addrs->function;
2726 filename = ln->filename;
2727 lineno = ln->lineno;
2729 ret = report_inlined_functions (pc, function, callback, data,
2730 &filename, &lineno);
2731 if (ret != 0)
2732 return ret;
2734 return callback (data, pc, filename, lineno, function->name);
2738 /* Return the file/line information for a PC using the DWARF mapping
2739 we built earlier. */
2741 static int
2742 dwarf_fileline (struct backtrace_state *state, uintptr_t pc,
2743 backtrace_full_callback callback,
2744 backtrace_error_callback error_callback, void *data)
2746 struct dwarf_data *ddata;
2747 int found;
2748 int ret;
2750 if (!state->threaded)
2752 for (ddata = (struct dwarf_data *) state->fileline_data;
2753 ddata != NULL;
2754 ddata = ddata->next)
2756 ret = dwarf_lookup_pc (state, ddata, pc, callback, error_callback,
2757 data, &found);
2758 if (ret != 0 || found)
2759 return ret;
2762 else
2764 struct dwarf_data **pp;
2766 pp = (struct dwarf_data **) (void *) &state->fileline_data;
2767 while (1)
2769 ddata = *pp;
2770 /* Atomic load. */
2771 while (!__sync_bool_compare_and_swap (pp, ddata, ddata))
2772 ddata = *pp;
2774 if (ddata == NULL)
2775 break;
2777 ret = dwarf_lookup_pc (state, ddata, pc, callback, error_callback,
2778 data, &found);
2779 if (ret != 0 || found)
2780 return ret;
2782 pp = &ddata->next;
2786 /* FIXME: See if any libraries have been dlopen'ed. */
2788 return callback (data, pc, NULL, 0, NULL);
2791 /* Initialize our data structures from the DWARF debug info for a
2792 file. Return NULL on failure. */
2794 static struct dwarf_data *
2795 build_dwarf_data (struct backtrace_state *state,
2796 uintptr_t base_address,
2797 const unsigned char *dwarf_info,
2798 size_t dwarf_info_size,
2799 const unsigned char *dwarf_line,
2800 size_t dwarf_line_size,
2801 const unsigned char *dwarf_abbrev,
2802 size_t dwarf_abbrev_size,
2803 const unsigned char *dwarf_ranges,
2804 size_t dwarf_ranges_size,
2805 const unsigned char *dwarf_str,
2806 size_t dwarf_str_size,
2807 int is_bigendian,
2808 backtrace_error_callback error_callback,
2809 void *data)
2811 struct unit_addrs_vector addrs_vec;
2812 struct unit_addrs *addrs;
2813 size_t addrs_count;
2814 struct dwarf_data *fdata;
2816 if (!build_address_map (state, base_address, dwarf_info, dwarf_info_size,
2817 dwarf_abbrev, dwarf_abbrev_size, dwarf_ranges,
2818 dwarf_ranges_size, dwarf_str, dwarf_str_size,
2819 is_bigendian, error_callback, data, &addrs_vec))
2820 return NULL;
2822 if (!backtrace_vector_release (state, &addrs_vec.vec, error_callback, data))
2823 return NULL;
2824 addrs = (struct unit_addrs *) addrs_vec.vec.base;
2825 addrs_count = addrs_vec.count;
2826 qsort (addrs, addrs_count, sizeof (struct unit_addrs), unit_addrs_compare);
2828 fdata = ((struct dwarf_data *)
2829 backtrace_alloc (state, sizeof (struct dwarf_data),
2830 error_callback, data));
2831 if (fdata == NULL)
2832 return NULL;
2834 fdata->next = NULL;
2835 fdata->base_address = base_address;
2836 fdata->addrs = addrs;
2837 fdata->addrs_count = addrs_count;
2838 fdata->dwarf_info = dwarf_info;
2839 fdata->dwarf_info_size = dwarf_info_size;
2840 fdata->dwarf_line = dwarf_line;
2841 fdata->dwarf_line_size = dwarf_line_size;
2842 fdata->dwarf_ranges = dwarf_ranges;
2843 fdata->dwarf_ranges_size = dwarf_ranges_size;
2844 fdata->dwarf_str = dwarf_str;
2845 fdata->dwarf_str_size = dwarf_str_size;
2846 fdata->is_bigendian = is_bigendian;
2847 memset (&fdata->fvec, 0, sizeof fdata->fvec);
2849 return fdata;
2852 /* Build our data structures from the DWARF sections for a module.
2853 Set FILELINE_FN and STATE->FILELINE_DATA. Return 1 on success, 0
2854 on failure. */
2857 backtrace_dwarf_add (struct backtrace_state *state,
2858 uintptr_t base_address,
2859 const unsigned char *dwarf_info,
2860 size_t dwarf_info_size,
2861 const unsigned char *dwarf_line,
2862 size_t dwarf_line_size,
2863 const unsigned char *dwarf_abbrev,
2864 size_t dwarf_abbrev_size,
2865 const unsigned char *dwarf_ranges,
2866 size_t dwarf_ranges_size,
2867 const unsigned char *dwarf_str,
2868 size_t dwarf_str_size,
2869 int is_bigendian,
2870 backtrace_error_callback error_callback,
2871 void *data, fileline *fileline_fn)
2873 struct dwarf_data *fdata;
2875 fdata = build_dwarf_data (state, base_address, dwarf_info, dwarf_info_size,
2876 dwarf_line, dwarf_line_size, dwarf_abbrev,
2877 dwarf_abbrev_size, dwarf_ranges, dwarf_ranges_size,
2878 dwarf_str, dwarf_str_size, is_bigendian,
2879 error_callback, data);
2880 if (fdata == NULL)
2881 return 0;
2883 if (!state->threaded)
2885 struct dwarf_data **pp;
2887 for (pp = (struct dwarf_data **) (void *) &state->fileline_data;
2888 *pp != NULL;
2889 pp = &(*pp)->next)
2891 *pp = fdata;
2893 else
2895 while (1)
2897 struct dwarf_data **pp;
2899 pp = (struct dwarf_data **) (void *) &state->fileline_data;
2901 while (1)
2903 struct dwarf_data *p;
2905 /* Atomic load. */
2906 p = *pp;
2907 while (!__sync_bool_compare_and_swap (pp, p, p))
2908 p = *pp;
2910 if (p == NULL)
2911 break;
2913 pp = &p->next;
2916 if (__sync_bool_compare_and_swap (pp, NULL, fdata))
2917 break;
2921 *fileline_fn = dwarf_fileline;
2923 return 1;