1 /* elf.c -- Get debug data from an ELF file for backtraces.
2 Copyright (C) 2012-2014 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. */
37 #include <sys/types.h>
42 #ifdef HAVE_DL_ITERATE_PHDR
46 #include "backtrace.h"
49 #ifndef HAVE_DL_ITERATE_PHDR
51 /* Dummy version of dl_iterate_phdr for systems that don't have it. */
53 #define dl_phdr_info x_dl_phdr_info
54 #define dl_iterate_phdr x_dl_iterate_phdr
59 const char *dlpi_name;
63 dl_iterate_phdr (int (*callback) (struct dl_phdr_info *,
64 size_t, void *) ATTRIBUTE_UNUSED,
65 void *data ATTRIBUTE_UNUSED)
70 #endif /* ! defined (HAVE_DL_ITERATE_PHDR) */
72 /* The configure script must tell us whether we are 32-bit or 64-bit
73 ELF. We could make this code test and support either possibility,
74 but there is no point. This code only works for the currently
75 running executable, which means that we know the ELF mode at
78 #if BACKTRACE_ELF_SIZE != 32 && BACKTRACE_ELF_SIZE != 64
79 #error "Unknown BACKTRACE_ELF_SIZE"
82 /* <link.h> might #include <elf.h> which might define our constants
83 with slightly different values. Undefine them to be safe. */
114 typedef uint16_t b_elf_half; /* Elf_Half. */
115 typedef uint32_t b_elf_word; /* Elf_Word. */
116 typedef int32_t b_elf_sword; /* Elf_Sword. */
118 #if BACKTRACE_ELF_SIZE == 32
120 typedef uint32_t b_elf_addr; /* Elf_Addr. */
121 typedef uint32_t b_elf_off; /* Elf_Off. */
123 typedef uint32_t b_elf_wxword; /* 32-bit Elf_Word, 64-bit ELF_Xword. */
127 typedef uint64_t b_elf_addr; /* Elf_Addr. */
128 typedef uint64_t b_elf_off; /* Elf_Off. */
129 typedef uint64_t b_elf_xword; /* Elf_Xword. */
131 typedef uint64_t b_elf_wxword; /* 32-bit Elf_Word, 64-bit ELF_Xword. */
135 /* Data structures and associated constants. */
140 unsigned char e_ident[EI_NIDENT]; /* ELF "magic number" */
141 b_elf_half e_type; /* Identifies object file type */
142 b_elf_half e_machine; /* Specifies required architecture */
143 b_elf_word e_version; /* Identifies object file version */
144 b_elf_addr e_entry; /* Entry point virtual address */
145 b_elf_off e_phoff; /* Program header table file offset */
146 b_elf_off e_shoff; /* Section header table file offset */
147 b_elf_word e_flags; /* Processor-specific flags */
148 b_elf_half e_ehsize; /* ELF header size in bytes */
149 b_elf_half e_phentsize; /* Program header table entry size */
150 b_elf_half e_phnum; /* Program header table entry count */
151 b_elf_half e_shentsize; /* Section header table entry size */
152 b_elf_half e_shnum; /* Section header table entry count */
153 b_elf_half e_shstrndx; /* Section header string table index */
154 } b_elf_ehdr; /* Elf_Ehdr. */
172 #define ELFDATA2LSB 1
173 #define ELFDATA2MSB 2
180 b_elf_word sh_name; /* Section name, index in string tbl */
181 b_elf_word sh_type; /* Type of section */
182 b_elf_wxword sh_flags; /* Miscellaneous section attributes */
183 b_elf_addr sh_addr; /* Section virtual addr at execution */
184 b_elf_off sh_offset; /* Section file offset */
185 b_elf_wxword sh_size; /* Size of section in bytes */
186 b_elf_word sh_link; /* Index of another section */
187 b_elf_word sh_info; /* Additional section information */
188 b_elf_wxword sh_addralign; /* Section alignment */
189 b_elf_wxword sh_entsize; /* Entry size if section holds table */
190 } b_elf_shdr; /* Elf_Shdr. */
192 #define SHN_UNDEF 0x0000 /* Undefined section */
193 #define SHN_LORESERVE 0xFF00 /* Begin range of reserved indices */
194 #define SHN_XINDEX 0xFFFF /* Section index is held elsewhere */
198 #define SHT_DYNSYM 11
200 #if BACKTRACE_ELF_SIZE == 32
204 b_elf_word st_name; /* Symbol name, index in string tbl */
205 b_elf_addr st_value; /* Symbol value */
206 b_elf_word st_size; /* Symbol size */
207 unsigned char st_info; /* Symbol binding and type */
208 unsigned char st_other; /* Visibility and other data */
209 b_elf_half st_shndx; /* Symbol section index */
210 } b_elf_sym; /* Elf_Sym. */
212 #else /* BACKTRACE_ELF_SIZE != 32 */
216 b_elf_word st_name; /* Symbol name, index in string tbl */
217 unsigned char st_info; /* Symbol binding and type */
218 unsigned char st_other; /* Visibility and other data */
219 b_elf_half st_shndx; /* Symbol section index */
220 b_elf_addr st_value; /* Symbol value */
221 b_elf_xword st_size; /* Symbol size */
222 } b_elf_sym; /* Elf_Sym. */
224 #endif /* BACKTRACE_ELF_SIZE != 32 */
229 /* An index of ELF sections we care about. */
242 /* Names of sections, indexed by enum elf_section. */
244 static const char * const debug_section_names[DEBUG_MAX] =
254 /* Information we gather for the sections we care about. */
256 struct debug_section_info
258 /* Section file offset. */
262 /* Section contents, after read from file. */
263 const unsigned char *data;
266 /* Information we keep for an ELF symbol. */
270 /* The name of the symbol. */
272 /* The address of the symbol. */
274 /* The size of the symbol. */
278 /* Information to pass to elf_syminfo. */
280 struct elf_syminfo_data
282 /* Symbols for the next module. */
283 struct elf_syminfo_data *next;
284 /* The ELF symbols, sorted by address. */
285 struct elf_symbol *symbols;
286 /* The number of symbols. */
288 /* The base address for this module. */
289 uintptr_t base_address;
290 /* Address symbols size. */
291 uintptr_t symbol_size;
294 /* A dummy callback function used when we can't find any debug info. */
297 elf_nodebug (struct backtrace_state *state ATTRIBUTE_UNUSED,
298 uintptr_t pc ATTRIBUTE_UNUSED,
299 backtrace_full_callback callback ATTRIBUTE_UNUSED,
300 backtrace_error_callback error_callback, void *data)
302 error_callback (data, "no debug info in ELF executable", -1);
306 /* A dummy callback function used when we can't find a symbol
310 elf_nosyms (struct backtrace_state *state ATTRIBUTE_UNUSED,
311 uintptr_t addr ATTRIBUTE_UNUSED,
312 backtrace_syminfo_callback callback ATTRIBUTE_UNUSED,
313 backtrace_error_callback error_callback, void *data)
315 error_callback (data, "no symbol table in ELF executable", -1);
318 /* Compare struct elf_symbol for qsort. */
321 elf_symbol_compare (const void *v1, const void *v2)
323 const struct elf_symbol *e1 = (const struct elf_symbol *) v1;
324 const struct elf_symbol *e2 = (const struct elf_symbol *) v2;
326 if (e1->address < e2->address)
328 else if (e1->address > e2->address)
334 /* Compare an ADDR against an elf_symbol for bsearch. We allocate one
335 extra entry in the array so that this can look safely at the next
339 elf_symbol_search (const void *vkey, const void *ventry)
341 const uintptr_t *key = (const uintptr_t *) vkey;
342 const struct elf_symbol *entry = (const struct elf_symbol *) ventry;
346 if (addr < entry->address)
348 else if (addr >= entry->address + entry->size)
354 /* Initialize the symbol table info for elf_syminfo. */
357 elf_initialize_syminfo (struct backtrace_state *state,
358 uintptr_t base_address,
359 const unsigned char *symtab_data, size_t symtab_size,
360 const unsigned char *strtab, size_t strtab_size,
361 backtrace_error_callback error_callback,
362 void *data, struct elf_syminfo_data *sdata)
365 const b_elf_sym *sym;
366 size_t elf_symbol_count;
367 size_t elf_symbol_size;
368 struct elf_symbol *elf_symbols;
369 size_t symbol_size = 0;
373 sym_count = symtab_size / sizeof (b_elf_sym);
375 /* We only care about function symbols. Count them. */
376 sym = (const b_elf_sym *) symtab_data;
377 elf_symbol_count = 0;
378 for (i = 0; i < sym_count; ++i, ++sym)
382 info = sym->st_info & 0xf;
383 if ((info == STT_FUNC || info == STT_OBJECT)
384 && sym->st_shndx != SHN_UNDEF)
388 elf_symbol_size = elf_symbol_count * sizeof (struct elf_symbol);
389 elf_symbols = ((struct elf_symbol *)
390 backtrace_alloc (state, elf_symbol_size, error_callback,
392 if (elf_symbols == NULL)
395 sym = (const b_elf_sym *) symtab_data;
397 for (i = 0; i < sym_count; ++i, ++sym)
401 info = sym->st_info & 0xf;
402 if (info != STT_FUNC && info != STT_OBJECT)
404 if (sym->st_shndx == SHN_UNDEF)
406 if (sym->st_name >= strtab_size)
408 error_callback (data, "symbol string index out of range", 0);
409 backtrace_free (state, elf_symbols, elf_symbol_size, error_callback,
413 elf_symbols[j].name = (const char *) strtab + sym->st_name;
414 elf_symbols[j].address = sym->st_value + base_address;
415 elf_symbols[j].size = sym->st_size;
417 if (symbol_size < sym->st_value + sym->st_size)
418 symbol_size = sym->st_value + sym->st_size;
422 qsort (elf_symbols, elf_symbol_count, sizeof (struct elf_symbol),
426 sdata->symbols = elf_symbols;
427 sdata->count = elf_symbol_count;
428 sdata->base_address = base_address;
429 sdata->symbol_size = symbol_size;
434 /* Add EDATA to the list in STATE. */
437 elf_add_syminfo_data (struct backtrace_state *state,
438 struct elf_syminfo_data *edata)
440 if (!state->threaded)
442 struct elf_syminfo_data **pp;
444 for (pp = (struct elf_syminfo_data **) (void *) &state->syminfo_data;
454 struct elf_syminfo_data **pp;
456 pp = (struct elf_syminfo_data **) (void *) &state->syminfo_data;
460 struct elf_syminfo_data *p;
462 p = backtrace_atomic_load_pointer (pp);
470 if (__sync_bool_compare_and_swap (pp, NULL, edata))
476 /* Return the symbol name and value for an ADDR. */
479 elf_syminfo (struct backtrace_state *state, uintptr_t addr,
480 backtrace_syminfo_callback callback,
481 backtrace_error_callback error_callback ATTRIBUTE_UNUSED,
484 struct elf_syminfo_data *edata;
485 struct elf_symbol *sym = NULL;
487 if (!state->threaded)
489 for (edata = (struct elf_syminfo_data *) state->syminfo_data;
493 if ((addr >= edata->base_address) && (addr < edata->base_address + edata->symbol_size))
495 sym = ((struct elf_symbol *)
496 bsearch (&addr, edata->symbols, edata->count,
497 sizeof (struct elf_symbol), elf_symbol_search));
505 struct elf_syminfo_data **pp;
507 pp = (struct elf_syminfo_data **) (void *) &state->syminfo_data;
510 edata = backtrace_atomic_load_pointer (pp);
514 if ((addr >= edata->base_address) && (addr < edata->base_address + edata->symbol_size))
516 sym = ((struct elf_symbol *)
517 bsearch (&addr, edata->symbols, edata->count,
518 sizeof (struct elf_symbol), elf_symbol_search));
528 callback (data, addr, NULL, 0, 0);
530 callback (data, addr, sym->name, sym->address, sym->size);
533 /* Search this section for the .gnu_debuglink build id uuid. */
536 elf_parse_gnu_buildid(const uint8_t *data, size_t len, uint8_t uuid[20])
540 uint32_t name_len; // Length of note name
541 uint32_t desc_len; // Length of note descriptor
542 uint32_t type; // Type of note (1 is ABI_TAG, 3 is BUILD_ID)
543 char name[4]; // "GNU\0"
544 uint8_t uuid[16]; // This can be 16 or 20 bytes
547 /* Try to parse the note section (ie .note.gnu.build-id|.notes|.note|...) and get the build id.
548 BuildID documentation: https://fedoraproject.org/wiki/Releases/FeatureBuildId */
549 static const uint32_t s_gnu_build_id = 3; // NT_GNU_BUILD_ID from elf.h
551 while (len >= sizeof(elf_notehdr))
553 const elf_notehdr *notehdr = (const elf_notehdr *)data;
554 uint32_t name_len = (notehdr->name_len + 3) & ~3;
555 uint32_t desc_len = (notehdr->desc_len + 3) & ~3;
556 size_t offset_next_note = name_len + desc_len;
558 /* 16 bytes is UUID|MD5, 20 bytes is SHA1 */
559 if ((notehdr->type == s_gnu_build_id) &&
560 (desc_len == 16 || desc_len == 20) &&
561 (name_len == 4) && !strcmp(notehdr->name, "GNU"))
563 const uint8_t *uuidbuf = data + offsetof(elf_notehdr, uuid);
565 memcpy(uuid, uuidbuf, desc_len);
569 if (offset_next_note >= len)
571 data += offset_next_note;
572 len -= offset_next_note;
579 uuid_to_str(char *uuid_str, const uint8_t *uuid, int len)
582 static const char hex[] = "0123456789abcdef";
584 for (i = 0; i < len; i++)
588 *uuid_str++ = hex[c >> 4];
589 *uuid_str++ = hex[c & 0xf];
595 elf_add (struct backtrace_state *state, const char *filename, uintptr_t base_address,
596 backtrace_error_callback error_callback, void *data,
597 fileline *fileline_fn, int *found_sym, int *found_dwarf, int exe,
598 const uint8_t *uuid_to_match, uint32_t uuid_to_match_len);
600 /* Search and add the backtrace for a gnu_debuglink file. */
603 elf_add_gnu_debuglink (struct backtrace_state *state, const char *modulename, const char *gnu_debuglink,
604 uintptr_t base_address, backtrace_error_callback error_callback, void *data,
605 fileline *fileline_fn, int *found_sym, int *found_dwarf,
606 const uint8_t *uuid_to_match, uint32_t uuid_to_match_len)
610 char uuid_str[20 * 2 + 1];
611 const char *homedir = NULL;
612 const char *modulefile = strrchr(modulename, '/');
613 int moduledirlen = modulefile ? (modulefile - modulename) : 0;
615 uuid_to_str(uuid_str, uuid_to_match, uuid_to_match_len);
617 for (pass = 0;; ++pass)
624 /* Try the gnu_debuglink filename we were passed in. */
625 strncpy(file, gnu_debuglink, sizeof(file));
629 if (moduledirlen > 0)
631 /* Try current module directory. */
632 snprintf(file, sizeof(file), "%.*s/%s", moduledirlen, modulename, gnu_debuglink);
638 /* current exe directory. */
640 int len = readlink("/proc/self/exe", buf, sizeof(buf));
643 snprintf(file, sizeof(file), "%.*s/%s", len, buf, gnu_debuglink);
649 if (moduledirlen > 0)
651 /* Try /lib/x86_64-linux-gnu/libc-2.15.so --> /usr/lib/debug/lib/x86_64-linux-gnu/libc-2.15.so */
652 snprintf(file, sizeof(file), "/usr/lib/debug%.*s/%s", moduledirlen, modulename, gnu_debuglink);
658 struct passwd *pw = getpwuid(getuid());
659 homedir = pw ? pw->pw_dir : NULL;
662 /* try ~/.debug/.build-id/af/1da84a6c83719a4c92cc6e7144623c5a1d1f6d */
663 snprintf(file, sizeof(file), "%s/.debug/.build-id/%.2s/%s", homedir, uuid_str, uuid_str + 2);
671 /* try /home/mikesart/.debug/.build-id/af/1da84a6c83719a4c92cc6e7144623c5a1d1f6d.debug */
672 snprintf(file, sizeof(file), "%s/.debug/.build-id/%.2s/%s.debug", homedir, uuid_str, uuid_str + 2);
677 snprintf(file, sizeof(file), "%s/.build-id/%.2s/%s.debug", "/mnt/symstoresymbols/Debug", uuid_str, uuid_str + 2);
679 // Check debug-file-directory from .gdbinit maybe?
680 // /mnt/symstoresymbols/Debug/.build-id/f5/a99d37b2b105c50bccc3fd87e4cd27492f3032.debug
681 // set debug-file-directory /usr/lib/debug:/mnt/symstoresymbols/debug
688 file[sizeof(file) - 1] = 0;
690 /* If we've got a filename and the file exists, try loading it. */
692 if (file[0] && (access(file, F_OK) == 0))
694 if (elf_add(state, file, base_address,
695 error_callback, data,
696 fileline_fn, found_sym, found_dwarf, 0,
697 uuid_to_match, uuid_to_match_len))
699 /* Success. Woot! Return our fresh new symbols. */
709 /* Add the backtrace data for one ELF file. */
712 elf_add (struct backtrace_state *state, const char *filename, uintptr_t base_address,
713 backtrace_error_callback error_callback, void *data,
714 fileline *fileline_fn, int *found_sym, int *found_dwarf, int exe,
715 const uint8_t *uuid_to_match, uint32_t uuid_to_match_len)
717 struct backtrace_view ehdr_view;
721 unsigned int shstrndx;
722 struct backtrace_view shdrs_view;
723 int shdrs_view_valid;
724 const b_elf_shdr *shdrs;
725 const b_elf_shdr *shstrhdr;
728 struct backtrace_view names_view;
729 int names_view_valid;
731 unsigned int symtab_shndx;
732 unsigned int dynsym_shndx;
734 struct debug_section_info sections[DEBUG_MAX];
735 struct backtrace_view symtab_view;
736 int symtab_view_valid;
737 struct backtrace_view strtab_view;
738 int strtab_view_valid;
741 struct backtrace_view debug_view;
742 int debug_view_valid;
750 shdrs_view_valid = 0;
751 names_view_valid = 0;
752 symtab_view_valid = 0;
753 strtab_view_valid = 0;
754 debug_view_valid = 0;
756 int descriptor = backtrace_open (filename, error_callback, data, NULL);
760 if (!backtrace_get_view (state, descriptor, 0, sizeof ehdr, error_callback,
764 memcpy (&ehdr, ehdr_view.data, sizeof ehdr);
766 backtrace_release_view (state, &ehdr_view, error_callback, data);
768 if (ehdr.e_ident[EI_MAG0] != ELFMAG0
769 || ehdr.e_ident[EI_MAG1] != ELFMAG1
770 || ehdr.e_ident[EI_MAG2] != ELFMAG2
771 || ehdr.e_ident[EI_MAG3] != ELFMAG3)
773 error_callback (data, "executable file is not ELF", 0);
776 if (ehdr.e_ident[EI_VERSION] != EV_CURRENT)
778 error_callback (data, "executable file is unrecognized ELF version", 0);
782 #if BACKTRACE_ELF_SIZE == 32
783 #define BACKTRACE_ELFCLASS ELFCLASS32
785 #define BACKTRACE_ELFCLASS ELFCLASS64
788 if (ehdr.e_ident[EI_CLASS] != BACKTRACE_ELFCLASS)
790 error_callback (data, "executable file is unexpected ELF class", 0);
794 if (ehdr.e_ident[EI_DATA] != ELFDATA2LSB
795 && ehdr.e_ident[EI_DATA] != ELFDATA2MSB)
797 error_callback (data, "executable file has unknown endianness", 0);
801 /* If the executable is ET_DYN, it is either a PIE, or we are running
802 directly a shared library with .interp. We need to wait for
803 dl_iterate_phdr in that case to determine the actual base_address. */
806 /* If we were given a base address and this isn't PIE, set addr to 0. */
807 if (base_address && (ehdr.e_type != ET_DYN))
809 else if (!base_address && (ehdr.e_type == ET_DYN))
813 shoff = ehdr.e_shoff;
814 shnum = ehdr.e_shnum;
815 shstrndx = ehdr.e_shstrndx;
817 if ((shnum == 0 || shstrndx == SHN_XINDEX)
820 struct backtrace_view shdr_view;
821 const b_elf_shdr *shdr;
823 if (!backtrace_get_view (state, descriptor, shoff, sizeof shdr,
824 error_callback, data, &shdr_view))
827 shdr = (const b_elf_shdr *) shdr_view.data;
830 shnum = shdr->sh_size;
832 if (shstrndx == SHN_XINDEX)
834 shstrndx = shdr->sh_link;
836 /* Versions of the GNU binutils between 2.12 and 2.18 did
837 not handle objects with more than SHN_LORESERVE sections
838 correctly. All large section indexes were offset by
839 0x100. There is more information at
840 http://sourceware.org/bugzilla/show_bug.cgi?id-5900 .
841 Fortunately these object files are easy to detect, as the
842 GNU binutils always put the section header string table
843 near the end of the list of sections. Thus if the
844 section header string table index is larger than the
845 number of sections, then we know we have to subtract
846 0x100 to get the real section index. */
847 if (shstrndx >= shnum && shstrndx >= SHN_LORESERVE + 0x100)
851 backtrace_release_view (state, &shdr_view, error_callback, data);
854 /* To translate PC to file/line when using DWARF, we need to find
855 the .debug_info and .debug_line sections. */
857 /* Read the section headers, skipping the first one. */
859 if (!backtrace_get_view (state, descriptor, shoff + sizeof (b_elf_shdr),
860 (shnum - 1) * sizeof (b_elf_shdr),
861 error_callback, data, &shdrs_view))
863 shdrs_view_valid = 1;
864 shdrs = (const b_elf_shdr *) shdrs_view.data;
866 /* Read the section names. */
868 shstrhdr = &shdrs[shstrndx - 1];
869 shstr_size = shstrhdr->sh_size;
870 shstr_off = shstrhdr->sh_offset;
872 if (!backtrace_get_view (state, descriptor, shstr_off, shstr_size,
873 error_callback, data, &names_view))
875 names_view_valid = 1;
876 names = (const char *) names_view.data;
881 memset (sections, 0, sizeof sections);
882 memset (uuid, 0, sizeof(uuid));
884 /* Look for the symbol table. */
885 for (i = 1; i < shnum; ++i)
887 const b_elf_shdr *shdr;
888 unsigned int sh_name;
892 shdr = &shdrs[i - 1];
894 if (shdr->sh_type == SHT_SYMTAB)
896 else if (shdr->sh_type == SHT_DYNSYM)
898 else if ((shdr->sh_type == SHT_NOTE) && (uuid_len == 0))
900 struct backtrace_view note_view;
902 if (backtrace_get_view (state, descriptor, shdr->sh_offset,
903 shdr->sh_size, error_callback, data,
906 uuid_len = elf_parse_gnu_buildid(note_view.data, shdr->sh_size, uuid);
907 backtrace_release_view (state, ¬e_view, error_callback, data);
911 sh_name = shdr->sh_name;
912 if (sh_name >= shstr_size)
914 error_callback (data, "ELF section name out of range", 0);
918 name = names + sh_name;
920 for (j = 0; j < (int) DEBUG_MAX; ++j)
922 if (strcmp (name, debug_section_names[j]) == 0)
924 sections[j].offset = shdr->sh_offset;
925 sections[j].size = shdr->sh_size;
931 if (uuid_to_match_len)
933 /* We're supposed to be looking for a gnu_debuglink file... */
935 /* Found debug file but uuids don't match... */
936 if ((uuid_to_match_len != uuid_len) || memcmp(uuid_to_match, uuid, uuid_len))
939 /* Found debug file, but no .debuginfo in it? */
940 if (!sections[DEBUG_INFO].size)
943 else if (uuid_len && !sections[DEBUG_INFO].size && sections[GNU_DEBUGLINK].size)
945 /* We're not searching for a gnu_debuglink file, we have no .debug_info section, we
946 have a uuid, and we have a gnu_debuglink filename - try looking for this debug file. */
947 struct backtrace_view gnu_debuglink_view;
949 if (backtrace_get_view (state, descriptor, sections[GNU_DEBUGLINK].offset,
950 sections[GNU_DEBUGLINK].size, error_callback, data,
951 &gnu_debuglink_view))
954 int debug_found_sym = 0;
955 int debug_found_dwarf = 0;
956 const char *gnu_debuglink_file = gnu_debuglink_view.data;
958 if (gnu_debuglink_file && gnu_debuglink_file[0])
960 added = elf_add_gnu_debuglink (state, filename, gnu_debuglink_file, base_address, error_callback,
961 data, fileline_fn, &debug_found_sym, &debug_found_dwarf,
965 backtrace_release_view (state, &gnu_debuglink_view, error_callback, data);
966 gnu_debuglink_file = NULL;
968 /* If we found symbols of any kind, head on out... */
969 if (added && (debug_found_sym || debug_found_dwarf))
971 *found_sym = debug_found_sym;
972 *found_dwarf = debug_found_dwarf;
978 if (symtab_shndx == 0)
979 symtab_shndx = dynsym_shndx;
980 if (symtab_shndx != 0)
982 const b_elf_shdr *symtab_shdr;
983 unsigned int strtab_shndx;
984 const b_elf_shdr *strtab_shdr;
985 struct elf_syminfo_data *sdata;
987 symtab_shdr = &shdrs[symtab_shndx - 1];
988 strtab_shndx = symtab_shdr->sh_link;
989 if (strtab_shndx >= shnum)
991 error_callback (data,
992 "ELF symbol table strtab link out of range", 0);
995 strtab_shdr = &shdrs[strtab_shndx - 1];
997 if (!backtrace_get_view (state, descriptor, symtab_shdr->sh_offset,
998 symtab_shdr->sh_size, error_callback, data,
1001 symtab_view_valid = 1;
1003 if (!backtrace_get_view (state, descriptor, strtab_shdr->sh_offset,
1004 strtab_shdr->sh_size, error_callback, data,
1007 strtab_view_valid = 1;
1009 sdata = ((struct elf_syminfo_data *)
1010 backtrace_alloc (state, sizeof *sdata, error_callback, data));
1014 if (!elf_initialize_syminfo (state, base_address,
1015 symtab_view.data, symtab_shdr->sh_size,
1016 strtab_view.data, strtab_shdr->sh_size,
1017 error_callback, data, sdata))
1019 backtrace_free (state, sdata, sizeof *sdata, error_callback, data);
1023 /* We no longer need the symbol table, but we hold on to the
1024 string table permanently. */
1025 backtrace_release_view (state, &symtab_view, error_callback, data);
1029 elf_add_syminfo_data (state, sdata);
1032 /* FIXME: Need to handle compressed debug sections. */
1034 backtrace_release_view (state, &shdrs_view, error_callback, data);
1035 shdrs_view_valid = 0;
1036 backtrace_release_view (state, &names_view, error_callback, data);
1037 names_view_valid = 0;
1039 /* Read all the debug sections in a single view, since they are
1040 probably adjacent in the file. We never release this view. */
1044 for (i = 0; i < (int) DEBUG_MAX; ++i)
1048 if (sections[i].size == 0)
1050 if (min_offset == 0 || sections[i].offset < min_offset)
1051 min_offset = sections[i].offset;
1052 end = sections[i].offset + sections[i].size;
1053 if (end > max_offset)
1056 if (min_offset == 0 || max_offset == 0)
1058 if (!backtrace_close (descriptor, error_callback, data))
1063 *fileline_fn = elf_nodebug;
1067 if (!backtrace_get_view (state, descriptor, min_offset,
1068 max_offset - min_offset,
1069 error_callback, data, &debug_view))
1071 debug_view_valid = 1;
1073 /* We've read all we need from the executable. */
1074 if (!backtrace_close (descriptor, error_callback, data))
1081 for (i = 0; i < (int) DEBUG_MAX; ++i)
1083 if (sections[i].size == 0)
1084 sections[i].data = NULL;
1086 sections[i].data = ((const unsigned char *) debug_view.data
1087 + (sections[i].offset - min_offset));
1090 if (!backtrace_dwarf_add (state, base_address,
1091 sections[DEBUG_INFO].data,
1092 sections[DEBUG_INFO].size,
1093 sections[DEBUG_LINE].data,
1094 sections[DEBUG_LINE].size,
1095 sections[DEBUG_ABBREV].data,
1096 sections[DEBUG_ABBREV].size,
1097 sections[DEBUG_RANGES].data,
1098 sections[DEBUG_RANGES].size,
1099 sections[DEBUG_STR].data,
1100 sections[DEBUG_STR].size,
1101 ehdr.e_ident[EI_DATA] == ELFDATA2MSB,
1102 error_callback, data, fileline_fn))
1105 state->debug_filename = backtrace_strdup(state, filename, error_callback, data);
1113 if (shdrs_view_valid)
1114 backtrace_release_view (state, &shdrs_view, error_callback, data);
1115 if (names_view_valid)
1116 backtrace_release_view (state, &names_view, error_callback, data);
1117 if (symtab_view_valid)
1118 backtrace_release_view (state, &symtab_view, error_callback, data);
1119 if (strtab_view_valid)
1120 backtrace_release_view (state, &strtab_view, error_callback, data);
1121 if (debug_view_valid)
1122 backtrace_release_view (state, &debug_view, error_callback, data);
1123 if (descriptor != -1)
1124 backtrace_close (descriptor, error_callback, data);
1128 /* Data passed to phdr_callback. */
1132 struct backtrace_state *state;
1133 backtrace_error_callback error_callback;
1135 fileline *fileline_fn;
1138 const char * exe_filename;
1141 /* Callback passed to dl_iterate_phdr. Load debug info from shared
1145 phdr_callback (struct dl_phdr_info *info, size_t size ATTRIBUTE_UNUSED,
1148 struct phdr_data *pd = (struct phdr_data *) pdata;
1149 fileline elf_fileline_fn;
1151 const char * filename = info->dlpi_name;
1153 /* There is not much we can do if we don't have the module name,
1154 unless executable is ET_DYN, where we expect the very first
1155 phdr_callback to be for the PIE. */
1156 if (filename == NULL || filename[0] == '\0')
1158 if (!pd->exe_filename)
1160 filename = pd->exe_filename;
1161 pd->exe_filename = NULL;
1164 if (elf_add (pd->state, filename, info->dlpi_addr, pd->error_callback,
1165 pd->data, &elf_fileline_fn, pd->found_sym, &found_dwarf, 0,
1170 *pd->found_dwarf = 1;
1171 *pd->fileline_fn = elf_fileline_fn;
1178 /* Initialize the backtrace data we need from an ELF executable. At
1179 the ELF level, all we need to do is find the debug info
1183 backtrace_initialize (struct backtrace_state *state, const char *filename,
1184 uintptr_t base_address, int is_exe,
1185 backtrace_error_callback error_callback,
1186 void *data, fileline *fileline_fn)
1191 fileline elf_fileline_fn;
1193 ret = elf_add (state, filename, base_address, error_callback, data, &elf_fileline_fn,
1194 &found_sym, &found_dwarf, is_exe, NULL, 0);
1200 struct phdr_data pd;
1203 pd.error_callback = error_callback;
1205 pd.fileline_fn = &elf_fileline_fn;
1206 pd.found_sym = &found_sym;
1207 pd.found_dwarf = &found_dwarf;
1208 pd.exe_filename = filename;
1210 dl_iterate_phdr (phdr_callback, (void *) &pd);
1213 if (!state->threaded)
1216 state->syminfo_fn = elf_syminfo;
1217 else if (state->syminfo_fn == NULL)
1218 state->syminfo_fn = elf_nosyms;
1223 backtrace_atomic_store_pointer (&state->syminfo_fn, elf_syminfo);
1225 __sync_bool_compare_and_swap (&state->syminfo_fn, NULL, elf_nosyms);
1228 if (!state->threaded)
1230 if (state->fileline_fn == NULL || state->fileline_fn == elf_nodebug)
1231 *fileline_fn = elf_fileline_fn;
1235 fileline current_fn;
1237 current_fn = backtrace_atomic_load_pointer (&state->fileline_fn);
1238 if (current_fn == NULL || current_fn == elf_nodebug)
1239 *fileline_fn = elf_fileline_fn;
1245 elf_get_uuid_error_callback(void *data, const char *msg, int errnum)
1247 // Do nothing here. elf_get_uuid() will return the error.
1251 elf_get_uuid (struct backtrace_state *state,
1252 const char *filename, uint8_t uuid[20], int *uuid_len)
1254 struct backtrace_view ehdr_view;
1258 struct backtrace_view shdrs_view;
1259 int shdrs_view_valid = 0;
1260 const b_elf_shdr *shdrs;
1264 struct backtrace_state *state_alloced = NULL;
1268 state_alloced = backtrace_create_state(filename, 0, elf_get_uuid_error_callback, NULL);
1269 state = state_alloced;
1272 int descriptor = backtrace_open (filename, elf_get_uuid_error_callback, data, NULL);
1276 if (!backtrace_get_view (state, descriptor, 0, sizeof ehdr, elf_get_uuid_error_callback,
1280 memcpy (&ehdr, ehdr_view.data, sizeof ehdr);
1282 backtrace_release_view (state, &ehdr_view, elf_get_uuid_error_callback, data);
1284 if (ehdr.e_ident[EI_MAG0] != ELFMAG0
1285 || ehdr.e_ident[EI_MAG1] != ELFMAG1
1286 || ehdr.e_ident[EI_MAG2] != ELFMAG2
1287 || ehdr.e_ident[EI_MAG3] != ELFMAG3)
1289 // elf_get_uuid_error_callback (data, "executable file is not ELF", 0);
1292 if (ehdr.e_ident[EI_VERSION] != EV_CURRENT)
1294 // elf_get_uuid_error_callback (data, "executable file is unrecognized ELF version", 0);
1298 #if BACKTRACE_ELF_SIZE == 32
1299 #define BACKTRACE_ELFCLASS ELFCLASS32
1301 #define BACKTRACE_ELFCLASS ELFCLASS64
1304 if (ehdr.e_ident[EI_CLASS] != BACKTRACE_ELFCLASS)
1306 // elf_get_uuid_error_callback (data, "executable file is unexpected ELF class", 0);
1310 if (ehdr.e_ident[EI_DATA] != ELFDATA2LSB
1311 && ehdr.e_ident[EI_DATA] != ELFDATA2MSB)
1313 // elf_get_uuid_error_callback (data, "executable file has unknown endianness", 0);
1317 shoff = ehdr.e_shoff;
1318 shnum = ehdr.e_shnum;
1320 if ((shnum == 0) && (shoff != 0))
1322 struct backtrace_view shdr_view;
1323 const b_elf_shdr *shdr;
1325 if (!backtrace_get_view (state, descriptor, shoff, sizeof shdr,
1326 elf_get_uuid_error_callback, data, &shdr_view))
1329 shdr = (const b_elf_shdr *) shdr_view.data;
1332 shnum = shdr->sh_size;
1334 backtrace_release_view (state, &shdr_view, elf_get_uuid_error_callback, data);
1337 if (!backtrace_get_view (state, descriptor, shoff + sizeof (b_elf_shdr),
1338 (shnum - 1) * sizeof (b_elf_shdr),
1339 elf_get_uuid_error_callback, data, &shdrs_view))
1341 shdrs_view_valid = 1;
1342 shdrs = (const b_elf_shdr *) shdrs_view.data;
1344 for (i = 1; i < shnum; ++i)
1346 const b_elf_shdr *shdr = &shdrs[i - 1];
1348 if (shdr->sh_type == SHT_NOTE)
1350 struct backtrace_view note_view;
1352 if (backtrace_get_view (state, descriptor, shdr->sh_offset,
1353 shdr->sh_size, elf_get_uuid_error_callback, data,
1356 *uuid_len = elf_parse_gnu_buildid(note_view.data, shdr->sh_size, uuid);
1357 backtrace_release_view (state, ¬e_view, elf_get_uuid_error_callback, data);
1368 if (shdrs_view_valid)
1369 backtrace_release_view (state, &shdrs_view, elf_get_uuid_error_callback, data);
1370 if (descriptor != -1)
1371 backtrace_close (descriptor, elf_get_uuid_error_callback, data);
1373 backtrace_free(state_alloced, state_alloced, sizeof(*state_alloced), elf_get_uuid_error_callback, NULL);