1 /* elf.c -- Get debug data from an ELF file for backtraces.
2 Copyright (C) 2012-2017 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)
59 #if !defined(HAVE_DECL_STRNLEN) || !HAVE_DECL_STRNLEN
61 /* If strnlen is not declared, provide our own version. */
64 xstrnlen (const char *s
, size_t maxlen
)
68 for (i
= 0; i
< maxlen
; ++i
)
74 #define strnlen xstrnlen
80 /* Dummy version of lstat for systems that don't have it. */
83 xlstat (const char *path ATTRIBUTE_UNUSED
, struct stat
*st ATTRIBUTE_UNUSED
)
94 /* Dummy version of readlink for systems that don't have it. */
97 xreadlink (const char *path ATTRIBUTE_UNUSED
, char *buf ATTRIBUTE_UNUSED
,
98 size_t bufsz ATTRIBUTE_UNUSED
)
103 #define readlink xreadlink
107 #ifndef HAVE_DL_ITERATE_PHDR
109 /* Dummy version of dl_iterate_phdr for systems that don't have it. */
111 #define dl_phdr_info x_dl_phdr_info
112 #define dl_iterate_phdr x_dl_iterate_phdr
117 const char *dlpi_name
;
121 dl_iterate_phdr (int (*callback
) (struct dl_phdr_info
*,
122 size_t, void *) ATTRIBUTE_UNUSED
,
123 void *data ATTRIBUTE_UNUSED
)
128 #endif /* ! defined (HAVE_DL_ITERATE_PHDR) */
130 /* The configure script must tell us whether we are 32-bit or 64-bit
131 ELF. We could make this code test and support either possibility,
132 but there is no point. This code only works for the currently
133 running executable, which means that we know the ELF mode at
136 #if BACKTRACE_ELF_SIZE != 32 && BACKTRACE_ELF_SIZE != 64
137 #error "Unknown BACKTRACE_ELF_SIZE"
140 /* <link.h> might #include <elf.h> which might define our constants
141 with slightly different values. Undefine them to be safe. */
169 #undef NT_GNU_BUILD_ID
173 typedef uint16_t b_elf_half
; /* Elf_Half. */
174 typedef uint32_t b_elf_word
; /* Elf_Word. */
175 typedef int32_t b_elf_sword
; /* Elf_Sword. */
177 #if BACKTRACE_ELF_SIZE == 32
179 typedef uint32_t b_elf_addr
; /* Elf_Addr. */
180 typedef uint32_t b_elf_off
; /* Elf_Off. */
182 typedef uint32_t b_elf_wxword
; /* 32-bit Elf_Word, 64-bit ELF_Xword. */
186 typedef uint64_t b_elf_addr
; /* Elf_Addr. */
187 typedef uint64_t b_elf_off
; /* Elf_Off. */
188 typedef uint64_t b_elf_xword
; /* Elf_Xword. */
189 typedef int64_t b_elf_sxword
; /* Elf_Sxword. */
191 typedef uint64_t b_elf_wxword
; /* 32-bit Elf_Word, 64-bit ELF_Xword. */
195 /* Data structures and associated constants. */
200 unsigned char e_ident
[EI_NIDENT
]; /* ELF "magic number" */
201 b_elf_half e_type
; /* Identifies object file type */
202 b_elf_half e_machine
; /* Specifies required architecture */
203 b_elf_word e_version
; /* Identifies object file version */
204 b_elf_addr e_entry
; /* Entry point virtual address */
205 b_elf_off e_phoff
; /* Program header table file offset */
206 b_elf_off e_shoff
; /* Section header table file offset */
207 b_elf_word e_flags
; /* Processor-specific flags */
208 b_elf_half e_ehsize
; /* ELF header size in bytes */
209 b_elf_half e_phentsize
; /* Program header table entry size */
210 b_elf_half e_phnum
; /* Program header table entry count */
211 b_elf_half e_shentsize
; /* Section header table entry size */
212 b_elf_half e_shnum
; /* Section header table entry count */
213 b_elf_half e_shstrndx
; /* Section header string table index */
214 } b_elf_ehdr
; /* Elf_Ehdr. */
232 #define ELFDATA2LSB 1
233 #define ELFDATA2MSB 2
240 b_elf_word sh_name
; /* Section name, index in string tbl */
241 b_elf_word sh_type
; /* Type of section */
242 b_elf_wxword sh_flags
; /* Miscellaneous section attributes */
243 b_elf_addr sh_addr
; /* Section virtual addr at execution */
244 b_elf_off sh_offset
; /* Section file offset */
245 b_elf_wxword sh_size
; /* Size of section in bytes */
246 b_elf_word sh_link
; /* Index of another section */
247 b_elf_word sh_info
; /* Additional section information */
248 b_elf_wxword sh_addralign
; /* Section alignment */
249 b_elf_wxword sh_entsize
; /* Entry size if section holds table */
250 } b_elf_shdr
; /* Elf_Shdr. */
252 #define SHN_UNDEF 0x0000 /* Undefined section */
253 #define SHN_LORESERVE 0xFF00 /* Begin range of reserved indices */
254 #define SHN_XINDEX 0xFFFF /* Section index is held elsewhere */
258 #define SHT_DYNSYM 11
260 #if BACKTRACE_ELF_SIZE == 32
264 b_elf_word st_name
; /* Symbol name, index in string tbl */
265 b_elf_addr st_value
; /* Symbol value */
266 b_elf_word st_size
; /* Symbol size */
267 unsigned char st_info
; /* Symbol binding and type */
268 unsigned char st_other
; /* Visibility and other data */
269 b_elf_half st_shndx
; /* Symbol section index */
270 } b_elf_sym
; /* Elf_Sym. */
272 #else /* BACKTRACE_ELF_SIZE != 32 */
276 b_elf_word st_name
; /* Symbol name, index in string tbl */
277 unsigned char st_info
; /* Symbol binding and type */
278 unsigned char st_other
; /* Visibility and other data */
279 b_elf_half st_shndx
; /* Symbol section index */
280 b_elf_addr st_value
; /* Symbol value */
281 b_elf_xword st_size
; /* Symbol size */
282 } b_elf_sym
; /* Elf_Sym. */
284 #endif /* BACKTRACE_ELF_SIZE != 32 */
297 #define NT_GNU_BUILD_ID 3
299 /* An index of ELF sections we care about. */
311 /* Names of sections, indexed by enum elf_section. */
313 static const char * const debug_section_names
[DEBUG_MAX
] =
322 /* Information we gather for the sections we care about. */
324 struct debug_section_info
326 /* Section file offset. */
330 /* Section contents, after read from file. */
331 const unsigned char *data
;
334 /* Information we keep for an ELF symbol. */
338 /* The name of the symbol. */
340 /* The address of the symbol. */
342 /* The size of the symbol. */
346 /* Information to pass to elf_syminfo. */
348 struct elf_syminfo_data
350 /* Symbols for the next module. */
351 struct elf_syminfo_data
*next
;
352 /* The ELF symbols, sorted by address. */
353 struct elf_symbol
*symbols
;
354 /* The number of symbols. */
358 /* Compute the CRC-32 of BUF/LEN. This uses the CRC used for
359 .gnu_debuglink files. */
362 elf_crc32 (uint32_t crc
, const unsigned char *buf
, size_t len
)
364 static const uint32_t crc32_table
[256] =
366 0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419,
367 0x706af48f, 0xe963a535, 0x9e6495a3, 0x0edb8832, 0x79dcb8a4,
368 0xe0d5e91e, 0x97d2d988, 0x09b64c2b, 0x7eb17cbd, 0xe7b82d07,
369 0x90bf1d91, 0x1db71064, 0x6ab020f2, 0xf3b97148, 0x84be41de,
370 0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7, 0x136c9856,
371 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec, 0x14015c4f, 0x63066cd9,
372 0xfa0f3d63, 0x8d080df5, 0x3b6e20c8, 0x4c69105e, 0xd56041e4,
373 0xa2677172, 0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b,
374 0x35b5a8fa, 0x42b2986c, 0xdbbbc9d6, 0xacbcf940, 0x32d86ce3,
375 0x45df5c75, 0xdcd60dcf, 0xabd13d59, 0x26d930ac, 0x51de003a,
376 0xc8d75180, 0xbfd06116, 0x21b4f4b5, 0x56b3c423, 0xcfba9599,
377 0xb8bda50f, 0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924,
378 0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d, 0x76dc4190,
379 0x01db7106, 0x98d220bc, 0xefd5102a, 0x71b18589, 0x06b6b51f,
380 0x9fbfe4a5, 0xe8b8d433, 0x7807c9a2, 0x0f00f934, 0x9609a88e,
381 0xe10e9818, 0x7f6a0dbb, 0x086d3d2d, 0x91646c97, 0xe6635c01,
382 0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e, 0x6c0695ed,
383 0x1b01a57b, 0x8208f4c1, 0xf50fc457, 0x65b0d9c6, 0x12b7e950,
384 0x8bbeb8ea, 0xfcb9887c, 0x62dd1ddf, 0x15da2d49, 0x8cd37cf3,
385 0xfbd44c65, 0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2,
386 0x4adfa541, 0x3dd895d7, 0xa4d1c46d, 0xd3d6f4fb, 0x4369e96a,
387 0x346ed9fc, 0xad678846, 0xda60b8d0, 0x44042d73, 0x33031de5,
388 0xaa0a4c5f, 0xdd0d7cc9, 0x5005713c, 0x270241aa, 0xbe0b1010,
389 0xc90c2086, 0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f,
390 0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4, 0x59b33d17,
391 0x2eb40d81, 0xb7bd5c3b, 0xc0ba6cad, 0xedb88320, 0x9abfb3b6,
392 0x03b6e20c, 0x74b1d29a, 0xead54739, 0x9dd277af, 0x04db2615,
393 0x73dc1683, 0xe3630b12, 0x94643b84, 0x0d6d6a3e, 0x7a6a5aa8,
394 0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1, 0xf00f9344,
395 0x8708a3d2, 0x1e01f268, 0x6906c2fe, 0xf762575d, 0x806567cb,
396 0x196c3671, 0x6e6b06e7, 0xfed41b76, 0x89d32be0, 0x10da7a5a,
397 0x67dd4acc, 0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5,
398 0xd6d6a3e8, 0xa1d1937e, 0x38d8c2c4, 0x4fdff252, 0xd1bb67f1,
399 0xa6bc5767, 0x3fb506dd, 0x48b2364b, 0xd80d2bda, 0xaf0a1b4c,
400 0x36034af6, 0x41047a60, 0xdf60efc3, 0xa867df55, 0x316e8eef,
401 0x4669be79, 0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236,
402 0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f, 0xc5ba3bbe,
403 0xb2bd0b28, 0x2bb45a92, 0x5cb36a04, 0xc2d7ffa7, 0xb5d0cf31,
404 0x2cd99e8b, 0x5bdeae1d, 0x9b64c2b0, 0xec63f226, 0x756aa39c,
405 0x026d930a, 0x9c0906a9, 0xeb0e363f, 0x72076785, 0x05005713,
406 0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38, 0x92d28e9b,
407 0xe5d5be0d, 0x7cdcefb7, 0x0bdbdf21, 0x86d3d2d4, 0xf1d4e242,
408 0x68ddb3f8, 0x1fda836e, 0x81be16cd, 0xf6b9265b, 0x6fb077e1,
409 0x18b74777, 0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c,
410 0x8f659eff, 0xf862ae69, 0x616bffd3, 0x166ccf45, 0xa00ae278,
411 0xd70dd2ee, 0x4e048354, 0x3903b3c2, 0xa7672661, 0xd06016f7,
412 0x4969474d, 0x3e6e77db, 0xaed16a4a, 0xd9d65adc, 0x40df0b66,
413 0x37d83bf0, 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9,
414 0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6, 0xbad03605,
415 0xcdd70693, 0x54de5729, 0x23d967bf, 0xb3667a2e, 0xc4614ab8,
416 0x5d681b02, 0x2a6f2b94, 0xb40bbe37, 0xc30c8ea1, 0x5a05df1b,
419 const unsigned char *end
;
422 for (end
= buf
+ len
; buf
< end
; ++ buf
)
423 crc
= crc32_table
[(crc
^ *buf
) & 0xff] ^ (crc
>> 8);
427 /* Return the CRC-32 of the entire file open at DESCRIPTOR. */
430 elf_crc32_file (struct backtrace_state
*state
, int descriptor
,
431 backtrace_error_callback error_callback
, void *data
)
434 struct backtrace_view file_view
;
437 if (fstat (descriptor
, &st
) < 0)
439 error_callback (data
, "fstat", errno
);
443 if (!backtrace_get_view (state
, descriptor
, 0, st
.st_size
, error_callback
,
447 ret
= elf_crc32 (0, (const unsigned char *) file_view
.data
, st
.st_size
);
449 backtrace_release_view (state
, &file_view
, error_callback
, data
);
454 /* A dummy callback function used when we can't find any debug info. */
457 elf_nodebug (struct backtrace_state
*state ATTRIBUTE_UNUSED
,
458 uintptr_t pc ATTRIBUTE_UNUSED
,
459 backtrace_full_callback callback ATTRIBUTE_UNUSED
,
460 backtrace_error_callback error_callback
, void *data
)
462 error_callback (data
, "no debug info in ELF executable", -1);
466 /* A dummy callback function used when we can't find a symbol
470 elf_nosyms (struct backtrace_state
*state ATTRIBUTE_UNUSED
,
471 uintptr_t addr ATTRIBUTE_UNUSED
,
472 backtrace_syminfo_callback callback ATTRIBUTE_UNUSED
,
473 backtrace_error_callback error_callback
, void *data
)
475 error_callback (data
, "no symbol table in ELF executable", -1);
478 /* Compare struct elf_symbol for qsort. */
481 elf_symbol_compare (const void *v1
, const void *v2
)
483 const struct elf_symbol
*e1
= (const struct elf_symbol
*) v1
;
484 const struct elf_symbol
*e2
= (const struct elf_symbol
*) v2
;
486 if (e1
->address
< e2
->address
)
488 else if (e1
->address
> e2
->address
)
494 /* Compare an ADDR against an elf_symbol for bsearch. We allocate one
495 extra entry in the array so that this can look safely at the next
499 elf_symbol_search (const void *vkey
, const void *ventry
)
501 const uintptr_t *key
= (const uintptr_t *) vkey
;
502 const struct elf_symbol
*entry
= (const struct elf_symbol
*) ventry
;
506 if (addr
< entry
->address
)
508 else if (addr
>= entry
->address
+ entry
->size
)
514 /* Initialize the symbol table info for elf_syminfo. */
517 elf_initialize_syminfo (struct backtrace_state
*state
,
518 uintptr_t base_address
,
519 const unsigned char *symtab_data
, size_t symtab_size
,
520 const unsigned char *strtab
, size_t strtab_size
,
521 backtrace_error_callback error_callback
,
522 void *data
, struct elf_syminfo_data
*sdata
)
525 const b_elf_sym
*sym
;
526 size_t elf_symbol_count
;
527 size_t elf_symbol_size
;
528 struct elf_symbol
*elf_symbols
;
532 sym_count
= symtab_size
/ sizeof (b_elf_sym
);
534 /* We only care about function symbols. Count them. */
535 sym
= (const b_elf_sym
*) symtab_data
;
536 elf_symbol_count
= 0;
537 for (i
= 0; i
< sym_count
; ++i
, ++sym
)
541 info
= sym
->st_info
& 0xf;
542 if ((info
== STT_FUNC
|| info
== STT_OBJECT
)
543 && sym
->st_shndx
!= SHN_UNDEF
)
547 elf_symbol_size
= elf_symbol_count
* sizeof (struct elf_symbol
);
548 elf_symbols
= ((struct elf_symbol
*)
549 backtrace_alloc (state
, elf_symbol_size
, error_callback
,
551 if (elf_symbols
== NULL
)
554 sym
= (const b_elf_sym
*) symtab_data
;
556 for (i
= 0; i
< sym_count
; ++i
, ++sym
)
560 info
= sym
->st_info
& 0xf;
561 if (info
!= STT_FUNC
&& info
!= STT_OBJECT
)
563 if (sym
->st_shndx
== SHN_UNDEF
)
565 if (sym
->st_name
>= strtab_size
)
567 error_callback (data
, "symbol string index out of range", 0);
568 backtrace_free (state
, elf_symbols
, elf_symbol_size
, error_callback
,
572 elf_symbols
[j
].name
= (const char *) strtab
+ sym
->st_name
;
573 elf_symbols
[j
].address
= sym
->st_value
+ base_address
;
574 elf_symbols
[j
].size
= sym
->st_size
;
578 backtrace_qsort (elf_symbols
, elf_symbol_count
, sizeof (struct elf_symbol
),
582 sdata
->symbols
= elf_symbols
;
583 sdata
->count
= elf_symbol_count
;
588 /* Add EDATA to the list in STATE. */
591 elf_add_syminfo_data (struct backtrace_state
*state
,
592 struct elf_syminfo_data
*edata
)
594 if (!state
->threaded
)
596 struct elf_syminfo_data
**pp
;
598 for (pp
= (struct elf_syminfo_data
**) (void *) &state
->syminfo_data
;
608 struct elf_syminfo_data
**pp
;
610 pp
= (struct elf_syminfo_data
**) (void *) &state
->syminfo_data
;
614 struct elf_syminfo_data
*p
;
616 p
= backtrace_atomic_load_pointer (pp
);
624 if (__sync_bool_compare_and_swap (pp
, NULL
, edata
))
630 /* Return the symbol name and value for an ADDR. */
633 elf_syminfo (struct backtrace_state
*state
, uintptr_t addr
,
634 backtrace_syminfo_callback callback
,
635 backtrace_error_callback error_callback ATTRIBUTE_UNUSED
,
638 struct elf_syminfo_data
*edata
;
639 struct elf_symbol
*sym
= NULL
;
641 if (!state
->threaded
)
643 for (edata
= (struct elf_syminfo_data
*) state
->syminfo_data
;
647 sym
= ((struct elf_symbol
*)
648 bsearch (&addr
, edata
->symbols
, edata
->count
,
649 sizeof (struct elf_symbol
), elf_symbol_search
));
656 struct elf_syminfo_data
**pp
;
658 pp
= (struct elf_syminfo_data
**) (void *) &state
->syminfo_data
;
661 edata
= backtrace_atomic_load_pointer (pp
);
665 sym
= ((struct elf_symbol
*)
666 bsearch (&addr
, edata
->symbols
, edata
->count
,
667 sizeof (struct elf_symbol
), elf_symbol_search
));
676 callback (data
, addr
, NULL
, 0, 0);
678 callback (data
, addr
, sym
->name
, sym
->address
, sym
->size
);
681 /* Return whether FILENAME is a symlink. */
684 elf_is_symlink (const char *filename
)
688 if (lstat (filename
, &st
) < 0)
690 return S_ISLNK (st
.st_mode
);
693 /* Return the results of reading the symlink FILENAME in a buffer
694 allocated by backtrace_alloc. Return the length of the buffer in
698 elf_readlink (struct backtrace_state
*state
, const char *filename
,
699 backtrace_error_callback error_callback
, void *data
,
710 buf
= backtrace_alloc (state
, len
, error_callback
, data
);
713 rl
= readlink (filename
, buf
, len
);
716 backtrace_free (state
, buf
, len
, error_callback
, data
);
719 if ((size_t) rl
< len
- 1)
725 backtrace_free (state
, buf
, len
, error_callback
, data
);
730 /* Open a separate debug info file, using the build ID to find it.
731 Returns an open file descriptor, or -1.
733 The GDB manual says that the only place gdb looks for a debug file
734 when the build ID is known is in /usr/lib/debug/.build-id. */
737 elf_open_debugfile_by_buildid (struct backtrace_state
*state
,
738 const char *buildid_data
, size_t buildid_size
,
739 backtrace_error_callback error_callback
,
742 const char * const prefix
= "/usr/lib/debug/.build-id/";
743 const size_t prefix_len
= strlen (prefix
);
744 const char * const suffix
= ".debug";
745 const size_t suffix_len
= strlen (suffix
);
753 len
= prefix_len
+ buildid_size
* 2 + suffix_len
+ 2;
754 bd_filename
= backtrace_alloc (state
, len
, error_callback
, data
);
755 if (bd_filename
== NULL
)
759 memcpy (t
, prefix
, prefix_len
);
761 for (i
= 0; i
< buildid_size
; i
++)
766 b
= (unsigned char) buildid_data
[i
];
767 nib
= (b
& 0xf0) >> 4;
768 *t
++ = nib
< 10 ? '0' + nib
: 'a' + nib
- 10;
770 *t
++ = nib
< 10 ? '0' + nib
: 'a' + nib
- 10;
774 memcpy (t
, suffix
, suffix_len
);
775 t
[suffix_len
] = '\0';
777 ret
= backtrace_open (bd_filename
, error_callback
, data
, &does_not_exist
);
779 backtrace_free (state
, bd_filename
, len
, error_callback
, data
);
781 /* gdb checks that the debuginfo file has the same build ID note.
782 That seems kind of pointless to me--why would it have the right
783 name but not the right build ID?--so skipping the check. */
788 /* Try to open a file whose name is PREFIX (length PREFIX_LEN)
789 concatenated with PREFIX2 (length PREFIX2_LEN) concatenated with
790 DEBUGLINK_NAME. Returns an open file descriptor, or -1. */
793 elf_try_debugfile (struct backtrace_state
*state
, const char *prefix
,
794 size_t prefix_len
, const char *prefix2
, size_t prefix2_len
,
795 const char *debuglink_name
,
796 backtrace_error_callback error_callback
, void *data
)
798 size_t debuglink_len
;
804 debuglink_len
= strlen (debuglink_name
);
805 try_len
= prefix_len
+ prefix2_len
+ debuglink_len
+ 1;
806 try = backtrace_alloc (state
, try_len
, error_callback
, data
);
810 memcpy (try, prefix
, prefix_len
);
811 memcpy (try + prefix_len
, prefix2
, prefix2_len
);
812 memcpy (try + prefix_len
+ prefix2_len
, debuglink_name
, debuglink_len
);
813 try[prefix_len
+ prefix2_len
+ debuglink_len
] = '\0';
815 ret
= backtrace_open (try, error_callback
, data
, &does_not_exist
);
817 backtrace_free (state
, try, try_len
, error_callback
, data
);
822 /* Find a separate debug info file, using the debuglink section data
823 to find it. Returns an open file descriptor, or -1. */
826 elf_find_debugfile_by_debuglink (struct backtrace_state
*state
,
827 const char *filename
,
828 const char *debuglink_name
,
829 backtrace_error_callback error_callback
,
840 /* Resolve symlinks in FILENAME. Since FILENAME is fairly likely to
841 be /proc/self/exe, symlinks are common. We don't try to resolve
842 the whole path name, just the base name. */
846 while (elf_is_symlink (filename
))
851 new_buf
= elf_readlink (state
, filename
, error_callback
, data
, &new_len
);
855 if (new_buf
[0] == '/')
859 slash
= strrchr (filename
, '/');
868 clen
= slash
- filename
+ strlen (new_buf
) + 1;
869 c
= backtrace_alloc (state
, clen
, error_callback
, data
);
873 memcpy (c
, filename
, slash
- filename
);
874 memcpy (c
+ (slash
- filename
), new_buf
, strlen (new_buf
));
875 c
[slash
- filename
+ strlen (new_buf
)] = '\0';
876 backtrace_free (state
, new_buf
, new_len
, error_callback
, data
);
884 backtrace_free (state
, alc
, alc_len
, error_callback
, data
);
889 /* Look for DEBUGLINK_NAME in the same directory as FILENAME. */
891 slash
= strrchr (filename
, '/');
901 prefix_len
= slash
- filename
;
904 ddescriptor
= elf_try_debugfile (state
, prefix
, prefix_len
, "", 0,
905 debuglink_name
, error_callback
, data
);
906 if (ddescriptor
>= 0)
912 /* Look for DEBUGLINK_NAME in a .debug subdirectory of FILENAME. */
914 ddescriptor
= elf_try_debugfile (state
, prefix
, prefix_len
, ".debug/",
915 strlen (".debug/"), debuglink_name
,
916 error_callback
, data
);
917 if (ddescriptor
>= 0)
923 /* Look for DEBUGLINK_NAME in /usr/lib/debug. */
925 ddescriptor
= elf_try_debugfile (state
, "/usr/lib/debug/",
926 strlen ("/usr/lib/debug/"), prefix
,
927 prefix_len
, debuglink_name
,
928 error_callback
, data
);
929 if (ddescriptor
>= 0)
933 if (alc
!= NULL
&& alc_len
> 0)
934 backtrace_free (state
, alc
, alc_len
, error_callback
, data
);
938 /* Open a separate debug info file, using the debuglink section data
939 to find it. Returns an open file descriptor, or -1. */
942 elf_open_debugfile_by_debuglink (struct backtrace_state
*state
,
943 const char *filename
,
944 const char *debuglink_name
,
945 uint32_t debuglink_crc
,
946 backtrace_error_callback error_callback
,
952 ddescriptor
= elf_find_debugfile_by_debuglink (state
, filename
,
954 error_callback
, data
);
958 got_crc
= elf_crc32_file (state
, ddescriptor
, error_callback
, data
);
959 if (got_crc
!= debuglink_crc
)
961 backtrace_close (ddescriptor
, error_callback
, data
);
968 /* Add the backtrace data for one ELF file. Returns 1 on success,
969 0 on failure (in both cases descriptor is closed) or -1 if exe
970 is non-zero and the ELF file is ET_DYN, which tells the caller that
971 elf_add will need to be called on the descriptor again after
972 base_address is determined. */
975 elf_add (struct backtrace_state
*state
, const char *filename
, int descriptor
,
976 uintptr_t base_address
, backtrace_error_callback error_callback
,
977 void *data
, fileline
*fileline_fn
, int *found_sym
, int *found_dwarf
,
978 int exe
, int debuginfo
)
980 struct backtrace_view ehdr_view
;
984 unsigned int shstrndx
;
985 struct backtrace_view shdrs_view
;
986 int shdrs_view_valid
;
987 const b_elf_shdr
*shdrs
;
988 const b_elf_shdr
*shstrhdr
;
991 struct backtrace_view names_view
;
992 int names_view_valid
;
994 unsigned int symtab_shndx
;
995 unsigned int dynsym_shndx
;
997 struct debug_section_info sections
[DEBUG_MAX
];
998 struct backtrace_view symtab_view
;
999 int symtab_view_valid
;
1000 struct backtrace_view strtab_view
;
1001 int strtab_view_valid
;
1002 struct backtrace_view buildid_view
;
1003 int buildid_view_valid
;
1004 const char *buildid_data
;
1005 uint32_t buildid_size
;
1006 struct backtrace_view debuglink_view
;
1007 int debuglink_view_valid
;
1008 const char *debuglink_name
;
1009 uint32_t debuglink_crc
;
1012 struct backtrace_view debug_view
;
1013 int debug_view_valid
;
1018 shdrs_view_valid
= 0;
1019 names_view_valid
= 0;
1020 symtab_view_valid
= 0;
1021 strtab_view_valid
= 0;
1022 buildid_view_valid
= 0;
1023 buildid_data
= NULL
;
1025 debuglink_view_valid
= 0;
1026 debuglink_name
= NULL
;
1028 debug_view_valid
= 0;
1030 if (!backtrace_get_view (state
, descriptor
, 0, sizeof ehdr
, error_callback
,
1034 memcpy (&ehdr
, ehdr_view
.data
, sizeof ehdr
);
1036 backtrace_release_view (state
, &ehdr_view
, error_callback
, data
);
1038 if (ehdr
.e_ident
[EI_MAG0
] != ELFMAG0
1039 || ehdr
.e_ident
[EI_MAG1
] != ELFMAG1
1040 || ehdr
.e_ident
[EI_MAG2
] != ELFMAG2
1041 || ehdr
.e_ident
[EI_MAG3
] != ELFMAG3
)
1043 error_callback (data
, "executable file is not ELF", 0);
1046 if (ehdr
.e_ident
[EI_VERSION
] != EV_CURRENT
)
1048 error_callback (data
, "executable file is unrecognized ELF version", 0);
1052 #if BACKTRACE_ELF_SIZE == 32
1053 #define BACKTRACE_ELFCLASS ELFCLASS32
1055 #define BACKTRACE_ELFCLASS ELFCLASS64
1058 if (ehdr
.e_ident
[EI_CLASS
] != BACKTRACE_ELFCLASS
)
1060 error_callback (data
, "executable file is unexpected ELF class", 0);
1064 if (ehdr
.e_ident
[EI_DATA
] != ELFDATA2LSB
1065 && ehdr
.e_ident
[EI_DATA
] != ELFDATA2MSB
)
1067 error_callback (data
, "executable file has unknown endianness", 0);
1071 /* If the executable is ET_DYN, it is either a PIE, or we are running
1072 directly a shared library with .interp. We need to wait for
1073 dl_iterate_phdr in that case to determine the actual base_address. */
1074 if (exe
&& ehdr
.e_type
== ET_DYN
)
1077 shoff
= ehdr
.e_shoff
;
1078 shnum
= ehdr
.e_shnum
;
1079 shstrndx
= ehdr
.e_shstrndx
;
1081 if ((shnum
== 0 || shstrndx
== SHN_XINDEX
)
1084 struct backtrace_view shdr_view
;
1085 const b_elf_shdr
*shdr
;
1087 if (!backtrace_get_view (state
, descriptor
, shoff
, sizeof shdr
,
1088 error_callback
, data
, &shdr_view
))
1091 shdr
= (const b_elf_shdr
*) shdr_view
.data
;
1094 shnum
= shdr
->sh_size
;
1096 if (shstrndx
== SHN_XINDEX
)
1098 shstrndx
= shdr
->sh_link
;
1100 /* Versions of the GNU binutils between 2.12 and 2.18 did
1101 not handle objects with more than SHN_LORESERVE sections
1102 correctly. All large section indexes were offset by
1103 0x100. There is more information at
1104 http://sourceware.org/bugzilla/show_bug.cgi?id-5900 .
1105 Fortunately these object files are easy to detect, as the
1106 GNU binutils always put the section header string table
1107 near the end of the list of sections. Thus if the
1108 section header string table index is larger than the
1109 number of sections, then we know we have to subtract
1110 0x100 to get the real section index. */
1111 if (shstrndx
>= shnum
&& shstrndx
>= SHN_LORESERVE
+ 0x100)
1115 backtrace_release_view (state
, &shdr_view
, error_callback
, data
);
1118 /* To translate PC to file/line when using DWARF, we need to find
1119 the .debug_info and .debug_line sections. */
1121 /* Read the section headers, skipping the first one. */
1123 if (!backtrace_get_view (state
, descriptor
, shoff
+ sizeof (b_elf_shdr
),
1124 (shnum
- 1) * sizeof (b_elf_shdr
),
1125 error_callback
, data
, &shdrs_view
))
1127 shdrs_view_valid
= 1;
1128 shdrs
= (const b_elf_shdr
*) shdrs_view
.data
;
1130 /* Read the section names. */
1132 shstrhdr
= &shdrs
[shstrndx
- 1];
1133 shstr_size
= shstrhdr
->sh_size
;
1134 shstr_off
= shstrhdr
->sh_offset
;
1136 if (!backtrace_get_view (state
, descriptor
, shstr_off
, shstr_size
,
1137 error_callback
, data
, &names_view
))
1139 names_view_valid
= 1;
1140 names
= (const char *) names_view
.data
;
1145 memset (sections
, 0, sizeof sections
);
1147 /* Look for the symbol table. */
1148 for (i
= 1; i
< shnum
; ++i
)
1150 const b_elf_shdr
*shdr
;
1151 unsigned int sh_name
;
1155 shdr
= &shdrs
[i
- 1];
1157 if (shdr
->sh_type
== SHT_SYMTAB
)
1159 else if (shdr
->sh_type
== SHT_DYNSYM
)
1162 sh_name
= shdr
->sh_name
;
1163 if (sh_name
>= shstr_size
)
1165 error_callback (data
, "ELF section name out of range", 0);
1169 name
= names
+ sh_name
;
1171 for (j
= 0; j
< (int) DEBUG_MAX
; ++j
)
1173 if (strcmp (name
, debug_section_names
[j
]) == 0)
1175 sections
[j
].offset
= shdr
->sh_offset
;
1176 sections
[j
].size
= shdr
->sh_size
;
1181 /* Read the build ID if present. This could check for any
1182 SHT_NOTE section with the right note name and type, but gdb
1183 looks for a specific section name. */
1185 && !buildid_view_valid
1186 && strcmp (name
, ".note.gnu.build-id") == 0)
1188 const b_elf_note
*note
;
1190 if (!backtrace_get_view (state
, descriptor
, shdr
->sh_offset
,
1191 shdr
->sh_size
, error_callback
, data
,
1195 buildid_view_valid
= 1;
1196 note
= (const b_elf_note
*) buildid_view
.data
;
1197 if (note
->type
== NT_GNU_BUILD_ID
1198 && note
->namesz
== 4
1199 && strncmp (note
->name
, "GNU", 4) == 0
1200 && shdr
->sh_size
< 12 + ((note
->namesz
+ 3) & ~ 3) + note
->descsz
)
1202 buildid_data
= ¬e
->name
[0] + ((note
->namesz
+ 3) & ~ 3);
1203 buildid_size
= note
->descsz
;
1207 /* Read the debuglink file if present. */
1209 && !debuglink_view_valid
1210 && strcmp (name
, ".gnu_debuglink") == 0)
1212 const char *debuglink_data
;
1215 if (!backtrace_get_view (state
, descriptor
, shdr
->sh_offset
,
1216 shdr
->sh_size
, error_callback
, data
,
1220 debuglink_view_valid
= 1;
1221 debuglink_data
= (const char *) debuglink_view
.data
;
1222 crc_offset
= strnlen (debuglink_data
, shdr
->sh_size
);
1223 crc_offset
= (crc_offset
+ 3) & ~3;
1224 if (crc_offset
+ 4 <= shdr
->sh_size
)
1226 debuglink_name
= debuglink_data
;
1227 debuglink_crc
= *(const uint32_t*)(debuglink_data
+ crc_offset
);
1232 if (symtab_shndx
== 0)
1233 symtab_shndx
= dynsym_shndx
;
1234 if (symtab_shndx
!= 0 && !debuginfo
)
1236 const b_elf_shdr
*symtab_shdr
;
1237 unsigned int strtab_shndx
;
1238 const b_elf_shdr
*strtab_shdr
;
1239 struct elf_syminfo_data
*sdata
;
1241 symtab_shdr
= &shdrs
[symtab_shndx
- 1];
1242 strtab_shndx
= symtab_shdr
->sh_link
;
1243 if (strtab_shndx
>= shnum
)
1245 error_callback (data
,
1246 "ELF symbol table strtab link out of range", 0);
1249 strtab_shdr
= &shdrs
[strtab_shndx
- 1];
1251 if (!backtrace_get_view (state
, descriptor
, symtab_shdr
->sh_offset
,
1252 symtab_shdr
->sh_size
, error_callback
, data
,
1255 symtab_view_valid
= 1;
1257 if (!backtrace_get_view (state
, descriptor
, strtab_shdr
->sh_offset
,
1258 strtab_shdr
->sh_size
, error_callback
, data
,
1261 strtab_view_valid
= 1;
1263 sdata
= ((struct elf_syminfo_data
*)
1264 backtrace_alloc (state
, sizeof *sdata
, error_callback
, data
));
1268 if (!elf_initialize_syminfo (state
, base_address
,
1269 symtab_view
.data
, symtab_shdr
->sh_size
,
1270 strtab_view
.data
, strtab_shdr
->sh_size
,
1271 error_callback
, data
, sdata
))
1273 backtrace_free (state
, sdata
, sizeof *sdata
, error_callback
, data
);
1277 /* We no longer need the symbol table, but we hold on to the
1278 string table permanently. */
1279 backtrace_release_view (state
, &symtab_view
, error_callback
, data
);
1280 symtab_view_valid
= 0;
1284 elf_add_syminfo_data (state
, sdata
);
1287 /* FIXME: Need to handle compressed debug sections. */
1289 backtrace_release_view (state
, &shdrs_view
, error_callback
, data
);
1290 shdrs_view_valid
= 0;
1291 backtrace_release_view (state
, &names_view
, error_callback
, data
);
1292 names_view_valid
= 0;
1294 /* If the debug info is in a separate file, read that one instead. */
1296 if (buildid_data
!= NULL
)
1300 d
= elf_open_debugfile_by_buildid (state
, buildid_data
, buildid_size
,
1301 error_callback
, data
);
1304 backtrace_release_view (state
, &buildid_view
, error_callback
, data
);
1305 if (debuglink_view_valid
)
1306 backtrace_release_view (state
, &debuglink_view
, error_callback
,
1308 return elf_add (state
, NULL
, d
, base_address
, error_callback
, data
,
1309 fileline_fn
, found_sym
, found_dwarf
, 0, 1);
1313 if (buildid_view_valid
)
1315 backtrace_release_view (state
, &buildid_view
, error_callback
, data
);
1316 buildid_view_valid
= 0;
1319 if (debuglink_name
!= NULL
)
1323 d
= elf_open_debugfile_by_debuglink (state
, filename
, debuglink_name
,
1324 debuglink_crc
, error_callback
,
1328 backtrace_release_view (state
, &debuglink_view
, error_callback
,
1330 return elf_add (state
, NULL
, d
, base_address
, error_callback
, data
,
1331 fileline_fn
, found_sym
, found_dwarf
, 0, 1);
1335 if (debuglink_view_valid
)
1337 backtrace_release_view (state
, &debuglink_view
, error_callback
, data
);
1338 debuglink_view_valid
= 0;
1341 /* Read all the debug sections in a single view, since they are
1342 probably adjacent in the file. We never release this view. */
1346 for (i
= 0; i
< (int) DEBUG_MAX
; ++i
)
1350 if (sections
[i
].size
== 0)
1352 if (min_offset
== 0 || sections
[i
].offset
< min_offset
)
1353 min_offset
= sections
[i
].offset
;
1354 end
= sections
[i
].offset
+ sections
[i
].size
;
1355 if (end
> max_offset
)
1358 if (min_offset
== 0 || max_offset
== 0)
1360 if (!backtrace_close (descriptor
, error_callback
, data
))
1365 if (!backtrace_get_view (state
, descriptor
, min_offset
,
1366 max_offset
- min_offset
,
1367 error_callback
, data
, &debug_view
))
1369 debug_view_valid
= 1;
1371 /* We've read all we need from the executable. */
1372 if (!backtrace_close (descriptor
, error_callback
, data
))
1376 for (i
= 0; i
< (int) DEBUG_MAX
; ++i
)
1378 if (sections
[i
].size
== 0)
1379 sections
[i
].data
= NULL
;
1381 sections
[i
].data
= ((const unsigned char *) debug_view
.data
1382 + (sections
[i
].offset
- min_offset
));
1385 if (!backtrace_dwarf_add (state
, base_address
,
1386 sections
[DEBUG_INFO
].data
,
1387 sections
[DEBUG_INFO
].size
,
1388 sections
[DEBUG_LINE
].data
,
1389 sections
[DEBUG_LINE
].size
,
1390 sections
[DEBUG_ABBREV
].data
,
1391 sections
[DEBUG_ABBREV
].size
,
1392 sections
[DEBUG_RANGES
].data
,
1393 sections
[DEBUG_RANGES
].size
,
1394 sections
[DEBUG_STR
].data
,
1395 sections
[DEBUG_STR
].size
,
1396 ehdr
.e_ident
[EI_DATA
] == ELFDATA2MSB
,
1397 error_callback
, data
, fileline_fn
))
1405 if (shdrs_view_valid
)
1406 backtrace_release_view (state
, &shdrs_view
, error_callback
, data
);
1407 if (names_view_valid
)
1408 backtrace_release_view (state
, &names_view
, error_callback
, data
);
1409 if (symtab_view_valid
)
1410 backtrace_release_view (state
, &symtab_view
, error_callback
, data
);
1411 if (strtab_view_valid
)
1412 backtrace_release_view (state
, &strtab_view
, error_callback
, data
);
1413 if (debuglink_view_valid
)
1414 backtrace_release_view (state
, &debuglink_view
, error_callback
, data
);
1415 if (buildid_view_valid
)
1416 backtrace_release_view (state
, &buildid_view
, error_callback
, data
);
1417 if (debug_view_valid
)
1418 backtrace_release_view (state
, &debug_view
, error_callback
, data
);
1419 if (descriptor
!= -1)
1420 backtrace_close (descriptor
, error_callback
, data
);
1424 /* Data passed to phdr_callback. */
1428 struct backtrace_state
*state
;
1429 backtrace_error_callback error_callback
;
1431 fileline
*fileline_fn
;
1434 const char *exe_filename
;
1438 /* Callback passed to dl_iterate_phdr. Load debug info from shared
1443 __attribute__ ((__force_align_arg_pointer__
))
1445 phdr_callback (struct dl_phdr_info
*info
, size_t size ATTRIBUTE_UNUSED
,
1448 struct phdr_data
*pd
= (struct phdr_data
*) pdata
;
1449 const char *filename
;
1452 fileline elf_fileline_fn
;
1455 /* There is not much we can do if we don't have the module name,
1456 unless executable is ET_DYN, where we expect the very first
1457 phdr_callback to be for the PIE. */
1458 if (info
->dlpi_name
== NULL
|| info
->dlpi_name
[0] == '\0')
1460 if (pd
->exe_descriptor
== -1)
1462 filename
= pd
->exe_filename
;
1463 descriptor
= pd
->exe_descriptor
;
1464 pd
->exe_descriptor
= -1;
1468 if (pd
->exe_descriptor
!= -1)
1470 backtrace_close (pd
->exe_descriptor
, pd
->error_callback
, pd
->data
);
1471 pd
->exe_descriptor
= -1;
1474 filename
= info
->dlpi_name
;
1475 descriptor
= backtrace_open (info
->dlpi_name
, pd
->error_callback
,
1476 pd
->data
, &does_not_exist
);
1481 if (elf_add (pd
->state
, filename
, descriptor
, info
->dlpi_addr
,
1482 pd
->error_callback
, pd
->data
, &elf_fileline_fn
, pd
->found_sym
,
1483 &found_dwarf
, 0, 0))
1487 *pd
->found_dwarf
= 1;
1488 *pd
->fileline_fn
= elf_fileline_fn
;
1495 /* Initialize the backtrace data we need from an ELF executable. At
1496 the ELF level, all we need to do is find the debug info
1500 backtrace_initialize (struct backtrace_state
*state
, const char *filename
,
1501 int descriptor
, backtrace_error_callback error_callback
,
1502 void *data
, fileline
*fileline_fn
)
1507 fileline elf_fileline_fn
= elf_nodebug
;
1508 struct phdr_data pd
;
1510 ret
= elf_add (state
, filename
, descriptor
, 0, error_callback
, data
,
1511 &elf_fileline_fn
, &found_sym
, &found_dwarf
, 1, 0);
1516 pd
.error_callback
= error_callback
;
1518 pd
.fileline_fn
= &elf_fileline_fn
;
1519 pd
.found_sym
= &found_sym
;
1520 pd
.found_dwarf
= &found_dwarf
;
1521 pd
.exe_filename
= filename
;
1522 pd
.exe_descriptor
= ret
< 0 ? descriptor
: -1;
1524 dl_iterate_phdr (phdr_callback
, (void *) &pd
);
1526 if (!state
->threaded
)
1529 state
->syminfo_fn
= elf_syminfo
;
1530 else if (state
->syminfo_fn
== NULL
)
1531 state
->syminfo_fn
= elf_nosyms
;
1536 backtrace_atomic_store_pointer (&state
->syminfo_fn
, elf_syminfo
);
1538 (void) __sync_bool_compare_and_swap (&state
->syminfo_fn
, NULL
,
1542 if (!state
->threaded
)
1543 *fileline_fn
= state
->fileline_fn
;
1545 *fileline_fn
= backtrace_atomic_load_pointer (&state
->fileline_fn
);
1547 if (*fileline_fn
== NULL
|| *fileline_fn
== elf_nodebug
)
1548 *fileline_fn
= elf_fileline_fn
;