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