12 const char *sym_hist_filter;
24 static struct symbol *symbol__new(u64 start, u64 len,
25 const char *name, unsigned int priv_size,
28 size_t namelen = strlen(name) + 1;
29 struct symbol *self = calloc(1, priv_size + sizeof(*self) + namelen);
35 printf("new symbol: %016Lx [%08lx]: %s, hist: %p, obj_start: %p\n",
36 (u64)start, (unsigned long)len, name, self->hist, (void *)(unsigned long)obj_start);
38 self->obj_start= obj_start;
42 if (sym_hist_filter && !strcmp(name, sym_hist_filter))
43 self->hist = calloc(sizeof(u64), len);
46 memset(self, 0, priv_size);
47 self = ((void *)self) + priv_size;
50 self->end = len ? start + len - 1 : start;
51 memcpy(self->name, name, namelen);
56 static void symbol__delete(struct symbol *self, unsigned int priv_size)
58 free(((void *)self) - priv_size);
61 static size_t symbol__fprintf(struct symbol *self, FILE *fp)
64 return fprintf(fp, " %llx-%llx %s\n",
65 self->start, self->end, self->name);
67 return fprintf(fp, " %llx-%llx %s \t[%s]\n",
68 self->start, self->end, self->name, self->module->name);
71 struct dso *dso__new(const char *name, unsigned int sym_priv_size)
73 struct dso *self = malloc(sizeof(*self) + strlen(name) + 1);
76 strcpy(self->name, name);
78 self->sym_priv_size = sym_priv_size;
79 self->find_symbol = dso__find_symbol;
80 self->slen_calculated = 0;
81 self->origin = DSO__ORIG_NOT_FOUND;
87 static void dso__delete_symbols(struct dso *self)
90 struct rb_node *next = rb_first(&self->syms);
93 pos = rb_entry(next, struct symbol, rb_node);
94 next = rb_next(&pos->rb_node);
95 rb_erase(&pos->rb_node, &self->syms);
96 symbol__delete(pos, self->sym_priv_size);
100 void dso__delete(struct dso *self)
102 dso__delete_symbols(self);
106 static void dso__insert_symbol(struct dso *self, struct symbol *sym)
108 struct rb_node **p = &self->syms.rb_node;
109 struct rb_node *parent = NULL;
110 const u64 ip = sym->start;
115 s = rb_entry(parent, struct symbol, rb_node);
121 rb_link_node(&sym->rb_node, parent, p);
122 rb_insert_color(&sym->rb_node, &self->syms);
125 struct symbol *dso__find_symbol(struct dso *self, u64 ip)
132 n = self->syms.rb_node;
135 struct symbol *s = rb_entry(n, struct symbol, rb_node);
139 else if (ip > s->end)
148 size_t dso__fprintf(struct dso *self, FILE *fp)
150 size_t ret = fprintf(fp, "dso: %s\n", self->name);
153 for (nd = rb_first(&self->syms); nd; nd = rb_next(nd)) {
154 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
155 ret += symbol__fprintf(pos, fp);
161 static int dso__load_kallsyms(struct dso *self, symbol_filter_t filter, int v)
163 struct rb_node *nd, *prevnd;
166 FILE *file = fopen("/proc/kallsyms", "r");
172 while (!feof(file)) {
178 line_len = getline(&line, &n, file);
185 line[--line_len] = '\0'; /* \n */
187 len = hex2u64(line, &start);
190 if (len + 2 >= line_len)
193 symbol_type = toupper(line[len]);
195 * We're interested only in code ('T'ext)
197 if (symbol_type != 'T' && symbol_type != 'W')
200 * Well fix up the end later, when we have all sorted.
202 sym = symbol__new(start, 0xdead, line + len + 2,
203 self->sym_priv_size, 0, v);
206 goto out_delete_line;
208 if (filter && filter(self, sym))
209 symbol__delete(sym, self->sym_priv_size);
211 dso__insert_symbol(self, sym);
217 * Now that we have all sorted out, just set the ->end of all
220 prevnd = rb_first(&self->syms);
223 goto out_delete_line;
225 for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
226 struct symbol *prev = rb_entry(prevnd, struct symbol, rb_node),
227 *curr = rb_entry(nd, struct symbol, rb_node);
229 prev->end = curr->start - 1;
244 static int dso__load_perf_map(struct dso *self, symbol_filter_t filter, int v)
251 file = fopen(self->name, "r");
255 while (!feof(file)) {
260 line_len = getline(&line, &n, file);
267 line[--line_len] = '\0'; /* \n */
269 len = hex2u64(line, &start);
272 if (len + 2 >= line_len)
275 len += hex2u64(line + len, &size);
278 if (len + 2 >= line_len)
281 sym = symbol__new(start, size, line + len,
282 self->sym_priv_size, start, v);
285 goto out_delete_line;
287 if (filter && filter(self, sym))
288 symbol__delete(sym, self->sym_priv_size);
290 dso__insert_symbol(self, sym);
307 * elf_symtab__for_each_symbol - iterate thru all the symbols
309 * @self: struct elf_symtab instance to iterate
311 * @sym: GElf_Sym iterator
313 #define elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) \
314 for (idx = 0, gelf_getsym(syms, idx, &sym);\
316 idx++, gelf_getsym(syms, idx, &sym))
318 static inline uint8_t elf_sym__type(const GElf_Sym *sym)
320 return GELF_ST_TYPE(sym->st_info);
323 static inline int elf_sym__is_function(const GElf_Sym *sym)
325 return elf_sym__type(sym) == STT_FUNC &&
327 sym->st_shndx != SHN_UNDEF &&
331 static inline int elf_sym__is_label(const GElf_Sym *sym)
333 return elf_sym__type(sym) == STT_NOTYPE &&
335 sym->st_shndx != SHN_UNDEF &&
336 sym->st_shndx != SHN_ABS;
339 static inline const char *elf_sec__name(const GElf_Shdr *shdr,
340 const Elf_Data *secstrs)
342 return secstrs->d_buf + shdr->sh_name;
345 static inline int elf_sec__is_text(const GElf_Shdr *shdr,
346 const Elf_Data *secstrs)
348 return strstr(elf_sec__name(shdr, secstrs), "text") != NULL;
351 static inline const char *elf_sym__name(const GElf_Sym *sym,
352 const Elf_Data *symstrs)
354 return symstrs->d_buf + sym->st_name;
357 static Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep,
358 GElf_Shdr *shp, const char *name,
364 while ((sec = elf_nextscn(elf, sec)) != NULL) {
367 gelf_getshdr(sec, shp);
368 str = elf_strptr(elf, ep->e_shstrndx, shp->sh_name);
369 if (!strcmp(name, str)) {
380 #define elf_section__for_each_rel(reldata, pos, pos_mem, idx, nr_entries) \
381 for (idx = 0, pos = gelf_getrel(reldata, 0, &pos_mem); \
383 ++idx, pos = gelf_getrel(reldata, idx, &pos_mem))
385 #define elf_section__for_each_rela(reldata, pos, pos_mem, idx, nr_entries) \
386 for (idx = 0, pos = gelf_getrela(reldata, 0, &pos_mem); \
388 ++idx, pos = gelf_getrela(reldata, idx, &pos_mem))
391 * We need to check if we have a .dynsym, so that we can handle the
392 * .plt, synthesizing its symbols, that aren't on the symtabs (be it
393 * .dynsym or .symtab).
394 * And always look at the original dso, not at debuginfo packages, that
395 * have the PLT data stripped out (shdr_rel_plt.sh_type == SHT_NOBITS).
397 static int dso__synthesize_plt_symbols(struct dso *self, int v)
399 uint32_t nr_rel_entries, idx;
404 GElf_Shdr shdr_rel_plt, shdr_dynsym;
405 Elf_Data *reldata, *syms, *symstrs;
406 Elf_Scn *scn_plt_rel, *scn_symstrs, *scn_dynsym;
409 char sympltname[1024];
411 int nr = 0, symidx, fd, err = 0;
413 fd = open(self->name, O_RDONLY);
417 elf = elf_begin(fd, ELF_C_READ_MMAP, NULL);
421 if (gelf_getehdr(elf, &ehdr) == NULL)
424 scn_dynsym = elf_section_by_name(elf, &ehdr, &shdr_dynsym,
425 ".dynsym", &dynsym_idx);
426 if (scn_dynsym == NULL)
429 scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt,
431 if (scn_plt_rel == NULL) {
432 scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt,
434 if (scn_plt_rel == NULL)
440 if (shdr_rel_plt.sh_link != dynsym_idx)
443 if (elf_section_by_name(elf, &ehdr, &shdr_plt, ".plt", NULL) == NULL)
447 * Fetch the relocation section to find the idxes to the GOT
448 * and the symbols in the .dynsym they refer to.
450 reldata = elf_getdata(scn_plt_rel, NULL);
454 syms = elf_getdata(scn_dynsym, NULL);
458 scn_symstrs = elf_getscn(elf, shdr_dynsym.sh_link);
459 if (scn_symstrs == NULL)
462 symstrs = elf_getdata(scn_symstrs, NULL);
466 nr_rel_entries = shdr_rel_plt.sh_size / shdr_rel_plt.sh_entsize;
467 plt_offset = shdr_plt.sh_offset;
469 if (shdr_rel_plt.sh_type == SHT_RELA) {
470 GElf_Rela pos_mem, *pos;
472 elf_section__for_each_rela(reldata, pos, pos_mem, idx,
474 symidx = GELF_R_SYM(pos->r_info);
475 plt_offset += shdr_plt.sh_entsize;
476 gelf_getsym(syms, symidx, &sym);
477 snprintf(sympltname, sizeof(sympltname),
478 "%s@plt", elf_sym__name(&sym, symstrs));
480 f = symbol__new(plt_offset, shdr_plt.sh_entsize,
481 sympltname, self->sym_priv_size, 0, v);
485 dso__insert_symbol(self, f);
488 } else if (shdr_rel_plt.sh_type == SHT_REL) {
489 GElf_Rel pos_mem, *pos;
490 elf_section__for_each_rel(reldata, pos, pos_mem, idx,
492 symidx = GELF_R_SYM(pos->r_info);
493 plt_offset += shdr_plt.sh_entsize;
494 gelf_getsym(syms, symidx, &sym);
495 snprintf(sympltname, sizeof(sympltname),
496 "%s@plt", elf_sym__name(&sym, symstrs));
498 f = symbol__new(plt_offset, shdr_plt.sh_entsize,
499 sympltname, self->sym_priv_size, 0, v);
503 dso__insert_symbol(self, f);
517 fprintf(stderr, "%s: problems reading %s PLT info.\n",
518 __func__, self->name);
522 static int dso__load_sym(struct dso *self, int fd, const char *name,
523 symbol_filter_t filter, int v, struct module *mod)
525 Elf_Data *symstrs, *secstrs;
533 Elf_Scn *sec, *sec_strndx;
535 int nr = 0, kernel = !strcmp("[kernel]", self->name);
537 elf = elf_begin(fd, ELF_C_READ_MMAP, NULL);
540 fprintf(stderr, "%s: cannot read %s ELF file.\n",
545 if (gelf_getehdr(elf, &ehdr) == NULL) {
547 fprintf(stderr, "%s: cannot get elf header.\n", __func__);
551 sec = elf_section_by_name(elf, &ehdr, &shdr, ".symtab", NULL);
553 sec = elf_section_by_name(elf, &ehdr, &shdr, ".dynsym", NULL);
558 syms = elf_getdata(sec, NULL);
562 sec = elf_getscn(elf, shdr.sh_link);
566 symstrs = elf_getdata(sec, NULL);
570 sec_strndx = elf_getscn(elf, ehdr.e_shstrndx);
571 if (sec_strndx == NULL)
574 secstrs = elf_getdata(sec_strndx, NULL);
578 nr_syms = shdr.sh_size / shdr.sh_entsize;
580 memset(&sym, 0, sizeof(sym));
582 self->adjust_symbols = (ehdr.e_type == ET_EXEC ||
583 elf_section_by_name(elf, &ehdr, &shdr,
586 } else self->adjust_symbols = 0;
588 elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) {
590 const char *elf_name;
593 struct section *section = NULL;
594 int is_label = elf_sym__is_label(&sym);
595 const char *section_name;
597 if (!is_label && !elf_sym__is_function(&sym))
600 sec = elf_getscn(elf, sym.st_shndx);
604 gelf_getshdr(sec, &shdr);
606 if (is_label && !elf_sec__is_text(&shdr, secstrs))
609 section_name = elf_sec__name(&shdr, secstrs);
610 obj_start = sym.st_value;
612 if (self->adjust_symbols) {
614 printf("adjusting symbol: st_value: %Lx sh_addr: %Lx sh_offset: %Lx\n",
615 (u64)sym.st_value, (u64)shdr.sh_addr, (u64)shdr.sh_offset);
617 sym.st_value -= shdr.sh_addr - shdr.sh_offset;
621 section = mod->sections->find_section(mod->sections, section_name);
623 sym.st_value += section->vma;
625 fprintf(stderr, "dso__load_sym() module %s lookup of %s failed\n",
626 mod->name, section_name);
631 * We need to figure out if the object was created from C++ sources
632 * DWARF DW_compile_unit has this, but we don't always have access
635 elf_name = elf_sym__name(&sym, symstrs);
636 demangled = bfd_demangle(NULL, elf_name, DMGL_PARAMS | DMGL_ANSI);
637 if (demangled != NULL)
638 elf_name = demangled;
640 f = symbol__new(sym.st_value, sym.st_size, elf_name,
641 self->sym_priv_size, obj_start, v);
646 if (filter && filter(self, f))
647 symbol__delete(f, self->sym_priv_size);
650 dso__insert_symbol(self, f);
662 #define BUILD_ID_SIZE 128
664 static char *dso__read_build_id(struct dso *self, int v)
669 Elf_Data *build_id_data;
671 char *build_id = NULL, *bid;
674 int fd = open(self->name, O_RDONLY);
679 elf = elf_begin(fd, ELF_C_READ_MMAP, NULL);
682 fprintf(stderr, "%s: cannot read %s ELF file.\n",
683 __func__, self->name);
687 if (gelf_getehdr(elf, &ehdr) == NULL) {
689 fprintf(stderr, "%s: cannot get elf header.\n", __func__);
693 sec = elf_section_by_name(elf, &ehdr, &shdr, ".note.gnu.build-id", NULL);
697 build_id_data = elf_getdata(sec, NULL);
698 if (build_id_data == NULL)
700 build_id = malloc(BUILD_ID_SIZE);
701 if (build_id == NULL)
703 raw = build_id_data->d_buf + 16;
706 for (i = 0; i < 20; ++i) {
707 sprintf(bid, "%02x", *raw);
712 printf("%s(%s): %s\n", __func__, self->name, build_id);
721 char dso__symtab_origin(const struct dso *self)
723 static const char origin[] = {
724 [DSO__ORIG_KERNEL] = 'k',
725 [DSO__ORIG_JAVA_JIT] = 'j',
726 [DSO__ORIG_FEDORA] = 'f',
727 [DSO__ORIG_UBUNTU] = 'u',
728 [DSO__ORIG_BUILDID] = 'b',
729 [DSO__ORIG_DSO] = 'd',
732 if (self == NULL || self->origin == DSO__ORIG_NOT_FOUND)
734 return origin[self->origin];
737 int dso__load(struct dso *self, symbol_filter_t filter, int v)
740 char *name = malloc(size), *build_id = NULL;
747 self->adjust_symbols = 0;
749 if (strncmp(self->name, "/tmp/perf-", 10) == 0) {
750 ret = dso__load_perf_map(self, filter, v);
751 self->origin = ret > 0 ? DSO__ORIG_JAVA_JIT :
756 self->origin = DSO__ORIG_FEDORA - 1;
761 switch (self->origin) {
762 case DSO__ORIG_FEDORA:
763 snprintf(name, size, "/usr/lib/debug%s.debug", self->name);
765 case DSO__ORIG_UBUNTU:
766 snprintf(name, size, "/usr/lib/debug%s", self->name);
768 case DSO__ORIG_BUILDID:
769 build_id = dso__read_build_id(self, v);
770 if (build_id != NULL) {
772 "/usr/lib/debug/.build-id/%.2s/%s.debug",
773 build_id, build_id + 2);
780 snprintf(name, size, "%s", self->name);
787 fd = open(name, O_RDONLY);
790 ret = dso__load_sym(self, fd, name, filter, v, NULL);
794 * Some people seem to have debuginfo files _WITHOUT_ debug info!?!?
800 int nr_plt = dso__synthesize_plt_symbols(self, v);
806 if (ret < 0 && strstr(self->name, " (deleted)") != NULL)
811 static int dso__load_module(struct dso *self, struct mod_dso *mods, const char *name,
812 symbol_filter_t filter, int v)
814 struct module *mod = mod_dso__find_module(mods, name);
817 if (mod == NULL || !mod->active)
820 fd = open(mod->path, O_RDONLY);
825 err = dso__load_sym(self, fd, name, filter, v, mod);
831 int dso__load_modules(struct dso *self, symbol_filter_t filter, int v)
833 struct mod_dso *mods = mod_dso__new_dso("modules");
835 struct rb_node *next;
838 err = mod_dso__load_modules(mods);
844 * Iterate over modules, and load active symbols.
846 next = rb_first(&mods->mods);
848 pos = rb_entry(next, struct module, rb_node);
849 err = dso__load_module(self, mods, pos->name, filter, v);
854 next = rb_next(&pos->rb_node);
858 mod_dso__delete_modules(mods);
859 mod_dso__delete_self(mods);
865 static inline void dso__fill_symbol_holes(struct dso *self)
867 struct symbol *prev = NULL;
870 for (nd = rb_last(&self->syms); nd; nd = rb_prev(nd)) {
871 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
875 int alias = pos->start == prev->start;
878 hole = prev->start - pos->end - 1;
882 pos->end = prev->end;
884 pos->end = prev->start - 1;
891 static int dso__load_vmlinux(struct dso *self, const char *vmlinux,
892 symbol_filter_t filter, int v)
894 int err, fd = open(vmlinux, O_RDONLY);
899 err = dso__load_sym(self, fd, vmlinux, filter, v, NULL);
902 dso__fill_symbol_holes(self);
909 int dso__load_kernel(struct dso *self, const char *vmlinux,
910 symbol_filter_t filter, int v, int use_modules)
915 err = dso__load_vmlinux(self, vmlinux, filter, v);
916 if (err > 0 && use_modules)
917 err = dso__load_modules(self, filter, v);
921 err = dso__load_kallsyms(self, filter, v);
924 self->origin = DSO__ORIG_KERNEL;
930 struct dso *kernel_dso;
932 struct dso *hypervisor_dso;
934 const char *vmlinux_name = "vmlinux";
937 static void dsos__add(struct dso *dso)
939 list_add_tail(&dso->node, &dsos);
942 static struct dso *dsos__find(const char *name)
946 list_for_each_entry(pos, &dsos, node)
947 if (strcmp(pos->name, name) == 0)
952 struct dso *dsos__findnew(const char *name)
954 struct dso *dso = dsos__find(name);
960 dso = dso__new(name, 0);
964 nr = dso__load(dso, NULL, verbose);
966 eprintf("Failed to open: %s\n", name);
970 eprintf("No symbols found in: %s, maybe install a debug package?\n", name);
981 void dsos__fprintf(FILE *fp)
985 list_for_each_entry(pos, &dsos, node)
986 dso__fprintf(pos, fp);
989 static struct symbol *vdso__find_symbol(struct dso *dso, u64 ip)
991 return dso__find_symbol(dso, ip);
994 int load_kernel(void)
998 kernel_dso = dso__new("[kernel]", 0);
1002 err = dso__load_kernel(kernel_dso, vmlinux_name, NULL, verbose, modules);
1004 dso__delete(kernel_dso);
1007 dsos__add(kernel_dso);
1009 vdso = dso__new("[vdso]", 0);
1013 vdso->find_symbol = vdso__find_symbol;
1017 hypervisor_dso = dso__new("[hypervisor]", 0);
1018 if (!hypervisor_dso)
1020 dsos__add(hypervisor_dso);
1026 void symbol__init(void)
1028 elf_version(EV_CURRENT);