]> git.ipfire.org Git - thirdparty/gcc.git/blame - libbacktrace/dwarf.c
Move more code to new gimple-ssa-warn-access pass.
[thirdparty/gcc.git] / libbacktrace / dwarf.c
CommitLineData
eff02e4f 1/* dwarf.c -- Get file/line information from DWARF for backtraces.
99dee823 2 Copyright (C) 2012-2021 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
84ebf639 10 notice, this list of conditions and the following disclaimer.
eff02e4f
ILT
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
84ebf639
CL
15 distribution.
16
eff02e4f
ILT
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;
c926fd82
ILT
95 /* The attribute value, for DW_FORM_implicit_const. */
96 int64_t val;
eff02e4f
ILT
97};
98
99/* A single DWARF abbreviation. */
100
101struct abbrev
102{
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;
113};
114
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. */
121
122struct abbrevs
123{
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;
128};
129
130/* The different kinds of attribute values. */
131
132enum attr_val_encoding
133{
944f59ff
TV
134 /* No attribute value. */
135 ATTR_VAL_NONE,
eff02e4f
ILT
136 /* An address. */
137 ATTR_VAL_ADDRESS,
c926fd82
ILT
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,
eff02e4f
ILT
141 /* A unsigned integer. */
142 ATTR_VAL_UINT,
143 /* A sigd integer. */
144 ATTR_VAL_SINT,
145 /* A string. */
146 ATTR_VAL_STRING,
c926fd82
ILT
147 /* An index into the .debug_str_offsets section. */
148 ATTR_VAL_STRING_INDEX,
eff02e4f
ILT
149 /* An offset to other data in the containing unit. */
150 ATTR_VAL_REF_UNIT,
c926fd82 151 /* An offset to other data within the .debug_info section. */
eff02e4f 152 ATTR_VAL_REF_INFO,
c926fd82 153 /* An offset to other data within the alt .debug_info section. */
1c2a9a37 154 ATTR_VAL_REF_ALT_INFO,
eff02e4f
ILT
155 /* An offset to data in some other section. */
156 ATTR_VAL_REF_SECTION,
157 /* A type signature. */
158 ATTR_VAL_REF_TYPE,
c926fd82
ILT
159 /* An index into the .debug_rnglists section. */
160 ATTR_VAL_RNGLISTS_INDEX,
eff02e4f
ILT
161 /* A block of data (not represented). */
162 ATTR_VAL_BLOCK,
163 /* An expression (not represented). */
164 ATTR_VAL_EXPR,
165};
166
167/* An attribute value. */
168
169struct attr_val
170{
171 /* How the value is stored in the field u. */
172 enum attr_val_encoding encoding;
173 union
174 {
c926fd82 175 /* ATTR_VAL_ADDRESS*, ATTR_VAL_UINT, ATTR_VAL_REF*. */
eff02e4f
ILT
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;
183};
184
185/* The line number program header. */
186
187struct line_header
188{
189 /* The version of the line number information. */
190 int version;
c926fd82
ILT
191 /* Address size. */
192 int addrsize;
eff02e4f
ILT
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;
213};
214
c926fd82
ILT
215/* A format description from a line header. */
216
217struct line_header_format
218{
219 int lnct; /* LNCT code. */
220 enum dwarf_form form; /* Form of entry data. */
221};
222
eff02e4f
ILT
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. */
227
228struct line
229{
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;
d99a7b4d
MS
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;
eff02e4f
ILT
241};
242
243/* A growable vector of line number information. This is used while
244 reading the line numbers. */
245
246struct line_vector
247{
248 /* Memory. This is an array of struct line. */
249 struct backtrace_vector vec;
250 /* Number of valid mappings. */
251 size_t count;
252};
253
254/* A function described in the debug info. */
255
256struct function
257{
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;
269};
270
271/* An address range for a function. This maps a PC value to a
272 specific function. */
273
274struct function_addrs
275{
276 /* Range is LOW <= PC < HIGH. */
277 uint64_t low;
278 uint64_t high;
279 /* Function for this address range. */
280 struct function *function;
281};
282
283/* A growable vector of function address ranges. */
284
285struct function_vector
286{
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;
291};
292
293/* A DWARF compilation unit. This only holds the information we need
294 to map a PC to a file and line. */
295
296struct unit
297{
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;
9d576782
TV
305 /* Offset of the start of the compilation unit from the start of the
306 .debug_info section. */
3f5d2012 307 size_t low_offset;
9d576782
TV
308 /* Offset of the end of the compilation unit from the start of the
309 .debug_info section. */
3f5d2012 310 size_t high_offset;
eff02e4f
ILT
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;
c926fd82
ILT
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;
6b514c53
ILT
325 /* Primary source file. */
326 const char *filename;
eff02e4f
ILT
327 /* Compilation command working directory. */
328 const char *comp_dir;
6b514c53
ILT
329 /* Absolute file name, only set if needed. */
330 const char *abs_filename;
eff02e4f
ILT
331 /* The abbreviations for this unit. */
332 struct abbrevs abbrevs;
333
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. */
338
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;
348};
349
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. */
355
356struct unit_addrs
357{
358 /* Range is LOW <= PC < HIGH. */
359 uint64_t low;
360 uint64_t high;
361 /* Compilation unit for this address range. */
362 struct unit *u;
363};
364
365/* A growable vector of compilation unit address ranges. */
366
367struct unit_addrs_vector
368{
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;
373};
374
9d576782
TV
375/* A growable vector of compilation unit pointer. */
376
377struct unit_vector
378{
379 struct backtrace_vector vec;
380 size_t count;
381};
382
eff02e4f
ILT
383/* The information we need to map a PC to a file and line. */
384
385struct dwarf_data
386{
e561a992
ILT
387 /* The data for the next file we know about. */
388 struct dwarf_data *next;
9ad458d5
TV
389 /* The data for .gnu_debugaltlink. */
390 struct dwarf_data *altlink;
e561a992
ILT
391 /* The base address for this file. */
392 uintptr_t base_address;
eff02e4f
ILT
393 /* A sorted list of address ranges. */
394 struct unit_addrs *addrs;
395 /* Number of address ranges in list. */
396 size_t addrs_count;
9d576782
TV
397 /* A sorted list of units. */
398 struct unit **units;
399 /* Number of units in the list. */
400 size_t units_count;
66ab5839
ILT
401 /* The unparsed DWARF debug data. */
402 struct dwarf_sections dwarf_sections;
eff02e4f
ILT
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;
408};
409
410/* Report an error for a DWARF buffer. */
411
412static void
df003d1e 413dwarf_buf_error (struct dwarf_buf *buf, const char *msg, int errnum)
eff02e4f
ILT
414{
415 char b[200];
416
417 snprintf (b, sizeof b, "%s in %s at %d",
418 msg, buf->name, (int) (buf->buf - buf->start));
df003d1e 419 buf->error_callback (buf->data, b, errnum);
eff02e4f
ILT
420}
421
422/* Require at least COUNT bytes in BUF. Return 1 if all is well, 0 on
423 error. */
424
425static int
426require (struct dwarf_buf *buf, size_t count)
427{
428 if (buf->left >= count)
429 return 1;
430
431 if (!buf->reported_underflow)
432 {
df003d1e 433 dwarf_buf_error (buf, "DWARF underflow", 0);
eff02e4f
ILT
434 buf->reported_underflow = 1;
435 }
436
437 return 0;
438}
439
440/* Advance COUNT bytes in BUF. Return 1 if all is well, 0 on
441 error. */
442
443static int
444advance (struct dwarf_buf *buf, size_t count)
445{
446 if (!require (buf, count))
447 return 0;
448 buf->buf += count;
449 buf->left -= count;
450 return 1;
451}
452
17112570
TV
453/* Read one zero-terminated string from BUF and advance past the string. */
454
455static const char *
456read_string (struct dwarf_buf *buf)
457{
458 const char *p = (const char *)buf->buf;
459 size_t len = strnlen (p, buf->left);
460
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;
465
466 if (!advance (buf, count))
467 return NULL;
468
469 return p;
470}
471
eff02e4f
ILT
472/* Read one byte from BUF and advance 1 byte. */
473
474static unsigned char
475read_byte (struct dwarf_buf *buf)
476{
477 const unsigned char *p = buf->buf;
478
479 if (!advance (buf, 1))
480 return 0;
481 return p[0];
482}
483
484/* Read a signed char from BUF and advance 1 byte. */
485
486static signed char
487read_sbyte (struct dwarf_buf *buf)
488{
489 const unsigned char *p = buf->buf;
490
491 if (!advance (buf, 1))
492 return 0;
493 return (*p ^ 0x80) - 0x80;
494}
495
496/* Read a uint16 from BUF and advance 2 bytes. */
497
498static uint16_t
499read_uint16 (struct dwarf_buf *buf)
500{
501 const unsigned char *p = buf->buf;
502
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];
509}
510
c926fd82
ILT
511/* Read a 24 bit value from BUF and advance 3 bytes. */
512
513static uint32_t
514read_uint24 (struct dwarf_buf *buf)
515{
516 const unsigned char *p = buf->buf;
517
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]);
526}
527
eff02e4f
ILT
528/* Read a uint32 from BUF and advance 4 bytes. */
529
530static uint32_t
531read_uint32 (struct dwarf_buf *buf)
532{
533 const unsigned char *p = buf->buf;
534
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]);
543}
544
545/* Read a uint64 from BUF and advance 8 bytes. */
546
547static uint64_t
548read_uint64 (struct dwarf_buf *buf)
549{
550 const unsigned char *p = buf->buf;
551
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]);
564}
565
566/* Read an offset from BUF and advance the appropriate number of
567 bytes. */
568
569static uint64_t
570read_offset (struct dwarf_buf *buf, int is_dwarf64)
571{
572 if (is_dwarf64)
573 return read_uint64 (buf);
574 else
575 return read_uint32 (buf);
576}
577
578/* Read an address from BUF and advance the appropriate number of
579 bytes. */
580
581static uint64_t
582read_address (struct dwarf_buf *buf, int addrsize)
583{
584 switch (addrsize)
585 {
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:
df003d1e 595 dwarf_buf_error (buf, "unrecognized address size", 0);
eff02e4f
ILT
596 return 0;
597 }
598}
599
600/* Return whether a value is the highest possible address, given the
601 address size. */
602
603static int
604is_highest_address (uint64_t address, int addrsize)
605{
606 switch (addrsize)
607 {
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;
618 }
619}
620
621/* Read an unsigned LEB128 number. */
622
623static uint64_t
624read_uleb128 (struct dwarf_buf *buf)
625{
626 uint64_t ret;
627 unsigned int shift;
be4ba8ae 628 int overflow;
eff02e4f
ILT
629 unsigned char b;
630
631 ret = 0;
632 shift = 0;
be4ba8ae 633 overflow = 0;
eff02e4f
ILT
634 do
635 {
636 const unsigned char *p;
637
638 p = buf->buf;
639 if (!advance (buf, 1))
640 return 0;
641 b = *p;
be4ba8ae
ILT
642 if (shift < 64)
643 ret |= ((uint64_t) (b & 0x7f)) << shift;
644 else if (!overflow)
645 {
df003d1e 646 dwarf_buf_error (buf, "LEB128 overflows uint64_t", 0);
be4ba8ae
ILT
647 overflow = 1;
648 }
eff02e4f
ILT
649 shift += 7;
650 }
651 while ((b & 0x80) != 0);
652
eff02e4f
ILT
653 return ret;
654}
655
656/* Read a signed LEB128 number. */
657
658static int64_t
659read_sleb128 (struct dwarf_buf *buf)
660{
661 uint64_t val;
662 unsigned int shift;
be4ba8ae 663 int overflow;
eff02e4f
ILT
664 unsigned char b;
665
666 val = 0;
667 shift = 0;
be4ba8ae 668 overflow = 0;
eff02e4f
ILT
669 do
670 {
671 const unsigned char *p;
672
673 p = buf->buf;
674 if (!advance (buf, 1))
675 return 0;
676 b = *p;
be4ba8ae
ILT
677 if (shift < 64)
678 val |= ((uint64_t) (b & 0x7f)) << shift;
679 else if (!overflow)
680 {
df003d1e 681 dwarf_buf_error (buf, "signed LEB128 overflows uint64_t", 0);
be4ba8ae
ILT
682 overflow = 1;
683 }
eff02e4f
ILT
684 shift += 7;
685 }
686 while ((b & 0x80) != 0);
687
be4ba8ae 688 if ((b & 0x40) != 0 && shift < 64)
eff02e4f
ILT
689 val |= ((uint64_t) -1) << shift;
690
691 return (int64_t) val;
692}
693
694/* Return the length of an LEB128 number. */
695
696static size_t
697leb128_len (const unsigned char *p)
698{
699 size_t ret;
700
701 ret = 1;
702 while ((*p & 0x80) != 0)
703 {
704 ++p;
705 ++ret;
706 }
707 return ret;
708}
709
292592c5
TV
710/* Read initial_length from BUF and advance the appropriate number of bytes. */
711
712static uint64_t
713read_initial_length (struct dwarf_buf *buf, int *is_dwarf64)
714{
715 uint64_t len;
716
717 len = read_uint32 (buf);
718 if (len == 0xffffffff)
719 {
720 len = read_uint64 (buf);
721 *is_dwarf64 = 1;
722 }
723 else
724 *is_dwarf64 = 0;
725
726 return len;
727}
728
eff02e4f
ILT
729/* Free an abbreviations structure. */
730
731static void
732free_abbrevs (struct backtrace_state *state, struct abbrevs *abbrevs,
733 backtrace_error_callback error_callback, void *data)
734{
735 size_t i;
736
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;
746}
747
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. */
752
753static int
c926fd82
ILT
754read_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,
944f59ff 757 struct dwarf_data *altlink, struct attr_val *val)
eff02e4f 758{
b20ade36
ILT
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);
764
eff02e4f
ILT
765 switch (form)
766 {
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;
c926fd82
ILT
789 case DW_FORM_data16:
790 val->encoding = ATTR_VAL_BLOCK;
791 return advance (buf, 16);
eff02e4f
ILT
792 case DW_FORM_string:
793 val->encoding = ATTR_VAL_STRING;
17112570
TV
794 val->u.string = read_string (buf);
795 return val->u.string == NULL ? 0 : 1;
eff02e4f
ILT
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:
815 {
816 uint64_t offset;
817
818 offset = read_offset (buf, is_dwarf64);
c926fd82 819 if (offset >= dwarf_sections->size[DEBUG_STR])
eff02e4f 820 {
df003d1e 821 dwarf_buf_error (buf, "DW_FORM_strp out of range", 0);
eff02e4f
ILT
822 return 0;
823 }
824 val->encoding = ATTR_VAL_STRING;
c926fd82
ILT
825 val->u.string =
826 (const char *) dwarf_sections->data[DEBUG_STR] + offset;
827 return 1;
828 }
829 case DW_FORM_line_strp:
830 {
831 uint64_t offset;
832
833 offset = read_offset (buf, is_dwarf64);
834 if (offset >= dwarf_sections->size[DEBUG_LINE_STR])
835 {
df003d1e 836 dwarf_buf_error (buf, "DW_FORM_line_strp out of range", 0);
c926fd82
ILT
837 return 0;
838 }
839 val->encoding = ATTR_VAL_STRING;
840 val->u.string =
841 (const char *) dwarf_sections->data[DEBUG_LINE_STR] + offset;
eff02e4f
ILT
842 return 1;
843 }
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:
876 {
877 uint64_t form;
878
879 form = read_uleb128 (buf);
c926fd82
ILT
880 if (form == DW_FORM_implicit_const)
881 {
882 dwarf_buf_error (buf,
df003d1e
ILT
883 "DW_FORM_indirect to DW_FORM_implicit_const",
884 0);
c926fd82
ILT
885 return 0;
886 }
887 return read_attribute ((enum dwarf_form) form, 0, buf, is_dwarf64,
888 version, addrsize, dwarf_sections, altlink,
889 val);
eff02e4f
ILT
890 }
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;
c926fd82
ILT
906 case DW_FORM_strx: case DW_FORM_strx1: case DW_FORM_strx2:
907 case DW_FORM_strx3: case DW_FORM_strx4:
908 {
909 uint64_t offset;
910
911 switch (form)
912 {
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;
931 }
932 val->encoding = ATTR_VAL_STRING_INDEX;
933 val->u.uint = offset;
934 return 1;
935 }
936 case DW_FORM_addrx: case DW_FORM_addrx1: case DW_FORM_addrx2:
937 case DW_FORM_addrx3: case DW_FORM_addrx4:
938 {
939 uint64_t offset;
940
941 switch (form)
942 {
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;
961 }
962 val->encoding = ATTR_VAL_ADDRESS_INDEX;
963 val->u.uint = offset;
964 return 1;
965 }
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;
eff02e4f
ILT
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:
eff02e4f 997 val->u.uint = read_offset (buf, is_dwarf64);
944f59ff
TV
998 if (altlink == NULL)
999 {
1000 val->encoding = ATTR_VAL_NONE;
1001 return 1;
1002 }
1c2a9a37 1003 val->encoding = ATTR_VAL_REF_ALT_INFO;
eff02e4f 1004 return 1;
c926fd82 1005 case DW_FORM_strp_sup: case DW_FORM_GNU_strp_alt:
f2f00d3a
TV
1006 {
1007 uint64_t offset;
c926fd82 1008
f2f00d3a
TV
1009 offset = read_offset (buf, is_dwarf64);
1010 if (altlink == NULL)
1011 {
1012 val->encoding = ATTR_VAL_NONE;
1013 return 1;
1014 }
66ab5839 1015 if (offset >= altlink->dwarf_sections.size[DEBUG_STR])
f2f00d3a 1016 {
df003d1e 1017 dwarf_buf_error (buf, "DW_FORM_strp_sup out of range", 0);
f2f00d3a
TV
1018 return 0;
1019 }
1020 val->encoding = ATTR_VAL_STRING;
66ab5839
ILT
1021 val->u.string =
1022 (const char *) altlink->dwarf_sections.data[DEBUG_STR] + offset;
f2f00d3a
TV
1023 return 1;
1024 }
eff02e4f 1025 default:
df003d1e 1026 dwarf_buf_error (buf, "unrecognized DWARF form", -1);
eff02e4f
ILT
1027 return 0;
1028 }
1029}
1030
c926fd82
ILT
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. */
1036
1037static int
1038resolve_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)
1043{
1044 switch (val->encoding)
1045 {
1046 case ATTR_VAL_STRING:
1047 *string = val->u.string;
1048 return 1;
1049
1050 case ATTR_VAL_STRING_INDEX:
1051 {
1052 uint64_t offset;
1053 struct dwarf_buf offset_buf;
1054
1055 offset = val->u.uint * (is_dwarf64 ? 8 : 4) + str_offsets_base;
1056 if (offset + (is_dwarf64 ? 8 : 4)
8f461a88 1057 > dwarf_sections->size[DEBUG_STR_OFFSETS])
c926fd82
ILT
1058 {
1059 error_callback (data, "DW_FORM_strx value out of range", 0);
1060 return 0;
1061 }
1062
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;
1071
1072 offset = read_offset (&offset_buf, is_dwarf64);
1073 if (offset >= dwarf_sections->size[DEBUG_STR])
1074 {
df003d1e
ILT
1075 dwarf_buf_error (&offset_buf,
1076 "DW_FORM_strx offset out of range",
1077 0);
c926fd82
ILT
1078 return 0;
1079 }
1080 *string = (const char *) dwarf_sections->data[DEBUG_STR] + offset;
1081 return 1;
1082 }
1083
1084 default:
1085 return 1;
1086 }
1087}
1088
1089/* Set *ADDRESS to the real address for a ATTR_VAL_ADDRESS_INDEX.
1090 Return 1 on success, 0 on error. */
1091
1092static int
1093resolve_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 uint64_t *address)
1098{
1099 uint64_t offset;
1100 struct dwarf_buf addr_buf;
1101
1102 offset = addr_index * addrsize + addr_base;
8f461a88 1103 if (offset + addrsize > dwarf_sections->size[DEBUG_ADDR])
c926fd82
ILT
1104 {
1105 error_callback (data, "DW_FORM_addrx value out of range", 0);
1106 return 0;
1107 }
1108
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;
1117
1118 *address = read_address (&addr_buf, addrsize);
1119 return 1;
1120}
1121
9d576782
TV
1122/* Compare a unit offset against a unit for bsearch. */
1123
1124static int
1125units_search (const void *vkey, const void *ventry)
1126{
3f5d2012 1127 const size_t *key = (const size_t *) vkey;
9d576782 1128 const struct unit *entry = *((const struct unit *const *) ventry);
3f5d2012 1129 size_t offset;
9d576782
TV
1130
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;
1138}
1139
1140/* Find a unit in PU containing OFFSET. */
1141
1142static struct unit *
3f5d2012 1143find_unit (struct unit **pu, size_t units_count, size_t offset)
9d576782
TV
1144{
1145 struct unit **u;
1146 u = bsearch (&offset, pu, units_count, sizeof (struct unit *), units_search);
1147 return u == NULL ? NULL : *u;
1148}
1149
eff02e4f
ILT
1150/* Compare function_addrs for qsort. When ranges are nested, make the
1151 smallest one sort last. */
1152
1153static int
1154function_addrs_compare (const void *v1, const void *v2)
1155{
1156 const struct function_addrs *a1 = (const struct function_addrs *) v1;
1157 const struct function_addrs *a2 = (const struct function_addrs *) v2;
1158
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);
1168}
1169
0755f573
ILT
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. */
eff02e4f
ILT
1175
1176static int
1177function_addrs_search (const void *vkey, const void *ventry)
1178{
1179 const uintptr_t *key = (const uintptr_t *) vkey;
1180 const struct function_addrs *entry = (const struct function_addrs *) ventry;
1181 uintptr_t pc;
1182
1183 pc = *key;
1184 if (pc < entry->low)
1185 return -1;
0755f573 1186 else if (pc > (entry + 1)->low)
eff02e4f
ILT
1187 return 1;
1188 else
1189 return 0;
1190}
1191
4b3fc188
ILT
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. */
eff02e4f
ILT
1194
1195static int
4b3fc188
ILT
1196add_unit_addr (struct backtrace_state *state, void *rdata,
1197 uint64_t lowpc, uint64_t highpc,
eff02e4f 1198 backtrace_error_callback error_callback, void *data,
4b3fc188 1199 void *pvec)
eff02e4f 1200{
4b3fc188
ILT
1201 struct unit *u = (struct unit *) rdata;
1202 struct unit_addrs_vector *vec = (struct unit_addrs_vector *) pvec;
eff02e4f
ILT
1203 struct unit_addrs *p;
1204
1205 /* Try to merge with the last entry. */
1206 if (vec->count > 0)
1207 {
1208 p = (struct unit_addrs *) vec->vec.base + (vec->count - 1);
4b3fc188
ILT
1209 if ((lowpc == p->high || lowpc == p->high + 1)
1210 && u == p->u)
eff02e4f 1211 {
4b3fc188
ILT
1212 if (highpc > p->high)
1213 p->high = highpc;
eff02e4f
ILT
1214 return 1;
1215 }
1216 }
1217
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;
1223
4b3fc188
ILT
1224 p->low = lowpc;
1225 p->high = highpc;
1226 p->u = u;
1227
eff02e4f 1228 ++vec->count;
4b3fc188 1229
eff02e4f
ILT
1230 return 1;
1231}
1232
eff02e4f
ILT
1233/* Compare unit_addrs for qsort. When ranges are nested, make the
1234 smallest one sort last. */
1235
1236static int
1237unit_addrs_compare (const void *v1, const void *v2)
1238{
1239 const struct unit_addrs *a1 = (const struct unit_addrs *) v1;
1240 const struct unit_addrs *a2 = (const struct unit_addrs *) v2;
1241
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;
1255}
1256
0755f573
ILT
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. */
eff02e4f
ILT
1262
1263static int
1264unit_addrs_search (const void *vkey, const void *ventry)
1265{
1266 const uintptr_t *key = (const uintptr_t *) vkey;
1267 const struct unit_addrs *entry = (const struct unit_addrs *) ventry;
1268 uintptr_t pc;
1269
1270 pc = *key;
1271 if (pc < entry->low)
1272 return -1;
0755f573 1273 else if (pc > (entry + 1)->low)
eff02e4f
ILT
1274 return 1;
1275 else
1276 return 0;
1277}
1278
d99a7b4d
MS
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. */
eff02e4f
ILT
1283
1284static int
1285line_compare (const void *v1, const void *v2)
1286{
1287 const struct line *ln1 = (const struct line *) v1;
1288 const struct line *ln2 = (const struct line *) v2;
1289
1290 if (ln1->pc < ln2->pc)
1291 return -1;
1292 else if (ln1->pc > ln2->pc)
1293 return 1;
d99a7b4d 1294 else if (ln1->idx < ln2->idx)
eff02e4f 1295 return -1;
d99a7b4d 1296 else if (ln1->idx > ln2->idx)
eff02e4f
ILT
1297 return 1;
1298 else
1299 return 0;
1300}
1301
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. */
1306
1307static int
1308line_search (const void *vkey, const void *ventry)
1309{
1310 const uintptr_t *key = (const uintptr_t *) vkey;
1311 const struct line *entry = (const struct line *) ventry;
1312 uintptr_t pc;
1313
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;
1321}
1322
1323/* Sort the abbrevs by the abbrev code. This function is passed to
1324 both qsort and bsearch. */
1325
1326static int
1327abbrev_compare (const void *v1, const void *v2)
1328{
1329 const struct abbrev *a1 = (const struct abbrev *) v1;
1330 const struct abbrev *a2 = (const struct abbrev *) v2;
1331
1332 if (a1->code < a2->code)
1333 return -1;
1334 else if (a1->code > a2->code)
1335 return 1;
1336 else
1337 {
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;
1342 }
1343}
1344
1345/* Read the abbreviation table for a compilation unit. Returns 1 on
1346 success, 0 on failure. */
1347
1348static int
1349read_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)
1353{
1354 struct dwarf_buf abbrev_buf;
1355 struct dwarf_buf count_buf;
1356 size_t num_abbrevs;
1357
1358 abbrevs->num_abbrevs = 0;
1359 abbrevs->abbrevs = NULL;
1360
1361 if (abbrev_offset >= dwarf_abbrev_size)
1362 {
1363 error_callback (data, "abbrev offset out of range", 0);
1364 return 0;
1365 }
1366
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;
1375
1376 /* Count the number of abbrevs in this list. */
1377
1378 count_buf = abbrev_buf;
1379 num_abbrevs = 0;
1380 while (read_uleb128 (&count_buf) != 0)
1381 {
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)
c926fd82
ILT
1391 {
1392 uint64_t form;
1393
1394 form = read_uleb128 (&count_buf);
1395 if ((enum dwarf_form) form == DW_FORM_implicit_const)
1396 read_sleb128 (&count_buf);
1397 }
eff02e4f
ILT
1398 // Skip form of last attribute.
1399 read_uleb128 (&count_buf);
1400 }
1401
1402 if (count_buf.reported_underflow)
1403 return 0;
1404
1405 if (num_abbrevs == 0)
1406 return 1;
1407
eff02e4f
ILT
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;
18c742b5 1414 abbrevs->num_abbrevs = num_abbrevs;
eff02e4f
ILT
1415 memset (abbrevs->abbrevs, 0, num_abbrevs * sizeof (struct abbrev));
1416
1417 num_abbrevs = 0;
1418 while (1)
1419 {
1420 uint64_t code;
1421 struct abbrev a;
1422 size_t num_attrs;
1423 struct attr *attrs;
1424
1425 if (abbrev_buf.reported_underflow)
1426 goto fail;
1427
1428 code = read_uleb128 (&abbrev_buf);
1429 if (code == 0)
1430 break;
1431
1432 a.code = code;
1433 a.tag = (enum dwarf_tag) read_uleb128 (&abbrev_buf);
1434 a.has_children = read_byte (&abbrev_buf);
1435
1436 count_buf = abbrev_buf;
1437 num_attrs = 0;
1438 while (read_uleb128 (&count_buf) != 0)
1439 {
c926fd82
ILT
1440 uint64_t form;
1441
eff02e4f 1442 ++num_attrs;
c926fd82
ILT
1443 form = read_uleb128 (&count_buf);
1444 if ((enum dwarf_form) form == DW_FORM_implicit_const)
1445 read_sleb128 (&count_buf);
eff02e4f
ILT
1446 }
1447
1448 if (num_attrs == 0)
1449 {
1450 attrs = NULL;
1451 read_uleb128 (&abbrev_buf);
1452 read_uleb128 (&abbrev_buf);
1453 }
1454 else
1455 {
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)
1463 {
1464 uint64_t name;
1465 uint64_t form;
1466
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;
c926fd82
ILT
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;
eff02e4f
ILT
1477 ++num_attrs;
1478 }
1479 }
1480
1481 a.num_attrs = num_attrs;
1482 a.attrs = attrs;
1483
1484 abbrevs->abbrevs[num_abbrevs] = a;
1485 ++num_abbrevs;
1486 }
1487
c5604b48
ILT
1488 backtrace_qsort (abbrevs->abbrevs, abbrevs->num_abbrevs,
1489 sizeof (struct abbrev), abbrev_compare);
eff02e4f
ILT
1490
1491 return 1;
1492
1493 fail:
1494 free_abbrevs (state, abbrevs, error_callback, data);
1495 return 0;
1496}
1497
1498/* Return the abbrev information for an abbrev code. */
1499
1500static const struct abbrev *
1501lookup_abbrev (struct abbrevs *abbrevs, uint64_t code,
1502 backtrace_error_callback error_callback, void *data)
1503{
1504 struct abbrev key;
1505 void *p;
1506
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];
1512
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)
1519 {
1520 error_callback (data, "invalid abbreviation code", 0);
1521 return NULL;
1522 }
1523 return (const struct abbrev *) p;
1524}
1525
4b3fc188
ILT
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. */
1531
1532struct pcrange {
1533 uint64_t lowpc; /* The low PC value. */
1534 int have_lowpc; /* Whether a low PC value was found. */
c926fd82 1535 int lowpc_is_addr_index; /* Whether lowpc is in .debug_addr. */
4b3fc188
ILT
1536 uint64_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. */
c926fd82 1539 int highpc_is_addr_index; /* Whether highpc is in .debug_addr. */
4b3fc188
ILT
1540 uint64_t ranges; /* Offset in ranges section. */
1541 int have_ranges; /* Whether ranges is valid. */
c926fd82 1542 int ranges_is_index; /* Whether ranges is DW_FORM_rnglistx. */
4b3fc188
ILT
1543};
1544
1545/* Update PCRANGE from an attribute value. */
1546
1547static void
1548update_pcrange (const struct attr* attr, const struct attr_val* val,
1549 struct pcrange *pcrange)
1550{
1551 switch (attr->name)
1552 {
1553 case DW_AT_low_pc:
1554 if (val->encoding == ATTR_VAL_ADDRESS)
1555 {
1556 pcrange->lowpc = val->u.uint;
1557 pcrange->have_lowpc = 1;
1558 }
c926fd82
ILT
1559 else if (val->encoding == ATTR_VAL_ADDRESS_INDEX)
1560 {
1561 pcrange->lowpc = val->u.uint;
1562 pcrange->have_lowpc = 1;
1563 pcrange->lowpc_is_addr_index = 1;
1564 }
4b3fc188
ILT
1565 break;
1566
1567 case DW_AT_high_pc:
1568 if (val->encoding == ATTR_VAL_ADDRESS)
1569 {
1570 pcrange->highpc = val->u.uint;
1571 pcrange->have_highpc = 1;
1572 }
1573 else if (val->encoding == ATTR_VAL_UINT)
1574 {
1575 pcrange->highpc = val->u.uint;
1576 pcrange->have_highpc = 1;
1577 pcrange->highpc_is_relative = 1;
1578 }
c926fd82
ILT
1579 else if (val->encoding == ATTR_VAL_ADDRESS_INDEX)
1580 {
1581 pcrange->highpc = val->u.uint;
1582 pcrange->have_highpc = 1;
1583 pcrange->highpc_is_addr_index = 1;
1584 }
4b3fc188
ILT
1585 break;
1586
1587 case DW_AT_ranges:
1588 if (val->encoding == ATTR_VAL_UINT
1589 || val->encoding == ATTR_VAL_REF_SECTION)
1590 {
1591 pcrange->ranges = val->u.uint;
1592 pcrange->have_ranges = 1;
1593 }
c926fd82
ILT
1594 else if (val->encoding == ATTR_VAL_RNGLISTS_INDEX)
1595 {
1596 pcrange->ranges = val->u.uint;
1597 pcrange->have_ranges = 1;
1598 pcrange->ranges_is_index = 1;
1599 }
4b3fc188
ILT
1600 break;
1601
1602 default:
1603 break;
1604 }
1605}
1606
c926fd82
ILT
1607/* Call ADD_RANGE for a low/high PC pair. Returns 1 on success, 0 on
1608 error. */
eff02e4f
ILT
1609
1610static int
c926fd82
ILT
1611add_low_high_range (struct backtrace_state *state,
1612 const struct dwarf_sections *dwarf_sections,
1613 uintptr_t base_address, int is_bigendian,
1614 struct unit *u, const struct pcrange *pcrange,
1615 int (*add_range) (struct backtrace_state *state,
1616 void *rdata, uint64_t lowpc,
1617 uint64_t highpc,
1618 backtrace_error_callback error_callback,
1619 void *data, void *vec),
1620 void *rdata,
1621 backtrace_error_callback error_callback, void *data,
1622 void *vec)
eff02e4f 1623{
c926fd82
ILT
1624 uint64_t lowpc;
1625 uint64_t highpc;
eff02e4f 1626
c926fd82
ILT
1627 lowpc = pcrange->lowpc;
1628 if (pcrange->lowpc_is_addr_index)
4b3fc188 1629 {
c926fd82
ILT
1630 if (!resolve_addr_index (dwarf_sections, u->addr_base, u->addrsize,
1631 is_bigendian, lowpc, error_callback, data,
1632 &lowpc))
1633 return 0;
4b3fc188
ILT
1634 }
1635
c926fd82
ILT
1636 highpc = pcrange->highpc;
1637 if (pcrange->highpc_is_addr_index)
4b3fc188 1638 {
c926fd82
ILT
1639 if (!resolve_addr_index (dwarf_sections, u->addr_base, u->addrsize,
1640 is_bigendian, highpc, error_callback, data,
1641 &highpc))
1642 return 0;
4b3fc188 1643 }
c926fd82
ILT
1644 if (pcrange->highpc_is_relative)
1645 highpc += lowpc;
1646
1647 /* Add in the base address of the module when recording PC values,
1648 so that we can look up the PC directly. */
1649 lowpc += base_address;
1650 highpc += base_address;
1651
1652 return add_range (state, rdata, lowpc, highpc, error_callback, data, vec);
1653}
1654
1655/* Call ADD_RANGE for each range read from .debug_ranges, as used in
1656 DWARF versions 2 through 4. */
1657
1658static int
1659add_ranges_from_ranges (
1660 struct backtrace_state *state,
1661 const struct dwarf_sections *dwarf_sections,
1662 uintptr_t base_address, int is_bigendian,
1663 struct unit *u, uint64_t base,
1664 const struct pcrange *pcrange,
1665 int (*add_range) (struct backtrace_state *state, void *rdata,
1666 uint64_t lowpc, uint64_t highpc,
1667 backtrace_error_callback error_callback, void *data,
1668 void *vec),
1669 void *rdata,
1670 backtrace_error_callback error_callback, void *data,
1671 void *vec)
1672{
1673 struct dwarf_buf ranges_buf;
4b3fc188
ILT
1674
1675 if (pcrange->ranges >= dwarf_sections->size[DEBUG_RANGES])
eff02e4f
ILT
1676 {
1677 error_callback (data, "ranges offset out of range", 0);
1678 return 0;
1679 }
1680
1681 ranges_buf.name = ".debug_ranges";
4b3fc188
ILT
1682 ranges_buf.start = dwarf_sections->data[DEBUG_RANGES];
1683 ranges_buf.buf = dwarf_sections->data[DEBUG_RANGES] + pcrange->ranges;
1684 ranges_buf.left = dwarf_sections->size[DEBUG_RANGES] - pcrange->ranges;
eff02e4f
ILT
1685 ranges_buf.is_bigendian = is_bigendian;
1686 ranges_buf.error_callback = error_callback;
1687 ranges_buf.data = data;
1688 ranges_buf.reported_underflow = 0;
1689
1690 while (1)
1691 {
1692 uint64_t low;
1693 uint64_t high;
1694
1695 if (ranges_buf.reported_underflow)
1696 return 0;
1697
1698 low = read_address (&ranges_buf, u->addrsize);
1699 high = read_address (&ranges_buf, u->addrsize);
1700
1701 if (low == 0 && high == 0)
1702 break;
1703
1704 if (is_highest_address (low, u->addrsize))
1705 base = high;
1706 else
1707 {
4b3fc188
ILT
1708 if (!add_range (state, rdata,
1709 low + base + base_address,
1710 high + base + base_address,
1711 error_callback, data, vec))
eff02e4f
ILT
1712 return 0;
1713 }
1714 }
1715
1716 if (ranges_buf.reported_underflow)
1717 return 0;
1718
1719 return 1;
1720}
1721
c926fd82
ILT
1722/* Call ADD_RANGE for each range read from .debug_rnglists, as used in
1723 DWARF version 5. */
1724
1725static int
1726add_ranges_from_rnglists (
1727 struct backtrace_state *state,
1728 const struct dwarf_sections *dwarf_sections,
1729 uintptr_t base_address, int is_bigendian,
1730 struct unit *u, uint64_t base,
1731 const struct pcrange *pcrange,
1732 int (*add_range) (struct backtrace_state *state, void *rdata,
1733 uint64_t lowpc, uint64_t highpc,
1734 backtrace_error_callback error_callback, void *data,
1735 void *vec),
1736 void *rdata,
1737 backtrace_error_callback error_callback, void *data,
1738 void *vec)
1739{
1740 uint64_t offset;
1741 struct dwarf_buf rnglists_buf;
1742
1743 if (!pcrange->ranges_is_index)
1744 offset = pcrange->ranges;
1745 else
1746 offset = u->rnglists_base + pcrange->ranges * (u->is_dwarf64 ? 8 : 4);
1747 if (offset >= dwarf_sections->size[DEBUG_RNGLISTS])
1748 {
1749 error_callback (data, "rnglists offset out of range", 0);
1750 return 0;
1751 }
1752
1753 rnglists_buf.name = ".debug_rnglists";
1754 rnglists_buf.start = dwarf_sections->data[DEBUG_RNGLISTS];
1755 rnglists_buf.buf = dwarf_sections->data[DEBUG_RNGLISTS] + offset;
1756 rnglists_buf.left = dwarf_sections->size[DEBUG_RNGLISTS] - offset;
1757 rnglists_buf.is_bigendian = is_bigendian;
1758 rnglists_buf.error_callback = error_callback;
1759 rnglists_buf.data = data;
1760 rnglists_buf.reported_underflow = 0;
1761
1762 if (pcrange->ranges_is_index)
1763 {
1764 offset = read_offset (&rnglists_buf, u->is_dwarf64);
1765 offset += u->rnglists_base;
1766 if (offset >= dwarf_sections->size[DEBUG_RNGLISTS])
1767 {
1768 error_callback (data, "rnglists index offset out of range", 0);
1769 return 0;
1770 }
1771 rnglists_buf.buf = dwarf_sections->data[DEBUG_RNGLISTS] + offset;
1772 rnglists_buf.left = dwarf_sections->size[DEBUG_RNGLISTS] - offset;
1773 }
1774
1775 while (1)
1776 {
1777 unsigned char rle;
1778
1779 rle = read_byte (&rnglists_buf);
1780 if (rle == DW_RLE_end_of_list)
1781 break;
1782 switch (rle)
1783 {
1784 case DW_RLE_base_addressx:
1785 {
1786 uint64_t index;
1787
1788 index = read_uleb128 (&rnglists_buf);
1789 if (!resolve_addr_index (dwarf_sections, u->addr_base,
1790 u->addrsize, is_bigendian, index,
1791 error_callback, data, &base))
1792 return 0;
1793 }
1794 break;
1795
1796 case DW_RLE_startx_endx:
1797 {
1798 uint64_t index;
1799 uint64_t low;
1800 uint64_t high;
1801
1802 index = read_uleb128 (&rnglists_buf);
1803 if (!resolve_addr_index (dwarf_sections, u->addr_base,
1804 u->addrsize, is_bigendian, index,
1805 error_callback, data, &low))
1806 return 0;
1807 index = read_uleb128 (&rnglists_buf);
1808 if (!resolve_addr_index (dwarf_sections, u->addr_base,
1809 u->addrsize, is_bigendian, index,
1810 error_callback, data, &high))
1811 return 0;
1812 if (!add_range (state, rdata, low + base_address,
1813 high + base_address, error_callback, data,
1814 vec))
1815 return 0;
1816 }
1817 break;
1818
1819 case DW_RLE_startx_length:
1820 {
1821 uint64_t index;
1822 uint64_t low;
1823 uint64_t length;
1824
1825 index = read_uleb128 (&rnglists_buf);
1826 if (!resolve_addr_index (dwarf_sections, u->addr_base,
1827 u->addrsize, is_bigendian, index,
1828 error_callback, data, &low))
1829 return 0;
1830 length = read_uleb128 (&rnglists_buf);
1831 low += base_address;
1832 if (!add_range (state, rdata, low, low + length,
1833 error_callback, data, vec))
1834 return 0;
1835 }
1836 break;
1837
1838 case DW_RLE_offset_pair:
1839 {
1840 uint64_t low;
1841 uint64_t high;
1842
1843 low = read_uleb128 (&rnglists_buf);
1844 high = read_uleb128 (&rnglists_buf);
1845 if (!add_range (state, rdata, low + base + base_address,
1846 high + base + base_address,
1847 error_callback, data, vec))
1848 return 0;
1849 }
1850 break;
1851
1852 case DW_RLE_base_address:
1853 base = read_address (&rnglists_buf, u->addrsize);
1854 break;
1855
1856 case DW_RLE_start_end:
1857 {
1858 uint64_t low;
1859 uint64_t high;
1860
1861 low = read_address (&rnglists_buf, u->addrsize);
1862 high = read_address (&rnglists_buf, u->addrsize);
1863 if (!add_range (state, rdata, low + base_address,
1864 high + base_address, error_callback, data,
1865 vec))
1866 return 0;
1867 }
1868 break;
1869
1870 case DW_RLE_start_length:
1871 {
1872 uint64_t low;
1873 uint64_t length;
1874
1875 low = read_address (&rnglists_buf, u->addrsize);
1876 length = read_uleb128 (&rnglists_buf);
1877 low += base_address;
1878 if (!add_range (state, rdata, low, low + length,
1879 error_callback, data, vec))
1880 return 0;
1881 }
1882 break;
1883
1884 default:
df003d1e 1885 dwarf_buf_error (&rnglists_buf, "unrecognized DW_RLE value", -1);
c926fd82
ILT
1886 return 0;
1887 }
1888 }
1889
1890 if (rnglists_buf.reported_underflow)
1891 return 0;
1892
1893 return 1;
1894}
1895
1896/* Call ADD_RANGE for each lowpc/highpc pair in PCRANGE. RDATA is
1897 passed to ADD_RANGE, and is either a struct unit * or a struct
1898 function *. VEC is the vector we are adding ranges to, and is
1899 either a struct unit_addrs_vector * or a struct function_vector *.
1900 Returns 1 on success, 0 on error. */
1901
1902static int
1903add_ranges (struct backtrace_state *state,
1904 const struct dwarf_sections *dwarf_sections,
1905 uintptr_t base_address, int is_bigendian,
1906 struct unit *u, uint64_t base, const struct pcrange *pcrange,
1907 int (*add_range) (struct backtrace_state *state, void *rdata,
1908 uint64_t lowpc, uint64_t highpc,
1909 backtrace_error_callback error_callback,
1910 void *data, void *vec),
1911 void *rdata,
1912 backtrace_error_callback error_callback, void *data,
1913 void *vec)
1914{
1915 if (pcrange->have_lowpc && pcrange->have_highpc)
1916 return add_low_high_range (state, dwarf_sections, base_address,
1917 is_bigendian, u, pcrange, add_range, rdata,
1918 error_callback, data, vec);
1919
1920 if (!pcrange->have_ranges)
1921 {
1922 /* Did not find any address ranges to add. */
1923 return 1;
1924 }
1925
1926 if (u->version < 5)
1927 return add_ranges_from_ranges (state, dwarf_sections, base_address,
1928 is_bigendian, u, base, pcrange, add_range,
1929 rdata, error_callback, data, vec);
1930 else
1931 return add_ranges_from_rnglists (state, dwarf_sections, base_address,
1932 is_bigendian, u, base, pcrange, add_range,
1933 rdata, error_callback, data, vec);
1934}
1935
b8ddd61b
ILT
1936/* Find the address range covered by a compilation unit, reading from
1937 UNIT_BUF and adding values to U. Returns 1 if all data could be
1938 read, 0 if there is some error. */
eff02e4f
ILT
1939
1940static int
b8ddd61b 1941find_address_ranges (struct backtrace_state *state, uintptr_t base_address,
84ebf639 1942 struct dwarf_buf *unit_buf,
4b3fc188 1943 const struct dwarf_sections *dwarf_sections,
944f59ff
TV
1944 int is_bigendian, struct dwarf_data *altlink,
1945 backtrace_error_callback error_callback, void *data,
9d576782
TV
1946 struct unit *u, struct unit_addrs_vector *addrs,
1947 enum dwarf_tag *unit_tag)
eff02e4f 1948{
b8ddd61b 1949 while (unit_buf->left > 0)
eff02e4f 1950 {
eff02e4f 1951 uint64_t code;
b8ddd61b 1952 const struct abbrev *abbrev;
4b3fc188 1953 struct pcrange pcrange;
c926fd82
ILT
1954 struct attr_val name_val;
1955 int have_name_val;
1956 struct attr_val comp_dir_val;
1957 int have_comp_dir_val;
b8ddd61b 1958 size_t i;
eff02e4f 1959
b8ddd61b
ILT
1960 code = read_uleb128 (unit_buf);
1961 if (code == 0)
1962 return 1;
eff02e4f 1963
b8ddd61b 1964 abbrev = lookup_abbrev (&u->abbrevs, code, error_callback, data);
eff02e4f 1965 if (abbrev == NULL)
b8ddd61b 1966 return 0;
eff02e4f 1967
9d576782
TV
1968 if (unit_tag != NULL)
1969 *unit_tag = abbrev->tag;
1970
4b3fc188 1971 memset (&pcrange, 0, sizeof pcrange);
c926fd82
ILT
1972 memset (&name_val, 0, sizeof name_val);
1973 have_name_val = 0;
1974 memset (&comp_dir_val, 0, sizeof comp_dir_val);
1975 have_comp_dir_val = 0;
eff02e4f
ILT
1976 for (i = 0; i < abbrev->num_attrs; ++i)
1977 {
1978 struct attr_val val;
1979
c926fd82
ILT
1980 if (!read_attribute (abbrev->attrs[i].form, abbrev->attrs[i].val,
1981 unit_buf, u->is_dwarf64, u->version,
1982 u->addrsize, dwarf_sections, altlink, &val))
b8ddd61b 1983 return 0;
eff02e4f
ILT
1984
1985 switch (abbrev->attrs[i].name)
1986 {
4b3fc188
ILT
1987 case DW_AT_low_pc: case DW_AT_high_pc: case DW_AT_ranges:
1988 update_pcrange (&abbrev->attrs[i], &val, &pcrange);
eff02e4f 1989 break;
b8ddd61b 1990
eff02e4f 1991 case DW_AT_stmt_list:
b8ddd61b
ILT
1992 if (abbrev->tag == DW_TAG_compile_unit
1993 && (val.encoding == ATTR_VAL_UINT
1994 || val.encoding == ATTR_VAL_REF_SECTION))
1995 u->lineoff = val.u.uint;
eff02e4f 1996 break;
b8ddd61b 1997
6b514c53 1998 case DW_AT_name:
c926fd82
ILT
1999 if (abbrev->tag == DW_TAG_compile_unit)
2000 {
2001 name_val = val;
2002 have_name_val = 1;
2003 }
6b514c53 2004 break;
b8ddd61b 2005
eff02e4f 2006 case DW_AT_comp_dir:
c926fd82
ILT
2007 if (abbrev->tag == DW_TAG_compile_unit)
2008 {
2009 comp_dir_val = val;
2010 have_comp_dir_val = 1;
2011 }
2012 break;
2013
2014 case DW_AT_str_offsets_base:
2015 if (abbrev->tag == DW_TAG_compile_unit
2016 && val.encoding == ATTR_VAL_REF_SECTION)
2017 u->str_offsets_base = val.u.uint;
2018 break;
2019
2020 case DW_AT_addr_base:
2021 if (abbrev->tag == DW_TAG_compile_unit
2022 && val.encoding == ATTR_VAL_REF_SECTION)
2023 u->addr_base = val.u.uint;
2024 break;
2025
2026 case DW_AT_rnglists_base:
b8ddd61b 2027 if (abbrev->tag == DW_TAG_compile_unit
c926fd82
ILT
2028 && val.encoding == ATTR_VAL_REF_SECTION)
2029 u->rnglists_base = val.u.uint;
eff02e4f 2030 break;
b8ddd61b 2031
eff02e4f
ILT
2032 default:
2033 break;
2034 }
2035 }
2036
c926fd82
ILT
2037 // Resolve strings after we're sure that we have seen
2038 // DW_AT_str_offsets_base.
2039 if (have_name_val)
2040 {
2041 if (!resolve_string (dwarf_sections, u->is_dwarf64, is_bigendian,
2042 u->str_offsets_base, &name_val,
2043 error_callback, data, &u->filename))
2044 return 0;
2045 }
2046 if (have_comp_dir_val)
2047 {
2048 if (!resolve_string (dwarf_sections, u->is_dwarf64, is_bigendian,
2049 u->str_offsets_base, &comp_dir_val,
2050 error_callback, data, &u->comp_dir))
2051 return 0;
2052 }
2053
b8ddd61b
ILT
2054 if (abbrev->tag == DW_TAG_compile_unit
2055 || abbrev->tag == DW_TAG_subprogram)
eff02e4f 2056 {
4b3fc188
ILT
2057 if (!add_ranges (state, dwarf_sections, base_address,
2058 is_bigendian, u, pcrange.lowpc, &pcrange,
2059 add_unit_addr, (void *) u, error_callback, data,
2060 (void *) addrs))
2061 return 0;
b8ddd61b
ILT
2062
2063 /* If we found the PC range in the DW_TAG_compile_unit, we
2064 can stop now. */
2065 if (abbrev->tag == DW_TAG_compile_unit
4b3fc188
ILT
2066 && (pcrange.have_ranges
2067 || (pcrange.have_lowpc && pcrange.have_highpc)))
b8ddd61b 2068 return 1;
eff02e4f 2069 }
b8ddd61b
ILT
2070
2071 if (abbrev->has_children)
eff02e4f 2072 {
b8ddd61b 2073 if (!find_address_ranges (state, base_address, unit_buf,
4b3fc188
ILT
2074 dwarf_sections, is_bigendian, altlink,
2075 error_callback, data, u, addrs, NULL))
b8ddd61b
ILT
2076 return 0;
2077 }
2078 }
2079
2080 return 1;
2081}
2082
2083/* Build a mapping from address ranges to the compilation units where
2084 the line number information for that range can be found. Returns 1
2085 on success, 0 on failure. */
2086
2087static int
2088build_address_map (struct backtrace_state *state, uintptr_t base_address,
66ab5839 2089 const struct dwarf_sections *dwarf_sections,
944f59ff
TV
2090 int is_bigendian, struct dwarf_data *altlink,
2091 backtrace_error_callback error_callback, void *data,
9d576782
TV
2092 struct unit_addrs_vector *addrs,
2093 struct unit_vector *unit_vec)
b8ddd61b
ILT
2094{
2095 struct dwarf_buf info;
40b8d3b2
ILT
2096 struct backtrace_vector units;
2097 size_t units_count;
2098 size_t i;
2099 struct unit **pu;
3f5d2012 2100 size_t unit_offset = 0;
0755f573 2101 struct unit_addrs *pa;
b8ddd61b
ILT
2102
2103 memset (&addrs->vec, 0, sizeof addrs->vec);
9d576782 2104 memset (&unit_vec->vec, 0, sizeof unit_vec->vec);
b8ddd61b 2105 addrs->count = 0;
9d576782 2106 unit_vec->count = 0;
b8ddd61b
ILT
2107
2108 /* Read through the .debug_info section. FIXME: Should we use the
2109 .debug_aranges section? gdb and addr2line don't use it, but I'm
2110 not sure why. */
2111
2112 info.name = ".debug_info";
66ab5839
ILT
2113 info.start = dwarf_sections->data[DEBUG_INFO];
2114 info.buf = info.start;
2115 info.left = dwarf_sections->size[DEBUG_INFO];
b8ddd61b
ILT
2116 info.is_bigendian = is_bigendian;
2117 info.error_callback = error_callback;
2118 info.data = data;
2119 info.reported_underflow = 0;
2120
40b8d3b2
ILT
2121 memset (&units, 0, sizeof units);
2122 units_count = 0;
2123
b8ddd61b
ILT
2124 while (info.left > 0)
2125 {
2126 const unsigned char *unit_data_start;
2127 uint64_t len;
2128 int is_dwarf64;
2129 struct dwarf_buf unit_buf;
2130 int version;
c926fd82 2131 int unit_type;
b8ddd61b
ILT
2132 uint64_t abbrev_offset;
2133 int addrsize;
2134 struct unit *u;
9d576782 2135 enum dwarf_tag unit_tag;
b8ddd61b
ILT
2136
2137 if (info.reported_underflow)
2138 goto fail;
2139
2140 unit_data_start = info.buf;
2141
292592c5 2142 len = read_initial_length (&info, &is_dwarf64);
b8ddd61b
ILT
2143 unit_buf = info;
2144 unit_buf.left = len;
2145
2146 if (!advance (&info, len))
2147 goto fail;
2148
2149 version = read_uint16 (&unit_buf);
c926fd82 2150 if (version < 2 || version > 5)
b8ddd61b 2151 {
df003d1e 2152 dwarf_buf_error (&unit_buf, "unrecognized DWARF version", -1);
b8ddd61b
ILT
2153 goto fail;
2154 }
2155
c926fd82
ILT
2156 if (version < 5)
2157 unit_type = 0;
2158 else
2159 {
2160 unit_type = read_byte (&unit_buf);
2161 if (unit_type == DW_UT_type || unit_type == DW_UT_split_type)
2162 {
2163 /* This unit doesn't have anything we need. */
2164 continue;
2165 }
2166 }
2167
40b8d3b2
ILT
2168 pu = ((struct unit **)
2169 backtrace_vector_grow (state, sizeof (struct unit *),
2170 error_callback, data, &units));
2171 if (pu == NULL)
2172 goto fail;
2173
b8ddd61b
ILT
2174 u = ((struct unit *)
2175 backtrace_alloc (state, sizeof *u, error_callback, data));
2176 if (u == NULL)
2177 goto fail;
40b8d3b2
ILT
2178
2179 *pu = u;
2180 ++units_count;
2181
c926fd82
ILT
2182 if (version < 5)
2183 addrsize = 0; /* Set below. */
2184 else
2185 addrsize = read_byte (&unit_buf);
2186
d9aa0961
TV
2187 memset (&u->abbrevs, 0, sizeof u->abbrevs);
2188 abbrev_offset = read_offset (&unit_buf, is_dwarf64);
66ab5839
ILT
2189 if (!read_abbrevs (state, abbrev_offset,
2190 dwarf_sections->data[DEBUG_ABBREV],
2191 dwarf_sections->size[DEBUG_ABBREV],
d9aa0961
TV
2192 is_bigendian, error_callback, data, &u->abbrevs))
2193 goto fail;
2194
c926fd82
ILT
2195 if (version < 5)
2196 addrsize = read_byte (&unit_buf);
2197
2198 switch (unit_type)
2199 {
2200 case 0:
2201 break;
2202 case DW_UT_compile: case DW_UT_partial:
2203 break;
2204 case DW_UT_skeleton: case DW_UT_split_compile:
2205 read_uint64 (&unit_buf); /* dwo_id */
2206 break;
2207 default:
2208 break;
2209 }
d9aa0961 2210
9d576782
TV
2211 u->low_offset = unit_offset;
2212 unit_offset += len + (is_dwarf64 ? 12 : 4);
2213 u->high_offset = unit_offset;
b8ddd61b
ILT
2214 u->unit_data = unit_buf.buf;
2215 u->unit_data_len = unit_buf.left;
2216 u->unit_data_offset = unit_buf.buf - unit_data_start;
2217 u->version = version;
2218 u->is_dwarf64 = is_dwarf64;
2219 u->addrsize = addrsize;
2220 u->filename = NULL;
2221 u->comp_dir = NULL;
2222 u->abs_filename = NULL;
2223 u->lineoff = 0;
b8ddd61b
ILT
2224
2225 /* The actual line number mappings will be read as needed. */
2226 u->lines = NULL;
2227 u->lines_count = 0;
2228 u->function_addrs = NULL;
2229 u->function_addrs_count = 0;
2230
4b3fc188 2231 if (!find_address_ranges (state, base_address, &unit_buf, dwarf_sections,
944f59ff 2232 is_bigendian, altlink, error_callback, data,
9d576782 2233 u, addrs, &unit_tag))
40b8d3b2 2234 goto fail;
b8ddd61b
ILT
2235
2236 if (unit_buf.reported_underflow)
40b8d3b2 2237 goto fail;
eff02e4f
ILT
2238 }
2239 if (info.reported_underflow)
2240 goto fail;
2241
0755f573
ILT
2242 /* Add a trailing addrs entry, but don't include it in addrs->count. */
2243 pa = ((struct unit_addrs *)
2244 backtrace_vector_grow (state, sizeof (struct unit_addrs),
2245 error_callback, data, &addrs->vec));
2246 if (pa == NULL)
2247 goto fail;
2248 pa->low = 0;
2249 --pa->low;
2250 pa->high = pa->low;
2251 pa->u = NULL;
2252
9d576782
TV
2253 unit_vec->vec = units;
2254 unit_vec->count = units_count;
eff02e4f
ILT
2255 return 1;
2256
2257 fail:
40b8d3b2
ILT
2258 if (units_count > 0)
2259 {
2260 pu = (struct unit **) units.base;
2261 for (i = 0; i < units_count; i++)
2262 {
2263 free_abbrevs (state, &pu[i]->abbrevs, error_callback, data);
2264 backtrace_free (state, pu[i], sizeof **pu, error_callback, data);
2265 }
2266 backtrace_vector_free (state, &units, error_callback, data);
2267 }
53a52133
TV
2268 if (addrs->count > 0)
2269 {
2270 backtrace_vector_free (state, &addrs->vec, error_callback, data);
2271 addrs->count = 0;
2272 }
eff02e4f
ILT
2273 return 0;
2274}
2275
2276/* Add a new mapping to the vector of line mappings that we are
2277 building. Returns 1 on success, 0 on failure. */
2278
2279static int
e561a992
ILT
2280add_line (struct backtrace_state *state, struct dwarf_data *ddata,
2281 uintptr_t pc, const char *filename, int lineno,
2282 backtrace_error_callback error_callback, void *data,
eff02e4f
ILT
2283 struct line_vector *vec)
2284{
2285 struct line *ln;
2286
2287 /* If we are adding the same mapping, ignore it. This can happen
2288 when using discriminators. */
2289 if (vec->count > 0)
2290 {
2291 ln = (struct line *) vec->vec.base + (vec->count - 1);
2292 if (pc == ln->pc && filename == ln->filename && lineno == ln->lineno)
2293 return 1;
2294 }
2295
2296 ln = ((struct line *)
2297 backtrace_vector_grow (state, sizeof (struct line), error_callback,
2298 data, &vec->vec));
2299 if (ln == NULL)
2300 return 0;
2301
e561a992
ILT
2302 /* Add in the base address here, so that we can look up the PC
2303 directly. */
2304 ln->pc = pc + ddata->base_address;
2305
eff02e4f
ILT
2306 ln->filename = filename;
2307 ln->lineno = lineno;
d99a7b4d 2308 ln->idx = vec->count;
eff02e4f
ILT
2309
2310 ++vec->count;
2311
2312 return 1;
2313}
2314
281161d1 2315/* Free the line header information. */
eff02e4f
ILT
2316
2317static void
2318free_line_header (struct backtrace_state *state, struct line_header *hdr,
2319 backtrace_error_callback error_callback, void *data)
2320{
281161d1
TM
2321 if (hdr->dirs_count != 0)
2322 backtrace_free (state, hdr->dirs, hdr->dirs_count * sizeof (const char *),
2323 error_callback, data);
eff02e4f
ILT
2324 backtrace_free (state, hdr->filenames,
2325 hdr->filenames_count * sizeof (char *),
2326 error_callback, data);
2327}
2328
c926fd82
ILT
2329/* Read the directories and file names for a line header for version
2330 2, setting fields in HDR. Return 1 on success, 0 on failure. */
eff02e4f
ILT
2331
2332static int
c926fd82
ILT
2333read_v2_paths (struct backtrace_state *state, struct unit *u,
2334 struct dwarf_buf *hdr_buf, struct line_header *hdr)
eff02e4f 2335{
eff02e4f
ILT
2336 const unsigned char *p;
2337 const unsigned char *pend;
2338 size_t i;
2339
eff02e4f
ILT
2340 /* Count the number of directory entries. */
2341 hdr->dirs_count = 0;
c926fd82
ILT
2342 p = hdr_buf->buf;
2343 pend = p + hdr_buf->left;
eff02e4f
ILT
2344 while (p < pend && *p != '\0')
2345 {
2346 p += strnlen((const char *) p, pend - p) + 1;
2347 ++hdr->dirs_count;
2348 }
2349
325e70b4
ILT
2350 /* The index of the first entry in the list of directories is 1. Index 0 is
2351 used for the current directory of the compilation. To simplify index
2352 handling, we set entry 0 to the compilation unit directory. */
2353 ++hdr->dirs_count;
2354 hdr->dirs = ((const char **)
2355 backtrace_alloc (state,
2356 hdr->dirs_count * sizeof (const char *),
2357 hdr_buf->error_callback,
2358 hdr_buf->data));
2359 if (hdr->dirs == NULL)
2360 return 0;
eff02e4f 2361
325e70b4
ILT
2362 hdr->dirs[0] = u->comp_dir;
2363 i = 1;
c926fd82 2364 while (*hdr_buf->buf != '\0')
eff02e4f 2365 {
c926fd82 2366 if (hdr_buf->reported_underflow)
eff02e4f
ILT
2367 return 0;
2368
c926fd82 2369 hdr->dirs[i] = read_string (hdr_buf);
17112570 2370 if (hdr->dirs[i] == NULL)
eff02e4f 2371 return 0;
17112570 2372 ++i;
eff02e4f 2373 }
c926fd82 2374 if (!advance (hdr_buf, 1))
eff02e4f
ILT
2375 return 0;
2376
2377 /* Count the number of file entries. */
2378 hdr->filenames_count = 0;
c926fd82
ILT
2379 p = hdr_buf->buf;
2380 pend = p + hdr_buf->left;
eff02e4f
ILT
2381 while (p < pend && *p != '\0')
2382 {
2383 p += strnlen ((const char *) p, pend - p) + 1;
2384 p += leb128_len (p);
2385 p += leb128_len (p);
2386 p += leb128_len (p);
2387 ++hdr->filenames_count;
2388 }
2389
325e70b4
ILT
2390 /* The index of the first entry in the list of file names is 1. Index 0 is
2391 used for the DW_AT_name of the compilation unit. To simplify index
2392 handling, we set entry 0 to the compilation unit file name. */
2393 ++hdr->filenames_count;
eff02e4f
ILT
2394 hdr->filenames = ((const char **)
2395 backtrace_alloc (state,
2396 hdr->filenames_count * sizeof (char *),
c926fd82
ILT
2397 hdr_buf->error_callback,
2398 hdr_buf->data));
eff02e4f
ILT
2399 if (hdr->filenames == NULL)
2400 return 0;
325e70b4
ILT
2401 hdr->filenames[0] = u->filename;
2402 i = 1;
c926fd82 2403 while (*hdr_buf->buf != '\0')
eff02e4f
ILT
2404 {
2405 const char *filename;
2406 uint64_t dir_index;
2407
c926fd82 2408 if (hdr_buf->reported_underflow)
eff02e4f
ILT
2409 return 0;
2410
c926fd82 2411 filename = read_string (hdr_buf);
17112570 2412 if (filename == NULL)
eff02e4f 2413 return 0;
c926fd82 2414 dir_index = read_uleb128 (hdr_buf);
1e678aed 2415 if (IS_ABSOLUTE_PATH (filename)
325e70b4 2416 || (dir_index < hdr->dirs_count && hdr->dirs[dir_index] == NULL))
eff02e4f
ILT
2417 hdr->filenames[i] = filename;
2418 else
2419 {
2420 const char *dir;
2421 size_t dir_len;
2422 size_t filename_len;
2423 char *s;
2424
325e70b4
ILT
2425 if (dir_index < hdr->dirs_count)
2426 dir = hdr->dirs[dir_index];
eff02e4f
ILT
2427 else
2428 {
c926fd82 2429 dwarf_buf_error (hdr_buf,
eff02e4f 2430 ("invalid directory index in "
df003d1e
ILT
2431 "line number program header"),
2432 0);
eff02e4f
ILT
2433 return 0;
2434 }
2435 dir_len = strlen (dir);
2436 filename_len = strlen (filename);
c926fd82
ILT
2437 s = ((char *) backtrace_alloc (state, dir_len + filename_len + 2,
2438 hdr_buf->error_callback,
2439 hdr_buf->data));
eff02e4f
ILT
2440 if (s == NULL)
2441 return 0;
2442 memcpy (s, dir, dir_len);
2443 /* FIXME: If we are on a DOS-based file system, and the
2444 directory or the file name use backslashes, then we
2445 should use a backslash here. */
2446 s[dir_len] = '/';
2447 memcpy (s + dir_len + 1, filename, filename_len + 1);
2448 hdr->filenames[i] = s;
2449 }
2450
2451 /* Ignore the modification time and size. */
c926fd82
ILT
2452 read_uleb128 (hdr_buf);
2453 read_uleb128 (hdr_buf);
eff02e4f
ILT
2454
2455 ++i;
2456 }
2457
c926fd82
ILT
2458 return 1;
2459}
2460
2461/* Read a single version 5 LNCT entry for a directory or file name in a
2462 line header. Sets *STRING to the resulting name, ignoring other
2463 data. Return 1 on success, 0 on failure. */
2464
2465static int
2466read_lnct (struct backtrace_state *state, struct dwarf_data *ddata,
2467 struct unit *u, struct dwarf_buf *hdr_buf,
2468 const struct line_header *hdr, size_t formats_count,
2469 const struct line_header_format *formats, const char **string)
2470{
2471 size_t i;
2472 const char *dir;
2473 const char *path;
2474
2475 dir = NULL;
2476 path = NULL;
2477 for (i = 0; i < formats_count; i++)
2478 {
2479 struct attr_val val;
2480
2481 if (!read_attribute (formats[i].form, 0, hdr_buf, u->is_dwarf64,
2482 u->version, hdr->addrsize, &ddata->dwarf_sections,
2483 ddata->altlink, &val))
2484 return 0;
2485 switch (formats[i].lnct)
2486 {
2487 case DW_LNCT_path:
2488 if (!resolve_string (&ddata->dwarf_sections, u->is_dwarf64,
2489 ddata->is_bigendian, u->str_offsets_base,
2490 &val, hdr_buf->error_callback, hdr_buf->data,
2491 &path))
2492 return 0;
2493 break;
2494 case DW_LNCT_directory_index:
2495 if (val.encoding == ATTR_VAL_UINT)
2496 {
2497 if (val.u.uint >= hdr->dirs_count)
2498 {
2499 dwarf_buf_error (hdr_buf,
2500 ("invalid directory index in "
df003d1e
ILT
2501 "line number program header"),
2502 0);
c926fd82
ILT
2503 return 0;
2504 }
2505 dir = hdr->dirs[val.u.uint];
2506 }
2507 break;
2508 default:
2509 /* We don't care about timestamps or sizes or hashes. */
2510 break;
2511 }
2512 }
2513
2514 if (path == NULL)
2515 {
2516 dwarf_buf_error (hdr_buf,
df003d1e
ILT
2517 "missing file name in line number program header",
2518 0);
c926fd82
ILT
2519 return 0;
2520 }
2521
2522 if (dir == NULL)
2523 *string = path;
2524 else
2525 {
2526 size_t dir_len;
2527 size_t path_len;
2528 char *s;
2529
2530 dir_len = strlen (dir);
2531 path_len = strlen (path);
2532 s = (char *) backtrace_alloc (state, dir_len + path_len + 2,
2533 hdr_buf->error_callback, hdr_buf->data);
2534 if (s == NULL)
2535 return 0;
2536 memcpy (s, dir, dir_len);
2537 /* FIXME: If we are on a DOS-based file system, and the
2538 directory or the path name use backslashes, then we should
2539 use a backslash here. */
2540 s[dir_len] = '/';
2541 memcpy (s + dir_len + 1, path, path_len + 1);
2542 *string = s;
2543 }
2544
2545 return 1;
2546}
2547
2548/* Read a set of DWARF 5 line header format entries, setting *PCOUNT
2549 and *PPATHS. Return 1 on success, 0 on failure. */
2550
2551static int
2552read_line_header_format_entries (struct backtrace_state *state,
2553 struct dwarf_data *ddata,
2554 struct unit *u,
2555 struct dwarf_buf *hdr_buf,
2556 struct line_header *hdr,
2557 size_t *pcount,
2558 const char ***ppaths)
2559{
2560 size_t formats_count;
2561 struct line_header_format *formats;
2562 size_t paths_count;
2563 const char **paths;
2564 size_t i;
2565 int ret;
2566
2567 formats_count = read_byte (hdr_buf);
2568 if (formats_count == 0)
2569 formats = NULL;
2570 else
2571 {
2572 formats = ((struct line_header_format *)
2573 backtrace_alloc (state,
2574 (formats_count
2575 * sizeof (struct line_header_format)),
2576 hdr_buf->error_callback,
2577 hdr_buf->data));
2578 if (formats == NULL)
2579 return 0;
2580
2581 for (i = 0; i < formats_count; i++)
2582 {
2583 formats[i].lnct = (int) read_uleb128(hdr_buf);
2584 formats[i].form = (enum dwarf_form) read_uleb128 (hdr_buf);
2585 }
2586 }
2587
2588 paths_count = read_uleb128 (hdr_buf);
2589 if (paths_count == 0)
2590 {
2591 *pcount = 0;
2592 *ppaths = NULL;
2593 ret = 1;
2594 goto exit;
2595 }
2596
2597 paths = ((const char **)
2598 backtrace_alloc (state, paths_count * sizeof (const char *),
2599 hdr_buf->error_callback, hdr_buf->data));
2600 if (paths == NULL)
2601 {
2602 ret = 0;
2603 goto exit;
2604 }
2605 for (i = 0; i < paths_count; i++)
2606 {
2607 if (!read_lnct (state, ddata, u, hdr_buf, hdr, formats_count,
2608 formats, &paths[i]))
2609 {
2610 backtrace_free (state, paths,
2611 paths_count * sizeof (const char *),
2612 hdr_buf->error_callback, hdr_buf->data);
2613 ret = 0;
2614 goto exit;
2615 }
2616 }
2617
2618 *pcount = paths_count;
2619 *ppaths = paths;
2620
2621 ret = 1;
2622
2623 exit:
2624 if (formats != NULL)
2625 backtrace_free (state, formats,
2626 formats_count * sizeof (struct line_header_format),
2627 hdr_buf->error_callback, hdr_buf->data);
2628
2629 return ret;
2630}
2631
2632/* Read the line header. Return 1 on success, 0 on failure. */
2633
2634static int
2635read_line_header (struct backtrace_state *state, struct dwarf_data *ddata,
2636 struct unit *u, int is_dwarf64, struct dwarf_buf *line_buf,
2637 struct line_header *hdr)
2638{
2639 uint64_t hdrlen;
2640 struct dwarf_buf hdr_buf;
2641
2642 hdr->version = read_uint16 (line_buf);
2643 if (hdr->version < 2 || hdr->version > 5)
2644 {
df003d1e 2645 dwarf_buf_error (line_buf, "unsupported line number version", -1);
c926fd82
ILT
2646 return 0;
2647 }
2648
2649 if (hdr->version < 5)
2650 hdr->addrsize = u->addrsize;
2651 else
2652 {
2653 hdr->addrsize = read_byte (line_buf);
2654 /* We could support a non-zero segment_selector_size but I doubt
2655 we'll ever see it. */
2656 if (read_byte (line_buf) != 0)
2657 {
2658 dwarf_buf_error (line_buf,
df003d1e
ILT
2659 "non-zero segment_selector_size not supported",
2660 -1);
c926fd82
ILT
2661 return 0;
2662 }
2663 }
2664
2665 hdrlen = read_offset (line_buf, is_dwarf64);
2666
2667 hdr_buf = *line_buf;
2668 hdr_buf.left = hdrlen;
2669
2670 if (!advance (line_buf, hdrlen))
2671 return 0;
2672
2673 hdr->min_insn_len = read_byte (&hdr_buf);
2674 if (hdr->version < 4)
2675 hdr->max_ops_per_insn = 1;
2676 else
2677 hdr->max_ops_per_insn = read_byte (&hdr_buf);
2678
2679 /* We don't care about default_is_stmt. */
2680 read_byte (&hdr_buf);
2681
2682 hdr->line_base = read_sbyte (&hdr_buf);
2683 hdr->line_range = read_byte (&hdr_buf);
2684
2685 hdr->opcode_base = read_byte (&hdr_buf);
2686 hdr->opcode_lengths = hdr_buf.buf;
2687 if (!advance (&hdr_buf, hdr->opcode_base - 1))
2688 return 0;
2689
2690 if (hdr->version < 5)
2691 {
2692 if (!read_v2_paths (state, u, &hdr_buf, hdr))
2693 return 0;
2694 }
2695 else
2696 {
2697 if (!read_line_header_format_entries (state, ddata, u, &hdr_buf, hdr,
2698 &hdr->dirs_count,
2699 &hdr->dirs))
2700 return 0;
2701 if (!read_line_header_format_entries (state, ddata, u, &hdr_buf, hdr,
2702 &hdr->filenames_count,
2703 &hdr->filenames))
2704 return 0;
2705 }
2706
eff02e4f
ILT
2707 if (hdr_buf.reported_underflow)
2708 return 0;
2709
2710 return 1;
2711}
2712
2713/* Read the line program, adding line mappings to VEC. Return 1 on
2714 success, 0 on failure. */
2715
2716static int
e561a992 2717read_line_program (struct backtrace_state *state, struct dwarf_data *ddata,
325e70b4
ILT
2718 const struct line_header *hdr, struct dwarf_buf *line_buf,
2719 struct line_vector *vec)
eff02e4f
ILT
2720{
2721 uint64_t address;
2722 unsigned int op_index;
2723 const char *reset_filename;
2724 const char *filename;
2725 int lineno;
2726
2727 address = 0;
2728 op_index = 0;
325e70b4
ILT
2729 if (hdr->filenames_count > 1)
2730 reset_filename = hdr->filenames[1];
eff02e4f
ILT
2731 else
2732 reset_filename = "";
2733 filename = reset_filename;
2734 lineno = 1;
2735 while (line_buf->left > 0)
2736 {
2737 unsigned int op;
2738
2739 op = read_byte (line_buf);
2740 if (op >= hdr->opcode_base)
2741 {
2742 unsigned int advance;
2743
2744 /* Special opcode. */
2745 op -= hdr->opcode_base;
2746 advance = op / hdr->line_range;
2747 address += (hdr->min_insn_len * (op_index + advance)
2748 / hdr->max_ops_per_insn);
2749 op_index = (op_index + advance) % hdr->max_ops_per_insn;
2750 lineno += hdr->line_base + (int) (op % hdr->line_range);
e561a992
ILT
2751 add_line (state, ddata, address, filename, lineno,
2752 line_buf->error_callback, line_buf->data, vec);
eff02e4f
ILT
2753 }
2754 else if (op == DW_LNS_extended_op)
2755 {
2756 uint64_t len;
2757
2758 len = read_uleb128 (line_buf);
2759 op = read_byte (line_buf);
2760 switch (op)
2761 {
2762 case DW_LNE_end_sequence:
2763 /* FIXME: Should we mark the high PC here? It seems
2764 that we already have that information from the
2765 compilation unit. */
2766 address = 0;
2767 op_index = 0;
2768 filename = reset_filename;
2769 lineno = 1;
2770 break;
2771 case DW_LNE_set_address:
c926fd82 2772 address = read_address (line_buf, hdr->addrsize);
eff02e4f
ILT
2773 break;
2774 case DW_LNE_define_file:
2775 {
2776 const char *f;
2777 unsigned int dir_index;
2778
17112570
TV
2779 f = read_string (line_buf);
2780 if (f == NULL)
eff02e4f
ILT
2781 return 0;
2782 dir_index = read_uleb128 (line_buf);
2783 /* Ignore that time and length. */
2784 read_uleb128 (line_buf);
2785 read_uleb128 (line_buf);
2786 if (IS_ABSOLUTE_PATH (f))
2787 filename = f;
2788 else
2789 {
2790 const char *dir;
2791 size_t dir_len;
2792 size_t f_len;
2793 char *p;
2794
325e70b4
ILT
2795 if (dir_index < hdr->dirs_count)
2796 dir = hdr->dirs[dir_index];
eff02e4f
ILT
2797 else
2798 {
2799 dwarf_buf_error (line_buf,
2800 ("invalid directory index "
df003d1e
ILT
2801 "in line number program"),
2802 0);
eff02e4f
ILT
2803 return 0;
2804 }
2805 dir_len = strlen (dir);
2806 f_len = strlen (f);
2807 p = ((char *)
2808 backtrace_alloc (state, dir_len + f_len + 2,
2809 line_buf->error_callback,
2810 line_buf->data));
2811 if (p == NULL)
2812 return 0;
2813 memcpy (p, dir, dir_len);
2814 /* FIXME: If we are on a DOS-based file system,
2815 and the directory or the file name use
2816 backslashes, then we should use a backslash
2817 here. */
2818 p[dir_len] = '/';
2819 memcpy (p + dir_len + 1, f, f_len + 1);
2820 filename = p;
2821 }
2822 }
2823 break;
2824 case DW_LNE_set_discriminator:
2825 /* We don't care about discriminators. */
2826 read_uleb128 (line_buf);
2827 break;
2828 default:
2829 if (!advance (line_buf, len - 1))
2830 return 0;
2831 break;
2832 }
2833 }
2834 else
2835 {
2836 switch (op)
2837 {
2838 case DW_LNS_copy:
e561a992 2839 add_line (state, ddata, address, filename, lineno,
eff02e4f
ILT
2840 line_buf->error_callback, line_buf->data, vec);
2841 break;
2842 case DW_LNS_advance_pc:
2843 {
2844 uint64_t advance;
2845
2846 advance = read_uleb128 (line_buf);
2847 address += (hdr->min_insn_len * (op_index + advance)
2848 / hdr->max_ops_per_insn);
2849 op_index = (op_index + advance) % hdr->max_ops_per_insn;
2850 }
2851 break;
2852 case DW_LNS_advance_line:
2853 lineno += (int) read_sleb128 (line_buf);
2854 break;
2855 case DW_LNS_set_file:
2856 {
2857 uint64_t fileno;
2858
2859 fileno = read_uleb128 (line_buf);
9b2084db 2860 if (fileno >= hdr->filenames_count)
eff02e4f 2861 {
9b2084db
ILT
2862 dwarf_buf_error (line_buf,
2863 ("invalid file number in "
2864 "line number program"),
2865 0);
2866 return 0;
eff02e4f 2867 }
9b2084db 2868 filename = hdr->filenames[fileno];
eff02e4f
ILT
2869 }
2870 break;
2871 case DW_LNS_set_column:
2872 read_uleb128 (line_buf);
2873 break;
2874 case DW_LNS_negate_stmt:
2875 break;
2876 case DW_LNS_set_basic_block:
2877 break;
2878 case DW_LNS_const_add_pc:
2879 {
2880 unsigned int advance;
2881
2882 op = 255 - hdr->opcode_base;
2883 advance = op / hdr->line_range;
2884 address += (hdr->min_insn_len * (op_index + advance)
2885 / hdr->max_ops_per_insn);
2886 op_index = (op_index + advance) % hdr->max_ops_per_insn;
2887 }
2888 break;
2889 case DW_LNS_fixed_advance_pc:
2890 address += read_uint16 (line_buf);
2891 op_index = 0;
2892 break;
2893 case DW_LNS_set_prologue_end:
2894 break;
2895 case DW_LNS_set_epilogue_begin:
2896 break;
2897 case DW_LNS_set_isa:
2898 read_uleb128 (line_buf);
2899 break;
2900 default:
2901 {
2902 unsigned int i;
2903
2904 for (i = hdr->opcode_lengths[op - 1]; i > 0; --i)
2905 read_uleb128 (line_buf);
2906 }
2907 break;
2908 }
2909 }
2910 }
2911
2912 return 1;
2913}
2914
2915/* Read the line number information for a compilation unit. Returns 1
2916 on success, 0 on failure. */
2917
2918static int
2919read_line_info (struct backtrace_state *state, struct dwarf_data *ddata,
2920 backtrace_error_callback error_callback, void *data,
2921 struct unit *u, struct line_header *hdr, struct line **lines,
2922 size_t *lines_count)
2923{
2924 struct line_vector vec;
2925 struct dwarf_buf line_buf;
2926 uint64_t len;
2927 int is_dwarf64;
2928 struct line *ln;
2929
2930 memset (&vec.vec, 0, sizeof vec.vec);
2931 vec.count = 0;
2932
2933 memset (hdr, 0, sizeof *hdr);
2934
2935 if (u->lineoff != (off_t) (size_t) u->lineoff
66ab5839 2936 || (size_t) u->lineoff >= ddata->dwarf_sections.size[DEBUG_LINE])
eff02e4f
ILT
2937 {
2938 error_callback (data, "unit line offset out of range", 0);
2939 goto fail;
2940 }
2941
2942 line_buf.name = ".debug_line";
66ab5839
ILT
2943 line_buf.start = ddata->dwarf_sections.data[DEBUG_LINE];
2944 line_buf.buf = ddata->dwarf_sections.data[DEBUG_LINE] + u->lineoff;
2945 line_buf.left = ddata->dwarf_sections.size[DEBUG_LINE] - u->lineoff;
eff02e4f
ILT
2946 line_buf.is_bigendian = ddata->is_bigendian;
2947 line_buf.error_callback = error_callback;
2948 line_buf.data = data;
2949 line_buf.reported_underflow = 0;
2950
292592c5 2951 len = read_initial_length (&line_buf, &is_dwarf64);
eff02e4f
ILT
2952 line_buf.left = len;
2953
c926fd82 2954 if (!read_line_header (state, ddata, u, is_dwarf64, &line_buf, hdr))
eff02e4f
ILT
2955 goto fail;
2956
325e70b4 2957 if (!read_line_program (state, ddata, hdr, &line_buf, &vec))
eff02e4f
ILT
2958 goto fail;
2959
2960 if (line_buf.reported_underflow)
2961 goto fail;
2962
2963 if (vec.count == 0)
2964 {
2965 /* This is not a failure in the sense of a generating an error,
2966 but it is a failure in that sense that we have no useful
2967 information. */
2968 goto fail;
2969 }
2970
2971 /* Allocate one extra entry at the end. */
2972 ln = ((struct line *)
2973 backtrace_vector_grow (state, sizeof (struct line), error_callback,
2974 data, &vec.vec));
2975 if (ln == NULL)
2976 goto fail;
2977 ln->pc = (uintptr_t) -1;
2978 ln->filename = NULL;
2979 ln->lineno = 0;
d99a7b4d 2980 ln->idx = 0;
eff02e4f
ILT
2981
2982 if (!backtrace_vector_release (state, &vec.vec, error_callback, data))
2983 goto fail;
2984
2985 ln = (struct line *) vec.vec.base;
c5604b48 2986 backtrace_qsort (ln, vec.count, sizeof (struct line), line_compare);
eff02e4f
ILT
2987
2988 *lines = ln;
2989 *lines_count = vec.count;
2990
2991 return 1;
2992
2993 fail:
518a3a21 2994 backtrace_vector_free (state, &vec.vec, error_callback, data);
eff02e4f
ILT
2995 free_line_header (state, hdr, error_callback, data);
2996 *lines = (struct line *) (uintptr_t) -1;
2997 *lines_count = 0;
2998 return 0;
2999}
3000
07e1534f
TV
3001static const char *read_referenced_name (struct dwarf_data *, struct unit *,
3002 uint64_t, backtrace_error_callback,
3003 void *);
3004
3005/* Read the name of a function from a DIE referenced by ATTR with VAL. */
3006
3007static const char *
3008read_referenced_name_from_attr (struct dwarf_data *ddata, struct unit *u,
3009 struct attr *attr, struct attr_val *val,
3010 backtrace_error_callback error_callback,
3011 void *data)
3012{
3013 switch (attr->name)
3014 {
3015 case DW_AT_abstract_origin:
3016 case DW_AT_specification:
3017 break;
3018 default:
3019 return NULL;
3020 }
3021
2bd0a246
TV
3022 if (attr->form == DW_FORM_ref_sig8)
3023 return NULL;
3024
3025 if (val->encoding == ATTR_VAL_REF_INFO)
07e1534f 3026 {
2bd0a246
TV
3027 struct unit *unit
3028 = find_unit (ddata->units, ddata->units_count,
3029 val->u.uint);
3030 if (unit == NULL)
3031 return NULL;
3032
3033 uint64_t offset = val->u.uint - unit->low_offset;
3034 return read_referenced_name (ddata, unit, offset, error_callback, data);
07e1534f
TV
3035 }
3036
3037 if (val->encoding == ATTR_VAL_UINT
3038 || val->encoding == ATTR_VAL_REF_UNIT)
3039 return read_referenced_name (ddata, u, val->u.uint, error_callback, data);
3040
1c2a9a37
TV
3041 if (val->encoding == ATTR_VAL_REF_ALT_INFO)
3042 {
3043 struct unit *alt_unit
3044 = find_unit (ddata->altlink->units, ddata->altlink->units_count,
3045 val->u.uint);
3046 if (alt_unit == NULL)
3047 return NULL;
3048
3049 uint64_t offset = val->u.uint - alt_unit->low_offset;
3050 return read_referenced_name (ddata->altlink, alt_unit, offset,
3051 error_callback, data);
3052 }
3053
07e1534f
TV
3054 return NULL;
3055}
3056
eff02e4f
ILT
3057/* Read the name of a function from a DIE referenced by a
3058 DW_AT_abstract_origin or DW_AT_specification tag. OFFSET is within
3059 the same compilation unit. */
3060
3061static const char *
3062read_referenced_name (struct dwarf_data *ddata, struct unit *u,
3063 uint64_t offset, backtrace_error_callback error_callback,
3064 void *data)
3065{
3066 struct dwarf_buf unit_buf;
3067 uint64_t code;
3068 const struct abbrev *abbrev;
3069 const char *ret;
3070 size_t i;
3071
3072 /* OFFSET is from the start of the data for this compilation unit.
3073 U->unit_data is the data, but it starts U->unit_data_offset bytes
3074 from the beginning. */
3075
3076 if (offset < u->unit_data_offset
3077 || offset - u->unit_data_offset >= u->unit_data_len)
3078 {
3079 error_callback (data,
3080 "abstract origin or specification out of range",
3081 0);
3082 return NULL;
3083 }
3084
3085 offset -= u->unit_data_offset;
3086
3087 unit_buf.name = ".debug_info";
66ab5839 3088 unit_buf.start = ddata->dwarf_sections.data[DEBUG_INFO];
eff02e4f
ILT
3089 unit_buf.buf = u->unit_data + offset;
3090 unit_buf.left = u->unit_data_len - offset;
3091 unit_buf.is_bigendian = ddata->is_bigendian;
3092 unit_buf.error_callback = error_callback;
3093 unit_buf.data = data;
3094 unit_buf.reported_underflow = 0;
3095
3096 code = read_uleb128 (&unit_buf);
3097 if (code == 0)
3098 {
df003d1e
ILT
3099 dwarf_buf_error (&unit_buf,
3100 "invalid abstract origin or specification",
3101 0);
eff02e4f
ILT
3102 return NULL;
3103 }
3104
3105 abbrev = lookup_abbrev (&u->abbrevs, code, error_callback, data);
3106 if (abbrev == NULL)
3107 return NULL;
3108
3109 ret = NULL;
3110 for (i = 0; i < abbrev->num_attrs; ++i)
3111 {
3112 struct attr_val val;
3113
c926fd82
ILT
3114 if (!read_attribute (abbrev->attrs[i].form, abbrev->attrs[i].val,
3115 &unit_buf, u->is_dwarf64, u->version, u->addrsize,
3116 &ddata->dwarf_sections, ddata->altlink, &val))
eff02e4f
ILT
3117 return NULL;
3118
3119 switch (abbrev->attrs[i].name)
3120 {
3121 case DW_AT_name:
df1de064
TV
3122 /* Third name preference: don't override. A name we found in some
3123 other way, will normally be more useful -- e.g., this name is
3124 normally not mangled. */
3125 if (ret != NULL)
3126 break;
c926fd82
ILT
3127 if (!resolve_string (&ddata->dwarf_sections, u->is_dwarf64,
3128 ddata->is_bigendian, u->str_offsets_base,
3129 &val, error_callback, data, &ret))
3130 return NULL;
eff02e4f
ILT
3131 break;
3132
3133 case DW_AT_linkage_name:
3134 case DW_AT_MIPS_linkage_name:
df1de064 3135 /* First name preference: override all. */
c926fd82
ILT
3136 {
3137 const char *s;
3138
3139 s = NULL;
3140 if (!resolve_string (&ddata->dwarf_sections, u->is_dwarf64,
3141 ddata->is_bigendian, u->str_offsets_base,
3142 &val, error_callback, data, &s))
3143 return NULL;
3144 if (s != NULL)
3145 return s;
3146 }
eff02e4f
ILT
3147 break;
3148
3149 case DW_AT_specification:
df1de064
TV
3150 /* Second name preference: override DW_AT_name, don't override
3151 DW_AT_linkage_name. */
07e1534f
TV
3152 {
3153 const char *name;
eff02e4f 3154
07e1534f
TV
3155 name = read_referenced_name_from_attr (ddata, u, &abbrev->attrs[i],
3156 &val, error_callback, data);
3157 if (name != NULL)
3158 ret = name;
3159 }
eff02e4f
ILT
3160 break;
3161
3162 default:
3163 break;
3164 }
3165 }
3166
3167 return ret;
3168}
3169
4b3fc188
ILT
3170/* Add a range to a unit that maps to a function. This is called via
3171 add_ranges. Returns 1 on success, 0 on error. */
eff02e4f
ILT
3172
3173static int
4b3fc188
ILT
3174add_function_range (struct backtrace_state *state, void *rdata,
3175 uint64_t lowpc, uint64_t highpc,
3176 backtrace_error_callback error_callback, void *data,
3177 void *pvec)
eff02e4f 3178{
4b3fc188
ILT
3179 struct function *function = (struct function *) rdata;
3180 struct function_vector *vec = (struct function_vector *) pvec;
eff02e4f
ILT
3181 struct function_addrs *p;
3182
3183 if (vec->count > 0)
3184 {
4b3fc188 3185 p = (struct function_addrs *) vec->vec.base + (vec->count - 1);
eff02e4f
ILT
3186 if ((lowpc == p->high || lowpc == p->high + 1)
3187 && function == p->function)
3188 {
3189 if (highpc > p->high)
3190 p->high = highpc;
3191 return 1;
3192 }
3193 }
3194
3195 p = ((struct function_addrs *)
3196 backtrace_vector_grow (state, sizeof (struct function_addrs),
3197 error_callback, data, &vec->vec));
3198 if (p == NULL)
3199 return 0;
3200
3201 p->low = lowpc;
3202 p->high = highpc;
3203 p->function = function;
eff02e4f 3204
4b3fc188 3205 ++vec->count;
eff02e4f
ILT
3206
3207 return 1;
3208}
3209
3210/* Read one entry plus all its children. Add function addresses to
3211 VEC. Returns 1 on success, 0 on error. */
3212
3213static int
3214read_function_entry (struct backtrace_state *state, struct dwarf_data *ddata,
3215 struct unit *u, uint64_t base, struct dwarf_buf *unit_buf,
3216 const struct line_header *lhdr,
3217 backtrace_error_callback error_callback, void *data,
d96fa208
ILT
3218 struct function_vector *vec_function,
3219 struct function_vector *vec_inlined)
eff02e4f
ILT
3220{
3221 while (unit_buf->left > 0)
3222 {
3223 uint64_t code;
3224 const struct abbrev *abbrev;
3225 int is_function;
3226 struct function *function;
d96fa208 3227 struct function_vector *vec;
eff02e4f 3228 size_t i;
4b3fc188 3229 struct pcrange pcrange;
df1de064 3230 int have_linkage_name;
eff02e4f
ILT
3231
3232 code = read_uleb128 (unit_buf);
3233 if (code == 0)
3234 return 1;
3235
3236 abbrev = lookup_abbrev (&u->abbrevs, code, error_callback, data);
3237 if (abbrev == NULL)
3238 return 0;
3239
3240 is_function = (abbrev->tag == DW_TAG_subprogram
3241 || abbrev->tag == DW_TAG_entry_point
3242 || abbrev->tag == DW_TAG_inlined_subroutine);
3243
d96fa208
ILT
3244 if (abbrev->tag == DW_TAG_inlined_subroutine)
3245 vec = vec_inlined;
3246 else
3247 vec = vec_function;
3248
eff02e4f
ILT
3249 function = NULL;
3250 if (is_function)
3251 {
3252 function = ((struct function *)
3253 backtrace_alloc (state, sizeof *function,
3254 error_callback, data));
3255 if (function == NULL)
3256 return 0;
3257 memset (function, 0, sizeof *function);
3258 }
3259
4b3fc188 3260 memset (&pcrange, 0, sizeof pcrange);
df1de064 3261 have_linkage_name = 0;
eff02e4f
ILT
3262 for (i = 0; i < abbrev->num_attrs; ++i)
3263 {
3264 struct attr_val val;
3265
c926fd82
ILT
3266 if (!read_attribute (abbrev->attrs[i].form, abbrev->attrs[i].val,
3267 unit_buf, u->is_dwarf64, u->version,
3268 u->addrsize, &ddata->dwarf_sections,
944f59ff 3269 ddata->altlink, &val))
eff02e4f
ILT
3270 return 0;
3271
3272 /* The compile unit sets the base address for any address
3273 ranges in the function entries. */
3274 if (abbrev->tag == DW_TAG_compile_unit
c926fd82
ILT
3275 && abbrev->attrs[i].name == DW_AT_low_pc)
3276 {
3277 if (val.encoding == ATTR_VAL_ADDRESS)
3278 base = val.u.uint;
3279 else if (val.encoding == ATTR_VAL_ADDRESS_INDEX)
3280 {
3281 if (!resolve_addr_index (&ddata->dwarf_sections,
3282 u->addr_base, u->addrsize,
3283 ddata->is_bigendian, val.u.uint,
3284 error_callback, data, &base))
3285 return 0;
3286 }
3287 }
eff02e4f
ILT
3288
3289 if (is_function)
3290 {
3291 switch (abbrev->attrs[i].name)
3292 {
3293 case DW_AT_call_file:
3294 if (val.encoding == ATTR_VAL_UINT)
3295 {
9b2084db 3296 if (val.u.uint >= lhdr->filenames_count)
eff02e4f 3297 {
9b2084db
ILT
3298 dwarf_buf_error (unit_buf,
3299 ("invalid file number in "
3300 "DW_AT_call_file attribute"),
3301 0);
3302 return 0;
eff02e4f 3303 }
9b2084db 3304 function->caller_filename = lhdr->filenames[val.u.uint];
eff02e4f
ILT
3305 }
3306 break;
3307
3308 case DW_AT_call_line:
3309 if (val.encoding == ATTR_VAL_UINT)
3310 function->caller_lineno = val.u.uint;
3311 break;
3312
3313 case DW_AT_abstract_origin:
3314 case DW_AT_specification:
df1de064
TV
3315 /* Second name preference: override DW_AT_name, don't override
3316 DW_AT_linkage_name. */
3317 if (have_linkage_name)
3318 break;
07e1534f
TV
3319 {
3320 const char *name;
3321
3322 name
3323 = read_referenced_name_from_attr (ddata, u,
3324 &abbrev->attrs[i], &val,
3325 error_callback, data);
3326 if (name != NULL)
3327 function->name = name;
3328 }
eff02e4f
ILT
3329 break;
3330
3331 case DW_AT_name:
df1de064
TV
3332 /* Third name preference: don't override. */
3333 if (function->name != NULL)
3334 break;
c926fd82
ILT
3335 if (!resolve_string (&ddata->dwarf_sections, u->is_dwarf64,
3336 ddata->is_bigendian,
3337 u->str_offsets_base, &val,
3338 error_callback, data, &function->name))
3339 return 0;
eff02e4f
ILT
3340 break;
3341
3342 case DW_AT_linkage_name:
3343 case DW_AT_MIPS_linkage_name:
df1de064 3344 /* First name preference: override all. */
c926fd82
ILT
3345 {
3346 const char *s;
3347
3348 s = NULL;
3349 if (!resolve_string (&ddata->dwarf_sections, u->is_dwarf64,
3350 ddata->is_bigendian,
3351 u->str_offsets_base, &val,
3352 error_callback, data, &s))
3353 return 0;
3354 if (s != NULL)
3355 {
3356 function->name = s;
3357 have_linkage_name = 1;
3358 }
3359 }
eff02e4f
ILT
3360 break;
3361
4b3fc188
ILT
3362 case DW_AT_low_pc: case DW_AT_high_pc: case DW_AT_ranges:
3363 update_pcrange (&abbrev->attrs[i], &val, &pcrange);
eff02e4f
ILT
3364 break;
3365
3366 default:
3367 break;
3368 }
3369 }
3370 }
3371
3372 /* If we couldn't find a name for the function, we have no use
3373 for it. */
3374 if (is_function && function->name == NULL)
3375 {
3376 backtrace_free (state, function, sizeof *function,
3377 error_callback, data);
3378 is_function = 0;
3379 }
3380
3381 if (is_function)
3382 {
4b3fc188
ILT
3383 if (pcrange.have_ranges
3384 || (pcrange.have_lowpc && pcrange.have_highpc))
eff02e4f 3385 {
4b3fc188
ILT
3386 if (!add_ranges (state, &ddata->dwarf_sections,
3387 ddata->base_address, ddata->is_bigendian,
3388 u, base, &pcrange, add_function_range,
3389 (void *) function, error_callback, data,
3390 (void *) vec))
eff02e4f
ILT
3391 return 0;
3392 }
3393 else
3394 {
3395 backtrace_free (state, function, sizeof *function,
3396 error_callback, data);
3397 is_function = 0;
3398 }
3399 }
3400
3401 if (abbrev->has_children)
3402 {
3403 if (!is_function)
3404 {
3405 if (!read_function_entry (state, ddata, u, base, unit_buf, lhdr,
d96fa208
ILT
3406 error_callback, data, vec_function,
3407 vec_inlined))
eff02e4f
ILT
3408 return 0;
3409 }
3410 else
3411 {
3412 struct function_vector fvec;
3413
3414 /* Gather any information for inlined functions in
3415 FVEC. */
3416
3417 memset (&fvec, 0, sizeof fvec);
3418
3419 if (!read_function_entry (state, ddata, u, base, unit_buf, lhdr,
d96fa208
ILT
3420 error_callback, data, vec_function,
3421 &fvec))
eff02e4f
ILT
3422 return 0;
3423
3424 if (fvec.count > 0)
3425 {
0755f573 3426 struct function_addrs *p;
eff02e4f
ILT
3427 struct function_addrs *faddrs;
3428
0755f573
ILT
3429 /* Allocate a trailing entry, but don't include it
3430 in fvec.count. */
3431 p = ((struct function_addrs *)
3432 backtrace_vector_grow (state,
3433 sizeof (struct function_addrs),
3434 error_callback, data,
3435 &fvec.vec));
3436 if (p == NULL)
3437 return 0;
3438 p->low = 0;
3439 --p->low;
3440 p->high = p->low;
3441 p->function = NULL;
3442
eff02e4f
ILT
3443 if (!backtrace_vector_release (state, &fvec.vec,
3444 error_callback, data))
3445 return 0;
3446
3447 faddrs = (struct function_addrs *) fvec.vec.base;
c5604b48
ILT
3448 backtrace_qsort (faddrs, fvec.count,
3449 sizeof (struct function_addrs),
3450 function_addrs_compare);
eff02e4f
ILT
3451
3452 function->function_addrs = faddrs;
3453 function->function_addrs_count = fvec.count;
3454 }
3455 }
3456 }
3457 }
3458
3459 return 1;
3460}
3461
3462/* Read function name information for a compilation unit. We look
3463 through the whole unit looking for function tags. */
3464
3465static void
3466read_function_info (struct backtrace_state *state, struct dwarf_data *ddata,
3467 const struct line_header *lhdr,
3468 backtrace_error_callback error_callback, void *data,
3469 struct unit *u, struct function_vector *fvec,
3470 struct function_addrs **ret_addrs,
3471 size_t *ret_addrs_count)
3472{
38811401
ILT
3473 struct function_vector lvec;
3474 struct function_vector *pfvec;
eff02e4f 3475 struct dwarf_buf unit_buf;
0755f573 3476 struct function_addrs *p;
eff02e4f
ILT
3477 struct function_addrs *addrs;
3478 size_t addrs_count;
3479
38811401
ILT
3480 /* Use FVEC if it is not NULL. Otherwise use our own vector. */
3481 if (fvec != NULL)
3482 pfvec = fvec;
3483 else
3484 {
3485 memset (&lvec, 0, sizeof lvec);
3486 pfvec = &lvec;
3487 }
3488
eff02e4f 3489 unit_buf.name = ".debug_info";
66ab5839 3490 unit_buf.start = ddata->dwarf_sections.data[DEBUG_INFO];
eff02e4f
ILT
3491 unit_buf.buf = u->unit_data;
3492 unit_buf.left = u->unit_data_len;
3493 unit_buf.is_bigendian = ddata->is_bigendian;
3494 unit_buf.error_callback = error_callback;
3495 unit_buf.data = data;
3496 unit_buf.reported_underflow = 0;
3497
3498 while (unit_buf.left > 0)
3499 {
3500 if (!read_function_entry (state, ddata, u, 0, &unit_buf, lhdr,
d96fa208 3501 error_callback, data, pfvec, pfvec))
eff02e4f
ILT
3502 return;
3503 }
3504
38811401 3505 if (pfvec->count == 0)
eff02e4f
ILT
3506 return;
3507
0755f573
ILT
3508 /* Allocate a trailing entry, but don't include it in
3509 pfvec->count. */
3510 p = ((struct function_addrs *)
3511 backtrace_vector_grow (state, sizeof (struct function_addrs),
3512 error_callback, data, &pfvec->vec));
3513 if (p == NULL)
3514 return;
3515 p->low = 0;
3516 --p->low;
3517 p->high = p->low;
3518 p->function = NULL;
3519
38811401 3520 addrs_count = pfvec->count;
eff02e4f 3521
38811401
ILT
3522 if (fvec == NULL)
3523 {
3524 if (!backtrace_vector_release (state, &lvec.vec, error_callback, data))
3525 return;
bfd74f22 3526 addrs = (struct function_addrs *) pfvec->vec.base;
38811401
ILT
3527 }
3528 else
3529 {
3530 /* Finish this list of addresses, but leave the remaining space in
3531 the vector available for the next function unit. */
bfd74f22
ILT
3532 addrs = ((struct function_addrs *)
3533 backtrace_vector_finish (state, &fvec->vec,
3534 error_callback, data));
3535 if (addrs == NULL)
3536 return;
38811401
ILT
3537 fvec->count = 0;
3538 }
eff02e4f 3539
c5604b48
ILT
3540 backtrace_qsort (addrs, addrs_count, sizeof (struct function_addrs),
3541 function_addrs_compare);
eff02e4f
ILT
3542
3543 *ret_addrs = addrs;
3544 *ret_addrs_count = addrs_count;
3545}
3546
3547/* See if PC is inlined in FUNCTION. If it is, print out the inlined
3548 information, and update FILENAME and LINENO for the caller.
3549 Returns whatever CALLBACK returns, or 0 to keep going. */
3550
3551static int
3552report_inlined_functions (uintptr_t pc, struct function *function,
3553 backtrace_full_callback callback, void *data,
3554 const char **filename, int *lineno)
3555{
0755f573
ILT
3556 struct function_addrs *p;
3557 struct function_addrs *match;
eff02e4f
ILT
3558 struct function *inlined;
3559 int ret;
3560
3561 if (function->function_addrs_count == 0)
3562 return 0;
3563
e41e66b7
ILT
3564 /* Our search isn't safe if pc == -1, as that is the sentinel
3565 value. */
3566 if (pc + 1 == 0)
3567 return 0;
3568
0755f573
ILT
3569 p = ((struct function_addrs *)
3570 bsearch (&pc, function->function_addrs,
3571 function->function_addrs_count,
3572 sizeof (struct function_addrs),
3573 function_addrs_search));
3574 if (p == NULL)
eff02e4f
ILT
3575 return 0;
3576
0755f573 3577 /* Here pc >= p->low && pc < (p + 1)->low. The function_addrs are
e41e66b7
ILT
3578 sorted by low, so if pc > p->low we are at the end of a range of
3579 function_addrs with the same low value. If pc == p->low walk
3580 forward to the end of the range with that low value. Then walk
3581 backward and use the first range that includes pc. */
3582 while (pc == (p + 1)->low)
3583 ++p;
0755f573
ILT
3584 match = NULL;
3585 while (1)
3586 {
3587 if (pc < p->high)
3588 {
3589 match = p;
3590 break;
3591 }
3592 if (p == function->function_addrs)
3593 break;
3594 if ((p - 1)->low < p->low)
3595 break;
3596 --p;
3597 }
3598 if (match == NULL)
3599 return 0;
eff02e4f
ILT
3600
3601 /* We found an inlined call. */
3602
0755f573 3603 inlined = match->function;
eff02e4f
ILT
3604
3605 /* Report any calls inlined into this one. */
3606 ret = report_inlined_functions (pc, inlined, callback, data,
3607 filename, lineno);
3608 if (ret != 0)
3609 return ret;
3610
3611 /* Report this inlined call. */
3612 ret = callback (data, pc, *filename, *lineno, inlined->name);
3613 if (ret != 0)
3614 return ret;
3615
3616 /* Our caller will report the caller of the inlined function; tell
3617 it the appropriate filename and line number. */
3618 *filename = inlined->caller_filename;
3619 *lineno = inlined->caller_lineno;
3620
3621 return 0;
3622}
3623
e561a992
ILT
3624/* Look for a PC in the DWARF mapping for one module. On success,
3625 call CALLBACK and return whatever it returns. On error, call
3626 ERROR_CALLBACK and return 0. Sets *FOUND to 1 if the PC is found,
3627 0 if not. */
eff02e4f
ILT
3628
3629static int
e561a992
ILT
3630dwarf_lookup_pc (struct backtrace_state *state, struct dwarf_data *ddata,
3631 uintptr_t pc, backtrace_full_callback callback,
3632 backtrace_error_callback error_callback, void *data,
3633 int *found)
eff02e4f 3634{
eff02e4f 3635 struct unit_addrs *entry;
0755f573 3636 int found_entry;
eff02e4f
ILT
3637 struct unit *u;
3638 int new_data;
3639 struct line *lines;
3640 struct line *ln;
0755f573
ILT
3641 struct function_addrs *p;
3642 struct function_addrs *fmatch;
eff02e4f
ILT
3643 struct function *function;
3644 const char *filename;
3645 int lineno;
3646 int ret;
3647
e561a992 3648 *found = 1;
eff02e4f 3649
e41e66b7
ILT
3650 /* Find an address range that includes PC. Our search isn't safe if
3651 PC == -1, as we use that as a sentinel value, so skip the search
3652 in that case. */
3653 entry = (ddata->addrs_count == 0 || pc + 1 == 0
68641fb7
TV
3654 ? NULL
3655 : bsearch (&pc, ddata->addrs, ddata->addrs_count,
3656 sizeof (struct unit_addrs), unit_addrs_search));
eff02e4f
ILT
3657
3658 if (entry == NULL)
e561a992
ILT
3659 {
3660 *found = 0;
3661 return 0;
3662 }
eff02e4f 3663
0755f573 3664 /* Here pc >= entry->low && pc < (entry + 1)->low. The unit_addrs
e41e66b7
ILT
3665 are sorted by low, so if pc > p->low we are at the end of a range
3666 of unit_addrs with the same low value. If pc == p->low walk
3667 forward to the end of the range with that low value. Then walk
3668 backward and use the first range that includes pc. */
3669 while (pc == (entry + 1)->low)
3670 ++entry;
0755f573
ILT
3671 found_entry = 0;
3672 while (1)
3673 {
3674 if (pc < entry->high)
3675 {
3676 found_entry = 1;
3677 break;
3678 }
3679 if (entry == ddata->addrs)
3680 break;
3681 if ((entry - 1)->low < entry->low)
3682 break;
3683 --entry;
3684 }
3685 if (!found_entry)
3686 {
3687 *found = 0;
3688 return 0;
3689 }
eff02e4f
ILT
3690
3691 /* We need the lines, lines_count, function_addrs,
3692 function_addrs_count fields of u. If they are not set, we need
3693 to set them. When running in threaded mode, we need to allow for
3694 the possibility that some other thread is setting them
3695 simultaneously. */
3696
3697 u = entry->u;
3698 lines = u->lines;
3699
3700 /* Skip units with no useful line number information by walking
3701 backward. Useless line number information is marked by setting
3702 lines == -1. */
3703 while (entry > ddata->addrs
3704 && pc >= (entry - 1)->low
3705 && pc < (entry - 1)->high)
3706 {
3707 if (state->threaded)
49579c7e 3708 lines = (struct line *) backtrace_atomic_load_pointer (&u->lines);
eff02e4f
ILT
3709
3710 if (lines != (struct line *) (uintptr_t) -1)
3711 break;
3712
3713 --entry;
3714
3715 u = entry->u;
3716 lines = u->lines;
3717 }
3718
eff02e4f 3719 if (state->threaded)
49579c7e 3720 lines = backtrace_atomic_load_pointer (&u->lines);
eff02e4f
ILT
3721
3722 new_data = 0;
3723 if (lines == NULL)
3724 {
0755f573 3725 struct function_addrs *function_addrs;
eff02e4f
ILT
3726 size_t function_addrs_count;
3727 struct line_header lhdr;
3728 size_t count;
3729
3730 /* We have never read the line information for this unit. Read
3731 it now. */
3732
3733 function_addrs = NULL;
3734 function_addrs_count = 0;
3735 if (read_line_info (state, ddata, error_callback, data, entry->u, &lhdr,
3736 &lines, &count))
3737 {
38811401
ILT
3738 struct function_vector *pfvec;
3739
3740 /* If not threaded, reuse DDATA->FVEC for better memory
3741 consumption. */
3742 if (state->threaded)
3743 pfvec = NULL;
3744 else
3745 pfvec = &ddata->fvec;
eff02e4f 3746 read_function_info (state, ddata, &lhdr, error_callback, data,
38811401 3747 entry->u, pfvec, &function_addrs,
eff02e4f
ILT
3748 &function_addrs_count);
3749 free_line_header (state, &lhdr, error_callback, data);
3750 new_data = 1;
3751 }
3752
3753 /* Atomically store the information we just read into the unit.
3754 If another thread is simultaneously writing, it presumably
3755 read the same information, and we don't care which one we
3756 wind up with; we just leak the other one. We do have to
3757 write the lines field last, so that the acquire-loads above
3758 ensure that the other fields are set. */
3759
3760 if (!state->threaded)
3761 {
3762 u->lines_count = count;
3763 u->function_addrs = function_addrs;
3764 u->function_addrs_count = function_addrs_count;
3765 u->lines = lines;
3766 }
3767 else
3768 {
49579c7e
ILT
3769 backtrace_atomic_store_size_t (&u->lines_count, count);
3770 backtrace_atomic_store_pointer (&u->function_addrs, function_addrs);
3771 backtrace_atomic_store_size_t (&u->function_addrs_count,
3772 function_addrs_count);
3773 backtrace_atomic_store_pointer (&u->lines, lines);
eff02e4f
ILT
3774 }
3775 }
3776
3777 /* Now all fields of U have been initialized. */
3778
3779 if (lines == (struct line *) (uintptr_t) -1)
3780 {
3781 /* If reading the line number information failed in some way,
3782 try again to see if there is a better compilation unit for
3783 this PC. */
3784 if (new_data)
e561a992
ILT
3785 return dwarf_lookup_pc (state, ddata, pc, callback, error_callback,
3786 data, found);
eff02e4f
ILT
3787 return callback (data, pc, NULL, 0, NULL);
3788 }
3789
3790 /* Search for PC within this unit. */
3791
3792 ln = (struct line *) bsearch (&pc, lines, entry->u->lines_count,
3793 sizeof (struct line), line_search);
3794 if (ln == NULL)
3795 {
6b514c53
ILT
3796 /* The PC is between the low_pc and high_pc attributes of the
3797 compilation unit, but no entry in the line table covers it.
3798 This implies that the start of the compilation unit has no
3799 line number information. */
3800
3801 if (entry->u->abs_filename == NULL)
3802 {
3803 const char *filename;
3804
3805 filename = entry->u->filename;
3806 if (filename != NULL
3807 && !IS_ABSOLUTE_PATH (filename)
3808 && entry->u->comp_dir != NULL)
3809 {
3810 size_t filename_len;
3811 const char *dir;
3812 size_t dir_len;
3813 char *s;
3814
3815 filename_len = strlen (filename);
3816 dir = entry->u->comp_dir;
3817 dir_len = strlen (dir);
3818 s = (char *) backtrace_alloc (state, dir_len + filename_len + 2,
3819 error_callback, data);
3820 if (s == NULL)
3821 {
3822 *found = 0;
3823 return 0;
3824 }
3825 memcpy (s, dir, dir_len);
3826 /* FIXME: Should use backslash if DOS file system. */
3827 s[dir_len] = '/';
3828 memcpy (s + dir_len + 1, filename, filename_len + 1);
3829 filename = s;
3830 }
3831 entry->u->abs_filename = filename;
3832 }
3833
3834 return callback (data, pc, entry->u->abs_filename, 0, NULL);
eff02e4f
ILT
3835 }
3836
3837 /* Search for function name within this unit. */
3838
3839 if (entry->u->function_addrs_count == 0)
3840 return callback (data, pc, ln->filename, ln->lineno, NULL);
3841
0755f573
ILT
3842 p = ((struct function_addrs *)
3843 bsearch (&pc, entry->u->function_addrs,
3844 entry->u->function_addrs_count,
3845 sizeof (struct function_addrs),
3846 function_addrs_search));
3847 if (p == NULL)
eff02e4f
ILT
3848 return callback (data, pc, ln->filename, ln->lineno, NULL);
3849
0755f573 3850 /* Here pc >= p->low && pc < (p + 1)->low. The function_addrs are
e41e66b7
ILT
3851 sorted by low, so if pc > p->low we are at the end of a range of
3852 function_addrs with the same low value. If pc == p->low walk
3853 forward to the end of the range with that low value. Then walk
3854 backward and use the first range that includes pc. */
3855 while (pc == (p + 1)->low)
3856 ++p;
0755f573
ILT
3857 fmatch = NULL;
3858 while (1)
3859 {
3860 if (pc < p->high)
3861 {
3862 fmatch = p;
3863 break;
3864 }
3865 if (p == entry->u->function_addrs)
3866 break;
3867 if ((p - 1)->low < p->low)
3868 break;
3869 --p;
3870 }
3871 if (fmatch == NULL)
3872 return callback (data, pc, ln->filename, ln->lineno, NULL);
eff02e4f 3873
0755f573 3874 function = fmatch->function;
eff02e4f
ILT
3875
3876 filename = ln->filename;
3877 lineno = ln->lineno;
3878
3879 ret = report_inlined_functions (pc, function, callback, data,
3880 &filename, &lineno);
3881 if (ret != 0)
3882 return ret;
3883
3884 return callback (data, pc, filename, lineno, function->name);
3885}
3886
eff02e4f 3887
e561a992
ILT
3888/* Return the file/line information for a PC using the DWARF mapping
3889 we built earlier. */
3890
3891static int
3892dwarf_fileline (struct backtrace_state *state, uintptr_t pc,
3893 backtrace_full_callback callback,
3894 backtrace_error_callback error_callback, void *data)
3895{
3896 struct dwarf_data *ddata;
3897 int found;
3898 int ret;
3899
3900 if (!state->threaded)
3901 {
3902 for (ddata = (struct dwarf_data *) state->fileline_data;
3903 ddata != NULL;
3904 ddata = ddata->next)
3905 {
3906 ret = dwarf_lookup_pc (state, ddata, pc, callback, error_callback,
3907 data, &found);
3908 if (ret != 0 || found)
3909 return ret;
3910 }
3911 }
3912 else
3913 {
3914 struct dwarf_data **pp;
3915
ce8aa074 3916 pp = (struct dwarf_data **) (void *) &state->fileline_data;
e561a992
ILT
3917 while (1)
3918 {
49579c7e 3919 ddata = backtrace_atomic_load_pointer (pp);
e561a992
ILT
3920 if (ddata == NULL)
3921 break;
3922
3923 ret = dwarf_lookup_pc (state, ddata, pc, callback, error_callback,
3924 data, &found);
3925 if (ret != 0 || found)
3926 return ret;
3927
3928 pp = &ddata->next;
3929 }
3930 }
3931
3932 /* FIXME: See if any libraries have been dlopen'ed. */
3933
3934 return callback (data, pc, NULL, 0, NULL);
3935}
3936
3937/* Initialize our data structures from the DWARF debug info for a
3938 file. Return NULL on failure. */
3939
3940static struct dwarf_data *
3941build_dwarf_data (struct backtrace_state *state,
3942 uintptr_t base_address,
66ab5839 3943 const struct dwarf_sections *dwarf_sections,
e561a992 3944 int is_bigendian,
9ad458d5 3945 struct dwarf_data *altlink,
e561a992
ILT
3946 backtrace_error_callback error_callback,
3947 void *data)
eff02e4f
ILT
3948{
3949 struct unit_addrs_vector addrs_vec;
3950 struct unit_addrs *addrs;
3951 size_t addrs_count;
9d576782
TV
3952 struct unit_vector units_vec;
3953 struct unit **units;
3954 size_t units_count;
eff02e4f
ILT
3955 struct dwarf_data *fdata;
3956
66ab5839
ILT
3957 if (!build_address_map (state, base_address, dwarf_sections, is_bigendian,
3958 altlink, error_callback, data, &addrs_vec,
3959 &units_vec))
e561a992 3960 return NULL;
eff02e4f
ILT
3961
3962 if (!backtrace_vector_release (state, &addrs_vec.vec, error_callback, data))
e561a992 3963 return NULL;
9d576782
TV
3964 if (!backtrace_vector_release (state, &units_vec.vec, error_callback, data))
3965 return NULL;
eff02e4f 3966 addrs = (struct unit_addrs *) addrs_vec.vec.base;
9d576782 3967 units = (struct unit **) units_vec.vec.base;
eff02e4f 3968 addrs_count = addrs_vec.count;
9d576782 3969 units_count = units_vec.count;
c5604b48
ILT
3970 backtrace_qsort (addrs, addrs_count, sizeof (struct unit_addrs),
3971 unit_addrs_compare);
9d576782 3972 /* No qsort for units required, already sorted. */
eff02e4f
ILT
3973
3974 fdata = ((struct dwarf_data *)
3975 backtrace_alloc (state, sizeof (struct dwarf_data),
3976 error_callback, data));
3977 if (fdata == NULL)
e561a992 3978 return NULL;
eff02e4f 3979
e561a992 3980 fdata->next = NULL;
9ad458d5 3981 fdata->altlink = altlink;
e561a992 3982 fdata->base_address = base_address;
eff02e4f
ILT
3983 fdata->addrs = addrs;
3984 fdata->addrs_count = addrs_count;
9d576782
TV
3985 fdata->units = units;
3986 fdata->units_count = units_count;
66ab5839 3987 fdata->dwarf_sections = *dwarf_sections;
eff02e4f
ILT
3988 fdata->is_bigendian = is_bigendian;
3989 memset (&fdata->fvec, 0, sizeof fdata->fvec);
3990
e561a992
ILT
3991 return fdata;
3992}
3993
3994/* Build our data structures from the DWARF sections for a module.
3995 Set FILELINE_FN and STATE->FILELINE_DATA. Return 1 on success, 0
3996 on failure. */
3997
3998int
3999backtrace_dwarf_add (struct backtrace_state *state,
4000 uintptr_t base_address,
66ab5839 4001 const struct dwarf_sections *dwarf_sections,
e561a992 4002 int is_bigendian,
9ad458d5 4003 struct dwarf_data *fileline_altlink,
e561a992 4004 backtrace_error_callback error_callback,
e6f00c83
TV
4005 void *data, fileline *fileline_fn,
4006 struct dwarf_data **fileline_entry)
e561a992
ILT
4007{
4008 struct dwarf_data *fdata;
4009
66ab5839 4010 fdata = build_dwarf_data (state, base_address, dwarf_sections, is_bigendian,
9ad458d5 4011 fileline_altlink, error_callback, data);
e561a992
ILT
4012 if (fdata == NULL)
4013 return 0;
4014
e6f00c83
TV
4015 if (fileline_entry != NULL)
4016 *fileline_entry = fdata;
4017
e561a992
ILT
4018 if (!state->threaded)
4019 {
4020 struct dwarf_data **pp;
4021
ce8aa074 4022 for (pp = (struct dwarf_data **) (void *) &state->fileline_data;
e561a992
ILT
4023 *pp != NULL;
4024 pp = &(*pp)->next)
4025 ;
4026 *pp = fdata;
4027 }
4028 else
4029 {
4030 while (1)
4031 {
4032 struct dwarf_data **pp;
4033
ce8aa074 4034 pp = (struct dwarf_data **) (void *) &state->fileline_data;
e561a992
ILT
4035
4036 while (1)
4037 {
4038 struct dwarf_data *p;
4039
49579c7e 4040 p = backtrace_atomic_load_pointer (pp);
e561a992
ILT
4041
4042 if (p == NULL)
4043 break;
4044
4045 pp = &p->next;
4046 }
4047
4048 if (__sync_bool_compare_and_swap (pp, NULL, fdata))
4049 break;
4050 }
4051 }
eff02e4f
ILT
4052
4053 *fileline_fn = dwarf_fileline;
4054
4055 return 1;
4056}