1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * DWARF debug information handling code. Copied from probe-finder.c.
4 *
5 * Written by Masami Hiramatsu <mhiramat@redhat.com>
6 */
7
8 #include <errno.h>
9 #include <fcntl.h>
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include <unistd.h>
14 #include <linux/zalloc.h>
15
16 #include "build-id.h"
17 #include "dso.h"
18 #include "debug.h"
19 #include "debuginfo.h"
20 #include "symbol.h"
21
22 #ifdef HAVE_DEBUGINFOD_SUPPORT
23 #include <elfutils/debuginfod.h>
24 #endif
25
26 /* Dwarf FL wrappers */
27 static char *debuginfo_path; /* Currently dummy */
28
29 static const Dwfl_Callbacks offline_callbacks = {
30 .find_debuginfo = dwfl_standard_find_debuginfo,
31 .debuginfo_path = &debuginfo_path,
32
33 .section_address = dwfl_offline_section_address,
34
35 /* We use this table for core files too. */
36 .find_elf = dwfl_build_id_find_elf,
37 };
38
39 /* Get a Dwarf from offline image */
debuginfo__init_offline_dwarf(struct debuginfo * dbg,const char * path)40 static int debuginfo__init_offline_dwarf(struct debuginfo *dbg,
41 const char *path)
42 {
43 GElf_Addr dummy;
44 int fd;
45
46 fd = open(path, O_RDONLY);
47 if (fd < 0)
48 return fd;
49
50 dbg->dwfl = dwfl_begin(&offline_callbacks);
51 if (!dbg->dwfl)
52 goto error;
53
54 dwfl_report_begin(dbg->dwfl);
55 dbg->mod = dwfl_report_offline(dbg->dwfl, "", "", fd);
56 if (!dbg->mod)
57 goto error;
58
59 dbg->dbg = dwfl_module_getdwarf(dbg->mod, &dbg->bias);
60 if (!dbg->dbg)
61 goto error;
62
63 dwfl_module_build_id(dbg->mod, &dbg->build_id, &dummy);
64
65 dwfl_report_end(dbg->dwfl, NULL, NULL);
66
67 return 0;
68 error:
69 if (dbg->dwfl)
70 dwfl_end(dbg->dwfl);
71 else
72 close(fd);
73 memset(dbg, 0, sizeof(*dbg));
74
75 return -ENOENT;
76 }
77
__debuginfo__new(const char * path)78 static struct debuginfo *__debuginfo__new(const char *path)
79 {
80 struct debuginfo *dbg = zalloc(sizeof(*dbg));
81 if (!dbg)
82 return NULL;
83
84 if (debuginfo__init_offline_dwarf(dbg, path) < 0)
85 zfree(&dbg);
86 if (dbg)
87 pr_debug("Open Debuginfo file: %s\n", path);
88 return dbg;
89 }
90
91 enum dso_binary_type distro_dwarf_types[] = {
92 DSO_BINARY_TYPE__FEDORA_DEBUGINFO,
93 DSO_BINARY_TYPE__UBUNTU_DEBUGINFO,
94 DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO,
95 DSO_BINARY_TYPE__BUILDID_DEBUGINFO,
96 DSO_BINARY_TYPE__MIXEDUP_UBUNTU_DEBUGINFO,
97 DSO_BINARY_TYPE__NOT_FOUND,
98 };
99
debuginfo__new(const char * path)100 struct debuginfo *debuginfo__new(const char *path)
101 {
102 enum dso_binary_type *type;
103 char buf[PATH_MAX], nil = '\0';
104 struct dso *dso;
105 struct debuginfo *dinfo = NULL;
106 struct build_id bid;
107
108 /* Try to open distro debuginfo files */
109 dso = dso__new(path);
110 if (!dso)
111 goto out;
112
113 /* Set the build id for DSO_BINARY_TYPE__BUILDID_DEBUGINFO */
114 if (is_regular_file(path) && filename__read_build_id(path, &bid) > 0)
115 dso__set_build_id(dso, &bid);
116
117 for (type = distro_dwarf_types;
118 !dinfo && *type != DSO_BINARY_TYPE__NOT_FOUND;
119 type++) {
120 if (dso__read_binary_type_filename(dso, *type, &nil,
121 buf, PATH_MAX) < 0)
122 continue;
123 dinfo = __debuginfo__new(buf);
124 }
125 dso__put(dso);
126
127 out:
128 if (dinfo)
129 return dinfo;
130
131 /* if failed to open all distro debuginfo, open given binary */
132 symbol__join_symfs(buf, path);
133 return __debuginfo__new(buf);
134 }
135
debuginfo__delete(struct debuginfo * dbg)136 void debuginfo__delete(struct debuginfo *dbg)
137 {
138 if (dbg) {
139 if (dbg->dwfl)
140 dwfl_end(dbg->dwfl);
141 free(dbg);
142 }
143 }
144
145 /* For the kernel module, we need a special code to get a DIE */
debuginfo__get_text_offset(struct debuginfo * dbg,Dwarf_Addr * offs,bool adjust_offset)146 int debuginfo__get_text_offset(struct debuginfo *dbg, Dwarf_Addr *offs,
147 bool adjust_offset)
148 {
149 int n, i;
150 Elf32_Word shndx;
151 Elf_Scn *scn;
152 Elf *elf;
153 GElf_Shdr mem, *shdr;
154 const char *p;
155
156 elf = dwfl_module_getelf(dbg->mod, &dbg->bias);
157 if (!elf)
158 return -EINVAL;
159
160 /* Get the number of relocations */
161 n = dwfl_module_relocations(dbg->mod);
162 if (n < 0)
163 return -ENOENT;
164 /* Search the relocation related .text section */
165 for (i = 0; i < n; i++) {
166 p = dwfl_module_relocation_info(dbg->mod, i, &shndx);
167 if (strcmp(p, ".text") == 0) {
168 /* OK, get the section header */
169 scn = elf_getscn(elf, shndx);
170 if (!scn)
171 return -ENOENT;
172 shdr = gelf_getshdr(scn, &mem);
173 if (!shdr)
174 return -ENOENT;
175 *offs = shdr->sh_addr;
176 if (adjust_offset)
177 *offs -= shdr->sh_offset;
178 }
179 }
180 return 0;
181 }
182
183 #ifdef HAVE_DEBUGINFOD_SUPPORT
get_source_from_debuginfod(const char * raw_path,const char * sbuild_id,char ** new_path)184 int get_source_from_debuginfod(const char *raw_path,
185 const char *sbuild_id, char **new_path)
186 {
187 debuginfod_client *c = debuginfod_begin();
188 const char *p = raw_path;
189 int fd;
190
191 if (!c)
192 return -ENOMEM;
193
194 fd = debuginfod_find_source(c, (const unsigned char *)sbuild_id,
195 0, p, new_path);
196 pr_debug("Search %s from debuginfod -> %d\n", p, fd);
197 if (fd >= 0)
198 close(fd);
199 debuginfod_end(c);
200 if (fd < 0) {
201 pr_debug("Failed to find %s in debuginfod (%s)\n",
202 raw_path, sbuild_id);
203 return -ENOENT;
204 }
205 pr_debug("Got a source %s\n", *new_path);
206
207 return 0;
208 }
209 #endif /* HAVE_DEBUGINFOD_SUPPORT */
210