14 char *bfd_demangle(void __used *v, const char __used *c, int __used i)
20 const char *sym_hist_filter;
23 #define DMGL_PARAMS (1 << 0) /* Include function args */
24 #define DMGL_ANSI (1 << 1) /* Include const, volatile, etc */
27 static struct symbol *symbol__new(u64 start, u64 len,
28 const char *name, unsigned int priv_size,
29 u64 obj_start, int verbose)
31 size_t namelen = strlen(name) + 1;
32 struct symbol *self = calloc(1, priv_size + sizeof(*self) + namelen);
38 printf("new symbol: %016Lx [%08lx]: %s, hist: %p, obj_start: %p\n",
39 (u64)start, (unsigned long)len, name, self->hist, (void *)(unsigned long)obj_start);
41 self->obj_start= obj_start;
45 if (sym_hist_filter && !strcmp(name, sym_hist_filter))
46 self->hist = calloc(sizeof(u64), len);
49 memset(self, 0, priv_size);
50 self = ((void *)self) + priv_size;
53 self->end = len ? start + len - 1 : start;
54 memcpy(self->name, name, namelen);
59 static void symbol__delete(struct symbol *self, unsigned int priv_size)
61 free(((void *)self) - priv_size);
64 static size_t symbol__fprintf(struct symbol *self, FILE *fp)
67 return fprintf(fp, " %llx-%llx %s\n",
68 self->start, self->end, self->name);
70 return fprintf(fp, " %llx-%llx %s \t[%s]\n",
71 self->start, self->end, self->name, self->module->name);
74 struct dso *dso__new(const char *name, unsigned int sym_priv_size)
76 struct dso *self = malloc(sizeof(*self) + strlen(name) + 1);
79 strcpy(self->name, name);
81 self->sym_priv_size = sym_priv_size;
82 self->find_symbol = dso__find_symbol;
83 self->slen_calculated = 0;
89 static void dso__delete_symbols(struct dso *self)
92 struct rb_node *next = rb_first(&self->syms);
95 pos = rb_entry(next, struct symbol, rb_node);
96 next = rb_next(&pos->rb_node);
97 rb_erase(&pos->rb_node, &self->syms);
98 symbol__delete(pos, self->sym_priv_size);
102 void dso__delete(struct dso *self)
104 dso__delete_symbols(self);
108 static void dso__insert_symbol(struct dso *self, struct symbol *sym)
110 struct rb_node **p = &self->syms.rb_node;
111 struct rb_node *parent = NULL;
112 const u64 ip = sym->start;
117 s = rb_entry(parent, struct symbol, rb_node);
123 rb_link_node(&sym->rb_node, parent, p);
124 rb_insert_color(&sym->rb_node, &self->syms);
127 struct symbol *dso__find_symbol(struct dso *self, u64 ip)
134 n = self->syms.rb_node;
137 struct symbol *s = rb_entry(n, struct symbol, rb_node);
141 else if (ip > s->end)
150 size_t dso__fprintf(struct dso *self, FILE *fp)
152 size_t ret = fprintf(fp, "dso: %s\n", self->name);
155 for (nd = rb_first(&self->syms); nd; nd = rb_next(nd)) {
156 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
157 ret += symbol__fprintf(pos, fp);
163 static int dso__load_kallsyms(struct dso *self, symbol_filter_t filter, int verbose)
165 struct rb_node *nd, *prevnd;
168 FILE *file = fopen("/proc/kallsyms", "r");
174 while (!feof(file)) {
180 line_len = getline(&line, &n, file);
187 line[--line_len] = '\0'; /* \n */
189 len = hex2u64(line, &start);
192 if (len + 2 >= line_len)
195 symbol_type = toupper(line[len]);
197 * We're interested only in code ('T'ext)
199 if (symbol_type != 'T' && symbol_type != 'W')
202 * Well fix up the end later, when we have all sorted.
204 sym = symbol__new(start, 0xdead, line + len + 2,
205 self->sym_priv_size, 0, verbose);
208 goto out_delete_line;
210 if (filter && filter(self, sym))
211 symbol__delete(sym, self->sym_priv_size);
213 dso__insert_symbol(self, sym);
219 * Now that we have all sorted out, just set the ->end of all
222 prevnd = rb_first(&self->syms);
225 goto out_delete_line;
227 for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
228 struct symbol *prev = rb_entry(prevnd, struct symbol, rb_node),
229 *curr = rb_entry(nd, struct symbol, rb_node);
231 prev->end = curr->start - 1;
246 static int dso__load_perf_map(struct dso *self, symbol_filter_t filter, int verbose)
253 file = fopen(self->name, "r");
257 while (!feof(file)) {
262 line_len = getline(&line, &n, file);
269 line[--line_len] = '\0'; /* \n */
271 len = hex2u64(line, &start);
274 if (len + 2 >= line_len)
277 len += hex2u64(line + len, &size);
280 if (len + 2 >= line_len)
283 sym = symbol__new(start, size, line + len,
284 self->sym_priv_size, start, verbose);
287 goto out_delete_line;
289 if (filter && filter(self, sym))
290 symbol__delete(sym, self->sym_priv_size);
292 dso__insert_symbol(self, sym);
309 * elf_symtab__for_each_symbol - iterate thru all the symbols
311 * @self: struct elf_symtab instance to iterate
312 * @index: uint32_t index
313 * @sym: GElf_Sym iterator
315 #define elf_symtab__for_each_symbol(syms, nr_syms, index, sym) \
316 for (index = 0, gelf_getsym(syms, index, &sym);\
318 index++, gelf_getsym(syms, index, &sym))
320 static inline uint8_t elf_sym__type(const GElf_Sym *sym)
322 return GELF_ST_TYPE(sym->st_info);
325 static inline int elf_sym__is_function(const GElf_Sym *sym)
327 return elf_sym__type(sym) == STT_FUNC &&
329 sym->st_shndx != SHN_UNDEF &&
333 static inline int elf_sym__is_label(const GElf_Sym *sym)
335 return elf_sym__type(sym) == STT_NOTYPE &&
337 sym->st_shndx != SHN_UNDEF &&
338 sym->st_shndx != SHN_ABS;
341 static inline const char *elf_sec__name(const GElf_Shdr *shdr,
342 const Elf_Data *secstrs)
344 return secstrs->d_buf + shdr->sh_name;
347 static inline int elf_sec__is_text(const GElf_Shdr *shdr,
348 const Elf_Data *secstrs)
350 return strstr(elf_sec__name(shdr, secstrs), "text") != NULL;
353 static inline const char *elf_sym__name(const GElf_Sym *sym,
354 const Elf_Data *symstrs)
356 return symstrs->d_buf + sym->st_name;
359 static Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep,
360 GElf_Shdr *shp, const char *name,
366 while ((sec = elf_nextscn(elf, sec)) != NULL) {
369 gelf_getshdr(sec, shp);
370 str = elf_strptr(elf, ep->e_shstrndx, shp->sh_name);
371 if (!strcmp(name, str)) {
382 #define elf_section__for_each_rel(reldata, pos, pos_mem, idx, nr_entries) \
383 for (idx = 0, pos = gelf_getrel(reldata, 0, &pos_mem); \
385 ++idx, pos = gelf_getrel(reldata, idx, &pos_mem))
387 #define elf_section__for_each_rela(reldata, pos, pos_mem, idx, nr_entries) \
388 for (idx = 0, pos = gelf_getrela(reldata, 0, &pos_mem); \
390 ++idx, pos = gelf_getrela(reldata, idx, &pos_mem))
393 * We need to check if we have a .dynsym, so that we can handle the
394 * .plt, synthesizing its symbols, that aren't on the symtabs (be it
395 * .dynsym or .symtab).
396 * And always look at the original dso, not at debuginfo packages, that
397 * have the PLT data stripped out (shdr_rel_plt.sh_type == SHT_NOBITS).
399 static int dso__synthesize_plt_symbols(struct dso *self, int verbose)
401 uint32_t nr_rel_entries, idx;
406 GElf_Shdr shdr_rel_plt, shdr_dynsym;
407 Elf_Data *reldata, *syms, *symstrs;
408 Elf_Scn *scn_plt_rel, *scn_symstrs, *scn_dynsym;
411 char sympltname[1024];
413 int nr = 0, symidx, fd, err = 0;
415 fd = open(self->name, O_RDONLY);
419 elf = elf_begin(fd, ELF_C_READ_MMAP, NULL);
423 if (gelf_getehdr(elf, &ehdr) == NULL)
426 scn_dynsym = elf_section_by_name(elf, &ehdr, &shdr_dynsym,
427 ".dynsym", &dynsym_idx);
428 if (scn_dynsym == NULL)
431 scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt,
433 if (scn_plt_rel == NULL) {
434 scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt,
436 if (scn_plt_rel == NULL)
442 if (shdr_rel_plt.sh_link != dynsym_idx)
445 if (elf_section_by_name(elf, &ehdr, &shdr_plt, ".plt", NULL) == NULL)
449 * Fetch the relocation section to find the indexes to the GOT
450 * and the symbols in the .dynsym they refer to.
452 reldata = elf_getdata(scn_plt_rel, NULL);
456 syms = elf_getdata(scn_dynsym, NULL);
460 scn_symstrs = elf_getscn(elf, shdr_dynsym.sh_link);
461 if (scn_symstrs == NULL)
464 symstrs = elf_getdata(scn_symstrs, NULL);
468 nr_rel_entries = shdr_rel_plt.sh_size / shdr_rel_plt.sh_entsize;
469 plt_offset = shdr_plt.sh_offset;
471 if (shdr_rel_plt.sh_type == SHT_RELA) {
472 GElf_Rela pos_mem, *pos;
474 elf_section__for_each_rela(reldata, pos, pos_mem, idx,
476 symidx = GELF_R_SYM(pos->r_info);
477 plt_offset += shdr_plt.sh_entsize;
478 gelf_getsym(syms, symidx, &sym);
479 snprintf(sympltname, sizeof(sympltname),
480 "%s@plt", elf_sym__name(&sym, symstrs));
482 f = symbol__new(plt_offset, shdr_plt.sh_entsize,
483 sympltname, self->sym_priv_size, 0, verbose);
487 dso__insert_symbol(self, f);
490 } else if (shdr_rel_plt.sh_type == SHT_REL) {
491 GElf_Rel pos_mem, *pos;
492 elf_section__for_each_rel(reldata, pos, pos_mem, idx,
494 symidx = GELF_R_SYM(pos->r_info);
495 plt_offset += shdr_plt.sh_entsize;
496 gelf_getsym(syms, symidx, &sym);
497 snprintf(sympltname, sizeof(sympltname),
498 "%s@plt", elf_sym__name(&sym, symstrs));
500 f = symbol__new(plt_offset, shdr_plt.sh_entsize,
501 sympltname, self->sym_priv_size, 0, verbose);
505 dso__insert_symbol(self, f);
519 fprintf(stderr, "%s: problems reading %s PLT info.\n",
520 __func__, self->name);
524 static int dso__load_sym(struct dso *self, int fd, const char *name,
525 symbol_filter_t filter, int verbose, struct module *mod)
527 Elf_Data *symstrs, *secstrs;
535 Elf_Scn *sec, *sec_strndx;
537 int nr = 0, kernel = !strcmp("[kernel]", self->name);
539 elf = elf_begin(fd, ELF_C_READ_MMAP, NULL);
542 fprintf(stderr, "%s: cannot read %s ELF file.\n",
547 if (gelf_getehdr(elf, &ehdr) == NULL) {
549 fprintf(stderr, "%s: cannot get elf header.\n", __func__);
553 sec = elf_section_by_name(elf, &ehdr, &shdr, ".symtab", NULL);
555 sec = elf_section_by_name(elf, &ehdr, &shdr, ".dynsym", NULL);
560 syms = elf_getdata(sec, NULL);
564 sec = elf_getscn(elf, shdr.sh_link);
568 symstrs = elf_getdata(sec, NULL);
572 sec_strndx = elf_getscn(elf, ehdr.e_shstrndx);
573 if (sec_strndx == NULL)
576 secstrs = elf_getdata(sec_strndx, NULL);
580 nr_syms = shdr.sh_size / shdr.sh_entsize;
582 memset(&sym, 0, sizeof(sym));
584 self->adjust_symbols = (ehdr.e_type == ET_EXEC ||
585 elf_section_by_name(elf, &ehdr, &shdr,
588 } else self->adjust_symbols = 0;
590 elf_symtab__for_each_symbol(syms, nr_syms, index, sym) {
595 struct section *section = NULL;
596 int is_label = elf_sym__is_label(&sym);
597 const char *section_name;
599 if (!is_label && !elf_sym__is_function(&sym))
602 sec = elf_getscn(elf, sym.st_shndx);
606 gelf_getshdr(sec, &shdr);
608 if (is_label && !elf_sec__is_text(&shdr, secstrs))
611 section_name = elf_sec__name(&shdr, secstrs);
612 obj_start = sym.st_value;
614 if (self->adjust_symbols) {
616 printf("adjusting symbol: st_value: %Lx sh_addr: %Lx sh_offset: %Lx\n",
617 (u64)sym.st_value, (u64)shdr.sh_addr, (u64)shdr.sh_offset);
619 sym.st_value -= shdr.sh_addr - shdr.sh_offset;
623 section = mod->sections->find_section(mod->sections, section_name);
625 sym.st_value += section->vma;
627 fprintf(stderr, "dso__load_sym() module %s lookup of %s failed\n",
628 mod->name, section_name);
633 * We need to figure out if the object was created from C++ sources
634 * DWARF DW_compile_unit has this, but we don't always have access
637 name = elf_sym__name(&sym, symstrs);
638 demangled = bfd_demangle(NULL, name, DMGL_PARAMS | DMGL_ANSI);
639 if (demangled != NULL)
642 f = symbol__new(sym.st_value, sym.st_size, name,
643 self->sym_priv_size, obj_start, verbose);
648 if (filter && filter(self, f))
649 symbol__delete(f, self->sym_priv_size);
652 dso__insert_symbol(self, f);
664 int dso__load(struct dso *self, symbol_filter_t filter, int verbose)
666 int size = strlen(self->name) + sizeof("/usr/lib/debug%s.debug");
667 char *name = malloc(size);
675 self->adjust_symbols = 0;
677 if (strncmp(self->name, "/tmp/perf-", 10) == 0)
678 return dso__load_perf_map(self, filter, verbose);
684 snprintf(name, size, "/usr/lib/debug%s.debug", self->name);
687 snprintf(name, size, "/usr/lib/debug%s", self->name);
689 case 2: /* Sane people */
690 snprintf(name, size, "%s", self->name);
698 fd = open(name, O_RDONLY);
701 ret = dso__load_sym(self, fd, name, filter, verbose, NULL);
705 * Some people seem to have debuginfo files _WITHOUT_ debug info!?!?
711 int nr_plt = dso__synthesize_plt_symbols(self, verbose);
720 static int dso__load_module(struct dso *self, struct mod_dso *mods, const char *name,
721 symbol_filter_t filter, int verbose)
723 struct module *mod = mod_dso__find_module(mods, name);
726 if (mod == NULL || !mod->active)
729 fd = open(mod->path, O_RDONLY);
734 err = dso__load_sym(self, fd, name, filter, verbose, mod);
740 int dso__load_modules(struct dso *self, symbol_filter_t filter, int verbose)
742 struct mod_dso *mods = mod_dso__new_dso("modules");
744 struct rb_node *next;
747 err = mod_dso__load_modules(mods);
753 * Iterate over modules, and load active symbols.
755 next = rb_first(&mods->mods);
757 pos = rb_entry(next, struct module, rb_node);
758 err = dso__load_module(self, mods, pos->name, filter, verbose);
763 next = rb_next(&pos->rb_node);
767 mod_dso__delete_modules(mods);
768 mod_dso__delete_self(mods);
774 static inline void dso__fill_symbol_holes(struct dso *self)
776 struct symbol *prev = NULL;
779 for (nd = rb_last(&self->syms); nd; nd = rb_prev(nd)) {
780 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
784 int alias = pos->start == prev->start;
787 hole = prev->start - pos->end - 1;
791 pos->end = prev->end;
793 pos->end = prev->start - 1;
800 static int dso__load_vmlinux(struct dso *self, const char *vmlinux,
801 symbol_filter_t filter, int verbose)
803 int err, fd = open(vmlinux, O_RDONLY);
808 err = dso__load_sym(self, fd, vmlinux, filter, verbose, NULL);
811 dso__fill_symbol_holes(self);
818 int dso__load_kernel(struct dso *self, const char *vmlinux,
819 symbol_filter_t filter, int verbose, int modules)
824 err = dso__load_vmlinux(self, vmlinux, filter, verbose);
825 if (err > 0 && modules)
826 err = dso__load_modules(self, filter, verbose);
830 err = dso__load_kallsyms(self, filter, verbose);
835 void symbol__init(void)
837 elf_version(EV_CURRENT);