Coverage Report

Created: 2025-07-11 06:46

/src/elfutils/libdwfl/dwfl_build_id_find_debuginfo.c
Line
Count
Source (jump to first uncovered line)
1
/* Find the debuginfo file for a module from its build ID.
2
   Copyright (C) 2007, 2009, 2014 Red Hat, Inc.
3
   This file is part of elfutils.
4
5
   This file is free software; you can redistribute it and/or modify
6
   it under the terms of either
7
8
     * the GNU Lesser General Public License as published by the Free
9
       Software Foundation; either version 3 of the License, or (at
10
       your option) any later version
11
12
   or
13
14
     * the GNU General Public License as published by the Free
15
       Software Foundation; either version 2 of the License, or (at
16
       your option) any later version
17
18
   or both in parallel, as here.
19
20
   elfutils is distributed in the hope that it will be useful, but
21
   WITHOUT ANY WARRANTY; without even the implied warranty of
22
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
23
   General Public License for more details.
24
25
   You should have received copies of the GNU General Public License and
26
   the GNU Lesser General Public License along with this program.  If
27
   not, see <http://www.gnu.org/licenses/>.  */
28
29
#ifdef HAVE_CONFIG_H
30
# include <config.h>
31
#endif
32
33
#include "libdwflP.h"
34
35
int
36
dwfl_build_id_find_debuginfo (Dwfl_Module *mod,
37
            void **userdata __attribute__ ((unused)),
38
            const char *modname __attribute__ ((unused)),
39
            Dwarf_Addr base __attribute__ ((unused)),
40
            const char *file __attribute__ ((unused)),
41
            const char *debuglink __attribute__ ((unused)),
42
            GElf_Word crc __attribute__ ((unused)),
43
            char **debuginfo_file_name)
44
68
{
45
68
  int fd = -1;
46
47
  /* Are we looking for a separate debug file for the main file or for
48
     an alternate (dwz multi) debug file?  Alternatively we could check
49
     whether the dwbias == -1.  */
50
68
  if (mod->dw != NULL)
51
0
    {
52
0
      const void *build_id;
53
0
      const char *altname;
54
0
      ssize_t build_id_len = INTUSE(dwelf_dwarf_gnu_debugaltlink) (mod->dw,
55
0
                   &altname,
56
0
                   &build_id);
57
0
      if (build_id_len > 0)
58
0
  fd = __libdwfl_open_by_build_id (mod, true, debuginfo_file_name,
59
0
           build_id_len, build_id);
60
61
0
      if (fd >= 0)
62
0
  {
63
    /* We need to open an Elf handle on the file so we can check its
64
       build ID note for validation.  Backdoor the handle into the
65
       module data structure since we had to open it early anyway.  */
66
0
    Dwfl_Error error = __libdw_open_file (&fd, &mod->alt_elf,
67
0
            true, false);
68
0
    if (error != DWFL_E_NOERROR)
69
0
      __libdwfl_seterrno (error);
70
0
    else
71
0
      {
72
0
        const void *alt_build_id;
73
0
        ssize_t alt_len = INTUSE(dwelf_elf_gnu_build_id) (mod->alt_elf,
74
0
                &alt_build_id);
75
0
        if (alt_len > 0 && alt_len == build_id_len
76
0
      && memcmp (build_id, alt_build_id, alt_len) == 0)
77
0
    return fd;
78
0
        else
79
0
    {
80
      /* A mismatch!  */
81
0
      elf_end (mod->alt_elf);
82
0
      mod->alt_elf = NULL;
83
0
      close (fd);
84
0
      fd = -1;
85
0
    }
86
0
        free (*debuginfo_file_name);
87
0
        *debuginfo_file_name = NULL;
88
0
        errno = 0;
89
0
      }
90
0
  }
91
0
      return fd;
92
0
    }
93
94
  /* We don't even have the Dwarf yet and it isn't in the main file.
95
     Try to find separate debug file now using the module build id.  */
96
68
  const unsigned char *bits;
97
68
  GElf_Addr vaddr;
98
99
68
  if (INTUSE(dwfl_module_build_id) (mod, &bits, &vaddr) > 0)
100
68
    fd = __libdwfl_open_mod_by_build_id (mod, true, debuginfo_file_name);
101
68
  if (fd >= 0)
102
0
    {
103
      /* We need to open an Elf handle on the file so we can check its
104
   build ID note for validation.  Backdoor the handle into the
105
   module data structure since we had to open it early anyway.  */
106
0
      Dwfl_Error error = __libdw_open_file (&fd, &mod->debug.elf, true, false);
107
0
      if (error != DWFL_E_NOERROR)
108
0
  __libdwfl_seterrno (error);
109
0
      else if (likely (__libdwfl_find_build_id (mod, false,
110
0
            mod->debug.elf) == 2))
111
0
  {
112
    /* Also backdoor the gratuitous flag.  */
113
0
    mod->debug.valid = true;
114
0
    return fd;
115
0
  }
116
0
      else
117
0
  {
118
    /* A mismatch!  */
119
0
    elf_end (mod->debug.elf);
120
0
    mod->debug.elf = NULL;
121
0
    close (fd);
122
0
    fd = -1;
123
0
  }
124
0
      free (*debuginfo_file_name);
125
0
      *debuginfo_file_name = NULL;
126
0
      errno = 0;
127
0
    }
128
68
  return fd;
129
68
}
130
INTDEF (dwfl_build_id_find_debuginfo)