2012-09-27 Vladimir Makarov <vmakarov@redhat.com>
[official-gcc.git] / libbacktrace / dwarf.c
blob68ebb4e3ad16d40b7c69684c54a801c756e0b070
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
47 /* The function is defined in libiberty if needed. */
48 extern size_t strnlen (const char *, size_t);
49 #endif
51 /* A buffer to read DWARF info. */
53 struct dwarf_buf
55 /* Buffer name for error messages. */
56 const char *name;
57 /* Start of the buffer. */
58 const unsigned char *start;
59 /* Next byte to read. */
60 const unsigned char *buf;
61 /* The number of bytes remaining. */
62 size_t left;
63 /* Whether the data is big-endian. */
64 int is_bigendian;
65 /* Error callback routine. */
66 backtrace_error_callback error_callback;
67 /* Data for error_callback. */
68 void *data;
69 /* Non-zero if we've reported an underflow error. */
70 int reported_underflow;
73 /* A single attribute in a DWARF abbreviation. */
75 struct attr
77 /* The attribute name. */
78 enum dwarf_attribute name;
79 /* The attribute form. */
80 enum dwarf_form form;
83 /* A single DWARF abbreviation. */
85 struct abbrev
87 /* The abbrev code--the number used to refer to the abbrev. */
88 uint64_t code;
89 /* The entry tag. */
90 enum dwarf_tag tag;
91 /* Non-zero if this abbrev has child entries. */
92 int has_children;
93 /* The number of attributes. */
94 size_t num_attrs;
95 /* The attributes. */
96 struct attr *attrs;
99 /* The DWARF abbreviations for a compilation unit. This structure
100 only exists while reading the compilation unit. Most DWARF readers
101 seem to a hash table to map abbrev ID's to abbrev entries.
102 However, we primarily care about GCC, and GCC simply issues ID's in
103 numerical order starting at 1. So we simply keep a sorted vector,
104 and try to just look up the code. */
106 struct abbrevs
108 /* The number of abbrevs in the vector. */
109 size_t num_abbrevs;
110 /* The abbrevs, sorted by the code field. */
111 struct abbrev *abbrevs;
114 /* The different kinds of attribute values. */
116 enum attr_val_encoding
118 /* An address. */
119 ATTR_VAL_ADDRESS,
120 /* A unsigned integer. */
121 ATTR_VAL_UINT,
122 /* A sigd integer. */
123 ATTR_VAL_SINT,
124 /* A string. */
125 ATTR_VAL_STRING,
126 /* An offset to other data in the containing unit. */
127 ATTR_VAL_REF_UNIT,
128 /* An offset to other data within the .dwarf_info section. */
129 ATTR_VAL_REF_INFO,
130 /* An offset to data in some other section. */
131 ATTR_VAL_REF_SECTION,
132 /* A type signature. */
133 ATTR_VAL_REF_TYPE,
134 /* A block of data (not represented). */
135 ATTR_VAL_BLOCK,
136 /* An expression (not represented). */
137 ATTR_VAL_EXPR,
140 /* An attribute value. */
142 struct attr_val
144 /* How the value is stored in the field u. */
145 enum attr_val_encoding encoding;
146 union
148 /* ATTR_VAL_ADDRESS, ATTR_VAL_UINT, ATTR_VAL_REF*. */
149 uint64_t uint;
150 /* ATTR_VAL_SINT. */
151 int64_t sint;
152 /* ATTR_VAL_STRING. */
153 const char *string;
154 /* ATTR_VAL_BLOCK not stored. */
155 } u;
158 /* The line number program header. */
160 struct line_header
162 /* The version of the line number information. */
163 int version;
164 /* The minimum instruction length. */
165 unsigned int min_insn_len;
166 /* The maximum number of ops per instruction. */
167 unsigned int max_ops_per_insn;
168 /* The line base for special opcodes. */
169 int line_base;
170 /* The line range for special opcodes. */
171 unsigned int line_range;
172 /* The opcode base--the first special opcode. */
173 unsigned int opcode_base;
174 /* Opcode lengths, indexed by opcode - 1. */
175 const unsigned char *opcode_lengths;
176 /* The number of directory entries. */
177 size_t dirs_count;
178 /* The directory entries. */
179 const char **dirs;
180 /* The number of filenames. */
181 size_t filenames_count;
182 /* The filenames. */
183 const char **filenames;
186 /* Map a single PC value to a file/line. We will keep a vector of
187 these sorted by PC value. Each file/line will be correct from the
188 PC up to the PC of the next entry if there is one. We allocate one
189 extra entry at the end so that we can use bsearch. */
191 struct line
193 /* PC. */
194 uintptr_t pc;
195 /* File name. Many entries in the array are expected to point to
196 the same file name. */
197 const char *filename;
198 /* Line number. */
199 int lineno;
202 /* A growable vector of line number information. This is used while
203 reading the line numbers. */
205 struct line_vector
207 /* Memory. This is an array of struct line. */
208 struct backtrace_vector vec;
209 /* Number of valid mappings. */
210 size_t count;
213 /* A function described in the debug info. */
215 struct function
217 /* The name of the function. */
218 const char *name;
219 /* If this is an inlined function, the filename of the call
220 site. */
221 const char *caller_filename;
222 /* If this is an inlined function, the line number of the call
223 site. */
224 int caller_lineno;
225 /* Map PC ranges to inlined functions. */
226 struct function_addrs *function_addrs;
227 size_t function_addrs_count;
230 /* An address range for a function. This maps a PC value to a
231 specific function. */
233 struct function_addrs
235 /* Range is LOW <= PC < HIGH. */
236 uint64_t low;
237 uint64_t high;
238 /* Function for this address range. */
239 struct function *function;
242 /* A growable vector of function address ranges. */
244 struct function_vector
246 /* Memory. This is an array of struct function_addrs. */
247 struct backtrace_vector vec;
248 /* Number of address ranges present. */
249 size_t count;
252 /* A DWARF compilation unit. This only holds the information we need
253 to map a PC to a file and line. */
255 struct unit
257 /* The first entry for this compilation unit. */
258 const unsigned char *unit_data;
259 /* The length of the data for this compilation unit. */
260 size_t unit_data_len;
261 /* The offset of UNIT_DATA from the start of the information for
262 this compilation unit. */
263 size_t unit_data_offset;
264 /* DWARF version. */
265 int version;
266 /* Whether unit is DWARF64. */
267 int is_dwarf64;
268 /* Address size. */
269 int addrsize;
270 /* Offset into line number information. */
271 off_t lineoff;
272 /* Compilation command working directory. */
273 const char *comp_dir;
274 /* The abbreviations for this unit. */
275 struct abbrevs abbrevs;
277 /* The fields above this point are read in during initialization and
278 may be accessed freely. The fields below this point are read in
279 as needed, and therefore require care, as different threads may
280 try to initialize them simultaneously. */
282 /* PC to line number mapping. This is NULL if the values have not
283 been read. This is (struct line *) -1 if there was an error
284 reading the values. */
285 struct line *lines;
286 /* Number of entries in lines. */
287 size_t lines_count;
288 /* PC ranges to function. */
289 struct function_addrs *function_addrs;
290 size_t function_addrs_count;
293 /* An address range for a compilation unit. This maps a PC value to a
294 specific compilation unit. Note that we invert the representation
295 in DWARF: instead of listing the units and attaching a list of
296 ranges, we list the ranges and have each one point to the unit.
297 This lets us do a binary search to find the unit. */
299 struct unit_addrs
301 /* Range is LOW <= PC < HIGH. */
302 uint64_t low;
303 uint64_t high;
304 /* Compilation unit for this address range. */
305 struct unit *u;
308 /* A growable vector of compilation unit address ranges. */
310 struct unit_addrs_vector
312 /* Memory. This is an array of struct unit_addrs. */
313 struct backtrace_vector vec;
314 /* Number of address ranges present. */
315 size_t count;
318 /* The information we need to map a PC to a file and line. */
320 struct dwarf_data
322 /* A sorted list of address ranges. */
323 struct unit_addrs *addrs;
324 /* Number of address ranges in list. */
325 size_t addrs_count;
326 /* The unparsed .debug_info section. */
327 const unsigned char *dwarf_info;
328 size_t dwarf_info_size;
329 /* The unparsed .debug_line section. */
330 const unsigned char *dwarf_line;
331 size_t dwarf_line_size;
332 /* The unparsed .debug_ranges section. */
333 const unsigned char *dwarf_ranges;
334 size_t dwarf_ranges_size;
335 /* The unparsed .debug_str section. */
336 const unsigned char *dwarf_str;
337 size_t dwarf_str_size;
338 /* Whether the data is big-endian or not. */
339 int is_bigendian;
340 /* A vector used for function addresses. We keep this here so that
341 we can grow the vector as we read more functions. */
342 struct function_vector fvec;
345 /* Report an error for a DWARF buffer. */
347 static void
348 dwarf_buf_error (struct dwarf_buf *buf, const char *msg)
350 char b[200];
352 snprintf (b, sizeof b, "%s in %s at %d",
353 msg, buf->name, (int) (buf->buf - buf->start));
354 buf->error_callback (buf->data, b, 0);
357 /* Require at least COUNT bytes in BUF. Return 1 if all is well, 0 on
358 error. */
360 static int
361 require (struct dwarf_buf *buf, size_t count)
363 if (buf->left >= count)
364 return 1;
366 if (!buf->reported_underflow)
368 dwarf_buf_error (buf, "DWARF underflow");
369 buf->reported_underflow = 1;
372 return 0;
375 /* Advance COUNT bytes in BUF. Return 1 if all is well, 0 on
376 error. */
378 static int
379 advance (struct dwarf_buf *buf, size_t count)
381 if (!require (buf, count))
382 return 0;
383 buf->buf += count;
384 buf->left -= count;
385 return 1;
388 /* Read one byte from BUF and advance 1 byte. */
390 static unsigned char
391 read_byte (struct dwarf_buf *buf)
393 const unsigned char *p = buf->buf;
395 if (!advance (buf, 1))
396 return 0;
397 return p[0];
400 /* Read a signed char from BUF and advance 1 byte. */
402 static signed char
403 read_sbyte (struct dwarf_buf *buf)
405 const unsigned char *p = buf->buf;
407 if (!advance (buf, 1))
408 return 0;
409 return (*p ^ 0x80) - 0x80;
412 /* Read a uint16 from BUF and advance 2 bytes. */
414 static uint16_t
415 read_uint16 (struct dwarf_buf *buf)
417 const unsigned char *p = buf->buf;
419 if (!advance (buf, 2))
420 return 0;
421 if (buf->is_bigendian)
422 return ((uint16_t) p[0] << 8) | (uint16_t) p[1];
423 else
424 return ((uint16_t) p[1] << 8) | (uint16_t) p[0];
427 /* Read a uint32 from BUF and advance 4 bytes. */
429 static uint32_t
430 read_uint32 (struct dwarf_buf *buf)
432 const unsigned char *p = buf->buf;
434 if (!advance (buf, 4))
435 return 0;
436 if (buf->is_bigendian)
437 return (((uint32_t) p[0] << 24) | ((uint32_t) p[1] << 16)
438 | ((uint32_t) p[2] << 8) | (uint32_t) p[3]);
439 else
440 return (((uint32_t) p[3] << 24) | ((uint32_t) p[2] << 16)
441 | ((uint32_t) p[1] << 8) | (uint32_t) p[0]);
444 /* Read a uint64 from BUF and advance 8 bytes. */
446 static uint64_t
447 read_uint64 (struct dwarf_buf *buf)
449 const unsigned char *p = buf->buf;
451 if (!advance (buf, 8))
452 return 0;
453 if (buf->is_bigendian)
454 return (((uint64_t) p[0] << 56) | ((uint64_t) p[1] << 48)
455 | ((uint64_t) p[2] << 40) | ((uint64_t) p[3] << 32)
456 | ((uint64_t) p[4] << 24) | ((uint64_t) p[5] << 16)
457 | ((uint64_t) p[6] << 8) | (uint64_t) p[7]);
458 else
459 return (((uint64_t) p[7] << 56) | ((uint64_t) p[6] << 48)
460 | ((uint64_t) p[5] << 40) | ((uint64_t) p[4] << 32)
461 | ((uint64_t) p[3] << 24) | ((uint64_t) p[2] << 16)
462 | ((uint64_t) p[1] << 8) | (uint64_t) p[0]);
465 /* Read an offset from BUF and advance the appropriate number of
466 bytes. */
468 static uint64_t
469 read_offset (struct dwarf_buf *buf, int is_dwarf64)
471 if (is_dwarf64)
472 return read_uint64 (buf);
473 else
474 return read_uint32 (buf);
477 /* Read an address from BUF and advance the appropriate number of
478 bytes. */
480 static uint64_t
481 read_address (struct dwarf_buf *buf, int addrsize)
483 switch (addrsize)
485 case 1:
486 return read_byte (buf);
487 case 2:
488 return read_uint16 (buf);
489 case 4:
490 return read_uint32 (buf);
491 case 8:
492 return read_uint64 (buf);
493 default:
494 dwarf_buf_error (buf, "unrecognized address size");
495 return 0;
499 /* Return whether a value is the highest possible address, given the
500 address size. */
502 static int
503 is_highest_address (uint64_t address, int addrsize)
505 switch (addrsize)
507 case 1:
508 return address == (unsigned char) -1;
509 case 2:
510 return address == (uint16_t) -1;
511 case 4:
512 return address == (uint32_t) -1;
513 case 8:
514 return address == (uint64_t) -1;
515 default:
516 return 0;
520 /* Read an unsigned LEB128 number. */
522 static uint64_t
523 read_uleb128 (struct dwarf_buf *buf)
525 uint64_t ret;
526 unsigned int shift;
527 unsigned char b;
529 ret = 0;
530 shift = 0;
533 const unsigned char *p;
535 p = buf->buf;
536 if (!advance (buf, 1))
537 return 0;
538 b = *p;
539 ret |= ((uint64_t) (b & 0x7f)) << shift;
540 shift += 7;
542 while ((b & 0x80) != 0);
544 if (shift > 64)
545 dwarf_buf_error (buf, "LEB128 overflows uint64_5");
547 return ret;
550 /* Read a signed LEB128 number. */
552 static int64_t
553 read_sleb128 (struct dwarf_buf *buf)
555 uint64_t val;
556 unsigned int shift;
557 unsigned char b;
559 val = 0;
560 shift = 0;
563 const unsigned char *p;
565 p = buf->buf;
566 if (!advance (buf, 1))
567 return 0;
568 b = *p;
569 val |= ((uint64_t) (b & 0x7f)) << shift;
570 shift += 7;
572 while ((b & 0x80) != 0);
574 if (shift > 64)
575 dwarf_buf_error (buf, "signed LEB128 overflows uint64_t");
577 if ((b & 0x40) != 0)
578 val |= ((uint64_t) -1) << shift;
580 return (int64_t) val;
583 /* Return the length of an LEB128 number. */
585 static size_t
586 leb128_len (const unsigned char *p)
588 size_t ret;
590 ret = 1;
591 while ((*p & 0x80) != 0)
593 ++p;
594 ++ret;
596 return ret;
599 /* Free an abbreviations structure. */
601 static void
602 free_abbrevs (struct backtrace_state *state, struct abbrevs *abbrevs,
603 backtrace_error_callback error_callback, void *data)
605 size_t i;
607 for (i = 0; i < abbrevs->num_abbrevs; ++i)
608 backtrace_free (state, abbrevs->abbrevs[i].attrs,
609 abbrevs->abbrevs[i].num_attrs * sizeof (struct attr),
610 error_callback, data);
611 backtrace_free (state, abbrevs->abbrevs,
612 abbrevs->num_abbrevs * sizeof (struct abbrev),
613 error_callback, data);
614 abbrevs->num_abbrevs = 0;
615 abbrevs->abbrevs = NULL;
618 /* Read an attribute value. Returns 1 on success, 0 on failure. If
619 the value can be represented as a uint64_t, sets *VAL and sets
620 *IS_VALID to 1. We don't try to store the value of other attribute
621 forms, because we don't care about them. */
623 static int
624 read_attribute (enum dwarf_form form, struct dwarf_buf *buf,
625 int is_dwarf64, int version, int addrsize,
626 const unsigned char *dwarf_str, size_t dwarf_str_size,
627 struct attr_val *val)
629 switch (form)
631 case DW_FORM_addr:
632 val->encoding = ATTR_VAL_ADDRESS;
633 val->u.uint = read_address (buf, addrsize);
634 return 1;
635 case DW_FORM_block2:
636 val->encoding = ATTR_VAL_BLOCK;
637 return advance (buf, read_uint16 (buf));
638 case DW_FORM_block4:
639 val->encoding = ATTR_VAL_BLOCK;
640 return advance (buf, read_uint32 (buf));
641 case DW_FORM_data2:
642 val->encoding = ATTR_VAL_UINT;
643 val->u.uint = read_uint16 (buf);
644 return 1;
645 case DW_FORM_data4:
646 val->encoding = ATTR_VAL_UINT;
647 val->u.uint = read_uint32 (buf);
648 return 1;
649 case DW_FORM_data8:
650 val->encoding = ATTR_VAL_UINT;
651 val->u.uint = read_uint64 (buf);
652 return 1;
653 case DW_FORM_string:
654 val->encoding = ATTR_VAL_STRING;
655 val->u.string = (const char *) buf->buf;
656 return advance (buf, strnlen ((const char *) buf->buf, buf->left) + 1);
657 case DW_FORM_block:
658 val->encoding = ATTR_VAL_BLOCK;
659 return advance (buf, read_uleb128 (buf));
660 case DW_FORM_block1:
661 val->encoding = ATTR_VAL_BLOCK;
662 return advance (buf, read_byte (buf));
663 case DW_FORM_data1:
664 val->encoding = ATTR_VAL_UINT;
665 val->u.uint = read_byte (buf);
666 return 1;
667 case DW_FORM_flag:
668 val->encoding = ATTR_VAL_UINT;
669 val->u.uint = read_byte (buf);
670 return 1;
671 case DW_FORM_sdata:
672 val->encoding = ATTR_VAL_SINT;
673 val->u.sint = read_sleb128 (buf);
674 return 1;
675 case DW_FORM_strp:
677 uint64_t offset;
679 offset = read_offset (buf, is_dwarf64);
680 if (offset >= dwarf_str_size)
682 dwarf_buf_error (buf, "DW_FORM_strp out of range");
683 return 0;
685 val->encoding = ATTR_VAL_STRING;
686 val->u.string = (const char *) dwarf_str + offset;
687 return 1;
689 case DW_FORM_udata:
690 val->encoding = ATTR_VAL_UINT;
691 val->u.uint = read_uleb128 (buf);
692 return 1;
693 case DW_FORM_ref_addr:
694 val->encoding = ATTR_VAL_REF_INFO;
695 if (version == 2)
696 val->u.uint = read_address (buf, addrsize);
697 else
698 val->u.uint = read_offset (buf, is_dwarf64);
699 return 1;
700 case DW_FORM_ref1:
701 val->encoding = ATTR_VAL_REF_UNIT;
702 val->u.uint = read_byte (buf);
703 return 1;
704 case DW_FORM_ref2:
705 val->encoding = ATTR_VAL_REF_UNIT;
706 val->u.uint = read_uint16 (buf);
707 return 1;
708 case DW_FORM_ref4:
709 val->encoding = ATTR_VAL_REF_UNIT;
710 val->u.uint = read_uint32 (buf);
711 return 1;
712 case DW_FORM_ref8:
713 val->encoding = ATTR_VAL_REF_UNIT;
714 val->u.uint = read_uint64 (buf);
715 return 1;
716 case DW_FORM_ref_udata:
717 val->encoding = ATTR_VAL_REF_UNIT;
718 val->u.uint = read_uleb128 (buf);
719 return 1;
720 case DW_FORM_indirect:
722 uint64_t form;
724 form = read_uleb128 (buf);
725 return read_attribute ((enum dwarf_form) form, buf, is_dwarf64,
726 version, addrsize, dwarf_str, dwarf_str_size,
727 val);
729 case DW_FORM_sec_offset:
730 val->encoding = ATTR_VAL_REF_SECTION;
731 val->u.uint = read_offset (buf, is_dwarf64);
732 return 1;
733 case DW_FORM_exprloc:
734 val->encoding = ATTR_VAL_EXPR;
735 return advance (buf, read_uleb128 (buf));
736 case DW_FORM_flag_present:
737 val->encoding = ATTR_VAL_UINT;
738 val->u.uint = 1;
739 return 1;
740 case DW_FORM_ref_sig8:
741 val->encoding = ATTR_VAL_REF_TYPE;
742 val->u.uint = read_uint64 (buf);
743 return 1;
744 case DW_FORM_GNU_addr_index:
745 val->encoding = ATTR_VAL_REF_SECTION;
746 val->u.uint = read_uleb128 (buf);
747 return 1;
748 case DW_FORM_GNU_str_index:
749 val->encoding = ATTR_VAL_REF_SECTION;
750 val->u.uint = read_uleb128 (buf);
751 return 1;
752 case DW_FORM_GNU_ref_alt:
753 val->encoding = ATTR_VAL_REF_SECTION;
754 val->u.uint = read_offset (buf, is_dwarf64);
755 return 1;
756 case DW_FORM_GNU_strp_alt:
757 val->encoding = ATTR_VAL_REF_SECTION;
758 val->u.uint = read_offset (buf, is_dwarf64);
759 return 1;
760 default:
761 dwarf_buf_error (buf, "unrecognized DWARF form");
762 return 0;
766 /* Compare function_addrs for qsort. When ranges are nested, make the
767 smallest one sort last. */
769 static int
770 function_addrs_compare (const void *v1, const void *v2)
772 const struct function_addrs *a1 = (const struct function_addrs *) v1;
773 const struct function_addrs *a2 = (const struct function_addrs *) v2;
775 if (a1->low < a2->low)
776 return -1;
777 if (a1->low > a2->low)
778 return 1;
779 if (a1->high < a2->high)
780 return 1;
781 if (a1->high > a2->high)
782 return -1;
783 return strcmp (a1->function->name, a2->function->name);
786 /* Compare a PC against a function_addrs for bsearch. Note that if
787 there are multiple ranges containing PC, which one will be returned
788 is unpredictable. We compensate for that in dwarf_fileline. */
790 static int
791 function_addrs_search (const void *vkey, const void *ventry)
793 const uintptr_t *key = (const uintptr_t *) vkey;
794 const struct function_addrs *entry = (const struct function_addrs *) ventry;
795 uintptr_t pc;
797 pc = *key;
798 if (pc < entry->low)
799 return -1;
800 else if (pc >= entry->high)
801 return 1;
802 else
803 return 0;
806 /* Add a new compilation unit address range to a vector. Returns 1 on
807 success, 0 on failure. */
809 static int
810 add_unit_addr (struct backtrace_state *state, struct unit_addrs addrs,
811 backtrace_error_callback error_callback, void *data,
812 struct unit_addrs_vector *vec)
814 struct unit_addrs *p;
816 /* Try to merge with the last entry. */
817 if (vec->count > 0)
819 p = (struct unit_addrs *) vec->vec.base + (vec->count - 1);
820 if ((addrs.low == p->high || addrs.low == p->high + 1)
821 && addrs.u == p->u)
823 if (addrs.high > p->high)
824 p->high = addrs.high;
825 return 1;
829 p = ((struct unit_addrs *)
830 backtrace_vector_grow (state, sizeof (struct unit_addrs),
831 error_callback, data, &vec->vec));
832 if (p == NULL)
833 return 0;
835 *p = addrs;
836 ++vec->count;
837 return 1;
840 /* Free a unit address vector. */
842 static void
843 free_unit_addrs_vector (struct backtrace_state *state,
844 struct unit_addrs_vector *vec,
845 backtrace_error_callback error_callback, void *data)
847 struct unit_addrs *addrs;
848 size_t i;
850 addrs = (struct unit_addrs *) vec->vec.base;
851 for (i = 0; i < vec->count; ++i)
852 free_abbrevs (state, &addrs[i].u->abbrevs, error_callback, data);
855 /* Compare unit_addrs for qsort. When ranges are nested, make the
856 smallest one sort last. */
858 static int
859 unit_addrs_compare (const void *v1, const void *v2)
861 const struct unit_addrs *a1 = (const struct unit_addrs *) v1;
862 const struct unit_addrs *a2 = (const struct unit_addrs *) v2;
864 if (a1->low < a2->low)
865 return -1;
866 if (a1->low > a2->low)
867 return 1;
868 if (a1->high < a2->high)
869 return 1;
870 if (a1->high > a2->high)
871 return -1;
872 if (a1->u->lineoff < a2->u->lineoff)
873 return -1;
874 if (a1->u->lineoff > a2->u->lineoff)
875 return 1;
876 return 0;
879 /* Compare a PC against a unit_addrs for bsearch. Note that if there
880 are multiple ranges containing PC, which one will be returned is
881 unpredictable. We compensate for that in dwarf_fileline. */
883 static int
884 unit_addrs_search (const void *vkey, const void *ventry)
886 const uintptr_t *key = (const uintptr_t *) vkey;
887 const struct unit_addrs *entry = (const struct unit_addrs *) ventry;
888 uintptr_t pc;
890 pc = *key;
891 if (pc < entry->low)
892 return -1;
893 else if (pc >= entry->high)
894 return 1;
895 else
896 return 0;
899 /* Sort the line vector by PC. We want a stable sort here. We know
900 that the pointers are into the same array, so it is safe to compare
901 them directly. */
903 static int
904 line_compare (const void *v1, const void *v2)
906 const struct line *ln1 = (const struct line *) v1;
907 const struct line *ln2 = (const struct line *) v2;
909 if (ln1->pc < ln2->pc)
910 return -1;
911 else if (ln1->pc > ln2->pc)
912 return 1;
913 else if (ln1 < ln2)
914 return -1;
915 else if (ln1 > ln2)
916 return 1;
917 else
918 return 0;
921 /* Find a PC in a line vector. We always allocate an extra entry at
922 the end of the lines vector, so that this routine can safely look
923 at the next entry. Note that when there are multiple mappings for
924 the same PC value, this will return the last one. */
926 static int
927 line_search (const void *vkey, const void *ventry)
929 const uintptr_t *key = (const uintptr_t *) vkey;
930 const struct line *entry = (const struct line *) ventry;
931 uintptr_t pc;
933 pc = *key;
934 if (pc < entry->pc)
935 return -1;
936 else if (pc >= (entry + 1)->pc)
937 return 1;
938 else
939 return 0;
942 /* Sort the abbrevs by the abbrev code. This function is passed to
943 both qsort and bsearch. */
945 static int
946 abbrev_compare (const void *v1, const void *v2)
948 const struct abbrev *a1 = (const struct abbrev *) v1;
949 const struct abbrev *a2 = (const struct abbrev *) v2;
951 if (a1->code < a2->code)
952 return -1;
953 else if (a1->code > a2->code)
954 return 1;
955 else
957 /* This really shouldn't happen. It means there are two
958 different abbrevs with the same code, and that means we don't
959 know which one lookup_abbrev should return. */
960 return 0;
964 /* Read the abbreviation table for a compilation unit. Returns 1 on
965 success, 0 on failure. */
967 static int
968 read_abbrevs (struct backtrace_state *state, uint64_t abbrev_offset,
969 const unsigned char *dwarf_abbrev, size_t dwarf_abbrev_size,
970 int is_bigendian, backtrace_error_callback error_callback,
971 void *data, struct abbrevs *abbrevs)
973 struct dwarf_buf abbrev_buf;
974 struct dwarf_buf count_buf;
975 size_t num_abbrevs;
977 abbrevs->num_abbrevs = 0;
978 abbrevs->abbrevs = NULL;
980 if (abbrev_offset >= dwarf_abbrev_size)
982 error_callback (data, "abbrev offset out of range", 0);
983 return 0;
986 abbrev_buf.name = ".debug_abbrev";
987 abbrev_buf.start = dwarf_abbrev;
988 abbrev_buf.buf = dwarf_abbrev + abbrev_offset;
989 abbrev_buf.left = dwarf_abbrev_size - abbrev_offset;
990 abbrev_buf.is_bigendian = is_bigendian;
991 abbrev_buf.error_callback = error_callback;
992 abbrev_buf.data = data;
993 abbrev_buf.reported_underflow = 0;
995 /* Count the number of abbrevs in this list. */
997 count_buf = abbrev_buf;
998 num_abbrevs = 0;
999 while (read_uleb128 (&count_buf) != 0)
1001 if (count_buf.reported_underflow)
1002 return 0;
1003 ++num_abbrevs;
1004 // Skip tag.
1005 read_uleb128 (&count_buf);
1006 // Skip has_children.
1007 read_byte (&count_buf);
1008 // Skip attributes.
1009 while (read_uleb128 (&count_buf) != 0)
1010 read_uleb128 (&count_buf);
1011 // Skip form of last attribute.
1012 read_uleb128 (&count_buf);
1015 if (count_buf.reported_underflow)
1016 return 0;
1018 if (num_abbrevs == 0)
1019 return 1;
1021 abbrevs->num_abbrevs = num_abbrevs;
1022 abbrevs->abbrevs = ((struct abbrev *)
1023 backtrace_alloc (state,
1024 num_abbrevs * sizeof (struct abbrev),
1025 error_callback, data));
1026 if (abbrevs->abbrevs == NULL)
1027 return 0;
1028 memset (abbrevs->abbrevs, 0, num_abbrevs * sizeof (struct abbrev));
1030 num_abbrevs = 0;
1031 while (1)
1033 uint64_t code;
1034 struct abbrev a;
1035 size_t num_attrs;
1036 struct attr *attrs;
1038 if (abbrev_buf.reported_underflow)
1039 goto fail;
1041 code = read_uleb128 (&abbrev_buf);
1042 if (code == 0)
1043 break;
1045 a.code = code;
1046 a.tag = (enum dwarf_tag) read_uleb128 (&abbrev_buf);
1047 a.has_children = read_byte (&abbrev_buf);
1049 count_buf = abbrev_buf;
1050 num_attrs = 0;
1051 while (read_uleb128 (&count_buf) != 0)
1053 ++num_attrs;
1054 read_uleb128 (&count_buf);
1057 if (num_attrs == 0)
1059 attrs = NULL;
1060 read_uleb128 (&abbrev_buf);
1061 read_uleb128 (&abbrev_buf);
1063 else
1065 attrs = ((struct attr *)
1066 backtrace_alloc (state, num_attrs * sizeof *attrs,
1067 error_callback, data));
1068 if (attrs == NULL)
1069 goto fail;
1070 num_attrs = 0;
1071 while (1)
1073 uint64_t name;
1074 uint64_t form;
1076 name = read_uleb128 (&abbrev_buf);
1077 form = read_uleb128 (&abbrev_buf);
1078 if (name == 0)
1079 break;
1080 attrs[num_attrs].name = (enum dwarf_attribute) name;
1081 attrs[num_attrs].form = (enum dwarf_form) form;
1082 ++num_attrs;
1086 a.num_attrs = num_attrs;
1087 a.attrs = attrs;
1089 abbrevs->abbrevs[num_abbrevs] = a;
1090 ++num_abbrevs;
1093 qsort (abbrevs->abbrevs, abbrevs->num_abbrevs, sizeof (struct abbrev),
1094 abbrev_compare);
1096 return 1;
1098 fail:
1099 free_abbrevs (state, abbrevs, error_callback, data);
1100 return 0;
1103 /* Return the abbrev information for an abbrev code. */
1105 static const struct abbrev *
1106 lookup_abbrev (struct abbrevs *abbrevs, uint64_t code,
1107 backtrace_error_callback error_callback, void *data)
1109 struct abbrev key;
1110 void *p;
1112 /* With GCC, where abbrevs are simply numbered in order, we should
1113 be able to just look up the entry. */
1114 if (code - 1 < abbrevs->num_abbrevs
1115 && abbrevs->abbrevs[code - 1].code == code)
1116 return &abbrevs->abbrevs[code - 1];
1118 /* Otherwise we have to search. */
1119 memset (&key, 0, sizeof key);
1120 key.code = code;
1121 p = bsearch (&key, abbrevs->abbrevs, abbrevs->num_abbrevs,
1122 sizeof (struct abbrev), abbrev_compare);
1123 if (p == NULL)
1125 error_callback (data, "invalid abbreviation code", 0);
1126 return NULL;
1128 return (const struct abbrev *) p;
1131 /* Add non-contiguous address ranges for a compilation unit. Returns
1132 1 on success, 0 on failure. */
1134 static int
1135 add_unit_ranges (struct backtrace_state *state, struct unit *u,
1136 uint64_t ranges, uint64_t base, int is_bigendian,
1137 const unsigned char *dwarf_ranges, size_t dwarf_ranges_size,
1138 backtrace_error_callback error_callback, void *data,
1139 struct unit_addrs_vector *addrs)
1141 struct dwarf_buf ranges_buf;
1143 if (ranges >= dwarf_ranges_size)
1145 error_callback (data, "ranges offset out of range", 0);
1146 return 0;
1149 ranges_buf.name = ".debug_ranges";
1150 ranges_buf.start = dwarf_ranges;
1151 ranges_buf.buf = dwarf_ranges + ranges;
1152 ranges_buf.left = dwarf_ranges_size - ranges;
1153 ranges_buf.is_bigendian = is_bigendian;
1154 ranges_buf.error_callback = error_callback;
1155 ranges_buf.data = data;
1156 ranges_buf.reported_underflow = 0;
1158 while (1)
1160 uint64_t low;
1161 uint64_t high;
1163 if (ranges_buf.reported_underflow)
1164 return 0;
1166 low = read_address (&ranges_buf, u->addrsize);
1167 high = read_address (&ranges_buf, u->addrsize);
1169 if (low == 0 && high == 0)
1170 break;
1172 if (is_highest_address (low, u->addrsize))
1173 base = high;
1174 else
1176 struct unit_addrs a;
1178 a.low = low + base;
1179 a.high = high + base;
1180 a.u = u;
1181 if (!add_unit_addr (state, a, error_callback, data, addrs))
1182 return 0;
1186 if (ranges_buf.reported_underflow)
1187 return 0;
1189 return 1;
1192 /* Build a mapping from address ranges to the compilation units where
1193 the line number information for that range can be found. Returns 1
1194 on success, 0 on failure. */
1196 static int
1197 build_address_map (struct backtrace_state *state,
1198 const unsigned char *dwarf_info, size_t dwarf_info_size,
1199 const unsigned char *dwarf_abbrev, size_t dwarf_abbrev_size,
1200 const unsigned char *dwarf_ranges, size_t dwarf_ranges_size,
1201 const unsigned char *dwarf_str, size_t dwarf_str_size,
1202 int is_bigendian, backtrace_error_callback error_callback,
1203 void *data, struct unit_addrs_vector *addrs)
1205 struct dwarf_buf info;
1206 struct abbrevs abbrevs;
1208 memset (&addrs->vec, 0, sizeof addrs->vec);
1209 addrs->count = 0;
1211 /* Read through the .debug_info section. FIXME: Should we use the
1212 .debug_aranges section? gdb and addr2line don't use it, but I'm
1213 not sure why. */
1215 info.name = ".debug_info";
1216 info.start = dwarf_info;
1217 info.buf = dwarf_info;
1218 info.left = dwarf_info_size;
1219 info.is_bigendian = is_bigendian;
1220 info.error_callback = error_callback;
1221 info.data = data;
1222 info.reported_underflow = 0;
1224 memset (&abbrevs, 0, sizeof abbrevs);
1225 while (info.left > 0)
1227 const unsigned char *unit_data_start;
1228 uint64_t len;
1229 int is_dwarf64;
1230 struct dwarf_buf unit_buf;
1231 int version;
1232 uint64_t abbrev_offset;
1233 const struct abbrev *abbrev;
1234 int addrsize;
1235 const unsigned char *unit_data;
1236 size_t unit_data_len;
1237 size_t unit_data_offset;
1238 uint64_t code;
1239 size_t i;
1240 uint64_t lowpc;
1241 int have_lowpc;
1242 uint64_t highpc;
1243 int have_highpc;
1244 int highpc_is_relative;
1245 uint64_t ranges;
1246 int have_ranges;
1247 uint64_t lineoff;
1248 int have_lineoff;
1249 const char *comp_dir;
1251 if (info.reported_underflow)
1252 goto fail;
1254 unit_data_start = info.buf;
1256 is_dwarf64 = 0;
1257 len = read_uint32 (&info);
1258 if (len == 0xffffffff)
1260 len = read_uint64 (&info);
1261 is_dwarf64 = 1;
1264 unit_buf = info;
1265 unit_buf.start = info.buf;
1266 unit_buf.left = len;
1268 if (!advance (&info, len))
1269 goto fail;
1271 version = read_uint16 (&unit_buf);
1272 if (version < 2 || version > 4)
1274 dwarf_buf_error (&unit_buf, "unrecognized DWARF version");
1275 goto fail;
1278 abbrev_offset = read_offset (&unit_buf, is_dwarf64);
1279 if (!read_abbrevs (state, abbrev_offset, dwarf_abbrev, dwarf_abbrev_size,
1280 is_bigendian, error_callback, data, &abbrevs))
1281 goto fail;
1283 addrsize = read_byte (&unit_buf);
1285 unit_data = unit_buf.buf;
1286 unit_data_len = unit_buf.left;
1287 unit_data_offset = unit_buf.buf - unit_data_start;
1289 /* We only look at the first attribute in the compilation unit.
1290 In practice this will be a DW_TAG_compile_unit which will
1291 tell us the PC range and where to find the line number
1292 information. */
1294 code = read_uleb128 (&unit_buf);
1295 abbrev = lookup_abbrev (&abbrevs, code, error_callback, data);
1296 if (abbrev == NULL)
1297 goto fail;
1299 lowpc = 0;
1300 have_lowpc = 0;
1301 highpc = 0;
1302 have_highpc = 0;
1303 highpc_is_relative = 0;
1304 ranges = 0;
1305 have_ranges = 0;
1306 lineoff = 0;
1307 have_lineoff = 0;
1308 comp_dir = NULL;
1309 for (i = 0; i < abbrev->num_attrs; ++i)
1311 struct attr_val val;
1313 if (!read_attribute (abbrev->attrs[i].form, &unit_buf, is_dwarf64,
1314 version, addrsize, dwarf_str, dwarf_str_size,
1315 &val))
1316 goto fail;
1318 switch (abbrev->attrs[i].name)
1320 case DW_AT_low_pc:
1321 if (val.encoding == ATTR_VAL_ADDRESS)
1323 lowpc = val.u.uint;
1324 have_lowpc = 1;
1326 break;
1327 case DW_AT_high_pc:
1328 if (val.encoding == ATTR_VAL_ADDRESS)
1330 highpc = val.u.uint;
1331 have_highpc = 1;
1333 else if (val.encoding == ATTR_VAL_UINT)
1335 highpc = val.u.uint;
1336 have_highpc = 1;
1337 highpc_is_relative = 1;
1339 break;
1340 case DW_AT_ranges:
1341 if (val.encoding == ATTR_VAL_UINT
1342 || val.encoding == ATTR_VAL_REF_SECTION)
1344 ranges = val.u.uint;
1345 have_ranges = 1;
1347 break;
1348 case DW_AT_stmt_list:
1349 if (val.encoding == ATTR_VAL_UINT
1350 || val.encoding == ATTR_VAL_REF_SECTION)
1352 lineoff = val.u.uint;
1353 have_lineoff = 1;
1355 break;
1356 case DW_AT_comp_dir:
1357 if (val.encoding == ATTR_VAL_STRING)
1358 comp_dir = val.u.string;
1359 break;
1360 default:
1361 break;
1365 if (unit_buf.reported_underflow)
1366 goto fail;
1368 if (((have_lowpc && have_highpc) || have_ranges) && have_lineoff)
1370 struct unit *u;
1371 struct unit_addrs a;
1373 u = ((struct unit *)
1374 backtrace_alloc (state, sizeof *u, error_callback, data));
1375 if (u == NULL)
1376 goto fail;
1377 u->unit_data = unit_data;
1378 u->unit_data_len = unit_data_len;
1379 u->unit_data_offset = unit_data_offset;
1380 u->version = version;
1381 u->is_dwarf64 = is_dwarf64;
1382 u->addrsize = addrsize;
1383 u->comp_dir = comp_dir;
1384 u->lineoff = lineoff;
1385 u->abbrevs = abbrevs;
1386 memset (&abbrevs, 0, sizeof abbrevs);
1388 /* The actual line number mappings will be read as
1389 needed. */
1390 u->lines = NULL;
1391 u->lines_count = 0;
1392 u->function_addrs = NULL;
1393 u->function_addrs_count = 0;
1395 if (have_ranges)
1397 if (!add_unit_ranges (state, u, ranges, lowpc, is_bigendian,
1398 dwarf_ranges, dwarf_ranges_size,
1399 error_callback, data, addrs))
1401 free_abbrevs (state, &u->abbrevs, error_callback, data);
1402 backtrace_free (state, u, sizeof *u, error_callback, data);
1403 goto fail;
1406 else
1408 if (highpc_is_relative)
1409 highpc += lowpc;
1410 a.low = lowpc;
1411 a.high = highpc;
1412 a.u = u;
1414 if (!add_unit_addr (state, a, error_callback, data, addrs))
1416 free_abbrevs (state, &u->abbrevs, error_callback, data);
1417 backtrace_free (state, u, sizeof *u, error_callback, data);
1418 goto fail;
1422 else
1424 free_abbrevs (state, &abbrevs, error_callback, data);
1425 memset (&abbrevs, 0, sizeof abbrevs);
1428 if (info.reported_underflow)
1429 goto fail;
1431 return 1;
1433 fail:
1434 free_abbrevs (state, &abbrevs, error_callback, data);
1435 free_unit_addrs_vector (state, addrs, error_callback, data);
1436 return 0;
1439 /* Add a new mapping to the vector of line mappings that we are
1440 building. Returns 1 on success, 0 on failure. */
1442 static int
1443 add_line (struct backtrace_state *state, uintptr_t pc, const char *filename,
1444 int lineno, backtrace_error_callback error_callback, void *data,
1445 struct line_vector *vec)
1447 struct line *ln;
1449 /* If we are adding the same mapping, ignore it. This can happen
1450 when using discriminators. */
1451 if (vec->count > 0)
1453 ln = (struct line *) vec->vec.base + (vec->count - 1);
1454 if (pc == ln->pc && filename == ln->filename && lineno == ln->lineno)
1455 return 1;
1458 ln = ((struct line *)
1459 backtrace_vector_grow (state, sizeof (struct line), error_callback,
1460 data, &vec->vec));
1461 if (ln == NULL)
1462 return 0;
1464 ln->pc = pc;
1465 ln->filename = filename;
1466 ln->lineno = lineno;
1468 ++vec->count;
1470 return 1;
1473 /* Free the line header information. If FREE_FILENAMES is true we
1474 free the file names themselves, otherwise we leave them, as there
1475 may be line structures pointing to them. */
1477 static void
1478 free_line_header (struct backtrace_state *state, struct line_header *hdr,
1479 backtrace_error_callback error_callback, void *data)
1481 backtrace_free (state, hdr->dirs, hdr->dirs_count * sizeof (const char *),
1482 error_callback, data);
1483 backtrace_free (state, hdr->filenames,
1484 hdr->filenames_count * sizeof (char *),
1485 error_callback, data);
1488 /* Read the line header. Return 1 on success, 0 on failure. */
1490 static int
1491 read_line_header (struct backtrace_state *state, struct unit *u,
1492 int is_dwarf64, struct dwarf_buf *line_buf,
1493 struct line_header *hdr)
1495 uint64_t hdrlen;
1496 struct dwarf_buf hdr_buf;
1497 const unsigned char *p;
1498 const unsigned char *pend;
1499 size_t i;
1501 hdr->version = read_uint16 (line_buf);
1502 if (hdr->version < 2 || hdr->version > 4)
1504 dwarf_buf_error (line_buf, "unsupported line number version");
1505 return 0;
1508 hdrlen = read_offset (line_buf, is_dwarf64);
1510 hdr_buf = *line_buf;
1511 hdr_buf.left = hdrlen;
1513 if (!advance (line_buf, hdrlen))
1514 return 0;
1516 hdr->min_insn_len = read_byte (&hdr_buf);
1517 if (hdr->version < 4)
1518 hdr->max_ops_per_insn = 1;
1519 else
1520 hdr->max_ops_per_insn = read_byte (&hdr_buf);
1522 /* We don't care about default_is_stmt. */
1523 read_byte (&hdr_buf);
1525 hdr->line_base = read_sbyte (&hdr_buf);
1526 hdr->line_range = read_byte (&hdr_buf);
1528 hdr->opcode_base = read_byte (&hdr_buf);
1529 hdr->opcode_lengths = hdr_buf.buf;
1530 if (!advance (&hdr_buf, hdr->opcode_base - 1))
1531 return 0;
1533 /* Count the number of directory entries. */
1534 hdr->dirs_count = 0;
1535 p = hdr_buf.buf;
1536 pend = p + hdr_buf.left;
1537 while (p < pend && *p != '\0')
1539 p += strnlen((const char *) p, pend - p) + 1;
1540 ++hdr->dirs_count;
1543 hdr->dirs = ((const char **)
1544 backtrace_alloc (state,
1545 hdr->dirs_count * sizeof (const char *),
1546 line_buf->error_callback, line_buf->data));
1547 if (hdr->dirs == NULL)
1548 return 0;
1550 i = 0;
1551 while (*hdr_buf.buf != '\0')
1553 if (hdr_buf.reported_underflow)
1554 return 0;
1556 hdr->dirs[i] = (const char *) hdr_buf.buf;
1557 ++i;
1558 if (!advance (&hdr_buf,
1559 strnlen ((const char *) hdr_buf.buf, hdr_buf.left) + 1))
1560 return 0;
1562 if (!advance (&hdr_buf, 1))
1563 return 0;
1565 /* Count the number of file entries. */
1566 hdr->filenames_count = 0;
1567 p = hdr_buf.buf;
1568 pend = p + hdr_buf.left;
1569 while (p < pend && *p != '\0')
1571 p += strnlen ((const char *) p, pend - p) + 1;
1572 p += leb128_len (p);
1573 p += leb128_len (p);
1574 p += leb128_len (p);
1575 ++hdr->filenames_count;
1578 hdr->filenames = ((const char **)
1579 backtrace_alloc (state,
1580 hdr->filenames_count * sizeof (char *),
1581 line_buf->error_callback,
1582 line_buf->data));
1583 if (hdr->filenames == NULL)
1584 return 0;
1585 i = 0;
1586 while (*hdr_buf.buf != '\0')
1588 const char *filename;
1589 uint64_t dir_index;
1591 if (hdr_buf.reported_underflow)
1592 return 0;
1594 filename = (const char *) hdr_buf.buf;
1595 if (!advance (&hdr_buf,
1596 strnlen ((const char *) hdr_buf.buf, hdr_buf.left) + 1))
1597 return 0;
1598 dir_index = read_uleb128 (&hdr_buf);
1599 if (IS_ABSOLUTE_PATH (filename))
1600 hdr->filenames[i] = filename;
1601 else
1603 const char *dir;
1604 size_t dir_len;
1605 size_t filename_len;
1606 char *s;
1608 if (dir_index == 0)
1609 dir = u->comp_dir;
1610 else if (dir_index - 1 < hdr->dirs_count)
1611 dir = hdr->dirs[dir_index - 1];
1612 else
1614 dwarf_buf_error (line_buf,
1615 ("invalid directory index in "
1616 "line number program header"));
1617 return 0;
1619 dir_len = strlen (dir);
1620 filename_len = strlen (filename);
1621 s = ((char *)
1622 backtrace_alloc (state, dir_len + filename_len + 2,
1623 line_buf->error_callback, line_buf->data));
1624 if (s == NULL)
1625 return 0;
1626 memcpy (s, dir, dir_len);
1627 /* FIXME: If we are on a DOS-based file system, and the
1628 directory or the file name use backslashes, then we
1629 should use a backslash here. */
1630 s[dir_len] = '/';
1631 memcpy (s + dir_len + 1, filename, filename_len + 1);
1632 hdr->filenames[i] = s;
1635 /* Ignore the modification time and size. */
1636 read_uleb128 (&hdr_buf);
1637 read_uleb128 (&hdr_buf);
1639 ++i;
1642 if (hdr_buf.reported_underflow)
1643 return 0;
1645 return 1;
1648 /* Read the line program, adding line mappings to VEC. Return 1 on
1649 success, 0 on failure. */
1651 static int
1652 read_line_program (struct backtrace_state *state, struct unit *u,
1653 const struct line_header *hdr, struct dwarf_buf *line_buf,
1654 struct line_vector *vec)
1656 uint64_t address;
1657 unsigned int op_index;
1658 const char *reset_filename;
1659 const char *filename;
1660 int lineno;
1662 address = 0;
1663 op_index = 0;
1664 if (hdr->filenames_count > 0)
1665 reset_filename = hdr->filenames[0];
1666 else
1667 reset_filename = "";
1668 filename = reset_filename;
1669 lineno = 1;
1670 while (line_buf->left > 0)
1672 unsigned int op;
1674 op = read_byte (line_buf);
1675 if (op >= hdr->opcode_base)
1677 unsigned int advance;
1679 /* Special opcode. */
1680 op -= hdr->opcode_base;
1681 advance = op / hdr->line_range;
1682 address += (hdr->min_insn_len * (op_index + advance)
1683 / hdr->max_ops_per_insn);
1684 op_index = (op_index + advance) % hdr->max_ops_per_insn;
1685 lineno += hdr->line_base + (int) (op % hdr->line_range);
1686 add_line (state, address, filename, lineno, line_buf->error_callback,
1687 line_buf->data, vec);
1689 else if (op == DW_LNS_extended_op)
1691 uint64_t len;
1693 len = read_uleb128 (line_buf);
1694 op = read_byte (line_buf);
1695 switch (op)
1697 case DW_LNE_end_sequence:
1698 /* FIXME: Should we mark the high PC here? It seems
1699 that we already have that information from the
1700 compilation unit. */
1701 address = 0;
1702 op_index = 0;
1703 filename = reset_filename;
1704 lineno = 1;
1705 break;
1706 case DW_LNE_set_address:
1707 address = read_address (line_buf, u->addrsize);
1708 break;
1709 case DW_LNE_define_file:
1711 const char *f;
1712 unsigned int dir_index;
1714 f = (const char *) line_buf->buf;
1715 if (!advance (line_buf, strnlen (f, line_buf->left) + 1))
1716 return 0;
1717 dir_index = read_uleb128 (line_buf);
1718 /* Ignore that time and length. */
1719 read_uleb128 (line_buf);
1720 read_uleb128 (line_buf);
1721 if (IS_ABSOLUTE_PATH (f))
1722 filename = f;
1723 else
1725 const char *dir;
1726 size_t dir_len;
1727 size_t f_len;
1728 char *p;
1730 if (dir_index == 0)
1731 dir = u->comp_dir;
1732 else if (dir_index - 1 < hdr->dirs_count)
1733 dir = hdr->dirs[dir_index - 1];
1734 else
1736 dwarf_buf_error (line_buf,
1737 ("invalid directory index "
1738 "in line number program"));
1739 return 0;
1741 dir_len = strlen (dir);
1742 f_len = strlen (f);
1743 p = ((char *)
1744 backtrace_alloc (state, dir_len + f_len + 2,
1745 line_buf->error_callback,
1746 line_buf->data));
1747 if (p == NULL)
1748 return 0;
1749 memcpy (p, dir, dir_len);
1750 /* FIXME: If we are on a DOS-based file system,
1751 and the directory or the file name use
1752 backslashes, then we should use a backslash
1753 here. */
1754 p[dir_len] = '/';
1755 memcpy (p + dir_len + 1, f, f_len + 1);
1756 filename = p;
1759 break;
1760 case DW_LNE_set_discriminator:
1761 /* We don't care about discriminators. */
1762 read_uleb128 (line_buf);
1763 break;
1764 default:
1765 if (!advance (line_buf, len - 1))
1766 return 0;
1767 break;
1770 else
1772 switch (op)
1774 case DW_LNS_copy:
1775 add_line (state, address, filename, lineno,
1776 line_buf->error_callback, line_buf->data, vec);
1777 break;
1778 case DW_LNS_advance_pc:
1780 uint64_t advance;
1782 advance = read_uleb128 (line_buf);
1783 address += (hdr->min_insn_len * (op_index + advance)
1784 / hdr->max_ops_per_insn);
1785 op_index = (op_index + advance) % hdr->max_ops_per_insn;
1787 break;
1788 case DW_LNS_advance_line:
1789 lineno += (int) read_sleb128 (line_buf);
1790 break;
1791 case DW_LNS_set_file:
1793 uint64_t fileno;
1795 fileno = read_uleb128 (line_buf);
1796 if (fileno == 0)
1797 filename = "";
1798 else
1800 if (fileno - 1 >= hdr->filenames_count)
1802 dwarf_buf_error (line_buf,
1803 ("invalid file number in "
1804 "line number program"));
1805 return 0;
1807 filename = hdr->filenames[fileno - 1];
1810 break;
1811 case DW_LNS_set_column:
1812 read_uleb128 (line_buf);
1813 break;
1814 case DW_LNS_negate_stmt:
1815 break;
1816 case DW_LNS_set_basic_block:
1817 break;
1818 case DW_LNS_const_add_pc:
1820 unsigned int advance;
1822 op = 255 - hdr->opcode_base;
1823 advance = op / hdr->line_range;
1824 address += (hdr->min_insn_len * (op_index + advance)
1825 / hdr->max_ops_per_insn);
1826 op_index = (op_index + advance) % hdr->max_ops_per_insn;
1828 break;
1829 case DW_LNS_fixed_advance_pc:
1830 address += read_uint16 (line_buf);
1831 op_index = 0;
1832 break;
1833 case DW_LNS_set_prologue_end:
1834 break;
1835 case DW_LNS_set_epilogue_begin:
1836 break;
1837 case DW_LNS_set_isa:
1838 read_uleb128 (line_buf);
1839 break;
1840 default:
1842 unsigned int i;
1844 for (i = hdr->opcode_lengths[op - 1]; i > 0; --i)
1845 read_uleb128 (line_buf);
1847 break;
1852 return 1;
1855 /* Read the line number information for a compilation unit. Returns 1
1856 on success, 0 on failure. */
1858 static int
1859 read_line_info (struct backtrace_state *state, struct dwarf_data *ddata,
1860 backtrace_error_callback error_callback, void *data,
1861 struct unit *u, struct line_header *hdr, struct line **lines,
1862 size_t *lines_count)
1864 struct line_vector vec;
1865 struct dwarf_buf line_buf;
1866 uint64_t len;
1867 int is_dwarf64;
1868 struct line *ln;
1870 memset (&vec.vec, 0, sizeof vec.vec);
1871 vec.count = 0;
1873 memset (hdr, 0, sizeof *hdr);
1875 if (u->lineoff != (off_t) (size_t) u->lineoff
1876 || (size_t) u->lineoff >= ddata->dwarf_line_size)
1878 error_callback (data, "unit line offset out of range", 0);
1879 goto fail;
1882 line_buf.name = ".debug_line";
1883 line_buf.start = ddata->dwarf_line;
1884 line_buf.buf = ddata->dwarf_line + u->lineoff;
1885 line_buf.left = ddata->dwarf_line_size - u->lineoff;
1886 line_buf.is_bigendian = ddata->is_bigendian;
1887 line_buf.error_callback = error_callback;
1888 line_buf.data = data;
1889 line_buf.reported_underflow = 0;
1891 is_dwarf64 = 0;
1892 len = read_uint32 (&line_buf);
1893 if (len == 0xffffffff)
1895 len = read_uint64 (&line_buf);
1896 is_dwarf64 = 1;
1898 line_buf.left = len;
1900 if (!read_line_header (state, u, is_dwarf64, &line_buf, hdr))
1901 goto fail;
1903 if (!read_line_program (state, u, hdr, &line_buf, &vec))
1904 goto fail;
1906 if (line_buf.reported_underflow)
1907 goto fail;
1909 if (vec.count == 0)
1911 /* This is not a failure in the sense of a generating an error,
1912 but it is a failure in that sense that we have no useful
1913 information. */
1914 goto fail;
1917 /* Allocate one extra entry at the end. */
1918 ln = ((struct line *)
1919 backtrace_vector_grow (state, sizeof (struct line), error_callback,
1920 data, &vec.vec));
1921 if (ln == NULL)
1922 goto fail;
1923 ln->pc = (uintptr_t) -1;
1924 ln->filename = NULL;
1925 ln->lineno = 0;
1927 if (!backtrace_vector_release (state, &vec.vec, error_callback, data))
1928 goto fail;
1930 ln = (struct line *) vec.vec.base;
1931 qsort (ln, vec.count, sizeof (struct line), line_compare);
1933 *lines = ln;
1934 *lines_count = vec.count;
1936 return 1;
1938 fail:
1939 vec.vec.alc += vec.vec.size;
1940 vec.vec.size = 0;
1941 backtrace_vector_release (state, &vec.vec, error_callback, data);
1942 free_line_header (state, hdr, error_callback, data);
1943 *lines = (struct line *) (uintptr_t) -1;
1944 *lines_count = 0;
1945 return 0;
1948 /* Read the name of a function from a DIE referenced by a
1949 DW_AT_abstract_origin or DW_AT_specification tag. OFFSET is within
1950 the same compilation unit. */
1952 static const char *
1953 read_referenced_name (struct dwarf_data *ddata, struct unit *u,
1954 uint64_t offset, backtrace_error_callback error_callback,
1955 void *data)
1957 struct dwarf_buf unit_buf;
1958 uint64_t code;
1959 const struct abbrev *abbrev;
1960 const char *ret;
1961 size_t i;
1963 /* OFFSET is from the start of the data for this compilation unit.
1964 U->unit_data is the data, but it starts U->unit_data_offset bytes
1965 from the beginning. */
1967 if (offset < u->unit_data_offset
1968 || offset - u->unit_data_offset >= u->unit_data_len)
1970 error_callback (data,
1971 "abstract origin or specification out of range",
1973 return NULL;
1976 offset -= u->unit_data_offset;
1978 unit_buf.name = ".debug_info";
1979 unit_buf.start = ddata->dwarf_info;
1980 unit_buf.buf = u->unit_data + offset;
1981 unit_buf.left = u->unit_data_len - offset;
1982 unit_buf.is_bigendian = ddata->is_bigendian;
1983 unit_buf.error_callback = error_callback;
1984 unit_buf.data = data;
1985 unit_buf.reported_underflow = 0;
1987 code = read_uleb128 (&unit_buf);
1988 if (code == 0)
1990 dwarf_buf_error (&unit_buf, "invalid abstract origin or specification");
1991 return NULL;
1994 abbrev = lookup_abbrev (&u->abbrevs, code, error_callback, data);
1995 if (abbrev == NULL)
1996 return NULL;
1998 ret = NULL;
1999 for (i = 0; i < abbrev->num_attrs; ++i)
2001 struct attr_val val;
2003 if (!read_attribute (abbrev->attrs[i].form, &unit_buf,
2004 u->is_dwarf64, u->version, u->addrsize,
2005 ddata->dwarf_str, ddata->dwarf_str_size,
2006 &val))
2007 return NULL;
2009 switch (abbrev->attrs[i].name)
2011 case DW_AT_name:
2012 /* We prefer the linkage name if get one. */
2013 if (val.encoding == ATTR_VAL_STRING)
2014 ret = val.u.string;
2015 break;
2017 case DW_AT_linkage_name:
2018 case DW_AT_MIPS_linkage_name:
2019 if (val.encoding == ATTR_VAL_STRING)
2020 return val.u.string;
2021 break;
2023 case DW_AT_specification:
2024 if (abbrev->attrs[i].form == DW_FORM_ref_addr
2025 || abbrev->attrs[i].form == DW_FORM_ref_sig8)
2027 /* This refers to a specification defined in some other
2028 compilation unit. We can handle this case if we
2029 must, but it's harder. */
2030 break;
2032 if (val.encoding == ATTR_VAL_UINT
2033 || val.encoding == ATTR_VAL_REF_UNIT)
2035 const char *name;
2037 name = read_referenced_name (ddata, u, val.u.uint,
2038 error_callback, data);
2039 if (name != NULL)
2040 ret = name;
2042 break;
2044 default:
2045 break;
2049 return ret;
2052 /* Add a single range to U that maps to function. Returns 1 on
2053 success, 0 on error. */
2055 static int
2056 add_function_range (struct backtrace_state *state, struct function *function,
2057 uint64_t lowpc, uint64_t highpc,
2058 backtrace_error_callback error_callback,
2059 void *data, struct function_vector *vec)
2061 struct function_addrs *p;
2063 if (vec->count > 0)
2065 p = (struct function_addrs *) vec->vec.base + vec->count - 1;
2066 if ((lowpc == p->high || lowpc == p->high + 1)
2067 && function == p->function)
2069 if (highpc > p->high)
2070 p->high = highpc;
2071 return 1;
2075 p = ((struct function_addrs *)
2076 backtrace_vector_grow (state, sizeof (struct function_addrs),
2077 error_callback, data, &vec->vec));
2078 if (p == NULL)
2079 return 0;
2081 p->low = lowpc;
2082 p->high = highpc;
2083 p->function = function;
2084 ++vec->count;
2085 return 1;
2088 /* Add PC ranges to U that map to FUNCTION. Returns 1 on success, 0
2089 on error. */
2091 static int
2092 add_function_ranges (struct backtrace_state *state, struct dwarf_data *ddata,
2093 struct unit *u, struct function *function,
2094 uint64_t ranges, uint64_t base,
2095 backtrace_error_callback error_callback, void *data,
2096 struct function_vector *vec)
2098 struct dwarf_buf ranges_buf;
2100 if (ranges >= ddata->dwarf_ranges_size)
2102 error_callback (data, "function ranges offset out of range", 0);
2103 return 0;
2106 ranges_buf.name = ".debug_ranges";
2107 ranges_buf.start = ddata->dwarf_ranges;
2108 ranges_buf.buf = ddata->dwarf_ranges + ranges;
2109 ranges_buf.left = ddata->dwarf_ranges_size - ranges;
2110 ranges_buf.is_bigendian = ddata->is_bigendian;
2111 ranges_buf.error_callback = error_callback;
2112 ranges_buf.data = data;
2113 ranges_buf.reported_underflow = 0;
2115 while (1)
2117 uint64_t low;
2118 uint64_t high;
2120 if (ranges_buf.reported_underflow)
2121 return 0;
2123 low = read_address (&ranges_buf, u->addrsize);
2124 high = read_address (&ranges_buf, u->addrsize);
2126 if (low == 0 && high == 0)
2127 break;
2129 if (is_highest_address (low, u->addrsize))
2130 base = high;
2131 else
2133 if (!add_function_range (state, function, low + base, high + base,
2134 error_callback, data, vec))
2135 return 0;
2139 if (ranges_buf.reported_underflow)
2140 return 0;
2142 return 1;
2145 /* Read one entry plus all its children. Add function addresses to
2146 VEC. Returns 1 on success, 0 on error. */
2148 static int
2149 read_function_entry (struct backtrace_state *state, struct dwarf_data *ddata,
2150 struct unit *u, uint64_t base, struct dwarf_buf *unit_buf,
2151 const struct line_header *lhdr,
2152 backtrace_error_callback error_callback, void *data,
2153 struct function_vector *vec)
2155 while (unit_buf->left > 0)
2157 uint64_t code;
2158 const struct abbrev *abbrev;
2159 int is_function;
2160 struct function *function;
2161 size_t i;
2162 uint64_t lowpc;
2163 int have_lowpc;
2164 uint64_t highpc;
2165 int have_highpc;
2166 int highpc_is_relative;
2167 uint64_t ranges;
2168 int have_ranges;
2170 code = read_uleb128 (unit_buf);
2171 if (code == 0)
2172 return 1;
2174 abbrev = lookup_abbrev (&u->abbrevs, code, error_callback, data);
2175 if (abbrev == NULL)
2176 return 0;
2178 is_function = (abbrev->tag == DW_TAG_subprogram
2179 || abbrev->tag == DW_TAG_entry_point
2180 || abbrev->tag == DW_TAG_inlined_subroutine);
2182 function = NULL;
2183 if (is_function)
2185 function = ((struct function *)
2186 backtrace_alloc (state, sizeof *function,
2187 error_callback, data));
2188 if (function == NULL)
2189 return 0;
2190 memset (function, 0, sizeof *function);
2193 lowpc = 0;
2194 have_lowpc = 0;
2195 highpc = 0;
2196 have_highpc = 0;
2197 highpc_is_relative = 0;
2198 ranges = 0;
2199 have_ranges = 0;
2200 for (i = 0; i < abbrev->num_attrs; ++i)
2202 struct attr_val val;
2204 if (!read_attribute (abbrev->attrs[i].form, unit_buf,
2205 u->is_dwarf64, u->version, u->addrsize,
2206 ddata->dwarf_str, ddata->dwarf_str_size,
2207 &val))
2208 return 0;
2210 /* The compile unit sets the base address for any address
2211 ranges in the function entries. */
2212 if (abbrev->tag == DW_TAG_compile_unit
2213 && abbrev->attrs[i].name == DW_AT_low_pc
2214 && val.encoding == ATTR_VAL_ADDRESS)
2215 base = val.u.uint;
2217 if (is_function)
2219 switch (abbrev->attrs[i].name)
2221 case DW_AT_call_file:
2222 if (val.encoding == ATTR_VAL_UINT)
2224 if (val.u.uint == 0)
2225 function->caller_filename = "";
2226 else
2228 if (val.u.uint - 1 >= lhdr->filenames_count)
2230 dwarf_buf_error (unit_buf,
2231 ("invalid file number in "
2232 "DW_AT_call_file attribute"));
2233 return 0;
2235 function->caller_filename =
2236 lhdr->filenames[val.u.uint - 1];
2239 break;
2241 case DW_AT_call_line:
2242 if (val.encoding == ATTR_VAL_UINT)
2243 function->caller_lineno = val.u.uint;
2244 break;
2246 case DW_AT_abstract_origin:
2247 case DW_AT_specification:
2248 if (abbrev->attrs[i].form == DW_FORM_ref_addr
2249 || abbrev->attrs[i].form == DW_FORM_ref_sig8)
2251 /* This refers to an abstract origin defined in
2252 some other compilation unit. We can handle
2253 this case if we must, but it's harder. */
2254 break;
2256 if (val.encoding == ATTR_VAL_UINT
2257 || val.encoding == ATTR_VAL_REF_UNIT)
2259 const char *name;
2261 name = read_referenced_name (ddata, u, val.u.uint,
2262 error_callback, data);
2263 if (name != NULL)
2264 function->name = name;
2266 break;
2268 case DW_AT_name:
2269 if (val.encoding == ATTR_VAL_STRING)
2271 /* Don't override a name we found in some other
2272 way, as it will normally be more
2273 useful--e.g., this name is normally not
2274 mangled. */
2275 if (function->name == NULL)
2276 function->name = val.u.string;
2278 break;
2280 case DW_AT_linkage_name:
2281 case DW_AT_MIPS_linkage_name:
2282 if (val.encoding == ATTR_VAL_STRING)
2283 function->name = val.u.string;
2284 break;
2286 case DW_AT_low_pc:
2287 if (val.encoding == ATTR_VAL_ADDRESS)
2289 lowpc = val.u.uint;
2290 have_lowpc = 1;
2292 break;
2294 case DW_AT_high_pc:
2295 if (val.encoding == ATTR_VAL_ADDRESS)
2297 highpc = val.u.uint;
2298 have_highpc = 1;
2300 else if (val.encoding == ATTR_VAL_UINT)
2302 highpc = val.u.uint;
2303 have_highpc = 1;
2304 highpc_is_relative = 1;
2306 break;
2308 case DW_AT_ranges:
2309 if (val.encoding == ATTR_VAL_UINT
2310 || val.encoding == ATTR_VAL_REF_SECTION)
2312 ranges = val.u.uint;
2313 have_ranges = 1;
2315 break;
2317 default:
2318 break;
2323 /* If we couldn't find a name for the function, we have no use
2324 for it. */
2325 if (is_function && function->name == NULL)
2327 backtrace_free (state, function, sizeof *function,
2328 error_callback, data);
2329 is_function = 0;
2332 if (is_function)
2334 if (have_ranges)
2336 if (!add_function_ranges (state, ddata, u, function, ranges,
2337 base, error_callback, data, vec))
2338 return 0;
2340 else if (have_lowpc && have_highpc)
2342 if (highpc_is_relative)
2343 highpc += lowpc;
2344 if (!add_function_range (state, function, lowpc, highpc,
2345 error_callback, data, vec))
2346 return 0;
2348 else
2350 backtrace_free (state, function, sizeof *function,
2351 error_callback, data);
2352 is_function = 0;
2356 if (abbrev->has_children)
2358 if (!is_function)
2360 if (!read_function_entry (state, ddata, u, base, unit_buf, lhdr,
2361 error_callback, data, vec))
2362 return 0;
2364 else
2366 struct function_vector fvec;
2368 /* Gather any information for inlined functions in
2369 FVEC. */
2371 memset (&fvec, 0, sizeof fvec);
2373 if (!read_function_entry (state, ddata, u, base, unit_buf, lhdr,
2374 error_callback, data, &fvec))
2375 return 0;
2377 if (fvec.count > 0)
2379 struct function_addrs *faddrs;
2381 if (!backtrace_vector_release (state, &fvec.vec,
2382 error_callback, data))
2383 return 0;
2385 faddrs = (struct function_addrs *) fvec.vec.base;
2386 qsort (faddrs, fvec.count,
2387 sizeof (struct function_addrs),
2388 function_addrs_compare);
2390 function->function_addrs = faddrs;
2391 function->function_addrs_count = fvec.count;
2397 return 1;
2400 /* Read function name information for a compilation unit. We look
2401 through the whole unit looking for function tags. */
2403 static void
2404 read_function_info (struct backtrace_state *state, struct dwarf_data *ddata,
2405 const struct line_header *lhdr,
2406 backtrace_error_callback error_callback, void *data,
2407 struct unit *u, struct function_vector *fvec,
2408 struct function_addrs **ret_addrs,
2409 size_t *ret_addrs_count)
2411 struct dwarf_buf unit_buf;
2412 struct function_addrs *addrs;
2413 size_t addrs_count;
2415 unit_buf.name = ".debug_info";
2416 unit_buf.start = ddata->dwarf_info;
2417 unit_buf.buf = u->unit_data;
2418 unit_buf.left = u->unit_data_len;
2419 unit_buf.is_bigendian = ddata->is_bigendian;
2420 unit_buf.error_callback = error_callback;
2421 unit_buf.data = data;
2422 unit_buf.reported_underflow = 0;
2424 while (unit_buf.left > 0)
2426 if (!read_function_entry (state, ddata, u, 0, &unit_buf, lhdr,
2427 error_callback, data, fvec))
2428 return;
2431 if (fvec->count == 0)
2432 return;
2434 addrs = (struct function_addrs *) fvec->vec.base;
2435 addrs_count = fvec->count;
2437 /* Finish this list of addresses, but leave the remaining space in
2438 the vector available for the next function unit. */
2439 backtrace_vector_finish (state, &fvec->vec);
2440 fvec->count = 0;
2442 qsort (addrs, addrs_count, sizeof (struct function_addrs),
2443 function_addrs_compare);
2445 *ret_addrs = addrs;
2446 *ret_addrs_count = addrs_count;
2449 /* See if PC is inlined in FUNCTION. If it is, print out the inlined
2450 information, and update FILENAME and LINENO for the caller.
2451 Returns whatever CALLBACK returns, or 0 to keep going. */
2453 static int
2454 report_inlined_functions (uintptr_t pc, struct function *function,
2455 backtrace_full_callback callback, void *data,
2456 const char **filename, int *lineno)
2458 struct function_addrs *function_addrs;
2459 struct function *inlined;
2460 int ret;
2462 if (function->function_addrs_count == 0)
2463 return 0;
2465 function_addrs = ((struct function_addrs *)
2466 bsearch (&pc, function->function_addrs,
2467 function->function_addrs_count,
2468 sizeof (struct function_addrs),
2469 function_addrs_search));
2470 if (function_addrs == NULL)
2471 return 0;
2473 while (((size_t) (function_addrs - function->function_addrs) + 1
2474 < function->function_addrs_count)
2475 && pc >= (function_addrs + 1)->low
2476 && pc < (function_addrs + 1)->high)
2477 ++function_addrs;
2479 /* We found an inlined call. */
2481 inlined = function_addrs->function;
2483 /* Report any calls inlined into this one. */
2484 ret = report_inlined_functions (pc, inlined, callback, data,
2485 filename, lineno);
2486 if (ret != 0)
2487 return ret;
2489 /* Report this inlined call. */
2490 ret = callback (data, pc, *filename, *lineno, inlined->name);
2491 if (ret != 0)
2492 return ret;
2494 /* Our caller will report the caller of the inlined function; tell
2495 it the appropriate filename and line number. */
2496 *filename = inlined->caller_filename;
2497 *lineno = inlined->caller_lineno;
2499 return 0;
2502 /* Return the file/line information for a PC using the DWARF mapping
2503 we built earlier. */
2505 static int
2506 dwarf_fileline (struct backtrace_state *state, uintptr_t pc,
2507 backtrace_full_callback callback,
2508 backtrace_error_callback error_callback, void *data)
2510 struct dwarf_data *ddata;
2511 struct unit_addrs *entry;
2512 struct unit *u;
2513 int new_data;
2514 struct line *lines;
2515 struct line *ln;
2516 struct function_addrs *function_addrs;
2517 struct function *function;
2518 const char *filename;
2519 int lineno;
2520 int ret;
2522 ddata = (struct dwarf_data *) state->fileline_data;
2524 /* Find an address range that includes PC. */
2525 entry = bsearch (&pc, ddata->addrs, ddata->addrs_count,
2526 sizeof (struct unit_addrs), unit_addrs_search);
2528 if (entry == NULL)
2529 return callback (data, pc, NULL, 0, NULL);
2531 /* If there are multiple ranges that contain PC, use the last one,
2532 in order to produce predictable results. If we assume that all
2533 ranges are properly nested, then the last range will be the
2534 smallest one. */
2535 while ((size_t) (entry - ddata->addrs) + 1 < ddata->addrs_count
2536 && pc >= (entry + 1)->low
2537 && pc < (entry + 1)->high)
2538 ++entry;
2540 /* We need the lines, lines_count, function_addrs,
2541 function_addrs_count fields of u. If they are not set, we need
2542 to set them. When running in threaded mode, we need to allow for
2543 the possibility that some other thread is setting them
2544 simultaneously. */
2546 u = entry->u;
2547 lines = u->lines;
2549 /* Skip units with no useful line number information by walking
2550 backward. Useless line number information is marked by setting
2551 lines == -1. */
2552 while (entry > ddata->addrs
2553 && pc >= (entry - 1)->low
2554 && pc < (entry - 1)->high)
2556 if (state->threaded)
2558 /* Use __sync_bool_compare_and_swap to do a
2559 load-acquire. */
2560 while (!__sync_bool_compare_and_swap (&u->lines, lines, lines))
2561 lines = u->lines;
2564 if (lines != (struct line *) (uintptr_t) -1)
2565 break;
2567 --entry;
2569 u = entry->u;
2570 lines = u->lines;
2573 /* Do a load-acquire of u->lines. */
2574 if (state->threaded)
2576 /* Use __sync_bool_compare_and_swap to do an atomic load. */
2577 while (!__sync_bool_compare_and_swap (&u->lines, lines, lines))
2578 lines = u->lines;
2581 new_data = 0;
2582 if (lines == NULL)
2584 size_t function_addrs_count;
2585 struct line_header lhdr;
2586 size_t count;
2588 /* We have never read the line information for this unit. Read
2589 it now. */
2591 function_addrs = NULL;
2592 function_addrs_count = 0;
2593 if (read_line_info (state, ddata, error_callback, data, entry->u, &lhdr,
2594 &lines, &count))
2596 read_function_info (state, ddata, &lhdr, error_callback, data,
2597 entry->u, &ddata->fvec, &function_addrs,
2598 &function_addrs_count);
2599 free_line_header (state, &lhdr, error_callback, data);
2600 new_data = 1;
2603 /* Atomically store the information we just read into the unit.
2604 If another thread is simultaneously writing, it presumably
2605 read the same information, and we don't care which one we
2606 wind up with; we just leak the other one. We do have to
2607 write the lines field last, so that the acquire-loads above
2608 ensure that the other fields are set. */
2610 if (!state->threaded)
2612 u->lines_count = count;
2613 u->function_addrs = function_addrs;
2614 u->function_addrs_count = function_addrs_count;
2615 u->lines = lines;
2617 else
2619 __sync_bool_compare_and_swap (&u->lines_count, 0, count);
2620 __sync_bool_compare_and_swap (&u->function_addrs, NULL,
2621 function_addrs);
2622 __sync_bool_compare_and_swap (&u->function_addrs_count, 0,
2623 function_addrs_count);
2624 __sync_bool_compare_and_swap (&u->lines, NULL, lines);
2628 /* Now all fields of U have been initialized. */
2630 if (lines == (struct line *) (uintptr_t) -1)
2632 /* If reading the line number information failed in some way,
2633 try again to see if there is a better compilation unit for
2634 this PC. */
2635 if (new_data)
2636 dwarf_fileline (state, pc, callback, error_callback, data);
2637 return callback (data, pc, NULL, 0, NULL);
2640 /* Search for PC within this unit. */
2642 ln = (struct line *) bsearch (&pc, lines, entry->u->lines_count,
2643 sizeof (struct line), line_search);
2644 if (ln == NULL)
2646 error_callback (data, "inconsistent DWARF line number info", 0);
2647 return 0;
2650 /* Search for function name within this unit. */
2652 if (entry->u->function_addrs_count == 0)
2653 return callback (data, pc, ln->filename, ln->lineno, NULL);
2655 function_addrs = ((struct function_addrs *)
2656 bsearch (&pc, entry->u->function_addrs,
2657 entry->u->function_addrs_count,
2658 sizeof (struct function_addrs),
2659 function_addrs_search));
2660 if (function_addrs == NULL)
2661 return callback (data, pc, ln->filename, ln->lineno, NULL);
2663 /* If there are multiple function ranges that contain PC, use the
2664 last one, in order to produce predictable results. */
2666 while (((size_t) (function_addrs - entry->u->function_addrs + 1)
2667 < entry->u->function_addrs_count)
2668 && pc >= (function_addrs + 1)->low
2669 && pc < (function_addrs + 1)->high)
2670 ++function_addrs;
2672 function = function_addrs->function;
2674 filename = ln->filename;
2675 lineno = ln->lineno;
2677 ret = report_inlined_functions (pc, function, callback, data,
2678 &filename, &lineno);
2679 if (ret != 0)
2680 return ret;
2682 return callback (data, pc, filename, lineno, function->name);
2685 /* Build our data structures from the .debug_info and .debug_line
2686 sections. Set *FILELINE_FN and *FILELINE_DATA. Return 1 on
2687 success, 0 on failure. */
2690 backtrace_dwarf_initialize (struct backtrace_state *state,
2691 const unsigned char *dwarf_info,
2692 size_t dwarf_info_size,
2693 const unsigned char *dwarf_line,
2694 size_t dwarf_line_size,
2695 const unsigned char *dwarf_abbrev,
2696 size_t dwarf_abbrev_size,
2697 const unsigned char *dwarf_ranges,
2698 size_t dwarf_ranges_size,
2699 const unsigned char *dwarf_str,
2700 size_t dwarf_str_size,
2701 int is_bigendian,
2702 backtrace_error_callback error_callback,
2703 void *data, fileline *fileline_fn)
2705 struct unit_addrs_vector addrs_vec;
2706 struct unit_addrs *addrs;
2707 size_t addrs_count;
2708 struct dwarf_data *fdata;
2710 if (!build_address_map (state, dwarf_info, dwarf_info_size, dwarf_abbrev,
2711 dwarf_abbrev_size, dwarf_ranges, dwarf_ranges_size,
2712 dwarf_str, dwarf_str_size, is_bigendian,
2713 error_callback, data, &addrs_vec))
2714 return 0;
2716 if (!backtrace_vector_release (state, &addrs_vec.vec, error_callback, data))
2717 return 0;
2718 addrs = (struct unit_addrs *) addrs_vec.vec.base;
2719 addrs_count = addrs_vec.count;
2720 qsort (addrs, addrs_count, sizeof (struct unit_addrs), unit_addrs_compare);
2722 fdata = ((struct dwarf_data *)
2723 backtrace_alloc (state, sizeof (struct dwarf_data),
2724 error_callback, data));
2725 if (fdata == NULL)
2726 return 0;
2728 fdata->addrs = addrs;
2729 fdata->addrs_count = addrs_count;
2730 fdata->dwarf_info = dwarf_info;
2731 fdata->dwarf_info_size = dwarf_info_size;
2732 fdata->dwarf_line = dwarf_line;
2733 fdata->dwarf_line_size = dwarf_line_size;
2734 fdata->dwarf_ranges = dwarf_ranges;
2735 fdata->dwarf_ranges_size = dwarf_ranges_size;
2736 fdata->dwarf_str = dwarf_str;
2737 fdata->dwarf_str_size = dwarf_str_size;
2738 fdata->is_bigendian = is_bigendian;
2739 memset (&fdata->fvec, 0, sizeof fdata->fvec);
2741 state->fileline_data = fdata;
2743 *fileline_fn = dwarf_fileline;
2745 return 1;