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
78 #ifndef HAVE_DL_ITERATE_PHDR
80 /* Dummy version of dl_iterate_phdr for systems that don't have it. */
82 #define dl_phdr_info x_dl_phdr_info
83 #define dl_iterate_phdr x_dl_iterate_phdr
88 const char *dlpi_name
;
92 dl_iterate_phdr (int (*callback
) (struct dl_phdr_info
*,
93 size_t, void *) ATTRIBUTE_UNUSED
,
94 void *data ATTRIBUTE_UNUSED
)
99 #endif /* ! defined (HAVE_DL_ITERATE_PHDR) */
101 /* The configure script must tell us whether we are 32-bit or 64-bit
102 ELF. We could make this code test and support either possibility,
103 but there is no point. This code only works for the currently
104 running executable, which means that we know the ELF mode at
107 #if BACKTRACE_ELF_SIZE != 32 && BACKTRACE_ELF_SIZE != 64
108 #error "Unknown BACKTRACE_ELF_SIZE"
111 /* <link.h> might #include <elf.h> which might define our constants
112 with slightly different values. Undefine them to be safe. */
140 #undef NT_GNU_BUILD_ID
144 typedef uint16_t b_elf_half
; /* Elf_Half. */
145 typedef uint32_t b_elf_word
; /* Elf_Word. */
146 typedef int32_t b_elf_sword
; /* Elf_Sword. */
148 #if BACKTRACE_ELF_SIZE == 32
150 typedef uint32_t b_elf_addr
; /* Elf_Addr. */
151 typedef uint32_t b_elf_off
; /* Elf_Off. */
153 typedef uint32_t b_elf_wxword
; /* 32-bit Elf_Word, 64-bit ELF_Xword. */
157 typedef uint64_t b_elf_addr
; /* Elf_Addr. */
158 typedef uint64_t b_elf_off
; /* Elf_Off. */
159 typedef uint64_t b_elf_xword
; /* Elf_Xword. */
160 typedef int64_t b_elf_sxword
; /* Elf_Sxword. */
162 typedef uint64_t b_elf_wxword
; /* 32-bit Elf_Word, 64-bit ELF_Xword. */
166 /* Data structures and associated constants. */
171 unsigned char e_ident
[EI_NIDENT
]; /* ELF "magic number" */
172 b_elf_half e_type
; /* Identifies object file type */
173 b_elf_half e_machine
; /* Specifies required architecture */
174 b_elf_word e_version
; /* Identifies object file version */
175 b_elf_addr e_entry
; /* Entry point virtual address */
176 b_elf_off e_phoff
; /* Program header table file offset */
177 b_elf_off e_shoff
; /* Section header table file offset */
178 b_elf_word e_flags
; /* Processor-specific flags */
179 b_elf_half e_ehsize
; /* ELF header size in bytes */
180 b_elf_half e_phentsize
; /* Program header table entry size */
181 b_elf_half e_phnum
; /* Program header table entry count */
182 b_elf_half e_shentsize
; /* Section header table entry size */
183 b_elf_half e_shnum
; /* Section header table entry count */
184 b_elf_half e_shstrndx
; /* Section header string table index */
185 } b_elf_ehdr
; /* Elf_Ehdr. */
203 #define ELFDATA2LSB 1
204 #define ELFDATA2MSB 2
211 b_elf_word sh_name
; /* Section name, index in string tbl */
212 b_elf_word sh_type
; /* Type of section */
213 b_elf_wxword sh_flags
; /* Miscellaneous section attributes */
214 b_elf_addr sh_addr
; /* Section virtual addr at execution */
215 b_elf_off sh_offset
; /* Section file offset */
216 b_elf_wxword sh_size
; /* Size of section in bytes */
217 b_elf_word sh_link
; /* Index of another section */
218 b_elf_word sh_info
; /* Additional section information */
219 b_elf_wxword sh_addralign
; /* Section alignment */
220 b_elf_wxword sh_entsize
; /* Entry size if section holds table */
221 } b_elf_shdr
; /* Elf_Shdr. */
223 #define SHN_UNDEF 0x0000 /* Undefined section */
224 #define SHN_LORESERVE 0xFF00 /* Begin range of reserved indices */
225 #define SHN_XINDEX 0xFFFF /* Section index is held elsewhere */
229 #define SHT_DYNSYM 11
231 #if BACKTRACE_ELF_SIZE == 32
235 b_elf_word st_name
; /* Symbol name, index in string tbl */
236 b_elf_addr st_value
; /* Symbol value */
237 b_elf_word st_size
; /* Symbol size */
238 unsigned char st_info
; /* Symbol binding and type */
239 unsigned char st_other
; /* Visibility and other data */
240 b_elf_half st_shndx
; /* Symbol section index */
241 } b_elf_sym
; /* Elf_Sym. */
243 #else /* BACKTRACE_ELF_SIZE != 32 */
247 b_elf_word st_name
; /* Symbol name, index in string tbl */
248 unsigned char st_info
; /* Symbol binding and type */
249 unsigned char st_other
; /* Visibility and other data */
250 b_elf_half st_shndx
; /* Symbol section index */
251 b_elf_addr st_value
; /* Symbol value */
252 b_elf_xword st_size
; /* Symbol size */
253 } b_elf_sym
; /* Elf_Sym. */
255 #endif /* BACKTRACE_ELF_SIZE != 32 */
268 #define NT_GNU_BUILD_ID 3
270 /* An index of ELF sections we care about. */
282 /* Names of sections, indexed by enum elf_section. */
284 static const char * const debug_section_names
[DEBUG_MAX
] =
293 /* Information we gather for the sections we care about. */
295 struct debug_section_info
297 /* Section file offset. */
301 /* Section contents, after read from file. */
302 const unsigned char *data
;
305 /* Information we keep for an ELF symbol. */
309 /* The name of the symbol. */
311 /* The address of the symbol. */
313 /* The size of the symbol. */
317 /* Information to pass to elf_syminfo. */
319 struct elf_syminfo_data
321 /* Symbols for the next module. */
322 struct elf_syminfo_data
*next
;
323 /* The ELF symbols, sorted by address. */
324 struct elf_symbol
*symbols
;
325 /* The number of symbols. */
329 /* Compute the CRC-32 of BUF/LEN. This uses the CRC used for
330 .gnu_debuglink files. */
333 elf_crc32 (uint32_t crc
, const unsigned char *buf
, size_t len
)
335 static const uint32_t crc32_table
[256] =
337 0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419,
338 0x706af48f, 0xe963a535, 0x9e6495a3, 0x0edb8832, 0x79dcb8a4,
339 0xe0d5e91e, 0x97d2d988, 0x09b64c2b, 0x7eb17cbd, 0xe7b82d07,
340 0x90bf1d91, 0x1db71064, 0x6ab020f2, 0xf3b97148, 0x84be41de,
341 0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7, 0x136c9856,
342 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec, 0x14015c4f, 0x63066cd9,
343 0xfa0f3d63, 0x8d080df5, 0x3b6e20c8, 0x4c69105e, 0xd56041e4,
344 0xa2677172, 0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b,
345 0x35b5a8fa, 0x42b2986c, 0xdbbbc9d6, 0xacbcf940, 0x32d86ce3,
346 0x45df5c75, 0xdcd60dcf, 0xabd13d59, 0x26d930ac, 0x51de003a,
347 0xc8d75180, 0xbfd06116, 0x21b4f4b5, 0x56b3c423, 0xcfba9599,
348 0xb8bda50f, 0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924,
349 0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d, 0x76dc4190,
350 0x01db7106, 0x98d220bc, 0xefd5102a, 0x71b18589, 0x06b6b51f,
351 0x9fbfe4a5, 0xe8b8d433, 0x7807c9a2, 0x0f00f934, 0x9609a88e,
352 0xe10e9818, 0x7f6a0dbb, 0x086d3d2d, 0x91646c97, 0xe6635c01,
353 0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e, 0x6c0695ed,
354 0x1b01a57b, 0x8208f4c1, 0xf50fc457, 0x65b0d9c6, 0x12b7e950,
355 0x8bbeb8ea, 0xfcb9887c, 0x62dd1ddf, 0x15da2d49, 0x8cd37cf3,
356 0xfbd44c65, 0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2,
357 0x4adfa541, 0x3dd895d7, 0xa4d1c46d, 0xd3d6f4fb, 0x4369e96a,
358 0x346ed9fc, 0xad678846, 0xda60b8d0, 0x44042d73, 0x33031de5,
359 0xaa0a4c5f, 0xdd0d7cc9, 0x5005713c, 0x270241aa, 0xbe0b1010,
360 0xc90c2086, 0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f,
361 0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4, 0x59b33d17,
362 0x2eb40d81, 0xb7bd5c3b, 0xc0ba6cad, 0xedb88320, 0x9abfb3b6,
363 0x03b6e20c, 0x74b1d29a, 0xead54739, 0x9dd277af, 0x04db2615,
364 0x73dc1683, 0xe3630b12, 0x94643b84, 0x0d6d6a3e, 0x7a6a5aa8,
365 0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1, 0xf00f9344,
366 0x8708a3d2, 0x1e01f268, 0x6906c2fe, 0xf762575d, 0x806567cb,
367 0x196c3671, 0x6e6b06e7, 0xfed41b76, 0x89d32be0, 0x10da7a5a,
368 0x67dd4acc, 0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5,
369 0xd6d6a3e8, 0xa1d1937e, 0x38d8c2c4, 0x4fdff252, 0xd1bb67f1,
370 0xa6bc5767, 0x3fb506dd, 0x48b2364b, 0xd80d2bda, 0xaf0a1b4c,
371 0x36034af6, 0x41047a60, 0xdf60efc3, 0xa867df55, 0x316e8eef,
372 0x4669be79, 0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236,
373 0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f, 0xc5ba3bbe,
374 0xb2bd0b28, 0x2bb45a92, 0x5cb36a04, 0xc2d7ffa7, 0xb5d0cf31,
375 0x2cd99e8b, 0x5bdeae1d, 0x9b64c2b0, 0xec63f226, 0x756aa39c,
376 0x026d930a, 0x9c0906a9, 0xeb0e363f, 0x72076785, 0x05005713,
377 0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38, 0x92d28e9b,
378 0xe5d5be0d, 0x7cdcefb7, 0x0bdbdf21, 0x86d3d2d4, 0xf1d4e242,
379 0x68ddb3f8, 0x1fda836e, 0x81be16cd, 0xf6b9265b, 0x6fb077e1,
380 0x18b74777, 0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c,
381 0x8f659eff, 0xf862ae69, 0x616bffd3, 0x166ccf45, 0xa00ae278,
382 0xd70dd2ee, 0x4e048354, 0x3903b3c2, 0xa7672661, 0xd06016f7,
383 0x4969474d, 0x3e6e77db, 0xaed16a4a, 0xd9d65adc, 0x40df0b66,
384 0x37d83bf0, 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9,
385 0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6, 0xbad03605,
386 0xcdd70693, 0x54de5729, 0x23d967bf, 0xb3667a2e, 0xc4614ab8,
387 0x5d681b02, 0x2a6f2b94, 0xb40bbe37, 0xc30c8ea1, 0x5a05df1b,
390 const unsigned char *end
;
393 for (end
= buf
+ len
; buf
< end
; ++ buf
)
394 crc
= crc32_table
[(crc
^ *buf
) & 0xff] ^ (crc
>> 8);
398 /* Return the CRC-32 of the entire file open at DESCRIPTOR. */
401 elf_crc32_file (struct backtrace_state
*state
, int descriptor
,
402 backtrace_error_callback error_callback
, void *data
)
405 struct backtrace_view file_view
;
408 if (fstat (descriptor
, &st
) < 0)
410 error_callback (data
, "fstat", errno
);
414 if (!backtrace_get_view (state
, descriptor
, 0, st
.st_size
, error_callback
,
418 ret
= elf_crc32 (0, (const unsigned char *) file_view
.data
, st
.st_size
);
420 backtrace_release_view (state
, &file_view
, error_callback
, data
);
425 /* A dummy callback function used when we can't find any debug info. */
428 elf_nodebug (struct backtrace_state
*state ATTRIBUTE_UNUSED
,
429 uintptr_t pc ATTRIBUTE_UNUSED
,
430 backtrace_full_callback callback ATTRIBUTE_UNUSED
,
431 backtrace_error_callback error_callback
, void *data
)
433 error_callback (data
, "no debug info in ELF executable", -1);
437 /* A dummy callback function used when we can't find a symbol
441 elf_nosyms (struct backtrace_state
*state ATTRIBUTE_UNUSED
,
442 uintptr_t addr ATTRIBUTE_UNUSED
,
443 backtrace_syminfo_callback callback ATTRIBUTE_UNUSED
,
444 backtrace_error_callback error_callback
, void *data
)
446 error_callback (data
, "no symbol table in ELF executable", -1);
449 /* Compare struct elf_symbol for qsort. */
452 elf_symbol_compare (const void *v1
, const void *v2
)
454 const struct elf_symbol
*e1
= (const struct elf_symbol
*) v1
;
455 const struct elf_symbol
*e2
= (const struct elf_symbol
*) v2
;
457 if (e1
->address
< e2
->address
)
459 else if (e1
->address
> e2
->address
)
465 /* Compare an ADDR against an elf_symbol for bsearch. We allocate one
466 extra entry in the array so that this can look safely at the next
470 elf_symbol_search (const void *vkey
, const void *ventry
)
472 const uintptr_t *key
= (const uintptr_t *) vkey
;
473 const struct elf_symbol
*entry
= (const struct elf_symbol
*) ventry
;
477 if (addr
< entry
->address
)
479 else if (addr
>= entry
->address
+ entry
->size
)
485 /* Initialize the symbol table info for elf_syminfo. */
488 elf_initialize_syminfo (struct backtrace_state
*state
,
489 uintptr_t base_address
,
490 const unsigned char *symtab_data
, size_t symtab_size
,
491 const unsigned char *strtab
, size_t strtab_size
,
492 backtrace_error_callback error_callback
,
493 void *data
, struct elf_syminfo_data
*sdata
)
496 const b_elf_sym
*sym
;
497 size_t elf_symbol_count
;
498 size_t elf_symbol_size
;
499 struct elf_symbol
*elf_symbols
;
503 sym_count
= symtab_size
/ sizeof (b_elf_sym
);
505 /* We only care about function symbols. Count them. */
506 sym
= (const b_elf_sym
*) symtab_data
;
507 elf_symbol_count
= 0;
508 for (i
= 0; i
< sym_count
; ++i
, ++sym
)
512 info
= sym
->st_info
& 0xf;
513 if ((info
== STT_FUNC
|| info
== STT_OBJECT
)
514 && sym
->st_shndx
!= SHN_UNDEF
)
518 elf_symbol_size
= elf_symbol_count
* sizeof (struct elf_symbol
);
519 elf_symbols
= ((struct elf_symbol
*)
520 backtrace_alloc (state
, elf_symbol_size
, error_callback
,
522 if (elf_symbols
== NULL
)
525 sym
= (const b_elf_sym
*) symtab_data
;
527 for (i
= 0; i
< sym_count
; ++i
, ++sym
)
531 info
= sym
->st_info
& 0xf;
532 if (info
!= STT_FUNC
&& info
!= STT_OBJECT
)
534 if (sym
->st_shndx
== SHN_UNDEF
)
536 if (sym
->st_name
>= strtab_size
)
538 error_callback (data
, "symbol string index out of range", 0);
539 backtrace_free (state
, elf_symbols
, elf_symbol_size
, error_callback
,
543 elf_symbols
[j
].name
= (const char *) strtab
+ sym
->st_name
;
544 elf_symbols
[j
].address
= sym
->st_value
+ base_address
;
545 elf_symbols
[j
].size
= sym
->st_size
;
549 backtrace_qsort (elf_symbols
, elf_symbol_count
, sizeof (struct elf_symbol
),
553 sdata
->symbols
= elf_symbols
;
554 sdata
->count
= elf_symbol_count
;
559 /* Add EDATA to the list in STATE. */
562 elf_add_syminfo_data (struct backtrace_state
*state
,
563 struct elf_syminfo_data
*edata
)
565 if (!state
->threaded
)
567 struct elf_syminfo_data
**pp
;
569 for (pp
= (struct elf_syminfo_data
**) (void *) &state
->syminfo_data
;
579 struct elf_syminfo_data
**pp
;
581 pp
= (struct elf_syminfo_data
**) (void *) &state
->syminfo_data
;
585 struct elf_syminfo_data
*p
;
587 p
= backtrace_atomic_load_pointer (pp
);
595 if (__sync_bool_compare_and_swap (pp
, NULL
, edata
))
601 /* Return the symbol name and value for an ADDR. */
604 elf_syminfo (struct backtrace_state
*state
, uintptr_t addr
,
605 backtrace_syminfo_callback callback
,
606 backtrace_error_callback error_callback ATTRIBUTE_UNUSED
,
609 struct elf_syminfo_data
*edata
;
610 struct elf_symbol
*sym
= NULL
;
612 if (!state
->threaded
)
614 for (edata
= (struct elf_syminfo_data
*) state
->syminfo_data
;
618 sym
= ((struct elf_symbol
*)
619 bsearch (&addr
, edata
->symbols
, edata
->count
,
620 sizeof (struct elf_symbol
), elf_symbol_search
));
627 struct elf_syminfo_data
**pp
;
629 pp
= (struct elf_syminfo_data
**) (void *) &state
->syminfo_data
;
632 edata
= backtrace_atomic_load_pointer (pp
);
636 sym
= ((struct elf_symbol
*)
637 bsearch (&addr
, edata
->symbols
, edata
->count
,
638 sizeof (struct elf_symbol
), elf_symbol_search
));
647 callback (data
, addr
, NULL
, 0, 0);
649 callback (data
, addr
, sym
->name
, sym
->address
, sym
->size
);
652 /* Return whether FILENAME is a symlink. */
655 elf_is_symlink (const char *filename
)
659 if (lstat (filename
, &st
) < 0)
661 return S_ISLNK (st
.st_mode
);
664 /* Return the results of reading the symlink FILENAME in a buffer
665 allocated by backtrace_alloc. Return the length of the buffer in
669 elf_readlink (struct backtrace_state
*state
, const char *filename
,
670 backtrace_error_callback error_callback
, void *data
,
681 buf
= backtrace_alloc (state
, len
, error_callback
, data
);
684 rl
= readlink (filename
, buf
, len
);
687 backtrace_free (state
, buf
, len
, error_callback
, data
);
690 if ((size_t) rl
< len
- 1)
696 backtrace_free (state
, buf
, len
, error_callback
, data
);
701 /* Open a separate debug info file, using the build ID to find it.
702 Returns an open file descriptor, or -1.
704 The GDB manual says that the only place gdb looks for a debug file
705 when the build ID is known is in /usr/lib/debug/.build-id. */
708 elf_open_debugfile_by_buildid (struct backtrace_state
*state
,
709 const char *buildid_data
, size_t buildid_size
,
710 backtrace_error_callback error_callback
,
713 const char * const prefix
= "/usr/lib/debug/.build-id/";
714 const size_t prefix_len
= strlen (prefix
);
715 const char * const suffix
= ".debug";
716 const size_t suffix_len
= strlen (suffix
);
724 len
= prefix_len
+ buildid_size
* 2 + suffix_len
+ 2;
725 bd_filename
= backtrace_alloc (state
, len
, error_callback
, data
);
726 if (bd_filename
== NULL
)
730 memcpy (t
, prefix
, prefix_len
);
732 for (i
= 0; i
< buildid_size
; i
++)
737 b
= (unsigned char) buildid_data
[i
];
738 nib
= (b
& 0xf0) >> 4;
739 *t
++ = nib
< 10 ? '0' + nib
: 'a' + nib
- 10;
741 *t
++ = nib
< 10 ? '0' + nib
: 'a' + nib
- 10;
745 memcpy (t
, suffix
, suffix_len
);
746 t
[suffix_len
] = '\0';
748 ret
= backtrace_open (bd_filename
, error_callback
, data
, &does_not_exist
);
750 backtrace_free (state
, bd_filename
, len
, error_callback
, data
);
752 /* gdb checks that the debuginfo file has the same build ID note.
753 That seems kind of pointless to me--why would it have the right
754 name but not the right build ID?--so skipping the check. */
759 /* Try to open a file whose name is PREFIX (length PREFIX_LEN)
760 concatenated with PREFIX2 (length PREFIX2_LEN) concatenated with
761 DEBUGLINK_NAME. Returns an open file descriptor, or -1. */
764 elf_try_debugfile (struct backtrace_state
*state
, const char *prefix
,
765 size_t prefix_len
, const char *prefix2
, size_t prefix2_len
,
766 const char *debuglink_name
,
767 backtrace_error_callback error_callback
, void *data
)
769 size_t debuglink_len
;
775 debuglink_len
= strlen (debuglink_name
);
776 try_len
= prefix_len
+ prefix2_len
+ debuglink_len
+ 1;
777 try = backtrace_alloc (state
, try_len
, error_callback
, data
);
781 memcpy (try, prefix
, prefix_len
);
782 memcpy (try + prefix_len
, prefix2
, prefix2_len
);
783 memcpy (try + prefix_len
+ prefix2_len
, debuglink_name
, debuglink_len
);
784 try[prefix_len
+ prefix2_len
+ debuglink_len
] = '\0';
786 ret
= backtrace_open (try, error_callback
, data
, &does_not_exist
);
788 backtrace_free (state
, try, try_len
, error_callback
, data
);
793 /* Find a separate debug info file, using the debuglink section data
794 to find it. Returns an open file descriptor, or -1. */
797 elf_find_debugfile_by_debuglink (struct backtrace_state
*state
,
798 const char *filename
,
799 const char *debuglink_name
,
800 backtrace_error_callback error_callback
,
811 /* Resolve symlinks in FILENAME. Since FILENAME is fairly likely to
812 be /proc/self/exe, symlinks are common. We don't try to resolve
813 the whole path name, just the base name. */
817 while (elf_is_symlink (filename
))
822 new_buf
= elf_readlink (state
, filename
, error_callback
, data
, &new_len
);
826 if (new_buf
[0] == '/')
830 slash
= strrchr (filename
, '/');
839 clen
= slash
- filename
+ strlen (new_buf
) + 1;
840 c
= backtrace_alloc (state
, clen
, error_callback
, data
);
844 memcpy (c
, filename
, slash
- filename
);
845 memcpy (c
+ (slash
- filename
), new_buf
, strlen (new_buf
));
846 c
[slash
- filename
+ strlen (new_buf
)] = '\0';
847 backtrace_free (state
, new_buf
, new_len
, error_callback
, data
);
855 backtrace_free (state
, alc
, alc_len
, error_callback
, data
);
860 /* Look for DEBUGLINK_NAME in the same directory as FILENAME. */
862 slash
= strrchr (filename
, '/');
872 prefix_len
= slash
- filename
;
875 ddescriptor
= elf_try_debugfile (state
, prefix
, prefix_len
, "", 0,
876 debuglink_name
, error_callback
, data
);
877 if (ddescriptor
>= 0)
883 /* Look for DEBUGLINK_NAME in a .debug subdirectory of FILENAME. */
885 ddescriptor
= elf_try_debugfile (state
, prefix
, prefix_len
, ".debug/",
886 strlen (".debug/"), debuglink_name
,
887 error_callback
, data
);
888 if (ddescriptor
>= 0)
894 /* Look for DEBUGLINK_NAME in /usr/lib/debug. */
896 ddescriptor
= elf_try_debugfile (state
, "/usr/lib/debug/",
897 strlen ("/usr/lib/debug/"), prefix
,
898 prefix_len
, debuglink_name
,
899 error_callback
, data
);
900 if (ddescriptor
>= 0)
904 if (alc
!= NULL
&& alc_len
> 0)
905 backtrace_free (state
, alc
, alc_len
, error_callback
, data
);
909 /* Open a separate debug info file, using the debuglink section data
910 to find it. Returns an open file descriptor, or -1. */
913 elf_open_debugfile_by_debuglink (struct backtrace_state
*state
,
914 const char *filename
,
915 const char *debuglink_name
,
916 uint32_t debuglink_crc
,
917 backtrace_error_callback error_callback
,
923 ddescriptor
= elf_find_debugfile_by_debuglink (state
, filename
,
925 error_callback
, data
);
929 got_crc
= elf_crc32_file (state
, ddescriptor
, error_callback
, data
);
930 if (got_crc
!= debuglink_crc
)
932 backtrace_close (ddescriptor
, error_callback
, data
);
939 /* Add the backtrace data for one ELF file. Returns 1 on success,
940 0 on failure (in both cases descriptor is closed) or -1 if exe
941 is non-zero and the ELF file is ET_DYN, which tells the caller that
942 elf_add will need to be called on the descriptor again after
943 base_address is determined. */
946 elf_add (struct backtrace_state
*state
, const char *filename
, int descriptor
,
947 uintptr_t base_address
, backtrace_error_callback error_callback
,
948 void *data
, fileline
*fileline_fn
, int *found_sym
, int *found_dwarf
,
949 int exe
, int debuginfo
)
951 struct backtrace_view ehdr_view
;
955 unsigned int shstrndx
;
956 struct backtrace_view shdrs_view
;
957 int shdrs_view_valid
;
958 const b_elf_shdr
*shdrs
;
959 const b_elf_shdr
*shstrhdr
;
962 struct backtrace_view names_view
;
963 int names_view_valid
;
965 unsigned int symtab_shndx
;
966 unsigned int dynsym_shndx
;
968 struct debug_section_info sections
[DEBUG_MAX
];
969 struct backtrace_view symtab_view
;
970 int symtab_view_valid
;
971 struct backtrace_view strtab_view
;
972 int strtab_view_valid
;
973 struct backtrace_view buildid_view
;
974 int buildid_view_valid
;
975 const char *buildid_data
;
976 uint32_t buildid_size
;
977 struct backtrace_view debuglink_view
;
978 int debuglink_view_valid
;
979 const char *debuglink_name
;
980 uint32_t debuglink_crc
;
983 struct backtrace_view debug_view
;
984 int debug_view_valid
;
989 shdrs_view_valid
= 0;
990 names_view_valid
= 0;
991 symtab_view_valid
= 0;
992 strtab_view_valid
= 0;
993 buildid_view_valid
= 0;
996 debuglink_view_valid
= 0;
997 debuglink_name
= NULL
;
999 debug_view_valid
= 0;
1001 if (!backtrace_get_view (state
, descriptor
, 0, sizeof ehdr
, error_callback
,
1005 memcpy (&ehdr
, ehdr_view
.data
, sizeof ehdr
);
1007 backtrace_release_view (state
, &ehdr_view
, error_callback
, data
);
1009 if (ehdr
.e_ident
[EI_MAG0
] != ELFMAG0
1010 || ehdr
.e_ident
[EI_MAG1
] != ELFMAG1
1011 || ehdr
.e_ident
[EI_MAG2
] != ELFMAG2
1012 || ehdr
.e_ident
[EI_MAG3
] != ELFMAG3
)
1014 error_callback (data
, "executable file is not ELF", 0);
1017 if (ehdr
.e_ident
[EI_VERSION
] != EV_CURRENT
)
1019 error_callback (data
, "executable file is unrecognized ELF version", 0);
1023 #if BACKTRACE_ELF_SIZE == 32
1024 #define BACKTRACE_ELFCLASS ELFCLASS32
1026 #define BACKTRACE_ELFCLASS ELFCLASS64
1029 if (ehdr
.e_ident
[EI_CLASS
] != BACKTRACE_ELFCLASS
)
1031 error_callback (data
, "executable file is unexpected ELF class", 0);
1035 if (ehdr
.e_ident
[EI_DATA
] != ELFDATA2LSB
1036 && ehdr
.e_ident
[EI_DATA
] != ELFDATA2MSB
)
1038 error_callback (data
, "executable file has unknown endianness", 0);
1042 /* If the executable is ET_DYN, it is either a PIE, or we are running
1043 directly a shared library with .interp. We need to wait for
1044 dl_iterate_phdr in that case to determine the actual base_address. */
1045 if (exe
&& ehdr
.e_type
== ET_DYN
)
1048 shoff
= ehdr
.e_shoff
;
1049 shnum
= ehdr
.e_shnum
;
1050 shstrndx
= ehdr
.e_shstrndx
;
1052 if ((shnum
== 0 || shstrndx
== SHN_XINDEX
)
1055 struct backtrace_view shdr_view
;
1056 const b_elf_shdr
*shdr
;
1058 if (!backtrace_get_view (state
, descriptor
, shoff
, sizeof shdr
,
1059 error_callback
, data
, &shdr_view
))
1062 shdr
= (const b_elf_shdr
*) shdr_view
.data
;
1065 shnum
= shdr
->sh_size
;
1067 if (shstrndx
== SHN_XINDEX
)
1069 shstrndx
= shdr
->sh_link
;
1071 /* Versions of the GNU binutils between 2.12 and 2.18 did
1072 not handle objects with more than SHN_LORESERVE sections
1073 correctly. All large section indexes were offset by
1074 0x100. There is more information at
1075 http://sourceware.org/bugzilla/show_bug.cgi?id-5900 .
1076 Fortunately these object files are easy to detect, as the
1077 GNU binutils always put the section header string table
1078 near the end of the list of sections. Thus if the
1079 section header string table index is larger than the
1080 number of sections, then we know we have to subtract
1081 0x100 to get the real section index. */
1082 if (shstrndx
>= shnum
&& shstrndx
>= SHN_LORESERVE
+ 0x100)
1086 backtrace_release_view (state
, &shdr_view
, error_callback
, data
);
1089 /* To translate PC to file/line when using DWARF, we need to find
1090 the .debug_info and .debug_line sections. */
1092 /* Read the section headers, skipping the first one. */
1094 if (!backtrace_get_view (state
, descriptor
, shoff
+ sizeof (b_elf_shdr
),
1095 (shnum
- 1) * sizeof (b_elf_shdr
),
1096 error_callback
, data
, &shdrs_view
))
1098 shdrs_view_valid
= 1;
1099 shdrs
= (const b_elf_shdr
*) shdrs_view
.data
;
1101 /* Read the section names. */
1103 shstrhdr
= &shdrs
[shstrndx
- 1];
1104 shstr_size
= shstrhdr
->sh_size
;
1105 shstr_off
= shstrhdr
->sh_offset
;
1107 if (!backtrace_get_view (state
, descriptor
, shstr_off
, shstr_size
,
1108 error_callback
, data
, &names_view
))
1110 names_view_valid
= 1;
1111 names
= (const char *) names_view
.data
;
1116 memset (sections
, 0, sizeof sections
);
1118 /* Look for the symbol table. */
1119 for (i
= 1; i
< shnum
; ++i
)
1121 const b_elf_shdr
*shdr
;
1122 unsigned int sh_name
;
1126 shdr
= &shdrs
[i
- 1];
1128 if (shdr
->sh_type
== SHT_SYMTAB
)
1130 else if (shdr
->sh_type
== SHT_DYNSYM
)
1133 sh_name
= shdr
->sh_name
;
1134 if (sh_name
>= shstr_size
)
1136 error_callback (data
, "ELF section name out of range", 0);
1140 name
= names
+ sh_name
;
1142 for (j
= 0; j
< (int) DEBUG_MAX
; ++j
)
1144 if (strcmp (name
, debug_section_names
[j
]) == 0)
1146 sections
[j
].offset
= shdr
->sh_offset
;
1147 sections
[j
].size
= shdr
->sh_size
;
1152 /* Read the build ID if present. This could check for any
1153 SHT_NOTE section with the right note name and type, but gdb
1154 looks for a specific section name. */
1156 && !buildid_view_valid
1157 && strcmp (name
, ".note.gnu.build-id") == 0)
1159 const b_elf_note
*note
;
1161 if (!backtrace_get_view (state
, descriptor
, shdr
->sh_offset
,
1162 shdr
->sh_size
, error_callback
, data
,
1166 buildid_view_valid
= 1;
1167 note
= (const b_elf_note
*) buildid_view
.data
;
1168 if (note
->type
== NT_GNU_BUILD_ID
1169 && note
->namesz
== 4
1170 && strncmp (note
->name
, "GNU", 4) == 0
1171 && shdr
->sh_size
< 12 + ((note
->namesz
+ 3) & ~ 3) + note
->descsz
)
1173 buildid_data
= ¬e
->name
[0] + ((note
->namesz
+ 3) & ~ 3);
1174 buildid_size
= note
->descsz
;
1178 /* Read the debuglink file if present. */
1180 && !debuglink_view_valid
1181 && strcmp (name
, ".gnu_debuglink") == 0)
1183 const char *debuglink_data
;
1186 if (!backtrace_get_view (state
, descriptor
, shdr
->sh_offset
,
1187 shdr
->sh_size
, error_callback
, data
,
1191 debuglink_view_valid
= 1;
1192 debuglink_data
= (const char *) debuglink_view
.data
;
1193 crc_offset
= strnlen (debuglink_data
, shdr
->sh_size
);
1194 crc_offset
= (crc_offset
+ 3) & ~3;
1195 if (crc_offset
+ 4 <= shdr
->sh_size
)
1197 debuglink_name
= debuglink_data
;
1198 debuglink_crc
= *(const uint32_t*)(debuglink_data
+ crc_offset
);
1203 if (symtab_shndx
== 0)
1204 symtab_shndx
= dynsym_shndx
;
1205 if (symtab_shndx
!= 0 && !debuginfo
)
1207 const b_elf_shdr
*symtab_shdr
;
1208 unsigned int strtab_shndx
;
1209 const b_elf_shdr
*strtab_shdr
;
1210 struct elf_syminfo_data
*sdata
;
1212 symtab_shdr
= &shdrs
[symtab_shndx
- 1];
1213 strtab_shndx
= symtab_shdr
->sh_link
;
1214 if (strtab_shndx
>= shnum
)
1216 error_callback (data
,
1217 "ELF symbol table strtab link out of range", 0);
1220 strtab_shdr
= &shdrs
[strtab_shndx
- 1];
1222 if (!backtrace_get_view (state
, descriptor
, symtab_shdr
->sh_offset
,
1223 symtab_shdr
->sh_size
, error_callback
, data
,
1226 symtab_view_valid
= 1;
1228 if (!backtrace_get_view (state
, descriptor
, strtab_shdr
->sh_offset
,
1229 strtab_shdr
->sh_size
, error_callback
, data
,
1232 strtab_view_valid
= 1;
1234 sdata
= ((struct elf_syminfo_data
*)
1235 backtrace_alloc (state
, sizeof *sdata
, error_callback
, data
));
1239 if (!elf_initialize_syminfo (state
, base_address
,
1240 symtab_view
.data
, symtab_shdr
->sh_size
,
1241 strtab_view
.data
, strtab_shdr
->sh_size
,
1242 error_callback
, data
, sdata
))
1244 backtrace_free (state
, sdata
, sizeof *sdata
, error_callback
, data
);
1248 /* We no longer need the symbol table, but we hold on to the
1249 string table permanently. */
1250 backtrace_release_view (state
, &symtab_view
, error_callback
, data
);
1251 symtab_view_valid
= 0;
1255 elf_add_syminfo_data (state
, sdata
);
1258 /* FIXME: Need to handle compressed debug sections. */
1260 backtrace_release_view (state
, &shdrs_view
, error_callback
, data
);
1261 shdrs_view_valid
= 0;
1262 backtrace_release_view (state
, &names_view
, error_callback
, data
);
1263 names_view_valid
= 0;
1265 /* If the debug info is in a separate file, read that one instead. */
1267 if (buildid_data
!= NULL
)
1271 d
= elf_open_debugfile_by_buildid (state
, buildid_data
, buildid_size
,
1272 error_callback
, data
);
1275 backtrace_release_view (state
, &buildid_view
, error_callback
, data
);
1276 if (debuglink_view_valid
)
1277 backtrace_release_view (state
, &debuglink_view
, error_callback
,
1279 return elf_add (state
, NULL
, d
, base_address
, error_callback
, data
,
1280 fileline_fn
, found_sym
, found_dwarf
, 0, 1);
1284 if (buildid_view_valid
)
1286 backtrace_release_view (state
, &buildid_view
, error_callback
, data
);
1287 buildid_view_valid
= 0;
1290 if (debuglink_name
!= NULL
)
1294 d
= elf_open_debugfile_by_debuglink (state
, filename
, debuglink_name
,
1295 debuglink_crc
, error_callback
,
1299 backtrace_release_view (state
, &debuglink_view
, error_callback
,
1301 return elf_add (state
, NULL
, d
, base_address
, error_callback
, data
,
1302 fileline_fn
, found_sym
, found_dwarf
, 0, 1);
1306 if (debuglink_view_valid
)
1308 backtrace_release_view (state
, &debuglink_view
, error_callback
, data
);
1309 debuglink_view_valid
= 0;
1312 /* Read all the debug sections in a single view, since they are
1313 probably adjacent in the file. We never release this view. */
1317 for (i
= 0; i
< (int) DEBUG_MAX
; ++i
)
1321 if (sections
[i
].size
== 0)
1323 if (min_offset
== 0 || sections
[i
].offset
< min_offset
)
1324 min_offset
= sections
[i
].offset
;
1325 end
= sections
[i
].offset
+ sections
[i
].size
;
1326 if (end
> max_offset
)
1329 if (min_offset
== 0 || max_offset
== 0)
1331 if (!backtrace_close (descriptor
, error_callback
, data
))
1336 if (!backtrace_get_view (state
, descriptor
, min_offset
,
1337 max_offset
- min_offset
,
1338 error_callback
, data
, &debug_view
))
1340 debug_view_valid
= 1;
1342 /* We've read all we need from the executable. */
1343 if (!backtrace_close (descriptor
, error_callback
, data
))
1347 for (i
= 0; i
< (int) DEBUG_MAX
; ++i
)
1349 if (sections
[i
].size
== 0)
1350 sections
[i
].data
= NULL
;
1352 sections
[i
].data
= ((const unsigned char *) debug_view
.data
1353 + (sections
[i
].offset
- min_offset
));
1356 if (!backtrace_dwarf_add (state
, base_address
,
1357 sections
[DEBUG_INFO
].data
,
1358 sections
[DEBUG_INFO
].size
,
1359 sections
[DEBUG_LINE
].data
,
1360 sections
[DEBUG_LINE
].size
,
1361 sections
[DEBUG_ABBREV
].data
,
1362 sections
[DEBUG_ABBREV
].size
,
1363 sections
[DEBUG_RANGES
].data
,
1364 sections
[DEBUG_RANGES
].size
,
1365 sections
[DEBUG_STR
].data
,
1366 sections
[DEBUG_STR
].size
,
1367 ehdr
.e_ident
[EI_DATA
] == ELFDATA2MSB
,
1368 error_callback
, data
, fileline_fn
))
1376 if (shdrs_view_valid
)
1377 backtrace_release_view (state
, &shdrs_view
, error_callback
, data
);
1378 if (names_view_valid
)
1379 backtrace_release_view (state
, &names_view
, error_callback
, data
);
1380 if (symtab_view_valid
)
1381 backtrace_release_view (state
, &symtab_view
, error_callback
, data
);
1382 if (strtab_view_valid
)
1383 backtrace_release_view (state
, &strtab_view
, error_callback
, data
);
1384 if (debuglink_view_valid
)
1385 backtrace_release_view (state
, &debuglink_view
, error_callback
, data
);
1386 if (buildid_view_valid
)
1387 backtrace_release_view (state
, &buildid_view
, error_callback
, data
);
1388 if (debug_view_valid
)
1389 backtrace_release_view (state
, &debug_view
, error_callback
, data
);
1390 if (descriptor
!= -1)
1391 backtrace_close (descriptor
, error_callback
, data
);
1395 /* Data passed to phdr_callback. */
1399 struct backtrace_state
*state
;
1400 backtrace_error_callback error_callback
;
1402 fileline
*fileline_fn
;
1405 const char *exe_filename
;
1409 /* Callback passed to dl_iterate_phdr. Load debug info from shared
1414 __attribute__ ((__force_align_arg_pointer__
))
1416 phdr_callback (struct dl_phdr_info
*info
, size_t size ATTRIBUTE_UNUSED
,
1419 struct phdr_data
*pd
= (struct phdr_data
*) pdata
;
1420 const char *filename
;
1423 fileline elf_fileline_fn
;
1426 /* There is not much we can do if we don't have the module name,
1427 unless executable is ET_DYN, where we expect the very first
1428 phdr_callback to be for the PIE. */
1429 if (info
->dlpi_name
== NULL
|| info
->dlpi_name
[0] == '\0')
1431 if (pd
->exe_descriptor
== -1)
1433 filename
= pd
->exe_filename
;
1434 descriptor
= pd
->exe_descriptor
;
1435 pd
->exe_descriptor
= -1;
1439 if (pd
->exe_descriptor
!= -1)
1441 backtrace_close (pd
->exe_descriptor
, pd
->error_callback
, pd
->data
);
1442 pd
->exe_descriptor
= -1;
1445 filename
= info
->dlpi_name
;
1446 descriptor
= backtrace_open (info
->dlpi_name
, pd
->error_callback
,
1447 pd
->data
, &does_not_exist
);
1452 if (elf_add (pd
->state
, filename
, descriptor
, info
->dlpi_addr
,
1453 pd
->error_callback
, pd
->data
, &elf_fileline_fn
, pd
->found_sym
,
1454 &found_dwarf
, 0, 0))
1458 *pd
->found_dwarf
= 1;
1459 *pd
->fileline_fn
= elf_fileline_fn
;
1466 /* Initialize the backtrace data we need from an ELF executable. At
1467 the ELF level, all we need to do is find the debug info
1471 backtrace_initialize (struct backtrace_state
*state
, const char *filename
,
1472 int descriptor
, backtrace_error_callback error_callback
,
1473 void *data
, fileline
*fileline_fn
)
1478 fileline elf_fileline_fn
= elf_nodebug
;
1479 struct phdr_data pd
;
1481 ret
= elf_add (state
, filename
, descriptor
, 0, error_callback
, data
,
1482 &elf_fileline_fn
, &found_sym
, &found_dwarf
, 1, 0);
1487 pd
.error_callback
= error_callback
;
1489 pd
.fileline_fn
= &elf_fileline_fn
;
1490 pd
.found_sym
= &found_sym
;
1491 pd
.found_dwarf
= &found_dwarf
;
1492 pd
.exe_descriptor
= ret
< 0 ? descriptor
: -1;
1494 dl_iterate_phdr (phdr_callback
, (void *) &pd
);
1496 if (!state
->threaded
)
1499 state
->syminfo_fn
= elf_syminfo
;
1500 else if (state
->syminfo_fn
== NULL
)
1501 state
->syminfo_fn
= elf_nosyms
;
1506 backtrace_atomic_store_pointer (&state
->syminfo_fn
, elf_syminfo
);
1508 (void) __sync_bool_compare_and_swap (&state
->syminfo_fn
, NULL
,
1512 if (!state
->threaded
)
1513 *fileline_fn
= state
->fileline_fn
;
1515 *fileline_fn
= backtrace_atomic_load_pointer (&state
->fileline_fn
);
1517 if (*fileline_fn
== NULL
|| *fileline_fn
== elf_nodebug
)
1518 *fileline_fn
= elf_fileline_fn
;