10 const char *sym_hist_filter;
22 static struct symbol *symbol__new(u64 start, u64 len,
23 const char *name, unsigned int priv_size,
24 u64 obj_start, int verbose)
26 size_t namelen = strlen(name) + 1;
27 struct symbol *self = calloc(1, priv_size + sizeof(*self) + namelen);
33 printf("new symbol: %016Lx [%08lx]: %s, hist: %p, obj_start: %p\n",
34 (u64)start, (unsigned long)len, name, self->hist, (void *)(unsigned long)obj_start);
36 self->obj_start= obj_start;
40 if (sym_hist_filter && !strcmp(name, sym_hist_filter))
41 self->hist = calloc(sizeof(u64), len);
44 memset(self, 0, priv_size);
45 self = ((void *)self) + priv_size;
48 self->end = len ? start + len - 1 : start;
49 memcpy(self->name, name, namelen);
54 static void symbol__delete(struct symbol *self, unsigned int priv_size)
56 free(((void *)self) - priv_size);
59 static size_t symbol__fprintf(struct symbol *self, FILE *fp)
62 return fprintf(fp, " %llx-%llx %s\n",
63 self->start, self->end, self->name);
65 return fprintf(fp, " %llx-%llx %s \t[%s]\n",
66 self->start, self->end, self->name, self->module->name);
69 struct dso *dso__new(const char *name, unsigned int sym_priv_size)
71 struct dso *self = malloc(sizeof(*self) + strlen(name) + 1);
74 strcpy(self->name, name);
76 self->sym_priv_size = sym_priv_size;
77 self->find_symbol = dso__find_symbol;
78 self->slen_calculated = 0;
79 self->origin = DSO__ORIG_NOT_FOUND;
85 static void dso__delete_symbols(struct dso *self)
88 struct rb_node *next = rb_first(&self->syms);
91 pos = rb_entry(next, struct symbol, rb_node);
92 next = rb_next(&pos->rb_node);
93 rb_erase(&pos->rb_node, &self->syms);
94 symbol__delete(pos, self->sym_priv_size);
98 void dso__delete(struct dso *self)
100 dso__delete_symbols(self);
104 static void dso__insert_symbol(struct dso *self, struct symbol *sym)
106 struct rb_node **p = &self->syms.rb_node;
107 struct rb_node *parent = NULL;
108 const u64 ip = sym->start;
113 s = rb_entry(parent, struct symbol, rb_node);
119 rb_link_node(&sym->rb_node, parent, p);
120 rb_insert_color(&sym->rb_node, &self->syms);
123 struct symbol *dso__find_symbol(struct dso *self, u64 ip)
130 n = self->syms.rb_node;
133 struct symbol *s = rb_entry(n, struct symbol, rb_node);
137 else if (ip > s->end)
146 size_t dso__fprintf(struct dso *self, FILE *fp)
148 size_t ret = fprintf(fp, "dso: %s\n", self->name);
151 for (nd = rb_first(&self->syms); nd; nd = rb_next(nd)) {
152 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
153 ret += symbol__fprintf(pos, fp);
159 static int dso__load_kallsyms(struct dso *self, symbol_filter_t filter, int verbose)
161 struct rb_node *nd, *prevnd;
164 FILE *file = fopen("/proc/kallsyms", "r");
170 while (!feof(file)) {
176 line_len = getline(&line, &n, file);
183 line[--line_len] = '\0'; /* \n */
185 len = hex2u64(line, &start);
188 if (len + 2 >= line_len)
191 symbol_type = toupper(line[len]);
193 * We're interested only in code ('T'ext)
195 if (symbol_type != 'T' && symbol_type != 'W')
198 * Well fix up the end later, when we have all sorted.
200 sym = symbol__new(start, 0xdead, line + len + 2,
201 self->sym_priv_size, 0, verbose);
204 goto out_delete_line;
206 if (filter && filter(self, sym))
207 symbol__delete(sym, self->sym_priv_size);
209 dso__insert_symbol(self, sym);
215 * Now that we have all sorted out, just set the ->end of all
218 prevnd = rb_first(&self->syms);
221 goto out_delete_line;
223 for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
224 struct symbol *prev = rb_entry(prevnd, struct symbol, rb_node),
225 *curr = rb_entry(nd, struct symbol, rb_node);
227 prev->end = curr->start - 1;
242 static int dso__load_perf_map(struct dso *self, symbol_filter_t filter, int verbose)
249 file = fopen(self->name, "r");
253 while (!feof(file)) {
258 line_len = getline(&line, &n, file);
265 line[--line_len] = '\0'; /* \n */
267 len = hex2u64(line, &start);
270 if (len + 2 >= line_len)
273 len += hex2u64(line + len, &size);
276 if (len + 2 >= line_len)
279 sym = symbol__new(start, size, line + len,
280 self->sym_priv_size, start, verbose);
283 goto out_delete_line;
285 if (filter && filter(self, sym))
286 symbol__delete(sym, self->sym_priv_size);
288 dso__insert_symbol(self, sym);
305 * elf_symtab__for_each_symbol - iterate thru all the symbols
307 * @self: struct elf_symtab instance to iterate
308 * @index: uint32_t index
309 * @sym: GElf_Sym iterator
311 #define elf_symtab__for_each_symbol(syms, nr_syms, index, sym) \
312 for (index = 0, gelf_getsym(syms, index, &sym);\
314 index++, gelf_getsym(syms, index, &sym))
316 static inline uint8_t elf_sym__type(const GElf_Sym *sym)
318 return GELF_ST_TYPE(sym->st_info);
321 static inline int elf_sym__is_function(const GElf_Sym *sym)
323 return elf_sym__type(sym) == STT_FUNC &&
325 sym->st_shndx != SHN_UNDEF &&
329 static inline int elf_sym__is_label(const GElf_Sym *sym)
331 return elf_sym__type(sym) == STT_NOTYPE &&
333 sym->st_shndx != SHN_UNDEF &&
334 sym->st_shndx != SHN_ABS;
337 static inline const char *elf_sec__name(const GElf_Shdr *shdr,
338 const Elf_Data *secstrs)
340 return secstrs->d_buf + shdr->sh_name;
343 static inline int elf_sec__is_text(const GElf_Shdr *shdr,
344 const Elf_Data *secstrs)
346 return strstr(elf_sec__name(shdr, secstrs), "text") != NULL;
349 static inline const char *elf_sym__name(const GElf_Sym *sym,
350 const Elf_Data *symstrs)
352 return symstrs->d_buf + sym->st_name;
355 static Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep,
356 GElf_Shdr *shp, const char *name,
362 while ((sec = elf_nextscn(elf, sec)) != NULL) {
365 gelf_getshdr(sec, shp);
366 str = elf_strptr(elf, ep->e_shstrndx, shp->sh_name);
367 if (!strcmp(name, str)) {
378 #define elf_section__for_each_rel(reldata, pos, pos_mem, idx, nr_entries) \
379 for (idx = 0, pos = gelf_getrel(reldata, 0, &pos_mem); \
381 ++idx, pos = gelf_getrel(reldata, idx, &pos_mem))
383 #define elf_section__for_each_rela(reldata, pos, pos_mem, idx, nr_entries) \
384 for (idx = 0, pos = gelf_getrela(reldata, 0, &pos_mem); \
386 ++idx, pos = gelf_getrela(reldata, idx, &pos_mem))
389 * We need to check if we have a .dynsym, so that we can handle the
390 * .plt, synthesizing its symbols, that aren't on the symtabs (be it
391 * .dynsym or .symtab).
392 * And always look at the original dso, not at debuginfo packages, that
393 * have the PLT data stripped out (shdr_rel_plt.sh_type == SHT_NOBITS).
395 static int dso__synthesize_plt_symbols(struct dso *self, int verbose)
397 uint32_t nr_rel_entries, idx;
402 GElf_Shdr shdr_rel_plt, shdr_dynsym;
403 Elf_Data *reldata, *syms, *symstrs;
404 Elf_Scn *scn_plt_rel, *scn_symstrs, *scn_dynsym;
407 char sympltname[1024];
409 int nr = 0, symidx, fd, err = 0;
411 fd = open(self->name, O_RDONLY);
415 elf = elf_begin(fd, ELF_C_READ_MMAP, NULL);
419 if (gelf_getehdr(elf, &ehdr) == NULL)
422 scn_dynsym = elf_section_by_name(elf, &ehdr, &shdr_dynsym,
423 ".dynsym", &dynsym_idx);
424 if (scn_dynsym == NULL)
427 scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt,
429 if (scn_plt_rel == NULL) {
430 scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt,
432 if (scn_plt_rel == NULL)
438 if (shdr_rel_plt.sh_link != dynsym_idx)
441 if (elf_section_by_name(elf, &ehdr, &shdr_plt, ".plt", NULL) == NULL)
445 * Fetch the relocation section to find the indexes to the GOT
446 * and the symbols in the .dynsym they refer to.
448 reldata = elf_getdata(scn_plt_rel, NULL);
452 syms = elf_getdata(scn_dynsym, NULL);
456 scn_symstrs = elf_getscn(elf, shdr_dynsym.sh_link);
457 if (scn_symstrs == NULL)
460 symstrs = elf_getdata(scn_symstrs, NULL);
464 nr_rel_entries = shdr_rel_plt.sh_size / shdr_rel_plt.sh_entsize;
465 plt_offset = shdr_plt.sh_offset;
467 if (shdr_rel_plt.sh_type == SHT_RELA) {
468 GElf_Rela pos_mem, *pos;
470 elf_section__for_each_rela(reldata, pos, pos_mem, idx,
472 symidx = GELF_R_SYM(pos->r_info);
473 plt_offset += shdr_plt.sh_entsize;
474 gelf_getsym(syms, symidx, &sym);
475 snprintf(sympltname, sizeof(sympltname),
476 "%s@plt", elf_sym__name(&sym, symstrs));
478 f = symbol__new(plt_offset, shdr_plt.sh_entsize,
479 sympltname, self->sym_priv_size, 0, verbose);
483 dso__insert_symbol(self, f);
486 } else if (shdr_rel_plt.sh_type == SHT_REL) {
487 GElf_Rel pos_mem, *pos;
488 elf_section__for_each_rel(reldata, pos, pos_mem, idx,
490 symidx = GELF_R_SYM(pos->r_info);
491 plt_offset += shdr_plt.sh_entsize;
492 gelf_getsym(syms, symidx, &sym);
493 snprintf(sympltname, sizeof(sympltname),
494 "%s@plt", elf_sym__name(&sym, symstrs));
496 f = symbol__new(plt_offset, shdr_plt.sh_entsize,
497 sympltname, self->sym_priv_size, 0, verbose);
501 dso__insert_symbol(self, f);
515 fprintf(stderr, "%s: problems reading %s PLT info.\n",
516 __func__, self->name);
520 static int dso__load_sym(struct dso *self, int fd, const char *name,
521 symbol_filter_t filter, int verbose, struct module *mod)
523 Elf_Data *symstrs, *secstrs;
531 Elf_Scn *sec, *sec_strndx;
533 int nr = 0, kernel = !strcmp("[kernel]", self->name);
535 elf = elf_begin(fd, ELF_C_READ_MMAP, NULL);
538 fprintf(stderr, "%s: cannot read %s ELF file.\n",
543 if (gelf_getehdr(elf, &ehdr) == NULL) {
545 fprintf(stderr, "%s: cannot get elf header.\n", __func__);
549 sec = elf_section_by_name(elf, &ehdr, &shdr, ".symtab", NULL);
551 sec = elf_section_by_name(elf, &ehdr, &shdr, ".dynsym", NULL);
556 syms = elf_getdata(sec, NULL);
560 sec = elf_getscn(elf, shdr.sh_link);
564 symstrs = elf_getdata(sec, NULL);
568 sec_strndx = elf_getscn(elf, ehdr.e_shstrndx);
569 if (sec_strndx == NULL)
572 secstrs = elf_getdata(sec_strndx, NULL);
576 nr_syms = shdr.sh_size / shdr.sh_entsize;
578 memset(&sym, 0, sizeof(sym));
580 self->adjust_symbols = (ehdr.e_type == ET_EXEC ||
581 elf_section_by_name(elf, &ehdr, &shdr,
584 } else self->adjust_symbols = 0;
586 elf_symtab__for_each_symbol(syms, nr_syms, index, sym) {
591 struct section *section = NULL;
592 int is_label = elf_sym__is_label(&sym);
593 const char *section_name;
595 if (!is_label && !elf_sym__is_function(&sym))
598 sec = elf_getscn(elf, sym.st_shndx);
602 gelf_getshdr(sec, &shdr);
604 if (is_label && !elf_sec__is_text(&shdr, secstrs))
607 section_name = elf_sec__name(&shdr, secstrs);
608 obj_start = sym.st_value;
610 if (self->adjust_symbols) {
612 printf("adjusting symbol: st_value: %Lx sh_addr: %Lx sh_offset: %Lx\n",
613 (u64)sym.st_value, (u64)shdr.sh_addr, (u64)shdr.sh_offset);
615 sym.st_value -= shdr.sh_addr - shdr.sh_offset;
619 section = mod->sections->find_section(mod->sections, section_name);
621 sym.st_value += section->vma;
623 fprintf(stderr, "dso__load_sym() module %s lookup of %s failed\n",
624 mod->name, section_name);
629 * We need to figure out if the object was created from C++ sources
630 * DWARF DW_compile_unit has this, but we don't always have access
633 name = elf_sym__name(&sym, symstrs);
634 demangled = bfd_demangle(NULL, name, DMGL_PARAMS | DMGL_ANSI);
635 if (demangled != NULL)
638 f = symbol__new(sym.st_value, sym.st_size, name,
639 self->sym_priv_size, obj_start, verbose);
644 if (filter && filter(self, f))
645 symbol__delete(f, self->sym_priv_size);
648 dso__insert_symbol(self, f);
660 #define BUILD_ID_SIZE 128
662 static char *dso__read_build_id(struct dso *self, int verbose)
667 Elf_Data *build_id_data;
669 char *build_id = NULL, *bid;
672 int fd = open(self->name, O_RDONLY);
677 elf = elf_begin(fd, ELF_C_READ_MMAP, NULL);
680 fprintf(stderr, "%s: cannot read %s ELF file.\n",
681 __func__, self->name);
685 if (gelf_getehdr(elf, &ehdr) == NULL) {
687 fprintf(stderr, "%s: cannot get elf header.\n", __func__);
691 sec = elf_section_by_name(elf, &ehdr, &shdr, ".note.gnu.build-id", NULL);
695 build_id_data = elf_getdata(sec, NULL);
696 if (build_id_data == NULL)
698 build_id = malloc(BUILD_ID_SIZE);
699 if (build_id == NULL)
701 raw = build_id_data->d_buf + 16;
704 for (i = 0; i < 20; ++i) {
705 sprintf(bid, "%02x", *raw);
710 printf("%s(%s): %s\n", __func__, self->name, build_id);
719 char dso__symtab_origin(const struct dso *self)
721 static const char origin[] = {
722 [DSO__ORIG_KERNEL] = 'k',
723 [DSO__ORIG_JAVA_JIT] = 'j',
724 [DSO__ORIG_FEDORA] = 'f',
725 [DSO__ORIG_UBUNTU] = 'u',
726 [DSO__ORIG_BUILDID] = 'b',
727 [DSO__ORIG_DSO] = 'd',
730 if (self == NULL || self->origin == DSO__ORIG_NOT_FOUND)
732 return origin[self->origin];
735 int dso__load(struct dso *self, symbol_filter_t filter, int verbose)
738 char *name = malloc(size), *build_id = NULL;
745 self->adjust_symbols = 0;
747 if (strncmp(self->name, "/tmp/perf-", 10) == 0) {
748 ret = dso__load_perf_map(self, filter, verbose);
749 self->origin = ret > 0 ? DSO__ORIG_JAVA_JIT :
754 self->origin = DSO__ORIG_FEDORA - 1;
759 switch (self->origin) {
760 case DSO__ORIG_FEDORA:
761 snprintf(name, size, "/usr/lib/debug%s.debug", self->name);
763 case DSO__ORIG_UBUNTU:
764 snprintf(name, size, "/usr/lib/debug%s", self->name);
766 case DSO__ORIG_BUILDID:
767 build_id = dso__read_build_id(self, verbose);
768 if (build_id != NULL) {
770 "/usr/lib/debug/.build-id/%.2s/%s.debug",
771 build_id, build_id + 2);
778 snprintf(name, size, "%s", self->name);
785 fd = open(name, O_RDONLY);
788 ret = dso__load_sym(self, fd, name, filter, verbose, NULL);
792 * Some people seem to have debuginfo files _WITHOUT_ debug info!?!?
798 int nr_plt = dso__synthesize_plt_symbols(self, verbose);
804 if (ret < 0 && strstr(self->name, " (deleted)") != NULL)
809 static int dso__load_module(struct dso *self, struct mod_dso *mods, const char *name,
810 symbol_filter_t filter, int verbose)
812 struct module *mod = mod_dso__find_module(mods, name);
815 if (mod == NULL || !mod->active)
818 fd = open(mod->path, O_RDONLY);
823 err = dso__load_sym(self, fd, name, filter, verbose, mod);
829 int dso__load_modules(struct dso *self, symbol_filter_t filter, int verbose)
831 struct mod_dso *mods = mod_dso__new_dso("modules");
833 struct rb_node *next;
836 err = mod_dso__load_modules(mods);
842 * Iterate over modules, and load active symbols.
844 next = rb_first(&mods->mods);
846 pos = rb_entry(next, struct module, rb_node);
847 err = dso__load_module(self, mods, pos->name, filter, verbose);
852 next = rb_next(&pos->rb_node);
856 mod_dso__delete_modules(mods);
857 mod_dso__delete_self(mods);
863 static inline void dso__fill_symbol_holes(struct dso *self)
865 struct symbol *prev = NULL;
868 for (nd = rb_last(&self->syms); nd; nd = rb_prev(nd)) {
869 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
873 int alias = pos->start == prev->start;
876 hole = prev->start - pos->end - 1;
880 pos->end = prev->end;
882 pos->end = prev->start - 1;
889 static int dso__load_vmlinux(struct dso *self, const char *vmlinux,
890 symbol_filter_t filter, int verbose)
892 int err, fd = open(vmlinux, O_RDONLY);
897 err = dso__load_sym(self, fd, vmlinux, filter, verbose, NULL);
900 dso__fill_symbol_holes(self);
907 int dso__load_kernel(struct dso *self, const char *vmlinux,
908 symbol_filter_t filter, int verbose, int modules)
913 err = dso__load_vmlinux(self, vmlinux, filter, verbose);
914 if (err > 0 && modules)
915 err = dso__load_modules(self, filter, verbose);
919 err = dso__load_kallsyms(self, filter, verbose);
922 self->origin = DSO__ORIG_KERNEL;
927 void symbol__init(void)
929 elf_version(EV_CURRENT);