]> git.ipfire.org Git - thirdparty/gcc.git/blame - libbacktrace/dwarf.c
Update copyright years in libbacktrace.
[thirdparty/gcc.git] / libbacktrace / dwarf.c
CommitLineData
eff02e4f 1/* dwarf.c -- Get file/line information from DWARF for backtraces.
f8a7e1a4 2 Copyright (C) 2012-2013 Free Software Foundation, Inc.
eff02e4f
ILT
3 Written by Ian Lance Taylor, Google.
4
5Redistribution and use in source and binary forms, with or without
6modification, are permitted provided that the following conditions are
7met:
8
9 (1) Redistributions of source code must retain the above copyright
10 notice, this list of conditions and the following disclaimer.
11
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.
16
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.
20
21THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
25INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
29STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
30IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31POSSIBILITY OF SUCH DAMAGE. */
32
33#include "config.h"
34
35#include <errno.h>
eff02e4f
ILT
36#include <stdlib.h>
37#include <string.h>
38#include <sys/types.h>
39
40#include "dwarf2.h"
41#include "filenames.h"
42
43#include "backtrace.h"
44#include "internal.h"
45
30e15876 46#if !defined(HAVE_DECL_STRNLEN) || !HAVE_DECL_STRNLEN
244e2d9c
ILT
47
48/* If strnlen is not declared, provide our own version. */
49
50static size_t
51xstrnlen (const char *s, size_t maxlen)
52{
53 size_t i;
54
55 for (i = 0; i < maxlen; ++i)
56 if (s[i] == '\0')
57 break;
58 return i;
59}
60
61#define strnlen xstrnlen
62
772a71a9
ILT
63#endif
64
eff02e4f
ILT
65/* A buffer to read DWARF info. */
66
67struct dwarf_buf
68{
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;
85};
86
87/* A single attribute in a DWARF abbreviation. */
88
89struct attr
90{
91 /* The attribute name. */
92 enum dwarf_attribute name;
93 /* The attribute form. */
94 enum dwarf_form form;
95};
96
97/* A single DWARF abbreviation. */
98
99struct abbrev
100{
101 /* The abbrev code--the number used to refer to the abbrev. */
102 uint64_t code;
103 /* The entry tag. */
104 enum dwarf_tag tag;
105 /* Non-zero if this abbrev has child entries. */
106 int has_children;
107 /* The number of attributes. */
108 size_t num_attrs;
109 /* The attributes. */
110 struct attr *attrs;
111};
112
113/* The DWARF abbreviations for a compilation unit. This structure
114 only exists while reading the compilation unit. Most DWARF readers
115 seem to a hash table to map abbrev ID's to abbrev entries.
116 However, we primarily care about GCC, and GCC simply issues ID's in
117 numerical order starting at 1. So we simply keep a sorted vector,
118 and try to just look up the code. */
119
120struct abbrevs
121{
122 /* The number of abbrevs in the vector. */
123 size_t num_abbrevs;
124 /* The abbrevs, sorted by the code field. */
125 struct abbrev *abbrevs;
126};
127
128/* The different kinds of attribute values. */
129
130enum attr_val_encoding
131{
132 /* An address. */
133 ATTR_VAL_ADDRESS,
134 /* A unsigned integer. */
135 ATTR_VAL_UINT,
136 /* A sigd integer. */
137 ATTR_VAL_SINT,
138 /* A string. */
139 ATTR_VAL_STRING,
140 /* An offset to other data in the containing unit. */
141 ATTR_VAL_REF_UNIT,
142 /* An offset to other data within the .dwarf_info section. */
143 ATTR_VAL_REF_INFO,
144 /* An offset to data in some other section. */
145 ATTR_VAL_REF_SECTION,
146 /* A type signature. */
147 ATTR_VAL_REF_TYPE,
148 /* A block of data (not represented). */
149 ATTR_VAL_BLOCK,
150 /* An expression (not represented). */
151 ATTR_VAL_EXPR,
152};
153
154/* An attribute value. */
155
156struct attr_val
157{
158 /* How the value is stored in the field u. */
159 enum attr_val_encoding encoding;
160 union
161 {
162 /* ATTR_VAL_ADDRESS, ATTR_VAL_UINT, ATTR_VAL_REF*. */
163 uint64_t uint;
164 /* ATTR_VAL_SINT. */
165 int64_t sint;
166 /* ATTR_VAL_STRING. */
167 const char *string;
168 /* ATTR_VAL_BLOCK not stored. */
169 } u;
170};
171
172/* The line number program header. */
173
174struct line_header
175{
176 /* The version of the line number information. */
177 int version;
178 /* The minimum instruction length. */
179 unsigned int min_insn_len;
180 /* The maximum number of ops per instruction. */
181 unsigned int max_ops_per_insn;
182 /* The line base for special opcodes. */
183 int line_base;
184 /* The line range for special opcodes. */
185 unsigned int line_range;
186 /* The opcode base--the first special opcode. */
187 unsigned int opcode_base;
188 /* Opcode lengths, indexed by opcode - 1. */
189 const unsigned char *opcode_lengths;
190 /* The number of directory entries. */
191 size_t dirs_count;
192 /* The directory entries. */
193 const char **dirs;
194 /* The number of filenames. */
195 size_t filenames_count;
196 /* The filenames. */
197 const char **filenames;
198};
199
200/* Map a single PC value to a file/line. We will keep a vector of
201 these sorted by PC value. Each file/line will be correct from the
202 PC up to the PC of the next entry if there is one. We allocate one
203 extra entry at the end so that we can use bsearch. */
204
205struct line
206{
207 /* PC. */
208 uintptr_t pc;
209 /* File name. Many entries in the array are expected to point to
210 the same file name. */
211 const char *filename;
212 /* Line number. */
213 int lineno;
214};
215
216/* A growable vector of line number information. This is used while
217 reading the line numbers. */
218
219struct line_vector
220{
221 /* Memory. This is an array of struct line. */
222 struct backtrace_vector vec;
223 /* Number of valid mappings. */
224 size_t count;
225};
226
227/* A function described in the debug info. */
228
229struct function
230{
231 /* The name of the function. */
232 const char *name;
233 /* If this is an inlined function, the filename of the call
234 site. */
235 const char *caller_filename;
236 /* If this is an inlined function, the line number of the call
237 site. */
238 int caller_lineno;
239 /* Map PC ranges to inlined functions. */
240 struct function_addrs *function_addrs;
241 size_t function_addrs_count;
242};
243
244/* An address range for a function. This maps a PC value to a
245 specific function. */
246
247struct function_addrs
248{
249 /* Range is LOW <= PC < HIGH. */
250 uint64_t low;
251 uint64_t high;
252 /* Function for this address range. */
253 struct function *function;
254};
255
256/* A growable vector of function address ranges. */
257
258struct function_vector
259{
260 /* Memory. This is an array of struct function_addrs. */
261 struct backtrace_vector vec;
262 /* Number of address ranges present. */
263 size_t count;
264};
265
266/* A DWARF compilation unit. This only holds the information we need
267 to map a PC to a file and line. */
268
269struct unit
270{
271 /* The first entry for this compilation unit. */
272 const unsigned char *unit_data;
273 /* The length of the data for this compilation unit. */
274 size_t unit_data_len;
275 /* The offset of UNIT_DATA from the start of the information for
276 this compilation unit. */
277 size_t unit_data_offset;
278 /* DWARF version. */
279 int version;
280 /* Whether unit is DWARF64. */
281 int is_dwarf64;
282 /* Address size. */
283 int addrsize;
284 /* Offset into line number information. */
285 off_t lineoff;
286 /* Compilation command working directory. */
287 const char *comp_dir;
288 /* The abbreviations for this unit. */
289 struct abbrevs abbrevs;
290
291 /* The fields above this point are read in during initialization and
292 may be accessed freely. The fields below this point are read in
293 as needed, and therefore require care, as different threads may
294 try to initialize them simultaneously. */
295
296 /* PC to line number mapping. This is NULL if the values have not
297 been read. This is (struct line *) -1 if there was an error
298 reading the values. */
299 struct line *lines;
300 /* Number of entries in lines. */
301 size_t lines_count;
302 /* PC ranges to function. */
303 struct function_addrs *function_addrs;
304 size_t function_addrs_count;
305};
306
307/* An address range for a compilation unit. This maps a PC value to a
308 specific compilation unit. Note that we invert the representation
309 in DWARF: instead of listing the units and attaching a list of
310 ranges, we list the ranges and have each one point to the unit.
311 This lets us do a binary search to find the unit. */
312
313struct unit_addrs
314{
315 /* Range is LOW <= PC < HIGH. */
316 uint64_t low;
317 uint64_t high;
318 /* Compilation unit for this address range. */
319 struct unit *u;
320};
321
322/* A growable vector of compilation unit address ranges. */
323
324struct unit_addrs_vector
325{
326 /* Memory. This is an array of struct unit_addrs. */
327 struct backtrace_vector vec;
328 /* Number of address ranges present. */
329 size_t count;
330};
331
332/* The information we need to map a PC to a file and line. */
333
334struct dwarf_data
335{
e561a992
ILT
336 /* The data for the next file we know about. */
337 struct dwarf_data *next;
338 /* The base address for this file. */
339 uintptr_t base_address;
eff02e4f
ILT
340 /* A sorted list of address ranges. */
341 struct unit_addrs *addrs;
342 /* Number of address ranges in list. */
343 size_t addrs_count;
344 /* The unparsed .debug_info section. */
345 const unsigned char *dwarf_info;
346 size_t dwarf_info_size;
347 /* The unparsed .debug_line section. */
348 const unsigned char *dwarf_line;
349 size_t dwarf_line_size;
350 /* The unparsed .debug_ranges section. */
351 const unsigned char *dwarf_ranges;
352 size_t dwarf_ranges_size;
353 /* The unparsed .debug_str section. */
354 const unsigned char *dwarf_str;
355 size_t dwarf_str_size;
356 /* Whether the data is big-endian or not. */
357 int is_bigendian;
358 /* A vector used for function addresses. We keep this here so that
359 we can grow the vector as we read more functions. */
360 struct function_vector fvec;
361};
362
363/* Report an error for a DWARF buffer. */
364
365static void
366dwarf_buf_error (struct dwarf_buf *buf, const char *msg)
367{
368 char b[200];
369
370 snprintf (b, sizeof b, "%s in %s at %d",
371 msg, buf->name, (int) (buf->buf - buf->start));
372 buf->error_callback (buf->data, b, 0);
373}
374
375/* Require at least COUNT bytes in BUF. Return 1 if all is well, 0 on
376 error. */
377
378static int
379require (struct dwarf_buf *buf, size_t count)
380{
381 if (buf->left >= count)
382 return 1;
383
384 if (!buf->reported_underflow)
385 {
386 dwarf_buf_error (buf, "DWARF underflow");
387 buf->reported_underflow = 1;
388 }
389
390 return 0;
391}
392
393/* Advance COUNT bytes in BUF. Return 1 if all is well, 0 on
394 error. */
395
396static int
397advance (struct dwarf_buf *buf, size_t count)
398{
399 if (!require (buf, count))
400 return 0;
401 buf->buf += count;
402 buf->left -= count;
403 return 1;
404}
405
406/* Read one byte from BUF and advance 1 byte. */
407
408static unsigned char
409read_byte (struct dwarf_buf *buf)
410{
411 const unsigned char *p = buf->buf;
412
413 if (!advance (buf, 1))
414 return 0;
415 return p[0];
416}
417
418/* Read a signed char from BUF and advance 1 byte. */
419
420static signed char
421read_sbyte (struct dwarf_buf *buf)
422{
423 const unsigned char *p = buf->buf;
424
425 if (!advance (buf, 1))
426 return 0;
427 return (*p ^ 0x80) - 0x80;
428}
429
430/* Read a uint16 from BUF and advance 2 bytes. */
431
432static uint16_t
433read_uint16 (struct dwarf_buf *buf)
434{
435 const unsigned char *p = buf->buf;
436
437 if (!advance (buf, 2))
438 return 0;
439 if (buf->is_bigendian)
440 return ((uint16_t) p[0] << 8) | (uint16_t) p[1];
441 else
442 return ((uint16_t) p[1] << 8) | (uint16_t) p[0];
443}
444
445/* Read a uint32 from BUF and advance 4 bytes. */
446
447static uint32_t
448read_uint32 (struct dwarf_buf *buf)
449{
450 const unsigned char *p = buf->buf;
451
452 if (!advance (buf, 4))
453 return 0;
454 if (buf->is_bigendian)
455 return (((uint32_t) p[0] << 24) | ((uint32_t) p[1] << 16)
456 | ((uint32_t) p[2] << 8) | (uint32_t) p[3]);
457 else
458 return (((uint32_t) p[3] << 24) | ((uint32_t) p[2] << 16)
459 | ((uint32_t) p[1] << 8) | (uint32_t) p[0]);
460}
461
462/* Read a uint64 from BUF and advance 8 bytes. */
463
464static uint64_t
465read_uint64 (struct dwarf_buf *buf)
466{
467 const unsigned char *p = buf->buf;
468
469 if (!advance (buf, 8))
470 return 0;
471 if (buf->is_bigendian)
472 return (((uint64_t) p[0] << 56) | ((uint64_t) p[1] << 48)
473 | ((uint64_t) p[2] << 40) | ((uint64_t) p[3] << 32)
474 | ((uint64_t) p[4] << 24) | ((uint64_t) p[5] << 16)
475 | ((uint64_t) p[6] << 8) | (uint64_t) p[7]);
476 else
477 return (((uint64_t) p[7] << 56) | ((uint64_t) p[6] << 48)
478 | ((uint64_t) p[5] << 40) | ((uint64_t) p[4] << 32)
479 | ((uint64_t) p[3] << 24) | ((uint64_t) p[2] << 16)
480 | ((uint64_t) p[1] << 8) | (uint64_t) p[0]);
481}
482
483/* Read an offset from BUF and advance the appropriate number of
484 bytes. */
485
486static uint64_t
487read_offset (struct dwarf_buf *buf, int is_dwarf64)
488{
489 if (is_dwarf64)
490 return read_uint64 (buf);
491 else
492 return read_uint32 (buf);
493}
494
495/* Read an address from BUF and advance the appropriate number of
496 bytes. */
497
498static uint64_t
499read_address (struct dwarf_buf *buf, int addrsize)
500{
501 switch (addrsize)
502 {
503 case 1:
504 return read_byte (buf);
505 case 2:
506 return read_uint16 (buf);
507 case 4:
508 return read_uint32 (buf);
509 case 8:
510 return read_uint64 (buf);
511 default:
512 dwarf_buf_error (buf, "unrecognized address size");
513 return 0;
514 }
515}
516
517/* Return whether a value is the highest possible address, given the
518 address size. */
519
520static int
521is_highest_address (uint64_t address, int addrsize)
522{
523 switch (addrsize)
524 {
525 case 1:
526 return address == (unsigned char) -1;
527 case 2:
528 return address == (uint16_t) -1;
529 case 4:
530 return address == (uint32_t) -1;
531 case 8:
532 return address == (uint64_t) -1;
533 default:
534 return 0;
535 }
536}
537
538/* Read an unsigned LEB128 number. */
539
540static uint64_t
541read_uleb128 (struct dwarf_buf *buf)
542{
543 uint64_t ret;
544 unsigned int shift;
be4ba8ae 545 int overflow;
eff02e4f
ILT
546 unsigned char b;
547
548 ret = 0;
549 shift = 0;
be4ba8ae 550 overflow = 0;
eff02e4f
ILT
551 do
552 {
553 const unsigned char *p;
554
555 p = buf->buf;
556 if (!advance (buf, 1))
557 return 0;
558 b = *p;
be4ba8ae
ILT
559 if (shift < 64)
560 ret |= ((uint64_t) (b & 0x7f)) << shift;
561 else if (!overflow)
562 {
563 dwarf_buf_error (buf, "LEB128 overflows uint64_t");
564 overflow = 1;
565 }
eff02e4f
ILT
566 shift += 7;
567 }
568 while ((b & 0x80) != 0);
569
eff02e4f
ILT
570 return ret;
571}
572
573/* Read a signed LEB128 number. */
574
575static int64_t
576read_sleb128 (struct dwarf_buf *buf)
577{
578 uint64_t val;
579 unsigned int shift;
be4ba8ae 580 int overflow;
eff02e4f
ILT
581 unsigned char b;
582
583 val = 0;
584 shift = 0;
be4ba8ae 585 overflow = 0;
eff02e4f
ILT
586 do
587 {
588 const unsigned char *p;
589
590 p = buf->buf;
591 if (!advance (buf, 1))
592 return 0;
593 b = *p;
be4ba8ae
ILT
594 if (shift < 64)
595 val |= ((uint64_t) (b & 0x7f)) << shift;
596 else if (!overflow)
597 {
598 dwarf_buf_error (buf, "signed LEB128 overflows uint64_t");
599 overflow = 1;
600 }
eff02e4f
ILT
601 shift += 7;
602 }
603 while ((b & 0x80) != 0);
604
be4ba8ae 605 if ((b & 0x40) != 0 && shift < 64)
eff02e4f
ILT
606 val |= ((uint64_t) -1) << shift;
607
608 return (int64_t) val;
609}
610
611/* Return the length of an LEB128 number. */
612
613static size_t
614leb128_len (const unsigned char *p)
615{
616 size_t ret;
617
618 ret = 1;
619 while ((*p & 0x80) != 0)
620 {
621 ++p;
622 ++ret;
623 }
624 return ret;
625}
626
627/* Free an abbreviations structure. */
628
629static void
630free_abbrevs (struct backtrace_state *state, struct abbrevs *abbrevs,
631 backtrace_error_callback error_callback, void *data)
632{
633 size_t i;
634
635 for (i = 0; i < abbrevs->num_abbrevs; ++i)
636 backtrace_free (state, abbrevs->abbrevs[i].attrs,
637 abbrevs->abbrevs[i].num_attrs * sizeof (struct attr),
638 error_callback, data);
639 backtrace_free (state, abbrevs->abbrevs,
640 abbrevs->num_abbrevs * sizeof (struct abbrev),
641 error_callback, data);
642 abbrevs->num_abbrevs = 0;
643 abbrevs->abbrevs = NULL;
644}
645
646/* Read an attribute value. Returns 1 on success, 0 on failure. If
647 the value can be represented as a uint64_t, sets *VAL and sets
648 *IS_VALID to 1. We don't try to store the value of other attribute
649 forms, because we don't care about them. */
650
651static int
652read_attribute (enum dwarf_form form, struct dwarf_buf *buf,
653 int is_dwarf64, int version, int addrsize,
654 const unsigned char *dwarf_str, size_t dwarf_str_size,
655 struct attr_val *val)
656{
b20ade36
ILT
657 /* Avoid warnings about val.u.FIELD may be used uninitialized if
658 this function is inlined. The warnings aren't valid but can
659 occur because the different fields are set and used
660 conditionally. */
661 memset (val, 0, sizeof *val);
662
eff02e4f
ILT
663 switch (form)
664 {
665 case DW_FORM_addr:
666 val->encoding = ATTR_VAL_ADDRESS;
667 val->u.uint = read_address (buf, addrsize);
668 return 1;
669 case DW_FORM_block2:
670 val->encoding = ATTR_VAL_BLOCK;
671 return advance (buf, read_uint16 (buf));
672 case DW_FORM_block4:
673 val->encoding = ATTR_VAL_BLOCK;
674 return advance (buf, read_uint32 (buf));
675 case DW_FORM_data2:
676 val->encoding = ATTR_VAL_UINT;
677 val->u.uint = read_uint16 (buf);
678 return 1;
679 case DW_FORM_data4:
680 val->encoding = ATTR_VAL_UINT;
681 val->u.uint = read_uint32 (buf);
682 return 1;
683 case DW_FORM_data8:
684 val->encoding = ATTR_VAL_UINT;
685 val->u.uint = read_uint64 (buf);
686 return 1;
687 case DW_FORM_string:
688 val->encoding = ATTR_VAL_STRING;
689 val->u.string = (const char *) buf->buf;
690 return advance (buf, strnlen ((const char *) buf->buf, buf->left) + 1);
691 case DW_FORM_block:
692 val->encoding = ATTR_VAL_BLOCK;
693 return advance (buf, read_uleb128 (buf));
694 case DW_FORM_block1:
695 val->encoding = ATTR_VAL_BLOCK;
696 return advance (buf, read_byte (buf));
697 case DW_FORM_data1:
698 val->encoding = ATTR_VAL_UINT;
699 val->u.uint = read_byte (buf);
700 return 1;
701 case DW_FORM_flag:
702 val->encoding = ATTR_VAL_UINT;
703 val->u.uint = read_byte (buf);
704 return 1;
705 case DW_FORM_sdata:
706 val->encoding = ATTR_VAL_SINT;
707 val->u.sint = read_sleb128 (buf);
708 return 1;
709 case DW_FORM_strp:
710 {
711 uint64_t offset;
712
713 offset = read_offset (buf, is_dwarf64);
714 if (offset >= dwarf_str_size)
715 {
716 dwarf_buf_error (buf, "DW_FORM_strp out of range");
717 return 0;
718 }
719 val->encoding = ATTR_VAL_STRING;
720 val->u.string = (const char *) dwarf_str + offset;
721 return 1;
722 }
723 case DW_FORM_udata:
724 val->encoding = ATTR_VAL_UINT;
725 val->u.uint = read_uleb128 (buf);
726 return 1;
727 case DW_FORM_ref_addr:
728 val->encoding = ATTR_VAL_REF_INFO;
729 if (version == 2)
730 val->u.uint = read_address (buf, addrsize);
731 else
732 val->u.uint = read_offset (buf, is_dwarf64);
733 return 1;
734 case DW_FORM_ref1:
735 val->encoding = ATTR_VAL_REF_UNIT;
736 val->u.uint = read_byte (buf);
737 return 1;
738 case DW_FORM_ref2:
739 val->encoding = ATTR_VAL_REF_UNIT;
740 val->u.uint = read_uint16 (buf);
741 return 1;
742 case DW_FORM_ref4:
743 val->encoding = ATTR_VAL_REF_UNIT;
744 val->u.uint = read_uint32 (buf);
745 return 1;
746 case DW_FORM_ref8:
747 val->encoding = ATTR_VAL_REF_UNIT;
748 val->u.uint = read_uint64 (buf);
749 return 1;
750 case DW_FORM_ref_udata:
751 val->encoding = ATTR_VAL_REF_UNIT;
752 val->u.uint = read_uleb128 (buf);
753 return 1;
754 case DW_FORM_indirect:
755 {
756 uint64_t form;
757
758 form = read_uleb128 (buf);
759 return read_attribute ((enum dwarf_form) form, buf, is_dwarf64,
760 version, addrsize, dwarf_str, dwarf_str_size,
761 val);
762 }
763 case DW_FORM_sec_offset:
764 val->encoding = ATTR_VAL_REF_SECTION;
765 val->u.uint = read_offset (buf, is_dwarf64);
766 return 1;
767 case DW_FORM_exprloc:
768 val->encoding = ATTR_VAL_EXPR;
769 return advance (buf, read_uleb128 (buf));
770 case DW_FORM_flag_present:
771 val->encoding = ATTR_VAL_UINT;
772 val->u.uint = 1;
773 return 1;
774 case DW_FORM_ref_sig8:
775 val->encoding = ATTR_VAL_REF_TYPE;
776 val->u.uint = read_uint64 (buf);
777 return 1;
778 case DW_FORM_GNU_addr_index:
779 val->encoding = ATTR_VAL_REF_SECTION;
780 val->u.uint = read_uleb128 (buf);
781 return 1;
782 case DW_FORM_GNU_str_index:
783 val->encoding = ATTR_VAL_REF_SECTION;
784 val->u.uint = read_uleb128 (buf);
785 return 1;
786 case DW_FORM_GNU_ref_alt:
787 val->encoding = ATTR_VAL_REF_SECTION;
788 val->u.uint = read_offset (buf, is_dwarf64);
789 return 1;
790 case DW_FORM_GNU_strp_alt:
791 val->encoding = ATTR_VAL_REF_SECTION;
792 val->u.uint = read_offset (buf, is_dwarf64);
793 return 1;
794 default:
795 dwarf_buf_error (buf, "unrecognized DWARF form");
796 return 0;
797 }
798}
799
800/* Compare function_addrs for qsort. When ranges are nested, make the
801 smallest one sort last. */
802
803static int
804function_addrs_compare (const void *v1, const void *v2)
805{
806 const struct function_addrs *a1 = (const struct function_addrs *) v1;
807 const struct function_addrs *a2 = (const struct function_addrs *) v2;
808
809 if (a1->low < a2->low)
810 return -1;
811 if (a1->low > a2->low)
812 return 1;
813 if (a1->high < a2->high)
814 return 1;
815 if (a1->high > a2->high)
816 return -1;
817 return strcmp (a1->function->name, a2->function->name);
818}
819
820/* Compare a PC against a function_addrs for bsearch. Note that if
821 there are multiple ranges containing PC, which one will be returned
822 is unpredictable. We compensate for that in dwarf_fileline. */
823
824static int
825function_addrs_search (const void *vkey, const void *ventry)
826{
827 const uintptr_t *key = (const uintptr_t *) vkey;
828 const struct function_addrs *entry = (const struct function_addrs *) ventry;
829 uintptr_t pc;
830
831 pc = *key;
832 if (pc < entry->low)
833 return -1;
834 else if (pc >= entry->high)
835 return 1;
836 else
837 return 0;
838}
839
840/* Add a new compilation unit address range to a vector. Returns 1 on
841 success, 0 on failure. */
842
843static int
e561a992
ILT
844add_unit_addr (struct backtrace_state *state, uintptr_t base_address,
845 struct unit_addrs addrs,
eff02e4f
ILT
846 backtrace_error_callback error_callback, void *data,
847 struct unit_addrs_vector *vec)
848{
849 struct unit_addrs *p;
850
e561a992
ILT
851 /* Add in the base address of the module here, so that we can look
852 up the PC directly. */
853 addrs.low += base_address;
854 addrs.high += base_address;
855
eff02e4f
ILT
856 /* Try to merge with the last entry. */
857 if (vec->count > 0)
858 {
859 p = (struct unit_addrs *) vec->vec.base + (vec->count - 1);
860 if ((addrs.low == p->high || addrs.low == p->high + 1)
861 && addrs.u == p->u)
862 {
863 if (addrs.high > p->high)
864 p->high = addrs.high;
865 return 1;
866 }
867 }
868
869 p = ((struct unit_addrs *)
870 backtrace_vector_grow (state, sizeof (struct unit_addrs),
871 error_callback, data, &vec->vec));
872 if (p == NULL)
873 return 0;
874
875 *p = addrs;
876 ++vec->count;
877 return 1;
878}
879
880/* Free a unit address vector. */
881
882static void
883free_unit_addrs_vector (struct backtrace_state *state,
884 struct unit_addrs_vector *vec,
885 backtrace_error_callback error_callback, void *data)
886{
887 struct unit_addrs *addrs;
888 size_t i;
889
890 addrs = (struct unit_addrs *) vec->vec.base;
891 for (i = 0; i < vec->count; ++i)
892 free_abbrevs (state, &addrs[i].u->abbrevs, error_callback, data);
893}
894
895/* Compare unit_addrs for qsort. When ranges are nested, make the
896 smallest one sort last. */
897
898static int
899unit_addrs_compare (const void *v1, const void *v2)
900{
901 const struct unit_addrs *a1 = (const struct unit_addrs *) v1;
902 const struct unit_addrs *a2 = (const struct unit_addrs *) v2;
903
904 if (a1->low < a2->low)
905 return -1;
906 if (a1->low > a2->low)
907 return 1;
908 if (a1->high < a2->high)
909 return 1;
910 if (a1->high > a2->high)
911 return -1;
912 if (a1->u->lineoff < a2->u->lineoff)
913 return -1;
914 if (a1->u->lineoff > a2->u->lineoff)
915 return 1;
916 return 0;
917}
918
919/* Compare a PC against a unit_addrs for bsearch. Note that if there
920 are multiple ranges containing PC, which one will be returned is
921 unpredictable. We compensate for that in dwarf_fileline. */
922
923static int
924unit_addrs_search (const void *vkey, const void *ventry)
925{
926 const uintptr_t *key = (const uintptr_t *) vkey;
927 const struct unit_addrs *entry = (const struct unit_addrs *) ventry;
928 uintptr_t pc;
929
930 pc = *key;
931 if (pc < entry->low)
932 return -1;
933 else if (pc >= entry->high)
934 return 1;
935 else
936 return 0;
937}
938
939/* Sort the line vector by PC. We want a stable sort here. We know
940 that the pointers are into the same array, so it is safe to compare
941 them directly. */
942
943static int
944line_compare (const void *v1, const void *v2)
945{
946 const struct line *ln1 = (const struct line *) v1;
947 const struct line *ln2 = (const struct line *) v2;
948
949 if (ln1->pc < ln2->pc)
950 return -1;
951 else if (ln1->pc > ln2->pc)
952 return 1;
953 else if (ln1 < ln2)
954 return -1;
955 else if (ln1 > ln2)
956 return 1;
957 else
958 return 0;
959}
960
961/* Find a PC in a line vector. We always allocate an extra entry at
962 the end of the lines vector, so that this routine can safely look
963 at the next entry. Note that when there are multiple mappings for
964 the same PC value, this will return the last one. */
965
966static int
967line_search (const void *vkey, const void *ventry)
968{
969 const uintptr_t *key = (const uintptr_t *) vkey;
970 const struct line *entry = (const struct line *) ventry;
971 uintptr_t pc;
972
973 pc = *key;
974 if (pc < entry->pc)
975 return -1;
976 else if (pc >= (entry + 1)->pc)
977 return 1;
978 else
979 return 0;
980}
981
982/* Sort the abbrevs by the abbrev code. This function is passed to
983 both qsort and bsearch. */
984
985static int
986abbrev_compare (const void *v1, const void *v2)
987{
988 const struct abbrev *a1 = (const struct abbrev *) v1;
989 const struct abbrev *a2 = (const struct abbrev *) v2;
990
991 if (a1->code < a2->code)
992 return -1;
993 else if (a1->code > a2->code)
994 return 1;
995 else
996 {
997 /* This really shouldn't happen. It means there are two
998 different abbrevs with the same code, and that means we don't
999 know which one lookup_abbrev should return. */
1000 return 0;
1001 }
1002}
1003
1004/* Read the abbreviation table for a compilation unit. Returns 1 on
1005 success, 0 on failure. */
1006
1007static int
1008read_abbrevs (struct backtrace_state *state, uint64_t abbrev_offset,
1009 const unsigned char *dwarf_abbrev, size_t dwarf_abbrev_size,
1010 int is_bigendian, backtrace_error_callback error_callback,
1011 void *data, struct abbrevs *abbrevs)
1012{
1013 struct dwarf_buf abbrev_buf;
1014 struct dwarf_buf count_buf;
1015 size_t num_abbrevs;
1016
1017 abbrevs->num_abbrevs = 0;
1018 abbrevs->abbrevs = NULL;
1019
1020 if (abbrev_offset >= dwarf_abbrev_size)
1021 {
1022 error_callback (data, "abbrev offset out of range", 0);
1023 return 0;
1024 }
1025
1026 abbrev_buf.name = ".debug_abbrev";
1027 abbrev_buf.start = dwarf_abbrev;
1028 abbrev_buf.buf = dwarf_abbrev + abbrev_offset;
1029 abbrev_buf.left = dwarf_abbrev_size - abbrev_offset;
1030 abbrev_buf.is_bigendian = is_bigendian;
1031 abbrev_buf.error_callback = error_callback;
1032 abbrev_buf.data = data;
1033 abbrev_buf.reported_underflow = 0;
1034
1035 /* Count the number of abbrevs in this list. */
1036
1037 count_buf = abbrev_buf;
1038 num_abbrevs = 0;
1039 while (read_uleb128 (&count_buf) != 0)
1040 {
1041 if (count_buf.reported_underflow)
1042 return 0;
1043 ++num_abbrevs;
1044 // Skip tag.
1045 read_uleb128 (&count_buf);
1046 // Skip has_children.
1047 read_byte (&count_buf);
1048 // Skip attributes.
1049 while (read_uleb128 (&count_buf) != 0)
1050 read_uleb128 (&count_buf);
1051 // Skip form of last attribute.
1052 read_uleb128 (&count_buf);
1053 }
1054
1055 if (count_buf.reported_underflow)
1056 return 0;
1057
1058 if (num_abbrevs == 0)
1059 return 1;
1060
1061 abbrevs->num_abbrevs = num_abbrevs;
1062 abbrevs->abbrevs = ((struct abbrev *)
1063 backtrace_alloc (state,
1064 num_abbrevs * sizeof (struct abbrev),
1065 error_callback, data));
1066 if (abbrevs->abbrevs == NULL)
1067 return 0;
1068 memset (abbrevs->abbrevs, 0, num_abbrevs * sizeof (struct abbrev));
1069
1070 num_abbrevs = 0;
1071 while (1)
1072 {
1073 uint64_t code;
1074 struct abbrev a;
1075 size_t num_attrs;
1076 struct attr *attrs;
1077
1078 if (abbrev_buf.reported_underflow)
1079 goto fail;
1080
1081 code = read_uleb128 (&abbrev_buf);
1082 if (code == 0)
1083 break;
1084
1085 a.code = code;
1086 a.tag = (enum dwarf_tag) read_uleb128 (&abbrev_buf);
1087 a.has_children = read_byte (&abbrev_buf);
1088
1089 count_buf = abbrev_buf;
1090 num_attrs = 0;
1091 while (read_uleb128 (&count_buf) != 0)
1092 {
1093 ++num_attrs;
1094 read_uleb128 (&count_buf);
1095 }
1096
1097 if (num_attrs == 0)
1098 {
1099 attrs = NULL;
1100 read_uleb128 (&abbrev_buf);
1101 read_uleb128 (&abbrev_buf);
1102 }
1103 else
1104 {
1105 attrs = ((struct attr *)
1106 backtrace_alloc (state, num_attrs * sizeof *attrs,
1107 error_callback, data));
1108 if (attrs == NULL)
1109 goto fail;
1110 num_attrs = 0;
1111 while (1)
1112 {
1113 uint64_t name;
1114 uint64_t form;
1115
1116 name = read_uleb128 (&abbrev_buf);
1117 form = read_uleb128 (&abbrev_buf);
1118 if (name == 0)
1119 break;
1120 attrs[num_attrs].name = (enum dwarf_attribute) name;
1121 attrs[num_attrs].form = (enum dwarf_form) form;
1122 ++num_attrs;
1123 }
1124 }
1125
1126 a.num_attrs = num_attrs;
1127 a.attrs = attrs;
1128
1129 abbrevs->abbrevs[num_abbrevs] = a;
1130 ++num_abbrevs;
1131 }
1132
1133 qsort (abbrevs->abbrevs, abbrevs->num_abbrevs, sizeof (struct abbrev),
1134 abbrev_compare);
1135
1136 return 1;
1137
1138 fail:
1139 free_abbrevs (state, abbrevs, error_callback, data);
1140 return 0;
1141}
1142
1143/* Return the abbrev information for an abbrev code. */
1144
1145static const struct abbrev *
1146lookup_abbrev (struct abbrevs *abbrevs, uint64_t code,
1147 backtrace_error_callback error_callback, void *data)
1148{
1149 struct abbrev key;
1150 void *p;
1151
1152 /* With GCC, where abbrevs are simply numbered in order, we should
1153 be able to just look up the entry. */
1154 if (code - 1 < abbrevs->num_abbrevs
1155 && abbrevs->abbrevs[code - 1].code == code)
1156 return &abbrevs->abbrevs[code - 1];
1157
1158 /* Otherwise we have to search. */
1159 memset (&key, 0, sizeof key);
1160 key.code = code;
1161 p = bsearch (&key, abbrevs->abbrevs, abbrevs->num_abbrevs,
1162 sizeof (struct abbrev), abbrev_compare);
1163 if (p == NULL)
1164 {
1165 error_callback (data, "invalid abbreviation code", 0);
1166 return NULL;
1167 }
1168 return (const struct abbrev *) p;
1169}
1170
1171/* Add non-contiguous address ranges for a compilation unit. Returns
1172 1 on success, 0 on failure. */
1173
1174static int
e561a992
ILT
1175add_unit_ranges (struct backtrace_state *state, uintptr_t base_address,
1176 struct unit *u, uint64_t ranges, uint64_t base,
1177 int is_bigendian, const unsigned char *dwarf_ranges,
1178 size_t dwarf_ranges_size,
eff02e4f
ILT
1179 backtrace_error_callback error_callback, void *data,
1180 struct unit_addrs_vector *addrs)
1181{
1182 struct dwarf_buf ranges_buf;
1183
1184 if (ranges >= dwarf_ranges_size)
1185 {
1186 error_callback (data, "ranges offset out of range", 0);
1187 return 0;
1188 }
1189
1190 ranges_buf.name = ".debug_ranges";
1191 ranges_buf.start = dwarf_ranges;
1192 ranges_buf.buf = dwarf_ranges + ranges;
1193 ranges_buf.left = dwarf_ranges_size - ranges;
1194 ranges_buf.is_bigendian = is_bigendian;
1195 ranges_buf.error_callback = error_callback;
1196 ranges_buf.data = data;
1197 ranges_buf.reported_underflow = 0;
1198
1199 while (1)
1200 {
1201 uint64_t low;
1202 uint64_t high;
1203
1204 if (ranges_buf.reported_underflow)
1205 return 0;
1206
1207 low = read_address (&ranges_buf, u->addrsize);
1208 high = read_address (&ranges_buf, u->addrsize);
1209
1210 if (low == 0 && high == 0)
1211 break;
1212
1213 if (is_highest_address (low, u->addrsize))
1214 base = high;
1215 else
1216 {
1217 struct unit_addrs a;
1218
1219 a.low = low + base;
1220 a.high = high + base;
1221 a.u = u;
e561a992
ILT
1222 if (!add_unit_addr (state, base_address, a, error_callback, data,
1223 addrs))
eff02e4f
ILT
1224 return 0;
1225 }
1226 }
1227
1228 if (ranges_buf.reported_underflow)
1229 return 0;
1230
1231 return 1;
1232}
1233
1234/* Build a mapping from address ranges to the compilation units where
1235 the line number information for that range can be found. Returns 1
1236 on success, 0 on failure. */
1237
1238static int
e561a992 1239build_address_map (struct backtrace_state *state, uintptr_t base_address,
eff02e4f
ILT
1240 const unsigned char *dwarf_info, size_t dwarf_info_size,
1241 const unsigned char *dwarf_abbrev, size_t dwarf_abbrev_size,
1242 const unsigned char *dwarf_ranges, size_t dwarf_ranges_size,
1243 const unsigned char *dwarf_str, size_t dwarf_str_size,
1244 int is_bigendian, backtrace_error_callback error_callback,
1245 void *data, struct unit_addrs_vector *addrs)
1246{
1247 struct dwarf_buf info;
1248 struct abbrevs abbrevs;
1249
1250 memset (&addrs->vec, 0, sizeof addrs->vec);
1251 addrs->count = 0;
1252
1253 /* Read through the .debug_info section. FIXME: Should we use the
1254 .debug_aranges section? gdb and addr2line don't use it, but I'm
1255 not sure why. */
1256
1257 info.name = ".debug_info";
1258 info.start = dwarf_info;
1259 info.buf = dwarf_info;
1260 info.left = dwarf_info_size;
1261 info.is_bigendian = is_bigendian;
1262 info.error_callback = error_callback;
1263 info.data = data;
1264 info.reported_underflow = 0;
1265
1266 memset (&abbrevs, 0, sizeof abbrevs);
1267 while (info.left > 0)
1268 {
1269 const unsigned char *unit_data_start;
1270 uint64_t len;
1271 int is_dwarf64;
1272 struct dwarf_buf unit_buf;
1273 int version;
1274 uint64_t abbrev_offset;
1275 const struct abbrev *abbrev;
1276 int addrsize;
1277 const unsigned char *unit_data;
1278 size_t unit_data_len;
1279 size_t unit_data_offset;
1280 uint64_t code;
1281 size_t i;
1282 uint64_t lowpc;
1283 int have_lowpc;
1284 uint64_t highpc;
1285 int have_highpc;
1286 int highpc_is_relative;
1287 uint64_t ranges;
1288 int have_ranges;
1289 uint64_t lineoff;
1290 int have_lineoff;
1291 const char *comp_dir;
1292
1293 if (info.reported_underflow)
1294 goto fail;
1295
1296 unit_data_start = info.buf;
1297
1298 is_dwarf64 = 0;
1299 len = read_uint32 (&info);
1300 if (len == 0xffffffff)
1301 {
1302 len = read_uint64 (&info);
1303 is_dwarf64 = 1;
1304 }
1305
1306 unit_buf = info;
eff02e4f
ILT
1307 unit_buf.left = len;
1308
1309 if (!advance (&info, len))
1310 goto fail;
1311
1312 version = read_uint16 (&unit_buf);
1313 if (version < 2 || version > 4)
1314 {
1315 dwarf_buf_error (&unit_buf, "unrecognized DWARF version");
1316 goto fail;
1317 }
1318
1319 abbrev_offset = read_offset (&unit_buf, is_dwarf64);
1320 if (!read_abbrevs (state, abbrev_offset, dwarf_abbrev, dwarf_abbrev_size,
1321 is_bigendian, error_callback, data, &abbrevs))
1322 goto fail;
1323
1324 addrsize = read_byte (&unit_buf);
1325
1326 unit_data = unit_buf.buf;
1327 unit_data_len = unit_buf.left;
1328 unit_data_offset = unit_buf.buf - unit_data_start;
1329
1330 /* We only look at the first attribute in the compilation unit.
1331 In practice this will be a DW_TAG_compile_unit which will
1332 tell us the PC range and where to find the line number
1333 information. */
1334
1335 code = read_uleb128 (&unit_buf);
1336 abbrev = lookup_abbrev (&abbrevs, code, error_callback, data);
1337 if (abbrev == NULL)
1338 goto fail;
1339
1340 lowpc = 0;
1341 have_lowpc = 0;
1342 highpc = 0;
1343 have_highpc = 0;
1344 highpc_is_relative = 0;
1345 ranges = 0;
1346 have_ranges = 0;
1347 lineoff = 0;
1348 have_lineoff = 0;
1349 comp_dir = NULL;
1350 for (i = 0; i < abbrev->num_attrs; ++i)
1351 {
1352 struct attr_val val;
1353
1354 if (!read_attribute (abbrev->attrs[i].form, &unit_buf, is_dwarf64,
1355 version, addrsize, dwarf_str, dwarf_str_size,
1356 &val))
1357 goto fail;
1358
1359 switch (abbrev->attrs[i].name)
1360 {
1361 case DW_AT_low_pc:
1362 if (val.encoding == ATTR_VAL_ADDRESS)
1363 {
1364 lowpc = val.u.uint;
1365 have_lowpc = 1;
1366 }
1367 break;
1368 case DW_AT_high_pc:
1369 if (val.encoding == ATTR_VAL_ADDRESS)
1370 {
1371 highpc = val.u.uint;
1372 have_highpc = 1;
1373 }
1374 else if (val.encoding == ATTR_VAL_UINT)
1375 {
1376 highpc = val.u.uint;
1377 have_highpc = 1;
1378 highpc_is_relative = 1;
1379 }
1380 break;
1381 case DW_AT_ranges:
1382 if (val.encoding == ATTR_VAL_UINT
1383 || val.encoding == ATTR_VAL_REF_SECTION)
1384 {
1385 ranges = val.u.uint;
1386 have_ranges = 1;
1387 }
1388 break;
1389 case DW_AT_stmt_list:
1390 if (val.encoding == ATTR_VAL_UINT
1391 || val.encoding == ATTR_VAL_REF_SECTION)
1392 {
1393 lineoff = val.u.uint;
1394 have_lineoff = 1;
1395 }
1396 break;
1397 case DW_AT_comp_dir:
1398 if (val.encoding == ATTR_VAL_STRING)
1399 comp_dir = val.u.string;
1400 break;
1401 default:
1402 break;
1403 }
1404 }
1405
1406 if (unit_buf.reported_underflow)
1407 goto fail;
1408
1409 if (((have_lowpc && have_highpc) || have_ranges) && have_lineoff)
1410 {
1411 struct unit *u;
1412 struct unit_addrs a;
1413
1414 u = ((struct unit *)
1415 backtrace_alloc (state, sizeof *u, error_callback, data));
1416 if (u == NULL)
1417 goto fail;
1418 u->unit_data = unit_data;
1419 u->unit_data_len = unit_data_len;
1420 u->unit_data_offset = unit_data_offset;
1421 u->version = version;
1422 u->is_dwarf64 = is_dwarf64;
1423 u->addrsize = addrsize;
1424 u->comp_dir = comp_dir;
1425 u->lineoff = lineoff;
1426 u->abbrevs = abbrevs;
1427 memset (&abbrevs, 0, sizeof abbrevs);
1428
1429 /* The actual line number mappings will be read as
1430 needed. */
1431 u->lines = NULL;
1432 u->lines_count = 0;
1433 u->function_addrs = NULL;
1434 u->function_addrs_count = 0;
1435
1436 if (have_ranges)
1437 {
e561a992
ILT
1438 if (!add_unit_ranges (state, base_address, u, ranges, lowpc,
1439 is_bigendian, dwarf_ranges,
1440 dwarf_ranges_size, error_callback, data,
1441 addrs))
eff02e4f
ILT
1442 {
1443 free_abbrevs (state, &u->abbrevs, error_callback, data);
1444 backtrace_free (state, u, sizeof *u, error_callback, data);
1445 goto fail;
1446 }
1447 }
1448 else
1449 {
1450 if (highpc_is_relative)
1451 highpc += lowpc;
1452 a.low = lowpc;
1453 a.high = highpc;
1454 a.u = u;
1455
e561a992
ILT
1456 if (!add_unit_addr (state, base_address, a, error_callback, data,
1457 addrs))
eff02e4f
ILT
1458 {
1459 free_abbrevs (state, &u->abbrevs, error_callback, data);
1460 backtrace_free (state, u, sizeof *u, error_callback, data);
1461 goto fail;
1462 }
1463 }
1464 }
1465 else
1466 {
1467 free_abbrevs (state, &abbrevs, error_callback, data);
1468 memset (&abbrevs, 0, sizeof abbrevs);
1469 }
1470 }
1471 if (info.reported_underflow)
1472 goto fail;
1473
1474 return 1;
1475
1476 fail:
1477 free_abbrevs (state, &abbrevs, error_callback, data);
1478 free_unit_addrs_vector (state, addrs, error_callback, data);
1479 return 0;
1480}
1481
1482/* Add a new mapping to the vector of line mappings that we are
1483 building. Returns 1 on success, 0 on failure. */
1484
1485static int
e561a992
ILT
1486add_line (struct backtrace_state *state, struct dwarf_data *ddata,
1487 uintptr_t pc, const char *filename, int lineno,
1488 backtrace_error_callback error_callback, void *data,
eff02e4f
ILT
1489 struct line_vector *vec)
1490{
1491 struct line *ln;
1492
1493 /* If we are adding the same mapping, ignore it. This can happen
1494 when using discriminators. */
1495 if (vec->count > 0)
1496 {
1497 ln = (struct line *) vec->vec.base + (vec->count - 1);
1498 if (pc == ln->pc && filename == ln->filename && lineno == ln->lineno)
1499 return 1;
1500 }
1501
1502 ln = ((struct line *)
1503 backtrace_vector_grow (state, sizeof (struct line), error_callback,
1504 data, &vec->vec));
1505 if (ln == NULL)
1506 return 0;
1507
e561a992
ILT
1508 /* Add in the base address here, so that we can look up the PC
1509 directly. */
1510 ln->pc = pc + ddata->base_address;
1511
eff02e4f
ILT
1512 ln->filename = filename;
1513 ln->lineno = lineno;
1514
1515 ++vec->count;
1516
1517 return 1;
1518}
1519
1520/* Free the line header information. If FREE_FILENAMES is true we
1521 free the file names themselves, otherwise we leave them, as there
1522 may be line structures pointing to them. */
1523
1524static void
1525free_line_header (struct backtrace_state *state, struct line_header *hdr,
1526 backtrace_error_callback error_callback, void *data)
1527{
1528 backtrace_free (state, hdr->dirs, hdr->dirs_count * sizeof (const char *),
1529 error_callback, data);
1530 backtrace_free (state, hdr->filenames,
1531 hdr->filenames_count * sizeof (char *),
1532 error_callback, data);
1533}
1534
1535/* Read the line header. Return 1 on success, 0 on failure. */
1536
1537static int
1538read_line_header (struct backtrace_state *state, struct unit *u,
1539 int is_dwarf64, struct dwarf_buf *line_buf,
1540 struct line_header *hdr)
1541{
1542 uint64_t hdrlen;
1543 struct dwarf_buf hdr_buf;
1544 const unsigned char *p;
1545 const unsigned char *pend;
1546 size_t i;
1547
1548 hdr->version = read_uint16 (line_buf);
1549 if (hdr->version < 2 || hdr->version > 4)
1550 {
1551 dwarf_buf_error (line_buf, "unsupported line number version");
1552 return 0;
1553 }
1554
1555 hdrlen = read_offset (line_buf, is_dwarf64);
1556
1557 hdr_buf = *line_buf;
1558 hdr_buf.left = hdrlen;
1559
1560 if (!advance (line_buf, hdrlen))
1561 return 0;
1562
1563 hdr->min_insn_len = read_byte (&hdr_buf);
1564 if (hdr->version < 4)
1565 hdr->max_ops_per_insn = 1;
1566 else
1567 hdr->max_ops_per_insn = read_byte (&hdr_buf);
1568
1569 /* We don't care about default_is_stmt. */
1570 read_byte (&hdr_buf);
1571
1572 hdr->line_base = read_sbyte (&hdr_buf);
1573 hdr->line_range = read_byte (&hdr_buf);
1574
1575 hdr->opcode_base = read_byte (&hdr_buf);
1576 hdr->opcode_lengths = hdr_buf.buf;
1577 if (!advance (&hdr_buf, hdr->opcode_base - 1))
1578 return 0;
1579
1580 /* Count the number of directory entries. */
1581 hdr->dirs_count = 0;
1582 p = hdr_buf.buf;
1583 pend = p + hdr_buf.left;
1584 while (p < pend && *p != '\0')
1585 {
1586 p += strnlen((const char *) p, pend - p) + 1;
1587 ++hdr->dirs_count;
1588 }
1589
1590 hdr->dirs = ((const char **)
1591 backtrace_alloc (state,
1592 hdr->dirs_count * sizeof (const char *),
1593 line_buf->error_callback, line_buf->data));
1594 if (hdr->dirs == NULL)
1595 return 0;
1596
1597 i = 0;
1598 while (*hdr_buf.buf != '\0')
1599 {
1600 if (hdr_buf.reported_underflow)
1601 return 0;
1602
1603 hdr->dirs[i] = (const char *) hdr_buf.buf;
1604 ++i;
1605 if (!advance (&hdr_buf,
1606 strnlen ((const char *) hdr_buf.buf, hdr_buf.left) + 1))
1607 return 0;
1608 }
1609 if (!advance (&hdr_buf, 1))
1610 return 0;
1611
1612 /* Count the number of file entries. */
1613 hdr->filenames_count = 0;
1614 p = hdr_buf.buf;
1615 pend = p + hdr_buf.left;
1616 while (p < pend && *p != '\0')
1617 {
1618 p += strnlen ((const char *) p, pend - p) + 1;
1619 p += leb128_len (p);
1620 p += leb128_len (p);
1621 p += leb128_len (p);
1622 ++hdr->filenames_count;
1623 }
1624
1625 hdr->filenames = ((const char **)
1626 backtrace_alloc (state,
1627 hdr->filenames_count * sizeof (char *),
1628 line_buf->error_callback,
1629 line_buf->data));
1630 if (hdr->filenames == NULL)
1631 return 0;
1632 i = 0;
1633 while (*hdr_buf.buf != '\0')
1634 {
1635 const char *filename;
1636 uint64_t dir_index;
1637
1638 if (hdr_buf.reported_underflow)
1639 return 0;
1640
1641 filename = (const char *) hdr_buf.buf;
1642 if (!advance (&hdr_buf,
1643 strnlen ((const char *) hdr_buf.buf, hdr_buf.left) + 1))
1644 return 0;
1645 dir_index = read_uleb128 (&hdr_buf);
1646 if (IS_ABSOLUTE_PATH (filename))
1647 hdr->filenames[i] = filename;
1648 else
1649 {
1650 const char *dir;
1651 size_t dir_len;
1652 size_t filename_len;
1653 char *s;
1654
1655 if (dir_index == 0)
1656 dir = u->comp_dir;
1657 else if (dir_index - 1 < hdr->dirs_count)
1658 dir = hdr->dirs[dir_index - 1];
1659 else
1660 {
1661 dwarf_buf_error (line_buf,
1662 ("invalid directory index in "
1663 "line number program header"));
1664 return 0;
1665 }
1666 dir_len = strlen (dir);
1667 filename_len = strlen (filename);
1668 s = ((char *)
1669 backtrace_alloc (state, dir_len + filename_len + 2,
1670 line_buf->error_callback, line_buf->data));
1671 if (s == NULL)
1672 return 0;
1673 memcpy (s, dir, dir_len);
1674 /* FIXME: If we are on a DOS-based file system, and the
1675 directory or the file name use backslashes, then we
1676 should use a backslash here. */
1677 s[dir_len] = '/';
1678 memcpy (s + dir_len + 1, filename, filename_len + 1);
1679 hdr->filenames[i] = s;
1680 }
1681
1682 /* Ignore the modification time and size. */
1683 read_uleb128 (&hdr_buf);
1684 read_uleb128 (&hdr_buf);
1685
1686 ++i;
1687 }
1688
1689 if (hdr_buf.reported_underflow)
1690 return 0;
1691
1692 return 1;
1693}
1694
1695/* Read the line program, adding line mappings to VEC. Return 1 on
1696 success, 0 on failure. */
1697
1698static int
e561a992
ILT
1699read_line_program (struct backtrace_state *state, struct dwarf_data *ddata,
1700 struct unit *u, const struct line_header *hdr,
1701 struct dwarf_buf *line_buf, struct line_vector *vec)
eff02e4f
ILT
1702{
1703 uint64_t address;
1704 unsigned int op_index;
1705 const char *reset_filename;
1706 const char *filename;
1707 int lineno;
1708
1709 address = 0;
1710 op_index = 0;
1711 if (hdr->filenames_count > 0)
1712 reset_filename = hdr->filenames[0];
1713 else
1714 reset_filename = "";
1715 filename = reset_filename;
1716 lineno = 1;
1717 while (line_buf->left > 0)
1718 {
1719 unsigned int op;
1720
1721 op = read_byte (line_buf);
1722 if (op >= hdr->opcode_base)
1723 {
1724 unsigned int advance;
1725
1726 /* Special opcode. */
1727 op -= hdr->opcode_base;
1728 advance = op / hdr->line_range;
1729 address += (hdr->min_insn_len * (op_index + advance)
1730 / hdr->max_ops_per_insn);
1731 op_index = (op_index + advance) % hdr->max_ops_per_insn;
1732 lineno += hdr->line_base + (int) (op % hdr->line_range);
e561a992
ILT
1733 add_line (state, ddata, address, filename, lineno,
1734 line_buf->error_callback, line_buf->data, vec);
eff02e4f
ILT
1735 }
1736 else if (op == DW_LNS_extended_op)
1737 {
1738 uint64_t len;
1739
1740 len = read_uleb128 (line_buf);
1741 op = read_byte (line_buf);
1742 switch (op)
1743 {
1744 case DW_LNE_end_sequence:
1745 /* FIXME: Should we mark the high PC here? It seems
1746 that we already have that information from the
1747 compilation unit. */
1748 address = 0;
1749 op_index = 0;
1750 filename = reset_filename;
1751 lineno = 1;
1752 break;
1753 case DW_LNE_set_address:
1754 address = read_address (line_buf, u->addrsize);
1755 break;
1756 case DW_LNE_define_file:
1757 {
1758 const char *f;
1759 unsigned int dir_index;
1760
1761 f = (const char *) line_buf->buf;
1762 if (!advance (line_buf, strnlen (f, line_buf->left) + 1))
1763 return 0;
1764 dir_index = read_uleb128 (line_buf);
1765 /* Ignore that time and length. */
1766 read_uleb128 (line_buf);
1767 read_uleb128 (line_buf);
1768 if (IS_ABSOLUTE_PATH (f))
1769 filename = f;
1770 else
1771 {
1772 const char *dir;
1773 size_t dir_len;
1774 size_t f_len;
1775 char *p;
1776
1777 if (dir_index == 0)
1778 dir = u->comp_dir;
1779 else if (dir_index - 1 < hdr->dirs_count)
1780 dir = hdr->dirs[dir_index - 1];
1781 else
1782 {
1783 dwarf_buf_error (line_buf,
1784 ("invalid directory index "
1785 "in line number program"));
1786 return 0;
1787 }
1788 dir_len = strlen (dir);
1789 f_len = strlen (f);
1790 p = ((char *)
1791 backtrace_alloc (state, dir_len + f_len + 2,
1792 line_buf->error_callback,
1793 line_buf->data));
1794 if (p == NULL)
1795 return 0;
1796 memcpy (p, dir, dir_len);
1797 /* FIXME: If we are on a DOS-based file system,
1798 and the directory or the file name use
1799 backslashes, then we should use a backslash
1800 here. */
1801 p[dir_len] = '/';
1802 memcpy (p + dir_len + 1, f, f_len + 1);
1803 filename = p;
1804 }
1805 }
1806 break;
1807 case DW_LNE_set_discriminator:
1808 /* We don't care about discriminators. */
1809 read_uleb128 (line_buf);
1810 break;
1811 default:
1812 if (!advance (line_buf, len - 1))
1813 return 0;
1814 break;
1815 }
1816 }
1817 else
1818 {
1819 switch (op)
1820 {
1821 case DW_LNS_copy:
e561a992 1822 add_line (state, ddata, address, filename, lineno,
eff02e4f
ILT
1823 line_buf->error_callback, line_buf->data, vec);
1824 break;
1825 case DW_LNS_advance_pc:
1826 {
1827 uint64_t advance;
1828
1829 advance = read_uleb128 (line_buf);
1830 address += (hdr->min_insn_len * (op_index + advance)
1831 / hdr->max_ops_per_insn);
1832 op_index = (op_index + advance) % hdr->max_ops_per_insn;
1833 }
1834 break;
1835 case DW_LNS_advance_line:
1836 lineno += (int) read_sleb128 (line_buf);
1837 break;
1838 case DW_LNS_set_file:
1839 {
1840 uint64_t fileno;
1841
1842 fileno = read_uleb128 (line_buf);
1843 if (fileno == 0)
1844 filename = "";
1845 else
1846 {
1847 if (fileno - 1 >= hdr->filenames_count)
1848 {
1849 dwarf_buf_error (line_buf,
1850 ("invalid file number in "
1851 "line number program"));
1852 return 0;
1853 }
1854 filename = hdr->filenames[fileno - 1];
1855 }
1856 }
1857 break;
1858 case DW_LNS_set_column:
1859 read_uleb128 (line_buf);
1860 break;
1861 case DW_LNS_negate_stmt:
1862 break;
1863 case DW_LNS_set_basic_block:
1864 break;
1865 case DW_LNS_const_add_pc:
1866 {
1867 unsigned int advance;
1868
1869 op = 255 - hdr->opcode_base;
1870 advance = op / hdr->line_range;
1871 address += (hdr->min_insn_len * (op_index + advance)
1872 / hdr->max_ops_per_insn);
1873 op_index = (op_index + advance) % hdr->max_ops_per_insn;
1874 }
1875 break;
1876 case DW_LNS_fixed_advance_pc:
1877 address += read_uint16 (line_buf);
1878 op_index = 0;
1879 break;
1880 case DW_LNS_set_prologue_end:
1881 break;
1882 case DW_LNS_set_epilogue_begin:
1883 break;
1884 case DW_LNS_set_isa:
1885 read_uleb128 (line_buf);
1886 break;
1887 default:
1888 {
1889 unsigned int i;
1890
1891 for (i = hdr->opcode_lengths[op - 1]; i > 0; --i)
1892 read_uleb128 (line_buf);
1893 }
1894 break;
1895 }
1896 }
1897 }
1898
1899 return 1;
1900}
1901
1902/* Read the line number information for a compilation unit. Returns 1
1903 on success, 0 on failure. */
1904
1905static int
1906read_line_info (struct backtrace_state *state, struct dwarf_data *ddata,
1907 backtrace_error_callback error_callback, void *data,
1908 struct unit *u, struct line_header *hdr, struct line **lines,
1909 size_t *lines_count)
1910{
1911 struct line_vector vec;
1912 struct dwarf_buf line_buf;
1913 uint64_t len;
1914 int is_dwarf64;
1915 struct line *ln;
1916
1917 memset (&vec.vec, 0, sizeof vec.vec);
1918 vec.count = 0;
1919
1920 memset (hdr, 0, sizeof *hdr);
1921
1922 if (u->lineoff != (off_t) (size_t) u->lineoff
1923 || (size_t) u->lineoff >= ddata->dwarf_line_size)
1924 {
1925 error_callback (data, "unit line offset out of range", 0);
1926 goto fail;
1927 }
1928
1929 line_buf.name = ".debug_line";
1930 line_buf.start = ddata->dwarf_line;
1931 line_buf.buf = ddata->dwarf_line + u->lineoff;
1932 line_buf.left = ddata->dwarf_line_size - u->lineoff;
1933 line_buf.is_bigendian = ddata->is_bigendian;
1934 line_buf.error_callback = error_callback;
1935 line_buf.data = data;
1936 line_buf.reported_underflow = 0;
1937
1938 is_dwarf64 = 0;
1939 len = read_uint32 (&line_buf);
1940 if (len == 0xffffffff)
1941 {
1942 len = read_uint64 (&line_buf);
1943 is_dwarf64 = 1;
1944 }
1945 line_buf.left = len;
1946
1947 if (!read_line_header (state, u, is_dwarf64, &line_buf, hdr))
1948 goto fail;
1949
e561a992 1950 if (!read_line_program (state, ddata, u, hdr, &line_buf, &vec))
eff02e4f
ILT
1951 goto fail;
1952
1953 if (line_buf.reported_underflow)
1954 goto fail;
1955
1956 if (vec.count == 0)
1957 {
1958 /* This is not a failure in the sense of a generating an error,
1959 but it is a failure in that sense that we have no useful
1960 information. */
1961 goto fail;
1962 }
1963
1964 /* Allocate one extra entry at the end. */
1965 ln = ((struct line *)
1966 backtrace_vector_grow (state, sizeof (struct line), error_callback,
1967 data, &vec.vec));
1968 if (ln == NULL)
1969 goto fail;
1970 ln->pc = (uintptr_t) -1;
1971 ln->filename = NULL;
1972 ln->lineno = 0;
1973
1974 if (!backtrace_vector_release (state, &vec.vec, error_callback, data))
1975 goto fail;
1976
1977 ln = (struct line *) vec.vec.base;
1978 qsort (ln, vec.count, sizeof (struct line), line_compare);
1979
1980 *lines = ln;
1981 *lines_count = vec.count;
1982
1983 return 1;
1984
1985 fail:
1986 vec.vec.alc += vec.vec.size;
1987 vec.vec.size = 0;
1988 backtrace_vector_release (state, &vec.vec, error_callback, data);
1989 free_line_header (state, hdr, error_callback, data);
1990 *lines = (struct line *) (uintptr_t) -1;
1991 *lines_count = 0;
1992 return 0;
1993}
1994
1995/* Read the name of a function from a DIE referenced by a
1996 DW_AT_abstract_origin or DW_AT_specification tag. OFFSET is within
1997 the same compilation unit. */
1998
1999static const char *
2000read_referenced_name (struct dwarf_data *ddata, struct unit *u,
2001 uint64_t offset, backtrace_error_callback error_callback,
2002 void *data)
2003{
2004 struct dwarf_buf unit_buf;
2005 uint64_t code;
2006 const struct abbrev *abbrev;
2007 const char *ret;
2008 size_t i;
2009
2010 /* OFFSET is from the start of the data for this compilation unit.
2011 U->unit_data is the data, but it starts U->unit_data_offset bytes
2012 from the beginning. */
2013
2014 if (offset < u->unit_data_offset
2015 || offset - u->unit_data_offset >= u->unit_data_len)
2016 {
2017 error_callback (data,
2018 "abstract origin or specification out of range",
2019 0);
2020 return NULL;
2021 }
2022
2023 offset -= u->unit_data_offset;
2024
2025 unit_buf.name = ".debug_info";
2026 unit_buf.start = ddata->dwarf_info;
2027 unit_buf.buf = u->unit_data + offset;
2028 unit_buf.left = u->unit_data_len - offset;
2029 unit_buf.is_bigendian = ddata->is_bigendian;
2030 unit_buf.error_callback = error_callback;
2031 unit_buf.data = data;
2032 unit_buf.reported_underflow = 0;
2033
2034 code = read_uleb128 (&unit_buf);
2035 if (code == 0)
2036 {
2037 dwarf_buf_error (&unit_buf, "invalid abstract origin or specification");
2038 return NULL;
2039 }
2040
2041 abbrev = lookup_abbrev (&u->abbrevs, code, error_callback, data);
2042 if (abbrev == NULL)
2043 return NULL;
2044
2045 ret = NULL;
2046 for (i = 0; i < abbrev->num_attrs; ++i)
2047 {
2048 struct attr_val val;
2049
2050 if (!read_attribute (abbrev->attrs[i].form, &unit_buf,
2051 u->is_dwarf64, u->version, u->addrsize,
2052 ddata->dwarf_str, ddata->dwarf_str_size,
2053 &val))
2054 return NULL;
2055
2056 switch (abbrev->attrs[i].name)
2057 {
2058 case DW_AT_name:
2059 /* We prefer the linkage name if get one. */
2060 if (val.encoding == ATTR_VAL_STRING)
2061 ret = val.u.string;
2062 break;
2063
2064 case DW_AT_linkage_name:
2065 case DW_AT_MIPS_linkage_name:
2066 if (val.encoding == ATTR_VAL_STRING)
2067 return val.u.string;
2068 break;
2069
2070 case DW_AT_specification:
2071 if (abbrev->attrs[i].form == DW_FORM_ref_addr
2072 || abbrev->attrs[i].form == DW_FORM_ref_sig8)
2073 {
2074 /* This refers to a specification defined in some other
2075 compilation unit. We can handle this case if we
2076 must, but it's harder. */
2077 break;
2078 }
2079 if (val.encoding == ATTR_VAL_UINT
2080 || val.encoding == ATTR_VAL_REF_UNIT)
2081 {
2082 const char *name;
2083
2084 name = read_referenced_name (ddata, u, val.u.uint,
2085 error_callback, data);
2086 if (name != NULL)
2087 ret = name;
2088 }
2089 break;
2090
2091 default:
2092 break;
2093 }
2094 }
2095
2096 return ret;
2097}
2098
2099/* Add a single range to U that maps to function. Returns 1 on
2100 success, 0 on error. */
2101
2102static int
e561a992
ILT
2103add_function_range (struct backtrace_state *state, struct dwarf_data *ddata,
2104 struct function *function, uint64_t lowpc, uint64_t highpc,
eff02e4f
ILT
2105 backtrace_error_callback error_callback,
2106 void *data, struct function_vector *vec)
2107{
2108 struct function_addrs *p;
2109
e561a992
ILT
2110 /* Add in the base address here, so that we can look up the PC
2111 directly. */
2112 lowpc += ddata->base_address;
2113 highpc += ddata->base_address;
2114
eff02e4f
ILT
2115 if (vec->count > 0)
2116 {
2117 p = (struct function_addrs *) vec->vec.base + vec->count - 1;
2118 if ((lowpc == p->high || lowpc == p->high + 1)
2119 && function == p->function)
2120 {
2121 if (highpc > p->high)
2122 p->high = highpc;
2123 return 1;
2124 }
2125 }
2126
2127 p = ((struct function_addrs *)
2128 backtrace_vector_grow (state, sizeof (struct function_addrs),
2129 error_callback, data, &vec->vec));
2130 if (p == NULL)
2131 return 0;
2132
2133 p->low = lowpc;
2134 p->high = highpc;
2135 p->function = function;
2136 ++vec->count;
2137 return 1;
2138}
2139
2140/* Add PC ranges to U that map to FUNCTION. Returns 1 on success, 0
2141 on error. */
2142
2143static int
2144add_function_ranges (struct backtrace_state *state, struct dwarf_data *ddata,
2145 struct unit *u, struct function *function,
2146 uint64_t ranges, uint64_t base,
2147 backtrace_error_callback error_callback, void *data,
2148 struct function_vector *vec)
2149{
2150 struct dwarf_buf ranges_buf;
2151
2152 if (ranges >= ddata->dwarf_ranges_size)
2153 {
2154 error_callback (data, "function ranges offset out of range", 0);
2155 return 0;
2156 }
2157
2158 ranges_buf.name = ".debug_ranges";
2159 ranges_buf.start = ddata->dwarf_ranges;
2160 ranges_buf.buf = ddata->dwarf_ranges + ranges;
2161 ranges_buf.left = ddata->dwarf_ranges_size - ranges;
2162 ranges_buf.is_bigendian = ddata->is_bigendian;
2163 ranges_buf.error_callback = error_callback;
2164 ranges_buf.data = data;
2165 ranges_buf.reported_underflow = 0;
2166
2167 while (1)
2168 {
2169 uint64_t low;
2170 uint64_t high;
2171
2172 if (ranges_buf.reported_underflow)
2173 return 0;
2174
2175 low = read_address (&ranges_buf, u->addrsize);
2176 high = read_address (&ranges_buf, u->addrsize);
2177
2178 if (low == 0 && high == 0)
2179 break;
2180
2181 if (is_highest_address (low, u->addrsize))
2182 base = high;
2183 else
2184 {
e561a992
ILT
2185 if (!add_function_range (state, ddata, function, low + base,
2186 high + base, error_callback, data, vec))
eff02e4f
ILT
2187 return 0;
2188 }
2189 }
2190
2191 if (ranges_buf.reported_underflow)
2192 return 0;
2193
2194 return 1;
2195}
2196
2197/* Read one entry plus all its children. Add function addresses to
2198 VEC. Returns 1 on success, 0 on error. */
2199
2200static int
2201read_function_entry (struct backtrace_state *state, struct dwarf_data *ddata,
2202 struct unit *u, uint64_t base, struct dwarf_buf *unit_buf,
2203 const struct line_header *lhdr,
2204 backtrace_error_callback error_callback, void *data,
2205 struct function_vector *vec)
2206{
2207 while (unit_buf->left > 0)
2208 {
2209 uint64_t code;
2210 const struct abbrev *abbrev;
2211 int is_function;
2212 struct function *function;
2213 size_t i;
2214 uint64_t lowpc;
2215 int have_lowpc;
2216 uint64_t highpc;
2217 int have_highpc;
2218 int highpc_is_relative;
2219 uint64_t ranges;
2220 int have_ranges;
2221
2222 code = read_uleb128 (unit_buf);
2223 if (code == 0)
2224 return 1;
2225
2226 abbrev = lookup_abbrev (&u->abbrevs, code, error_callback, data);
2227 if (abbrev == NULL)
2228 return 0;
2229
2230 is_function = (abbrev->tag == DW_TAG_subprogram
2231 || abbrev->tag == DW_TAG_entry_point
2232 || abbrev->tag == DW_TAG_inlined_subroutine);
2233
2234 function = NULL;
2235 if (is_function)
2236 {
2237 function = ((struct function *)
2238 backtrace_alloc (state, sizeof *function,
2239 error_callback, data));
2240 if (function == NULL)
2241 return 0;
2242 memset (function, 0, sizeof *function);
2243 }
2244
2245 lowpc = 0;
2246 have_lowpc = 0;
2247 highpc = 0;
2248 have_highpc = 0;
2249 highpc_is_relative = 0;
2250 ranges = 0;
2251 have_ranges = 0;
2252 for (i = 0; i < abbrev->num_attrs; ++i)
2253 {
2254 struct attr_val val;
2255
2256 if (!read_attribute (abbrev->attrs[i].form, unit_buf,
2257 u->is_dwarf64, u->version, u->addrsize,
2258 ddata->dwarf_str, ddata->dwarf_str_size,
2259 &val))
2260 return 0;
2261
2262 /* The compile unit sets the base address for any address
2263 ranges in the function entries. */
2264 if (abbrev->tag == DW_TAG_compile_unit
2265 && abbrev->attrs[i].name == DW_AT_low_pc
2266 && val.encoding == ATTR_VAL_ADDRESS)
2267 base = val.u.uint;
2268
2269 if (is_function)
2270 {
2271 switch (abbrev->attrs[i].name)
2272 {
2273 case DW_AT_call_file:
2274 if (val.encoding == ATTR_VAL_UINT)
2275 {
2276 if (val.u.uint == 0)
2277 function->caller_filename = "";
2278 else
2279 {
2280 if (val.u.uint - 1 >= lhdr->filenames_count)
2281 {
2282 dwarf_buf_error (unit_buf,
2283 ("invalid file number in "
2284 "DW_AT_call_file attribute"));
2285 return 0;
2286 }
2287 function->caller_filename =
2288 lhdr->filenames[val.u.uint - 1];
2289 }
2290 }
2291 break;
2292
2293 case DW_AT_call_line:
2294 if (val.encoding == ATTR_VAL_UINT)
2295 function->caller_lineno = val.u.uint;
2296 break;
2297
2298 case DW_AT_abstract_origin:
2299 case DW_AT_specification:
2300 if (abbrev->attrs[i].form == DW_FORM_ref_addr
2301 || abbrev->attrs[i].form == DW_FORM_ref_sig8)
2302 {
2303 /* This refers to an abstract origin defined in
2304 some other compilation unit. We can handle
2305 this case if we must, but it's harder. */
2306 break;
2307 }
2308 if (val.encoding == ATTR_VAL_UINT
2309 || val.encoding == ATTR_VAL_REF_UNIT)
2310 {
2311 const char *name;
2312
2313 name = read_referenced_name (ddata, u, val.u.uint,
2314 error_callback, data);
2315 if (name != NULL)
2316 function->name = name;
2317 }
2318 break;
2319
2320 case DW_AT_name:
2321 if (val.encoding == ATTR_VAL_STRING)
2322 {
2323 /* Don't override a name we found in some other
2324 way, as it will normally be more
2325 useful--e.g., this name is normally not
2326 mangled. */
2327 if (function->name == NULL)
2328 function->name = val.u.string;
2329 }
2330 break;
2331
2332 case DW_AT_linkage_name:
2333 case DW_AT_MIPS_linkage_name:
2334 if (val.encoding == ATTR_VAL_STRING)
2335 function->name = val.u.string;
2336 break;
2337
2338 case DW_AT_low_pc:
2339 if (val.encoding == ATTR_VAL_ADDRESS)
2340 {
2341 lowpc = val.u.uint;
2342 have_lowpc = 1;
2343 }
2344 break;
2345
2346 case DW_AT_high_pc:
2347 if (val.encoding == ATTR_VAL_ADDRESS)
2348 {
2349 highpc = val.u.uint;
2350 have_highpc = 1;
2351 }
2352 else if (val.encoding == ATTR_VAL_UINT)
2353 {
2354 highpc = val.u.uint;
2355 have_highpc = 1;
2356 highpc_is_relative = 1;
2357 }
2358 break;
2359
2360 case DW_AT_ranges:
2361 if (val.encoding == ATTR_VAL_UINT
2362 || val.encoding == ATTR_VAL_REF_SECTION)
2363 {
2364 ranges = val.u.uint;
2365 have_ranges = 1;
2366 }
2367 break;
2368
2369 default:
2370 break;
2371 }
2372 }
2373 }
2374
2375 /* If we couldn't find a name for the function, we have no use
2376 for it. */
2377 if (is_function && function->name == NULL)
2378 {
2379 backtrace_free (state, function, sizeof *function,
2380 error_callback, data);
2381 is_function = 0;
2382 }
2383
2384 if (is_function)
2385 {
2386 if (have_ranges)
2387 {
2388 if (!add_function_ranges (state, ddata, u, function, ranges,
2389 base, error_callback, data, vec))
2390 return 0;
2391 }
2392 else if (have_lowpc && have_highpc)
2393 {
2394 if (highpc_is_relative)
2395 highpc += lowpc;
e561a992 2396 if (!add_function_range (state, ddata, function, lowpc, highpc,
eff02e4f
ILT
2397 error_callback, data, vec))
2398 return 0;
2399 }
2400 else
2401 {
2402 backtrace_free (state, function, sizeof *function,
2403 error_callback, data);
2404 is_function = 0;
2405 }
2406 }
2407
2408 if (abbrev->has_children)
2409 {
2410 if (!is_function)
2411 {
2412 if (!read_function_entry (state, ddata, u, base, unit_buf, lhdr,
2413 error_callback, data, vec))
2414 return 0;
2415 }
2416 else
2417 {
2418 struct function_vector fvec;
2419
2420 /* Gather any information for inlined functions in
2421 FVEC. */
2422
2423 memset (&fvec, 0, sizeof fvec);
2424
2425 if (!read_function_entry (state, ddata, u, base, unit_buf, lhdr,
2426 error_callback, data, &fvec))
2427 return 0;
2428
2429 if (fvec.count > 0)
2430 {
2431 struct function_addrs *faddrs;
2432
2433 if (!backtrace_vector_release (state, &fvec.vec,
2434 error_callback, data))
2435 return 0;
2436
2437 faddrs = (struct function_addrs *) fvec.vec.base;
2438 qsort (faddrs, fvec.count,
2439 sizeof (struct function_addrs),
2440 function_addrs_compare);
2441
2442 function->function_addrs = faddrs;
2443 function->function_addrs_count = fvec.count;
2444 }
2445 }
2446 }
2447 }
2448
2449 return 1;
2450}
2451
2452/* Read function name information for a compilation unit. We look
2453 through the whole unit looking for function tags. */
2454
2455static void
2456read_function_info (struct backtrace_state *state, struct dwarf_data *ddata,
2457 const struct line_header *lhdr,
2458 backtrace_error_callback error_callback, void *data,
2459 struct unit *u, struct function_vector *fvec,
2460 struct function_addrs **ret_addrs,
2461 size_t *ret_addrs_count)
2462{
2463 struct dwarf_buf unit_buf;
2464 struct function_addrs *addrs;
2465 size_t addrs_count;
2466
2467 unit_buf.name = ".debug_info";
2468 unit_buf.start = ddata->dwarf_info;
2469 unit_buf.buf = u->unit_data;
2470 unit_buf.left = u->unit_data_len;
2471 unit_buf.is_bigendian = ddata->is_bigendian;
2472 unit_buf.error_callback = error_callback;
2473 unit_buf.data = data;
2474 unit_buf.reported_underflow = 0;
2475
2476 while (unit_buf.left > 0)
2477 {
2478 if (!read_function_entry (state, ddata, u, 0, &unit_buf, lhdr,
2479 error_callback, data, fvec))
2480 return;
2481 }
2482
2483 if (fvec->count == 0)
2484 return;
2485
2486 addrs = (struct function_addrs *) fvec->vec.base;
2487 addrs_count = fvec->count;
2488
2489 /* Finish this list of addresses, but leave the remaining space in
2490 the vector available for the next function unit. */
2491 backtrace_vector_finish (state, &fvec->vec);
2492 fvec->count = 0;
2493
2494 qsort (addrs, addrs_count, sizeof (struct function_addrs),
2495 function_addrs_compare);
2496
2497 *ret_addrs = addrs;
2498 *ret_addrs_count = addrs_count;
2499}
2500
2501/* See if PC is inlined in FUNCTION. If it is, print out the inlined
2502 information, and update FILENAME and LINENO for the caller.
2503 Returns whatever CALLBACK returns, or 0 to keep going. */
2504
2505static int
2506report_inlined_functions (uintptr_t pc, struct function *function,
2507 backtrace_full_callback callback, void *data,
2508 const char **filename, int *lineno)
2509{
2510 struct function_addrs *function_addrs;
2511 struct function *inlined;
2512 int ret;
2513
2514 if (function->function_addrs_count == 0)
2515 return 0;
2516
2517 function_addrs = ((struct function_addrs *)
2518 bsearch (&pc, function->function_addrs,
2519 function->function_addrs_count,
2520 sizeof (struct function_addrs),
2521 function_addrs_search));
2522 if (function_addrs == NULL)
2523 return 0;
2524
2525 while (((size_t) (function_addrs - function->function_addrs) + 1
2526 < function->function_addrs_count)
2527 && pc >= (function_addrs + 1)->low
2528 && pc < (function_addrs + 1)->high)
2529 ++function_addrs;
2530
2531 /* We found an inlined call. */
2532
2533 inlined = function_addrs->function;
2534
2535 /* Report any calls inlined into this one. */
2536 ret = report_inlined_functions (pc, inlined, callback, data,
2537 filename, lineno);
2538 if (ret != 0)
2539 return ret;
2540
2541 /* Report this inlined call. */
2542 ret = callback (data, pc, *filename, *lineno, inlined->name);
2543 if (ret != 0)
2544 return ret;
2545
2546 /* Our caller will report the caller of the inlined function; tell
2547 it the appropriate filename and line number. */
2548 *filename = inlined->caller_filename;
2549 *lineno = inlined->caller_lineno;
2550
2551 return 0;
2552}
2553
e561a992
ILT
2554/* Look for a PC in the DWARF mapping for one module. On success,
2555 call CALLBACK and return whatever it returns. On error, call
2556 ERROR_CALLBACK and return 0. Sets *FOUND to 1 if the PC is found,
2557 0 if not. */
eff02e4f
ILT
2558
2559static int
e561a992
ILT
2560dwarf_lookup_pc (struct backtrace_state *state, struct dwarf_data *ddata,
2561 uintptr_t pc, backtrace_full_callback callback,
2562 backtrace_error_callback error_callback, void *data,
2563 int *found)
eff02e4f 2564{
eff02e4f
ILT
2565 struct unit_addrs *entry;
2566 struct unit *u;
2567 int new_data;
2568 struct line *lines;
2569 struct line *ln;
2570 struct function_addrs *function_addrs;
2571 struct function *function;
2572 const char *filename;
2573 int lineno;
2574 int ret;
2575
e561a992 2576 *found = 1;
eff02e4f
ILT
2577
2578 /* Find an address range that includes PC. */
2579 entry = bsearch (&pc, ddata->addrs, ddata->addrs_count,
2580 sizeof (struct unit_addrs), unit_addrs_search);
2581
2582 if (entry == NULL)
e561a992
ILT
2583 {
2584 *found = 0;
2585 return 0;
2586 }
eff02e4f
ILT
2587
2588 /* If there are multiple ranges that contain PC, use the last one,
2589 in order to produce predictable results. If we assume that all
2590 ranges are properly nested, then the last range will be the
2591 smallest one. */
2592 while ((size_t) (entry - ddata->addrs) + 1 < ddata->addrs_count
2593 && pc >= (entry + 1)->low
2594 && pc < (entry + 1)->high)
2595 ++entry;
2596
2597 /* We need the lines, lines_count, function_addrs,
2598 function_addrs_count fields of u. If they are not set, we need
2599 to set them. When running in threaded mode, we need to allow for
2600 the possibility that some other thread is setting them
2601 simultaneously. */
2602
2603 u = entry->u;
2604 lines = u->lines;
2605
2606 /* Skip units with no useful line number information by walking
2607 backward. Useless line number information is marked by setting
2608 lines == -1. */
2609 while (entry > ddata->addrs
2610 && pc >= (entry - 1)->low
2611 && pc < (entry - 1)->high)
2612 {
2613 if (state->threaded)
2614 {
2615 /* Use __sync_bool_compare_and_swap to do a
2616 load-acquire. */
2617 while (!__sync_bool_compare_and_swap (&u->lines, lines, lines))
2618 lines = u->lines;
2619 }
2620
2621 if (lines != (struct line *) (uintptr_t) -1)
2622 break;
2623
2624 --entry;
2625
2626 u = entry->u;
2627 lines = u->lines;
2628 }
2629
2630 /* Do a load-acquire of u->lines. */
2631 if (state->threaded)
2632 {
2633 /* Use __sync_bool_compare_and_swap to do an atomic load. */
2634 while (!__sync_bool_compare_and_swap (&u->lines, lines, lines))
2635 lines = u->lines;
2636 }
2637
2638 new_data = 0;
2639 if (lines == NULL)
2640 {
2641 size_t function_addrs_count;
2642 struct line_header lhdr;
2643 size_t count;
2644
2645 /* We have never read the line information for this unit. Read
2646 it now. */
2647
2648 function_addrs = NULL;
2649 function_addrs_count = 0;
2650 if (read_line_info (state, ddata, error_callback, data, entry->u, &lhdr,
2651 &lines, &count))
2652 {
2653 read_function_info (state, ddata, &lhdr, error_callback, data,
2654 entry->u, &ddata->fvec, &function_addrs,
2655 &function_addrs_count);
2656 free_line_header (state, &lhdr, error_callback, data);
2657 new_data = 1;
2658 }
2659
2660 /* Atomically store the information we just read into the unit.
2661 If another thread is simultaneously writing, it presumably
2662 read the same information, and we don't care which one we
2663 wind up with; we just leak the other one. We do have to
2664 write the lines field last, so that the acquire-loads above
2665 ensure that the other fields are set. */
2666
2667 if (!state->threaded)
2668 {
2669 u->lines_count = count;
2670 u->function_addrs = function_addrs;
2671 u->function_addrs_count = function_addrs_count;
2672 u->lines = lines;
2673 }
2674 else
2675 {
2676 __sync_bool_compare_and_swap (&u->lines_count, 0, count);
2677 __sync_bool_compare_and_swap (&u->function_addrs, NULL,
2678 function_addrs);
2679 __sync_bool_compare_and_swap (&u->function_addrs_count, 0,
2680 function_addrs_count);
2681 __sync_bool_compare_and_swap (&u->lines, NULL, lines);
2682 }
2683 }
2684
2685 /* Now all fields of U have been initialized. */
2686
2687 if (lines == (struct line *) (uintptr_t) -1)
2688 {
2689 /* If reading the line number information failed in some way,
2690 try again to see if there is a better compilation unit for
2691 this PC. */
2692 if (new_data)
e561a992
ILT
2693 return dwarf_lookup_pc (state, ddata, pc, callback, error_callback,
2694 data, found);
eff02e4f
ILT
2695 return callback (data, pc, NULL, 0, NULL);
2696 }
2697
2698 /* Search for PC within this unit. */
2699
2700 ln = (struct line *) bsearch (&pc, lines, entry->u->lines_count,
2701 sizeof (struct line), line_search);
2702 if (ln == NULL)
2703 {
2704 error_callback (data, "inconsistent DWARF line number info", 0);
2705 return 0;
2706 }
2707
2708 /* Search for function name within this unit. */
2709
2710 if (entry->u->function_addrs_count == 0)
2711 return callback (data, pc, ln->filename, ln->lineno, NULL);
2712
2713 function_addrs = ((struct function_addrs *)
2714 bsearch (&pc, entry->u->function_addrs,
2715 entry->u->function_addrs_count,
2716 sizeof (struct function_addrs),
2717 function_addrs_search));
2718 if (function_addrs == NULL)
2719 return callback (data, pc, ln->filename, ln->lineno, NULL);
2720
2721 /* If there are multiple function ranges that contain PC, use the
2722 last one, in order to produce predictable results. */
2723
2724 while (((size_t) (function_addrs - entry->u->function_addrs + 1)
2725 < entry->u->function_addrs_count)
2726 && pc >= (function_addrs + 1)->low
2727 && pc < (function_addrs + 1)->high)
2728 ++function_addrs;
2729
2730 function = function_addrs->function;
2731
2732 filename = ln->filename;
2733 lineno = ln->lineno;
2734
2735 ret = report_inlined_functions (pc, function, callback, data,
2736 &filename, &lineno);
2737 if (ret != 0)
2738 return ret;
2739
2740 return callback (data, pc, filename, lineno, function->name);
2741}
2742
eff02e4f 2743
e561a992
ILT
2744/* Return the file/line information for a PC using the DWARF mapping
2745 we built earlier. */
2746
2747static int
2748dwarf_fileline (struct backtrace_state *state, uintptr_t pc,
2749 backtrace_full_callback callback,
2750 backtrace_error_callback error_callback, void *data)
2751{
2752 struct dwarf_data *ddata;
2753 int found;
2754 int ret;
2755
2756 if (!state->threaded)
2757 {
2758 for (ddata = (struct dwarf_data *) state->fileline_data;
2759 ddata != NULL;
2760 ddata = ddata->next)
2761 {
2762 ret = dwarf_lookup_pc (state, ddata, pc, callback, error_callback,
2763 data, &found);
2764 if (ret != 0 || found)
2765 return ret;
2766 }
2767 }
2768 else
2769 {
2770 struct dwarf_data **pp;
2771
ce8aa074 2772 pp = (struct dwarf_data **) (void *) &state->fileline_data;
e561a992
ILT
2773 while (1)
2774 {
2775 ddata = *pp;
2776 /* Atomic load. */
2777 while (!__sync_bool_compare_and_swap (pp, ddata, ddata))
2778 ddata = *pp;
2779
2780 if (ddata == NULL)
2781 break;
2782
2783 ret = dwarf_lookup_pc (state, ddata, pc, callback, error_callback,
2784 data, &found);
2785 if (ret != 0 || found)
2786 return ret;
2787
2788 pp = &ddata->next;
2789 }
2790 }
2791
2792 /* FIXME: See if any libraries have been dlopen'ed. */
2793
2794 return callback (data, pc, NULL, 0, NULL);
2795}
2796
2797/* Initialize our data structures from the DWARF debug info for a
2798 file. Return NULL on failure. */
2799
2800static struct dwarf_data *
2801build_dwarf_data (struct backtrace_state *state,
2802 uintptr_t base_address,
2803 const unsigned char *dwarf_info,
2804 size_t dwarf_info_size,
2805 const unsigned char *dwarf_line,
2806 size_t dwarf_line_size,
2807 const unsigned char *dwarf_abbrev,
2808 size_t dwarf_abbrev_size,
2809 const unsigned char *dwarf_ranges,
2810 size_t dwarf_ranges_size,
2811 const unsigned char *dwarf_str,
2812 size_t dwarf_str_size,
2813 int is_bigendian,
2814 backtrace_error_callback error_callback,
2815 void *data)
eff02e4f
ILT
2816{
2817 struct unit_addrs_vector addrs_vec;
2818 struct unit_addrs *addrs;
2819 size_t addrs_count;
2820 struct dwarf_data *fdata;
2821
e561a992
ILT
2822 if (!build_address_map (state, base_address, dwarf_info, dwarf_info_size,
2823 dwarf_abbrev, dwarf_abbrev_size, dwarf_ranges,
2824 dwarf_ranges_size, dwarf_str, dwarf_str_size,
2825 is_bigendian, error_callback, data, &addrs_vec))
2826 return NULL;
eff02e4f
ILT
2827
2828 if (!backtrace_vector_release (state, &addrs_vec.vec, error_callback, data))
e561a992 2829 return NULL;
eff02e4f
ILT
2830 addrs = (struct unit_addrs *) addrs_vec.vec.base;
2831 addrs_count = addrs_vec.count;
2832 qsort (addrs, addrs_count, sizeof (struct unit_addrs), unit_addrs_compare);
2833
2834 fdata = ((struct dwarf_data *)
2835 backtrace_alloc (state, sizeof (struct dwarf_data),
2836 error_callback, data));
2837 if (fdata == NULL)
e561a992 2838 return NULL;
eff02e4f 2839
e561a992
ILT
2840 fdata->next = NULL;
2841 fdata->base_address = base_address;
eff02e4f
ILT
2842 fdata->addrs = addrs;
2843 fdata->addrs_count = addrs_count;
2844 fdata->dwarf_info = dwarf_info;
2845 fdata->dwarf_info_size = dwarf_info_size;
2846 fdata->dwarf_line = dwarf_line;
2847 fdata->dwarf_line_size = dwarf_line_size;
2848 fdata->dwarf_ranges = dwarf_ranges;
2849 fdata->dwarf_ranges_size = dwarf_ranges_size;
2850 fdata->dwarf_str = dwarf_str;
2851 fdata->dwarf_str_size = dwarf_str_size;
2852 fdata->is_bigendian = is_bigendian;
2853 memset (&fdata->fvec, 0, sizeof fdata->fvec);
2854
e561a992
ILT
2855 return fdata;
2856}
2857
2858/* Build our data structures from the DWARF sections for a module.
2859 Set FILELINE_FN and STATE->FILELINE_DATA. Return 1 on success, 0
2860 on failure. */
2861
2862int
2863backtrace_dwarf_add (struct backtrace_state *state,
2864 uintptr_t base_address,
2865 const unsigned char *dwarf_info,
2866 size_t dwarf_info_size,
2867 const unsigned char *dwarf_line,
2868 size_t dwarf_line_size,
2869 const unsigned char *dwarf_abbrev,
2870 size_t dwarf_abbrev_size,
2871 const unsigned char *dwarf_ranges,
2872 size_t dwarf_ranges_size,
2873 const unsigned char *dwarf_str,
2874 size_t dwarf_str_size,
2875 int is_bigendian,
2876 backtrace_error_callback error_callback,
2877 void *data, fileline *fileline_fn)
2878{
2879 struct dwarf_data *fdata;
2880
2881 fdata = build_dwarf_data (state, base_address, dwarf_info, dwarf_info_size,
2882 dwarf_line, dwarf_line_size, dwarf_abbrev,
2883 dwarf_abbrev_size, dwarf_ranges, dwarf_ranges_size,
2884 dwarf_str, dwarf_str_size, is_bigendian,
2885 error_callback, data);
2886 if (fdata == NULL)
2887 return 0;
2888
2889 if (!state->threaded)
2890 {
2891 struct dwarf_data **pp;
2892
ce8aa074 2893 for (pp = (struct dwarf_data **) (void *) &state->fileline_data;
e561a992
ILT
2894 *pp != NULL;
2895 pp = &(*pp)->next)
2896 ;
2897 *pp = fdata;
2898 }
2899 else
2900 {
2901 while (1)
2902 {
2903 struct dwarf_data **pp;
2904
ce8aa074 2905 pp = (struct dwarf_data **) (void *) &state->fileline_data;
e561a992
ILT
2906
2907 while (1)
2908 {
2909 struct dwarf_data *p;
2910
2911 /* Atomic load. */
2912 p = *pp;
2913 while (!__sync_bool_compare_and_swap (pp, p, p))
2914 p = *pp;
2915
2916 if (p == NULL)
2917 break;
2918
2919 pp = &p->next;
2920 }
2921
2922 if (__sync_bool_compare_and_swap (pp, NULL, fdata))
2923 break;
2924 }
2925 }
eff02e4f
ILT
2926
2927 *fileline_fn = dwarf_fileline;
2928
2929 return 1;
2930}