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