1 /* elf.c -- Get debug data from an ELF file for backtraces.
2 Copyright (C) 2012-2020 Free Software Foundation, Inc.
3 Written by Ian Lance Taylor, Google.
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are
9 (1) Redistributions of source code must retain the above copyright
10 notice, this list of conditions and the following disclaimer.
12 (2) Redistributions in binary form must reproduce the above copyright
13 notice, this list of conditions and the following disclaimer in
14 the documentation and/or other materials provided with the
17 (3) The name of the author may not be used to
18 endorse or promote products derived from this software without
19 specific prior written permission.
21 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
25 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
29 STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
30 IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 POSSIBILITY OF SUCH DAMAGE. */
38 #include <sys/types.h>
42 #ifdef HAVE_DL_ITERATE_PHDR
46 #include "backtrace.h"
51 #define S_IFLNK 0120000
54 #define S_IFMT 0170000
56 #define S_ISLNK(m) (((m) & S_IFMT) == S_IFLNK)
60 #define __builtin_prefetch(p, r, l)
61 #define unlikely(x) (x)
63 #define unlikely(x) __builtin_expect(!!(x), 0)
66 #if !defined(HAVE_DECL_STRNLEN) || !HAVE_DECL_STRNLEN
68 /* If strnlen is not declared, provide our own version. */
71 xstrnlen (const char *s
, size_t maxlen
)
75 for (i
= 0; i
< maxlen
; ++i
)
81 #define strnlen xstrnlen
87 /* Dummy version of lstat for systems that don't have it. */
90 xlstat (const char *path ATTRIBUTE_UNUSED
, struct stat
*st ATTRIBUTE_UNUSED
)
101 /* Dummy version of readlink for systems that don't have it. */
104 xreadlink (const char *path ATTRIBUTE_UNUSED
, char *buf ATTRIBUTE_UNUSED
,
105 size_t bufsz ATTRIBUTE_UNUSED
)
110 #define readlink xreadlink
114 #ifndef HAVE_DL_ITERATE_PHDR
116 /* Dummy version of dl_iterate_phdr for systems that don't have it. */
118 #define dl_phdr_info x_dl_phdr_info
119 #define dl_iterate_phdr x_dl_iterate_phdr
124 const char *dlpi_name
;
128 dl_iterate_phdr (int (*callback
) (struct dl_phdr_info
*,
129 size_t, void *) ATTRIBUTE_UNUSED
,
130 void *data ATTRIBUTE_UNUSED
)
135 #endif /* ! defined (HAVE_DL_ITERATE_PHDR) */
137 /* The configure script must tell us whether we are 32-bit or 64-bit
138 ELF. We could make this code test and support either possibility,
139 but there is no point. This code only works for the currently
140 running executable, which means that we know the ELF mode at
143 #if BACKTRACE_ELF_SIZE != 32 && BACKTRACE_ELF_SIZE != 64
144 #error "Unknown BACKTRACE_ELF_SIZE"
147 /* <link.h> might #include <elf.h> which might define our constants
148 with slightly different values. Undefine them to be safe. */
177 #undef SHF_COMPRESSED
180 #undef NT_GNU_BUILD_ID
181 #undef ELFCOMPRESS_ZLIB
185 typedef uint16_t b_elf_half
; /* Elf_Half. */
186 typedef uint32_t b_elf_word
; /* Elf_Word. */
187 typedef int32_t b_elf_sword
; /* Elf_Sword. */
189 #if BACKTRACE_ELF_SIZE == 32
191 typedef uint32_t b_elf_addr
; /* Elf_Addr. */
192 typedef uint32_t b_elf_off
; /* Elf_Off. */
194 typedef uint32_t b_elf_wxword
; /* 32-bit Elf_Word, 64-bit ELF_Xword. */
198 typedef uint64_t b_elf_addr
; /* Elf_Addr. */
199 typedef uint64_t b_elf_off
; /* Elf_Off. */
200 typedef uint64_t b_elf_xword
; /* Elf_Xword. */
201 typedef int64_t b_elf_sxword
; /* Elf_Sxword. */
203 typedef uint64_t b_elf_wxword
; /* 32-bit Elf_Word, 64-bit ELF_Xword. */
207 /* Data structures and associated constants. */
212 unsigned char e_ident
[EI_NIDENT
]; /* ELF "magic number" */
213 b_elf_half e_type
; /* Identifies object file type */
214 b_elf_half e_machine
; /* Specifies required architecture */
215 b_elf_word e_version
; /* Identifies object file version */
216 b_elf_addr e_entry
; /* Entry point virtual address */
217 b_elf_off e_phoff
; /* Program header table file offset */
218 b_elf_off e_shoff
; /* Section header table file offset */
219 b_elf_word e_flags
; /* Processor-specific flags */
220 b_elf_half e_ehsize
; /* ELF header size in bytes */
221 b_elf_half e_phentsize
; /* Program header table entry size */
222 b_elf_half e_phnum
; /* Program header table entry count */
223 b_elf_half e_shentsize
; /* Section header table entry size */
224 b_elf_half e_shnum
; /* Section header table entry count */
225 b_elf_half e_shstrndx
; /* Section header string table index */
226 } b_elf_ehdr
; /* Elf_Ehdr. */
244 #define ELFDATA2LSB 1
245 #define ELFDATA2MSB 2
252 #define EF_PPC64_ABI 3
255 b_elf_word sh_name
; /* Section name, index in string tbl */
256 b_elf_word sh_type
; /* Type of section */
257 b_elf_wxword sh_flags
; /* Miscellaneous section attributes */
258 b_elf_addr sh_addr
; /* Section virtual addr at execution */
259 b_elf_off sh_offset
; /* Section file offset */
260 b_elf_wxword sh_size
; /* Size of section in bytes */
261 b_elf_word sh_link
; /* Index of another section */
262 b_elf_word sh_info
; /* Additional section information */
263 b_elf_wxword sh_addralign
; /* Section alignment */
264 b_elf_wxword sh_entsize
; /* Entry size if section holds table */
265 } b_elf_shdr
; /* Elf_Shdr. */
267 #define SHN_UNDEF 0x0000 /* Undefined section */
268 #define SHN_LORESERVE 0xFF00 /* Begin range of reserved indices */
269 #define SHN_XINDEX 0xFFFF /* Section index is held elsewhere */
271 #define SHT_PROGBITS 1
274 #define SHT_DYNSYM 11
276 #define SHF_COMPRESSED 0x800
278 #if BACKTRACE_ELF_SIZE == 32
282 b_elf_word st_name
; /* Symbol name, index in string tbl */
283 b_elf_addr st_value
; /* Symbol value */
284 b_elf_word st_size
; /* Symbol size */
285 unsigned char st_info
; /* Symbol binding and type */
286 unsigned char st_other
; /* Visibility and other data */
287 b_elf_half st_shndx
; /* Symbol section index */
288 } b_elf_sym
; /* Elf_Sym. */
290 #else /* BACKTRACE_ELF_SIZE != 32 */
294 b_elf_word st_name
; /* Symbol name, index in string tbl */
295 unsigned char st_info
; /* Symbol binding and type */
296 unsigned char st_other
; /* Visibility and other data */
297 b_elf_half st_shndx
; /* Symbol section index */
298 b_elf_addr st_value
; /* Symbol value */
299 b_elf_xword st_size
; /* Symbol size */
300 } b_elf_sym
; /* Elf_Sym. */
302 #endif /* BACKTRACE_ELF_SIZE != 32 */
315 #define NT_GNU_BUILD_ID 3
317 #if BACKTRACE_ELF_SIZE == 32
321 b_elf_word ch_type
; /* Compresstion algorithm */
322 b_elf_word ch_size
; /* Uncompressed size */
323 b_elf_word ch_addralign
; /* Alignment for uncompressed data */
324 } b_elf_chdr
; /* Elf_Chdr */
326 #else /* BACKTRACE_ELF_SIZE != 32 */
330 b_elf_word ch_type
; /* Compression algorithm */
331 b_elf_word ch_reserved
; /* Reserved */
332 b_elf_xword ch_size
; /* Uncompressed size */
333 b_elf_xword ch_addralign
; /* Alignment for uncompressed data */
334 } b_elf_chdr
; /* Elf_Chdr */
336 #endif /* BACKTRACE_ELF_SIZE != 32 */
338 #define ELFCOMPRESS_ZLIB 1
340 /* Names of sections, indexed by enum dwarf_section in internal.h. */
342 static const char * const dwarf_section_names
[DEBUG_MAX
] =
350 ".debug_str_offsets",
355 /* Information we gather for the sections we care about. */
357 struct debug_section_info
359 /* Section file offset. */
363 /* Section contents, after read from file. */
364 const unsigned char *data
;
365 /* Whether the SHF_COMPRESSED flag is set for the section. */
369 /* Information we keep for an ELF symbol. */
373 /* The name of the symbol. */
375 /* The address of the symbol. */
377 /* The size of the symbol. */
381 /* Information to pass to elf_syminfo. */
383 struct elf_syminfo_data
385 /* Symbols for the next module. */
386 struct elf_syminfo_data
*next
;
387 /* The ELF symbols, sorted by address. */
388 struct elf_symbol
*symbols
;
389 /* The number of symbols. */
393 /* Information about PowerPC64 ELFv1 .opd section. */
395 struct elf_ppc64_opd_data
397 /* Address of the .opd section. */
401 /* Size of the .opd section. */
403 /* Corresponding section view. */
404 struct backtrace_view view
;
407 /* Compute the CRC-32 of BUF/LEN. This uses the CRC used for
408 .gnu_debuglink files. */
411 elf_crc32 (uint32_t crc
, const unsigned char *buf
, size_t len
)
413 static const uint32_t crc32_table
[256] =
415 0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419,
416 0x706af48f, 0xe963a535, 0x9e6495a3, 0x0edb8832, 0x79dcb8a4,
417 0xe0d5e91e, 0x97d2d988, 0x09b64c2b, 0x7eb17cbd, 0xe7b82d07,
418 0x90bf1d91, 0x1db71064, 0x6ab020f2, 0xf3b97148, 0x84be41de,
419 0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7, 0x136c9856,
420 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec, 0x14015c4f, 0x63066cd9,
421 0xfa0f3d63, 0x8d080df5, 0x3b6e20c8, 0x4c69105e, 0xd56041e4,
422 0xa2677172, 0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b,
423 0x35b5a8fa, 0x42b2986c, 0xdbbbc9d6, 0xacbcf940, 0x32d86ce3,
424 0x45df5c75, 0xdcd60dcf, 0xabd13d59, 0x26d930ac, 0x51de003a,
425 0xc8d75180, 0xbfd06116, 0x21b4f4b5, 0x56b3c423, 0xcfba9599,
426 0xb8bda50f, 0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924,
427 0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d, 0x76dc4190,
428 0x01db7106, 0x98d220bc, 0xefd5102a, 0x71b18589, 0x06b6b51f,
429 0x9fbfe4a5, 0xe8b8d433, 0x7807c9a2, 0x0f00f934, 0x9609a88e,
430 0xe10e9818, 0x7f6a0dbb, 0x086d3d2d, 0x91646c97, 0xe6635c01,
431 0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e, 0x6c0695ed,
432 0x1b01a57b, 0x8208f4c1, 0xf50fc457, 0x65b0d9c6, 0x12b7e950,
433 0x8bbeb8ea, 0xfcb9887c, 0x62dd1ddf, 0x15da2d49, 0x8cd37cf3,
434 0xfbd44c65, 0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2,
435 0x4adfa541, 0x3dd895d7, 0xa4d1c46d, 0xd3d6f4fb, 0x4369e96a,
436 0x346ed9fc, 0xad678846, 0xda60b8d0, 0x44042d73, 0x33031de5,
437 0xaa0a4c5f, 0xdd0d7cc9, 0x5005713c, 0x270241aa, 0xbe0b1010,
438 0xc90c2086, 0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f,
439 0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4, 0x59b33d17,
440 0x2eb40d81, 0xb7bd5c3b, 0xc0ba6cad, 0xedb88320, 0x9abfb3b6,
441 0x03b6e20c, 0x74b1d29a, 0xead54739, 0x9dd277af, 0x04db2615,
442 0x73dc1683, 0xe3630b12, 0x94643b84, 0x0d6d6a3e, 0x7a6a5aa8,
443 0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1, 0xf00f9344,
444 0x8708a3d2, 0x1e01f268, 0x6906c2fe, 0xf762575d, 0x806567cb,
445 0x196c3671, 0x6e6b06e7, 0xfed41b76, 0x89d32be0, 0x10da7a5a,
446 0x67dd4acc, 0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5,
447 0xd6d6a3e8, 0xa1d1937e, 0x38d8c2c4, 0x4fdff252, 0xd1bb67f1,
448 0xa6bc5767, 0x3fb506dd, 0x48b2364b, 0xd80d2bda, 0xaf0a1b4c,
449 0x36034af6, 0x41047a60, 0xdf60efc3, 0xa867df55, 0x316e8eef,
450 0x4669be79, 0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236,
451 0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f, 0xc5ba3bbe,
452 0xb2bd0b28, 0x2bb45a92, 0x5cb36a04, 0xc2d7ffa7, 0xb5d0cf31,
453 0x2cd99e8b, 0x5bdeae1d, 0x9b64c2b0, 0xec63f226, 0x756aa39c,
454 0x026d930a, 0x9c0906a9, 0xeb0e363f, 0x72076785, 0x05005713,
455 0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38, 0x92d28e9b,
456 0xe5d5be0d, 0x7cdcefb7, 0x0bdbdf21, 0x86d3d2d4, 0xf1d4e242,
457 0x68ddb3f8, 0x1fda836e, 0x81be16cd, 0xf6b9265b, 0x6fb077e1,
458 0x18b74777, 0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c,
459 0x8f659eff, 0xf862ae69, 0x616bffd3, 0x166ccf45, 0xa00ae278,
460 0xd70dd2ee, 0x4e048354, 0x3903b3c2, 0xa7672661, 0xd06016f7,
461 0x4969474d, 0x3e6e77db, 0xaed16a4a, 0xd9d65adc, 0x40df0b66,
462 0x37d83bf0, 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9,
463 0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6, 0xbad03605,
464 0xcdd70693, 0x54de5729, 0x23d967bf, 0xb3667a2e, 0xc4614ab8,
465 0x5d681b02, 0x2a6f2b94, 0xb40bbe37, 0xc30c8ea1, 0x5a05df1b,
468 const unsigned char *end
;
471 for (end
= buf
+ len
; buf
< end
; ++ buf
)
472 crc
= crc32_table
[(crc
^ *buf
) & 0xff] ^ (crc
>> 8);
476 /* Return the CRC-32 of the entire file open at DESCRIPTOR. */
479 elf_crc32_file (struct backtrace_state
*state
, int descriptor
,
480 backtrace_error_callback error_callback
, void *data
)
483 struct backtrace_view file_view
;
486 if (fstat (descriptor
, &st
) < 0)
488 error_callback (data
, "fstat", errno
);
492 if (!backtrace_get_view (state
, descriptor
, 0, st
.st_size
, error_callback
,
496 ret
= elf_crc32 (0, (const unsigned char *) file_view
.data
, st
.st_size
);
498 backtrace_release_view (state
, &file_view
, error_callback
, data
);
503 /* A dummy callback function used when we can't find any debug info. */
506 elf_nodebug (struct backtrace_state
*state ATTRIBUTE_UNUSED
,
507 uintptr_t pc ATTRIBUTE_UNUSED
,
508 backtrace_full_callback callback ATTRIBUTE_UNUSED
,
509 backtrace_error_callback error_callback
, void *data
)
511 error_callback (data
, "no debug info in ELF executable", -1);
515 /* A dummy callback function used when we can't find a symbol
519 elf_nosyms (struct backtrace_state
*state ATTRIBUTE_UNUSED
,
520 uintptr_t addr ATTRIBUTE_UNUSED
,
521 backtrace_syminfo_callback callback ATTRIBUTE_UNUSED
,
522 backtrace_error_callback error_callback
, void *data
)
524 error_callback (data
, "no symbol table in ELF executable", -1);
527 /* Compare struct elf_symbol for qsort. */
530 elf_symbol_compare (const void *v1
, const void *v2
)
532 const struct elf_symbol
*e1
= (const struct elf_symbol
*) v1
;
533 const struct elf_symbol
*e2
= (const struct elf_symbol
*) v2
;
535 if (e1
->address
< e2
->address
)
537 else if (e1
->address
> e2
->address
)
543 /* Compare an ADDR against an elf_symbol for bsearch. We allocate one
544 extra entry in the array so that this can look safely at the next
548 elf_symbol_search (const void *vkey
, const void *ventry
)
550 const uintptr_t *key
= (const uintptr_t *) vkey
;
551 const struct elf_symbol
*entry
= (const struct elf_symbol
*) ventry
;
555 if (addr
< entry
->address
)
557 else if (addr
>= entry
->address
+ entry
->size
)
563 /* Initialize the symbol table info for elf_syminfo. */
566 elf_initialize_syminfo (struct backtrace_state
*state
,
567 uintptr_t base_address
,
568 const unsigned char *symtab_data
, size_t symtab_size
,
569 const unsigned char *strtab
, size_t strtab_size
,
570 backtrace_error_callback error_callback
,
571 void *data
, struct elf_syminfo_data
*sdata
,
572 struct elf_ppc64_opd_data
*opd
)
575 const b_elf_sym
*sym
;
576 size_t elf_symbol_count
;
577 size_t elf_symbol_size
;
578 struct elf_symbol
*elf_symbols
;
582 sym_count
= symtab_size
/ sizeof (b_elf_sym
);
584 /* We only care about function symbols. Count them. */
585 sym
= (const b_elf_sym
*) symtab_data
;
586 elf_symbol_count
= 0;
587 for (i
= 0; i
< sym_count
; ++i
, ++sym
)
591 info
= sym
->st_info
& 0xf;
592 if ((info
== STT_FUNC
|| info
== STT_OBJECT
)
593 && sym
->st_shndx
!= SHN_UNDEF
)
597 elf_symbol_size
= elf_symbol_count
* sizeof (struct elf_symbol
);
598 elf_symbols
= ((struct elf_symbol
*)
599 backtrace_alloc (state
, elf_symbol_size
, error_callback
,
601 if (elf_symbols
== NULL
)
604 sym
= (const b_elf_sym
*) symtab_data
;
606 for (i
= 0; i
< sym_count
; ++i
, ++sym
)
610 info
= sym
->st_info
& 0xf;
611 if (info
!= STT_FUNC
&& info
!= STT_OBJECT
)
613 if (sym
->st_shndx
== SHN_UNDEF
)
615 if (sym
->st_name
>= strtab_size
)
617 error_callback (data
, "symbol string index out of range", 0);
618 backtrace_free (state
, elf_symbols
, elf_symbol_size
, error_callback
,
622 elf_symbols
[j
].name
= (const char *) strtab
+ sym
->st_name
;
623 /* Special case PowerPC64 ELFv1 symbols in .opd section, if the symbol
624 is a function descriptor, read the actual code address from the
627 && sym
->st_value
>= opd
->addr
628 && sym
->st_value
< opd
->addr
+ opd
->size
)
629 elf_symbols
[j
].address
630 = *(const b_elf_addr
*) (opd
->data
+ (sym
->st_value
- opd
->addr
));
632 elf_symbols
[j
].address
= sym
->st_value
;
633 elf_symbols
[j
].address
+= base_address
;
634 elf_symbols
[j
].size
= sym
->st_size
;
638 backtrace_qsort (elf_symbols
, elf_symbol_count
, sizeof (struct elf_symbol
),
642 sdata
->symbols
= elf_symbols
;
643 sdata
->count
= elf_symbol_count
;
648 /* Add EDATA to the list in STATE. */
651 elf_add_syminfo_data (struct backtrace_state
*state
,
652 struct elf_syminfo_data
*edata
)
654 if (!state
->threaded
)
656 struct elf_syminfo_data
**pp
;
658 for (pp
= (struct elf_syminfo_data
**) (void *) &state
->syminfo_data
;
668 struct elf_syminfo_data
**pp
;
670 pp
= (struct elf_syminfo_data
**) (void *) &state
->syminfo_data
;
674 struct elf_syminfo_data
*p
;
676 p
= backtrace_atomic_load_pointer (pp
);
684 if (__sync_bool_compare_and_swap (pp
, NULL
, edata
))
690 /* Return the symbol name and value for an ADDR. */
693 elf_syminfo (struct backtrace_state
*state
, uintptr_t addr
,
694 backtrace_syminfo_callback callback
,
695 backtrace_error_callback error_callback ATTRIBUTE_UNUSED
,
698 struct elf_syminfo_data
*edata
;
699 struct elf_symbol
*sym
= NULL
;
701 if (!state
->threaded
)
703 for (edata
= (struct elf_syminfo_data
*) state
->syminfo_data
;
707 sym
= ((struct elf_symbol
*)
708 bsearch (&addr
, edata
->symbols
, edata
->count
,
709 sizeof (struct elf_symbol
), elf_symbol_search
));
716 struct elf_syminfo_data
**pp
;
718 pp
= (struct elf_syminfo_data
**) (void *) &state
->syminfo_data
;
721 edata
= backtrace_atomic_load_pointer (pp
);
725 sym
= ((struct elf_symbol
*)
726 bsearch (&addr
, edata
->symbols
, edata
->count
,
727 sizeof (struct elf_symbol
), elf_symbol_search
));
736 callback (data
, addr
, NULL
, 0, 0);
738 callback (data
, addr
, sym
->name
, sym
->address
, sym
->size
);
741 /* Return whether FILENAME is a symlink. */
744 elf_is_symlink (const char *filename
)
748 if (lstat (filename
, &st
) < 0)
750 return S_ISLNK (st
.st_mode
);
753 /* Return the results of reading the symlink FILENAME in a buffer
754 allocated by backtrace_alloc. Return the length of the buffer in
758 elf_readlink (struct backtrace_state
*state
, const char *filename
,
759 backtrace_error_callback error_callback
, void *data
,
770 buf
= backtrace_alloc (state
, len
, error_callback
, data
);
773 rl
= readlink (filename
, buf
, len
);
776 backtrace_free (state
, buf
, len
, error_callback
, data
);
779 if ((size_t) rl
< len
- 1)
785 backtrace_free (state
, buf
, len
, error_callback
, data
);
790 #define SYSTEM_BUILD_ID_DIR "/usr/lib/debug/.build-id/"
792 /* Open a separate debug info file, using the build ID to find it.
793 Returns an open file descriptor, or -1.
795 The GDB manual says that the only place gdb looks for a debug file
796 when the build ID is known is in /usr/lib/debug/.build-id. */
799 elf_open_debugfile_by_buildid (struct backtrace_state
*state
,
800 const char *buildid_data
, size_t buildid_size
,
801 backtrace_error_callback error_callback
,
804 const char * const prefix
= SYSTEM_BUILD_ID_DIR
;
805 const size_t prefix_len
= strlen (prefix
);
806 const char * const suffix
= ".debug";
807 const size_t suffix_len
= strlen (suffix
);
815 len
= prefix_len
+ buildid_size
* 2 + suffix_len
+ 2;
816 bd_filename
= backtrace_alloc (state
, len
, error_callback
, data
);
817 if (bd_filename
== NULL
)
821 memcpy (t
, prefix
, prefix_len
);
823 for (i
= 0; i
< buildid_size
; i
++)
828 b
= (unsigned char) buildid_data
[i
];
829 nib
= (b
& 0xf0) >> 4;
830 *t
++ = nib
< 10 ? '0' + nib
: 'a' + nib
- 10;
832 *t
++ = nib
< 10 ? '0' + nib
: 'a' + nib
- 10;
836 memcpy (t
, suffix
, suffix_len
);
837 t
[suffix_len
] = '\0';
839 ret
= backtrace_open (bd_filename
, error_callback
, data
, &does_not_exist
);
841 backtrace_free (state
, bd_filename
, len
, error_callback
, data
);
843 /* gdb checks that the debuginfo file has the same build ID note.
844 That seems kind of pointless to me--why would it have the right
845 name but not the right build ID?--so skipping the check. */
850 /* Try to open a file whose name is PREFIX (length PREFIX_LEN)
851 concatenated with PREFIX2 (length PREFIX2_LEN) concatenated with
852 DEBUGLINK_NAME. Returns an open file descriptor, or -1. */
855 elf_try_debugfile (struct backtrace_state
*state
, const char *prefix
,
856 size_t prefix_len
, const char *prefix2
, size_t prefix2_len
,
857 const char *debuglink_name
,
858 backtrace_error_callback error_callback
, void *data
)
860 size_t debuglink_len
;
866 debuglink_len
= strlen (debuglink_name
);
867 try_len
= prefix_len
+ prefix2_len
+ debuglink_len
+ 1;
868 try = backtrace_alloc (state
, try_len
, error_callback
, data
);
872 memcpy (try, prefix
, prefix_len
);
873 memcpy (try + prefix_len
, prefix2
, prefix2_len
);
874 memcpy (try + prefix_len
+ prefix2_len
, debuglink_name
, debuglink_len
);
875 try[prefix_len
+ prefix2_len
+ debuglink_len
] = '\0';
877 ret
= backtrace_open (try, error_callback
, data
, &does_not_exist
);
879 backtrace_free (state
, try, try_len
, error_callback
, data
);
884 /* Find a separate debug info file, using the debuglink section data
885 to find it. Returns an open file descriptor, or -1. */
888 elf_find_debugfile_by_debuglink (struct backtrace_state
*state
,
889 const char *filename
,
890 const char *debuglink_name
,
891 backtrace_error_callback error_callback
,
902 /* Resolve symlinks in FILENAME. Since FILENAME is fairly likely to
903 be /proc/self/exe, symlinks are common. We don't try to resolve
904 the whole path name, just the base name. */
908 while (elf_is_symlink (filename
))
913 new_buf
= elf_readlink (state
, filename
, error_callback
, data
, &new_len
);
917 if (new_buf
[0] == '/')
921 slash
= strrchr (filename
, '/');
930 clen
= slash
- filename
+ strlen (new_buf
) + 1;
931 c
= backtrace_alloc (state
, clen
, error_callback
, data
);
935 memcpy (c
, filename
, slash
- filename
);
936 memcpy (c
+ (slash
- filename
), new_buf
, strlen (new_buf
));
937 c
[slash
- filename
+ strlen (new_buf
)] = '\0';
938 backtrace_free (state
, new_buf
, new_len
, error_callback
, data
);
946 backtrace_free (state
, alc
, alc_len
, error_callback
, data
);
951 /* Look for DEBUGLINK_NAME in the same directory as FILENAME. */
953 slash
= strrchr (filename
, '/');
963 prefix_len
= slash
- filename
;
966 ddescriptor
= elf_try_debugfile (state
, prefix
, prefix_len
, "", 0,
967 debuglink_name
, error_callback
, data
);
968 if (ddescriptor
>= 0)
974 /* Look for DEBUGLINK_NAME in a .debug subdirectory of FILENAME. */
976 ddescriptor
= elf_try_debugfile (state
, prefix
, prefix_len
, ".debug/",
977 strlen (".debug/"), debuglink_name
,
978 error_callback
, data
);
979 if (ddescriptor
>= 0)
985 /* Look for DEBUGLINK_NAME in /usr/lib/debug. */
987 ddescriptor
= elf_try_debugfile (state
, "/usr/lib/debug/",
988 strlen ("/usr/lib/debug/"), prefix
,
989 prefix_len
, debuglink_name
,
990 error_callback
, data
);
991 if (ddescriptor
>= 0)
995 if (alc
!= NULL
&& alc_len
> 0)
996 backtrace_free (state
, alc
, alc_len
, error_callback
, data
);
1000 /* Open a separate debug info file, using the debuglink section data
1001 to find it. Returns an open file descriptor, or -1. */
1004 elf_open_debugfile_by_debuglink (struct backtrace_state
*state
,
1005 const char *filename
,
1006 const char *debuglink_name
,
1007 uint32_t debuglink_crc
,
1008 backtrace_error_callback error_callback
,
1013 ddescriptor
= elf_find_debugfile_by_debuglink (state
, filename
,
1015 error_callback
, data
);
1016 if (ddescriptor
< 0)
1019 if (debuglink_crc
!= 0)
1023 got_crc
= elf_crc32_file (state
, ddescriptor
, error_callback
, data
);
1024 if (got_crc
!= debuglink_crc
)
1026 backtrace_close (ddescriptor
, error_callback
, data
);
1034 /* A function useful for setting a breakpoint for an inflation failure
1035 when this code is compiled with -g. */
1038 elf_zlib_failed(void)
1042 /* *PVAL is the current value being read from the stream, and *PBITS
1043 is the number of valid bits. Ensure that *PVAL holds at least 15
1044 bits by reading additional bits from *PPIN, up to PINEND, as
1045 needed. Updates *PPIN, *PVAL and *PBITS. Returns 1 on success, 0
1049 elf_zlib_fetch (const unsigned char **ppin
, const unsigned char *pinend
,
1050 uint64_t *pval
, unsigned int *pbits
)
1053 const unsigned char *pin
;
1063 if (unlikely (pinend
- pin
< 4))
1069 #if defined(__BYTE_ORDER__) && defined(__ORDER_LITTLE_ENDIAN__) \
1070 && defined(__ORDER_BIG_ENDIAN__) \
1071 && (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ \
1072 || __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__)
1073 /* We've ensured that PIN is aligned. */
1074 next
= *(const uint32_t *)pin
;
1076 #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
1077 next
= __builtin_bswap32 (next
);
1080 next
= pin
[0] | (pin
[1] << 8) | (pin
[2] << 16) | (pin
[3] << 24);
1083 val
|= (uint64_t)next
<< bits
;
1087 /* We will need the next four bytes soon. */
1088 __builtin_prefetch (pin
, 0, 0);
1096 /* Huffman code tables, like the rest of the zlib format, are defined
1097 by RFC 1951. We store a Huffman code table as a series of tables
1098 stored sequentially in memory. Each entry in a table is 16 bits.
1099 The first, main, table has 256 entries. It is followed by a set of
1100 secondary tables of length 2 to 128 entries. The maximum length of
1101 a code sequence in the deflate format is 15 bits, so that is all we
1102 need. Each secondary table has an index, which is the offset of
1103 the table in the overall memory storage.
1105 The deflate format says that all codes of a given bit length are
1106 lexicographically consecutive. Perhaps we could have 130 values
1107 that require a 15-bit code, perhaps requiring three secondary
1108 tables of size 128. I don't know if this is actually possible, but
1109 it suggests that the maximum size required for secondary tables is
1110 3 * 128 + 3 * 64 ... == 768. The zlib enough program reports 660
1111 as the maximum. We permit 768, since in addition to the 256 for
1112 the primary table, with two bytes per entry, and with the two
1113 tables we need, that gives us a page.
1115 A single table entry needs to store a value or (for the main table
1116 only) the index and size of a secondary table. Values range from 0
1117 to 285, inclusive. Secondary table indexes, per above, range from
1118 0 to 510. For a value we need to store the number of bits we need
1119 to determine that value (one value may appear multiple times in the
1120 table), which is 1 to 8. For a secondary table we need to store
1121 the number of bits used to index into the table, which is 1 to 7.
1122 And of course we need 1 bit to decide whether we have a value or a
1123 secondary table index. So each entry needs 9 bits for value/table
1124 index, 3 bits for size, 1 bit what it is. For simplicity we use 16
1127 /* Number of entries we allocate to for one code table. We get a page
1128 for the two code tables we need. */
1130 #define HUFFMAN_TABLE_SIZE (1024)
1132 /* Bit masks and shifts for the values in the table. */
1134 #define HUFFMAN_VALUE_MASK 0x01ff
1135 #define HUFFMAN_BITS_SHIFT 9
1136 #define HUFFMAN_BITS_MASK 0x7
1137 #define HUFFMAN_SECONDARY_SHIFT 12
1139 /* For working memory while inflating we need two code tables, we need
1140 an array of code lengths (max value 15, so we use unsigned char),
1141 and an array of unsigned shorts used while building a table. The
1142 latter two arrays must be large enough to hold the maximum number
1143 of code lengths, which RFC 1951 defines as 286 + 30. */
1145 #define ZDEBUG_TABLE_SIZE \
1146 (2 * HUFFMAN_TABLE_SIZE * sizeof (uint16_t) \
1147 + (286 + 30) * sizeof (uint16_t) \
1148 + (286 + 30) * sizeof (unsigned char))
1150 #define ZDEBUG_TABLE_CODELEN_OFFSET \
1151 (2 * HUFFMAN_TABLE_SIZE * sizeof (uint16_t) \
1152 + (286 + 30) * sizeof (uint16_t))
1154 #define ZDEBUG_TABLE_WORK_OFFSET \
1155 (2 * HUFFMAN_TABLE_SIZE * sizeof (uint16_t))
1157 #ifdef BACKTRACE_GENERATE_FIXED_HUFFMAN_TABLE
1159 /* Used by the main function that generates the fixed table to learn
1161 static size_t final_next_secondary
;
1165 /* Build a Huffman code table from an array of lengths in CODES of
1166 length CODES_LEN. The table is stored into *TABLE. ZDEBUG_TABLE
1167 is the same as for elf_zlib_inflate, used to find some work space.
1168 Returns 1 on success, 0 on error. */
1171 elf_zlib_inflate_table (unsigned char *codes
, size_t codes_len
,
1172 uint16_t *zdebug_table
, uint16_t *table
)
1177 uint16_t firstcode
[7];
1182 size_t next_secondary
;
1184 /* Count the number of code of each length. Set NEXT[val] to be the
1185 next value after VAL with the same bit length. */
1187 next
= (uint16_t *) (((unsigned char *) zdebug_table
)
1188 + ZDEBUG_TABLE_WORK_OFFSET
);
1190 memset (&count
[0], 0, 16 * sizeof (uint16_t));
1191 for (i
= 0; i
< codes_len
; ++i
)
1193 if (unlikely (codes
[i
] >= 16))
1199 if (count
[codes
[i
]] == 0)
1201 start
[codes
[i
]] = i
;
1206 next
[prev
[codes
[i
]]] = i
;
1213 /* For each length, fill in the table for the codes of that
1216 memset (table
, 0, HUFFMAN_TABLE_SIZE
* sizeof (uint16_t));
1218 /* Handle the values that do not require a secondary table. */
1221 for (j
= 1; j
<= 8; ++j
)
1230 if (unlikely (jcnt
> (1U << j
)))
1236 /* There are JCNT values that have this length, the values
1237 starting from START[j] continuing through NEXT[VAL]. Those
1238 values are assigned consecutive values starting at CODE. */
1241 for (i
= 0; i
< jcnt
; ++i
)
1247 /* In the compressed bit stream, the value VAL is encoded as
1248 J bits with the value C. */
1250 if (unlikely ((val
& ~HUFFMAN_VALUE_MASK
) != 0))
1256 tval
= val
| ((j
- 1) << HUFFMAN_BITS_SHIFT
);
1258 /* The table lookup uses 8 bits. If J is less than 8, we
1259 don't know what the other bits will be. We need to fill
1260 in all possibilities in the table. Since the Huffman
1261 code is unambiguous, those entries can't be used for any
1264 for (ind
= code
; ind
< 0x100; ind
+= 1 << j
)
1266 if (unlikely (table
[ind
] != 0))
1274 /* Advance to the next value with this length. */
1278 /* The Huffman codes are stored in the bitstream with the
1279 most significant bit first, as is required to make them
1280 unambiguous. The effect is that when we read them from
1281 the bitstream we see the bit sequence in reverse order:
1282 the most significant bit of the Huffman code is the least
1283 significant bit of the value we read from the bitstream.
1284 That means that to make our table lookups work, we need
1285 to reverse the bits of CODE. Since reversing bits is
1286 tedious and in general requires using a table, we instead
1287 increment CODE in reverse order. That is, if the number
1288 of bits we are currently using, here named J, is 3, we
1289 count as 000, 100, 010, 110, 001, 101, 011, 111, which is
1290 to say the numbers from 0 to 7 but with the bits
1291 reversed. Going to more bits, aka incrementing J,
1292 effectively just adds more zero bits as the beginning,
1293 and as such does not change the numeric value of CODE.
1295 To increment CODE of length J in reverse order, find the
1296 most significant zero bit and set it to one while
1297 clearing all higher bits. In other words, add 1 modulo
1298 2^J, only reversed. */
1300 incr
= 1U << (j
- 1);
1301 while ((code
& incr
) != 0)
1313 /* Handle the values that require a secondary table. */
1315 /* Set FIRSTCODE, the number at which the codes start, for each
1318 for (j
= 9; j
< 16; j
++)
1327 /* There are JCNT values that have this length, the values
1328 starting from START[j]. Those values are assigned
1329 consecutive values starting at CODE. */
1331 firstcode
[j
- 9] = code
;
1333 /* Reverse add JCNT to CODE modulo 2^J. */
1334 for (k
= 0; k
< j
; ++k
)
1336 if ((jcnt
& (1U << k
)) != 0)
1341 bit
= 1U << (j
- k
- 1);
1342 for (m
= 0; m
< j
- k
; ++m
, bit
>>= 1)
1344 if ((code
& bit
) == 0)
1354 if (unlikely (jcnt
!= 0))
1361 /* For J from 9 to 15, inclusive, we store COUNT[J] consecutive
1362 values starting at START[J] with consecutive codes starting at
1363 FIRSTCODE[J - 9]. In the primary table we need to point to the
1364 secondary table, and the secondary table will be indexed by J - 9
1365 bits. We count down from 15 so that we install the larger
1366 secondary tables first, as the smaller ones may be embedded in
1369 next_secondary
= 0; /* Index of next secondary table (after primary). */
1370 for (j
= 15; j
>= 9; j
--)
1374 size_t primary
; /* Current primary index. */
1375 size_t secondary
; /* Offset to current secondary table. */
1376 size_t secondary_bits
; /* Bit size of current secondary table. */
1383 code
= firstcode
[j
- 9];
1387 for (i
= 0; i
< jcnt
; ++i
)
1393 if ((code
& 0xff) != primary
)
1397 /* Fill in a new primary table entry. */
1399 primary
= code
& 0xff;
1401 tprimary
= table
[primary
];
1404 /* Start a new secondary table. */
1406 if (unlikely ((next_secondary
& HUFFMAN_VALUE_MASK
)
1413 secondary
= next_secondary
;
1414 secondary_bits
= j
- 8;
1415 next_secondary
+= 1 << secondary_bits
;
1416 table
[primary
] = (secondary
1417 + ((j
- 8) << HUFFMAN_BITS_SHIFT
)
1418 + (1U << HUFFMAN_SECONDARY_SHIFT
));
1422 /* There is an existing entry. It had better be a
1423 secondary table with enough bits. */
1424 if (unlikely ((tprimary
& (1U << HUFFMAN_SECONDARY_SHIFT
))
1430 secondary
= tprimary
& HUFFMAN_VALUE_MASK
;
1431 secondary_bits
= ((tprimary
>> HUFFMAN_BITS_SHIFT
)
1432 & HUFFMAN_BITS_MASK
);
1433 if (unlikely (secondary_bits
< j
- 8))
1441 /* Fill in secondary table entries. */
1443 tval
= val
| ((j
- 8) << HUFFMAN_BITS_SHIFT
);
1445 for (ind
= code
>> 8;
1446 ind
< (1U << secondary_bits
);
1447 ind
+= 1U << (j
- 8))
1449 if (unlikely (table
[secondary
+ 0x100 + ind
] != 0))
1454 table
[secondary
+ 0x100 + ind
] = tval
;
1460 incr
= 1U << (j
- 1);
1461 while ((code
& incr
) != 0)
1473 #ifdef BACKTRACE_GENERATE_FIXED_HUFFMAN_TABLE
1474 final_next_secondary
= next_secondary
;
1480 #ifdef BACKTRACE_GENERATE_FIXED_HUFFMAN_TABLE
1482 /* Used to generate the fixed Huffman table for block type 1. */
1486 static uint16_t table
[ZDEBUG_TABLE_SIZE
];
1487 static unsigned char codes
[288];
1494 for (i
= 0; i
<= 143; ++i
)
1496 for (i
= 144; i
<= 255; ++i
)
1498 for (i
= 256; i
<= 279; ++i
)
1500 for (i
= 280; i
<= 287; ++i
)
1502 if (!elf_zlib_inflate_table (&codes
[0], 288, &table
[0], &table
[0]))
1504 fprintf (stderr
, "elf_zlib_inflate_table failed\n");
1505 exit (EXIT_FAILURE
);
1508 printf ("static const uint16_t elf_zlib_default_table[%#zx] =\n",
1509 final_next_secondary
+ 0x100);
1511 for (i
= 0; i
< final_next_secondary
+ 0x100; i
+= 8)
1516 for (j
= i
; j
< final_next_secondary
+ 0x100 && j
< i
+ 8; ++j
)
1517 printf (" %#x,", table
[j
]);
1523 for (i
= 0; i
< 32; ++i
)
1525 if (!elf_zlib_inflate_table (&codes
[0], 32, &table
[0], &table
[0]))
1527 fprintf (stderr
, "elf_zlib_inflate_table failed\n");
1528 exit (EXIT_FAILURE
);
1531 printf ("static const uint16_t elf_zlib_default_dist_table[%#zx] =\n",
1532 final_next_secondary
+ 0x100);
1534 for (i
= 0; i
< final_next_secondary
+ 0x100; i
+= 8)
1539 for (j
= i
; j
< final_next_secondary
+ 0x100 && j
< i
+ 8; ++j
)
1540 printf (" %#x,", table
[j
]);
1550 /* The fixed tables generated by the #ifdef'ed out main function
1553 static const uint16_t elf_zlib_default_table
[0x170] =
1555 0xd00, 0xe50, 0xe10, 0xf18, 0xd10, 0xe70, 0xe30, 0x1230,
1556 0xd08, 0xe60, 0xe20, 0x1210, 0xe00, 0xe80, 0xe40, 0x1250,
1557 0xd04, 0xe58, 0xe18, 0x1200, 0xd14, 0xe78, 0xe38, 0x1240,
1558 0xd0c, 0xe68, 0xe28, 0x1220, 0xe08, 0xe88, 0xe48, 0x1260,
1559 0xd02, 0xe54, 0xe14, 0xf1c, 0xd12, 0xe74, 0xe34, 0x1238,
1560 0xd0a, 0xe64, 0xe24, 0x1218, 0xe04, 0xe84, 0xe44, 0x1258,
1561 0xd06, 0xe5c, 0xe1c, 0x1208, 0xd16, 0xe7c, 0xe3c, 0x1248,
1562 0xd0e, 0xe6c, 0xe2c, 0x1228, 0xe0c, 0xe8c, 0xe4c, 0x1268,
1563 0xd01, 0xe52, 0xe12, 0xf1a, 0xd11, 0xe72, 0xe32, 0x1234,
1564 0xd09, 0xe62, 0xe22, 0x1214, 0xe02, 0xe82, 0xe42, 0x1254,
1565 0xd05, 0xe5a, 0xe1a, 0x1204, 0xd15, 0xe7a, 0xe3a, 0x1244,
1566 0xd0d, 0xe6a, 0xe2a, 0x1224, 0xe0a, 0xe8a, 0xe4a, 0x1264,
1567 0xd03, 0xe56, 0xe16, 0xf1e, 0xd13, 0xe76, 0xe36, 0x123c,
1568 0xd0b, 0xe66, 0xe26, 0x121c, 0xe06, 0xe86, 0xe46, 0x125c,
1569 0xd07, 0xe5e, 0xe1e, 0x120c, 0xd17, 0xe7e, 0xe3e, 0x124c,
1570 0xd0f, 0xe6e, 0xe2e, 0x122c, 0xe0e, 0xe8e, 0xe4e, 0x126c,
1571 0xd00, 0xe51, 0xe11, 0xf19, 0xd10, 0xe71, 0xe31, 0x1232,
1572 0xd08, 0xe61, 0xe21, 0x1212, 0xe01, 0xe81, 0xe41, 0x1252,
1573 0xd04, 0xe59, 0xe19, 0x1202, 0xd14, 0xe79, 0xe39, 0x1242,
1574 0xd0c, 0xe69, 0xe29, 0x1222, 0xe09, 0xe89, 0xe49, 0x1262,
1575 0xd02, 0xe55, 0xe15, 0xf1d, 0xd12, 0xe75, 0xe35, 0x123a,
1576 0xd0a, 0xe65, 0xe25, 0x121a, 0xe05, 0xe85, 0xe45, 0x125a,
1577 0xd06, 0xe5d, 0xe1d, 0x120a, 0xd16, 0xe7d, 0xe3d, 0x124a,
1578 0xd0e, 0xe6d, 0xe2d, 0x122a, 0xe0d, 0xe8d, 0xe4d, 0x126a,
1579 0xd01, 0xe53, 0xe13, 0xf1b, 0xd11, 0xe73, 0xe33, 0x1236,
1580 0xd09, 0xe63, 0xe23, 0x1216, 0xe03, 0xe83, 0xe43, 0x1256,
1581 0xd05, 0xe5b, 0xe1b, 0x1206, 0xd15, 0xe7b, 0xe3b, 0x1246,
1582 0xd0d, 0xe6b, 0xe2b, 0x1226, 0xe0b, 0xe8b, 0xe4b, 0x1266,
1583 0xd03, 0xe57, 0xe17, 0xf1f, 0xd13, 0xe77, 0xe37, 0x123e,
1584 0xd0b, 0xe67, 0xe27, 0x121e, 0xe07, 0xe87, 0xe47, 0x125e,
1585 0xd07, 0xe5f, 0xe1f, 0x120e, 0xd17, 0xe7f, 0xe3f, 0x124e,
1586 0xd0f, 0xe6f, 0xe2f, 0x122e, 0xe0f, 0xe8f, 0xe4f, 0x126e,
1587 0x290, 0x291, 0x292, 0x293, 0x294, 0x295, 0x296, 0x297,
1588 0x298, 0x299, 0x29a, 0x29b, 0x29c, 0x29d, 0x29e, 0x29f,
1589 0x2a0, 0x2a1, 0x2a2, 0x2a3, 0x2a4, 0x2a5, 0x2a6, 0x2a7,
1590 0x2a8, 0x2a9, 0x2aa, 0x2ab, 0x2ac, 0x2ad, 0x2ae, 0x2af,
1591 0x2b0, 0x2b1, 0x2b2, 0x2b3, 0x2b4, 0x2b5, 0x2b6, 0x2b7,
1592 0x2b8, 0x2b9, 0x2ba, 0x2bb, 0x2bc, 0x2bd, 0x2be, 0x2bf,
1593 0x2c0, 0x2c1, 0x2c2, 0x2c3, 0x2c4, 0x2c5, 0x2c6, 0x2c7,
1594 0x2c8, 0x2c9, 0x2ca, 0x2cb, 0x2cc, 0x2cd, 0x2ce, 0x2cf,
1595 0x2d0, 0x2d1, 0x2d2, 0x2d3, 0x2d4, 0x2d5, 0x2d6, 0x2d7,
1596 0x2d8, 0x2d9, 0x2da, 0x2db, 0x2dc, 0x2dd, 0x2de, 0x2df,
1597 0x2e0, 0x2e1, 0x2e2, 0x2e3, 0x2e4, 0x2e5, 0x2e6, 0x2e7,
1598 0x2e8, 0x2e9, 0x2ea, 0x2eb, 0x2ec, 0x2ed, 0x2ee, 0x2ef,
1599 0x2f0, 0x2f1, 0x2f2, 0x2f3, 0x2f4, 0x2f5, 0x2f6, 0x2f7,
1600 0x2f8, 0x2f9, 0x2fa, 0x2fb, 0x2fc, 0x2fd, 0x2fe, 0x2ff,
1603 static const uint16_t elf_zlib_default_dist_table
[0x100] =
1605 0x800, 0x810, 0x808, 0x818, 0x804, 0x814, 0x80c, 0x81c,
1606 0x802, 0x812, 0x80a, 0x81a, 0x806, 0x816, 0x80e, 0x81e,
1607 0x801, 0x811, 0x809, 0x819, 0x805, 0x815, 0x80d, 0x81d,
1608 0x803, 0x813, 0x80b, 0x81b, 0x807, 0x817, 0x80f, 0x81f,
1609 0x800, 0x810, 0x808, 0x818, 0x804, 0x814, 0x80c, 0x81c,
1610 0x802, 0x812, 0x80a, 0x81a, 0x806, 0x816, 0x80e, 0x81e,
1611 0x801, 0x811, 0x809, 0x819, 0x805, 0x815, 0x80d, 0x81d,
1612 0x803, 0x813, 0x80b, 0x81b, 0x807, 0x817, 0x80f, 0x81f,
1613 0x800, 0x810, 0x808, 0x818, 0x804, 0x814, 0x80c, 0x81c,
1614 0x802, 0x812, 0x80a, 0x81a, 0x806, 0x816, 0x80e, 0x81e,
1615 0x801, 0x811, 0x809, 0x819, 0x805, 0x815, 0x80d, 0x81d,
1616 0x803, 0x813, 0x80b, 0x81b, 0x807, 0x817, 0x80f, 0x81f,
1617 0x800, 0x810, 0x808, 0x818, 0x804, 0x814, 0x80c, 0x81c,
1618 0x802, 0x812, 0x80a, 0x81a, 0x806, 0x816, 0x80e, 0x81e,
1619 0x801, 0x811, 0x809, 0x819, 0x805, 0x815, 0x80d, 0x81d,
1620 0x803, 0x813, 0x80b, 0x81b, 0x807, 0x817, 0x80f, 0x81f,
1621 0x800, 0x810, 0x808, 0x818, 0x804, 0x814, 0x80c, 0x81c,
1622 0x802, 0x812, 0x80a, 0x81a, 0x806, 0x816, 0x80e, 0x81e,
1623 0x801, 0x811, 0x809, 0x819, 0x805, 0x815, 0x80d, 0x81d,
1624 0x803, 0x813, 0x80b, 0x81b, 0x807, 0x817, 0x80f, 0x81f,
1625 0x800, 0x810, 0x808, 0x818, 0x804, 0x814, 0x80c, 0x81c,
1626 0x802, 0x812, 0x80a, 0x81a, 0x806, 0x816, 0x80e, 0x81e,
1627 0x801, 0x811, 0x809, 0x819, 0x805, 0x815, 0x80d, 0x81d,
1628 0x803, 0x813, 0x80b, 0x81b, 0x807, 0x817, 0x80f, 0x81f,
1629 0x800, 0x810, 0x808, 0x818, 0x804, 0x814, 0x80c, 0x81c,
1630 0x802, 0x812, 0x80a, 0x81a, 0x806, 0x816, 0x80e, 0x81e,
1631 0x801, 0x811, 0x809, 0x819, 0x805, 0x815, 0x80d, 0x81d,
1632 0x803, 0x813, 0x80b, 0x81b, 0x807, 0x817, 0x80f, 0x81f,
1633 0x800, 0x810, 0x808, 0x818, 0x804, 0x814, 0x80c, 0x81c,
1634 0x802, 0x812, 0x80a, 0x81a, 0x806, 0x816, 0x80e, 0x81e,
1635 0x801, 0x811, 0x809, 0x819, 0x805, 0x815, 0x80d, 0x81d,
1636 0x803, 0x813, 0x80b, 0x81b, 0x807, 0x817, 0x80f, 0x81f,
1639 /* Inflate a zlib stream from PIN/SIN to POUT/SOUT. Return 1 on
1640 success, 0 on some error parsing the stream. */
1643 elf_zlib_inflate (const unsigned char *pin
, size_t sin
, uint16_t *zdebug_table
,
1644 unsigned char *pout
, size_t sout
)
1646 unsigned char *porigout
;
1647 const unsigned char *pinend
;
1648 unsigned char *poutend
;
1650 /* We can apparently see multiple zlib streams concatenated
1651 together, so keep going as long as there is something to read.
1652 The last 4 bytes are the checksum. */
1655 poutend
= pout
+ sout
;
1656 while ((pinend
- pin
) > 4)
1662 /* Read the two byte zlib header. */
1664 if (unlikely ((pin
[0] & 0xf) != 8)) /* 8 is zlib encoding. */
1666 /* Unknown compression method. */
1670 if (unlikely ((pin
[0] >> 4) > 7))
1672 /* Window size too large. Other than this check, we don't
1673 care about the window size. */
1677 if (unlikely ((pin
[1] & 0x20) != 0))
1679 /* Stream expects a predefined dictionary, but we have no
1684 val
= (pin
[0] << 8) | pin
[1];
1685 if (unlikely (val
% 31 != 0))
1687 /* Header check failure. */
1693 /* Align PIN to a 32-bit boundary. */
1697 while ((((uintptr_t) pin
) & 3) != 0)
1699 val
|= (uint64_t)*pin
<< bits
;
1704 /* Read blocks until one is marked last. */
1711 const uint16_t *tlit
;
1712 const uint16_t *tdist
;
1714 if (!elf_zlib_fetch (&pin
, pinend
, &val
, &bits
))
1718 type
= (val
>> 1) & 3;
1722 if (unlikely (type
== 3))
1724 /* Invalid block type. */
1734 /* An uncompressed block. */
1736 /* If we've read ahead more than a byte, back up. */
1745 if (unlikely ((pinend
- pin
) < 4))
1747 /* Missing length. */
1751 len
= pin
[0] | (pin
[1] << 8);
1752 lenc
= pin
[2] | (pin
[3] << 8);
1755 if (unlikely (len
!= lenc
))
1761 if (unlikely (len
> (unsigned int) (pinend
- pin
)
1762 || len
> (unsigned int) (poutend
- pout
)))
1764 /* Not enough space in buffers. */
1768 memcpy (pout
, pin
, len
);
1773 while ((((uintptr_t) pin
) & 3) != 0)
1775 val
|= (uint64_t)*pin
<< bits
;
1780 /* Go around to read the next block. */
1786 tlit
= elf_zlib_default_table
;
1787 tdist
= elf_zlib_default_dist_table
;
1794 unsigned char codebits
[19];
1795 unsigned char *plenbase
;
1796 unsigned char *plen
;
1797 unsigned char *plenend
;
1799 /* Read a Huffman encoding table. The various magic
1800 numbers here are from RFC 1951. */
1802 if (!elf_zlib_fetch (&pin
, pinend
, &val
, &bits
))
1805 nlit
= (val
& 0x1f) + 257;
1807 ndist
= (val
& 0x1f) + 1;
1809 nclen
= (val
& 0xf) + 4;
1812 if (unlikely (nlit
> 286 || ndist
> 30))
1814 /* Values out of range. */
1819 /* Read and build the table used to compress the
1820 literal, length, and distance codes. */
1822 memset(&codebits
[0], 0, 19);
1824 /* There are always at least 4 elements in the
1827 if (!elf_zlib_fetch (&pin
, pinend
, &val
, &bits
))
1830 codebits
[16] = val
& 7;
1831 codebits
[17] = (val
>> 3) & 7;
1832 codebits
[18] = (val
>> 6) & 7;
1833 codebits
[0] = (val
>> 9) & 7;
1840 codebits
[8] = val
& 7;
1847 if (!elf_zlib_fetch (&pin
, pinend
, &val
, &bits
))
1850 codebits
[7] = val
& 7;
1857 codebits
[9] = val
& 7;
1864 codebits
[6] = val
& 7;
1871 codebits
[10] = val
& 7;
1878 codebits
[5] = val
& 7;
1885 if (!elf_zlib_fetch (&pin
, pinend
, &val
, &bits
))
1888 codebits
[11] = val
& 7;
1895 codebits
[4] = val
& 7;
1902 codebits
[12] = val
& 7;
1909 codebits
[3] = val
& 7;
1916 codebits
[13] = val
& 7;
1923 if (!elf_zlib_fetch (&pin
, pinend
, &val
, &bits
))
1926 codebits
[2] = val
& 7;
1933 codebits
[14] = val
& 7;
1940 codebits
[1] = val
& 7;
1947 codebits
[15] = val
& 7;
1953 if (!elf_zlib_inflate_table (codebits
, 19, zdebug_table
,
1957 /* Read the compressed bit lengths of the literal,
1958 length, and distance codes. We have allocated space
1959 at the end of zdebug_table to hold them. */
1961 plenbase
= (((unsigned char *) zdebug_table
)
1962 + ZDEBUG_TABLE_CODELEN_OFFSET
);
1964 plenend
= plen
+ nlit
+ ndist
;
1965 while (plen
< plenend
)
1971 if (!elf_zlib_fetch (&pin
, pinend
, &val
, &bits
))
1974 t
= zdebug_table
[val
& 0xff];
1976 /* The compression here uses bit lengths up to 7, so
1977 a secondary table is never necessary. */
1978 if (unlikely ((t
& (1U << HUFFMAN_SECONDARY_SHIFT
)) != 0))
1984 b
= (t
>> HUFFMAN_BITS_SHIFT
) & HUFFMAN_BITS_MASK
;
1988 v
= t
& HUFFMAN_VALUE_MASK
;
1996 /* Copy previous entry 3 to 6 times. */
1998 if (unlikely (plen
== plenbase
))
2004 /* We used up to 7 bits since the last
2005 elf_zlib_fetch, so we have at least 8 bits
2008 c
= 3 + (val
& 0x3);
2011 if (unlikely ((unsigned int) (plenend
- plen
) < c
))
2037 /* Store zero 3 to 10 times. */
2039 /* We used up to 7 bits since the last
2040 elf_zlib_fetch, so we have at least 8 bits
2043 c
= 3 + (val
& 0x7);
2046 if (unlikely ((unsigned int) (plenend
- plen
) < c
))
2083 /* Store zero 11 to 138 times. */
2085 /* We used up to 7 bits since the last
2086 elf_zlib_fetch, so we have at least 8 bits
2089 c
= 11 + (val
& 0x7f);
2092 if (unlikely ((unsigned int) (plenend
- plen
) < c
))
2098 memset (plen
, 0, c
);
2108 /* Make sure that the stop code can appear. */
2111 if (unlikely (plen
[256] == 0))
2117 /* Build the decompression tables. */
2119 if (!elf_zlib_inflate_table (plen
, nlit
, zdebug_table
,
2122 if (!elf_zlib_inflate_table (plen
+ nlit
, ndist
, zdebug_table
,
2123 zdebug_table
+ HUFFMAN_TABLE_SIZE
))
2125 tlit
= zdebug_table
;
2126 tdist
= zdebug_table
+ HUFFMAN_TABLE_SIZE
;
2129 /* Inflate values until the end of the block. This is the
2130 main loop of the inflation code. */
2139 if (!elf_zlib_fetch (&pin
, pinend
, &val
, &bits
))
2142 t
= tlit
[val
& 0xff];
2143 b
= (t
>> HUFFMAN_BITS_SHIFT
) & HUFFMAN_BITS_MASK
;
2144 v
= t
& HUFFMAN_VALUE_MASK
;
2146 if ((t
& (1U << HUFFMAN_SECONDARY_SHIFT
)) == 0)
2154 t
= tlit
[v
+ 0x100 + ((val
>> 8) & ((1U << b
) - 1))];
2155 b
= (t
>> HUFFMAN_BITS_SHIFT
) & HUFFMAN_BITS_MASK
;
2156 lit
= t
& HUFFMAN_VALUE_MASK
;
2163 if (unlikely (pout
== poutend
))
2171 /* We will need to write the next byte soon. We ask
2172 for high temporal locality because we will write
2173 to the whole cache line soon. */
2174 __builtin_prefetch (pout
, 1, 3);
2176 else if (lit
== 256)
2178 /* The end of the block. */
2186 /* Convert lit into a length. */
2189 len
= lit
- 257 + 3;
2190 else if (lit
== 285)
2192 else if (unlikely (lit
> 285))
2201 if (!elf_zlib_fetch (&pin
, pinend
, &val
, &bits
))
2204 /* This is an expression for the table of length
2205 codes in RFC 1951 3.2.5. */
2207 extra
= (lit
>> 2) + 1;
2208 len
= (lit
& 3) << extra
;
2210 len
+= ((1U << (extra
- 1)) - 1) << 3;
2211 len
+= val
& ((1U << extra
) - 1);
2216 if (!elf_zlib_fetch (&pin
, pinend
, &val
, &bits
))
2219 t
= tdist
[val
& 0xff];
2220 b
= (t
>> HUFFMAN_BITS_SHIFT
) & HUFFMAN_BITS_MASK
;
2221 v
= t
& HUFFMAN_VALUE_MASK
;
2223 if ((t
& (1U << HUFFMAN_SECONDARY_SHIFT
)) == 0)
2231 t
= tdist
[v
+ 0x100 + ((val
>> 8) & ((1U << b
) - 1))];
2232 b
= (t
>> HUFFMAN_BITS_SHIFT
) & HUFFMAN_BITS_MASK
;
2233 dist
= t
& HUFFMAN_VALUE_MASK
;
2238 /* Convert dist to a distance. */
2242 /* A distance of 1. A common case, meaning
2243 repeat the last character LEN times. */
2245 if (unlikely (pout
== porigout
))
2251 if (unlikely ((unsigned int) (poutend
- pout
) < len
))
2257 memset (pout
, pout
[-1], len
);
2260 else if (unlikely (dist
> 29))
2273 if (!elf_zlib_fetch (&pin
, pinend
, &val
, &bits
))
2276 /* This is an expression for the table of
2277 distance codes in RFC 1951 3.2.5. */
2279 extra
= (dist
>> 1) + 1;
2280 dist
= (dist
& 1) << extra
;
2282 dist
+= ((1U << (extra
- 1)) - 1) << 2;
2283 dist
+= val
& ((1U << extra
) - 1);
2288 /* Go back dist bytes, and copy len bytes from
2291 if (unlikely ((unsigned int) (pout
- porigout
) < dist
))
2297 if (unlikely ((unsigned int) (poutend
- pout
) < len
))
2305 memcpy (pout
, pout
- dist
, len
);
2314 copy
= len
< dist
? len
: dist
;
2315 memcpy (pout
, pout
- dist
, copy
);
2326 /* We should have filled the output buffer. */
2327 if (unlikely (pout
!= poutend
))
2336 /* Verify the zlib checksum. The checksum is in the 4 bytes at
2337 CHECKBYTES, and the uncompressed data is at UNCOMPRESSED /
2338 UNCOMPRESSED_SIZE. Returns 1 on success, 0 on failure. */
2341 elf_zlib_verify_checksum (const unsigned char *checkbytes
,
2342 const unsigned char *uncompressed
,
2343 size_t uncompressed_size
)
2347 const unsigned char *p
;
2353 for (i
= 0; i
< 4; i
++)
2354 cksum
= (cksum
<< 8) | checkbytes
[i
];
2359 /* Minimize modulo operations. */
2362 hsz
= uncompressed_size
;
2365 for (i
= 0; i
< 5552; i
+= 16)
2367 /* Manually unroll loop 16 times. */
2408 /* Manually unroll loop 16 times. */
2445 for (i
= 0; i
< hsz
; ++i
)
2454 if (unlikely ((s2
<< 16) + s1
!= cksum
))
2463 /* Inflate a zlib stream from PIN/SIN to POUT/SOUT, and verify the
2464 checksum. Return 1 on success, 0 on error. */
2467 elf_zlib_inflate_and_verify (const unsigned char *pin
, size_t sin
,
2468 uint16_t *zdebug_table
, unsigned char *pout
,
2471 if (!elf_zlib_inflate (pin
, sin
, zdebug_table
, pout
, sout
))
2473 if (!elf_zlib_verify_checksum (pin
+ sin
- 4, pout
, sout
))
2478 /* Uncompress the old compressed debug format, the one emitted by
2479 --compress-debug-sections=zlib-gnu. The compressed data is in
2480 COMPRESSED / COMPRESSED_SIZE, and the function writes to
2481 *UNCOMPRESSED / *UNCOMPRESSED_SIZE. ZDEBUG_TABLE is work space to
2482 hold Huffman tables. Returns 0 on error, 1 on successful
2483 decompression or if something goes wrong. In general we try to
2484 carry on, by returning 1, even if we can't decompress. */
2487 elf_uncompress_zdebug (struct backtrace_state
*state
,
2488 const unsigned char *compressed
, size_t compressed_size
,
2489 uint16_t *zdebug_table
,
2490 backtrace_error_callback error_callback
, void *data
,
2491 unsigned char **uncompressed
, size_t *uncompressed_size
)
2497 *uncompressed
= NULL
;
2498 *uncompressed_size
= 0;
2500 /* The format starts with the four bytes ZLIB, followed by the 8
2501 byte length of the uncompressed data in big-endian order,
2502 followed by a zlib stream. */
2504 if (compressed_size
< 12 || memcmp (compressed
, "ZLIB", 4) != 0)
2508 for (i
= 0; i
< 8; i
++)
2509 sz
= (sz
<< 8) | compressed
[i
+ 4];
2511 if (*uncompressed
!= NULL
&& *uncompressed_size
>= sz
)
2515 po
= (unsigned char *) backtrace_alloc (state
, sz
, error_callback
, data
);
2520 if (!elf_zlib_inflate_and_verify (compressed
+ 12, compressed_size
- 12,
2521 zdebug_table
, po
, sz
))
2525 *uncompressed_size
= sz
;
2530 /* Uncompress the new compressed debug format, the official standard
2531 ELF approach emitted by --compress-debug-sections=zlib-gabi. The
2532 compressed data is in COMPRESSED / COMPRESSED_SIZE, and the
2533 function writes to *UNCOMPRESSED / *UNCOMPRESSED_SIZE.
2534 ZDEBUG_TABLE is work space as for elf_uncompress_zdebug. Returns 0
2535 on error, 1 on successful decompression or if something goes wrong.
2536 In general we try to carry on, by returning 1, even if we can't
2540 elf_uncompress_chdr (struct backtrace_state
*state
,
2541 const unsigned char *compressed
, size_t compressed_size
,
2542 uint16_t *zdebug_table
,
2543 backtrace_error_callback error_callback
, void *data
,
2544 unsigned char **uncompressed
, size_t *uncompressed_size
)
2546 const b_elf_chdr
*chdr
;
2549 *uncompressed
= NULL
;
2550 *uncompressed_size
= 0;
2552 /* The format starts with an ELF compression header. */
2553 if (compressed_size
< sizeof (b_elf_chdr
))
2556 chdr
= (const b_elf_chdr
*) compressed
;
2558 if (chdr
->ch_type
!= ELFCOMPRESS_ZLIB
)
2560 /* Unsupported compression algorithm. */
2564 if (*uncompressed
!= NULL
&& *uncompressed_size
>= chdr
->ch_size
)
2568 po
= (unsigned char *) backtrace_alloc (state
, chdr
->ch_size
,
2569 error_callback
, data
);
2574 if (!elf_zlib_inflate_and_verify (compressed
+ sizeof (b_elf_chdr
),
2575 compressed_size
- sizeof (b_elf_chdr
),
2576 zdebug_table
, po
, chdr
->ch_size
))
2580 *uncompressed_size
= chdr
->ch_size
;
2585 /* This function is a hook for testing the zlib support. It is only
2589 backtrace_uncompress_zdebug (struct backtrace_state
*state
,
2590 const unsigned char *compressed
,
2591 size_t compressed_size
,
2592 backtrace_error_callback error_callback
,
2593 void *data
, unsigned char **uncompressed
,
2594 size_t *uncompressed_size
)
2596 uint16_t *zdebug_table
;
2599 zdebug_table
= ((uint16_t *) backtrace_alloc (state
, ZDEBUG_TABLE_SIZE
,
2600 error_callback
, data
));
2601 if (zdebug_table
== NULL
)
2603 ret
= elf_uncompress_zdebug (state
, compressed
, compressed_size
,
2604 zdebug_table
, error_callback
, data
,
2605 uncompressed
, uncompressed_size
);
2606 backtrace_free (state
, zdebug_table
, ZDEBUG_TABLE_SIZE
,
2607 error_callback
, data
);
2611 /* Add the backtrace data for one ELF file. Returns 1 on success,
2612 0 on failure (in both cases descriptor is closed) or -1 if exe
2613 is non-zero and the ELF file is ET_DYN, which tells the caller that
2614 elf_add will need to be called on the descriptor again after
2615 base_address is determined. */
2618 elf_add (struct backtrace_state
*state
, const char *filename
, int descriptor
,
2619 uintptr_t base_address
, backtrace_error_callback error_callback
,
2620 void *data
, fileline
*fileline_fn
, int *found_sym
, int *found_dwarf
,
2621 struct dwarf_data
**fileline_entry
, int exe
, int debuginfo
,
2622 const char *with_buildid_data
, uint32_t with_buildid_size
)
2624 struct backtrace_view ehdr_view
;
2628 unsigned int shstrndx
;
2629 struct backtrace_view shdrs_view
;
2630 int shdrs_view_valid
;
2631 const b_elf_shdr
*shdrs
;
2632 const b_elf_shdr
*shstrhdr
;
2635 struct backtrace_view names_view
;
2636 int names_view_valid
;
2638 unsigned int symtab_shndx
;
2639 unsigned int dynsym_shndx
;
2641 struct debug_section_info sections
[DEBUG_MAX
];
2642 struct debug_section_info zsections
[DEBUG_MAX
];
2643 struct backtrace_view symtab_view
;
2644 int symtab_view_valid
;
2645 struct backtrace_view strtab_view
;
2646 int strtab_view_valid
;
2647 struct backtrace_view buildid_view
;
2648 int buildid_view_valid
;
2649 const char *buildid_data
;
2650 uint32_t buildid_size
;
2651 struct backtrace_view debuglink_view
;
2652 int debuglink_view_valid
;
2653 const char *debuglink_name
;
2654 uint32_t debuglink_crc
;
2655 struct backtrace_view debugaltlink_view
;
2656 int debugaltlink_view_valid
;
2657 const char *debugaltlink_name
;
2658 const char *debugaltlink_buildid_data
;
2659 uint32_t debugaltlink_buildid_size
;
2663 struct backtrace_view debug_view
;
2664 int debug_view_valid
;
2665 unsigned int using_debug_view
;
2666 uint16_t *zdebug_table
;
2667 struct backtrace_view split_debug_view
[DEBUG_MAX
];
2668 unsigned char split_debug_view_valid
[DEBUG_MAX
];
2669 struct elf_ppc64_opd_data opd_data
, *opd
;
2670 struct dwarf_sections dwarf_sections
;
2678 shdrs_view_valid
= 0;
2679 names_view_valid
= 0;
2680 symtab_view_valid
= 0;
2681 strtab_view_valid
= 0;
2682 buildid_view_valid
= 0;
2683 buildid_data
= NULL
;
2685 debuglink_view_valid
= 0;
2686 debuglink_name
= NULL
;
2688 debugaltlink_view_valid
= 0;
2689 debugaltlink_name
= NULL
;
2690 debugaltlink_buildid_data
= NULL
;
2691 debugaltlink_buildid_size
= 0;
2692 debug_view_valid
= 0;
2693 memset (&split_debug_view_valid
[0], 0, sizeof split_debug_view_valid
);
2696 if (!backtrace_get_view (state
, descriptor
, 0, sizeof ehdr
, error_callback
,
2700 memcpy (&ehdr
, ehdr_view
.data
, sizeof ehdr
);
2702 backtrace_release_view (state
, &ehdr_view
, error_callback
, data
);
2704 if (ehdr
.e_ident
[EI_MAG0
] != ELFMAG0
2705 || ehdr
.e_ident
[EI_MAG1
] != ELFMAG1
2706 || ehdr
.e_ident
[EI_MAG2
] != ELFMAG2
2707 || ehdr
.e_ident
[EI_MAG3
] != ELFMAG3
)
2709 error_callback (data
, "executable file is not ELF", 0);
2712 if (ehdr
.e_ident
[EI_VERSION
] != EV_CURRENT
)
2714 error_callback (data
, "executable file is unrecognized ELF version", 0);
2718 #if BACKTRACE_ELF_SIZE == 32
2719 #define BACKTRACE_ELFCLASS ELFCLASS32
2721 #define BACKTRACE_ELFCLASS ELFCLASS64
2724 if (ehdr
.e_ident
[EI_CLASS
] != BACKTRACE_ELFCLASS
)
2726 error_callback (data
, "executable file is unexpected ELF class", 0);
2730 if (ehdr
.e_ident
[EI_DATA
] != ELFDATA2LSB
2731 && ehdr
.e_ident
[EI_DATA
] != ELFDATA2MSB
)
2733 error_callback (data
, "executable file has unknown endianness", 0);
2737 /* If the executable is ET_DYN, it is either a PIE, or we are running
2738 directly a shared library with .interp. We need to wait for
2739 dl_iterate_phdr in that case to determine the actual base_address. */
2740 if (exe
&& ehdr
.e_type
== ET_DYN
)
2743 shoff
= ehdr
.e_shoff
;
2744 shnum
= ehdr
.e_shnum
;
2745 shstrndx
= ehdr
.e_shstrndx
;
2747 if ((shnum
== 0 || shstrndx
== SHN_XINDEX
)
2750 struct backtrace_view shdr_view
;
2751 const b_elf_shdr
*shdr
;
2753 if (!backtrace_get_view (state
, descriptor
, shoff
, sizeof shdr
,
2754 error_callback
, data
, &shdr_view
))
2757 shdr
= (const b_elf_shdr
*) shdr_view
.data
;
2760 shnum
= shdr
->sh_size
;
2762 if (shstrndx
== SHN_XINDEX
)
2764 shstrndx
= shdr
->sh_link
;
2766 /* Versions of the GNU binutils between 2.12 and 2.18 did
2767 not handle objects with more than SHN_LORESERVE sections
2768 correctly. All large section indexes were offset by
2769 0x100. There is more information at
2770 http://sourceware.org/bugzilla/show_bug.cgi?id-5900 .
2771 Fortunately these object files are easy to detect, as the
2772 GNU binutils always put the section header string table
2773 near the end of the list of sections. Thus if the
2774 section header string table index is larger than the
2775 number of sections, then we know we have to subtract
2776 0x100 to get the real section index. */
2777 if (shstrndx
>= shnum
&& shstrndx
>= SHN_LORESERVE
+ 0x100)
2781 backtrace_release_view (state
, &shdr_view
, error_callback
, data
);
2784 if (shnum
== 0 || shstrndx
== 0)
2787 /* To translate PC to file/line when using DWARF, we need to find
2788 the .debug_info and .debug_line sections. */
2790 /* Read the section headers, skipping the first one. */
2792 if (!backtrace_get_view (state
, descriptor
, shoff
+ sizeof (b_elf_shdr
),
2793 (shnum
- 1) * sizeof (b_elf_shdr
),
2794 error_callback
, data
, &shdrs_view
))
2796 shdrs_view_valid
= 1;
2797 shdrs
= (const b_elf_shdr
*) shdrs_view
.data
;
2799 /* Read the section names. */
2801 shstrhdr
= &shdrs
[shstrndx
- 1];
2802 shstr_size
= shstrhdr
->sh_size
;
2803 shstr_off
= shstrhdr
->sh_offset
;
2805 if (!backtrace_get_view (state
, descriptor
, shstr_off
, shstrhdr
->sh_size
,
2806 error_callback
, data
, &names_view
))
2808 names_view_valid
= 1;
2809 names
= (const char *) names_view
.data
;
2814 memset (sections
, 0, sizeof sections
);
2815 memset (zsections
, 0, sizeof zsections
);
2817 /* Look for the symbol table. */
2818 for (i
= 1; i
< shnum
; ++i
)
2820 const b_elf_shdr
*shdr
;
2821 unsigned int sh_name
;
2825 shdr
= &shdrs
[i
- 1];
2827 if (shdr
->sh_type
== SHT_SYMTAB
)
2829 else if (shdr
->sh_type
== SHT_DYNSYM
)
2832 sh_name
= shdr
->sh_name
;
2833 if (sh_name
>= shstr_size
)
2835 error_callback (data
, "ELF section name out of range", 0);
2839 name
= names
+ sh_name
;
2841 for (j
= 0; j
< (int) DEBUG_MAX
; ++j
)
2843 if (strcmp (name
, dwarf_section_names
[j
]) == 0)
2845 sections
[j
].offset
= shdr
->sh_offset
;
2846 sections
[j
].size
= shdr
->sh_size
;
2847 sections
[j
].compressed
= (shdr
->sh_flags
& SHF_COMPRESSED
) != 0;
2852 if (name
[0] == '.' && name
[1] == 'z')
2854 for (j
= 0; j
< (int) DEBUG_MAX
; ++j
)
2856 if (strcmp (name
+ 2, dwarf_section_names
[j
] + 1) == 0)
2858 zsections
[j
].offset
= shdr
->sh_offset
;
2859 zsections
[j
].size
= shdr
->sh_size
;
2865 /* Read the build ID if present. This could check for any
2866 SHT_NOTE section with the right note name and type, but gdb
2867 looks for a specific section name. */
2868 if ((!debuginfo
|| with_buildid_data
!= NULL
)
2869 && !buildid_view_valid
2870 && strcmp (name
, ".note.gnu.build-id") == 0)
2872 const b_elf_note
*note
;
2874 if (!backtrace_get_view (state
, descriptor
, shdr
->sh_offset
,
2875 shdr
->sh_size
, error_callback
, data
,
2879 buildid_view_valid
= 1;
2880 note
= (const b_elf_note
*) buildid_view
.data
;
2881 if (note
->type
== NT_GNU_BUILD_ID
2882 && note
->namesz
== 4
2883 && strncmp (note
->name
, "GNU", 4) == 0
2884 && shdr
->sh_size
<= 12 + ((note
->namesz
+ 3) & ~ 3) + note
->descsz
)
2886 buildid_data
= ¬e
->name
[0] + ((note
->namesz
+ 3) & ~ 3);
2887 buildid_size
= note
->descsz
;
2890 if (with_buildid_size
!= 0)
2892 if (buildid_size
!= with_buildid_size
)
2895 if (memcmp (buildid_data
, with_buildid_data
, buildid_size
) != 0)
2900 /* Read the debuglink file if present. */
2902 && !debuglink_view_valid
2903 && strcmp (name
, ".gnu_debuglink") == 0)
2905 const char *debuglink_data
;
2908 if (!backtrace_get_view (state
, descriptor
, shdr
->sh_offset
,
2909 shdr
->sh_size
, error_callback
, data
,
2913 debuglink_view_valid
= 1;
2914 debuglink_data
= (const char *) debuglink_view
.data
;
2915 crc_offset
= strnlen (debuglink_data
, shdr
->sh_size
);
2916 crc_offset
= (crc_offset
+ 3) & ~3;
2917 if (crc_offset
+ 4 <= shdr
->sh_size
)
2919 debuglink_name
= debuglink_data
;
2920 debuglink_crc
= *(const uint32_t*)(debuglink_data
+ crc_offset
);
2924 if (!debugaltlink_view_valid
2925 && strcmp (name
, ".gnu_debugaltlink") == 0)
2927 const char *debugaltlink_data
;
2928 size_t debugaltlink_name_len
;
2930 if (!backtrace_get_view (state
, descriptor
, shdr
->sh_offset
,
2931 shdr
->sh_size
, error_callback
, data
,
2932 &debugaltlink_view
))
2935 debugaltlink_view_valid
= 1;
2936 debugaltlink_data
= (const char *) debugaltlink_view
.data
;
2937 debugaltlink_name
= debugaltlink_data
;
2938 debugaltlink_name_len
= strnlen (debugaltlink_data
, shdr
->sh_size
);
2939 if (debugaltlink_name_len
< shdr
->sh_size
)
2941 /* Include terminating zero. */
2942 debugaltlink_name_len
+= 1;
2944 debugaltlink_buildid_data
2945 = debugaltlink_data
+ debugaltlink_name_len
;
2946 debugaltlink_buildid_size
= shdr
->sh_size
- debugaltlink_name_len
;
2950 /* Read the .opd section on PowerPC64 ELFv1. */
2951 if (ehdr
.e_machine
== EM_PPC64
2952 && (ehdr
.e_flags
& EF_PPC64_ABI
) < 2
2953 && shdr
->sh_type
== SHT_PROGBITS
2954 && strcmp (name
, ".opd") == 0)
2956 if (!backtrace_get_view (state
, descriptor
, shdr
->sh_offset
,
2957 shdr
->sh_size
, error_callback
, data
,
2962 opd
->addr
= shdr
->sh_addr
;
2963 opd
->data
= (const char *) opd_data
.view
.data
;
2964 opd
->size
= shdr
->sh_size
;
2968 if (symtab_shndx
== 0)
2969 symtab_shndx
= dynsym_shndx
;
2970 if (symtab_shndx
!= 0 && !debuginfo
)
2972 const b_elf_shdr
*symtab_shdr
;
2973 unsigned int strtab_shndx
;
2974 const b_elf_shdr
*strtab_shdr
;
2975 struct elf_syminfo_data
*sdata
;
2977 symtab_shdr
= &shdrs
[symtab_shndx
- 1];
2978 strtab_shndx
= symtab_shdr
->sh_link
;
2979 if (strtab_shndx
>= shnum
)
2981 error_callback (data
,
2982 "ELF symbol table strtab link out of range", 0);
2985 strtab_shdr
= &shdrs
[strtab_shndx
- 1];
2987 if (!backtrace_get_view (state
, descriptor
, symtab_shdr
->sh_offset
,
2988 symtab_shdr
->sh_size
, error_callback
, data
,
2991 symtab_view_valid
= 1;
2993 if (!backtrace_get_view (state
, descriptor
, strtab_shdr
->sh_offset
,
2994 strtab_shdr
->sh_size
, error_callback
, data
,
2997 strtab_view_valid
= 1;
2999 sdata
= ((struct elf_syminfo_data
*)
3000 backtrace_alloc (state
, sizeof *sdata
, error_callback
, data
));
3004 if (!elf_initialize_syminfo (state
, base_address
,
3005 symtab_view
.data
, symtab_shdr
->sh_size
,
3006 strtab_view
.data
, strtab_shdr
->sh_size
,
3007 error_callback
, data
, sdata
, opd
))
3009 backtrace_free (state
, sdata
, sizeof *sdata
, error_callback
, data
);
3013 /* We no longer need the symbol table, but we hold on to the
3014 string table permanently. */
3015 backtrace_release_view (state
, &symtab_view
, error_callback
, data
);
3016 symtab_view_valid
= 0;
3017 strtab_view_valid
= 0;
3021 elf_add_syminfo_data (state
, sdata
);
3024 backtrace_release_view (state
, &shdrs_view
, error_callback
, data
);
3025 shdrs_view_valid
= 0;
3026 backtrace_release_view (state
, &names_view
, error_callback
, data
);
3027 names_view_valid
= 0;
3029 /* If the debug info is in a separate file, read that one instead. */
3031 if (buildid_data
!= NULL
)
3035 d
= elf_open_debugfile_by_buildid (state
, buildid_data
, buildid_size
,
3036 error_callback
, data
);
3041 backtrace_release_view (state
, &buildid_view
, error_callback
, data
);
3042 if (debuglink_view_valid
)
3043 backtrace_release_view (state
, &debuglink_view
, error_callback
,
3045 if (debugaltlink_view_valid
)
3046 backtrace_release_view (state
, &debugaltlink_view
, error_callback
,
3048 ret
= elf_add (state
, "", d
, base_address
, error_callback
, data
,
3049 fileline_fn
, found_sym
, found_dwarf
, NULL
, 0, 1, NULL
,
3052 backtrace_close (d
, error_callback
, data
);
3054 backtrace_close (descriptor
, error_callback
, data
);
3059 if (buildid_view_valid
)
3061 backtrace_release_view (state
, &buildid_view
, error_callback
, data
);
3062 buildid_view_valid
= 0;
3067 backtrace_release_view (state
, &opd
->view
, error_callback
, data
);
3071 if (debuglink_name
!= NULL
)
3075 d
= elf_open_debugfile_by_debuglink (state
, filename
, debuglink_name
,
3076 debuglink_crc
, error_callback
,
3082 backtrace_release_view (state
, &debuglink_view
, error_callback
,
3084 if (debugaltlink_view_valid
)
3085 backtrace_release_view (state
, &debugaltlink_view
, error_callback
,
3087 ret
= elf_add (state
, "", d
, base_address
, error_callback
, data
,
3088 fileline_fn
, found_sym
, found_dwarf
, NULL
, 0, 1, NULL
,
3091 backtrace_close (d
, error_callback
, data
);
3093 backtrace_close(descriptor
, error_callback
, data
);
3098 if (debuglink_view_valid
)
3100 backtrace_release_view (state
, &debuglink_view
, error_callback
, data
);
3101 debuglink_view_valid
= 0;
3104 struct dwarf_data
*fileline_altlink
= NULL
;
3105 if (debugaltlink_name
!= NULL
)
3109 d
= elf_open_debugfile_by_debuglink (state
, filename
, debugaltlink_name
,
3110 0, error_callback
, data
);
3115 ret
= elf_add (state
, filename
, d
, base_address
, error_callback
, data
,
3116 fileline_fn
, found_sym
, found_dwarf
, &fileline_altlink
,
3117 0, 1, debugaltlink_buildid_data
,
3118 debugaltlink_buildid_size
);
3119 backtrace_release_view (state
, &debugaltlink_view
, error_callback
,
3121 debugaltlink_view_valid
= 0;
3124 backtrace_close (d
, error_callback
, data
);
3130 if (debugaltlink_view_valid
)
3132 backtrace_release_view (state
, &debugaltlink_view
, error_callback
, data
);
3133 debugaltlink_view_valid
= 0;
3136 /* Read all the debug sections in a single view, since they are
3137 probably adjacent in the file. If any of sections are
3138 uncompressed, we never release this view. */
3143 for (i
= 0; i
< (int) DEBUG_MAX
; ++i
)
3147 if (sections
[i
].size
!= 0)
3149 if (min_offset
== 0 || sections
[i
].offset
< min_offset
)
3150 min_offset
= sections
[i
].offset
;
3151 end
= sections
[i
].offset
+ sections
[i
].size
;
3152 if (end
> max_offset
)
3154 debug_size
+= sections
[i
].size
;
3156 if (zsections
[i
].size
!= 0)
3158 if (min_offset
== 0 || zsections
[i
].offset
< min_offset
)
3159 min_offset
= zsections
[i
].offset
;
3160 end
= zsections
[i
].offset
+ zsections
[i
].size
;
3161 if (end
> max_offset
)
3163 debug_size
+= zsections
[i
].size
;
3166 if (min_offset
== 0 || max_offset
== 0)
3168 if (!backtrace_close (descriptor
, error_callback
, data
))
3173 /* If the total debug section size is large, assume that there are
3174 gaps between the sections, and read them individually. */
3176 if (max_offset
- min_offset
< 0x20000000
3177 || max_offset
- min_offset
< debug_size
+ 0x10000)
3179 if (!backtrace_get_view (state
, descriptor
, min_offset
,
3180 max_offset
- min_offset
,
3181 error_callback
, data
, &debug_view
))
3183 debug_view_valid
= 1;
3187 memset (&split_debug_view
[0], 0, sizeof split_debug_view
);
3188 for (i
= 0; i
< (int) DEBUG_MAX
; ++i
)
3190 struct debug_section_info
*dsec
;
3192 if (sections
[i
].size
!= 0)
3193 dsec
= §ions
[i
];
3194 else if (zsections
[i
].size
!= 0)
3195 dsec
= &zsections
[i
];
3199 if (!backtrace_get_view (state
, descriptor
, dsec
->offset
, dsec
->size
,
3200 error_callback
, data
, &split_debug_view
[i
]))
3202 split_debug_view_valid
[i
] = 1;
3204 if (sections
[i
].size
!= 0)
3205 sections
[i
].data
= ((const unsigned char *)
3206 split_debug_view
[i
].data
);
3208 zsections
[i
].data
= ((const unsigned char *)
3209 split_debug_view
[i
].data
);
3213 /* We've read all we need from the executable. */
3214 if (!backtrace_close (descriptor
, error_callback
, data
))
3218 using_debug_view
= 0;
3219 if (debug_view_valid
)
3221 for (i
= 0; i
< (int) DEBUG_MAX
; ++i
)
3223 if (sections
[i
].size
== 0)
3224 sections
[i
].data
= NULL
;
3227 sections
[i
].data
= ((const unsigned char *) debug_view
.data
3228 + (sections
[i
].offset
- min_offset
));
3232 if (zsections
[i
].size
== 0)
3233 zsections
[i
].data
= NULL
;
3235 zsections
[i
].data
= ((const unsigned char *) debug_view
.data
3236 + (zsections
[i
].offset
- min_offset
));
3240 /* Uncompress the old format (--compress-debug-sections=zlib-gnu). */
3242 zdebug_table
= NULL
;
3243 for (i
= 0; i
< (int) DEBUG_MAX
; ++i
)
3245 if (sections
[i
].size
== 0 && zsections
[i
].size
> 0)
3247 unsigned char *uncompressed_data
;
3248 size_t uncompressed_size
;
3250 if (zdebug_table
== NULL
)
3252 zdebug_table
= ((uint16_t *)
3253 backtrace_alloc (state
, ZDEBUG_TABLE_SIZE
,
3254 error_callback
, data
));
3255 if (zdebug_table
== NULL
)
3259 uncompressed_data
= NULL
;
3260 uncompressed_size
= 0;
3261 if (!elf_uncompress_zdebug (state
, zsections
[i
].data
,
3262 zsections
[i
].size
, zdebug_table
,
3263 error_callback
, data
,
3264 &uncompressed_data
, &uncompressed_size
))
3266 sections
[i
].data
= uncompressed_data
;
3267 sections
[i
].size
= uncompressed_size
;
3268 sections
[i
].compressed
= 0;
3270 if (split_debug_view_valid
[i
])
3272 backtrace_release_view (state
, &split_debug_view
[i
],
3273 error_callback
, data
);
3274 split_debug_view_valid
[i
] = 0;
3279 /* Uncompress the official ELF format
3280 (--compress-debug-sections=zlib-gabi). */
3281 for (i
= 0; i
< (int) DEBUG_MAX
; ++i
)
3283 unsigned char *uncompressed_data
;
3284 size_t uncompressed_size
;
3286 if (sections
[i
].size
== 0 || !sections
[i
].compressed
)
3289 if (zdebug_table
== NULL
)
3291 zdebug_table
= ((uint16_t *)
3292 backtrace_alloc (state
, ZDEBUG_TABLE_SIZE
,
3293 error_callback
, data
));
3294 if (zdebug_table
== NULL
)
3298 uncompressed_data
= NULL
;
3299 uncompressed_size
= 0;
3300 if (!elf_uncompress_chdr (state
, sections
[i
].data
, sections
[i
].size
,
3301 zdebug_table
, error_callback
, data
,
3302 &uncompressed_data
, &uncompressed_size
))
3304 sections
[i
].data
= uncompressed_data
;
3305 sections
[i
].size
= uncompressed_size
;
3306 sections
[i
].compressed
= 0;
3308 if (debug_view_valid
)
3310 else if (split_debug_view_valid
[i
])
3312 backtrace_release_view (state
, &split_debug_view
[i
],
3313 error_callback
, data
);
3314 split_debug_view_valid
[i
] = 0;
3318 if (zdebug_table
!= NULL
)
3319 backtrace_free (state
, zdebug_table
, ZDEBUG_TABLE_SIZE
,
3320 error_callback
, data
);
3322 if (debug_view_valid
&& using_debug_view
== 0)
3324 backtrace_release_view (state
, &debug_view
, error_callback
, data
);
3325 debug_view_valid
= 0;
3328 for (i
= 0; i
< (int) DEBUG_MAX
; ++i
)
3330 dwarf_sections
.data
[i
] = sections
[i
].data
;
3331 dwarf_sections
.size
[i
] = sections
[i
].size
;
3334 if (!backtrace_dwarf_add (state
, base_address
, &dwarf_sections
,
3335 ehdr
.e_ident
[EI_DATA
] == ELFDATA2MSB
,
3337 error_callback
, data
, fileline_fn
,
3346 if (shdrs_view_valid
)
3347 backtrace_release_view (state
, &shdrs_view
, error_callback
, data
);
3348 if (names_view_valid
)
3349 backtrace_release_view (state
, &names_view
, error_callback
, data
);
3350 if (symtab_view_valid
)
3351 backtrace_release_view (state
, &symtab_view
, error_callback
, data
);
3352 if (strtab_view_valid
)
3353 backtrace_release_view (state
, &strtab_view
, error_callback
, data
);
3354 if (debuglink_view_valid
)
3355 backtrace_release_view (state
, &debuglink_view
, error_callback
, data
);
3356 if (debugaltlink_view_valid
)
3357 backtrace_release_view (state
, &debugaltlink_view
, error_callback
, data
);
3358 if (buildid_view_valid
)
3359 backtrace_release_view (state
, &buildid_view
, error_callback
, data
);
3360 if (debug_view_valid
)
3361 backtrace_release_view (state
, &debug_view
, error_callback
, data
);
3362 for (i
= 0; i
< (int) DEBUG_MAX
; ++i
)
3364 if (split_debug_view_valid
[i
])
3365 backtrace_release_view (state
, &split_debug_view
[i
],
3366 error_callback
, data
);
3369 backtrace_release_view (state
, &opd
->view
, error_callback
, data
);
3370 if (descriptor
!= -1)
3371 backtrace_close (descriptor
, error_callback
, data
);
3375 /* Data passed to phdr_callback. */
3379 struct backtrace_state
*state
;
3380 backtrace_error_callback error_callback
;
3382 fileline
*fileline_fn
;
3385 const char *exe_filename
;
3389 /* Callback passed to dl_iterate_phdr. Load debug info from shared
3394 __attribute__ ((__force_align_arg_pointer__
))
3396 phdr_callback (struct dl_phdr_info
*info
, size_t size ATTRIBUTE_UNUSED
,
3399 struct phdr_data
*pd
= (struct phdr_data
*) pdata
;
3400 const char *filename
;
3403 fileline elf_fileline_fn
;
3406 /* There is not much we can do if we don't have the module name,
3407 unless executable is ET_DYN, where we expect the very first
3408 phdr_callback to be for the PIE. */
3409 if (info
->dlpi_name
== NULL
|| info
->dlpi_name
[0] == '\0')
3411 if (pd
->exe_descriptor
== -1)
3413 filename
= pd
->exe_filename
;
3414 descriptor
= pd
->exe_descriptor
;
3415 pd
->exe_descriptor
= -1;
3419 if (pd
->exe_descriptor
!= -1)
3421 backtrace_close (pd
->exe_descriptor
, pd
->error_callback
, pd
->data
);
3422 pd
->exe_descriptor
= -1;
3425 filename
= info
->dlpi_name
;
3426 descriptor
= backtrace_open (info
->dlpi_name
, pd
->error_callback
,
3427 pd
->data
, &does_not_exist
);
3432 if (elf_add (pd
->state
, filename
, descriptor
, info
->dlpi_addr
,
3433 pd
->error_callback
, pd
->data
, &elf_fileline_fn
, pd
->found_sym
,
3434 &found_dwarf
, NULL
, 0, 0, NULL
, 0))
3438 *pd
->found_dwarf
= 1;
3439 *pd
->fileline_fn
= elf_fileline_fn
;
3446 /* Initialize the backtrace data we need from an ELF executable. At
3447 the ELF level, all we need to do is find the debug info
3451 backtrace_initialize (struct backtrace_state
*state
, const char *filename
,
3452 int descriptor
, backtrace_error_callback error_callback
,
3453 void *data
, fileline
*fileline_fn
)
3458 fileline elf_fileline_fn
= elf_nodebug
;
3459 struct phdr_data pd
;
3461 ret
= elf_add (state
, filename
, descriptor
, 0, error_callback
, data
,
3462 &elf_fileline_fn
, &found_sym
, &found_dwarf
, NULL
, 1, 0, NULL
,
3468 pd
.error_callback
= error_callback
;
3470 pd
.fileline_fn
= &elf_fileline_fn
;
3471 pd
.found_sym
= &found_sym
;
3472 pd
.found_dwarf
= &found_dwarf
;
3473 pd
.exe_filename
= filename
;
3474 pd
.exe_descriptor
= ret
< 0 ? descriptor
: -1;
3476 dl_iterate_phdr (phdr_callback
, (void *) &pd
);
3478 if (!state
->threaded
)
3481 state
->syminfo_fn
= elf_syminfo
;
3482 else if (state
->syminfo_fn
== NULL
)
3483 state
->syminfo_fn
= elf_nosyms
;
3488 backtrace_atomic_store_pointer (&state
->syminfo_fn
, elf_syminfo
);
3490 (void) __sync_bool_compare_and_swap (&state
->syminfo_fn
, NULL
,
3494 if (!state
->threaded
)
3495 *fileline_fn
= state
->fileline_fn
;
3497 *fileline_fn
= backtrace_atomic_load_pointer (&state
->fileline_fn
);
3499 if (*fileline_fn
== NULL
|| *fileline_fn
== elf_nodebug
)
3500 *fileline_fn
= elf_fileline_fn
;