1 /* Find debugging and symbol information for a module in libdwfl.
2 Copyright (C) 2005-2011 Red Hat, Inc.
3 This file is part of elfutils.
5 This file is free software; you can redistribute it and/or modify
6 it under the terms of either
8 * the GNU Lesser General Public License as published by the Free
9 Software Foundation; either version 3 of the License, or (at
10 your option) any later version
14 * the GNU General Public License as published by the Free
15 Software Foundation; either version 2 of the License, or (at
16 your option) any later version
18 or both in parallel, as here.
20 elfutils is distributed in the hope that it will be useful, but
21 WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23 General Public License for more details.
25 You should have received copies of the GNU General Public License and
26 the GNU Lesser General Public License along with this program. If
27 not, see <http://www.gnu.org/licenses/>. */
33 #include "../libdw/libdwP.h" /* DWARF_E_* values are here. */
36 /* Open libelf FILE->fd and compute the load base of ELF as loaded in MOD.
37 When we return success, FILE->elf and FILE->vaddr are set up. */
38 static inline Dwfl_Error
39 open_elf (Dwfl_Module *mod, struct dwfl_file *file)
41 if (file->elf == NULL)
43 /* CBFAIL uses errno if it's set, so clear it first in case we don't
44 set it with an open failure below. */
47 /* If there was a pre-primed file name left that the callback left
48 behind, try to open that file name. */
49 if (file->fd < 0 && file->name != NULL)
50 file->fd = TEMP_FAILURE_RETRY (open64 (file->name, O_RDONLY));
55 Dwfl_Error error = __libdw_open_file (&file->fd, &file->elf, true, false);
56 if (error != DWFL_E_NOERROR)
59 else if (unlikely (elf_kind (file->elf) != ELF_K_ELF))
68 GElf_Ehdr ehdr_mem, *ehdr = gelf_getehdr (file->elf, &ehdr_mem);
76 return DWFL_E (LIBELF, elf_errno ());
79 if (mod->e_type != ET_REL)
81 /* In any non-ET_REL file, we compute the "synchronization address".
83 We start with the address at the end of the first PT_LOAD
84 segment. When prelink converts REL to RELA in an ET_DYN
85 file, it expands the space between the beginning of the
86 segment and the actual code/data addresses. Since that
87 change wasn't made in the debug file, the distance from
88 p_vaddr to an address of interest (in an st_value or DWARF
89 data) now differs between the main and debug files. The
90 distance from address_sync to an address of interest remains
93 If there are no section headers at all (full stripping), then
94 the end of the first segment is a valid synchronization address.
95 This cannot happen in a prelinked file, since prelink itself
96 relies on section headers for prelinking and for undoing it.
97 (If you do full stripping on a prelinked file, then you get what
98 you deserve--you can neither undo the prelinking, nor expect to
99 line it up with a debug file separated before prelinking.)
101 However, when prelink processes an ET_EXEC file, it can do
102 something different. There it juggles the "special" sections
103 (SHT_DYNSYM et al) to make space for the additional prelink
104 special sections. Sometimes it will do this by moving a special
105 section like .dynstr after the real program sections in the first
106 PT_LOAD segment--i.e. to the end. That changes the end address of
107 the segment, so it no longer lines up correctly and is not a valid
108 synchronization address to use. Because of this, we need to apply
109 a different prelink-savvy means to discover the synchronization
110 address when there is a separate debug file and a prelinked main
111 file. That is done in find_debuginfo, below. */
114 if (unlikely (elf_getphdrnum (file->elf, &phnum) != 0))
117 file->vaddr = file->address_sync = 0;
118 for (size_t i = 0; i < phnum; ++i)
121 GElf_Phdr *ph = gelf_getphdr (file->elf, i, &ph_mem);
122 if (unlikely (ph == NULL))
124 if (ph->p_type == PT_LOAD)
126 file->vaddr = ph->p_vaddr & -ph->p_align;
127 file->address_sync = ph->p_vaddr + ph->p_memsz;
133 mod->e_type = ehdr->e_type;
135 /* Relocatable Linux kernels are ET_EXEC but act like ET_DYN. */
136 if (mod->e_type == ET_EXEC && file->vaddr != mod->low_addr)
137 mod->e_type = ET_DYN;
139 return DWFL_E_NOERROR;
142 /* Find the main ELF file for this module and open libelf on it.
143 When we return success, MOD->main.elf and MOD->main.bias are set up. */
146 __libdwfl_getelf (Dwfl_Module *mod)
148 if (mod->main.elf != NULL /* Already done. */
149 || mod->elferr != DWFL_E_NOERROR) /* Cached failure. */
152 mod->main.fd = (*mod->dwfl->callbacks->find_elf) (MODCB_ARGS (mod),
155 const bool fallback = mod->main.elf == NULL && mod->main.fd < 0;
156 mod->elferr = open_elf (mod, &mod->main);
157 if (mod->elferr != DWFL_E_NOERROR)
160 if (!mod->main.valid)
162 /* Clear any explicitly reported build ID, just in case it was wrong.
163 We'll fetch it from the file when asked. */
164 free (mod->build_id_bits);
165 mod->build_id_bits = NULL;
166 mod->build_id_len = 0;
170 /* We have an authoritative build ID for this module, so
171 don't use a file by name that doesn't match that ID. */
173 assert (mod->build_id_len > 0);
175 switch (__builtin_expect (__libdwfl_find_build_id (mod, false,
179 /* Build ID matches as it should. */
182 case -1: /* ELF error. */
183 mod->elferr = INTUSE(dwfl_errno) ();
186 case 0: /* File has no build ID note. */
187 case 1: /* FIle has a build ID that does not match. */
188 mod->elferr = DWFL_E_WRONG_ID_ELF;
195 /* We get here when it was the right ELF file. Clear it out. */
196 elf_end (mod->main.elf);
197 mod->main.elf = NULL;
198 if (mod->main.fd >= 0)
200 close (mod->main.fd);
205 mod->main_bias = mod->e_type == ET_REL ? 0 : mod->low_addr - mod->main.vaddr;
208 /* Search an ELF file for a ".gnu_debuglink" section. */
210 find_debuglink (Elf *elf, GElf_Word *crc)
213 if (elf_getshdrstrndx (elf, &shstrndx) < 0)
217 while ((scn = elf_nextscn (elf, scn)) != NULL)
220 GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
224 const char *name = elf_strptr (elf, shstrndx, shdr->sh_name);
228 if (!strcmp (name, ".gnu_debuglink"))
235 /* Found the .gnu_debuglink section. Extract its contents. */
236 Elf_Data *rawdata = elf_rawdata (scn, NULL);
242 .d_type = ELF_T_WORD,
244 .d_size = sizeof *crc,
245 .d_version = EV_CURRENT,
249 .d_type = ELF_T_WORD,
250 .d_buf = rawdata->d_buf + rawdata->d_size - sizeof *crc,
251 .d_size = sizeof *crc,
252 .d_version = EV_CURRENT,
256 GElf_Ehdr *ehdr = gelf_getehdr (elf, &ehdr_mem);
260 Elf_Data *d = gelf_xlatetom (elf, &crcdata, &conv, ehdr->e_ident[EI_DATA]);
263 assert (d == &crcdata);
265 return rawdata->d_buf;
268 /* If the main file might have been prelinked, then we need to
269 discover the correct synchronization address between the main and
270 debug files. Because of prelink's section juggling, we cannot rely
271 on the address_sync computed from PT_LOAD segments (see open_elf).
273 We will attempt to discover a synchronization address based on the
274 section headers instead. But finding a section address that is
275 safe to use requires identifying which sections are SHT_PROGBITS.
276 We can do that in the main file, but in the debug file all the
277 allocated sections have been transformed into SHT_NOBITS so we have
278 lost the means to match them up correctly.
280 The only method left to us is to decode the .gnu.prelink_undo
281 section in the prelinked main file. This shows what the sections
282 looked like before prelink juggled them--when they still had a
283 direct correspondence to the debug file. */
285 find_prelink_address_sync (Dwfl_Module *mod)
287 /* The magic section is only identified by name. */
289 if (elf_getshdrstrndx (mod->main.elf, &shstrndx) < 0)
290 return DWFL_E_LIBELF;
293 while ((scn = elf_nextscn (mod->main.elf, scn)) != NULL)
296 GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
297 if (unlikely (shdr == NULL))
298 return DWFL_E_LIBELF;
299 if (shdr->sh_type == SHT_PROGBITS
300 && !(shdr->sh_flags & SHF_ALLOC)
301 && shdr->sh_name != 0)
303 const char *secname = elf_strptr (mod->main.elf, shstrndx,
305 if (unlikely (secname == NULL))
306 return DWFL_E_LIBELF;
307 if (!strcmp (secname, ".gnu.prelink_undo"))
313 /* There was no .gnu.prelink_undo section. */
314 return DWFL_E_NOERROR;
316 Elf_Data *undodata = elf_rawdata (scn, NULL);
317 if (unlikely (undodata == NULL))
318 return DWFL_E_LIBELF;
320 /* Decode the section. It consists of the original ehdr, phdrs,
321 and shdrs (but omits section 0). */
331 .d_size = sizeof ehdr,
332 .d_type = ELF_T_EHDR,
333 .d_version = EV_CURRENT
335 Elf_Data src = *undodata;
336 src.d_size = gelf_fsize (mod->main.elf, ELF_T_EHDR, 1, EV_CURRENT);
337 src.d_type = ELF_T_EHDR;
338 if (unlikely (gelf_xlatetom (mod->main.elf, &dst, &src,
339 elf_getident (mod->main.elf, NULL)[EI_DATA])
341 return DWFL_E_LIBELF;
343 size_t shentsize = gelf_fsize (mod->main.elf, ELF_T_SHDR, 1, EV_CURRENT);
344 size_t phentsize = gelf_fsize (mod->main.elf, ELF_T_PHDR, 1, EV_CURRENT);
348 if (ehdr.e32.e_ident[EI_CLASS] == ELFCLASS32)
350 if (ehdr.e32.e_shentsize != shentsize
351 || ehdr.e32.e_phentsize != phentsize)
352 return DWFL_E_BAD_PRELINK;
353 phnum = ehdr.e32.e_phnum;
354 shnum = ehdr.e32.e_shnum;
358 if (ehdr.e64.e_shentsize != shentsize
359 || ehdr.e64.e_phentsize != phentsize)
360 return DWFL_E_BAD_PRELINK;
361 phnum = ehdr.e64.e_phnum;
362 shnum = ehdr.e64.e_shnum;
365 /* Since prelink does not store the zeroth section header in the undo
366 section, it cannot support SHN_XINDEX encoding. */
367 if (unlikely (shnum >= SHN_LORESERVE)
368 || unlikely (undodata->d_size != (src.d_size
370 + (shnum - 1) * shentsize)))
371 return DWFL_E_BAD_PRELINK;
373 /* We look at the allocated SHT_PROGBITS (or SHT_NOBITS) sections. (Most
374 every file will have some SHT_PROGBITS sections, but it's possible to
375 have one with nothing but .bss, i.e. SHT_NOBITS.) The special sections
376 that can be moved around have different sh_type values--except for
377 .interp, the section that became the PT_INTERP segment. So we exclude
378 the SHT_PROGBITS section whose address matches the PT_INTERP p_vaddr.
379 For this reason, we must examine the phdrs first to find PT_INTERP. */
381 GElf_Addr main_interp = 0;
384 if (unlikely (elf_getphdrnum (mod->main.elf, &main_phnum)))
385 return DWFL_E_LIBELF;
386 for (size_t i = 0; i < main_phnum; ++i)
389 if (unlikely (gelf_getphdr (mod->main.elf, i, &phdr) == NULL))
390 return DWFL_E_LIBELF;
391 if (phdr.p_type == PT_INTERP)
393 main_interp = phdr.p_vaddr;
399 src.d_buf += src.d_size;
400 src.d_type = ELF_T_PHDR;
401 src.d_size = phnum * phentsize;
403 GElf_Addr undo_interp = 0;
407 Elf32_Phdr p32[phnum];
408 Elf64_Phdr p64[phnum];
411 dst.d_size = sizeof phdr;
412 if (unlikely (gelf_xlatetom (mod->main.elf, &dst, &src,
413 ehdr.e32.e_ident[EI_DATA]) == NULL))
414 return DWFL_E_LIBELF;
415 if (ehdr.e32.e_ident[EI_CLASS] == ELFCLASS32)
417 for (uint_fast16_t i = 0; i < phnum; ++i)
418 if (phdr.p32[i].p_type == PT_INTERP)
420 undo_interp = phdr.p32[i].p_vaddr;
426 for (uint_fast16_t i = 0; i < phnum; ++i)
427 if (phdr.p64[i].p_type == PT_INTERP)
429 undo_interp = phdr.p64[i].p_vaddr;
435 if (unlikely ((main_interp == 0) != (undo_interp == 0)))
436 return DWFL_E_BAD_PRELINK;
438 src.d_buf += src.d_size;
439 src.d_type = ELF_T_SHDR;
440 src.d_size = gelf_fsize (mod->main.elf, ELF_T_SHDR, shnum - 1, EV_CURRENT);
444 Elf32_Shdr s32[shnum - 1];
445 Elf64_Shdr s64[shnum - 1];
448 dst.d_size = sizeof shdr;
449 if (unlikely (gelf_xlatetom (mod->main.elf, &dst, &src,
450 ehdr.e32.e_ident[EI_DATA]) == NULL))
451 return DWFL_E_LIBELF;
453 /* Now we can look at the original section headers of the main file
454 before it was prelinked. First we'll apply our method to the main
455 file sections as they are after prelinking, to calculate the
456 synchronization address of the main file. Then we'll apply that
457 same method to the saved section headers, to calculate the matching
458 synchronization address of the debug file.
460 The method is to consider SHF_ALLOC sections that are either
461 SHT_PROGBITS or SHT_NOBITS, excluding the section whose sh_addr
462 matches the PT_INTERP p_vaddr. The special sections that can be
463 moved by prelink have other types, except for .interp (which
464 becomes PT_INTERP). The "real" sections cannot move as such, but
465 .bss can be split into .dynbss and .bss, with the total memory
466 image remaining the same but being spread across the two sections.
467 So we consider the highest section end, which still matches up. */
471 inline void consider_shdr (GElf_Addr interp,
477 if ((sh_flags & SHF_ALLOC)
478 && ((sh_type == SHT_PROGBITS && sh_addr != interp)
479 || sh_type == SHT_NOBITS))
481 const GElf_Addr sh_end = sh_addr + sh_size;
482 if (sh_end > highest)
489 while ((scn = elf_nextscn (mod->main.elf, scn)) != NULL)
492 GElf_Shdr *sh = gelf_getshdr (scn, &sh_mem);
493 if (unlikely (sh == NULL))
494 return DWFL_E_LIBELF;
495 consider_shdr (main_interp, sh->sh_type, sh->sh_flags,
496 sh->sh_addr, sh->sh_size);
498 if (highest > mod->main.vaddr)
500 mod->main.address_sync = highest;
503 if (ehdr.e32.e_ident[EI_CLASS] == ELFCLASS32)
504 for (size_t i = 0; i < shnum - 1; ++i)
505 consider_shdr (undo_interp, shdr.s32[i].sh_type, shdr.s32[i].sh_flags,
506 shdr.s32[i].sh_addr, shdr.s32[i].sh_size);
508 for (size_t i = 0; i < shnum - 1; ++i)
509 consider_shdr (undo_interp, shdr.s64[i].sh_type, shdr.s64[i].sh_flags,
510 shdr.s64[i].sh_addr, shdr.s64[i].sh_size);
512 if (highest > mod->debug.vaddr)
513 mod->debug.address_sync = highest;
515 return DWFL_E_BAD_PRELINK;
518 return DWFL_E_NOERROR;
521 /* Find the separate debuginfo file for this module and open libelf on it.
522 When we return success, MOD->debug is set up. */
524 find_debuginfo (Dwfl_Module *mod)
526 if (mod->debug.elf != NULL)
527 return DWFL_E_NOERROR;
529 GElf_Word debuglink_crc = 0;
530 const char *debuglink_file = find_debuglink (mod->main.elf, &debuglink_crc);
532 mod->debug.fd = (*mod->dwfl->callbacks->find_debuginfo) (MODCB_ARGS (mod),
537 Dwfl_Error result = open_elf (mod, &mod->debug);
538 if (result == DWFL_E_NOERROR && mod->debug.address_sync != 0)
539 result = find_prelink_address_sync (mod);
544 /* Try to find a symbol table in FILE.
545 Returns DWFL_E_NOERROR if a proper one is found.
546 Returns DWFL_E_NO_SYMTAB if not, but still sets results for SHT_DYNSYM. */
548 load_symtab (struct dwfl_file *file, struct dwfl_file **symfile,
549 Elf_Scn **symscn, Elf_Scn **xndxscn,
550 size_t *syments, int *first_global, GElf_Word *strshndx)
554 while ((scn = elf_nextscn (file->elf, scn)) != NULL)
556 GElf_Shdr shdr_mem, *shdr = gelf_getshdr (scn, &shdr_mem);
558 switch (shdr->sh_type)
564 *strshndx = shdr->sh_link;
565 *syments = shdr->sh_size / shdr->sh_entsize;
566 *first_global = shdr->sh_info;
567 if (*xndxscn != NULL)
568 return DWFL_E_NOERROR;
574 /* Use this if need be, but keep looking for SHT_SYMTAB. */
577 *strshndx = shdr->sh_link;
578 *syments = shdr->sh_size / shdr->sh_entsize;
581 case SHT_SYMTAB_SHNDX:
584 return DWFL_E_NOERROR;
593 /* We found one, though no SHT_SYMTAB_SHNDX to go with it. */
594 return DWFL_E_NOERROR;
596 /* We found no SHT_SYMTAB, so any SHT_SYMTAB_SHNDX was bogus.
597 We might have found an SHT_DYNSYM and set *SYMSCN et al though. */
599 return DWFL_E_NO_SYMTAB;
603 /* Translate addresses into file offsets.
604 OFFS[*] start out zero and remain zero if unresolved. */
606 find_offsets (Elf *elf, size_t phnum, size_t n,
607 GElf_Addr addrs[n], GElf_Off offs[n])
610 for (size_t i = 0; i < phnum; ++i)
613 GElf_Phdr *phdr = gelf_getphdr (elf, i, &phdr_mem);
614 if (phdr != NULL && phdr->p_type == PT_LOAD && phdr->p_memsz > 0)
615 for (size_t j = 0; j < n; ++j)
617 && addrs[j] >= phdr->p_vaddr
618 && addrs[j] - phdr->p_vaddr < phdr->p_filesz)
620 offs[j] = addrs[j] - phdr->p_vaddr + phdr->p_offset;
627 /* Try to find a dynamic symbol table via phdrs. */
629 find_dynsym (Dwfl_Module *mod)
632 GElf_Ehdr *ehdr = gelf_getehdr (mod->main.elf, &ehdr_mem);
635 if (unlikely (elf_getphdrnum (mod->main.elf, &phnum) != 0))
638 for (size_t i = 0; i < phnum; ++i)
641 GElf_Phdr *phdr = gelf_getphdr (mod->main.elf, i, &phdr_mem);
645 if (phdr->p_type == PT_DYNAMIC)
647 /* Examine the dynamic section for the pointers we need. */
649 Elf_Data *data = elf_getdata_rawchunk (mod->main.elf,
650 phdr->p_offset, phdr->p_filesz,
663 GElf_Addr addrs[i_max] = { 0, };
664 GElf_Xword strsz = 0;
665 size_t n = data->d_size / gelf_fsize (mod->main.elf,
666 ELF_T_DYN, 1, EV_CURRENT);
667 for (size_t j = 0; j < n; ++j)
670 GElf_Dyn *dyn = gelf_getdyn (data, j, &dyn_mem);
675 addrs[i_symtab] = dyn->d_un.d_ptr;
679 addrs[i_hash] = dyn->d_un.d_ptr;
683 addrs[i_gnu_hash] = dyn->d_un.d_ptr;
687 addrs[i_strtab] = dyn->d_un.d_ptr;
691 strsz = dyn->d_un.d_val;
703 /* Translate pointers into file offsets. */
704 GElf_Off offs[i_max] = { 0, };
705 find_offsets (mod->main.elf, phnum, i_max, addrs, offs);
707 /* Figure out the size of the symbol table. */
708 if (offs[i_hash] != 0)
710 /* In the original format, .hash says the size of .dynsym. */
712 size_t entsz = SH_ENTSIZE_HASH (ehdr);
713 data = elf_getdata_rawchunk (mod->main.elf,
714 offs[i_hash] + entsz, entsz,
715 entsz == 4 ? ELF_T_WORD
718 mod->syments = (entsz == 4
719 ? *(const GElf_Word *) data->d_buf
720 : *(const GElf_Xword *) data->d_buf);
722 if (offs[i_gnu_hash] != 0 && mod->syments == 0)
724 /* In the new format, we can derive it with some work. */
730 Elf32_Word maskwords;
734 data = elf_getdata_rawchunk (mod->main.elf, offs[i_gnu_hash],
735 sizeof *header, ELF_T_WORD);
738 header = data->d_buf;
739 Elf32_Word nbuckets = header->nbuckets;
740 Elf32_Word symndx = header->symndx;
741 GElf_Off buckets_at = (offs[i_gnu_hash] + sizeof *header
742 + (gelf_getclass (mod->main.elf)
743 * sizeof (Elf32_Word)
744 * header->maskwords));
746 data = elf_getdata_rawchunk (mod->main.elf, buckets_at,
747 nbuckets * sizeof (Elf32_Word),
749 if (data != NULL && symndx < nbuckets)
751 const Elf32_Word *const buckets = data->d_buf;
752 Elf32_Word maxndx = symndx;
753 for (Elf32_Word bucket = 0; bucket < nbuckets; ++bucket)
754 if (buckets[bucket] > maxndx)
755 maxndx = buckets[bucket];
757 GElf_Off hasharr_at = (buckets_at
758 + nbuckets * sizeof (Elf32_Word));
759 hasharr_at += (maxndx - symndx) * sizeof (Elf32_Word);
762 data = elf_getdata_rawchunk (mod->main.elf,
767 && (*(const Elf32_Word *) data->d_buf & 1u))
769 mod->syments = maxndx + 1;
773 hasharr_at += sizeof (Elf32_Word);
774 } while (data != NULL);
778 if (offs[i_strtab] > offs[i_symtab] && mod->syments == 0)
779 mod->syments = ((offs[i_strtab] - offs[i_symtab])
780 / gelf_fsize (mod->main.elf,
781 ELF_T_SYM, 1, EV_CURRENT));
783 if (mod->syments > 0)
785 mod->symdata = elf_getdata_rawchunk (mod->main.elf,
787 gelf_fsize (mod->main.elf,
792 if (mod->symdata != NULL)
794 mod->symstrdata = elf_getdata_rawchunk (mod->main.elf,
798 if (mod->symstrdata == NULL)
801 if (mod->symdata == NULL)
802 mod->symerr = DWFL_E (LIBELF, elf_errno ());
805 mod->symfile = &mod->main;
806 mod->symerr = DWFL_E_NOERROR;
814 /* Try to find a symbol table in either MOD->main.elf or MOD->debug.elf. */
816 find_symtab (Dwfl_Module *mod)
818 if (mod->symdata != NULL /* Already done. */
819 || mod->symerr != DWFL_E_NOERROR) /* Cached previous failure. */
822 __libdwfl_getelf (mod);
823 mod->symerr = mod->elferr;
824 if (mod->symerr != DWFL_E_NOERROR)
827 mod->first_global = -1; /* Unknown, unless explicitly set by load_symtab. */
829 /* First see if the main ELF file has the debugging information. */
830 Elf_Scn *symscn = NULL, *xndxscn = NULL;
832 mod->symerr = load_symtab (&mod->main, &mod->symfile, &symscn,
833 &xndxscn, &mod->syments, &mod->first_global,
843 case DWFL_E_NO_SYMTAB:
844 /* Now we have to look for a separate debuginfo file. */
845 mod->symerr = find_debuginfo (mod);
852 mod->symerr = load_symtab (&mod->debug, &mod->symfile, &symscn,
853 &xndxscn, &mod->syments,
854 &mod->first_global, &strshndx);
857 case DWFL_E_CB: /* The find_debuginfo hook failed. */
858 mod->symerr = DWFL_E_NO_SYMTAB;
870 case DWFL_E_NO_SYMTAB:
873 /* We still have the dynamic symbol table. */
874 mod->symerr = DWFL_E_NOERROR;
878 /* Last ditch, look for dynamic symbols without section headers. */
885 /* This does some sanity checks on the string table section. */
886 if (elf_strptr (mod->symfile->elf, strshndx, 0) == NULL)
889 mod->symerr = DWFL_E (LIBELF, elf_errno ());
893 /* Cache the data; MOD->syments and MOD->first_global were set above. */
895 mod->symstrdata = elf_getdata (elf_getscn (mod->symfile->elf, strshndx),
897 if (mod->symstrdata == NULL)
901 mod->symxndxdata = NULL;
904 mod->symxndxdata = elf_getdata (xndxscn, NULL);
905 if (mod->symxndxdata == NULL)
909 mod->symdata = elf_getdata (symscn, NULL);
910 if (mod->symdata == NULL)
915 /* Try to open a libebl backend for MOD. */
918 __libdwfl_module_getebl (Dwfl_Module *mod)
920 if (mod->ebl == NULL)
922 __libdwfl_getelf (mod);
923 if (mod->elferr != DWFL_E_NOERROR)
926 mod->ebl = ebl_openbackend (mod->main.elf);
927 if (mod->ebl == NULL)
928 return DWFL_E_LIBEBL;
930 return DWFL_E_NOERROR;
933 /* Try to start up libdw on DEBUGFILE. */
935 load_dw (Dwfl_Module *mod, struct dwfl_file *debugfile)
937 if (mod->e_type == ET_REL && !debugfile->relocated)
939 const Dwfl_Callbacks *const cb = mod->dwfl->callbacks;
941 /* The debugging sections have to be relocated. */
942 if (cb->section_address == NULL)
945 Dwfl_Error error = __libdwfl_module_getebl (mod);
946 if (error != DWFL_E_NOERROR)
950 Dwfl_Error result = mod->symerr;
951 if (result == DWFL_E_NOERROR)
952 result = __libdwfl_relocate (mod, debugfile->elf, true);
953 if (result != DWFL_E_NOERROR)
956 /* Don't keep the file descriptors around. */
957 if (mod->main.fd != -1 && elf_cntl (mod->main.elf, ELF_C_FDREAD) == 0)
959 close (mod->main.fd);
962 if (debugfile->fd != -1 && elf_cntl (debugfile->elf, ELF_C_FDREAD) == 0)
964 close (debugfile->fd);
969 mod->dw = INTUSE(dwarf_begin_elf) (debugfile->elf, DWARF_C_READ, NULL);
972 int err = INTUSE(dwarf_errno) ();
973 return err == DWARF_E_NO_DWARF ? DWFL_E_NO_DWARF : DWFL_E (LIBDW, err);
976 /* Until we have iterated through all CU's, we might do lazy lookups. */
979 return DWFL_E_NOERROR;
982 /* Try to start up libdw on either the main file or the debuginfo file. */
984 find_dw (Dwfl_Module *mod)
986 if (mod->dw != NULL /* Already done. */
987 || mod->dwerr != DWFL_E_NOERROR) /* Cached previous failure. */
990 __libdwfl_getelf (mod);
991 mod->dwerr = mod->elferr;
992 if (mod->dwerr != DWFL_E_NOERROR)
995 /* First see if the main ELF file has the debugging information. */
996 mod->dwerr = load_dw (mod, &mod->main);
1000 mod->debug.elf = mod->main.elf;
1001 mod->debug.address_sync = mod->main.address_sync;
1004 case DWFL_E_NO_DWARF:
1011 /* Now we have to look for a separate debuginfo file. */
1012 mod->dwerr = find_debuginfo (mod);
1015 case DWFL_E_NOERROR:
1016 mod->dwerr = load_dw (mod, &mod->debug);
1019 case DWFL_E_CB: /* The find_debuginfo hook failed. */
1020 mod->dwerr = DWFL_E_NO_DWARF;
1028 mod->dwerr = __libdwfl_canon_error (mod->dwerr);
1032 dwfl_module_getdwarf (Dwfl_Module *mod, Dwarf_Addr *bias)
1038 if (mod->dwerr == DWFL_E_NOERROR)
1040 /* If dwfl_module_getelf was used previously, then partial apply
1041 relocation to miscellaneous sections in the debug file too. */
1042 if (mod->e_type == ET_REL
1043 && mod->main.relocated && ! mod->debug.relocated)
1045 mod->debug.relocated = true;
1046 if (mod->debug.elf != mod->main.elf)
1047 (void) __libdwfl_relocate (mod, mod->debug.elf, false);
1050 *bias = dwfl_adjusted_dwarf_addr (mod, 0);
1054 __libdwfl_seterrno (mod->dwerr);
1057 INTDEF (dwfl_module_getdwarf)
1060 dwfl_module_getsymtab (Dwfl_Module *mod)
1066 if (mod->symerr == DWFL_E_NOERROR)
1067 return mod->syments;
1069 __libdwfl_seterrno (mod->symerr);
1072 INTDEF (dwfl_module_getsymtab)