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