10 const char *sym_hist_filter;
12 static struct symbol *symbol__new(u64 start, u64 len,
13 const char *name, unsigned int priv_size,
14 u64 obj_start, int verbose)
16 size_t namelen = strlen(name) + 1;
17 struct symbol *self = calloc(1, priv_size + sizeof(*self) + namelen);
23 printf("new symbol: %016Lx [%08lx]: %s, hist: %p, obj_start: %p\n",
24 (u64)start, (unsigned long)len, name, self->hist, (void *)(unsigned long)obj_start);
26 self->obj_start= obj_start;
30 if (sym_hist_filter && !strcmp(name, sym_hist_filter))
31 self->hist = calloc(sizeof(u64), len);
34 memset(self, 0, priv_size);
35 self = ((void *)self) + priv_size;
38 self->end = len ? start + len - 1 : start;
39 memcpy(self->name, name, namelen);
44 static void symbol__delete(struct symbol *self, unsigned int priv_size)
46 free(((void *)self) - priv_size);
49 static size_t symbol__fprintf(struct symbol *self, FILE *fp)
52 return fprintf(fp, " %llx-%llx %s\n",
53 self->start, self->end, self->name);
55 return fprintf(fp, " %llx-%llx %s \t[%s]\n",
56 self->start, self->end, self->name, self->module->name);
59 struct dso *dso__new(const char *name, unsigned int sym_priv_size)
61 struct dso *self = malloc(sizeof(*self) + strlen(name) + 1);
64 strcpy(self->name, name);
66 self->sym_priv_size = sym_priv_size;
67 self->find_symbol = dso__find_symbol;
73 static void dso__delete_symbols(struct dso *self)
76 struct rb_node *next = rb_first(&self->syms);
79 pos = rb_entry(next, struct symbol, rb_node);
80 next = rb_next(&pos->rb_node);
81 rb_erase(&pos->rb_node, &self->syms);
82 symbol__delete(pos, self->sym_priv_size);
86 void dso__delete(struct dso *self)
88 dso__delete_symbols(self);
92 static void dso__insert_symbol(struct dso *self, struct symbol *sym)
94 struct rb_node **p = &self->syms.rb_node;
95 struct rb_node *parent = NULL;
96 const u64 ip = sym->start;
101 s = rb_entry(parent, struct symbol, rb_node);
107 rb_link_node(&sym->rb_node, parent, p);
108 rb_insert_color(&sym->rb_node, &self->syms);
111 struct symbol *dso__find_symbol(struct dso *self, u64 ip)
118 n = self->syms.rb_node;
121 struct symbol *s = rb_entry(n, struct symbol, rb_node);
125 else if (ip > s->end)
134 size_t dso__fprintf(struct dso *self, FILE *fp)
136 size_t ret = fprintf(fp, "dso: %s\n", self->name);
139 for (nd = rb_first(&self->syms); nd; nd = rb_next(nd)) {
140 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
141 ret += symbol__fprintf(pos, fp);
147 static int dso__load_kallsyms(struct dso *self, symbol_filter_t filter, int verbose)
149 struct rb_node *nd, *prevnd;
152 FILE *file = fopen("/proc/kallsyms", "r");
158 while (!feof(file)) {
164 line_len = getline(&line, &n, file);
171 line[--line_len] = '\0'; /* \n */
173 len = hex2u64(line, &start);
176 if (len + 2 >= line_len)
179 symbol_type = toupper(line[len]);
181 * We're interested only in code ('T'ext)
183 if (symbol_type != 'T' && symbol_type != 'W')
186 * Well fix up the end later, when we have all sorted.
188 sym = symbol__new(start, 0xdead, line + len + 2,
189 self->sym_priv_size, 0, verbose);
192 goto out_delete_line;
194 if (filter && filter(self, sym))
195 symbol__delete(sym, self->sym_priv_size);
197 dso__insert_symbol(self, sym);
203 * Now that we have all sorted out, just set the ->end of all
206 prevnd = rb_first(&self->syms);
209 goto out_delete_line;
211 for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
212 struct symbol *prev = rb_entry(prevnd, struct symbol, rb_node),
213 *curr = rb_entry(nd, struct symbol, rb_node);
215 prev->end = curr->start - 1;
230 static int dso__load_perf_map(struct dso *self, symbol_filter_t filter, int verbose)
237 file = fopen(self->name, "r");
241 while (!feof(file)) {
246 line_len = getline(&line, &n, file);
253 line[--line_len] = '\0'; /* \n */
255 len = hex2u64(line, &start);
258 if (len + 2 >= line_len)
261 len += hex2u64(line + len, &size);
264 if (len + 2 >= line_len)
267 sym = symbol__new(start, size, line + len,
268 self->sym_priv_size, start, verbose);
271 goto out_delete_line;
273 if (filter && filter(self, sym))
274 symbol__delete(sym, self->sym_priv_size);
276 dso__insert_symbol(self, sym);
293 * elf_symtab__for_each_symbol - iterate thru all the symbols
295 * @self: struct elf_symtab instance to iterate
296 * @index: uint32_t index
297 * @sym: GElf_Sym iterator
299 #define elf_symtab__for_each_symbol(syms, nr_syms, index, sym) \
300 for (index = 0, gelf_getsym(syms, index, &sym);\
302 index++, gelf_getsym(syms, index, &sym))
304 static inline uint8_t elf_sym__type(const GElf_Sym *sym)
306 return GELF_ST_TYPE(sym->st_info);
309 static inline int elf_sym__is_function(const GElf_Sym *sym)
311 return elf_sym__type(sym) == STT_FUNC &&
313 sym->st_shndx != SHN_UNDEF &&
317 static inline int elf_sym__is_label(const GElf_Sym *sym)
319 return elf_sym__type(sym) == STT_NOTYPE &&
321 sym->st_shndx != SHN_UNDEF &&
322 sym->st_shndx != SHN_ABS;
325 static inline const char *elf_sec__name(const GElf_Shdr *shdr,
326 const Elf_Data *secstrs)
328 return secstrs->d_buf + shdr->sh_name;
331 static inline int elf_sec__is_text(const GElf_Shdr *shdr,
332 const Elf_Data *secstrs)
334 return strstr(elf_sec__name(shdr, secstrs), "text") != NULL;
337 static inline const char *elf_sym__name(const GElf_Sym *sym,
338 const Elf_Data *symstrs)
340 return symstrs->d_buf + sym->st_name;
343 static Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep,
344 GElf_Shdr *shp, const char *name,
350 while ((sec = elf_nextscn(elf, sec)) != NULL) {
353 gelf_getshdr(sec, shp);
354 str = elf_strptr(elf, ep->e_shstrndx, shp->sh_name);
355 if (!strcmp(name, str)) {
366 #define elf_section__for_each_rel(reldata, pos, pos_mem, idx, nr_entries) \
367 for (idx = 0, pos = gelf_getrel(reldata, 0, &pos_mem); \
369 ++idx, pos = gelf_getrel(reldata, idx, &pos_mem))
371 #define elf_section__for_each_rela(reldata, pos, pos_mem, idx, nr_entries) \
372 for (idx = 0, pos = gelf_getrela(reldata, 0, &pos_mem); \
374 ++idx, pos = gelf_getrela(reldata, idx, &pos_mem))
376 static int dso__synthesize_plt_symbols(struct dso *self, Elf *elf,
377 GElf_Ehdr *ehdr, Elf_Scn *scn_dynsym,
378 GElf_Shdr *shdr_dynsym,
379 size_t dynsym_idx, int verbose)
381 uint32_t nr_rel_entries, idx;
386 GElf_Shdr shdr_rel_plt;
387 Elf_Data *reldata, *syms, *symstrs;
388 Elf_Scn *scn_plt_rel, *scn_symstrs;
389 char sympltname[1024];
392 scn_plt_rel = elf_section_by_name(elf, ehdr, &shdr_rel_plt,
394 if (scn_plt_rel == NULL) {
395 scn_plt_rel = elf_section_by_name(elf, ehdr, &shdr_rel_plt,
397 if (scn_plt_rel == NULL)
401 if (shdr_rel_plt.sh_link != dynsym_idx)
404 if (elf_section_by_name(elf, ehdr, &shdr_plt, ".plt", NULL) == NULL)
408 * Fetch the relocation section to find the indexes to the GOT
409 * and the symbols in the .dynsym they refer to.
411 reldata = elf_getdata(scn_plt_rel, NULL);
415 syms = elf_getdata(scn_dynsym, NULL);
419 scn_symstrs = elf_getscn(elf, shdr_dynsym->sh_link);
420 if (scn_symstrs == NULL)
423 symstrs = elf_getdata(scn_symstrs, NULL);
427 nr_rel_entries = shdr_rel_plt.sh_size / shdr_rel_plt.sh_entsize;
428 plt_offset = shdr_plt.sh_offset;
430 if (shdr_rel_plt.sh_type == SHT_RELA) {
431 GElf_Rela pos_mem, *pos;
433 elf_section__for_each_rela(reldata, pos, pos_mem, idx,
435 symidx = GELF_R_SYM(pos->r_info);
436 plt_offset += shdr_plt.sh_entsize;
437 gelf_getsym(syms, symidx, &sym);
438 snprintf(sympltname, sizeof(sympltname),
439 "%s@plt", elf_sym__name(&sym, symstrs));
441 f = symbol__new(plt_offset, shdr_plt.sh_entsize,
442 sympltname, self->sym_priv_size, 0, verbose);
446 dso__insert_symbol(self, f);
449 } else if (shdr_rel_plt.sh_type == SHT_REL) {
450 GElf_Rel pos_mem, *pos;
451 elf_section__for_each_rel(reldata, pos, pos_mem, idx,
453 symidx = GELF_R_SYM(pos->r_info);
454 plt_offset += shdr_plt.sh_entsize;
455 gelf_getsym(syms, symidx, &sym);
456 snprintf(sympltname, sizeof(sympltname),
457 "%s@plt", elf_sym__name(&sym, symstrs));
459 f = symbol__new(plt_offset, shdr_plt.sh_entsize,
460 sympltname, self->sym_priv_size, 0, verbose);
464 dso__insert_symbol(self, f);
469 * TODO: There are still one more shdr_rel_plt.sh_type
470 * I have to investigate, but probably should be ignored.
477 static int dso__load_sym(struct dso *self, int fd, const char *name,
478 symbol_filter_t filter, int verbose, struct module *mod)
480 Elf_Data *symstrs, *secstrs;
488 Elf_Scn *sec, *sec_dynsym, *sec_strndx;
493 elf = elf_begin(fd, ELF_C_READ_MMAP, NULL);
496 fprintf(stderr, "%s: cannot read %s ELF file.\n",
501 if (gelf_getehdr(elf, &ehdr) == NULL) {
503 fprintf(stderr, "%s: cannot get elf header.\n", __func__);
508 * We need to check if we have a .dynsym, so that we can handle the
509 * .plt, synthesizing its symbols, that aren't on the symtabs (be it
510 * .dynsym or .symtab)
512 sec_dynsym = elf_section_by_name(elf, &ehdr, &shdr,
513 ".dynsym", &dynsym_idx);
514 if (sec_dynsym != NULL) {
515 nr = dso__synthesize_plt_symbols(self, elf, &ehdr,
517 dynsym_idx, verbose);
523 * But if we have a full .symtab (that is a superset of .dynsym) we
524 * should add the symbols not in the .dynsyn
526 sec = elf_section_by_name(elf, &ehdr, &shdr, ".symtab", NULL);
528 if (sec_dynsym == NULL)
532 gelf_getshdr(sec, &shdr);
535 syms = elf_getdata(sec, NULL);
539 sec = elf_getscn(elf, shdr.sh_link);
543 symstrs = elf_getdata(sec, NULL);
547 sec_strndx = elf_getscn(elf, ehdr.e_shstrndx);
548 if (sec_strndx == NULL)
551 secstrs = elf_getdata(sec_strndx, NULL);
555 nr_syms = shdr.sh_size / shdr.sh_entsize;
557 memset(&sym, 0, sizeof(sym));
558 self->adjust_symbols = (ehdr.e_type == ET_EXEC ||
559 elf_section_by_name(elf, &ehdr, &shdr,
562 elf_symtab__for_each_symbol(syms, nr_syms, index, sym) {
565 struct section *section = NULL;
566 int is_label = elf_sym__is_label(&sym);
567 const char *section_name;
569 if (!is_label && !elf_sym__is_function(&sym))
572 sec = elf_getscn(elf, sym.st_shndx);
576 gelf_getshdr(sec, &shdr);
578 if (is_label && !elf_sec__is_text(&shdr, secstrs))
581 section_name = elf_sec__name(&shdr, secstrs);
582 obj_start = sym.st_value;
584 if (self->adjust_symbols) {
586 printf("adjusting symbol: st_value: %Lx sh_addr: %Lx sh_offset: %Lx\n",
587 (u64)sym.st_value, (u64)shdr.sh_addr, (u64)shdr.sh_offset);
589 sym.st_value -= shdr.sh_addr - shdr.sh_offset;
593 section = mod->sections->find_section(mod->sections, section_name);
595 sym.st_value += section->vma;
597 fprintf(stderr, "dso__load_sym() module %s lookup of %s failed\n",
598 mod->name, section_name);
603 f = symbol__new(sym.st_value, sym.st_size,
604 elf_sym__name(&sym, symstrs),
605 self->sym_priv_size, obj_start, verbose);
609 if (filter && filter(self, f))
610 symbol__delete(f, self->sym_priv_size);
613 dso__insert_symbol(self, f);
625 int dso__load(struct dso *self, symbol_filter_t filter, int verbose)
627 int size = strlen(self->name) + sizeof("/usr/lib/debug%s.debug");
628 char *name = malloc(size);
636 self->adjust_symbols = 0;
638 if (strncmp(self->name, "/tmp/perf-", 10) == 0)
639 return dso__load_perf_map(self, filter, verbose);
645 snprintf(name, size, "/usr/lib/debug%s.debug", self->name);
648 snprintf(name, size, "/usr/lib/debug%s", self->name);
650 case 2: /* Sane people */
651 snprintf(name, size, "%s", self->name);
659 fd = open(name, O_RDONLY);
662 ret = dso__load_sym(self, fd, name, filter, verbose, NULL);
666 * Some people seem to have debuginfo files _WITHOUT_ debug info!?!?
676 static int dso__load_module(struct dso *self, struct mod_dso *mods, const char *name,
677 symbol_filter_t filter, int verbose)
679 struct module *mod = mod_dso__find_module(mods, name);
682 if (mod == NULL || !mod->active)
685 fd = open(mod->path, O_RDONLY);
690 err = dso__load_sym(self, fd, name, filter, verbose, mod);
696 int dso__load_modules(struct dso *self, symbol_filter_t filter, int verbose)
698 struct mod_dso *mods = mod_dso__new_dso("modules");
700 struct rb_node *next;
703 err = mod_dso__load_modules(mods);
709 * Iterate over modules, and load active symbols.
711 next = rb_first(&mods->mods);
713 pos = rb_entry(next, struct module, rb_node);
714 err = dso__load_module(self, mods, pos->name, filter, verbose);
719 next = rb_next(&pos->rb_node);
723 mod_dso__delete_modules(mods);
724 mod_dso__delete_self(mods);
730 static inline void dso__fill_symbol_holes(struct dso *self)
732 struct symbol *prev = NULL;
735 for (nd = rb_last(&self->syms); nd; nd = rb_prev(nd)) {
736 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
740 int alias = pos->start == prev->start;
743 hole = prev->start - pos->end - 1;
747 pos->end = prev->end;
749 pos->end = prev->start - 1;
756 static int dso__load_vmlinux(struct dso *self, const char *vmlinux,
757 symbol_filter_t filter, int verbose)
759 int err, fd = open(vmlinux, O_RDONLY);
764 err = dso__load_sym(self, fd, vmlinux, filter, verbose, NULL);
767 dso__fill_symbol_holes(self);
774 int dso__load_kernel(struct dso *self, const char *vmlinux,
775 symbol_filter_t filter, int verbose, int modules)
780 err = dso__load_vmlinux(self, vmlinux, filter, verbose);
781 if (err > 0 && modules)
782 err = dso__load_modules(self, filter, verbose);
786 err = dso__load_kallsyms(self, filter, verbose);
791 void symbol__init(void)
793 elf_version(EV_CURRENT);