Daily bump.
[official-gcc.git] / libbacktrace / dwarf.c
blobe32c46885735d6baed838e791a03fba649efb6eb
1 /* dwarf.c -- Get file/line information from DWARF for backtraces.
2 Copyright (C) 2012-2015 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;
214 /* Index of the object in the original array read from the DWARF
215 section, before it has been sorted. The index makes it possible
216 to use Quicksort and maintain stability. */
217 int idx;
220 /* A growable vector of line number information. This is used while
221 reading the line numbers. */
223 struct line_vector
225 /* Memory. This is an array of struct line. */
226 struct backtrace_vector vec;
227 /* Number of valid mappings. */
228 size_t count;
231 /* A function described in the debug info. */
233 struct function
235 /* The name of the function. */
236 const char *name;
237 /* If this is an inlined function, the filename of the call
238 site. */
239 const char *caller_filename;
240 /* If this is an inlined function, the line number of the call
241 site. */
242 int caller_lineno;
243 /* Map PC ranges to inlined functions. */
244 struct function_addrs *function_addrs;
245 size_t function_addrs_count;
248 /* An address range for a function. This maps a PC value to a
249 specific function. */
251 struct function_addrs
253 /* Range is LOW <= PC < HIGH. */
254 uint64_t low;
255 uint64_t high;
256 /* Function for this address range. */
257 struct function *function;
260 /* A growable vector of function address ranges. */
262 struct function_vector
264 /* Memory. This is an array of struct function_addrs. */
265 struct backtrace_vector vec;
266 /* Number of address ranges present. */
267 size_t count;
270 /* A DWARF compilation unit. This only holds the information we need
271 to map a PC to a file and line. */
273 struct unit
275 /* The first entry for this compilation unit. */
276 const unsigned char *unit_data;
277 /* The length of the data for this compilation unit. */
278 size_t unit_data_len;
279 /* The offset of UNIT_DATA from the start of the information for
280 this compilation unit. */
281 size_t unit_data_offset;
282 /* DWARF version. */
283 int version;
284 /* Whether unit is DWARF64. */
285 int is_dwarf64;
286 /* Address size. */
287 int addrsize;
288 /* Offset into line number information. */
289 off_t lineoff;
290 /* Primary source file. */
291 const char *filename;
292 /* Compilation command working directory. */
293 const char *comp_dir;
294 /* Absolute file name, only set if needed. */
295 const char *abs_filename;
296 /* The abbreviations for this unit. */
297 struct abbrevs abbrevs;
299 /* The fields above this point are read in during initialization and
300 may be accessed freely. The fields below this point are read in
301 as needed, and therefore require care, as different threads may
302 try to initialize them simultaneously. */
304 /* PC to line number mapping. This is NULL if the values have not
305 been read. This is (struct line *) -1 if there was an error
306 reading the values. */
307 struct line *lines;
308 /* Number of entries in lines. */
309 size_t lines_count;
310 /* PC ranges to function. */
311 struct function_addrs *function_addrs;
312 size_t function_addrs_count;
315 /* An address range for a compilation unit. This maps a PC value to a
316 specific compilation unit. Note that we invert the representation
317 in DWARF: instead of listing the units and attaching a list of
318 ranges, we list the ranges and have each one point to the unit.
319 This lets us do a binary search to find the unit. */
321 struct unit_addrs
323 /* Range is LOW <= PC < HIGH. */
324 uint64_t low;
325 uint64_t high;
326 /* Compilation unit for this address range. */
327 struct unit *u;
330 /* A growable vector of compilation unit address ranges. */
332 struct unit_addrs_vector
334 /* Memory. This is an array of struct unit_addrs. */
335 struct backtrace_vector vec;
336 /* Number of address ranges present. */
337 size_t count;
340 /* The information we need to map a PC to a file and line. */
342 struct dwarf_data
344 /* The data for the next file we know about. */
345 struct dwarf_data *next;
346 /* The base address for this file. */
347 uintptr_t base_address;
348 /* A sorted list of address ranges. */
349 struct unit_addrs *addrs;
350 /* Number of address ranges in list. */
351 size_t addrs_count;
352 /* The unparsed .debug_info section. */
353 const unsigned char *dwarf_info;
354 size_t dwarf_info_size;
355 /* The unparsed .debug_line section. */
356 const unsigned char *dwarf_line;
357 size_t dwarf_line_size;
358 /* The unparsed .debug_ranges section. */
359 const unsigned char *dwarf_ranges;
360 size_t dwarf_ranges_size;
361 /* The unparsed .debug_str section. */
362 const unsigned char *dwarf_str;
363 size_t dwarf_str_size;
364 /* Whether the data is big-endian or not. */
365 int is_bigendian;
366 /* A vector used for function addresses. We keep this here so that
367 we can grow the vector as we read more functions. */
368 struct function_vector fvec;
371 /* Report an error for a DWARF buffer. */
373 static void
374 dwarf_buf_error (struct dwarf_buf *buf, const char *msg)
376 char b[200];
378 snprintf (b, sizeof b, "%s in %s at %d",
379 msg, buf->name, (int) (buf->buf - buf->start));
380 buf->error_callback (buf->data, b, 0);
383 /* Require at least COUNT bytes in BUF. Return 1 if all is well, 0 on
384 error. */
386 static int
387 require (struct dwarf_buf *buf, size_t count)
389 if (buf->left >= count)
390 return 1;
392 if (!buf->reported_underflow)
394 dwarf_buf_error (buf, "DWARF underflow");
395 buf->reported_underflow = 1;
398 return 0;
401 /* Advance COUNT bytes in BUF. Return 1 if all is well, 0 on
402 error. */
404 static int
405 advance (struct dwarf_buf *buf, size_t count)
407 if (!require (buf, count))
408 return 0;
409 buf->buf += count;
410 buf->left -= count;
411 return 1;
414 /* Read one byte from BUF and advance 1 byte. */
416 static unsigned char
417 read_byte (struct dwarf_buf *buf)
419 const unsigned char *p = buf->buf;
421 if (!advance (buf, 1))
422 return 0;
423 return p[0];
426 /* Read a signed char from BUF and advance 1 byte. */
428 static signed char
429 read_sbyte (struct dwarf_buf *buf)
431 const unsigned char *p = buf->buf;
433 if (!advance (buf, 1))
434 return 0;
435 return (*p ^ 0x80) - 0x80;
438 /* Read a uint16 from BUF and advance 2 bytes. */
440 static uint16_t
441 read_uint16 (struct dwarf_buf *buf)
443 const unsigned char *p = buf->buf;
445 if (!advance (buf, 2))
446 return 0;
447 if (buf->is_bigendian)
448 return ((uint16_t) p[0] << 8) | (uint16_t) p[1];
449 else
450 return ((uint16_t) p[1] << 8) | (uint16_t) p[0];
453 /* Read a uint32 from BUF and advance 4 bytes. */
455 static uint32_t
456 read_uint32 (struct dwarf_buf *buf)
458 const unsigned char *p = buf->buf;
460 if (!advance (buf, 4))
461 return 0;
462 if (buf->is_bigendian)
463 return (((uint32_t) p[0] << 24) | ((uint32_t) p[1] << 16)
464 | ((uint32_t) p[2] << 8) | (uint32_t) p[3]);
465 else
466 return (((uint32_t) p[3] << 24) | ((uint32_t) p[2] << 16)
467 | ((uint32_t) p[1] << 8) | (uint32_t) p[0]);
470 /* Read a uint64 from BUF and advance 8 bytes. */
472 static uint64_t
473 read_uint64 (struct dwarf_buf *buf)
475 const unsigned char *p = buf->buf;
477 if (!advance (buf, 8))
478 return 0;
479 if (buf->is_bigendian)
480 return (((uint64_t) p[0] << 56) | ((uint64_t) p[1] << 48)
481 | ((uint64_t) p[2] << 40) | ((uint64_t) p[3] << 32)
482 | ((uint64_t) p[4] << 24) | ((uint64_t) p[5] << 16)
483 | ((uint64_t) p[6] << 8) | (uint64_t) p[7]);
484 else
485 return (((uint64_t) p[7] << 56) | ((uint64_t) p[6] << 48)
486 | ((uint64_t) p[5] << 40) | ((uint64_t) p[4] << 32)
487 | ((uint64_t) p[3] << 24) | ((uint64_t) p[2] << 16)
488 | ((uint64_t) p[1] << 8) | (uint64_t) p[0]);
491 /* Read an offset from BUF and advance the appropriate number of
492 bytes. */
494 static uint64_t
495 read_offset (struct dwarf_buf *buf, int is_dwarf64)
497 if (is_dwarf64)
498 return read_uint64 (buf);
499 else
500 return read_uint32 (buf);
503 /* Read an address from BUF and advance the appropriate number of
504 bytes. */
506 static uint64_t
507 read_address (struct dwarf_buf *buf, int addrsize)
509 switch (addrsize)
511 case 1:
512 return read_byte (buf);
513 case 2:
514 return read_uint16 (buf);
515 case 4:
516 return read_uint32 (buf);
517 case 8:
518 return read_uint64 (buf);
519 default:
520 dwarf_buf_error (buf, "unrecognized address size");
521 return 0;
525 /* Return whether a value is the highest possible address, given the
526 address size. */
528 static int
529 is_highest_address (uint64_t address, int addrsize)
531 switch (addrsize)
533 case 1:
534 return address == (unsigned char) -1;
535 case 2:
536 return address == (uint16_t) -1;
537 case 4:
538 return address == (uint32_t) -1;
539 case 8:
540 return address == (uint64_t) -1;
541 default:
542 return 0;
546 /* Read an unsigned LEB128 number. */
548 static uint64_t
549 read_uleb128 (struct dwarf_buf *buf)
551 uint64_t ret;
552 unsigned int shift;
553 int overflow;
554 unsigned char b;
556 ret = 0;
557 shift = 0;
558 overflow = 0;
561 const unsigned char *p;
563 p = buf->buf;
564 if (!advance (buf, 1))
565 return 0;
566 b = *p;
567 if (shift < 64)
568 ret |= ((uint64_t) (b & 0x7f)) << shift;
569 else if (!overflow)
571 dwarf_buf_error (buf, "LEB128 overflows uint64_t");
572 overflow = 1;
574 shift += 7;
576 while ((b & 0x80) != 0);
578 return ret;
581 /* Read a signed LEB128 number. */
583 static int64_t
584 read_sleb128 (struct dwarf_buf *buf)
586 uint64_t val;
587 unsigned int shift;
588 int overflow;
589 unsigned char b;
591 val = 0;
592 shift = 0;
593 overflow = 0;
596 const unsigned char *p;
598 p = buf->buf;
599 if (!advance (buf, 1))
600 return 0;
601 b = *p;
602 if (shift < 64)
603 val |= ((uint64_t) (b & 0x7f)) << shift;
604 else if (!overflow)
606 dwarf_buf_error (buf, "signed LEB128 overflows uint64_t");
607 overflow = 1;
609 shift += 7;
611 while ((b & 0x80) != 0);
613 if ((b & 0x40) != 0 && shift < 64)
614 val |= ((uint64_t) -1) << shift;
616 return (int64_t) val;
619 /* Return the length of an LEB128 number. */
621 static size_t
622 leb128_len (const unsigned char *p)
624 size_t ret;
626 ret = 1;
627 while ((*p & 0x80) != 0)
629 ++p;
630 ++ret;
632 return ret;
635 /* Free an abbreviations structure. */
637 static void
638 free_abbrevs (struct backtrace_state *state, struct abbrevs *abbrevs,
639 backtrace_error_callback error_callback, void *data)
641 size_t i;
643 for (i = 0; i < abbrevs->num_abbrevs; ++i)
644 backtrace_free (state, abbrevs->abbrevs[i].attrs,
645 abbrevs->abbrevs[i].num_attrs * sizeof (struct attr),
646 error_callback, data);
647 backtrace_free (state, abbrevs->abbrevs,
648 abbrevs->num_abbrevs * sizeof (struct abbrev),
649 error_callback, data);
650 abbrevs->num_abbrevs = 0;
651 abbrevs->abbrevs = NULL;
654 /* Read an attribute value. Returns 1 on success, 0 on failure. If
655 the value can be represented as a uint64_t, sets *VAL and sets
656 *IS_VALID to 1. We don't try to store the value of other attribute
657 forms, because we don't care about them. */
659 static int
660 read_attribute (enum dwarf_form form, struct dwarf_buf *buf,
661 int is_dwarf64, int version, int addrsize,
662 const unsigned char *dwarf_str, size_t dwarf_str_size,
663 struct attr_val *val)
665 /* Avoid warnings about val.u.FIELD may be used uninitialized if
666 this function is inlined. The warnings aren't valid but can
667 occur because the different fields are set and used
668 conditionally. */
669 memset (val, 0, sizeof *val);
671 switch (form)
673 case DW_FORM_addr:
674 val->encoding = ATTR_VAL_ADDRESS;
675 val->u.uint = read_address (buf, addrsize);
676 return 1;
677 case DW_FORM_block2:
678 val->encoding = ATTR_VAL_BLOCK;
679 return advance (buf, read_uint16 (buf));
680 case DW_FORM_block4:
681 val->encoding = ATTR_VAL_BLOCK;
682 return advance (buf, read_uint32 (buf));
683 case DW_FORM_data2:
684 val->encoding = ATTR_VAL_UINT;
685 val->u.uint = read_uint16 (buf);
686 return 1;
687 case DW_FORM_data4:
688 val->encoding = ATTR_VAL_UINT;
689 val->u.uint = read_uint32 (buf);
690 return 1;
691 case DW_FORM_data8:
692 val->encoding = ATTR_VAL_UINT;
693 val->u.uint = read_uint64 (buf);
694 return 1;
695 case DW_FORM_string:
696 val->encoding = ATTR_VAL_STRING;
697 val->u.string = (const char *) buf->buf;
698 return advance (buf, strnlen ((const char *) buf->buf, buf->left) + 1);
699 case DW_FORM_block:
700 val->encoding = ATTR_VAL_BLOCK;
701 return advance (buf, read_uleb128 (buf));
702 case DW_FORM_block1:
703 val->encoding = ATTR_VAL_BLOCK;
704 return advance (buf, read_byte (buf));
705 case DW_FORM_data1:
706 val->encoding = ATTR_VAL_UINT;
707 val->u.uint = read_byte (buf);
708 return 1;
709 case DW_FORM_flag:
710 val->encoding = ATTR_VAL_UINT;
711 val->u.uint = read_byte (buf);
712 return 1;
713 case DW_FORM_sdata:
714 val->encoding = ATTR_VAL_SINT;
715 val->u.sint = read_sleb128 (buf);
716 return 1;
717 case DW_FORM_strp:
719 uint64_t offset;
721 offset = read_offset (buf, is_dwarf64);
722 if (offset >= dwarf_str_size)
724 dwarf_buf_error (buf, "DW_FORM_strp out of range");
725 return 0;
727 val->encoding = ATTR_VAL_STRING;
728 val->u.string = (const char *) dwarf_str + offset;
729 return 1;
731 case DW_FORM_udata:
732 val->encoding = ATTR_VAL_UINT;
733 val->u.uint = read_uleb128 (buf);
734 return 1;
735 case DW_FORM_ref_addr:
736 val->encoding = ATTR_VAL_REF_INFO;
737 if (version == 2)
738 val->u.uint = read_address (buf, addrsize);
739 else
740 val->u.uint = read_offset (buf, is_dwarf64);
741 return 1;
742 case DW_FORM_ref1:
743 val->encoding = ATTR_VAL_REF_UNIT;
744 val->u.uint = read_byte (buf);
745 return 1;
746 case DW_FORM_ref2:
747 val->encoding = ATTR_VAL_REF_UNIT;
748 val->u.uint = read_uint16 (buf);
749 return 1;
750 case DW_FORM_ref4:
751 val->encoding = ATTR_VAL_REF_UNIT;
752 val->u.uint = read_uint32 (buf);
753 return 1;
754 case DW_FORM_ref8:
755 val->encoding = ATTR_VAL_REF_UNIT;
756 val->u.uint = read_uint64 (buf);
757 return 1;
758 case DW_FORM_ref_udata:
759 val->encoding = ATTR_VAL_REF_UNIT;
760 val->u.uint = read_uleb128 (buf);
761 return 1;
762 case DW_FORM_indirect:
764 uint64_t form;
766 form = read_uleb128 (buf);
767 return read_attribute ((enum dwarf_form) form, buf, is_dwarf64,
768 version, addrsize, dwarf_str, dwarf_str_size,
769 val);
771 case DW_FORM_sec_offset:
772 val->encoding = ATTR_VAL_REF_SECTION;
773 val->u.uint = read_offset (buf, is_dwarf64);
774 return 1;
775 case DW_FORM_exprloc:
776 val->encoding = ATTR_VAL_EXPR;
777 return advance (buf, read_uleb128 (buf));
778 case DW_FORM_flag_present:
779 val->encoding = ATTR_VAL_UINT;
780 val->u.uint = 1;
781 return 1;
782 case DW_FORM_ref_sig8:
783 val->encoding = ATTR_VAL_REF_TYPE;
784 val->u.uint = read_uint64 (buf);
785 return 1;
786 case DW_FORM_GNU_addr_index:
787 val->encoding = ATTR_VAL_REF_SECTION;
788 val->u.uint = read_uleb128 (buf);
789 return 1;
790 case DW_FORM_GNU_str_index:
791 val->encoding = ATTR_VAL_REF_SECTION;
792 val->u.uint = read_uleb128 (buf);
793 return 1;
794 case DW_FORM_GNU_ref_alt:
795 val->encoding = ATTR_VAL_REF_SECTION;
796 val->u.uint = read_offset (buf, is_dwarf64);
797 return 1;
798 case DW_FORM_GNU_strp_alt:
799 val->encoding = ATTR_VAL_REF_SECTION;
800 val->u.uint = read_offset (buf, is_dwarf64);
801 return 1;
802 default:
803 dwarf_buf_error (buf, "unrecognized DWARF form");
804 return 0;
808 /* Compare function_addrs for qsort. When ranges are nested, make the
809 smallest one sort last. */
811 static int
812 function_addrs_compare (const void *v1, const void *v2)
814 const struct function_addrs *a1 = (const struct function_addrs *) v1;
815 const struct function_addrs *a2 = (const struct function_addrs *) v2;
817 if (a1->low < a2->low)
818 return -1;
819 if (a1->low > a2->low)
820 return 1;
821 if (a1->high < a2->high)
822 return 1;
823 if (a1->high > a2->high)
824 return -1;
825 return strcmp (a1->function->name, a2->function->name);
828 /* Compare a PC against a function_addrs for bsearch. Note that if
829 there are multiple ranges containing PC, which one will be returned
830 is unpredictable. We compensate for that in dwarf_fileline. */
832 static int
833 function_addrs_search (const void *vkey, const void *ventry)
835 const uintptr_t *key = (const uintptr_t *) vkey;
836 const struct function_addrs *entry = (const struct function_addrs *) ventry;
837 uintptr_t pc;
839 pc = *key;
840 if (pc < entry->low)
841 return -1;
842 else if (pc >= entry->high)
843 return 1;
844 else
845 return 0;
848 /* Add a new compilation unit address range to a vector. Returns 1 on
849 success, 0 on failure. */
851 static int
852 add_unit_addr (struct backtrace_state *state, uintptr_t base_address,
853 struct unit_addrs addrs,
854 backtrace_error_callback error_callback, void *data,
855 struct unit_addrs_vector *vec)
857 struct unit_addrs *p;
859 /* Add in the base address of the module here, so that we can look
860 up the PC directly. */
861 addrs.low += base_address;
862 addrs.high += base_address;
864 /* Try to merge with the last entry. */
865 if (vec->count > 0)
867 p = (struct unit_addrs *) vec->vec.base + (vec->count - 1);
868 if ((addrs.low == p->high || addrs.low == p->high + 1)
869 && addrs.u == p->u)
871 if (addrs.high > p->high)
872 p->high = addrs.high;
873 return 1;
877 p = ((struct unit_addrs *)
878 backtrace_vector_grow (state, sizeof (struct unit_addrs),
879 error_callback, data, &vec->vec));
880 if (p == NULL)
881 return 0;
883 *p = addrs;
884 ++vec->count;
885 return 1;
888 /* Free a unit address vector. */
890 static void
891 free_unit_addrs_vector (struct backtrace_state *state,
892 struct unit_addrs_vector *vec,
893 backtrace_error_callback error_callback, void *data)
895 struct unit_addrs *addrs;
896 size_t i;
898 addrs = (struct unit_addrs *) vec->vec.base;
899 for (i = 0; i < vec->count; ++i)
900 free_abbrevs (state, &addrs[i].u->abbrevs, error_callback, data);
903 /* Compare unit_addrs for qsort. When ranges are nested, make the
904 smallest one sort last. */
906 static int
907 unit_addrs_compare (const void *v1, const void *v2)
909 const struct unit_addrs *a1 = (const struct unit_addrs *) v1;
910 const struct unit_addrs *a2 = (const struct unit_addrs *) v2;
912 if (a1->low < a2->low)
913 return -1;
914 if (a1->low > a2->low)
915 return 1;
916 if (a1->high < a2->high)
917 return 1;
918 if (a1->high > a2->high)
919 return -1;
920 if (a1->u->lineoff < a2->u->lineoff)
921 return -1;
922 if (a1->u->lineoff > a2->u->lineoff)
923 return 1;
924 return 0;
927 /* Compare a PC against a unit_addrs for bsearch. Note that if there
928 are multiple ranges containing PC, which one will be returned is
929 unpredictable. We compensate for that in dwarf_fileline. */
931 static int
932 unit_addrs_search (const void *vkey, const void *ventry)
934 const uintptr_t *key = (const uintptr_t *) vkey;
935 const struct unit_addrs *entry = (const struct unit_addrs *) ventry;
936 uintptr_t pc;
938 pc = *key;
939 if (pc < entry->low)
940 return -1;
941 else if (pc >= entry->high)
942 return 1;
943 else
944 return 0;
947 /* Sort the line vector by PC. We want a stable sort here to maintain
948 the order of lines for the same PC values. Since the sequence is
949 being sorted in place, their addresses cannot be relied on to
950 maintain stability. That is the purpose of the index member. */
952 static int
953 line_compare (const void *v1, const void *v2)
955 const struct line *ln1 = (const struct line *) v1;
956 const struct line *ln2 = (const struct line *) v2;
958 if (ln1->pc < ln2->pc)
959 return -1;
960 else if (ln1->pc > ln2->pc)
961 return 1;
962 else if (ln1->idx < ln2->idx)
963 return -1;
964 else if (ln1->idx > ln2->idx)
965 return 1;
966 else
967 return 0;
970 /* Find a PC in a line vector. We always allocate an extra entry at
971 the end of the lines vector, so that this routine can safely look
972 at the next entry. Note that when there are multiple mappings for
973 the same PC value, this will return the last one. */
975 static int
976 line_search (const void *vkey, const void *ventry)
978 const uintptr_t *key = (const uintptr_t *) vkey;
979 const struct line *entry = (const struct line *) ventry;
980 uintptr_t pc;
982 pc = *key;
983 if (pc < entry->pc)
984 return -1;
985 else if (pc >= (entry + 1)->pc)
986 return 1;
987 else
988 return 0;
991 /* Sort the abbrevs by the abbrev code. This function is passed to
992 both qsort and bsearch. */
994 static int
995 abbrev_compare (const void *v1, const void *v2)
997 const struct abbrev *a1 = (const struct abbrev *) v1;
998 const struct abbrev *a2 = (const struct abbrev *) v2;
1000 if (a1->code < a2->code)
1001 return -1;
1002 else if (a1->code > a2->code)
1003 return 1;
1004 else
1006 /* This really shouldn't happen. It means there are two
1007 different abbrevs with the same code, and that means we don't
1008 know which one lookup_abbrev should return. */
1009 return 0;
1013 /* Read the abbreviation table for a compilation unit. Returns 1 on
1014 success, 0 on failure. */
1016 static int
1017 read_abbrevs (struct backtrace_state *state, uint64_t abbrev_offset,
1018 const unsigned char *dwarf_abbrev, size_t dwarf_abbrev_size,
1019 int is_bigendian, backtrace_error_callback error_callback,
1020 void *data, struct abbrevs *abbrevs)
1022 struct dwarf_buf abbrev_buf;
1023 struct dwarf_buf count_buf;
1024 size_t num_abbrevs;
1026 abbrevs->num_abbrevs = 0;
1027 abbrevs->abbrevs = NULL;
1029 if (abbrev_offset >= dwarf_abbrev_size)
1031 error_callback (data, "abbrev offset out of range", 0);
1032 return 0;
1035 abbrev_buf.name = ".debug_abbrev";
1036 abbrev_buf.start = dwarf_abbrev;
1037 abbrev_buf.buf = dwarf_abbrev + abbrev_offset;
1038 abbrev_buf.left = dwarf_abbrev_size - abbrev_offset;
1039 abbrev_buf.is_bigendian = is_bigendian;
1040 abbrev_buf.error_callback = error_callback;
1041 abbrev_buf.data = data;
1042 abbrev_buf.reported_underflow = 0;
1044 /* Count the number of abbrevs in this list. */
1046 count_buf = abbrev_buf;
1047 num_abbrevs = 0;
1048 while (read_uleb128 (&count_buf) != 0)
1050 if (count_buf.reported_underflow)
1051 return 0;
1052 ++num_abbrevs;
1053 // Skip tag.
1054 read_uleb128 (&count_buf);
1055 // Skip has_children.
1056 read_byte (&count_buf);
1057 // Skip attributes.
1058 while (read_uleb128 (&count_buf) != 0)
1059 read_uleb128 (&count_buf);
1060 // Skip form of last attribute.
1061 read_uleb128 (&count_buf);
1064 if (count_buf.reported_underflow)
1065 return 0;
1067 if (num_abbrevs == 0)
1068 return 1;
1070 abbrevs->num_abbrevs = num_abbrevs;
1071 abbrevs->abbrevs = ((struct abbrev *)
1072 backtrace_alloc (state,
1073 num_abbrevs * sizeof (struct abbrev),
1074 error_callback, data));
1075 if (abbrevs->abbrevs == NULL)
1076 return 0;
1077 memset (abbrevs->abbrevs, 0, num_abbrevs * sizeof (struct abbrev));
1079 num_abbrevs = 0;
1080 while (1)
1082 uint64_t code;
1083 struct abbrev a;
1084 size_t num_attrs;
1085 struct attr *attrs;
1087 if (abbrev_buf.reported_underflow)
1088 goto fail;
1090 code = read_uleb128 (&abbrev_buf);
1091 if (code == 0)
1092 break;
1094 a.code = code;
1095 a.tag = (enum dwarf_tag) read_uleb128 (&abbrev_buf);
1096 a.has_children = read_byte (&abbrev_buf);
1098 count_buf = abbrev_buf;
1099 num_attrs = 0;
1100 while (read_uleb128 (&count_buf) != 0)
1102 ++num_attrs;
1103 read_uleb128 (&count_buf);
1106 if (num_attrs == 0)
1108 attrs = NULL;
1109 read_uleb128 (&abbrev_buf);
1110 read_uleb128 (&abbrev_buf);
1112 else
1114 attrs = ((struct attr *)
1115 backtrace_alloc (state, num_attrs * sizeof *attrs,
1116 error_callback, data));
1117 if (attrs == NULL)
1118 goto fail;
1119 num_attrs = 0;
1120 while (1)
1122 uint64_t name;
1123 uint64_t form;
1125 name = read_uleb128 (&abbrev_buf);
1126 form = read_uleb128 (&abbrev_buf);
1127 if (name == 0)
1128 break;
1129 attrs[num_attrs].name = (enum dwarf_attribute) name;
1130 attrs[num_attrs].form = (enum dwarf_form) form;
1131 ++num_attrs;
1135 a.num_attrs = num_attrs;
1136 a.attrs = attrs;
1138 abbrevs->abbrevs[num_abbrevs] = a;
1139 ++num_abbrevs;
1142 backtrace_qsort (abbrevs->abbrevs, abbrevs->num_abbrevs,
1143 sizeof (struct abbrev), abbrev_compare);
1145 return 1;
1147 fail:
1148 free_abbrevs (state, abbrevs, error_callback, data);
1149 return 0;
1152 /* Return the abbrev information for an abbrev code. */
1154 static const struct abbrev *
1155 lookup_abbrev (struct abbrevs *abbrevs, uint64_t code,
1156 backtrace_error_callback error_callback, void *data)
1158 struct abbrev key;
1159 void *p;
1161 /* With GCC, where abbrevs are simply numbered in order, we should
1162 be able to just look up the entry. */
1163 if (code - 1 < abbrevs->num_abbrevs
1164 && abbrevs->abbrevs[code - 1].code == code)
1165 return &abbrevs->abbrevs[code - 1];
1167 /* Otherwise we have to search. */
1168 memset (&key, 0, sizeof key);
1169 key.code = code;
1170 p = bsearch (&key, abbrevs->abbrevs, abbrevs->num_abbrevs,
1171 sizeof (struct abbrev), abbrev_compare);
1172 if (p == NULL)
1174 error_callback (data, "invalid abbreviation code", 0);
1175 return NULL;
1177 return (const struct abbrev *) p;
1180 /* Add non-contiguous address ranges for a compilation unit. Returns
1181 1 on success, 0 on failure. */
1183 static int
1184 add_unit_ranges (struct backtrace_state *state, uintptr_t base_address,
1185 struct unit *u, uint64_t ranges, uint64_t base,
1186 int is_bigendian, const unsigned char *dwarf_ranges,
1187 size_t dwarf_ranges_size,
1188 backtrace_error_callback error_callback, void *data,
1189 struct unit_addrs_vector *addrs)
1191 struct dwarf_buf ranges_buf;
1193 if (ranges >= dwarf_ranges_size)
1195 error_callback (data, "ranges offset out of range", 0);
1196 return 0;
1199 ranges_buf.name = ".debug_ranges";
1200 ranges_buf.start = dwarf_ranges;
1201 ranges_buf.buf = dwarf_ranges + ranges;
1202 ranges_buf.left = dwarf_ranges_size - ranges;
1203 ranges_buf.is_bigendian = is_bigendian;
1204 ranges_buf.error_callback = error_callback;
1205 ranges_buf.data = data;
1206 ranges_buf.reported_underflow = 0;
1208 while (1)
1210 uint64_t low;
1211 uint64_t high;
1213 if (ranges_buf.reported_underflow)
1214 return 0;
1216 low = read_address (&ranges_buf, u->addrsize);
1217 high = read_address (&ranges_buf, u->addrsize);
1219 if (low == 0 && high == 0)
1220 break;
1222 if (is_highest_address (low, u->addrsize))
1223 base = high;
1224 else
1226 struct unit_addrs a;
1228 a.low = low + base;
1229 a.high = high + base;
1230 a.u = u;
1231 if (!add_unit_addr (state, base_address, a, error_callback, data,
1232 addrs))
1233 return 0;
1237 if (ranges_buf.reported_underflow)
1238 return 0;
1240 return 1;
1243 /* Find the address range covered by a compilation unit, reading from
1244 UNIT_BUF and adding values to U. Returns 1 if all data could be
1245 read, 0 if there is some error. */
1247 static int
1248 find_address_ranges (struct backtrace_state *state, uintptr_t base_address,
1249 struct dwarf_buf *unit_buf,
1250 const unsigned char *dwarf_str, size_t dwarf_str_size,
1251 const unsigned char *dwarf_ranges,
1252 size_t dwarf_ranges_size,
1253 int is_bigendian, backtrace_error_callback error_callback,
1254 void *data, struct unit *u,
1255 struct unit_addrs_vector *addrs)
1257 while (unit_buf->left > 0)
1259 uint64_t code;
1260 const struct abbrev *abbrev;
1261 uint64_t lowpc;
1262 int have_lowpc;
1263 uint64_t highpc;
1264 int have_highpc;
1265 int highpc_is_relative;
1266 uint64_t ranges;
1267 int have_ranges;
1268 size_t i;
1270 code = read_uleb128 (unit_buf);
1271 if (code == 0)
1272 return 1;
1274 abbrev = lookup_abbrev (&u->abbrevs, code, error_callback, data);
1275 if (abbrev == NULL)
1276 return 0;
1278 lowpc = 0;
1279 have_lowpc = 0;
1280 highpc = 0;
1281 have_highpc = 0;
1282 highpc_is_relative = 0;
1283 ranges = 0;
1284 have_ranges = 0;
1285 for (i = 0; i < abbrev->num_attrs; ++i)
1287 struct attr_val val;
1289 if (!read_attribute (abbrev->attrs[i].form, unit_buf,
1290 u->is_dwarf64, u->version, u->addrsize,
1291 dwarf_str, dwarf_str_size, &val))
1292 return 0;
1294 switch (abbrev->attrs[i].name)
1296 case DW_AT_low_pc:
1297 if (val.encoding == ATTR_VAL_ADDRESS)
1299 lowpc = val.u.uint;
1300 have_lowpc = 1;
1302 break;
1304 case DW_AT_high_pc:
1305 if (val.encoding == ATTR_VAL_ADDRESS)
1307 highpc = val.u.uint;
1308 have_highpc = 1;
1310 else if (val.encoding == ATTR_VAL_UINT)
1312 highpc = val.u.uint;
1313 have_highpc = 1;
1314 highpc_is_relative = 1;
1316 break;
1318 case DW_AT_ranges:
1319 if (val.encoding == ATTR_VAL_UINT
1320 || val.encoding == ATTR_VAL_REF_SECTION)
1322 ranges = val.u.uint;
1323 have_ranges = 1;
1325 break;
1327 case DW_AT_stmt_list:
1328 if (abbrev->tag == DW_TAG_compile_unit
1329 && (val.encoding == ATTR_VAL_UINT
1330 || val.encoding == ATTR_VAL_REF_SECTION))
1331 u->lineoff = val.u.uint;
1332 break;
1334 case DW_AT_name:
1335 if (abbrev->tag == DW_TAG_compile_unit
1336 && val.encoding == ATTR_VAL_STRING)
1337 u->filename = val.u.string;
1338 break;
1340 case DW_AT_comp_dir:
1341 if (abbrev->tag == DW_TAG_compile_unit
1342 && val.encoding == ATTR_VAL_STRING)
1343 u->comp_dir = val.u.string;
1344 break;
1346 default:
1347 break;
1351 if (abbrev->tag == DW_TAG_compile_unit
1352 || abbrev->tag == DW_TAG_subprogram)
1354 if (have_ranges)
1356 if (!add_unit_ranges (state, base_address, u, ranges, lowpc,
1357 is_bigendian, dwarf_ranges,
1358 dwarf_ranges_size, error_callback,
1359 data, addrs))
1360 return 0;
1362 else if (have_lowpc && have_highpc)
1364 struct unit_addrs a;
1366 if (highpc_is_relative)
1367 highpc += lowpc;
1368 a.low = lowpc;
1369 a.high = highpc;
1370 a.u = u;
1372 if (!add_unit_addr (state, base_address, a, error_callback, data,
1373 addrs))
1374 return 0;
1377 /* If we found the PC range in the DW_TAG_compile_unit, we
1378 can stop now. */
1379 if (abbrev->tag == DW_TAG_compile_unit
1380 && (have_ranges || (have_lowpc && have_highpc)))
1381 return 1;
1384 if (abbrev->has_children)
1386 if (!find_address_ranges (state, base_address, unit_buf,
1387 dwarf_str, dwarf_str_size,
1388 dwarf_ranges, dwarf_ranges_size,
1389 is_bigendian, error_callback, data,
1390 u, addrs))
1391 return 0;
1395 return 1;
1398 /* Build a mapping from address ranges to the compilation units where
1399 the line number information for that range can be found. Returns 1
1400 on success, 0 on failure. */
1402 static int
1403 build_address_map (struct backtrace_state *state, uintptr_t base_address,
1404 const unsigned char *dwarf_info, size_t dwarf_info_size,
1405 const unsigned char *dwarf_abbrev, size_t dwarf_abbrev_size,
1406 const unsigned char *dwarf_ranges, size_t dwarf_ranges_size,
1407 const unsigned char *dwarf_str, size_t dwarf_str_size,
1408 int is_bigendian, backtrace_error_callback error_callback,
1409 void *data, struct unit_addrs_vector *addrs)
1411 struct dwarf_buf info;
1412 struct abbrevs abbrevs;
1414 memset (&addrs->vec, 0, sizeof addrs->vec);
1415 addrs->count = 0;
1417 /* Read through the .debug_info section. FIXME: Should we use the
1418 .debug_aranges section? gdb and addr2line don't use it, but I'm
1419 not sure why. */
1421 info.name = ".debug_info";
1422 info.start = dwarf_info;
1423 info.buf = dwarf_info;
1424 info.left = dwarf_info_size;
1425 info.is_bigendian = is_bigendian;
1426 info.error_callback = error_callback;
1427 info.data = data;
1428 info.reported_underflow = 0;
1430 memset (&abbrevs, 0, sizeof abbrevs);
1431 while (info.left > 0)
1433 const unsigned char *unit_data_start;
1434 uint64_t len;
1435 int is_dwarf64;
1436 struct dwarf_buf unit_buf;
1437 int version;
1438 uint64_t abbrev_offset;
1439 int addrsize;
1440 struct unit *u;
1442 if (info.reported_underflow)
1443 goto fail;
1445 unit_data_start = info.buf;
1447 is_dwarf64 = 0;
1448 len = read_uint32 (&info);
1449 if (len == 0xffffffff)
1451 len = read_uint64 (&info);
1452 is_dwarf64 = 1;
1455 unit_buf = info;
1456 unit_buf.left = len;
1458 if (!advance (&info, len))
1459 goto fail;
1461 version = read_uint16 (&unit_buf);
1462 if (version < 2 || version > 4)
1464 dwarf_buf_error (&unit_buf, "unrecognized DWARF version");
1465 goto fail;
1468 abbrev_offset = read_offset (&unit_buf, is_dwarf64);
1469 if (!read_abbrevs (state, abbrev_offset, dwarf_abbrev, dwarf_abbrev_size,
1470 is_bigendian, error_callback, data, &abbrevs))
1471 goto fail;
1473 addrsize = read_byte (&unit_buf);
1475 u = ((struct unit *)
1476 backtrace_alloc (state, sizeof *u, error_callback, data));
1477 if (u == NULL)
1478 goto fail;
1479 u->unit_data = unit_buf.buf;
1480 u->unit_data_len = unit_buf.left;
1481 u->unit_data_offset = unit_buf.buf - unit_data_start;
1482 u->version = version;
1483 u->is_dwarf64 = is_dwarf64;
1484 u->addrsize = addrsize;
1485 u->filename = NULL;
1486 u->comp_dir = NULL;
1487 u->abs_filename = NULL;
1488 u->lineoff = 0;
1489 u->abbrevs = abbrevs;
1490 memset (&abbrevs, 0, sizeof abbrevs);
1492 /* The actual line number mappings will be read as needed. */
1493 u->lines = NULL;
1494 u->lines_count = 0;
1495 u->function_addrs = NULL;
1496 u->function_addrs_count = 0;
1498 if (!find_address_ranges (state, base_address, &unit_buf,
1499 dwarf_str, dwarf_str_size,
1500 dwarf_ranges, dwarf_ranges_size,
1501 is_bigendian, error_callback, data,
1502 u, addrs))
1504 free_abbrevs (state, &u->abbrevs, error_callback, data);
1505 backtrace_free (state, u, sizeof *u, error_callback, data);
1506 goto fail;
1509 if (unit_buf.reported_underflow)
1511 free_abbrevs (state, &u->abbrevs, error_callback, data);
1512 backtrace_free (state, u, sizeof *u, error_callback, data);
1513 goto fail;
1516 if (info.reported_underflow)
1517 goto fail;
1519 return 1;
1521 fail:
1522 free_abbrevs (state, &abbrevs, error_callback, data);
1523 free_unit_addrs_vector (state, addrs, error_callback, data);
1524 return 0;
1527 /* Add a new mapping to the vector of line mappings that we are
1528 building. Returns 1 on success, 0 on failure. */
1530 static int
1531 add_line (struct backtrace_state *state, struct dwarf_data *ddata,
1532 uintptr_t pc, const char *filename, int lineno,
1533 backtrace_error_callback error_callback, void *data,
1534 struct line_vector *vec)
1536 struct line *ln;
1538 /* If we are adding the same mapping, ignore it. This can happen
1539 when using discriminators. */
1540 if (vec->count > 0)
1542 ln = (struct line *) vec->vec.base + (vec->count - 1);
1543 if (pc == ln->pc && filename == ln->filename && lineno == ln->lineno)
1544 return 1;
1547 ln = ((struct line *)
1548 backtrace_vector_grow (state, sizeof (struct line), error_callback,
1549 data, &vec->vec));
1550 if (ln == NULL)
1551 return 0;
1553 /* Add in the base address here, so that we can look up the PC
1554 directly. */
1555 ln->pc = pc + ddata->base_address;
1557 ln->filename = filename;
1558 ln->lineno = lineno;
1559 ln->idx = vec->count;
1561 ++vec->count;
1563 return 1;
1566 /* Free the line header information. If FREE_FILENAMES is true we
1567 free the file names themselves, otherwise we leave them, as there
1568 may be line structures pointing to them. */
1570 static void
1571 free_line_header (struct backtrace_state *state, struct line_header *hdr,
1572 backtrace_error_callback error_callback, void *data)
1574 backtrace_free (state, hdr->dirs, hdr->dirs_count * sizeof (const char *),
1575 error_callback, data);
1576 backtrace_free (state, hdr->filenames,
1577 hdr->filenames_count * sizeof (char *),
1578 error_callback, data);
1581 /* Read the line header. Return 1 on success, 0 on failure. */
1583 static int
1584 read_line_header (struct backtrace_state *state, struct unit *u,
1585 int is_dwarf64, struct dwarf_buf *line_buf,
1586 struct line_header *hdr)
1588 uint64_t hdrlen;
1589 struct dwarf_buf hdr_buf;
1590 const unsigned char *p;
1591 const unsigned char *pend;
1592 size_t i;
1594 hdr->version = read_uint16 (line_buf);
1595 if (hdr->version < 2 || hdr->version > 4)
1597 dwarf_buf_error (line_buf, "unsupported line number version");
1598 return 0;
1601 hdrlen = read_offset (line_buf, is_dwarf64);
1603 hdr_buf = *line_buf;
1604 hdr_buf.left = hdrlen;
1606 if (!advance (line_buf, hdrlen))
1607 return 0;
1609 hdr->min_insn_len = read_byte (&hdr_buf);
1610 if (hdr->version < 4)
1611 hdr->max_ops_per_insn = 1;
1612 else
1613 hdr->max_ops_per_insn = read_byte (&hdr_buf);
1615 /* We don't care about default_is_stmt. */
1616 read_byte (&hdr_buf);
1618 hdr->line_base = read_sbyte (&hdr_buf);
1619 hdr->line_range = read_byte (&hdr_buf);
1621 hdr->opcode_base = read_byte (&hdr_buf);
1622 hdr->opcode_lengths = hdr_buf.buf;
1623 if (!advance (&hdr_buf, hdr->opcode_base - 1))
1624 return 0;
1626 /* Count the number of directory entries. */
1627 hdr->dirs_count = 0;
1628 p = hdr_buf.buf;
1629 pend = p + hdr_buf.left;
1630 while (p < pend && *p != '\0')
1632 p += strnlen((const char *) p, pend - p) + 1;
1633 ++hdr->dirs_count;
1636 hdr->dirs = ((const char **)
1637 backtrace_alloc (state,
1638 hdr->dirs_count * sizeof (const char *),
1639 line_buf->error_callback, line_buf->data));
1640 if (hdr->dirs == NULL)
1641 return 0;
1643 i = 0;
1644 while (*hdr_buf.buf != '\0')
1646 if (hdr_buf.reported_underflow)
1647 return 0;
1649 hdr->dirs[i] = (const char *) hdr_buf.buf;
1650 ++i;
1651 if (!advance (&hdr_buf,
1652 strnlen ((const char *) hdr_buf.buf, hdr_buf.left) + 1))
1653 return 0;
1655 if (!advance (&hdr_buf, 1))
1656 return 0;
1658 /* Count the number of file entries. */
1659 hdr->filenames_count = 0;
1660 p = hdr_buf.buf;
1661 pend = p + hdr_buf.left;
1662 while (p < pend && *p != '\0')
1664 p += strnlen ((const char *) p, pend - p) + 1;
1665 p += leb128_len (p);
1666 p += leb128_len (p);
1667 p += leb128_len (p);
1668 ++hdr->filenames_count;
1671 hdr->filenames = ((const char **)
1672 backtrace_alloc (state,
1673 hdr->filenames_count * sizeof (char *),
1674 line_buf->error_callback,
1675 line_buf->data));
1676 if (hdr->filenames == NULL)
1677 return 0;
1678 i = 0;
1679 while (*hdr_buf.buf != '\0')
1681 const char *filename;
1682 uint64_t dir_index;
1684 if (hdr_buf.reported_underflow)
1685 return 0;
1687 filename = (const char *) hdr_buf.buf;
1688 if (!advance (&hdr_buf,
1689 strnlen ((const char *) hdr_buf.buf, hdr_buf.left) + 1))
1690 return 0;
1691 dir_index = read_uleb128 (&hdr_buf);
1692 if (IS_ABSOLUTE_PATH (filename)
1693 || (dir_index == 0 && u->comp_dir == NULL))
1694 hdr->filenames[i] = filename;
1695 else
1697 const char *dir;
1698 size_t dir_len;
1699 size_t filename_len;
1700 char *s;
1702 if (dir_index == 0)
1703 dir = u->comp_dir;
1704 else if (dir_index - 1 < hdr->dirs_count)
1705 dir = hdr->dirs[dir_index - 1];
1706 else
1708 dwarf_buf_error (line_buf,
1709 ("invalid directory index in "
1710 "line number program header"));
1711 return 0;
1713 dir_len = strlen (dir);
1714 filename_len = strlen (filename);
1715 s = ((char *)
1716 backtrace_alloc (state, dir_len + filename_len + 2,
1717 line_buf->error_callback, line_buf->data));
1718 if (s == NULL)
1719 return 0;
1720 memcpy (s, dir, dir_len);
1721 /* FIXME: If we are on a DOS-based file system, and the
1722 directory or the file name use backslashes, then we
1723 should use a backslash here. */
1724 s[dir_len] = '/';
1725 memcpy (s + dir_len + 1, filename, filename_len + 1);
1726 hdr->filenames[i] = s;
1729 /* Ignore the modification time and size. */
1730 read_uleb128 (&hdr_buf);
1731 read_uleb128 (&hdr_buf);
1733 ++i;
1736 if (hdr_buf.reported_underflow)
1737 return 0;
1739 return 1;
1742 /* Read the line program, adding line mappings to VEC. Return 1 on
1743 success, 0 on failure. */
1745 static int
1746 read_line_program (struct backtrace_state *state, struct dwarf_data *ddata,
1747 struct unit *u, const struct line_header *hdr,
1748 struct dwarf_buf *line_buf, struct line_vector *vec)
1750 uint64_t address;
1751 unsigned int op_index;
1752 const char *reset_filename;
1753 const char *filename;
1754 int lineno;
1756 address = 0;
1757 op_index = 0;
1758 if (hdr->filenames_count > 0)
1759 reset_filename = hdr->filenames[0];
1760 else
1761 reset_filename = "";
1762 filename = reset_filename;
1763 lineno = 1;
1764 while (line_buf->left > 0)
1766 unsigned int op;
1768 op = read_byte (line_buf);
1769 if (op >= hdr->opcode_base)
1771 unsigned int advance;
1773 /* Special opcode. */
1774 op -= hdr->opcode_base;
1775 advance = op / hdr->line_range;
1776 address += (hdr->min_insn_len * (op_index + advance)
1777 / hdr->max_ops_per_insn);
1778 op_index = (op_index + advance) % hdr->max_ops_per_insn;
1779 lineno += hdr->line_base + (int) (op % hdr->line_range);
1780 add_line (state, ddata, address, filename, lineno,
1781 line_buf->error_callback, line_buf->data, vec);
1783 else if (op == DW_LNS_extended_op)
1785 uint64_t len;
1787 len = read_uleb128 (line_buf);
1788 op = read_byte (line_buf);
1789 switch (op)
1791 case DW_LNE_end_sequence:
1792 /* FIXME: Should we mark the high PC here? It seems
1793 that we already have that information from the
1794 compilation unit. */
1795 address = 0;
1796 op_index = 0;
1797 filename = reset_filename;
1798 lineno = 1;
1799 break;
1800 case DW_LNE_set_address:
1801 address = read_address (line_buf, u->addrsize);
1802 break;
1803 case DW_LNE_define_file:
1805 const char *f;
1806 unsigned int dir_index;
1808 f = (const char *) line_buf->buf;
1809 if (!advance (line_buf, strnlen (f, line_buf->left) + 1))
1810 return 0;
1811 dir_index = read_uleb128 (line_buf);
1812 /* Ignore that time and length. */
1813 read_uleb128 (line_buf);
1814 read_uleb128 (line_buf);
1815 if (IS_ABSOLUTE_PATH (f))
1816 filename = f;
1817 else
1819 const char *dir;
1820 size_t dir_len;
1821 size_t f_len;
1822 char *p;
1824 if (dir_index == 0)
1825 dir = u->comp_dir;
1826 else if (dir_index - 1 < hdr->dirs_count)
1827 dir = hdr->dirs[dir_index - 1];
1828 else
1830 dwarf_buf_error (line_buf,
1831 ("invalid directory index "
1832 "in line number program"));
1833 return 0;
1835 dir_len = strlen (dir);
1836 f_len = strlen (f);
1837 p = ((char *)
1838 backtrace_alloc (state, dir_len + f_len + 2,
1839 line_buf->error_callback,
1840 line_buf->data));
1841 if (p == NULL)
1842 return 0;
1843 memcpy (p, dir, dir_len);
1844 /* FIXME: If we are on a DOS-based file system,
1845 and the directory or the file name use
1846 backslashes, then we should use a backslash
1847 here. */
1848 p[dir_len] = '/';
1849 memcpy (p + dir_len + 1, f, f_len + 1);
1850 filename = p;
1853 break;
1854 case DW_LNE_set_discriminator:
1855 /* We don't care about discriminators. */
1856 read_uleb128 (line_buf);
1857 break;
1858 default:
1859 if (!advance (line_buf, len - 1))
1860 return 0;
1861 break;
1864 else
1866 switch (op)
1868 case DW_LNS_copy:
1869 add_line (state, ddata, address, filename, lineno,
1870 line_buf->error_callback, line_buf->data, vec);
1871 break;
1872 case DW_LNS_advance_pc:
1874 uint64_t advance;
1876 advance = read_uleb128 (line_buf);
1877 address += (hdr->min_insn_len * (op_index + advance)
1878 / hdr->max_ops_per_insn);
1879 op_index = (op_index + advance) % hdr->max_ops_per_insn;
1881 break;
1882 case DW_LNS_advance_line:
1883 lineno += (int) read_sleb128 (line_buf);
1884 break;
1885 case DW_LNS_set_file:
1887 uint64_t fileno;
1889 fileno = read_uleb128 (line_buf);
1890 if (fileno == 0)
1891 filename = "";
1892 else
1894 if (fileno - 1 >= hdr->filenames_count)
1896 dwarf_buf_error (line_buf,
1897 ("invalid file number in "
1898 "line number program"));
1899 return 0;
1901 filename = hdr->filenames[fileno - 1];
1904 break;
1905 case DW_LNS_set_column:
1906 read_uleb128 (line_buf);
1907 break;
1908 case DW_LNS_negate_stmt:
1909 break;
1910 case DW_LNS_set_basic_block:
1911 break;
1912 case DW_LNS_const_add_pc:
1914 unsigned int advance;
1916 op = 255 - hdr->opcode_base;
1917 advance = op / hdr->line_range;
1918 address += (hdr->min_insn_len * (op_index + advance)
1919 / hdr->max_ops_per_insn);
1920 op_index = (op_index + advance) % hdr->max_ops_per_insn;
1922 break;
1923 case DW_LNS_fixed_advance_pc:
1924 address += read_uint16 (line_buf);
1925 op_index = 0;
1926 break;
1927 case DW_LNS_set_prologue_end:
1928 break;
1929 case DW_LNS_set_epilogue_begin:
1930 break;
1931 case DW_LNS_set_isa:
1932 read_uleb128 (line_buf);
1933 break;
1934 default:
1936 unsigned int i;
1938 for (i = hdr->opcode_lengths[op - 1]; i > 0; --i)
1939 read_uleb128 (line_buf);
1941 break;
1946 return 1;
1949 /* Read the line number information for a compilation unit. Returns 1
1950 on success, 0 on failure. */
1952 static int
1953 read_line_info (struct backtrace_state *state, struct dwarf_data *ddata,
1954 backtrace_error_callback error_callback, void *data,
1955 struct unit *u, struct line_header *hdr, struct line **lines,
1956 size_t *lines_count)
1958 struct line_vector vec;
1959 struct dwarf_buf line_buf;
1960 uint64_t len;
1961 int is_dwarf64;
1962 struct line *ln;
1964 memset (&vec.vec, 0, sizeof vec.vec);
1965 vec.count = 0;
1967 memset (hdr, 0, sizeof *hdr);
1969 if (u->lineoff != (off_t) (size_t) u->lineoff
1970 || (size_t) u->lineoff >= ddata->dwarf_line_size)
1972 error_callback (data, "unit line offset out of range", 0);
1973 goto fail;
1976 line_buf.name = ".debug_line";
1977 line_buf.start = ddata->dwarf_line;
1978 line_buf.buf = ddata->dwarf_line + u->lineoff;
1979 line_buf.left = ddata->dwarf_line_size - u->lineoff;
1980 line_buf.is_bigendian = ddata->is_bigendian;
1981 line_buf.error_callback = error_callback;
1982 line_buf.data = data;
1983 line_buf.reported_underflow = 0;
1985 is_dwarf64 = 0;
1986 len = read_uint32 (&line_buf);
1987 if (len == 0xffffffff)
1989 len = read_uint64 (&line_buf);
1990 is_dwarf64 = 1;
1992 line_buf.left = len;
1994 if (!read_line_header (state, u, is_dwarf64, &line_buf, hdr))
1995 goto fail;
1997 if (!read_line_program (state, ddata, u, hdr, &line_buf, &vec))
1998 goto fail;
2000 if (line_buf.reported_underflow)
2001 goto fail;
2003 if (vec.count == 0)
2005 /* This is not a failure in the sense of a generating an error,
2006 but it is a failure in that sense that we have no useful
2007 information. */
2008 goto fail;
2011 /* Allocate one extra entry at the end. */
2012 ln = ((struct line *)
2013 backtrace_vector_grow (state, sizeof (struct line), error_callback,
2014 data, &vec.vec));
2015 if (ln == NULL)
2016 goto fail;
2017 ln->pc = (uintptr_t) -1;
2018 ln->filename = NULL;
2019 ln->lineno = 0;
2020 ln->idx = 0;
2022 if (!backtrace_vector_release (state, &vec.vec, error_callback, data))
2023 goto fail;
2025 ln = (struct line *) vec.vec.base;
2026 backtrace_qsort (ln, vec.count, sizeof (struct line), line_compare);
2028 *lines = ln;
2029 *lines_count = vec.count;
2031 return 1;
2033 fail:
2034 vec.vec.alc += vec.vec.size;
2035 vec.vec.size = 0;
2036 backtrace_vector_release (state, &vec.vec, error_callback, data);
2037 free_line_header (state, hdr, error_callback, data);
2038 *lines = (struct line *) (uintptr_t) -1;
2039 *lines_count = 0;
2040 return 0;
2043 /* Read the name of a function from a DIE referenced by a
2044 DW_AT_abstract_origin or DW_AT_specification tag. OFFSET is within
2045 the same compilation unit. */
2047 static const char *
2048 read_referenced_name (struct dwarf_data *ddata, struct unit *u,
2049 uint64_t offset, backtrace_error_callback error_callback,
2050 void *data)
2052 struct dwarf_buf unit_buf;
2053 uint64_t code;
2054 const struct abbrev *abbrev;
2055 const char *ret;
2056 size_t i;
2058 /* OFFSET is from the start of the data for this compilation unit.
2059 U->unit_data is the data, but it starts U->unit_data_offset bytes
2060 from the beginning. */
2062 if (offset < u->unit_data_offset
2063 || offset - u->unit_data_offset >= u->unit_data_len)
2065 error_callback (data,
2066 "abstract origin or specification out of range",
2068 return NULL;
2071 offset -= u->unit_data_offset;
2073 unit_buf.name = ".debug_info";
2074 unit_buf.start = ddata->dwarf_info;
2075 unit_buf.buf = u->unit_data + offset;
2076 unit_buf.left = u->unit_data_len - offset;
2077 unit_buf.is_bigendian = ddata->is_bigendian;
2078 unit_buf.error_callback = error_callback;
2079 unit_buf.data = data;
2080 unit_buf.reported_underflow = 0;
2082 code = read_uleb128 (&unit_buf);
2083 if (code == 0)
2085 dwarf_buf_error (&unit_buf, "invalid abstract origin or specification");
2086 return NULL;
2089 abbrev = lookup_abbrev (&u->abbrevs, code, error_callback, data);
2090 if (abbrev == NULL)
2091 return NULL;
2093 ret = NULL;
2094 for (i = 0; i < abbrev->num_attrs; ++i)
2096 struct attr_val val;
2098 if (!read_attribute (abbrev->attrs[i].form, &unit_buf,
2099 u->is_dwarf64, u->version, u->addrsize,
2100 ddata->dwarf_str, ddata->dwarf_str_size,
2101 &val))
2102 return NULL;
2104 switch (abbrev->attrs[i].name)
2106 case DW_AT_name:
2107 /* We prefer the linkage name if get one. */
2108 if (val.encoding == ATTR_VAL_STRING)
2109 ret = val.u.string;
2110 break;
2112 case DW_AT_linkage_name:
2113 case DW_AT_MIPS_linkage_name:
2114 if (val.encoding == ATTR_VAL_STRING)
2115 return val.u.string;
2116 break;
2118 case DW_AT_specification:
2119 if (abbrev->attrs[i].form == DW_FORM_ref_addr
2120 || abbrev->attrs[i].form == DW_FORM_ref_sig8)
2122 /* This refers to a specification defined in some other
2123 compilation unit. We can handle this case if we
2124 must, but it's harder. */
2125 break;
2127 if (val.encoding == ATTR_VAL_UINT
2128 || val.encoding == ATTR_VAL_REF_UNIT)
2130 const char *name;
2132 name = read_referenced_name (ddata, u, val.u.uint,
2133 error_callback, data);
2134 if (name != NULL)
2135 ret = name;
2137 break;
2139 default:
2140 break;
2144 return ret;
2147 /* Add a single range to U that maps to function. Returns 1 on
2148 success, 0 on error. */
2150 static int
2151 add_function_range (struct backtrace_state *state, struct dwarf_data *ddata,
2152 struct function *function, uint64_t lowpc, uint64_t highpc,
2153 backtrace_error_callback error_callback,
2154 void *data, struct function_vector *vec)
2156 struct function_addrs *p;
2158 /* Add in the base address here, so that we can look up the PC
2159 directly. */
2160 lowpc += ddata->base_address;
2161 highpc += ddata->base_address;
2163 if (vec->count > 0)
2165 p = (struct function_addrs *) vec->vec.base + vec->count - 1;
2166 if ((lowpc == p->high || lowpc == p->high + 1)
2167 && function == p->function)
2169 if (highpc > p->high)
2170 p->high = highpc;
2171 return 1;
2175 p = ((struct function_addrs *)
2176 backtrace_vector_grow (state, sizeof (struct function_addrs),
2177 error_callback, data, &vec->vec));
2178 if (p == NULL)
2179 return 0;
2181 p->low = lowpc;
2182 p->high = highpc;
2183 p->function = function;
2184 ++vec->count;
2185 return 1;
2188 /* Add PC ranges to U that map to FUNCTION. Returns 1 on success, 0
2189 on error. */
2191 static int
2192 add_function_ranges (struct backtrace_state *state, struct dwarf_data *ddata,
2193 struct unit *u, struct function *function,
2194 uint64_t ranges, uint64_t base,
2195 backtrace_error_callback error_callback, void *data,
2196 struct function_vector *vec)
2198 struct dwarf_buf ranges_buf;
2200 if (ranges >= ddata->dwarf_ranges_size)
2202 error_callback (data, "function ranges offset out of range", 0);
2203 return 0;
2206 ranges_buf.name = ".debug_ranges";
2207 ranges_buf.start = ddata->dwarf_ranges;
2208 ranges_buf.buf = ddata->dwarf_ranges + ranges;
2209 ranges_buf.left = ddata->dwarf_ranges_size - ranges;
2210 ranges_buf.is_bigendian = ddata->is_bigendian;
2211 ranges_buf.error_callback = error_callback;
2212 ranges_buf.data = data;
2213 ranges_buf.reported_underflow = 0;
2215 while (1)
2217 uint64_t low;
2218 uint64_t high;
2220 if (ranges_buf.reported_underflow)
2221 return 0;
2223 low = read_address (&ranges_buf, u->addrsize);
2224 high = read_address (&ranges_buf, u->addrsize);
2226 if (low == 0 && high == 0)
2227 break;
2229 if (is_highest_address (low, u->addrsize))
2230 base = high;
2231 else
2233 if (!add_function_range (state, ddata, function, low + base,
2234 high + base, error_callback, data, vec))
2235 return 0;
2239 if (ranges_buf.reported_underflow)
2240 return 0;
2242 return 1;
2245 /* Read one entry plus all its children. Add function addresses to
2246 VEC. Returns 1 on success, 0 on error. */
2248 static int
2249 read_function_entry (struct backtrace_state *state, struct dwarf_data *ddata,
2250 struct unit *u, uint64_t base, struct dwarf_buf *unit_buf,
2251 const struct line_header *lhdr,
2252 backtrace_error_callback error_callback, void *data,
2253 struct function_vector *vec)
2255 while (unit_buf->left > 0)
2257 uint64_t code;
2258 const struct abbrev *abbrev;
2259 int is_function;
2260 struct function *function;
2261 size_t i;
2262 uint64_t lowpc;
2263 int have_lowpc;
2264 uint64_t highpc;
2265 int have_highpc;
2266 int highpc_is_relative;
2267 uint64_t ranges;
2268 int have_ranges;
2270 code = read_uleb128 (unit_buf);
2271 if (code == 0)
2272 return 1;
2274 abbrev = lookup_abbrev (&u->abbrevs, code, error_callback, data);
2275 if (abbrev == NULL)
2276 return 0;
2278 is_function = (abbrev->tag == DW_TAG_subprogram
2279 || abbrev->tag == DW_TAG_entry_point
2280 || abbrev->tag == DW_TAG_inlined_subroutine);
2282 function = NULL;
2283 if (is_function)
2285 function = ((struct function *)
2286 backtrace_alloc (state, sizeof *function,
2287 error_callback, data));
2288 if (function == NULL)
2289 return 0;
2290 memset (function, 0, sizeof *function);
2293 lowpc = 0;
2294 have_lowpc = 0;
2295 highpc = 0;
2296 have_highpc = 0;
2297 highpc_is_relative = 0;
2298 ranges = 0;
2299 have_ranges = 0;
2300 for (i = 0; i < abbrev->num_attrs; ++i)
2302 struct attr_val val;
2304 if (!read_attribute (abbrev->attrs[i].form, unit_buf,
2305 u->is_dwarf64, u->version, u->addrsize,
2306 ddata->dwarf_str, ddata->dwarf_str_size,
2307 &val))
2308 return 0;
2310 /* The compile unit sets the base address for any address
2311 ranges in the function entries. */
2312 if (abbrev->tag == DW_TAG_compile_unit
2313 && abbrev->attrs[i].name == DW_AT_low_pc
2314 && val.encoding == ATTR_VAL_ADDRESS)
2315 base = val.u.uint;
2317 if (is_function)
2319 switch (abbrev->attrs[i].name)
2321 case DW_AT_call_file:
2322 if (val.encoding == ATTR_VAL_UINT)
2324 if (val.u.uint == 0)
2325 function->caller_filename = "";
2326 else
2328 if (val.u.uint - 1 >= lhdr->filenames_count)
2330 dwarf_buf_error (unit_buf,
2331 ("invalid file number in "
2332 "DW_AT_call_file attribute"));
2333 return 0;
2335 function->caller_filename =
2336 lhdr->filenames[val.u.uint - 1];
2339 break;
2341 case DW_AT_call_line:
2342 if (val.encoding == ATTR_VAL_UINT)
2343 function->caller_lineno = val.u.uint;
2344 break;
2346 case DW_AT_abstract_origin:
2347 case DW_AT_specification:
2348 if (abbrev->attrs[i].form == DW_FORM_ref_addr
2349 || abbrev->attrs[i].form == DW_FORM_ref_sig8)
2351 /* This refers to an abstract origin defined in
2352 some other compilation unit. We can handle
2353 this case if we must, but it's harder. */
2354 break;
2356 if (val.encoding == ATTR_VAL_UINT
2357 || val.encoding == ATTR_VAL_REF_UNIT)
2359 const char *name;
2361 name = read_referenced_name (ddata, u, val.u.uint,
2362 error_callback, data);
2363 if (name != NULL)
2364 function->name = name;
2366 break;
2368 case DW_AT_name:
2369 if (val.encoding == ATTR_VAL_STRING)
2371 /* Don't override a name we found in some other
2372 way, as it will normally be more
2373 useful--e.g., this name is normally not
2374 mangled. */
2375 if (function->name == NULL)
2376 function->name = val.u.string;
2378 break;
2380 case DW_AT_linkage_name:
2381 case DW_AT_MIPS_linkage_name:
2382 if (val.encoding == ATTR_VAL_STRING)
2383 function->name = val.u.string;
2384 break;
2386 case DW_AT_low_pc:
2387 if (val.encoding == ATTR_VAL_ADDRESS)
2389 lowpc = val.u.uint;
2390 have_lowpc = 1;
2392 break;
2394 case DW_AT_high_pc:
2395 if (val.encoding == ATTR_VAL_ADDRESS)
2397 highpc = val.u.uint;
2398 have_highpc = 1;
2400 else if (val.encoding == ATTR_VAL_UINT)
2402 highpc = val.u.uint;
2403 have_highpc = 1;
2404 highpc_is_relative = 1;
2406 break;
2408 case DW_AT_ranges:
2409 if (val.encoding == ATTR_VAL_UINT
2410 || val.encoding == ATTR_VAL_REF_SECTION)
2412 ranges = val.u.uint;
2413 have_ranges = 1;
2415 break;
2417 default:
2418 break;
2423 /* If we couldn't find a name for the function, we have no use
2424 for it. */
2425 if (is_function && function->name == NULL)
2427 backtrace_free (state, function, sizeof *function,
2428 error_callback, data);
2429 is_function = 0;
2432 if (is_function)
2434 if (have_ranges)
2436 if (!add_function_ranges (state, ddata, u, function, ranges,
2437 base, error_callback, data, vec))
2438 return 0;
2440 else if (have_lowpc && have_highpc)
2442 if (highpc_is_relative)
2443 highpc += lowpc;
2444 if (!add_function_range (state, ddata, function, lowpc, highpc,
2445 error_callback, data, vec))
2446 return 0;
2448 else
2450 backtrace_free (state, function, sizeof *function,
2451 error_callback, data);
2452 is_function = 0;
2456 if (abbrev->has_children)
2458 if (!is_function)
2460 if (!read_function_entry (state, ddata, u, base, unit_buf, lhdr,
2461 error_callback, data, vec))
2462 return 0;
2464 else
2466 struct function_vector fvec;
2468 /* Gather any information for inlined functions in
2469 FVEC. */
2471 memset (&fvec, 0, sizeof fvec);
2473 if (!read_function_entry (state, ddata, u, base, unit_buf, lhdr,
2474 error_callback, data, &fvec))
2475 return 0;
2477 if (fvec.count > 0)
2479 struct function_addrs *faddrs;
2481 if (!backtrace_vector_release (state, &fvec.vec,
2482 error_callback, data))
2483 return 0;
2485 faddrs = (struct function_addrs *) fvec.vec.base;
2486 backtrace_qsort (faddrs, fvec.count,
2487 sizeof (struct function_addrs),
2488 function_addrs_compare);
2490 function->function_addrs = faddrs;
2491 function->function_addrs_count = fvec.count;
2497 return 1;
2500 /* Read function name information for a compilation unit. We look
2501 through the whole unit looking for function tags. */
2503 static void
2504 read_function_info (struct backtrace_state *state, struct dwarf_data *ddata,
2505 const struct line_header *lhdr,
2506 backtrace_error_callback error_callback, void *data,
2507 struct unit *u, struct function_vector *fvec,
2508 struct function_addrs **ret_addrs,
2509 size_t *ret_addrs_count)
2511 struct function_vector lvec;
2512 struct function_vector *pfvec;
2513 struct dwarf_buf unit_buf;
2514 struct function_addrs *addrs;
2515 size_t addrs_count;
2517 /* Use FVEC if it is not NULL. Otherwise use our own vector. */
2518 if (fvec != NULL)
2519 pfvec = fvec;
2520 else
2522 memset (&lvec, 0, sizeof lvec);
2523 pfvec = &lvec;
2526 unit_buf.name = ".debug_info";
2527 unit_buf.start = ddata->dwarf_info;
2528 unit_buf.buf = u->unit_data;
2529 unit_buf.left = u->unit_data_len;
2530 unit_buf.is_bigendian = ddata->is_bigendian;
2531 unit_buf.error_callback = error_callback;
2532 unit_buf.data = data;
2533 unit_buf.reported_underflow = 0;
2535 while (unit_buf.left > 0)
2537 if (!read_function_entry (state, ddata, u, 0, &unit_buf, lhdr,
2538 error_callback, data, pfvec))
2539 return;
2542 if (pfvec->count == 0)
2543 return;
2545 addrs_count = pfvec->count;
2547 if (fvec == NULL)
2549 if (!backtrace_vector_release (state, &lvec.vec, error_callback, data))
2550 return;
2551 addrs = (struct function_addrs *) pfvec->vec.base;
2553 else
2555 /* Finish this list of addresses, but leave the remaining space in
2556 the vector available for the next function unit. */
2557 addrs = ((struct function_addrs *)
2558 backtrace_vector_finish (state, &fvec->vec,
2559 error_callback, data));
2560 if (addrs == NULL)
2561 return;
2562 fvec->count = 0;
2565 backtrace_qsort (addrs, addrs_count, sizeof (struct function_addrs),
2566 function_addrs_compare);
2568 *ret_addrs = addrs;
2569 *ret_addrs_count = addrs_count;
2572 /* See if PC is inlined in FUNCTION. If it is, print out the inlined
2573 information, and update FILENAME and LINENO for the caller.
2574 Returns whatever CALLBACK returns, or 0 to keep going. */
2576 static int
2577 report_inlined_functions (uintptr_t pc, struct function *function,
2578 backtrace_full_callback callback, void *data,
2579 const char **filename, int *lineno)
2581 struct function_addrs *function_addrs;
2582 struct function *inlined;
2583 int ret;
2585 if (function->function_addrs_count == 0)
2586 return 0;
2588 function_addrs = ((struct function_addrs *)
2589 bsearch (&pc, function->function_addrs,
2590 function->function_addrs_count,
2591 sizeof (struct function_addrs),
2592 function_addrs_search));
2593 if (function_addrs == NULL)
2594 return 0;
2596 while (((size_t) (function_addrs - function->function_addrs) + 1
2597 < function->function_addrs_count)
2598 && pc >= (function_addrs + 1)->low
2599 && pc < (function_addrs + 1)->high)
2600 ++function_addrs;
2602 /* We found an inlined call. */
2604 inlined = function_addrs->function;
2606 /* Report any calls inlined into this one. */
2607 ret = report_inlined_functions (pc, inlined, callback, data,
2608 filename, lineno);
2609 if (ret != 0)
2610 return ret;
2612 /* Report this inlined call. */
2613 ret = callback (data, pc, *filename, *lineno, inlined->name);
2614 if (ret != 0)
2615 return ret;
2617 /* Our caller will report the caller of the inlined function; tell
2618 it the appropriate filename and line number. */
2619 *filename = inlined->caller_filename;
2620 *lineno = inlined->caller_lineno;
2622 return 0;
2625 /* Look for a PC in the DWARF mapping for one module. On success,
2626 call CALLBACK and return whatever it returns. On error, call
2627 ERROR_CALLBACK and return 0. Sets *FOUND to 1 if the PC is found,
2628 0 if not. */
2630 static int
2631 dwarf_lookup_pc (struct backtrace_state *state, struct dwarf_data *ddata,
2632 uintptr_t pc, backtrace_full_callback callback,
2633 backtrace_error_callback error_callback, void *data,
2634 int *found)
2636 struct unit_addrs *entry;
2637 struct unit *u;
2638 int new_data;
2639 struct line *lines;
2640 struct line *ln;
2641 struct function_addrs *function_addrs;
2642 struct function *function;
2643 const char *filename;
2644 int lineno;
2645 int ret;
2647 *found = 1;
2649 /* Find an address range that includes PC. */
2650 entry = bsearch (&pc, ddata->addrs, ddata->addrs_count,
2651 sizeof (struct unit_addrs), unit_addrs_search);
2653 if (entry == NULL)
2655 *found = 0;
2656 return 0;
2659 /* If there are multiple ranges that contain PC, use the last one,
2660 in order to produce predictable results. If we assume that all
2661 ranges are properly nested, then the last range will be the
2662 smallest one. */
2663 while ((size_t) (entry - ddata->addrs) + 1 < ddata->addrs_count
2664 && pc >= (entry + 1)->low
2665 && pc < (entry + 1)->high)
2666 ++entry;
2668 /* We need the lines, lines_count, function_addrs,
2669 function_addrs_count fields of u. If they are not set, we need
2670 to set them. When running in threaded mode, we need to allow for
2671 the possibility that some other thread is setting them
2672 simultaneously. */
2674 u = entry->u;
2675 lines = u->lines;
2677 /* Skip units with no useful line number information by walking
2678 backward. Useless line number information is marked by setting
2679 lines == -1. */
2680 while (entry > ddata->addrs
2681 && pc >= (entry - 1)->low
2682 && pc < (entry - 1)->high)
2684 if (state->threaded)
2685 lines = (struct line *) backtrace_atomic_load_pointer (&u->lines);
2687 if (lines != (struct line *) (uintptr_t) -1)
2688 break;
2690 --entry;
2692 u = entry->u;
2693 lines = u->lines;
2696 if (state->threaded)
2697 lines = backtrace_atomic_load_pointer (&u->lines);
2699 new_data = 0;
2700 if (lines == NULL)
2702 size_t function_addrs_count;
2703 struct line_header lhdr;
2704 size_t count;
2706 /* We have never read the line information for this unit. Read
2707 it now. */
2709 function_addrs = NULL;
2710 function_addrs_count = 0;
2711 if (read_line_info (state, ddata, error_callback, data, entry->u, &lhdr,
2712 &lines, &count))
2714 struct function_vector *pfvec;
2716 /* If not threaded, reuse DDATA->FVEC for better memory
2717 consumption. */
2718 if (state->threaded)
2719 pfvec = NULL;
2720 else
2721 pfvec = &ddata->fvec;
2722 read_function_info (state, ddata, &lhdr, error_callback, data,
2723 entry->u, pfvec, &function_addrs,
2724 &function_addrs_count);
2725 free_line_header (state, &lhdr, error_callback, data);
2726 new_data = 1;
2729 /* Atomically store the information we just read into the unit.
2730 If another thread is simultaneously writing, it presumably
2731 read the same information, and we don't care which one we
2732 wind up with; we just leak the other one. We do have to
2733 write the lines field last, so that the acquire-loads above
2734 ensure that the other fields are set. */
2736 if (!state->threaded)
2738 u->lines_count = count;
2739 u->function_addrs = function_addrs;
2740 u->function_addrs_count = function_addrs_count;
2741 u->lines = lines;
2743 else
2745 backtrace_atomic_store_size_t (&u->lines_count, count);
2746 backtrace_atomic_store_pointer (&u->function_addrs, function_addrs);
2747 backtrace_atomic_store_size_t (&u->function_addrs_count,
2748 function_addrs_count);
2749 backtrace_atomic_store_pointer (&u->lines, lines);
2753 /* Now all fields of U have been initialized. */
2755 if (lines == (struct line *) (uintptr_t) -1)
2757 /* If reading the line number information failed in some way,
2758 try again to see if there is a better compilation unit for
2759 this PC. */
2760 if (new_data)
2761 return dwarf_lookup_pc (state, ddata, pc, callback, error_callback,
2762 data, found);
2763 return callback (data, pc, NULL, 0, NULL);
2766 /* Search for PC within this unit. */
2768 ln = (struct line *) bsearch (&pc, lines, entry->u->lines_count,
2769 sizeof (struct line), line_search);
2770 if (ln == NULL)
2772 /* The PC is between the low_pc and high_pc attributes of the
2773 compilation unit, but no entry in the line table covers it.
2774 This implies that the start of the compilation unit has no
2775 line number information. */
2777 if (entry->u->abs_filename == NULL)
2779 const char *filename;
2781 filename = entry->u->filename;
2782 if (filename != NULL
2783 && !IS_ABSOLUTE_PATH (filename)
2784 && entry->u->comp_dir != NULL)
2786 size_t filename_len;
2787 const char *dir;
2788 size_t dir_len;
2789 char *s;
2791 filename_len = strlen (filename);
2792 dir = entry->u->comp_dir;
2793 dir_len = strlen (dir);
2794 s = (char *) backtrace_alloc (state, dir_len + filename_len + 2,
2795 error_callback, data);
2796 if (s == NULL)
2798 *found = 0;
2799 return 0;
2801 memcpy (s, dir, dir_len);
2802 /* FIXME: Should use backslash if DOS file system. */
2803 s[dir_len] = '/';
2804 memcpy (s + dir_len + 1, filename, filename_len + 1);
2805 filename = s;
2807 entry->u->abs_filename = filename;
2810 return callback (data, pc, entry->u->abs_filename, 0, NULL);
2813 /* Search for function name within this unit. */
2815 if (entry->u->function_addrs_count == 0)
2816 return callback (data, pc, ln->filename, ln->lineno, NULL);
2818 function_addrs = ((struct function_addrs *)
2819 bsearch (&pc, entry->u->function_addrs,
2820 entry->u->function_addrs_count,
2821 sizeof (struct function_addrs),
2822 function_addrs_search));
2823 if (function_addrs == NULL)
2824 return callback (data, pc, ln->filename, ln->lineno, NULL);
2826 /* If there are multiple function ranges that contain PC, use the
2827 last one, in order to produce predictable results. */
2829 while (((size_t) (function_addrs - entry->u->function_addrs + 1)
2830 < entry->u->function_addrs_count)
2831 && pc >= (function_addrs + 1)->low
2832 && pc < (function_addrs + 1)->high)
2833 ++function_addrs;
2835 function = function_addrs->function;
2837 filename = ln->filename;
2838 lineno = ln->lineno;
2840 ret = report_inlined_functions (pc, function, callback, data,
2841 &filename, &lineno);
2842 if (ret != 0)
2843 return ret;
2845 return callback (data, pc, filename, lineno, function->name);
2849 /* Return the file/line information for a PC using the DWARF mapping
2850 we built earlier. */
2852 static int
2853 dwarf_fileline (struct backtrace_state *state, uintptr_t pc,
2854 backtrace_full_callback callback,
2855 backtrace_error_callback error_callback, void *data)
2857 struct dwarf_data *ddata;
2858 int found;
2859 int ret;
2861 if (!state->threaded)
2863 for (ddata = (struct dwarf_data *) state->fileline_data;
2864 ddata != NULL;
2865 ddata = ddata->next)
2867 ret = dwarf_lookup_pc (state, ddata, pc, callback, error_callback,
2868 data, &found);
2869 if (ret != 0 || found)
2870 return ret;
2873 else
2875 struct dwarf_data **pp;
2877 pp = (struct dwarf_data **) (void *) &state->fileline_data;
2878 while (1)
2880 ddata = backtrace_atomic_load_pointer (pp);
2881 if (ddata == NULL)
2882 break;
2884 ret = dwarf_lookup_pc (state, ddata, pc, callback, error_callback,
2885 data, &found);
2886 if (ret != 0 || found)
2887 return ret;
2889 pp = &ddata->next;
2893 /* FIXME: See if any libraries have been dlopen'ed. */
2895 return callback (data, pc, NULL, 0, NULL);
2898 /* Initialize our data structures from the DWARF debug info for a
2899 file. Return NULL on failure. */
2901 static struct dwarf_data *
2902 build_dwarf_data (struct backtrace_state *state,
2903 uintptr_t base_address,
2904 const unsigned char *dwarf_info,
2905 size_t dwarf_info_size,
2906 const unsigned char *dwarf_line,
2907 size_t dwarf_line_size,
2908 const unsigned char *dwarf_abbrev,
2909 size_t dwarf_abbrev_size,
2910 const unsigned char *dwarf_ranges,
2911 size_t dwarf_ranges_size,
2912 const unsigned char *dwarf_str,
2913 size_t dwarf_str_size,
2914 int is_bigendian,
2915 backtrace_error_callback error_callback,
2916 void *data)
2918 struct unit_addrs_vector addrs_vec;
2919 struct unit_addrs *addrs;
2920 size_t addrs_count;
2921 struct dwarf_data *fdata;
2923 if (!build_address_map (state, base_address, dwarf_info, dwarf_info_size,
2924 dwarf_abbrev, dwarf_abbrev_size, dwarf_ranges,
2925 dwarf_ranges_size, dwarf_str, dwarf_str_size,
2926 is_bigendian, error_callback, data, &addrs_vec))
2927 return NULL;
2929 if (!backtrace_vector_release (state, &addrs_vec.vec, error_callback, data))
2930 return NULL;
2931 addrs = (struct unit_addrs *) addrs_vec.vec.base;
2932 addrs_count = addrs_vec.count;
2933 backtrace_qsort (addrs, addrs_count, sizeof (struct unit_addrs),
2934 unit_addrs_compare);
2936 fdata = ((struct dwarf_data *)
2937 backtrace_alloc (state, sizeof (struct dwarf_data),
2938 error_callback, data));
2939 if (fdata == NULL)
2940 return NULL;
2942 fdata->next = NULL;
2943 fdata->base_address = base_address;
2944 fdata->addrs = addrs;
2945 fdata->addrs_count = addrs_count;
2946 fdata->dwarf_info = dwarf_info;
2947 fdata->dwarf_info_size = dwarf_info_size;
2948 fdata->dwarf_line = dwarf_line;
2949 fdata->dwarf_line_size = dwarf_line_size;
2950 fdata->dwarf_ranges = dwarf_ranges;
2951 fdata->dwarf_ranges_size = dwarf_ranges_size;
2952 fdata->dwarf_str = dwarf_str;
2953 fdata->dwarf_str_size = dwarf_str_size;
2954 fdata->is_bigendian = is_bigendian;
2955 memset (&fdata->fvec, 0, sizeof fdata->fvec);
2957 return fdata;
2960 /* Build our data structures from the DWARF sections for a module.
2961 Set FILELINE_FN and STATE->FILELINE_DATA. Return 1 on success, 0
2962 on failure. */
2965 backtrace_dwarf_add (struct backtrace_state *state,
2966 uintptr_t base_address,
2967 const unsigned char *dwarf_info,
2968 size_t dwarf_info_size,
2969 const unsigned char *dwarf_line,
2970 size_t dwarf_line_size,
2971 const unsigned char *dwarf_abbrev,
2972 size_t dwarf_abbrev_size,
2973 const unsigned char *dwarf_ranges,
2974 size_t dwarf_ranges_size,
2975 const unsigned char *dwarf_str,
2976 size_t dwarf_str_size,
2977 int is_bigendian,
2978 backtrace_error_callback error_callback,
2979 void *data, fileline *fileline_fn)
2981 struct dwarf_data *fdata;
2983 fdata = build_dwarf_data (state, base_address, dwarf_info, dwarf_info_size,
2984 dwarf_line, dwarf_line_size, dwarf_abbrev,
2985 dwarf_abbrev_size, dwarf_ranges, dwarf_ranges_size,
2986 dwarf_str, dwarf_str_size, is_bigendian,
2987 error_callback, data);
2988 if (fdata == NULL)
2989 return 0;
2991 if (!state->threaded)
2993 struct dwarf_data **pp;
2995 for (pp = (struct dwarf_data **) (void *) &state->fileline_data;
2996 *pp != NULL;
2997 pp = &(*pp)->next)
2999 *pp = fdata;
3001 else
3003 while (1)
3005 struct dwarf_data **pp;
3007 pp = (struct dwarf_data **) (void *) &state->fileline_data;
3009 while (1)
3011 struct dwarf_data *p;
3013 p = backtrace_atomic_load_pointer (pp);
3015 if (p == NULL)
3016 break;
3018 pp = &p->next;
3021 if (__sync_bool_compare_and_swap (pp, NULL, fdata))
3022 break;
3026 *fileline_fn = dwarf_fileline;
3028 return 1;