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 bool fd_consumed = false; 46 47 fd = open(path, O_RDONLY); 48 if (fd < 0) 49 return fd; 50 51 dbg->dwfl = dwfl_begin(&offline_callbacks); 52 if (!dbg->dwfl) 53 goto error; 54 55 dwfl_report_begin(dbg->dwfl); 56 dbg->mod = dwfl_report_offline(dbg->dwfl, "", "", fd); 57 if (!dbg->mod) 58 goto error; 59 fd_consumed = true; 60 61 dbg->dbg = dwfl_module_getdwarf(dbg->mod, &dbg->bias); 62 if (!dbg->dbg) 63 goto error; 64 65 dwfl_module_build_id(dbg->mod, &dbg->build_id, &dummy); 66 67 if (dwfl_report_end(dbg->dwfl, NULL, NULL) != 0) 68 goto error; 69 70 return 0; 71 error: 72 if (dbg->dwfl) 73 dwfl_end(dbg->dwfl); 74 if (!fd_consumed) 75 close(fd); 76 memset(dbg, 0, sizeof(*dbg)); 77 78 return -ENOENT; 79 } 80 81 static struct debuginfo *__debuginfo__new(const char *path) 82 { 83 struct debuginfo *dbg = zalloc(sizeof(*dbg)); 84 if (!dbg) 85 return NULL; 86 87 if (debuginfo__init_offline_dwarf(dbg, path) < 0) 88 zfree(&dbg); 89 if (dbg) 90 pr_debug("Open Debuginfo file: %s\n", path); 91 return dbg; 92 } 93 94 struct debuginfo *debuginfo__new(struct dso *dso) 95 { 96 struct debuginfo *dinfo; 97 char *path; 98 bool decomp = false; 99 100 dso__find_dbginfo_type(dso); 101 path = dso__get_filename(dso, "", &decomp, dso__dbginfo_type(dso)); 102 if (path == NULL) 103 return NULL; 104 105 dinfo = __debuginfo__new(path); 106 dso__put_filename(dso, path, decomp); 107 return dinfo; 108 } 109 110 struct debuginfo *debuginfo__from_path(const char *path) 111 { 112 struct dso *dso; 113 struct debuginfo *dinfo; 114 struct build_id bid = { .size = 0 }; 115 116 /* Try to open distro debuginfo files */ 117 dso = dso__new(path); 118 if (!dso) 119 return NULL; 120 121 /* 122 * Set the build id for DSO_BINARY_TYPE__BUILDID_DEBUGINFO. Don't block 123 * incase the path isn't for a regular file. 124 */ 125 assert(!dso__has_build_id(dso)); 126 if (filename__read_build_id(path, &bid) > 0) 127 dso__set_build_id(dso, &bid); 128 129 dinfo = debuginfo__new(dso); 130 dso__put(dso); 131 132 return dinfo; 133 } 134 135 void debuginfo__delete(struct debuginfo *dbg) 136 { 137 if (dbg) { 138 if (dbg->dwfl) 139 dwfl_end(dbg->dwfl); 140 free(dbg); 141 } 142 } 143 144 /* For the kernel module, we need a special code to get a DIE */ 145 int debuginfo__get_text_offset(struct debuginfo *dbg, Dwarf_Addr *offs, 146 bool adjust_offset) 147 { 148 int n, i; 149 Elf32_Word shndx; 150 Elf_Scn *scn; 151 Elf *elf; 152 GElf_Shdr mem, *shdr; 153 const char *p; 154 155 elf = dwfl_module_getelf(dbg->mod, &dbg->bias); 156 if (!elf) 157 return -EINVAL; 158 159 /* Get the number of relocations */ 160 n = dwfl_module_relocations(dbg->mod); 161 if (n < 0) 162 return -ENOENT; 163 /* Search the relocation related .text section */ 164 for (i = 0; i < n; i++) { 165 p = dwfl_module_relocation_info(dbg->mod, i, &shndx); 166 if (p && strcmp(p, ".text") == 0) { 167 /* OK, get the section header */ 168 scn = elf_getscn(elf, shndx); 169 if (!scn) 170 return -ENOENT; 171 shdr = gelf_getshdr(scn, &mem); 172 if (!shdr) 173 return -ENOENT; 174 *offs = shdr->sh_addr; 175 if (adjust_offset) 176 *offs -= shdr->sh_offset; 177 } 178 } 179 return 0; 180 } 181 182 #ifdef HAVE_DEBUGINFOD_SUPPORT 183 int get_source_from_debuginfod(const char *raw_path, 184 const char *sbuild_id, char **new_path) 185 { 186 debuginfod_client *c = debuginfod_begin(); 187 const char *p = raw_path; 188 int fd; 189 190 if (!c) 191 return -ENOMEM; 192 193 fd = debuginfod_find_source(c, (const unsigned char *)sbuild_id, 194 0, p, new_path); 195 pr_debug("Search %s from debuginfod -> %d\n", p, fd); 196 if (fd >= 0) 197 close(fd); 198 debuginfod_end(c); 199 if (fd < 0) { 200 pr_debug("Failed to find %s in debuginfod (%s)\n", 201 raw_path, sbuild_id); 202 return -ENOENT; 203 } 204 pr_debug("Got a source %s\n", *new_path); 205 206 return 0; 207 } 208 #endif /* HAVE_DEBUGINFOD_SUPPORT */ 209