]> git.ipfire.org Git - thirdparty/gcc.git/blame - libbacktrace/elf.c
lex.c (search_line_fast): Correct for little endian.
[thirdparty/gcc.git] / libbacktrace / elf.c
CommitLineData
eff02e4f 1/* elf.c -- Get debug data from an ELF file for backtraces.
f8a7e1a4 2 Copyright (C) 2012-2013 Free Software Foundation, Inc.
eff02e4f
ILT
3 Written by Ian Lance Taylor, Google.
4
5Redistribution and use in source and binary forms, with or without
6modification, are permitted provided that the following conditions are
7met:
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
21THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
25INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
29STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
30IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31POSSIBILITY OF SUCH DAMAGE. */
32
33#include "config.h"
34
35#include <stdlib.h>
36#include <string.h>
37#include <sys/types.h>
38
e561a992
ILT
39#ifdef HAVE_DL_ITERATE_PHDR
40#include <link.h>
41#endif
42
eff02e4f
ILT
43#include "backtrace.h"
44#include "internal.h"
45
e561a992
ILT
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
53struct dl_phdr_info
54{
55 uintptr_t dlpi_addr;
56 const char *dlpi_name;
57};
58
59static int
60dl_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
eff02e4f
ILT
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
e561a992
ILT
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 SHT_SYMTAB
102#undef SHT_STRTAB
103#undef SHT_DYNSYM
cfa658e4 104#undef STT_OBJECT
e561a992
ILT
105#undef STT_FUNC
106
eff02e4f
ILT
107/* Basic types. */
108
40d15b5b
ILT
109typedef uint16_t b_elf_half; /* Elf_Half. */
110typedef uint32_t b_elf_word; /* Elf_Word. */
111typedef int32_t b_elf_sword; /* Elf_Sword. */
eff02e4f
ILT
112
113#if BACKTRACE_ELF_SIZE == 32
114
40d15b5b
ILT
115typedef uint32_t b_elf_addr; /* Elf_Addr. */
116typedef uint32_t b_elf_off; /* Elf_Off. */
eff02e4f 117
40d15b5b 118typedef uint32_t b_elf_wxword; /* 32-bit Elf_Word, 64-bit ELF_Xword. */
eff02e4f
ILT
119
120#else
121
40d15b5b
ILT
122typedef uint64_t b_elf_addr; /* Elf_Addr. */
123typedef uint64_t b_elf_off; /* Elf_Off. */
124typedef uint64_t b_elf_xword; /* Elf_Xword. */
125typedef int64_t b_elf_sxword; /* Elf_Sxword. */
eff02e4f 126
40d15b5b 127typedef uint64_t b_elf_wxword; /* 32-bit Elf_Word, 64-bit ELF_Xword. */
eff02e4f
ILT
128
129#endif
130
131/* Data structures and associated constants. */
132
133#define EI_NIDENT 16
134
135typedef struct {
136 unsigned char e_ident[EI_NIDENT]; /* ELF "magic number" */
40d15b5b
ILT
137 b_elf_half e_type; /* Identifies object file type */
138 b_elf_half e_machine; /* Specifies required architecture */
139 b_elf_word e_version; /* Identifies object file version */
140 b_elf_addr e_entry; /* Entry point virtual address */
141 b_elf_off e_phoff; /* Program header table file offset */
142 b_elf_off e_shoff; /* Section header table file offset */
143 b_elf_word e_flags; /* Processor-specific flags */
144 b_elf_half e_ehsize; /* ELF header size in bytes */
145 b_elf_half e_phentsize; /* Program header table entry size */
146 b_elf_half e_phnum; /* Program header table entry count */
147 b_elf_half e_shentsize; /* Section header table entry size */
148 b_elf_half e_shnum; /* Section header table entry count */
149 b_elf_half e_shstrndx; /* Section header string table index */
150} b_elf_ehdr; /* Elf_Ehdr. */
eff02e4f
ILT
151
152#define EI_MAG0 0
153#define EI_MAG1 1
154#define EI_MAG2 2
155#define EI_MAG3 3
156#define EI_CLASS 4
157#define EI_DATA 5
158#define EI_VERSION 6
159
160#define ELFMAG0 0x7f
161#define ELFMAG1 'E'
162#define ELFMAG2 'L'
163#define ELFMAG3 'F'
164
165#define ELFCLASS32 1
166#define ELFCLASS64 2
167
168#define ELFDATA2LSB 1
169#define ELFDATA2MSB 2
170
171#define EV_CURRENT 1
172
173typedef struct {
40d15b5b
ILT
174 b_elf_word sh_name; /* Section name, index in string tbl */
175 b_elf_word sh_type; /* Type of section */
176 b_elf_wxword sh_flags; /* Miscellaneous section attributes */
177 b_elf_addr sh_addr; /* Section virtual addr at execution */
178 b_elf_off sh_offset; /* Section file offset */
179 b_elf_wxword sh_size; /* Size of section in bytes */
180 b_elf_word sh_link; /* Index of another section */
181 b_elf_word sh_info; /* Additional section information */
182 b_elf_wxword sh_addralign; /* Section alignment */
183 b_elf_wxword sh_entsize; /* Entry size if section holds table */
184} b_elf_shdr; /* Elf_Shdr. */
eff02e4f
ILT
185
186#define SHN_LORESERVE 0xFF00 /* Begin range of reserved indices */
187#define SHN_XINDEX 0xFFFF /* Section index is held elsewhere */
188
189#define SHT_SYMTAB 2
190#define SHT_STRTAB 3
191#define SHT_DYNSYM 11
192
193#if BACKTRACE_ELF_SIZE == 32
194
195typedef struct
196{
40d15b5b
ILT
197 b_elf_word st_name; /* Symbol name, index in string tbl */
198 b_elf_addr st_value; /* Symbol value */
199 b_elf_word st_size; /* Symbol size */
eff02e4f
ILT
200 unsigned char st_info; /* Symbol binding and type */
201 unsigned char st_other; /* Visibility and other data */
40d15b5b
ILT
202 b_elf_half st_shndx; /* Symbol section index */
203} b_elf_sym; /* Elf_Sym. */
eff02e4f
ILT
204
205#else /* BACKTRACE_ELF_SIZE != 32 */
206
207typedef struct
208{
40d15b5b 209 b_elf_word st_name; /* Symbol name, index in string tbl */
eff02e4f
ILT
210 unsigned char st_info; /* Symbol binding and type */
211 unsigned char st_other; /* Visibility and other data */
40d15b5b
ILT
212 b_elf_half st_shndx; /* Symbol section index */
213 b_elf_addr st_value; /* Symbol value */
214 b_elf_xword st_size; /* Symbol size */
215} b_elf_sym; /* Elf_Sym. */
eff02e4f
ILT
216
217#endif /* BACKTRACE_ELF_SIZE != 32 */
218
cfa658e4 219#define STT_OBJECT 1
eff02e4f
ILT
220#define STT_FUNC 2
221
222/* An index of ELF sections we care about. */
223
224enum debug_section
225{
226 DEBUG_INFO,
227 DEBUG_LINE,
228 DEBUG_ABBREV,
229 DEBUG_RANGES,
230 DEBUG_STR,
231 DEBUG_MAX
232};
233
234/* Names of sections, indexed by enum elf_section. */
235
236static const char * const debug_section_names[DEBUG_MAX] =
237{
238 ".debug_info",
239 ".debug_line",
240 ".debug_abbrev",
241 ".debug_ranges",
242 ".debug_str"
243};
244
245/* Information we gather for the sections we care about. */
246
247struct debug_section_info
248{
249 /* Section file offset. */
250 off_t offset;
251 /* Section size. */
252 size_t size;
253 /* Section contents, after read from file. */
254 const unsigned char *data;
255};
256
257/* Information we keep for an ELF symbol. */
258
259struct elf_symbol
260{
261 /* The name of the symbol. */
262 const char *name;
263 /* The address of the symbol. */
264 uintptr_t address;
265 /* The size of the symbol. */
266 size_t size;
267};
268
269/* Information to pass to elf_syminfo. */
270
271struct elf_syminfo_data
272{
e561a992
ILT
273 /* Symbols for the next module. */
274 struct elf_syminfo_data *next;
eff02e4f
ILT
275 /* The ELF symbols, sorted by address. */
276 struct elf_symbol *symbols;
277 /* The number of symbols. */
278 size_t count;
279};
280
281/* A dummy callback function used when we can't find any debug info. */
282
283static int
284elf_nodebug (struct backtrace_state *state ATTRIBUTE_UNUSED,
285 uintptr_t pc ATTRIBUTE_UNUSED,
286 backtrace_full_callback callback ATTRIBUTE_UNUSED,
287 backtrace_error_callback error_callback, void *data)
288{
289 error_callback (data, "no debug info in ELF executable", -1);
290 return 0;
291}
292
293/* A dummy callback function used when we can't find a symbol
294 table. */
295
296static void
297elf_nosyms (struct backtrace_state *state ATTRIBUTE_UNUSED,
cfa658e4 298 uintptr_t addr ATTRIBUTE_UNUSED,
eff02e4f
ILT
299 backtrace_syminfo_callback callback ATTRIBUTE_UNUSED,
300 backtrace_error_callback error_callback, void *data)
301{
302 error_callback (data, "no symbol table in ELF executable", -1);
303}
304
305/* Compare struct elf_symbol for qsort. */
306
307static int
308elf_symbol_compare (const void *v1, const void *v2)
309{
310 const struct elf_symbol *e1 = (const struct elf_symbol *) v1;
311 const struct elf_symbol *e2 = (const struct elf_symbol *) v2;
312
313 if (e1->address < e2->address)
314 return -1;
315 else if (e1->address > e2->address)
316 return 1;
317 else
318 return 0;
319}
320
cfa658e4 321/* Compare an ADDR against an elf_symbol for bsearch. We allocate one
eff02e4f
ILT
322 extra entry in the array so that this can look safely at the next
323 entry. */
324
325static int
326elf_symbol_search (const void *vkey, const void *ventry)
327{
328 const uintptr_t *key = (const uintptr_t *) vkey;
329 const struct elf_symbol *entry = (const struct elf_symbol *) ventry;
cfa658e4 330 uintptr_t addr;
eff02e4f 331
cfa658e4
ILT
332 addr = *key;
333 if (addr < entry->address)
eff02e4f 334 return -1;
cfa658e4 335 else if (addr >= entry->address + entry->size)
eff02e4f
ILT
336 return 1;
337 else
338 return 0;
339}
340
341/* Initialize the symbol table info for elf_syminfo. */
342
343static int
344elf_initialize_syminfo (struct backtrace_state *state,
345 const unsigned char *symtab_data, size_t symtab_size,
346 const unsigned char *strtab, size_t strtab_size,
347 backtrace_error_callback error_callback,
348 void *data, struct elf_syminfo_data *sdata)
349{
350 size_t sym_count;
40d15b5b 351 const b_elf_sym *sym;
eff02e4f
ILT
352 size_t elf_symbol_count;
353 size_t elf_symbol_size;
354 struct elf_symbol *elf_symbols;
355 size_t i;
356 unsigned int j;
357
40d15b5b 358 sym_count = symtab_size / sizeof (b_elf_sym);
eff02e4f
ILT
359
360 /* We only care about function symbols. Count them. */
40d15b5b 361 sym = (const b_elf_sym *) symtab_data;
eff02e4f
ILT
362 elf_symbol_count = 0;
363 for (i = 0; i < sym_count; ++i, ++sym)
364 {
cfa658e4
ILT
365 int info;
366
367 info = sym->st_info & 0xf;
368 if (info == STT_FUNC || info == STT_OBJECT)
eff02e4f
ILT
369 ++elf_symbol_count;
370 }
371
372 elf_symbol_size = elf_symbol_count * sizeof (struct elf_symbol);
373 elf_symbols = ((struct elf_symbol *)
374 backtrace_alloc (state, elf_symbol_size, error_callback,
375 data));
376 if (elf_symbols == NULL)
377 return 0;
378
40d15b5b 379 sym = (const b_elf_sym *) symtab_data;
eff02e4f
ILT
380 j = 0;
381 for (i = 0; i < sym_count; ++i, ++sym)
382 {
cfa658e4
ILT
383 int info;
384
385 info = sym->st_info & 0xf;
386 if (info != STT_FUNC && info != STT_OBJECT)
eff02e4f
ILT
387 continue;
388 if (sym->st_name >= strtab_size)
389 {
390 error_callback (data, "symbol string index out of range", 0);
391 backtrace_free (state, elf_symbols, elf_symbol_size, error_callback,
392 data);
393 return 0;
394 }
395 elf_symbols[j].name = (const char *) strtab + sym->st_name;
396 elf_symbols[j].address = sym->st_value;
397 elf_symbols[j].size = sym->st_size;
398 ++j;
399 }
400
401 qsort (elf_symbols, elf_symbol_count, sizeof (struct elf_symbol),
402 elf_symbol_compare);
403
e561a992 404 sdata->next = NULL;
eff02e4f
ILT
405 sdata->symbols = elf_symbols;
406 sdata->count = elf_symbol_count;
407
408 return 1;
409}
410
e561a992
ILT
411/* Add EDATA to the list in STATE. */
412
413static void
414elf_add_syminfo_data (struct backtrace_state *state,
415 struct elf_syminfo_data *edata)
416{
417 if (!state->threaded)
418 {
419 struct elf_syminfo_data **pp;
420
74f80620 421 for (pp = (struct elf_syminfo_data **) (void *) &state->syminfo_data;
e561a992
ILT
422 *pp != NULL;
423 pp = &(*pp)->next)
424 ;
425 *pp = edata;
426 }
427 else
428 {
429 while (1)
430 {
431 struct elf_syminfo_data **pp;
432
74f80620 433 pp = (struct elf_syminfo_data **) (void *) &state->syminfo_data;
e561a992
ILT
434
435 while (1)
436 {
437 struct elf_syminfo_data *p;
438
439 /* Atomic load. */
440 p = *pp;
441 while (!__sync_bool_compare_and_swap (pp, p, p))
442 p = *pp;
443
444 if (p == NULL)
445 break;
446
447 pp = &p->next;
448 }
449
450 if (__sync_bool_compare_and_swap (pp, NULL, edata))
451 break;
452 }
453 }
454}
455
cfa658e4 456/* Return the symbol name and value for an ADDR. */
eff02e4f
ILT
457
458static void
cfa658e4 459elf_syminfo (struct backtrace_state *state, uintptr_t addr,
eff02e4f
ILT
460 backtrace_syminfo_callback callback,
461 backtrace_error_callback error_callback ATTRIBUTE_UNUSED,
462 void *data)
463{
464 struct elf_syminfo_data *edata;
78625ce6
AM
465 struct elf_symbol *sym = NULL;
466
467 if (!state->threaded)
468 {
469 for (edata = (struct elf_syminfo_data *) state->syminfo_data;
470 edata != NULL;
471 edata = edata->next)
472 {
473 sym = ((struct elf_symbol *)
cfa658e4 474 bsearch (&addr, edata->symbols, edata->count,
78625ce6
AM
475 sizeof (struct elf_symbol), elf_symbol_search));
476 if (sym != NULL)
477 break;
478 }
479 }
480 else
481 {
482 struct elf_syminfo_data **pp;
483
484 pp = (struct elf_syminfo_data **) (void *) &state->syminfo_data;
485 while (1)
486 {
487 edata = *pp;
488 /* Atomic load. */
489 while (!__sync_bool_compare_and_swap (pp, edata, edata))
490 edata = *pp;
491
492 if (edata == NULL)
493 break;
494
495 sym = ((struct elf_symbol *)
cfa658e4 496 bsearch (&addr, edata->symbols, edata->count,
78625ce6
AM
497 sizeof (struct elf_symbol), elf_symbol_search));
498 if (sym != NULL)
499 break;
500
501 pp = &edata->next;
502 }
503 }
eff02e4f 504
eff02e4f 505 if (sym == NULL)
cfa658e4 506 callback (data, addr, NULL, 0);
eff02e4f 507 else
cfa658e4 508 callback (data, addr, sym->name, sym->address);
eff02e4f
ILT
509}
510
e561a992 511/* Add the backtrace data for one ELF file. */
eff02e4f 512
e561a992
ILT
513static int
514elf_add (struct backtrace_state *state, int descriptor, uintptr_t base_address,
515 backtrace_error_callback error_callback, void *data,
516 fileline *fileline_fn, int *found_sym, int *found_dwarf)
eff02e4f
ILT
517{
518 struct backtrace_view ehdr_view;
40d15b5b 519 b_elf_ehdr ehdr;
eff02e4f
ILT
520 off_t shoff;
521 unsigned int shnum;
522 unsigned int shstrndx;
523 struct backtrace_view shdrs_view;
524 int shdrs_view_valid;
40d15b5b
ILT
525 const b_elf_shdr *shdrs;
526 const b_elf_shdr *shstrhdr;
eff02e4f
ILT
527 size_t shstr_size;
528 off_t shstr_off;
529 struct backtrace_view names_view;
530 int names_view_valid;
531 const char *names;
532 unsigned int symtab_shndx;
533 unsigned int dynsym_shndx;
534 unsigned int i;
535 struct debug_section_info sections[DEBUG_MAX];
536 struct backtrace_view symtab_view;
537 int symtab_view_valid;
538 struct backtrace_view strtab_view;
539 int strtab_view_valid;
540 off_t min_offset;
541 off_t max_offset;
542 struct backtrace_view debug_view;
543 int debug_view_valid;
544
e561a992
ILT
545 *found_sym = 0;
546 *found_dwarf = 0;
547
eff02e4f
ILT
548 shdrs_view_valid = 0;
549 names_view_valid = 0;
550 symtab_view_valid = 0;
551 strtab_view_valid = 0;
552 debug_view_valid = 0;
553
554 if (!backtrace_get_view (state, descriptor, 0, sizeof ehdr, error_callback,
555 data, &ehdr_view))
556 goto fail;
557
558 memcpy (&ehdr, ehdr_view.data, sizeof ehdr);
559
560 backtrace_release_view (state, &ehdr_view, error_callback, data);
561
562 if (ehdr.e_ident[EI_MAG0] != ELFMAG0
563 || ehdr.e_ident[EI_MAG1] != ELFMAG1
564 || ehdr.e_ident[EI_MAG2] != ELFMAG2
565 || ehdr.e_ident[EI_MAG3] != ELFMAG3)
566 {
567 error_callback (data, "executable file is not ELF", 0);
568 goto fail;
569 }
570 if (ehdr.e_ident[EI_VERSION] != EV_CURRENT)
571 {
572 error_callback (data, "executable file is unrecognized ELF version", 0);
573 goto fail;
574 }
575
576#if BACKTRACE_ELF_SIZE == 32
577#define BACKTRACE_ELFCLASS ELFCLASS32
578#else
579#define BACKTRACE_ELFCLASS ELFCLASS64
580#endif
581
582 if (ehdr.e_ident[EI_CLASS] != BACKTRACE_ELFCLASS)
583 {
584 error_callback (data, "executable file is unexpected ELF class", 0);
585 goto fail;
586 }
587
588 if (ehdr.e_ident[EI_DATA] != ELFDATA2LSB
589 && ehdr.e_ident[EI_DATA] != ELFDATA2MSB)
590 {
591 error_callback (data, "executable file has unknown endianness", 0);
592 goto fail;
593 }
594
595 shoff = ehdr.e_shoff;
596 shnum = ehdr.e_shnum;
597 shstrndx = ehdr.e_shstrndx;
598
599 if ((shnum == 0 || shstrndx == SHN_XINDEX)
600 && shoff != 0)
601 {
602 struct backtrace_view shdr_view;
40d15b5b 603 const b_elf_shdr *shdr;
eff02e4f
ILT
604
605 if (!backtrace_get_view (state, descriptor, shoff, sizeof shdr,
606 error_callback, data, &shdr_view))
607 goto fail;
608
40d15b5b 609 shdr = (const b_elf_shdr *) shdr_view.data;
eff02e4f
ILT
610
611 if (shnum == 0)
612 shnum = shdr->sh_size;
613
614 if (shstrndx == SHN_XINDEX)
615 {
616 shstrndx = shdr->sh_link;
617
618 /* Versions of the GNU binutils between 2.12 and 2.18 did
619 not handle objects with more than SHN_LORESERVE sections
620 correctly. All large section indexes were offset by
621 0x100. There is more information at
622 http://sourceware.org/bugzilla/show_bug.cgi?id-5900 .
623 Fortunately these object files are easy to detect, as the
624 GNU binutils always put the section header string table
625 near the end of the list of sections. Thus if the
626 section header string table index is larger than the
627 number of sections, then we know we have to subtract
628 0x100 to get the real section index. */
629 if (shstrndx >= shnum && shstrndx >= SHN_LORESERVE + 0x100)
630 shstrndx -= 0x100;
631 }
632
633 backtrace_release_view (state, &shdr_view, error_callback, data);
634 }
635
636 /* To translate PC to file/line when using DWARF, we need to find
637 the .debug_info and .debug_line sections. */
638
639 /* Read the section headers, skipping the first one. */
640
40d15b5b
ILT
641 if (!backtrace_get_view (state, descriptor, shoff + sizeof (b_elf_shdr),
642 (shnum - 1) * sizeof (b_elf_shdr),
eff02e4f
ILT
643 error_callback, data, &shdrs_view))
644 goto fail;
645 shdrs_view_valid = 1;
40d15b5b 646 shdrs = (const b_elf_shdr *) shdrs_view.data;
eff02e4f
ILT
647
648 /* Read the section names. */
649
650 shstrhdr = &shdrs[shstrndx - 1];
651 shstr_size = shstrhdr->sh_size;
652 shstr_off = shstrhdr->sh_offset;
653
654 if (!backtrace_get_view (state, descriptor, shstr_off, shstr_size,
655 error_callback, data, &names_view))
656 goto fail;
657 names_view_valid = 1;
658 names = (const char *) names_view.data;
659
660 symtab_shndx = 0;
661 dynsym_shndx = 0;
662
663 memset (sections, 0, sizeof sections);
e561a992
ILT
664
665 /* Look for the symbol table. */
eff02e4f
ILT
666 for (i = 1; i < shnum; ++i)
667 {
40d15b5b 668 const b_elf_shdr *shdr;
eff02e4f
ILT
669 unsigned int sh_name;
670 const char *name;
671 int j;
672
673 shdr = &shdrs[i - 1];
674
675 if (shdr->sh_type == SHT_SYMTAB)
676 symtab_shndx = i;
677 else if (shdr->sh_type == SHT_DYNSYM)
678 dynsym_shndx = i;
679
680 sh_name = shdr->sh_name;
681 if (sh_name >= shstr_size)
682 {
683 error_callback (data, "ELF section name out of range", 0);
684 goto fail;
685 }
686
687 name = names + sh_name;
688
689 for (j = 0; j < (int) DEBUG_MAX; ++j)
690 {
691 if (strcmp (name, debug_section_names[j]) == 0)
692 {
693 sections[j].offset = shdr->sh_offset;
694 sections[j].size = shdr->sh_size;
695 break;
696 }
697 }
698 }
699
700 if (symtab_shndx == 0)
701 symtab_shndx = dynsym_shndx;
e561a992 702 if (symtab_shndx != 0)
eff02e4f 703 {
40d15b5b 704 const b_elf_shdr *symtab_shdr;
eff02e4f 705 unsigned int strtab_shndx;
40d15b5b 706 const b_elf_shdr *strtab_shdr;
eff02e4f
ILT
707 struct elf_syminfo_data *sdata;
708
709 symtab_shdr = &shdrs[symtab_shndx - 1];
710 strtab_shndx = symtab_shdr->sh_link;
711 if (strtab_shndx >= shnum)
712 {
713 error_callback (data,
714 "ELF symbol table strtab link out of range", 0);
715 goto fail;
716 }
717 strtab_shdr = &shdrs[strtab_shndx - 1];
718
719 if (!backtrace_get_view (state, descriptor, symtab_shdr->sh_offset,
720 symtab_shdr->sh_size, error_callback, data,
721 &symtab_view))
722 goto fail;
723 symtab_view_valid = 1;
724
725 if (!backtrace_get_view (state, descriptor, strtab_shdr->sh_offset,
726 strtab_shdr->sh_size, error_callback, data,
727 &strtab_view))
728 goto fail;
729 strtab_view_valid = 1;
730
731 sdata = ((struct elf_syminfo_data *)
732 backtrace_alloc (state, sizeof *sdata, error_callback, data));
733 if (sdata == NULL)
734 goto fail;
735
736 if (!elf_initialize_syminfo (state,
737 symtab_view.data, symtab_shdr->sh_size,
738 strtab_view.data, strtab_shdr->sh_size,
739 error_callback, data, sdata))
740 {
741 backtrace_free (state, sdata, sizeof *sdata, error_callback, data);
742 goto fail;
743 }
744
745 /* We no longer need the symbol table, but we hold on to the
746 string table permanently. */
747 backtrace_release_view (state, &symtab_view, error_callback, data);
748
e561a992
ILT
749 *found_sym = 1;
750
751 elf_add_syminfo_data (state, sdata);
eff02e4f
ILT
752 }
753
754 /* FIXME: Need to handle compressed debug sections. */
755
756 backtrace_release_view (state, &shdrs_view, error_callback, data);
757 shdrs_view_valid = 0;
758 backtrace_release_view (state, &names_view, error_callback, data);
759 names_view_valid = 0;
760
761 /* Read all the debug sections in a single view, since they are
762 probably adjacent in the file. We never release this view. */
763
764 min_offset = 0;
765 max_offset = 0;
766 for (i = 0; i < (int) DEBUG_MAX; ++i)
767 {
768 off_t end;
769
6c084a5b
ILT
770 if (sections[i].size == 0)
771 continue;
eff02e4f
ILT
772 if (min_offset == 0 || sections[i].offset < min_offset)
773 min_offset = sections[i].offset;
774 end = sections[i].offset + sections[i].size;
775 if (end > max_offset)
776 max_offset = end;
777 }
778 if (min_offset == 0 || max_offset == 0)
779 {
780 if (!backtrace_close (descriptor, error_callback, data))
781 goto fail;
588f4f8f 782 *fileline_fn = elf_nodebug;
eff02e4f
ILT
783 return 1;
784 }
785
786 if (!backtrace_get_view (state, descriptor, min_offset,
787 max_offset - min_offset,
788 error_callback, data, &debug_view))
789 goto fail;
790 debug_view_valid = 1;
791
792 /* We've read all we need from the executable. */
793 if (!backtrace_close (descriptor, error_callback, data))
794 goto fail;
795 descriptor = -1;
796
797 for (i = 0; i < (int) DEBUG_MAX; ++i)
6c084a5b
ILT
798 {
799 if (sections[i].size == 0)
800 sections[i].data = NULL;
801 else
802 sections[i].data = ((const unsigned char *) debug_view.data
803 + (sections[i].offset - min_offset));
804 }
eff02e4f 805
e561a992
ILT
806 if (!backtrace_dwarf_add (state, base_address,
807 sections[DEBUG_INFO].data,
808 sections[DEBUG_INFO].size,
809 sections[DEBUG_LINE].data,
810 sections[DEBUG_LINE].size,
811 sections[DEBUG_ABBREV].data,
812 sections[DEBUG_ABBREV].size,
813 sections[DEBUG_RANGES].data,
814 sections[DEBUG_RANGES].size,
815 sections[DEBUG_STR].data,
816 sections[DEBUG_STR].size,
817 ehdr.e_ident[EI_DATA] == ELFDATA2MSB,
818 error_callback, data, fileline_fn))
eff02e4f
ILT
819 goto fail;
820
e561a992
ILT
821 *found_dwarf = 1;
822
eff02e4f
ILT
823 return 1;
824
825 fail:
826 if (shdrs_view_valid)
827 backtrace_release_view (state, &shdrs_view, error_callback, data);
828 if (names_view_valid)
829 backtrace_release_view (state, &names_view, error_callback, data);
830 if (symtab_view_valid)
831 backtrace_release_view (state, &symtab_view, error_callback, data);
832 if (strtab_view_valid)
833 backtrace_release_view (state, &strtab_view, error_callback, data);
834 if (debug_view_valid)
835 backtrace_release_view (state, &debug_view, error_callback, data);
836 if (descriptor != -1)
837 backtrace_close (descriptor, error_callback, data);
838 return 0;
839}
e561a992
ILT
840
841/* Data passed to phdr_callback. */
842
843struct phdr_data
844{
845 struct backtrace_state *state;
846 backtrace_error_callback error_callback;
847 void *data;
848 fileline *fileline_fn;
849 int *found_sym;
850 int *found_dwarf;
851};
852
853/* Callback passed to dl_iterate_phdr. Load debug info from shared
854 libraries. */
855
856static int
857phdr_callback (struct dl_phdr_info *info, size_t size ATTRIBUTE_UNUSED,
858 void *pdata)
859{
860 struct phdr_data *pd = (struct phdr_data *) pdata;
861 int descriptor;
73f41491 862 int does_not_exist;
e561a992
ILT
863 fileline elf_fileline_fn;
864 int found_dwarf;
865
866 /* There is not much we can do if we don't have the module name. If
867 the base address is 0, this is probably the executable, which we
868 already loaded. */
869 if (info->dlpi_name == NULL
870 || info->dlpi_name[0] == '\0'
871 || info->dlpi_addr == 0)
872 return 0;
873
73f41491
ILT
874 descriptor = backtrace_open (info->dlpi_name, pd->error_callback, pd->data,
875 &does_not_exist);
e561a992
ILT
876 if (descriptor < 0)
877 return 0;
878
879 if (elf_add (pd->state, descriptor, info->dlpi_addr, pd->error_callback,
880 pd->data, &elf_fileline_fn, pd->found_sym, &found_dwarf))
881 {
882 if (found_dwarf)
883 {
884 *pd->found_dwarf = 1;
885 *pd->fileline_fn = elf_fileline_fn;
886 }
887 }
888
889 return 0;
890}
891
892/* Initialize the backtrace data we need from an ELF executable. At
893 the ELF level, all we need to do is find the debug info
894 sections. */
895
896int
897backtrace_initialize (struct backtrace_state *state, int descriptor,
898 backtrace_error_callback error_callback,
899 void *data, fileline *fileline_fn)
900{
901 int found_sym;
902 int found_dwarf;
903 syminfo elf_syminfo_fn;
904 fileline elf_fileline_fn;
905 struct phdr_data pd;
906
907 if (!elf_add (state, descriptor, 0, error_callback, data, &elf_fileline_fn,
908 &found_sym, &found_dwarf))
909 return 0;
910
911 pd.state = state;
912 pd.error_callback = error_callback;
913 pd.data = data;
0153887c 914 pd.fileline_fn = &elf_fileline_fn;
e561a992
ILT
915 pd.found_sym = &found_sym;
916 pd.found_dwarf = &found_dwarf;
917
918 dl_iterate_phdr (phdr_callback, (void *) &pd);
919
920 elf_syminfo_fn = found_sym ? elf_syminfo : elf_nosyms;
921 if (!state->threaded)
922 {
923 if (state->syminfo_fn == NULL || found_sym)
924 state->syminfo_fn = elf_syminfo_fn;
925 }
926 else
927 {
928 __sync_bool_compare_and_swap (&state->syminfo_fn, NULL, elf_syminfo_fn);
929 if (found_sym)
930 __sync_bool_compare_and_swap (&state->syminfo_fn, elf_nosyms,
931 elf_syminfo_fn);
932 }
933
934 if (!state->threaded)
935 {
936 if (state->fileline_fn == NULL || state->fileline_fn == elf_nodebug)
937 *fileline_fn = elf_fileline_fn;
938 }
939 else
940 {
941 fileline current_fn;
942
943 /* Atomic load. */
944 current_fn = state->fileline_fn;
945 while (!__sync_bool_compare_and_swap (&state->fileline_fn, current_fn,
946 current_fn))
947 current_fn = state->fileline_fn;
948 if (current_fn == NULL || current_fn == elf_nodebug)
949 *fileline_fn = elf_fileline_fn;
950 }
951
952 return 1;
953}