Daily bump.
[official-gcc.git] / libbacktrace / elf.c
blob3747c03079c59998faa3adc5409a37b7ca7e7805
1 /* elf.c -- Get debug data from an ELF file for backtraces.
2 Copyright (C) 2012-2013 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
7 met:
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
15 distribution.
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. */
33 #include "config.h"
35 #include <stdlib.h>
36 #include <string.h>
37 #include <sys/types.h>
39 #ifdef HAVE_DL_ITERATE_PHDR
40 #include <link.h>
41 #endif
43 #include "backtrace.h"
44 #include "internal.h"
46 #ifndef HAVE_DL_ITERATE_PHDR
48 /* Dummy version of dl_iterate_phdr for systems that don't have it. */
50 #define dl_phdr_info x_dl_phdr_info
51 #define dl_iterate_phdr x_dl_iterate_phdr
53 struct dl_phdr_info
55 uintptr_t dlpi_addr;
56 const char *dlpi_name;
59 static int
60 dl_iterate_phdr (int (*callback) (struct dl_phdr_info *,
61 size_t, void *) ATTRIBUTE_UNUSED,
62 void *data ATTRIBUTE_UNUSED)
64 return 0;
67 #endif /* ! defined (HAVE_DL_ITERATE_PHDR) */
69 /* The configure script must tell us whether we are 32-bit or 64-bit
70 ELF. We could make this code test and support either possibility,
71 but there is no point. This code only works for the currently
72 running executable, which means that we know the ELF mode at
73 configure mode. */
75 #if BACKTRACE_ELF_SIZE != 32 && BACKTRACE_ELF_SIZE != 64
76 #error "Unknown BACKTRACE_ELF_SIZE"
77 #endif
79 /* <link.h> might #include <elf.h> which might define our constants
80 with slightly different values. Undefine them to be safe. */
82 #undef EI_NIDENT
83 #undef EI_MAG0
84 #undef EI_MAG1
85 #undef EI_MAG2
86 #undef EI_MAG3
87 #undef EI_CLASS
88 #undef EI_DATA
89 #undef EI_VERSION
90 #undef ELF_MAG0
91 #undef ELF_MAG1
92 #undef ELF_MAG2
93 #undef ELF_MAG3
94 #undef ELFCLASS32
95 #undef ELFCLASS64
96 #undef ELFDATA2LSB
97 #undef ELFDATA2MSB
98 #undef EV_CURRENT
99 #undef SHN_LORESERVE
100 #undef SHN_XINDEX
101 #undef SHN_UNDEF
102 #undef SHT_SYMTAB
103 #undef SHT_STRTAB
104 #undef SHT_DYNSYM
105 #undef STT_OBJECT
106 #undef STT_FUNC
108 /* Basic types. */
110 typedef uint16_t b_elf_half; /* Elf_Half. */
111 typedef uint32_t b_elf_word; /* Elf_Word. */
112 typedef int32_t b_elf_sword; /* Elf_Sword. */
114 #if BACKTRACE_ELF_SIZE == 32
116 typedef uint32_t b_elf_addr; /* Elf_Addr. */
117 typedef uint32_t b_elf_off; /* Elf_Off. */
119 typedef uint32_t b_elf_wxword; /* 32-bit Elf_Word, 64-bit ELF_Xword. */
121 #else
123 typedef uint64_t b_elf_addr; /* Elf_Addr. */
124 typedef uint64_t b_elf_off; /* Elf_Off. */
125 typedef uint64_t b_elf_xword; /* Elf_Xword. */
126 typedef int64_t b_elf_sxword; /* Elf_Sxword. */
128 typedef uint64_t b_elf_wxword; /* 32-bit Elf_Word, 64-bit ELF_Xword. */
130 #endif
132 /* Data structures and associated constants. */
134 #define EI_NIDENT 16
136 typedef struct {
137 unsigned char e_ident[EI_NIDENT]; /* ELF "magic number" */
138 b_elf_half e_type; /* Identifies object file type */
139 b_elf_half e_machine; /* Specifies required architecture */
140 b_elf_word e_version; /* Identifies object file version */
141 b_elf_addr e_entry; /* Entry point virtual address */
142 b_elf_off e_phoff; /* Program header table file offset */
143 b_elf_off e_shoff; /* Section header table file offset */
144 b_elf_word e_flags; /* Processor-specific flags */
145 b_elf_half e_ehsize; /* ELF header size in bytes */
146 b_elf_half e_phentsize; /* Program header table entry size */
147 b_elf_half e_phnum; /* Program header table entry count */
148 b_elf_half e_shentsize; /* Section header table entry size */
149 b_elf_half e_shnum; /* Section header table entry count */
150 b_elf_half e_shstrndx; /* Section header string table index */
151 } b_elf_ehdr; /* Elf_Ehdr. */
153 #define EI_MAG0 0
154 #define EI_MAG1 1
155 #define EI_MAG2 2
156 #define EI_MAG3 3
157 #define EI_CLASS 4
158 #define EI_DATA 5
159 #define EI_VERSION 6
161 #define ELFMAG0 0x7f
162 #define ELFMAG1 'E'
163 #define ELFMAG2 'L'
164 #define ELFMAG3 'F'
166 #define ELFCLASS32 1
167 #define ELFCLASS64 2
169 #define ELFDATA2LSB 1
170 #define ELFDATA2MSB 2
172 #define EV_CURRENT 1
174 typedef struct {
175 b_elf_word sh_name; /* Section name, index in string tbl */
176 b_elf_word sh_type; /* Type of section */
177 b_elf_wxword sh_flags; /* Miscellaneous section attributes */
178 b_elf_addr sh_addr; /* Section virtual addr at execution */
179 b_elf_off sh_offset; /* Section file offset */
180 b_elf_wxword sh_size; /* Size of section in bytes */
181 b_elf_word sh_link; /* Index of another section */
182 b_elf_word sh_info; /* Additional section information */
183 b_elf_wxword sh_addralign; /* Section alignment */
184 b_elf_wxword sh_entsize; /* Entry size if section holds table */
185 } b_elf_shdr; /* Elf_Shdr. */
187 #define SHN_UNDEF 0x0000 /* Undefined section */
188 #define SHN_LORESERVE 0xFF00 /* Begin range of reserved indices */
189 #define SHN_XINDEX 0xFFFF /* Section index is held elsewhere */
191 #define SHT_SYMTAB 2
192 #define SHT_STRTAB 3
193 #define SHT_DYNSYM 11
195 #if BACKTRACE_ELF_SIZE == 32
197 typedef struct
199 b_elf_word st_name; /* Symbol name, index in string tbl */
200 b_elf_addr st_value; /* Symbol value */
201 b_elf_word st_size; /* Symbol size */
202 unsigned char st_info; /* Symbol binding and type */
203 unsigned char st_other; /* Visibility and other data */
204 b_elf_half st_shndx; /* Symbol section index */
205 } b_elf_sym; /* Elf_Sym. */
207 #else /* BACKTRACE_ELF_SIZE != 32 */
209 typedef struct
211 b_elf_word st_name; /* Symbol name, index in string tbl */
212 unsigned char st_info; /* Symbol binding and type */
213 unsigned char st_other; /* Visibility and other data */
214 b_elf_half st_shndx; /* Symbol section index */
215 b_elf_addr st_value; /* Symbol value */
216 b_elf_xword st_size; /* Symbol size */
217 } b_elf_sym; /* Elf_Sym. */
219 #endif /* BACKTRACE_ELF_SIZE != 32 */
221 #define STT_OBJECT 1
222 #define STT_FUNC 2
224 /* An index of ELF sections we care about. */
226 enum debug_section
228 DEBUG_INFO,
229 DEBUG_LINE,
230 DEBUG_ABBREV,
231 DEBUG_RANGES,
232 DEBUG_STR,
233 DEBUG_MAX
236 /* Names of sections, indexed by enum elf_section. */
238 static const char * const debug_section_names[DEBUG_MAX] =
240 ".debug_info",
241 ".debug_line",
242 ".debug_abbrev",
243 ".debug_ranges",
244 ".debug_str"
247 /* Information we gather for the sections we care about. */
249 struct debug_section_info
251 /* Section file offset. */
252 off_t offset;
253 /* Section size. */
254 size_t size;
255 /* Section contents, after read from file. */
256 const unsigned char *data;
259 /* Information we keep for an ELF symbol. */
261 struct elf_symbol
263 /* The name of the symbol. */
264 const char *name;
265 /* The address of the symbol. */
266 uintptr_t address;
267 /* The size of the symbol. */
268 size_t size;
271 /* Information to pass to elf_syminfo. */
273 struct elf_syminfo_data
275 /* Symbols for the next module. */
276 struct elf_syminfo_data *next;
277 /* The ELF symbols, sorted by address. */
278 struct elf_symbol *symbols;
279 /* The number of symbols. */
280 size_t count;
283 /* A dummy callback function used when we can't find any debug info. */
285 static int
286 elf_nodebug (struct backtrace_state *state ATTRIBUTE_UNUSED,
287 uintptr_t pc ATTRIBUTE_UNUSED,
288 backtrace_full_callback callback ATTRIBUTE_UNUSED,
289 backtrace_error_callback error_callback, void *data)
291 error_callback (data, "no debug info in ELF executable", -1);
292 return 0;
295 /* A dummy callback function used when we can't find a symbol
296 table. */
298 static void
299 elf_nosyms (struct backtrace_state *state ATTRIBUTE_UNUSED,
300 uintptr_t addr ATTRIBUTE_UNUSED,
301 backtrace_syminfo_callback callback ATTRIBUTE_UNUSED,
302 backtrace_error_callback error_callback, void *data)
304 error_callback (data, "no symbol table in ELF executable", -1);
307 /* Compare struct elf_symbol for qsort. */
309 static int
310 elf_symbol_compare (const void *v1, const void *v2)
312 const struct elf_symbol *e1 = (const struct elf_symbol *) v1;
313 const struct elf_symbol *e2 = (const struct elf_symbol *) v2;
315 if (e1->address < e2->address)
316 return -1;
317 else if (e1->address > e2->address)
318 return 1;
319 else
320 return 0;
323 /* Compare an ADDR against an elf_symbol for bsearch. We allocate one
324 extra entry in the array so that this can look safely at the next
325 entry. */
327 static int
328 elf_symbol_search (const void *vkey, const void *ventry)
330 const uintptr_t *key = (const uintptr_t *) vkey;
331 const struct elf_symbol *entry = (const struct elf_symbol *) ventry;
332 uintptr_t addr;
334 addr = *key;
335 if (addr < entry->address)
336 return -1;
337 else if (addr >= entry->address + entry->size)
338 return 1;
339 else
340 return 0;
343 /* Initialize the symbol table info for elf_syminfo. */
345 static int
346 elf_initialize_syminfo (struct backtrace_state *state,
347 uintptr_t base_address,
348 const unsigned char *symtab_data, size_t symtab_size,
349 const unsigned char *strtab, size_t strtab_size,
350 backtrace_error_callback error_callback,
351 void *data, struct elf_syminfo_data *sdata)
353 size_t sym_count;
354 const b_elf_sym *sym;
355 size_t elf_symbol_count;
356 size_t elf_symbol_size;
357 struct elf_symbol *elf_symbols;
358 size_t i;
359 unsigned int j;
361 sym_count = symtab_size / sizeof (b_elf_sym);
363 /* We only care about function symbols. Count them. */
364 sym = (const b_elf_sym *) symtab_data;
365 elf_symbol_count = 0;
366 for (i = 0; i < sym_count; ++i, ++sym)
368 int info;
370 info = sym->st_info & 0xf;
371 if ((info == STT_FUNC || info == STT_OBJECT)
372 && sym->st_shndx != SHN_UNDEF)
373 ++elf_symbol_count;
376 elf_symbol_size = elf_symbol_count * sizeof (struct elf_symbol);
377 elf_symbols = ((struct elf_symbol *)
378 backtrace_alloc (state, elf_symbol_size, error_callback,
379 data));
380 if (elf_symbols == NULL)
381 return 0;
383 sym = (const b_elf_sym *) symtab_data;
384 j = 0;
385 for (i = 0; i < sym_count; ++i, ++sym)
387 int info;
389 info = sym->st_info & 0xf;
390 if (info != STT_FUNC && info != STT_OBJECT)
391 continue;
392 if (sym->st_shndx == SHN_UNDEF)
393 continue;
394 if (sym->st_name >= strtab_size)
396 error_callback (data, "symbol string index out of range", 0);
397 backtrace_free (state, elf_symbols, elf_symbol_size, error_callback,
398 data);
399 return 0;
401 elf_symbols[j].name = (const char *) strtab + sym->st_name;
402 elf_symbols[j].address = sym->st_value + base_address;
403 elf_symbols[j].size = sym->st_size;
404 ++j;
407 qsort (elf_symbols, elf_symbol_count, sizeof (struct elf_symbol),
408 elf_symbol_compare);
410 sdata->next = NULL;
411 sdata->symbols = elf_symbols;
412 sdata->count = elf_symbol_count;
414 return 1;
417 /* Add EDATA to the list in STATE. */
419 static void
420 elf_add_syminfo_data (struct backtrace_state *state,
421 struct elf_syminfo_data *edata)
423 if (!state->threaded)
425 struct elf_syminfo_data **pp;
427 for (pp = (struct elf_syminfo_data **) (void *) &state->syminfo_data;
428 *pp != NULL;
429 pp = &(*pp)->next)
431 *pp = edata;
433 else
435 while (1)
437 struct elf_syminfo_data **pp;
439 pp = (struct elf_syminfo_data **) (void *) &state->syminfo_data;
441 while (1)
443 struct elf_syminfo_data *p;
445 p = backtrace_atomic_load_pointer (pp);
447 if (p == NULL)
448 break;
450 pp = &p->next;
453 if (__sync_bool_compare_and_swap (pp, NULL, edata))
454 break;
459 /* Return the symbol name and value for an ADDR. */
461 static void
462 elf_syminfo (struct backtrace_state *state, uintptr_t addr,
463 backtrace_syminfo_callback callback,
464 backtrace_error_callback error_callback ATTRIBUTE_UNUSED,
465 void *data)
467 struct elf_syminfo_data *edata;
468 struct elf_symbol *sym = NULL;
470 if (!state->threaded)
472 for (edata = (struct elf_syminfo_data *) state->syminfo_data;
473 edata != NULL;
474 edata = edata->next)
476 sym = ((struct elf_symbol *)
477 bsearch (&addr, edata->symbols, edata->count,
478 sizeof (struct elf_symbol), elf_symbol_search));
479 if (sym != NULL)
480 break;
483 else
485 struct elf_syminfo_data **pp;
487 pp = (struct elf_syminfo_data **) (void *) &state->syminfo_data;
488 while (1)
490 edata = backtrace_atomic_load_pointer (pp);
491 if (edata == NULL)
492 break;
494 sym = ((struct elf_symbol *)
495 bsearch (&addr, edata->symbols, edata->count,
496 sizeof (struct elf_symbol), elf_symbol_search));
497 if (sym != NULL)
498 break;
500 pp = &edata->next;
504 if (sym == NULL)
505 callback (data, addr, NULL, 0, 0);
506 else
507 callback (data, addr, sym->name, sym->address, sym->size);
510 /* Add the backtrace data for one ELF file. */
512 static int
513 elf_add (struct backtrace_state *state, int descriptor, uintptr_t base_address,
514 backtrace_error_callback error_callback, void *data,
515 fileline *fileline_fn, int *found_sym, int *found_dwarf)
517 struct backtrace_view ehdr_view;
518 b_elf_ehdr ehdr;
519 off_t shoff;
520 unsigned int shnum;
521 unsigned int shstrndx;
522 struct backtrace_view shdrs_view;
523 int shdrs_view_valid;
524 const b_elf_shdr *shdrs;
525 const b_elf_shdr *shstrhdr;
526 size_t shstr_size;
527 off_t shstr_off;
528 struct backtrace_view names_view;
529 int names_view_valid;
530 const char *names;
531 unsigned int symtab_shndx;
532 unsigned int dynsym_shndx;
533 unsigned int i;
534 struct debug_section_info sections[DEBUG_MAX];
535 struct backtrace_view symtab_view;
536 int symtab_view_valid;
537 struct backtrace_view strtab_view;
538 int strtab_view_valid;
539 off_t min_offset;
540 off_t max_offset;
541 struct backtrace_view debug_view;
542 int debug_view_valid;
544 *found_sym = 0;
545 *found_dwarf = 0;
547 shdrs_view_valid = 0;
548 names_view_valid = 0;
549 symtab_view_valid = 0;
550 strtab_view_valid = 0;
551 debug_view_valid = 0;
553 if (!backtrace_get_view (state, descriptor, 0, sizeof ehdr, error_callback,
554 data, &ehdr_view))
555 goto fail;
557 memcpy (&ehdr, ehdr_view.data, sizeof ehdr);
559 backtrace_release_view (state, &ehdr_view, error_callback, data);
561 if (ehdr.e_ident[EI_MAG0] != ELFMAG0
562 || ehdr.e_ident[EI_MAG1] != ELFMAG1
563 || ehdr.e_ident[EI_MAG2] != ELFMAG2
564 || ehdr.e_ident[EI_MAG3] != ELFMAG3)
566 error_callback (data, "executable file is not ELF", 0);
567 goto fail;
569 if (ehdr.e_ident[EI_VERSION] != EV_CURRENT)
571 error_callback (data, "executable file is unrecognized ELF version", 0);
572 goto fail;
575 #if BACKTRACE_ELF_SIZE == 32
576 #define BACKTRACE_ELFCLASS ELFCLASS32
577 #else
578 #define BACKTRACE_ELFCLASS ELFCLASS64
579 #endif
581 if (ehdr.e_ident[EI_CLASS] != BACKTRACE_ELFCLASS)
583 error_callback (data, "executable file is unexpected ELF class", 0);
584 goto fail;
587 if (ehdr.e_ident[EI_DATA] != ELFDATA2LSB
588 && ehdr.e_ident[EI_DATA] != ELFDATA2MSB)
590 error_callback (data, "executable file has unknown endianness", 0);
591 goto fail;
594 shoff = ehdr.e_shoff;
595 shnum = ehdr.e_shnum;
596 shstrndx = ehdr.e_shstrndx;
598 if ((shnum == 0 || shstrndx == SHN_XINDEX)
599 && shoff != 0)
601 struct backtrace_view shdr_view;
602 const b_elf_shdr *shdr;
604 if (!backtrace_get_view (state, descriptor, shoff, sizeof shdr,
605 error_callback, data, &shdr_view))
606 goto fail;
608 shdr = (const b_elf_shdr *) shdr_view.data;
610 if (shnum == 0)
611 shnum = shdr->sh_size;
613 if (shstrndx == SHN_XINDEX)
615 shstrndx = shdr->sh_link;
617 /* Versions of the GNU binutils between 2.12 and 2.18 did
618 not handle objects with more than SHN_LORESERVE sections
619 correctly. All large section indexes were offset by
620 0x100. There is more information at
621 http://sourceware.org/bugzilla/show_bug.cgi?id-5900 .
622 Fortunately these object files are easy to detect, as the
623 GNU binutils always put the section header string table
624 near the end of the list of sections. Thus if the
625 section header string table index is larger than the
626 number of sections, then we know we have to subtract
627 0x100 to get the real section index. */
628 if (shstrndx >= shnum && shstrndx >= SHN_LORESERVE + 0x100)
629 shstrndx -= 0x100;
632 backtrace_release_view (state, &shdr_view, error_callback, data);
635 /* To translate PC to file/line when using DWARF, we need to find
636 the .debug_info and .debug_line sections. */
638 /* Read the section headers, skipping the first one. */
640 if (!backtrace_get_view (state, descriptor, shoff + sizeof (b_elf_shdr),
641 (shnum - 1) * sizeof (b_elf_shdr),
642 error_callback, data, &shdrs_view))
643 goto fail;
644 shdrs_view_valid = 1;
645 shdrs = (const b_elf_shdr *) shdrs_view.data;
647 /* Read the section names. */
649 shstrhdr = &shdrs[shstrndx - 1];
650 shstr_size = shstrhdr->sh_size;
651 shstr_off = shstrhdr->sh_offset;
653 if (!backtrace_get_view (state, descriptor, shstr_off, shstr_size,
654 error_callback, data, &names_view))
655 goto fail;
656 names_view_valid = 1;
657 names = (const char *) names_view.data;
659 symtab_shndx = 0;
660 dynsym_shndx = 0;
662 memset (sections, 0, sizeof sections);
664 /* Look for the symbol table. */
665 for (i = 1; i < shnum; ++i)
667 const b_elf_shdr *shdr;
668 unsigned int sh_name;
669 const char *name;
670 int j;
672 shdr = &shdrs[i - 1];
674 if (shdr->sh_type == SHT_SYMTAB)
675 symtab_shndx = i;
676 else if (shdr->sh_type == SHT_DYNSYM)
677 dynsym_shndx = i;
679 sh_name = shdr->sh_name;
680 if (sh_name >= shstr_size)
682 error_callback (data, "ELF section name out of range", 0);
683 goto fail;
686 name = names + sh_name;
688 for (j = 0; j < (int) DEBUG_MAX; ++j)
690 if (strcmp (name, debug_section_names[j]) == 0)
692 sections[j].offset = shdr->sh_offset;
693 sections[j].size = shdr->sh_size;
694 break;
699 if (symtab_shndx == 0)
700 symtab_shndx = dynsym_shndx;
701 if (symtab_shndx != 0)
703 const b_elf_shdr *symtab_shdr;
704 unsigned int strtab_shndx;
705 const b_elf_shdr *strtab_shdr;
706 struct elf_syminfo_data *sdata;
708 symtab_shdr = &shdrs[symtab_shndx - 1];
709 strtab_shndx = symtab_shdr->sh_link;
710 if (strtab_shndx >= shnum)
712 error_callback (data,
713 "ELF symbol table strtab link out of range", 0);
714 goto fail;
716 strtab_shdr = &shdrs[strtab_shndx - 1];
718 if (!backtrace_get_view (state, descriptor, symtab_shdr->sh_offset,
719 symtab_shdr->sh_size, error_callback, data,
720 &symtab_view))
721 goto fail;
722 symtab_view_valid = 1;
724 if (!backtrace_get_view (state, descriptor, strtab_shdr->sh_offset,
725 strtab_shdr->sh_size, error_callback, data,
726 &strtab_view))
727 goto fail;
728 strtab_view_valid = 1;
730 sdata = ((struct elf_syminfo_data *)
731 backtrace_alloc (state, sizeof *sdata, error_callback, data));
732 if (sdata == NULL)
733 goto fail;
735 if (!elf_initialize_syminfo (state, base_address,
736 symtab_view.data, symtab_shdr->sh_size,
737 strtab_view.data, strtab_shdr->sh_size,
738 error_callback, data, sdata))
740 backtrace_free (state, sdata, sizeof *sdata, error_callback, data);
741 goto fail;
744 /* We no longer need the symbol table, but we hold on to the
745 string table permanently. */
746 backtrace_release_view (state, &symtab_view, error_callback, data);
748 *found_sym = 1;
750 elf_add_syminfo_data (state, sdata);
753 /* FIXME: Need to handle compressed debug sections. */
755 backtrace_release_view (state, &shdrs_view, error_callback, data);
756 shdrs_view_valid = 0;
757 backtrace_release_view (state, &names_view, error_callback, data);
758 names_view_valid = 0;
760 /* Read all the debug sections in a single view, since they are
761 probably adjacent in the file. We never release this view. */
763 min_offset = 0;
764 max_offset = 0;
765 for (i = 0; i < (int) DEBUG_MAX; ++i)
767 off_t end;
769 if (sections[i].size == 0)
770 continue;
771 if (min_offset == 0 || sections[i].offset < min_offset)
772 min_offset = sections[i].offset;
773 end = sections[i].offset + sections[i].size;
774 if (end > max_offset)
775 max_offset = end;
777 if (min_offset == 0 || max_offset == 0)
779 if (!backtrace_close (descriptor, error_callback, data))
780 goto fail;
781 *fileline_fn = elf_nodebug;
782 return 1;
785 if (!backtrace_get_view (state, descriptor, min_offset,
786 max_offset - min_offset,
787 error_callback, data, &debug_view))
788 goto fail;
789 debug_view_valid = 1;
791 /* We've read all we need from the executable. */
792 if (!backtrace_close (descriptor, error_callback, data))
793 goto fail;
794 descriptor = -1;
796 for (i = 0; i < (int) DEBUG_MAX; ++i)
798 if (sections[i].size == 0)
799 sections[i].data = NULL;
800 else
801 sections[i].data = ((const unsigned char *) debug_view.data
802 + (sections[i].offset - min_offset));
805 if (!backtrace_dwarf_add (state, base_address,
806 sections[DEBUG_INFO].data,
807 sections[DEBUG_INFO].size,
808 sections[DEBUG_LINE].data,
809 sections[DEBUG_LINE].size,
810 sections[DEBUG_ABBREV].data,
811 sections[DEBUG_ABBREV].size,
812 sections[DEBUG_RANGES].data,
813 sections[DEBUG_RANGES].size,
814 sections[DEBUG_STR].data,
815 sections[DEBUG_STR].size,
816 ehdr.e_ident[EI_DATA] == ELFDATA2MSB,
817 error_callback, data, fileline_fn))
818 goto fail;
820 *found_dwarf = 1;
822 return 1;
824 fail:
825 if (shdrs_view_valid)
826 backtrace_release_view (state, &shdrs_view, error_callback, data);
827 if (names_view_valid)
828 backtrace_release_view (state, &names_view, error_callback, data);
829 if (symtab_view_valid)
830 backtrace_release_view (state, &symtab_view, error_callback, data);
831 if (strtab_view_valid)
832 backtrace_release_view (state, &strtab_view, error_callback, data);
833 if (debug_view_valid)
834 backtrace_release_view (state, &debug_view, error_callback, data);
835 if (descriptor != -1)
836 backtrace_close (descriptor, error_callback, data);
837 return 0;
840 /* Data passed to phdr_callback. */
842 struct phdr_data
844 struct backtrace_state *state;
845 backtrace_error_callback error_callback;
846 void *data;
847 fileline *fileline_fn;
848 int *found_sym;
849 int *found_dwarf;
852 /* Callback passed to dl_iterate_phdr. Load debug info from shared
853 libraries. */
855 static int
856 phdr_callback (struct dl_phdr_info *info, size_t size ATTRIBUTE_UNUSED,
857 void *pdata)
859 struct phdr_data *pd = (struct phdr_data *) pdata;
860 int descriptor;
861 int does_not_exist;
862 fileline elf_fileline_fn;
863 int found_dwarf;
865 /* There is not much we can do if we don't have the module name. */
866 if (info->dlpi_name == NULL || info->dlpi_name[0] == '\0')
867 return 0;
869 descriptor = backtrace_open (info->dlpi_name, pd->error_callback, pd->data,
870 &does_not_exist);
871 if (descriptor < 0)
872 return 0;
874 if (elf_add (pd->state, descriptor, info->dlpi_addr, pd->error_callback,
875 pd->data, &elf_fileline_fn, pd->found_sym, &found_dwarf))
877 if (found_dwarf)
879 *pd->found_dwarf = 1;
880 *pd->fileline_fn = elf_fileline_fn;
884 return 0;
887 /* Initialize the backtrace data we need from an ELF executable. At
888 the ELF level, all we need to do is find the debug info
889 sections. */
892 backtrace_initialize (struct backtrace_state *state, int descriptor,
893 backtrace_error_callback error_callback,
894 void *data, fileline *fileline_fn)
896 int found_sym;
897 int found_dwarf;
898 fileline elf_fileline_fn;
899 struct phdr_data pd;
901 if (!elf_add (state, descriptor, 0, error_callback, data, &elf_fileline_fn,
902 &found_sym, &found_dwarf))
903 return 0;
905 pd.state = state;
906 pd.error_callback = error_callback;
907 pd.data = data;
908 pd.fileline_fn = &elf_fileline_fn;
909 pd.found_sym = &found_sym;
910 pd.found_dwarf = &found_dwarf;
912 dl_iterate_phdr (phdr_callback, (void *) &pd);
914 if (!state->threaded)
916 if (found_sym)
917 state->syminfo_fn = elf_syminfo;
918 else if (state->syminfo_fn == NULL)
919 state->syminfo_fn = elf_nosyms;
921 else
923 if (found_sym)
924 backtrace_atomic_store_pointer (&state->syminfo_fn, elf_syminfo);
925 else
926 __sync_bool_compare_and_swap (&state->syminfo_fn, NULL, elf_nosyms);
929 if (!state->threaded)
931 if (state->fileline_fn == NULL || state->fileline_fn == elf_nodebug)
932 *fileline_fn = elf_fileline_fn;
934 else
936 fileline current_fn;
938 current_fn = backtrace_atomic_load_pointer (&state->fileline_fn);
939 if (current_fn == NULL || current_fn == elf_nodebug)
940 *fileline_fn = elf_fileline_fn;
943 return 1;