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