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