2012-10-06 Janus Weil <janus@gcc.gnu.org>
[official-gcc.git] / libbacktrace / dwarf.c
blob4e13fc541ee96231aff426fbe01c30635d20c8c0
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 /* A sorted list of address ranges. */
337 struct unit_addrs *addrs;
338 /* Number of address ranges in list. */
339 size_t addrs_count;
340 /* The unparsed .debug_info section. */
341 const unsigned char *dwarf_info;
342 size_t dwarf_info_size;
343 /* The unparsed .debug_line section. */
344 const unsigned char *dwarf_line;
345 size_t dwarf_line_size;
346 /* The unparsed .debug_ranges section. */
347 const unsigned char *dwarf_ranges;
348 size_t dwarf_ranges_size;
349 /* The unparsed .debug_str section. */
350 const unsigned char *dwarf_str;
351 size_t dwarf_str_size;
352 /* Whether the data is big-endian or not. */
353 int is_bigendian;
354 /* A vector used for function addresses. We keep this here so that
355 we can grow the vector as we read more functions. */
356 struct function_vector fvec;
359 /* Report an error for a DWARF buffer. */
361 static void
362 dwarf_buf_error (struct dwarf_buf *buf, const char *msg)
364 char b[200];
366 snprintf (b, sizeof b, "%s in %s at %d",
367 msg, buf->name, (int) (buf->buf - buf->start));
368 buf->error_callback (buf->data, b, 0);
371 /* Require at least COUNT bytes in BUF. Return 1 if all is well, 0 on
372 error. */
374 static int
375 require (struct dwarf_buf *buf, size_t count)
377 if (buf->left >= count)
378 return 1;
380 if (!buf->reported_underflow)
382 dwarf_buf_error (buf, "DWARF underflow");
383 buf->reported_underflow = 1;
386 return 0;
389 /* Advance COUNT bytes in BUF. Return 1 if all is well, 0 on
390 error. */
392 static int
393 advance (struct dwarf_buf *buf, size_t count)
395 if (!require (buf, count))
396 return 0;
397 buf->buf += count;
398 buf->left -= count;
399 return 1;
402 /* Read one byte from BUF and advance 1 byte. */
404 static unsigned char
405 read_byte (struct dwarf_buf *buf)
407 const unsigned char *p = buf->buf;
409 if (!advance (buf, 1))
410 return 0;
411 return p[0];
414 /* Read a signed char from BUF and advance 1 byte. */
416 static signed char
417 read_sbyte (struct dwarf_buf *buf)
419 const unsigned char *p = buf->buf;
421 if (!advance (buf, 1))
422 return 0;
423 return (*p ^ 0x80) - 0x80;
426 /* Read a uint16 from BUF and advance 2 bytes. */
428 static uint16_t
429 read_uint16 (struct dwarf_buf *buf)
431 const unsigned char *p = buf->buf;
433 if (!advance (buf, 2))
434 return 0;
435 if (buf->is_bigendian)
436 return ((uint16_t) p[0] << 8) | (uint16_t) p[1];
437 else
438 return ((uint16_t) p[1] << 8) | (uint16_t) p[0];
441 /* Read a uint32 from BUF and advance 4 bytes. */
443 static uint32_t
444 read_uint32 (struct dwarf_buf *buf)
446 const unsigned char *p = buf->buf;
448 if (!advance (buf, 4))
449 return 0;
450 if (buf->is_bigendian)
451 return (((uint32_t) p[0] << 24) | ((uint32_t) p[1] << 16)
452 | ((uint32_t) p[2] << 8) | (uint32_t) p[3]);
453 else
454 return (((uint32_t) p[3] << 24) | ((uint32_t) p[2] << 16)
455 | ((uint32_t) p[1] << 8) | (uint32_t) p[0]);
458 /* Read a uint64 from BUF and advance 8 bytes. */
460 static uint64_t
461 read_uint64 (struct dwarf_buf *buf)
463 const unsigned char *p = buf->buf;
465 if (!advance (buf, 8))
466 return 0;
467 if (buf->is_bigendian)
468 return (((uint64_t) p[0] << 56) | ((uint64_t) p[1] << 48)
469 | ((uint64_t) p[2] << 40) | ((uint64_t) p[3] << 32)
470 | ((uint64_t) p[4] << 24) | ((uint64_t) p[5] << 16)
471 | ((uint64_t) p[6] << 8) | (uint64_t) p[7]);
472 else
473 return (((uint64_t) p[7] << 56) | ((uint64_t) p[6] << 48)
474 | ((uint64_t) p[5] << 40) | ((uint64_t) p[4] << 32)
475 | ((uint64_t) p[3] << 24) | ((uint64_t) p[2] << 16)
476 | ((uint64_t) p[1] << 8) | (uint64_t) p[0]);
479 /* Read an offset from BUF and advance the appropriate number of
480 bytes. */
482 static uint64_t
483 read_offset (struct dwarf_buf *buf, int is_dwarf64)
485 if (is_dwarf64)
486 return read_uint64 (buf);
487 else
488 return read_uint32 (buf);
491 /* Read an address from BUF and advance the appropriate number of
492 bytes. */
494 static uint64_t
495 read_address (struct dwarf_buf *buf, int addrsize)
497 switch (addrsize)
499 case 1:
500 return read_byte (buf);
501 case 2:
502 return read_uint16 (buf);
503 case 4:
504 return read_uint32 (buf);
505 case 8:
506 return read_uint64 (buf);
507 default:
508 dwarf_buf_error (buf, "unrecognized address size");
509 return 0;
513 /* Return whether a value is the highest possible address, given the
514 address size. */
516 static int
517 is_highest_address (uint64_t address, int addrsize)
519 switch (addrsize)
521 case 1:
522 return address == (unsigned char) -1;
523 case 2:
524 return address == (uint16_t) -1;
525 case 4:
526 return address == (uint32_t) -1;
527 case 8:
528 return address == (uint64_t) -1;
529 default:
530 return 0;
534 /* Read an unsigned LEB128 number. */
536 static uint64_t
537 read_uleb128 (struct dwarf_buf *buf)
539 uint64_t ret;
540 unsigned int shift;
541 int overflow;
542 unsigned char b;
544 ret = 0;
545 shift = 0;
546 overflow = 0;
549 const unsigned char *p;
551 p = buf->buf;
552 if (!advance (buf, 1))
553 return 0;
554 b = *p;
555 if (shift < 64)
556 ret |= ((uint64_t) (b & 0x7f)) << shift;
557 else if (!overflow)
559 dwarf_buf_error (buf, "LEB128 overflows uint64_t");
560 overflow = 1;
562 shift += 7;
564 while ((b & 0x80) != 0);
566 return ret;
569 /* Read a signed LEB128 number. */
571 static int64_t
572 read_sleb128 (struct dwarf_buf *buf)
574 uint64_t val;
575 unsigned int shift;
576 int overflow;
577 unsigned char b;
579 val = 0;
580 shift = 0;
581 overflow = 0;
584 const unsigned char *p;
586 p = buf->buf;
587 if (!advance (buf, 1))
588 return 0;
589 b = *p;
590 if (shift < 64)
591 val |= ((uint64_t) (b & 0x7f)) << shift;
592 else if (!overflow)
594 dwarf_buf_error (buf, "signed LEB128 overflows uint64_t");
595 overflow = 1;
597 shift += 7;
599 while ((b & 0x80) != 0);
601 if ((b & 0x40) != 0 && shift < 64)
602 val |= ((uint64_t) -1) << shift;
604 return (int64_t) val;
607 /* Return the length of an LEB128 number. */
609 static size_t
610 leb128_len (const unsigned char *p)
612 size_t ret;
614 ret = 1;
615 while ((*p & 0x80) != 0)
617 ++p;
618 ++ret;
620 return ret;
623 /* Free an abbreviations structure. */
625 static void
626 free_abbrevs (struct backtrace_state *state, struct abbrevs *abbrevs,
627 backtrace_error_callback error_callback, void *data)
629 size_t i;
631 for (i = 0; i < abbrevs->num_abbrevs; ++i)
632 backtrace_free (state, abbrevs->abbrevs[i].attrs,
633 abbrevs->abbrevs[i].num_attrs * sizeof (struct attr),
634 error_callback, data);
635 backtrace_free (state, abbrevs->abbrevs,
636 abbrevs->num_abbrevs * sizeof (struct abbrev),
637 error_callback, data);
638 abbrevs->num_abbrevs = 0;
639 abbrevs->abbrevs = NULL;
642 /* Read an attribute value. Returns 1 on success, 0 on failure. If
643 the value can be represented as a uint64_t, sets *VAL and sets
644 *IS_VALID to 1. We don't try to store the value of other attribute
645 forms, because we don't care about them. */
647 static int
648 read_attribute (enum dwarf_form form, struct dwarf_buf *buf,
649 int is_dwarf64, int version, int addrsize,
650 const unsigned char *dwarf_str, size_t dwarf_str_size,
651 struct attr_val *val)
653 switch (form)
655 case DW_FORM_addr:
656 val->encoding = ATTR_VAL_ADDRESS;
657 val->u.uint = read_address (buf, addrsize);
658 return 1;
659 case DW_FORM_block2:
660 val->encoding = ATTR_VAL_BLOCK;
661 return advance (buf, read_uint16 (buf));
662 case DW_FORM_block4:
663 val->encoding = ATTR_VAL_BLOCK;
664 return advance (buf, read_uint32 (buf));
665 case DW_FORM_data2:
666 val->encoding = ATTR_VAL_UINT;
667 val->u.uint = read_uint16 (buf);
668 return 1;
669 case DW_FORM_data4:
670 val->encoding = ATTR_VAL_UINT;
671 val->u.uint = read_uint32 (buf);
672 return 1;
673 case DW_FORM_data8:
674 val->encoding = ATTR_VAL_UINT;
675 val->u.uint = read_uint64 (buf);
676 return 1;
677 case DW_FORM_string:
678 val->encoding = ATTR_VAL_STRING;
679 val->u.string = (const char *) buf->buf;
680 return advance (buf, strnlen ((const char *) buf->buf, buf->left) + 1);
681 case DW_FORM_block:
682 val->encoding = ATTR_VAL_BLOCK;
683 return advance (buf, read_uleb128 (buf));
684 case DW_FORM_block1:
685 val->encoding = ATTR_VAL_BLOCK;
686 return advance (buf, read_byte (buf));
687 case DW_FORM_data1:
688 val->encoding = ATTR_VAL_UINT;
689 val->u.uint = read_byte (buf);
690 return 1;
691 case DW_FORM_flag:
692 val->encoding = ATTR_VAL_UINT;
693 val->u.uint = read_byte (buf);
694 return 1;
695 case DW_FORM_sdata:
696 val->encoding = ATTR_VAL_SINT;
697 val->u.sint = read_sleb128 (buf);
698 return 1;
699 case DW_FORM_strp:
701 uint64_t offset;
703 offset = read_offset (buf, is_dwarf64);
704 if (offset >= dwarf_str_size)
706 dwarf_buf_error (buf, "DW_FORM_strp out of range");
707 return 0;
709 val->encoding = ATTR_VAL_STRING;
710 val->u.string = (const char *) dwarf_str + offset;
711 return 1;
713 case DW_FORM_udata:
714 val->encoding = ATTR_VAL_UINT;
715 val->u.uint = read_uleb128 (buf);
716 return 1;
717 case DW_FORM_ref_addr:
718 val->encoding = ATTR_VAL_REF_INFO;
719 if (version == 2)
720 val->u.uint = read_address (buf, addrsize);
721 else
722 val->u.uint = read_offset (buf, is_dwarf64);
723 return 1;
724 case DW_FORM_ref1:
725 val->encoding = ATTR_VAL_REF_UNIT;
726 val->u.uint = read_byte (buf);
727 return 1;
728 case DW_FORM_ref2:
729 val->encoding = ATTR_VAL_REF_UNIT;
730 val->u.uint = read_uint16 (buf);
731 return 1;
732 case DW_FORM_ref4:
733 val->encoding = ATTR_VAL_REF_UNIT;
734 val->u.uint = read_uint32 (buf);
735 return 1;
736 case DW_FORM_ref8:
737 val->encoding = ATTR_VAL_REF_UNIT;
738 val->u.uint = read_uint64 (buf);
739 return 1;
740 case DW_FORM_ref_udata:
741 val->encoding = ATTR_VAL_REF_UNIT;
742 val->u.uint = read_uleb128 (buf);
743 return 1;
744 case DW_FORM_indirect:
746 uint64_t form;
748 form = read_uleb128 (buf);
749 return read_attribute ((enum dwarf_form) form, buf, is_dwarf64,
750 version, addrsize, dwarf_str, dwarf_str_size,
751 val);
753 case DW_FORM_sec_offset:
754 val->encoding = ATTR_VAL_REF_SECTION;
755 val->u.uint = read_offset (buf, is_dwarf64);
756 return 1;
757 case DW_FORM_exprloc:
758 val->encoding = ATTR_VAL_EXPR;
759 return advance (buf, read_uleb128 (buf));
760 case DW_FORM_flag_present:
761 val->encoding = ATTR_VAL_UINT;
762 val->u.uint = 1;
763 return 1;
764 case DW_FORM_ref_sig8:
765 val->encoding = ATTR_VAL_REF_TYPE;
766 val->u.uint = read_uint64 (buf);
767 return 1;
768 case DW_FORM_GNU_addr_index:
769 val->encoding = ATTR_VAL_REF_SECTION;
770 val->u.uint = read_uleb128 (buf);
771 return 1;
772 case DW_FORM_GNU_str_index:
773 val->encoding = ATTR_VAL_REF_SECTION;
774 val->u.uint = read_uleb128 (buf);
775 return 1;
776 case DW_FORM_GNU_ref_alt:
777 val->encoding = ATTR_VAL_REF_SECTION;
778 val->u.uint = read_offset (buf, is_dwarf64);
779 return 1;
780 case DW_FORM_GNU_strp_alt:
781 val->encoding = ATTR_VAL_REF_SECTION;
782 val->u.uint = read_offset (buf, is_dwarf64);
783 return 1;
784 default:
785 dwarf_buf_error (buf, "unrecognized DWARF form");
786 return 0;
790 /* Compare function_addrs for qsort. When ranges are nested, make the
791 smallest one sort last. */
793 static int
794 function_addrs_compare (const void *v1, const void *v2)
796 const struct function_addrs *a1 = (const struct function_addrs *) v1;
797 const struct function_addrs *a2 = (const struct function_addrs *) v2;
799 if (a1->low < a2->low)
800 return -1;
801 if (a1->low > a2->low)
802 return 1;
803 if (a1->high < a2->high)
804 return 1;
805 if (a1->high > a2->high)
806 return -1;
807 return strcmp (a1->function->name, a2->function->name);
810 /* Compare a PC against a function_addrs for bsearch. Note that if
811 there are multiple ranges containing PC, which one will be returned
812 is unpredictable. We compensate for that in dwarf_fileline. */
814 static int
815 function_addrs_search (const void *vkey, const void *ventry)
817 const uintptr_t *key = (const uintptr_t *) vkey;
818 const struct function_addrs *entry = (const struct function_addrs *) ventry;
819 uintptr_t pc;
821 pc = *key;
822 if (pc < entry->low)
823 return -1;
824 else if (pc >= entry->high)
825 return 1;
826 else
827 return 0;
830 /* Add a new compilation unit address range to a vector. Returns 1 on
831 success, 0 on failure. */
833 static int
834 add_unit_addr (struct backtrace_state *state, struct unit_addrs addrs,
835 backtrace_error_callback error_callback, void *data,
836 struct unit_addrs_vector *vec)
838 struct unit_addrs *p;
840 /* Try to merge with the last entry. */
841 if (vec->count > 0)
843 p = (struct unit_addrs *) vec->vec.base + (vec->count - 1);
844 if ((addrs.low == p->high || addrs.low == p->high + 1)
845 && addrs.u == p->u)
847 if (addrs.high > p->high)
848 p->high = addrs.high;
849 return 1;
853 p = ((struct unit_addrs *)
854 backtrace_vector_grow (state, sizeof (struct unit_addrs),
855 error_callback, data, &vec->vec));
856 if (p == NULL)
857 return 0;
859 *p = addrs;
860 ++vec->count;
861 return 1;
864 /* Free a unit address vector. */
866 static void
867 free_unit_addrs_vector (struct backtrace_state *state,
868 struct unit_addrs_vector *vec,
869 backtrace_error_callback error_callback, void *data)
871 struct unit_addrs *addrs;
872 size_t i;
874 addrs = (struct unit_addrs *) vec->vec.base;
875 for (i = 0; i < vec->count; ++i)
876 free_abbrevs (state, &addrs[i].u->abbrevs, error_callback, data);
879 /* Compare unit_addrs for qsort. When ranges are nested, make the
880 smallest one sort last. */
882 static int
883 unit_addrs_compare (const void *v1, const void *v2)
885 const struct unit_addrs *a1 = (const struct unit_addrs *) v1;
886 const struct unit_addrs *a2 = (const struct unit_addrs *) v2;
888 if (a1->low < a2->low)
889 return -1;
890 if (a1->low > a2->low)
891 return 1;
892 if (a1->high < a2->high)
893 return 1;
894 if (a1->high > a2->high)
895 return -1;
896 if (a1->u->lineoff < a2->u->lineoff)
897 return -1;
898 if (a1->u->lineoff > a2->u->lineoff)
899 return 1;
900 return 0;
903 /* Compare a PC against a unit_addrs for bsearch. Note that if there
904 are multiple ranges containing PC, which one will be returned is
905 unpredictable. We compensate for that in dwarf_fileline. */
907 static int
908 unit_addrs_search (const void *vkey, const void *ventry)
910 const uintptr_t *key = (const uintptr_t *) vkey;
911 const struct unit_addrs *entry = (const struct unit_addrs *) ventry;
912 uintptr_t pc;
914 pc = *key;
915 if (pc < entry->low)
916 return -1;
917 else if (pc >= entry->high)
918 return 1;
919 else
920 return 0;
923 /* Sort the line vector by PC. We want a stable sort here. We know
924 that the pointers are into the same array, so it is safe to compare
925 them directly. */
927 static int
928 line_compare (const void *v1, const void *v2)
930 const struct line *ln1 = (const struct line *) v1;
931 const struct line *ln2 = (const struct line *) v2;
933 if (ln1->pc < ln2->pc)
934 return -1;
935 else if (ln1->pc > ln2->pc)
936 return 1;
937 else if (ln1 < ln2)
938 return -1;
939 else if (ln1 > ln2)
940 return 1;
941 else
942 return 0;
945 /* Find a PC in a line vector. We always allocate an extra entry at
946 the end of the lines vector, so that this routine can safely look
947 at the next entry. Note that when there are multiple mappings for
948 the same PC value, this will return the last one. */
950 static int
951 line_search (const void *vkey, const void *ventry)
953 const uintptr_t *key = (const uintptr_t *) vkey;
954 const struct line *entry = (const struct line *) ventry;
955 uintptr_t pc;
957 pc = *key;
958 if (pc < entry->pc)
959 return -1;
960 else if (pc >= (entry + 1)->pc)
961 return 1;
962 else
963 return 0;
966 /* Sort the abbrevs by the abbrev code. This function is passed to
967 both qsort and bsearch. */
969 static int
970 abbrev_compare (const void *v1, const void *v2)
972 const struct abbrev *a1 = (const struct abbrev *) v1;
973 const struct abbrev *a2 = (const struct abbrev *) v2;
975 if (a1->code < a2->code)
976 return -1;
977 else if (a1->code > a2->code)
978 return 1;
979 else
981 /* This really shouldn't happen. It means there are two
982 different abbrevs with the same code, and that means we don't
983 know which one lookup_abbrev should return. */
984 return 0;
988 /* Read the abbreviation table for a compilation unit. Returns 1 on
989 success, 0 on failure. */
991 static int
992 read_abbrevs (struct backtrace_state *state, uint64_t abbrev_offset,
993 const unsigned char *dwarf_abbrev, size_t dwarf_abbrev_size,
994 int is_bigendian, backtrace_error_callback error_callback,
995 void *data, struct abbrevs *abbrevs)
997 struct dwarf_buf abbrev_buf;
998 struct dwarf_buf count_buf;
999 size_t num_abbrevs;
1001 abbrevs->num_abbrevs = 0;
1002 abbrevs->abbrevs = NULL;
1004 if (abbrev_offset >= dwarf_abbrev_size)
1006 error_callback (data, "abbrev offset out of range", 0);
1007 return 0;
1010 abbrev_buf.name = ".debug_abbrev";
1011 abbrev_buf.start = dwarf_abbrev;
1012 abbrev_buf.buf = dwarf_abbrev + abbrev_offset;
1013 abbrev_buf.left = dwarf_abbrev_size - abbrev_offset;
1014 abbrev_buf.is_bigendian = is_bigendian;
1015 abbrev_buf.error_callback = error_callback;
1016 abbrev_buf.data = data;
1017 abbrev_buf.reported_underflow = 0;
1019 /* Count the number of abbrevs in this list. */
1021 count_buf = abbrev_buf;
1022 num_abbrevs = 0;
1023 while (read_uleb128 (&count_buf) != 0)
1025 if (count_buf.reported_underflow)
1026 return 0;
1027 ++num_abbrevs;
1028 // Skip tag.
1029 read_uleb128 (&count_buf);
1030 // Skip has_children.
1031 read_byte (&count_buf);
1032 // Skip attributes.
1033 while (read_uleb128 (&count_buf) != 0)
1034 read_uleb128 (&count_buf);
1035 // Skip form of last attribute.
1036 read_uleb128 (&count_buf);
1039 if (count_buf.reported_underflow)
1040 return 0;
1042 if (num_abbrevs == 0)
1043 return 1;
1045 abbrevs->num_abbrevs = num_abbrevs;
1046 abbrevs->abbrevs = ((struct abbrev *)
1047 backtrace_alloc (state,
1048 num_abbrevs * sizeof (struct abbrev),
1049 error_callback, data));
1050 if (abbrevs->abbrevs == NULL)
1051 return 0;
1052 memset (abbrevs->abbrevs, 0, num_abbrevs * sizeof (struct abbrev));
1054 num_abbrevs = 0;
1055 while (1)
1057 uint64_t code;
1058 struct abbrev a;
1059 size_t num_attrs;
1060 struct attr *attrs;
1062 if (abbrev_buf.reported_underflow)
1063 goto fail;
1065 code = read_uleb128 (&abbrev_buf);
1066 if (code == 0)
1067 break;
1069 a.code = code;
1070 a.tag = (enum dwarf_tag) read_uleb128 (&abbrev_buf);
1071 a.has_children = read_byte (&abbrev_buf);
1073 count_buf = abbrev_buf;
1074 num_attrs = 0;
1075 while (read_uleb128 (&count_buf) != 0)
1077 ++num_attrs;
1078 read_uleb128 (&count_buf);
1081 if (num_attrs == 0)
1083 attrs = NULL;
1084 read_uleb128 (&abbrev_buf);
1085 read_uleb128 (&abbrev_buf);
1087 else
1089 attrs = ((struct attr *)
1090 backtrace_alloc (state, num_attrs * sizeof *attrs,
1091 error_callback, data));
1092 if (attrs == NULL)
1093 goto fail;
1094 num_attrs = 0;
1095 while (1)
1097 uint64_t name;
1098 uint64_t form;
1100 name = read_uleb128 (&abbrev_buf);
1101 form = read_uleb128 (&abbrev_buf);
1102 if (name == 0)
1103 break;
1104 attrs[num_attrs].name = (enum dwarf_attribute) name;
1105 attrs[num_attrs].form = (enum dwarf_form) form;
1106 ++num_attrs;
1110 a.num_attrs = num_attrs;
1111 a.attrs = attrs;
1113 abbrevs->abbrevs[num_abbrevs] = a;
1114 ++num_abbrevs;
1117 qsort (abbrevs->abbrevs, abbrevs->num_abbrevs, sizeof (struct abbrev),
1118 abbrev_compare);
1120 return 1;
1122 fail:
1123 free_abbrevs (state, abbrevs, error_callback, data);
1124 return 0;
1127 /* Return the abbrev information for an abbrev code. */
1129 static const struct abbrev *
1130 lookup_abbrev (struct abbrevs *abbrevs, uint64_t code,
1131 backtrace_error_callback error_callback, void *data)
1133 struct abbrev key;
1134 void *p;
1136 /* With GCC, where abbrevs are simply numbered in order, we should
1137 be able to just look up the entry. */
1138 if (code - 1 < abbrevs->num_abbrevs
1139 && abbrevs->abbrevs[code - 1].code == code)
1140 return &abbrevs->abbrevs[code - 1];
1142 /* Otherwise we have to search. */
1143 memset (&key, 0, sizeof key);
1144 key.code = code;
1145 p = bsearch (&key, abbrevs->abbrevs, abbrevs->num_abbrevs,
1146 sizeof (struct abbrev), abbrev_compare);
1147 if (p == NULL)
1149 error_callback (data, "invalid abbreviation code", 0);
1150 return NULL;
1152 return (const struct abbrev *) p;
1155 /* Add non-contiguous address ranges for a compilation unit. Returns
1156 1 on success, 0 on failure. */
1158 static int
1159 add_unit_ranges (struct backtrace_state *state, struct unit *u,
1160 uint64_t ranges, uint64_t base, int is_bigendian,
1161 const unsigned char *dwarf_ranges, size_t dwarf_ranges_size,
1162 backtrace_error_callback error_callback, void *data,
1163 struct unit_addrs_vector *addrs)
1165 struct dwarf_buf ranges_buf;
1167 if (ranges >= dwarf_ranges_size)
1169 error_callback (data, "ranges offset out of range", 0);
1170 return 0;
1173 ranges_buf.name = ".debug_ranges";
1174 ranges_buf.start = dwarf_ranges;
1175 ranges_buf.buf = dwarf_ranges + ranges;
1176 ranges_buf.left = dwarf_ranges_size - ranges;
1177 ranges_buf.is_bigendian = is_bigendian;
1178 ranges_buf.error_callback = error_callback;
1179 ranges_buf.data = data;
1180 ranges_buf.reported_underflow = 0;
1182 while (1)
1184 uint64_t low;
1185 uint64_t high;
1187 if (ranges_buf.reported_underflow)
1188 return 0;
1190 low = read_address (&ranges_buf, u->addrsize);
1191 high = read_address (&ranges_buf, u->addrsize);
1193 if (low == 0 && high == 0)
1194 break;
1196 if (is_highest_address (low, u->addrsize))
1197 base = high;
1198 else
1200 struct unit_addrs a;
1202 a.low = low + base;
1203 a.high = high + base;
1204 a.u = u;
1205 if (!add_unit_addr (state, a, error_callback, data, addrs))
1206 return 0;
1210 if (ranges_buf.reported_underflow)
1211 return 0;
1213 return 1;
1216 /* Build a mapping from address ranges to the compilation units where
1217 the line number information for that range can be found. Returns 1
1218 on success, 0 on failure. */
1220 static int
1221 build_address_map (struct backtrace_state *state,
1222 const unsigned char *dwarf_info, size_t dwarf_info_size,
1223 const unsigned char *dwarf_abbrev, size_t dwarf_abbrev_size,
1224 const unsigned char *dwarf_ranges, size_t dwarf_ranges_size,
1225 const unsigned char *dwarf_str, size_t dwarf_str_size,
1226 int is_bigendian, backtrace_error_callback error_callback,
1227 void *data, struct unit_addrs_vector *addrs)
1229 struct dwarf_buf info;
1230 struct abbrevs abbrevs;
1232 memset (&addrs->vec, 0, sizeof addrs->vec);
1233 addrs->count = 0;
1235 /* Read through the .debug_info section. FIXME: Should we use the
1236 .debug_aranges section? gdb and addr2line don't use it, but I'm
1237 not sure why. */
1239 info.name = ".debug_info";
1240 info.start = dwarf_info;
1241 info.buf = dwarf_info;
1242 info.left = dwarf_info_size;
1243 info.is_bigendian = is_bigendian;
1244 info.error_callback = error_callback;
1245 info.data = data;
1246 info.reported_underflow = 0;
1248 memset (&abbrevs, 0, sizeof abbrevs);
1249 while (info.left > 0)
1251 const unsigned char *unit_data_start;
1252 uint64_t len;
1253 int is_dwarf64;
1254 struct dwarf_buf unit_buf;
1255 int version;
1256 uint64_t abbrev_offset;
1257 const struct abbrev *abbrev;
1258 int addrsize;
1259 const unsigned char *unit_data;
1260 size_t unit_data_len;
1261 size_t unit_data_offset;
1262 uint64_t code;
1263 size_t i;
1264 uint64_t lowpc;
1265 int have_lowpc;
1266 uint64_t highpc;
1267 int have_highpc;
1268 int highpc_is_relative;
1269 uint64_t ranges;
1270 int have_ranges;
1271 uint64_t lineoff;
1272 int have_lineoff;
1273 const char *comp_dir;
1275 if (info.reported_underflow)
1276 goto fail;
1278 unit_data_start = info.buf;
1280 is_dwarf64 = 0;
1281 len = read_uint32 (&info);
1282 if (len == 0xffffffff)
1284 len = read_uint64 (&info);
1285 is_dwarf64 = 1;
1288 unit_buf = info;
1289 unit_buf.left = len;
1291 if (!advance (&info, len))
1292 goto fail;
1294 version = read_uint16 (&unit_buf);
1295 if (version < 2 || version > 4)
1297 dwarf_buf_error (&unit_buf, "unrecognized DWARF version");
1298 goto fail;
1301 abbrev_offset = read_offset (&unit_buf, is_dwarf64);
1302 if (!read_abbrevs (state, abbrev_offset, dwarf_abbrev, dwarf_abbrev_size,
1303 is_bigendian, error_callback, data, &abbrevs))
1304 goto fail;
1306 addrsize = read_byte (&unit_buf);
1308 unit_data = unit_buf.buf;
1309 unit_data_len = unit_buf.left;
1310 unit_data_offset = unit_buf.buf - unit_data_start;
1312 /* We only look at the first attribute in the compilation unit.
1313 In practice this will be a DW_TAG_compile_unit which will
1314 tell us the PC range and where to find the line number
1315 information. */
1317 code = read_uleb128 (&unit_buf);
1318 abbrev = lookup_abbrev (&abbrevs, code, error_callback, data);
1319 if (abbrev == NULL)
1320 goto fail;
1322 lowpc = 0;
1323 have_lowpc = 0;
1324 highpc = 0;
1325 have_highpc = 0;
1326 highpc_is_relative = 0;
1327 ranges = 0;
1328 have_ranges = 0;
1329 lineoff = 0;
1330 have_lineoff = 0;
1331 comp_dir = NULL;
1332 for (i = 0; i < abbrev->num_attrs; ++i)
1334 struct attr_val val;
1336 if (!read_attribute (abbrev->attrs[i].form, &unit_buf, is_dwarf64,
1337 version, addrsize, dwarf_str, dwarf_str_size,
1338 &val))
1339 goto fail;
1341 switch (abbrev->attrs[i].name)
1343 case DW_AT_low_pc:
1344 if (val.encoding == ATTR_VAL_ADDRESS)
1346 lowpc = val.u.uint;
1347 have_lowpc = 1;
1349 break;
1350 case DW_AT_high_pc:
1351 if (val.encoding == ATTR_VAL_ADDRESS)
1353 highpc = val.u.uint;
1354 have_highpc = 1;
1356 else if (val.encoding == ATTR_VAL_UINT)
1358 highpc = val.u.uint;
1359 have_highpc = 1;
1360 highpc_is_relative = 1;
1362 break;
1363 case DW_AT_ranges:
1364 if (val.encoding == ATTR_VAL_UINT
1365 || val.encoding == ATTR_VAL_REF_SECTION)
1367 ranges = val.u.uint;
1368 have_ranges = 1;
1370 break;
1371 case DW_AT_stmt_list:
1372 if (val.encoding == ATTR_VAL_UINT
1373 || val.encoding == ATTR_VAL_REF_SECTION)
1375 lineoff = val.u.uint;
1376 have_lineoff = 1;
1378 break;
1379 case DW_AT_comp_dir:
1380 if (val.encoding == ATTR_VAL_STRING)
1381 comp_dir = val.u.string;
1382 break;
1383 default:
1384 break;
1388 if (unit_buf.reported_underflow)
1389 goto fail;
1391 if (((have_lowpc && have_highpc) || have_ranges) && have_lineoff)
1393 struct unit *u;
1394 struct unit_addrs a;
1396 u = ((struct unit *)
1397 backtrace_alloc (state, sizeof *u, error_callback, data));
1398 if (u == NULL)
1399 goto fail;
1400 u->unit_data = unit_data;
1401 u->unit_data_len = unit_data_len;
1402 u->unit_data_offset = unit_data_offset;
1403 u->version = version;
1404 u->is_dwarf64 = is_dwarf64;
1405 u->addrsize = addrsize;
1406 u->comp_dir = comp_dir;
1407 u->lineoff = lineoff;
1408 u->abbrevs = abbrevs;
1409 memset (&abbrevs, 0, sizeof abbrevs);
1411 /* The actual line number mappings will be read as
1412 needed. */
1413 u->lines = NULL;
1414 u->lines_count = 0;
1415 u->function_addrs = NULL;
1416 u->function_addrs_count = 0;
1418 if (have_ranges)
1420 if (!add_unit_ranges (state, u, ranges, lowpc, is_bigendian,
1421 dwarf_ranges, dwarf_ranges_size,
1422 error_callback, data, addrs))
1424 free_abbrevs (state, &u->abbrevs, error_callback, data);
1425 backtrace_free (state, u, sizeof *u, error_callback, data);
1426 goto fail;
1429 else
1431 if (highpc_is_relative)
1432 highpc += lowpc;
1433 a.low = lowpc;
1434 a.high = highpc;
1435 a.u = u;
1437 if (!add_unit_addr (state, a, error_callback, data, addrs))
1439 free_abbrevs (state, &u->abbrevs, error_callback, data);
1440 backtrace_free (state, u, sizeof *u, error_callback, data);
1441 goto fail;
1445 else
1447 free_abbrevs (state, &abbrevs, error_callback, data);
1448 memset (&abbrevs, 0, sizeof abbrevs);
1451 if (info.reported_underflow)
1452 goto fail;
1454 return 1;
1456 fail:
1457 free_abbrevs (state, &abbrevs, error_callback, data);
1458 free_unit_addrs_vector (state, addrs, error_callback, data);
1459 return 0;
1462 /* Add a new mapping to the vector of line mappings that we are
1463 building. Returns 1 on success, 0 on failure. */
1465 static int
1466 add_line (struct backtrace_state *state, uintptr_t pc, const char *filename,
1467 int lineno, backtrace_error_callback error_callback, void *data,
1468 struct line_vector *vec)
1470 struct line *ln;
1472 /* If we are adding the same mapping, ignore it. This can happen
1473 when using discriminators. */
1474 if (vec->count > 0)
1476 ln = (struct line *) vec->vec.base + (vec->count - 1);
1477 if (pc == ln->pc && filename == ln->filename && lineno == ln->lineno)
1478 return 1;
1481 ln = ((struct line *)
1482 backtrace_vector_grow (state, sizeof (struct line), error_callback,
1483 data, &vec->vec));
1484 if (ln == NULL)
1485 return 0;
1487 ln->pc = pc;
1488 ln->filename = filename;
1489 ln->lineno = lineno;
1491 ++vec->count;
1493 return 1;
1496 /* Free the line header information. If FREE_FILENAMES is true we
1497 free the file names themselves, otherwise we leave them, as there
1498 may be line structures pointing to them. */
1500 static void
1501 free_line_header (struct backtrace_state *state, struct line_header *hdr,
1502 backtrace_error_callback error_callback, void *data)
1504 backtrace_free (state, hdr->dirs, hdr->dirs_count * sizeof (const char *),
1505 error_callback, data);
1506 backtrace_free (state, hdr->filenames,
1507 hdr->filenames_count * sizeof (char *),
1508 error_callback, data);
1511 /* Read the line header. Return 1 on success, 0 on failure. */
1513 static int
1514 read_line_header (struct backtrace_state *state, struct unit *u,
1515 int is_dwarf64, struct dwarf_buf *line_buf,
1516 struct line_header *hdr)
1518 uint64_t hdrlen;
1519 struct dwarf_buf hdr_buf;
1520 const unsigned char *p;
1521 const unsigned char *pend;
1522 size_t i;
1524 hdr->version = read_uint16 (line_buf);
1525 if (hdr->version < 2 || hdr->version > 4)
1527 dwarf_buf_error (line_buf, "unsupported line number version");
1528 return 0;
1531 hdrlen = read_offset (line_buf, is_dwarf64);
1533 hdr_buf = *line_buf;
1534 hdr_buf.left = hdrlen;
1536 if (!advance (line_buf, hdrlen))
1537 return 0;
1539 hdr->min_insn_len = read_byte (&hdr_buf);
1540 if (hdr->version < 4)
1541 hdr->max_ops_per_insn = 1;
1542 else
1543 hdr->max_ops_per_insn = read_byte (&hdr_buf);
1545 /* We don't care about default_is_stmt. */
1546 read_byte (&hdr_buf);
1548 hdr->line_base = read_sbyte (&hdr_buf);
1549 hdr->line_range = read_byte (&hdr_buf);
1551 hdr->opcode_base = read_byte (&hdr_buf);
1552 hdr->opcode_lengths = hdr_buf.buf;
1553 if (!advance (&hdr_buf, hdr->opcode_base - 1))
1554 return 0;
1556 /* Count the number of directory entries. */
1557 hdr->dirs_count = 0;
1558 p = hdr_buf.buf;
1559 pend = p + hdr_buf.left;
1560 while (p < pend && *p != '\0')
1562 p += strnlen((const char *) p, pend - p) + 1;
1563 ++hdr->dirs_count;
1566 hdr->dirs = ((const char **)
1567 backtrace_alloc (state,
1568 hdr->dirs_count * sizeof (const char *),
1569 line_buf->error_callback, line_buf->data));
1570 if (hdr->dirs == NULL)
1571 return 0;
1573 i = 0;
1574 while (*hdr_buf.buf != '\0')
1576 if (hdr_buf.reported_underflow)
1577 return 0;
1579 hdr->dirs[i] = (const char *) hdr_buf.buf;
1580 ++i;
1581 if (!advance (&hdr_buf,
1582 strnlen ((const char *) hdr_buf.buf, hdr_buf.left) + 1))
1583 return 0;
1585 if (!advance (&hdr_buf, 1))
1586 return 0;
1588 /* Count the number of file entries. */
1589 hdr->filenames_count = 0;
1590 p = hdr_buf.buf;
1591 pend = p + hdr_buf.left;
1592 while (p < pend && *p != '\0')
1594 p += strnlen ((const char *) p, pend - p) + 1;
1595 p += leb128_len (p);
1596 p += leb128_len (p);
1597 p += leb128_len (p);
1598 ++hdr->filenames_count;
1601 hdr->filenames = ((const char **)
1602 backtrace_alloc (state,
1603 hdr->filenames_count * sizeof (char *),
1604 line_buf->error_callback,
1605 line_buf->data));
1606 if (hdr->filenames == NULL)
1607 return 0;
1608 i = 0;
1609 while (*hdr_buf.buf != '\0')
1611 const char *filename;
1612 uint64_t dir_index;
1614 if (hdr_buf.reported_underflow)
1615 return 0;
1617 filename = (const char *) hdr_buf.buf;
1618 if (!advance (&hdr_buf,
1619 strnlen ((const char *) hdr_buf.buf, hdr_buf.left) + 1))
1620 return 0;
1621 dir_index = read_uleb128 (&hdr_buf);
1622 if (IS_ABSOLUTE_PATH (filename))
1623 hdr->filenames[i] = filename;
1624 else
1626 const char *dir;
1627 size_t dir_len;
1628 size_t filename_len;
1629 char *s;
1631 if (dir_index == 0)
1632 dir = u->comp_dir;
1633 else if (dir_index - 1 < hdr->dirs_count)
1634 dir = hdr->dirs[dir_index - 1];
1635 else
1637 dwarf_buf_error (line_buf,
1638 ("invalid directory index in "
1639 "line number program header"));
1640 return 0;
1642 dir_len = strlen (dir);
1643 filename_len = strlen (filename);
1644 s = ((char *)
1645 backtrace_alloc (state, dir_len + filename_len + 2,
1646 line_buf->error_callback, line_buf->data));
1647 if (s == NULL)
1648 return 0;
1649 memcpy (s, dir, dir_len);
1650 /* FIXME: If we are on a DOS-based file system, and the
1651 directory or the file name use backslashes, then we
1652 should use a backslash here. */
1653 s[dir_len] = '/';
1654 memcpy (s + dir_len + 1, filename, filename_len + 1);
1655 hdr->filenames[i] = s;
1658 /* Ignore the modification time and size. */
1659 read_uleb128 (&hdr_buf);
1660 read_uleb128 (&hdr_buf);
1662 ++i;
1665 if (hdr_buf.reported_underflow)
1666 return 0;
1668 return 1;
1671 /* Read the line program, adding line mappings to VEC. Return 1 on
1672 success, 0 on failure. */
1674 static int
1675 read_line_program (struct backtrace_state *state, struct unit *u,
1676 const struct line_header *hdr, struct dwarf_buf *line_buf,
1677 struct line_vector *vec)
1679 uint64_t address;
1680 unsigned int op_index;
1681 const char *reset_filename;
1682 const char *filename;
1683 int lineno;
1685 address = 0;
1686 op_index = 0;
1687 if (hdr->filenames_count > 0)
1688 reset_filename = hdr->filenames[0];
1689 else
1690 reset_filename = "";
1691 filename = reset_filename;
1692 lineno = 1;
1693 while (line_buf->left > 0)
1695 unsigned int op;
1697 op = read_byte (line_buf);
1698 if (op >= hdr->opcode_base)
1700 unsigned int advance;
1702 /* Special opcode. */
1703 op -= hdr->opcode_base;
1704 advance = op / hdr->line_range;
1705 address += (hdr->min_insn_len * (op_index + advance)
1706 / hdr->max_ops_per_insn);
1707 op_index = (op_index + advance) % hdr->max_ops_per_insn;
1708 lineno += hdr->line_base + (int) (op % hdr->line_range);
1709 add_line (state, address, filename, lineno, line_buf->error_callback,
1710 line_buf->data, vec);
1712 else if (op == DW_LNS_extended_op)
1714 uint64_t len;
1716 len = read_uleb128 (line_buf);
1717 op = read_byte (line_buf);
1718 switch (op)
1720 case DW_LNE_end_sequence:
1721 /* FIXME: Should we mark the high PC here? It seems
1722 that we already have that information from the
1723 compilation unit. */
1724 address = 0;
1725 op_index = 0;
1726 filename = reset_filename;
1727 lineno = 1;
1728 break;
1729 case DW_LNE_set_address:
1730 address = read_address (line_buf, u->addrsize);
1731 break;
1732 case DW_LNE_define_file:
1734 const char *f;
1735 unsigned int dir_index;
1737 f = (const char *) line_buf->buf;
1738 if (!advance (line_buf, strnlen (f, line_buf->left) + 1))
1739 return 0;
1740 dir_index = read_uleb128 (line_buf);
1741 /* Ignore that time and length. */
1742 read_uleb128 (line_buf);
1743 read_uleb128 (line_buf);
1744 if (IS_ABSOLUTE_PATH (f))
1745 filename = f;
1746 else
1748 const char *dir;
1749 size_t dir_len;
1750 size_t f_len;
1751 char *p;
1753 if (dir_index == 0)
1754 dir = u->comp_dir;
1755 else if (dir_index - 1 < hdr->dirs_count)
1756 dir = hdr->dirs[dir_index - 1];
1757 else
1759 dwarf_buf_error (line_buf,
1760 ("invalid directory index "
1761 "in line number program"));
1762 return 0;
1764 dir_len = strlen (dir);
1765 f_len = strlen (f);
1766 p = ((char *)
1767 backtrace_alloc (state, dir_len + f_len + 2,
1768 line_buf->error_callback,
1769 line_buf->data));
1770 if (p == NULL)
1771 return 0;
1772 memcpy (p, dir, dir_len);
1773 /* FIXME: If we are on a DOS-based file system,
1774 and the directory or the file name use
1775 backslashes, then we should use a backslash
1776 here. */
1777 p[dir_len] = '/';
1778 memcpy (p + dir_len + 1, f, f_len + 1);
1779 filename = p;
1782 break;
1783 case DW_LNE_set_discriminator:
1784 /* We don't care about discriminators. */
1785 read_uleb128 (line_buf);
1786 break;
1787 default:
1788 if (!advance (line_buf, len - 1))
1789 return 0;
1790 break;
1793 else
1795 switch (op)
1797 case DW_LNS_copy:
1798 add_line (state, address, filename, lineno,
1799 line_buf->error_callback, line_buf->data, vec);
1800 break;
1801 case DW_LNS_advance_pc:
1803 uint64_t advance;
1805 advance = read_uleb128 (line_buf);
1806 address += (hdr->min_insn_len * (op_index + advance)
1807 / hdr->max_ops_per_insn);
1808 op_index = (op_index + advance) % hdr->max_ops_per_insn;
1810 break;
1811 case DW_LNS_advance_line:
1812 lineno += (int) read_sleb128 (line_buf);
1813 break;
1814 case DW_LNS_set_file:
1816 uint64_t fileno;
1818 fileno = read_uleb128 (line_buf);
1819 if (fileno == 0)
1820 filename = "";
1821 else
1823 if (fileno - 1 >= hdr->filenames_count)
1825 dwarf_buf_error (line_buf,
1826 ("invalid file number in "
1827 "line number program"));
1828 return 0;
1830 filename = hdr->filenames[fileno - 1];
1833 break;
1834 case DW_LNS_set_column:
1835 read_uleb128 (line_buf);
1836 break;
1837 case DW_LNS_negate_stmt:
1838 break;
1839 case DW_LNS_set_basic_block:
1840 break;
1841 case DW_LNS_const_add_pc:
1843 unsigned int advance;
1845 op = 255 - hdr->opcode_base;
1846 advance = op / hdr->line_range;
1847 address += (hdr->min_insn_len * (op_index + advance)
1848 / hdr->max_ops_per_insn);
1849 op_index = (op_index + advance) % hdr->max_ops_per_insn;
1851 break;
1852 case DW_LNS_fixed_advance_pc:
1853 address += read_uint16 (line_buf);
1854 op_index = 0;
1855 break;
1856 case DW_LNS_set_prologue_end:
1857 break;
1858 case DW_LNS_set_epilogue_begin:
1859 break;
1860 case DW_LNS_set_isa:
1861 read_uleb128 (line_buf);
1862 break;
1863 default:
1865 unsigned int i;
1867 for (i = hdr->opcode_lengths[op - 1]; i > 0; --i)
1868 read_uleb128 (line_buf);
1870 break;
1875 return 1;
1878 /* Read the line number information for a compilation unit. Returns 1
1879 on success, 0 on failure. */
1881 static int
1882 read_line_info (struct backtrace_state *state, struct dwarf_data *ddata,
1883 backtrace_error_callback error_callback, void *data,
1884 struct unit *u, struct line_header *hdr, struct line **lines,
1885 size_t *lines_count)
1887 struct line_vector vec;
1888 struct dwarf_buf line_buf;
1889 uint64_t len;
1890 int is_dwarf64;
1891 struct line *ln;
1893 memset (&vec.vec, 0, sizeof vec.vec);
1894 vec.count = 0;
1896 memset (hdr, 0, sizeof *hdr);
1898 if (u->lineoff != (off_t) (size_t) u->lineoff
1899 || (size_t) u->lineoff >= ddata->dwarf_line_size)
1901 error_callback (data, "unit line offset out of range", 0);
1902 goto fail;
1905 line_buf.name = ".debug_line";
1906 line_buf.start = ddata->dwarf_line;
1907 line_buf.buf = ddata->dwarf_line + u->lineoff;
1908 line_buf.left = ddata->dwarf_line_size - u->lineoff;
1909 line_buf.is_bigendian = ddata->is_bigendian;
1910 line_buf.error_callback = error_callback;
1911 line_buf.data = data;
1912 line_buf.reported_underflow = 0;
1914 is_dwarf64 = 0;
1915 len = read_uint32 (&line_buf);
1916 if (len == 0xffffffff)
1918 len = read_uint64 (&line_buf);
1919 is_dwarf64 = 1;
1921 line_buf.left = len;
1923 if (!read_line_header (state, u, is_dwarf64, &line_buf, hdr))
1924 goto fail;
1926 if (!read_line_program (state, u, hdr, &line_buf, &vec))
1927 goto fail;
1929 if (line_buf.reported_underflow)
1930 goto fail;
1932 if (vec.count == 0)
1934 /* This is not a failure in the sense of a generating an error,
1935 but it is a failure in that sense that we have no useful
1936 information. */
1937 goto fail;
1940 /* Allocate one extra entry at the end. */
1941 ln = ((struct line *)
1942 backtrace_vector_grow (state, sizeof (struct line), error_callback,
1943 data, &vec.vec));
1944 if (ln == NULL)
1945 goto fail;
1946 ln->pc = (uintptr_t) -1;
1947 ln->filename = NULL;
1948 ln->lineno = 0;
1950 if (!backtrace_vector_release (state, &vec.vec, error_callback, data))
1951 goto fail;
1953 ln = (struct line *) vec.vec.base;
1954 qsort (ln, vec.count, sizeof (struct line), line_compare);
1956 *lines = ln;
1957 *lines_count = vec.count;
1959 return 1;
1961 fail:
1962 vec.vec.alc += vec.vec.size;
1963 vec.vec.size = 0;
1964 backtrace_vector_release (state, &vec.vec, error_callback, data);
1965 free_line_header (state, hdr, error_callback, data);
1966 *lines = (struct line *) (uintptr_t) -1;
1967 *lines_count = 0;
1968 return 0;
1971 /* Read the name of a function from a DIE referenced by a
1972 DW_AT_abstract_origin or DW_AT_specification tag. OFFSET is within
1973 the same compilation unit. */
1975 static const char *
1976 read_referenced_name (struct dwarf_data *ddata, struct unit *u,
1977 uint64_t offset, backtrace_error_callback error_callback,
1978 void *data)
1980 struct dwarf_buf unit_buf;
1981 uint64_t code;
1982 const struct abbrev *abbrev;
1983 const char *ret;
1984 size_t i;
1986 /* OFFSET is from the start of the data for this compilation unit.
1987 U->unit_data is the data, but it starts U->unit_data_offset bytes
1988 from the beginning. */
1990 if (offset < u->unit_data_offset
1991 || offset - u->unit_data_offset >= u->unit_data_len)
1993 error_callback (data,
1994 "abstract origin or specification out of range",
1996 return NULL;
1999 offset -= u->unit_data_offset;
2001 unit_buf.name = ".debug_info";
2002 unit_buf.start = ddata->dwarf_info;
2003 unit_buf.buf = u->unit_data + offset;
2004 unit_buf.left = u->unit_data_len - offset;
2005 unit_buf.is_bigendian = ddata->is_bigendian;
2006 unit_buf.error_callback = error_callback;
2007 unit_buf.data = data;
2008 unit_buf.reported_underflow = 0;
2010 code = read_uleb128 (&unit_buf);
2011 if (code == 0)
2013 dwarf_buf_error (&unit_buf, "invalid abstract origin or specification");
2014 return NULL;
2017 abbrev = lookup_abbrev (&u->abbrevs, code, error_callback, data);
2018 if (abbrev == NULL)
2019 return NULL;
2021 ret = NULL;
2022 for (i = 0; i < abbrev->num_attrs; ++i)
2024 struct attr_val val;
2026 if (!read_attribute (abbrev->attrs[i].form, &unit_buf,
2027 u->is_dwarf64, u->version, u->addrsize,
2028 ddata->dwarf_str, ddata->dwarf_str_size,
2029 &val))
2030 return NULL;
2032 switch (abbrev->attrs[i].name)
2034 case DW_AT_name:
2035 /* We prefer the linkage name if get one. */
2036 if (val.encoding == ATTR_VAL_STRING)
2037 ret = val.u.string;
2038 break;
2040 case DW_AT_linkage_name:
2041 case DW_AT_MIPS_linkage_name:
2042 if (val.encoding == ATTR_VAL_STRING)
2043 return val.u.string;
2044 break;
2046 case DW_AT_specification:
2047 if (abbrev->attrs[i].form == DW_FORM_ref_addr
2048 || abbrev->attrs[i].form == DW_FORM_ref_sig8)
2050 /* This refers to a specification defined in some other
2051 compilation unit. We can handle this case if we
2052 must, but it's harder. */
2053 break;
2055 if (val.encoding == ATTR_VAL_UINT
2056 || val.encoding == ATTR_VAL_REF_UNIT)
2058 const char *name;
2060 name = read_referenced_name (ddata, u, val.u.uint,
2061 error_callback, data);
2062 if (name != NULL)
2063 ret = name;
2065 break;
2067 default:
2068 break;
2072 return ret;
2075 /* Add a single range to U that maps to function. Returns 1 on
2076 success, 0 on error. */
2078 static int
2079 add_function_range (struct backtrace_state *state, struct function *function,
2080 uint64_t lowpc, uint64_t highpc,
2081 backtrace_error_callback error_callback,
2082 void *data, struct function_vector *vec)
2084 struct function_addrs *p;
2086 if (vec->count > 0)
2088 p = (struct function_addrs *) vec->vec.base + vec->count - 1;
2089 if ((lowpc == p->high || lowpc == p->high + 1)
2090 && function == p->function)
2092 if (highpc > p->high)
2093 p->high = highpc;
2094 return 1;
2098 p = ((struct function_addrs *)
2099 backtrace_vector_grow (state, sizeof (struct function_addrs),
2100 error_callback, data, &vec->vec));
2101 if (p == NULL)
2102 return 0;
2104 p->low = lowpc;
2105 p->high = highpc;
2106 p->function = function;
2107 ++vec->count;
2108 return 1;
2111 /* Add PC ranges to U that map to FUNCTION. Returns 1 on success, 0
2112 on error. */
2114 static int
2115 add_function_ranges (struct backtrace_state *state, struct dwarf_data *ddata,
2116 struct unit *u, struct function *function,
2117 uint64_t ranges, uint64_t base,
2118 backtrace_error_callback error_callback, void *data,
2119 struct function_vector *vec)
2121 struct dwarf_buf ranges_buf;
2123 if (ranges >= ddata->dwarf_ranges_size)
2125 error_callback (data, "function ranges offset out of range", 0);
2126 return 0;
2129 ranges_buf.name = ".debug_ranges";
2130 ranges_buf.start = ddata->dwarf_ranges;
2131 ranges_buf.buf = ddata->dwarf_ranges + ranges;
2132 ranges_buf.left = ddata->dwarf_ranges_size - ranges;
2133 ranges_buf.is_bigendian = ddata->is_bigendian;
2134 ranges_buf.error_callback = error_callback;
2135 ranges_buf.data = data;
2136 ranges_buf.reported_underflow = 0;
2138 while (1)
2140 uint64_t low;
2141 uint64_t high;
2143 if (ranges_buf.reported_underflow)
2144 return 0;
2146 low = read_address (&ranges_buf, u->addrsize);
2147 high = read_address (&ranges_buf, u->addrsize);
2149 if (low == 0 && high == 0)
2150 break;
2152 if (is_highest_address (low, u->addrsize))
2153 base = high;
2154 else
2156 if (!add_function_range (state, function, low + base, high + base,
2157 error_callback, data, vec))
2158 return 0;
2162 if (ranges_buf.reported_underflow)
2163 return 0;
2165 return 1;
2168 /* Read one entry plus all its children. Add function addresses to
2169 VEC. Returns 1 on success, 0 on error. */
2171 static int
2172 read_function_entry (struct backtrace_state *state, struct dwarf_data *ddata,
2173 struct unit *u, uint64_t base, struct dwarf_buf *unit_buf,
2174 const struct line_header *lhdr,
2175 backtrace_error_callback error_callback, void *data,
2176 struct function_vector *vec)
2178 while (unit_buf->left > 0)
2180 uint64_t code;
2181 const struct abbrev *abbrev;
2182 int is_function;
2183 struct function *function;
2184 size_t i;
2185 uint64_t lowpc;
2186 int have_lowpc;
2187 uint64_t highpc;
2188 int have_highpc;
2189 int highpc_is_relative;
2190 uint64_t ranges;
2191 int have_ranges;
2193 code = read_uleb128 (unit_buf);
2194 if (code == 0)
2195 return 1;
2197 abbrev = lookup_abbrev (&u->abbrevs, code, error_callback, data);
2198 if (abbrev == NULL)
2199 return 0;
2201 is_function = (abbrev->tag == DW_TAG_subprogram
2202 || abbrev->tag == DW_TAG_entry_point
2203 || abbrev->tag == DW_TAG_inlined_subroutine);
2205 function = NULL;
2206 if (is_function)
2208 function = ((struct function *)
2209 backtrace_alloc (state, sizeof *function,
2210 error_callback, data));
2211 if (function == NULL)
2212 return 0;
2213 memset (function, 0, sizeof *function);
2216 lowpc = 0;
2217 have_lowpc = 0;
2218 highpc = 0;
2219 have_highpc = 0;
2220 highpc_is_relative = 0;
2221 ranges = 0;
2222 have_ranges = 0;
2223 for (i = 0; i < abbrev->num_attrs; ++i)
2225 struct attr_val val;
2227 if (!read_attribute (abbrev->attrs[i].form, unit_buf,
2228 u->is_dwarf64, u->version, u->addrsize,
2229 ddata->dwarf_str, ddata->dwarf_str_size,
2230 &val))
2231 return 0;
2233 /* The compile unit sets the base address for any address
2234 ranges in the function entries. */
2235 if (abbrev->tag == DW_TAG_compile_unit
2236 && abbrev->attrs[i].name == DW_AT_low_pc
2237 && val.encoding == ATTR_VAL_ADDRESS)
2238 base = val.u.uint;
2240 if (is_function)
2242 switch (abbrev->attrs[i].name)
2244 case DW_AT_call_file:
2245 if (val.encoding == ATTR_VAL_UINT)
2247 if (val.u.uint == 0)
2248 function->caller_filename = "";
2249 else
2251 if (val.u.uint - 1 >= lhdr->filenames_count)
2253 dwarf_buf_error (unit_buf,
2254 ("invalid file number in "
2255 "DW_AT_call_file attribute"));
2256 return 0;
2258 function->caller_filename =
2259 lhdr->filenames[val.u.uint - 1];
2262 break;
2264 case DW_AT_call_line:
2265 if (val.encoding == ATTR_VAL_UINT)
2266 function->caller_lineno = val.u.uint;
2267 break;
2269 case DW_AT_abstract_origin:
2270 case DW_AT_specification:
2271 if (abbrev->attrs[i].form == DW_FORM_ref_addr
2272 || abbrev->attrs[i].form == DW_FORM_ref_sig8)
2274 /* This refers to an abstract origin defined in
2275 some other compilation unit. We can handle
2276 this case if we must, but it's harder. */
2277 break;
2279 if (val.encoding == ATTR_VAL_UINT
2280 || val.encoding == ATTR_VAL_REF_UNIT)
2282 const char *name;
2284 name = read_referenced_name (ddata, u, val.u.uint,
2285 error_callback, data);
2286 if (name != NULL)
2287 function->name = name;
2289 break;
2291 case DW_AT_name:
2292 if (val.encoding == ATTR_VAL_STRING)
2294 /* Don't override a name we found in some other
2295 way, as it will normally be more
2296 useful--e.g., this name is normally not
2297 mangled. */
2298 if (function->name == NULL)
2299 function->name = val.u.string;
2301 break;
2303 case DW_AT_linkage_name:
2304 case DW_AT_MIPS_linkage_name:
2305 if (val.encoding == ATTR_VAL_STRING)
2306 function->name = val.u.string;
2307 break;
2309 case DW_AT_low_pc:
2310 if (val.encoding == ATTR_VAL_ADDRESS)
2312 lowpc = val.u.uint;
2313 have_lowpc = 1;
2315 break;
2317 case DW_AT_high_pc:
2318 if (val.encoding == ATTR_VAL_ADDRESS)
2320 highpc = val.u.uint;
2321 have_highpc = 1;
2323 else if (val.encoding == ATTR_VAL_UINT)
2325 highpc = val.u.uint;
2326 have_highpc = 1;
2327 highpc_is_relative = 1;
2329 break;
2331 case DW_AT_ranges:
2332 if (val.encoding == ATTR_VAL_UINT
2333 || val.encoding == ATTR_VAL_REF_SECTION)
2335 ranges = val.u.uint;
2336 have_ranges = 1;
2338 break;
2340 default:
2341 break;
2346 /* If we couldn't find a name for the function, we have no use
2347 for it. */
2348 if (is_function && function->name == NULL)
2350 backtrace_free (state, function, sizeof *function,
2351 error_callback, data);
2352 is_function = 0;
2355 if (is_function)
2357 if (have_ranges)
2359 if (!add_function_ranges (state, ddata, u, function, ranges,
2360 base, error_callback, data, vec))
2361 return 0;
2363 else if (have_lowpc && have_highpc)
2365 if (highpc_is_relative)
2366 highpc += lowpc;
2367 if (!add_function_range (state, function, lowpc, highpc,
2368 error_callback, data, vec))
2369 return 0;
2371 else
2373 backtrace_free (state, function, sizeof *function,
2374 error_callback, data);
2375 is_function = 0;
2379 if (abbrev->has_children)
2381 if (!is_function)
2383 if (!read_function_entry (state, ddata, u, base, unit_buf, lhdr,
2384 error_callback, data, vec))
2385 return 0;
2387 else
2389 struct function_vector fvec;
2391 /* Gather any information for inlined functions in
2392 FVEC. */
2394 memset (&fvec, 0, sizeof fvec);
2396 if (!read_function_entry (state, ddata, u, base, unit_buf, lhdr,
2397 error_callback, data, &fvec))
2398 return 0;
2400 if (fvec.count > 0)
2402 struct function_addrs *faddrs;
2404 if (!backtrace_vector_release (state, &fvec.vec,
2405 error_callback, data))
2406 return 0;
2408 faddrs = (struct function_addrs *) fvec.vec.base;
2409 qsort (faddrs, fvec.count,
2410 sizeof (struct function_addrs),
2411 function_addrs_compare);
2413 function->function_addrs = faddrs;
2414 function->function_addrs_count = fvec.count;
2420 return 1;
2423 /* Read function name information for a compilation unit. We look
2424 through the whole unit looking for function tags. */
2426 static void
2427 read_function_info (struct backtrace_state *state, struct dwarf_data *ddata,
2428 const struct line_header *lhdr,
2429 backtrace_error_callback error_callback, void *data,
2430 struct unit *u, struct function_vector *fvec,
2431 struct function_addrs **ret_addrs,
2432 size_t *ret_addrs_count)
2434 struct dwarf_buf unit_buf;
2435 struct function_addrs *addrs;
2436 size_t addrs_count;
2438 unit_buf.name = ".debug_info";
2439 unit_buf.start = ddata->dwarf_info;
2440 unit_buf.buf = u->unit_data;
2441 unit_buf.left = u->unit_data_len;
2442 unit_buf.is_bigendian = ddata->is_bigendian;
2443 unit_buf.error_callback = error_callback;
2444 unit_buf.data = data;
2445 unit_buf.reported_underflow = 0;
2447 while (unit_buf.left > 0)
2449 if (!read_function_entry (state, ddata, u, 0, &unit_buf, lhdr,
2450 error_callback, data, fvec))
2451 return;
2454 if (fvec->count == 0)
2455 return;
2457 addrs = (struct function_addrs *) fvec->vec.base;
2458 addrs_count = fvec->count;
2460 /* Finish this list of addresses, but leave the remaining space in
2461 the vector available for the next function unit. */
2462 backtrace_vector_finish (state, &fvec->vec);
2463 fvec->count = 0;
2465 qsort (addrs, addrs_count, sizeof (struct function_addrs),
2466 function_addrs_compare);
2468 *ret_addrs = addrs;
2469 *ret_addrs_count = addrs_count;
2472 /* See if PC is inlined in FUNCTION. If it is, print out the inlined
2473 information, and update FILENAME and LINENO for the caller.
2474 Returns whatever CALLBACK returns, or 0 to keep going. */
2476 static int
2477 report_inlined_functions (uintptr_t pc, struct function *function,
2478 backtrace_full_callback callback, void *data,
2479 const char **filename, int *lineno)
2481 struct function_addrs *function_addrs;
2482 struct function *inlined;
2483 int ret;
2485 if (function->function_addrs_count == 0)
2486 return 0;
2488 function_addrs = ((struct function_addrs *)
2489 bsearch (&pc, function->function_addrs,
2490 function->function_addrs_count,
2491 sizeof (struct function_addrs),
2492 function_addrs_search));
2493 if (function_addrs == NULL)
2494 return 0;
2496 while (((size_t) (function_addrs - function->function_addrs) + 1
2497 < function->function_addrs_count)
2498 && pc >= (function_addrs + 1)->low
2499 && pc < (function_addrs + 1)->high)
2500 ++function_addrs;
2502 /* We found an inlined call. */
2504 inlined = function_addrs->function;
2506 /* Report any calls inlined into this one. */
2507 ret = report_inlined_functions (pc, inlined, callback, data,
2508 filename, lineno);
2509 if (ret != 0)
2510 return ret;
2512 /* Report this inlined call. */
2513 ret = callback (data, pc, *filename, *lineno, inlined->name);
2514 if (ret != 0)
2515 return ret;
2517 /* Our caller will report the caller of the inlined function; tell
2518 it the appropriate filename and line number. */
2519 *filename = inlined->caller_filename;
2520 *lineno = inlined->caller_lineno;
2522 return 0;
2525 /* Return the file/line information for a PC using the DWARF mapping
2526 we built earlier. */
2528 static int
2529 dwarf_fileline (struct backtrace_state *state, uintptr_t pc,
2530 backtrace_full_callback callback,
2531 backtrace_error_callback error_callback, void *data)
2533 struct dwarf_data *ddata;
2534 struct unit_addrs *entry;
2535 struct unit *u;
2536 int new_data;
2537 struct line *lines;
2538 struct line *ln;
2539 struct function_addrs *function_addrs;
2540 struct function *function;
2541 const char *filename;
2542 int lineno;
2543 int ret;
2545 ddata = (struct dwarf_data *) state->fileline_data;
2547 /* Find an address range that includes PC. */
2548 entry = bsearch (&pc, ddata->addrs, ddata->addrs_count,
2549 sizeof (struct unit_addrs), unit_addrs_search);
2551 if (entry == NULL)
2552 return callback (data, pc, NULL, 0, NULL);
2554 /* If there are multiple ranges that contain PC, use the last one,
2555 in order to produce predictable results. If we assume that all
2556 ranges are properly nested, then the last range will be the
2557 smallest one. */
2558 while ((size_t) (entry - ddata->addrs) + 1 < ddata->addrs_count
2559 && pc >= (entry + 1)->low
2560 && pc < (entry + 1)->high)
2561 ++entry;
2563 /* We need the lines, lines_count, function_addrs,
2564 function_addrs_count fields of u. If they are not set, we need
2565 to set them. When running in threaded mode, we need to allow for
2566 the possibility that some other thread is setting them
2567 simultaneously. */
2569 u = entry->u;
2570 lines = u->lines;
2572 /* Skip units with no useful line number information by walking
2573 backward. Useless line number information is marked by setting
2574 lines == -1. */
2575 while (entry > ddata->addrs
2576 && pc >= (entry - 1)->low
2577 && pc < (entry - 1)->high)
2579 if (state->threaded)
2581 /* Use __sync_bool_compare_and_swap to do a
2582 load-acquire. */
2583 while (!__sync_bool_compare_and_swap (&u->lines, lines, lines))
2584 lines = u->lines;
2587 if (lines != (struct line *) (uintptr_t) -1)
2588 break;
2590 --entry;
2592 u = entry->u;
2593 lines = u->lines;
2596 /* Do a load-acquire of u->lines. */
2597 if (state->threaded)
2599 /* Use __sync_bool_compare_and_swap to do an atomic load. */
2600 while (!__sync_bool_compare_and_swap (&u->lines, lines, lines))
2601 lines = u->lines;
2604 new_data = 0;
2605 if (lines == NULL)
2607 size_t function_addrs_count;
2608 struct line_header lhdr;
2609 size_t count;
2611 /* We have never read the line information for this unit. Read
2612 it now. */
2614 function_addrs = NULL;
2615 function_addrs_count = 0;
2616 if (read_line_info (state, ddata, error_callback, data, entry->u, &lhdr,
2617 &lines, &count))
2619 read_function_info (state, ddata, &lhdr, error_callback, data,
2620 entry->u, &ddata->fvec, &function_addrs,
2621 &function_addrs_count);
2622 free_line_header (state, &lhdr, error_callback, data);
2623 new_data = 1;
2626 /* Atomically store the information we just read into the unit.
2627 If another thread is simultaneously writing, it presumably
2628 read the same information, and we don't care which one we
2629 wind up with; we just leak the other one. We do have to
2630 write the lines field last, so that the acquire-loads above
2631 ensure that the other fields are set. */
2633 if (!state->threaded)
2635 u->lines_count = count;
2636 u->function_addrs = function_addrs;
2637 u->function_addrs_count = function_addrs_count;
2638 u->lines = lines;
2640 else
2642 __sync_bool_compare_and_swap (&u->lines_count, 0, count);
2643 __sync_bool_compare_and_swap (&u->function_addrs, NULL,
2644 function_addrs);
2645 __sync_bool_compare_and_swap (&u->function_addrs_count, 0,
2646 function_addrs_count);
2647 __sync_bool_compare_and_swap (&u->lines, NULL, lines);
2651 /* Now all fields of U have been initialized. */
2653 if (lines == (struct line *) (uintptr_t) -1)
2655 /* If reading the line number information failed in some way,
2656 try again to see if there is a better compilation unit for
2657 this PC. */
2658 if (new_data)
2659 dwarf_fileline (state, pc, callback, error_callback, data);
2660 return callback (data, pc, NULL, 0, NULL);
2663 /* Search for PC within this unit. */
2665 ln = (struct line *) bsearch (&pc, lines, entry->u->lines_count,
2666 sizeof (struct line), line_search);
2667 if (ln == NULL)
2669 error_callback (data, "inconsistent DWARF line number info", 0);
2670 return 0;
2673 /* Search for function name within this unit. */
2675 if (entry->u->function_addrs_count == 0)
2676 return callback (data, pc, ln->filename, ln->lineno, NULL);
2678 function_addrs = ((struct function_addrs *)
2679 bsearch (&pc, entry->u->function_addrs,
2680 entry->u->function_addrs_count,
2681 sizeof (struct function_addrs),
2682 function_addrs_search));
2683 if (function_addrs == NULL)
2684 return callback (data, pc, ln->filename, ln->lineno, NULL);
2686 /* If there are multiple function ranges that contain PC, use the
2687 last one, in order to produce predictable results. */
2689 while (((size_t) (function_addrs - entry->u->function_addrs + 1)
2690 < entry->u->function_addrs_count)
2691 && pc >= (function_addrs + 1)->low
2692 && pc < (function_addrs + 1)->high)
2693 ++function_addrs;
2695 function = function_addrs->function;
2697 filename = ln->filename;
2698 lineno = ln->lineno;
2700 ret = report_inlined_functions (pc, function, callback, data,
2701 &filename, &lineno);
2702 if (ret != 0)
2703 return ret;
2705 return callback (data, pc, filename, lineno, function->name);
2708 /* Build our data structures from the .debug_info and .debug_line
2709 sections. Set *FILELINE_FN and *FILELINE_DATA. Return 1 on
2710 success, 0 on failure. */
2713 backtrace_dwarf_initialize (struct backtrace_state *state,
2714 const unsigned char *dwarf_info,
2715 size_t dwarf_info_size,
2716 const unsigned char *dwarf_line,
2717 size_t dwarf_line_size,
2718 const unsigned char *dwarf_abbrev,
2719 size_t dwarf_abbrev_size,
2720 const unsigned char *dwarf_ranges,
2721 size_t dwarf_ranges_size,
2722 const unsigned char *dwarf_str,
2723 size_t dwarf_str_size,
2724 int is_bigendian,
2725 backtrace_error_callback error_callback,
2726 void *data, fileline *fileline_fn)
2728 struct unit_addrs_vector addrs_vec;
2729 struct unit_addrs *addrs;
2730 size_t addrs_count;
2731 struct dwarf_data *fdata;
2733 if (!build_address_map (state, dwarf_info, dwarf_info_size, dwarf_abbrev,
2734 dwarf_abbrev_size, dwarf_ranges, dwarf_ranges_size,
2735 dwarf_str, dwarf_str_size, is_bigendian,
2736 error_callback, data, &addrs_vec))
2737 return 0;
2739 if (!backtrace_vector_release (state, &addrs_vec.vec, error_callback, data))
2740 return 0;
2741 addrs = (struct unit_addrs *) addrs_vec.vec.base;
2742 addrs_count = addrs_vec.count;
2743 qsort (addrs, addrs_count, sizeof (struct unit_addrs), unit_addrs_compare);
2745 fdata = ((struct dwarf_data *)
2746 backtrace_alloc (state, sizeof (struct dwarf_data),
2747 error_callback, data));
2748 if (fdata == NULL)
2749 return 0;
2751 fdata->addrs = addrs;
2752 fdata->addrs_count = addrs_count;
2753 fdata->dwarf_info = dwarf_info;
2754 fdata->dwarf_info_size = dwarf_info_size;
2755 fdata->dwarf_line = dwarf_line;
2756 fdata->dwarf_line_size = dwarf_line_size;
2757 fdata->dwarf_ranges = dwarf_ranges;
2758 fdata->dwarf_ranges_size = dwarf_ranges_size;
2759 fdata->dwarf_str = dwarf_str;
2760 fdata->dwarf_str_size = dwarf_str_size;
2761 fdata->is_bigendian = is_bigendian;
2762 memset (&fdata->fvec, 0, sizeof fdata->fvec);
2764 state->fileline_data = fdata;
2766 *fileline_fn = dwarf_fileline;
2768 return 1;