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