compiler: If type defined as type, forward hash/equal functions.
[gcc.git] / libbacktrace / elf.c
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.
4
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are
7 met:
8
9 (1) Redistributions of source code must retain the above copyright
10 notice, this list of conditions and the following disclaimer.
11
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.
16
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.
20
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. */
32
33 #include "config.h"
34
35 #include <stdlib.h>
36 #include <string.h>
37 #include <sys/types.h>
38
39 #ifdef HAVE_DL_ITERATE_PHDR
40 #include <link.h>
41 #endif
42
43 #include "backtrace.h"
44 #include "internal.h"
45
46 #ifndef HAVE_DL_ITERATE_PHDR
47
48 /* Dummy version of dl_iterate_phdr for systems that don't have it. */
49
50 #define dl_phdr_info x_dl_phdr_info
51 #define dl_iterate_phdr x_dl_iterate_phdr
52
53 struct dl_phdr_info
54 {
55 uintptr_t dlpi_addr;
56 const char *dlpi_name;
57 };
58
59 static int
60 dl_iterate_phdr (int (*callback) (struct dl_phdr_info *,
61 size_t, void *) ATTRIBUTE_UNUSED,
62 void *data ATTRIBUTE_UNUSED)
63 {
64 return 0;
65 }
66
67 #endif /* ! defined (HAVE_DL_ITERATE_PHDR) */
68
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. */
74
75 #if BACKTRACE_ELF_SIZE != 32 && BACKTRACE_ELF_SIZE != 64
76 #error "Unknown BACKTRACE_ELF_SIZE"
77 #endif
78
79 /* <link.h> might #include <elf.h> which might define our constants
80 with slightly different values. Undefine them to be safe. */
81
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
107
108 /* Basic types. */
109
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. */
113
114 #if BACKTRACE_ELF_SIZE == 32
115
116 typedef uint32_t b_elf_addr; /* Elf_Addr. */
117 typedef uint32_t b_elf_off; /* Elf_Off. */
118
119 typedef uint32_t b_elf_wxword; /* 32-bit Elf_Word, 64-bit ELF_Xword. */
120
121 #else
122
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. */
127
128 typedef uint64_t b_elf_wxword; /* 32-bit Elf_Word, 64-bit ELF_Xword. */
129
130 #endif
131
132 /* Data structures and associated constants. */
133
134 #define EI_NIDENT 16
135
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. */
152
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
160
161 #define ELFMAG0 0x7f
162 #define ELFMAG1 'E'
163 #define ELFMAG2 'L'
164 #define ELFMAG3 'F'
165
166 #define ELFCLASS32 1
167 #define ELFCLASS64 2
168
169 #define ELFDATA2LSB 1
170 #define ELFDATA2MSB 2
171
172 #define EV_CURRENT 1
173
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. */
186
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 */
190
191 #define SHT_SYMTAB 2
192 #define SHT_STRTAB 3
193 #define SHT_DYNSYM 11
194
195 #if BACKTRACE_ELF_SIZE == 32
196
197 typedef struct
198 {
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. */
206
207 #else /* BACKTRACE_ELF_SIZE != 32 */
208
209 typedef struct
210 {
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. */
218
219 #endif /* BACKTRACE_ELF_SIZE != 32 */
220
221 #define STT_OBJECT 1
222 #define STT_FUNC 2
223
224 /* An index of ELF sections we care about. */
225
226 enum debug_section
227 {
228 DEBUG_INFO,
229 DEBUG_LINE,
230 DEBUG_ABBREV,
231 DEBUG_RANGES,
232 DEBUG_STR,
233 DEBUG_MAX
234 };
235
236 /* Names of sections, indexed by enum elf_section. */
237
238 static const char * const debug_section_names[DEBUG_MAX] =
239 {
240 ".debug_info",
241 ".debug_line",
242 ".debug_abbrev",
243 ".debug_ranges",
244 ".debug_str"
245 };
246
247 /* Information we gather for the sections we care about. */
248
249 struct debug_section_info
250 {
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;
257 };
258
259 /* Information we keep for an ELF symbol. */
260
261 struct elf_symbol
262 {
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;
269 };
270
271 /* Information to pass to elf_syminfo. */
272
273 struct elf_syminfo_data
274 {
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;
281 };
282
283 /* A dummy callback function used when we can't find any debug info. */
284
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)
290 {
291 error_callback (data, "no debug info in ELF executable", -1);
292 return 0;
293 }
294
295 /* A dummy callback function used when we can't find a symbol
296 table. */
297
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)
303 {
304 error_callback (data, "no symbol table in ELF executable", -1);
305 }
306
307 /* Compare struct elf_symbol for qsort. */
308
309 static int
310 elf_symbol_compare (const void *v1, const void *v2)
311 {
312 const struct elf_symbol *e1 = (const struct elf_symbol *) v1;
313 const struct elf_symbol *e2 = (const struct elf_symbol *) v2;
314
315 if (e1->address < e2->address)
316 return -1;
317 else if (e1->address > e2->address)
318 return 1;
319 else
320 return 0;
321 }
322
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. */
326
327 static int
328 elf_symbol_search (const void *vkey, const void *ventry)
329 {
330 const uintptr_t *key = (const uintptr_t *) vkey;
331 const struct elf_symbol *entry = (const struct elf_symbol *) ventry;
332 uintptr_t addr;
333
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;
341 }
342
343 /* Initialize the symbol table info for elf_syminfo. */
344
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)
352 {
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;
360
361 sym_count = symtab_size / sizeof (b_elf_sym);
362
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)
367 {
368 int info;
369
370 info = sym->st_info & 0xf;
371 if ((info == STT_FUNC || info == STT_OBJECT)
372 && sym->st_shndx != SHN_UNDEF)
373 ++elf_symbol_count;
374 }
375
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;
382
383 sym = (const b_elf_sym *) symtab_data;
384 j = 0;
385 for (i = 0; i < sym_count; ++i, ++sym)
386 {
387 int info;
388
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)
395 {
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;
400 }
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;
405 }
406
407 qsort (elf_symbols, elf_symbol_count, sizeof (struct elf_symbol),
408 elf_symbol_compare);
409
410 sdata->next = NULL;
411 sdata->symbols = elf_symbols;
412 sdata->count = elf_symbol_count;
413
414 return 1;
415 }
416
417 /* Add EDATA to the list in STATE. */
418
419 static void
420 elf_add_syminfo_data (struct backtrace_state *state,
421 struct elf_syminfo_data *edata)
422 {
423 if (!state->threaded)
424 {
425 struct elf_syminfo_data **pp;
426
427 for (pp = (struct elf_syminfo_data **) (void *) &state->syminfo_data;
428 *pp != NULL;
429 pp = &(*pp)->next)
430 ;
431 *pp = edata;
432 }
433 else
434 {
435 while (1)
436 {
437 struct elf_syminfo_data **pp;
438
439 pp = (struct elf_syminfo_data **) (void *) &state->syminfo_data;
440
441 while (1)
442 {
443 struct elf_syminfo_data *p;
444
445 p = backtrace_atomic_load_pointer (pp);
446
447 if (p == NULL)
448 break;
449
450 pp = &p->next;
451 }
452
453 if (__sync_bool_compare_and_swap (pp, NULL, edata))
454 break;
455 }
456 }
457 }
458
459 /* Return the symbol name and value for an ADDR. */
460
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)
466 {
467 struct elf_syminfo_data *edata;
468 struct elf_symbol *sym = NULL;
469
470 if (!state->threaded)
471 {
472 for (edata = (struct elf_syminfo_data *) state->syminfo_data;
473 edata != NULL;
474 edata = edata->next)
475 {
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;
481 }
482 }
483 else
484 {
485 struct elf_syminfo_data **pp;
486
487 pp = (struct elf_syminfo_data **) (void *) &state->syminfo_data;
488 while (1)
489 {
490 edata = backtrace_atomic_load_pointer (pp);
491 if (edata == NULL)
492 break;
493
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;
499
500 pp = &edata->next;
501 }
502 }
503
504 if (sym == NULL)
505 callback (data, addr, NULL, 0, 0);
506 else
507 callback (data, addr, sym->name, sym->address, sym->size);
508 }
509
510 /* Add the backtrace data for one ELF file. */
511
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)
516 {
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;
543
544 *found_sym = 0;
545 *found_dwarf = 0;
546
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;
552
553 if (!backtrace_get_view (state, descriptor, 0, sizeof ehdr, error_callback,
554 data, &ehdr_view))
555 goto fail;
556
557 memcpy (&ehdr, ehdr_view.data, sizeof ehdr);
558
559 backtrace_release_view (state, &ehdr_view, error_callback, data);
560
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)
565 {
566 error_callback (data, "executable file is not ELF", 0);
567 goto fail;
568 }
569 if (ehdr.e_ident[EI_VERSION] != EV_CURRENT)
570 {
571 error_callback (data, "executable file is unrecognized ELF version", 0);
572 goto fail;
573 }
574
575 #if BACKTRACE_ELF_SIZE == 32
576 #define BACKTRACE_ELFCLASS ELFCLASS32
577 #else
578 #define BACKTRACE_ELFCLASS ELFCLASS64
579 #endif
580
581 if (ehdr.e_ident[EI_CLASS] != BACKTRACE_ELFCLASS)
582 {
583 error_callback (data, "executable file is unexpected ELF class", 0);
584 goto fail;
585 }
586
587 if (ehdr.e_ident[EI_DATA] != ELFDATA2LSB
588 && ehdr.e_ident[EI_DATA] != ELFDATA2MSB)
589 {
590 error_callback (data, "executable file has unknown endianness", 0);
591 goto fail;
592 }
593
594 shoff = ehdr.e_shoff;
595 shnum = ehdr.e_shnum;
596 shstrndx = ehdr.e_shstrndx;
597
598 if ((shnum == 0 || shstrndx == SHN_XINDEX)
599 && shoff != 0)
600 {
601 struct backtrace_view shdr_view;
602 const b_elf_shdr *shdr;
603
604 if (!backtrace_get_view (state, descriptor, shoff, sizeof shdr,
605 error_callback, data, &shdr_view))
606 goto fail;
607
608 shdr = (const b_elf_shdr *) shdr_view.data;
609
610 if (shnum == 0)
611 shnum = shdr->sh_size;
612
613 if (shstrndx == SHN_XINDEX)
614 {
615 shstrndx = shdr->sh_link;
616
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;
630 }
631
632 backtrace_release_view (state, &shdr_view, error_callback, data);
633 }
634
635 /* To translate PC to file/line when using DWARF, we need to find
636 the .debug_info and .debug_line sections. */
637
638 /* Read the section headers, skipping the first one. */
639
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;
646
647 /* Read the section names. */
648
649 shstrhdr = &shdrs[shstrndx - 1];
650 shstr_size = shstrhdr->sh_size;
651 shstr_off = shstrhdr->sh_offset;
652
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;
658
659 symtab_shndx = 0;
660 dynsym_shndx = 0;
661
662 memset (sections, 0, sizeof sections);
663
664 /* Look for the symbol table. */
665 for (i = 1; i < shnum; ++i)
666 {
667 const b_elf_shdr *shdr;
668 unsigned int sh_name;
669 const char *name;
670 int j;
671
672 shdr = &shdrs[i - 1];
673
674 if (shdr->sh_type == SHT_SYMTAB)
675 symtab_shndx = i;
676 else if (shdr->sh_type == SHT_DYNSYM)
677 dynsym_shndx = i;
678
679 sh_name = shdr->sh_name;
680 if (sh_name >= shstr_size)
681 {
682 error_callback (data, "ELF section name out of range", 0);
683 goto fail;
684 }
685
686 name = names + sh_name;
687
688 for (j = 0; j < (int) DEBUG_MAX; ++j)
689 {
690 if (strcmp (name, debug_section_names[j]) == 0)
691 {
692 sections[j].offset = shdr->sh_offset;
693 sections[j].size = shdr->sh_size;
694 break;
695 }
696 }
697 }
698
699 if (symtab_shndx == 0)
700 symtab_shndx = dynsym_shndx;
701 if (symtab_shndx != 0)
702 {
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;
707
708 symtab_shdr = &shdrs[symtab_shndx - 1];
709 strtab_shndx = symtab_shdr->sh_link;
710 if (strtab_shndx >= shnum)
711 {
712 error_callback (data,
713 "ELF symbol table strtab link out of range", 0);
714 goto fail;
715 }
716 strtab_shdr = &shdrs[strtab_shndx - 1];
717
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;
723
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;
729
730 sdata = ((struct elf_syminfo_data *)
731 backtrace_alloc (state, sizeof *sdata, error_callback, data));
732 if (sdata == NULL)
733 goto fail;
734
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))
739 {
740 backtrace_free (state, sdata, sizeof *sdata, error_callback, data);
741 goto fail;
742 }
743
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);
747
748 *found_sym = 1;
749
750 elf_add_syminfo_data (state, sdata);
751 }
752
753 /* FIXME: Need to handle compressed debug sections. */
754
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;
759
760 /* Read all the debug sections in a single view, since they are
761 probably adjacent in the file. We never release this view. */
762
763 min_offset = 0;
764 max_offset = 0;
765 for (i = 0; i < (int) DEBUG_MAX; ++i)
766 {
767 off_t end;
768
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;
776 }
777 if (min_offset == 0 || max_offset == 0)
778 {
779 if (!backtrace_close (descriptor, error_callback, data))
780 goto fail;
781 *fileline_fn = elf_nodebug;
782 return 1;
783 }
784
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;
790
791 /* We've read all we need from the executable. */
792 if (!backtrace_close (descriptor, error_callback, data))
793 goto fail;
794 descriptor = -1;
795
796 for (i = 0; i < (int) DEBUG_MAX; ++i)
797 {
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));
803 }
804
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;
819
820 *found_dwarf = 1;
821
822 return 1;
823
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;
838 }
839
840 /* Data passed to phdr_callback. */
841
842 struct phdr_data
843 {
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;
850 };
851
852 /* Callback passed to dl_iterate_phdr. Load debug info from shared
853 libraries. */
854
855 static int
856 phdr_callback (struct dl_phdr_info *info, size_t size ATTRIBUTE_UNUSED,
857 void *pdata)
858 {
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;
864
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;
868
869 descriptor = backtrace_open (info->dlpi_name, pd->error_callback, pd->data,
870 &does_not_exist);
871 if (descriptor < 0)
872 return 0;
873
874 if (elf_add (pd->state, descriptor, info->dlpi_addr, pd->error_callback,
875 pd->data, &elf_fileline_fn, pd->found_sym, &found_dwarf))
876 {
877 if (found_dwarf)
878 {
879 *pd->found_dwarf = 1;
880 *pd->fileline_fn = elf_fileline_fn;
881 }
882 }
883
884 return 0;
885 }
886
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. */
890
891 int
892 backtrace_initialize (struct backtrace_state *state, int descriptor,
893 backtrace_error_callback error_callback,
894 void *data, fileline *fileline_fn)
895 {
896 int found_sym;
897 int found_dwarf;
898 fileline elf_fileline_fn;
899 struct phdr_data pd;
900
901 if (!elf_add (state, descriptor, 0, error_callback, data, &elf_fileline_fn,
902 &found_sym, &found_dwarf))
903 return 0;
904
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;
911
912 dl_iterate_phdr (phdr_callback, (void *) &pd);
913
914 if (!state->threaded)
915 {
916 if (found_sym)
917 state->syminfo_fn = elf_syminfo;
918 else if (state->syminfo_fn == NULL)
919 state->syminfo_fn = elf_nosyms;
920 }
921 else
922 {
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);
927 }
928
929 if (!state->threaded)
930 {
931 if (state->fileline_fn == NULL || state->fileline_fn == elf_nodebug)
932 *fileline_fn = elf_fileline_fn;
933 }
934 else
935 {
936 fileline current_fn;
937
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;
941 }
942
943 return 1;
944 }