Coverage Report

Created: 2025-07-18 06:08

/src/elfutils/libdwfl/dwfl_module_getdwarf.c
Line
Count
Source (jump to first uncovered line)
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
open_elf_file (Elf **elf, int *fd, char **name)
45
4.20k
{
46
4.20k
  if (*elf == NULL)
47
4.20k
    {
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
4.20k
      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
4.20k
      if (*fd < 0 && *name != NULL)
55
0
  *fd = TEMP_FAILURE_RETRY (open (*name, O_RDONLY));
56
57
4.20k
      if (*fd < 0)
58
4.19k
  return CBFAIL;
59
60
8
      return __libdw_open_file (fd, elf, true, false);
61
4.20k
    }
62
0
  else if (unlikely (elf_kind (*elf) != ELF_K_ELF))
63
0
    {
64
0
      elf_end (*elf);
65
0
      *elf = NULL;
66
0
      close (*fd);
67
0
      *fd = -1;
68
0
      return DWFL_E_BADELF;
69
0
    }
70
71
  /* Elf file already open and looks fine.  */
72
0
  return DWFL_E_NOERROR;
73
4.20k
}
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
open_elf (Dwfl_Module *mod, struct dwfl_file *file)
79
4.19k
{
80
4.19k
  Dwfl_Error error = open_elf_file (&file->elf, &file->fd, &file->name);
81
4.19k
  if (error != DWFL_E_NOERROR)
82
4.19k
    return error;
83
84
  /* Cache file->elf in Dwflst_Process_Tracker if available: */
85
0
  if (mod->dwfl->tracker != NULL && file->name != NULL)
86
0
    {
87
0
      INTUSE(dwflst_tracker_cache_elf) (mod->dwfl->tracker, file->name,
88
0
          file->name, file->elf, file->fd);
89
0
    }
90
91
0
  GElf_Ehdr ehdr_mem, *ehdr = gelf_getehdr (file->elf, &ehdr_mem);
92
0
  if (ehdr == NULL)
93
0
    {
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
0
    }
101
102
0
  if (ehdr->e_type != ET_REL)
103
0
    {
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
0
      size_t phnum;
137
0
      if (unlikely (elf_getphdrnum (file->elf, &phnum) != 0))
138
0
  goto elf_error;
139
140
0
      file->vaddr = file->address_sync = 0;
141
0
      for (size_t i = 0; i < phnum; ++i)
142
0
  {
143
0
    GElf_Phdr ph_mem;
144
0
    GElf_Phdr *ph = gelf_getphdr (file->elf, i, &ph_mem);
145
0
    if (unlikely (ph == NULL))
146
0
      goto elf_error;
147
0
    if (ph->p_type == PT_LOAD)
148
0
      {
149
0
        file->vaddr = ph->p_vaddr & -ph->p_align;
150
0
        file->address_sync = ph->p_vaddr + ph->p_memsz;
151
0
        break;
152
0
      }
153
0
  }
154
0
    }
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
0
  if (file == &mod->main)
165
0
    {
166
0
      mod->e_type = ehdr->e_type;
167
168
      /* Relocatable Linux kernels are ET_EXEC but act like ET_DYN.  */
169
0
      if (mod->e_type == ET_EXEC && file->vaddr != mod->low_addr)
170
0
  mod->e_type = ET_DYN;
171
0
    }
172
0
  else
173
0
    assert (mod->main.elf != NULL);
174
175
0
  return DWFL_E_NOERROR;
176
0
}
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
mod_verify_build_id (Dwfl_Module *mod)
182
0
{
183
0
  assert (mod->build_id_len > 0);
184
185
0
  switch (__builtin_expect (__libdwfl_find_build_id (mod, false,
186
0
                 mod->main.elf), 2))
187
0
    {
188
0
    case 2:
189
      /* Build ID matches as it should. */
190
0
      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
0
    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
0
    }
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
0
    {
210
0
      close (mod->main.fd);
211
0
      mod->main.fd = -1;
212
0
    }
213
0
}
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
__libdwfl_getelf (Dwfl_Module *mod)
220
7.22k
{
221
7.22k
  if (mod->main.elf != NULL  /* Already done.  */
222
7.22k
      || mod->elferr != DWFL_E_NOERROR) /* Cached failure.  */
223
7.22k
    return;
224
225
0
  mod->main.fd = (*mod->dwfl->callbacks->find_elf) (MODCB_ARGS (mod),
226
0
                &mod->main.name,
227
0
                &mod->main.elf);
228
0
  const bool fallback = mod->main.elf == NULL && mod->main.fd < 0;
229
0
  mod->elferr = open_elf (mod, &mod->main);
230
0
  if (mod->elferr != DWFL_E_NOERROR)
231
0
    return;
232
233
0
  if (!mod->main.valid)
234
0
    {
235
      /* Clear any explicitly reported build ID, just in case it was wrong.
236
   We'll fetch it from the file when asked.  */
237
0
      free (mod->build_id_bits);
238
0
      mod->build_id_bits = NULL;
239
0
      mod->build_id_len = 0;
240
0
    }
241
0
  else if (fallback)
242
0
    mod_verify_build_id (mod);
243
244
0
  mod->main_bias = mod->e_type == ET_REL ? 0 : mod->low_addr - mod->main.vaddr;
245
0
}
246
247
static inline void
248
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
0
{
255
0
  if ((sh_flags & SHF_ALLOC)
256
0
      && ((sh_type == SHT_PROGBITS && sh_addr != interp)
257
0
          || sh_type == SHT_NOBITS))
258
0
    {
259
0
      const GElf_Addr sh_end = sh_addr + sh_size;
260
0
      if (sh_end > *phighest)
261
0
        *phighest = sh_end;
262
0
    }
263
0
}
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
find_prelink_address_sync (Dwfl_Module *mod, struct dwfl_file *file)
283
0
{
284
  /* The magic section is only identified by name.  */
285
0
  size_t shstrndx;
286
0
  if (elf_getshdrstrndx (mod->main.elf, &shstrndx) < 0)
287
0
    return DWFL_E_LIBELF;
288
289
0
  Elf_Scn *scn = NULL;
290
0
  while ((scn = elf_nextscn (mod->main.elf, scn)) != NULL)
291
0
    {
292
0
      GElf_Shdr shdr_mem;
293
0
      GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
294
0
      if (unlikely (shdr == NULL))
295
0
  return DWFL_E_LIBELF;
296
0
      if (shdr->sh_type == SHT_PROGBITS
297
0
    && !(shdr->sh_flags & SHF_ALLOC)
298
0
    && shdr->sh_name != 0)
299
0
  {
300
0
    const char *secname = elf_strptr (mod->main.elf, shstrndx,
301
0
              shdr->sh_name);
302
0
    if (unlikely (secname == NULL))
303
0
      return DWFL_E_LIBELF;
304
0
    if (!strcmp (secname, ".gnu.prelink_undo"))
305
0
      break;
306
0
  }
307
0
    }
308
309
0
  if (scn == NULL)
310
    /* There was no .gnu.prelink_undo section.  */
311
0
    return DWFL_E_NOERROR;
312
313
0
  Elf_Data *undodata = elf_rawdata (scn, NULL);
314
0
  if (unlikely (undodata == NULL))
315
0
    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
0
  union
321
0
  {
322
0
    Elf32_Ehdr e32;
323
0
    Elf64_Ehdr e64;
324
0
  } ehdr;
325
0
  Elf_Data dst =
326
0
    {
327
0
      .d_buf = &ehdr,
328
0
      .d_size = sizeof ehdr,
329
0
      .d_type = ELF_T_EHDR,
330
0
      .d_version = EV_CURRENT
331
0
    };
332
0
  Elf_Data src = *undodata;
333
0
  src.d_size = gelf_fsize (mod->main.elf, ELF_T_EHDR, 1, EV_CURRENT);
334
0
  src.d_type = ELF_T_EHDR;
335
0
  if (unlikely (gelf_xlatetom (mod->main.elf, &dst, &src,
336
0
             elf_getident (mod->main.elf, NULL)[EI_DATA])
337
0
    == NULL))
338
0
    return DWFL_E_LIBELF;
339
340
0
  size_t shentsize = gelf_fsize (mod->main.elf, ELF_T_SHDR, 1, EV_CURRENT);
341
0
  size_t phentsize = gelf_fsize (mod->main.elf, ELF_T_PHDR, 1, EV_CURRENT);
342
343
0
  uint_fast16_t phnum;
344
0
  uint_fast16_t shnum;
345
0
  if (ehdr.e32.e_ident[EI_CLASS] == ELFCLASS32)
346
0
    {
347
0
      if (ehdr.e32.e_shentsize != shentsize
348
0
    || ehdr.e32.e_phentsize != phentsize)
349
0
  return DWFL_E_BAD_PRELINK;
350
0
      phnum = ehdr.e32.e_phnum;
351
0
      shnum = ehdr.e32.e_shnum;
352
0
    }
353
0
  else
354
0
    {
355
0
      if (ehdr.e64.e_shentsize != shentsize
356
0
    || ehdr.e64.e_phentsize != phentsize)
357
0
  return DWFL_E_BAD_PRELINK;
358
0
      phnum = ehdr.e64.e_phnum;
359
0
      shnum = ehdr.e64.e_shnum;
360
0
    }
361
362
  /* Since prelink does not store the zeroth section header in the undo
363
     section, it cannot support SHN_XINDEX encoding.  */
364
0
  if (unlikely (shnum >= SHN_LORESERVE) || unlikely(shnum == 0)
365
0
      || unlikely (undodata->d_size != (src.d_size
366
0
          + phnum * phentsize
367
0
          + (shnum - 1) * shentsize)))
368
0
    return DWFL_E_BAD_PRELINK;
369
370
0
  --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
0
  GElf_Addr main_interp = 0;
381
0
  {
382
0
    size_t main_phnum;
383
0
    if (unlikely (elf_getphdrnum (mod->main.elf, &main_phnum)))
384
0
      return DWFL_E_LIBELF;
385
0
    for (size_t i = 0; i < main_phnum; ++i)
386
0
      {
387
0
  GElf_Phdr phdr;
388
0
  if (unlikely (gelf_getphdr (mod->main.elf, i, &phdr) == NULL))
389
0
    return DWFL_E_LIBELF;
390
0
  if (phdr.p_type == PT_INTERP)
391
0
    {
392
0
      main_interp = phdr.p_vaddr;
393
0
      break;
394
0
    }
395
0
      }
396
0
  }
397
398
0
  src.d_buf += src.d_size;
399
0
  src.d_type = ELF_T_PHDR;
400
0
  src.d_size = phnum * phentsize;
401
402
0
  GElf_Addr undo_interp = 0;
403
0
  bool class32 = ehdr.e32.e_ident[EI_CLASS] == ELFCLASS32;
404
0
  {
405
0
    size_t phdr_size = class32 ? sizeof (Elf32_Phdr) : sizeof (Elf64_Phdr);
406
0
    if (unlikely (phnum > SIZE_MAX / phdr_size))
407
0
      return DWFL_E_NOMEM;
408
0
    const size_t phdrs_bytes = phnum * phdr_size;
409
0
    void *phdrs = malloc (phdrs_bytes);
410
0
    if (unlikely (phdrs == NULL))
411
0
      return DWFL_E_NOMEM;
412
0
    dst.d_buf = phdrs;
413
0
    dst.d_size = phdrs_bytes;
414
0
    if (unlikely (gelf_xlatetom (mod->main.elf, &dst, &src,
415
0
         ehdr.e32.e_ident[EI_DATA]) == NULL))
416
0
      {
417
0
  free (phdrs);
418
0
  return DWFL_E_LIBELF;
419
0
      }
420
0
    if (class32)
421
0
      {
422
0
  Elf32_Phdr (*p32)[phnum] = phdrs;
423
0
  for (uint_fast16_t i = 0; i < phnum; ++i)
424
0
    if ((*p32)[i].p_type == PT_INTERP)
425
0
      {
426
0
        undo_interp = (*p32)[i].p_vaddr;
427
0
        break;
428
0
      }
429
0
      }
430
0
    else
431
0
      {
432
0
  Elf64_Phdr (*p64)[phnum] = phdrs;
433
0
  for (uint_fast16_t i = 0; i < phnum; ++i)
434
0
    if ((*p64)[i].p_type == PT_INTERP)
435
0
      {
436
0
        undo_interp = (*p64)[i].p_vaddr;
437
0
        break;
438
0
      }
439
0
      }
440
0
    free (phdrs);
441
0
  }
442
443
0
  if (unlikely ((main_interp == 0) != (undo_interp == 0)))
444
0
    return DWFL_E_BAD_PRELINK;
445
446
0
  src.d_buf += src.d_size;
447
0
  src.d_type = ELF_T_SHDR;
448
0
  src.d_size = gelf_fsize (mod->main.elf, ELF_T_SHDR, shnum, EV_CURRENT);
449
450
0
  size_t shdr_size = class32 ? sizeof (Elf32_Shdr) : sizeof (Elf64_Shdr);
451
0
  if (unlikely (shnum > SIZE_MAX / shdr_size))
452
0
    return DWFL_E_NOMEM;
453
0
  const size_t shdrs_bytes = shnum * shdr_size;
454
0
  void *shdrs = malloc (shdrs_bytes);
455
0
  if (unlikely (shdrs == NULL))
456
0
    return DWFL_E_NOMEM;
457
0
  dst.d_buf = shdrs;
458
0
  dst.d_size = shdrs_bytes;
459
0
  if (unlikely (gelf_xlatetom (mod->main.elf, &dst, &src,
460
0
             ehdr.e32.e_ident[EI_DATA]) == NULL))
461
0
    {
462
0
      free (shdrs);
463
0
      return DWFL_E_LIBELF;
464
0
    }
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
0
  GElf_Addr highest;
483
484
0
  highest = 0;
485
0
  scn = NULL;
486
0
  while ((scn = elf_nextscn (mod->main.elf, scn)) != NULL)
487
0
    {
488
0
      GElf_Shdr sh_mem;
489
0
      GElf_Shdr *sh = gelf_getshdr (scn, &sh_mem);
490
0
      if (unlikely (sh == NULL))
491
0
  {
492
0
    free (shdrs);
493
0
    return DWFL_E_LIBELF;
494
0
  }
495
0
      consider_shdr (main_interp, sh->sh_type, sh->sh_flags,
496
0
         sh->sh_addr, sh->sh_size, &highest);
497
0
    }
498
0
  if (highest > mod->main.vaddr)
499
0
    {
500
0
      mod->main.address_sync = highest;
501
502
0
      highest = 0;
503
0
      if (class32)
504
0
  {
505
0
    Elf32_Shdr (*s32)[shnum] = shdrs;
506
0
    for (size_t i = 0; i < shnum; ++i)
507
0
      consider_shdr (undo_interp, (*s32)[i].sh_type,
508
0
         (*s32)[i].sh_flags, (*s32)[i].sh_addr,
509
0
         (*s32)[i].sh_size, &highest);
510
0
  }
511
0
      else
512
0
  {
513
0
    Elf64_Shdr (*s64)[shnum] = shdrs;
514
0
    for (size_t i = 0; i < shnum; ++i)
515
0
      consider_shdr (undo_interp, (*s64)[i].sh_type,
516
0
         (*s64)[i].sh_flags, (*s64)[i].sh_addr,
517
0
         (*s64)[i].sh_size, &highest);
518
0
  }
519
520
0
      if (highest > file->vaddr)
521
0
  file->address_sync = highest;
522
0
      else
523
0
  {
524
0
    free (shdrs);
525
0
    return DWFL_E_BAD_PRELINK;
526
0
  }
527
0
    }
528
529
0
  free (shdrs);
530
531
0
  return DWFL_E_NOERROR;
532
0
}
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
find_debuginfo (Dwfl_Module *mod)
538
4.19k
{
539
4.19k
  if (mod->debug.elf != NULL)
540
0
    return DWFL_E_NOERROR;
541
542
4.19k
  GElf_Word debuglink_crc = 0;
543
4.19k
  const char *debuglink_file;
544
4.19k
  debuglink_file = INTUSE(dwelf_elf_gnu_debuglink) (mod->main.elf,
545
4.19k
                &debuglink_crc);
546
547
4.19k
  mod->debug.fd = (*mod->dwfl->callbacks->find_debuginfo) (MODCB_ARGS (mod),
548
4.19k
                 mod->main.name,
549
4.19k
                 debuglink_file,
550
4.19k
                 debuglink_crc,
551
4.19k
                 &mod->debug.name);
552
4.19k
  Dwfl_Error result = open_elf (mod, &mod->debug);
553
4.19k
  if (result == DWFL_E_NOERROR && mod->debug.address_sync != 0)
554
0
    result = find_prelink_address_sync (mod, &mod->debug);
555
4.19k
  return result;
556
4.19k
}
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
find_debug_altlink (Dwfl_Module *mod, const char *filename)
564
138
{
565
138
  assert (mod->dw != NULL);
566
567
138
  const char *altname;
568
138
  const void *build_id;
569
138
  ssize_t build_id_len = INTUSE(dwelf_dwarf_gnu_debugaltlink) (mod->dw,
570
138
                     &altname,
571
138
                     &build_id);
572
573
138
  if (build_id_len > 0)
574
10
    {
575
      /* We could store altfile in the module, but don't really need it.  */
576
10
      char *altfile = NULL;
577
10
      mod->alt_fd = (*mod->dwfl->callbacks->find_debuginfo) (MODCB_ARGS (mod),
578
10
                   filename,
579
10
                   altname,
580
10
                   0,
581
10
                   &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
10
      Dwfl_Error error = open_elf_file (&mod->alt_elf, &mod->alt_fd,
588
10
          &altfile);
589
10
      if (error == DWFL_E_NOERROR)
590
0
  {
591
0
    mod->alt = INTUSE(dwarf_begin_elf) (mod->alt_elf,
592
0
                DWARF_C_READ, NULL);
593
0
    if (mod->alt == NULL)
594
0
      {
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
0
      }
600
0
    else
601
0
      dwarf_setalt (mod->dw, mod->alt);
602
0
  }
603
604
10
      free (altfile); /* See above, we don't really need it.  */
605
10
    }
606
138
}
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
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
0
{
616
0
  bool symtab = false;
617
0
  Elf_Scn *scn = NULL;
618
0
  while ((scn = elf_nextscn (file->elf, scn)) != NULL)
619
0
    {
620
0
      GElf_Shdr shdr_mem, *shdr = gelf_getshdr (scn, &shdr_mem);
621
0
      if (shdr != NULL)
622
0
  switch (shdr->sh_type)
623
0
    {
624
0
    case SHT_SYMTAB:
625
0
      if (shdr->sh_entsize == 0)
626
0
        break;
627
0
      symtab = true;
628
0
      *symscn = scn;
629
0
      *symfile = file;
630
0
      *strshndx = shdr->sh_link;
631
0
      *syments = shdr->sh_size / shdr->sh_entsize;
632
0
      *first_global = shdr->sh_info;
633
0
      if (*xndxscn != NULL)
634
0
        return DWFL_E_NOERROR;
635
0
      break;
636
637
0
    case SHT_DYNSYM:
638
0
      if (symtab)
639
0
        break;
640
      /* Use this if need be, but keep looking for SHT_SYMTAB.  */
641
0
      if (shdr->sh_entsize == 0)
642
0
        break;
643
0
      *symscn = scn;
644
0
      *symfile = file;
645
0
      *strshndx = shdr->sh_link;
646
0
      *syments = shdr->sh_size / shdr->sh_entsize;
647
0
      *first_global = shdr->sh_info;
648
0
      break;
649
650
0
    case SHT_SYMTAB_SHNDX:
651
0
      *xndxscn = scn;
652
0
      if (symtab)
653
0
        return DWFL_E_NOERROR;
654
0
      break;
655
656
0
    default:
657
0
      break;
658
0
    }
659
0
    }
660
661
0
  if (symtab)
662
    /* We found one, though no SHT_SYMTAB_SHNDX to go with it.  */
663
0
    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
0
  *xndxscn = NULL;
668
0
  return DWFL_E_NO_SYMTAB;
669
0
}
670
671
672
/* Translate addresses into file offsets.
673
   OFFS[*] start out zero and remain zero if unresolved.  */
674
static void
675
find_offsets (Elf *elf, GElf_Addr main_bias, size_t phnum, size_t n,
676
        GElf_Addr addrs[n], GElf_Off offs[n])
677
0
{
678
0
  size_t unsolved = n;
679
0
  for (size_t i = 0; i < phnum; ++i)
680
0
    {
681
0
      GElf_Phdr phdr_mem;
682
0
      GElf_Phdr *phdr = gelf_getphdr (elf, i, &phdr_mem);
683
0
      if (phdr != NULL && phdr->p_type == PT_LOAD && phdr->p_memsz > 0)
684
0
  for (size_t j = 0; j < n; ++j)
685
0
    if (offs[j] == 0
686
0
        && addrs[j] >= phdr->p_vaddr + main_bias
687
0
        && addrs[j] - (phdr->p_vaddr + main_bias) < phdr->p_filesz)
688
0
      {
689
0
        offs[j] = addrs[j] - (phdr->p_vaddr + main_bias) + phdr->p_offset;
690
0
        if (--unsolved == 0)
691
0
    break;
692
0
      }
693
0
    }
694
0
}
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
validate_strdata (Elf_Data *symstrdata)
700
0
{
701
0
  size_t size = symstrdata->d_size;
702
0
  const char *buf = symstrdata->d_buf;
703
0
  while (size > 0 && *(buf + size - 1) != '\0')
704
0
    --size;
705
0
  symstrdata->d_size = size;
706
0
}
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
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
0
{
729
0
  GElf_Off offs[i_max] = { 0, };
730
0
  find_offsets (mod->main.elf, adjust, phnum, i_max, addrs, offs);
731
732
  /* Figure out the size of the symbol table.  */
733
0
  if (offs[i_hash] != 0)
734
0
    {
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
0
               (entsz == 4
741
0
                ? 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
0
    }
747
0
  if (offs[i_gnu_hash] != 0 && mod->syments == 0)
748
0
    {
749
      /* In the new format, we can derive it with some work.  */
750
751
0
      const struct
752
0
      {
753
0
        Elf32_Word nbuckets;
754
0
        Elf32_Word symndx;
755
0
        Elf32_Word maskwords;
756
0
        Elf32_Word shift2;
757
0
      } *header;
758
759
0
      Elf_Data *data = elf_getdata_rawchunk (mod->main.elf, offs[i_gnu_hash],
760
0
               sizeof *header, ELF_T_WORD);
761
0
      if (data != NULL)
762
0
        {
763
0
          header = data->d_buf;
764
0
          Elf32_Word nbuckets = header->nbuckets;
765
0
          Elf32_Word symndx = header->symndx;
766
0
          GElf_Off buckets_at = (offs[i_gnu_hash] + sizeof *header
767
0
         + (gelf_getclass (mod->main.elf)
768
0
            * sizeof (Elf32_Word)
769
0
            * 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
0
            data = elf_getdata_rawchunk (mod->main.elf, buckets_at,
779
0
             nbuckets * sizeof (Elf32_Word),
780
0
             ELF_T_WORD);
781
0
    if (data != NULL && symndx < nbuckets)
782
0
      {
783
0
        const Elf32_Word *const buckets = data->d_buf;
784
0
        Elf32_Word maxndx = symndx;
785
0
        for (Elf32_Word bucket = 0; bucket < nbuckets; ++bucket)
786
0
    if (buckets[bucket] > maxndx)
787
0
      maxndx = buckets[bucket];
788
789
0
        GElf_Off hasharr_at = (buckets_at
790
0
             + nbuckets * sizeof (Elf32_Word));
791
0
        hasharr_at += (maxndx - symndx) * sizeof (Elf32_Word);
792
0
        do
793
0
    {
794
0
      data = elf_getdata_rawchunk (mod->main.elf,
795
0
                 hasharr_at,
796
0
                 sizeof (Elf32_Word),
797
0
                 ELF_T_WORD);
798
0
      if (data != NULL
799
0
          && (*(const Elf32_Word *) data->d_buf & 1u))
800
0
        {
801
0
          mod->syments = maxndx + 1;
802
0
          break;
803
0
        }
804
0
      ++maxndx;
805
0
      hasharr_at += sizeof (Elf32_Word);
806
0
    }
807
0
        while (data != NULL);
808
0
      }
809
0
  }
810
0
    }
811
0
  if (offs[i_strtab] > offs[i_symtab] && mod->syments == 0)
812
0
    mod->syments = ((offs[i_strtab] - offs[i_symtab])
813
0
        / gelf_fsize (mod->main.elf,
814
0
          ELF_T_SYM, 1, EV_CURRENT));
815
816
0
  if (mod->syments > 0)
817
0
    {
818
0
      mod->symdata = elf_getdata_rawchunk (mod->main.elf,
819
0
             offs[i_symtab],
820
0
             gelf_fsize (mod->main.elf,
821
0
                   ELF_T_SYM,
822
0
                   mod->syments,
823
0
                   EV_CURRENT),
824
0
                   ELF_T_SYM);
825
0
      if (mod->symdata != NULL)
826
0
  {
827
0
    mod->symstrdata = elf_getdata_rawchunk (mod->main.elf,
828
0
              offs[i_strtab],
829
0
              strsz,
830
0
              ELF_T_BYTE);
831
0
    if (mod->symstrdata == NULL)
832
0
      mod->symdata = NULL;
833
0
    else
834
0
      validate_strdata (mod->symstrdata);
835
0
  }
836
0
      if (mod->symdata == NULL)
837
0
  mod->symerr = DWFL_E (LIBELF, elf_errno ());
838
0
      else
839
0
  {
840
0
    mod->symfile = &mod->main;
841
0
    mod->symerr = DWFL_E_NOERROR;
842
0
  }
843
0
    }
844
0
}
845
846
/* Try to find a dynamic symbol table via phdrs.  */
847
static void
848
find_dynsym (Dwfl_Module *mod)
849
0
{
850
0
  GElf_Ehdr ehdr_mem;
851
0
  GElf_Ehdr *ehdr = gelf_getehdr (mod->main.elf, &ehdr_mem);
852
853
0
  size_t phnum;
854
0
  if (unlikely (elf_getphdrnum (mod->main.elf, &phnum) != 0))
855
0
    return;
856
857
0
  for (size_t i = 0; i < phnum; ++i)
858
0
    {
859
0
      GElf_Phdr phdr_mem;
860
0
      GElf_Phdr *phdr = gelf_getphdr (mod->main.elf, i, &phdr_mem);
861
0
      if (phdr == NULL)
862
0
  break;
863
864
0
      if (phdr->p_type == PT_DYNAMIC)
865
0
  {
866
    /* Examine the dynamic section for the pointers we need.  */
867
868
0
    Elf_Data *data = elf_getdata_rawchunk (mod->main.elf,
869
0
             phdr->p_offset, phdr->p_filesz,
870
0
             ELF_T_DYN);
871
0
    if (data == NULL)
872
0
      continue;
873
874
0
    GElf_Addr addrs[i_max] = { 0, };
875
0
    GElf_Xword strsz = 0;
876
0
    size_t n = data->d_size / gelf_fsize (mod->main.elf,
877
0
            ELF_T_DYN, 1, EV_CURRENT);
878
0
    for (size_t j = 0; j < n; ++j)
879
0
      {
880
0
        GElf_Dyn dyn_mem;
881
0
        GElf_Dyn *dyn = gelf_getdyn (data, j, &dyn_mem);
882
0
        if (dyn != NULL)
883
0
    switch (dyn->d_tag)
884
0
      {
885
0
      case DT_SYMTAB:
886
0
        addrs[i_symtab] = dyn->d_un.d_ptr;
887
0
        continue;
888
889
0
      case DT_HASH:
890
0
        addrs[i_hash] = dyn->d_un.d_ptr;
891
0
        continue;
892
893
0
      case DT_GNU_HASH:
894
0
        addrs[i_gnu_hash] = dyn->d_un.d_ptr;
895
0
        continue;
896
897
0
      case DT_STRTAB:
898
0
        addrs[i_strtab] = dyn->d_un.d_ptr;
899
0
        continue;
900
901
0
      case DT_STRSZ:
902
0
        strsz = dyn->d_un.d_val;
903
0
        continue;
904
905
0
      default:
906
0
        continue;
907
908
0
      case DT_NULL:
909
0
        break;
910
0
      }
911
0
        break;
912
0
      }
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
0
    translate_offs (0, mod, phnum, addrs, strsz, ehdr);
918
0
    if (mod->symfile == NULL)
919
0
      translate_offs (mod->main_bias, mod, phnum, addrs, strsz, ehdr);
920
921
0
    return;
922
0
  }
923
0
    }
924
0
}
925
926
927
#if USE_LZMA
928
/* Try to find the offset between the main file and .gnu_debugdata.  */
929
static bool
930
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
  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
  GElf_Ehdr ehdr_main, ehdr_aux;
939
  if (unlikely (gelf_getehdr (mod->main.elf, &ehdr_main) == NULL)
940
      || unlikely (gelf_getehdr (mod->aux_sym.elf, &ehdr_aux) == NULL))
941
    return false;
942
  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
  if (mod->aux_sym.address_sync != 0)
947
    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
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
0
{
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
  Elf *elf = mod->main.elf;
966
967
  size_t shstrndx;
968
  if (elf_getshdrstrndx (elf, &shstrndx) < 0)
969
    return;
970
971
  Elf_Scn *scn = NULL;
972
  while ((scn = elf_nextscn (elf, scn)) != NULL)
973
    {
974
      GElf_Shdr shdr_mem;
975
      GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
976
      if (shdr == NULL)
977
  return;
978
979
      const char *name = elf_strptr (elf, shstrndx, shdr->sh_name);
980
      if (name == NULL)
981
  return;
982
983
      if (!strcmp (name, ".gnu_debugdata"))
984
  break;
985
    }
986
987
  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
  Elf_Data *rawdata = elf_rawdata (scn, NULL);
993
  if (rawdata == NULL)
994
    return;
995
996
  Dwfl_Error error;
997
  void *buffer = NULL;
998
  size_t size = 0;
999
  error = __libdw_unlzma (-1, 0, rawdata->d_buf, rawdata->d_size,
1000
        &buffer, &size);
1001
  if (error == DWFL_E_NOERROR)
1002
    {
1003
      if (unlikely (size == 0))
1004
  free (buffer);
1005
      else
1006
  {
1007
    mod->aux_sym.elf = elf_memory (buffer, size);
1008
    if (mod->aux_sym.elf == NULL)
1009
      free (buffer);
1010
    else
1011
      {
1012
        mod->aux_sym.fd = -1;
1013
        mod->aux_sym.elf->flags |= ELF_F_MALLOCED;
1014
        if (open_elf (mod, &mod->aux_sym) != DWFL_E_NOERROR)
1015
    return;
1016
        if (! find_aux_address_sync (mod))
1017
    {
1018
      elf_end (mod->aux_sym.elf);
1019
      mod->aux_sym.elf = NULL;
1020
      return;
1021
    }
1022
1023
        /* So far, so good. Get minisymtab table data and cache it. */
1024
        bool minisymtab = false;
1025
        scn = NULL;
1026
        while ((scn = elf_nextscn (mod->aux_sym.elf, scn)) != NULL)
1027
    {
1028
      GElf_Shdr shdr_mem, *shdr = gelf_getshdr (scn, &shdr_mem);
1029
      if (shdr != NULL)
1030
        switch (shdr->sh_type)
1031
          {
1032
          case SHT_SYMTAB:
1033
      if (shdr->sh_entsize == 0)
1034
        return;
1035
      minisymtab = true;
1036
      *aux_symscn = scn;
1037
      *aux_strshndx = shdr->sh_link;
1038
      mod->aux_syments = shdr->sh_size / shdr->sh_entsize;
1039
      mod->aux_first_global = shdr->sh_info;
1040
      if (*aux_xndxscn != NULL)
1041
        return;
1042
      break;
1043
1044
          case SHT_SYMTAB_SHNDX:
1045
      *aux_xndxscn = scn;
1046
      if (minisymtab)
1047
        return;
1048
      break;
1049
1050
          default:
1051
      break;
1052
          }
1053
    }
1054
1055
        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
        *aux_xndxscn = NULL;
1061
        *aux_strshndx = 0;
1062
        mod->aux_syments = 0;
1063
        elf_end (mod->aux_sym.elf);
1064
        mod->aux_sym.elf = NULL;
1065
        return;
1066
      }
1067
  }
1068
    }
1069
  else
1070
    free (buffer);
1071
#endif
1072
0
}
1073
1074
/* Try to find a symbol table in either MOD->main.elf or MOD->debug.elf.  */
1075
static void
1076
find_symtab (Dwfl_Module *mod)
1077
0
{
1078
0
  if (mod->symdata != NULL || mod->aux_symdata != NULL  /* Already done.  */
1079
0
      || mod->symerr != DWFL_E_NOERROR) /* Cached previous failure.  */
1080
0
    return;
1081
1082
0
  __libdwfl_getelf (mod);
1083
0
  mod->symerr = mod->elferr;
1084
0
  if (mod->symerr != DWFL_E_NOERROR)
1085
0
    return;
1086
1087
  /* First see if the main ELF file has the debugging information.  */
1088
0
  Elf_Scn *symscn = NULL, *xndxscn = NULL;
1089
0
  Elf_Scn *aux_symscn = NULL, *aux_xndxscn = NULL;
1090
0
  GElf_Word strshndx, aux_strshndx = 0;
1091
0
  mod->symerr = load_symtab (&mod->main, &mod->symfile, &symscn,
1092
0
           &xndxscn, &mod->syments, &mod->first_global,
1093
0
           &strshndx);
1094
0
  switch (mod->symerr)
1095
0
    {
1096
0
    default:
1097
0
      return;
1098
1099
0
    case DWFL_E_NOERROR:
1100
0
      break;
1101
1102
0
    case DWFL_E_NO_SYMTAB:
1103
      /* Now we have to look for a separate debuginfo file.  */
1104
0
      mod->symerr = find_debuginfo (mod);
1105
0
      switch (mod->symerr)
1106
0
  {
1107
0
  default:
1108
0
    return;
1109
1110
0
  case DWFL_E_NOERROR:
1111
0
    mod->symerr = load_symtab (&mod->debug, &mod->symfile, &symscn,
1112
0
             &xndxscn, &mod->syments,
1113
0
             &mod->first_global, &strshndx);
1114
0
    break;
1115
1116
0
  case DWFL_E_CB:   /* The find_debuginfo hook failed.  */
1117
0
    mod->symerr = DWFL_E_NO_SYMTAB;
1118
0
    break;
1119
0
  }
1120
1121
0
      switch (mod->symerr)
1122
0
  {
1123
0
  default:
1124
0
    return;
1125
1126
0
  case DWFL_E_NOERROR:
1127
0
    break;
1128
1129
0
  case DWFL_E_NO_SYMTAB:
1130
    /* There might be an auxiliary table.  */
1131
0
    find_aux_sym (mod, &aux_symscn, &aux_xndxscn, &aux_strshndx);
1132
1133
0
    if (symscn != NULL)
1134
0
      {
1135
        /* We still have the dynamic symbol table.  */
1136
0
        mod->symerr = DWFL_E_NOERROR;
1137
0
        break;
1138
0
      }
1139
1140
0
    if (aux_symscn != NULL)
1141
0
      {
1142
        /* We still have the auxiliary symbol table.  */
1143
0
        mod->symerr = DWFL_E_NOERROR;
1144
0
        goto aux_cache;
1145
0
      }
1146
1147
    /* Last ditch, look for dynamic symbols without section headers.  */
1148
0
    find_dynsym (mod);
1149
0
    return;
1150
0
  }
1151
0
      break;
1152
0
    }
1153
1154
  /* This does some sanity checks on the string table section.  */
1155
0
  if (elf_strptr (mod->symfile->elf, strshndx, 0) == NULL)
1156
0
    {
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
0
    }
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
0
  Elf_Scn *symstrscn = elf_getscn (mod->symfile->elf, strshndx);
1172
0
  if (symstrscn == NULL)
1173
0
    goto elferr;
1174
1175
0
  GElf_Shdr shdr_mem;
1176
0
  GElf_Shdr *shdr = gelf_getshdr (symstrscn, &shdr_mem);
1177
0
  if (shdr == NULL)
1178
0
    goto elferr;
1179
1180
0
  size_t shstrndx;
1181
0
  if (elf_getshdrstrndx (mod->symfile->elf, &shstrndx) < 0)
1182
0
    goto elferr;
1183
1184
0
  const char *sname = elf_strptr (mod->symfile->elf, shstrndx, shdr->sh_name);
1185
0
  if (sname == NULL)
1186
0
    goto elferr;
1187
1188
0
  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
0
  if ((shdr->sh_flags & SHF_COMPRESSED) != 0)
1194
0
    if (elf_compress (symstrscn, 0, 0) < 0)
1195
0
      goto elferr;
1196
1197
0
  mod->symstrdata = elf_getdata (symstrscn, NULL);
1198
0
  if (mod->symstrdata == NULL || mod->symstrdata->d_buf == NULL)
1199
0
    goto elferr;
1200
0
  else
1201
0
    validate_strdata (mod->symstrdata);
1202
1203
0
  if (xndxscn == NULL)
1204
0
    mod->symxndxdata = NULL;
1205
0
  else
1206
0
    {
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
0
    }
1219
1220
0
  shdr = gelf_getshdr (symscn, &shdr_mem);
1221
0
  if (shdr == NULL)
1222
0
    goto elferr;
1223
1224
0
  if ((shdr->sh_flags & SHF_COMPRESSED) != 0)
1225
0
    if (elf_compress (symscn, 0, 0) < 0)
1226
0
      goto elferr;
1227
1228
0
  mod->symdata = elf_getdata (symscn, NULL);
1229
0
  if (mod->symdata == NULL || mod->symdata->d_buf == NULL)
1230
0
    goto elferr;
1231
1232
  // Sanity check number of symbols.
1233
0
  shdr = gelf_getshdr (symscn, &shdr_mem);
1234
0
  if (shdr == NULL || shdr->sh_entsize == 0
1235
0
      || mod->syments > mod->symdata->d_size / shdr->sh_entsize
1236
0
      || (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
0
  if (aux_symscn != NULL)
1241
0
    {
1242
0
  aux_cache:
1243
      /* This does some sanity checks on the string table section.  */
1244
0
      if (elf_strptr (mod->aux_sym.elf, aux_strshndx, 0) == NULL)
1245
0
  {
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
0
  }
1255
1256
0
      Elf_Scn *aux_strscn = elf_getscn (mod->aux_sym.elf, aux_strshndx);
1257
0
      if (aux_strscn == NULL)
1258
0
  goto elferr;
1259
1260
0
      shdr = gelf_getshdr (aux_strscn, &shdr_mem);
1261
0
      if (shdr == NULL)
1262
0
  goto elferr;
1263
1264
0
      size_t aux_shstrndx;
1265
0
      if (elf_getshdrstrndx (mod->aux_sym.elf, &aux_shstrndx) < 0)
1266
0
  goto elferr;
1267
1268
0
      sname = elf_strptr (mod->aux_sym.elf, aux_shstrndx,
1269
0
              shdr->sh_name);
1270
0
      if (sname == NULL)
1271
0
  goto elferr;
1272
1273
0
      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
0
      if ((shdr->sh_flags & SHF_COMPRESSED) != 0)
1279
0
  if (elf_compress (aux_strscn, 0, 0) < 0)
1280
0
    goto elferr;
1281
1282
0
      mod->aux_symstrdata = elf_getdata (aux_strscn, NULL);
1283
0
      if (mod->aux_symstrdata == NULL || mod->aux_symstrdata->d_buf == NULL)
1284
0
  goto aux_cleanup;
1285
0
      else
1286
0
  validate_strdata (mod->aux_symstrdata);
1287
1288
0
      if (aux_xndxscn == NULL)
1289
0
  mod->aux_symxndxdata = NULL;
1290
0
      else
1291
0
  {
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
0
  }
1305
1306
0
      shdr = gelf_getshdr (aux_symscn, &shdr_mem);
1307
0
      if (shdr == NULL)
1308
0
  goto elferr;
1309
1310
0
      if ((shdr->sh_flags & SHF_COMPRESSED) != 0)
1311
0
  if (elf_compress (aux_symscn, 0, 0) < 0)
1312
0
    goto elferr;
1313
1314
0
      mod->aux_symdata = elf_getdata (aux_symscn, NULL);
1315
0
      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
0
      shdr = gelf_getshdr (aux_symscn, &shdr_mem);
1320
0
      if (mod->aux_syments > mod->aux_symdata->d_size / shdr->sh_entsize
1321
0
    || (size_t) mod->aux_first_global > mod->aux_syments)
1322
0
  goto aux_cleanup;
1323
0
    }
1324
0
}
1325
1326
1327
/* Try to open a libebl backend for MOD.  */
1328
Dwfl_Error
1329
internal_function
1330
__libdwfl_module_getebl (Dwfl_Module *mod)
1331
0
{
1332
0
  if (mod->ebl == NULL)
1333
0
    {
1334
0
      __libdwfl_getelf (mod);
1335
0
      if (mod->elferr != DWFL_E_NOERROR)
1336
0
  return mod->elferr;
1337
1338
0
      mod->ebl = ebl_openbackend (mod->main.elf);
1339
0
      if (mod->ebl == NULL)
1340
0
  return DWFL_E_LIBEBL;
1341
0
    }
1342
0
  return DWFL_E_NOERROR;
1343
0
}
1344
1345
/* Try to start up libdw on DEBUGFILE.  */
1346
static Dwfl_Error
1347
load_dw (Dwfl_Module *mod, struct dwfl_file *debugfile)
1348
7.22k
{
1349
7.22k
  if (mod->e_type == ET_REL && !debugfile->relocated)
1350
1.04k
    {
1351
1.04k
      const Dwfl_Callbacks *const cb = mod->dwfl->callbacks;
1352
1353
      /* The debugging sections have to be relocated.  */
1354
1.04k
      if (cb->section_address == NULL)
1355
1.04k
  return DWFL_E_NOREL;
1356
1357
0
      Dwfl_Error error = __libdwfl_module_getebl (mod);
1358
0
      if (error != DWFL_E_NOERROR)
1359
0
  return error;
1360
1361
0
      find_symtab (mod);
1362
0
      Dwfl_Error result = mod->symerr;
1363
0
      if (result == DWFL_E_NOERROR)
1364
0
  result = __libdwfl_relocate (mod, debugfile->elf, true);
1365
0
      if (result != DWFL_E_NOERROR)
1366
0
  return result;
1367
0
    }
1368
1369
6.17k
  mod->dw = INTUSE(dwarf_begin_elf) (debugfile->elf, DWARF_C_READ, NULL);
1370
6.17k
  if (mod->dw == NULL)
1371
6.03k
    {
1372
6.03k
      int err = INTUSE(dwarf_errno) ();
1373
6.03k
      return err == DWARF_E_NO_DWARF ? DWFL_E_NO_DWARF : DWFL_E (LIBDW, err);
1374
6.03k
    }
1375
1376
  /* Do this after dwarf_begin_elf has a chance to process the fd.  */
1377
138
  if (mod->e_type == ET_REL && !debugfile->relocated)
1378
0
    {
1379
      /* Don't keep the file descriptors around.  */
1380
0
      if (mod->main.fd != -1 && elf_cntl (mod->main.elf, ELF_C_FDREAD) == 0)
1381
0
  {
1382
0
    close (mod->main.fd);
1383
0
    mod->main.fd = -1;
1384
0
  }
1385
0
      if (debugfile->fd != -1 && elf_cntl (debugfile->elf, ELF_C_FDREAD) == 0)
1386
0
  {
1387
0
    close (debugfile->fd);
1388
0
    debugfile->fd = -1;
1389
0
  }
1390
0
    }
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
138
  if (mod->dw->elfpath == NULL && mod->elfpath != NULL
1396
138
      && debugfile == &mod->main)
1397
114
    {
1398
114
      mod->dw->elfpath = strdup (mod->elfpath);
1399
114
      __libdw_set_debugdir (mod->dw);
1400
114
    }
1401
1402
  /* Until we have iterated through all CU's, we might do lazy lookups.  */
1403
138
  mod->lazycu = 1;
1404
1405
138
  return DWFL_E_NOERROR;
1406
6.17k
}
1407
1408
/* Try to start up libdw on either the main file or the debuginfo file.  */
1409
static void
1410
find_dw (Dwfl_Module *mod)
1411
7.22k
{
1412
7.22k
  if (mod->dw != NULL    /* Already done.  */
1413
7.22k
      || mod->dwerr != DWFL_E_NOERROR) /* Cached previous failure.  */
1414
0
    return;
1415
1416
7.22k
  __libdwfl_getelf (mod);
1417
7.22k
  mod->dwerr = mod->elferr;
1418
7.22k
  if (mod->dwerr != DWFL_E_NOERROR)
1419
0
    return;
1420
1421
  /* First see if the main ELF file has the debugging information.  */
1422
7.22k
  mod->dwerr = load_dw (mod, &mod->main);
1423
7.22k
  switch (mod->dwerr)
1424
7.22k
    {
1425
138
    case DWFL_E_NOERROR:
1426
138
      mod->debug.elf = mod->main.elf;
1427
138
      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
138
      find_debug_altlink (mod, mod->main.name);
1433
138
      return;
1434
1435
4.19k
    case DWFL_E_NO_DWARF:
1436
4.19k
      break;
1437
1438
2.88k
    default:
1439
2.88k
      goto canonicalize;
1440
7.22k
    }
1441
1442
  /* Now we have to look for a separate debuginfo file.  */
1443
4.19k
  mod->dwerr = find_debuginfo (mod);
1444
4.19k
  switch (mod->dwerr)
1445
4.19k
    {
1446
0
    case DWFL_E_NOERROR:
1447
0
      mod->dwerr = load_dw (mod, &mod->debug);
1448
0
      if (mod->dwerr == DWFL_E_NOERROR)
1449
0
  {
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
0
    find_debug_altlink (mod, mod->debug.name);
1454
0
    return;
1455
0
  }
1456
1457
0
      break;
1458
1459
4.18k
    case DWFL_E_CB:   /* The find_debuginfo hook failed.  */
1460
4.18k
      mod->dwerr = DWFL_E_NO_DWARF;
1461
4.18k
      return;
1462
1463
8
    default:
1464
8
      break;
1465
4.19k
    }
1466
1467
2.89k
 canonicalize:
1468
2.89k
  mod->dwerr = __libdwfl_canon_error (mod->dwerr);
1469
2.89k
}
1470
1471
Dwarf *
1472
dwfl_module_getdwarf (Dwfl_Module *mod, Dwarf_Addr *bias)
1473
11.0k
{
1474
11.0k
  if (mod == NULL)
1475
3.80k
    return NULL;
1476
1477
7.22k
  find_dw (mod);
1478
7.22k
  if (mod->dwerr == DWFL_E_NOERROR)
1479
138
    {
1480
      /* If dwfl_module_getelf was used previously, then partial apply
1481
   relocation to miscellaneous sections in the debug file too.  */
1482
138
      if (mod->e_type == ET_REL
1483
138
    && mod->main.relocated && ! mod->debug.relocated)
1484
0
  {
1485
0
    mod->debug.relocated = true;
1486
0
    if (mod->debug.elf != mod->main.elf)
1487
0
      (void) __libdwfl_relocate (mod, mod->debug.elf, false);
1488
0
  }
1489
1490
138
      *bias = dwfl_adjusted_dwarf_addr (mod, 0);
1491
138
      return mod->dw;
1492
138
    }
1493
1494
7.08k
  __libdwfl_seterrno (mod->dwerr);
1495
7.08k
  return NULL;
1496
7.22k
}
1497
INTDEF (dwfl_module_getdwarf)
1498
1499
int
1500
dwfl_module_getsymtab (Dwfl_Module *mod)
1501
0
{
1502
0
  if (mod == NULL)
1503
0
    return -1;
1504
1505
0
  find_symtab (mod);
1506
0
  if (mod->symerr == DWFL_E_NOERROR)
1507
    /* We will skip the auxiliary zero entry if there is another one.  */
1508
0
    return (mod->syments + mod->aux_syments
1509
0
      - (mod->syments > 0 && mod->aux_syments > 0 ? 1 : 0));
1510
1511
0
  __libdwfl_seterrno (mod->symerr);
1512
0
  return -1;
1513
0
}
1514
INTDEF (dwfl_module_getsymtab)
1515
1516
int
1517
dwfl_module_getsymtab_first_global (Dwfl_Module *mod)
1518
0
{
1519
0
  if (mod == NULL)
1520
0
    return -1;
1521
1522
0
  find_symtab (mod);
1523
0
  if (mod->symerr == DWFL_E_NOERROR)
1524
0
    {
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
0
      int skip_aux_zero = (mod->syments > 0 && mod->aux_syments > 0) ? 1 : 0;
1531
0
      return mod->first_global + mod->aux_first_global - skip_aux_zero;
1532
0
    }
1533
1534
0
  __libdwfl_seterrno (mod->symerr);
1535
0
  return -1;
1536
0
}
1537
INTDEF (dwfl_module_getsymtab_first_global)