c++: Fix g++.dg/ext/sve-sizeless-1.C regression
[official-gcc.git] / libbacktrace / dwarf.c
blob96ffc4cc481b69fe6e4742f58866d9ca58e5999f
1 /* dwarf.c -- Get file/line information from DWARF for backtraces.
2 Copyright (C) 2012-2024 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;
95 /* The attribute value, for DW_FORM_implicit_const. */
96 int64_t val;
99 /* A single DWARF abbreviation. */
101 struct abbrev
103 /* The abbrev code--the number used to refer to the abbrev. */
104 uint64_t code;
105 /* The entry tag. */
106 enum dwarf_tag tag;
107 /* Non-zero if this abbrev has child entries. */
108 int has_children;
109 /* The number of attributes. */
110 size_t num_attrs;
111 /* The attributes. */
112 struct attr *attrs;
115 /* The DWARF abbreviations for a compilation unit. This structure
116 only exists while reading the compilation unit. Most DWARF readers
117 seem to a hash table to map abbrev ID's to abbrev entries.
118 However, we primarily care about GCC, and GCC simply issues ID's in
119 numerical order starting at 1. So we simply keep a sorted vector,
120 and try to just look up the code. */
122 struct abbrevs
124 /* The number of abbrevs in the vector. */
125 size_t num_abbrevs;
126 /* The abbrevs, sorted by the code field. */
127 struct abbrev *abbrevs;
130 /* The different kinds of attribute values. */
132 enum attr_val_encoding
134 /* No attribute value. */
135 ATTR_VAL_NONE,
136 /* An address. */
137 ATTR_VAL_ADDRESS,
138 /* An index into the .debug_addr section, whose value is relative to
139 the DW_AT_addr_base attribute of the compilation unit. */
140 ATTR_VAL_ADDRESS_INDEX,
141 /* A unsigned integer. */
142 ATTR_VAL_UINT,
143 /* A sigd integer. */
144 ATTR_VAL_SINT,
145 /* A string. */
146 ATTR_VAL_STRING,
147 /* An index into the .debug_str_offsets section. */
148 ATTR_VAL_STRING_INDEX,
149 /* An offset to other data in the containing unit. */
150 ATTR_VAL_REF_UNIT,
151 /* An offset to other data within the .debug_info section. */
152 ATTR_VAL_REF_INFO,
153 /* An offset to other data within the alt .debug_info section. */
154 ATTR_VAL_REF_ALT_INFO,
155 /* An offset to data in some other section. */
156 ATTR_VAL_REF_SECTION,
157 /* A type signature. */
158 ATTR_VAL_REF_TYPE,
159 /* An index into the .debug_rnglists section. */
160 ATTR_VAL_RNGLISTS_INDEX,
161 /* A block of data (not represented). */
162 ATTR_VAL_BLOCK,
163 /* An expression (not represented). */
164 ATTR_VAL_EXPR,
167 /* An attribute value. */
169 struct attr_val
171 /* How the value is stored in the field u. */
172 enum attr_val_encoding encoding;
173 union
175 /* ATTR_VAL_ADDRESS*, ATTR_VAL_UINT, ATTR_VAL_REF*. */
176 uint64_t uint;
177 /* ATTR_VAL_SINT. */
178 int64_t sint;
179 /* ATTR_VAL_STRING. */
180 const char *string;
181 /* ATTR_VAL_BLOCK not stored. */
182 } u;
185 /* The line number program header. */
187 struct line_header
189 /* The version of the line number information. */
190 int version;
191 /* Address size. */
192 int addrsize;
193 /* The minimum instruction length. */
194 unsigned int min_insn_len;
195 /* The maximum number of ops per instruction. */
196 unsigned int max_ops_per_insn;
197 /* The line base for special opcodes. */
198 int line_base;
199 /* The line range for special opcodes. */
200 unsigned int line_range;
201 /* The opcode base--the first special opcode. */
202 unsigned int opcode_base;
203 /* Opcode lengths, indexed by opcode - 1. */
204 const unsigned char *opcode_lengths;
205 /* The number of directory entries. */
206 size_t dirs_count;
207 /* The directory entries. */
208 const char **dirs;
209 /* The number of filenames. */
210 size_t filenames_count;
211 /* The filenames. */
212 const char **filenames;
215 /* A format description from a line header. */
217 struct line_header_format
219 int lnct; /* LNCT code. */
220 enum dwarf_form form; /* Form of entry data. */
223 /* Map a single PC value to a file/line. We will keep a vector of
224 these sorted by PC value. Each file/line will be correct from the
225 PC up to the PC of the next entry if there is one. We allocate one
226 extra entry at the end so that we can use bsearch. */
228 struct line
230 /* PC. */
231 uintptr_t pc;
232 /* File name. Many entries in the array are expected to point to
233 the same file name. */
234 const char *filename;
235 /* Line number. */
236 int lineno;
237 /* Index of the object in the original array read from the DWARF
238 section, before it has been sorted. The index makes it possible
239 to use Quicksort and maintain stability. */
240 int idx;
243 /* A growable vector of line number information. This is used while
244 reading the line numbers. */
246 struct line_vector
248 /* Memory. This is an array of struct line. */
249 struct backtrace_vector vec;
250 /* Number of valid mappings. */
251 size_t count;
254 /* A function described in the debug info. */
256 struct function
258 /* The name of the function. */
259 const char *name;
260 /* If this is an inlined function, the filename of the call
261 site. */
262 const char *caller_filename;
263 /* If this is an inlined function, the line number of the call
264 site. */
265 int caller_lineno;
266 /* Map PC ranges to inlined functions. */
267 struct function_addrs *function_addrs;
268 size_t function_addrs_count;
271 /* An address range for a function. This maps a PC value to a
272 specific function. */
274 struct function_addrs
276 /* Range is LOW <= PC < HIGH. */
277 uintptr_t low;
278 uintptr_t high;
279 /* Function for this address range. */
280 struct function *function;
283 /* A growable vector of function address ranges. */
285 struct function_vector
287 /* Memory. This is an array of struct function_addrs. */
288 struct backtrace_vector vec;
289 /* Number of address ranges present. */
290 size_t count;
293 /* A DWARF compilation unit. This only holds the information we need
294 to map a PC to a file and line. */
296 struct unit
298 /* The first entry for this compilation unit. */
299 const unsigned char *unit_data;
300 /* The length of the data for this compilation unit. */
301 size_t unit_data_len;
302 /* The offset of UNIT_DATA from the start of the information for
303 this compilation unit. */
304 size_t unit_data_offset;
305 /* Offset of the start of the compilation unit from the start of the
306 .debug_info section. */
307 size_t low_offset;
308 /* Offset of the end of the compilation unit from the start of the
309 .debug_info section. */
310 size_t high_offset;
311 /* DWARF version. */
312 int version;
313 /* Whether unit is DWARF64. */
314 int is_dwarf64;
315 /* Address size. */
316 int addrsize;
317 /* Offset into line number information. */
318 off_t lineoff;
319 /* Offset of compilation unit in .debug_str_offsets. */
320 uint64_t str_offsets_base;
321 /* Offset of compilation unit in .debug_addr. */
322 uint64_t addr_base;
323 /* Offset of compilation unit in .debug_rnglists. */
324 uint64_t rnglists_base;
325 /* Primary source file. */
326 const char *filename;
327 /* Compilation command working directory. */
328 const char *comp_dir;
329 /* Absolute file name, only set if needed. */
330 const char *abs_filename;
331 /* The abbreviations for this unit. */
332 struct abbrevs abbrevs;
334 /* The fields above this point are read in during initialization and
335 may be accessed freely. The fields below this point are read in
336 as needed, and therefore require care, as different threads may
337 try to initialize them simultaneously. */
339 /* PC to line number mapping. This is NULL if the values have not
340 been read. This is (struct line *) -1 if there was an error
341 reading the values. */
342 struct line *lines;
343 /* Number of entries in lines. */
344 size_t lines_count;
345 /* PC ranges to function. */
346 struct function_addrs *function_addrs;
347 size_t function_addrs_count;
350 /* An address range for a compilation unit. This maps a PC value to a
351 specific compilation unit. Note that we invert the representation
352 in DWARF: instead of listing the units and attaching a list of
353 ranges, we list the ranges and have each one point to the unit.
354 This lets us do a binary search to find the unit. */
356 struct unit_addrs
358 /* Range is LOW <= PC < HIGH. */
359 uintptr_t low;
360 uintptr_t high;
361 /* Compilation unit for this address range. */
362 struct unit *u;
365 /* A growable vector of compilation unit address ranges. */
367 struct unit_addrs_vector
369 /* Memory. This is an array of struct unit_addrs. */
370 struct backtrace_vector vec;
371 /* Number of address ranges present. */
372 size_t count;
375 /* A growable vector of compilation unit pointer. */
377 struct unit_vector
379 struct backtrace_vector vec;
380 size_t count;
383 /* The information we need to map a PC to a file and line. */
385 struct dwarf_data
387 /* The data for the next file we know about. */
388 struct dwarf_data *next;
389 /* The data for .gnu_debugaltlink. */
390 struct dwarf_data *altlink;
391 /* The base address mapping for this file. */
392 struct libbacktrace_base_address base_address;
393 /* A sorted list of address ranges. */
394 struct unit_addrs *addrs;
395 /* Number of address ranges in list. */
396 size_t addrs_count;
397 /* A sorted list of units. */
398 struct unit **units;
399 /* Number of units in the list. */
400 size_t units_count;
401 /* The unparsed DWARF debug data. */
402 struct dwarf_sections dwarf_sections;
403 /* Whether the data is big-endian or not. */
404 int is_bigendian;
405 /* A vector used for function addresses. We keep this here so that
406 we can grow the vector as we read more functions. */
407 struct function_vector fvec;
410 /* Report an error for a DWARF buffer. */
412 static void
413 dwarf_buf_error (struct dwarf_buf *buf, const char *msg, int errnum)
415 char b[200];
417 snprintf (b, sizeof b, "%s in %s at %d",
418 msg, buf->name, (int) (buf->buf - buf->start));
419 buf->error_callback (buf->data, b, errnum);
422 /* Require at least COUNT bytes in BUF. Return 1 if all is well, 0 on
423 error. */
425 static int
426 require (struct dwarf_buf *buf, size_t count)
428 if (buf->left >= count)
429 return 1;
431 if (!buf->reported_underflow)
433 dwarf_buf_error (buf, "DWARF underflow", 0);
434 buf->reported_underflow = 1;
437 return 0;
440 /* Advance COUNT bytes in BUF. Return 1 if all is well, 0 on
441 error. */
443 static int
444 advance (struct dwarf_buf *buf, size_t count)
446 if (!require (buf, count))
447 return 0;
448 buf->buf += count;
449 buf->left -= count;
450 return 1;
453 /* Read one zero-terminated string from BUF and advance past the string. */
455 static const char *
456 read_string (struct dwarf_buf *buf)
458 const char *p = (const char *)buf->buf;
459 size_t len = strnlen (p, buf->left);
461 /* - If len == left, we ran out of buffer before finding the zero terminator.
462 Generate an error by advancing len + 1.
463 - If len < left, advance by len + 1 to skip past the zero terminator. */
464 size_t count = len + 1;
466 if (!advance (buf, count))
467 return NULL;
469 return p;
472 /* Read one byte from BUF and advance 1 byte. */
474 static unsigned char
475 read_byte (struct dwarf_buf *buf)
477 const unsigned char *p = buf->buf;
479 if (!advance (buf, 1))
480 return 0;
481 return p[0];
484 /* Read a signed char from BUF and advance 1 byte. */
486 static signed char
487 read_sbyte (struct dwarf_buf *buf)
489 const unsigned char *p = buf->buf;
491 if (!advance (buf, 1))
492 return 0;
493 return (*p ^ 0x80) - 0x80;
496 /* Read a uint16 from BUF and advance 2 bytes. */
498 static uint16_t
499 read_uint16 (struct dwarf_buf *buf)
501 const unsigned char *p = buf->buf;
503 if (!advance (buf, 2))
504 return 0;
505 if (buf->is_bigendian)
506 return ((uint16_t) p[0] << 8) | (uint16_t) p[1];
507 else
508 return ((uint16_t) p[1] << 8) | (uint16_t) p[0];
511 /* Read a 24 bit value from BUF and advance 3 bytes. */
513 static uint32_t
514 read_uint24 (struct dwarf_buf *buf)
516 const unsigned char *p = buf->buf;
518 if (!advance (buf, 3))
519 return 0;
520 if (buf->is_bigendian)
521 return (((uint32_t) p[0] << 16) | ((uint32_t) p[1] << 8)
522 | (uint32_t) p[2]);
523 else
524 return (((uint32_t) p[2] << 16) | ((uint32_t) p[1] << 8)
525 | (uint32_t) p[0]);
528 /* Read a uint32 from BUF and advance 4 bytes. */
530 static uint32_t
531 read_uint32 (struct dwarf_buf *buf)
533 const unsigned char *p = buf->buf;
535 if (!advance (buf, 4))
536 return 0;
537 if (buf->is_bigendian)
538 return (((uint32_t) p[0] << 24) | ((uint32_t) p[1] << 16)
539 | ((uint32_t) p[2] << 8) | (uint32_t) p[3]);
540 else
541 return (((uint32_t) p[3] << 24) | ((uint32_t) p[2] << 16)
542 | ((uint32_t) p[1] << 8) | (uint32_t) p[0]);
545 /* Read a uint64 from BUF and advance 8 bytes. */
547 static uint64_t
548 read_uint64 (struct dwarf_buf *buf)
550 const unsigned char *p = buf->buf;
552 if (!advance (buf, 8))
553 return 0;
554 if (buf->is_bigendian)
555 return (((uint64_t) p[0] << 56) | ((uint64_t) p[1] << 48)
556 | ((uint64_t) p[2] << 40) | ((uint64_t) p[3] << 32)
557 | ((uint64_t) p[4] << 24) | ((uint64_t) p[5] << 16)
558 | ((uint64_t) p[6] << 8) | (uint64_t) p[7]);
559 else
560 return (((uint64_t) p[7] << 56) | ((uint64_t) p[6] << 48)
561 | ((uint64_t) p[5] << 40) | ((uint64_t) p[4] << 32)
562 | ((uint64_t) p[3] << 24) | ((uint64_t) p[2] << 16)
563 | ((uint64_t) p[1] << 8) | (uint64_t) p[0]);
566 /* Read an offset from BUF and advance the appropriate number of
567 bytes. */
569 static uint64_t
570 read_offset (struct dwarf_buf *buf, int is_dwarf64)
572 if (is_dwarf64)
573 return read_uint64 (buf);
574 else
575 return read_uint32 (buf);
578 /* Read an address from BUF and advance the appropriate number of
579 bytes. */
581 static uint64_t
582 read_address (struct dwarf_buf *buf, int addrsize)
584 switch (addrsize)
586 case 1:
587 return read_byte (buf);
588 case 2:
589 return read_uint16 (buf);
590 case 4:
591 return read_uint32 (buf);
592 case 8:
593 return read_uint64 (buf);
594 default:
595 dwarf_buf_error (buf, "unrecognized address size", 0);
596 return 0;
600 /* Return whether a value is the highest possible address, given the
601 address size. */
603 static int
604 is_highest_address (uint64_t address, int addrsize)
606 switch (addrsize)
608 case 1:
609 return address == (unsigned char) -1;
610 case 2:
611 return address == (uint16_t) -1;
612 case 4:
613 return address == (uint32_t) -1;
614 case 8:
615 return address == (uint64_t) -1;
616 default:
617 return 0;
621 /* Read an unsigned LEB128 number. */
623 static uint64_t
624 read_uleb128 (struct dwarf_buf *buf)
626 uint64_t ret;
627 unsigned int shift;
628 int overflow;
629 unsigned char b;
631 ret = 0;
632 shift = 0;
633 overflow = 0;
636 const unsigned char *p;
638 p = buf->buf;
639 if (!advance (buf, 1))
640 return 0;
641 b = *p;
642 if (shift < 64)
643 ret |= ((uint64_t) (b & 0x7f)) << shift;
644 else if (!overflow)
646 dwarf_buf_error (buf, "LEB128 overflows uint64_t", 0);
647 overflow = 1;
649 shift += 7;
651 while ((b & 0x80) != 0);
653 return ret;
656 /* Read a signed LEB128 number. */
658 static int64_t
659 read_sleb128 (struct dwarf_buf *buf)
661 uint64_t val;
662 unsigned int shift;
663 int overflow;
664 unsigned char b;
666 val = 0;
667 shift = 0;
668 overflow = 0;
671 const unsigned char *p;
673 p = buf->buf;
674 if (!advance (buf, 1))
675 return 0;
676 b = *p;
677 if (shift < 64)
678 val |= ((uint64_t) (b & 0x7f)) << shift;
679 else if (!overflow)
681 dwarf_buf_error (buf, "signed LEB128 overflows uint64_t", 0);
682 overflow = 1;
684 shift += 7;
686 while ((b & 0x80) != 0);
688 if ((b & 0x40) != 0 && shift < 64)
689 val |= ((uint64_t) -1) << shift;
691 return (int64_t) val;
694 /* Return the length of an LEB128 number. */
696 static size_t
697 leb128_len (const unsigned char *p)
699 size_t ret;
701 ret = 1;
702 while ((*p & 0x80) != 0)
704 ++p;
705 ++ret;
707 return ret;
710 /* Read initial_length from BUF and advance the appropriate number of bytes. */
712 static uint64_t
713 read_initial_length (struct dwarf_buf *buf, int *is_dwarf64)
715 uint64_t len;
717 len = read_uint32 (buf);
718 if (len == 0xffffffff)
720 len = read_uint64 (buf);
721 *is_dwarf64 = 1;
723 else
724 *is_dwarf64 = 0;
726 return len;
729 /* Free an abbreviations structure. */
731 static void
732 free_abbrevs (struct backtrace_state *state, struct abbrevs *abbrevs,
733 backtrace_error_callback error_callback, void *data)
735 size_t i;
737 for (i = 0; i < abbrevs->num_abbrevs; ++i)
738 backtrace_free (state, abbrevs->abbrevs[i].attrs,
739 abbrevs->abbrevs[i].num_attrs * sizeof (struct attr),
740 error_callback, data);
741 backtrace_free (state, abbrevs->abbrevs,
742 abbrevs->num_abbrevs * sizeof (struct abbrev),
743 error_callback, data);
744 abbrevs->num_abbrevs = 0;
745 abbrevs->abbrevs = NULL;
748 /* Read an attribute value. Returns 1 on success, 0 on failure. If
749 the value can be represented as a uint64_t, sets *VAL and sets
750 *IS_VALID to 1. We don't try to store the value of other attribute
751 forms, because we don't care about them. */
753 static int
754 read_attribute (enum dwarf_form form, uint64_t implicit_val,
755 struct dwarf_buf *buf, int is_dwarf64, int version,
756 int addrsize, const struct dwarf_sections *dwarf_sections,
757 struct dwarf_data *altlink, struct attr_val *val)
759 /* Avoid warnings about val.u.FIELD may be used uninitialized if
760 this function is inlined. The warnings aren't valid but can
761 occur because the different fields are set and used
762 conditionally. */
763 memset (val, 0, sizeof *val);
765 switch (form)
767 case DW_FORM_addr:
768 val->encoding = ATTR_VAL_ADDRESS;
769 val->u.uint = read_address (buf, addrsize);
770 return 1;
771 case DW_FORM_block2:
772 val->encoding = ATTR_VAL_BLOCK;
773 return advance (buf, read_uint16 (buf));
774 case DW_FORM_block4:
775 val->encoding = ATTR_VAL_BLOCK;
776 return advance (buf, read_uint32 (buf));
777 case DW_FORM_data2:
778 val->encoding = ATTR_VAL_UINT;
779 val->u.uint = read_uint16 (buf);
780 return 1;
781 case DW_FORM_data4:
782 val->encoding = ATTR_VAL_UINT;
783 val->u.uint = read_uint32 (buf);
784 return 1;
785 case DW_FORM_data8:
786 val->encoding = ATTR_VAL_UINT;
787 val->u.uint = read_uint64 (buf);
788 return 1;
789 case DW_FORM_data16:
790 val->encoding = ATTR_VAL_BLOCK;
791 return advance (buf, 16);
792 case DW_FORM_string:
793 val->encoding = ATTR_VAL_STRING;
794 val->u.string = read_string (buf);
795 return val->u.string == NULL ? 0 : 1;
796 case DW_FORM_block:
797 val->encoding = ATTR_VAL_BLOCK;
798 return advance (buf, read_uleb128 (buf));
799 case DW_FORM_block1:
800 val->encoding = ATTR_VAL_BLOCK;
801 return advance (buf, read_byte (buf));
802 case DW_FORM_data1:
803 val->encoding = ATTR_VAL_UINT;
804 val->u.uint = read_byte (buf);
805 return 1;
806 case DW_FORM_flag:
807 val->encoding = ATTR_VAL_UINT;
808 val->u.uint = read_byte (buf);
809 return 1;
810 case DW_FORM_sdata:
811 val->encoding = ATTR_VAL_SINT;
812 val->u.sint = read_sleb128 (buf);
813 return 1;
814 case DW_FORM_strp:
816 uint64_t offset;
818 offset = read_offset (buf, is_dwarf64);
819 if (offset >= dwarf_sections->size[DEBUG_STR])
821 dwarf_buf_error (buf, "DW_FORM_strp out of range", 0);
822 return 0;
824 val->encoding = ATTR_VAL_STRING;
825 val->u.string =
826 (const char *) dwarf_sections->data[DEBUG_STR] + offset;
827 return 1;
829 case DW_FORM_line_strp:
831 uint64_t offset;
833 offset = read_offset (buf, is_dwarf64);
834 if (offset >= dwarf_sections->size[DEBUG_LINE_STR])
836 dwarf_buf_error (buf, "DW_FORM_line_strp out of range", 0);
837 return 0;
839 val->encoding = ATTR_VAL_STRING;
840 val->u.string =
841 (const char *) dwarf_sections->data[DEBUG_LINE_STR] + offset;
842 return 1;
844 case DW_FORM_udata:
845 val->encoding = ATTR_VAL_UINT;
846 val->u.uint = read_uleb128 (buf);
847 return 1;
848 case DW_FORM_ref_addr:
849 val->encoding = ATTR_VAL_REF_INFO;
850 if (version == 2)
851 val->u.uint = read_address (buf, addrsize);
852 else
853 val->u.uint = read_offset (buf, is_dwarf64);
854 return 1;
855 case DW_FORM_ref1:
856 val->encoding = ATTR_VAL_REF_UNIT;
857 val->u.uint = read_byte (buf);
858 return 1;
859 case DW_FORM_ref2:
860 val->encoding = ATTR_VAL_REF_UNIT;
861 val->u.uint = read_uint16 (buf);
862 return 1;
863 case DW_FORM_ref4:
864 val->encoding = ATTR_VAL_REF_UNIT;
865 val->u.uint = read_uint32 (buf);
866 return 1;
867 case DW_FORM_ref8:
868 val->encoding = ATTR_VAL_REF_UNIT;
869 val->u.uint = read_uint64 (buf);
870 return 1;
871 case DW_FORM_ref_udata:
872 val->encoding = ATTR_VAL_REF_UNIT;
873 val->u.uint = read_uleb128 (buf);
874 return 1;
875 case DW_FORM_indirect:
877 uint64_t form;
879 form = read_uleb128 (buf);
880 if (form == DW_FORM_implicit_const)
882 dwarf_buf_error (buf,
883 "DW_FORM_indirect to DW_FORM_implicit_const",
885 return 0;
887 return read_attribute ((enum dwarf_form) form, 0, buf, is_dwarf64,
888 version, addrsize, dwarf_sections, altlink,
889 val);
891 case DW_FORM_sec_offset:
892 val->encoding = ATTR_VAL_REF_SECTION;
893 val->u.uint = read_offset (buf, is_dwarf64);
894 return 1;
895 case DW_FORM_exprloc:
896 val->encoding = ATTR_VAL_EXPR;
897 return advance (buf, read_uleb128 (buf));
898 case DW_FORM_flag_present:
899 val->encoding = ATTR_VAL_UINT;
900 val->u.uint = 1;
901 return 1;
902 case DW_FORM_ref_sig8:
903 val->encoding = ATTR_VAL_REF_TYPE;
904 val->u.uint = read_uint64 (buf);
905 return 1;
906 case DW_FORM_strx: case DW_FORM_strx1: case DW_FORM_strx2:
907 case DW_FORM_strx3: case DW_FORM_strx4:
909 uint64_t offset;
911 switch (form)
913 case DW_FORM_strx:
914 offset = read_uleb128 (buf);
915 break;
916 case DW_FORM_strx1:
917 offset = read_byte (buf);
918 break;
919 case DW_FORM_strx2:
920 offset = read_uint16 (buf);
921 break;
922 case DW_FORM_strx3:
923 offset = read_uint24 (buf);
924 break;
925 case DW_FORM_strx4:
926 offset = read_uint32 (buf);
927 break;
928 default:
929 /* This case can't happen. */
930 return 0;
932 val->encoding = ATTR_VAL_STRING_INDEX;
933 val->u.uint = offset;
934 return 1;
936 case DW_FORM_addrx: case DW_FORM_addrx1: case DW_FORM_addrx2:
937 case DW_FORM_addrx3: case DW_FORM_addrx4:
939 uint64_t offset;
941 switch (form)
943 case DW_FORM_addrx:
944 offset = read_uleb128 (buf);
945 break;
946 case DW_FORM_addrx1:
947 offset = read_byte (buf);
948 break;
949 case DW_FORM_addrx2:
950 offset = read_uint16 (buf);
951 break;
952 case DW_FORM_addrx3:
953 offset = read_uint24 (buf);
954 break;
955 case DW_FORM_addrx4:
956 offset = read_uint32 (buf);
957 break;
958 default:
959 /* This case can't happen. */
960 return 0;
962 val->encoding = ATTR_VAL_ADDRESS_INDEX;
963 val->u.uint = offset;
964 return 1;
966 case DW_FORM_ref_sup4:
967 val->encoding = ATTR_VAL_REF_SECTION;
968 val->u.uint = read_uint32 (buf);
969 return 1;
970 case DW_FORM_ref_sup8:
971 val->encoding = ATTR_VAL_REF_SECTION;
972 val->u.uint = read_uint64 (buf);
973 return 1;
974 case DW_FORM_implicit_const:
975 val->encoding = ATTR_VAL_UINT;
976 val->u.uint = implicit_val;
977 return 1;
978 case DW_FORM_loclistx:
979 /* We don't distinguish this from DW_FORM_sec_offset. It
980 * shouldn't matter since we don't care about loclists. */
981 val->encoding = ATTR_VAL_REF_SECTION;
982 val->u.uint = read_uleb128 (buf);
983 return 1;
984 case DW_FORM_rnglistx:
985 val->encoding = ATTR_VAL_RNGLISTS_INDEX;
986 val->u.uint = read_uleb128 (buf);
987 return 1;
988 case DW_FORM_GNU_addr_index:
989 val->encoding = ATTR_VAL_REF_SECTION;
990 val->u.uint = read_uleb128 (buf);
991 return 1;
992 case DW_FORM_GNU_str_index:
993 val->encoding = ATTR_VAL_REF_SECTION;
994 val->u.uint = read_uleb128 (buf);
995 return 1;
996 case DW_FORM_GNU_ref_alt:
997 val->u.uint = read_offset (buf, is_dwarf64);
998 if (altlink == NULL)
1000 val->encoding = ATTR_VAL_NONE;
1001 return 1;
1003 val->encoding = ATTR_VAL_REF_ALT_INFO;
1004 return 1;
1005 case DW_FORM_strp_sup: case DW_FORM_GNU_strp_alt:
1007 uint64_t offset;
1009 offset = read_offset (buf, is_dwarf64);
1010 if (altlink == NULL)
1012 val->encoding = ATTR_VAL_NONE;
1013 return 1;
1015 if (offset >= altlink->dwarf_sections.size[DEBUG_STR])
1017 dwarf_buf_error (buf, "DW_FORM_strp_sup out of range", 0);
1018 return 0;
1020 val->encoding = ATTR_VAL_STRING;
1021 val->u.string =
1022 (const char *) altlink->dwarf_sections.data[DEBUG_STR] + offset;
1023 return 1;
1025 default:
1026 dwarf_buf_error (buf, "unrecognized DWARF form", -1);
1027 return 0;
1031 /* If we can determine the value of a string attribute, set *STRING to
1032 point to the string. Return 1 on success, 0 on error. If we don't
1033 know the value, we consider that a success, and we don't change
1034 *STRING. An error is only reported for some sort of out of range
1035 offset. */
1037 static int
1038 resolve_string (const struct dwarf_sections *dwarf_sections, int is_dwarf64,
1039 int is_bigendian, uint64_t str_offsets_base,
1040 const struct attr_val *val,
1041 backtrace_error_callback error_callback, void *data,
1042 const char **string)
1044 switch (val->encoding)
1046 case ATTR_VAL_STRING:
1047 *string = val->u.string;
1048 return 1;
1050 case ATTR_VAL_STRING_INDEX:
1052 uint64_t offset;
1053 struct dwarf_buf offset_buf;
1055 offset = val->u.uint * (is_dwarf64 ? 8 : 4) + str_offsets_base;
1056 if (offset + (is_dwarf64 ? 8 : 4)
1057 > dwarf_sections->size[DEBUG_STR_OFFSETS])
1059 error_callback (data, "DW_FORM_strx value out of range", 0);
1060 return 0;
1063 offset_buf.name = ".debug_str_offsets";
1064 offset_buf.start = dwarf_sections->data[DEBUG_STR_OFFSETS];
1065 offset_buf.buf = dwarf_sections->data[DEBUG_STR_OFFSETS] + offset;
1066 offset_buf.left = dwarf_sections->size[DEBUG_STR_OFFSETS] - offset;
1067 offset_buf.is_bigendian = is_bigendian;
1068 offset_buf.error_callback = error_callback;
1069 offset_buf.data = data;
1070 offset_buf.reported_underflow = 0;
1072 offset = read_offset (&offset_buf, is_dwarf64);
1073 if (offset >= dwarf_sections->size[DEBUG_STR])
1075 dwarf_buf_error (&offset_buf,
1076 "DW_FORM_strx offset out of range",
1078 return 0;
1080 *string = (const char *) dwarf_sections->data[DEBUG_STR] + offset;
1081 return 1;
1084 default:
1085 return 1;
1089 /* Set *ADDRESS to the real address for a ATTR_VAL_ADDRESS_INDEX.
1090 Return 1 on success, 0 on error. */
1092 static int
1093 resolve_addr_index (const struct dwarf_sections *dwarf_sections,
1094 uint64_t addr_base, int addrsize, int is_bigendian,
1095 uint64_t addr_index,
1096 backtrace_error_callback error_callback, void *data,
1097 uintptr_t *address)
1099 uint64_t offset;
1100 struct dwarf_buf addr_buf;
1102 offset = addr_index * addrsize + addr_base;
1103 if (offset + addrsize > dwarf_sections->size[DEBUG_ADDR])
1105 error_callback (data, "DW_FORM_addrx value out of range", 0);
1106 return 0;
1109 addr_buf.name = ".debug_addr";
1110 addr_buf.start = dwarf_sections->data[DEBUG_ADDR];
1111 addr_buf.buf = dwarf_sections->data[DEBUG_ADDR] + offset;
1112 addr_buf.left = dwarf_sections->size[DEBUG_ADDR] - offset;
1113 addr_buf.is_bigendian = is_bigendian;
1114 addr_buf.error_callback = error_callback;
1115 addr_buf.data = data;
1116 addr_buf.reported_underflow = 0;
1118 *address = (uintptr_t) read_address (&addr_buf, addrsize);
1119 return 1;
1122 /* Compare a unit offset against a unit for bsearch. */
1124 static int
1125 units_search (const void *vkey, const void *ventry)
1127 const size_t *key = (const size_t *) vkey;
1128 const struct unit *entry = *((const struct unit *const *) ventry);
1129 size_t offset;
1131 offset = *key;
1132 if (offset < entry->low_offset)
1133 return -1;
1134 else if (offset >= entry->high_offset)
1135 return 1;
1136 else
1137 return 0;
1140 /* Find a unit in PU containing OFFSET. */
1142 static struct unit *
1143 find_unit (struct unit **pu, size_t units_count, size_t offset)
1145 struct unit **u;
1146 u = bsearch (&offset, pu, units_count, sizeof (struct unit *), units_search);
1147 return u == NULL ? NULL : *u;
1150 /* Compare function_addrs for qsort. When ranges are nested, make the
1151 smallest one sort last. */
1153 static int
1154 function_addrs_compare (const void *v1, const void *v2)
1156 const struct function_addrs *a1 = (const struct function_addrs *) v1;
1157 const struct function_addrs *a2 = (const struct function_addrs *) v2;
1159 if (a1->low < a2->low)
1160 return -1;
1161 if (a1->low > a2->low)
1162 return 1;
1163 if (a1->high < a2->high)
1164 return 1;
1165 if (a1->high > a2->high)
1166 return -1;
1167 return strcmp (a1->function->name, a2->function->name);
1170 /* Compare a PC against a function_addrs for bsearch. We always
1171 allocate an entra entry at the end of the vector, so that this
1172 routine can safely look at the next entry. Note that if there are
1173 multiple ranges containing PC, which one will be returned is
1174 unpredictable. We compensate for that in dwarf_fileline. */
1176 static int
1177 function_addrs_search (const void *vkey, const void *ventry)
1179 const uintptr_t *key = (const uintptr_t *) vkey;
1180 const struct function_addrs *entry = (const struct function_addrs *) ventry;
1181 uintptr_t pc;
1183 pc = *key;
1184 if (pc < entry->low)
1185 return -1;
1186 else if (pc > (entry + 1)->low)
1187 return 1;
1188 else
1189 return 0;
1192 /* Add a new compilation unit address range to a vector. This is
1193 called via add_ranges. Returns 1 on success, 0 on failure. */
1195 static int
1196 add_unit_addr (struct backtrace_state *state, void *rdata,
1197 uintptr_t lowpc, uintptr_t highpc,
1198 backtrace_error_callback error_callback, void *data,
1199 void *pvec)
1201 struct unit *u = (struct unit *) rdata;
1202 struct unit_addrs_vector *vec = (struct unit_addrs_vector *) pvec;
1203 struct unit_addrs *p;
1205 /* Try to merge with the last entry. */
1206 if (vec->count > 0)
1208 p = (struct unit_addrs *) vec->vec.base + (vec->count - 1);
1209 if ((lowpc == p->high || lowpc == p->high + 1)
1210 && u == p->u)
1212 if (highpc > p->high)
1213 p->high = highpc;
1214 return 1;
1218 p = ((struct unit_addrs *)
1219 backtrace_vector_grow (state, sizeof (struct unit_addrs),
1220 error_callback, data, &vec->vec));
1221 if (p == NULL)
1222 return 0;
1224 p->low = lowpc;
1225 p->high = highpc;
1226 p->u = u;
1228 ++vec->count;
1230 return 1;
1233 /* Compare unit_addrs for qsort. When ranges are nested, make the
1234 smallest one sort last. */
1236 static int
1237 unit_addrs_compare (const void *v1, const void *v2)
1239 const struct unit_addrs *a1 = (const struct unit_addrs *) v1;
1240 const struct unit_addrs *a2 = (const struct unit_addrs *) v2;
1242 if (a1->low < a2->low)
1243 return -1;
1244 if (a1->low > a2->low)
1245 return 1;
1246 if (a1->high < a2->high)
1247 return 1;
1248 if (a1->high > a2->high)
1249 return -1;
1250 if (a1->u->lineoff < a2->u->lineoff)
1251 return -1;
1252 if (a1->u->lineoff > a2->u->lineoff)
1253 return 1;
1254 return 0;
1257 /* Compare a PC against a unit_addrs for bsearch. We always allocate
1258 an entry entry at the end of the vector, so that this routine can
1259 safely look at the next entry. Note that if there are multiple
1260 ranges containing PC, which one will be returned is unpredictable.
1261 We compensate for that in dwarf_fileline. */
1263 static int
1264 unit_addrs_search (const void *vkey, const void *ventry)
1266 const uintptr_t *key = (const uintptr_t *) vkey;
1267 const struct unit_addrs *entry = (const struct unit_addrs *) ventry;
1268 uintptr_t pc;
1270 pc = *key;
1271 if (pc < entry->low)
1272 return -1;
1273 else if (pc > (entry + 1)->low)
1274 return 1;
1275 else
1276 return 0;
1279 /* Sort the line vector by PC. We want a stable sort here to maintain
1280 the order of lines for the same PC values. Since the sequence is
1281 being sorted in place, their addresses cannot be relied on to
1282 maintain stability. That is the purpose of the index member. */
1284 static int
1285 line_compare (const void *v1, const void *v2)
1287 const struct line *ln1 = (const struct line *) v1;
1288 const struct line *ln2 = (const struct line *) v2;
1290 if (ln1->pc < ln2->pc)
1291 return -1;
1292 else if (ln1->pc > ln2->pc)
1293 return 1;
1294 else if (ln1->idx < ln2->idx)
1295 return -1;
1296 else if (ln1->idx > ln2->idx)
1297 return 1;
1298 else
1299 return 0;
1302 /* Find a PC in a line vector. We always allocate an extra entry at
1303 the end of the lines vector, so that this routine can safely look
1304 at the next entry. Note that when there are multiple mappings for
1305 the same PC value, this will return the last one. */
1307 static int
1308 line_search (const void *vkey, const void *ventry)
1310 const uintptr_t *key = (const uintptr_t *) vkey;
1311 const struct line *entry = (const struct line *) ventry;
1312 uintptr_t pc;
1314 pc = *key;
1315 if (pc < entry->pc)
1316 return -1;
1317 else if (pc >= (entry + 1)->pc)
1318 return 1;
1319 else
1320 return 0;
1323 /* Sort the abbrevs by the abbrev code. This function is passed to
1324 both qsort and bsearch. */
1326 static int
1327 abbrev_compare (const void *v1, const void *v2)
1329 const struct abbrev *a1 = (const struct abbrev *) v1;
1330 const struct abbrev *a2 = (const struct abbrev *) v2;
1332 if (a1->code < a2->code)
1333 return -1;
1334 else if (a1->code > a2->code)
1335 return 1;
1336 else
1338 /* This really shouldn't happen. It means there are two
1339 different abbrevs with the same code, and that means we don't
1340 know which one lookup_abbrev should return. */
1341 return 0;
1345 /* Read the abbreviation table for a compilation unit. Returns 1 on
1346 success, 0 on failure. */
1348 static int
1349 read_abbrevs (struct backtrace_state *state, uint64_t abbrev_offset,
1350 const unsigned char *dwarf_abbrev, size_t dwarf_abbrev_size,
1351 int is_bigendian, backtrace_error_callback error_callback,
1352 void *data, struct abbrevs *abbrevs)
1354 struct dwarf_buf abbrev_buf;
1355 struct dwarf_buf count_buf;
1356 size_t num_abbrevs;
1358 abbrevs->num_abbrevs = 0;
1359 abbrevs->abbrevs = NULL;
1361 if (abbrev_offset >= dwarf_abbrev_size)
1363 error_callback (data, "abbrev offset out of range", 0);
1364 return 0;
1367 abbrev_buf.name = ".debug_abbrev";
1368 abbrev_buf.start = dwarf_abbrev;
1369 abbrev_buf.buf = dwarf_abbrev + abbrev_offset;
1370 abbrev_buf.left = dwarf_abbrev_size - abbrev_offset;
1371 abbrev_buf.is_bigendian = is_bigendian;
1372 abbrev_buf.error_callback = error_callback;
1373 abbrev_buf.data = data;
1374 abbrev_buf.reported_underflow = 0;
1376 /* Count the number of abbrevs in this list. */
1378 count_buf = abbrev_buf;
1379 num_abbrevs = 0;
1380 while (read_uleb128 (&count_buf) != 0)
1382 if (count_buf.reported_underflow)
1383 return 0;
1384 ++num_abbrevs;
1385 // Skip tag.
1386 read_uleb128 (&count_buf);
1387 // Skip has_children.
1388 read_byte (&count_buf);
1389 // Skip attributes.
1390 while (read_uleb128 (&count_buf) != 0)
1392 uint64_t form;
1394 form = read_uleb128 (&count_buf);
1395 if ((enum dwarf_form) form == DW_FORM_implicit_const)
1396 read_sleb128 (&count_buf);
1398 // Skip form of last attribute.
1399 read_uleb128 (&count_buf);
1402 if (count_buf.reported_underflow)
1403 return 0;
1405 if (num_abbrevs == 0)
1406 return 1;
1408 abbrevs->abbrevs = ((struct abbrev *)
1409 backtrace_alloc (state,
1410 num_abbrevs * sizeof (struct abbrev),
1411 error_callback, data));
1412 if (abbrevs->abbrevs == NULL)
1413 return 0;
1414 abbrevs->num_abbrevs = num_abbrevs;
1415 memset (abbrevs->abbrevs, 0, num_abbrevs * sizeof (struct abbrev));
1417 num_abbrevs = 0;
1418 while (1)
1420 uint64_t code;
1421 struct abbrev a;
1422 size_t num_attrs;
1423 struct attr *attrs;
1425 if (abbrev_buf.reported_underflow)
1426 goto fail;
1428 code = read_uleb128 (&abbrev_buf);
1429 if (code == 0)
1430 break;
1432 a.code = code;
1433 a.tag = (enum dwarf_tag) read_uleb128 (&abbrev_buf);
1434 a.has_children = read_byte (&abbrev_buf);
1436 count_buf = abbrev_buf;
1437 num_attrs = 0;
1438 while (read_uleb128 (&count_buf) != 0)
1440 uint64_t form;
1442 ++num_attrs;
1443 form = read_uleb128 (&count_buf);
1444 if ((enum dwarf_form) form == DW_FORM_implicit_const)
1445 read_sleb128 (&count_buf);
1448 if (num_attrs == 0)
1450 attrs = NULL;
1451 read_uleb128 (&abbrev_buf);
1452 read_uleb128 (&abbrev_buf);
1454 else
1456 attrs = ((struct attr *)
1457 backtrace_alloc (state, num_attrs * sizeof *attrs,
1458 error_callback, data));
1459 if (attrs == NULL)
1460 goto fail;
1461 num_attrs = 0;
1462 while (1)
1464 uint64_t name;
1465 uint64_t form;
1467 name = read_uleb128 (&abbrev_buf);
1468 form = read_uleb128 (&abbrev_buf);
1469 if (name == 0)
1470 break;
1471 attrs[num_attrs].name = (enum dwarf_attribute) name;
1472 attrs[num_attrs].form = (enum dwarf_form) form;
1473 if ((enum dwarf_form) form == DW_FORM_implicit_const)
1474 attrs[num_attrs].val = read_sleb128 (&abbrev_buf);
1475 else
1476 attrs[num_attrs].val = 0;
1477 ++num_attrs;
1481 a.num_attrs = num_attrs;
1482 a.attrs = attrs;
1484 abbrevs->abbrevs[num_abbrevs] = a;
1485 ++num_abbrevs;
1488 backtrace_qsort (abbrevs->abbrevs, abbrevs->num_abbrevs,
1489 sizeof (struct abbrev), abbrev_compare);
1491 return 1;
1493 fail:
1494 free_abbrevs (state, abbrevs, error_callback, data);
1495 return 0;
1498 /* Return the abbrev information for an abbrev code. */
1500 static const struct abbrev *
1501 lookup_abbrev (struct abbrevs *abbrevs, uint64_t code,
1502 backtrace_error_callback error_callback, void *data)
1504 struct abbrev key;
1505 void *p;
1507 /* With GCC, where abbrevs are simply numbered in order, we should
1508 be able to just look up the entry. */
1509 if (code - 1 < abbrevs->num_abbrevs
1510 && abbrevs->abbrevs[code - 1].code == code)
1511 return &abbrevs->abbrevs[code - 1];
1513 /* Otherwise we have to search. */
1514 memset (&key, 0, sizeof key);
1515 key.code = code;
1516 p = bsearch (&key, abbrevs->abbrevs, abbrevs->num_abbrevs,
1517 sizeof (struct abbrev), abbrev_compare);
1518 if (p == NULL)
1520 error_callback (data, "invalid abbreviation code", 0);
1521 return NULL;
1523 return (const struct abbrev *) p;
1526 /* This struct is used to gather address range information while
1527 reading attributes. We use this while building a mapping from
1528 address ranges to compilation units and then again while mapping
1529 from address ranges to function entries. Normally either
1530 lowpc/highpc is set or ranges is set. */
1532 struct pcrange {
1533 uintptr_t lowpc; /* The low PC value. */
1534 int have_lowpc; /* Whether a low PC value was found. */
1535 int lowpc_is_addr_index; /* Whether lowpc is in .debug_addr. */
1536 uintptr_t highpc; /* The high PC value. */
1537 int have_highpc; /* Whether a high PC value was found. */
1538 int highpc_is_relative; /* Whether highpc is relative to lowpc. */
1539 int highpc_is_addr_index; /* Whether highpc is in .debug_addr. */
1540 uint64_t ranges; /* Offset in ranges section. */
1541 int have_ranges; /* Whether ranges is valid. */
1542 int ranges_is_index; /* Whether ranges is DW_FORM_rnglistx. */
1545 /* Update PCRANGE from an attribute value. */
1547 static void
1548 update_pcrange (const struct attr* attr, const struct attr_val* val,
1549 struct pcrange *pcrange)
1551 switch (attr->name)
1553 case DW_AT_low_pc:
1554 if (val->encoding == ATTR_VAL_ADDRESS)
1556 pcrange->lowpc = (uintptr_t) val->u.uint;
1557 pcrange->have_lowpc = 1;
1559 else if (val->encoding == ATTR_VAL_ADDRESS_INDEX)
1561 pcrange->lowpc = (uintptr_t) val->u.uint;
1562 pcrange->have_lowpc = 1;
1563 pcrange->lowpc_is_addr_index = 1;
1565 break;
1567 case DW_AT_high_pc:
1568 if (val->encoding == ATTR_VAL_ADDRESS)
1570 pcrange->highpc = (uintptr_t) val->u.uint;
1571 pcrange->have_highpc = 1;
1573 else if (val->encoding == ATTR_VAL_UINT)
1575 pcrange->highpc = (uintptr_t) val->u.uint;
1576 pcrange->have_highpc = 1;
1577 pcrange->highpc_is_relative = 1;
1579 else if (val->encoding == ATTR_VAL_ADDRESS_INDEX)
1581 pcrange->highpc = (uintptr_t) val->u.uint;
1582 pcrange->have_highpc = 1;
1583 pcrange->highpc_is_addr_index = 1;
1585 break;
1587 case DW_AT_ranges:
1588 if (val->encoding == ATTR_VAL_UINT
1589 || val->encoding == ATTR_VAL_REF_SECTION)
1591 pcrange->ranges = val->u.uint;
1592 pcrange->have_ranges = 1;
1594 else if (val->encoding == ATTR_VAL_RNGLISTS_INDEX)
1596 pcrange->ranges = val->u.uint;
1597 pcrange->have_ranges = 1;
1598 pcrange->ranges_is_index = 1;
1600 break;
1602 default:
1603 break;
1607 /* Call ADD_RANGE for a low/high PC pair. Returns 1 on success, 0 on
1608 error. */
1610 static int
1611 add_low_high_range (struct backtrace_state *state,
1612 const struct dwarf_sections *dwarf_sections,
1613 struct libbacktrace_base_address base_address,
1614 int is_bigendian, struct unit *u,
1615 const struct pcrange *pcrange,
1616 int (*add_range) (struct backtrace_state *state,
1617 void *rdata, uintptr_t lowpc,
1618 uintptr_t highpc,
1619 backtrace_error_callback error_callback,
1620 void *data, void *vec),
1621 void *rdata,
1622 backtrace_error_callback error_callback, void *data,
1623 void *vec)
1625 uintptr_t lowpc;
1626 uintptr_t highpc;
1628 lowpc = pcrange->lowpc;
1629 if (pcrange->lowpc_is_addr_index)
1631 if (!resolve_addr_index (dwarf_sections, u->addr_base, u->addrsize,
1632 is_bigendian, lowpc, error_callback, data,
1633 &lowpc))
1634 return 0;
1637 highpc = pcrange->highpc;
1638 if (pcrange->highpc_is_addr_index)
1640 if (!resolve_addr_index (dwarf_sections, u->addr_base, u->addrsize,
1641 is_bigendian, highpc, error_callback, data,
1642 &highpc))
1643 return 0;
1645 if (pcrange->highpc_is_relative)
1646 highpc += lowpc;
1648 /* Add in the base address of the module when recording PC values,
1649 so that we can look up the PC directly. */
1650 lowpc = libbacktrace_add_base (lowpc, base_address);
1651 highpc = libbacktrace_add_base (highpc, base_address);
1653 return add_range (state, rdata, lowpc, highpc, error_callback, data, vec);
1656 /* Call ADD_RANGE for each range read from .debug_ranges, as used in
1657 DWARF versions 2 through 4. */
1659 static int
1660 add_ranges_from_ranges (
1661 struct backtrace_state *state,
1662 const struct dwarf_sections *dwarf_sections,
1663 struct libbacktrace_base_address base_address, int is_bigendian,
1664 struct unit *u, uintptr_t base,
1665 const struct pcrange *pcrange,
1666 int (*add_range) (struct backtrace_state *state, void *rdata,
1667 uintptr_t lowpc, uintptr_t highpc,
1668 backtrace_error_callback error_callback, void *data,
1669 void *vec),
1670 void *rdata,
1671 backtrace_error_callback error_callback, void *data,
1672 void *vec)
1674 struct dwarf_buf ranges_buf;
1676 if (pcrange->ranges >= dwarf_sections->size[DEBUG_RANGES])
1678 error_callback (data, "ranges offset out of range", 0);
1679 return 0;
1682 ranges_buf.name = ".debug_ranges";
1683 ranges_buf.start = dwarf_sections->data[DEBUG_RANGES];
1684 ranges_buf.buf = dwarf_sections->data[DEBUG_RANGES] + pcrange->ranges;
1685 ranges_buf.left = dwarf_sections->size[DEBUG_RANGES] - pcrange->ranges;
1686 ranges_buf.is_bigendian = is_bigendian;
1687 ranges_buf.error_callback = error_callback;
1688 ranges_buf.data = data;
1689 ranges_buf.reported_underflow = 0;
1691 while (1)
1693 uint64_t low;
1694 uint64_t high;
1696 if (ranges_buf.reported_underflow)
1697 return 0;
1699 low = read_address (&ranges_buf, u->addrsize);
1700 high = read_address (&ranges_buf, u->addrsize);
1702 if (low == 0 && high == 0)
1703 break;
1705 if (is_highest_address (low, u->addrsize))
1706 base = (uintptr_t) high;
1707 else
1709 uintptr_t rl, rh;
1711 rl = libbacktrace_add_base ((uintptr_t) low + base, base_address);
1712 rh = libbacktrace_add_base ((uintptr_t) high + base, base_address);
1713 if (!add_range (state, rdata, rl, rh, error_callback, data, vec))
1714 return 0;
1718 if (ranges_buf.reported_underflow)
1719 return 0;
1721 return 1;
1724 /* Call ADD_RANGE for each range read from .debug_rnglists, as used in
1725 DWARF version 5. */
1727 static int
1728 add_ranges_from_rnglists (
1729 struct backtrace_state *state,
1730 const struct dwarf_sections *dwarf_sections,
1731 struct libbacktrace_base_address base_address, int is_bigendian,
1732 struct unit *u, uintptr_t base,
1733 const struct pcrange *pcrange,
1734 int (*add_range) (struct backtrace_state *state, void *rdata,
1735 uintptr_t lowpc, uintptr_t highpc,
1736 backtrace_error_callback error_callback, void *data,
1737 void *vec),
1738 void *rdata,
1739 backtrace_error_callback error_callback, void *data,
1740 void *vec)
1742 uint64_t offset;
1743 struct dwarf_buf rnglists_buf;
1745 if (!pcrange->ranges_is_index)
1746 offset = pcrange->ranges;
1747 else
1748 offset = u->rnglists_base + pcrange->ranges * (u->is_dwarf64 ? 8 : 4);
1749 if (offset >= dwarf_sections->size[DEBUG_RNGLISTS])
1751 error_callback (data, "rnglists offset out of range", 0);
1752 return 0;
1755 rnglists_buf.name = ".debug_rnglists";
1756 rnglists_buf.start = dwarf_sections->data[DEBUG_RNGLISTS];
1757 rnglists_buf.buf = dwarf_sections->data[DEBUG_RNGLISTS] + offset;
1758 rnglists_buf.left = dwarf_sections->size[DEBUG_RNGLISTS] - offset;
1759 rnglists_buf.is_bigendian = is_bigendian;
1760 rnglists_buf.error_callback = error_callback;
1761 rnglists_buf.data = data;
1762 rnglists_buf.reported_underflow = 0;
1764 if (pcrange->ranges_is_index)
1766 offset = read_offset (&rnglists_buf, u->is_dwarf64);
1767 offset += u->rnglists_base;
1768 if (offset >= dwarf_sections->size[DEBUG_RNGLISTS])
1770 error_callback (data, "rnglists index offset out of range", 0);
1771 return 0;
1773 rnglists_buf.buf = dwarf_sections->data[DEBUG_RNGLISTS] + offset;
1774 rnglists_buf.left = dwarf_sections->size[DEBUG_RNGLISTS] - offset;
1777 while (1)
1779 unsigned char rle;
1781 rle = read_byte (&rnglists_buf);
1782 if (rle == DW_RLE_end_of_list)
1783 break;
1784 switch (rle)
1786 case DW_RLE_base_addressx:
1788 uint64_t index;
1790 index = read_uleb128 (&rnglists_buf);
1791 if (!resolve_addr_index (dwarf_sections, u->addr_base,
1792 u->addrsize, is_bigendian, index,
1793 error_callback, data, &base))
1794 return 0;
1796 break;
1798 case DW_RLE_startx_endx:
1800 uint64_t index;
1801 uintptr_t low;
1802 uintptr_t high;
1804 index = read_uleb128 (&rnglists_buf);
1805 if (!resolve_addr_index (dwarf_sections, u->addr_base,
1806 u->addrsize, is_bigendian, index,
1807 error_callback, data, &low))
1808 return 0;
1809 index = read_uleb128 (&rnglists_buf);
1810 if (!resolve_addr_index (dwarf_sections, u->addr_base,
1811 u->addrsize, is_bigendian, index,
1812 error_callback, data, &high))
1813 return 0;
1814 if (!add_range (state, rdata,
1815 libbacktrace_add_base (low, base_address),
1816 libbacktrace_add_base (high, base_address),
1817 error_callback, data, vec))
1818 return 0;
1820 break;
1822 case DW_RLE_startx_length:
1824 uint64_t index;
1825 uintptr_t low;
1826 uintptr_t length;
1828 index = read_uleb128 (&rnglists_buf);
1829 if (!resolve_addr_index (dwarf_sections, u->addr_base,
1830 u->addrsize, is_bigendian, index,
1831 error_callback, data, &low))
1832 return 0;
1833 length = read_uleb128 (&rnglists_buf);
1834 low = libbacktrace_add_base (low, base_address);
1835 if (!add_range (state, rdata, low, low + length,
1836 error_callback, data, vec))
1837 return 0;
1839 break;
1841 case DW_RLE_offset_pair:
1843 uint64_t low;
1844 uint64_t high;
1846 low = read_uleb128 (&rnglists_buf);
1847 high = read_uleb128 (&rnglists_buf);
1848 if (!add_range (state, rdata,
1849 libbacktrace_add_base (low + base, base_address),
1850 libbacktrace_add_base (high + base, base_address),
1851 error_callback, data, vec))
1852 return 0;
1854 break;
1856 case DW_RLE_base_address:
1857 base = (uintptr_t) read_address (&rnglists_buf, u->addrsize);
1858 break;
1860 case DW_RLE_start_end:
1862 uintptr_t low;
1863 uintptr_t high;
1865 low = (uintptr_t) read_address (&rnglists_buf, u->addrsize);
1866 high = (uintptr_t) read_address (&rnglists_buf, u->addrsize);
1867 if (!add_range (state, rdata,
1868 libbacktrace_add_base (low, base_address),
1869 libbacktrace_add_base (high, base_address),
1870 error_callback, data, vec))
1871 return 0;
1873 break;
1875 case DW_RLE_start_length:
1877 uintptr_t low;
1878 uintptr_t length;
1880 low = (uintptr_t) read_address (&rnglists_buf, u->addrsize);
1881 length = (uintptr_t) read_uleb128 (&rnglists_buf);
1882 low = libbacktrace_add_base (low, base_address);
1883 if (!add_range (state, rdata, low, low + length,
1884 error_callback, data, vec))
1885 return 0;
1887 break;
1889 default:
1890 dwarf_buf_error (&rnglists_buf, "unrecognized DW_RLE value", -1);
1891 return 0;
1895 if (rnglists_buf.reported_underflow)
1896 return 0;
1898 return 1;
1901 /* Call ADD_RANGE for each lowpc/highpc pair in PCRANGE. RDATA is
1902 passed to ADD_RANGE, and is either a struct unit * or a struct
1903 function *. VEC is the vector we are adding ranges to, and is
1904 either a struct unit_addrs_vector * or a struct function_vector *.
1905 Returns 1 on success, 0 on error. */
1907 static int
1908 add_ranges (struct backtrace_state *state,
1909 const struct dwarf_sections *dwarf_sections,
1910 struct libbacktrace_base_address base_address, int is_bigendian,
1911 struct unit *u, uintptr_t base, const struct pcrange *pcrange,
1912 int (*add_range) (struct backtrace_state *state, void *rdata,
1913 uintptr_t lowpc, uintptr_t highpc,
1914 backtrace_error_callback error_callback,
1915 void *data, void *vec),
1916 void *rdata,
1917 backtrace_error_callback error_callback, void *data,
1918 void *vec)
1920 if (pcrange->have_lowpc && pcrange->have_highpc)
1921 return add_low_high_range (state, dwarf_sections, base_address,
1922 is_bigendian, u, pcrange, add_range, rdata,
1923 error_callback, data, vec);
1925 if (!pcrange->have_ranges)
1927 /* Did not find any address ranges to add. */
1928 return 1;
1931 if (u->version < 5)
1932 return add_ranges_from_ranges (state, dwarf_sections, base_address,
1933 is_bigendian, u, base, pcrange, add_range,
1934 rdata, error_callback, data, vec);
1935 else
1936 return add_ranges_from_rnglists (state, dwarf_sections, base_address,
1937 is_bigendian, u, base, pcrange, add_range,
1938 rdata, error_callback, data, vec);
1941 /* Find the address range covered by a compilation unit, reading from
1942 UNIT_BUF and adding values to U. Returns 1 if all data could be
1943 read, 0 if there is some error. */
1945 static int
1946 find_address_ranges (struct backtrace_state *state,
1947 struct libbacktrace_base_address base_address,
1948 struct dwarf_buf *unit_buf,
1949 const struct dwarf_sections *dwarf_sections,
1950 int is_bigendian, struct dwarf_data *altlink,
1951 backtrace_error_callback error_callback, void *data,
1952 struct unit *u, struct unit_addrs_vector *addrs,
1953 enum dwarf_tag *unit_tag)
1955 while (unit_buf->left > 0)
1957 uint64_t code;
1958 const struct abbrev *abbrev;
1959 struct pcrange pcrange;
1960 struct attr_val name_val;
1961 int have_name_val;
1962 struct attr_val comp_dir_val;
1963 int have_comp_dir_val;
1964 size_t i;
1966 code = read_uleb128 (unit_buf);
1967 if (code == 0)
1968 return 1;
1970 abbrev = lookup_abbrev (&u->abbrevs, code, error_callback, data);
1971 if (abbrev == NULL)
1972 return 0;
1974 if (unit_tag != NULL)
1975 *unit_tag = abbrev->tag;
1977 memset (&pcrange, 0, sizeof pcrange);
1978 memset (&name_val, 0, sizeof name_val);
1979 have_name_val = 0;
1980 memset (&comp_dir_val, 0, sizeof comp_dir_val);
1981 have_comp_dir_val = 0;
1982 for (i = 0; i < abbrev->num_attrs; ++i)
1984 struct attr_val val;
1986 if (!read_attribute (abbrev->attrs[i].form, abbrev->attrs[i].val,
1987 unit_buf, u->is_dwarf64, u->version,
1988 u->addrsize, dwarf_sections, altlink, &val))
1989 return 0;
1991 switch (abbrev->attrs[i].name)
1993 case DW_AT_low_pc: case DW_AT_high_pc: case DW_AT_ranges:
1994 update_pcrange (&abbrev->attrs[i], &val, &pcrange);
1995 break;
1997 case DW_AT_stmt_list:
1998 if ((abbrev->tag == DW_TAG_compile_unit
1999 || abbrev->tag == DW_TAG_skeleton_unit)
2000 && (val.encoding == ATTR_VAL_UINT
2001 || val.encoding == ATTR_VAL_REF_SECTION))
2002 u->lineoff = val.u.uint;
2003 break;
2005 case DW_AT_name:
2006 if (abbrev->tag == DW_TAG_compile_unit
2007 || abbrev->tag == DW_TAG_skeleton_unit)
2009 name_val = val;
2010 have_name_val = 1;
2012 break;
2014 case DW_AT_comp_dir:
2015 if (abbrev->tag == DW_TAG_compile_unit
2016 || abbrev->tag == DW_TAG_skeleton_unit)
2018 comp_dir_val = val;
2019 have_comp_dir_val = 1;
2021 break;
2023 case DW_AT_str_offsets_base:
2024 if ((abbrev->tag == DW_TAG_compile_unit
2025 || abbrev->tag == DW_TAG_skeleton_unit)
2026 && val.encoding == ATTR_VAL_REF_SECTION)
2027 u->str_offsets_base = val.u.uint;
2028 break;
2030 case DW_AT_addr_base:
2031 if ((abbrev->tag == DW_TAG_compile_unit
2032 || abbrev->tag == DW_TAG_skeleton_unit)
2033 && val.encoding == ATTR_VAL_REF_SECTION)
2034 u->addr_base = val.u.uint;
2035 break;
2037 case DW_AT_rnglists_base:
2038 if ((abbrev->tag == DW_TAG_compile_unit
2039 || abbrev->tag == DW_TAG_skeleton_unit)
2040 && val.encoding == ATTR_VAL_REF_SECTION)
2041 u->rnglists_base = val.u.uint;
2042 break;
2044 default:
2045 break;
2049 // Resolve strings after we're sure that we have seen
2050 // DW_AT_str_offsets_base.
2051 if (have_name_val)
2053 if (!resolve_string (dwarf_sections, u->is_dwarf64, is_bigendian,
2054 u->str_offsets_base, &name_val,
2055 error_callback, data, &u->filename))
2056 return 0;
2058 if (have_comp_dir_val)
2060 if (!resolve_string (dwarf_sections, u->is_dwarf64, is_bigendian,
2061 u->str_offsets_base, &comp_dir_val,
2062 error_callback, data, &u->comp_dir))
2063 return 0;
2066 if (abbrev->tag == DW_TAG_compile_unit
2067 || abbrev->tag == DW_TAG_subprogram
2068 || abbrev->tag == DW_TAG_skeleton_unit)
2070 if (!add_ranges (state, dwarf_sections, base_address,
2071 is_bigendian, u, pcrange.lowpc, &pcrange,
2072 add_unit_addr, (void *) u, error_callback, data,
2073 (void *) addrs))
2074 return 0;
2076 /* If we found the PC range in the DW_TAG_compile_unit or
2077 DW_TAG_skeleton_unit, we can stop now. */
2078 if ((abbrev->tag == DW_TAG_compile_unit
2079 || abbrev->tag == DW_TAG_skeleton_unit)
2080 && (pcrange.have_ranges
2081 || (pcrange.have_lowpc && pcrange.have_highpc)))
2082 return 1;
2085 if (abbrev->has_children)
2087 if (!find_address_ranges (state, base_address, unit_buf,
2088 dwarf_sections, is_bigendian, altlink,
2089 error_callback, data, u, addrs, NULL))
2090 return 0;
2094 return 1;
2097 /* Build a mapping from address ranges to the compilation units where
2098 the line number information for that range can be found. Returns 1
2099 on success, 0 on failure. */
2101 static int
2102 build_address_map (struct backtrace_state *state,
2103 struct libbacktrace_base_address base_address,
2104 const struct dwarf_sections *dwarf_sections,
2105 int is_bigendian, struct dwarf_data *altlink,
2106 backtrace_error_callback error_callback, void *data,
2107 struct unit_addrs_vector *addrs,
2108 struct unit_vector *unit_vec)
2110 struct dwarf_buf info;
2111 struct backtrace_vector units;
2112 size_t units_count;
2113 size_t i;
2114 struct unit **pu;
2115 size_t unit_offset = 0;
2116 struct unit_addrs *pa;
2118 memset (&addrs->vec, 0, sizeof addrs->vec);
2119 memset (&unit_vec->vec, 0, sizeof unit_vec->vec);
2120 addrs->count = 0;
2121 unit_vec->count = 0;
2123 /* Read through the .debug_info section. FIXME: Should we use the
2124 .debug_aranges section? gdb and addr2line don't use it, but I'm
2125 not sure why. */
2127 info.name = ".debug_info";
2128 info.start = dwarf_sections->data[DEBUG_INFO];
2129 info.buf = info.start;
2130 info.left = dwarf_sections->size[DEBUG_INFO];
2131 info.is_bigendian = is_bigendian;
2132 info.error_callback = error_callback;
2133 info.data = data;
2134 info.reported_underflow = 0;
2136 memset (&units, 0, sizeof units);
2137 units_count = 0;
2139 while (info.left > 0)
2141 const unsigned char *unit_data_start;
2142 uint64_t len;
2143 int is_dwarf64;
2144 struct dwarf_buf unit_buf;
2145 int version;
2146 int unit_type;
2147 uint64_t abbrev_offset;
2148 int addrsize;
2149 struct unit *u;
2150 enum dwarf_tag unit_tag;
2152 if (info.reported_underflow)
2153 goto fail;
2155 unit_data_start = info.buf;
2157 len = read_initial_length (&info, &is_dwarf64);
2158 unit_buf = info;
2159 unit_buf.left = len;
2161 if (!advance (&info, len))
2162 goto fail;
2164 version = read_uint16 (&unit_buf);
2165 if (version < 2 || version > 5)
2167 dwarf_buf_error (&unit_buf, "unrecognized DWARF version", -1);
2168 goto fail;
2171 if (version < 5)
2172 unit_type = 0;
2173 else
2175 unit_type = read_byte (&unit_buf);
2176 if (unit_type == DW_UT_type || unit_type == DW_UT_split_type)
2178 /* This unit doesn't have anything we need. */
2179 continue;
2183 pu = ((struct unit **)
2184 backtrace_vector_grow (state, sizeof (struct unit *),
2185 error_callback, data, &units));
2186 if (pu == NULL)
2187 goto fail;
2189 u = ((struct unit *)
2190 backtrace_alloc (state, sizeof *u, error_callback, data));
2191 if (u == NULL)
2192 goto fail;
2194 *pu = u;
2195 ++units_count;
2197 if (version < 5)
2198 addrsize = 0; /* Set below. */
2199 else
2200 addrsize = read_byte (&unit_buf);
2202 memset (&u->abbrevs, 0, sizeof u->abbrevs);
2203 abbrev_offset = read_offset (&unit_buf, is_dwarf64);
2204 if (!read_abbrevs (state, abbrev_offset,
2205 dwarf_sections->data[DEBUG_ABBREV],
2206 dwarf_sections->size[DEBUG_ABBREV],
2207 is_bigendian, error_callback, data, &u->abbrevs))
2208 goto fail;
2210 if (version < 5)
2211 addrsize = read_byte (&unit_buf);
2213 switch (unit_type)
2215 case 0:
2216 break;
2217 case DW_UT_compile: case DW_UT_partial:
2218 break;
2219 case DW_UT_skeleton: case DW_UT_split_compile:
2220 read_uint64 (&unit_buf); /* dwo_id */
2221 break;
2222 default:
2223 break;
2226 u->low_offset = unit_offset;
2227 unit_offset += len + (is_dwarf64 ? 12 : 4);
2228 u->high_offset = unit_offset;
2229 u->unit_data = unit_buf.buf;
2230 u->unit_data_len = unit_buf.left;
2231 u->unit_data_offset = unit_buf.buf - unit_data_start;
2232 u->version = version;
2233 u->is_dwarf64 = is_dwarf64;
2234 u->addrsize = addrsize;
2235 u->filename = NULL;
2236 u->comp_dir = NULL;
2237 u->abs_filename = NULL;
2238 u->lineoff = 0;
2239 u->str_offsets_base = 0;
2240 u->addr_base = 0;
2241 u->rnglists_base = 0;
2243 /* The actual line number mappings will be read as needed. */
2244 u->lines = NULL;
2245 u->lines_count = 0;
2246 u->function_addrs = NULL;
2247 u->function_addrs_count = 0;
2249 if (!find_address_ranges (state, base_address, &unit_buf, dwarf_sections,
2250 is_bigendian, altlink, error_callback, data,
2251 u, addrs, &unit_tag))
2252 goto fail;
2254 if (unit_buf.reported_underflow)
2255 goto fail;
2257 if (info.reported_underflow)
2258 goto fail;
2260 /* Add a trailing addrs entry, but don't include it in addrs->count. */
2261 pa = ((struct unit_addrs *)
2262 backtrace_vector_grow (state, sizeof (struct unit_addrs),
2263 error_callback, data, &addrs->vec));
2264 if (pa == NULL)
2265 goto fail;
2266 pa->low = 0;
2267 --pa->low;
2268 pa->high = pa->low;
2269 pa->u = NULL;
2271 unit_vec->vec = units;
2272 unit_vec->count = units_count;
2273 return 1;
2275 fail:
2276 if (units_count > 0)
2278 pu = (struct unit **) units.base;
2279 for (i = 0; i < units_count; i++)
2281 free_abbrevs (state, &pu[i]->abbrevs, error_callback, data);
2282 backtrace_free (state, pu[i], sizeof **pu, error_callback, data);
2284 backtrace_vector_free (state, &units, error_callback, data);
2286 if (addrs->count > 0)
2288 backtrace_vector_free (state, &addrs->vec, error_callback, data);
2289 addrs->count = 0;
2291 return 0;
2294 /* Add a new mapping to the vector of line mappings that we are
2295 building. Returns 1 on success, 0 on failure. */
2297 static int
2298 add_line (struct backtrace_state *state, struct dwarf_data *ddata,
2299 uintptr_t pc, const char *filename, int lineno,
2300 backtrace_error_callback error_callback, void *data,
2301 struct line_vector *vec)
2303 struct line *ln;
2305 /* If we are adding the same mapping, ignore it. This can happen
2306 when using discriminators. */
2307 if (vec->count > 0)
2309 ln = (struct line *) vec->vec.base + (vec->count - 1);
2310 if (pc == ln->pc && filename == ln->filename && lineno == ln->lineno)
2311 return 1;
2314 ln = ((struct line *)
2315 backtrace_vector_grow (state, sizeof (struct line), error_callback,
2316 data, &vec->vec));
2317 if (ln == NULL)
2318 return 0;
2320 /* Add in the base address here, so that we can look up the PC
2321 directly. */
2322 ln->pc = libbacktrace_add_base (pc, ddata->base_address);
2324 ln->filename = filename;
2325 ln->lineno = lineno;
2326 ln->idx = vec->count;
2328 ++vec->count;
2330 return 1;
2333 /* Free the line header information. */
2335 static void
2336 free_line_header (struct backtrace_state *state, struct line_header *hdr,
2337 backtrace_error_callback error_callback, void *data)
2339 if (hdr->dirs_count != 0)
2340 backtrace_free (state, hdr->dirs, hdr->dirs_count * sizeof (const char *),
2341 error_callback, data);
2342 backtrace_free (state, hdr->filenames,
2343 hdr->filenames_count * sizeof (char *),
2344 error_callback, data);
2347 /* Read the directories and file names for a line header for version
2348 2, setting fields in HDR. Return 1 on success, 0 on failure. */
2350 static int
2351 read_v2_paths (struct backtrace_state *state, struct unit *u,
2352 struct dwarf_buf *hdr_buf, struct line_header *hdr)
2354 const unsigned char *p;
2355 const unsigned char *pend;
2356 size_t i;
2358 /* Count the number of directory entries. */
2359 hdr->dirs_count = 0;
2360 p = hdr_buf->buf;
2361 pend = p + hdr_buf->left;
2362 while (p < pend && *p != '\0')
2364 p += strnlen((const char *) p, pend - p) + 1;
2365 ++hdr->dirs_count;
2368 /* The index of the first entry in the list of directories is 1. Index 0 is
2369 used for the current directory of the compilation. To simplify index
2370 handling, we set entry 0 to the compilation unit directory. */
2371 ++hdr->dirs_count;
2372 hdr->dirs = ((const char **)
2373 backtrace_alloc (state,
2374 hdr->dirs_count * sizeof (const char *),
2375 hdr_buf->error_callback,
2376 hdr_buf->data));
2377 if (hdr->dirs == NULL)
2378 return 0;
2380 hdr->dirs[0] = u->comp_dir;
2381 i = 1;
2382 while (*hdr_buf->buf != '\0')
2384 if (hdr_buf->reported_underflow)
2385 return 0;
2387 hdr->dirs[i] = read_string (hdr_buf);
2388 if (hdr->dirs[i] == NULL)
2389 return 0;
2390 ++i;
2392 if (!advance (hdr_buf, 1))
2393 return 0;
2395 /* Count the number of file entries. */
2396 hdr->filenames_count = 0;
2397 p = hdr_buf->buf;
2398 pend = p + hdr_buf->left;
2399 while (p < pend && *p != '\0')
2401 p += strnlen ((const char *) p, pend - p) + 1;
2402 p += leb128_len (p);
2403 p += leb128_len (p);
2404 p += leb128_len (p);
2405 ++hdr->filenames_count;
2408 /* The index of the first entry in the list of file names is 1. Index 0 is
2409 used for the DW_AT_name of the compilation unit. To simplify index
2410 handling, we set entry 0 to the compilation unit file name. */
2411 ++hdr->filenames_count;
2412 hdr->filenames = ((const char **)
2413 backtrace_alloc (state,
2414 hdr->filenames_count * sizeof (char *),
2415 hdr_buf->error_callback,
2416 hdr_buf->data));
2417 if (hdr->filenames == NULL)
2418 return 0;
2419 hdr->filenames[0] = u->filename;
2420 i = 1;
2421 while (*hdr_buf->buf != '\0')
2423 const char *filename;
2424 uint64_t dir_index;
2426 if (hdr_buf->reported_underflow)
2427 return 0;
2429 filename = read_string (hdr_buf);
2430 if (filename == NULL)
2431 return 0;
2432 dir_index = read_uleb128 (hdr_buf);
2433 if (IS_ABSOLUTE_PATH (filename)
2434 || (dir_index < hdr->dirs_count && hdr->dirs[dir_index] == NULL))
2435 hdr->filenames[i] = filename;
2436 else
2438 const char *dir;
2439 size_t dir_len;
2440 size_t filename_len;
2441 char *s;
2443 if (dir_index < hdr->dirs_count)
2444 dir = hdr->dirs[dir_index];
2445 else
2447 dwarf_buf_error (hdr_buf,
2448 ("invalid directory index in "
2449 "line number program header"),
2451 return 0;
2453 dir_len = strlen (dir);
2454 filename_len = strlen (filename);
2455 s = ((char *) backtrace_alloc (state, dir_len + filename_len + 2,
2456 hdr_buf->error_callback,
2457 hdr_buf->data));
2458 if (s == NULL)
2459 return 0;
2460 memcpy (s, dir, dir_len);
2461 /* FIXME: If we are on a DOS-based file system, and the
2462 directory or the file name use backslashes, then we
2463 should use a backslash here. */
2464 s[dir_len] = '/';
2465 memcpy (s + dir_len + 1, filename, filename_len + 1);
2466 hdr->filenames[i] = s;
2469 /* Ignore the modification time and size. */
2470 read_uleb128 (hdr_buf);
2471 read_uleb128 (hdr_buf);
2473 ++i;
2476 return 1;
2479 /* Read a single version 5 LNCT entry for a directory or file name in a
2480 line header. Sets *STRING to the resulting name, ignoring other
2481 data. Return 1 on success, 0 on failure. */
2483 static int
2484 read_lnct (struct backtrace_state *state, struct dwarf_data *ddata,
2485 struct unit *u, struct dwarf_buf *hdr_buf,
2486 const struct line_header *hdr, size_t formats_count,
2487 const struct line_header_format *formats, const char **string)
2489 size_t i;
2490 const char *dir;
2491 const char *path;
2493 dir = NULL;
2494 path = NULL;
2495 for (i = 0; i < formats_count; i++)
2497 struct attr_val val;
2499 if (!read_attribute (formats[i].form, 0, hdr_buf, u->is_dwarf64,
2500 u->version, hdr->addrsize, &ddata->dwarf_sections,
2501 ddata->altlink, &val))
2502 return 0;
2503 switch (formats[i].lnct)
2505 case DW_LNCT_path:
2506 if (!resolve_string (&ddata->dwarf_sections, u->is_dwarf64,
2507 ddata->is_bigendian, u->str_offsets_base,
2508 &val, hdr_buf->error_callback, hdr_buf->data,
2509 &path))
2510 return 0;
2511 break;
2512 case DW_LNCT_directory_index:
2513 if (val.encoding == ATTR_VAL_UINT)
2515 if (val.u.uint >= hdr->dirs_count)
2517 dwarf_buf_error (hdr_buf,
2518 ("invalid directory index in "
2519 "line number program header"),
2521 return 0;
2523 dir = hdr->dirs[val.u.uint];
2525 break;
2526 default:
2527 /* We don't care about timestamps or sizes or hashes. */
2528 break;
2532 if (path == NULL)
2534 dwarf_buf_error (hdr_buf,
2535 "missing file name in line number program header",
2537 return 0;
2540 if (dir == NULL)
2541 *string = path;
2542 else
2544 size_t dir_len;
2545 size_t path_len;
2546 char *s;
2548 dir_len = strlen (dir);
2549 path_len = strlen (path);
2550 s = (char *) backtrace_alloc (state, dir_len + path_len + 2,
2551 hdr_buf->error_callback, hdr_buf->data);
2552 if (s == NULL)
2553 return 0;
2554 memcpy (s, dir, dir_len);
2555 /* FIXME: If we are on a DOS-based file system, and the
2556 directory or the path name use backslashes, then we should
2557 use a backslash here. */
2558 s[dir_len] = '/';
2559 memcpy (s + dir_len + 1, path, path_len + 1);
2560 *string = s;
2563 return 1;
2566 /* Read a set of DWARF 5 line header format entries, setting *PCOUNT
2567 and *PPATHS. Return 1 on success, 0 on failure. */
2569 static int
2570 read_line_header_format_entries (struct backtrace_state *state,
2571 struct dwarf_data *ddata,
2572 struct unit *u,
2573 struct dwarf_buf *hdr_buf,
2574 struct line_header *hdr,
2575 size_t *pcount,
2576 const char ***ppaths)
2578 size_t formats_count;
2579 struct line_header_format *formats;
2580 size_t paths_count;
2581 const char **paths;
2582 size_t i;
2583 int ret;
2585 formats_count = read_byte (hdr_buf);
2586 if (formats_count == 0)
2587 formats = NULL;
2588 else
2590 formats = ((struct line_header_format *)
2591 backtrace_alloc (state,
2592 (formats_count
2593 * sizeof (struct line_header_format)),
2594 hdr_buf->error_callback,
2595 hdr_buf->data));
2596 if (formats == NULL)
2597 return 0;
2599 for (i = 0; i < formats_count; i++)
2601 formats[i].lnct = (int) read_uleb128(hdr_buf);
2602 formats[i].form = (enum dwarf_form) read_uleb128 (hdr_buf);
2606 paths_count = read_uleb128 (hdr_buf);
2607 if (paths_count == 0)
2609 *pcount = 0;
2610 *ppaths = NULL;
2611 ret = 1;
2612 goto exit;
2615 paths = ((const char **)
2616 backtrace_alloc (state, paths_count * sizeof (const char *),
2617 hdr_buf->error_callback, hdr_buf->data));
2618 if (paths == NULL)
2620 ret = 0;
2621 goto exit;
2623 for (i = 0; i < paths_count; i++)
2625 if (!read_lnct (state, ddata, u, hdr_buf, hdr, formats_count,
2626 formats, &paths[i]))
2628 backtrace_free (state, paths,
2629 paths_count * sizeof (const char *),
2630 hdr_buf->error_callback, hdr_buf->data);
2631 ret = 0;
2632 goto exit;
2636 *pcount = paths_count;
2637 *ppaths = paths;
2639 ret = 1;
2641 exit:
2642 if (formats != NULL)
2643 backtrace_free (state, formats,
2644 formats_count * sizeof (struct line_header_format),
2645 hdr_buf->error_callback, hdr_buf->data);
2647 return ret;
2650 /* Read the line header. Return 1 on success, 0 on failure. */
2652 static int
2653 read_line_header (struct backtrace_state *state, struct dwarf_data *ddata,
2654 struct unit *u, int is_dwarf64, struct dwarf_buf *line_buf,
2655 struct line_header *hdr)
2657 uint64_t hdrlen;
2658 struct dwarf_buf hdr_buf;
2660 hdr->version = read_uint16 (line_buf);
2661 if (hdr->version < 2 || hdr->version > 5)
2663 dwarf_buf_error (line_buf, "unsupported line number version", -1);
2664 return 0;
2667 if (hdr->version < 5)
2668 hdr->addrsize = u->addrsize;
2669 else
2671 hdr->addrsize = read_byte (line_buf);
2672 /* We could support a non-zero segment_selector_size but I doubt
2673 we'll ever see it. */
2674 if (read_byte (line_buf) != 0)
2676 dwarf_buf_error (line_buf,
2677 "non-zero segment_selector_size not supported",
2678 -1);
2679 return 0;
2683 hdrlen = read_offset (line_buf, is_dwarf64);
2685 hdr_buf = *line_buf;
2686 hdr_buf.left = hdrlen;
2688 if (!advance (line_buf, hdrlen))
2689 return 0;
2691 hdr->min_insn_len = read_byte (&hdr_buf);
2692 if (hdr->version < 4)
2693 hdr->max_ops_per_insn = 1;
2694 else
2695 hdr->max_ops_per_insn = read_byte (&hdr_buf);
2697 /* We don't care about default_is_stmt. */
2698 read_byte (&hdr_buf);
2700 hdr->line_base = read_sbyte (&hdr_buf);
2701 hdr->line_range = read_byte (&hdr_buf);
2703 hdr->opcode_base = read_byte (&hdr_buf);
2704 hdr->opcode_lengths = hdr_buf.buf;
2705 if (!advance (&hdr_buf, hdr->opcode_base - 1))
2706 return 0;
2708 if (hdr->version < 5)
2710 if (!read_v2_paths (state, u, &hdr_buf, hdr))
2711 return 0;
2713 else
2715 if (!read_line_header_format_entries (state, ddata, u, &hdr_buf, hdr,
2716 &hdr->dirs_count,
2717 &hdr->dirs))
2718 return 0;
2719 if (!read_line_header_format_entries (state, ddata, u, &hdr_buf, hdr,
2720 &hdr->filenames_count,
2721 &hdr->filenames))
2722 return 0;
2725 if (hdr_buf.reported_underflow)
2726 return 0;
2728 return 1;
2731 /* Read the line program, adding line mappings to VEC. Return 1 on
2732 success, 0 on failure. */
2734 static int
2735 read_line_program (struct backtrace_state *state, struct dwarf_data *ddata,
2736 const struct line_header *hdr, struct dwarf_buf *line_buf,
2737 struct line_vector *vec)
2739 uint64_t address;
2740 unsigned int op_index;
2741 const char *reset_filename;
2742 const char *filename;
2743 int lineno;
2745 address = 0;
2746 op_index = 0;
2747 if (hdr->filenames_count > 1)
2748 reset_filename = hdr->filenames[1];
2749 else
2750 reset_filename = "";
2751 filename = reset_filename;
2752 lineno = 1;
2753 while (line_buf->left > 0)
2755 unsigned int op;
2757 op = read_byte (line_buf);
2758 if (op >= hdr->opcode_base)
2760 unsigned int advance;
2762 /* Special opcode. */
2763 op -= hdr->opcode_base;
2764 advance = op / hdr->line_range;
2765 address += (hdr->min_insn_len * (op_index + advance)
2766 / hdr->max_ops_per_insn);
2767 op_index = (op_index + advance) % hdr->max_ops_per_insn;
2768 lineno += hdr->line_base + (int) (op % hdr->line_range);
2769 add_line (state, ddata, address, filename, lineno,
2770 line_buf->error_callback, line_buf->data, vec);
2772 else if (op == DW_LNS_extended_op)
2774 uint64_t len;
2776 len = read_uleb128 (line_buf);
2777 op = read_byte (line_buf);
2778 switch (op)
2780 case DW_LNE_end_sequence:
2781 /* FIXME: Should we mark the high PC here? It seems
2782 that we already have that information from the
2783 compilation unit. */
2784 address = 0;
2785 op_index = 0;
2786 filename = reset_filename;
2787 lineno = 1;
2788 break;
2789 case DW_LNE_set_address:
2790 address = read_address (line_buf, hdr->addrsize);
2791 break;
2792 case DW_LNE_define_file:
2794 const char *f;
2795 unsigned int dir_index;
2797 f = read_string (line_buf);
2798 if (f == NULL)
2799 return 0;
2800 dir_index = read_uleb128 (line_buf);
2801 /* Ignore that time and length. */
2802 read_uleb128 (line_buf);
2803 read_uleb128 (line_buf);
2804 if (IS_ABSOLUTE_PATH (f))
2805 filename = f;
2806 else
2808 const char *dir;
2809 size_t dir_len;
2810 size_t f_len;
2811 char *p;
2813 if (dir_index < hdr->dirs_count)
2814 dir = hdr->dirs[dir_index];
2815 else
2817 dwarf_buf_error (line_buf,
2818 ("invalid directory index "
2819 "in line number program"),
2821 return 0;
2823 dir_len = strlen (dir);
2824 f_len = strlen (f);
2825 p = ((char *)
2826 backtrace_alloc (state, dir_len + f_len + 2,
2827 line_buf->error_callback,
2828 line_buf->data));
2829 if (p == NULL)
2830 return 0;
2831 memcpy (p, dir, dir_len);
2832 /* FIXME: If we are on a DOS-based file system,
2833 and the directory or the file name use
2834 backslashes, then we should use a backslash
2835 here. */
2836 p[dir_len] = '/';
2837 memcpy (p + dir_len + 1, f, f_len + 1);
2838 filename = p;
2841 break;
2842 case DW_LNE_set_discriminator:
2843 /* We don't care about discriminators. */
2844 read_uleb128 (line_buf);
2845 break;
2846 default:
2847 if (!advance (line_buf, len - 1))
2848 return 0;
2849 break;
2852 else
2854 switch (op)
2856 case DW_LNS_copy:
2857 add_line (state, ddata, address, filename, lineno,
2858 line_buf->error_callback, line_buf->data, vec);
2859 break;
2860 case DW_LNS_advance_pc:
2862 uint64_t advance;
2864 advance = read_uleb128 (line_buf);
2865 address += (hdr->min_insn_len * (op_index + advance)
2866 / hdr->max_ops_per_insn);
2867 op_index = (op_index + advance) % hdr->max_ops_per_insn;
2869 break;
2870 case DW_LNS_advance_line:
2871 lineno += (int) read_sleb128 (line_buf);
2872 break;
2873 case DW_LNS_set_file:
2875 uint64_t fileno;
2877 fileno = read_uleb128 (line_buf);
2878 if (fileno >= hdr->filenames_count)
2880 dwarf_buf_error (line_buf,
2881 ("invalid file number in "
2882 "line number program"),
2884 return 0;
2886 filename = hdr->filenames[fileno];
2888 break;
2889 case DW_LNS_set_column:
2890 read_uleb128 (line_buf);
2891 break;
2892 case DW_LNS_negate_stmt:
2893 break;
2894 case DW_LNS_set_basic_block:
2895 break;
2896 case DW_LNS_const_add_pc:
2898 unsigned int advance;
2900 op = 255 - hdr->opcode_base;
2901 advance = op / hdr->line_range;
2902 address += (hdr->min_insn_len * (op_index + advance)
2903 / hdr->max_ops_per_insn);
2904 op_index = (op_index + advance) % hdr->max_ops_per_insn;
2906 break;
2907 case DW_LNS_fixed_advance_pc:
2908 address += read_uint16 (line_buf);
2909 op_index = 0;
2910 break;
2911 case DW_LNS_set_prologue_end:
2912 break;
2913 case DW_LNS_set_epilogue_begin:
2914 break;
2915 case DW_LNS_set_isa:
2916 read_uleb128 (line_buf);
2917 break;
2918 default:
2920 unsigned int i;
2922 for (i = hdr->opcode_lengths[op - 1]; i > 0; --i)
2923 read_uleb128 (line_buf);
2925 break;
2930 return 1;
2933 /* Read the line number information for a compilation unit. Returns 1
2934 on success, 0 on failure. */
2936 static int
2937 read_line_info (struct backtrace_state *state, struct dwarf_data *ddata,
2938 backtrace_error_callback error_callback, void *data,
2939 struct unit *u, struct line_header *hdr, struct line **lines,
2940 size_t *lines_count)
2942 struct line_vector vec;
2943 struct dwarf_buf line_buf;
2944 uint64_t len;
2945 int is_dwarf64;
2946 struct line *ln;
2948 memset (&vec.vec, 0, sizeof vec.vec);
2949 vec.count = 0;
2951 memset (hdr, 0, sizeof *hdr);
2953 if (u->lineoff != (off_t) (size_t) u->lineoff
2954 || (size_t) u->lineoff >= ddata->dwarf_sections.size[DEBUG_LINE])
2956 error_callback (data, "unit line offset out of range", 0);
2957 goto fail;
2960 line_buf.name = ".debug_line";
2961 line_buf.start = ddata->dwarf_sections.data[DEBUG_LINE];
2962 line_buf.buf = ddata->dwarf_sections.data[DEBUG_LINE] + u->lineoff;
2963 line_buf.left = ddata->dwarf_sections.size[DEBUG_LINE] - u->lineoff;
2964 line_buf.is_bigendian = ddata->is_bigendian;
2965 line_buf.error_callback = error_callback;
2966 line_buf.data = data;
2967 line_buf.reported_underflow = 0;
2969 len = read_initial_length (&line_buf, &is_dwarf64);
2970 line_buf.left = len;
2972 if (!read_line_header (state, ddata, u, is_dwarf64, &line_buf, hdr))
2973 goto fail;
2975 if (!read_line_program (state, ddata, hdr, &line_buf, &vec))
2976 goto fail;
2978 if (line_buf.reported_underflow)
2979 goto fail;
2981 if (vec.count == 0)
2983 /* This is not a failure in the sense of a generating an error,
2984 but it is a failure in that sense that we have no useful
2985 information. */
2986 goto fail;
2989 /* Allocate one extra entry at the end. */
2990 ln = ((struct line *)
2991 backtrace_vector_grow (state, sizeof (struct line), error_callback,
2992 data, &vec.vec));
2993 if (ln == NULL)
2994 goto fail;
2995 ln->pc = (uintptr_t) -1;
2996 ln->filename = NULL;
2997 ln->lineno = 0;
2998 ln->idx = 0;
3000 if (!backtrace_vector_release (state, &vec.vec, error_callback, data))
3001 goto fail;
3003 ln = (struct line *) vec.vec.base;
3004 backtrace_qsort (ln, vec.count, sizeof (struct line), line_compare);
3006 *lines = ln;
3007 *lines_count = vec.count;
3009 return 1;
3011 fail:
3012 backtrace_vector_free (state, &vec.vec, error_callback, data);
3013 free_line_header (state, hdr, error_callback, data);
3014 *lines = (struct line *) (uintptr_t) -1;
3015 *lines_count = 0;
3016 return 0;
3019 static const char *read_referenced_name (struct dwarf_data *, struct unit *,
3020 uint64_t, backtrace_error_callback,
3021 void *);
3023 /* Read the name of a function from a DIE referenced by ATTR with VAL. */
3025 static const char *
3026 read_referenced_name_from_attr (struct dwarf_data *ddata, struct unit *u,
3027 struct attr *attr, struct attr_val *val,
3028 backtrace_error_callback error_callback,
3029 void *data)
3031 switch (attr->name)
3033 case DW_AT_abstract_origin:
3034 case DW_AT_specification:
3035 break;
3036 default:
3037 return NULL;
3040 if (attr->form == DW_FORM_ref_sig8)
3041 return NULL;
3043 if (val->encoding == ATTR_VAL_REF_INFO)
3045 struct unit *unit
3046 = find_unit (ddata->units, ddata->units_count,
3047 val->u.uint);
3048 if (unit == NULL)
3049 return NULL;
3051 uint64_t offset = val->u.uint - unit->low_offset;
3052 return read_referenced_name (ddata, unit, offset, error_callback, data);
3055 if (val->encoding == ATTR_VAL_UINT
3056 || val->encoding == ATTR_VAL_REF_UNIT)
3057 return read_referenced_name (ddata, u, val->u.uint, error_callback, data);
3059 if (val->encoding == ATTR_VAL_REF_ALT_INFO)
3061 struct unit *alt_unit
3062 = find_unit (ddata->altlink->units, ddata->altlink->units_count,
3063 val->u.uint);
3064 if (alt_unit == NULL)
3065 return NULL;
3067 uint64_t offset = val->u.uint - alt_unit->low_offset;
3068 return read_referenced_name (ddata->altlink, alt_unit, offset,
3069 error_callback, data);
3072 return NULL;
3075 /* Read the name of a function from a DIE referenced by a
3076 DW_AT_abstract_origin or DW_AT_specification tag. OFFSET is within
3077 the same compilation unit. */
3079 static const char *
3080 read_referenced_name (struct dwarf_data *ddata, struct unit *u,
3081 uint64_t offset, backtrace_error_callback error_callback,
3082 void *data)
3084 struct dwarf_buf unit_buf;
3085 uint64_t code;
3086 const struct abbrev *abbrev;
3087 const char *ret;
3088 size_t i;
3090 /* OFFSET is from the start of the data for this compilation unit.
3091 U->unit_data is the data, but it starts U->unit_data_offset bytes
3092 from the beginning. */
3094 if (offset < u->unit_data_offset
3095 || offset - u->unit_data_offset >= u->unit_data_len)
3097 error_callback (data,
3098 "abstract origin or specification out of range",
3100 return NULL;
3103 offset -= u->unit_data_offset;
3105 unit_buf.name = ".debug_info";
3106 unit_buf.start = ddata->dwarf_sections.data[DEBUG_INFO];
3107 unit_buf.buf = u->unit_data + offset;
3108 unit_buf.left = u->unit_data_len - offset;
3109 unit_buf.is_bigendian = ddata->is_bigendian;
3110 unit_buf.error_callback = error_callback;
3111 unit_buf.data = data;
3112 unit_buf.reported_underflow = 0;
3114 code = read_uleb128 (&unit_buf);
3115 if (code == 0)
3117 dwarf_buf_error (&unit_buf,
3118 "invalid abstract origin or specification",
3120 return NULL;
3123 abbrev = lookup_abbrev (&u->abbrevs, code, error_callback, data);
3124 if (abbrev == NULL)
3125 return NULL;
3127 ret = NULL;
3128 for (i = 0; i < abbrev->num_attrs; ++i)
3130 struct attr_val val;
3132 if (!read_attribute (abbrev->attrs[i].form, abbrev->attrs[i].val,
3133 &unit_buf, u->is_dwarf64, u->version, u->addrsize,
3134 &ddata->dwarf_sections, ddata->altlink, &val))
3135 return NULL;
3137 switch (abbrev->attrs[i].name)
3139 case DW_AT_name:
3140 /* Third name preference: don't override. A name we found in some
3141 other way, will normally be more useful -- e.g., this name is
3142 normally not mangled. */
3143 if (ret != NULL)
3144 break;
3145 if (!resolve_string (&ddata->dwarf_sections, u->is_dwarf64,
3146 ddata->is_bigendian, u->str_offsets_base,
3147 &val, error_callback, data, &ret))
3148 return NULL;
3149 break;
3151 case DW_AT_linkage_name:
3152 case DW_AT_MIPS_linkage_name:
3153 /* First name preference: override all. */
3155 const char *s;
3157 s = NULL;
3158 if (!resolve_string (&ddata->dwarf_sections, u->is_dwarf64,
3159 ddata->is_bigendian, u->str_offsets_base,
3160 &val, error_callback, data, &s))
3161 return NULL;
3162 if (s != NULL)
3163 return s;
3165 break;
3167 case DW_AT_specification:
3168 /* Second name preference: override DW_AT_name, don't override
3169 DW_AT_linkage_name. */
3171 const char *name;
3173 name = read_referenced_name_from_attr (ddata, u, &abbrev->attrs[i],
3174 &val, error_callback, data);
3175 if (name != NULL)
3176 ret = name;
3178 break;
3180 default:
3181 break;
3185 return ret;
3188 /* Add a range to a unit that maps to a function. This is called via
3189 add_ranges. Returns 1 on success, 0 on error. */
3191 static int
3192 add_function_range (struct backtrace_state *state, void *rdata,
3193 uintptr_t lowpc, uintptr_t highpc,
3194 backtrace_error_callback error_callback, void *data,
3195 void *pvec)
3197 struct function *function = (struct function *) rdata;
3198 struct function_vector *vec = (struct function_vector *) pvec;
3199 struct function_addrs *p;
3201 if (vec->count > 0)
3203 p = (struct function_addrs *) vec->vec.base + (vec->count - 1);
3204 if ((lowpc == p->high || lowpc == p->high + 1)
3205 && function == p->function)
3207 if (highpc > p->high)
3208 p->high = highpc;
3209 return 1;
3213 p = ((struct function_addrs *)
3214 backtrace_vector_grow (state, sizeof (struct function_addrs),
3215 error_callback, data, &vec->vec));
3216 if (p == NULL)
3217 return 0;
3219 p->low = lowpc;
3220 p->high = highpc;
3221 p->function = function;
3223 ++vec->count;
3225 return 1;
3228 /* Read one entry plus all its children. Add function addresses to
3229 VEC. Returns 1 on success, 0 on error. */
3231 static int
3232 read_function_entry (struct backtrace_state *state, struct dwarf_data *ddata,
3233 struct unit *u, uintptr_t base, struct dwarf_buf *unit_buf,
3234 const struct line_header *lhdr,
3235 backtrace_error_callback error_callback, void *data,
3236 struct function_vector *vec_function,
3237 struct function_vector *vec_inlined)
3239 while (unit_buf->left > 0)
3241 uint64_t code;
3242 const struct abbrev *abbrev;
3243 int is_function;
3244 struct function *function;
3245 struct function_vector *vec;
3246 size_t i;
3247 struct pcrange pcrange;
3248 int have_linkage_name;
3250 code = read_uleb128 (unit_buf);
3251 if (code == 0)
3252 return 1;
3254 abbrev = lookup_abbrev (&u->abbrevs, code, error_callback, data);
3255 if (abbrev == NULL)
3256 return 0;
3258 is_function = (abbrev->tag == DW_TAG_subprogram
3259 || abbrev->tag == DW_TAG_entry_point
3260 || abbrev->tag == DW_TAG_inlined_subroutine);
3262 if (abbrev->tag == DW_TAG_inlined_subroutine)
3263 vec = vec_inlined;
3264 else
3265 vec = vec_function;
3267 function = NULL;
3268 if (is_function)
3270 function = ((struct function *)
3271 backtrace_alloc (state, sizeof *function,
3272 error_callback, data));
3273 if (function == NULL)
3274 return 0;
3275 memset (function, 0, sizeof *function);
3278 memset (&pcrange, 0, sizeof pcrange);
3279 have_linkage_name = 0;
3280 for (i = 0; i < abbrev->num_attrs; ++i)
3282 struct attr_val val;
3284 if (!read_attribute (abbrev->attrs[i].form, abbrev->attrs[i].val,
3285 unit_buf, u->is_dwarf64, u->version,
3286 u->addrsize, &ddata->dwarf_sections,
3287 ddata->altlink, &val))
3288 return 0;
3290 /* The compile unit sets the base address for any address
3291 ranges in the function entries. */
3292 if ((abbrev->tag == DW_TAG_compile_unit
3293 || abbrev->tag == DW_TAG_skeleton_unit)
3294 && abbrev->attrs[i].name == DW_AT_low_pc)
3296 if (val.encoding == ATTR_VAL_ADDRESS)
3297 base = (uintptr_t) val.u.uint;
3298 else if (val.encoding == ATTR_VAL_ADDRESS_INDEX)
3300 if (!resolve_addr_index (&ddata->dwarf_sections,
3301 u->addr_base, u->addrsize,
3302 ddata->is_bigendian, val.u.uint,
3303 error_callback, data, &base))
3304 return 0;
3308 if (is_function)
3310 switch (abbrev->attrs[i].name)
3312 case DW_AT_call_file:
3313 if (val.encoding == ATTR_VAL_UINT)
3315 if (val.u.uint >= lhdr->filenames_count)
3317 dwarf_buf_error (unit_buf,
3318 ("invalid file number in "
3319 "DW_AT_call_file attribute"),
3321 return 0;
3323 function->caller_filename = lhdr->filenames[val.u.uint];
3325 break;
3327 case DW_AT_call_line:
3328 if (val.encoding == ATTR_VAL_UINT)
3329 function->caller_lineno = val.u.uint;
3330 break;
3332 case DW_AT_abstract_origin:
3333 case DW_AT_specification:
3334 /* Second name preference: override DW_AT_name, don't override
3335 DW_AT_linkage_name. */
3336 if (have_linkage_name)
3337 break;
3339 const char *name;
3341 name
3342 = read_referenced_name_from_attr (ddata, u,
3343 &abbrev->attrs[i], &val,
3344 error_callback, data);
3345 if (name != NULL)
3346 function->name = name;
3348 break;
3350 case DW_AT_name:
3351 /* Third name preference: don't override. */
3352 if (function->name != NULL)
3353 break;
3354 if (!resolve_string (&ddata->dwarf_sections, u->is_dwarf64,
3355 ddata->is_bigendian,
3356 u->str_offsets_base, &val,
3357 error_callback, data, &function->name))
3358 return 0;
3359 break;
3361 case DW_AT_linkage_name:
3362 case DW_AT_MIPS_linkage_name:
3363 /* First name preference: override all. */
3365 const char *s;
3367 s = NULL;
3368 if (!resolve_string (&ddata->dwarf_sections, u->is_dwarf64,
3369 ddata->is_bigendian,
3370 u->str_offsets_base, &val,
3371 error_callback, data, &s))
3372 return 0;
3373 if (s != NULL)
3375 function->name = s;
3376 have_linkage_name = 1;
3379 break;
3381 case DW_AT_low_pc: case DW_AT_high_pc: case DW_AT_ranges:
3382 update_pcrange (&abbrev->attrs[i], &val, &pcrange);
3383 break;
3385 default:
3386 break;
3391 /* If we couldn't find a name for the function, we have no use
3392 for it. */
3393 if (is_function && function->name == NULL)
3395 backtrace_free (state, function, sizeof *function,
3396 error_callback, data);
3397 is_function = 0;
3400 if (is_function)
3402 if (pcrange.have_ranges
3403 || (pcrange.have_lowpc && pcrange.have_highpc))
3405 if (!add_ranges (state, &ddata->dwarf_sections,
3406 ddata->base_address, ddata->is_bigendian,
3407 u, base, &pcrange, add_function_range,
3408 (void *) function, error_callback, data,
3409 (void *) vec))
3410 return 0;
3412 else
3414 backtrace_free (state, function, sizeof *function,
3415 error_callback, data);
3416 is_function = 0;
3420 if (abbrev->has_children)
3422 if (!is_function)
3424 if (!read_function_entry (state, ddata, u, base, unit_buf, lhdr,
3425 error_callback, data, vec_function,
3426 vec_inlined))
3427 return 0;
3429 else
3431 struct function_vector fvec;
3433 /* Gather any information for inlined functions in
3434 FVEC. */
3436 memset (&fvec, 0, sizeof fvec);
3438 if (!read_function_entry (state, ddata, u, base, unit_buf, lhdr,
3439 error_callback, data, vec_function,
3440 &fvec))
3441 return 0;
3443 if (fvec.count > 0)
3445 struct function_addrs *p;
3446 struct function_addrs *faddrs;
3448 /* Allocate a trailing entry, but don't include it
3449 in fvec.count. */
3450 p = ((struct function_addrs *)
3451 backtrace_vector_grow (state,
3452 sizeof (struct function_addrs),
3453 error_callback, data,
3454 &fvec.vec));
3455 if (p == NULL)
3456 return 0;
3457 p->low = 0;
3458 --p->low;
3459 p->high = p->low;
3460 p->function = NULL;
3462 if (!backtrace_vector_release (state, &fvec.vec,
3463 error_callback, data))
3464 return 0;
3466 faddrs = (struct function_addrs *) fvec.vec.base;
3467 backtrace_qsort (faddrs, fvec.count,
3468 sizeof (struct function_addrs),
3469 function_addrs_compare);
3471 function->function_addrs = faddrs;
3472 function->function_addrs_count = fvec.count;
3478 return 1;
3481 /* Read function name information for a compilation unit. We look
3482 through the whole unit looking for function tags. */
3484 static void
3485 read_function_info (struct backtrace_state *state, struct dwarf_data *ddata,
3486 const struct line_header *lhdr,
3487 backtrace_error_callback error_callback, void *data,
3488 struct unit *u, struct function_vector *fvec,
3489 struct function_addrs **ret_addrs,
3490 size_t *ret_addrs_count)
3492 struct function_vector lvec;
3493 struct function_vector *pfvec;
3494 struct dwarf_buf unit_buf;
3495 struct function_addrs *p;
3496 struct function_addrs *addrs;
3497 size_t addrs_count;
3499 /* Use FVEC if it is not NULL. Otherwise use our own vector. */
3500 if (fvec != NULL)
3501 pfvec = fvec;
3502 else
3504 memset (&lvec, 0, sizeof lvec);
3505 pfvec = &lvec;
3508 unit_buf.name = ".debug_info";
3509 unit_buf.start = ddata->dwarf_sections.data[DEBUG_INFO];
3510 unit_buf.buf = u->unit_data;
3511 unit_buf.left = u->unit_data_len;
3512 unit_buf.is_bigendian = ddata->is_bigendian;
3513 unit_buf.error_callback = error_callback;
3514 unit_buf.data = data;
3515 unit_buf.reported_underflow = 0;
3517 while (unit_buf.left > 0)
3519 if (!read_function_entry (state, ddata, u, 0, &unit_buf, lhdr,
3520 error_callback, data, pfvec, pfvec))
3521 return;
3524 if (pfvec->count == 0)
3525 return;
3527 /* Allocate a trailing entry, but don't include it in
3528 pfvec->count. */
3529 p = ((struct function_addrs *)
3530 backtrace_vector_grow (state, sizeof (struct function_addrs),
3531 error_callback, data, &pfvec->vec));
3532 if (p == NULL)
3533 return;
3534 p->low = 0;
3535 --p->low;
3536 p->high = p->low;
3537 p->function = NULL;
3539 addrs_count = pfvec->count;
3541 if (fvec == NULL)
3543 if (!backtrace_vector_release (state, &lvec.vec, error_callback, data))
3544 return;
3545 addrs = (struct function_addrs *) pfvec->vec.base;
3547 else
3549 /* Finish this list of addresses, but leave the remaining space in
3550 the vector available for the next function unit. */
3551 addrs = ((struct function_addrs *)
3552 backtrace_vector_finish (state, &fvec->vec,
3553 error_callback, data));
3554 if (addrs == NULL)
3555 return;
3556 fvec->count = 0;
3559 backtrace_qsort (addrs, addrs_count, sizeof (struct function_addrs),
3560 function_addrs_compare);
3562 *ret_addrs = addrs;
3563 *ret_addrs_count = addrs_count;
3566 /* See if PC is inlined in FUNCTION. If it is, print out the inlined
3567 information, and update FILENAME and LINENO for the caller.
3568 Returns whatever CALLBACK returns, or 0 to keep going. */
3570 static int
3571 report_inlined_functions (uintptr_t pc, struct function *function,
3572 backtrace_full_callback callback, void *data,
3573 const char **filename, int *lineno)
3575 struct function_addrs *p;
3576 struct function_addrs *match;
3577 struct function *inlined;
3578 int ret;
3580 if (function->function_addrs_count == 0)
3581 return 0;
3583 /* Our search isn't safe if pc == -1, as that is the sentinel
3584 value. */
3585 if (pc + 1 == 0)
3586 return 0;
3588 p = ((struct function_addrs *)
3589 bsearch (&pc, function->function_addrs,
3590 function->function_addrs_count,
3591 sizeof (struct function_addrs),
3592 function_addrs_search));
3593 if (p == NULL)
3594 return 0;
3596 /* Here pc >= p->low && pc < (p + 1)->low. The function_addrs are
3597 sorted by low, so if pc > p->low we are at the end of a range of
3598 function_addrs with the same low value. If pc == p->low walk
3599 forward to the end of the range with that low value. Then walk
3600 backward and use the first range that includes pc. */
3601 while (pc == (p + 1)->low)
3602 ++p;
3603 match = NULL;
3604 while (1)
3606 if (pc < p->high)
3608 match = p;
3609 break;
3611 if (p == function->function_addrs)
3612 break;
3613 if ((p - 1)->low < p->low)
3614 break;
3615 --p;
3617 if (match == NULL)
3618 return 0;
3620 /* We found an inlined call. */
3622 inlined = match->function;
3624 /* Report any calls inlined into this one. */
3625 ret = report_inlined_functions (pc, inlined, callback, data,
3626 filename, lineno);
3627 if (ret != 0)
3628 return ret;
3630 /* Report this inlined call. */
3631 ret = callback (data, pc, *filename, *lineno, inlined->name);
3632 if (ret != 0)
3633 return ret;
3635 /* Our caller will report the caller of the inlined function; tell
3636 it the appropriate filename and line number. */
3637 *filename = inlined->caller_filename;
3638 *lineno = inlined->caller_lineno;
3640 return 0;
3643 /* Look for a PC in the DWARF mapping for one module. On success,
3644 call CALLBACK and return whatever it returns. On error, call
3645 ERROR_CALLBACK and return 0. Sets *FOUND to 1 if the PC is found,
3646 0 if not. */
3648 static int
3649 dwarf_lookup_pc (struct backtrace_state *state, struct dwarf_data *ddata,
3650 uintptr_t pc, backtrace_full_callback callback,
3651 backtrace_error_callback error_callback, void *data,
3652 int *found)
3654 struct unit_addrs *entry;
3655 int found_entry;
3656 struct unit *u;
3657 int new_data;
3658 struct line *lines;
3659 struct line *ln;
3660 struct function_addrs *p;
3661 struct function_addrs *fmatch;
3662 struct function *function;
3663 const char *filename;
3664 int lineno;
3665 int ret;
3667 *found = 1;
3669 /* Find an address range that includes PC. Our search isn't safe if
3670 PC == -1, as we use that as a sentinel value, so skip the search
3671 in that case. */
3672 entry = (ddata->addrs_count == 0 || pc + 1 == 0
3673 ? NULL
3674 : bsearch (&pc, ddata->addrs, ddata->addrs_count,
3675 sizeof (struct unit_addrs), unit_addrs_search));
3677 if (entry == NULL)
3679 *found = 0;
3680 return 0;
3683 /* Here pc >= entry->low && pc < (entry + 1)->low. The unit_addrs
3684 are sorted by low, so if pc > p->low we are at the end of a range
3685 of unit_addrs with the same low value. If pc == p->low walk
3686 forward to the end of the range with that low value. Then walk
3687 backward and use the first range that includes pc. */
3688 while (pc == (entry + 1)->low)
3689 ++entry;
3690 found_entry = 0;
3691 while (1)
3693 if (pc < entry->high)
3695 found_entry = 1;
3696 break;
3698 if (entry == ddata->addrs)
3699 break;
3700 if ((entry - 1)->low < entry->low)
3701 break;
3702 --entry;
3704 if (!found_entry)
3706 *found = 0;
3707 return 0;
3710 /* We need the lines, lines_count, function_addrs,
3711 function_addrs_count fields of u. If they are not set, we need
3712 to set them. When running in threaded mode, we need to allow for
3713 the possibility that some other thread is setting them
3714 simultaneously. */
3716 u = entry->u;
3717 lines = u->lines;
3719 /* Skip units with no useful line number information by walking
3720 backward. Useless line number information is marked by setting
3721 lines == -1. */
3722 while (entry > ddata->addrs
3723 && pc >= (entry - 1)->low
3724 && pc < (entry - 1)->high)
3726 if (state->threaded)
3727 lines = (struct line *) backtrace_atomic_load_pointer (&u->lines);
3729 if (lines != (struct line *) (uintptr_t) -1)
3730 break;
3732 --entry;
3734 u = entry->u;
3735 lines = u->lines;
3738 if (state->threaded)
3739 lines = backtrace_atomic_load_pointer (&u->lines);
3741 new_data = 0;
3742 if (lines == NULL)
3744 struct function_addrs *function_addrs;
3745 size_t function_addrs_count;
3746 struct line_header lhdr;
3747 size_t count;
3749 /* We have never read the line information for this unit. Read
3750 it now. */
3752 function_addrs = NULL;
3753 function_addrs_count = 0;
3754 if (read_line_info (state, ddata, error_callback, data, entry->u, &lhdr,
3755 &lines, &count))
3757 struct function_vector *pfvec;
3759 /* If not threaded, reuse DDATA->FVEC for better memory
3760 consumption. */
3761 if (state->threaded)
3762 pfvec = NULL;
3763 else
3764 pfvec = &ddata->fvec;
3765 read_function_info (state, ddata, &lhdr, error_callback, data,
3766 entry->u, pfvec, &function_addrs,
3767 &function_addrs_count);
3768 free_line_header (state, &lhdr, error_callback, data);
3769 new_data = 1;
3772 /* Atomically store the information we just read into the unit.
3773 If another thread is simultaneously writing, it presumably
3774 read the same information, and we don't care which one we
3775 wind up with; we just leak the other one. We do have to
3776 write the lines field last, so that the acquire-loads above
3777 ensure that the other fields are set. */
3779 if (!state->threaded)
3781 u->lines_count = count;
3782 u->function_addrs = function_addrs;
3783 u->function_addrs_count = function_addrs_count;
3784 u->lines = lines;
3786 else
3788 backtrace_atomic_store_size_t (&u->lines_count, count);
3789 backtrace_atomic_store_pointer (&u->function_addrs, function_addrs);
3790 backtrace_atomic_store_size_t (&u->function_addrs_count,
3791 function_addrs_count);
3792 backtrace_atomic_store_pointer (&u->lines, lines);
3796 /* Now all fields of U have been initialized. */
3798 if (lines == (struct line *) (uintptr_t) -1)
3800 /* If reading the line number information failed in some way,
3801 try again to see if there is a better compilation unit for
3802 this PC. */
3803 if (new_data)
3804 return dwarf_lookup_pc (state, ddata, pc, callback, error_callback,
3805 data, found);
3806 return callback (data, pc, NULL, 0, NULL);
3809 /* Search for PC within this unit. */
3811 ln = (struct line *) bsearch (&pc, lines, entry->u->lines_count,
3812 sizeof (struct line), line_search);
3813 if (ln == NULL)
3815 /* The PC is between the low_pc and high_pc attributes of the
3816 compilation unit, but no entry in the line table covers it.
3817 This implies that the start of the compilation unit has no
3818 line number information. */
3820 if (entry->u->abs_filename == NULL)
3822 const char *filename;
3824 filename = entry->u->filename;
3825 if (filename != NULL
3826 && !IS_ABSOLUTE_PATH (filename)
3827 && entry->u->comp_dir != NULL)
3829 size_t filename_len;
3830 const char *dir;
3831 size_t dir_len;
3832 char *s;
3834 filename_len = strlen (filename);
3835 dir = entry->u->comp_dir;
3836 dir_len = strlen (dir);
3837 s = (char *) backtrace_alloc (state, dir_len + filename_len + 2,
3838 error_callback, data);
3839 if (s == NULL)
3841 *found = 0;
3842 return 0;
3844 memcpy (s, dir, dir_len);
3845 /* FIXME: Should use backslash if DOS file system. */
3846 s[dir_len] = '/';
3847 memcpy (s + dir_len + 1, filename, filename_len + 1);
3848 filename = s;
3850 entry->u->abs_filename = filename;
3853 return callback (data, pc, entry->u->abs_filename, 0, NULL);
3856 /* Search for function name within this unit. */
3858 if (entry->u->function_addrs_count == 0)
3859 return callback (data, pc, ln->filename, ln->lineno, NULL);
3861 p = ((struct function_addrs *)
3862 bsearch (&pc, entry->u->function_addrs,
3863 entry->u->function_addrs_count,
3864 sizeof (struct function_addrs),
3865 function_addrs_search));
3866 if (p == NULL)
3867 return callback (data, pc, ln->filename, ln->lineno, NULL);
3869 /* Here pc >= p->low && pc < (p + 1)->low. The function_addrs are
3870 sorted by low, so if pc > p->low we are at the end of a range of
3871 function_addrs with the same low value. If pc == p->low walk
3872 forward to the end of the range with that low value. Then walk
3873 backward and use the first range that includes pc. */
3874 while (pc == (p + 1)->low)
3875 ++p;
3876 fmatch = NULL;
3877 while (1)
3879 if (pc < p->high)
3881 fmatch = p;
3882 break;
3884 if (p == entry->u->function_addrs)
3885 break;
3886 if ((p - 1)->low < p->low)
3887 break;
3888 --p;
3890 if (fmatch == NULL)
3891 return callback (data, pc, ln->filename, ln->lineno, NULL);
3893 function = fmatch->function;
3895 filename = ln->filename;
3896 lineno = ln->lineno;
3898 ret = report_inlined_functions (pc, function, callback, data,
3899 &filename, &lineno);
3900 if (ret != 0)
3901 return ret;
3903 return callback (data, pc, filename, lineno, function->name);
3907 /* Return the file/line information for a PC using the DWARF mapping
3908 we built earlier. */
3910 static int
3911 dwarf_fileline (struct backtrace_state *state, uintptr_t pc,
3912 backtrace_full_callback callback,
3913 backtrace_error_callback error_callback, void *data)
3915 struct dwarf_data *ddata;
3916 int found;
3917 int ret;
3919 if (!state->threaded)
3921 for (ddata = (struct dwarf_data *) state->fileline_data;
3922 ddata != NULL;
3923 ddata = ddata->next)
3925 ret = dwarf_lookup_pc (state, ddata, pc, callback, error_callback,
3926 data, &found);
3927 if (ret != 0 || found)
3928 return ret;
3931 else
3933 struct dwarf_data **pp;
3935 pp = (struct dwarf_data **) (void *) &state->fileline_data;
3936 while (1)
3938 ddata = backtrace_atomic_load_pointer (pp);
3939 if (ddata == NULL)
3940 break;
3942 ret = dwarf_lookup_pc (state, ddata, pc, callback, error_callback,
3943 data, &found);
3944 if (ret != 0 || found)
3945 return ret;
3947 pp = &ddata->next;
3951 /* FIXME: See if any libraries have been dlopen'ed. */
3953 return callback (data, pc, NULL, 0, NULL);
3956 /* Initialize our data structures from the DWARF debug info for a
3957 file. Return NULL on failure. */
3959 static struct dwarf_data *
3960 build_dwarf_data (struct backtrace_state *state,
3961 struct libbacktrace_base_address base_address,
3962 const struct dwarf_sections *dwarf_sections,
3963 int is_bigendian,
3964 struct dwarf_data *altlink,
3965 backtrace_error_callback error_callback,
3966 void *data)
3968 struct unit_addrs_vector addrs_vec;
3969 struct unit_addrs *addrs;
3970 size_t addrs_count;
3971 struct unit_vector units_vec;
3972 struct unit **units;
3973 size_t units_count;
3974 struct dwarf_data *fdata;
3976 if (!build_address_map (state, base_address, dwarf_sections, is_bigendian,
3977 altlink, error_callback, data, &addrs_vec,
3978 &units_vec))
3979 return NULL;
3981 if (!backtrace_vector_release (state, &addrs_vec.vec, error_callback, data))
3982 return NULL;
3983 if (!backtrace_vector_release (state, &units_vec.vec, error_callback, data))
3984 return NULL;
3985 addrs = (struct unit_addrs *) addrs_vec.vec.base;
3986 units = (struct unit **) units_vec.vec.base;
3987 addrs_count = addrs_vec.count;
3988 units_count = units_vec.count;
3989 backtrace_qsort (addrs, addrs_count, sizeof (struct unit_addrs),
3990 unit_addrs_compare);
3991 /* No qsort for units required, already sorted. */
3993 fdata = ((struct dwarf_data *)
3994 backtrace_alloc (state, sizeof (struct dwarf_data),
3995 error_callback, data));
3996 if (fdata == NULL)
3997 return NULL;
3999 fdata->next = NULL;
4000 fdata->altlink = altlink;
4001 fdata->base_address = base_address;
4002 fdata->addrs = addrs;
4003 fdata->addrs_count = addrs_count;
4004 fdata->units = units;
4005 fdata->units_count = units_count;
4006 fdata->dwarf_sections = *dwarf_sections;
4007 fdata->is_bigendian = is_bigendian;
4008 memset (&fdata->fvec, 0, sizeof fdata->fvec);
4010 return fdata;
4013 /* Build our data structures from the DWARF sections for a module.
4014 Set FILELINE_FN and STATE->FILELINE_DATA. Return 1 on success, 0
4015 on failure. */
4018 backtrace_dwarf_add (struct backtrace_state *state,
4019 struct libbacktrace_base_address base_address,
4020 const struct dwarf_sections *dwarf_sections,
4021 int is_bigendian,
4022 struct dwarf_data *fileline_altlink,
4023 backtrace_error_callback error_callback,
4024 void *data, fileline *fileline_fn,
4025 struct dwarf_data **fileline_entry)
4027 struct dwarf_data *fdata;
4029 fdata = build_dwarf_data (state, base_address, dwarf_sections, is_bigendian,
4030 fileline_altlink, error_callback, data);
4031 if (fdata == NULL)
4032 return 0;
4034 if (fileline_entry != NULL)
4035 *fileline_entry = fdata;
4037 if (!state->threaded)
4039 struct dwarf_data **pp;
4041 for (pp = (struct dwarf_data **) (void *) &state->fileline_data;
4042 *pp != NULL;
4043 pp = &(*pp)->next)
4045 *pp = fdata;
4047 else
4049 while (1)
4051 struct dwarf_data **pp;
4053 pp = (struct dwarf_data **) (void *) &state->fileline_data;
4055 while (1)
4057 struct dwarf_data *p;
4059 p = backtrace_atomic_load_pointer (pp);
4061 if (p == NULL)
4062 break;
4064 pp = &p->next;
4067 if (__sync_bool_compare_and_swap (pp, NULL, fdata))
4068 break;
4072 *fileline_fn = dwarf_fileline;
4074 return 1;