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