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;
68 self->slen_calculated = 0;
74 static void dso__delete_symbols(struct dso *self)
77 struct rb_node *next = rb_first(&self->syms);
80 pos = rb_entry(next, struct symbol, rb_node);
81 next = rb_next(&pos->rb_node);
82 rb_erase(&pos->rb_node, &self->syms);
83 symbol__delete(pos, self->sym_priv_size);
87 void dso__delete(struct dso *self)
89 dso__delete_symbols(self);
93 static void dso__insert_symbol(struct dso *self, struct symbol *sym)
95 struct rb_node **p = &self->syms.rb_node;
96 struct rb_node *parent = NULL;
97 const u64 ip = sym->start;
102 s = rb_entry(parent, struct symbol, rb_node);
108 rb_link_node(&sym->rb_node, parent, p);
109 rb_insert_color(&sym->rb_node, &self->syms);
112 struct symbol *dso__find_symbol(struct dso *self, u64 ip)
119 n = self->syms.rb_node;
122 struct symbol *s = rb_entry(n, struct symbol, rb_node);
126 else if (ip > s->end)
135 size_t dso__fprintf(struct dso *self, FILE *fp)
137 size_t ret = fprintf(fp, "dso: %s\n", self->name);
140 for (nd = rb_first(&self->syms); nd; nd = rb_next(nd)) {
141 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
142 ret += symbol__fprintf(pos, fp);
148 static int dso__load_kallsyms(struct dso *self, symbol_filter_t filter, int verbose)
150 struct rb_node *nd, *prevnd;
153 FILE *file = fopen("/proc/kallsyms", "r");
159 while (!feof(file)) {
165 line_len = getline(&line, &n, file);
172 line[--line_len] = '\0'; /* \n */
174 len = hex2u64(line, &start);
177 if (len + 2 >= line_len)
180 symbol_type = toupper(line[len]);
182 * We're interested only in code ('T'ext)
184 if (symbol_type != 'T' && symbol_type != 'W')
187 * Well fix up the end later, when we have all sorted.
189 sym = symbol__new(start, 0xdead, line + len + 2,
190 self->sym_priv_size, 0, verbose);
193 goto out_delete_line;
195 if (filter && filter(self, sym))
196 symbol__delete(sym, self->sym_priv_size);
198 dso__insert_symbol(self, sym);
204 * Now that we have all sorted out, just set the ->end of all
207 prevnd = rb_first(&self->syms);
210 goto out_delete_line;
212 for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
213 struct symbol *prev = rb_entry(prevnd, struct symbol, rb_node),
214 *curr = rb_entry(nd, struct symbol, rb_node);
216 prev->end = curr->start - 1;
231 static int dso__load_perf_map(struct dso *self, symbol_filter_t filter, int verbose)
238 file = fopen(self->name, "r");
242 while (!feof(file)) {
247 line_len = getline(&line, &n, file);
254 line[--line_len] = '\0'; /* \n */
256 len = hex2u64(line, &start);
259 if (len + 2 >= line_len)
262 len += hex2u64(line + len, &size);
265 if (len + 2 >= line_len)
268 sym = symbol__new(start, size, line + len,
269 self->sym_priv_size, start, verbose);
272 goto out_delete_line;
274 if (filter && filter(self, sym))
275 symbol__delete(sym, self->sym_priv_size);
277 dso__insert_symbol(self, sym);
294 * elf_symtab__for_each_symbol - iterate thru all the symbols
296 * @self: struct elf_symtab instance to iterate
297 * @index: uint32_t index
298 * @sym: GElf_Sym iterator
300 #define elf_symtab__for_each_symbol(syms, nr_syms, index, sym) \
301 for (index = 0, gelf_getsym(syms, index, &sym);\
303 index++, gelf_getsym(syms, index, &sym))
305 static inline uint8_t elf_sym__type(const GElf_Sym *sym)
307 return GELF_ST_TYPE(sym->st_info);
310 static inline int elf_sym__is_function(const GElf_Sym *sym)
312 return elf_sym__type(sym) == STT_FUNC &&
314 sym->st_shndx != SHN_UNDEF &&
318 static inline int elf_sym__is_label(const GElf_Sym *sym)
320 return elf_sym__type(sym) == STT_NOTYPE &&
322 sym->st_shndx != SHN_UNDEF &&
323 sym->st_shndx != SHN_ABS;
326 static inline const char *elf_sec__name(const GElf_Shdr *shdr,
327 const Elf_Data *secstrs)
329 return secstrs->d_buf + shdr->sh_name;
332 static inline int elf_sec__is_text(const GElf_Shdr *shdr,
333 const Elf_Data *secstrs)
335 return strstr(elf_sec__name(shdr, secstrs), "text") != NULL;
338 static inline const char *elf_sym__name(const GElf_Sym *sym,
339 const Elf_Data *symstrs)
341 return symstrs->d_buf + sym->st_name;
344 static Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep,
345 GElf_Shdr *shp, const char *name,
351 while ((sec = elf_nextscn(elf, sec)) != NULL) {
354 gelf_getshdr(sec, shp);
355 str = elf_strptr(elf, ep->e_shstrndx, shp->sh_name);
356 if (!strcmp(name, str)) {
367 #define elf_section__for_each_rel(reldata, pos, pos_mem, idx, nr_entries) \
368 for (idx = 0, pos = gelf_getrel(reldata, 0, &pos_mem); \
370 ++idx, pos = gelf_getrel(reldata, idx, &pos_mem))
372 #define elf_section__for_each_rela(reldata, pos, pos_mem, idx, nr_entries) \
373 for (idx = 0, pos = gelf_getrela(reldata, 0, &pos_mem); \
375 ++idx, pos = gelf_getrela(reldata, idx, &pos_mem))
378 * We need to check if we have a .dynsym, so that we can handle the
379 * .plt, synthesizing its symbols, that aren't on the symtabs (be it
380 * .dynsym or .symtab).
381 * And always look at the original dso, not at debuginfo packages, that
382 * have the PLT data stripped out (shdr_rel_plt.sh_type == SHT_NOBITS).
384 static int dso__synthesize_plt_symbols(struct dso *self, int verbose)
386 uint32_t nr_rel_entries, idx;
391 GElf_Shdr shdr_rel_plt, shdr_dynsym;
392 Elf_Data *reldata, *syms, *symstrs;
393 Elf_Scn *scn_plt_rel, *scn_symstrs, *scn_dynsym;
396 char sympltname[1024];
398 int nr = 0, symidx, fd, err = 0;
400 fd = open(self->name, O_RDONLY);
404 elf = elf_begin(fd, ELF_C_READ_MMAP, NULL);
408 if (gelf_getehdr(elf, &ehdr) == NULL)
411 scn_dynsym = elf_section_by_name(elf, &ehdr, &shdr_dynsym,
412 ".dynsym", &dynsym_idx);
413 if (scn_dynsym == NULL)
416 scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt,
418 if (scn_plt_rel == NULL) {
419 scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt,
421 if (scn_plt_rel == NULL)
427 if (shdr_rel_plt.sh_link != dynsym_idx)
430 if (elf_section_by_name(elf, &ehdr, &shdr_plt, ".plt", NULL) == NULL)
434 * Fetch the relocation section to find the indexes to the GOT
435 * and the symbols in the .dynsym they refer to.
437 reldata = elf_getdata(scn_plt_rel, NULL);
441 syms = elf_getdata(scn_dynsym, NULL);
445 scn_symstrs = elf_getscn(elf, shdr_dynsym.sh_link);
446 if (scn_symstrs == NULL)
449 symstrs = elf_getdata(scn_symstrs, NULL);
453 nr_rel_entries = shdr_rel_plt.sh_size / shdr_rel_plt.sh_entsize;
454 plt_offset = shdr_plt.sh_offset;
456 if (shdr_rel_plt.sh_type == SHT_RELA) {
457 GElf_Rela pos_mem, *pos;
459 elf_section__for_each_rela(reldata, pos, pos_mem, idx,
461 symidx = GELF_R_SYM(pos->r_info);
462 plt_offset += shdr_plt.sh_entsize;
463 gelf_getsym(syms, symidx, &sym);
464 snprintf(sympltname, sizeof(sympltname),
465 "%s@plt", elf_sym__name(&sym, symstrs));
467 f = symbol__new(plt_offset, shdr_plt.sh_entsize,
468 sympltname, self->sym_priv_size, 0, verbose);
472 dso__insert_symbol(self, f);
475 } else if (shdr_rel_plt.sh_type == SHT_REL) {
476 GElf_Rel pos_mem, *pos;
477 elf_section__for_each_rel(reldata, pos, pos_mem, idx,
479 symidx = GELF_R_SYM(pos->r_info);
480 plt_offset += shdr_plt.sh_entsize;
481 gelf_getsym(syms, symidx, &sym);
482 snprintf(sympltname, sizeof(sympltname),
483 "%s@plt", elf_sym__name(&sym, symstrs));
485 f = symbol__new(plt_offset, shdr_plt.sh_entsize,
486 sympltname, self->sym_priv_size, 0, verbose);
490 dso__insert_symbol(self, f);
504 fprintf(stderr, "%s: problems reading %s PLT info.\n",
505 __func__, self->name);
509 static int dso__load_sym(struct dso *self, int fd, const char *name,
510 symbol_filter_t filter, int verbose, struct module *mod)
512 Elf_Data *symstrs, *secstrs;
520 Elf_Scn *sec, *sec_strndx;
524 elf = elf_begin(fd, ELF_C_READ_MMAP, NULL);
527 fprintf(stderr, "%s: cannot read %s ELF file.\n",
532 if (gelf_getehdr(elf, &ehdr) == NULL) {
534 fprintf(stderr, "%s: cannot get elf header.\n", __func__);
538 sec = elf_section_by_name(elf, &ehdr, &shdr, ".symtab", NULL);
540 sec = elf_section_by_name(elf, &ehdr, &shdr, ".dynsym", NULL);
545 syms = elf_getdata(sec, NULL);
549 sec = elf_getscn(elf, shdr.sh_link);
553 symstrs = elf_getdata(sec, NULL);
557 sec_strndx = elf_getscn(elf, ehdr.e_shstrndx);
558 if (sec_strndx == NULL)
561 secstrs = elf_getdata(sec_strndx, NULL);
565 nr_syms = shdr.sh_size / shdr.sh_entsize;
567 memset(&sym, 0, sizeof(sym));
568 self->adjust_symbols = (ehdr.e_type == ET_EXEC ||
569 elf_section_by_name(elf, &ehdr, &shdr,
572 elf_symtab__for_each_symbol(syms, nr_syms, index, sym) {
575 struct section *section = NULL;
576 int is_label = elf_sym__is_label(&sym);
577 const char *section_name;
579 if (!is_label && !elf_sym__is_function(&sym))
582 sec = elf_getscn(elf, sym.st_shndx);
586 gelf_getshdr(sec, &shdr);
588 if (is_label && !elf_sec__is_text(&shdr, secstrs))
591 section_name = elf_sec__name(&shdr, secstrs);
592 obj_start = sym.st_value;
594 if (self->adjust_symbols) {
596 printf("adjusting symbol: st_value: %Lx sh_addr: %Lx sh_offset: %Lx\n",
597 (u64)sym.st_value, (u64)shdr.sh_addr, (u64)shdr.sh_offset);
599 sym.st_value -= shdr.sh_addr - shdr.sh_offset;
603 section = mod->sections->find_section(mod->sections, section_name);
605 sym.st_value += section->vma;
607 fprintf(stderr, "dso__load_sym() module %s lookup of %s failed\n",
608 mod->name, section_name);
613 f = symbol__new(sym.st_value, sym.st_size,
614 elf_sym__name(&sym, symstrs),
615 self->sym_priv_size, obj_start, verbose);
619 if (filter && filter(self, f))
620 symbol__delete(f, self->sym_priv_size);
623 dso__insert_symbol(self, f);
635 int dso__load(struct dso *self, symbol_filter_t filter, int verbose)
637 int size = strlen(self->name) + sizeof("/usr/lib/debug%s.debug");
638 char *name = malloc(size);
646 self->adjust_symbols = 0;
648 if (strncmp(self->name, "/tmp/perf-", 10) == 0)
649 return dso__load_perf_map(self, filter, verbose);
655 snprintf(name, size, "/usr/lib/debug%s.debug", self->name);
658 snprintf(name, size, "/usr/lib/debug%s", self->name);
660 case 2: /* Sane people */
661 snprintf(name, size, "%s", self->name);
669 fd = open(name, O_RDONLY);
672 ret = dso__load_sym(self, fd, name, filter, verbose, NULL);
676 * Some people seem to have debuginfo files _WITHOUT_ debug info!?!?
682 int nr_plt = dso__synthesize_plt_symbols(self, verbose);
691 static int dso__load_module(struct dso *self, struct mod_dso *mods, const char *name,
692 symbol_filter_t filter, int verbose)
694 struct module *mod = mod_dso__find_module(mods, name);
697 if (mod == NULL || !mod->active)
700 fd = open(mod->path, O_RDONLY);
705 err = dso__load_sym(self, fd, name, filter, verbose, mod);
711 int dso__load_modules(struct dso *self, symbol_filter_t filter, int verbose)
713 struct mod_dso *mods = mod_dso__new_dso("modules");
715 struct rb_node *next;
718 err = mod_dso__load_modules(mods);
724 * Iterate over modules, and load active symbols.
726 next = rb_first(&mods->mods);
728 pos = rb_entry(next, struct module, rb_node);
729 err = dso__load_module(self, mods, pos->name, filter, verbose);
734 next = rb_next(&pos->rb_node);
738 mod_dso__delete_modules(mods);
739 mod_dso__delete_self(mods);
745 static inline void dso__fill_symbol_holes(struct dso *self)
747 struct symbol *prev = NULL;
750 for (nd = rb_last(&self->syms); nd; nd = rb_prev(nd)) {
751 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
755 int alias = pos->start == prev->start;
758 hole = prev->start - pos->end - 1;
762 pos->end = prev->end;
764 pos->end = prev->start - 1;
771 static int dso__load_vmlinux(struct dso *self, const char *vmlinux,
772 symbol_filter_t filter, int verbose)
774 int err, fd = open(vmlinux, O_RDONLY);
779 err = dso__load_sym(self, fd, vmlinux, filter, verbose, NULL);
782 dso__fill_symbol_holes(self);
789 int dso__load_kernel(struct dso *self, const char *vmlinux,
790 symbol_filter_t filter, int verbose, int modules)
795 err = dso__load_vmlinux(self, vmlinux, filter, verbose);
796 if (err > 0 && modules)
797 err = dso__load_modules(self, filter, verbose);
801 err = dso__load_kallsyms(self, filter, verbose);
806 void symbol__init(void)
808 elf_version(EV_CURRENT);