Bug Summary

File:build/libbacktrace/dwarf.c
Warning:line 1513, column 7
Null pointer passed to 2nd parameter expecting 'nonnull'

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -cc1 -triple x86_64-unknown-linux-gnu -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name dwarf.c -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -analyzer-config-compatibility-mode=true -mrelocation-model static -mframe-pointer=none -fmath-errno -fno-rounding-math -mconstructor-aliases -munwind-tables -target-cpu x86-64 -fno-split-dwarf-inlining -debugger-tuning=gdb -resource-dir /usr/lib64/clang/11.0.0 -D HAVE_CONFIG_H -I . -I /home/marxin/BIG/buildbot/buildworker/marxinbox-gcc-clang-static-analyzer/build/libbacktrace -I /home/marxin/BIG/buildbot/buildworker/marxinbox-gcc-clang-static-analyzer/build/libbacktrace/../include -I /home/marxin/BIG/buildbot/buildworker/marxinbox-gcc-clang-static-analyzer/build/libbacktrace/../libgcc -I ../libgcc -internal-isystem /usr/local/include -internal-isystem /usr/lib64/clang/11.0.0/include -internal-externc-isystem /include -internal-externc-isystem /usr/include -O2 -Wwrite-strings -fconst-strings -fdebug-compilation-dir /home/marxin/BIG/buildbot/buildworker/marxinbox-gcc-clang-static-analyzer/objdir/libbacktrace -ferror-limit 19 -fgnuc-version=4.2.1 -vectorize-loops -vectorize-slp -analyzer-output=plist-html -analyzer-config silence-checkers=core.NullDereference -faddrsig -o /home/marxin/BIG/buildbot/buildworker/marxinbox-gcc-clang-static-analyzer/objdir/clang-static-analyzer/2021-01-16-135054-17580-1/report-CruThc.plist -x c /home/marxin/BIG/buildbot/buildworker/marxinbox-gcc-clang-static-analyzer/build/libbacktrace/dwarf.c
1/* dwarf.c -- Get file/line information from DWARF for backtraces.
2 Copyright (C) 2012-2021 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(*__errno_location ()).h>
36#include <stdlib.h>
37#include <string.h>
38#include <sys/types.h>
39
40#include "dwarf2.h"
41#include "filenames.h"
42
43#include "backtrace.h"
44#include "internal.h"
45
46#if !defined(HAVE_DECL_STRNLEN1) || !HAVE_DECL_STRNLEN1
47
48/* If strnlen is not declared, provide our own version. */
49
50static size_t
51xstrnlen (const char *s, size_t maxlen)
52{
53 size_t i;
54
55 for (i = 0; i < maxlen; ++i)
56 if (s[i] == '\0')
57 break;
58 return i;
59}
60
61#define strnlen xstrnlen
62
63#endif
64
65/* A buffer to read DWARF info. */
66
67struct dwarf_buf
68{
69 /* Buffer name for error messages. */
70 const char *name;
71 /* Start of the buffer. */
72 const unsigned char *start;
73 /* Next byte to read. */
74 const unsigned char *buf;
75 /* The number of bytes remaining. */
76 size_t left;
77 /* Whether the data is big-endian. */
78 int is_bigendian;
79 /* Error callback routine. */
80 backtrace_error_callback error_callback;
81 /* Data for error_callback. */
82 void *data;
83 /* Non-zero if we've reported an underflow error. */
84 int reported_underflow;
85};
86
87/* A single attribute in a DWARF abbreviation. */
88
89struct attr
90{
91 /* The attribute name. */
92 enum dwarf_attribute name;
93 /* The attribute form. */
94 enum dwarf_form form;
95 /* The attribute value, for DW_FORM_implicit_const. */
96 int64_t val;
97};
98
99/* A single DWARF abbreviation. */
100
101struct abbrev
102{
103 /* The abbrev code--the number used to refer to the abbrev. */
104 uint64_t code;
105 /* The entry tag. */
106 enum dwarf_tag tag;
107 /* Non-zero if this abbrev has child entries. */
108 int has_children;
109 /* The number of attributes. */
110 size_t num_attrs;
111 /* The attributes. */
112 struct attr *attrs;
113};
114
115/* The DWARF abbreviations for a compilation unit. This structure
116 only exists while reading the compilation unit. Most DWARF readers
117 seem to a hash table to map abbrev ID's to abbrev entries.
118 However, we primarily care about GCC, and GCC simply issues ID's in
119 numerical order starting at 1. So we simply keep a sorted vector,
120 and try to just look up the code. */
121
122struct abbrevs
123{
124 /* The number of abbrevs in the vector. */
125 size_t num_abbrevs;
126 /* The abbrevs, sorted by the code field. */
127 struct abbrev *abbrevs;
128};
129
130/* The different kinds of attribute values. */
131
132enum attr_val_encoding
133{
134 /* No attribute value. */
135 ATTR_VAL_NONE,
136 /* An address. */
137 ATTR_VAL_ADDRESS,
138 /* An index into the .debug_addr section, whose value is relative to
139 * the DW_AT_addr_base attribute of the compilation unit. */
140 ATTR_VAL_ADDRESS_INDEX,
141 /* A unsigned integer. */
142 ATTR_VAL_UINT,
143 /* A sigd integer. */
144 ATTR_VAL_SINT,
145 /* A string. */
146 ATTR_VAL_STRING,
147 /* An index into the .debug_str_offsets section. */
148 ATTR_VAL_STRING_INDEX,
149 /* An offset to other data in the containing unit. */
150 ATTR_VAL_REF_UNIT,
151 /* An offset to other data within the .debug_info section. */
152 ATTR_VAL_REF_INFO,
153 /* An offset to other data within the alt .debug_info section. */
154 ATTR_VAL_REF_ALT_INFO,
155 /* An offset to data in some other section. */
156 ATTR_VAL_REF_SECTION,
157 /* A type signature. */
158 ATTR_VAL_REF_TYPE,
159 /* An index into the .debug_rnglists section. */
160 ATTR_VAL_RNGLISTS_INDEX,
161 /* A block of data (not represented). */
162 ATTR_VAL_BLOCK,
163 /* An expression (not represented). */
164 ATTR_VAL_EXPR,
165};
166
167/* An attribute value. */
168
169struct attr_val
170{
171 /* How the value is stored in the field u. */
172 enum attr_val_encoding encoding;
173 union
174 {
175 /* ATTR_VAL_ADDRESS*, ATTR_VAL_UINT, ATTR_VAL_REF*. */
176 uint64_t uint;
177 /* ATTR_VAL_SINT. */
178 int64_t sint;
179 /* ATTR_VAL_STRING. */
180 const char *string;
181 /* ATTR_VAL_BLOCK not stored. */
182 } u;
183};
184
185/* The line number program header. */
186
187struct line_header
188{
189 /* The version of the line number information. */
190 int version;
191 /* Address size. */
192 int addrsize;
193 /* The minimum instruction length. */
194 unsigned int min_insn_len;
195 /* The maximum number of ops per instruction. */
196 unsigned int max_ops_per_insn;
197 /* The line base for special opcodes. */
198 int line_base;
199 /* The line range for special opcodes. */
200 unsigned int line_range;
201 /* The opcode base--the first special opcode. */
202 unsigned int opcode_base;
203 /* Opcode lengths, indexed by opcode - 1. */
204 const unsigned char *opcode_lengths;
205 /* The number of directory entries. */
206 size_t dirs_count;
207 /* The directory entries. */
208 const char **dirs;
209 /* The number of filenames. */
210 size_t filenames_count;
211 /* The filenames. */
212 const char **filenames;
213};
214
215/* A format description from a line header. */
216
217struct line_header_format
218{
219 int lnct; /* LNCT code. */
220 enum dwarf_form form; /* Form of entry data. */
221};
222
223/* Map a single PC value to a file/line. We will keep a vector of
224 these sorted by PC value. Each file/line will be correct from the
225 PC up to the PC of the next entry if there is one. We allocate one
226 extra entry at the end so that we can use bsearch. */
227
228struct line
229{
230 /* PC. */
231 uintptr_t pc;
232 /* File name. Many entries in the array are expected to point to
233 the same file name. */
234 const char *filename;
235 /* Line number. */
236 int lineno;
237 /* Index of the object in the original array read from the DWARF
238 section, before it has been sorted. The index makes it possible
239 to use Quicksort and maintain stability. */
240 int idx;
241};
242
243/* A growable vector of line number information. This is used while
244 reading the line numbers. */
245
246struct line_vector
247{
248 /* Memory. This is an array of struct line. */
249 struct backtrace_vector vec;
250 /* Number of valid mappings. */
251 size_t count;
252};
253
254/* A function described in the debug info. */
255
256struct function
257{
258 /* The name of the function. */
259 const char *name;
260 /* If this is an inlined function, the filename of the call
261 site. */
262 const char *caller_filename;
263 /* If this is an inlined function, the line number of the call
264 site. */
265 int caller_lineno;
266 /* Map PC ranges to inlined functions. */
267 struct function_addrs *function_addrs;
268 size_t function_addrs_count;
269};
270
271/* An address range for a function. This maps a PC value to a
272 specific function. */
273
274struct function_addrs
275{
276 /* Range is LOW <= PC < HIGH. */
277 uint64_t low;
278 uint64_t high;
279 /* Function for this address range. */
280 struct function *function;
281};
282
283/* A growable vector of function address ranges. */
284
285struct function_vector
286{
287 /* Memory. This is an array of struct function_addrs. */
288 struct backtrace_vector vec;
289 /* Number of address ranges present. */
290 size_t count;
291};
292
293/* A DWARF compilation unit. This only holds the information we need
294 to map a PC to a file and line. */
295
296struct unit
297{
298 /* The first entry for this compilation unit. */
299 const unsigned char *unit_data;
300 /* The length of the data for this compilation unit. */
301 size_t unit_data_len;
302 /* The offset of UNIT_DATA from the start of the information for
303 this compilation unit. */
304 size_t unit_data_offset;
305 /* Offset of the start of the compilation unit from the start of the
306 .debug_info section. */
307 size_t low_offset;
308 /* Offset of the end of the compilation unit from the start of the
309 .debug_info section. */
310 size_t high_offset;
311 /* DWARF version. */
312 int version;
313 /* Whether unit is DWARF64. */
314 int is_dwarf64;
315 /* Address size. */
316 int addrsize;
317 /* Offset into line number information. */
318 off_t lineoff;
319 /* Offset of compilation unit in .debug_str_offsets. */
320 uint64_t str_offsets_base;
321 /* Offset of compilation unit in .debug_addr. */
322 uint64_t addr_base;
323 /* Offset of compilation unit in .debug_rnglists. */
324 uint64_t rnglists_base;
325 /* Primary source file. */
326 const char *filename;
327 /* Compilation command working directory. */
328 const char *comp_dir;
329 /* Absolute file name, only set if needed. */
330 const char *abs_filename;
331 /* The abbreviations for this unit. */
332 struct abbrevs abbrevs;
333
334 /* The fields above this point are read in during initialization and
335 may be accessed freely. The fields below this point are read in
336 as needed, and therefore require care, as different threads may
337 try to initialize them simultaneously. */
338
339 /* PC to line number mapping. This is NULL if the values have not
340 been read. This is (struct line *) -1 if there was an error
341 reading the values. */
342 struct line *lines;
343 /* Number of entries in lines. */
344 size_t lines_count;
345 /* PC ranges to function. */
346 struct function_addrs *function_addrs;
347 size_t function_addrs_count;
348};
349
350/* An address range for a compilation unit. This maps a PC value to a
351 specific compilation unit. Note that we invert the representation
352 in DWARF: instead of listing the units and attaching a list of
353 ranges, we list the ranges and have each one point to the unit.
354 This lets us do a binary search to find the unit. */
355
356struct unit_addrs
357{
358 /* Range is LOW <= PC < HIGH. */
359 uint64_t low;
360 uint64_t high;
361 /* Compilation unit for this address range. */
362 struct unit *u;
363};
364
365/* A growable vector of compilation unit address ranges. */
366
367struct unit_addrs_vector
368{
369 /* Memory. This is an array of struct unit_addrs. */
370 struct backtrace_vector vec;
371 /* Number of address ranges present. */
372 size_t count;
373};
374
375/* A growable vector of compilation unit pointer. */
376
377struct unit_vector
378{
379 struct backtrace_vector vec;
380 size_t count;
381};
382
383/* The information we need to map a PC to a file and line. */
384
385struct dwarf_data
386{
387 /* The data for the next file we know about. */
388 struct dwarf_data *next;
389 /* The data for .gnu_debugaltlink. */
390 struct dwarf_data *altlink;
391 /* The base address for this file. */
392 uintptr_t base_address;
393 /* A sorted list of address ranges. */
394 struct unit_addrs *addrs;
395 /* Number of address ranges in list. */
396 size_t addrs_count;
397 /* A sorted list of units. */
398 struct unit **units;
399 /* Number of units in the list. */
400 size_t units_count;
401 /* The unparsed DWARF debug data. */
402 struct dwarf_sections dwarf_sections;
403 /* Whether the data is big-endian or not. */
404 int is_bigendian;
405 /* A vector used for function addresses. We keep this here so that
406 we can grow the vector as we read more functions. */
407 struct function_vector fvec;
408};
409
410/* Report an error for a DWARF buffer. */
411
412static void
413dwarf_buf_error (struct dwarf_buf *buf, const char *msg)
414{
415 char b[200];
416
417 snprintf (b, sizeof b, "%s in %s at %d",
418 msg, buf->name, (int) (buf->buf - buf->start));
419 buf->error_callback (buf->data, b, 0);
420}
421
422/* Require at least COUNT bytes in BUF. Return 1 if all is well, 0 on
423 error. */
424
425static int
426require (struct dwarf_buf *buf, size_t count)
427{
428 if (buf->left >= count)
429 return 1;
430
431 if (!buf->reported_underflow)
432 {
433 dwarf_buf_error (buf, "DWARF underflow");
434 buf->reported_underflow = 1;
435 }
436
437 return 0;
438}
439
440/* Advance COUNT bytes in BUF. Return 1 if all is well, 0 on
441 error. */
442
443static int
444advance (struct dwarf_buf *buf, size_t count)
445{
446 if (!require (buf, count))
447 return 0;
448 buf->buf += count;
449 buf->left -= count;
450 return 1;
451}
452
453/* Read one zero-terminated string from BUF and advance past the string. */
454
455static const char *
456read_string (struct dwarf_buf *buf)
457{
458 const char *p = (const char *)buf->buf;
459 size_t len = strnlen (p, buf->left);
460
461 /* - If len == left, we ran out of buffer before finding the zero terminator.
462 Generate an error by advancing len + 1.
463 - If len < left, advance by len + 1 to skip past the zero terminator. */
464 size_t count = len + 1;
465
466 if (!advance (buf, count))
467 return NULL((void*)0);
468
469 return p;
470}
471
472/* Read one byte from BUF and advance 1 byte. */
473
474static unsigned char
475read_byte (struct dwarf_buf *buf)
476{
477 const unsigned char *p = buf->buf;
478
479 if (!advance (buf, 1))
480 return 0;
481 return p[0];
482}
483
484/* Read a signed char from BUF and advance 1 byte. */
485
486static signed char
487read_sbyte (struct dwarf_buf *buf)
488{
489 const unsigned char *p = buf->buf;
490
491 if (!advance (buf, 1))
492 return 0;
493 return (*p ^ 0x80) - 0x80;
494}
495
496/* Read a uint16 from BUF and advance 2 bytes. */
497
498static uint16_t
499read_uint16 (struct dwarf_buf *buf)
500{
501 const unsigned char *p = buf->buf;
502
503 if (!advance (buf, 2))
504 return 0;
505 if (buf->is_bigendian)
506 return ((uint16_t) p[0] << 8) | (uint16_t) p[1];
507 else
508 return ((uint16_t) p[1] << 8) | (uint16_t) p[0];
509}
510
511/* Read a 24 bit value from BUF and advance 3 bytes. */
512
513static uint32_t
514read_uint24 (struct dwarf_buf *buf)
515{
516 const unsigned char *p = buf->buf;
517
518 if (!advance (buf, 3))
519 return 0;
520 if (buf->is_bigendian)
521 return (((uint32_t) p[0] << 16) | ((uint32_t) p[1] << 8)
522 | (uint32_t) p[2]);
523 else
524 return (((uint32_t) p[2] << 16) | ((uint32_t) p[1] << 8)
525 | (uint32_t) p[0]);
526}
527
528/* Read a uint32 from BUF and advance 4 bytes. */
529
530static uint32_t
531read_uint32 (struct dwarf_buf *buf)
532{
533 const unsigned char *p = buf->buf;
534
535 if (!advance (buf, 4))
536 return 0;
537 if (buf->is_bigendian)
538 return (((uint32_t) p[0] << 24) | ((uint32_t) p[1] << 16)
539 | ((uint32_t) p[2] << 8) | (uint32_t) p[3]);
540 else
541 return (((uint32_t) p[3] << 24) | ((uint32_t) p[2] << 16)
542 | ((uint32_t) p[1] << 8) | (uint32_t) p[0]);
543}
544
545/* Read a uint64 from BUF and advance 8 bytes. */
546
547static uint64_t
548read_uint64 (struct dwarf_buf *buf)
549{
550 const unsigned char *p = buf->buf;
551
552 if (!advance (buf, 8))
553 return 0;
554 if (buf->is_bigendian)
555 return (((uint64_t) p[0] << 56) | ((uint64_t) p[1] << 48)
556 | ((uint64_t) p[2] << 40) | ((uint64_t) p[3] << 32)
557 | ((uint64_t) p[4] << 24) | ((uint64_t) p[5] << 16)
558 | ((uint64_t) p[6] << 8) | (uint64_t) p[7]);
559 else
560 return (((uint64_t) p[7] << 56) | ((uint64_t) p[6] << 48)
561 | ((uint64_t) p[5] << 40) | ((uint64_t) p[4] << 32)
562 | ((uint64_t) p[3] << 24) | ((uint64_t) p[2] << 16)
563 | ((uint64_t) p[1] << 8) | (uint64_t) p[0]);
564}
565
566/* Read an offset from BUF and advance the appropriate number of
567 bytes. */
568
569static uint64_t
570read_offset (struct dwarf_buf *buf, int is_dwarf64)
571{
572 if (is_dwarf64)
573 return read_uint64 (buf);
574 else
575 return read_uint32 (buf);
576}
577
578/* Read an address from BUF and advance the appropriate number of
579 bytes. */
580
581static uint64_t
582read_address (struct dwarf_buf *buf, int addrsize)
583{
584 switch (addrsize)
585 {
586 case 1:
587 return read_byte (buf);
588 case 2:
589 return read_uint16 (buf);
590 case 4:
591 return read_uint32 (buf);
592 case 8:
593 return read_uint64 (buf);
594 default:
595 dwarf_buf_error (buf, "unrecognized address size");
596 return 0;
597 }
598}
599
600/* Return whether a value is the highest possible address, given the
601 address size. */
602
603static int
604is_highest_address (uint64_t address, int addrsize)
605{
606 switch (addrsize)
607 {
608 case 1:
609 return address == (unsigned char) -1;
610 case 2:
611 return address == (uint16_t) -1;
612 case 4:
613 return address == (uint32_t) -1;
614 case 8:
615 return address == (uint64_t) -1;
616 default:
617 return 0;
618 }
619}
620
621/* Read an unsigned LEB128 number. */
622
623static uint64_t
624read_uleb128 (struct dwarf_buf *buf)
625{
626 uint64_t ret;
627 unsigned int shift;
628 int overflow;
629 unsigned char b;
630
631 ret = 0;
632 shift = 0;
633 overflow = 0;
634 do
635 {
636 const unsigned char *p;
637
638 p = buf->buf;
639 if (!advance (buf, 1))
640 return 0;
641 b = *p;
642 if (shift < 64)
643 ret |= ((uint64_t) (b & 0x7f)) << shift;
644 else if (!overflow)
645 {
646 dwarf_buf_error (buf, "LEB128 overflows uint64_t");
647 overflow = 1;
648 }
649 shift += 7;
650 }
651 while ((b & 0x80) != 0);
652
653 return ret;
654}
655
656/* Read a signed LEB128 number. */
657
658static int64_t
659read_sleb128 (struct dwarf_buf *buf)
660{
661 uint64_t val;
662 unsigned int shift;
663 int overflow;
664 unsigned char b;
665
666 val = 0;
667 shift = 0;
668 overflow = 0;
669 do
670 {
671 const unsigned char *p;
672
673 p = buf->buf;
674 if (!advance (buf, 1))
675 return 0;
676 b = *p;
677 if (shift < 64)
678 val |= ((uint64_t) (b & 0x7f)) << shift;
679 else if (!overflow)
680 {
681 dwarf_buf_error (buf, "signed LEB128 overflows uint64_t");
682 overflow = 1;
683 }
684 shift += 7;
685 }
686 while ((b & 0x80) != 0);
687
688 if ((b & 0x40) != 0 && shift < 64)
689 val |= ((uint64_t) -1) << shift;
690
691 return (int64_t) val;
692}
693
694/* Return the length of an LEB128 number. */
695
696static size_t
697leb128_len (const unsigned char *p)
698{
699 size_t ret;
700
701 ret = 1;
702 while ((*p & 0x80) != 0)
703 {
704 ++p;
705 ++ret;
706 }
707 return ret;
708}
709
710/* Read initial_length from BUF and advance the appropriate number of bytes. */
711
712static uint64_t
713read_initial_length (struct dwarf_buf *buf, int *is_dwarf64)
714{
715 uint64_t len;
716
717 len = read_uint32 (buf);
718 if (len == 0xffffffff)
719 {
720 len = read_uint64 (buf);
721 *is_dwarf64 = 1;
722 }
723 else
724 *is_dwarf64 = 0;
725
726 return len;
727}
728
729/* Free an abbreviations structure. */
730
731static void
732free_abbrevs (struct backtrace_state *state, struct abbrevs *abbrevs,
733 backtrace_error_callback error_callback, void *data)
734{
735 size_t i;
736
737 for (i = 0; i < abbrevs->num_abbrevs; ++i)
738 backtrace_free (state, abbrevs->abbrevs[i].attrs,
739 abbrevs->abbrevs[i].num_attrs * sizeof (struct attr),
740 error_callback, data);
741 backtrace_free (state, abbrevs->abbrevs,
742 abbrevs->num_abbrevs * sizeof (struct abbrev),
743 error_callback, data);
744 abbrevs->num_abbrevs = 0;
745 abbrevs->abbrevs = NULL((void*)0);
746}
747
748/* Read an attribute value. Returns 1 on success, 0 on failure. If
749 the value can be represented as a uint64_t, sets *VAL and sets
750 *IS_VALID to 1. We don't try to store the value of other attribute
751 forms, because we don't care about them. */
752
753static int
754read_attribute (enum dwarf_form form, uint64_t implicit_val,
755 struct dwarf_buf *buf, int is_dwarf64, int version,
756 int addrsize, const struct dwarf_sections *dwarf_sections,
757 struct dwarf_data *altlink, struct attr_val *val)
758{
759 /* Avoid warnings about val.u.FIELD may be used uninitialized if
760 this function is inlined. The warnings aren't valid but can
761 occur because the different fields are set and used
762 conditionally. */
763 memset (val, 0, sizeof *val);
764
765 switch (form)
766 {
767 case DW_FORM_addr:
768 val->encoding = ATTR_VAL_ADDRESS;
769 val->u.uint = read_address (buf, addrsize);
770 return 1;
771 case DW_FORM_block2:
772 val->encoding = ATTR_VAL_BLOCK;
773 return advance (buf, read_uint16 (buf));
774 case DW_FORM_block4:
775 val->encoding = ATTR_VAL_BLOCK;
776 return advance (buf, read_uint32 (buf));
777 case DW_FORM_data2:
778 val->encoding = ATTR_VAL_UINT;
779 val->u.uint = read_uint16 (buf);
780 return 1;
781 case DW_FORM_data4:
782 val->encoding = ATTR_VAL_UINT;
783 val->u.uint = read_uint32 (buf);
784 return 1;
785 case DW_FORM_data8:
786 val->encoding = ATTR_VAL_UINT;
787 val->u.uint = read_uint64 (buf);
788 return 1;
789 case DW_FORM_data16:
790 val->encoding = ATTR_VAL_BLOCK;
791 return advance (buf, 16);
792 case DW_FORM_string:
793 val->encoding = ATTR_VAL_STRING;
794 val->u.string = read_string (buf);
795 return val->u.string == NULL((void*)0) ? 0 : 1;
796 case DW_FORM_block:
797 val->encoding = ATTR_VAL_BLOCK;
798 return advance (buf, read_uleb128 (buf));
799 case DW_FORM_block1:
800 val->encoding = ATTR_VAL_BLOCK;
801 return advance (buf, read_byte (buf));
802 case DW_FORM_data1:
803 val->encoding = ATTR_VAL_UINT;
804 val->u.uint = read_byte (buf);
805 return 1;
806 case DW_FORM_flag:
807 val->encoding = ATTR_VAL_UINT;
808 val->u.uint = read_byte (buf);
809 return 1;
810 case DW_FORM_sdata:
811 val->encoding = ATTR_VAL_SINT;
812 val->u.sint = read_sleb128 (buf);
813 return 1;
814 case DW_FORM_strp:
815 {
816 uint64_t offset;
817
818 offset = read_offset (buf, is_dwarf64);
819 if (offset >= dwarf_sections->size[DEBUG_STR])
820 {
821 dwarf_buf_error (buf, "DW_FORM_strp out of range");
822 return 0;
823 }
824 val->encoding = ATTR_VAL_STRING;
825 val->u.string =
826 (const char *) dwarf_sections->data[DEBUG_STR] + offset;
827 return 1;
828 }
829 case DW_FORM_line_strp:
830 {
831 uint64_t offset;
832
833 offset = read_offset (buf, is_dwarf64);
834 if (offset >= dwarf_sections->size[DEBUG_LINE_STR])
835 {
836 dwarf_buf_error (buf, "DW_FORM_line_strp out of range");
837 return 0;
838 }
839 val->encoding = ATTR_VAL_STRING;
840 val->u.string =
841 (const char *) dwarf_sections->data[DEBUG_LINE_STR] + offset;
842 return 1;
843 }
844 case DW_FORM_udata:
845 val->encoding = ATTR_VAL_UINT;
846 val->u.uint = read_uleb128 (buf);
847 return 1;
848 case DW_FORM_ref_addr:
849 val->encoding = ATTR_VAL_REF_INFO;
850 if (version == 2)
851 val->u.uint = read_address (buf, addrsize);
852 else
853 val->u.uint = read_offset (buf, is_dwarf64);
854 return 1;
855 case DW_FORM_ref1:
856 val->encoding = ATTR_VAL_REF_UNIT;
857 val->u.uint = read_byte (buf);
858 return 1;
859 case DW_FORM_ref2:
860 val->encoding = ATTR_VAL_REF_UNIT;
861 val->u.uint = read_uint16 (buf);
862 return 1;
863 case DW_FORM_ref4:
864 val->encoding = ATTR_VAL_REF_UNIT;
865 val->u.uint = read_uint32 (buf);
866 return 1;
867 case DW_FORM_ref8:
868 val->encoding = ATTR_VAL_REF_UNIT;
869 val->u.uint = read_uint64 (buf);
870 return 1;
871 case DW_FORM_ref_udata:
872 val->encoding = ATTR_VAL_REF_UNIT;
873 val->u.uint = read_uleb128 (buf);
874 return 1;
875 case DW_FORM_indirect:
876 {
877 uint64_t form;
878
879 form = read_uleb128 (buf);
880 if (form == DW_FORM_implicit_const)
881 {
882 dwarf_buf_error (buf,
883 "DW_FORM_indirect to DW_FORM_implicit_const");
884 return 0;
885 }
886 return read_attribute ((enum dwarf_form) form, 0, buf, is_dwarf64,
887 version, addrsize, dwarf_sections, altlink,
888 val);
889 }
890 case DW_FORM_sec_offset:
891 val->encoding = ATTR_VAL_REF_SECTION;
892 val->u.uint = read_offset (buf, is_dwarf64);
893 return 1;
894 case DW_FORM_exprloc:
895 val->encoding = ATTR_VAL_EXPR;
896 return advance (buf, read_uleb128 (buf));
897 case DW_FORM_flag_present:
898 val->encoding = ATTR_VAL_UINT;
899 val->u.uint = 1;
900 return 1;
901 case DW_FORM_ref_sig8:
902 val->encoding = ATTR_VAL_REF_TYPE;
903 val->u.uint = read_uint64 (buf);
904 return 1;
905 case DW_FORM_strx: case DW_FORM_strx1: case DW_FORM_strx2:
906 case DW_FORM_strx3: case DW_FORM_strx4:
907 {
908 uint64_t offset;
909
910 switch (form)
911 {
912 case DW_FORM_strx:
913 offset = read_uleb128 (buf);
914 break;
915 case DW_FORM_strx1:
916 offset = read_byte (buf);
917 break;
918 case DW_FORM_strx2:
919 offset = read_uint16 (buf);
920 break;
921 case DW_FORM_strx3:
922 offset = read_uint24 (buf);
923 break;
924 case DW_FORM_strx4:
925 offset = read_uint32 (buf);
926 break;
927 default:
928 /* This case can't happen. */
929 return 0;
930 }
931 val->encoding = ATTR_VAL_STRING_INDEX;
932 val->u.uint = offset;
933 return 1;
934 }
935 case DW_FORM_addrx: case DW_FORM_addrx1: case DW_FORM_addrx2:
936 case DW_FORM_addrx3: case DW_FORM_addrx4:
937 {
938 uint64_t offset;
939
940 switch (form)
941 {
942 case DW_FORM_addrx:
943 offset = read_uleb128 (buf);
944 break;
945 case DW_FORM_addrx1:
946 offset = read_byte (buf);
947 break;
948 case DW_FORM_addrx2:
949 offset = read_uint16 (buf);
950 break;
951 case DW_FORM_addrx3:
952 offset = read_uint24 (buf);
953 break;
954 case DW_FORM_addrx4:
955 offset = read_uint32 (buf);
956 break;
957 default:
958 /* This case can't happen. */
959 return 0;
960 }
961 val->encoding = ATTR_VAL_ADDRESS_INDEX;
962 val->u.uint = offset;
963 return 1;
964 }
965 case DW_FORM_ref_sup4:
966 val->encoding = ATTR_VAL_REF_SECTION;
967 val->u.uint = read_uint32 (buf);
968 return 1;
969 case DW_FORM_ref_sup8:
970 val->encoding = ATTR_VAL_REF_SECTION;
971 val->u.uint = read_uint64 (buf);
972 return 1;
973 case DW_FORM_implicit_const:
974 val->encoding = ATTR_VAL_UINT;
975 val->u.uint = implicit_val;
976 return 1;
977 case DW_FORM_loclistx:
978 /* We don't distinguish this from DW_FORM_sec_offset. It
979 * shouldn't matter since we don't care about loclists. */
980 val->encoding = ATTR_VAL_REF_SECTION;
981 val->u.uint = read_uleb128 (buf);
982 return 1;
983 case DW_FORM_rnglistx:
984 val->encoding = ATTR_VAL_RNGLISTS_INDEX;
985 val->u.uint = read_uleb128 (buf);
986 return 1;
987 case DW_FORM_GNU_addr_index:
988 val->encoding = ATTR_VAL_REF_SECTION;
989 val->u.uint = read_uleb128 (buf);
990 return 1;
991 case DW_FORM_GNU_str_index:
992 val->encoding = ATTR_VAL_REF_SECTION;
993 val->u.uint = read_uleb128 (buf);
994 return 1;
995 case DW_FORM_GNU_ref_alt:
996 val->u.uint = read_offset (buf, is_dwarf64);
997 if (altlink == NULL((void*)0))
998 {
999 val->encoding = ATTR_VAL_NONE;
1000 return 1;
1001 }
1002 val->encoding = ATTR_VAL_REF_ALT_INFO;
1003 return 1;
1004 case DW_FORM_strp_sup: case DW_FORM_GNU_strp_alt:
1005 {
1006 uint64_t offset;
1007
1008 offset = read_offset (buf, is_dwarf64);
1009 if (altlink == NULL((void*)0))
1010 {
1011 val->encoding = ATTR_VAL_NONE;
1012 return 1;
1013 }
1014 if (offset >= altlink->dwarf_sections.size[DEBUG_STR])
1015 {
1016 dwarf_buf_error (buf, "DW_FORM_strp_sup out of range");
1017 return 0;
1018 }
1019 val->encoding = ATTR_VAL_STRING;
1020 val->u.string =
1021 (const char *) altlink->dwarf_sections.data[DEBUG_STR] + offset;
1022 return 1;
1023 }
1024 default:
1025 dwarf_buf_error (buf, "unrecognized DWARF form");
1026 return 0;
1027 }
1028}
1029
1030/* If we can determine the value of a string attribute, set *STRING to
1031 point to the string. Return 1 on success, 0 on error. If we don't
1032 know the value, we consider that a success, and we don't change
1033 *STRING. An error is only reported for some sort of out of range
1034 offset. */
1035
1036static int
1037resolve_string (const struct dwarf_sections *dwarf_sections, int is_dwarf64,
1038 int is_bigendian, uint64_t str_offsets_base,
1039 const struct attr_val *val,
1040 backtrace_error_callback error_callback, void *data,
1041 const char **string)
1042{
1043 switch (val->encoding)
1044 {
1045 case ATTR_VAL_STRING:
1046 *string = val->u.string;
1047 return 1;
1048
1049 case ATTR_VAL_STRING_INDEX:
1050 {
1051 uint64_t offset;
1052 struct dwarf_buf offset_buf;
1053
1054 offset = val->u.uint * (is_dwarf64 ? 8 : 4) + str_offsets_base;
1055 if (offset + (is_dwarf64 ? 8 : 4)
1056 > dwarf_sections->size[DEBUG_STR_OFFSETS])
1057 {
1058 error_callback (data, "DW_FORM_strx value out of range", 0);
1059 return 0;
1060 }
1061
1062 offset_buf.name = ".debug_str_offsets";
1063 offset_buf.start = dwarf_sections->data[DEBUG_STR_OFFSETS];
1064 offset_buf.buf = dwarf_sections->data[DEBUG_STR_OFFSETS] + offset;
1065 offset_buf.left = dwarf_sections->size[DEBUG_STR_OFFSETS] - offset;
1066 offset_buf.is_bigendian = is_bigendian;
1067 offset_buf.error_callback = error_callback;
1068 offset_buf.data = data;
1069 offset_buf.reported_underflow = 0;
1070
1071 offset = read_offset (&offset_buf, is_dwarf64);
1072 if (offset >= dwarf_sections->size[DEBUG_STR])
1073 {
1074 dwarf_buf_error (&offset_buf, "DW_FORM_strx offset out of range");
1075 return 0;
1076 }
1077 *string = (const char *) dwarf_sections->data[DEBUG_STR] + offset;
1078 return 1;
1079 }
1080
1081 default:
1082 return 1;
1083 }
1084}
1085
1086/* Set *ADDRESS to the real address for a ATTR_VAL_ADDRESS_INDEX.
1087 Return 1 on success, 0 on error. */
1088
1089static int
1090resolve_addr_index (const struct dwarf_sections *dwarf_sections,
1091 uint64_t addr_base, int addrsize, int is_bigendian,
1092 uint64_t addr_index,
1093 backtrace_error_callback error_callback, void *data,
1094 uint64_t *address)
1095{
1096 uint64_t offset;
1097 struct dwarf_buf addr_buf;
1098
1099 offset = addr_index * addrsize + addr_base;
1100 if (offset + addrsize > dwarf_sections->size[DEBUG_ADDR])
1101 {
1102 error_callback (data, "DW_FORM_addrx value out of range", 0);
1103 return 0;
1104 }
1105
1106 addr_buf.name = ".debug_addr";
1107 addr_buf.start = dwarf_sections->data[DEBUG_ADDR];
1108 addr_buf.buf = dwarf_sections->data[DEBUG_ADDR] + offset;
1109 addr_buf.left = dwarf_sections->size[DEBUG_ADDR] - offset;
1110 addr_buf.is_bigendian = is_bigendian;
1111 addr_buf.error_callback = error_callback;
1112 addr_buf.data = data;
1113 addr_buf.reported_underflow = 0;
1114
1115 *address = read_address (&addr_buf, addrsize);
1116 return 1;
1117}
1118
1119/* Compare a unit offset against a unit for bsearch. */
1120
1121static int
1122units_search (const void *vkey, const void *ventry)
1123{
1124 const size_t *key = (const size_t *) vkey;
1125 const struct unit *entry = *((const struct unit *const *) ventry);
1126 size_t offset;
1127
1128 offset = *key;
1129 if (offset < entry->low_offset)
1130 return -1;
1131 else if (offset >= entry->high_offset)
1132 return 1;
1133 else
1134 return 0;
1135}
1136
1137/* Find a unit in PU containing OFFSET. */
1138
1139static struct unit *
1140find_unit (struct unit **pu, size_t units_count, size_t offset)
1141{
1142 struct unit **u;
1143 u = bsearch (&offset, pu, units_count, sizeof (struct unit *), units_search);
1144 return u == NULL((void*)0) ? NULL((void*)0) : *u;
1145}
1146
1147/* Compare function_addrs for qsort. When ranges are nested, make the
1148 smallest one sort last. */
1149
1150static int
1151function_addrs_compare (const void *v1, const void *v2)
1152{
1153 const struct function_addrs *a1 = (const struct function_addrs *) v1;
1154 const struct function_addrs *a2 = (const struct function_addrs *) v2;
1155
1156 if (a1->low < a2->low)
1157 return -1;
1158 if (a1->low > a2->low)
1159 return 1;
1160 if (a1->high < a2->high)
1161 return 1;
1162 if (a1->high > a2->high)
1163 return -1;
1164 return strcmp (a1->function->name, a2->function->name);
1165}
1166
1167/* Compare a PC against a function_addrs for bsearch. We always
1168 allocate an entra entry at the end of the vector, so that this
1169 routine can safely look at the next entry. Note that if there are
1170 multiple ranges containing PC, which one will be returned is
1171 unpredictable. We compensate for that in dwarf_fileline. */
1172
1173static int
1174function_addrs_search (const void *vkey, const void *ventry)
1175{
1176 const uintptr_t *key = (const uintptr_t *) vkey;
1177 const struct function_addrs *entry = (const struct function_addrs *) ventry;
1178 uintptr_t pc;
1179
1180 pc = *key;
1181 if (pc < entry->low)
1182 return -1;
1183 else if (pc > (entry + 1)->low)
1184 return 1;
1185 else
1186 return 0;
1187}
1188
1189/* Add a new compilation unit address range to a vector. This is
1190 called via add_ranges. Returns 1 on success, 0 on failure. */
1191
1192static int
1193add_unit_addr (struct backtrace_state *state, void *rdata,
1194 uint64_t lowpc, uint64_t highpc,
1195 backtrace_error_callback error_callback, void *data,
1196 void *pvec)
1197{
1198 struct unit *u = (struct unit *) rdata;
1199 struct unit_addrs_vector *vec = (struct unit_addrs_vector *) pvec;
1200 struct unit_addrs *p;
1201
1202 /* Try to merge with the last entry. */
1203 if (vec->count > 0)
1204 {
1205 p = (struct unit_addrs *) vec->vec.base + (vec->count - 1);
1206 if ((lowpc == p->high || lowpc == p->high + 1)
1207 && u == p->u)
1208 {
1209 if (highpc > p->high)
1210 p->high = highpc;
1211 return 1;
1212 }
1213 }
1214
1215 p = ((struct unit_addrs *)
1216 backtrace_vector_grow (state, sizeof (struct unit_addrs),
1217 error_callback, data, &vec->vec));
1218 if (p == NULL((void*)0))
1219 return 0;
1220
1221 p->low = lowpc;
1222 p->high = highpc;
1223 p->u = u;
1224
1225 ++vec->count;
1226
1227 return 1;
1228}
1229
1230/* Compare unit_addrs for qsort. When ranges are nested, make the
1231 smallest one sort last. */
1232
1233static int
1234unit_addrs_compare (const void *v1, const void *v2)
1235{
1236 const struct unit_addrs *a1 = (const struct unit_addrs *) v1;
1237 const struct unit_addrs *a2 = (const struct unit_addrs *) v2;
1238
1239 if (a1->low < a2->low)
1240 return -1;
1241 if (a1->low > a2->low)
1242 return 1;
1243 if (a1->high < a2->high)
1244 return 1;
1245 if (a1->high > a2->high)
1246 return -1;
1247 if (a1->u->lineoff < a2->u->lineoff)
1248 return -1;
1249 if (a1->u->lineoff > a2->u->lineoff)
1250 return 1;
1251 return 0;
1252}
1253
1254/* Compare a PC against a unit_addrs for bsearch. We always allocate
1255 an entry entry at the end of the vector, so that this routine can
1256 safely look at the next entry. Note that if there are multiple
1257 ranges containing PC, which one will be returned is unpredictable.
1258 We compensate for that in dwarf_fileline. */
1259
1260static int
1261unit_addrs_search (const void *vkey, const void *ventry)
1262{
1263 const uintptr_t *key = (const uintptr_t *) vkey;
1264 const struct unit_addrs *entry = (const struct unit_addrs *) ventry;
1265 uintptr_t pc;
1266
1267 pc = *key;
1268 if (pc < entry->low)
1269 return -1;
1270 else if (pc > (entry + 1)->low)
1271 return 1;
1272 else
1273 return 0;
1274}
1275
1276/* Sort the line vector by PC. We want a stable sort here to maintain
1277 the order of lines for the same PC values. Since the sequence is
1278 being sorted in place, their addresses cannot be relied on to
1279 maintain stability. That is the purpose of the index member. */
1280
1281static int
1282line_compare (const void *v1, const void *v2)
1283{
1284 const struct line *ln1 = (const struct line *) v1;
1285 const struct line *ln2 = (const struct line *) v2;
1286
1287 if (ln1->pc < ln2->pc)
1288 return -1;
1289 else if (ln1->pc > ln2->pc)
1290 return 1;
1291 else if (ln1->idx < ln2->idx)
1292 return -1;
1293 else if (ln1->idx > ln2->idx)
1294 return 1;
1295 else
1296 return 0;
1297}
1298
1299/* Find a PC in a line vector. We always allocate an extra entry at
1300 the end of the lines vector, so that this routine can safely look
1301 at the next entry. Note that when there are multiple mappings for
1302 the same PC value, this will return the last one. */
1303
1304static int
1305line_search (const void *vkey, const void *ventry)
1306{
1307 const uintptr_t *key = (const uintptr_t *) vkey;
1308 const struct line *entry = (const struct line *) ventry;
1309 uintptr_t pc;
1310
1311 pc = *key;
1312 if (pc < entry->pc)
1313 return -1;
1314 else if (pc >= (entry + 1)->pc)
1315 return 1;
1316 else
1317 return 0;
1318}
1319
1320/* Sort the abbrevs by the abbrev code. This function is passed to
1321 both qsort and bsearch. */
1322
1323static int
1324abbrev_compare (const void *v1, const void *v2)
1325{
1326 const struct abbrev *a1 = (const struct abbrev *) v1;
1327 const struct abbrev *a2 = (const struct abbrev *) v2;
1328
1329 if (a1->code < a2->code)
1330 return -1;
1331 else if (a1->code > a2->code)
1332 return 1;
1333 else
1334 {
1335 /* This really shouldn't happen. It means there are two
1336 different abbrevs with the same code, and that means we don't
1337 know which one lookup_abbrev should return. */
1338 return 0;
1339 }
1340}
1341
1342/* Read the abbreviation table for a compilation unit. Returns 1 on
1343 success, 0 on failure. */
1344
1345static int
1346read_abbrevs (struct backtrace_state *state, uint64_t abbrev_offset,
1347 const unsigned char *dwarf_abbrev, size_t dwarf_abbrev_size,
1348 int is_bigendian, backtrace_error_callback error_callback,
1349 void *data, struct abbrevs *abbrevs)
1350{
1351 struct dwarf_buf abbrev_buf;
1352 struct dwarf_buf count_buf;
1353 size_t num_abbrevs;
1354
1355 abbrevs->num_abbrevs = 0;
1356 abbrevs->abbrevs = NULL((void*)0);
21
Null pointer value stored to field 'abbrevs'
1357
1358 if (abbrev_offset >= dwarf_abbrev_size)
22
Assuming 'abbrev_offset' is < 'dwarf_abbrev_size'
23
Taking false branch
1359 {
1360 error_callback (data, "abbrev offset out of range", 0);
1361 return 0;
1362 }
1363
1364 abbrev_buf.name = ".debug_abbrev";
1365 abbrev_buf.start = dwarf_abbrev;
1366 abbrev_buf.buf = dwarf_abbrev + abbrev_offset;
1367 abbrev_buf.left = dwarf_abbrev_size - abbrev_offset;
1368 abbrev_buf.is_bigendian = is_bigendian;
1369 abbrev_buf.error_callback = error_callback;
1370 abbrev_buf.data = data;
1371 abbrev_buf.reported_underflow = 0;
1372
1373 /* Count the number of abbrevs in this list. */
1374
1375 count_buf = abbrev_buf;
1376 num_abbrevs = 0;
1377 while (read_uleb128 (&count_buf) != 0)
24
Loop condition is false. Execution continues on line 1399
1378 {
1379 if (count_buf.reported_underflow)
1380 return 0;
1381 ++num_abbrevs;
1382 // Skip tag.
1383 read_uleb128 (&count_buf);
1384 // Skip has_children.
1385 read_byte (&count_buf);
1386 // Skip attributes.
1387 while (read_uleb128 (&count_buf) != 0)
1388 {
1389 uint64_t form;
1390
1391 form = read_uleb128 (&count_buf);
1392 if ((enum dwarf_form) form == DW_FORM_implicit_const)
1393 read_sleb128 (&count_buf);
1394 }
1395 // Skip form of last attribute.
1396 read_uleb128 (&count_buf);
1397 }
1398
1399 if (count_buf.reported_underflow)
25
Assuming field 'reported_underflow' is 0
26
Taking false branch
1400 return 0;
1401
1402 if (num_abbrevs
26.1
'num_abbrevs' is equal to 0
== 0)
27
Taking true branch
1403 return 1;
1404
1405 abbrevs->abbrevs = ((struct abbrev *)
1406 backtrace_alloc (state,
1407 num_abbrevs * sizeof (struct abbrev),
1408 error_callback, data));
1409 if (abbrevs->abbrevs == NULL((void*)0))
1410 return 0;
1411 abbrevs->num_abbrevs = num_abbrevs;
1412 memset (abbrevs->abbrevs, 0, num_abbrevs * sizeof (struct abbrev));
1413
1414 num_abbrevs = 0;
1415 while (1)
1416 {
1417 uint64_t code;
1418 struct abbrev a;
1419 size_t num_attrs;
1420 struct attr *attrs;
1421
1422 if (abbrev_buf.reported_underflow)
1423 goto fail;
1424
1425 code = read_uleb128 (&abbrev_buf);
1426 if (code == 0)
1427 break;
1428
1429 a.code = code;
1430 a.tag = (enum dwarf_tag) read_uleb128 (&abbrev_buf);
1431 a.has_children = read_byte (&abbrev_buf);
1432
1433 count_buf = abbrev_buf;
1434 num_attrs = 0;
1435 while (read_uleb128 (&count_buf) != 0)
1436 {
1437 uint64_t form;
1438
1439 ++num_attrs;
1440 form = read_uleb128 (&count_buf);
1441 if ((enum dwarf_form) form == DW_FORM_implicit_const)
1442 read_sleb128 (&count_buf);
1443 }
1444
1445 if (num_attrs == 0)
1446 {
1447 attrs = NULL((void*)0);
1448 read_uleb128 (&abbrev_buf);
1449 read_uleb128 (&abbrev_buf);
1450 }
1451 else
1452 {
1453 attrs = ((struct attr *)
1454 backtrace_alloc (state, num_attrs * sizeof *attrs,
1455 error_callback, data));
1456 if (attrs == NULL((void*)0))
1457 goto fail;
1458 num_attrs = 0;
1459 while (1)
1460 {
1461 uint64_t name;
1462 uint64_t form;
1463
1464 name = read_uleb128 (&abbrev_buf);
1465 form = read_uleb128 (&abbrev_buf);
1466 if (name == 0)
1467 break;
1468 attrs[num_attrs].name = (enum dwarf_attribute) name;
1469 attrs[num_attrs].form = (enum dwarf_form) form;
1470 if ((enum dwarf_form) form == DW_FORM_implicit_const)
1471 attrs[num_attrs].val = read_sleb128 (&abbrev_buf);
1472 else
1473 attrs[num_attrs].val = 0;
1474 ++num_attrs;
1475 }
1476 }
1477
1478 a.num_attrs = num_attrs;
1479 a.attrs = attrs;
1480
1481 abbrevs->abbrevs[num_abbrevs] = a;
1482 ++num_abbrevs;
1483 }
1484
1485 backtrace_qsort (abbrevs->abbrevs, abbrevs->num_abbrevs,
1486 sizeof (struct abbrev), abbrev_compare);
1487
1488 return 1;
1489
1490 fail:
1491 free_abbrevs (state, abbrevs, error_callback, data);
1492 return 0;
1493}
1494
1495/* Return the abbrev information for an abbrev code. */
1496
1497static const struct abbrev *
1498lookup_abbrev (struct abbrevs *abbrevs, uint64_t code,
1499 backtrace_error_callback error_callback, void *data)
1500{
1501 struct abbrev key;
1502 void *p;
1503
1504 /* With GCC, where abbrevs are simply numbered in order, we should
1505 be able to just look up the entry. */
1506 if (code - 1 < abbrevs->num_abbrevs
1507 && abbrevs->abbrevs[code - 1].code == code)
1508 return &abbrevs->abbrevs[code - 1];
1509
1510 /* Otherwise we have to search. */
1511 memset (&key, 0, sizeof key);
1512 key.code = code;
1513 p = bsearch (&key, abbrevs->abbrevs, abbrevs->num_abbrevs,
40
Null pointer passed to 2nd parameter expecting 'nonnull'
1514 sizeof (struct abbrev), abbrev_compare);
1515 if (p == NULL((void*)0))
1516 {
1517 error_callback (data, "invalid abbreviation code", 0);
1518 return NULL((void*)0);
1519 }
1520 return (const struct abbrev *) p;
1521}
1522
1523/* This struct is used to gather address range information while
1524 reading attributes. We use this while building a mapping from
1525 address ranges to compilation units and then again while mapping
1526 from address ranges to function entries. Normally either
1527 lowpc/highpc is set or ranges is set. */
1528
1529struct pcrange {
1530 uint64_t lowpc; /* The low PC value. */
1531 int have_lowpc; /* Whether a low PC value was found. */
1532 int lowpc_is_addr_index; /* Whether lowpc is in .debug_addr. */
1533 uint64_t highpc; /* The high PC value. */
1534 int have_highpc; /* Whether a high PC value was found. */
1535 int highpc_is_relative; /* Whether highpc is relative to lowpc. */
1536 int highpc_is_addr_index; /* Whether highpc is in .debug_addr. */
1537 uint64_t ranges; /* Offset in ranges section. */
1538 int have_ranges; /* Whether ranges is valid. */
1539 int ranges_is_index; /* Whether ranges is DW_FORM_rnglistx. */
1540};
1541
1542/* Update PCRANGE from an attribute value. */
1543
1544static void
1545update_pcrange (const struct attr* attr, const struct attr_val* val,
1546 struct pcrange *pcrange)
1547{
1548 switch (attr->name)
1549 {
1550 case DW_AT_low_pc:
1551 if (val->encoding == ATTR_VAL_ADDRESS)
1552 {
1553 pcrange->lowpc = val->u.uint;
1554 pcrange->have_lowpc = 1;
1555 }
1556 else if (val->encoding == ATTR_VAL_ADDRESS_INDEX)
1557 {
1558 pcrange->lowpc = val->u.uint;
1559 pcrange->have_lowpc = 1;
1560 pcrange->lowpc_is_addr_index = 1;
1561 }
1562 break;
1563
1564 case DW_AT_high_pc:
1565 if (val->encoding == ATTR_VAL_ADDRESS)
1566 {
1567 pcrange->highpc = val->u.uint;
1568 pcrange->have_highpc = 1;
1569 }
1570 else if (val->encoding == ATTR_VAL_UINT)
1571 {
1572 pcrange->highpc = val->u.uint;
1573 pcrange->have_highpc = 1;
1574 pcrange->highpc_is_relative = 1;
1575 }
1576 else if (val->encoding == ATTR_VAL_ADDRESS_INDEX)
1577 {
1578 pcrange->highpc = val->u.uint;
1579 pcrange->have_highpc = 1;
1580 pcrange->highpc_is_addr_index = 1;
1581 }
1582 break;
1583
1584 case DW_AT_ranges:
1585 if (val->encoding == ATTR_VAL_UINT
1586 || val->encoding == ATTR_VAL_REF_SECTION)
1587 {
1588 pcrange->ranges = val->u.uint;
1589 pcrange->have_ranges = 1;
1590 }
1591 else if (val->encoding == ATTR_VAL_RNGLISTS_INDEX)
1592 {
1593 pcrange->ranges = val->u.uint;
1594 pcrange->have_ranges = 1;
1595 pcrange->ranges_is_index = 1;
1596 }
1597 break;
1598
1599 default:
1600 break;
1601 }
1602}
1603
1604/* Call ADD_RANGE for a low/high PC pair. Returns 1 on success, 0 on
1605 error. */
1606
1607static int
1608add_low_high_range (struct backtrace_state *state,
1609 const struct dwarf_sections *dwarf_sections,
1610 uintptr_t base_address, int is_bigendian,
1611 struct unit *u, const struct pcrange *pcrange,
1612 int (*add_range) (struct backtrace_state *state,
1613 void *rdata, uint64_t lowpc,
1614 uint64_t highpc,
1615 backtrace_error_callback error_callback,
1616 void *data, void *vec),
1617 void *rdata,
1618 backtrace_error_callback error_callback, void *data,
1619 void *vec)
1620{
1621 uint64_t lowpc;
1622 uint64_t highpc;
1623
1624 lowpc = pcrange->lowpc;
1625 if (pcrange->lowpc_is_addr_index)
1626 {
1627 if (!resolve_addr_index (dwarf_sections, u->addr_base, u->addrsize,
1628 is_bigendian, lowpc, error_callback, data,
1629 &lowpc))
1630 return 0;
1631 }
1632
1633 highpc = pcrange->highpc;
1634 if (pcrange->highpc_is_addr_index)
1635 {
1636 if (!resolve_addr_index (dwarf_sections, u->addr_base, u->addrsize,
1637 is_bigendian, highpc, error_callback, data,
1638 &highpc))
1639 return 0;
1640 }
1641 if (pcrange->highpc_is_relative)
1642 highpc += lowpc;
1643
1644 /* Add in the base address of the module when recording PC values,
1645 so that we can look up the PC directly. */
1646 lowpc += base_address;
1647 highpc += base_address;
1648
1649 return add_range (state, rdata, lowpc, highpc, error_callback, data, vec);
1650}
1651
1652/* Call ADD_RANGE for each range read from .debug_ranges, as used in
1653 DWARF versions 2 through 4. */
1654
1655static int
1656add_ranges_from_ranges (
1657 struct backtrace_state *state,
1658 const struct dwarf_sections *dwarf_sections,
1659 uintptr_t base_address, int is_bigendian,
1660 struct unit *u, uint64_t base,
1661 const struct pcrange *pcrange,
1662 int (*add_range) (struct backtrace_state *state, void *rdata,
1663 uint64_t lowpc, uint64_t highpc,
1664 backtrace_error_callback error_callback, void *data,
1665 void *vec),
1666 void *rdata,
1667 backtrace_error_callback error_callback, void *data,
1668 void *vec)
1669{
1670 struct dwarf_buf ranges_buf;
1671
1672 if (pcrange->ranges >= dwarf_sections->size[DEBUG_RANGES])
1673 {
1674 error_callback (data, "ranges offset out of range", 0);
1675 return 0;
1676 }
1677
1678 ranges_buf.name = ".debug_ranges";
1679 ranges_buf.start = dwarf_sections->data[DEBUG_RANGES];
1680 ranges_buf.buf = dwarf_sections->data[DEBUG_RANGES] + pcrange->ranges;
1681 ranges_buf.left = dwarf_sections->size[DEBUG_RANGES] - pcrange->ranges;
1682 ranges_buf.is_bigendian = is_bigendian;
1683 ranges_buf.error_callback = error_callback;
1684 ranges_buf.data = data;
1685 ranges_buf.reported_underflow = 0;
1686
1687 while (1)
1688 {
1689 uint64_t low;
1690 uint64_t high;
1691
1692 if (ranges_buf.reported_underflow)
1693 return 0;
1694
1695 low = read_address (&ranges_buf, u->addrsize);
1696 high = read_address (&ranges_buf, u->addrsize);
1697
1698 if (low == 0 && high == 0)
1699 break;
1700
1701 if (is_highest_address (low, u->addrsize))
1702 base = high;
1703 else
1704 {
1705 if (!add_range (state, rdata,
1706 low + base + base_address,
1707 high + base + base_address,
1708 error_callback, data, vec))
1709 return 0;
1710 }
1711 }
1712
1713 if (ranges_buf.reported_underflow)
1714 return 0;
1715
1716 return 1;
1717}
1718
1719/* Call ADD_RANGE for each range read from .debug_rnglists, as used in
1720 DWARF version 5. */
1721
1722static int
1723add_ranges_from_rnglists (
1724 struct backtrace_state *state,
1725 const struct dwarf_sections *dwarf_sections,
1726 uintptr_t base_address, int is_bigendian,
1727 struct unit *u, uint64_t base,
1728 const struct pcrange *pcrange,
1729 int (*add_range) (struct backtrace_state *state, void *rdata,
1730 uint64_t lowpc, uint64_t highpc,
1731 backtrace_error_callback error_callback, void *data,
1732 void *vec),
1733 void *rdata,
1734 backtrace_error_callback error_callback, void *data,
1735 void *vec)
1736{
1737 uint64_t offset;
1738 struct dwarf_buf rnglists_buf;
1739
1740 if (!pcrange->ranges_is_index)
1741 offset = pcrange->ranges;
1742 else
1743 offset = u->rnglists_base + pcrange->ranges * (u->is_dwarf64 ? 8 : 4);
1744 if (offset >= dwarf_sections->size[DEBUG_RNGLISTS])
1745 {
1746 error_callback (data, "rnglists offset out of range", 0);
1747 return 0;
1748 }
1749
1750 rnglists_buf.name = ".debug_rnglists";
1751 rnglists_buf.start = dwarf_sections->data[DEBUG_RNGLISTS];
1752 rnglists_buf.buf = dwarf_sections->data[DEBUG_RNGLISTS] + offset;
1753 rnglists_buf.left = dwarf_sections->size[DEBUG_RNGLISTS] - offset;
1754 rnglists_buf.is_bigendian = is_bigendian;
1755 rnglists_buf.error_callback = error_callback;
1756 rnglists_buf.data = data;
1757 rnglists_buf.reported_underflow = 0;
1758
1759 if (pcrange->ranges_is_index)
1760 {
1761 offset = read_offset (&rnglists_buf, u->is_dwarf64);
1762 offset += u->rnglists_base;
1763 if (offset >= dwarf_sections->size[DEBUG_RNGLISTS])
1764 {
1765 error_callback (data, "rnglists index offset out of range", 0);
1766 return 0;
1767 }
1768 rnglists_buf.buf = dwarf_sections->data[DEBUG_RNGLISTS] + offset;
1769 rnglists_buf.left = dwarf_sections->size[DEBUG_RNGLISTS] - offset;
1770 }
1771
1772 while (1)
1773 {
1774 unsigned char rle;
1775
1776 rle = read_byte (&rnglists_buf);
1777 if (rle == DW_RLE_end_of_list)
1778 break;
1779 switch (rle)
1780 {
1781 case DW_RLE_base_addressx:
1782 {
1783 uint64_t index;
1784
1785 index = read_uleb128 (&rnglists_buf);
1786 if (!resolve_addr_index (dwarf_sections, u->addr_base,
1787 u->addrsize, is_bigendian, index,
1788 error_callback, data, &base))
1789 return 0;
1790 }
1791 break;
1792
1793 case DW_RLE_startx_endx:
1794 {
1795 uint64_t index;
1796 uint64_t low;
1797 uint64_t high;
1798
1799 index = read_uleb128 (&rnglists_buf);
1800 if (!resolve_addr_index (dwarf_sections, u->addr_base,
1801 u->addrsize, is_bigendian, index,
1802 error_callback, data, &low))
1803 return 0;
1804 index = read_uleb128 (&rnglists_buf);
1805 if (!resolve_addr_index (dwarf_sections, u->addr_base,
1806 u->addrsize, is_bigendian, index,
1807 error_callback, data, &high))
1808 return 0;
1809 if (!add_range (state, rdata, low + base_address,
1810 high + base_address, error_callback, data,
1811 vec))
1812 return 0;
1813 }
1814 break;
1815
1816 case DW_RLE_startx_length:
1817 {
1818 uint64_t index;
1819 uint64_t low;
1820 uint64_t length;
1821
1822 index = read_uleb128 (&rnglists_buf);
1823 if (!resolve_addr_index (dwarf_sections, u->addr_base,
1824 u->addrsize, is_bigendian, index,
1825 error_callback, data, &low))
1826 return 0;
1827 length = read_uleb128 (&rnglists_buf);
1828 low += base_address;
1829 if (!add_range (state, rdata, low, low + length,
1830 error_callback, data, vec))
1831 return 0;
1832 }
1833 break;
1834
1835 case DW_RLE_offset_pair:
1836 {
1837 uint64_t low;
1838 uint64_t high;
1839
1840 low = read_uleb128 (&rnglists_buf);
1841 high = read_uleb128 (&rnglists_buf);
1842 if (!add_range (state, rdata, low + base + base_address,
1843 high + base + base_address,
1844 error_callback, data, vec))
1845 return 0;
1846 }
1847 break;
1848
1849 case DW_RLE_base_address:
1850 base = read_address (&rnglists_buf, u->addrsize);
1851 break;
1852
1853 case DW_RLE_start_end:
1854 {
1855 uint64_t low;
1856 uint64_t high;
1857
1858 low = read_address (&rnglists_buf, u->addrsize);
1859 high = read_address (&rnglists_buf, u->addrsize);
1860 if (!add_range (state, rdata, low + base_address,
1861 high + base_address, error_callback, data,
1862 vec))
1863 return 0;
1864 }
1865 break;
1866
1867 case DW_RLE_start_length:
1868 {
1869 uint64_t low;
1870 uint64_t length;
1871
1872 low = read_address (&rnglists_buf, u->addrsize);
1873 length = read_uleb128 (&rnglists_buf);
1874 low += base_address;
1875 if (!add_range (state, rdata, low, low + length,
1876 error_callback, data, vec))
1877 return 0;
1878 }
1879 break;
1880
1881 default:
1882 dwarf_buf_error (&rnglists_buf, "unrecognized DW_RLE value");
1883 return 0;
1884 }
1885 }
1886
1887 if (rnglists_buf.reported_underflow)
1888 return 0;
1889
1890 return 1;
1891}
1892
1893/* Call ADD_RANGE for each lowpc/highpc pair in PCRANGE. RDATA is
1894 passed to ADD_RANGE, and is either a struct unit * or a struct
1895 function *. VEC is the vector we are adding ranges to, and is
1896 either a struct unit_addrs_vector * or a struct function_vector *.
1897 Returns 1 on success, 0 on error. */
1898
1899static int
1900add_ranges (struct backtrace_state *state,
1901 const struct dwarf_sections *dwarf_sections,
1902 uintptr_t base_address, int is_bigendian,
1903 struct unit *u, uint64_t base, const struct pcrange *pcrange,
1904 int (*add_range) (struct backtrace_state *state, void *rdata,
1905 uint64_t lowpc, uint64_t highpc,
1906 backtrace_error_callback error_callback,
1907 void *data, void *vec),
1908 void *rdata,
1909 backtrace_error_callback error_callback, void *data,
1910 void *vec)
1911{
1912 if (pcrange->have_lowpc && pcrange->have_highpc)
1913 return add_low_high_range (state, dwarf_sections, base_address,
1914 is_bigendian, u, pcrange, add_range, rdata,
1915 error_callback, data, vec);
1916
1917 if (!pcrange->have_ranges)
1918 {
1919 /* Did not find any address ranges to add. */
1920 return 1;
1921 }
1922
1923 if (u->version < 5)
1924 return add_ranges_from_ranges (state, dwarf_sections, base_address,
1925 is_bigendian, u, base, pcrange, add_range,
1926 rdata, error_callback, data, vec);
1927 else
1928 return add_ranges_from_rnglists (state, dwarf_sections, base_address,
1929 is_bigendian, u, base, pcrange, add_range,
1930 rdata, error_callback, data, vec);
1931}
1932
1933/* Find the address range covered by a compilation unit, reading from
1934 UNIT_BUF and adding values to U. Returns 1 if all data could be
1935 read, 0 if there is some error. */
1936
1937static int
1938find_address_ranges (struct backtrace_state *state, uintptr_t base_address,
1939 struct dwarf_buf *unit_buf,
1940 const struct dwarf_sections *dwarf_sections,
1941 int is_bigendian, struct dwarf_data *altlink,
1942 backtrace_error_callback error_callback, void *data,
1943 struct unit *u, struct unit_addrs_vector *addrs,
1944 enum dwarf_tag *unit_tag)
1945{
1946 while (unit_buf->left > 0)
35
Assuming field 'left' is > 0
36
Loop condition is true. Entering loop body
1947 {
1948 uint64_t code;
1949 const struct abbrev *abbrev;
1950 struct pcrange pcrange;
1951 struct attr_val name_val;
1952 int have_name_val;
1953 struct attr_val comp_dir_val;
1954 int have_comp_dir_val;
1955 size_t i;
1956
1957 code = read_uleb128 (unit_buf);
1958 if (code == 0)
37
Assuming 'code' is not equal to 0
38
Taking false branch
1959 return 1;
1960
1961 abbrev = lookup_abbrev (&u->abbrevs, code, error_callback, data);
39
Calling 'lookup_abbrev'
1962 if (abbrev == NULL((void*)0))
1963 return 0;
1964
1965 if (unit_tag != NULL((void*)0))
1966 *unit_tag = abbrev->tag;
1967
1968 memset (&pcrange, 0, sizeof pcrange);
1969 memset (&name_val, 0, sizeof name_val);
1970 have_name_val = 0;
1971 memset (&comp_dir_val, 0, sizeof comp_dir_val);
1972 have_comp_dir_val = 0;
1973 for (i = 0; i < abbrev->num_attrs; ++i)
1974 {
1975 struct attr_val val;
1976
1977 if (!read_attribute (abbrev->attrs[i].form, abbrev->attrs[i].val,
1978 unit_buf, u->is_dwarf64, u->version,
1979 u->addrsize, dwarf_sections, altlink, &val))
1980 return 0;
1981
1982 switch (abbrev->attrs[i].name)
1983 {
1984 case DW_AT_low_pc: case DW_AT_high_pc: case DW_AT_ranges:
1985 update_pcrange (&abbrev->attrs[i], &val, &pcrange);
1986 break;
1987
1988 case DW_AT_stmt_list:
1989 if (abbrev->tag == DW_TAG_compile_unit
1990 && (val.encoding == ATTR_VAL_UINT
1991 || val.encoding == ATTR_VAL_REF_SECTION))
1992 u->lineoff = val.u.uint;
1993 break;
1994
1995 case DW_AT_name:
1996 if (abbrev->tag == DW_TAG_compile_unit)
1997 {
1998 name_val = val;
1999 have_name_val = 1;
2000 }
2001 break;
2002
2003 case DW_AT_comp_dir:
2004 if (abbrev->tag == DW_TAG_compile_unit)
2005 {
2006 comp_dir_val = val;
2007 have_comp_dir_val = 1;
2008 }
2009 break;
2010
2011 case DW_AT_str_offsets_base:
2012 if (abbrev->tag == DW_TAG_compile_unit
2013 && val.encoding == ATTR_VAL_REF_SECTION)
2014 u->str_offsets_base = val.u.uint;
2015 break;
2016
2017 case DW_AT_addr_base:
2018 if (abbrev->tag == DW_TAG_compile_unit
2019 && val.encoding == ATTR_VAL_REF_SECTION)
2020 u->addr_base = val.u.uint;
2021 break;
2022
2023 case DW_AT_rnglists_base:
2024 if (abbrev->tag == DW_TAG_compile_unit
2025 && val.encoding == ATTR_VAL_REF_SECTION)
2026 u->rnglists_base = val.u.uint;
2027 break;
2028
2029 default:
2030 break;
2031 }
2032 }
2033
2034 // Resolve strings after we're sure that we have seen
2035 // DW_AT_str_offsets_base.
2036 if (have_name_val)
2037 {
2038 if (!resolve_string (dwarf_sections, u->is_dwarf64, is_bigendian,
2039 u->str_offsets_base, &name_val,
2040 error_callback, data, &u->filename))
2041 return 0;
2042 }
2043 if (have_comp_dir_val)
2044 {
2045 if (!resolve_string (dwarf_sections, u->is_dwarf64, is_bigendian,
2046 u->str_offsets_base, &comp_dir_val,
2047 error_callback, data, &u->comp_dir))
2048 return 0;
2049 }
2050
2051 if (abbrev->tag == DW_TAG_compile_unit
2052 || abbrev->tag == DW_TAG_subprogram)
2053 {
2054 if (!add_ranges (state, dwarf_sections, base_address,
2055 is_bigendian, u, pcrange.lowpc, &pcrange,
2056 add_unit_addr, (void *) u, error_callback, data,
2057 (void *) addrs))
2058 return 0;
2059
2060 /* If we found the PC range in the DW_TAG_compile_unit, we
2061 can stop now. */
2062 if (abbrev->tag == DW_TAG_compile_unit
2063 && (pcrange.have_ranges
2064 || (pcrange.have_lowpc && pcrange.have_highpc)))
2065 return 1;
2066 }
2067
2068 if (abbrev->has_children)
2069 {
2070 if (!find_address_ranges (state, base_address, unit_buf,
2071 dwarf_sections, is_bigendian, altlink,
2072 error_callback, data, u, addrs, NULL((void*)0)))
2073 return 0;
2074 }
2075 }
2076
2077 return 1;
2078}
2079
2080/* Build a mapping from address ranges to the compilation units where
2081 the line number information for that range can be found. Returns 1
2082 on success, 0 on failure. */
2083
2084static int
2085build_address_map (struct backtrace_state *state, uintptr_t base_address,
2086 const struct dwarf_sections *dwarf_sections,
2087 int is_bigendian, struct dwarf_data *altlink,
2088 backtrace_error_callback error_callback, void *data,
2089 struct unit_addrs_vector *addrs,
2090 struct unit_vector *unit_vec)
2091{
2092 struct dwarf_buf info;
2093 struct backtrace_vector units;
2094 size_t units_count;
2095 size_t i;
2096 struct unit **pu;
2097 size_t unit_offset = 0;
2098 struct unit_addrs *pa;
2099
2100 memset (&addrs->vec, 0, sizeof addrs->vec);
2101 memset (&unit_vec->vec, 0, sizeof unit_vec->vec);
2102 addrs->count = 0;
2103 unit_vec->count = 0;
2104
2105 /* Read through the .debug_info section. FIXME: Should we use the
2106 .debug_aranges section? gdb and addr2line don't use it, but I'm
2107 not sure why. */
2108
2109 info.name = ".debug_info";
2110 info.start = dwarf_sections->data[DEBUG_INFO];
2111 info.buf = info.start;
2112 info.left = dwarf_sections->size[DEBUG_INFO];
2113 info.is_bigendian = is_bigendian;
2114 info.error_callback = error_callback;
2115 info.data = data;
2116 info.reported_underflow = 0;
2117
2118 memset (&units, 0, sizeof units);
2119 units_count = 0;
2120
2121 while (info.left > 0)
3
Assuming field 'left' is > 0
4
Loop condition is true. Entering loop body
2122 {
2123 const unsigned char *unit_data_start;
2124 uint64_t len;
2125 int is_dwarf64;
2126 struct dwarf_buf unit_buf;
2127 int version;
2128 int unit_type;
2129 uint64_t abbrev_offset;
2130 int addrsize;
2131 struct unit *u;
2132 enum dwarf_tag unit_tag;
2133
2134 if (info.reported_underflow
4.1
Field 'reported_underflow' is 0
)
5
Taking false branch
2135 goto fail;
2136
2137 unit_data_start = info.buf;
2138
2139 len = read_initial_length (&info, &is_dwarf64);
2140 unit_buf = info;
2141 unit_buf.left = len;
2142
2143 if (!advance (&info, len))
6
Taking false branch
2144 goto fail;
2145
2146 version = read_uint16 (&unit_buf);
2147 if (version < 2 || version > 5)
7
Assuming 'version' is >= 2
8
Assuming 'version' is <= 5
9
Taking false branch
2148 {
2149 dwarf_buf_error (&unit_buf, "unrecognized DWARF version");
2150 goto fail;
2151 }
2152
2153 if (version < 5)
10
Assuming 'version' is >= 5
11
Taking false branch
2154 unit_type = 0;
2155 else
2156 {
2157 unit_type = read_byte (&unit_buf);
2158 if (unit_type == DW_UT_type || unit_type == DW_UT_split_type)
12
Assuming 'unit_type' is not equal to DW_UT_type
13
Assuming 'unit_type' is not equal to DW_UT_split_type
14
Taking false branch
2159 {
2160 /* This unit doesn't have anything we need. */
2161 continue;
2162 }
2163 }
2164
2165 pu = ((struct unit **)
2166 backtrace_vector_grow (state, sizeof (struct unit *),
2167 error_callback, data, &units));
2168 if (pu == NULL((void*)0))
15
Assuming 'pu' is not equal to NULL
16
Taking false branch
2169 goto fail;
2170
2171 u = ((struct unit *)
2172 backtrace_alloc (state, sizeof *u, error_callback, data));
2173 if (u == NULL((void*)0))
17
Assuming 'u' is not equal to NULL
18
Taking false branch
2174 goto fail;
2175
2176 *pu = u;
2177 ++units_count;
2178
2179 if (version
18.1
'version' is >= 5
< 5)
19
Taking false branch
2180 addrsize = 0; /* Set below. */
2181 else
2182 addrsize = read_byte (&unit_buf);
2183
2184 memset (&u->abbrevs, 0, sizeof u->abbrevs);
2185 abbrev_offset = read_offset (&unit_buf, is_dwarf64);
2186 if (!read_abbrevs (state, abbrev_offset,
20
Calling 'read_abbrevs'
28
Returning from 'read_abbrevs'
29
Taking false branch
2187 dwarf_sections->data[DEBUG_ABBREV],
2188 dwarf_sections->size[DEBUG_ABBREV],
2189 is_bigendian, error_callback, data, &u->abbrevs))
2190 goto fail;
2191
2192 if (version
29.1
'version' is >= 5
< 5)
30
Taking false branch
2193 addrsize = read_byte (&unit_buf);
2194
2195 switch (unit_type)
31
Control jumps to the 'default' case at line 2204
2196 {
2197 case 0:
2198 break;
2199 case DW_UT_compile: case DW_UT_partial:
2200 break;
2201 case DW_UT_skeleton: case DW_UT_split_compile:
2202 read_uint64 (&unit_buf); /* dwo_id */
2203 break;
2204 default:
2205 break;
32
Execution continues on line 2208
2206 }
2207
2208 u->low_offset = unit_offset;
2209 unit_offset += len + (is_dwarf64
32.1
'is_dwarf64' is 0
? 12 : 4);
33
'?' condition is false
2210 u->high_offset = unit_offset;
2211 u->unit_data = unit_buf.buf;
2212 u->unit_data_len = unit_buf.left;
2213 u->unit_data_offset = unit_buf.buf - unit_data_start;
2214 u->version = version;
2215 u->is_dwarf64 = is_dwarf64;
2216 u->addrsize = addrsize;
2217 u->filename = NULL((void*)0);
2218 u->comp_dir = NULL((void*)0);
2219 u->abs_filename = NULL((void*)0);
2220 u->lineoff = 0;
2221
2222 /* The actual line number mappings will be read as needed. */
2223 u->lines = NULL((void*)0);
2224 u->lines_count = 0;
2225 u->function_addrs = NULL((void*)0);
2226 u->function_addrs_count = 0;
2227
2228 if (!find_address_ranges (state, base_address, &unit_buf, dwarf_sections,
34
Calling 'find_address_ranges'
2229 is_bigendian, altlink, error_callback, data,
2230 u, addrs, &unit_tag))
2231 goto fail;
2232
2233 if (unit_buf.reported_underflow)
2234 goto fail;
2235 }
2236 if (info.reported_underflow)
2237 goto fail;
2238
2239 /* Add a trailing addrs entry, but don't include it in addrs->count. */
2240 pa = ((struct unit_addrs *)
2241 backtrace_vector_grow (state, sizeof (struct unit_addrs),
2242 error_callback, data, &addrs->vec));
2243 if (pa == NULL((void*)0))
2244 goto fail;
2245 pa->low = 0;
2246 --pa->low;
2247 pa->high = pa->low;
2248 pa->u = NULL((void*)0);
2249
2250 unit_vec->vec = units;
2251 unit_vec->count = units_count;
2252 return 1;
2253
2254 fail:
2255 if (units_count > 0)
2256 {
2257 pu = (struct unit **) units.base;
2258 for (i = 0; i < units_count; i++)
2259 {
2260 free_abbrevs (state, &pu[i]->abbrevs, error_callback, data);
2261 backtrace_free (state, pu[i], sizeof **pu, error_callback, data);
2262 }
2263 backtrace_vector_free (state, &units, error_callback, data);
2264 }
2265 if (addrs->count > 0)
2266 {
2267 backtrace_vector_free (state, &addrs->vec, error_callback, data);
2268 addrs->count = 0;
2269 }
2270 return 0;
2271}
2272
2273/* Add a new mapping to the vector of line mappings that we are
2274 building. Returns 1 on success, 0 on failure. */
2275
2276static int
2277add_line (struct backtrace_state *state, struct dwarf_data *ddata,
2278 uintptr_t pc, const char *filename, int lineno,
2279 backtrace_error_callback error_callback, void *data,
2280 struct line_vector *vec)
2281{
2282 struct line *ln;
2283
2284 /* If we are adding the same mapping, ignore it. This can happen
2285 when using discriminators. */
2286 if (vec->count > 0)
2287 {
2288 ln = (struct line *) vec->vec.base + (vec->count - 1);
2289 if (pc == ln->pc && filename == ln->filename && lineno == ln->lineno)
2290 return 1;
2291 }
2292
2293 ln = ((struct line *)
2294 backtrace_vector_grow (state, sizeof (struct line), error_callback,
2295 data, &vec->vec));
2296 if (ln == NULL((void*)0))
2297 return 0;
2298
2299 /* Add in the base address here, so that we can look up the PC
2300 directly. */
2301 ln->pc = pc + ddata->base_address;
2302
2303 ln->filename = filename;
2304 ln->lineno = lineno;
2305 ln->idx = vec->count;
2306
2307 ++vec->count;
2308
2309 return 1;
2310}
2311
2312/* Free the line header information. */
2313
2314static void
2315free_line_header (struct backtrace_state *state, struct line_header *hdr,
2316 backtrace_error_callback error_callback, void *data)
2317{
2318 if (hdr->dirs_count != 0)
2319 backtrace_free (state, hdr->dirs, hdr->dirs_count * sizeof (const char *),
2320 error_callback, data);
2321 backtrace_free (state, hdr->filenames,
2322 hdr->filenames_count * sizeof (char *),
2323 error_callback, data);
2324}
2325
2326/* Read the directories and file names for a line header for version
2327 2, setting fields in HDR. Return 1 on success, 0 on failure. */
2328
2329static int
2330read_v2_paths (struct backtrace_state *state, struct unit *u,
2331 struct dwarf_buf *hdr_buf, struct line_header *hdr)
2332{
2333 const unsigned char *p;
2334 const unsigned char *pend;
2335 size_t i;
2336
2337 /* Count the number of directory entries. */
2338 hdr->dirs_count = 0;
2339 p = hdr_buf->buf;
2340 pend = p + hdr_buf->left;
2341 while (p < pend && *p != '\0')
2342 {
2343 p += strnlen((const char *) p, pend - p) + 1;
2344 ++hdr->dirs_count;
2345 }
2346
2347 hdr->dirs = NULL((void*)0);
2348 if (hdr->dirs_count != 0)
2349 {
2350 hdr->dirs = ((const char **)
2351 backtrace_alloc (state,
2352 hdr->dirs_count * sizeof (const char *),
2353 hdr_buf->error_callback,
2354 hdr_buf->data));
2355 if (hdr->dirs == NULL((void*)0))
2356 return 0;
2357 }
2358
2359 i = 0;
2360 while (*hdr_buf->buf != '\0')
2361 {
2362 if (hdr_buf->reported_underflow)
2363 return 0;
2364
2365 hdr->dirs[i] = read_string (hdr_buf);
2366 if (hdr->dirs[i] == NULL((void*)0))
2367 return 0;
2368 ++i;
2369 }
2370 if (!advance (hdr_buf, 1))
2371 return 0;
2372
2373 /* Count the number of file entries. */
2374 hdr->filenames_count = 0;
2375 p = hdr_buf->buf;
2376 pend = p + hdr_buf->left;
2377 while (p < pend && *p != '\0')
2378 {
2379 p += strnlen ((const char *) p, pend - p) + 1;
2380 p += leb128_len (p);
2381 p += leb128_len (p);
2382 p += leb128_len (p);
2383 ++hdr->filenames_count;
2384 }
2385
2386 hdr->filenames = ((const char **)
2387 backtrace_alloc (state,
2388 hdr->filenames_count * sizeof (char *),
2389 hdr_buf->error_callback,
2390 hdr_buf->data));
2391 if (hdr->filenames == NULL((void*)0))
2392 return 0;
2393 i = 0;
2394 while (*hdr_buf->buf != '\0')
2395 {
2396 const char *filename;
2397 uint64_t dir_index;
2398
2399 if (hdr_buf->reported_underflow)
2400 return 0;
2401
2402 filename = read_string (hdr_buf);
2403 if (filename == NULL((void*)0))
2404 return 0;
2405 dir_index = read_uleb128 (hdr_buf);
2406 if (IS_ABSOLUTE_PATH (filename)(((((filename)[0]) == '/') || ((((filename)[0]) == '\\') &&
(0))) || ((filename)[0] && ((filename)[1] == ':') &&
(0)))
2407 || (dir_index == 0 && u->comp_dir == NULL((void*)0)))
2408 hdr->filenames[i] = filename;
2409 else
2410 {
2411 const char *dir;
2412 size_t dir_len;
2413 size_t filename_len;
2414 char *s;
2415
2416 if (dir_index == 0)
2417 dir = u->comp_dir;
2418 else if (dir_index - 1 < hdr->dirs_count)
2419 dir = hdr->dirs[dir_index - 1];
2420 else
2421 {
2422 dwarf_buf_error (hdr_buf,
2423 ("invalid directory index in "
2424 "line number program header"));
2425 return 0;
2426 }
2427 dir_len = strlen (dir);
2428 filename_len = strlen (filename);
2429 s = ((char *) backtrace_alloc (state, dir_len + filename_len + 2,
2430 hdr_buf->error_callback,
2431 hdr_buf->data));
2432 if (s == NULL((void*)0))
2433 return 0;
2434 memcpy (s, dir, dir_len);
2435 /* FIXME: If we are on a DOS-based file system, and the
2436 directory or the file name use backslashes, then we
2437 should use a backslash here. */
2438 s[dir_len] = '/';
2439 memcpy (s + dir_len + 1, filename, filename_len + 1);
2440 hdr->filenames[i] = s;
2441 }
2442
2443 /* Ignore the modification time and size. */
2444 read_uleb128 (hdr_buf);
2445 read_uleb128 (hdr_buf);
2446
2447 ++i;
2448 }
2449
2450 return 1;
2451}
2452
2453/* Read a single version 5 LNCT entry for a directory or file name in a
2454 line header. Sets *STRING to the resulting name, ignoring other
2455 data. Return 1 on success, 0 on failure. */
2456
2457static int
2458read_lnct (struct backtrace_state *state, struct dwarf_data *ddata,
2459 struct unit *u, struct dwarf_buf *hdr_buf,
2460 const struct line_header *hdr, size_t formats_count,
2461 const struct line_header_format *formats, const char **string)
2462{
2463 size_t i;
2464 const char *dir;
2465 const char *path;
2466
2467 dir = NULL((void*)0);
2468 path = NULL((void*)0);
2469 for (i = 0; i < formats_count; i++)
2470 {
2471 struct attr_val val;
2472
2473 if (!read_attribute (formats[i].form, 0, hdr_buf, u->is_dwarf64,
2474 u->version, hdr->addrsize, &ddata->dwarf_sections,
2475 ddata->altlink, &val))
2476 return 0;
2477 switch (formats[i].lnct)
2478 {
2479 case DW_LNCT_path:
2480 if (!resolve_string (&ddata->dwarf_sections, u->is_dwarf64,
2481 ddata->is_bigendian, u->str_offsets_base,
2482 &val, hdr_buf->error_callback, hdr_buf->data,
2483 &path))
2484 return 0;
2485 break;
2486 case DW_LNCT_directory_index:
2487 if (val.encoding == ATTR_VAL_UINT)
2488 {
2489 if (val.u.uint >= hdr->dirs_count)
2490 {
2491 dwarf_buf_error (hdr_buf,
2492 ("invalid directory index in "
2493 "line number program header"));
2494 return 0;
2495 }
2496 dir = hdr->dirs[val.u.uint];
2497 }
2498 break;
2499 default:
2500 /* We don't care about timestamps or sizes or hashes. */
2501 break;
2502 }
2503 }
2504
2505 if (path == NULL((void*)0))
2506 {
2507 dwarf_buf_error (hdr_buf,
2508 "missing file name in line number program header");
2509 return 0;
2510 }
2511
2512 if (dir == NULL((void*)0))
2513 *string = path;
2514 else
2515 {
2516 size_t dir_len;
2517 size_t path_len;
2518 char *s;
2519
2520 dir_len = strlen (dir);
2521 path_len = strlen (path);
2522 s = (char *) backtrace_alloc (state, dir_len + path_len + 2,
2523 hdr_buf->error_callback, hdr_buf->data);
2524 if (s == NULL((void*)0))
2525 return 0;
2526 memcpy (s, dir, dir_len);
2527 /* FIXME: If we are on a DOS-based file system, and the
2528 directory or the path name use backslashes, then we should
2529 use a backslash here. */
2530 s[dir_len] = '/';
2531 memcpy (s + dir_len + 1, path, path_len + 1);
2532 *string = s;
2533 }
2534
2535 return 1;
2536}
2537
2538/* Read a set of DWARF 5 line header format entries, setting *PCOUNT
2539 and *PPATHS. Return 1 on success, 0 on failure. */
2540
2541static int
2542read_line_header_format_entries (struct backtrace_state *state,
2543 struct dwarf_data *ddata,
2544 struct unit *u,
2545 struct dwarf_buf *hdr_buf,
2546 struct line_header *hdr,
2547 size_t *pcount,
2548 const char ***ppaths)
2549{
2550 size_t formats_count;
2551 struct line_header_format *formats;
2552 size_t paths_count;
2553 const char **paths;
2554 size_t i;
2555 int ret;
2556
2557 formats_count = read_byte (hdr_buf);
2558 if (formats_count == 0)
2559 formats = NULL((void*)0);
2560 else
2561 {
2562 formats = ((struct line_header_format *)
2563 backtrace_alloc (state,
2564 (formats_count
2565 * sizeof (struct line_header_format)),
2566 hdr_buf->error_callback,
2567 hdr_buf->data));
2568 if (formats == NULL((void*)0))
2569 return 0;
2570
2571 for (i = 0; i < formats_count; i++)
2572 {
2573 formats[i].lnct = (int) read_uleb128(hdr_buf);
2574 formats[i].form = (enum dwarf_form) read_uleb128 (hdr_buf);
2575 }
2576 }
2577
2578 paths_count = read_uleb128 (hdr_buf);
2579 if (paths_count == 0)
2580 {
2581 *pcount = 0;
2582 *ppaths = NULL((void*)0);
2583 ret = 1;
2584 goto exit;
2585 }
2586
2587 paths = ((const char **)
2588 backtrace_alloc (state, paths_count * sizeof (const char *),
2589 hdr_buf->error_callback, hdr_buf->data));
2590 if (paths == NULL((void*)0))
2591 {
2592 ret = 0;
2593 goto exit;
2594 }
2595 for (i = 0; i < paths_count; i++)
2596 {
2597 if (!read_lnct (state, ddata, u, hdr_buf, hdr, formats_count,
2598 formats, &paths[i]))
2599 {
2600 backtrace_free (state, paths,
2601 paths_count * sizeof (const char *),
2602 hdr_buf->error_callback, hdr_buf->data);
2603 ret = 0;
2604 goto exit;
2605 }
2606 }
2607
2608 *pcount = paths_count;
2609 *ppaths = paths;
2610
2611 ret = 1;
2612
2613 exit:
2614 if (formats != NULL((void*)0))
2615 backtrace_free (state, formats,
2616 formats_count * sizeof (struct line_header_format),
2617 hdr_buf->error_callback, hdr_buf->data);
2618
2619 return ret;
2620}
2621
2622/* Read the line header. Return 1 on success, 0 on failure. */
2623
2624static int
2625read_line_header (struct backtrace_state *state, struct dwarf_data *ddata,
2626 struct unit *u, int is_dwarf64, struct dwarf_buf *line_buf,
2627 struct line_header *hdr)
2628{
2629 uint64_t hdrlen;
2630 struct dwarf_buf hdr_buf;
2631
2632 hdr->version = read_uint16 (line_buf);
2633 if (hdr->version < 2 || hdr->version > 5)
2634 {
2635 dwarf_buf_error (line_buf, "unsupported line number version");
2636 return 0;
2637 }
2638
2639 if (hdr->version < 5)
2640 hdr->addrsize = u->addrsize;
2641 else
2642 {
2643 hdr->addrsize = read_byte (line_buf);
2644 /* We could support a non-zero segment_selector_size but I doubt
2645 we'll ever see it. */
2646 if (read_byte (line_buf) != 0)
2647 {
2648 dwarf_buf_error (line_buf,
2649 "non-zero segment_selector_size not supported");
2650 return 0;
2651 }
2652 }
2653
2654 hdrlen = read_offset (line_buf, is_dwarf64);
2655
2656 hdr_buf = *line_buf;
2657 hdr_buf.left = hdrlen;
2658
2659 if (!advance (line_buf, hdrlen))
2660 return 0;
2661
2662 hdr->min_insn_len = read_byte (&hdr_buf);
2663 if (hdr->version < 4)
2664 hdr->max_ops_per_insn = 1;
2665 else
2666 hdr->max_ops_per_insn = read_byte (&hdr_buf);
2667
2668 /* We don't care about default_is_stmt. */
2669 read_byte (&hdr_buf);
2670
2671 hdr->line_base = read_sbyte (&hdr_buf);
2672 hdr->line_range = read_byte (&hdr_buf);
2673
2674 hdr->opcode_base = read_byte (&hdr_buf);
2675 hdr->opcode_lengths = hdr_buf.buf;
2676 if (!advance (&hdr_buf, hdr->opcode_base - 1))
2677 return 0;
2678
2679 if (hdr->version < 5)
2680 {
2681 if (!read_v2_paths (state, u, &hdr_buf, hdr))
2682 return 0;
2683 }
2684 else
2685 {
2686 if (!read_line_header_format_entries (state, ddata, u, &hdr_buf, hdr,
2687 &hdr->dirs_count,
2688 &hdr->dirs))
2689 return 0;
2690 if (!read_line_header_format_entries (state, ddata, u, &hdr_buf, hdr,
2691 &hdr->filenames_count,
2692 &hdr->filenames))
2693 return 0;
2694 }
2695
2696 if (hdr_buf.reported_underflow)
2697 return 0;
2698
2699 return 1;
2700}
2701
2702/* Read the line program, adding line mappings to VEC. Return 1 on
2703 success, 0 on failure. */
2704
2705static int
2706read_line_program (struct backtrace_state *state, struct dwarf_data *ddata,
2707 struct unit *u, const struct line_header *hdr,
2708 struct dwarf_buf *line_buf, struct line_vector *vec)
2709{
2710 uint64_t address;
2711 unsigned int op_index;
2712 const char *reset_filename;
2713 const char *filename;
2714 int lineno;
2715
2716 address = 0;
2717 op_index = 0;
2718 if (hdr->filenames_count > 0)
2719 reset_filename = hdr->filenames[0];
2720 else
2721 reset_filename = "";
2722 filename = reset_filename;
2723 lineno = 1;
2724 while (line_buf->left > 0)
2725 {
2726 unsigned int op;
2727
2728 op = read_byte (line_buf);
2729 if (op >= hdr->opcode_base)
2730 {
2731 unsigned int advance;
2732
2733 /* Special opcode. */
2734 op -= hdr->opcode_base;
2735 advance = op / hdr->line_range;
2736 address += (hdr->min_insn_len * (op_index + advance)
2737 / hdr->max_ops_per_insn);
2738 op_index = (op_index + advance) % hdr->max_ops_per_insn;
2739 lineno += hdr->line_base + (int) (op % hdr->line_range);
2740 add_line (state, ddata, address, filename, lineno,
2741 line_buf->error_callback, line_buf->data, vec);
2742 }
2743 else if (op == DW_LNS_extended_op)
2744 {
2745 uint64_t len;
2746
2747 len = read_uleb128 (line_buf);
2748 op = read_byte (line_buf);
2749 switch (op)
2750 {
2751 case DW_LNE_end_sequence:
2752 /* FIXME: Should we mark the high PC here? It seems
2753 that we already have that information from the
2754 compilation unit. */
2755 address = 0;
2756 op_index = 0;
2757 filename = reset_filename;
2758 lineno = 1;
2759 break;
2760 case DW_LNE_set_address:
2761 address = read_address (line_buf, hdr->addrsize);
2762 break;
2763 case DW_LNE_define_file:
2764 {
2765 const char *f;
2766 unsigned int dir_index;
2767
2768 f = read_string (line_buf);
2769 if (f == NULL((void*)0))
2770 return 0;
2771 dir_index = read_uleb128 (line_buf);
2772 /* Ignore that time and length. */
2773 read_uleb128 (line_buf);
2774 read_uleb128 (line_buf);
2775 if (IS_ABSOLUTE_PATH (f)(((((f)[0]) == '/') || ((((f)[0]) == '\\') && (0))) ||
((f)[0] && ((f)[1] == ':') && (0)))
)
2776 filename = f;
2777 else
2778 {
2779 const char *dir;
2780 size_t dir_len;
2781 size_t f_len;
2782 char *p;
2783
2784 if (dir_index == 0 && hdr->version < 5)
2785 dir = u->comp_dir;
2786 else if (dir_index - 1 < hdr->dirs_count)
2787 dir = hdr->dirs[dir_index - 1];
2788 else
2789 {
2790 dwarf_buf_error (line_buf,
2791 ("invalid directory index "
2792 "in line number program"));
2793 return 0;
2794 }
2795 dir_len = strlen (dir);
2796 f_len = strlen (f);
2797 p = ((char *)
2798 backtrace_alloc (state, dir_len + f_len + 2,
2799 line_buf->error_callback,
2800 line_buf->data));
2801 if (p == NULL((void*)0))
2802 return 0;
2803 memcpy (p, dir, dir_len);
2804 /* FIXME: If we are on a DOS-based file system,
2805 and the directory or the file name use
2806 backslashes, then we should use a backslash
2807 here. */
2808 p[dir_len] = '/';
2809 memcpy (p + dir_len + 1, f, f_len + 1);
2810 filename = p;
2811 }
2812 }
2813 break;
2814 case DW_LNE_set_discriminator:
2815 /* We don't care about discriminators. */
2816 read_uleb128 (line_buf);
2817 break;
2818 default:
2819 if (!advance (line_buf, len - 1))
2820 return 0;
2821 break;
2822 }
2823 }
2824 else
2825 {
2826 switch (op)
2827 {
2828 case DW_LNS_copy:
2829 add_line (state, ddata, address, filename, lineno,
2830 line_buf->error_callback, line_buf->data, vec);
2831 break;
2832 case DW_LNS_advance_pc:
2833 {
2834 uint64_t advance;
2835
2836 advance = read_uleb128 (line_buf);
2837 address += (hdr->min_insn_len * (op_index + advance)
2838 / hdr->max_ops_per_insn);
2839 op_index = (op_index + advance) % hdr->max_ops_per_insn;
2840 }
2841 break;
2842 case DW_LNS_advance_line:
2843 lineno += (int) read_sleb128 (line_buf);
2844 break;
2845 case DW_LNS_set_file:
2846 {
2847 uint64_t fileno;
2848
2849 fileno = read_uleb128 (line_buf);
2850 if (fileno == 0)
2851 filename = "";
2852 else
2853 {
2854 if (fileno - 1 >= hdr->filenames_count)
2855 {
2856 dwarf_buf_error (line_buf,
2857 ("invalid file number in "
2858 "line number program"));
2859 return 0;
2860 }
2861 filename = hdr->filenames[fileno - 1];
2862 }
2863 }
2864 break;
2865 case DW_LNS_set_column:
2866 read_uleb128 (line_buf);
2867 break;
2868 case DW_LNS_negate_stmt:
2869 break;
2870 case DW_LNS_set_basic_block:
2871 break;
2872 case DW_LNS_const_add_pc:
2873 {
2874 unsigned int advance;
2875
2876 op = 255 - hdr->opcode_base;
2877 advance = op / hdr->line_range;
2878 address += (hdr->min_insn_len * (op_index + advance)
2879 / hdr->max_ops_per_insn);
2880 op_index = (op_index + advance) % hdr->max_ops_per_insn;
2881 }
2882 break;
2883 case DW_LNS_fixed_advance_pc:
2884 address += read_uint16 (line_buf);
2885 op_index = 0;
2886 break;
2887 case DW_LNS_set_prologue_end:
2888 break;
2889 case DW_LNS_set_epilogue_begin:
2890 break;
2891 case DW_LNS_set_isa:
2892 read_uleb128 (line_buf);
2893 break;
2894 default:
2895 {
2896 unsigned int i;
2897
2898 for (i = hdr->opcode_lengths[op - 1]; i > 0; --i)
2899 read_uleb128 (line_buf);
2900 }
2901 break;
2902 }
2903 }
2904 }
2905
2906 return 1;
2907}
2908
2909/* Read the line number information for a compilation unit. Returns 1
2910 on success, 0 on failure. */
2911
2912static int
2913read_line_info (struct backtrace_state *state, struct dwarf_data *ddata,
2914 backtrace_error_callback error_callback, void *data,
2915 struct unit *u, struct line_header *hdr, struct line **lines,
2916 size_t *lines_count)
2917{
2918 struct line_vector vec;
2919 struct dwarf_buf line_buf;
2920 uint64_t len;
2921 int is_dwarf64;
2922 struct line *ln;
2923
2924 memset (&vec.vec, 0, sizeof vec.vec);
2925 vec.count = 0;
2926
2927 memset (hdr, 0, sizeof *hdr);
2928
2929 if (u->lineoff != (off_t) (size_t) u->lineoff
2930 || (size_t) u->lineoff >= ddata->dwarf_sections.size[DEBUG_LINE])
2931 {
2932 error_callback (data, "unit line offset out of range", 0);
2933 goto fail;
2934 }
2935
2936 line_buf.name = ".debug_line";
2937 line_buf.start = ddata->dwarf_sections.data[DEBUG_LINE];
2938 line_buf.buf = ddata->dwarf_sections.data[DEBUG_LINE] + u->lineoff;
2939 line_buf.left = ddata->dwarf_sections.size[DEBUG_LINE] - u->lineoff;
2940 line_buf.is_bigendian = ddata->is_bigendian;
2941 line_buf.error_callback = error_callback;
2942 line_buf.data = data;
2943 line_buf.reported_underflow = 0;
2944
2945 len = read_initial_length (&line_buf, &is_dwarf64);
2946 line_buf.left = len;
2947
2948 if (!read_line_header (state, ddata, u, is_dwarf64, &line_buf, hdr))
2949 goto fail;
2950
2951 if (!read_line_program (state, ddata, u, hdr, &line_buf, &vec))
2952 goto fail;
2953
2954 if (line_buf.reported_underflow)
2955 goto fail;
2956
2957 if (vec.count == 0)
2958 {
2959 /* This is not a failure in the sense of a generating an error,
2960 but it is a failure in that sense that we have no useful
2961 information. */
2962 goto fail;
2963 }
2964
2965 /* Allocate one extra entry at the end. */
2966 ln = ((struct line *)
2967 backtrace_vector_grow (state, sizeof (struct line), error_callback,
2968 data, &vec.vec));
2969 if (ln == NULL((void*)0))
2970 goto fail;
2971 ln->pc = (uintptr_t) -1;
2972 ln->filename = NULL((void*)0);
2973 ln->lineno = 0;
2974 ln->idx = 0;
2975
2976 if (!backtrace_vector_release (state, &vec.vec, error_callback, data))
2977 goto fail;
2978
2979 ln = (struct line *) vec.vec.base;
2980 backtrace_qsort (ln, vec.count, sizeof (struct line), line_compare);
2981
2982 *lines = ln;
2983 *lines_count = vec.count;
2984
2985 return 1;
2986
2987 fail:
2988 backtrace_vector_free (state, &vec.vec, error_callback, data);
2989 free_line_header (state, hdr, error_callback, data);
2990 *lines = (struct line *) (uintptr_t) -1;
2991 *lines_count = 0;
2992 return 0;
2993}
2994
2995static const char *read_referenced_name (struct dwarf_data *, struct unit *,
2996 uint64_t, backtrace_error_callback,
2997 void *);
2998
2999/* Read the name of a function from a DIE referenced by ATTR with VAL. */
3000
3001static const char *
3002read_referenced_name_from_attr (struct dwarf_data *ddata, struct unit *u,
3003 struct attr *attr, struct attr_val *val,
3004 backtrace_error_callback error_callback,
3005 void *data)
3006{
3007 switch (attr->name)
3008 {
3009 case DW_AT_abstract_origin:
3010 case DW_AT_specification:
3011 break;
3012 default:
3013 return NULL((void*)0);
3014 }
3015
3016 if (attr->form == DW_FORM_ref_sig8)
3017 return NULL((void*)0);
3018
3019 if (val->encoding == ATTR_VAL_REF_INFO)
3020 {
3021 struct unit *unit
3022 = find_unit (ddata->units, ddata->units_count,
3023 val->u.uint);
3024 if (unit == NULL((void*)0))
3025 return NULL((void*)0);
3026
3027 uint64_t offset = val->u.uint - unit->low_offset;
3028 return read_referenced_name (ddata, unit, offset, error_callback, data);
3029 }
3030
3031 if (val->encoding == ATTR_VAL_UINT
3032 || val->encoding == ATTR_VAL_REF_UNIT)
3033 return read_referenced_name (ddata, u, val->u.uint, error_callback, data);
3034
3035 if (val->encoding == ATTR_VAL_REF_ALT_INFO)
3036 {
3037 struct unit *alt_unit
3038 = find_unit (ddata->altlink->units, ddata->altlink->units_count,
3039 val->u.uint);
3040 if (alt_unit == NULL((void*)0))
3041 return NULL((void*)0);
3042
3043 uint64_t offset = val->u.uint - alt_unit->low_offset;
3044 return read_referenced_name (ddata->altlink, alt_unit, offset,
3045 error_callback, data);
3046 }
3047
3048 return NULL((void*)0);
3049}
3050
3051/* Read the name of a function from a DIE referenced by a
3052 DW_AT_abstract_origin or DW_AT_specification tag. OFFSET is within
3053 the same compilation unit. */
3054
3055static const char *
3056read_referenced_name (struct dwarf_data *ddata, struct unit *u,
3057 uint64_t offset, backtrace_error_callback error_callback,
3058 void *data)
3059{
3060 struct dwarf_buf unit_buf;
3061 uint64_t code;
3062 const struct abbrev *abbrev;
3063 const char *ret;
3064 size_t i;
3065
3066 /* OFFSET is from the start of the data for this compilation unit.
3067 U->unit_data is the data, but it starts U->unit_data_offset bytes
3068 from the beginning. */
3069
3070 if (offset < u->unit_data_offset
3071 || offset - u->unit_data_offset >= u->unit_data_len)
3072 {
3073 error_callback (data,
3074 "abstract origin or specification out of range",
3075 0);
3076 return NULL((void*)0);
3077 }
3078
3079 offset -= u->unit_data_offset;
3080
3081 unit_buf.name = ".debug_info";
3082 unit_buf.start = ddata->dwarf_sections.data[DEBUG_INFO];
3083 unit_buf.buf = u->unit_data + offset;
3084 unit_buf.left = u->unit_data_len - offset;
3085 unit_buf.is_bigendian = ddata->is_bigendian;
3086 unit_buf.error_callback = error_callback;
3087 unit_buf.data = data;
3088 unit_buf.reported_underflow = 0;
3089
3090 code = read_uleb128 (&unit_buf);
3091 if (code == 0)
3092 {
3093 dwarf_buf_error (&unit_buf, "invalid abstract origin or specification");
3094 return NULL((void*)0);
3095 }
3096
3097 abbrev = lookup_abbrev (&u->abbrevs, code, error_callback, data);
3098 if (abbrev == NULL((void*)0))
3099 return NULL((void*)0);
3100
3101 ret = NULL((void*)0);
3102 for (i = 0; i < abbrev->num_attrs; ++i)
3103 {
3104 struct attr_val val;
3105
3106 if (!read_attribute (abbrev->attrs[i].form, abbrev->attrs[i].val,
3107 &unit_buf, u->is_dwarf64, u->version, u->addrsize,
3108 &ddata->dwarf_sections, ddata->altlink, &val))
3109 return NULL((void*)0);
3110
3111 switch (abbrev->attrs[i].name)
3112 {
3113 case DW_AT_name:
3114 /* Third name preference: don't override. A name we found in some
3115 other way, will normally be more useful -- e.g., this name is
3116 normally not mangled. */
3117 if (ret != NULL((void*)0))
3118 break;
3119 if (!resolve_string (&ddata->dwarf_sections, u->is_dwarf64,
3120 ddata->is_bigendian, u->str_offsets_base,
3121 &val, error_callback, data, &ret))
3122 return NULL((void*)0);
3123 break;
3124
3125 case DW_AT_linkage_name:
3126 case DW_AT_MIPS_linkage_name:
3127 /* First name preference: override all. */
3128 {
3129 const char *s;
3130
3131 s = NULL((void*)0);
3132 if (!resolve_string (&ddata->dwarf_sections, u->is_dwarf64,
3133 ddata->is_bigendian, u->str_offsets_base,
3134 &val, error_callback, data, &s))
3135 return NULL((void*)0);
3136 if (s != NULL((void*)0))
3137 return s;
3138 }
3139 break;
3140
3141 case DW_AT_specification:
3142 /* Second name preference: override DW_AT_name, don't override
3143 DW_AT_linkage_name. */
3144 {
3145 const char *name;
3146
3147 name = read_referenced_name_from_attr (ddata, u, &abbrev->attrs[i],
3148 &val, error_callback, data);
3149 if (name != NULL((void*)0))
3150 ret = name;
3151 }
3152 break;
3153
3154 default:
3155 break;
3156 }
3157 }
3158
3159 return ret;
3160}
3161
3162/* Add a range to a unit that maps to a function. This is called via
3163 add_ranges. Returns 1 on success, 0 on error. */
3164
3165static int
3166add_function_range (struct backtrace_state *state, void *rdata,
3167 uint64_t lowpc, uint64_t highpc,
3168 backtrace_error_callback error_callback, void *data,
3169 void *pvec)
3170{
3171 struct function *function = (struct function *) rdata;
3172 struct function_vector *vec = (struct function_vector *) pvec;
3173 struct function_addrs *p;
3174
3175 if (vec->count > 0)
3176 {
3177 p = (struct function_addrs *) vec->vec.base + (vec->count - 1);
3178 if ((lowpc == p->high || lowpc == p->high + 1)
3179 && function == p->function)
3180 {
3181 if (highpc > p->high)
3182 p->high = highpc;
3183 return 1;
3184 }
3185 }
3186
3187 p = ((struct function_addrs *)
3188 backtrace_vector_grow (state, sizeof (struct function_addrs),
3189 error_callback, data, &vec->vec));
3190 if (p == NULL((void*)0))
3191 return 0;
3192
3193 p->low = lowpc;
3194 p->high = highpc;
3195 p->function = function;
3196
3197 ++vec->count;
3198
3199 return 1;
3200}
3201
3202/* Read one entry plus all its children. Add function addresses to
3203 VEC. Returns 1 on success, 0 on error. */
3204
3205static int
3206read_function_entry (struct backtrace_state *state, struct dwarf_data *ddata,
3207 struct unit *u, uint64_t base, struct dwarf_buf *unit_buf,
3208 const struct line_header *lhdr,
3209 backtrace_error_callback error_callback, void *data,
3210 struct function_vector *vec_function,
3211 struct function_vector *vec_inlined)
3212{
3213 while (unit_buf->left > 0)
3214 {
3215 uint64_t code;
3216 const struct abbrev *abbrev;
3217 int is_function;
3218 struct function *function;
3219 struct function_vector *vec;
3220 size_t i;
3221 struct pcrange pcrange;
3222 int have_linkage_name;
3223
3224 code = read_uleb128 (unit_buf);
3225 if (code == 0)
3226 return 1;
3227
3228 abbrev = lookup_abbrev (&u->abbrevs, code, error_callback, data);
3229 if (abbrev == NULL((void*)0))
3230 return 0;
3231
3232 is_function = (abbrev->tag == DW_TAG_subprogram
3233 || abbrev->tag == DW_TAG_entry_point
3234 || abbrev->tag == DW_TAG_inlined_subroutine);
3235
3236 if (abbrev->tag == DW_TAG_inlined_subroutine)
3237 vec = vec_inlined;
3238 else
3239 vec = vec_function;
3240
3241 function = NULL((void*)0);
3242 if (is_function)
3243 {
3244 function = ((struct function *)
3245 backtrace_alloc (state, sizeof *function,
3246 error_callback, data));
3247 if (function == NULL((void*)0))
3248 return 0;
3249 memset (function, 0, sizeof *function);
3250 }
3251
3252 memset (&pcrange, 0, sizeof pcrange);
3253 have_linkage_name = 0;
3254 for (i = 0; i < abbrev->num_attrs; ++i)
3255 {
3256 struct attr_val val;
3257
3258 if (!read_attribute (abbrev->attrs[i].form, abbrev->attrs[i].val,
3259 unit_buf, u->is_dwarf64, u->version,
3260 u->addrsize, &ddata->dwarf_sections,
3261 ddata->altlink, &val))
3262 return 0;
3263
3264 /* The compile unit sets the base address for any address
3265 ranges in the function entries. */
3266 if (abbrev->tag == DW_TAG_compile_unit
3267 && abbrev->attrs[i].name == DW_AT_low_pc)
3268 {
3269 if (val.encoding == ATTR_VAL_ADDRESS)
3270 base = val.u.uint;
3271 else if (val.encoding == ATTR_VAL_ADDRESS_INDEX)
3272 {
3273 if (!resolve_addr_index (&ddata->dwarf_sections,
3274 u->addr_base, u->addrsize,
3275 ddata->is_bigendian, val.u.uint,
3276 error_callback, data, &base))
3277 return 0;
3278 }
3279 }
3280
3281 if (is_function)
3282 {
3283 switch (abbrev->attrs[i].name)
3284 {
3285 case DW_AT_call_file:
3286 if (val.encoding == ATTR_VAL_UINT)
3287 {
3288 if (val.u.uint == 0)
3289 function->caller_filename = "";
3290 else
3291 {
3292 if (val.u.uint - 1 >= lhdr->filenames_count)
3293 {
3294 dwarf_buf_error (unit_buf,
3295 ("invalid file number in "
3296 "DW_AT_call_file attribute"));
3297 return 0;
3298 }
3299 function->caller_filename =
3300 lhdr->filenames[val.u.uint - 1];
3301 }
3302 }
3303 break;
3304
3305 case DW_AT_call_line:
3306 if (val.encoding == ATTR_VAL_UINT)
3307 function->caller_lineno = val.u.uint;
3308 break;
3309
3310 case DW_AT_abstract_origin:
3311 case DW_AT_specification:
3312 /* Second name preference: override DW_AT_name, don't override
3313 DW_AT_linkage_name. */
3314 if (have_linkage_name)
3315 break;
3316 {
3317 const char *name;
3318
3319 name
3320 = read_referenced_name_from_attr (ddata, u,
3321 &abbrev->attrs[i], &val,
3322 error_callback, data);
3323 if (name != NULL((void*)0))
3324 function->name = name;
3325 }
3326 break;
3327
3328 case DW_AT_name:
3329 /* Third name preference: don't override. */
3330 if (function->name != NULL((void*)0))
3331 break;
3332 if (!resolve_string (&ddata->dwarf_sections, u->is_dwarf64,
3333 ddata->is_bigendian,
3334 u->str_offsets_base, &val,
3335 error_callback, data, &function->name))
3336 return 0;
3337 break;
3338
3339 case DW_AT_linkage_name:
3340 case DW_AT_MIPS_linkage_name:
3341 /* First name preference: override all. */
3342 {
3343 const char *s;
3344
3345 s = NULL((void*)0);
3346 if (!resolve_string (&ddata->dwarf_sections, u->is_dwarf64,
3347 ddata->is_bigendian,
3348 u->str_offsets_base, &val,
3349 error_callback, data, &s))
3350 return 0;
3351 if (s != NULL((void*)0))
3352 {
3353 function->name = s;
3354 have_linkage_name = 1;
3355 }
3356 }
3357 break;
3358
3359 case DW_AT_low_pc: case DW_AT_high_pc: case DW_AT_ranges:
3360 update_pcrange (&abbrev->attrs[i], &val, &pcrange);
3361 break;
3362
3363 default:
3364 break;
3365 }
3366 }
3367 }
3368
3369 /* If we couldn't find a name for the function, we have no use
3370 for it. */
3371 if (is_function && function->name == NULL((void*)0))
3372 {
3373 backtrace_free (state, function, sizeof *function,
3374 error_callback, data);
3375 is_function = 0;
3376 }
3377
3378 if (is_function)
3379 {
3380 if (pcrange.have_ranges
3381 || (pcrange.have_lowpc && pcrange.have_highpc))
3382 {
3383 if (!add_ranges (state, &ddata->dwarf_sections,
3384 ddata->base_address, ddata->is_bigendian,
3385 u, base, &pcrange, add_function_range,
3386 (void *) function, error_callback, data,
3387 (void *) vec))
3388 return 0;
3389 }
3390 else
3391 {
3392 backtrace_free (state, function, sizeof *function,
3393 error_callback, data);
3394 is_function = 0;
3395 }
3396 }
3397
3398 if (abbrev->has_children)
3399 {
3400 if (!is_function)
3401 {
3402 if (!read_function_entry (state, ddata, u, base, unit_buf, lhdr,
3403 error_callback, data, vec_function,
3404 vec_inlined))
3405 return 0;
3406 }
3407 else
3408 {
3409 struct function_vector fvec;
3410
3411 /* Gather any information for inlined functions in
3412 FVEC. */
3413
3414 memset (&fvec, 0, sizeof fvec);
3415
3416 if (!read_function_entry (state, ddata, u, base, unit_buf, lhdr,
3417 error_callback, data, vec_function,
3418 &fvec))
3419 return 0;
3420
3421 if (fvec.count > 0)
3422 {
3423 struct function_addrs *p;
3424 struct function_addrs *faddrs;
3425
3426 /* Allocate a trailing entry, but don't include it
3427 in fvec.count. */
3428 p = ((struct function_addrs *)
3429 backtrace_vector_grow (state,
3430 sizeof (struct function_addrs),
3431 error_callback, data,
3432 &fvec.vec));
3433 if (p == NULL((void*)0))
3434 return 0;
3435 p->low = 0;
3436 --p->low;
3437 p->high = p->low;
3438 p->function = NULL((void*)0);
3439
3440 if (!backtrace_vector_release (state, &fvec.vec,
3441 error_callback, data))
3442 return 0;
3443
3444 faddrs = (struct function_addrs *) fvec.vec.base;
3445 backtrace_qsort (faddrs, fvec.count,
3446 sizeof (struct function_addrs),
3447 function_addrs_compare);
3448
3449 function->function_addrs = faddrs;
3450 function->function_addrs_count = fvec.count;
3451 }
3452 }
3453 }
3454 }
3455
3456 return 1;
3457}
3458
3459/* Read function name information for a compilation unit. We look
3460 through the whole unit looking for function tags. */
3461
3462static void
3463read_function_info (struct backtrace_state *state, struct dwarf_data *ddata,
3464 const struct line_header *lhdr,
3465 backtrace_error_callback error_callback, void *data,
3466 struct unit *u, struct function_vector *fvec,
3467 struct function_addrs **ret_addrs,
3468 size_t *ret_addrs_count)
3469{
3470 struct function_vector lvec;
3471 struct function_vector *pfvec;
3472 struct dwarf_buf unit_buf;
3473 struct function_addrs *p;
3474 struct function_addrs *addrs;
3475 size_t addrs_count;
3476
3477 /* Use FVEC if it is not NULL. Otherwise use our own vector. */
3478 if (fvec != NULL((void*)0))
3479 pfvec = fvec;
3480 else
3481 {
3482 memset (&lvec, 0, sizeof lvec);
3483 pfvec = &lvec;
3484 }
3485
3486 unit_buf.name = ".debug_info";
3487 unit_buf.start = ddata->dwarf_sections.data[DEBUG_INFO];
3488 unit_buf.buf = u->unit_data;
3489 unit_buf.left = u->unit_data_len;
3490 unit_buf.is_bigendian = ddata->is_bigendian;
3491 unit_buf.error_callback = error_callback;
3492 unit_buf.data = data;
3493 unit_buf.reported_underflow = 0;
3494
3495 while (unit_buf.left > 0)
3496 {
3497 if (!read_function_entry (state, ddata, u, 0, &unit_buf, lhdr,
3498 error_callback, data, pfvec, pfvec))
3499 return;
3500 }
3501
3502 if (pfvec->count == 0)
3503 return;
3504
3505 /* Allocate a trailing entry, but don't include it in
3506 pfvec->count. */
3507 p = ((struct function_addrs *)
3508 backtrace_vector_grow (state, sizeof (struct function_addrs),
3509 error_callback, data, &pfvec->vec));
3510 if (p == NULL((void*)0))
3511 return;
3512 p->low = 0;
3513 --p->low;
3514 p->high = p->low;
3515 p->function = NULL((void*)0);
3516
3517 addrs_count = pfvec->count;
3518
3519 if (fvec == NULL((void*)0))
3520 {
3521 if (!backtrace_vector_release (state, &lvec.vec, error_callback, data))
3522 return;
3523 addrs = (struct function_addrs *) pfvec->vec.base;
3524 }
3525 else
3526 {
3527 /* Finish this list of addresses, but leave the remaining space in
3528 the vector available for the next function unit. */
3529 addrs = ((struct function_addrs *)
3530 backtrace_vector_finish (state, &fvec->vec,
3531 error_callback, data));
3532 if (addrs == NULL((void*)0))
3533 return;
3534 fvec->count = 0;
3535 }
3536
3537 backtrace_qsort (addrs, addrs_count, sizeof (struct function_addrs),
3538 function_addrs_compare);
3539
3540 *ret_addrs = addrs;
3541 *ret_addrs_count = addrs_count;
3542}
3543
3544/* See if PC is inlined in FUNCTION. If it is, print out the inlined
3545 information, and update FILENAME and LINENO for the caller.
3546 Returns whatever CALLBACK returns, or 0 to keep going. */
3547
3548static int
3549report_inlined_functions (uintptr_t pc, struct function *function,
3550 backtrace_full_callback callback, void *data,
3551 const char **filename, int *lineno)
3552{
3553 struct function_addrs *p;
3554 struct function_addrs *match;
3555 struct function *inlined;
3556 int ret;
3557
3558 if (function->function_addrs_count == 0)
3559 return 0;
3560
3561 /* Our search isn't safe if pc == -1, as that is the sentinel
3562 value. */
3563 if (pc + 1 == 0)
3564 return 0;
3565
3566 p = ((struct function_addrs *)
3567 bsearch (&pc, function->function_addrs,
3568 function->function_addrs_count,
3569 sizeof (struct function_addrs),
3570 function_addrs_search));
3571 if (p == NULL((void*)0))
3572 return 0;
3573
3574 /* Here pc >= p->low && pc < (p + 1)->low. The function_addrs are
3575 sorted by low, so if pc > p->low we are at the end of a range of
3576 function_addrs with the same low value. If pc == p->low walk
3577 forward to the end of the range with that low value. Then walk
3578 backward and use the first range that includes pc. */
3579 while (pc == (p + 1)->low)
3580 ++p;
3581 match = NULL((void*)0);
3582 while (1)
3583 {
3584 if (pc < p->high)
3585 {
3586 match = p;
3587 break;
3588 }
3589 if (p == function->function_addrs)
3590 break;
3591 if ((p - 1)->low < p->low)
3592 break;
3593 --p;
3594 }
3595 if (match == NULL((void*)0))
3596 return 0;
3597
3598 /* We found an inlined call. */
3599
3600 inlined = match->function;
3601
3602 /* Report any calls inlined into this one. */
3603 ret = report_inlined_functions (pc, inlined, callback, data,
3604 filename, lineno);
3605 if (ret != 0)
3606 return ret;
3607
3608 /* Report this inlined call. */
3609 ret = callback (data, pc, *filename, *lineno, inlined->name);
3610 if (ret != 0)
3611 return ret;
3612
3613 /* Our caller will report the caller of the inlined function; tell
3614 it the appropriate filename and line number. */
3615 *filename = inlined->caller_filename;
3616 *lineno = inlined->caller_lineno;
3617
3618 return 0;
3619}
3620
3621/* Look for a PC in the DWARF mapping for one module. On success,
3622 call CALLBACK and return whatever it returns. On error, call
3623 ERROR_CALLBACK and return 0. Sets *FOUND to 1 if the PC is found,
3624 0 if not. */
3625
3626static int
3627dwarf_lookup_pc (struct backtrace_state *state, struct dwarf_data *ddata,
3628 uintptr_t pc, backtrace_full_callback callback,
3629 backtrace_error_callback error_callback, void *data,
3630 int *found)
3631{
3632 struct unit_addrs *entry;
3633 int found_entry;
3634 struct unit *u;
3635 int new_data;
3636 struct line *lines;
3637 struct line *ln;
3638 struct function_addrs *p;
3639 struct function_addrs *fmatch;
3640 struct function *function;
3641 const char *filename;
3642 int lineno;
3643 int ret;
3644
3645 *found = 1;
3646
3647 /* Find an address range that includes PC. Our search isn't safe if
3648 PC == -1, as we use that as a sentinel value, so skip the search
3649 in that case. */
3650 entry = (ddata->addrs_count == 0 || pc + 1 == 0
3651 ? NULL((void*)0)
3652 : bsearch (&pc, ddata->addrs, ddata->addrs_count,
3653 sizeof (struct unit_addrs), unit_addrs_search));
3654
3655 if (entry == NULL((void*)0))
3656 {
3657 *found = 0;
3658 return 0;
3659 }
3660
3661 /* Here pc >= entry->low && pc < (entry + 1)->low. The unit_addrs
3662 are sorted by low, so if pc > p->low we are at the end of a range
3663 of unit_addrs with the same low value. If pc == p->low walk
3664 forward to the end of the range with that low value. Then walk
3665 backward and use the first range that includes pc. */
3666 while (pc == (entry + 1)->low)
3667 ++entry;
3668 found_entry = 0;
3669 while (1)
3670 {
3671 if (pc < entry->high)
3672 {
3673 found_entry = 1;
3674 break;
3675 }
3676 if (entry == ddata->addrs)
3677 break;
3678 if ((entry - 1)->low < entry->low)
3679 break;
3680 --entry;
3681 }
3682 if (!found_entry)
3683 {
3684 *found = 0;
3685 return 0;
3686 }
3687
3688 /* We need the lines, lines_count, function_addrs,
3689 function_addrs_count fields of u. If they are not set, we need
3690 to set them. When running in threaded mode, we need to allow for
3691 the possibility that some other thread is setting them
3692 simultaneously. */
3693
3694 u = entry->u;
3695 lines = u->lines;
3696
3697 /* Skip units with no useful line number information by walking
3698 backward. Useless line number information is marked by setting
3699 lines == -1. */
3700 while (entry > ddata->addrs
3701 && pc >= (entry - 1)->low
3702 && pc < (entry - 1)->high)
3703 {
3704 if (state->threaded)
3705 lines = (struct line *) backtrace_atomic_load_pointer (&u->lines)__atomic_load_n ((&u->lines), 2);
3706
3707 if (lines != (struct line *) (uintptr_t) -1)
3708 break;
3709
3710 --entry;
3711
3712 u = entry->u;
3713 lines = u->lines;
3714 }
3715
3716 if (state->threaded)
3717 lines = backtrace_atomic_load_pointer (&u->lines)__atomic_load_n ((&u->lines), 2);
3718
3719 new_data = 0;
3720 if (lines == NULL((void*)0))
3721 {
3722 struct function_addrs *function_addrs;
3723 size_t function_addrs_count;
3724 struct line_header lhdr;
3725 size_t count;
3726
3727 /* We have never read the line information for this unit. Read
3728 it now. */
3729
3730 function_addrs = NULL((void*)0);
3731 function_addrs_count = 0;
3732 if (read_line_info (state, ddata, error_callback, data, entry->u, &lhdr,
3733 &lines, &count))
3734 {
3735 struct function_vector *pfvec;
3736
3737 /* If not threaded, reuse DDATA->FVEC for better memory
3738 consumption. */
3739 if (state->threaded)
3740 pfvec = NULL((void*)0);
3741 else
3742 pfvec = &ddata->fvec;
3743 read_function_info (state, ddata, &lhdr, error_callback, data,
3744 entry->u, pfvec, &function_addrs,
3745 &function_addrs_count);
3746 free_line_header (state, &lhdr, error_callback, data);
3747 new_data = 1;
3748 }
3749
3750 /* Atomically store the information we just read into the unit.
3751 If another thread is simultaneously writing, it presumably
3752 read the same information, and we don't care which one we
3753 wind up with; we just leak the other one. We do have to
3754 write the lines field last, so that the acquire-loads above
3755 ensure that the other fields are set. */
3756
3757 if (!state->threaded)
3758 {
3759 u->lines_count = count;
3760 u->function_addrs = function_addrs;
3761 u->function_addrs_count = function_addrs_count;
3762 u->lines = lines;
3763 }
3764 else
3765 {
3766 backtrace_atomic_store_size_t (&u->lines_count, count)__atomic_store_n ((&u->lines_count), (count), 3);
3767 backtrace_atomic_store_pointer (&u->function_addrs, function_addrs)__atomic_store_n ((&u->function_addrs), (function_addrs
), 3)
;
3768 backtrace_atomic_store_size_t (&u->function_addrs_count,__atomic_store_n ((&u->function_addrs_count), (function_addrs_count
), 3)
3769 function_addrs_count)__atomic_store_n ((&u->function_addrs_count), (function_addrs_count
), 3)
;
3770 backtrace_atomic_store_pointer (&u->lines, lines)__atomic_store_n ((&u->lines), (lines), 3);
3771 }
3772 }
3773
3774 /* Now all fields of U have been initialized. */
3775
3776 if (lines == (struct line *) (uintptr_t) -1)
3777 {
3778 /* If reading the line number information failed in some way,
3779 try again to see if there is a better compilation unit for
3780 this PC. */
3781 if (new_data)
3782 return dwarf_lookup_pc (state, ddata, pc, callback, error_callback,
3783 data, found);
3784 return callback (data, pc, NULL((void*)0), 0, NULL((void*)0));
3785 }
3786
3787 /* Search for PC within this unit. */
3788
3789 ln = (struct line *) bsearch (&pc, lines, entry->u->lines_count,
3790 sizeof (struct line), line_search);
3791 if (ln == NULL((void*)0))
3792 {
3793 /* The PC is between the low_pc and high_pc attributes of the
3794 compilation unit, but no entry in the line table covers it.
3795 This implies that the start of the compilation unit has no
3796 line number information. */
3797
3798 if (entry->u->abs_filename == NULL((void*)0))
3799 {
3800 const char *filename;
3801
3802 filename = entry->u->filename;
3803 if (filename != NULL((void*)0)
3804 && !IS_ABSOLUTE_PATH (filename)(((((filename)[0]) == '/') || ((((filename)[0]) == '\\') &&
(0))) || ((filename)[0] && ((filename)[1] == ':') &&
(0)))
3805 && entry->u->comp_dir != NULL((void*)0))
3806 {
3807 size_t filename_len;
3808 const char *dir;
3809 size_t dir_len;
3810 char *s;
3811
3812 filename_len = strlen (filename);
3813 dir = entry->u->comp_dir;
3814 dir_len = strlen (dir);
3815 s = (char *) backtrace_alloc (state, dir_len + filename_len + 2,
3816 error_callback, data);
3817 if (s == NULL((void*)0))
3818 {
3819 *found = 0;
3820 return 0;
3821 }
3822 memcpy (s, dir, dir_len);
3823 /* FIXME: Should use backslash if DOS file system. */
3824 s[dir_len] = '/';
3825 memcpy (s + dir_len + 1, filename, filename_len + 1);
3826 filename = s;
3827 }
3828 entry->u->abs_filename = filename;
3829 }
3830
3831 return callback (data, pc, entry->u->abs_filename, 0, NULL((void*)0));
3832 }
3833
3834 /* Search for function name within this unit. */
3835
3836 if (entry->u->function_addrs_count == 0)
3837 return callback (data, pc, ln->filename, ln->lineno, NULL((void*)0));
3838
3839 p = ((struct function_addrs *)
3840 bsearch (&pc, entry->u->function_addrs,
3841 entry->u->function_addrs_count,
3842 sizeof (struct function_addrs),
3843 function_addrs_search));
3844 if (p == NULL((void*)0))
3845 return callback (data, pc, ln->filename, ln->lineno, NULL((void*)0));
3846
3847 /* Here pc >= p->low && pc < (p + 1)->low. The function_addrs are
3848 sorted by low, so if pc > p->low we are at the end of a range of
3849 function_addrs with the same low value. If pc == p->low walk
3850 forward to the end of the range with that low value. Then walk
3851 backward and use the first range that includes pc. */
3852 while (pc == (p + 1)->low)
3853 ++p;
3854 fmatch = NULL((void*)0);
3855 while (1)
3856 {
3857 if (pc < p->high)
3858 {
3859 fmatch = p;
3860 break;
3861 }
3862 if (p == entry->u->function_addrs)
3863 break;
3864 if ((p - 1)->low < p->low)
3865 break;
3866 --p;
3867 }
3868 if (fmatch == NULL((void*)0))
3869 return callback (data, pc, ln->filename, ln->lineno, NULL((void*)0));
3870
3871 function = fmatch->function;
3872
3873 filename = ln->filename;
3874 lineno = ln->lineno;
3875
3876 ret = report_inlined_functions (pc, function, callback, data,
3877 &filename, &lineno);
3878 if (ret != 0)
3879 return ret;
3880
3881 return callback (data, pc, filename, lineno, function->name);
3882}
3883
3884
3885/* Return the file/line information for a PC using the DWARF mapping
3886 we built earlier. */
3887
3888static int
3889dwarf_fileline (struct backtrace_state *state, uintptr_t pc,
3890 backtrace_full_callback callback,
3891 backtrace_error_callback error_callback, void *data)
3892{
3893 struct dwarf_data *ddata;
3894 int found;
3895 int ret;
3896
3897 if (!state->threaded)
3898 {
3899 for (ddata = (struct dwarf_data *) state->fileline_data;
3900 ddata != NULL((void*)0);
3901 ddata = ddata->next)
3902 {
3903 ret = dwarf_lookup_pc (state, ddata, pc, callback, error_callback,
3904 data, &found);
3905 if (ret != 0 || found)
3906 return ret;
3907 }
3908 }
3909 else
3910 {
3911 struct dwarf_data **pp;
3912
3913 pp = (struct dwarf_data **) (void *) &state->fileline_data;
3914 while (1)
3915 {
3916 ddata = backtrace_atomic_load_pointer (pp)__atomic_load_n ((pp), 2);
3917 if (ddata == NULL((void*)0))
3918 break;
3919
3920 ret = dwarf_lookup_pc (state, ddata, pc, callback, error_callback,
3921 data, &found);
3922 if (ret != 0 || found)
3923 return ret;
3924
3925 pp = &ddata->next;
3926 }
3927 }
3928
3929 /* FIXME: See if any libraries have been dlopen'ed. */
3930
3931 return callback (data, pc, NULL((void*)0), 0, NULL((void*)0));
3932}
3933
3934/* Initialize our data structures from the DWARF debug info for a
3935 file. Return NULL on failure. */
3936
3937static struct dwarf_data *
3938build_dwarf_data (struct backtrace_state *state,
3939 uintptr_t base_address,
3940 const struct dwarf_sections *dwarf_sections,
3941 int is_bigendian,
3942 struct dwarf_data *altlink,
3943 backtrace_error_callback error_callback,
3944 void *data)
3945{
3946 struct unit_addrs_vector addrs_vec;
3947 struct unit_addrs *addrs;
3948 size_t addrs_count;
3949 struct unit_vector units_vec;
3950 struct unit **units;
3951 size_t units_count;
3952 struct dwarf_data *fdata;
3953
3954 if (!build_address_map (state, base_address, dwarf_sections, is_bigendian,
2
Calling 'build_address_map'
3955 altlink, error_callback, data, &addrs_vec,
3956 &units_vec))
3957 return NULL((void*)0);
3958
3959 if (!backtrace_vector_release (state, &addrs_vec.vec, error_callback, data))
3960 return NULL((void*)0);
3961 if (!backtrace_vector_release (state, &units_vec.vec, error_callback, data))
3962 return NULL((void*)0);
3963 addrs = (struct unit_addrs *) addrs_vec.vec.base;
3964 units = (struct unit **) units_vec.vec.base;
3965 addrs_count = addrs_vec.count;
3966 units_count = units_vec.count;
3967 backtrace_qsort (addrs, addrs_count, sizeof (struct unit_addrs),
3968 unit_addrs_compare);
3969 /* No qsort for units required, already sorted. */
3970
3971 fdata = ((struct dwarf_data *)
3972 backtrace_alloc (state, sizeof (struct dwarf_data),
3973 error_callback, data));
3974 if (fdata == NULL((void*)0))
3975 return NULL((void*)0);
3976
3977 fdata->next = NULL((void*)0);
3978 fdata->altlink = altlink;
3979 fdata->base_address = base_address;
3980 fdata->addrs = addrs;
3981 fdata->addrs_count = addrs_count;
3982 fdata->units = units;
3983 fdata->units_count = units_count;
3984 fdata->dwarf_sections = *dwarf_sections;
3985 fdata->is_bigendian = is_bigendian;
3986 memset (&fdata->fvec, 0, sizeof fdata->fvec);
3987
3988 return fdata;
3989}
3990
3991/* Build our data structures from the DWARF sections for a module.
3992 Set FILELINE_FN and STATE->FILELINE_DATA. Return 1 on success, 0
3993 on failure. */
3994
3995int
3996backtrace_dwarf_add (struct backtrace_state *state,
3997 uintptr_t base_address,
3998 const struct dwarf_sections *dwarf_sections,
3999 int is_bigendian,
4000 struct dwarf_data *fileline_altlink,
4001 backtrace_error_callback error_callback,
4002 void *data, fileline *fileline_fn,
4003 struct dwarf_data **fileline_entry)
4004{
4005 struct dwarf_data *fdata;
4006
4007 fdata = build_dwarf_data (state, base_address, dwarf_sections, is_bigendian,
1
Calling 'build_dwarf_data'
4008 fileline_altlink, error_callback, data);
4009 if (fdata == NULL((void*)0))
4010 return 0;
4011
4012 if (fileline_entry != NULL((void*)0))
4013 *fileline_entry = fdata;
4014
4015 if (!state->threaded)
4016 {
4017 struct dwarf_data **pp;
4018
4019 for (pp = (struct dwarf_data **) (void *) &state->fileline_data;
4020 *pp != NULL((void*)0);
4021 pp = &(*pp)->next)
4022 ;
4023 *pp = fdata;
4024 }
4025 else
4026 {
4027 while (1)
4028 {
4029 struct dwarf_data **pp;
4030
4031 pp = (struct dwarf_data **) (void *) &state->fileline_data;
4032
4033 while (1)
4034 {
4035 struct dwarf_data *p;
4036
4037 p = backtrace_atomic_load_pointer (pp)__atomic_load_n ((pp), 2);
4038
4039 if (p == NULL((void*)0))
4040 break;
4041
4042 pp = &p->next;
4043 }
4044
4045 if (__sync_bool_compare_and_swap (pp, NULL((void*)0), fdata))
4046 break;
4047 }
4048 }
4049
4050 *fileline_fn = dwarf_fileline;
4051
4052 return 1;
4053}