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
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>
39 #ifdef HAVE_DL_ITERATE_PHDR
43 #include "backtrace.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
56 const char *dlpi_name
;
60 dl_iterate_phdr (int (*callback
) (struct dl_phdr_info
*,
61 size_t, void *) ATTRIBUTE_UNUSED
,
62 void *data ATTRIBUTE_UNUSED
)
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
75 #if BACKTRACE_ELF_SIZE != 32 && BACKTRACE_ELF_SIZE != 64
76 #error "Unknown BACKTRACE_ELF_SIZE"
79 /* <link.h> might #include <elf.h> which might define our constants
80 with slightly different values. Undefine them to be safe. */
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. */
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. */
132 /* Data structures and associated constants. */
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. */
169 #define ELFDATA2LSB 1
170 #define ELFDATA2MSB 2
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 */
193 #define SHT_DYNSYM 11
195 #if BACKTRACE_ELF_SIZE == 32
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 */
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 */
224 /* An index of ELF sections we care about. */
236 /* Names of sections, indexed by enum elf_section. */
238 static const char * const debug_section_names
[DEBUG_MAX
] =
247 /* Information we gather for the sections we care about. */
249 struct debug_section_info
251 /* Section file offset. */
255 /* Section contents, after read from file. */
256 const unsigned char *data
;
259 /* Information we keep for an ELF symbol. */
263 /* The name of the symbol. */
265 /* The address of the symbol. */
267 /* The size of the symbol. */
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. */
283 /* A dummy callback function used when we can't find any debug info. */
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);
295 /* A dummy callback function used when we can't find a symbol
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. */
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
)
317 else if (e1
->address
> e2
->address
)
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
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
;
335 if (addr
< entry
->address
)
337 else if (addr
>= entry
->address
+ entry
->size
)
343 /* Initialize the symbol table info for elf_syminfo. */
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
)
354 const b_elf_sym
*sym
;
355 size_t elf_symbol_count
;
356 size_t elf_symbol_size
;
357 struct elf_symbol
*elf_symbols
;
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
)
370 info
= sym
->st_info
& 0xf;
371 if ((info
== STT_FUNC
|| info
== STT_OBJECT
)
372 && sym
->st_shndx
!= SHN_UNDEF
)
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
,
380 if (elf_symbols
== NULL
)
383 sym
= (const b_elf_sym
*) symtab_data
;
385 for (i
= 0; i
< sym_count
; ++i
, ++sym
)
389 info
= sym
->st_info
& 0xf;
390 if (info
!= STT_FUNC
&& info
!= STT_OBJECT
)
392 if (sym
->st_shndx
== SHN_UNDEF
)
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
,
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
;
407 qsort (elf_symbols
, elf_symbol_count
, sizeof (struct elf_symbol
),
411 sdata
->symbols
= elf_symbols
;
412 sdata
->count
= elf_symbol_count
;
417 /* Add EDATA to the list in STATE. */
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
;
437 struct elf_syminfo_data
**pp
;
439 pp
= (struct elf_syminfo_data
**) (void *) &state
->syminfo_data
;
443 struct elf_syminfo_data
*p
;
445 p
= backtrace_atomic_load_pointer (pp
);
453 if (__sync_bool_compare_and_swap (pp
, NULL
, edata
))
459 /* Return the symbol name and value for an ADDR. */
462 elf_syminfo (struct backtrace_state
*state
, uintptr_t addr
,
463 backtrace_syminfo_callback callback
,
464 backtrace_error_callback error_callback ATTRIBUTE_UNUSED
,
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
;
476 sym
= ((struct elf_symbol
*)
477 bsearch (&addr
, edata
->symbols
, edata
->count
,
478 sizeof (struct elf_symbol
), elf_symbol_search
));
485 struct elf_syminfo_data
**pp
;
487 pp
= (struct elf_syminfo_data
**) (void *) &state
->syminfo_data
;
490 edata
= backtrace_atomic_load_pointer (pp
);
494 sym
= ((struct elf_symbol
*)
495 bsearch (&addr
, edata
->symbols
, edata
->count
,
496 sizeof (struct elf_symbol
), elf_symbol_search
));
505 callback (data
, addr
, NULL
, 0, 0);
507 callback (data
, addr
, sym
->name
, sym
->address
, sym
->size
);
510 /* Add the backtrace data for one ELF file. */
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
;
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
;
528 struct backtrace_view names_view
;
529 int names_view_valid
;
531 unsigned int symtab_shndx
;
532 unsigned int dynsym_shndx
;
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
;
541 struct backtrace_view debug_view
;
542 int debug_view_valid
;
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
,
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);
569 if (ehdr
.e_ident
[EI_VERSION
] != EV_CURRENT
)
571 error_callback (data
, "executable file is unrecognized ELF version", 0);
575 #if BACKTRACE_ELF_SIZE == 32
576 #define BACKTRACE_ELFCLASS ELFCLASS32
578 #define BACKTRACE_ELFCLASS ELFCLASS64
581 if (ehdr
.e_ident
[EI_CLASS
] != BACKTRACE_ELFCLASS
)
583 error_callback (data
, "executable file is unexpected ELF class", 0);
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);
594 shoff
= ehdr
.e_shoff
;
595 shnum
= ehdr
.e_shnum
;
596 shstrndx
= ehdr
.e_shstrndx
;
598 if ((shnum
== 0 || shstrndx
== SHN_XINDEX
)
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
))
608 shdr
= (const b_elf_shdr
*) shdr_view
.data
;
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)
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
))
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
))
656 names_view_valid
= 1;
657 names
= (const char *) names_view
.data
;
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
;
672 shdr
= &shdrs
[i
- 1];
674 if (shdr
->sh_type
== SHT_SYMTAB
)
676 else if (shdr
->sh_type
== SHT_DYNSYM
)
679 sh_name
= shdr
->sh_name
;
680 if (sh_name
>= shstr_size
)
682 error_callback (data
, "ELF section name out of range", 0);
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
;
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);
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
,
722 symtab_view_valid
= 1;
724 if (!backtrace_get_view (state
, descriptor
, strtab_shdr
->sh_offset
,
725 strtab_shdr
->sh_size
, error_callback
, data
,
728 strtab_view_valid
= 1;
730 sdata
= ((struct elf_syminfo_data
*)
731 backtrace_alloc (state
, sizeof *sdata
, error_callback
, data
));
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
);
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
);
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. */
765 for (i
= 0; i
< (int) DEBUG_MAX
; ++i
)
769 if (sections
[i
].size
== 0)
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
)
777 if (min_offset
== 0 || max_offset
== 0)
779 if (!backtrace_close (descriptor
, error_callback
, data
))
781 *fileline_fn
= elf_nodebug
;
785 if (!backtrace_get_view (state
, descriptor
, min_offset
,
786 max_offset
- min_offset
,
787 error_callback
, data
, &debug_view
))
789 debug_view_valid
= 1;
791 /* We've read all we need from the executable. */
792 if (!backtrace_close (descriptor
, error_callback
, data
))
796 for (i
= 0; i
< (int) DEBUG_MAX
; ++i
)
798 if (sections
[i
].size
== 0)
799 sections
[i
].data
= NULL
;
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
))
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
);
840 /* Data passed to phdr_callback. */
844 struct backtrace_state
*state
;
845 backtrace_error_callback error_callback
;
847 fileline
*fileline_fn
;
852 /* Callback passed to dl_iterate_phdr. Load debug info from shared
856 phdr_callback (struct dl_phdr_info
*info
, size_t size ATTRIBUTE_UNUSED
,
859 struct phdr_data
*pd
= (struct phdr_data
*) pdata
;
862 fileline elf_fileline_fn
;
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')
869 descriptor
= backtrace_open (info
->dlpi_name
, pd
->error_callback
, pd
->data
,
874 if (elf_add (pd
->state
, descriptor
, info
->dlpi_addr
, pd
->error_callback
,
875 pd
->data
, &elf_fileline_fn
, pd
->found_sym
, &found_dwarf
))
879 *pd
->found_dwarf
= 1;
880 *pd
->fileline_fn
= elf_fileline_fn
;
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
892 backtrace_initialize (struct backtrace_state
*state
, int descriptor
,
893 backtrace_error_callback error_callback
,
894 void *data
, fileline
*fileline_fn
)
898 fileline elf_fileline_fn
;
901 if (!elf_add (state
, descriptor
, 0, error_callback
, data
, &elf_fileline_fn
,
902 &found_sym
, &found_dwarf
))
906 pd
.error_callback
= error_callback
;
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
)
917 state
->syminfo_fn
= elf_syminfo
;
918 else if (state
->syminfo_fn
== NULL
)
919 state
->syminfo_fn
= elf_nosyms
;
924 backtrace_atomic_store_pointer (&state
->syminfo_fn
, elf_syminfo
);
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
;
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
;