Line data Source code
1 : /* Find debugging and symbol information for a module in libdwfl.
2 : Copyright (C) 2005-2012, 2014, 2015 Red Hat, Inc.
3 : This file is part of elfutils.
4 :
5 : This file is free software; you can redistribute it and/or modify
6 : it under the terms of either
7 :
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
11 :
12 : or
13 :
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
17 :
18 : or both in parallel, as here.
19 :
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.
24 :
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/>. */
28 :
29 : #ifdef HAVE_CONFIG_H
30 : # include <config.h>
31 : #endif
32 :
33 : #include "libdwflP.h"
34 : #include <inttypes.h>
35 : #include <fcntl.h>
36 : #include <string.h>
37 : #include <unistd.h>
38 : #include "../libdw/libdwP.h" /* DWARF_E_* values are here. */
39 : #include "../libelf/libelfP.h"
40 : #include "system.h"
41 :
42 : static inline Dwfl_Error
43 5212 : open_elf_file (Elf **elf, int *fd, char **name)
44 : {
45 5212 : if (*elf == NULL)
46 : {
47 : /* CBFAIL uses errno if it's set, so clear it first in case we don't
48 : set it with an open failure below. */
49 5158 : errno = 0;
50 :
51 : /* If there was a pre-primed file name left that the callback left
52 : behind, try to open that file name. */
53 5158 : if (*fd < 0 && *name != NULL)
54 0 : *fd = TEMP_FAILURE_RETRY (open (*name, O_RDONLY));
55 :
56 5158 : if (*fd < 0)
57 111 : return CBFAIL;
58 :
59 5047 : return __libdw_open_file (fd, elf, true, false);
60 : }
61 54 : else if (unlikely (elf_kind (*elf) != ELF_K_ELF))
62 : {
63 0 : elf_end (*elf);
64 0 : *elf = NULL;
65 0 : close (*fd);
66 0 : *fd = -1;
67 0 : return DWFL_E_BADELF;
68 : }
69 :
70 : /* Elf file already open and looks fine. */
71 : return DWFL_E_NOERROR;
72 : }
73 :
74 : /* Open libelf FILE->fd and compute the load base of ELF as loaded in MOD.
75 : When we return success, FILE->elf and FILE->vaddr are set up. */
76 : static inline Dwfl_Error
77 5207 : open_elf (Dwfl_Module *mod, struct dwfl_file *file)
78 : {
79 5207 : Dwfl_Error error = open_elf_file (&file->elf, &file->fd, &file->name);
80 5207 : if (error != DWFL_E_NOERROR)
81 : return error;
82 :
83 5096 : GElf_Ehdr ehdr_mem, *ehdr = gelf_getehdr (file->elf, &ehdr_mem);
84 5096 : if (ehdr == NULL)
85 : {
86 0 : elf_error:
87 0 : elf_end (file->elf);
88 0 : file->elf = NULL;
89 0 : close (file->fd);
90 0 : file->fd = -1;
91 0 : return DWFL_E (LIBELF, elf_errno ());
92 : }
93 :
94 5096 : if (ehdr->e_type != ET_REL)
95 : {
96 : /* In any non-ET_REL file, we compute the "synchronization address".
97 :
98 : We start with the address at the end of the first PT_LOAD
99 : segment. When prelink converts REL to RELA in an ET_DYN
100 : file, it expands the space between the beginning of the
101 : segment and the actual code/data addresses. Since that
102 : change wasn't made in the debug file, the distance from
103 : p_vaddr to an address of interest (in an st_value or DWARF
104 : data) now differs between the main and debug files. The
105 : distance from address_sync to an address of interest remains
106 : consistent.
107 :
108 : If there are no section headers at all (full stripping), then
109 : the end of the first segment is a valid synchronization address.
110 : This cannot happen in a prelinked file, since prelink itself
111 : relies on section headers for prelinking and for undoing it.
112 : (If you do full stripping on a prelinked file, then you get what
113 : you deserve--you can neither undo the prelinking, nor expect to
114 : line it up with a debug file separated before prelinking.)
115 :
116 : However, when prelink processes an ET_EXEC file, it can do
117 : something different. There it juggles the "special" sections
118 : (SHT_DYNSYM et al) to make space for the additional prelink
119 : special sections. Sometimes it will do this by moving a special
120 : section like .dynstr after the real program sections in the first
121 : PT_LOAD segment--i.e. to the end. That changes the end address of
122 : the segment, so it no longer lines up correctly and is not a valid
123 : synchronization address to use. Because of this, we need to apply
124 : a different prelink-savvy means to discover the synchronization
125 : address when there is a separate debug file and a prelinked main
126 : file. That is done in find_debuginfo, below. */
127 :
128 : size_t phnum;
129 5095 : if (unlikely (elf_getphdrnum (file->elf, &phnum) != 0))
130 : goto elf_error;
131 :
132 5095 : file->vaddr = file->address_sync = 0;
133 15193 : for (size_t i = 0; i < phnum; ++i)
134 : {
135 : GElf_Phdr ph_mem;
136 15193 : GElf_Phdr *ph = gelf_getphdr (file->elf, i, &ph_mem);
137 15193 : if (unlikely (ph == NULL))
138 : goto elf_error;
139 15193 : if (ph->p_type == PT_LOAD)
140 : {
141 5095 : file->vaddr = ph->p_vaddr & -ph->p_align;
142 5095 : file->address_sync = ph->p_vaddr + ph->p_memsz;
143 5095 : break;
144 : }
145 : }
146 : }
147 :
148 : /* We only want to set the module e_type explictly once, derived from
149 : the main ELF file. (It might be changed for the kernel, because
150 : that is special - see below.) open_elf is always called first for
151 : the main ELF file, because both find_dw and find_symtab call
152 : __libdwfl_getelf first to open the main file. So don't let debug
153 : or aux files override the module e_type. The kernel heuristic
154 : below could otherwise trigger for non-kernel/non-main files, since
155 : their phdrs might not match the actual load addresses. */
156 5096 : if (file == &mod->main)
157 : {
158 5051 : mod->e_type = ehdr->e_type;
159 :
160 : /* Relocatable Linux kernels are ET_EXEC but act like ET_DYN. */
161 5051 : if (mod->e_type == ET_EXEC && file->vaddr != mod->low_addr)
162 0 : mod->e_type = ET_DYN;
163 : }
164 : else
165 45 : assert (mod->main.elf != NULL);
166 :
167 : return DWFL_E_NOERROR;
168 : }
169 :
170 : /* We have an authoritative build ID for this module MOD, so don't use
171 : a file by name that doesn't match that ID. */
172 : static void
173 0 : mod_verify_build_id (Dwfl_Module *mod)
174 : {
175 0 : assert (mod->build_id_len > 0);
176 :
177 0 : switch (__builtin_expect (__libdwfl_find_build_id (mod, false,
178 : mod->main.elf), 2))
179 : {
180 : case 2:
181 : /* Build ID matches as it should. */
182 : return;
183 :
184 0 : case -1: /* ELF error. */
185 0 : mod->elferr = INTUSE(dwfl_errno) ();
186 0 : break;
187 :
188 0 : case 0: /* File has no build ID note. */
189 : case 1: /* FIle has a build ID that does not match. */
190 0 : mod->elferr = DWFL_E_WRONG_ID_ELF;
191 0 : break;
192 :
193 0 : default:
194 0 : abort ();
195 : }
196 :
197 : /* We get here when it was the right ELF file. Clear it out. */
198 0 : elf_end (mod->main.elf);
199 0 : mod->main.elf = NULL;
200 0 : if (mod->main.fd >= 0)
201 : {
202 0 : close (mod->main.fd);
203 0 : mod->main.fd = -1;
204 : }
205 : }
206 :
207 : /* Find the main ELF file for this module and open libelf on it.
208 : When we return success, MOD->main.elf and MOD->main.bias are set up. */
209 : void
210 : internal_function
211 8319 : __libdwfl_getelf (Dwfl_Module *mod)
212 : {
213 8319 : if (mod->main.elf != NULL /* Already done. */
214 5087 : || mod->elferr != DWFL_E_NOERROR) /* Cached failure. */
215 : return;
216 :
217 5067 : mod->main.fd = (*mod->dwfl->callbacks->find_elf) (MODCB_ARGS (mod),
218 : &mod->main.name,
219 : &mod->main.elf);
220 5067 : const bool fallback = mod->main.elf == NULL && mod->main.fd < 0;
221 5067 : mod->elferr = open_elf (mod, &mod->main);
222 5067 : if (mod->elferr != DWFL_E_NOERROR)
223 : return;
224 :
225 5051 : if (!mod->main.valid)
226 : {
227 : /* Clear any explicitly reported build ID, just in case it was wrong.
228 : We'll fetch it from the file when asked. */
229 5051 : free (mod->build_id_bits);
230 5051 : mod->build_id_bits = NULL;
231 5051 : mod->build_id_len = 0;
232 : }
233 0 : else if (fallback)
234 0 : mod_verify_build_id (mod);
235 :
236 5051 : mod->main_bias = mod->e_type == ET_REL ? 0 : mod->low_addr - mod->main.vaddr;
237 : }
238 :
239 : static inline void
240 : consider_shdr (GElf_Addr interp,
241 : GElf_Word sh_type,
242 : GElf_Xword sh_flags,
243 : GElf_Addr sh_addr,
244 : GElf_Xword sh_size,
245 : GElf_Addr *phighest)
246 : {
247 640 : if ((sh_flags & SHF_ALLOC)
248 548 : && ((sh_type == SHT_PROGBITS && sh_addr != interp)
249 288 : || sh_type == SHT_NOBITS))
250 : {
251 280 : const GElf_Addr sh_end = sh_addr + sh_size;
252 280 : if (sh_end > *phighest)
253 280 : *phighest = sh_end;
254 : }
255 : }
256 :
257 : /* If the main file might have been prelinked, then we need to
258 : discover the correct synchronization address between the main and
259 : debug files. Because of prelink's section juggling, we cannot rely
260 : on the address_sync computed from PT_LOAD segments (see open_elf).
261 :
262 : We will attempt to discover a synchronization address based on the
263 : section headers instead. But finding a section address that is
264 : safe to use requires identifying which sections are SHT_PROGBITS.
265 : We can do that in the main file, but in the debug file all the
266 : allocated sections have been transformed into SHT_NOBITS so we have
267 : lost the means to match them up correctly.
268 :
269 : The only method left to us is to decode the .gnu.prelink_undo
270 : section in the prelinked main file. This shows what the sections
271 : looked like before prelink juggled them--when they still had a
272 : direct correspondence to the debug file. */
273 : static Dwfl_Error
274 44 : find_prelink_address_sync (Dwfl_Module *mod, struct dwfl_file *file)
275 : {
276 : /* The magic section is only identified by name. */
277 : size_t shstrndx;
278 44 : if (elf_getshdrstrndx (mod->main.elf, &shstrndx) < 0)
279 : return DWFL_E_LIBELF;
280 :
281 : Elf_Scn *scn = NULL;
282 1428 : while ((scn = elf_nextscn (mod->main.elf, scn)) != NULL)
283 : {
284 : GElf_Shdr shdr_mem;
285 1396 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
286 1396 : if (unlikely (shdr == NULL))
287 0 : return DWFL_E_LIBELF;
288 1396 : if (shdr->sh_type == SHT_PROGBITS
289 846 : && !(shdr->sh_flags & SHF_ALLOC)
290 314 : && shdr->sh_name != 0)
291 : {
292 314 : const char *secname = elf_strptr (mod->main.elf, shstrndx,
293 : shdr->sh_name);
294 314 : if (unlikely (secname == NULL))
295 : return DWFL_E_LIBELF;
296 314 : if (!strcmp (secname, ".gnu.prelink_undo"))
297 : break;
298 : }
299 : }
300 :
301 44 : if (scn == NULL)
302 : /* There was no .gnu.prelink_undo section. */
303 : return DWFL_E_NOERROR;
304 :
305 12 : Elf_Data *undodata = elf_rawdata (scn, NULL);
306 12 : if (unlikely (undodata == NULL))
307 : return DWFL_E_LIBELF;
308 :
309 : /* Decode the section. It consists of the original ehdr, phdrs,
310 : and shdrs (but omits section 0). */
311 :
312 : union
313 : {
314 : Elf32_Ehdr e32;
315 : Elf64_Ehdr e64;
316 : } ehdr;
317 12 : Elf_Data dst =
318 : {
319 : .d_buf = &ehdr,
320 : .d_size = sizeof ehdr,
321 : .d_type = ELF_T_EHDR,
322 : .d_version = EV_CURRENT
323 : };
324 12 : Elf_Data src = *undodata;
325 12 : src.d_size = gelf_fsize (mod->main.elf, ELF_T_EHDR, 1, EV_CURRENT);
326 12 : src.d_type = ELF_T_EHDR;
327 12 : if (unlikely (gelf_xlatetom (mod->main.elf, &dst, &src,
328 : elf_getident (mod->main.elf, NULL)[EI_DATA])
329 : == NULL))
330 : return DWFL_E_LIBELF;
331 :
332 12 : size_t shentsize = gelf_fsize (mod->main.elf, ELF_T_SHDR, 1, EV_CURRENT);
333 12 : size_t phentsize = gelf_fsize (mod->main.elf, ELF_T_PHDR, 1, EV_CURRENT);
334 :
335 : uint_fast16_t phnum;
336 : uint_fast16_t shnum;
337 12 : if (ehdr.e32.e_ident[EI_CLASS] == ELFCLASS32)
338 : {
339 4 : if (ehdr.e32.e_shentsize != shentsize
340 4 : || ehdr.e32.e_phentsize != phentsize)
341 : return DWFL_E_BAD_PRELINK;
342 4 : phnum = ehdr.e32.e_phnum;
343 4 : shnum = ehdr.e32.e_shnum;
344 : }
345 : else
346 : {
347 8 : if (ehdr.e64.e_shentsize != shentsize
348 8 : || ehdr.e64.e_phentsize != phentsize)
349 : return DWFL_E_BAD_PRELINK;
350 8 : phnum = ehdr.e64.e_phnum;
351 8 : shnum = ehdr.e64.e_shnum;
352 : }
353 :
354 : /* Since prelink does not store the zeroth section header in the undo
355 : section, it cannot support SHN_XINDEX encoding. */
356 12 : if (unlikely (shnum >= SHN_LORESERVE) || unlikely(shnum == 0)
357 12 : || unlikely (undodata->d_size != (src.d_size
358 : + phnum * phentsize
359 : + (shnum - 1) * shentsize)))
360 : return DWFL_E_BAD_PRELINK;
361 :
362 12 : --shnum;
363 :
364 : /* We look at the allocated SHT_PROGBITS (or SHT_NOBITS) sections. (Most
365 : every file will have some SHT_PROGBITS sections, but it's possible to
366 : have one with nothing but .bss, i.e. SHT_NOBITS.) The special sections
367 : that can be moved around have different sh_type values--except for
368 : .interp, the section that became the PT_INTERP segment. So we exclude
369 : the SHT_PROGBITS section whose address matches the PT_INTERP p_vaddr.
370 : For this reason, we must examine the phdrs first to find PT_INTERP. */
371 :
372 12 : GElf_Addr main_interp = 0;
373 : {
374 : size_t main_phnum;
375 12 : if (unlikely (elf_getphdrnum (mod->main.elf, &main_phnum)))
376 0 : return DWFL_E_LIBELF;
377 29 : for (size_t i = 0; i < main_phnum; ++i)
378 : {
379 : GElf_Phdr phdr;
380 37 : if (unlikely (gelf_getphdr (mod->main.elf, i, &phdr) == NULL))
381 0 : return DWFL_E_LIBELF;
382 37 : if (phdr.p_type == PT_INTERP)
383 : {
384 8 : main_interp = phdr.p_vaddr;
385 8 : break;
386 : }
387 : }
388 : }
389 :
390 12 : src.d_buf += src.d_size;
391 12 : src.d_type = ELF_T_PHDR;
392 12 : src.d_size = phnum * phentsize;
393 :
394 12 : GElf_Addr undo_interp = 0;
395 12 : bool class32 = ehdr.e32.e_ident[EI_CLASS] == ELFCLASS32;
396 : {
397 24 : size_t phdr_size = class32 ? sizeof (Elf32_Phdr) : sizeof (Elf64_Phdr);
398 : if (unlikely (phnum > SIZE_MAX / phdr_size))
399 : return DWFL_E_NOMEM;
400 12 : const size_t phdrs_bytes = phnum * phdr_size;
401 12 : void *phdrs = malloc (phdrs_bytes);
402 12 : if (unlikely (phdrs == NULL))
403 : return DWFL_E_NOMEM;
404 12 : dst.d_buf = phdrs;
405 12 : dst.d_size = phdrs_bytes;
406 12 : if (unlikely (gelf_xlatetom (mod->main.elf, &dst, &src,
407 : ehdr.e32.e_ident[EI_DATA]) == NULL))
408 : {
409 0 : free (phdrs);
410 : return DWFL_E_LIBELF;
411 : }
412 12 : if (class32)
413 : {
414 : Elf32_Phdr (*p32)[phnum] = phdrs;
415 12 : for (uint_fast16_t i = 0; i < phnum; ++i)
416 14 : if ((*p32)[i].p_type == PT_INTERP)
417 : {
418 2 : undo_interp = (*p32)[i].p_vaddr;
419 : break;
420 : }
421 : }
422 : else
423 : {
424 : Elf64_Phdr (*p64)[phnum] = phdrs;
425 17 : for (uint_fast16_t i = 0; i < phnum; ++i)
426 23 : if ((*p64)[i].p_type == PT_INTERP)
427 : {
428 6 : undo_interp = (*p64)[i].p_vaddr;
429 : break;
430 : }
431 : }
432 12 : free (phdrs);
433 : }
434 :
435 12 : if (unlikely ((main_interp == 0) != (undo_interp == 0)))
436 : return DWFL_E_BAD_PRELINK;
437 :
438 12 : src.d_buf += src.d_size;
439 12 : src.d_type = ELF_T_SHDR;
440 12 : src.d_size = gelf_fsize (mod->main.elf, ELF_T_SHDR, shnum, EV_CURRENT);
441 :
442 12 : size_t shdr_size = class32 ? sizeof (Elf32_Shdr) : sizeof (Elf64_Shdr);
443 12 : if (unlikely (shnum > SIZE_MAX / shdr_size))
444 : return DWFL_E_NOMEM;
445 12 : const size_t shdrs_bytes = shnum * shdr_size;
446 12 : void *shdrs = malloc (shdrs_bytes);
447 12 : if (unlikely (shdrs == NULL))
448 : return DWFL_E_NOMEM;
449 12 : dst.d_buf = shdrs;
450 12 : dst.d_size = shdrs_bytes;
451 12 : if (unlikely (gelf_xlatetom (mod->main.elf, &dst, &src,
452 : ehdr.e32.e_ident[EI_DATA]) == NULL))
453 : {
454 0 : free (shdrs);
455 : return DWFL_E_LIBELF;
456 : }
457 :
458 : /* Now we can look at the original section headers of the main file
459 : before it was prelinked. First we'll apply our method to the main
460 : file sections as they are after prelinking, to calculate the
461 : synchronization address of the main file. Then we'll apply that
462 : same method to the saved section headers, to calculate the matching
463 : synchronization address of the debug file.
464 :
465 : The method is to consider SHF_ALLOC sections that are either
466 : SHT_PROGBITS or SHT_NOBITS, excluding the section whose sh_addr
467 : matches the PT_INTERP p_vaddr. The special sections that can be
468 : moved by prelink have other types, except for .interp (which
469 : becomes PT_INTERP). The "real" sections cannot move as such, but
470 : .bss can be split into .dynbss and .bss, with the total memory
471 : image remaining the same but being spread across the two sections.
472 : So we consider the highest section end, which still matches up. */
473 :
474 : GElf_Addr highest;
475 :
476 : highest = 0;
477 : scn = NULL;
478 351 : while ((scn = elf_nextscn (mod->main.elf, scn)) != NULL)
479 : {
480 : GElf_Shdr sh_mem;
481 339 : GElf_Shdr *sh = gelf_getshdr (scn, &sh_mem);
482 339 : if (unlikely (sh == NULL))
483 : {
484 0 : free (shdrs);
485 0 : return DWFL_E_LIBELF;
486 : }
487 678 : consider_shdr (main_interp, sh->sh_type, sh->sh_flags,
488 : sh->sh_addr, sh->sh_size, &highest);
489 : }
490 12 : if (highest > mod->main.vaddr)
491 : {
492 12 : mod->main.address_sync = highest;
493 :
494 12 : highest = 0;
495 12 : if (class32)
496 : {
497 : Elf32_Shdr (*s32)[shnum] = shdrs;
498 94 : for (size_t i = 0; i < shnum; ++i)
499 376 : consider_shdr (undo_interp, (*s32)[i].sh_type,
500 188 : (*s32)[i].sh_flags, (*s32)[i].sh_addr,
501 94 : (*s32)[i].sh_size, &highest);
502 : }
503 : else
504 : {
505 : Elf64_Shdr (*s64)[shnum] = shdrs;
506 207 : for (size_t i = 0; i < shnum; ++i)
507 414 : consider_shdr (undo_interp, (*s64)[i].sh_type,
508 : (*s64)[i].sh_flags, (*s64)[i].sh_addr,
509 : (*s64)[i].sh_size, &highest);
510 : }
511 :
512 12 : if (highest > file->vaddr)
513 12 : file->address_sync = highest;
514 : else
515 : {
516 0 : free (shdrs);
517 : return DWFL_E_BAD_PRELINK;
518 : }
519 : }
520 :
521 12 : free (shdrs);
522 :
523 : return DWFL_E_NOERROR;
524 : }
525 :
526 : /* Find the separate debuginfo file for this module and open libelf on it.
527 : When we return success, MOD->debug is set up. */
528 : static Dwfl_Error
529 159 : find_debuginfo (Dwfl_Module *mod)
530 : {
531 159 : if (mod->debug.elf != NULL)
532 : return DWFL_E_NOERROR;
533 :
534 132 : GElf_Word debuglink_crc = 0;
535 : const char *debuglink_file;
536 132 : debuglink_file = INTUSE(dwelf_elf_gnu_debuglink) (mod->main.elf,
537 : &debuglink_crc);
538 :
539 264 : mod->debug.fd = (*mod->dwfl->callbacks->find_debuginfo) (MODCB_ARGS (mod),
540 132 : mod->main.name,
541 : debuglink_file,
542 : debuglink_crc,
543 : &mod->debug.name);
544 132 : Dwfl_Error result = open_elf (mod, &mod->debug);
545 132 : if (result == DWFL_E_NOERROR && mod->debug.address_sync != 0)
546 36 : result = find_prelink_address_sync (mod, &mod->debug);
547 : return result;
548 : }
549 :
550 : /* Try to find the alternative debug link for the given DWARF and set
551 : it if found. Only called when mod->dw is already setup but still
552 : might need an alternative (dwz multi) debug file. filename is either
553 : the main or debug name from which the Dwarf was created. */
554 : static void
555 5308 : find_debug_altlink (Dwfl_Module *mod, const char *filename)
556 : {
557 5308 : assert (mod->dw != NULL);
558 :
559 : const char *altname;
560 : const void *build_id;
561 5308 : ssize_t build_id_len = INTUSE(dwelf_dwarf_gnu_debugaltlink) (mod->dw,
562 : &altname,
563 : &build_id);
564 :
565 5308 : if (build_id_len > 0)
566 : {
567 : /* We could store altfile in the module, but don't really need it. */
568 5 : char *altfile = NULL;
569 5 : mod->alt_fd = (*mod->dwfl->callbacks->find_debuginfo) (MODCB_ARGS (mod),
570 : filename,
571 : altname,
572 : 0,
573 : &altfile);
574 :
575 : /* The (internal) callbacks might just set mod->alt_elf directly
576 : because they open the Elf anyway for sanity checking.
577 : Otherwise open either the given file name or use the fd
578 : returned. */
579 5 : Dwfl_Error error = open_elf_file (&mod->alt_elf, &mod->alt_fd,
580 : &altfile);
581 5 : if (error == DWFL_E_NOERROR)
582 : {
583 5 : mod->alt = INTUSE(dwarf_begin_elf) (mod->alt_elf,
584 : DWARF_C_READ, NULL);
585 5 : if (mod->alt == NULL)
586 : {
587 0 : elf_end (mod->alt_elf);
588 0 : mod->alt_elf = NULL;
589 0 : close (mod->alt_fd);
590 0 : mod->alt_fd = -1;
591 : }
592 : else
593 5 : dwarf_setalt (mod->dw, mod->alt);
594 : }
595 :
596 5 : free (altfile); /* See above, we don't really need it. */
597 : }
598 5308 : }
599 :
600 : /* Try to find a symbol table in FILE.
601 : Returns DWFL_E_NOERROR if a proper one is found.
602 : Returns DWFL_E_NO_SYMTAB if not, but still sets results for SHT_DYNSYM. */
603 : static Dwfl_Error
604 266 : load_symtab (struct dwfl_file *file, struct dwfl_file **symfile,
605 : Elf_Scn **symscn, Elf_Scn **xndxscn,
606 : size_t *syments, int *first_global, GElf_Word *strshndx)
607 : {
608 266 : bool symtab = false;
609 266 : Elf_Scn *scn = NULL;
610 9065 : while ((scn = elf_nextscn (file->elf, scn)) != NULL)
611 : {
612 8533 : GElf_Shdr shdr_mem, *shdr = gelf_getshdr (scn, &shdr_mem);
613 8533 : if (shdr != NULL)
614 8533 : switch (shdr->sh_type)
615 : {
616 205 : case SHT_SYMTAB:
617 205 : if (shdr->sh_entsize == 0)
618 : break;
619 205 : symtab = true;
620 205 : *symscn = scn;
621 205 : *symfile = file;
622 205 : *strshndx = shdr->sh_link;
623 205 : *syments = shdr->sh_size / shdr->sh_entsize;
624 205 : *first_global = shdr->sh_info;
625 205 : if (*xndxscn != NULL)
626 0 : return DWFL_E_NOERROR;
627 : break;
628 :
629 135 : case SHT_DYNSYM:
630 135 : if (symtab)
631 : break;
632 : /* Use this if need be, but keep looking for SHT_SYMTAB. */
633 135 : if (shdr->sh_entsize == 0)
634 : break;
635 135 : *symscn = scn;
636 135 : *symfile = file;
637 135 : *strshndx = shdr->sh_link;
638 135 : *syments = shdr->sh_size / shdr->sh_entsize;
639 135 : *first_global = shdr->sh_info;
640 135 : break;
641 :
642 0 : case SHT_SYMTAB_SHNDX:
643 0 : *xndxscn = scn;
644 0 : if (symtab)
645 : return DWFL_E_NOERROR;
646 : break;
647 :
648 : default:
649 : break;
650 : }
651 0 : }
652 :
653 266 : if (symtab)
654 : /* We found one, though no SHT_SYMTAB_SHNDX to go with it. */
655 : return DWFL_E_NOERROR;
656 :
657 : /* We found no SHT_SYMTAB, so any SHT_SYMTAB_SHNDX was bogus.
658 : We might have found an SHT_DYNSYM and set *SYMSCN et al though. */
659 61 : *xndxscn = NULL;
660 61 : return DWFL_E_NO_SYMTAB;
661 : }
662 :
663 :
664 : /* Translate addresses into file offsets.
665 : OFFS[*] start out zero and remain zero if unresolved. */
666 : static void
667 10 : find_offsets (Elf *elf, GElf_Addr main_bias, size_t phnum, size_t n,
668 : GElf_Addr addrs[n], GElf_Off offs[n])
669 : {
670 10 : size_t unsolved = n;
671 88 : for (size_t i = 0; i < phnum; ++i)
672 : {
673 : GElf_Phdr phdr_mem;
674 78 : GElf_Phdr *phdr = gelf_getphdr (elf, i, &phdr_mem);
675 78 : if (phdr != NULL && phdr->p_type == PT_LOAD && phdr->p_memsz > 0)
676 96 : for (size_t j = 0; j < n; ++j)
677 96 : if (offs[j] == 0
678 75 : && addrs[j] >= phdr->p_vaddr + main_bias
679 42 : && addrs[j] - (phdr->p_vaddr + main_bias) < phdr->p_filesz)
680 : {
681 18 : offs[j] = addrs[j] - (phdr->p_vaddr + main_bias) + phdr->p_offset;
682 18 : if (--unsolved == 0)
683 : break;
684 : }
685 : }
686 10 : }
687 :
688 : /* Various addresses we might want to pull from the dynamic segment. */
689 : enum
690 : {
691 : i_symtab,
692 : i_strtab,
693 : i_hash,
694 : i_gnu_hash,
695 : i_max
696 : };
697 :
698 : /* Translate pointers into file offsets. ADJUST is either zero
699 : in case the dynamic segment wasn't adjusted or mod->main_bias.
700 : Will set mod->symfile if the translated offsets can be used as
701 : symbol table. */
702 : static void
703 10 : translate_offs (GElf_Addr adjust,
704 : Dwfl_Module *mod, size_t phnum,
705 : GElf_Addr addrs[i_max], GElf_Xword strsz,
706 : GElf_Ehdr *ehdr)
707 : {
708 10 : GElf_Off offs[i_max] = { 0, };
709 10 : find_offsets (mod->main.elf, adjust, phnum, i_max, addrs, offs);
710 :
711 : /* Figure out the size of the symbol table. */
712 10 : if (offs[i_hash] != 0)
713 : {
714 : /* In the original format, .hash says the size of .dynsym. */
715 :
716 0 : size_t entsz = SH_ENTSIZE_HASH (ehdr);
717 0 : Elf_Data *data = elf_getdata_rawchunk (mod->main.elf,
718 0 : offs[i_hash] + entsz, entsz,
719 : (entsz == 4
720 : ? ELF_T_WORD : ELF_T_XWORD));
721 0 : if (data != NULL)
722 0 : mod->syments = (entsz == 4
723 0 : ? *(const GElf_Word *) data->d_buf
724 0 : : *(const GElf_Xword *) data->d_buf);
725 : }
726 10 : if (offs[i_gnu_hash] != 0 && mod->syments == 0)
727 : {
728 : /* In the new format, we can derive it with some work. */
729 :
730 : const struct
731 : {
732 : Elf32_Word nbuckets;
733 : Elf32_Word symndx;
734 : Elf32_Word maskwords;
735 : Elf32_Word shift2;
736 : } *header;
737 :
738 5 : Elf_Data *data = elf_getdata_rawchunk (mod->main.elf, offs[i_gnu_hash],
739 : sizeof *header, ELF_T_WORD);
740 5 : if (data != NULL)
741 : {
742 5 : header = data->d_buf;
743 5 : Elf32_Word nbuckets = header->nbuckets;
744 5 : Elf32_Word symndx = header->symndx;
745 10 : GElf_Off buckets_at = (offs[i_gnu_hash] + sizeof *header
746 10 : + (gelf_getclass (mod->main.elf)
747 : * sizeof (Elf32_Word)
748 5 : * header->maskwords));
749 :
750 : // elf_getdata_rawchunk takes a size_t, make sure it
751 : // doesn't overflow.
752 : #if SIZE_MAX <= UINT32_MAX
753 : if (nbuckets > SIZE_MAX / sizeof (Elf32_Word))
754 : data = NULL;
755 : else
756 : #endif
757 5 : data = elf_getdata_rawchunk (mod->main.elf, buckets_at,
758 : nbuckets * sizeof (Elf32_Word),
759 : ELF_T_WORD);
760 5 : if (data != NULL && symndx < nbuckets)
761 : {
762 1 : const Elf32_Word *const buckets = data->d_buf;
763 1 : Elf32_Word maxndx = symndx;
764 4 : for (Elf32_Word bucket = 0; bucket < nbuckets; ++bucket)
765 3 : if (buckets[bucket] > maxndx)
766 1 : maxndx = buckets[bucket];
767 :
768 1 : GElf_Off hasharr_at = (buckets_at
769 : + nbuckets * sizeof (Elf32_Word));
770 1 : hasharr_at += (maxndx - symndx) * sizeof (Elf32_Word);
771 : do
772 : {
773 1 : data = elf_getdata_rawchunk (mod->main.elf,
774 : hasharr_at,
775 : sizeof (Elf32_Word),
776 : ELF_T_WORD);
777 1 : if (data != NULL
778 1 : && (*(const Elf32_Word *) data->d_buf & 1u))
779 : {
780 1 : mod->syments = maxndx + 1;
781 : break;
782 : }
783 0 : ++maxndx;
784 0 : hasharr_at += sizeof (Elf32_Word);
785 : }
786 0 : while (data != NULL);
787 : }
788 : }
789 : }
790 10 : if (offs[i_strtab] > offs[i_symtab] && mod->syments == 0)
791 8 : mod->syments = ((offs[i_strtab] - offs[i_symtab])
792 4 : / gelf_fsize (mod->main.elf,
793 : ELF_T_SYM, 1, EV_CURRENT));
794 :
795 10 : if (mod->syments > 0)
796 : {
797 10 : mod->symdata = elf_getdata_rawchunk (mod->main.elf,
798 5 : offs[i_symtab],
799 : gelf_fsize (mod->main.elf,
800 : ELF_T_SYM,
801 : mod->syments,
802 : EV_CURRENT),
803 : ELF_T_SYM);
804 5 : if (mod->symdata != NULL)
805 : {
806 5 : mod->symstrdata = elf_getdata_rawchunk (mod->main.elf,
807 5 : offs[i_strtab],
808 : strsz,
809 : ELF_T_BYTE);
810 5 : if (mod->symstrdata == NULL)
811 0 : mod->symdata = NULL;
812 : }
813 5 : if (mod->symdata == NULL)
814 0 : mod->symerr = DWFL_E (LIBELF, elf_errno ());
815 : else
816 : {
817 5 : mod->symfile = &mod->main;
818 5 : mod->symerr = DWFL_E_NOERROR;
819 : }
820 : }
821 10 : }
822 :
823 : /* Try to find a dynamic symbol table via phdrs. */
824 : static void
825 6 : find_dynsym (Dwfl_Module *mod)
826 : {
827 : GElf_Ehdr ehdr_mem;
828 6 : GElf_Ehdr *ehdr = gelf_getehdr (mod->main.elf, &ehdr_mem);
829 :
830 : size_t phnum;
831 6 : if (unlikely (elf_getphdrnum (mod->main.elf, &phnum) != 0))
832 6 : return;
833 :
834 20 : for (size_t i = 0; i < phnum; ++i)
835 : {
836 : GElf_Phdr phdr_mem;
837 26 : GElf_Phdr *phdr = gelf_getphdr (mod->main.elf, i, &phdr_mem);
838 26 : if (phdr == NULL)
839 : break;
840 :
841 26 : if (phdr->p_type == PT_DYNAMIC)
842 : {
843 : /* Examine the dynamic section for the pointers we need. */
844 :
845 12 : Elf_Data *data = elf_getdata_rawchunk (mod->main.elf,
846 6 : phdr->p_offset, phdr->p_filesz,
847 : ELF_T_DYN);
848 6 : if (data == NULL)
849 0 : continue;
850 :
851 6 : GElf_Addr addrs[i_max] = { 0, };
852 6 : GElf_Xword strsz = 0;
853 6 : size_t n = data->d_size / gelf_fsize (mod->main.elf,
854 : ELF_T_DYN, 1, EV_CURRENT);
855 196 : for (size_t j = 0; j < n; ++j)
856 : {
857 : GElf_Dyn dyn_mem;
858 98 : GElf_Dyn *dyn = gelf_getdyn (data, j, &dyn_mem);
859 98 : if (dyn != NULL)
860 98 : switch (dyn->d_tag)
861 : {
862 5 : case DT_SYMTAB:
863 5 : addrs[i_symtab] = dyn->d_un.d_ptr;
864 97 : continue;
865 :
866 0 : case DT_HASH:
867 0 : addrs[i_hash] = dyn->d_un.d_ptr;
868 0 : continue;
869 :
870 5 : case DT_GNU_HASH:
871 5 : addrs[i_gnu_hash] = dyn->d_un.d_ptr;
872 5 : continue;
873 :
874 5 : case DT_STRTAB:
875 5 : addrs[i_strtab] = dyn->d_un.d_ptr;
876 5 : continue;
877 :
878 5 : case DT_STRSZ:
879 5 : strsz = dyn->d_un.d_val;
880 5 : continue;
881 :
882 72 : default:
883 72 : continue;
884 :
885 : case DT_NULL:
886 : break;
887 : }
888 0 : break;
889 : }
890 :
891 : /* First try unadjusted, like ELF files from disk, vdso.
892 : Then try for already adjusted dynamic section, like ELF
893 : from remote memory. */
894 6 : translate_offs (0, mod, phnum, addrs, strsz, ehdr);
895 6 : if (mod->symfile == NULL)
896 4 : translate_offs (mod->main_bias, mod, phnum, addrs, strsz, ehdr);
897 :
898 6 : return;
899 : }
900 : }
901 : }
902 :
903 :
904 : #if USE_LZMA
905 : /* Try to find the offset between the main file and .gnu_debugdata. */
906 : static bool
907 8 : find_aux_address_sync (Dwfl_Module *mod)
908 : {
909 : /* Don't trust the phdrs in the minisymtab elf file to be setup correctly.
910 : The address_sync is equal to the main file it is embedded in at first. */
911 8 : mod->aux_sym.address_sync = mod->main.address_sync;
912 :
913 : /* Adjust address_sync for the difference in entry addresses, attempting to
914 : account for ELF relocation changes after aux was split. */
915 : GElf_Ehdr ehdr_main, ehdr_aux;
916 8 : if (unlikely (gelf_getehdr (mod->main.elf, &ehdr_main) == NULL)
917 8 : || unlikely (gelf_getehdr (mod->aux_sym.elf, &ehdr_aux) == NULL))
918 : return false;
919 8 : mod->aux_sym.address_sync += ehdr_aux.e_entry - ehdr_main.e_entry;
920 :
921 : /* The shdrs are setup OK to make find_prelink_address_sync () do the right
922 : thing, which is possibly more reliable, but it needs .gnu.prelink_undo. */
923 8 : if (mod->aux_sym.address_sync != 0)
924 8 : return find_prelink_address_sync (mod, &mod->aux_sym) == DWFL_E_NOERROR;
925 :
926 : return true;
927 : }
928 : #endif
929 :
930 : /* Try to find the auxiliary symbol table embedded in the main elf file
931 : section .gnu_debugdata. Only matters if the symbol information comes
932 : from the main file dynsym. No harm done if not found. */
933 : static void
934 29 : find_aux_sym (Dwfl_Module *mod __attribute__ ((unused)),
935 : Elf_Scn **aux_symscn __attribute__ ((unused)),
936 : Elf_Scn **aux_xndxscn __attribute__ ((unused)),
937 : GElf_Word *aux_strshndx __attribute__ ((unused)))
938 : {
939 : /* Since a .gnu_debugdata section is compressed using lzma don't do
940 : anything unless we have support for that. */
941 : #if USE_LZMA
942 29 : Elf *elf = mod->main.elf;
943 :
944 : size_t shstrndx;
945 29 : if (elf_getshdrstrndx (elf, &shstrndx) < 0)
946 29 : return;
947 :
948 : Elf_Scn *scn = NULL;
949 634 : while ((scn = elf_nextscn (elf, scn)) != NULL)
950 : {
951 : GElf_Shdr shdr_mem;
952 613 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
953 613 : if (shdr == NULL)
954 0 : return;
955 :
956 613 : const char *name = elf_strptr (elf, shstrndx, shdr->sh_name);
957 613 : if (name == NULL)
958 : return;
959 :
960 613 : if (!strcmp (name, ".gnu_debugdata"))
961 : break;
962 : }
963 :
964 29 : if (scn == NULL)
965 : return;
966 :
967 : /* Found the .gnu_debugdata section. Uncompress the lzma image and
968 : turn it into an ELF image. */
969 8 : Elf_Data *rawdata = elf_rawdata (scn, NULL);
970 8 : if (rawdata == NULL)
971 : return;
972 :
973 : Dwfl_Error error;
974 8 : void *buffer = NULL;
975 8 : size_t size = 0;
976 8 : error = __libdw_unlzma (-1, 0, rawdata->d_buf, rawdata->d_size,
977 : &buffer, &size);
978 8 : if (error == DWFL_E_NOERROR)
979 : {
980 8 : if (unlikely (size == 0))
981 0 : free (buffer);
982 : else
983 : {
984 8 : mod->aux_sym.elf = elf_memory (buffer, size);
985 8 : if (mod->aux_sym.elf == NULL)
986 0 : free (buffer);
987 : else
988 : {
989 8 : mod->aux_sym.fd = -1;
990 8 : mod->aux_sym.elf->flags |= ELF_F_MALLOCED;
991 8 : if (open_elf (mod, &mod->aux_sym) != DWFL_E_NOERROR)
992 : return;
993 8 : if (! find_aux_address_sync (mod))
994 : {
995 0 : elf_end (mod->aux_sym.elf);
996 0 : mod->aux_sym.elf = NULL;
997 0 : return;
998 : }
999 :
1000 : /* So far, so good. Get minisymtab table data and cache it. */
1001 : bool minisymtab = false;
1002 : scn = NULL;
1003 217 : while ((scn = elf_nextscn (mod->aux_sym.elf, scn)) != NULL)
1004 : {
1005 209 : GElf_Shdr shdr_mem, *shdr = gelf_getshdr (scn, &shdr_mem);
1006 209 : if (shdr != NULL)
1007 209 : switch (shdr->sh_type)
1008 : {
1009 8 : case SHT_SYMTAB:
1010 8 : if (shdr->sh_entsize == 0)
1011 0 : return;
1012 8 : minisymtab = true;
1013 8 : *aux_symscn = scn;
1014 8 : *aux_strshndx = shdr->sh_link;
1015 8 : mod->aux_syments = shdr->sh_size / shdr->sh_entsize;
1016 8 : mod->aux_first_global = shdr->sh_info;
1017 8 : if (*aux_xndxscn != NULL)
1018 : return;
1019 : break;
1020 :
1021 0 : case SHT_SYMTAB_SHNDX:
1022 0 : *aux_xndxscn = scn;
1023 0 : if (minisymtab)
1024 : return;
1025 : break;
1026 :
1027 : default:
1028 : break;
1029 : }
1030 0 : }
1031 :
1032 8 : if (minisymtab)
1033 : /* We found one, though no SHT_SYMTAB_SHNDX to go with it. */
1034 : return;
1035 :
1036 : /* We found no SHT_SYMTAB, so everything else is bogus. */
1037 0 : *aux_xndxscn = NULL;
1038 0 : *aux_strshndx = 0;
1039 0 : mod->aux_syments = 0;
1040 0 : elf_end (mod->aux_sym.elf);
1041 0 : mod->aux_sym.elf = NULL;
1042 0 : return;
1043 : }
1044 : }
1045 : }
1046 : else
1047 0 : free (buffer);
1048 : #endif
1049 : }
1050 :
1051 : /* Try to find a symbol table in either MOD->main.elf or MOD->debug.elf. */
1052 : static void
1053 1821941 : find_symtab (Dwfl_Module *mod)
1054 : {
1055 1821941 : if (mod->symdata != NULL || mod->aux_symdata != NULL /* Already done. */
1056 244 : || mod->symerr != DWFL_E_NOERROR) /* Cached previous failure. */
1057 1821713 : return;
1058 :
1059 237 : __libdwfl_getelf (mod);
1060 237 : mod->symerr = mod->elferr;
1061 237 : if (mod->symerr != DWFL_E_NOERROR)
1062 : return;
1063 :
1064 : /* First see if the main ELF file has the debugging information. */
1065 234 : Elf_Scn *symscn = NULL, *xndxscn = NULL;
1066 234 : Elf_Scn *aux_symscn = NULL, *aux_xndxscn = NULL;
1067 234 : GElf_Word strshndx, aux_strshndx = 0;
1068 234 : mod->symerr = load_symtab (&mod->main, &mod->symfile, &symscn,
1069 : &xndxscn, &mod->syments, &mod->first_global,
1070 : &strshndx);
1071 234 : switch (mod->symerr)
1072 : {
1073 : default:
1074 : return;
1075 :
1076 : case DWFL_E_NOERROR:
1077 : break;
1078 :
1079 60 : case DWFL_E_NO_SYMTAB:
1080 : /* Now we have to look for a separate debuginfo file. */
1081 60 : mod->symerr = find_debuginfo (mod);
1082 60 : switch (mod->symerr)
1083 : {
1084 : default:
1085 : return;
1086 :
1087 32 : case DWFL_E_NOERROR:
1088 32 : mod->symerr = load_symtab (&mod->debug, &mod->symfile, &symscn,
1089 : &xndxscn, &mod->syments,
1090 : &mod->first_global, &strshndx);
1091 32 : break;
1092 :
1093 28 : case DWFL_E_CB: /* The find_debuginfo hook failed. */
1094 28 : mod->symerr = DWFL_E_NO_SYMTAB;
1095 28 : break;
1096 : }
1097 :
1098 60 : switch (mod->symerr)
1099 : {
1100 : default:
1101 : return;
1102 :
1103 : case DWFL_E_NOERROR:
1104 : break;
1105 :
1106 29 : case DWFL_E_NO_SYMTAB:
1107 : /* There might be an auxiliary table. */
1108 29 : find_aux_sym (mod, &aux_symscn, &aux_xndxscn, &aux_strshndx);
1109 :
1110 29 : if (symscn != NULL)
1111 : {
1112 : /* We still have the dynamic symbol table. */
1113 22 : mod->symerr = DWFL_E_NOERROR;
1114 22 : break;
1115 : }
1116 :
1117 7 : if (aux_symscn != NULL)
1118 : {
1119 : /* We still have the auxiliary symbol table. */
1120 1 : mod->symerr = DWFL_E_NOERROR;
1121 1 : goto aux_cache;
1122 : }
1123 :
1124 : /* Last ditch, look for dynamic symbols without section headers. */
1125 6 : find_dynsym (mod);
1126 6 : return;
1127 : }
1128 : break;
1129 : }
1130 :
1131 : /* This does some sanity checks on the string table section. */
1132 227 : if (elf_strptr (mod->symfile->elf, strshndx, 0) == NULL)
1133 : {
1134 0 : elferr:
1135 0 : mod->symdata = NULL;
1136 0 : mod->syments = 0;
1137 0 : mod->first_global = 0;
1138 0 : mod->symerr = DWFL_E (LIBELF, elf_errno ());
1139 0 : goto aux_cleanup; /* This cleans up some more and tries find_dynsym. */
1140 : }
1141 :
1142 : /* Cache the data; MOD->syments and MOD->first_global were set
1143 : above. If any of the sections is compressed, uncompress it
1144 : first. Only the string data setion could theoretically be
1145 : compressed GNU style (as .zdebug_str). Everything else only ELF
1146 : gabi style (SHF_COMPRESSED). */
1147 :
1148 227 : Elf_Scn *symstrscn = elf_getscn (mod->symfile->elf, strshndx);
1149 227 : if (symstrscn == NULL)
1150 : goto elferr;
1151 :
1152 : GElf_Shdr shdr_mem;
1153 227 : GElf_Shdr *shdr = gelf_getshdr (symstrscn, &shdr_mem);
1154 227 : if (shdr == NULL)
1155 : goto elferr;
1156 :
1157 : size_t shstrndx;
1158 227 : if (elf_getshdrstrndx (mod->symfile->elf, &shstrndx) < 0)
1159 : goto elferr;
1160 :
1161 227 : const char *sname = elf_strptr (mod->symfile->elf, shstrndx, shdr->sh_name);
1162 227 : if (sname == NULL)
1163 : goto elferr;
1164 :
1165 227 : if (strncmp (sname, ".zdebug", strlen (".zdebug")) == 0)
1166 : /* Try to uncompress, but it might already have been, an error
1167 : might just indicate, already uncompressed. */
1168 0 : elf_compress_gnu (symstrscn, 0, 0);
1169 :
1170 227 : if ((shdr->sh_flags & SHF_COMPRESSED) != 0)
1171 0 : if (elf_compress (symstrscn, 0, 0) < 0)
1172 : goto elferr;
1173 :
1174 227 : mod->symstrdata = elf_getdata (symstrscn, NULL);
1175 454 : if (mod->symstrdata == NULL || mod->symstrdata->d_buf == NULL)
1176 : goto elferr;
1177 :
1178 227 : if (xndxscn == NULL)
1179 227 : mod->symxndxdata = NULL;
1180 : else
1181 : {
1182 0 : shdr = gelf_getshdr (xndxscn, &shdr_mem);
1183 0 : if (shdr == NULL)
1184 : goto elferr;
1185 :
1186 0 : if ((shdr->sh_flags & SHF_COMPRESSED) != 0)
1187 0 : if (elf_compress (xndxscn, 0, 0) < 0)
1188 : goto elferr;
1189 :
1190 0 : mod->symxndxdata = elf_getdata (xndxscn, NULL);
1191 0 : if (mod->symxndxdata == NULL || mod->symxndxdata->d_buf == NULL)
1192 : goto elferr;
1193 : }
1194 :
1195 227 : shdr = gelf_getshdr (symscn, &shdr_mem);
1196 227 : if (shdr == NULL)
1197 : goto elferr;
1198 :
1199 227 : if ((shdr->sh_flags & SHF_COMPRESSED) != 0)
1200 0 : if (elf_compress (symscn, 0, 0) < 0)
1201 : goto elferr;
1202 :
1203 227 : mod->symdata = elf_getdata (symscn, NULL);
1204 454 : if (mod->symdata == NULL || mod->symdata->d_buf == NULL)
1205 : goto elferr;
1206 :
1207 : // Sanity check number of symbols.
1208 227 : shdr = gelf_getshdr (symscn, &shdr_mem);
1209 454 : if (shdr == NULL || shdr->sh_entsize == 0
1210 227 : || mod->syments > mod->symdata->d_size / shdr->sh_entsize
1211 227 : || (size_t) mod->first_global > mod->syments)
1212 : goto elferr;
1213 :
1214 : /* Cache any auxiliary symbol info, when it fails, just ignore aux_sym. */
1215 227 : if (aux_symscn != NULL)
1216 : {
1217 7 : aux_cache:
1218 : /* This does some sanity checks on the string table section. */
1219 8 : if (elf_strptr (mod->aux_sym.elf, aux_strshndx, 0) == NULL)
1220 : {
1221 0 : aux_cleanup:
1222 0 : mod->aux_syments = 0;
1223 0 : elf_end (mod->aux_sym.elf);
1224 0 : mod->aux_sym.elf = NULL;
1225 : /* We thought we had something through shdrs, but it failed...
1226 : Last ditch, look for dynamic symbols without section headers. */
1227 0 : find_dynsym (mod);
1228 0 : return;
1229 : }
1230 :
1231 8 : Elf_Scn *aux_strscn = elf_getscn (mod->aux_sym.elf, aux_strshndx);
1232 8 : if (aux_strscn == NULL)
1233 : goto elferr;
1234 :
1235 8 : shdr = gelf_getshdr (aux_strscn, &shdr_mem);
1236 8 : if (shdr == NULL)
1237 : goto elferr;
1238 :
1239 : size_t aux_shstrndx;
1240 8 : if (elf_getshdrstrndx (mod->aux_sym.elf, &aux_shstrndx) < 0)
1241 : goto elferr;
1242 :
1243 8 : sname = elf_strptr (mod->aux_sym.elf, aux_shstrndx,
1244 8 : shdr->sh_name);
1245 8 : if (sname == NULL)
1246 : goto elferr;
1247 :
1248 8 : if (strncmp (sname, ".zdebug", strlen (".zdebug")) == 0)
1249 : /* Try to uncompress, but it might already have been, an error
1250 : might just indicate, already uncompressed. */
1251 0 : elf_compress_gnu (aux_strscn, 0, 0);
1252 :
1253 8 : if ((shdr->sh_flags & SHF_COMPRESSED) != 0)
1254 0 : if (elf_compress (aux_strscn, 0, 0) < 0)
1255 : goto elferr;
1256 :
1257 8 : mod->aux_symstrdata = elf_getdata (aux_strscn, NULL);
1258 16 : if (mod->aux_symstrdata == NULL || mod->aux_symstrdata->d_buf == NULL)
1259 : goto aux_cleanup;
1260 :
1261 8 : if (aux_xndxscn == NULL)
1262 8 : mod->aux_symxndxdata = NULL;
1263 : else
1264 : {
1265 0 : shdr = gelf_getshdr (aux_xndxscn, &shdr_mem);
1266 0 : if (shdr == NULL)
1267 : goto elferr;
1268 :
1269 0 : if ((shdr->sh_flags & SHF_COMPRESSED) != 0)
1270 0 : if (elf_compress (aux_xndxscn, 0, 0) < 0)
1271 : goto elferr;
1272 :
1273 0 : mod->aux_symxndxdata = elf_getdata (aux_xndxscn, NULL);
1274 0 : if (mod->aux_symxndxdata == NULL
1275 0 : || mod->aux_symxndxdata->d_buf == NULL)
1276 : goto aux_cleanup;
1277 : }
1278 :
1279 8 : shdr = gelf_getshdr (aux_symscn, &shdr_mem);
1280 8 : if (shdr == NULL)
1281 : goto elferr;
1282 :
1283 8 : if ((shdr->sh_flags & SHF_COMPRESSED) != 0)
1284 0 : if (elf_compress (aux_symscn, 0, 0) < 0)
1285 : goto elferr;
1286 :
1287 8 : mod->aux_symdata = elf_getdata (aux_symscn, NULL);
1288 16 : if (mod->aux_symdata == NULL || mod->aux_symdata->d_buf == NULL)
1289 : goto aux_cleanup;
1290 :
1291 : // Sanity check number of aux symbols.
1292 8 : shdr = gelf_getshdr (aux_symscn, &shdr_mem);
1293 8 : if (mod->aux_syments > mod->aux_symdata->d_size / shdr->sh_entsize
1294 8 : || (size_t) mod->aux_first_global > mod->aux_syments)
1295 : goto aux_cleanup;
1296 : }
1297 : }
1298 :
1299 :
1300 : /* Try to open a libebl backend for MOD. */
1301 : Dwfl_Error
1302 : internal_function
1303 366298610 : __libdwfl_module_getebl (Dwfl_Module *mod)
1304 : {
1305 366298610 : if (mod->ebl == NULL)
1306 : {
1307 364 : __libdwfl_getelf (mod);
1308 364 : if (mod->elferr != DWFL_E_NOERROR)
1309 : return mod->elferr;
1310 :
1311 364 : mod->ebl = ebl_openbackend (mod->main.elf);
1312 364 : if (mod->ebl == NULL)
1313 : return DWFL_E_LIBEBL;
1314 : }
1315 : return DWFL_E_NOERROR;
1316 : }
1317 :
1318 : /* Try to start up libdw on DEBUGFILE. */
1319 : static Dwfl_Error
1320 5408 : load_dw (Dwfl_Module *mod, struct dwfl_file *debugfile)
1321 : {
1322 5408 : if (mod->e_type == ET_REL && !debugfile->relocated)
1323 : {
1324 34 : const Dwfl_Callbacks *const cb = mod->dwfl->callbacks;
1325 :
1326 : /* The debugging sections have to be relocated. */
1327 34 : if (cb->section_address == NULL)
1328 : return DWFL_E_NOREL;
1329 :
1330 34 : Dwfl_Error error = __libdwfl_module_getebl (mod);
1331 34 : if (error != DWFL_E_NOERROR)
1332 : return error;
1333 :
1334 34 : find_symtab (mod);
1335 34 : Dwfl_Error result = mod->symerr;
1336 34 : if (result == DWFL_E_NOERROR)
1337 34 : result = __libdwfl_relocate (mod, debugfile->elf, true);
1338 34 : if (result != DWFL_E_NOERROR)
1339 : return result;
1340 : }
1341 :
1342 5408 : mod->dw = INTUSE(dwarf_begin_elf) (debugfile->elf, DWARF_C_READ, NULL);
1343 5408 : if (mod->dw == NULL)
1344 : {
1345 100 : int err = INTUSE(dwarf_errno) ();
1346 100 : return err == DWARF_E_NO_DWARF ? DWFL_E_NO_DWARF : DWFL_E (LIBDW, err);
1347 : }
1348 :
1349 : /* Do this after dwarf_begin_elf has a chance to process the fd. */
1350 5308 : if (mod->e_type == ET_REL && !debugfile->relocated)
1351 : {
1352 : /* Don't keep the file descriptors around. */
1353 28 : if (mod->main.fd != -1 && elf_cntl (mod->main.elf, ELF_C_FDREAD) == 0)
1354 : {
1355 0 : close (mod->main.fd);
1356 0 : mod->main.fd = -1;
1357 : }
1358 28 : if (debugfile->fd != -1 && elf_cntl (debugfile->elf, ELF_C_FDREAD) == 0)
1359 : {
1360 1 : close (debugfile->fd);
1361 1 : debugfile->fd = -1;
1362 : }
1363 : }
1364 :
1365 : /* We might have already closed the fd when we asked dwarf_begin_elf to
1366 : create an Dwarf. Help out a little in case we need to find an alt or
1367 : dwo file later. */
1368 5308 : if (mod->dw->debugdir == NULL && mod->elfdir != NULL
1369 257 : && debugfile == &mod->main)
1370 257 : mod->dw->debugdir = strdup (mod->elfdir);
1371 :
1372 : /* Until we have iterated through all CU's, we might do lazy lookups. */
1373 5308 : mod->lazycu = 1;
1374 :
1375 5308 : return DWFL_E_NOERROR;
1376 : }
1377 :
1378 : /* Try to start up libdw on either the main file or the debuginfo file. */
1379 : static void
1380 10373 : find_dw (Dwfl_Module *mod)
1381 : {
1382 10373 : if (mod->dw != NULL /* Already done. */
1383 6469 : || mod->dwerr != DWFL_E_NOERROR) /* Cached previous failure. */
1384 : return;
1385 :
1386 5390 : __libdwfl_getelf (mod);
1387 5390 : mod->dwerr = mod->elferr;
1388 5390 : if (mod->dwerr != DWFL_E_NOERROR)
1389 : return;
1390 :
1391 : /* First see if the main ELF file has the debugging information. */
1392 5376 : mod->dwerr = load_dw (mod, &mod->main);
1393 5376 : switch (mod->dwerr)
1394 : {
1395 5276 : case DWFL_E_NOERROR:
1396 5276 : mod->debug.elf = mod->main.elf;
1397 5276 : mod->debug.address_sync = mod->main.address_sync;
1398 :
1399 : /* The Dwarf might need an alt debug file, find that now after
1400 : everything about the debug file has been setup (the
1401 : find_debuginfo callback might need it). */
1402 5276 : find_debug_altlink (mod, mod->main.name);
1403 5276 : return;
1404 :
1405 : case DWFL_E_NO_DWARF:
1406 : break;
1407 :
1408 : default:
1409 : goto canonicalize;
1410 : }
1411 :
1412 : /* Now we have to look for a separate debuginfo file. */
1413 99 : mod->dwerr = find_debuginfo (mod);
1414 99 : switch (mod->dwerr)
1415 : {
1416 32 : case DWFL_E_NOERROR:
1417 32 : mod->dwerr = load_dw (mod, &mod->debug);
1418 32 : if (mod->dwerr == DWFL_E_NOERROR)
1419 : {
1420 : /* The Dwarf might need an alt debug file, find that now after
1421 : everything about the debug file has been setup (the
1422 : find_debuginfo callback might need it). */
1423 32 : find_debug_altlink (mod, mod->debug.name);
1424 32 : return;
1425 : }
1426 :
1427 : break;
1428 :
1429 67 : case DWFL_E_CB: /* The find_debuginfo hook failed. */
1430 67 : mod->dwerr = DWFL_E_NO_DWARF;
1431 67 : return;
1432 :
1433 : default:
1434 : break;
1435 : }
1436 :
1437 1 : canonicalize:
1438 1 : mod->dwerr = __libdwfl_canon_error (mod->dwerr);
1439 : }
1440 :
1441 : Dwarf *
1442 10373 : dwfl_module_getdwarf (Dwfl_Module *mod, Dwarf_Addr *bias)
1443 : {
1444 10373 : if (mod == NULL)
1445 : return NULL;
1446 :
1447 10373 : find_dw (mod);
1448 10373 : if (mod->dwerr == DWFL_E_NOERROR)
1449 : {
1450 : /* If dwfl_module_getelf was used previously, then partial apply
1451 : relocation to miscellaneous sections in the debug file too. */
1452 9212 : if (mod->e_type == ET_REL
1453 90 : && mod->main.relocated && ! mod->debug.relocated)
1454 : {
1455 52 : mod->debug.relocated = true;
1456 52 : if (mod->debug.elf != mod->main.elf)
1457 0 : (void) __libdwfl_relocate (mod, mod->debug.elf, false);
1458 : }
1459 :
1460 9212 : *bias = dwfl_adjusted_dwarf_addr (mod, 0);
1461 9212 : return mod->dw;
1462 : }
1463 :
1464 1161 : __libdwfl_seterrno (mod->dwerr);
1465 1161 : return NULL;
1466 : }
1467 : INTDEF (dwfl_module_getdwarf)
1468 :
1469 : int
1470 1214228 : dwfl_module_getsymtab (Dwfl_Module *mod)
1471 : {
1472 1214228 : if (mod == NULL)
1473 : return -1;
1474 :
1475 1214228 : find_symtab (mod);
1476 1214228 : if (mod->symerr == DWFL_E_NOERROR)
1477 : /* We will skip the auxiliary zero entry if there is another one. */
1478 1214217 : return (mod->syments + mod->aux_syments
1479 1214217 : - (mod->syments > 0 && mod->aux_syments > 0 ? 1 : 0));
1480 :
1481 11 : __libdwfl_seterrno (mod->symerr);
1482 11 : return -1;
1483 : }
1484 : INTDEF (dwfl_module_getsymtab)
1485 :
1486 : int
1487 607679 : dwfl_module_getsymtab_first_global (Dwfl_Module *mod)
1488 : {
1489 607679 : if (mod == NULL)
1490 : return -1;
1491 :
1492 607679 : find_symtab (mod);
1493 607679 : if (mod->symerr == DWFL_E_NOERROR)
1494 : {
1495 : /* All local symbols should come before all global symbols. If
1496 : we have an auxiliary table make sure all the main locals come
1497 : first, then all aux locals, then all main globals and finally all
1498 : aux globals. And skip the auxiliary table zero undefined
1499 : entry. */
1500 607679 : int skip_aux_zero = (mod->syments > 0 && mod->aux_syments > 0) ? 1 : 0;
1501 607679 : return mod->first_global + mod->aux_first_global - skip_aux_zero;
1502 : }
1503 :
1504 0 : __libdwfl_seterrno (mod->symerr);
1505 0 : return -1;
1506 : }
1507 : INTDEF (dwfl_module_getsymtab_first_global)
|