Daily bump.
[gcc.git] / libbacktrace / dwarf.c
1 /* dwarf.c -- Get file/line information from DWARF for backtraces.
2 Copyright (C) 2012-2018 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 /* Free a unit address vector. */
927
928 static void
929 free_unit_addrs_vector (struct backtrace_state *state,
930 struct unit_addrs_vector *vec,
931 backtrace_error_callback error_callback, void *data)
932 {
933 struct unit_addrs *addrs;
934 size_t i;
935
936 addrs = (struct unit_addrs *) vec->vec.base;
937 for (i = 0; i < vec->count; ++i)
938 free_abbrevs (state, &addrs[i].u->abbrevs, error_callback, data);
939 }
940
941 /* Compare unit_addrs for qsort. When ranges are nested, make the
942 smallest one sort last. */
943
944 static int
945 unit_addrs_compare (const void *v1, const void *v2)
946 {
947 const struct unit_addrs *a1 = (const struct unit_addrs *) v1;
948 const struct unit_addrs *a2 = (const struct unit_addrs *) v2;
949
950 if (a1->low < a2->low)
951 return -1;
952 if (a1->low > a2->low)
953 return 1;
954 if (a1->high < a2->high)
955 return 1;
956 if (a1->high > a2->high)
957 return -1;
958 if (a1->u->lineoff < a2->u->lineoff)
959 return -1;
960 if (a1->u->lineoff > a2->u->lineoff)
961 return 1;
962 return 0;
963 }
964
965 /* Compare a PC against a unit_addrs for bsearch. Note that if there
966 are multiple ranges containing PC, which one will be returned is
967 unpredictable. We compensate for that in dwarf_fileline. */
968
969 static int
970 unit_addrs_search (const void *vkey, const void *ventry)
971 {
972 const uintptr_t *key = (const uintptr_t *) vkey;
973 const struct unit_addrs *entry = (const struct unit_addrs *) ventry;
974 uintptr_t pc;
975
976 pc = *key;
977 if (pc < entry->low)
978 return -1;
979 else if (pc >= entry->high)
980 return 1;
981 else
982 return 0;
983 }
984
985 /* Sort the line vector by PC. We want a stable sort here to maintain
986 the order of lines for the same PC values. Since the sequence is
987 being sorted in place, their addresses cannot be relied on to
988 maintain stability. That is the purpose of the index member. */
989
990 static int
991 line_compare (const void *v1, const void *v2)
992 {
993 const struct line *ln1 = (const struct line *) v1;
994 const struct line *ln2 = (const struct line *) v2;
995
996 if (ln1->pc < ln2->pc)
997 return -1;
998 else if (ln1->pc > ln2->pc)
999 return 1;
1000 else if (ln1->idx < ln2->idx)
1001 return -1;
1002 else if (ln1->idx > ln2->idx)
1003 return 1;
1004 else
1005 return 0;
1006 }
1007
1008 /* Find a PC in a line vector. We always allocate an extra entry at
1009 the end of the lines vector, so that this routine can safely look
1010 at the next entry. Note that when there are multiple mappings for
1011 the same PC value, this will return the last one. */
1012
1013 static int
1014 line_search (const void *vkey, const void *ventry)
1015 {
1016 const uintptr_t *key = (const uintptr_t *) vkey;
1017 const struct line *entry = (const struct line *) ventry;
1018 uintptr_t pc;
1019
1020 pc = *key;
1021 if (pc < entry->pc)
1022 return -1;
1023 else if (pc >= (entry + 1)->pc)
1024 return 1;
1025 else
1026 return 0;
1027 }
1028
1029 /* Sort the abbrevs by the abbrev code. This function is passed to
1030 both qsort and bsearch. */
1031
1032 static int
1033 abbrev_compare (const void *v1, const void *v2)
1034 {
1035 const struct abbrev *a1 = (const struct abbrev *) v1;
1036 const struct abbrev *a2 = (const struct abbrev *) v2;
1037
1038 if (a1->code < a2->code)
1039 return -1;
1040 else if (a1->code > a2->code)
1041 return 1;
1042 else
1043 {
1044 /* This really shouldn't happen. It means there are two
1045 different abbrevs with the same code, and that means we don't
1046 know which one lookup_abbrev should return. */
1047 return 0;
1048 }
1049 }
1050
1051 /* Read the abbreviation table for a compilation unit. Returns 1 on
1052 success, 0 on failure. */
1053
1054 static int
1055 read_abbrevs (struct backtrace_state *state, uint64_t abbrev_offset,
1056 const unsigned char *dwarf_abbrev, size_t dwarf_abbrev_size,
1057 int is_bigendian, backtrace_error_callback error_callback,
1058 void *data, struct abbrevs *abbrevs)
1059 {
1060 struct dwarf_buf abbrev_buf;
1061 struct dwarf_buf count_buf;
1062 size_t num_abbrevs;
1063
1064 abbrevs->num_abbrevs = 0;
1065 abbrevs->abbrevs = NULL;
1066
1067 if (abbrev_offset >= dwarf_abbrev_size)
1068 {
1069 error_callback (data, "abbrev offset out of range", 0);
1070 return 0;
1071 }
1072
1073 abbrev_buf.name = ".debug_abbrev";
1074 abbrev_buf.start = dwarf_abbrev;
1075 abbrev_buf.buf = dwarf_abbrev + abbrev_offset;
1076 abbrev_buf.left = dwarf_abbrev_size - abbrev_offset;
1077 abbrev_buf.is_bigendian = is_bigendian;
1078 abbrev_buf.error_callback = error_callback;
1079 abbrev_buf.data = data;
1080 abbrev_buf.reported_underflow = 0;
1081
1082 /* Count the number of abbrevs in this list. */
1083
1084 count_buf = abbrev_buf;
1085 num_abbrevs = 0;
1086 while (read_uleb128 (&count_buf) != 0)
1087 {
1088 if (count_buf.reported_underflow)
1089 return 0;
1090 ++num_abbrevs;
1091 // Skip tag.
1092 read_uleb128 (&count_buf);
1093 // Skip has_children.
1094 read_byte (&count_buf);
1095 // Skip attributes.
1096 while (read_uleb128 (&count_buf) != 0)
1097 read_uleb128 (&count_buf);
1098 // Skip form of last attribute.
1099 read_uleb128 (&count_buf);
1100 }
1101
1102 if (count_buf.reported_underflow)
1103 return 0;
1104
1105 if (num_abbrevs == 0)
1106 return 1;
1107
1108 abbrevs->abbrevs = ((struct abbrev *)
1109 backtrace_alloc (state,
1110 num_abbrevs * sizeof (struct abbrev),
1111 error_callback, data));
1112 if (abbrevs->abbrevs == NULL)
1113 return 0;
1114 abbrevs->num_abbrevs = num_abbrevs;
1115 memset (abbrevs->abbrevs, 0, num_abbrevs * sizeof (struct abbrev));
1116
1117 num_abbrevs = 0;
1118 while (1)
1119 {
1120 uint64_t code;
1121 struct abbrev a;
1122 size_t num_attrs;
1123 struct attr *attrs;
1124
1125 if (abbrev_buf.reported_underflow)
1126 goto fail;
1127
1128 code = read_uleb128 (&abbrev_buf);
1129 if (code == 0)
1130 break;
1131
1132 a.code = code;
1133 a.tag = (enum dwarf_tag) read_uleb128 (&abbrev_buf);
1134 a.has_children = read_byte (&abbrev_buf);
1135
1136 count_buf = abbrev_buf;
1137 num_attrs = 0;
1138 while (read_uleb128 (&count_buf) != 0)
1139 {
1140 ++num_attrs;
1141 read_uleb128 (&count_buf);
1142 }
1143
1144 if (num_attrs == 0)
1145 {
1146 attrs = NULL;
1147 read_uleb128 (&abbrev_buf);
1148 read_uleb128 (&abbrev_buf);
1149 }
1150 else
1151 {
1152 attrs = ((struct attr *)
1153 backtrace_alloc (state, num_attrs * sizeof *attrs,
1154 error_callback, data));
1155 if (attrs == NULL)
1156 goto fail;
1157 num_attrs = 0;
1158 while (1)
1159 {
1160 uint64_t name;
1161 uint64_t form;
1162
1163 name = read_uleb128 (&abbrev_buf);
1164 form = read_uleb128 (&abbrev_buf);
1165 if (name == 0)
1166 break;
1167 attrs[num_attrs].name = (enum dwarf_attribute) name;
1168 attrs[num_attrs].form = (enum dwarf_form) form;
1169 ++num_attrs;
1170 }
1171 }
1172
1173 a.num_attrs = num_attrs;
1174 a.attrs = attrs;
1175
1176 abbrevs->abbrevs[num_abbrevs] = a;
1177 ++num_abbrevs;
1178 }
1179
1180 backtrace_qsort (abbrevs->abbrevs, abbrevs->num_abbrevs,
1181 sizeof (struct abbrev), abbrev_compare);
1182
1183 return 1;
1184
1185 fail:
1186 free_abbrevs (state, abbrevs, error_callback, data);
1187 return 0;
1188 }
1189
1190 /* Return the abbrev information for an abbrev code. */
1191
1192 static const struct abbrev *
1193 lookup_abbrev (struct abbrevs *abbrevs, uint64_t code,
1194 backtrace_error_callback error_callback, void *data)
1195 {
1196 struct abbrev key;
1197 void *p;
1198
1199 /* With GCC, where abbrevs are simply numbered in order, we should
1200 be able to just look up the entry. */
1201 if (code - 1 < abbrevs->num_abbrevs
1202 && abbrevs->abbrevs[code - 1].code == code)
1203 return &abbrevs->abbrevs[code - 1];
1204
1205 /* Otherwise we have to search. */
1206 memset (&key, 0, sizeof key);
1207 key.code = code;
1208 p = bsearch (&key, abbrevs->abbrevs, abbrevs->num_abbrevs,
1209 sizeof (struct abbrev), abbrev_compare);
1210 if (p == NULL)
1211 {
1212 error_callback (data, "invalid abbreviation code", 0);
1213 return NULL;
1214 }
1215 return (const struct abbrev *) p;
1216 }
1217
1218 /* Add non-contiguous address ranges for a compilation unit. Returns
1219 1 on success, 0 on failure. */
1220
1221 static int
1222 add_unit_ranges (struct backtrace_state *state, uintptr_t base_address,
1223 struct unit *u, uint64_t ranges, uint64_t base,
1224 int is_bigendian, const unsigned char *dwarf_ranges,
1225 size_t dwarf_ranges_size,
1226 backtrace_error_callback error_callback, void *data,
1227 struct unit_addrs_vector *addrs)
1228 {
1229 struct dwarf_buf ranges_buf;
1230
1231 if (ranges >= dwarf_ranges_size)
1232 {
1233 error_callback (data, "ranges offset out of range", 0);
1234 return 0;
1235 }
1236
1237 ranges_buf.name = ".debug_ranges";
1238 ranges_buf.start = dwarf_ranges;
1239 ranges_buf.buf = dwarf_ranges + ranges;
1240 ranges_buf.left = dwarf_ranges_size - ranges;
1241 ranges_buf.is_bigendian = is_bigendian;
1242 ranges_buf.error_callback = error_callback;
1243 ranges_buf.data = data;
1244 ranges_buf.reported_underflow = 0;
1245
1246 while (1)
1247 {
1248 uint64_t low;
1249 uint64_t high;
1250
1251 if (ranges_buf.reported_underflow)
1252 return 0;
1253
1254 low = read_address (&ranges_buf, u->addrsize);
1255 high = read_address (&ranges_buf, u->addrsize);
1256
1257 if (low == 0 && high == 0)
1258 break;
1259
1260 if (is_highest_address (low, u->addrsize))
1261 base = high;
1262 else
1263 {
1264 struct unit_addrs a;
1265
1266 a.low = low + base;
1267 a.high = high + base;
1268 a.u = u;
1269 if (!add_unit_addr (state, base_address, a, error_callback, data,
1270 addrs))
1271 return 0;
1272 }
1273 }
1274
1275 if (ranges_buf.reported_underflow)
1276 return 0;
1277
1278 return 1;
1279 }
1280
1281 /* Find the address range covered by a compilation unit, reading from
1282 UNIT_BUF and adding values to U. Returns 1 if all data could be
1283 read, 0 if there is some error. */
1284
1285 static int
1286 find_address_ranges (struct backtrace_state *state, uintptr_t base_address,
1287 struct dwarf_buf *unit_buf,
1288 const unsigned char *dwarf_str, size_t dwarf_str_size,
1289 const unsigned char *dwarf_ranges,
1290 size_t dwarf_ranges_size,
1291 int is_bigendian, backtrace_error_callback error_callback,
1292 void *data, struct unit *u,
1293 struct unit_addrs_vector *addrs)
1294 {
1295 while (unit_buf->left > 0)
1296 {
1297 uint64_t code;
1298 const struct abbrev *abbrev;
1299 uint64_t lowpc;
1300 int have_lowpc;
1301 uint64_t highpc;
1302 int have_highpc;
1303 int highpc_is_relative;
1304 uint64_t ranges;
1305 int have_ranges;
1306 size_t i;
1307
1308 code = read_uleb128 (unit_buf);
1309 if (code == 0)
1310 return 1;
1311
1312 abbrev = lookup_abbrev (&u->abbrevs, code, error_callback, data);
1313 if (abbrev == NULL)
1314 return 0;
1315
1316 lowpc = 0;
1317 have_lowpc = 0;
1318 highpc = 0;
1319 have_highpc = 0;
1320 highpc_is_relative = 0;
1321 ranges = 0;
1322 have_ranges = 0;
1323 for (i = 0; i < abbrev->num_attrs; ++i)
1324 {
1325 struct attr_val val;
1326
1327 if (!read_attribute (abbrev->attrs[i].form, unit_buf,
1328 u->is_dwarf64, u->version, u->addrsize,
1329 dwarf_str, dwarf_str_size, &val))
1330 return 0;
1331
1332 switch (abbrev->attrs[i].name)
1333 {
1334 case DW_AT_low_pc:
1335 if (val.encoding == ATTR_VAL_ADDRESS)
1336 {
1337 lowpc = val.u.uint;
1338 have_lowpc = 1;
1339 }
1340 break;
1341
1342 case DW_AT_high_pc:
1343 if (val.encoding == ATTR_VAL_ADDRESS)
1344 {
1345 highpc = val.u.uint;
1346 have_highpc = 1;
1347 }
1348 else if (val.encoding == ATTR_VAL_UINT)
1349 {
1350 highpc = val.u.uint;
1351 have_highpc = 1;
1352 highpc_is_relative = 1;
1353 }
1354 break;
1355
1356 case DW_AT_ranges:
1357 if (val.encoding == ATTR_VAL_UINT
1358 || val.encoding == ATTR_VAL_REF_SECTION)
1359 {
1360 ranges = val.u.uint;
1361 have_ranges = 1;
1362 }
1363 break;
1364
1365 case DW_AT_stmt_list:
1366 if (abbrev->tag == DW_TAG_compile_unit
1367 && (val.encoding == ATTR_VAL_UINT
1368 || val.encoding == ATTR_VAL_REF_SECTION))
1369 u->lineoff = val.u.uint;
1370 break;
1371
1372 case DW_AT_name:
1373 if (abbrev->tag == DW_TAG_compile_unit
1374 && val.encoding == ATTR_VAL_STRING)
1375 u->filename = val.u.string;
1376 break;
1377
1378 case DW_AT_comp_dir:
1379 if (abbrev->tag == DW_TAG_compile_unit
1380 && val.encoding == ATTR_VAL_STRING)
1381 u->comp_dir = val.u.string;
1382 break;
1383
1384 default:
1385 break;
1386 }
1387 }
1388
1389 if (abbrev->tag == DW_TAG_compile_unit
1390 || abbrev->tag == DW_TAG_subprogram)
1391 {
1392 if (have_ranges)
1393 {
1394 if (!add_unit_ranges (state, base_address, u, ranges, lowpc,
1395 is_bigendian, dwarf_ranges,
1396 dwarf_ranges_size, error_callback,
1397 data, addrs))
1398 return 0;
1399 }
1400 else if (have_lowpc && have_highpc)
1401 {
1402 struct unit_addrs a;
1403
1404 if (highpc_is_relative)
1405 highpc += lowpc;
1406 a.low = lowpc;
1407 a.high = highpc;
1408 a.u = u;
1409
1410 if (!add_unit_addr (state, base_address, a, error_callback, data,
1411 addrs))
1412 return 0;
1413 }
1414
1415 /* If we found the PC range in the DW_TAG_compile_unit, we
1416 can stop now. */
1417 if (abbrev->tag == DW_TAG_compile_unit
1418 && (have_ranges || (have_lowpc && have_highpc)))
1419 return 1;
1420 }
1421
1422 if (abbrev->has_children)
1423 {
1424 if (!find_address_ranges (state, base_address, unit_buf,
1425 dwarf_str, dwarf_str_size,
1426 dwarf_ranges, dwarf_ranges_size,
1427 is_bigendian, error_callback, data,
1428 u, addrs))
1429 return 0;
1430 }
1431 }
1432
1433 return 1;
1434 }
1435
1436 /* Build a mapping from address ranges to the compilation units where
1437 the line number information for that range can be found. Returns 1
1438 on success, 0 on failure. */
1439
1440 static int
1441 build_address_map (struct backtrace_state *state, uintptr_t base_address,
1442 const unsigned char *dwarf_info, size_t dwarf_info_size,
1443 const unsigned char *dwarf_abbrev, size_t dwarf_abbrev_size,
1444 const unsigned char *dwarf_ranges, size_t dwarf_ranges_size,
1445 const unsigned char *dwarf_str, size_t dwarf_str_size,
1446 int is_bigendian, backtrace_error_callback error_callback,
1447 void *data, struct unit_addrs_vector *addrs)
1448 {
1449 struct dwarf_buf info;
1450 struct abbrevs abbrevs;
1451
1452 memset (&addrs->vec, 0, sizeof addrs->vec);
1453 addrs->count = 0;
1454
1455 /* Read through the .debug_info section. FIXME: Should we use the
1456 .debug_aranges section? gdb and addr2line don't use it, but I'm
1457 not sure why. */
1458
1459 info.name = ".debug_info";
1460 info.start = dwarf_info;
1461 info.buf = dwarf_info;
1462 info.left = dwarf_info_size;
1463 info.is_bigendian = is_bigendian;
1464 info.error_callback = error_callback;
1465 info.data = data;
1466 info.reported_underflow = 0;
1467
1468 memset (&abbrevs, 0, sizeof abbrevs);
1469 while (info.left > 0)
1470 {
1471 const unsigned char *unit_data_start;
1472 uint64_t len;
1473 int is_dwarf64;
1474 struct dwarf_buf unit_buf;
1475 int version;
1476 uint64_t abbrev_offset;
1477 int addrsize;
1478 struct unit *u;
1479
1480 if (info.reported_underflow)
1481 goto fail;
1482
1483 unit_data_start = info.buf;
1484
1485 len = read_initial_length (&info, &is_dwarf64);
1486 unit_buf = info;
1487 unit_buf.left = len;
1488
1489 if (!advance (&info, len))
1490 goto fail;
1491
1492 version = read_uint16 (&unit_buf);
1493 if (version < 2 || version > 4)
1494 {
1495 dwarf_buf_error (&unit_buf, "unrecognized DWARF version");
1496 goto fail;
1497 }
1498
1499 abbrev_offset = read_offset (&unit_buf, is_dwarf64);
1500 if (!read_abbrevs (state, abbrev_offset, dwarf_abbrev, dwarf_abbrev_size,
1501 is_bigendian, error_callback, data, &abbrevs))
1502 goto fail;
1503
1504 addrsize = read_byte (&unit_buf);
1505
1506 u = ((struct unit *)
1507 backtrace_alloc (state, sizeof *u, error_callback, data));
1508 if (u == NULL)
1509 goto fail;
1510 u->unit_data = unit_buf.buf;
1511 u->unit_data_len = unit_buf.left;
1512 u->unit_data_offset = unit_buf.buf - unit_data_start;
1513 u->version = version;
1514 u->is_dwarf64 = is_dwarf64;
1515 u->addrsize = addrsize;
1516 u->filename = NULL;
1517 u->comp_dir = NULL;
1518 u->abs_filename = NULL;
1519 u->lineoff = 0;
1520 u->abbrevs = abbrevs;
1521 memset (&abbrevs, 0, sizeof abbrevs);
1522
1523 /* The actual line number mappings will be read as needed. */
1524 u->lines = NULL;
1525 u->lines_count = 0;
1526 u->function_addrs = NULL;
1527 u->function_addrs_count = 0;
1528
1529 if (!find_address_ranges (state, base_address, &unit_buf,
1530 dwarf_str, dwarf_str_size,
1531 dwarf_ranges, dwarf_ranges_size,
1532 is_bigendian, error_callback, data,
1533 u, addrs))
1534 {
1535 free_abbrevs (state, &u->abbrevs, error_callback, data);
1536 backtrace_free (state, u, sizeof *u, error_callback, data);
1537 goto fail;
1538 }
1539
1540 if (unit_buf.reported_underflow)
1541 {
1542 free_abbrevs (state, &u->abbrevs, error_callback, data);
1543 backtrace_free (state, u, sizeof *u, error_callback, data);
1544 goto fail;
1545 }
1546 }
1547 if (info.reported_underflow)
1548 goto fail;
1549
1550 return 1;
1551
1552 fail:
1553 free_abbrevs (state, &abbrevs, error_callback, data);
1554 free_unit_addrs_vector (state, addrs, error_callback, data);
1555 return 0;
1556 }
1557
1558 /* Add a new mapping to the vector of line mappings that we are
1559 building. Returns 1 on success, 0 on failure. */
1560
1561 static int
1562 add_line (struct backtrace_state *state, struct dwarf_data *ddata,
1563 uintptr_t pc, const char *filename, int lineno,
1564 backtrace_error_callback error_callback, void *data,
1565 struct line_vector *vec)
1566 {
1567 struct line *ln;
1568
1569 /* If we are adding the same mapping, ignore it. This can happen
1570 when using discriminators. */
1571 if (vec->count > 0)
1572 {
1573 ln = (struct line *) vec->vec.base + (vec->count - 1);
1574 if (pc == ln->pc && filename == ln->filename && lineno == ln->lineno)
1575 return 1;
1576 }
1577
1578 ln = ((struct line *)
1579 backtrace_vector_grow (state, sizeof (struct line), error_callback,
1580 data, &vec->vec));
1581 if (ln == NULL)
1582 return 0;
1583
1584 /* Add in the base address here, so that we can look up the PC
1585 directly. */
1586 ln->pc = pc + ddata->base_address;
1587
1588 ln->filename = filename;
1589 ln->lineno = lineno;
1590 ln->idx = vec->count;
1591
1592 ++vec->count;
1593
1594 return 1;
1595 }
1596
1597 /* Free the line header information. */
1598
1599 static void
1600 free_line_header (struct backtrace_state *state, struct line_header *hdr,
1601 backtrace_error_callback error_callback, void *data)
1602 {
1603 if (hdr->dirs_count != 0)
1604 backtrace_free (state, hdr->dirs, hdr->dirs_count * sizeof (const char *),
1605 error_callback, data);
1606 backtrace_free (state, hdr->filenames,
1607 hdr->filenames_count * sizeof (char *),
1608 error_callback, data);
1609 }
1610
1611 /* Read the line header. Return 1 on success, 0 on failure. */
1612
1613 static int
1614 read_line_header (struct backtrace_state *state, struct unit *u,
1615 int is_dwarf64, struct dwarf_buf *line_buf,
1616 struct line_header *hdr)
1617 {
1618 uint64_t hdrlen;
1619 struct dwarf_buf hdr_buf;
1620 const unsigned char *p;
1621 const unsigned char *pend;
1622 size_t i;
1623
1624 hdr->version = read_uint16 (line_buf);
1625 if (hdr->version < 2 || hdr->version > 4)
1626 {
1627 dwarf_buf_error (line_buf, "unsupported line number version");
1628 return 0;
1629 }
1630
1631 hdrlen = read_offset (line_buf, is_dwarf64);
1632
1633 hdr_buf = *line_buf;
1634 hdr_buf.left = hdrlen;
1635
1636 if (!advance (line_buf, hdrlen))
1637 return 0;
1638
1639 hdr->min_insn_len = read_byte (&hdr_buf);
1640 if (hdr->version < 4)
1641 hdr->max_ops_per_insn = 1;
1642 else
1643 hdr->max_ops_per_insn = read_byte (&hdr_buf);
1644
1645 /* We don't care about default_is_stmt. */
1646 read_byte (&hdr_buf);
1647
1648 hdr->line_base = read_sbyte (&hdr_buf);
1649 hdr->line_range = read_byte (&hdr_buf);
1650
1651 hdr->opcode_base = read_byte (&hdr_buf);
1652 hdr->opcode_lengths = hdr_buf.buf;
1653 if (!advance (&hdr_buf, hdr->opcode_base - 1))
1654 return 0;
1655
1656 /* Count the number of directory entries. */
1657 hdr->dirs_count = 0;
1658 p = hdr_buf.buf;
1659 pend = p + hdr_buf.left;
1660 while (p < pend && *p != '\0')
1661 {
1662 p += strnlen((const char *) p, pend - p) + 1;
1663 ++hdr->dirs_count;
1664 }
1665
1666 hdr->dirs = NULL;
1667 if (hdr->dirs_count != 0)
1668 {
1669 hdr->dirs = ((const char **)
1670 backtrace_alloc (state,
1671 hdr->dirs_count * sizeof (const char *),
1672 line_buf->error_callback, line_buf->data));
1673 if (hdr->dirs == NULL)
1674 return 0;
1675 }
1676
1677 i = 0;
1678 while (*hdr_buf.buf != '\0')
1679 {
1680 if (hdr_buf.reported_underflow)
1681 return 0;
1682
1683 hdr->dirs[i] = read_string (&hdr_buf);
1684 if (hdr->dirs[i] == NULL)
1685 return 0;
1686 ++i;
1687 }
1688 if (!advance (&hdr_buf, 1))
1689 return 0;
1690
1691 /* Count the number of file entries. */
1692 hdr->filenames_count = 0;
1693 p = hdr_buf.buf;
1694 pend = p + hdr_buf.left;
1695 while (p < pend && *p != '\0')
1696 {
1697 p += strnlen ((const char *) p, pend - p) + 1;
1698 p += leb128_len (p);
1699 p += leb128_len (p);
1700 p += leb128_len (p);
1701 ++hdr->filenames_count;
1702 }
1703
1704 hdr->filenames = ((const char **)
1705 backtrace_alloc (state,
1706 hdr->filenames_count * sizeof (char *),
1707 line_buf->error_callback,
1708 line_buf->data));
1709 if (hdr->filenames == NULL)
1710 return 0;
1711 i = 0;
1712 while (*hdr_buf.buf != '\0')
1713 {
1714 const char *filename;
1715 uint64_t dir_index;
1716
1717 if (hdr_buf.reported_underflow)
1718 return 0;
1719
1720 filename = read_string (&hdr_buf);
1721 if (filename == NULL)
1722 return 0;
1723 dir_index = read_uleb128 (&hdr_buf);
1724 if (IS_ABSOLUTE_PATH (filename)
1725 || (dir_index == 0 && u->comp_dir == NULL))
1726 hdr->filenames[i] = filename;
1727 else
1728 {
1729 const char *dir;
1730 size_t dir_len;
1731 size_t filename_len;
1732 char *s;
1733
1734 if (dir_index == 0)
1735 dir = u->comp_dir;
1736 else if (dir_index - 1 < hdr->dirs_count)
1737 dir = hdr->dirs[dir_index - 1];
1738 else
1739 {
1740 dwarf_buf_error (line_buf,
1741 ("invalid directory index in "
1742 "line number program header"));
1743 return 0;
1744 }
1745 dir_len = strlen (dir);
1746 filename_len = strlen (filename);
1747 s = ((char *)
1748 backtrace_alloc (state, dir_len + filename_len + 2,
1749 line_buf->error_callback, line_buf->data));
1750 if (s == NULL)
1751 return 0;
1752 memcpy (s, dir, dir_len);
1753 /* FIXME: If we are on a DOS-based file system, and the
1754 directory or the file name use backslashes, then we
1755 should use a backslash here. */
1756 s[dir_len] = '/';
1757 memcpy (s + dir_len + 1, filename, filename_len + 1);
1758 hdr->filenames[i] = s;
1759 }
1760
1761 /* Ignore the modification time and size. */
1762 read_uleb128 (&hdr_buf);
1763 read_uleb128 (&hdr_buf);
1764
1765 ++i;
1766 }
1767
1768 if (hdr_buf.reported_underflow)
1769 return 0;
1770
1771 return 1;
1772 }
1773
1774 /* Read the line program, adding line mappings to VEC. Return 1 on
1775 success, 0 on failure. */
1776
1777 static int
1778 read_line_program (struct backtrace_state *state, struct dwarf_data *ddata,
1779 struct unit *u, const struct line_header *hdr,
1780 struct dwarf_buf *line_buf, struct line_vector *vec)
1781 {
1782 uint64_t address;
1783 unsigned int op_index;
1784 const char *reset_filename;
1785 const char *filename;
1786 int lineno;
1787
1788 address = 0;
1789 op_index = 0;
1790 if (hdr->filenames_count > 0)
1791 reset_filename = hdr->filenames[0];
1792 else
1793 reset_filename = "";
1794 filename = reset_filename;
1795 lineno = 1;
1796 while (line_buf->left > 0)
1797 {
1798 unsigned int op;
1799
1800 op = read_byte (line_buf);
1801 if (op >= hdr->opcode_base)
1802 {
1803 unsigned int advance;
1804
1805 /* Special opcode. */
1806 op -= hdr->opcode_base;
1807 advance = op / hdr->line_range;
1808 address += (hdr->min_insn_len * (op_index + advance)
1809 / hdr->max_ops_per_insn);
1810 op_index = (op_index + advance) % hdr->max_ops_per_insn;
1811 lineno += hdr->line_base + (int) (op % hdr->line_range);
1812 add_line (state, ddata, address, filename, lineno,
1813 line_buf->error_callback, line_buf->data, vec);
1814 }
1815 else if (op == DW_LNS_extended_op)
1816 {
1817 uint64_t len;
1818
1819 len = read_uleb128 (line_buf);
1820 op = read_byte (line_buf);
1821 switch (op)
1822 {
1823 case DW_LNE_end_sequence:
1824 /* FIXME: Should we mark the high PC here? It seems
1825 that we already have that information from the
1826 compilation unit. */
1827 address = 0;
1828 op_index = 0;
1829 filename = reset_filename;
1830 lineno = 1;
1831 break;
1832 case DW_LNE_set_address:
1833 address = read_address (line_buf, u->addrsize);
1834 break;
1835 case DW_LNE_define_file:
1836 {
1837 const char *f;
1838 unsigned int dir_index;
1839
1840 f = read_string (line_buf);
1841 if (f == NULL)
1842 return 0;
1843 dir_index = read_uleb128 (line_buf);
1844 /* Ignore that time and length. */
1845 read_uleb128 (line_buf);
1846 read_uleb128 (line_buf);
1847 if (IS_ABSOLUTE_PATH (f))
1848 filename = f;
1849 else
1850 {
1851 const char *dir;
1852 size_t dir_len;
1853 size_t f_len;
1854 char *p;
1855
1856 if (dir_index == 0)
1857 dir = u->comp_dir;
1858 else if (dir_index - 1 < hdr->dirs_count)
1859 dir = hdr->dirs[dir_index - 1];
1860 else
1861 {
1862 dwarf_buf_error (line_buf,
1863 ("invalid directory index "
1864 "in line number program"));
1865 return 0;
1866 }
1867 dir_len = strlen (dir);
1868 f_len = strlen (f);
1869 p = ((char *)
1870 backtrace_alloc (state, dir_len + f_len + 2,
1871 line_buf->error_callback,
1872 line_buf->data));
1873 if (p == NULL)
1874 return 0;
1875 memcpy (p, dir, dir_len);
1876 /* FIXME: If we are on a DOS-based file system,
1877 and the directory or the file name use
1878 backslashes, then we should use a backslash
1879 here. */
1880 p[dir_len] = '/';
1881 memcpy (p + dir_len + 1, f, f_len + 1);
1882 filename = p;
1883 }
1884 }
1885 break;
1886 case DW_LNE_set_discriminator:
1887 /* We don't care about discriminators. */
1888 read_uleb128 (line_buf);
1889 break;
1890 default:
1891 if (!advance (line_buf, len - 1))
1892 return 0;
1893 break;
1894 }
1895 }
1896 else
1897 {
1898 switch (op)
1899 {
1900 case DW_LNS_copy:
1901 add_line (state, ddata, address, filename, lineno,
1902 line_buf->error_callback, line_buf->data, vec);
1903 break;
1904 case DW_LNS_advance_pc:
1905 {
1906 uint64_t advance;
1907
1908 advance = read_uleb128 (line_buf);
1909 address += (hdr->min_insn_len * (op_index + advance)
1910 / hdr->max_ops_per_insn);
1911 op_index = (op_index + advance) % hdr->max_ops_per_insn;
1912 }
1913 break;
1914 case DW_LNS_advance_line:
1915 lineno += (int) read_sleb128 (line_buf);
1916 break;
1917 case DW_LNS_set_file:
1918 {
1919 uint64_t fileno;
1920
1921 fileno = read_uleb128 (line_buf);
1922 if (fileno == 0)
1923 filename = "";
1924 else
1925 {
1926 if (fileno - 1 >= hdr->filenames_count)
1927 {
1928 dwarf_buf_error (line_buf,
1929 ("invalid file number in "
1930 "line number program"));
1931 return 0;
1932 }
1933 filename = hdr->filenames[fileno - 1];
1934 }
1935 }
1936 break;
1937 case DW_LNS_set_column:
1938 read_uleb128 (line_buf);
1939 break;
1940 case DW_LNS_negate_stmt:
1941 break;
1942 case DW_LNS_set_basic_block:
1943 break;
1944 case DW_LNS_const_add_pc:
1945 {
1946 unsigned int advance;
1947
1948 op = 255 - hdr->opcode_base;
1949 advance = op / hdr->line_range;
1950 address += (hdr->min_insn_len * (op_index + advance)
1951 / hdr->max_ops_per_insn);
1952 op_index = (op_index + advance) % hdr->max_ops_per_insn;
1953 }
1954 break;
1955 case DW_LNS_fixed_advance_pc:
1956 address += read_uint16 (line_buf);
1957 op_index = 0;
1958 break;
1959 case DW_LNS_set_prologue_end:
1960 break;
1961 case DW_LNS_set_epilogue_begin:
1962 break;
1963 case DW_LNS_set_isa:
1964 read_uleb128 (line_buf);
1965 break;
1966 default:
1967 {
1968 unsigned int i;
1969
1970 for (i = hdr->opcode_lengths[op - 1]; i > 0; --i)
1971 read_uleb128 (line_buf);
1972 }
1973 break;
1974 }
1975 }
1976 }
1977
1978 return 1;
1979 }
1980
1981 /* Read the line number information for a compilation unit. Returns 1
1982 on success, 0 on failure. */
1983
1984 static int
1985 read_line_info (struct backtrace_state *state, struct dwarf_data *ddata,
1986 backtrace_error_callback error_callback, void *data,
1987 struct unit *u, struct line_header *hdr, struct line **lines,
1988 size_t *lines_count)
1989 {
1990 struct line_vector vec;
1991 struct dwarf_buf line_buf;
1992 uint64_t len;
1993 int is_dwarf64;
1994 struct line *ln;
1995
1996 memset (&vec.vec, 0, sizeof vec.vec);
1997 vec.count = 0;
1998
1999 memset (hdr, 0, sizeof *hdr);
2000
2001 if (u->lineoff != (off_t) (size_t) u->lineoff
2002 || (size_t) u->lineoff >= ddata->dwarf_line_size)
2003 {
2004 error_callback (data, "unit line offset out of range", 0);
2005 goto fail;
2006 }
2007
2008 line_buf.name = ".debug_line";
2009 line_buf.start = ddata->dwarf_line;
2010 line_buf.buf = ddata->dwarf_line + u->lineoff;
2011 line_buf.left = ddata->dwarf_line_size - u->lineoff;
2012 line_buf.is_bigendian = ddata->is_bigendian;
2013 line_buf.error_callback = error_callback;
2014 line_buf.data = data;
2015 line_buf.reported_underflow = 0;
2016
2017 len = read_initial_length (&line_buf, &is_dwarf64);
2018 line_buf.left = len;
2019
2020 if (!read_line_header (state, u, is_dwarf64, &line_buf, hdr))
2021 goto fail;
2022
2023 if (!read_line_program (state, ddata, u, hdr, &line_buf, &vec))
2024 goto fail;
2025
2026 if (line_buf.reported_underflow)
2027 goto fail;
2028
2029 if (vec.count == 0)
2030 {
2031 /* This is not a failure in the sense of a generating an error,
2032 but it is a failure in that sense that we have no useful
2033 information. */
2034 goto fail;
2035 }
2036
2037 /* Allocate one extra entry at the end. */
2038 ln = ((struct line *)
2039 backtrace_vector_grow (state, sizeof (struct line), error_callback,
2040 data, &vec.vec));
2041 if (ln == NULL)
2042 goto fail;
2043 ln->pc = (uintptr_t) -1;
2044 ln->filename = NULL;
2045 ln->lineno = 0;
2046 ln->idx = 0;
2047
2048 if (!backtrace_vector_release (state, &vec.vec, error_callback, data))
2049 goto fail;
2050
2051 ln = (struct line *) vec.vec.base;
2052 backtrace_qsort (ln, vec.count, sizeof (struct line), line_compare);
2053
2054 *lines = ln;
2055 *lines_count = vec.count;
2056
2057 return 1;
2058
2059 fail:
2060 backtrace_vector_free (state, &vec.vec, error_callback, data);
2061 free_line_header (state, hdr, error_callback, data);
2062 *lines = (struct line *) (uintptr_t) -1;
2063 *lines_count = 0;
2064 return 0;
2065 }
2066
2067 /* Read the name of a function from a DIE referenced by a
2068 DW_AT_abstract_origin or DW_AT_specification tag. OFFSET is within
2069 the same compilation unit. */
2070
2071 static const char *
2072 read_referenced_name (struct dwarf_data *ddata, struct unit *u,
2073 uint64_t offset, backtrace_error_callback error_callback,
2074 void *data)
2075 {
2076 struct dwarf_buf unit_buf;
2077 uint64_t code;
2078 const struct abbrev *abbrev;
2079 const char *ret;
2080 size_t i;
2081
2082 /* OFFSET is from the start of the data for this compilation unit.
2083 U->unit_data is the data, but it starts U->unit_data_offset bytes
2084 from the beginning. */
2085
2086 if (offset < u->unit_data_offset
2087 || offset - u->unit_data_offset >= u->unit_data_len)
2088 {
2089 error_callback (data,
2090 "abstract origin or specification out of range",
2091 0);
2092 return NULL;
2093 }
2094
2095 offset -= u->unit_data_offset;
2096
2097 unit_buf.name = ".debug_info";
2098 unit_buf.start = ddata->dwarf_info;
2099 unit_buf.buf = u->unit_data + offset;
2100 unit_buf.left = u->unit_data_len - offset;
2101 unit_buf.is_bigendian = ddata->is_bigendian;
2102 unit_buf.error_callback = error_callback;
2103 unit_buf.data = data;
2104 unit_buf.reported_underflow = 0;
2105
2106 code = read_uleb128 (&unit_buf);
2107 if (code == 0)
2108 {
2109 dwarf_buf_error (&unit_buf, "invalid abstract origin or specification");
2110 return NULL;
2111 }
2112
2113 abbrev = lookup_abbrev (&u->abbrevs, code, error_callback, data);
2114 if (abbrev == NULL)
2115 return NULL;
2116
2117 ret = NULL;
2118 for (i = 0; i < abbrev->num_attrs; ++i)
2119 {
2120 struct attr_val val;
2121
2122 if (!read_attribute (abbrev->attrs[i].form, &unit_buf,
2123 u->is_dwarf64, u->version, u->addrsize,
2124 ddata->dwarf_str, ddata->dwarf_str_size,
2125 &val))
2126 return NULL;
2127
2128 switch (abbrev->attrs[i].name)
2129 {
2130 case DW_AT_name:
2131 /* We prefer the linkage name if get one. */
2132 if (val.encoding == ATTR_VAL_STRING)
2133 ret = val.u.string;
2134 break;
2135
2136 case DW_AT_linkage_name:
2137 case DW_AT_MIPS_linkage_name:
2138 if (val.encoding == ATTR_VAL_STRING)
2139 return val.u.string;
2140 break;
2141
2142 case DW_AT_specification:
2143 if (abbrev->attrs[i].form == DW_FORM_ref_addr
2144 || abbrev->attrs[i].form == DW_FORM_ref_sig8)
2145 {
2146 /* This refers to a specification defined in some other
2147 compilation unit. We can handle this case if we
2148 must, but it's harder. */
2149 break;
2150 }
2151 if (val.encoding == ATTR_VAL_UINT
2152 || val.encoding == ATTR_VAL_REF_UNIT)
2153 {
2154 const char *name;
2155
2156 name = read_referenced_name (ddata, u, val.u.uint,
2157 error_callback, data);
2158 if (name != NULL)
2159 ret = name;
2160 }
2161 break;
2162
2163 default:
2164 break;
2165 }
2166 }
2167
2168 return ret;
2169 }
2170
2171 /* Add a single range to U that maps to function. Returns 1 on
2172 success, 0 on error. */
2173
2174 static int
2175 add_function_range (struct backtrace_state *state, struct dwarf_data *ddata,
2176 struct function *function, uint64_t lowpc, uint64_t highpc,
2177 backtrace_error_callback error_callback,
2178 void *data, struct function_vector *vec)
2179 {
2180 struct function_addrs *p;
2181
2182 /* Add in the base address here, so that we can look up the PC
2183 directly. */
2184 lowpc += ddata->base_address;
2185 highpc += ddata->base_address;
2186
2187 if (vec->count > 0)
2188 {
2189 p = (struct function_addrs *) vec->vec.base + vec->count - 1;
2190 if ((lowpc == p->high || lowpc == p->high + 1)
2191 && function == p->function)
2192 {
2193 if (highpc > p->high)
2194 p->high = highpc;
2195 return 1;
2196 }
2197 }
2198
2199 p = ((struct function_addrs *)
2200 backtrace_vector_grow (state, sizeof (struct function_addrs),
2201 error_callback, data, &vec->vec));
2202 if (p == NULL)
2203 return 0;
2204
2205 p->low = lowpc;
2206 p->high = highpc;
2207 p->function = function;
2208 ++vec->count;
2209 return 1;
2210 }
2211
2212 /* Add PC ranges to U that map to FUNCTION. Returns 1 on success, 0
2213 on error. */
2214
2215 static int
2216 add_function_ranges (struct backtrace_state *state, struct dwarf_data *ddata,
2217 struct unit *u, struct function *function,
2218 uint64_t ranges, uint64_t base,
2219 backtrace_error_callback error_callback, void *data,
2220 struct function_vector *vec)
2221 {
2222 struct dwarf_buf ranges_buf;
2223
2224 if (ranges >= ddata->dwarf_ranges_size)
2225 {
2226 error_callback (data, "function ranges offset out of range", 0);
2227 return 0;
2228 }
2229
2230 ranges_buf.name = ".debug_ranges";
2231 ranges_buf.start = ddata->dwarf_ranges;
2232 ranges_buf.buf = ddata->dwarf_ranges + ranges;
2233 ranges_buf.left = ddata->dwarf_ranges_size - ranges;
2234 ranges_buf.is_bigendian = ddata->is_bigendian;
2235 ranges_buf.error_callback = error_callback;
2236 ranges_buf.data = data;
2237 ranges_buf.reported_underflow = 0;
2238
2239 while (1)
2240 {
2241 uint64_t low;
2242 uint64_t high;
2243
2244 if (ranges_buf.reported_underflow)
2245 return 0;
2246
2247 low = read_address (&ranges_buf, u->addrsize);
2248 high = read_address (&ranges_buf, u->addrsize);
2249
2250 if (low == 0 && high == 0)
2251 break;
2252
2253 if (is_highest_address (low, u->addrsize))
2254 base = high;
2255 else
2256 {
2257 if (!add_function_range (state, ddata, function, low + base,
2258 high + base, error_callback, data, vec))
2259 return 0;
2260 }
2261 }
2262
2263 if (ranges_buf.reported_underflow)
2264 return 0;
2265
2266 return 1;
2267 }
2268
2269 /* Read one entry plus all its children. Add function addresses to
2270 VEC. Returns 1 on success, 0 on error. */
2271
2272 static int
2273 read_function_entry (struct backtrace_state *state, struct dwarf_data *ddata,
2274 struct unit *u, uint64_t base, struct dwarf_buf *unit_buf,
2275 const struct line_header *lhdr,
2276 backtrace_error_callback error_callback, void *data,
2277 struct function_vector *vec_function,
2278 struct function_vector *vec_inlined)
2279 {
2280 while (unit_buf->left > 0)
2281 {
2282 uint64_t code;
2283 const struct abbrev *abbrev;
2284 int is_function;
2285 struct function *function;
2286 struct function_vector *vec;
2287 size_t i;
2288 uint64_t lowpc;
2289 int have_lowpc;
2290 uint64_t highpc;
2291 int have_highpc;
2292 int highpc_is_relative;
2293 uint64_t ranges;
2294 int have_ranges;
2295
2296 code = read_uleb128 (unit_buf);
2297 if (code == 0)
2298 return 1;
2299
2300 abbrev = lookup_abbrev (&u->abbrevs, code, error_callback, data);
2301 if (abbrev == NULL)
2302 return 0;
2303
2304 is_function = (abbrev->tag == DW_TAG_subprogram
2305 || abbrev->tag == DW_TAG_entry_point
2306 || abbrev->tag == DW_TAG_inlined_subroutine);
2307
2308 if (abbrev->tag == DW_TAG_inlined_subroutine)
2309 vec = vec_inlined;
2310 else
2311 vec = vec_function;
2312
2313 function = NULL;
2314 if (is_function)
2315 {
2316 function = ((struct function *)
2317 backtrace_alloc (state, sizeof *function,
2318 error_callback, data));
2319 if (function == NULL)
2320 return 0;
2321 memset (function, 0, sizeof *function);
2322 }
2323
2324 lowpc = 0;
2325 have_lowpc = 0;
2326 highpc = 0;
2327 have_highpc = 0;
2328 highpc_is_relative = 0;
2329 ranges = 0;
2330 have_ranges = 0;
2331 for (i = 0; i < abbrev->num_attrs; ++i)
2332 {
2333 struct attr_val val;
2334
2335 if (!read_attribute (abbrev->attrs[i].form, unit_buf,
2336 u->is_dwarf64, u->version, u->addrsize,
2337 ddata->dwarf_str, ddata->dwarf_str_size,
2338 &val))
2339 return 0;
2340
2341 /* The compile unit sets the base address for any address
2342 ranges in the function entries. */
2343 if (abbrev->tag == DW_TAG_compile_unit
2344 && abbrev->attrs[i].name == DW_AT_low_pc
2345 && val.encoding == ATTR_VAL_ADDRESS)
2346 base = val.u.uint;
2347
2348 if (is_function)
2349 {
2350 switch (abbrev->attrs[i].name)
2351 {
2352 case DW_AT_call_file:
2353 if (val.encoding == ATTR_VAL_UINT)
2354 {
2355 if (val.u.uint == 0)
2356 function->caller_filename = "";
2357 else
2358 {
2359 if (val.u.uint - 1 >= lhdr->filenames_count)
2360 {
2361 dwarf_buf_error (unit_buf,
2362 ("invalid file number in "
2363 "DW_AT_call_file attribute"));
2364 return 0;
2365 }
2366 function->caller_filename =
2367 lhdr->filenames[val.u.uint - 1];
2368 }
2369 }
2370 break;
2371
2372 case DW_AT_call_line:
2373 if (val.encoding == ATTR_VAL_UINT)
2374 function->caller_lineno = val.u.uint;
2375 break;
2376
2377 case DW_AT_abstract_origin:
2378 case DW_AT_specification:
2379 if (abbrev->attrs[i].form == DW_FORM_ref_addr
2380 || abbrev->attrs[i].form == DW_FORM_ref_sig8)
2381 {
2382 /* This refers to an abstract origin defined in
2383 some other compilation unit. We can handle
2384 this case if we must, but it's harder. */
2385 break;
2386 }
2387 if (val.encoding == ATTR_VAL_UINT
2388 || val.encoding == ATTR_VAL_REF_UNIT)
2389 {
2390 const char *name;
2391
2392 name = read_referenced_name (ddata, u, val.u.uint,
2393 error_callback, data);
2394 if (name != NULL)
2395 function->name = name;
2396 }
2397 break;
2398
2399 case DW_AT_name:
2400 if (val.encoding == ATTR_VAL_STRING)
2401 {
2402 /* Don't override a name we found in some other
2403 way, as it will normally be more
2404 useful--e.g., this name is normally not
2405 mangled. */
2406 if (function->name == NULL)
2407 function->name = val.u.string;
2408 }
2409 break;
2410
2411 case DW_AT_linkage_name:
2412 case DW_AT_MIPS_linkage_name:
2413 if (val.encoding == ATTR_VAL_STRING)
2414 function->name = val.u.string;
2415 break;
2416
2417 case DW_AT_low_pc:
2418 if (val.encoding == ATTR_VAL_ADDRESS)
2419 {
2420 lowpc = val.u.uint;
2421 have_lowpc = 1;
2422 }
2423 break;
2424
2425 case DW_AT_high_pc:
2426 if (val.encoding == ATTR_VAL_ADDRESS)
2427 {
2428 highpc = val.u.uint;
2429 have_highpc = 1;
2430 }
2431 else if (val.encoding == ATTR_VAL_UINT)
2432 {
2433 highpc = val.u.uint;
2434 have_highpc = 1;
2435 highpc_is_relative = 1;
2436 }
2437 break;
2438
2439 case DW_AT_ranges:
2440 if (val.encoding == ATTR_VAL_UINT
2441 || val.encoding == ATTR_VAL_REF_SECTION)
2442 {
2443 ranges = val.u.uint;
2444 have_ranges = 1;
2445 }
2446 break;
2447
2448 default:
2449 break;
2450 }
2451 }
2452 }
2453
2454 /* If we couldn't find a name for the function, we have no use
2455 for it. */
2456 if (is_function && function->name == NULL)
2457 {
2458 backtrace_free (state, function, sizeof *function,
2459 error_callback, data);
2460 is_function = 0;
2461 }
2462
2463 if (is_function)
2464 {
2465 if (have_ranges)
2466 {
2467 if (!add_function_ranges (state, ddata, u, function, ranges,
2468 base, error_callback, data, vec))
2469 return 0;
2470 }
2471 else if (have_lowpc && have_highpc)
2472 {
2473 if (highpc_is_relative)
2474 highpc += lowpc;
2475 if (!add_function_range (state, ddata, function, lowpc, highpc,
2476 error_callback, data, vec))
2477 return 0;
2478 }
2479 else
2480 {
2481 backtrace_free (state, function, sizeof *function,
2482 error_callback, data);
2483 is_function = 0;
2484 }
2485 }
2486
2487 if (abbrev->has_children)
2488 {
2489 if (!is_function)
2490 {
2491 if (!read_function_entry (state, ddata, u, base, unit_buf, lhdr,
2492 error_callback, data, vec_function,
2493 vec_inlined))
2494 return 0;
2495 }
2496 else
2497 {
2498 struct function_vector fvec;
2499
2500 /* Gather any information for inlined functions in
2501 FVEC. */
2502
2503 memset (&fvec, 0, sizeof fvec);
2504
2505 if (!read_function_entry (state, ddata, u, base, unit_buf, lhdr,
2506 error_callback, data, vec_function,
2507 &fvec))
2508 return 0;
2509
2510 if (fvec.count > 0)
2511 {
2512 struct function_addrs *faddrs;
2513
2514 if (!backtrace_vector_release (state, &fvec.vec,
2515 error_callback, data))
2516 return 0;
2517
2518 faddrs = (struct function_addrs *) fvec.vec.base;
2519 backtrace_qsort (faddrs, fvec.count,
2520 sizeof (struct function_addrs),
2521 function_addrs_compare);
2522
2523 function->function_addrs = faddrs;
2524 function->function_addrs_count = fvec.count;
2525 }
2526 }
2527 }
2528 }
2529
2530 return 1;
2531 }
2532
2533 /* Read function name information for a compilation unit. We look
2534 through the whole unit looking for function tags. */
2535
2536 static void
2537 read_function_info (struct backtrace_state *state, struct dwarf_data *ddata,
2538 const struct line_header *lhdr,
2539 backtrace_error_callback error_callback, void *data,
2540 struct unit *u, struct function_vector *fvec,
2541 struct function_addrs **ret_addrs,
2542 size_t *ret_addrs_count)
2543 {
2544 struct function_vector lvec;
2545 struct function_vector *pfvec;
2546 struct dwarf_buf unit_buf;
2547 struct function_addrs *addrs;
2548 size_t addrs_count;
2549
2550 /* Use FVEC if it is not NULL. Otherwise use our own vector. */
2551 if (fvec != NULL)
2552 pfvec = fvec;
2553 else
2554 {
2555 memset (&lvec, 0, sizeof lvec);
2556 pfvec = &lvec;
2557 }
2558
2559 unit_buf.name = ".debug_info";
2560 unit_buf.start = ddata->dwarf_info;
2561 unit_buf.buf = u->unit_data;
2562 unit_buf.left = u->unit_data_len;
2563 unit_buf.is_bigendian = ddata->is_bigendian;
2564 unit_buf.error_callback = error_callback;
2565 unit_buf.data = data;
2566 unit_buf.reported_underflow = 0;
2567
2568 while (unit_buf.left > 0)
2569 {
2570 if (!read_function_entry (state, ddata, u, 0, &unit_buf, lhdr,
2571 error_callback, data, pfvec, pfvec))
2572 return;
2573 }
2574
2575 if (pfvec->count == 0)
2576 return;
2577
2578 addrs_count = pfvec->count;
2579
2580 if (fvec == NULL)
2581 {
2582 if (!backtrace_vector_release (state, &lvec.vec, error_callback, data))
2583 return;
2584 addrs = (struct function_addrs *) pfvec->vec.base;
2585 }
2586 else
2587 {
2588 /* Finish this list of addresses, but leave the remaining space in
2589 the vector available for the next function unit. */
2590 addrs = ((struct function_addrs *)
2591 backtrace_vector_finish (state, &fvec->vec,
2592 error_callback, data));
2593 if (addrs == NULL)
2594 return;
2595 fvec->count = 0;
2596 }
2597
2598 backtrace_qsort (addrs, addrs_count, sizeof (struct function_addrs),
2599 function_addrs_compare);
2600
2601 *ret_addrs = addrs;
2602 *ret_addrs_count = addrs_count;
2603 }
2604
2605 /* See if PC is inlined in FUNCTION. If it is, print out the inlined
2606 information, and update FILENAME and LINENO for the caller.
2607 Returns whatever CALLBACK returns, or 0 to keep going. */
2608
2609 static int
2610 report_inlined_functions (uintptr_t pc, struct function *function,
2611 backtrace_full_callback callback, void *data,
2612 const char **filename, int *lineno)
2613 {
2614 struct function_addrs *function_addrs;
2615 struct function *inlined;
2616 int ret;
2617
2618 if (function->function_addrs_count == 0)
2619 return 0;
2620
2621 function_addrs = ((struct function_addrs *)
2622 bsearch (&pc, function->function_addrs,
2623 function->function_addrs_count,
2624 sizeof (struct function_addrs),
2625 function_addrs_search));
2626 if (function_addrs == NULL)
2627 return 0;
2628
2629 while (((size_t) (function_addrs - function->function_addrs) + 1
2630 < function->function_addrs_count)
2631 && pc >= (function_addrs + 1)->low
2632 && pc < (function_addrs + 1)->high)
2633 ++function_addrs;
2634
2635 /* We found an inlined call. */
2636
2637 inlined = function_addrs->function;
2638
2639 /* Report any calls inlined into this one. */
2640 ret = report_inlined_functions (pc, inlined, callback, data,
2641 filename, lineno);
2642 if (ret != 0)
2643 return ret;
2644
2645 /* Report this inlined call. */
2646 ret = callback (data, pc, *filename, *lineno, inlined->name);
2647 if (ret != 0)
2648 return ret;
2649
2650 /* Our caller will report the caller of the inlined function; tell
2651 it the appropriate filename and line number. */
2652 *filename = inlined->caller_filename;
2653 *lineno = inlined->caller_lineno;
2654
2655 return 0;
2656 }
2657
2658 /* Look for a PC in the DWARF mapping for one module. On success,
2659 call CALLBACK and return whatever it returns. On error, call
2660 ERROR_CALLBACK and return 0. Sets *FOUND to 1 if the PC is found,
2661 0 if not. */
2662
2663 static int
2664 dwarf_lookup_pc (struct backtrace_state *state, struct dwarf_data *ddata,
2665 uintptr_t pc, backtrace_full_callback callback,
2666 backtrace_error_callback error_callback, void *data,
2667 int *found)
2668 {
2669 struct unit_addrs *entry;
2670 struct unit *u;
2671 int new_data;
2672 struct line *lines;
2673 struct line *ln;
2674 struct function_addrs *function_addrs;
2675 struct function *function;
2676 const char *filename;
2677 int lineno;
2678 int ret;
2679
2680 *found = 1;
2681
2682 /* Find an address range that includes PC. */
2683 entry = bsearch (&pc, ddata->addrs, ddata->addrs_count,
2684 sizeof (struct unit_addrs), unit_addrs_search);
2685
2686 if (entry == NULL)
2687 {
2688 *found = 0;
2689 return 0;
2690 }
2691
2692 /* If there are multiple ranges that contain PC, use the last one,
2693 in order to produce predictable results. If we assume that all
2694 ranges are properly nested, then the last range will be the
2695 smallest one. */
2696 while ((size_t) (entry - ddata->addrs) + 1 < ddata->addrs_count
2697 && pc >= (entry + 1)->low
2698 && pc < (entry + 1)->high)
2699 ++entry;
2700
2701 /* We need the lines, lines_count, function_addrs,
2702 function_addrs_count fields of u. If they are not set, we need
2703 to set them. When running in threaded mode, we need to allow for
2704 the possibility that some other thread is setting them
2705 simultaneously. */
2706
2707 u = entry->u;
2708 lines = u->lines;
2709
2710 /* Skip units with no useful line number information by walking
2711 backward. Useless line number information is marked by setting
2712 lines == -1. */
2713 while (entry > ddata->addrs
2714 && pc >= (entry - 1)->low
2715 && pc < (entry - 1)->high)
2716 {
2717 if (state->threaded)
2718 lines = (struct line *) backtrace_atomic_load_pointer (&u->lines);
2719
2720 if (lines != (struct line *) (uintptr_t) -1)
2721 break;
2722
2723 --entry;
2724
2725 u = entry->u;
2726 lines = u->lines;
2727 }
2728
2729 if (state->threaded)
2730 lines = backtrace_atomic_load_pointer (&u->lines);
2731
2732 new_data = 0;
2733 if (lines == NULL)
2734 {
2735 size_t function_addrs_count;
2736 struct line_header lhdr;
2737 size_t count;
2738
2739 /* We have never read the line information for this unit. Read
2740 it now. */
2741
2742 function_addrs = NULL;
2743 function_addrs_count = 0;
2744 if (read_line_info (state, ddata, error_callback, data, entry->u, &lhdr,
2745 &lines, &count))
2746 {
2747 struct function_vector *pfvec;
2748
2749 /* If not threaded, reuse DDATA->FVEC for better memory
2750 consumption. */
2751 if (state->threaded)
2752 pfvec = NULL;
2753 else
2754 pfvec = &ddata->fvec;
2755 read_function_info (state, ddata, &lhdr, error_callback, data,
2756 entry->u, pfvec, &function_addrs,
2757 &function_addrs_count);
2758 free_line_header (state, &lhdr, error_callback, data);
2759 new_data = 1;
2760 }
2761
2762 /* Atomically store the information we just read into the unit.
2763 If another thread is simultaneously writing, it presumably
2764 read the same information, and we don't care which one we
2765 wind up with; we just leak the other one. We do have to
2766 write the lines field last, so that the acquire-loads above
2767 ensure that the other fields are set. */
2768
2769 if (!state->threaded)
2770 {
2771 u->lines_count = count;
2772 u->function_addrs = function_addrs;
2773 u->function_addrs_count = function_addrs_count;
2774 u->lines = lines;
2775 }
2776 else
2777 {
2778 backtrace_atomic_store_size_t (&u->lines_count, count);
2779 backtrace_atomic_store_pointer (&u->function_addrs, function_addrs);
2780 backtrace_atomic_store_size_t (&u->function_addrs_count,
2781 function_addrs_count);
2782 backtrace_atomic_store_pointer (&u->lines, lines);
2783 }
2784 }
2785
2786 /* Now all fields of U have been initialized. */
2787
2788 if (lines == (struct line *) (uintptr_t) -1)
2789 {
2790 /* If reading the line number information failed in some way,
2791 try again to see if there is a better compilation unit for
2792 this PC. */
2793 if (new_data)
2794 return dwarf_lookup_pc (state, ddata, pc, callback, error_callback,
2795 data, found);
2796 return callback (data, pc, NULL, 0, NULL);
2797 }
2798
2799 /* Search for PC within this unit. */
2800
2801 ln = (struct line *) bsearch (&pc, lines, entry->u->lines_count,
2802 sizeof (struct line), line_search);
2803 if (ln == NULL)
2804 {
2805 /* The PC is between the low_pc and high_pc attributes of the
2806 compilation unit, but no entry in the line table covers it.
2807 This implies that the start of the compilation unit has no
2808 line number information. */
2809
2810 if (entry->u->abs_filename == NULL)
2811 {
2812 const char *filename;
2813
2814 filename = entry->u->filename;
2815 if (filename != NULL
2816 && !IS_ABSOLUTE_PATH (filename)
2817 && entry->u->comp_dir != NULL)
2818 {
2819 size_t filename_len;
2820 const char *dir;
2821 size_t dir_len;
2822 char *s;
2823
2824 filename_len = strlen (filename);
2825 dir = entry->u->comp_dir;
2826 dir_len = strlen (dir);
2827 s = (char *) backtrace_alloc (state, dir_len + filename_len + 2,
2828 error_callback, data);
2829 if (s == NULL)
2830 {
2831 *found = 0;
2832 return 0;
2833 }
2834 memcpy (s, dir, dir_len);
2835 /* FIXME: Should use backslash if DOS file system. */
2836 s[dir_len] = '/';
2837 memcpy (s + dir_len + 1, filename, filename_len + 1);
2838 filename = s;
2839 }
2840 entry->u->abs_filename = filename;
2841 }
2842
2843 return callback (data, pc, entry->u->abs_filename, 0, NULL);
2844 }
2845
2846 /* Search for function name within this unit. */
2847
2848 if (entry->u->function_addrs_count == 0)
2849 return callback (data, pc, ln->filename, ln->lineno, NULL);
2850
2851 function_addrs = ((struct function_addrs *)
2852 bsearch (&pc, entry->u->function_addrs,
2853 entry->u->function_addrs_count,
2854 sizeof (struct function_addrs),
2855 function_addrs_search));
2856 if (function_addrs == NULL)
2857 return callback (data, pc, ln->filename, ln->lineno, NULL);
2858
2859 /* If there are multiple function ranges that contain PC, use the
2860 last one, in order to produce predictable results. */
2861
2862 while (((size_t) (function_addrs - entry->u->function_addrs + 1)
2863 < entry->u->function_addrs_count)
2864 && pc >= (function_addrs + 1)->low
2865 && pc < (function_addrs + 1)->high)
2866 ++function_addrs;
2867
2868 function = function_addrs->function;
2869
2870 filename = ln->filename;
2871 lineno = ln->lineno;
2872
2873 ret = report_inlined_functions (pc, function, callback, data,
2874 &filename, &lineno);
2875 if (ret != 0)
2876 return ret;
2877
2878 return callback (data, pc, filename, lineno, function->name);
2879 }
2880
2881
2882 /* Return the file/line information for a PC using the DWARF mapping
2883 we built earlier. */
2884
2885 static int
2886 dwarf_fileline (struct backtrace_state *state, uintptr_t pc,
2887 backtrace_full_callback callback,
2888 backtrace_error_callback error_callback, void *data)
2889 {
2890 struct dwarf_data *ddata;
2891 int found;
2892 int ret;
2893
2894 if (!state->threaded)
2895 {
2896 for (ddata = (struct dwarf_data *) state->fileline_data;
2897 ddata != NULL;
2898 ddata = ddata->next)
2899 {
2900 ret = dwarf_lookup_pc (state, ddata, pc, callback, error_callback,
2901 data, &found);
2902 if (ret != 0 || found)
2903 return ret;
2904 }
2905 }
2906 else
2907 {
2908 struct dwarf_data **pp;
2909
2910 pp = (struct dwarf_data **) (void *) &state->fileline_data;
2911 while (1)
2912 {
2913 ddata = backtrace_atomic_load_pointer (pp);
2914 if (ddata == NULL)
2915 break;
2916
2917 ret = dwarf_lookup_pc (state, ddata, pc, callback, error_callback,
2918 data, &found);
2919 if (ret != 0 || found)
2920 return ret;
2921
2922 pp = &ddata->next;
2923 }
2924 }
2925
2926 /* FIXME: See if any libraries have been dlopen'ed. */
2927
2928 return callback (data, pc, NULL, 0, NULL);
2929 }
2930
2931 /* Initialize our data structures from the DWARF debug info for a
2932 file. Return NULL on failure. */
2933
2934 static struct dwarf_data *
2935 build_dwarf_data (struct backtrace_state *state,
2936 uintptr_t base_address,
2937 const unsigned char *dwarf_info,
2938 size_t dwarf_info_size,
2939 const unsigned char *dwarf_line,
2940 size_t dwarf_line_size,
2941 const unsigned char *dwarf_abbrev,
2942 size_t dwarf_abbrev_size,
2943 const unsigned char *dwarf_ranges,
2944 size_t dwarf_ranges_size,
2945 const unsigned char *dwarf_str,
2946 size_t dwarf_str_size,
2947 int is_bigendian,
2948 backtrace_error_callback error_callback,
2949 void *data)
2950 {
2951 struct unit_addrs_vector addrs_vec;
2952 struct unit_addrs *addrs;
2953 size_t addrs_count;
2954 struct dwarf_data *fdata;
2955
2956 if (!build_address_map (state, base_address, dwarf_info, dwarf_info_size,
2957 dwarf_abbrev, dwarf_abbrev_size, dwarf_ranges,
2958 dwarf_ranges_size, dwarf_str, dwarf_str_size,
2959 is_bigendian, error_callback, data, &addrs_vec))
2960 return NULL;
2961
2962 if (!backtrace_vector_release (state, &addrs_vec.vec, error_callback, data))
2963 return NULL;
2964 addrs = (struct unit_addrs *) addrs_vec.vec.base;
2965 addrs_count = addrs_vec.count;
2966 backtrace_qsort (addrs, addrs_count, sizeof (struct unit_addrs),
2967 unit_addrs_compare);
2968
2969 fdata = ((struct dwarf_data *)
2970 backtrace_alloc (state, sizeof (struct dwarf_data),
2971 error_callback, data));
2972 if (fdata == NULL)
2973 return NULL;
2974
2975 fdata->next = NULL;
2976 fdata->base_address = base_address;
2977 fdata->addrs = addrs;
2978 fdata->addrs_count = addrs_count;
2979 fdata->dwarf_info = dwarf_info;
2980 fdata->dwarf_info_size = dwarf_info_size;
2981 fdata->dwarf_line = dwarf_line;
2982 fdata->dwarf_line_size = dwarf_line_size;
2983 fdata->dwarf_ranges = dwarf_ranges;
2984 fdata->dwarf_ranges_size = dwarf_ranges_size;
2985 fdata->dwarf_str = dwarf_str;
2986 fdata->dwarf_str_size = dwarf_str_size;
2987 fdata->is_bigendian = is_bigendian;
2988 memset (&fdata->fvec, 0, sizeof fdata->fvec);
2989
2990 return fdata;
2991 }
2992
2993 /* Build our data structures from the DWARF sections for a module.
2994 Set FILELINE_FN and STATE->FILELINE_DATA. Return 1 on success, 0
2995 on failure. */
2996
2997 int
2998 backtrace_dwarf_add (struct backtrace_state *state,
2999 uintptr_t base_address,
3000 const unsigned char *dwarf_info,
3001 size_t dwarf_info_size,
3002 const unsigned char *dwarf_line,
3003 size_t dwarf_line_size,
3004 const unsigned char *dwarf_abbrev,
3005 size_t dwarf_abbrev_size,
3006 const unsigned char *dwarf_ranges,
3007 size_t dwarf_ranges_size,
3008 const unsigned char *dwarf_str,
3009 size_t dwarf_str_size,
3010 int is_bigendian,
3011 backtrace_error_callback error_callback,
3012 void *data, fileline *fileline_fn)
3013 {
3014 struct dwarf_data *fdata;
3015
3016 fdata = build_dwarf_data (state, base_address, dwarf_info, dwarf_info_size,
3017 dwarf_line, dwarf_line_size, dwarf_abbrev,
3018 dwarf_abbrev_size, dwarf_ranges, dwarf_ranges_size,
3019 dwarf_str, dwarf_str_size, is_bigendian,
3020 error_callback, data);
3021 if (fdata == NULL)
3022 return 0;
3023
3024 if (!state->threaded)
3025 {
3026 struct dwarf_data **pp;
3027
3028 for (pp = (struct dwarf_data **) (void *) &state->fileline_data;
3029 *pp != NULL;
3030 pp = &(*pp)->next)
3031 ;
3032 *pp = fdata;
3033 }
3034 else
3035 {
3036 while (1)
3037 {
3038 struct dwarf_data **pp;
3039
3040 pp = (struct dwarf_data **) (void *) &state->fileline_data;
3041
3042 while (1)
3043 {
3044 struct dwarf_data *p;
3045
3046 p = backtrace_atomic_load_pointer (pp);
3047
3048 if (p == NULL)
3049 break;
3050
3051 pp = &p->next;
3052 }
3053
3054 if (__sync_bool_compare_and_swap (pp, NULL, fdata))
3055 break;
3056 }
3057 }
3058
3059 *fileline_fn = dwarf_fileline;
3060
3061 return 1;
3062 }