2013-01-03 Janus Weil <janus@gcc.gnu.org>
[official-gcc.git] / libbacktrace / dwarf.c
blobbd7419b9c9af08e45031b577a7a5a6ba79879c11
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 /* Avoid warnings about val.u.FIELD may be used uninitialized if
658 this function is inlined. The warnings aren't valid but can
659 occur because the different fields are set and used
660 conditionally. */
661 memset (val, 0, sizeof *val);
663 switch (form)
665 case DW_FORM_addr:
666 val->encoding = ATTR_VAL_ADDRESS;
667 val->u.uint = read_address (buf, addrsize);
668 return 1;
669 case DW_FORM_block2:
670 val->encoding = ATTR_VAL_BLOCK;
671 return advance (buf, read_uint16 (buf));
672 case DW_FORM_block4:
673 val->encoding = ATTR_VAL_BLOCK;
674 return advance (buf, read_uint32 (buf));
675 case DW_FORM_data2:
676 val->encoding = ATTR_VAL_UINT;
677 val->u.uint = read_uint16 (buf);
678 return 1;
679 case DW_FORM_data4:
680 val->encoding = ATTR_VAL_UINT;
681 val->u.uint = read_uint32 (buf);
682 return 1;
683 case DW_FORM_data8:
684 val->encoding = ATTR_VAL_UINT;
685 val->u.uint = read_uint64 (buf);
686 return 1;
687 case DW_FORM_string:
688 val->encoding = ATTR_VAL_STRING;
689 val->u.string = (const char *) buf->buf;
690 return advance (buf, strnlen ((const char *) buf->buf, buf->left) + 1);
691 case DW_FORM_block:
692 val->encoding = ATTR_VAL_BLOCK;
693 return advance (buf, read_uleb128 (buf));
694 case DW_FORM_block1:
695 val->encoding = ATTR_VAL_BLOCK;
696 return advance (buf, read_byte (buf));
697 case DW_FORM_data1:
698 val->encoding = ATTR_VAL_UINT;
699 val->u.uint = read_byte (buf);
700 return 1;
701 case DW_FORM_flag:
702 val->encoding = ATTR_VAL_UINT;
703 val->u.uint = read_byte (buf);
704 return 1;
705 case DW_FORM_sdata:
706 val->encoding = ATTR_VAL_SINT;
707 val->u.sint = read_sleb128 (buf);
708 return 1;
709 case DW_FORM_strp:
711 uint64_t offset;
713 offset = read_offset (buf, is_dwarf64);
714 if (offset >= dwarf_str_size)
716 dwarf_buf_error (buf, "DW_FORM_strp out of range");
717 return 0;
719 val->encoding = ATTR_VAL_STRING;
720 val->u.string = (const char *) dwarf_str + offset;
721 return 1;
723 case DW_FORM_udata:
724 val->encoding = ATTR_VAL_UINT;
725 val->u.uint = read_uleb128 (buf);
726 return 1;
727 case DW_FORM_ref_addr:
728 val->encoding = ATTR_VAL_REF_INFO;
729 if (version == 2)
730 val->u.uint = read_address (buf, addrsize);
731 else
732 val->u.uint = read_offset (buf, is_dwarf64);
733 return 1;
734 case DW_FORM_ref1:
735 val->encoding = ATTR_VAL_REF_UNIT;
736 val->u.uint = read_byte (buf);
737 return 1;
738 case DW_FORM_ref2:
739 val->encoding = ATTR_VAL_REF_UNIT;
740 val->u.uint = read_uint16 (buf);
741 return 1;
742 case DW_FORM_ref4:
743 val->encoding = ATTR_VAL_REF_UNIT;
744 val->u.uint = read_uint32 (buf);
745 return 1;
746 case DW_FORM_ref8:
747 val->encoding = ATTR_VAL_REF_UNIT;
748 val->u.uint = read_uint64 (buf);
749 return 1;
750 case DW_FORM_ref_udata:
751 val->encoding = ATTR_VAL_REF_UNIT;
752 val->u.uint = read_uleb128 (buf);
753 return 1;
754 case DW_FORM_indirect:
756 uint64_t form;
758 form = read_uleb128 (buf);
759 return read_attribute ((enum dwarf_form) form, buf, is_dwarf64,
760 version, addrsize, dwarf_str, dwarf_str_size,
761 val);
763 case DW_FORM_sec_offset:
764 val->encoding = ATTR_VAL_REF_SECTION;
765 val->u.uint = read_offset (buf, is_dwarf64);
766 return 1;
767 case DW_FORM_exprloc:
768 val->encoding = ATTR_VAL_EXPR;
769 return advance (buf, read_uleb128 (buf));
770 case DW_FORM_flag_present:
771 val->encoding = ATTR_VAL_UINT;
772 val->u.uint = 1;
773 return 1;
774 case DW_FORM_ref_sig8:
775 val->encoding = ATTR_VAL_REF_TYPE;
776 val->u.uint = read_uint64 (buf);
777 return 1;
778 case DW_FORM_GNU_addr_index:
779 val->encoding = ATTR_VAL_REF_SECTION;
780 val->u.uint = read_uleb128 (buf);
781 return 1;
782 case DW_FORM_GNU_str_index:
783 val->encoding = ATTR_VAL_REF_SECTION;
784 val->u.uint = read_uleb128 (buf);
785 return 1;
786 case DW_FORM_GNU_ref_alt:
787 val->encoding = ATTR_VAL_REF_SECTION;
788 val->u.uint = read_offset (buf, is_dwarf64);
789 return 1;
790 case DW_FORM_GNU_strp_alt:
791 val->encoding = ATTR_VAL_REF_SECTION;
792 val->u.uint = read_offset (buf, is_dwarf64);
793 return 1;
794 default:
795 dwarf_buf_error (buf, "unrecognized DWARF form");
796 return 0;
800 /* Compare function_addrs for qsort. When ranges are nested, make the
801 smallest one sort last. */
803 static int
804 function_addrs_compare (const void *v1, const void *v2)
806 const struct function_addrs *a1 = (const struct function_addrs *) v1;
807 const struct function_addrs *a2 = (const struct function_addrs *) v2;
809 if (a1->low < a2->low)
810 return -1;
811 if (a1->low > a2->low)
812 return 1;
813 if (a1->high < a2->high)
814 return 1;
815 if (a1->high > a2->high)
816 return -1;
817 return strcmp (a1->function->name, a2->function->name);
820 /* Compare a PC against a function_addrs for bsearch. Note that if
821 there are multiple ranges containing PC, which one will be returned
822 is unpredictable. We compensate for that in dwarf_fileline. */
824 static int
825 function_addrs_search (const void *vkey, const void *ventry)
827 const uintptr_t *key = (const uintptr_t *) vkey;
828 const struct function_addrs *entry = (const struct function_addrs *) ventry;
829 uintptr_t pc;
831 pc = *key;
832 if (pc < entry->low)
833 return -1;
834 else if (pc >= entry->high)
835 return 1;
836 else
837 return 0;
840 /* Add a new compilation unit address range to a vector. Returns 1 on
841 success, 0 on failure. */
843 static int
844 add_unit_addr (struct backtrace_state *state, uintptr_t base_address,
845 struct unit_addrs addrs,
846 backtrace_error_callback error_callback, void *data,
847 struct unit_addrs_vector *vec)
849 struct unit_addrs *p;
851 /* Add in the base address of the module here, so that we can look
852 up the PC directly. */
853 addrs.low += base_address;
854 addrs.high += base_address;
856 /* Try to merge with the last entry. */
857 if (vec->count > 0)
859 p = (struct unit_addrs *) vec->vec.base + (vec->count - 1);
860 if ((addrs.low == p->high || addrs.low == p->high + 1)
861 && addrs.u == p->u)
863 if (addrs.high > p->high)
864 p->high = addrs.high;
865 return 1;
869 p = ((struct unit_addrs *)
870 backtrace_vector_grow (state, sizeof (struct unit_addrs),
871 error_callback, data, &vec->vec));
872 if (p == NULL)
873 return 0;
875 *p = addrs;
876 ++vec->count;
877 return 1;
880 /* Free a unit address vector. */
882 static void
883 free_unit_addrs_vector (struct backtrace_state *state,
884 struct unit_addrs_vector *vec,
885 backtrace_error_callback error_callback, void *data)
887 struct unit_addrs *addrs;
888 size_t i;
890 addrs = (struct unit_addrs *) vec->vec.base;
891 for (i = 0; i < vec->count; ++i)
892 free_abbrevs (state, &addrs[i].u->abbrevs, error_callback, data);
895 /* Compare unit_addrs for qsort. When ranges are nested, make the
896 smallest one sort last. */
898 static int
899 unit_addrs_compare (const void *v1, const void *v2)
901 const struct unit_addrs *a1 = (const struct unit_addrs *) v1;
902 const struct unit_addrs *a2 = (const struct unit_addrs *) v2;
904 if (a1->low < a2->low)
905 return -1;
906 if (a1->low > a2->low)
907 return 1;
908 if (a1->high < a2->high)
909 return 1;
910 if (a1->high > a2->high)
911 return -1;
912 if (a1->u->lineoff < a2->u->lineoff)
913 return -1;
914 if (a1->u->lineoff > a2->u->lineoff)
915 return 1;
916 return 0;
919 /* Compare a PC against a unit_addrs for bsearch. Note that if there
920 are multiple ranges containing PC, which one will be returned is
921 unpredictable. We compensate for that in dwarf_fileline. */
923 static int
924 unit_addrs_search (const void *vkey, const void *ventry)
926 const uintptr_t *key = (const uintptr_t *) vkey;
927 const struct unit_addrs *entry = (const struct unit_addrs *) ventry;
928 uintptr_t pc;
930 pc = *key;
931 if (pc < entry->low)
932 return -1;
933 else if (pc >= entry->high)
934 return 1;
935 else
936 return 0;
939 /* Sort the line vector by PC. We want a stable sort here. We know
940 that the pointers are into the same array, so it is safe to compare
941 them directly. */
943 static int
944 line_compare (const void *v1, const void *v2)
946 const struct line *ln1 = (const struct line *) v1;
947 const struct line *ln2 = (const struct line *) v2;
949 if (ln1->pc < ln2->pc)
950 return -1;
951 else if (ln1->pc > ln2->pc)
952 return 1;
953 else if (ln1 < ln2)
954 return -1;
955 else if (ln1 > ln2)
956 return 1;
957 else
958 return 0;
961 /* Find a PC in a line vector. We always allocate an extra entry at
962 the end of the lines vector, so that this routine can safely look
963 at the next entry. Note that when there are multiple mappings for
964 the same PC value, this will return the last one. */
966 static int
967 line_search (const void *vkey, const void *ventry)
969 const uintptr_t *key = (const uintptr_t *) vkey;
970 const struct line *entry = (const struct line *) ventry;
971 uintptr_t pc;
973 pc = *key;
974 if (pc < entry->pc)
975 return -1;
976 else if (pc >= (entry + 1)->pc)
977 return 1;
978 else
979 return 0;
982 /* Sort the abbrevs by the abbrev code. This function is passed to
983 both qsort and bsearch. */
985 static int
986 abbrev_compare (const void *v1, const void *v2)
988 const struct abbrev *a1 = (const struct abbrev *) v1;
989 const struct abbrev *a2 = (const struct abbrev *) v2;
991 if (a1->code < a2->code)
992 return -1;
993 else if (a1->code > a2->code)
994 return 1;
995 else
997 /* This really shouldn't happen. It means there are two
998 different abbrevs with the same code, and that means we don't
999 know which one lookup_abbrev should return. */
1000 return 0;
1004 /* Read the abbreviation table for a compilation unit. Returns 1 on
1005 success, 0 on failure. */
1007 static int
1008 read_abbrevs (struct backtrace_state *state, uint64_t abbrev_offset,
1009 const unsigned char *dwarf_abbrev, size_t dwarf_abbrev_size,
1010 int is_bigendian, backtrace_error_callback error_callback,
1011 void *data, struct abbrevs *abbrevs)
1013 struct dwarf_buf abbrev_buf;
1014 struct dwarf_buf count_buf;
1015 size_t num_abbrevs;
1017 abbrevs->num_abbrevs = 0;
1018 abbrevs->abbrevs = NULL;
1020 if (abbrev_offset >= dwarf_abbrev_size)
1022 error_callback (data, "abbrev offset out of range", 0);
1023 return 0;
1026 abbrev_buf.name = ".debug_abbrev";
1027 abbrev_buf.start = dwarf_abbrev;
1028 abbrev_buf.buf = dwarf_abbrev + abbrev_offset;
1029 abbrev_buf.left = dwarf_abbrev_size - abbrev_offset;
1030 abbrev_buf.is_bigendian = is_bigendian;
1031 abbrev_buf.error_callback = error_callback;
1032 abbrev_buf.data = data;
1033 abbrev_buf.reported_underflow = 0;
1035 /* Count the number of abbrevs in this list. */
1037 count_buf = abbrev_buf;
1038 num_abbrevs = 0;
1039 while (read_uleb128 (&count_buf) != 0)
1041 if (count_buf.reported_underflow)
1042 return 0;
1043 ++num_abbrevs;
1044 // Skip tag.
1045 read_uleb128 (&count_buf);
1046 // Skip has_children.
1047 read_byte (&count_buf);
1048 // Skip attributes.
1049 while (read_uleb128 (&count_buf) != 0)
1050 read_uleb128 (&count_buf);
1051 // Skip form of last attribute.
1052 read_uleb128 (&count_buf);
1055 if (count_buf.reported_underflow)
1056 return 0;
1058 if (num_abbrevs == 0)
1059 return 1;
1061 abbrevs->num_abbrevs = num_abbrevs;
1062 abbrevs->abbrevs = ((struct abbrev *)
1063 backtrace_alloc (state,
1064 num_abbrevs * sizeof (struct abbrev),
1065 error_callback, data));
1066 if (abbrevs->abbrevs == NULL)
1067 return 0;
1068 memset (abbrevs->abbrevs, 0, num_abbrevs * sizeof (struct abbrev));
1070 num_abbrevs = 0;
1071 while (1)
1073 uint64_t code;
1074 struct abbrev a;
1075 size_t num_attrs;
1076 struct attr *attrs;
1078 if (abbrev_buf.reported_underflow)
1079 goto fail;
1081 code = read_uleb128 (&abbrev_buf);
1082 if (code == 0)
1083 break;
1085 a.code = code;
1086 a.tag = (enum dwarf_tag) read_uleb128 (&abbrev_buf);
1087 a.has_children = read_byte (&abbrev_buf);
1089 count_buf = abbrev_buf;
1090 num_attrs = 0;
1091 while (read_uleb128 (&count_buf) != 0)
1093 ++num_attrs;
1094 read_uleb128 (&count_buf);
1097 if (num_attrs == 0)
1099 attrs = NULL;
1100 read_uleb128 (&abbrev_buf);
1101 read_uleb128 (&abbrev_buf);
1103 else
1105 attrs = ((struct attr *)
1106 backtrace_alloc (state, num_attrs * sizeof *attrs,
1107 error_callback, data));
1108 if (attrs == NULL)
1109 goto fail;
1110 num_attrs = 0;
1111 while (1)
1113 uint64_t name;
1114 uint64_t form;
1116 name = read_uleb128 (&abbrev_buf);
1117 form = read_uleb128 (&abbrev_buf);
1118 if (name == 0)
1119 break;
1120 attrs[num_attrs].name = (enum dwarf_attribute) name;
1121 attrs[num_attrs].form = (enum dwarf_form) form;
1122 ++num_attrs;
1126 a.num_attrs = num_attrs;
1127 a.attrs = attrs;
1129 abbrevs->abbrevs[num_abbrevs] = a;
1130 ++num_abbrevs;
1133 qsort (abbrevs->abbrevs, abbrevs->num_abbrevs, sizeof (struct abbrev),
1134 abbrev_compare);
1136 return 1;
1138 fail:
1139 free_abbrevs (state, abbrevs, error_callback, data);
1140 return 0;
1143 /* Return the abbrev information for an abbrev code. */
1145 static const struct abbrev *
1146 lookup_abbrev (struct abbrevs *abbrevs, uint64_t code,
1147 backtrace_error_callback error_callback, void *data)
1149 struct abbrev key;
1150 void *p;
1152 /* With GCC, where abbrevs are simply numbered in order, we should
1153 be able to just look up the entry. */
1154 if (code - 1 < abbrevs->num_abbrevs
1155 && abbrevs->abbrevs[code - 1].code == code)
1156 return &abbrevs->abbrevs[code - 1];
1158 /* Otherwise we have to search. */
1159 memset (&key, 0, sizeof key);
1160 key.code = code;
1161 p = bsearch (&key, abbrevs->abbrevs, abbrevs->num_abbrevs,
1162 sizeof (struct abbrev), abbrev_compare);
1163 if (p == NULL)
1165 error_callback (data, "invalid abbreviation code", 0);
1166 return NULL;
1168 return (const struct abbrev *) p;
1171 /* Add non-contiguous address ranges for a compilation unit. Returns
1172 1 on success, 0 on failure. */
1174 static int
1175 add_unit_ranges (struct backtrace_state *state, uintptr_t base_address,
1176 struct unit *u, uint64_t ranges, uint64_t base,
1177 int is_bigendian, const unsigned char *dwarf_ranges,
1178 size_t dwarf_ranges_size,
1179 backtrace_error_callback error_callback, void *data,
1180 struct unit_addrs_vector *addrs)
1182 struct dwarf_buf ranges_buf;
1184 if (ranges >= dwarf_ranges_size)
1186 error_callback (data, "ranges offset out of range", 0);
1187 return 0;
1190 ranges_buf.name = ".debug_ranges";
1191 ranges_buf.start = dwarf_ranges;
1192 ranges_buf.buf = dwarf_ranges + ranges;
1193 ranges_buf.left = dwarf_ranges_size - ranges;
1194 ranges_buf.is_bigendian = is_bigendian;
1195 ranges_buf.error_callback = error_callback;
1196 ranges_buf.data = data;
1197 ranges_buf.reported_underflow = 0;
1199 while (1)
1201 uint64_t low;
1202 uint64_t high;
1204 if (ranges_buf.reported_underflow)
1205 return 0;
1207 low = read_address (&ranges_buf, u->addrsize);
1208 high = read_address (&ranges_buf, u->addrsize);
1210 if (low == 0 && high == 0)
1211 break;
1213 if (is_highest_address (low, u->addrsize))
1214 base = high;
1215 else
1217 struct unit_addrs a;
1219 a.low = low + base;
1220 a.high = high + base;
1221 a.u = u;
1222 if (!add_unit_addr (state, base_address, a, error_callback, data,
1223 addrs))
1224 return 0;
1228 if (ranges_buf.reported_underflow)
1229 return 0;
1231 return 1;
1234 /* Build a mapping from address ranges to the compilation units where
1235 the line number information for that range can be found. Returns 1
1236 on success, 0 on failure. */
1238 static int
1239 build_address_map (struct backtrace_state *state, uintptr_t base_address,
1240 const unsigned char *dwarf_info, size_t dwarf_info_size,
1241 const unsigned char *dwarf_abbrev, size_t dwarf_abbrev_size,
1242 const unsigned char *dwarf_ranges, size_t dwarf_ranges_size,
1243 const unsigned char *dwarf_str, size_t dwarf_str_size,
1244 int is_bigendian, backtrace_error_callback error_callback,
1245 void *data, struct unit_addrs_vector *addrs)
1247 struct dwarf_buf info;
1248 struct abbrevs abbrevs;
1250 memset (&addrs->vec, 0, sizeof addrs->vec);
1251 addrs->count = 0;
1253 /* Read through the .debug_info section. FIXME: Should we use the
1254 .debug_aranges section? gdb and addr2line don't use it, but I'm
1255 not sure why. */
1257 info.name = ".debug_info";
1258 info.start = dwarf_info;
1259 info.buf = dwarf_info;
1260 info.left = dwarf_info_size;
1261 info.is_bigendian = is_bigendian;
1262 info.error_callback = error_callback;
1263 info.data = data;
1264 info.reported_underflow = 0;
1266 memset (&abbrevs, 0, sizeof abbrevs);
1267 while (info.left > 0)
1269 const unsigned char *unit_data_start;
1270 uint64_t len;
1271 int is_dwarf64;
1272 struct dwarf_buf unit_buf;
1273 int version;
1274 uint64_t abbrev_offset;
1275 const struct abbrev *abbrev;
1276 int addrsize;
1277 const unsigned char *unit_data;
1278 size_t unit_data_len;
1279 size_t unit_data_offset;
1280 uint64_t code;
1281 size_t i;
1282 uint64_t lowpc;
1283 int have_lowpc;
1284 uint64_t highpc;
1285 int have_highpc;
1286 int highpc_is_relative;
1287 uint64_t ranges;
1288 int have_ranges;
1289 uint64_t lineoff;
1290 int have_lineoff;
1291 const char *comp_dir;
1293 if (info.reported_underflow)
1294 goto fail;
1296 unit_data_start = info.buf;
1298 is_dwarf64 = 0;
1299 len = read_uint32 (&info);
1300 if (len == 0xffffffff)
1302 len = read_uint64 (&info);
1303 is_dwarf64 = 1;
1306 unit_buf = info;
1307 unit_buf.left = len;
1309 if (!advance (&info, len))
1310 goto fail;
1312 version = read_uint16 (&unit_buf);
1313 if (version < 2 || version > 4)
1315 dwarf_buf_error (&unit_buf, "unrecognized DWARF version");
1316 goto fail;
1319 abbrev_offset = read_offset (&unit_buf, is_dwarf64);
1320 if (!read_abbrevs (state, abbrev_offset, dwarf_abbrev, dwarf_abbrev_size,
1321 is_bigendian, error_callback, data, &abbrevs))
1322 goto fail;
1324 addrsize = read_byte (&unit_buf);
1326 unit_data = unit_buf.buf;
1327 unit_data_len = unit_buf.left;
1328 unit_data_offset = unit_buf.buf - unit_data_start;
1330 /* We only look at the first attribute in the compilation unit.
1331 In practice this will be a DW_TAG_compile_unit which will
1332 tell us the PC range and where to find the line number
1333 information. */
1335 code = read_uleb128 (&unit_buf);
1336 abbrev = lookup_abbrev (&abbrevs, code, error_callback, data);
1337 if (abbrev == NULL)
1338 goto fail;
1340 lowpc = 0;
1341 have_lowpc = 0;
1342 highpc = 0;
1343 have_highpc = 0;
1344 highpc_is_relative = 0;
1345 ranges = 0;
1346 have_ranges = 0;
1347 lineoff = 0;
1348 have_lineoff = 0;
1349 comp_dir = NULL;
1350 for (i = 0; i < abbrev->num_attrs; ++i)
1352 struct attr_val val;
1354 if (!read_attribute (abbrev->attrs[i].form, &unit_buf, is_dwarf64,
1355 version, addrsize, dwarf_str, dwarf_str_size,
1356 &val))
1357 goto fail;
1359 switch (abbrev->attrs[i].name)
1361 case DW_AT_low_pc:
1362 if (val.encoding == ATTR_VAL_ADDRESS)
1364 lowpc = val.u.uint;
1365 have_lowpc = 1;
1367 break;
1368 case DW_AT_high_pc:
1369 if (val.encoding == ATTR_VAL_ADDRESS)
1371 highpc = val.u.uint;
1372 have_highpc = 1;
1374 else if (val.encoding == ATTR_VAL_UINT)
1376 highpc = val.u.uint;
1377 have_highpc = 1;
1378 highpc_is_relative = 1;
1380 break;
1381 case DW_AT_ranges:
1382 if (val.encoding == ATTR_VAL_UINT
1383 || val.encoding == ATTR_VAL_REF_SECTION)
1385 ranges = val.u.uint;
1386 have_ranges = 1;
1388 break;
1389 case DW_AT_stmt_list:
1390 if (val.encoding == ATTR_VAL_UINT
1391 || val.encoding == ATTR_VAL_REF_SECTION)
1393 lineoff = val.u.uint;
1394 have_lineoff = 1;
1396 break;
1397 case DW_AT_comp_dir:
1398 if (val.encoding == ATTR_VAL_STRING)
1399 comp_dir = val.u.string;
1400 break;
1401 default:
1402 break;
1406 if (unit_buf.reported_underflow)
1407 goto fail;
1409 if (((have_lowpc && have_highpc) || have_ranges) && have_lineoff)
1411 struct unit *u;
1412 struct unit_addrs a;
1414 u = ((struct unit *)
1415 backtrace_alloc (state, sizeof *u, error_callback, data));
1416 if (u == NULL)
1417 goto fail;
1418 u->unit_data = unit_data;
1419 u->unit_data_len = unit_data_len;
1420 u->unit_data_offset = unit_data_offset;
1421 u->version = version;
1422 u->is_dwarf64 = is_dwarf64;
1423 u->addrsize = addrsize;
1424 u->comp_dir = comp_dir;
1425 u->lineoff = lineoff;
1426 u->abbrevs = abbrevs;
1427 memset (&abbrevs, 0, sizeof abbrevs);
1429 /* The actual line number mappings will be read as
1430 needed. */
1431 u->lines = NULL;
1432 u->lines_count = 0;
1433 u->function_addrs = NULL;
1434 u->function_addrs_count = 0;
1436 if (have_ranges)
1438 if (!add_unit_ranges (state, base_address, u, ranges, lowpc,
1439 is_bigendian, dwarf_ranges,
1440 dwarf_ranges_size, error_callback, data,
1441 addrs))
1443 free_abbrevs (state, &u->abbrevs, error_callback, data);
1444 backtrace_free (state, u, sizeof *u, error_callback, data);
1445 goto fail;
1448 else
1450 if (highpc_is_relative)
1451 highpc += lowpc;
1452 a.low = lowpc;
1453 a.high = highpc;
1454 a.u = u;
1456 if (!add_unit_addr (state, base_address, a, error_callback, data,
1457 addrs))
1459 free_abbrevs (state, &u->abbrevs, error_callback, data);
1460 backtrace_free (state, u, sizeof *u, error_callback, data);
1461 goto fail;
1465 else
1467 free_abbrevs (state, &abbrevs, error_callback, data);
1468 memset (&abbrevs, 0, sizeof abbrevs);
1471 if (info.reported_underflow)
1472 goto fail;
1474 return 1;
1476 fail:
1477 free_abbrevs (state, &abbrevs, error_callback, data);
1478 free_unit_addrs_vector (state, addrs, error_callback, data);
1479 return 0;
1482 /* Add a new mapping to the vector of line mappings that we are
1483 building. Returns 1 on success, 0 on failure. */
1485 static int
1486 add_line (struct backtrace_state *state, struct dwarf_data *ddata,
1487 uintptr_t pc, const char *filename, int lineno,
1488 backtrace_error_callback error_callback, void *data,
1489 struct line_vector *vec)
1491 struct line *ln;
1493 /* If we are adding the same mapping, ignore it. This can happen
1494 when using discriminators. */
1495 if (vec->count > 0)
1497 ln = (struct line *) vec->vec.base + (vec->count - 1);
1498 if (pc == ln->pc && filename == ln->filename && lineno == ln->lineno)
1499 return 1;
1502 ln = ((struct line *)
1503 backtrace_vector_grow (state, sizeof (struct line), error_callback,
1504 data, &vec->vec));
1505 if (ln == NULL)
1506 return 0;
1508 /* Add in the base address here, so that we can look up the PC
1509 directly. */
1510 ln->pc = pc + ddata->base_address;
1512 ln->filename = filename;
1513 ln->lineno = lineno;
1515 ++vec->count;
1517 return 1;
1520 /* Free the line header information. If FREE_FILENAMES is true we
1521 free the file names themselves, otherwise we leave them, as there
1522 may be line structures pointing to them. */
1524 static void
1525 free_line_header (struct backtrace_state *state, struct line_header *hdr,
1526 backtrace_error_callback error_callback, void *data)
1528 backtrace_free (state, hdr->dirs, hdr->dirs_count * sizeof (const char *),
1529 error_callback, data);
1530 backtrace_free (state, hdr->filenames,
1531 hdr->filenames_count * sizeof (char *),
1532 error_callback, data);
1535 /* Read the line header. Return 1 on success, 0 on failure. */
1537 static int
1538 read_line_header (struct backtrace_state *state, struct unit *u,
1539 int is_dwarf64, struct dwarf_buf *line_buf,
1540 struct line_header *hdr)
1542 uint64_t hdrlen;
1543 struct dwarf_buf hdr_buf;
1544 const unsigned char *p;
1545 const unsigned char *pend;
1546 size_t i;
1548 hdr->version = read_uint16 (line_buf);
1549 if (hdr->version < 2 || hdr->version > 4)
1551 dwarf_buf_error (line_buf, "unsupported line number version");
1552 return 0;
1555 hdrlen = read_offset (line_buf, is_dwarf64);
1557 hdr_buf = *line_buf;
1558 hdr_buf.left = hdrlen;
1560 if (!advance (line_buf, hdrlen))
1561 return 0;
1563 hdr->min_insn_len = read_byte (&hdr_buf);
1564 if (hdr->version < 4)
1565 hdr->max_ops_per_insn = 1;
1566 else
1567 hdr->max_ops_per_insn = read_byte (&hdr_buf);
1569 /* We don't care about default_is_stmt. */
1570 read_byte (&hdr_buf);
1572 hdr->line_base = read_sbyte (&hdr_buf);
1573 hdr->line_range = read_byte (&hdr_buf);
1575 hdr->opcode_base = read_byte (&hdr_buf);
1576 hdr->opcode_lengths = hdr_buf.buf;
1577 if (!advance (&hdr_buf, hdr->opcode_base - 1))
1578 return 0;
1580 /* Count the number of directory entries. */
1581 hdr->dirs_count = 0;
1582 p = hdr_buf.buf;
1583 pend = p + hdr_buf.left;
1584 while (p < pend && *p != '\0')
1586 p += strnlen((const char *) p, pend - p) + 1;
1587 ++hdr->dirs_count;
1590 hdr->dirs = ((const char **)
1591 backtrace_alloc (state,
1592 hdr->dirs_count * sizeof (const char *),
1593 line_buf->error_callback, line_buf->data));
1594 if (hdr->dirs == NULL)
1595 return 0;
1597 i = 0;
1598 while (*hdr_buf.buf != '\0')
1600 if (hdr_buf.reported_underflow)
1601 return 0;
1603 hdr->dirs[i] = (const char *) hdr_buf.buf;
1604 ++i;
1605 if (!advance (&hdr_buf,
1606 strnlen ((const char *) hdr_buf.buf, hdr_buf.left) + 1))
1607 return 0;
1609 if (!advance (&hdr_buf, 1))
1610 return 0;
1612 /* Count the number of file entries. */
1613 hdr->filenames_count = 0;
1614 p = hdr_buf.buf;
1615 pend = p + hdr_buf.left;
1616 while (p < pend && *p != '\0')
1618 p += strnlen ((const char *) p, pend - p) + 1;
1619 p += leb128_len (p);
1620 p += leb128_len (p);
1621 p += leb128_len (p);
1622 ++hdr->filenames_count;
1625 hdr->filenames = ((const char **)
1626 backtrace_alloc (state,
1627 hdr->filenames_count * sizeof (char *),
1628 line_buf->error_callback,
1629 line_buf->data));
1630 if (hdr->filenames == NULL)
1631 return 0;
1632 i = 0;
1633 while (*hdr_buf.buf != '\0')
1635 const char *filename;
1636 uint64_t dir_index;
1638 if (hdr_buf.reported_underflow)
1639 return 0;
1641 filename = (const char *) hdr_buf.buf;
1642 if (!advance (&hdr_buf,
1643 strnlen ((const char *) hdr_buf.buf, hdr_buf.left) + 1))
1644 return 0;
1645 dir_index = read_uleb128 (&hdr_buf);
1646 if (IS_ABSOLUTE_PATH (filename))
1647 hdr->filenames[i] = filename;
1648 else
1650 const char *dir;
1651 size_t dir_len;
1652 size_t filename_len;
1653 char *s;
1655 if (dir_index == 0)
1656 dir = u->comp_dir;
1657 else if (dir_index - 1 < hdr->dirs_count)
1658 dir = hdr->dirs[dir_index - 1];
1659 else
1661 dwarf_buf_error (line_buf,
1662 ("invalid directory index in "
1663 "line number program header"));
1664 return 0;
1666 dir_len = strlen (dir);
1667 filename_len = strlen (filename);
1668 s = ((char *)
1669 backtrace_alloc (state, dir_len + filename_len + 2,
1670 line_buf->error_callback, line_buf->data));
1671 if (s == NULL)
1672 return 0;
1673 memcpy (s, dir, dir_len);
1674 /* FIXME: If we are on a DOS-based file system, and the
1675 directory or the file name use backslashes, then we
1676 should use a backslash here. */
1677 s[dir_len] = '/';
1678 memcpy (s + dir_len + 1, filename, filename_len + 1);
1679 hdr->filenames[i] = s;
1682 /* Ignore the modification time and size. */
1683 read_uleb128 (&hdr_buf);
1684 read_uleb128 (&hdr_buf);
1686 ++i;
1689 if (hdr_buf.reported_underflow)
1690 return 0;
1692 return 1;
1695 /* Read the line program, adding line mappings to VEC. Return 1 on
1696 success, 0 on failure. */
1698 static int
1699 read_line_program (struct backtrace_state *state, struct dwarf_data *ddata,
1700 struct unit *u, const struct line_header *hdr,
1701 struct dwarf_buf *line_buf, struct line_vector *vec)
1703 uint64_t address;
1704 unsigned int op_index;
1705 const char *reset_filename;
1706 const char *filename;
1707 int lineno;
1709 address = 0;
1710 op_index = 0;
1711 if (hdr->filenames_count > 0)
1712 reset_filename = hdr->filenames[0];
1713 else
1714 reset_filename = "";
1715 filename = reset_filename;
1716 lineno = 1;
1717 while (line_buf->left > 0)
1719 unsigned int op;
1721 op = read_byte (line_buf);
1722 if (op >= hdr->opcode_base)
1724 unsigned int advance;
1726 /* Special opcode. */
1727 op -= hdr->opcode_base;
1728 advance = op / hdr->line_range;
1729 address += (hdr->min_insn_len * (op_index + advance)
1730 / hdr->max_ops_per_insn);
1731 op_index = (op_index + advance) % hdr->max_ops_per_insn;
1732 lineno += hdr->line_base + (int) (op % hdr->line_range);
1733 add_line (state, ddata, address, filename, lineno,
1734 line_buf->error_callback, line_buf->data, vec);
1736 else if (op == DW_LNS_extended_op)
1738 uint64_t len;
1740 len = read_uleb128 (line_buf);
1741 op = read_byte (line_buf);
1742 switch (op)
1744 case DW_LNE_end_sequence:
1745 /* FIXME: Should we mark the high PC here? It seems
1746 that we already have that information from the
1747 compilation unit. */
1748 address = 0;
1749 op_index = 0;
1750 filename = reset_filename;
1751 lineno = 1;
1752 break;
1753 case DW_LNE_set_address:
1754 address = read_address (line_buf, u->addrsize);
1755 break;
1756 case DW_LNE_define_file:
1758 const char *f;
1759 unsigned int dir_index;
1761 f = (const char *) line_buf->buf;
1762 if (!advance (line_buf, strnlen (f, line_buf->left) + 1))
1763 return 0;
1764 dir_index = read_uleb128 (line_buf);
1765 /* Ignore that time and length. */
1766 read_uleb128 (line_buf);
1767 read_uleb128 (line_buf);
1768 if (IS_ABSOLUTE_PATH (f))
1769 filename = f;
1770 else
1772 const char *dir;
1773 size_t dir_len;
1774 size_t f_len;
1775 char *p;
1777 if (dir_index == 0)
1778 dir = u->comp_dir;
1779 else if (dir_index - 1 < hdr->dirs_count)
1780 dir = hdr->dirs[dir_index - 1];
1781 else
1783 dwarf_buf_error (line_buf,
1784 ("invalid directory index "
1785 "in line number program"));
1786 return 0;
1788 dir_len = strlen (dir);
1789 f_len = strlen (f);
1790 p = ((char *)
1791 backtrace_alloc (state, dir_len + f_len + 2,
1792 line_buf->error_callback,
1793 line_buf->data));
1794 if (p == NULL)
1795 return 0;
1796 memcpy (p, dir, dir_len);
1797 /* FIXME: If we are on a DOS-based file system,
1798 and the directory or the file name use
1799 backslashes, then we should use a backslash
1800 here. */
1801 p[dir_len] = '/';
1802 memcpy (p + dir_len + 1, f, f_len + 1);
1803 filename = p;
1806 break;
1807 case DW_LNE_set_discriminator:
1808 /* We don't care about discriminators. */
1809 read_uleb128 (line_buf);
1810 break;
1811 default:
1812 if (!advance (line_buf, len - 1))
1813 return 0;
1814 break;
1817 else
1819 switch (op)
1821 case DW_LNS_copy:
1822 add_line (state, ddata, address, filename, lineno,
1823 line_buf->error_callback, line_buf->data, vec);
1824 break;
1825 case DW_LNS_advance_pc:
1827 uint64_t advance;
1829 advance = read_uleb128 (line_buf);
1830 address += (hdr->min_insn_len * (op_index + advance)
1831 / hdr->max_ops_per_insn);
1832 op_index = (op_index + advance) % hdr->max_ops_per_insn;
1834 break;
1835 case DW_LNS_advance_line:
1836 lineno += (int) read_sleb128 (line_buf);
1837 break;
1838 case DW_LNS_set_file:
1840 uint64_t fileno;
1842 fileno = read_uleb128 (line_buf);
1843 if (fileno == 0)
1844 filename = "";
1845 else
1847 if (fileno - 1 >= hdr->filenames_count)
1849 dwarf_buf_error (line_buf,
1850 ("invalid file number in "
1851 "line number program"));
1852 return 0;
1854 filename = hdr->filenames[fileno - 1];
1857 break;
1858 case DW_LNS_set_column:
1859 read_uleb128 (line_buf);
1860 break;
1861 case DW_LNS_negate_stmt:
1862 break;
1863 case DW_LNS_set_basic_block:
1864 break;
1865 case DW_LNS_const_add_pc:
1867 unsigned int advance;
1869 op = 255 - hdr->opcode_base;
1870 advance = op / hdr->line_range;
1871 address += (hdr->min_insn_len * (op_index + advance)
1872 / hdr->max_ops_per_insn);
1873 op_index = (op_index + advance) % hdr->max_ops_per_insn;
1875 break;
1876 case DW_LNS_fixed_advance_pc:
1877 address += read_uint16 (line_buf);
1878 op_index = 0;
1879 break;
1880 case DW_LNS_set_prologue_end:
1881 break;
1882 case DW_LNS_set_epilogue_begin:
1883 break;
1884 case DW_LNS_set_isa:
1885 read_uleb128 (line_buf);
1886 break;
1887 default:
1889 unsigned int i;
1891 for (i = hdr->opcode_lengths[op - 1]; i > 0; --i)
1892 read_uleb128 (line_buf);
1894 break;
1899 return 1;
1902 /* Read the line number information for a compilation unit. Returns 1
1903 on success, 0 on failure. */
1905 static int
1906 read_line_info (struct backtrace_state *state, struct dwarf_data *ddata,
1907 backtrace_error_callback error_callback, void *data,
1908 struct unit *u, struct line_header *hdr, struct line **lines,
1909 size_t *lines_count)
1911 struct line_vector vec;
1912 struct dwarf_buf line_buf;
1913 uint64_t len;
1914 int is_dwarf64;
1915 struct line *ln;
1917 memset (&vec.vec, 0, sizeof vec.vec);
1918 vec.count = 0;
1920 memset (hdr, 0, sizeof *hdr);
1922 if (u->lineoff != (off_t) (size_t) u->lineoff
1923 || (size_t) u->lineoff >= ddata->dwarf_line_size)
1925 error_callback (data, "unit line offset out of range", 0);
1926 goto fail;
1929 line_buf.name = ".debug_line";
1930 line_buf.start = ddata->dwarf_line;
1931 line_buf.buf = ddata->dwarf_line + u->lineoff;
1932 line_buf.left = ddata->dwarf_line_size - u->lineoff;
1933 line_buf.is_bigendian = ddata->is_bigendian;
1934 line_buf.error_callback = error_callback;
1935 line_buf.data = data;
1936 line_buf.reported_underflow = 0;
1938 is_dwarf64 = 0;
1939 len = read_uint32 (&line_buf);
1940 if (len == 0xffffffff)
1942 len = read_uint64 (&line_buf);
1943 is_dwarf64 = 1;
1945 line_buf.left = len;
1947 if (!read_line_header (state, u, is_dwarf64, &line_buf, hdr))
1948 goto fail;
1950 if (!read_line_program (state, ddata, u, hdr, &line_buf, &vec))
1951 goto fail;
1953 if (line_buf.reported_underflow)
1954 goto fail;
1956 if (vec.count == 0)
1958 /* This is not a failure in the sense of a generating an error,
1959 but it is a failure in that sense that we have no useful
1960 information. */
1961 goto fail;
1964 /* Allocate one extra entry at the end. */
1965 ln = ((struct line *)
1966 backtrace_vector_grow (state, sizeof (struct line), error_callback,
1967 data, &vec.vec));
1968 if (ln == NULL)
1969 goto fail;
1970 ln->pc = (uintptr_t) -1;
1971 ln->filename = NULL;
1972 ln->lineno = 0;
1974 if (!backtrace_vector_release (state, &vec.vec, error_callback, data))
1975 goto fail;
1977 ln = (struct line *) vec.vec.base;
1978 qsort (ln, vec.count, sizeof (struct line), line_compare);
1980 *lines = ln;
1981 *lines_count = vec.count;
1983 return 1;
1985 fail:
1986 vec.vec.alc += vec.vec.size;
1987 vec.vec.size = 0;
1988 backtrace_vector_release (state, &vec.vec, error_callback, data);
1989 free_line_header (state, hdr, error_callback, data);
1990 *lines = (struct line *) (uintptr_t) -1;
1991 *lines_count = 0;
1992 return 0;
1995 /* Read the name of a function from a DIE referenced by a
1996 DW_AT_abstract_origin or DW_AT_specification tag. OFFSET is within
1997 the same compilation unit. */
1999 static const char *
2000 read_referenced_name (struct dwarf_data *ddata, struct unit *u,
2001 uint64_t offset, backtrace_error_callback error_callback,
2002 void *data)
2004 struct dwarf_buf unit_buf;
2005 uint64_t code;
2006 const struct abbrev *abbrev;
2007 const char *ret;
2008 size_t i;
2010 /* OFFSET is from the start of the data for this compilation unit.
2011 U->unit_data is the data, but it starts U->unit_data_offset bytes
2012 from the beginning. */
2014 if (offset < u->unit_data_offset
2015 || offset - u->unit_data_offset >= u->unit_data_len)
2017 error_callback (data,
2018 "abstract origin or specification out of range",
2020 return NULL;
2023 offset -= u->unit_data_offset;
2025 unit_buf.name = ".debug_info";
2026 unit_buf.start = ddata->dwarf_info;
2027 unit_buf.buf = u->unit_data + offset;
2028 unit_buf.left = u->unit_data_len - offset;
2029 unit_buf.is_bigendian = ddata->is_bigendian;
2030 unit_buf.error_callback = error_callback;
2031 unit_buf.data = data;
2032 unit_buf.reported_underflow = 0;
2034 code = read_uleb128 (&unit_buf);
2035 if (code == 0)
2037 dwarf_buf_error (&unit_buf, "invalid abstract origin or specification");
2038 return NULL;
2041 abbrev = lookup_abbrev (&u->abbrevs, code, error_callback, data);
2042 if (abbrev == NULL)
2043 return NULL;
2045 ret = NULL;
2046 for (i = 0; i < abbrev->num_attrs; ++i)
2048 struct attr_val val;
2050 if (!read_attribute (abbrev->attrs[i].form, &unit_buf,
2051 u->is_dwarf64, u->version, u->addrsize,
2052 ddata->dwarf_str, ddata->dwarf_str_size,
2053 &val))
2054 return NULL;
2056 switch (abbrev->attrs[i].name)
2058 case DW_AT_name:
2059 /* We prefer the linkage name if get one. */
2060 if (val.encoding == ATTR_VAL_STRING)
2061 ret = val.u.string;
2062 break;
2064 case DW_AT_linkage_name:
2065 case DW_AT_MIPS_linkage_name:
2066 if (val.encoding == ATTR_VAL_STRING)
2067 return val.u.string;
2068 break;
2070 case DW_AT_specification:
2071 if (abbrev->attrs[i].form == DW_FORM_ref_addr
2072 || abbrev->attrs[i].form == DW_FORM_ref_sig8)
2074 /* This refers to a specification defined in some other
2075 compilation unit. We can handle this case if we
2076 must, but it's harder. */
2077 break;
2079 if (val.encoding == ATTR_VAL_UINT
2080 || val.encoding == ATTR_VAL_REF_UNIT)
2082 const char *name;
2084 name = read_referenced_name (ddata, u, val.u.uint,
2085 error_callback, data);
2086 if (name != NULL)
2087 ret = name;
2089 break;
2091 default:
2092 break;
2096 return ret;
2099 /* Add a single range to U that maps to function. Returns 1 on
2100 success, 0 on error. */
2102 static int
2103 add_function_range (struct backtrace_state *state, struct dwarf_data *ddata,
2104 struct function *function, uint64_t lowpc, uint64_t highpc,
2105 backtrace_error_callback error_callback,
2106 void *data, struct function_vector *vec)
2108 struct function_addrs *p;
2110 /* Add in the base address here, so that we can look up the PC
2111 directly. */
2112 lowpc += ddata->base_address;
2113 highpc += ddata->base_address;
2115 if (vec->count > 0)
2117 p = (struct function_addrs *) vec->vec.base + vec->count - 1;
2118 if ((lowpc == p->high || lowpc == p->high + 1)
2119 && function == p->function)
2121 if (highpc > p->high)
2122 p->high = highpc;
2123 return 1;
2127 p = ((struct function_addrs *)
2128 backtrace_vector_grow (state, sizeof (struct function_addrs),
2129 error_callback, data, &vec->vec));
2130 if (p == NULL)
2131 return 0;
2133 p->low = lowpc;
2134 p->high = highpc;
2135 p->function = function;
2136 ++vec->count;
2137 return 1;
2140 /* Add PC ranges to U that map to FUNCTION. Returns 1 on success, 0
2141 on error. */
2143 static int
2144 add_function_ranges (struct backtrace_state *state, struct dwarf_data *ddata,
2145 struct unit *u, struct function *function,
2146 uint64_t ranges, uint64_t base,
2147 backtrace_error_callback error_callback, void *data,
2148 struct function_vector *vec)
2150 struct dwarf_buf ranges_buf;
2152 if (ranges >= ddata->dwarf_ranges_size)
2154 error_callback (data, "function ranges offset out of range", 0);
2155 return 0;
2158 ranges_buf.name = ".debug_ranges";
2159 ranges_buf.start = ddata->dwarf_ranges;
2160 ranges_buf.buf = ddata->dwarf_ranges + ranges;
2161 ranges_buf.left = ddata->dwarf_ranges_size - ranges;
2162 ranges_buf.is_bigendian = ddata->is_bigendian;
2163 ranges_buf.error_callback = error_callback;
2164 ranges_buf.data = data;
2165 ranges_buf.reported_underflow = 0;
2167 while (1)
2169 uint64_t low;
2170 uint64_t high;
2172 if (ranges_buf.reported_underflow)
2173 return 0;
2175 low = read_address (&ranges_buf, u->addrsize);
2176 high = read_address (&ranges_buf, u->addrsize);
2178 if (low == 0 && high == 0)
2179 break;
2181 if (is_highest_address (low, u->addrsize))
2182 base = high;
2183 else
2185 if (!add_function_range (state, ddata, function, low + base,
2186 high + base, error_callback, data, vec))
2187 return 0;
2191 if (ranges_buf.reported_underflow)
2192 return 0;
2194 return 1;
2197 /* Read one entry plus all its children. Add function addresses to
2198 VEC. Returns 1 on success, 0 on error. */
2200 static int
2201 read_function_entry (struct backtrace_state *state, struct dwarf_data *ddata,
2202 struct unit *u, uint64_t base, struct dwarf_buf *unit_buf,
2203 const struct line_header *lhdr,
2204 backtrace_error_callback error_callback, void *data,
2205 struct function_vector *vec)
2207 while (unit_buf->left > 0)
2209 uint64_t code;
2210 const struct abbrev *abbrev;
2211 int is_function;
2212 struct function *function;
2213 size_t i;
2214 uint64_t lowpc;
2215 int have_lowpc;
2216 uint64_t highpc;
2217 int have_highpc;
2218 int highpc_is_relative;
2219 uint64_t ranges;
2220 int have_ranges;
2222 code = read_uleb128 (unit_buf);
2223 if (code == 0)
2224 return 1;
2226 abbrev = lookup_abbrev (&u->abbrevs, code, error_callback, data);
2227 if (abbrev == NULL)
2228 return 0;
2230 is_function = (abbrev->tag == DW_TAG_subprogram
2231 || abbrev->tag == DW_TAG_entry_point
2232 || abbrev->tag == DW_TAG_inlined_subroutine);
2234 function = NULL;
2235 if (is_function)
2237 function = ((struct function *)
2238 backtrace_alloc (state, sizeof *function,
2239 error_callback, data));
2240 if (function == NULL)
2241 return 0;
2242 memset (function, 0, sizeof *function);
2245 lowpc = 0;
2246 have_lowpc = 0;
2247 highpc = 0;
2248 have_highpc = 0;
2249 highpc_is_relative = 0;
2250 ranges = 0;
2251 have_ranges = 0;
2252 for (i = 0; i < abbrev->num_attrs; ++i)
2254 struct attr_val val;
2256 if (!read_attribute (abbrev->attrs[i].form, unit_buf,
2257 u->is_dwarf64, u->version, u->addrsize,
2258 ddata->dwarf_str, ddata->dwarf_str_size,
2259 &val))
2260 return 0;
2262 /* The compile unit sets the base address for any address
2263 ranges in the function entries. */
2264 if (abbrev->tag == DW_TAG_compile_unit
2265 && abbrev->attrs[i].name == DW_AT_low_pc
2266 && val.encoding == ATTR_VAL_ADDRESS)
2267 base = val.u.uint;
2269 if (is_function)
2271 switch (abbrev->attrs[i].name)
2273 case DW_AT_call_file:
2274 if (val.encoding == ATTR_VAL_UINT)
2276 if (val.u.uint == 0)
2277 function->caller_filename = "";
2278 else
2280 if (val.u.uint - 1 >= lhdr->filenames_count)
2282 dwarf_buf_error (unit_buf,
2283 ("invalid file number in "
2284 "DW_AT_call_file attribute"));
2285 return 0;
2287 function->caller_filename =
2288 lhdr->filenames[val.u.uint - 1];
2291 break;
2293 case DW_AT_call_line:
2294 if (val.encoding == ATTR_VAL_UINT)
2295 function->caller_lineno = val.u.uint;
2296 break;
2298 case DW_AT_abstract_origin:
2299 case DW_AT_specification:
2300 if (abbrev->attrs[i].form == DW_FORM_ref_addr
2301 || abbrev->attrs[i].form == DW_FORM_ref_sig8)
2303 /* This refers to an abstract origin defined in
2304 some other compilation unit. We can handle
2305 this case if we must, but it's harder. */
2306 break;
2308 if (val.encoding == ATTR_VAL_UINT
2309 || val.encoding == ATTR_VAL_REF_UNIT)
2311 const char *name;
2313 name = read_referenced_name (ddata, u, val.u.uint,
2314 error_callback, data);
2315 if (name != NULL)
2316 function->name = name;
2318 break;
2320 case DW_AT_name:
2321 if (val.encoding == ATTR_VAL_STRING)
2323 /* Don't override a name we found in some other
2324 way, as it will normally be more
2325 useful--e.g., this name is normally not
2326 mangled. */
2327 if (function->name == NULL)
2328 function->name = val.u.string;
2330 break;
2332 case DW_AT_linkage_name:
2333 case DW_AT_MIPS_linkage_name:
2334 if (val.encoding == ATTR_VAL_STRING)
2335 function->name = val.u.string;
2336 break;
2338 case DW_AT_low_pc:
2339 if (val.encoding == ATTR_VAL_ADDRESS)
2341 lowpc = val.u.uint;
2342 have_lowpc = 1;
2344 break;
2346 case DW_AT_high_pc:
2347 if (val.encoding == ATTR_VAL_ADDRESS)
2349 highpc = val.u.uint;
2350 have_highpc = 1;
2352 else if (val.encoding == ATTR_VAL_UINT)
2354 highpc = val.u.uint;
2355 have_highpc = 1;
2356 highpc_is_relative = 1;
2358 break;
2360 case DW_AT_ranges:
2361 if (val.encoding == ATTR_VAL_UINT
2362 || val.encoding == ATTR_VAL_REF_SECTION)
2364 ranges = val.u.uint;
2365 have_ranges = 1;
2367 break;
2369 default:
2370 break;
2375 /* If we couldn't find a name for the function, we have no use
2376 for it. */
2377 if (is_function && function->name == NULL)
2379 backtrace_free (state, function, sizeof *function,
2380 error_callback, data);
2381 is_function = 0;
2384 if (is_function)
2386 if (have_ranges)
2388 if (!add_function_ranges (state, ddata, u, function, ranges,
2389 base, error_callback, data, vec))
2390 return 0;
2392 else if (have_lowpc && have_highpc)
2394 if (highpc_is_relative)
2395 highpc += lowpc;
2396 if (!add_function_range (state, ddata, function, lowpc, highpc,
2397 error_callback, data, vec))
2398 return 0;
2400 else
2402 backtrace_free (state, function, sizeof *function,
2403 error_callback, data);
2404 is_function = 0;
2408 if (abbrev->has_children)
2410 if (!is_function)
2412 if (!read_function_entry (state, ddata, u, base, unit_buf, lhdr,
2413 error_callback, data, vec))
2414 return 0;
2416 else
2418 struct function_vector fvec;
2420 /* Gather any information for inlined functions in
2421 FVEC. */
2423 memset (&fvec, 0, sizeof fvec);
2425 if (!read_function_entry (state, ddata, u, base, unit_buf, lhdr,
2426 error_callback, data, &fvec))
2427 return 0;
2429 if (fvec.count > 0)
2431 struct function_addrs *faddrs;
2433 if (!backtrace_vector_release (state, &fvec.vec,
2434 error_callback, data))
2435 return 0;
2437 faddrs = (struct function_addrs *) fvec.vec.base;
2438 qsort (faddrs, fvec.count,
2439 sizeof (struct function_addrs),
2440 function_addrs_compare);
2442 function->function_addrs = faddrs;
2443 function->function_addrs_count = fvec.count;
2449 return 1;
2452 /* Read function name information for a compilation unit. We look
2453 through the whole unit looking for function tags. */
2455 static void
2456 read_function_info (struct backtrace_state *state, struct dwarf_data *ddata,
2457 const struct line_header *lhdr,
2458 backtrace_error_callback error_callback, void *data,
2459 struct unit *u, struct function_vector *fvec,
2460 struct function_addrs **ret_addrs,
2461 size_t *ret_addrs_count)
2463 struct dwarf_buf unit_buf;
2464 struct function_addrs *addrs;
2465 size_t addrs_count;
2467 unit_buf.name = ".debug_info";
2468 unit_buf.start = ddata->dwarf_info;
2469 unit_buf.buf = u->unit_data;
2470 unit_buf.left = u->unit_data_len;
2471 unit_buf.is_bigendian = ddata->is_bigendian;
2472 unit_buf.error_callback = error_callback;
2473 unit_buf.data = data;
2474 unit_buf.reported_underflow = 0;
2476 while (unit_buf.left > 0)
2478 if (!read_function_entry (state, ddata, u, 0, &unit_buf, lhdr,
2479 error_callback, data, fvec))
2480 return;
2483 if (fvec->count == 0)
2484 return;
2486 addrs = (struct function_addrs *) fvec->vec.base;
2487 addrs_count = fvec->count;
2489 /* Finish this list of addresses, but leave the remaining space in
2490 the vector available for the next function unit. */
2491 backtrace_vector_finish (state, &fvec->vec);
2492 fvec->count = 0;
2494 qsort (addrs, addrs_count, sizeof (struct function_addrs),
2495 function_addrs_compare);
2497 *ret_addrs = addrs;
2498 *ret_addrs_count = addrs_count;
2501 /* See if PC is inlined in FUNCTION. If it is, print out the inlined
2502 information, and update FILENAME and LINENO for the caller.
2503 Returns whatever CALLBACK returns, or 0 to keep going. */
2505 static int
2506 report_inlined_functions (uintptr_t pc, struct function *function,
2507 backtrace_full_callback callback, void *data,
2508 const char **filename, int *lineno)
2510 struct function_addrs *function_addrs;
2511 struct function *inlined;
2512 int ret;
2514 if (function->function_addrs_count == 0)
2515 return 0;
2517 function_addrs = ((struct function_addrs *)
2518 bsearch (&pc, function->function_addrs,
2519 function->function_addrs_count,
2520 sizeof (struct function_addrs),
2521 function_addrs_search));
2522 if (function_addrs == NULL)
2523 return 0;
2525 while (((size_t) (function_addrs - function->function_addrs) + 1
2526 < function->function_addrs_count)
2527 && pc >= (function_addrs + 1)->low
2528 && pc < (function_addrs + 1)->high)
2529 ++function_addrs;
2531 /* We found an inlined call. */
2533 inlined = function_addrs->function;
2535 /* Report any calls inlined into this one. */
2536 ret = report_inlined_functions (pc, inlined, callback, data,
2537 filename, lineno);
2538 if (ret != 0)
2539 return ret;
2541 /* Report this inlined call. */
2542 ret = callback (data, pc, *filename, *lineno, inlined->name);
2543 if (ret != 0)
2544 return ret;
2546 /* Our caller will report the caller of the inlined function; tell
2547 it the appropriate filename and line number. */
2548 *filename = inlined->caller_filename;
2549 *lineno = inlined->caller_lineno;
2551 return 0;
2554 /* Look for a PC in the DWARF mapping for one module. On success,
2555 call CALLBACK and return whatever it returns. On error, call
2556 ERROR_CALLBACK and return 0. Sets *FOUND to 1 if the PC is found,
2557 0 if not. */
2559 static int
2560 dwarf_lookup_pc (struct backtrace_state *state, struct dwarf_data *ddata,
2561 uintptr_t pc, backtrace_full_callback callback,
2562 backtrace_error_callback error_callback, void *data,
2563 int *found)
2565 struct unit_addrs *entry;
2566 struct unit *u;
2567 int new_data;
2568 struct line *lines;
2569 struct line *ln;
2570 struct function_addrs *function_addrs;
2571 struct function *function;
2572 const char *filename;
2573 int lineno;
2574 int ret;
2576 *found = 1;
2578 /* Find an address range that includes PC. */
2579 entry = bsearch (&pc, ddata->addrs, ddata->addrs_count,
2580 sizeof (struct unit_addrs), unit_addrs_search);
2582 if (entry == NULL)
2584 *found = 0;
2585 return 0;
2588 /* If there are multiple ranges that contain PC, use the last one,
2589 in order to produce predictable results. If we assume that all
2590 ranges are properly nested, then the last range will be the
2591 smallest one. */
2592 while ((size_t) (entry - ddata->addrs) + 1 < ddata->addrs_count
2593 && pc >= (entry + 1)->low
2594 && pc < (entry + 1)->high)
2595 ++entry;
2597 /* We need the lines, lines_count, function_addrs,
2598 function_addrs_count fields of u. If they are not set, we need
2599 to set them. When running in threaded mode, we need to allow for
2600 the possibility that some other thread is setting them
2601 simultaneously. */
2603 u = entry->u;
2604 lines = u->lines;
2606 /* Skip units with no useful line number information by walking
2607 backward. Useless line number information is marked by setting
2608 lines == -1. */
2609 while (entry > ddata->addrs
2610 && pc >= (entry - 1)->low
2611 && pc < (entry - 1)->high)
2613 if (state->threaded)
2615 /* Use __sync_bool_compare_and_swap to do a
2616 load-acquire. */
2617 while (!__sync_bool_compare_and_swap (&u->lines, lines, lines))
2618 lines = u->lines;
2621 if (lines != (struct line *) (uintptr_t) -1)
2622 break;
2624 --entry;
2626 u = entry->u;
2627 lines = u->lines;
2630 /* Do a load-acquire of u->lines. */
2631 if (state->threaded)
2633 /* Use __sync_bool_compare_and_swap to do an atomic load. */
2634 while (!__sync_bool_compare_and_swap (&u->lines, lines, lines))
2635 lines = u->lines;
2638 new_data = 0;
2639 if (lines == NULL)
2641 size_t function_addrs_count;
2642 struct line_header lhdr;
2643 size_t count;
2645 /* We have never read the line information for this unit. Read
2646 it now. */
2648 function_addrs = NULL;
2649 function_addrs_count = 0;
2650 if (read_line_info (state, ddata, error_callback, data, entry->u, &lhdr,
2651 &lines, &count))
2653 read_function_info (state, ddata, &lhdr, error_callback, data,
2654 entry->u, &ddata->fvec, &function_addrs,
2655 &function_addrs_count);
2656 free_line_header (state, &lhdr, error_callback, data);
2657 new_data = 1;
2660 /* Atomically store the information we just read into the unit.
2661 If another thread is simultaneously writing, it presumably
2662 read the same information, and we don't care which one we
2663 wind up with; we just leak the other one. We do have to
2664 write the lines field last, so that the acquire-loads above
2665 ensure that the other fields are set. */
2667 if (!state->threaded)
2669 u->lines_count = count;
2670 u->function_addrs = function_addrs;
2671 u->function_addrs_count = function_addrs_count;
2672 u->lines = lines;
2674 else
2676 __sync_bool_compare_and_swap (&u->lines_count, 0, count);
2677 __sync_bool_compare_and_swap (&u->function_addrs, NULL,
2678 function_addrs);
2679 __sync_bool_compare_and_swap (&u->function_addrs_count, 0,
2680 function_addrs_count);
2681 __sync_bool_compare_and_swap (&u->lines, NULL, lines);
2685 /* Now all fields of U have been initialized. */
2687 if (lines == (struct line *) (uintptr_t) -1)
2689 /* If reading the line number information failed in some way,
2690 try again to see if there is a better compilation unit for
2691 this PC. */
2692 if (new_data)
2693 return dwarf_lookup_pc (state, ddata, pc, callback, error_callback,
2694 data, found);
2695 return callback (data, pc, NULL, 0, NULL);
2698 /* Search for PC within this unit. */
2700 ln = (struct line *) bsearch (&pc, lines, entry->u->lines_count,
2701 sizeof (struct line), line_search);
2702 if (ln == NULL)
2704 error_callback (data, "inconsistent DWARF line number info", 0);
2705 return 0;
2708 /* Search for function name within this unit. */
2710 if (entry->u->function_addrs_count == 0)
2711 return callback (data, pc, ln->filename, ln->lineno, NULL);
2713 function_addrs = ((struct function_addrs *)
2714 bsearch (&pc, entry->u->function_addrs,
2715 entry->u->function_addrs_count,
2716 sizeof (struct function_addrs),
2717 function_addrs_search));
2718 if (function_addrs == NULL)
2719 return callback (data, pc, ln->filename, ln->lineno, NULL);
2721 /* If there are multiple function ranges that contain PC, use the
2722 last one, in order to produce predictable results. */
2724 while (((size_t) (function_addrs - entry->u->function_addrs + 1)
2725 < entry->u->function_addrs_count)
2726 && pc >= (function_addrs + 1)->low
2727 && pc < (function_addrs + 1)->high)
2728 ++function_addrs;
2730 function = function_addrs->function;
2732 filename = ln->filename;
2733 lineno = ln->lineno;
2735 ret = report_inlined_functions (pc, function, callback, data,
2736 &filename, &lineno);
2737 if (ret != 0)
2738 return ret;
2740 return callback (data, pc, filename, lineno, function->name);
2744 /* Return the file/line information for a PC using the DWARF mapping
2745 we built earlier. */
2747 static int
2748 dwarf_fileline (struct backtrace_state *state, uintptr_t pc,
2749 backtrace_full_callback callback,
2750 backtrace_error_callback error_callback, void *data)
2752 struct dwarf_data *ddata;
2753 int found;
2754 int ret;
2756 if (!state->threaded)
2758 for (ddata = (struct dwarf_data *) state->fileline_data;
2759 ddata != NULL;
2760 ddata = ddata->next)
2762 ret = dwarf_lookup_pc (state, ddata, pc, callback, error_callback,
2763 data, &found);
2764 if (ret != 0 || found)
2765 return ret;
2768 else
2770 struct dwarf_data **pp;
2772 pp = (struct dwarf_data **) (void *) &state->fileline_data;
2773 while (1)
2775 ddata = *pp;
2776 /* Atomic load. */
2777 while (!__sync_bool_compare_and_swap (pp, ddata, ddata))
2778 ddata = *pp;
2780 if (ddata == NULL)
2781 break;
2783 ret = dwarf_lookup_pc (state, ddata, pc, callback, error_callback,
2784 data, &found);
2785 if (ret != 0 || found)
2786 return ret;
2788 pp = &ddata->next;
2792 /* FIXME: See if any libraries have been dlopen'ed. */
2794 return callback (data, pc, NULL, 0, NULL);
2797 /* Initialize our data structures from the DWARF debug info for a
2798 file. Return NULL on failure. */
2800 static struct dwarf_data *
2801 build_dwarf_data (struct backtrace_state *state,
2802 uintptr_t base_address,
2803 const unsigned char *dwarf_info,
2804 size_t dwarf_info_size,
2805 const unsigned char *dwarf_line,
2806 size_t dwarf_line_size,
2807 const unsigned char *dwarf_abbrev,
2808 size_t dwarf_abbrev_size,
2809 const unsigned char *dwarf_ranges,
2810 size_t dwarf_ranges_size,
2811 const unsigned char *dwarf_str,
2812 size_t dwarf_str_size,
2813 int is_bigendian,
2814 backtrace_error_callback error_callback,
2815 void *data)
2817 struct unit_addrs_vector addrs_vec;
2818 struct unit_addrs *addrs;
2819 size_t addrs_count;
2820 struct dwarf_data *fdata;
2822 if (!build_address_map (state, base_address, dwarf_info, dwarf_info_size,
2823 dwarf_abbrev, dwarf_abbrev_size, dwarf_ranges,
2824 dwarf_ranges_size, dwarf_str, dwarf_str_size,
2825 is_bigendian, error_callback, data, &addrs_vec))
2826 return NULL;
2828 if (!backtrace_vector_release (state, &addrs_vec.vec, error_callback, data))
2829 return NULL;
2830 addrs = (struct unit_addrs *) addrs_vec.vec.base;
2831 addrs_count = addrs_vec.count;
2832 qsort (addrs, addrs_count, sizeof (struct unit_addrs), unit_addrs_compare);
2834 fdata = ((struct dwarf_data *)
2835 backtrace_alloc (state, sizeof (struct dwarf_data),
2836 error_callback, data));
2837 if (fdata == NULL)
2838 return NULL;
2840 fdata->next = NULL;
2841 fdata->base_address = base_address;
2842 fdata->addrs = addrs;
2843 fdata->addrs_count = addrs_count;
2844 fdata->dwarf_info = dwarf_info;
2845 fdata->dwarf_info_size = dwarf_info_size;
2846 fdata->dwarf_line = dwarf_line;
2847 fdata->dwarf_line_size = dwarf_line_size;
2848 fdata->dwarf_ranges = dwarf_ranges;
2849 fdata->dwarf_ranges_size = dwarf_ranges_size;
2850 fdata->dwarf_str = dwarf_str;
2851 fdata->dwarf_str_size = dwarf_str_size;
2852 fdata->is_bigendian = is_bigendian;
2853 memset (&fdata->fvec, 0, sizeof fdata->fvec);
2855 return fdata;
2858 /* Build our data structures from the DWARF sections for a module.
2859 Set FILELINE_FN and STATE->FILELINE_DATA. Return 1 on success, 0
2860 on failure. */
2863 backtrace_dwarf_add (struct backtrace_state *state,
2864 uintptr_t base_address,
2865 const unsigned char *dwarf_info,
2866 size_t dwarf_info_size,
2867 const unsigned char *dwarf_line,
2868 size_t dwarf_line_size,
2869 const unsigned char *dwarf_abbrev,
2870 size_t dwarf_abbrev_size,
2871 const unsigned char *dwarf_ranges,
2872 size_t dwarf_ranges_size,
2873 const unsigned char *dwarf_str,
2874 size_t dwarf_str_size,
2875 int is_bigendian,
2876 backtrace_error_callback error_callback,
2877 void *data, fileline *fileline_fn)
2879 struct dwarf_data *fdata;
2881 fdata = build_dwarf_data (state, base_address, dwarf_info, dwarf_info_size,
2882 dwarf_line, dwarf_line_size, dwarf_abbrev,
2883 dwarf_abbrev_size, dwarf_ranges, dwarf_ranges_size,
2884 dwarf_str, dwarf_str_size, is_bigendian,
2885 error_callback, data);
2886 if (fdata == NULL)
2887 return 0;
2889 if (!state->threaded)
2891 struct dwarf_data **pp;
2893 for (pp = (struct dwarf_data **) (void *) &state->fileline_data;
2894 *pp != NULL;
2895 pp = &(*pp)->next)
2897 *pp = fdata;
2899 else
2901 while (1)
2903 struct dwarf_data **pp;
2905 pp = (struct dwarf_data **) (void *) &state->fileline_data;
2907 while (1)
2909 struct dwarf_data *p;
2911 /* Atomic load. */
2912 p = *pp;
2913 while (!__sync_bool_compare_and_swap (pp, p, p))
2914 p = *pp;
2916 if (p == NULL)
2917 break;
2919 pp = &p->next;
2922 if (__sync_bool_compare_and_swap (pp, NULL, fdata))
2923 break;
2927 *fileline_fn = dwarf_fileline;
2929 return 1;