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