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 5261 : open_elf_file (Elf **elf, int *fd, char **name)
44 : {
45 5261 : 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 5193 : 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 5193 : if (*fd < 0 && *name != NULL)
54 0 : *fd = TEMP_FAILURE_RETRY (open (*name, O_RDONLY));
55 :
56 5193 : if (*fd < 0)
57 146 : return CBFAIL;
58 :
59 5047 : return __libdw_open_file (fd, elf, true, false);
60 : }
61 68 : 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 5250 : open_elf (Dwfl_Module *mod, struct dwfl_file *file)
78 : {
79 5250 : Dwfl_Error error = open_elf_file (&file->elf, &file->fd, &file->name);
80 5250 : if (error != DWFL_E_NOERROR)
81 : return error;
82 :
83 5106 : GElf_Ehdr ehdr_mem, *ehdr = gelf_getehdr (file->elf, &ehdr_mem);
84 5106 : 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 5106 : 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 5105 : size_t phnum;
129 5105 : if (unlikely (elf_getphdrnum (file->elf, &phnum) != 0))
130 0 : goto elf_error;
131 :
132 5105 : file->vaddr = file->address_sync = 0;
133 15223 : for (size_t i = 0; i < phnum; ++i)
134 : {
135 15223 : GElf_Phdr ph_mem;
136 15223 : GElf_Phdr *ph = gelf_getphdr (file->elf, i, &ph_mem);
137 15223 : if (unlikely (ph == NULL))
138 0 : goto elf_error;
139 15223 : if (ph->p_type == PT_LOAD)
140 : {
141 5105 : file->vaddr = ph->p_vaddr & -ph->p_align;
142 5105 : file->address_sync = ph->p_vaddr + ph->p_memsz;
143 5105 : 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 5106 : 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 55 : 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 9006 : __libdwfl_getelf (Dwfl_Module *mod)
212 : {
213 9006 : 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 0 : if ((sh_flags & SHF_ALLOC)
248 0 : && ((sh_type == SHT_PROGBITS && sh_addr != interp)
249 0 : || sh_type == SHT_NOBITS))
250 : {
251 0 : const GElf_Addr sh_end = sh_addr + sh_size;
252 0 : if (sh_end > *phighest)
253 0 : *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 0 : find_prelink_address_sync (Dwfl_Module *mod, struct dwfl_file *file)
275 : {
276 : /* The magic section is only identified by name. */
277 0 : size_t shstrndx;
278 0 : if (elf_getshdrstrndx (mod->main.elf, &shstrndx) < 0)
279 0 : return DWFL_E_LIBELF;
280 :
281 : Elf_Scn *scn = NULL;
282 0 : while ((scn = elf_nextscn (mod->main.elf, scn)) != NULL)
283 : {
284 0 : GElf_Shdr shdr_mem;
285 0 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
286 0 : if (unlikely (shdr == NULL))
287 0 : return DWFL_E_LIBELF;
288 0 : if (shdr->sh_type == SHT_PROGBITS
289 0 : && !(shdr->sh_flags & SHF_ALLOC)
290 0 : && shdr->sh_name != 0)
291 : {
292 0 : const char *secname = elf_strptr (mod->main.elf, shstrndx,
293 : shdr->sh_name);
294 0 : if (unlikely (secname == NULL))
295 0 : return DWFL_E_LIBELF;
296 0 : if (!strcmp (secname, ".gnu.prelink_undo"))
297 : break;
298 : }
299 : }
300 :
301 0 : if (scn == NULL)
302 : /* There was no .gnu.prelink_undo section. */
303 0 : return DWFL_E_NOERROR;
304 :
305 0 : Elf_Data *undodata = elf_rawdata (scn, NULL);
306 0 : if (unlikely (undodata == NULL))
307 0 : 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 0 : union
313 : {
314 : Elf32_Ehdr e32;
315 : Elf64_Ehdr e64;
316 : } ehdr;
317 0 : 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 0 : Elf_Data src = *undodata;
325 0 : src.d_size = gelf_fsize (mod->main.elf, ELF_T_EHDR, 1, EV_CURRENT);
326 0 : src.d_type = ELF_T_EHDR;
327 0 : if (unlikely (gelf_xlatetom (mod->main.elf, &dst, &src,
328 : elf_getident (mod->main.elf, NULL)[EI_DATA])
329 : == NULL))
330 0 : return DWFL_E_LIBELF;
331 :
332 0 : size_t shentsize = gelf_fsize (mod->main.elf, ELF_T_SHDR, 1, EV_CURRENT);
333 0 : size_t phentsize = gelf_fsize (mod->main.elf, ELF_T_PHDR, 1, EV_CURRENT);
334 :
335 0 : uint_fast16_t phnum;
336 0 : uint_fast16_t shnum;
337 0 : if (ehdr.e32.e_ident[EI_CLASS] == ELFCLASS32)
338 : {
339 0 : if (ehdr.e32.e_shentsize != shentsize
340 0 : || ehdr.e32.e_phentsize != phentsize)
341 0 : return DWFL_E_BAD_PRELINK;
342 0 : phnum = ehdr.e32.e_phnum;
343 0 : shnum = ehdr.e32.e_shnum;
344 : }
345 : else
346 : {
347 0 : if (ehdr.e64.e_shentsize != shentsize
348 0 : || ehdr.e64.e_phentsize != phentsize)
349 0 : return DWFL_E_BAD_PRELINK;
350 0 : phnum = ehdr.e64.e_phnum;
351 0 : 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 0 : if (unlikely (shnum >= SHN_LORESERVE) || unlikely(shnum == 0)
357 0 : || unlikely (undodata->d_size != (src.d_size
358 : + phnum * phentsize
359 : + (shnum - 1) * shentsize)))
360 0 : return DWFL_E_BAD_PRELINK;
361 :
362 0 : --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 0 : GElf_Addr main_interp = 0;
373 : {
374 0 : size_t main_phnum;
375 0 : if (unlikely (elf_getphdrnum (mod->main.elf, &main_phnum)))
376 0 : return DWFL_E_LIBELF;
377 0 : for (size_t i = 0; i < main_phnum; ++i)
378 : {
379 0 : GElf_Phdr phdr;
380 0 : if (unlikely (gelf_getphdr (mod->main.elf, i, &phdr) == NULL))
381 0 : return DWFL_E_LIBELF;
382 0 : if (phdr.p_type == PT_INTERP)
383 : {
384 0 : main_interp = phdr.p_vaddr;
385 0 : break;
386 : }
387 : }
388 : }
389 :
390 0 : src.d_buf += src.d_size;
391 0 : src.d_type = ELF_T_PHDR;
392 0 : src.d_size = phnum * phentsize;
393 :
394 0 : GElf_Addr undo_interp = 0;
395 0 : bool class32 = ehdr.e32.e_ident[EI_CLASS] == ELFCLASS32;
396 : {
397 0 : size_t phdr_size = class32 ? sizeof (Elf32_Phdr) : sizeof (Elf64_Phdr);
398 0 : if (unlikely (phnum > SIZE_MAX / phdr_size))
399 : return DWFL_E_NOMEM;
400 0 : const size_t phdrs_bytes = phnum * phdr_size;
401 0 : void *phdrs = malloc (phdrs_bytes);
402 0 : if (unlikely (phdrs == NULL))
403 0 : return DWFL_E_NOMEM;
404 0 : dst.d_buf = phdrs;
405 0 : dst.d_size = phdrs_bytes;
406 0 : if (unlikely (gelf_xlatetom (mod->main.elf, &dst, &src,
407 : ehdr.e32.e_ident[EI_DATA]) == NULL))
408 : {
409 0 : free (phdrs);
410 0 : return DWFL_E_LIBELF;
411 : }
412 0 : if (class32)
413 : {
414 : Elf32_Phdr (*p32)[phnum] = phdrs;
415 0 : for (uint_fast16_t i = 0; i < phnum; ++i)
416 0 : if ((*p32)[i].p_type == PT_INTERP)
417 : {
418 0 : undo_interp = (*p32)[i].p_vaddr;
419 0 : break;
420 : }
421 : }
422 : else
423 : {
424 : Elf64_Phdr (*p64)[phnum] = phdrs;
425 0 : for (uint_fast16_t i = 0; i < phnum; ++i)
426 0 : if ((*p64)[i].p_type == PT_INTERP)
427 : {
428 0 : undo_interp = (*p64)[i].p_vaddr;
429 0 : break;
430 : }
431 : }
432 0 : free (phdrs);
433 : }
434 :
435 0 : if (unlikely ((main_interp == 0) != (undo_interp == 0)))
436 0 : return DWFL_E_BAD_PRELINK;
437 :
438 0 : src.d_buf += src.d_size;
439 0 : src.d_type = ELF_T_SHDR;
440 0 : src.d_size = gelf_fsize (mod->main.elf, ELF_T_SHDR, shnum, EV_CURRENT);
441 :
442 0 : size_t shdr_size = class32 ? sizeof (Elf32_Shdr) : sizeof (Elf64_Shdr);
443 0 : if (unlikely (shnum > SIZE_MAX / shdr_size))
444 0 : return DWFL_E_NOMEM;
445 0 : const size_t shdrs_bytes = shnum * shdr_size;
446 0 : void *shdrs = malloc (shdrs_bytes);
447 0 : if (unlikely (shdrs == NULL))
448 0 : return DWFL_E_NOMEM;
449 0 : dst.d_buf = shdrs;
450 0 : dst.d_size = shdrs_bytes;
451 0 : if (unlikely (gelf_xlatetom (mod->main.elf, &dst, &src,
452 : ehdr.e32.e_ident[EI_DATA]) == NULL))
453 : {
454 0 : free (shdrs);
455 0 : 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 0 : while ((scn = elf_nextscn (mod->main.elf, scn)) != NULL)
479 : {
480 0 : GElf_Shdr sh_mem;
481 0 : GElf_Shdr *sh = gelf_getshdr (scn, &sh_mem);
482 0 : if (unlikely (sh == NULL))
483 : {
484 0 : free (shdrs);
485 0 : return DWFL_E_LIBELF;
486 : }
487 0 : consider_shdr (main_interp, sh->sh_type, sh->sh_flags,
488 : sh->sh_addr, sh->sh_size, &highest);
489 : }
490 0 : if (highest > mod->main.vaddr)
491 : {
492 0 : mod->main.address_sync = highest;
493 :
494 0 : highest = 0;
495 0 : if (class32)
496 : {
497 : Elf32_Shdr (*s32)[shnum] = shdrs;
498 0 : for (size_t i = 0; i < shnum; ++i)
499 0 : consider_shdr (undo_interp, (*s32)[i].sh_type,
500 0 : (*s32)[i].sh_flags, (*s32)[i].sh_addr,
501 0 : (*s32)[i].sh_size, &highest);
502 : }
503 : else
504 : {
505 : Elf64_Shdr (*s64)[shnum] = shdrs;
506 0 : for (size_t i = 0; i < shnum; ++i)
507 0 : 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 0 : if (highest > file->vaddr)
513 0 : file->address_sync = highest;
514 : else
515 : {
516 0 : free (shdrs);
517 0 : return DWFL_E_BAD_PRELINK;
518 : }
519 : }
520 :
521 0 : free (shdrs);
522 :
523 0 : 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 194 : find_debuginfo (Dwfl_Module *mod)
530 : {
531 194 : if (mod->debug.elf != NULL)
532 : return DWFL_E_NOERROR;
533 :
534 167 : GElf_Word debuglink_crc = 0;
535 167 : const char *debuglink_file;
536 167 : debuglink_file = INTUSE(dwelf_elf_gnu_debuglink) (mod->main.elf,
537 : &debuglink_crc);
538 :
539 501 : mod->debug.fd = (*mod->dwfl->callbacks->find_debuginfo) (MODCB_ARGS (mod),
540 167 : mod->main.name,
541 : debuglink_file,
542 : debuglink_crc,
543 : &mod->debug.name);
544 167 : Dwfl_Error result = open_elf (mod, &mod->debug);
545 167 : if (result == DWFL_E_NOERROR && mod->debug.address_sync != 0)
546 38 : 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 5332 : find_debug_altlink (Dwfl_Module *mod, const char *filename)
556 : {
557 5332 : assert (mod->dw != NULL);
558 :
559 5332 : const char *altname;
560 5332 : const void *build_id;
561 5332 : ssize_t build_id_len = INTUSE(dwelf_dwarf_gnu_debugaltlink) (mod->dw,
562 : &altname,
563 : &build_id);
564 :
565 5332 : if (build_id_len > 0)
566 : {
567 : /* We could store altfile in the module, but don't really need it. */
568 11 : char *altfile = NULL;
569 11 : 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 11 : Dwfl_Error error = open_elf_file (&mod->alt_elf, &mod->alt_fd,
580 : &altfile);
581 11 : if (error == DWFL_E_NOERROR)
582 : {
583 9 : mod->alt = INTUSE(dwarf_begin_elf) (mod->alt_elf,
584 : DWARF_C_READ, NULL);
585 9 : 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 9 : dwarf_setalt (mod->dw, mod->alt);
594 : }
595 :
596 11 : free (altfile); /* See above, we don't really need it. */
597 : }
598 5332 : }
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 300 : 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 300 : bool symtab = false;
609 300 : Elf_Scn *scn = NULL;
610 9787 : while ((scn = elf_nextscn (file->elf, scn)) != NULL)
611 : {
612 9487 : GElf_Shdr shdr_mem, *shdr = gelf_getshdr (scn, &shdr_mem);
613 9487 : if (shdr != NULL)
614 9487 : switch (shdr->sh_type)
615 : {
616 225 : case SHT_SYMTAB:
617 225 : if (shdr->sh_entsize == 0)
618 : break;
619 225 : symtab = true;
620 225 : *symscn = scn;
621 225 : *symfile = file;
622 225 : *strshndx = shdr->sh_link;
623 225 : *syments = shdr->sh_size / shdr->sh_entsize;
624 225 : *first_global = shdr->sh_info;
625 225 : if (*xndxscn != NULL)
626 0 : return DWFL_E_NOERROR;
627 : break;
628 :
629 150 : case SHT_DYNSYM:
630 150 : if (symtab)
631 : break;
632 : /* Use this if need be, but keep looking for SHT_SYMTAB. */
633 150 : if (shdr->sh_entsize == 0)
634 : break;
635 150 : *symscn = scn;
636 150 : *symfile = file;
637 150 : *strshndx = shdr->sh_link;
638 150 : *syments = shdr->sh_size / shdr->sh_entsize;
639 150 : *first_global = shdr->sh_info;
640 150 : 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 300 : 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 75 : *xndxscn = NULL;
660 75 : 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 11 : 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 11 : size_t unsolved = n;
671 93 : for (size_t i = 0; i < phnum; ++i)
672 : {
673 82 : GElf_Phdr phdr_mem;
674 82 : GElf_Phdr *phdr = gelf_getphdr (elf, i, &phdr_mem);
675 82 : if (phdr != NULL && phdr->p_type == PT_LOAD && phdr->p_memsz > 0)
676 124 : for (size_t j = 0; j < n; ++j)
677 100 : if (offs[j] == 0
678 79 : && addrs[j] >= phdr->p_vaddr + main_bias
679 46 : && addrs[j] - (phdr->p_vaddr + main_bias) < phdr->p_filesz)
680 : {
681 22 : offs[j] = addrs[j] - (phdr->p_vaddr + main_bias) + phdr->p_offset;
682 22 : if (--unsolved == 0)
683 : break;
684 : }
685 : }
686 11 : }
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 0 : 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 0 : GElf_Off offs[i_max] = { 0, };
709 0 : find_offsets (mod->main.elf, adjust, phnum, i_max, addrs, offs);
710 :
711 : /* Figure out the size of the symbol table. */
712 0 : 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 0 : if (offs[i_gnu_hash] != 0 && mod->syments == 0)
727 : {
728 : /* In the new format, we can derive it with some work. */
729 :
730 0 : const struct
731 : {
732 : Elf32_Word nbuckets;
733 : Elf32_Word symndx;
734 : Elf32_Word maskwords;
735 : Elf32_Word shift2;
736 : } *header;
737 :
738 0 : Elf_Data *data = elf_getdata_rawchunk (mod->main.elf, offs[i_gnu_hash],
739 : sizeof *header, ELF_T_WORD);
740 0 : if (data != NULL)
741 : {
742 0 : header = data->d_buf;
743 0 : Elf32_Word nbuckets = header->nbuckets;
744 0 : Elf32_Word symndx = header->symndx;
745 0 : GElf_Off buckets_at = (offs[i_gnu_hash] + sizeof *header
746 0 : + (gelf_getclass (mod->main.elf)
747 : * sizeof (Elf32_Word)
748 0 : * 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 0 : data = elf_getdata_rawchunk (mod->main.elf, buckets_at,
758 : nbuckets * sizeof (Elf32_Word),
759 : ELF_T_WORD);
760 0 : if (data != NULL && symndx < nbuckets)
761 : {
762 0 : const Elf32_Word *const buckets = data->d_buf;
763 0 : Elf32_Word maxndx = symndx;
764 0 : for (Elf32_Word bucket = 0; bucket < nbuckets; ++bucket)
765 0 : if (buckets[bucket] > maxndx)
766 0 : maxndx = buckets[bucket];
767 :
768 0 : GElf_Off hasharr_at = (buckets_at
769 : + nbuckets * sizeof (Elf32_Word));
770 0 : hasharr_at += (maxndx - symndx) * sizeof (Elf32_Word);
771 0 : do
772 : {
773 0 : data = elf_getdata_rawchunk (mod->main.elf,
774 : hasharr_at,
775 : sizeof (Elf32_Word),
776 : ELF_T_WORD);
777 0 : if (data != NULL
778 0 : && (*(const Elf32_Word *) data->d_buf & 1u))
779 : {
780 0 : mod->syments = maxndx + 1;
781 0 : break;
782 : }
783 0 : ++maxndx;
784 0 : hasharr_at += sizeof (Elf32_Word);
785 : }
786 0 : while (data != NULL);
787 : }
788 : }
789 : }
790 0 : if (offs[i_strtab] > offs[i_symtab] && mod->syments == 0)
791 0 : mod->syments = ((offs[i_strtab] - offs[i_symtab])
792 0 : / gelf_fsize (mod->main.elf,
793 : ELF_T_SYM, 1, EV_CURRENT));
794 :
795 0 : if (mod->syments > 0)
796 : {
797 0 : mod->symdata = elf_getdata_rawchunk (mod->main.elf,
798 0 : offs[i_symtab],
799 : gelf_fsize (mod->main.elf,
800 : ELF_T_SYM,
801 : mod->syments,
802 : EV_CURRENT),
803 : ELF_T_SYM);
804 0 : if (mod->symdata != NULL)
805 : {
806 0 : mod->symstrdata = elf_getdata_rawchunk (mod->main.elf,
807 0 : offs[i_strtab],
808 : strsz,
809 : ELF_T_BYTE);
810 0 : if (mod->symstrdata == NULL)
811 0 : mod->symdata = NULL;
812 : }
813 0 : if (mod->symdata == NULL)
814 0 : mod->symerr = DWFL_E (LIBELF, elf_errno ());
815 : else
816 : {
817 0 : mod->symfile = &mod->main;
818 0 : mod->symerr = DWFL_E_NOERROR;
819 : }
820 : }
821 0 : }
822 :
823 : /* Try to find a dynamic symbol table via phdrs. */
824 : static void
825 7 : find_dynsym (Dwfl_Module *mod)
826 : {
827 7 : GElf_Ehdr ehdr_mem;
828 7 : GElf_Ehdr *ehdr = gelf_getehdr (mod->main.elf, &ehdr_mem);
829 :
830 7 : size_t phnum;
831 7 : if (unlikely (elf_getphdrnum (mod->main.elf, &phnum) != 0))
832 7 : return;
833 :
834 28 : for (size_t i = 0; i < phnum; ++i)
835 : {
836 28 : GElf_Phdr phdr_mem;
837 28 : GElf_Phdr *phdr = gelf_getphdr (mod->main.elf, i, &phdr_mem);
838 28 : if (phdr == NULL)
839 : break;
840 :
841 28 : if (phdr->p_type == PT_DYNAMIC)
842 : {
843 : /* Examine the dynamic section for the pointers we need. */
844 :
845 21 : Elf_Data *data = elf_getdata_rawchunk (mod->main.elf,
846 7 : phdr->p_offset, phdr->p_filesz,
847 : ELF_T_DYN);
848 7 : if (data == NULL)
849 0 : continue;
850 :
851 7 : GElf_Addr addrs[i_max] = { 0, };
852 7 : GElf_Xword strsz = 0;
853 7 : size_t n = data->d_size / gelf_fsize (mod->main.elf,
854 : ELF_T_DYN, 1, EV_CURRENT);
855 111 : for (size_t j = 0; j < n; ++j)
856 : {
857 111 : GElf_Dyn dyn_mem;
858 111 : GElf_Dyn *dyn = gelf_getdyn (data, j, &dyn_mem);
859 111 : if (dyn != NULL)
860 111 : switch (dyn->d_tag)
861 : {
862 6 : case DT_SYMTAB:
863 6 : addrs[i_symtab] = dyn->d_un.d_ptr;
864 104 : continue;
865 :
866 1 : case DT_HASH:
867 1 : addrs[i_hash] = dyn->d_un.d_ptr;
868 1 : continue;
869 :
870 6 : case DT_GNU_HASH:
871 6 : addrs[i_gnu_hash] = dyn->d_un.d_ptr;
872 6 : continue;
873 :
874 6 : case DT_STRTAB:
875 6 : addrs[i_strtab] = dyn->d_un.d_ptr;
876 6 : continue;
877 :
878 6 : case DT_STRSZ:
879 6 : strsz = dyn->d_un.d_val;
880 6 : continue;
881 :
882 : default:
883 : continue;
884 :
885 : case DT_NULL:
886 : break;
887 : }
888 7 : 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 7 : translate_offs (0, mod, phnum, addrs, strsz, ehdr);
895 7 : if (mod->symfile == NULL)
896 4 : translate_offs (mod->main_bias, mod, phnum, addrs, strsz, ehdr);
897 :
898 7 : 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 16 : 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 16 : 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 16 : GElf_Ehdr ehdr_main, ehdr_aux;
916 16 : if (unlikely (gelf_getehdr (mod->main.elf, &ehdr_main) == NULL)
917 16 : || unlikely (gelf_getehdr (mod->aux_sym.elf, &ehdr_aux) == NULL))
918 0 : return false;
919 16 : 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 16 : if (mod->aux_sym.address_sync != 0)
924 16 : 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 43 : 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 43 : Elf *elf = mod->main.elf;
943 :
944 43 : size_t shstrndx;
945 43 : if (elf_getshdrstrndx (elf, &shstrndx) < 0)
946 43 : return;
947 :
948 : Elf_Scn *scn = NULL;
949 992 : while ((scn = elf_nextscn (elf, scn)) != NULL)
950 : {
951 965 : GElf_Shdr shdr_mem;
952 965 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
953 965 : if (shdr == NULL)
954 0 : return;
955 :
956 965 : const char *name = elf_strptr (elf, shstrndx, shdr->sh_name);
957 965 : if (name == NULL)
958 : return;
959 :
960 965 : if (!strcmp (name, ".gnu_debugdata"))
961 : break;
962 : }
963 :
964 43 : 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 16 : Elf_Data *rawdata = elf_rawdata (scn, NULL);
970 16 : if (rawdata == NULL)
971 : return;
972 :
973 16 : Dwfl_Error error;
974 16 : void *buffer = NULL;
975 16 : size_t size = 0;
976 16 : error = __libdw_unlzma (-1, 0, rawdata->d_buf, rawdata->d_size,
977 : &buffer, &size);
978 16 : if (error == DWFL_E_NOERROR)
979 : {
980 16 : if (unlikely (size == 0))
981 0 : free (buffer);
982 : else
983 : {
984 16 : mod->aux_sym.elf = elf_memory (buffer, size);
985 16 : if (mod->aux_sym.elf == NULL)
986 0 : free (buffer);
987 : else
988 : {
989 16 : mod->aux_sym.fd = -1;
990 16 : mod->aux_sym.elf->flags |= ELF_F_MALLOCED;
991 16 : if (open_elf (mod, &mod->aux_sym) != DWFL_E_NOERROR)
992 : return;
993 16 : 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 445 : while ((scn = elf_nextscn (mod->aux_sym.elf, scn)) != NULL)
1004 : {
1005 429 : GElf_Shdr shdr_mem, *shdr = gelf_getshdr (scn, &shdr_mem);
1006 429 : if (shdr != NULL)
1007 429 : switch (shdr->sh_type)
1008 : {
1009 16 : case SHT_SYMTAB:
1010 16 : if (shdr->sh_entsize == 0)
1011 0 : return;
1012 16 : minisymtab = true;
1013 16 : *aux_symscn = scn;
1014 16 : *aux_strshndx = shdr->sh_link;
1015 16 : mod->aux_syments = shdr->sh_size / shdr->sh_entsize;
1016 16 : mod->aux_first_global = shdr->sh_info;
1017 16 : 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 16 : 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 3089477 : find_symtab (Dwfl_Module *mod)
1054 : {
1055 3089477 : if (mod->symdata != NULL || mod->aux_symdata != NULL /* Already done. */
1056 278 : || mod->symerr != DWFL_E_NOERROR) /* Cached previous failure. */
1057 3089216 : return;
1058 :
1059 271 : __libdwfl_getelf (mod);
1060 271 : mod->symerr = mod->elferr;
1061 271 : if (mod->symerr != DWFL_E_NOERROR)
1062 : return;
1063 :
1064 : /* First see if the main ELF file has the debugging information. */
1065 268 : Elf_Scn *symscn = NULL, *xndxscn = NULL;
1066 268 : Elf_Scn *aux_symscn = NULL, *aux_xndxscn = NULL;
1067 268 : GElf_Word strshndx, aux_strshndx = 0;
1068 268 : mod->symerr = load_symtab (&mod->main, &mod->symfile, &symscn,
1069 : &xndxscn, &mod->syments, &mod->first_global,
1070 : &strshndx);
1071 268 : switch (mod->symerr)
1072 : {
1073 : default:
1074 : return;
1075 :
1076 : case DWFL_E_NOERROR:
1077 : break;
1078 :
1079 74 : case DWFL_E_NO_SYMTAB:
1080 : /* Now we have to look for a separate debuginfo file. */
1081 74 : mod->symerr = find_debuginfo (mod);
1082 74 : 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 42 : case DWFL_E_CB: /* The find_debuginfo hook failed. */
1094 42 : mod->symerr = DWFL_E_NO_SYMTAB;
1095 42 : break;
1096 : }
1097 :
1098 74 : switch (mod->symerr)
1099 : {
1100 : default:
1101 : return;
1102 :
1103 : case DWFL_E_NOERROR:
1104 : break;
1105 :
1106 43 : case DWFL_E_NO_SYMTAB:
1107 : /* There might be an auxiliary table. */
1108 43 : find_aux_sym (mod, &aux_symscn, &aux_xndxscn, &aux_strshndx);
1109 :
1110 43 : if (symscn != NULL)
1111 : {
1112 : /* We still have the dynamic symbol table. */
1113 35 : mod->symerr = DWFL_E_NOERROR;
1114 35 : break;
1115 : }
1116 :
1117 8 : 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 7 : find_dynsym (mod);
1126 7 : return;
1127 : }
1128 : break;
1129 : }
1130 :
1131 : /* This does some sanity checks on the string table section. */
1132 260 : 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 260 : Elf_Scn *symstrscn = elf_getscn (mod->symfile->elf, strshndx);
1149 260 : if (symstrscn == NULL)
1150 : goto elferr;
1151 :
1152 260 : GElf_Shdr shdr_mem;
1153 260 : GElf_Shdr *shdr = gelf_getshdr (symstrscn, &shdr_mem);
1154 260 : if (shdr == NULL)
1155 : goto elferr;
1156 :
1157 260 : size_t shstrndx;
1158 260 : if (elf_getshdrstrndx (mod->symfile->elf, &shstrndx) < 0)
1159 : goto elferr;
1160 :
1161 260 : const char *sname = elf_strptr (mod->symfile->elf, shstrndx, shdr->sh_name);
1162 260 : if (sname == NULL)
1163 : goto elferr;
1164 :
1165 260 : 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 260 : if ((shdr->sh_flags & SHF_COMPRESSED) != 0)
1171 0 : if (elf_compress (symstrscn, 0, 0) < 0)
1172 : goto elferr;
1173 :
1174 260 : mod->symstrdata = elf_getdata (symstrscn, NULL);
1175 260 : if (mod->symstrdata == NULL || mod->symstrdata->d_buf == NULL)
1176 : goto elferr;
1177 :
1178 260 : if (xndxscn == NULL)
1179 260 : 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 260 : shdr = gelf_getshdr (symscn, &shdr_mem);
1196 260 : if (shdr == NULL)
1197 : goto elferr;
1198 :
1199 260 : if ((shdr->sh_flags & SHF_COMPRESSED) != 0)
1200 0 : if (elf_compress (symscn, 0, 0) < 0)
1201 : goto elferr;
1202 :
1203 260 : mod->symdata = elf_getdata (symscn, NULL);
1204 260 : if (mod->symdata == NULL || mod->symdata->d_buf == NULL)
1205 : goto elferr;
1206 :
1207 : // Sanity check number of symbols.
1208 260 : shdr = gelf_getshdr (symscn, &shdr_mem);
1209 260 : if (shdr == NULL || shdr->sh_entsize == 0
1210 260 : || mod->syments > mod->symdata->d_size / shdr->sh_entsize
1211 260 : || (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 260 : if (aux_symscn != NULL)
1216 : {
1217 15 : aux_cache:
1218 : /* This does some sanity checks on the string table section. */
1219 16 : 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 16 : Elf_Scn *aux_strscn = elf_getscn (mod->aux_sym.elf, aux_strshndx);
1232 16 : if (aux_strscn == NULL)
1233 0 : goto elferr;
1234 :
1235 16 : shdr = gelf_getshdr (aux_strscn, &shdr_mem);
1236 16 : if (shdr == NULL)
1237 : goto elferr;
1238 :
1239 16 : size_t aux_shstrndx;
1240 16 : if (elf_getshdrstrndx (mod->aux_sym.elf, &aux_shstrndx) < 0)
1241 : goto elferr;
1242 :
1243 48 : sname = elf_strptr (mod->aux_sym.elf, aux_shstrndx,
1244 16 : shdr->sh_name);
1245 16 : if (sname == NULL)
1246 : goto elferr;
1247 :
1248 16 : 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 16 : if ((shdr->sh_flags & SHF_COMPRESSED) != 0)
1254 0 : if (elf_compress (aux_strscn, 0, 0) < 0)
1255 : goto elferr;
1256 :
1257 16 : 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 16 : if (aux_xndxscn == NULL)
1262 16 : 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 16 : shdr = gelf_getshdr (aux_symscn, &shdr_mem);
1280 16 : if (shdr == NULL)
1281 : goto elferr;
1282 :
1283 16 : if ((shdr->sh_flags & SHF_COMPRESSED) != 0)
1284 0 : if (elf_compress (aux_symscn, 0, 0) < 0)
1285 : goto elferr;
1286 :
1287 16 : 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 16 : shdr = gelf_getshdr (aux_symscn, &shdr_mem);
1293 16 : if (mod->aux_syments > mod->aux_symdata->d_size / shdr->sh_entsize
1294 16 : || (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 940691422 : __libdwfl_module_getebl (Dwfl_Module *mod)
1304 : {
1305 940691422 : if (mod->ebl == NULL)
1306 : {
1307 407 : __libdwfl_getelf (mod);
1308 407 : if (mod->elferr != DWFL_E_NOERROR)
1309 : return mod->elferr;
1310 :
1311 407 : mod->ebl = ebl_openbackend (mod->main.elf);
1312 407 : if (mod->ebl == NULL)
1313 0 : 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 5453 : load_dw (Dwfl_Module *mod, struct dwfl_file *debugfile)
1321 : {
1322 5453 : if (mod->e_type == ET_REL && !debugfile->relocated)
1323 : {
1324 38 : const Dwfl_Callbacks *const cb = mod->dwfl->callbacks;
1325 :
1326 : /* The debugging sections have to be relocated. */
1327 38 : if (cb->section_address == NULL)
1328 : return DWFL_E_NOREL;
1329 :
1330 38 : Dwfl_Error error = __libdwfl_module_getebl (mod);
1331 38 : if (error != DWFL_E_NOERROR)
1332 : return error;
1333 :
1334 38 : find_symtab (mod);
1335 38 : Dwfl_Error result = mod->symerr;
1336 38 : if (result == DWFL_E_NOERROR)
1337 38 : result = __libdwfl_relocate (mod, debugfile->elf, true);
1338 38 : if (result != DWFL_E_NOERROR)
1339 : return result;
1340 : }
1341 :
1342 5453 : mod->dw = INTUSE(dwarf_begin_elf) (debugfile->elf, DWARF_C_READ, NULL);
1343 5453 : if (mod->dw == NULL)
1344 : {
1345 121 : int err = INTUSE(dwarf_errno) ();
1346 121 : 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 5332 : if (mod->e_type == ET_REL && !debugfile->relocated)
1351 : {
1352 : /* Don't keep the file descriptors around. */
1353 30 : 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 30 : 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 5332 : if (mod->dw->debugdir == NULL && mod->elfdir != NULL
1369 281 : && debugfile == &mod->main)
1370 281 : mod->dw->debugdir = strdup (mod->elfdir);
1371 :
1372 : /* Until we have iterated through all CU's, we might do lazy lookups. */
1373 5332 : mod->lazycu = 1;
1374 :
1375 5332 : 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 12967 : find_dw (Dwfl_Module *mod)
1381 : {
1382 12967 : if (mod->dw != NULL /* Already done. */
1383 8064 : || mod->dwerr != DWFL_E_NOERROR) /* Cached previous failure. */
1384 : return;
1385 :
1386 5433 : __libdwfl_getelf (mod);
1387 5433 : mod->dwerr = mod->elferr;
1388 5433 : if (mod->dwerr != DWFL_E_NOERROR)
1389 : return;
1390 :
1391 : /* First see if the main ELF file has the debugging information. */
1392 5419 : mod->dwerr = load_dw (mod, &mod->main);
1393 5419 : switch (mod->dwerr)
1394 : {
1395 5298 : case DWFL_E_NOERROR:
1396 5298 : mod->debug.elf = mod->main.elf;
1397 5298 : 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 5298 : find_debug_altlink (mod, mod->main.name);
1403 5298 : return;
1404 :
1405 : case DWFL_E_NO_DWARF:
1406 120 : break;
1407 :
1408 : default:
1409 : goto canonicalize;
1410 : }
1411 :
1412 : /* Now we have to look for a separate debuginfo file. */
1413 120 : mod->dwerr = find_debuginfo (mod);
1414 120 : switch (mod->dwerr)
1415 : {
1416 34 : case DWFL_E_NOERROR:
1417 34 : mod->dwerr = load_dw (mod, &mod->debug);
1418 34 : 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 34 : find_debug_altlink (mod, mod->debug.name);
1424 34 : return;
1425 : }
1426 :
1427 : break;
1428 :
1429 86 : case DWFL_E_CB: /* The find_debuginfo hook failed. */
1430 86 : mod->dwerr = DWFL_E_NO_DWARF;
1431 86 : 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 12967 : dwfl_module_getdwarf (Dwfl_Module *mod, Dwarf_Addr *bias)
1443 : {
1444 12967 : if (mod == NULL)
1445 : return NULL;
1446 :
1447 12967 : find_dw (mod);
1448 12967 : 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 10235 : if (mod->e_type == ET_REL
1453 99 : && mod->main.relocated && ! mod->debug.relocated)
1454 : {
1455 57 : mod->debug.relocated = true;
1456 57 : if (mod->debug.elf != mod->main.elf)
1457 0 : (void) __libdwfl_relocate (mod, mod->debug.elf, false);
1458 : }
1459 :
1460 10235 : *bias = dwfl_adjusted_dwarf_addr (mod, 0);
1461 10235 : return mod->dw;
1462 : }
1463 :
1464 2732 : __libdwfl_seterrno (mod->dwerr);
1465 2732 : return NULL;
1466 : }
1467 : INTDEF (dwfl_module_getdwarf)
1468 :
1469 : int
1470 2059216 : dwfl_module_getsymtab (Dwfl_Module *mod)
1471 : {
1472 2059216 : if (mod == NULL)
1473 : return -1;
1474 :
1475 2059216 : find_symtab (mod);
1476 2059216 : if (mod->symerr == DWFL_E_NOERROR)
1477 : /* We will skip the auxiliary zero entry if there is another one. */
1478 2059205 : return (mod->syments + mod->aux_syments
1479 4118263 : - (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 1030223 : dwfl_module_getsymtab_first_global (Dwfl_Module *mod)
1488 : {
1489 1030223 : if (mod == NULL)
1490 : return -1;
1491 :
1492 1030223 : find_symtab (mod);
1493 1030223 : 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 1030223 : int skip_aux_zero = (mod->syments > 0 && mod->aux_syments > 0) ? 1 : 0;
1501 1030223 : 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)
|