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 */ 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 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 struct debuginfo *debuginfo__new(const char *path) 92 { 93 static const enum dso_binary_type distro_dwarf_types[] = { 94 DSO_BINARY_TYPE__FEDORA_DEBUGINFO, 95 DSO_BINARY_TYPE__UBUNTU_DEBUGINFO, 96 DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO, 97 DSO_BINARY_TYPE__BUILDID_DEBUGINFO, 98 DSO_BINARY_TYPE__MIXEDUP_UBUNTU_DEBUGINFO, 99 DSO_BINARY_TYPE__NOT_FOUND, 100 }; 101 const enum dso_binary_type *type; 102 char buf[PATH_MAX], nil = '\0'; 103 struct dso *dso; 104 struct debuginfo *dinfo = NULL; 105 struct build_id bid = { .size = 0}; 106 107 /* Try to open distro debuginfo files */ 108 dso = dso__new(path); 109 if (!dso) 110 goto out; 111 112 /* 113 * Set the build id for DSO_BINARY_TYPE__BUILDID_DEBUGINFO. Don't block 114 * incase the path isn't for a regular file. 115 */ 116 assert(!dso__has_build_id(dso)); 117 if (filename__read_build_id(path, &bid) > 0) 118 dso__set_build_id(dso, &bid); 119 120 for (type = distro_dwarf_types; 121 !dinfo && *type != DSO_BINARY_TYPE__NOT_FOUND; 122 type++) { 123 if (dso__read_binary_type_filename(dso, *type, &nil, 124 buf, PATH_MAX) < 0) 125 continue; 126 dinfo = __debuginfo__new(buf); 127 } 128 dso__put(dso); 129 130 out: 131 if (dinfo) 132 return dinfo; 133 134 /* if failed to open all distro debuginfo, open given binary */ 135 symbol__join_symfs(buf, path); 136 return __debuginfo__new(buf); 137 } 138 139 void debuginfo__delete(struct debuginfo *dbg) 140 { 141 if (dbg) { 142 if (dbg->dwfl) 143 dwfl_end(dbg->dwfl); 144 free(dbg); 145 } 146 } 147 148 /* For the kernel module, we need a special code to get a DIE */ 149 int debuginfo__get_text_offset(struct debuginfo *dbg, Dwarf_Addr *offs, 150 bool adjust_offset) 151 { 152 int n, i; 153 Elf32_Word shndx; 154 Elf_Scn *scn; 155 Elf *elf; 156 GElf_Shdr mem, *shdr; 157 const char *p; 158 159 elf = dwfl_module_getelf(dbg->mod, &dbg->bias); 160 if (!elf) 161 return -EINVAL; 162 163 /* Get the number of relocations */ 164 n = dwfl_module_relocations(dbg->mod); 165 if (n < 0) 166 return -ENOENT; 167 /* Search the relocation related .text section */ 168 for (i = 0; i < n; i++) { 169 p = dwfl_module_relocation_info(dbg->mod, i, &shndx); 170 if (strcmp(p, ".text") == 0) { 171 /* OK, get the section header */ 172 scn = elf_getscn(elf, shndx); 173 if (!scn) 174 return -ENOENT; 175 shdr = gelf_getshdr(scn, &mem); 176 if (!shdr) 177 return -ENOENT; 178 *offs = shdr->sh_addr; 179 if (adjust_offset) 180 *offs -= shdr->sh_offset; 181 } 182 } 183 return 0; 184 } 185 186 #ifdef HAVE_DEBUGINFOD_SUPPORT 187 int get_source_from_debuginfod(const char *raw_path, 188 const char *sbuild_id, char **new_path) 189 { 190 debuginfod_client *c = debuginfod_begin(); 191 const char *p = raw_path; 192 int fd; 193 194 if (!c) 195 return -ENOMEM; 196 197 fd = debuginfod_find_source(c, (const unsigned char *)sbuild_id, 198 0, p, new_path); 199 pr_debug("Search %s from debuginfod -> %d\n", p, fd); 200 if (fd >= 0) 201 close(fd); 202 debuginfod_end(c); 203 if (fd < 0) { 204 pr_debug("Failed to find %s in debuginfod (%s)\n", 205 raw_path, sbuild_id); 206 return -ENOENT; 207 } 208 pr_debug("Got a source %s\n", *new_path); 209 210 return 0; 211 } 212 #endif /* HAVE_DEBUGINFOD_SUPPORT */ 213