Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 1 | #include "util.h" |
| 2 | #include "../perf.h" |
Arnaldo Carvalho de Melo | a0055ae | 2009-06-01 17:50:19 -0300 | [diff] [blame] | 3 | #include "string.h" |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 4 | #include "symbol.h" |
| 5 | |
| 6 | #include <libelf.h> |
| 7 | #include <gelf.h> |
| 8 | #include <elf.h> |
| 9 | |
| 10 | static struct symbol *symbol__new(uint64_t start, uint64_t len, |
Arnaldo Carvalho de Melo | 0085c954 | 2009-05-28 14:55:13 -0300 | [diff] [blame] | 11 | const char *name, unsigned int priv_size) |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 12 | { |
Arnaldo Carvalho de Melo | 0085c954 | 2009-05-28 14:55:13 -0300 | [diff] [blame] | 13 | size_t namelen = strlen(name) + 1; |
| 14 | struct symbol *self = malloc(priv_size + sizeof(*self) + namelen); |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 15 | |
| 16 | if (self != NULL) { |
Arnaldo Carvalho de Melo | 0085c954 | 2009-05-28 14:55:13 -0300 | [diff] [blame] | 17 | if (priv_size) { |
| 18 | memset(self, 0, priv_size); |
| 19 | self = ((void *)self) + priv_size; |
| 20 | } |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 21 | self->start = start; |
Arnaldo Carvalho de Melo | 18374ab | 2009-06-03 14:49:21 -0300 | [diff] [blame] | 22 | self->end = start + len - 1; |
Arnaldo Carvalho de Melo | 0085c954 | 2009-05-28 14:55:13 -0300 | [diff] [blame] | 23 | memcpy(self->name, name, namelen); |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 24 | } |
| 25 | |
| 26 | return self; |
| 27 | } |
| 28 | |
Arnaldo Carvalho de Melo | 0085c954 | 2009-05-28 14:55:13 -0300 | [diff] [blame] | 29 | static void symbol__delete(struct symbol *self, unsigned int priv_size) |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 30 | { |
Arnaldo Carvalho de Melo | 0085c954 | 2009-05-28 14:55:13 -0300 | [diff] [blame] | 31 | free(((void *)self) - priv_size); |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | static size_t symbol__fprintf(struct symbol *self, FILE *fp) |
| 35 | { |
| 36 | return fprintf(fp, " %llx-%llx %s\n", |
| 37 | self->start, self->end, self->name); |
| 38 | } |
| 39 | |
Arnaldo Carvalho de Melo | 0085c954 | 2009-05-28 14:55:13 -0300 | [diff] [blame] | 40 | struct dso *dso__new(const char *name, unsigned int sym_priv_size) |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 41 | { |
| 42 | struct dso *self = malloc(sizeof(*self) + strlen(name) + 1); |
| 43 | |
| 44 | if (self != NULL) { |
| 45 | strcpy(self->name, name); |
| 46 | self->syms = RB_ROOT; |
Arnaldo Carvalho de Melo | 0085c954 | 2009-05-28 14:55:13 -0300 | [diff] [blame] | 47 | self->sym_priv_size = sym_priv_size; |
Peter Zijlstra | fc54db5 | 2009-06-05 14:04:59 +0200 | [diff] [blame^] | 48 | self->find_symbol = dso__find_symbol; |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | return self; |
| 52 | } |
| 53 | |
| 54 | static void dso__delete_symbols(struct dso *self) |
| 55 | { |
| 56 | struct symbol *pos; |
| 57 | struct rb_node *next = rb_first(&self->syms); |
| 58 | |
| 59 | while (next) { |
| 60 | pos = rb_entry(next, struct symbol, rb_node); |
| 61 | next = rb_next(&pos->rb_node); |
Arnaldo Carvalho de Melo | c8c9652 | 2009-06-01 17:50:57 -0300 | [diff] [blame] | 62 | rb_erase(&pos->rb_node, &self->syms); |
Arnaldo Carvalho de Melo | 0085c954 | 2009-05-28 14:55:13 -0300 | [diff] [blame] | 63 | symbol__delete(pos, self->sym_priv_size); |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 64 | } |
| 65 | } |
| 66 | |
| 67 | void dso__delete(struct dso *self) |
| 68 | { |
| 69 | dso__delete_symbols(self); |
| 70 | free(self); |
| 71 | } |
| 72 | |
| 73 | static void dso__insert_symbol(struct dso *self, struct symbol *sym) |
| 74 | { |
| 75 | struct rb_node **p = &self->syms.rb_node; |
| 76 | struct rb_node *parent = NULL; |
| 77 | const uint64_t ip = sym->start; |
| 78 | struct symbol *s; |
| 79 | |
| 80 | while (*p != NULL) { |
| 81 | parent = *p; |
| 82 | s = rb_entry(parent, struct symbol, rb_node); |
| 83 | if (ip < s->start) |
| 84 | p = &(*p)->rb_left; |
| 85 | else |
| 86 | p = &(*p)->rb_right; |
| 87 | } |
| 88 | rb_link_node(&sym->rb_node, parent, p); |
| 89 | rb_insert_color(&sym->rb_node, &self->syms); |
| 90 | } |
| 91 | |
| 92 | struct symbol *dso__find_symbol(struct dso *self, uint64_t ip) |
| 93 | { |
| 94 | struct rb_node *n; |
| 95 | |
| 96 | if (self == NULL) |
| 97 | return NULL; |
| 98 | |
| 99 | n = self->syms.rb_node; |
| 100 | |
| 101 | while (n) { |
| 102 | struct symbol *s = rb_entry(n, struct symbol, rb_node); |
| 103 | |
| 104 | if (ip < s->start) |
| 105 | n = n->rb_left; |
| 106 | else if (ip > s->end) |
| 107 | n = n->rb_right; |
| 108 | else |
| 109 | return s; |
| 110 | } |
| 111 | |
| 112 | return NULL; |
| 113 | } |
| 114 | |
| 115 | size_t dso__fprintf(struct dso *self, FILE *fp) |
| 116 | { |
| 117 | size_t ret = fprintf(fp, "dso: %s\n", self->name); |
| 118 | |
| 119 | struct rb_node *nd; |
| 120 | for (nd = rb_first(&self->syms); nd; nd = rb_next(nd)) { |
| 121 | struct symbol *pos = rb_entry(nd, struct symbol, rb_node); |
| 122 | ret += symbol__fprintf(pos, fp); |
| 123 | } |
| 124 | |
| 125 | return ret; |
| 126 | } |
| 127 | |
Ingo Molnar | bd74137 | 2009-06-04 14:13:04 +0200 | [diff] [blame] | 128 | static int dso__load_kallsyms(struct dso *self, symbol_filter_t filter, int verbose) |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 129 | { |
| 130 | struct rb_node *nd, *prevnd; |
| 131 | char *line = NULL; |
| 132 | size_t n; |
| 133 | FILE *file = fopen("/proc/kallsyms", "r"); |
| 134 | |
| 135 | if (file == NULL) |
| 136 | goto out_failure; |
| 137 | |
| 138 | while (!feof(file)) { |
Arnaldo Carvalho de Melo | a0055ae | 2009-06-01 17:50:19 -0300 | [diff] [blame] | 139 | __u64 start; |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 140 | struct symbol *sym; |
| 141 | int line_len, len; |
| 142 | char symbol_type; |
| 143 | |
| 144 | line_len = getline(&line, &n, file); |
| 145 | if (line_len < 0) |
| 146 | break; |
| 147 | |
| 148 | if (!line) |
| 149 | goto out_failure; |
| 150 | |
| 151 | line[--line_len] = '\0'; /* \n */ |
| 152 | |
Arnaldo Carvalho de Melo | a0055ae | 2009-06-01 17:50:19 -0300 | [diff] [blame] | 153 | len = hex2u64(line, &start); |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 154 | |
| 155 | len++; |
| 156 | if (len + 2 >= line_len) |
| 157 | continue; |
| 158 | |
| 159 | symbol_type = toupper(line[len]); |
| 160 | /* |
| 161 | * We're interested only in code ('T'ext) |
| 162 | */ |
| 163 | if (symbol_type != 'T' && symbol_type != 'W') |
| 164 | continue; |
| 165 | /* |
| 166 | * Well fix up the end later, when we have all sorted. |
| 167 | */ |
Arnaldo Carvalho de Melo | 0085c954 | 2009-05-28 14:55:13 -0300 | [diff] [blame] | 168 | sym = symbol__new(start, 0xdead, line + len + 2, |
| 169 | self->sym_priv_size); |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 170 | |
| 171 | if (sym == NULL) |
| 172 | goto out_delete_line; |
| 173 | |
Arnaldo Carvalho de Melo | 69ee69f | 2009-05-28 14:55:26 -0300 | [diff] [blame] | 174 | if (filter && filter(self, sym)) |
| 175 | symbol__delete(sym, self->sym_priv_size); |
| 176 | else |
| 177 | dso__insert_symbol(self, sym); |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | /* |
| 181 | * Now that we have all sorted out, just set the ->end of all |
| 182 | * symbols |
| 183 | */ |
| 184 | prevnd = rb_first(&self->syms); |
| 185 | |
| 186 | if (prevnd == NULL) |
| 187 | goto out_delete_line; |
| 188 | |
| 189 | for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) { |
| 190 | struct symbol *prev = rb_entry(prevnd, struct symbol, rb_node), |
| 191 | *curr = rb_entry(nd, struct symbol, rb_node); |
| 192 | |
| 193 | prev->end = curr->start - 1; |
| 194 | prevnd = nd; |
| 195 | } |
| 196 | |
| 197 | free(line); |
| 198 | fclose(file); |
| 199 | |
| 200 | return 0; |
| 201 | |
| 202 | out_delete_line: |
| 203 | free(line); |
| 204 | out_failure: |
| 205 | return -1; |
| 206 | } |
| 207 | |
| 208 | /** |
| 209 | * elf_symtab__for_each_symbol - iterate thru all the symbols |
| 210 | * |
| 211 | * @self: struct elf_symtab instance to iterate |
| 212 | * @index: uint32_t index |
| 213 | * @sym: GElf_Sym iterator |
| 214 | */ |
| 215 | #define elf_symtab__for_each_symbol(syms, nr_syms, index, sym) \ |
| 216 | for (index = 0, gelf_getsym(syms, index, &sym);\ |
| 217 | index < nr_syms; \ |
| 218 | index++, gelf_getsym(syms, index, &sym)) |
| 219 | |
| 220 | static inline uint8_t elf_sym__type(const GElf_Sym *sym) |
| 221 | { |
| 222 | return GELF_ST_TYPE(sym->st_info); |
| 223 | } |
| 224 | |
| 225 | static inline int elf_sym__is_function(const GElf_Sym *sym) |
| 226 | { |
| 227 | return elf_sym__type(sym) == STT_FUNC && |
| 228 | sym->st_name != 0 && |
| 229 | sym->st_shndx != SHN_UNDEF && |
| 230 | sym->st_size != 0; |
| 231 | } |
| 232 | |
| 233 | static inline const char *elf_sym__name(const GElf_Sym *sym, |
| 234 | const Elf_Data *symstrs) |
| 235 | { |
| 236 | return symstrs->d_buf + sym->st_name; |
| 237 | } |
| 238 | |
| 239 | static Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep, |
| 240 | GElf_Shdr *shp, const char *name, |
| 241 | size_t *index) |
| 242 | { |
| 243 | Elf_Scn *sec = NULL; |
| 244 | size_t cnt = 1; |
| 245 | |
| 246 | while ((sec = elf_nextscn(elf, sec)) != NULL) { |
| 247 | char *str; |
| 248 | |
| 249 | gelf_getshdr(sec, shp); |
| 250 | str = elf_strptr(elf, ep->e_shstrndx, shp->sh_name); |
| 251 | if (!strcmp(name, str)) { |
| 252 | if (index) |
| 253 | *index = cnt; |
| 254 | break; |
| 255 | } |
| 256 | ++cnt; |
| 257 | } |
| 258 | |
| 259 | return sec; |
| 260 | } |
| 261 | |
Arnaldo Carvalho de Melo | 8ce998d | 2009-06-03 00:54:33 -0300 | [diff] [blame] | 262 | #define elf_section__for_each_rel(reldata, pos, pos_mem, idx, nr_entries) \ |
| 263 | for (idx = 0, pos = gelf_getrel(reldata, 0, &pos_mem); \ |
| 264 | idx < nr_entries; \ |
| 265 | ++idx, pos = gelf_getrel(reldata, idx, &pos_mem)) |
| 266 | |
| 267 | #define elf_section__for_each_rela(reldata, pos, pos_mem, idx, nr_entries) \ |
| 268 | for (idx = 0, pos = gelf_getrela(reldata, 0, &pos_mem); \ |
| 269 | idx < nr_entries; \ |
| 270 | ++idx, pos = gelf_getrela(reldata, idx, &pos_mem)) |
| 271 | |
| 272 | static int dso__synthesize_plt_symbols(struct dso *self, Elf *elf, |
| 273 | GElf_Ehdr *ehdr, Elf_Scn *scn_dynsym, |
| 274 | GElf_Shdr *shdr_dynsym, |
| 275 | size_t dynsym_idx) |
| 276 | { |
| 277 | uint32_t nr_rel_entries, idx; |
| 278 | GElf_Sym sym; |
| 279 | __u64 plt_offset; |
| 280 | GElf_Shdr shdr_plt; |
| 281 | struct symbol *f; |
| 282 | GElf_Shdr shdr_rel_plt; |
| 283 | Elf_Data *reldata, *syms, *symstrs; |
| 284 | Elf_Scn *scn_plt_rel, *scn_symstrs; |
| 285 | char sympltname[1024]; |
| 286 | int nr = 0, symidx; |
| 287 | |
| 288 | scn_plt_rel = elf_section_by_name(elf, ehdr, &shdr_rel_plt, |
| 289 | ".rela.plt", NULL); |
| 290 | if (scn_plt_rel == NULL) { |
| 291 | scn_plt_rel = elf_section_by_name(elf, ehdr, &shdr_rel_plt, |
| 292 | ".rel.plt", NULL); |
| 293 | if (scn_plt_rel == NULL) |
| 294 | return 0; |
| 295 | } |
| 296 | |
| 297 | if (shdr_rel_plt.sh_link != dynsym_idx) |
| 298 | return 0; |
| 299 | |
| 300 | if (elf_section_by_name(elf, ehdr, &shdr_plt, ".plt", NULL) == NULL) |
| 301 | return 0; |
| 302 | |
| 303 | /* |
| 304 | * Fetch the relocation section to find the indexes to the GOT |
| 305 | * and the symbols in the .dynsym they refer to. |
| 306 | */ |
| 307 | reldata = elf_getdata(scn_plt_rel, NULL); |
| 308 | if (reldata == NULL) |
| 309 | return -1; |
| 310 | |
| 311 | syms = elf_getdata(scn_dynsym, NULL); |
| 312 | if (syms == NULL) |
| 313 | return -1; |
| 314 | |
| 315 | scn_symstrs = elf_getscn(elf, shdr_dynsym->sh_link); |
| 316 | if (scn_symstrs == NULL) |
| 317 | return -1; |
| 318 | |
| 319 | symstrs = elf_getdata(scn_symstrs, NULL); |
| 320 | if (symstrs == NULL) |
| 321 | return -1; |
| 322 | |
| 323 | nr_rel_entries = shdr_rel_plt.sh_size / shdr_rel_plt.sh_entsize; |
| 324 | plt_offset = shdr_plt.sh_offset; |
| 325 | |
| 326 | if (shdr_rel_plt.sh_type == SHT_RELA) { |
| 327 | GElf_Rela pos_mem, *pos; |
| 328 | |
| 329 | elf_section__for_each_rela(reldata, pos, pos_mem, idx, |
| 330 | nr_rel_entries) { |
| 331 | symidx = GELF_R_SYM(pos->r_info); |
| 332 | plt_offset += shdr_plt.sh_entsize; |
| 333 | gelf_getsym(syms, symidx, &sym); |
| 334 | snprintf(sympltname, sizeof(sympltname), |
| 335 | "%s@plt", elf_sym__name(&sym, symstrs)); |
| 336 | |
| 337 | f = symbol__new(plt_offset, shdr_plt.sh_entsize, |
| 338 | sympltname, self->sym_priv_size); |
| 339 | if (!f) |
| 340 | return -1; |
| 341 | |
| 342 | dso__insert_symbol(self, f); |
| 343 | ++nr; |
| 344 | } |
| 345 | } else if (shdr_rel_plt.sh_type == SHT_REL) { |
| 346 | GElf_Rel pos_mem, *pos; |
| 347 | elf_section__for_each_rel(reldata, pos, pos_mem, idx, |
| 348 | nr_rel_entries) { |
| 349 | symidx = GELF_R_SYM(pos->r_info); |
| 350 | plt_offset += shdr_plt.sh_entsize; |
| 351 | gelf_getsym(syms, symidx, &sym); |
| 352 | snprintf(sympltname, sizeof(sympltname), |
| 353 | "%s@plt", elf_sym__name(&sym, symstrs)); |
| 354 | |
| 355 | f = symbol__new(plt_offset, shdr_plt.sh_entsize, |
| 356 | sympltname, self->sym_priv_size); |
| 357 | if (!f) |
| 358 | return -1; |
| 359 | |
| 360 | dso__insert_symbol(self, f); |
| 361 | ++nr; |
| 362 | } |
| 363 | } else { |
| 364 | /* |
| 365 | * TODO: There are still one more shdr_rel_plt.sh_type |
| 366 | * I have to investigate, but probably should be ignored. |
| 367 | */ |
| 368 | } |
| 369 | |
| 370 | return nr; |
| 371 | } |
| 372 | |
Arnaldo Carvalho de Melo | 69ee69f | 2009-05-28 14:55:26 -0300 | [diff] [blame] | 373 | static int dso__load_sym(struct dso *self, int fd, const char *name, |
Ingo Molnar | bd74137 | 2009-06-04 14:13:04 +0200 | [diff] [blame] | 374 | symbol_filter_t filter, int verbose) |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 375 | { |
| 376 | Elf_Data *symstrs; |
| 377 | uint32_t nr_syms; |
| 378 | int err = -1; |
| 379 | uint32_t index; |
| 380 | GElf_Ehdr ehdr; |
| 381 | GElf_Shdr shdr; |
| 382 | Elf_Data *syms; |
| 383 | GElf_Sym sym; |
Arnaldo Carvalho de Melo | 8ce998d | 2009-06-03 00:54:33 -0300 | [diff] [blame] | 384 | Elf_Scn *sec, *sec_dynsym; |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 385 | Elf *elf; |
Arnaldo Carvalho de Melo | 8ce998d | 2009-06-03 00:54:33 -0300 | [diff] [blame] | 386 | size_t dynsym_idx; |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 387 | int nr = 0; |
| 388 | |
| 389 | elf = elf_begin(fd, ELF_C_READ_MMAP, NULL); |
| 390 | if (elf == NULL) { |
Ingo Molnar | bd74137 | 2009-06-04 14:13:04 +0200 | [diff] [blame] | 391 | if (verbose) |
| 392 | fprintf(stderr, "%s: cannot read %s ELF file.\n", |
| 393 | __func__, name); |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 394 | goto out_close; |
| 395 | } |
| 396 | |
| 397 | if (gelf_getehdr(elf, &ehdr) == NULL) { |
Ingo Molnar | bd74137 | 2009-06-04 14:13:04 +0200 | [diff] [blame] | 398 | if (verbose) |
| 399 | fprintf(stderr, "%s: cannot get elf header.\n", __func__); |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 400 | goto out_elf_end; |
| 401 | } |
| 402 | |
Arnaldo Carvalho de Melo | 8ce998d | 2009-06-03 00:54:33 -0300 | [diff] [blame] | 403 | /* |
| 404 | * We need to check if we have a .dynsym, so that we can handle the |
| 405 | * .plt, synthesizing its symbols, that aren't on the symtabs (be it |
| 406 | * .dynsym or .symtab) |
| 407 | */ |
| 408 | sec_dynsym = elf_section_by_name(elf, &ehdr, &shdr, |
| 409 | ".dynsym", &dynsym_idx); |
| 410 | if (sec_dynsym != NULL) { |
| 411 | nr = dso__synthesize_plt_symbols(self, elf, &ehdr, |
| 412 | sec_dynsym, &shdr, |
| 413 | dynsym_idx); |
| 414 | if (nr < 0) |
| 415 | goto out_elf_end; |
| 416 | } |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 417 | |
Arnaldo Carvalho de Melo | 8ce998d | 2009-06-03 00:54:33 -0300 | [diff] [blame] | 418 | /* |
| 419 | * But if we have a full .symtab (that is a superset of .dynsym) we |
| 420 | * should add the symbols not in the .dynsyn |
| 421 | */ |
| 422 | sec = elf_section_by_name(elf, &ehdr, &shdr, ".symtab", NULL); |
| 423 | if (sec == NULL) { |
| 424 | if (sec_dynsym == NULL) |
| 425 | goto out_elf_end; |
| 426 | |
| 427 | sec = sec_dynsym; |
| 428 | gelf_getshdr(sec, &shdr); |
| 429 | } |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 430 | |
| 431 | syms = elf_getdata(sec, NULL); |
| 432 | if (syms == NULL) |
| 433 | goto out_elf_end; |
| 434 | |
| 435 | sec = elf_getscn(elf, shdr.sh_link); |
| 436 | if (sec == NULL) |
| 437 | goto out_elf_end; |
| 438 | |
| 439 | symstrs = elf_getdata(sec, NULL); |
| 440 | if (symstrs == NULL) |
| 441 | goto out_elf_end; |
| 442 | |
| 443 | nr_syms = shdr.sh_size / shdr.sh_entsize; |
| 444 | |
| 445 | elf_symtab__for_each_symbol(syms, nr_syms, index, sym) { |
| 446 | struct symbol *f; |
| 447 | |
| 448 | if (!elf_sym__is_function(&sym)) |
| 449 | continue; |
| 450 | |
| 451 | sec = elf_getscn(elf, sym.st_shndx); |
| 452 | if (!sec) |
| 453 | goto out_elf_end; |
| 454 | |
| 455 | gelf_getshdr(sec, &shdr); |
| 456 | sym.st_value -= shdr.sh_addr - shdr.sh_offset; |
| 457 | |
| 458 | f = symbol__new(sym.st_value, sym.st_size, |
Arnaldo Carvalho de Melo | 0085c954 | 2009-05-28 14:55:13 -0300 | [diff] [blame] | 459 | elf_sym__name(&sym, symstrs), |
| 460 | self->sym_priv_size); |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 461 | if (!f) |
| 462 | goto out_elf_end; |
| 463 | |
Arnaldo Carvalho de Melo | 69ee69f | 2009-05-28 14:55:26 -0300 | [diff] [blame] | 464 | if (filter && filter(self, f)) |
| 465 | symbol__delete(f, self->sym_priv_size); |
| 466 | else { |
| 467 | dso__insert_symbol(self, f); |
| 468 | nr++; |
| 469 | } |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 470 | } |
| 471 | |
| 472 | err = nr; |
| 473 | out_elf_end: |
| 474 | elf_end(elf); |
| 475 | out_close: |
| 476 | return err; |
| 477 | } |
| 478 | |
Ingo Molnar | bd74137 | 2009-06-04 14:13:04 +0200 | [diff] [blame] | 479 | int dso__load(struct dso *self, symbol_filter_t filter, int verbose) |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 480 | { |
| 481 | int size = strlen(self->name) + sizeof("/usr/lib/debug%s.debug"); |
| 482 | char *name = malloc(size); |
| 483 | int variant = 0; |
| 484 | int ret = -1; |
| 485 | int fd; |
| 486 | |
| 487 | if (!name) |
| 488 | return -1; |
| 489 | |
| 490 | more: |
| 491 | do { |
| 492 | switch (variant) { |
| 493 | case 0: /* Fedora */ |
| 494 | snprintf(name, size, "/usr/lib/debug%s.debug", self->name); |
| 495 | break; |
| 496 | case 1: /* Ubuntu */ |
| 497 | snprintf(name, size, "/usr/lib/debug%s", self->name); |
| 498 | break; |
| 499 | case 2: /* Sane people */ |
| 500 | snprintf(name, size, "%s", self->name); |
| 501 | break; |
| 502 | |
| 503 | default: |
| 504 | goto out; |
| 505 | } |
| 506 | variant++; |
| 507 | |
| 508 | fd = open(name, O_RDONLY); |
| 509 | } while (fd < 0); |
| 510 | |
Ingo Molnar | bd74137 | 2009-06-04 14:13:04 +0200 | [diff] [blame] | 511 | ret = dso__load_sym(self, fd, name, filter, verbose); |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 512 | close(fd); |
| 513 | |
| 514 | /* |
| 515 | * Some people seem to have debuginfo files _WITHOUT_ debug info!?!? |
| 516 | */ |
| 517 | if (!ret) |
| 518 | goto more; |
| 519 | |
| 520 | out: |
| 521 | free(name); |
| 522 | return ret; |
| 523 | } |
| 524 | |
Arnaldo Carvalho de Melo | 69ee69f | 2009-05-28 14:55:26 -0300 | [diff] [blame] | 525 | static int dso__load_vmlinux(struct dso *self, const char *vmlinux, |
Ingo Molnar | bd74137 | 2009-06-04 14:13:04 +0200 | [diff] [blame] | 526 | symbol_filter_t filter, int verbose) |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 527 | { |
| 528 | int err, fd = open(vmlinux, O_RDONLY); |
| 529 | |
| 530 | if (fd < 0) |
| 531 | return -1; |
| 532 | |
Ingo Molnar | bd74137 | 2009-06-04 14:13:04 +0200 | [diff] [blame] | 533 | err = dso__load_sym(self, fd, vmlinux, filter, verbose); |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 534 | close(fd); |
| 535 | |
| 536 | return err; |
| 537 | } |
| 538 | |
Ingo Molnar | bd74137 | 2009-06-04 14:13:04 +0200 | [diff] [blame] | 539 | int dso__load_kernel(struct dso *self, const char *vmlinux, |
| 540 | symbol_filter_t filter, int verbose) |
Arnaldo Carvalho de Melo | a827c87 | 2009-05-28 14:55:19 -0300 | [diff] [blame] | 541 | { |
| 542 | int err = -1; |
| 543 | |
| 544 | if (vmlinux) |
Ingo Molnar | bd74137 | 2009-06-04 14:13:04 +0200 | [diff] [blame] | 545 | err = dso__load_vmlinux(self, vmlinux, filter, verbose); |
Arnaldo Carvalho de Melo | a827c87 | 2009-05-28 14:55:19 -0300 | [diff] [blame] | 546 | |
| 547 | if (err) |
Ingo Molnar | bd74137 | 2009-06-04 14:13:04 +0200 | [diff] [blame] | 548 | err = dso__load_kallsyms(self, filter, verbose); |
Arnaldo Carvalho de Melo | a827c87 | 2009-05-28 14:55:19 -0300 | [diff] [blame] | 549 | |
| 550 | return err; |
| 551 | } |
| 552 | |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 553 | void symbol__init(void) |
| 554 | { |
| 555 | elf_version(EV_CURRENT); |
| 556 | } |