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