12 #include <sys/utsname.h>
25 static void dsos__add(struct dso *dso);
26 static struct dso *dsos__find(const char *name);
27 static struct map *map__new2(u64 start, struct dso *dso);
28 static void kernel_maps__insert(struct map *map);
30 static struct rb_root kernel_maps;
32 static void dso__fixup_sym_end(struct dso *self)
34 struct rb_node *nd, *prevnd = rb_first(&self->syms);
35 struct symbol *curr, *prev;
40 curr = rb_entry(prevnd, struct symbol, rb_node);
42 for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
44 curr = rb_entry(nd, struct symbol, rb_node);
46 if (prev->end == prev->start)
47 prev->end = curr->start - 1;
51 if (curr->end == curr->start)
52 curr->end = roundup(curr->start, 4096);
55 static void kernel_maps__fixup_end(void)
57 struct map *prev, *curr;
58 struct rb_node *nd, *prevnd = rb_first(&kernel_maps);
63 curr = rb_entry(prevnd, struct map, rb_node);
65 for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
67 curr = rb_entry(nd, struct map, rb_node);
68 prev->end = curr->start - 1;
71 nd = rb_last(&curr->dso->syms);
73 struct symbol *sym = rb_entry(nd, struct symbol, rb_node);
78 static struct symbol *symbol__new(u64 start, u64 len, const char *name,
79 unsigned int priv_size)
81 size_t namelen = strlen(name) + 1;
82 struct symbol *self = calloc(1, priv_size + sizeof(*self) + namelen);
88 memset(self, 0, priv_size);
89 self = ((void *)self) + priv_size;
92 self->end = len ? start + len - 1 : start;
94 pr_debug3("%s: %s %#Lx-%#Lx\n", __func__, name, start, self->end);
96 memcpy(self->name, name, namelen);
101 static void symbol__delete(struct symbol *self, unsigned int priv_size)
103 free(((void *)self) - priv_size);
106 static size_t symbol__fprintf(struct symbol *self, FILE *fp)
108 return fprintf(fp, " %llx-%llx %s\n",
109 self->start, self->end, self->name);
112 struct dso *dso__new(const char *name, unsigned int sym_priv_size)
114 struct dso *self = malloc(sizeof(*self) + strlen(name) + 1);
117 strcpy(self->name, name);
118 self->long_name = self->name;
119 self->short_name = self->name;
120 self->syms = RB_ROOT;
121 self->sym_priv_size = sym_priv_size;
122 self->find_symbol = dso__find_symbol;
123 self->slen_calculated = 0;
124 self->origin = DSO__ORIG_NOT_FOUND;
130 static void dso__delete_symbols(struct dso *self)
133 struct rb_node *next = rb_first(&self->syms);
136 pos = rb_entry(next, struct symbol, rb_node);
137 next = rb_next(&pos->rb_node);
138 rb_erase(&pos->rb_node, &self->syms);
139 symbol__delete(pos, self->sym_priv_size);
143 void dso__delete(struct dso *self)
145 dso__delete_symbols(self);
146 if (self->long_name != self->name)
147 free(self->long_name);
151 static void dso__insert_symbol(struct dso *self, struct symbol *sym)
153 struct rb_node **p = &self->syms.rb_node;
154 struct rb_node *parent = NULL;
155 const u64 ip = sym->start;
160 s = rb_entry(parent, struct symbol, rb_node);
166 rb_link_node(&sym->rb_node, parent, p);
167 rb_insert_color(&sym->rb_node, &self->syms);
170 struct symbol *dso__find_symbol(struct dso *self, u64 ip)
177 n = self->syms.rb_node;
180 struct symbol *s = rb_entry(n, struct symbol, rb_node);
184 else if (ip > s->end)
193 size_t dso__fprintf(struct dso *self, FILE *fp)
195 size_t ret = fprintf(fp, "dso: %s\n", self->short_name);
198 for (nd = rb_first(&self->syms); nd; nd = rb_next(nd)) {
199 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
200 ret += symbol__fprintf(pos, fp);
207 * Loads the function entries in /proc/kallsyms into kernel_map->dso,
208 * so that we can in the next step set the symbol ->end address and then
209 * call kernel_maps__split_kallsyms.
211 static int kernel_maps__load_all_kallsyms(void)
215 FILE *file = fopen("/proc/kallsyms", "r");
220 while (!feof(file)) {
227 line_len = getline(&line, &n, file);
234 line[--line_len] = '\0'; /* \n */
236 len = hex2u64(line, &start);
239 if (len + 2 >= line_len)
242 symbol_type = toupper(line[len]);
244 * We're interested only in code ('T'ext)
246 if (symbol_type != 'T' && symbol_type != 'W')
249 symbol_name = line + len + 2;
251 * Will fix up the end later, when we have all symbols sorted.
253 sym = symbol__new(start, 0, symbol_name,
254 kernel_map->dso->sym_priv_size);
257 goto out_delete_line;
259 dso__insert_symbol(kernel_map->dso, sym);
274 * Split the symbols into maps, making sure there are no overlaps, i.e. the
275 * kernel range is broken in several maps, named [kernel].N, as we don't have
276 * the original ELF section names vmlinux have.
278 static int kernel_maps__split_kallsyms(symbol_filter_t filter, int use_modules)
280 struct map *map = kernel_map;
283 struct rb_node *next = rb_first(&kernel_map->dso->syms);
284 int kernel_range = 0;
289 pos = rb_entry(next, struct symbol, rb_node);
290 next = rb_next(&pos->rb_node);
292 module = strchr(pos->name, '\t');
299 if (strcmp(map->dso->name, module)) {
300 map = kernel_maps__find_by_dso_name(module);
302 pr_err("/proc/{kallsyms,modules} "
308 * So that we look just like we get from .ko files,
309 * i.e. not prelinked, relative to map->start.
311 pos->start = map->map_ip(map, pos->start);
312 pos->end = map->map_ip(map, pos->end);
313 } else if (map != kernel_map) {
314 char dso_name[PATH_MAX];
317 snprintf(dso_name, sizeof(dso_name), "[kernel].%d",
320 dso = dso__new(dso_name,
321 kernel_map->dso->sym_priv_size);
325 map = map__new2(pos->start, dso);
331 map->map_ip = map->unmap_ip = identity__map_ip;
332 kernel_maps__insert(map);
336 if (filter && filter(map, pos)) {
338 rb_erase(&pos->rb_node, &kernel_map->dso->syms);
339 symbol__delete(pos, kernel_map->dso->sym_priv_size);
341 if (map != kernel_map) {
342 rb_erase(&pos->rb_node, &kernel_map->dso->syms);
343 dso__insert_symbol(map->dso, pos);
353 static int kernel_maps__load_kallsyms(symbol_filter_t filter, int use_modules)
355 if (kernel_maps__load_all_kallsyms())
358 dso__fixup_sym_end(kernel_map->dso);
360 return kernel_maps__split_kallsyms(filter, use_modules);
363 static size_t kernel_maps__fprintf(FILE *fp)
365 size_t printed = fprintf(fp, "Kernel maps:\n");
368 for (nd = rb_first(&kernel_maps); nd; nd = rb_next(nd)) {
369 struct map *pos = rb_entry(nd, struct map, rb_node);
371 printed += fprintf(fp, "Map:");
372 printed += map__fprintf(pos, fp);
374 printed += dso__fprintf(pos->dso, fp);
375 printed += fprintf(fp, "--\n");
379 return printed + fprintf(fp, "END kernel maps\n");
382 static int dso__load_perf_map(struct dso *self, struct map *map,
383 symbol_filter_t filter)
390 file = fopen(self->long_name, "r");
394 while (!feof(file)) {
399 line_len = getline(&line, &n, file);
406 line[--line_len] = '\0'; /* \n */
408 len = hex2u64(line, &start);
411 if (len + 2 >= line_len)
414 len += hex2u64(line + len, &size);
417 if (len + 2 >= line_len)
420 sym = symbol__new(start, size, line + len,
421 self->sym_priv_size);
424 goto out_delete_line;
426 if (filter && filter(map, sym))
427 symbol__delete(sym, self->sym_priv_size);
429 dso__insert_symbol(self, sym);
446 * elf_symtab__for_each_symbol - iterate thru all the symbols
448 * @self: struct elf_symtab instance to iterate
450 * @sym: GElf_Sym iterator
452 #define elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) \
453 for (idx = 0, gelf_getsym(syms, idx, &sym);\
455 idx++, gelf_getsym(syms, idx, &sym))
457 static inline uint8_t elf_sym__type(const GElf_Sym *sym)
459 return GELF_ST_TYPE(sym->st_info);
462 static inline int elf_sym__is_function(const GElf_Sym *sym)
464 return elf_sym__type(sym) == STT_FUNC &&
466 sym->st_shndx != SHN_UNDEF;
469 static inline int elf_sym__is_label(const GElf_Sym *sym)
471 return elf_sym__type(sym) == STT_NOTYPE &&
473 sym->st_shndx != SHN_UNDEF &&
474 sym->st_shndx != SHN_ABS;
477 static inline const char *elf_sec__name(const GElf_Shdr *shdr,
478 const Elf_Data *secstrs)
480 return secstrs->d_buf + shdr->sh_name;
483 static inline int elf_sec__is_text(const GElf_Shdr *shdr,
484 const Elf_Data *secstrs)
486 return strstr(elf_sec__name(shdr, secstrs), "text") != NULL;
489 static inline const char *elf_sym__name(const GElf_Sym *sym,
490 const Elf_Data *symstrs)
492 return symstrs->d_buf + sym->st_name;
495 static Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep,
496 GElf_Shdr *shp, const char *name,
502 while ((sec = elf_nextscn(elf, sec)) != NULL) {
505 gelf_getshdr(sec, shp);
506 str = elf_strptr(elf, ep->e_shstrndx, shp->sh_name);
507 if (!strcmp(name, str)) {
518 #define elf_section__for_each_rel(reldata, pos, pos_mem, idx, nr_entries) \
519 for (idx = 0, pos = gelf_getrel(reldata, 0, &pos_mem); \
521 ++idx, pos = gelf_getrel(reldata, idx, &pos_mem))
523 #define elf_section__for_each_rela(reldata, pos, pos_mem, idx, nr_entries) \
524 for (idx = 0, pos = gelf_getrela(reldata, 0, &pos_mem); \
526 ++idx, pos = gelf_getrela(reldata, idx, &pos_mem))
529 * We need to check if we have a .dynsym, so that we can handle the
530 * .plt, synthesizing its symbols, that aren't on the symtabs (be it
531 * .dynsym or .symtab).
532 * And always look at the original dso, not at debuginfo packages, that
533 * have the PLT data stripped out (shdr_rel_plt.sh_type == SHT_NOBITS).
535 static int dso__synthesize_plt_symbols(struct dso *self)
537 uint32_t nr_rel_entries, idx;
542 GElf_Shdr shdr_rel_plt, shdr_dynsym;
543 Elf_Data *reldata, *syms, *symstrs;
544 Elf_Scn *scn_plt_rel, *scn_symstrs, *scn_dynsym;
547 char sympltname[1024];
549 int nr = 0, symidx, fd, err = 0;
551 fd = open(self->long_name, O_RDONLY);
555 elf = elf_begin(fd, ELF_C_READ_MMAP, NULL);
559 if (gelf_getehdr(elf, &ehdr) == NULL)
562 scn_dynsym = elf_section_by_name(elf, &ehdr, &shdr_dynsym,
563 ".dynsym", &dynsym_idx);
564 if (scn_dynsym == NULL)
567 scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt,
569 if (scn_plt_rel == NULL) {
570 scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt,
572 if (scn_plt_rel == NULL)
578 if (shdr_rel_plt.sh_link != dynsym_idx)
581 if (elf_section_by_name(elf, &ehdr, &shdr_plt, ".plt", NULL) == NULL)
585 * Fetch the relocation section to find the idxes to the GOT
586 * and the symbols in the .dynsym they refer to.
588 reldata = elf_getdata(scn_plt_rel, NULL);
592 syms = elf_getdata(scn_dynsym, NULL);
596 scn_symstrs = elf_getscn(elf, shdr_dynsym.sh_link);
597 if (scn_symstrs == NULL)
600 symstrs = elf_getdata(scn_symstrs, NULL);
604 nr_rel_entries = shdr_rel_plt.sh_size / shdr_rel_plt.sh_entsize;
605 plt_offset = shdr_plt.sh_offset;
607 if (shdr_rel_plt.sh_type == SHT_RELA) {
608 GElf_Rela pos_mem, *pos;
610 elf_section__for_each_rela(reldata, pos, pos_mem, idx,
612 symidx = GELF_R_SYM(pos->r_info);
613 plt_offset += shdr_plt.sh_entsize;
614 gelf_getsym(syms, symidx, &sym);
615 snprintf(sympltname, sizeof(sympltname),
616 "%s@plt", elf_sym__name(&sym, symstrs));
618 f = symbol__new(plt_offset, shdr_plt.sh_entsize,
619 sympltname, self->sym_priv_size);
623 dso__insert_symbol(self, f);
626 } else if (shdr_rel_plt.sh_type == SHT_REL) {
627 GElf_Rel pos_mem, *pos;
628 elf_section__for_each_rel(reldata, pos, pos_mem, idx,
630 symidx = GELF_R_SYM(pos->r_info);
631 plt_offset += shdr_plt.sh_entsize;
632 gelf_getsym(syms, symidx, &sym);
633 snprintf(sympltname, sizeof(sympltname),
634 "%s@plt", elf_sym__name(&sym, symstrs));
636 f = symbol__new(plt_offset, shdr_plt.sh_entsize,
637 sympltname, self->sym_priv_size);
641 dso__insert_symbol(self, f);
655 pr_warning("%s: problems reading %s PLT info.\n",
656 __func__, self->long_name);
660 static int dso__load_sym(struct dso *self, struct map *map, const char *name,
661 int fd, symbol_filter_t filter, int kernel,
664 struct map *curr_map = map;
665 struct dso *curr_dso = self;
666 size_t dso_name_len = strlen(self->short_name);
667 Elf_Data *symstrs, *secstrs;
675 Elf_Scn *sec, *sec_strndx;
679 elf = elf_begin(fd, ELF_C_READ_MMAP, NULL);
681 pr_err("%s: cannot read %s ELF file.\n", __func__, name);
685 if (gelf_getehdr(elf, &ehdr) == NULL) {
686 pr_err("%s: cannot get elf header.\n", __func__);
690 sec = elf_section_by_name(elf, &ehdr, &shdr, ".symtab", NULL);
692 sec = elf_section_by_name(elf, &ehdr, &shdr, ".dynsym", NULL);
697 syms = elf_getdata(sec, NULL);
701 sec = elf_getscn(elf, shdr.sh_link);
705 symstrs = elf_getdata(sec, NULL);
709 sec_strndx = elf_getscn(elf, ehdr.e_shstrndx);
710 if (sec_strndx == NULL)
713 secstrs = elf_getdata(sec_strndx, NULL);
717 nr_syms = shdr.sh_size / shdr.sh_entsize;
719 memset(&sym, 0, sizeof(sym));
721 self->adjust_symbols = (ehdr.e_type == ET_EXEC ||
722 elf_section_by_name(elf, &ehdr, &shdr,
725 } else self->adjust_symbols = 0;
727 elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) {
729 const char *elf_name;
730 char *demangled = NULL;
731 int is_label = elf_sym__is_label(&sym);
732 const char *section_name;
734 if (!is_label && !elf_sym__is_function(&sym))
737 sec = elf_getscn(elf, sym.st_shndx);
741 gelf_getshdr(sec, &shdr);
743 if (is_label && !elf_sec__is_text(&shdr, secstrs))
746 elf_name = elf_sym__name(&sym, symstrs);
747 section_name = elf_sec__name(&shdr, secstrs);
749 if (kernel || kmodule) {
750 char dso_name[PATH_MAX];
752 if (strcmp(section_name,
753 curr_dso->short_name + dso_name_len) == 0)
756 if (strcmp(section_name, ".text") == 0) {
762 snprintf(dso_name, sizeof(dso_name),
763 "%s%s", self->short_name, section_name);
765 curr_map = kernel_maps__find_by_dso_name(dso_name);
766 if (curr_map == NULL) {
767 u64 start = sym.st_value;
770 start += map->start + shdr.sh_offset;
772 curr_dso = dso__new(dso_name, self->sym_priv_size);
773 if (curr_dso == NULL)
775 curr_map = map__new2(start, curr_dso);
776 if (curr_map == NULL) {
777 dso__delete(curr_dso);
780 curr_map->map_ip = identity__map_ip;
781 curr_map->unmap_ip = identity__map_ip;
782 curr_dso->origin = DSO__ORIG_KERNEL;
783 kernel_maps__insert(curr_map);
786 curr_dso = curr_map->dso;
791 if (curr_dso->adjust_symbols) {
792 pr_debug2("adjusting symbol: st_value: %Lx sh_addr: "
793 "%Lx sh_offset: %Lx\n", (u64)sym.st_value,
794 (u64)shdr.sh_addr, (u64)shdr.sh_offset);
795 sym.st_value -= shdr.sh_addr - shdr.sh_offset;
798 * We need to figure out if the object was created from C++ sources
799 * DWARF DW_compile_unit has this, but we don't always have access
802 demangled = bfd_demangle(NULL, elf_name, DMGL_PARAMS | DMGL_ANSI);
803 if (demangled != NULL)
804 elf_name = demangled;
806 f = symbol__new(sym.st_value, sym.st_size, elf_name,
807 curr_dso->sym_priv_size);
812 if (filter && filter(curr_map, f))
813 symbol__delete(f, curr_dso->sym_priv_size);
815 dso__insert_symbol(curr_dso, f);
821 * For misannotated, zeroed, ASM function sizes.
824 dso__fixup_sym_end(self);
832 #define BUILD_ID_SIZE 128
834 static char *dso__read_build_id(struct dso *self)
839 Elf_Data *build_id_data;
841 char *build_id = NULL, *bid;
844 int fd = open(self->long_name, O_RDONLY);
849 elf = elf_begin(fd, ELF_C_READ_MMAP, NULL);
851 pr_err("%s: cannot read %s ELF file.\n", __func__,
856 if (gelf_getehdr(elf, &ehdr) == NULL) {
857 pr_err("%s: cannot get elf header.\n", __func__);
861 sec = elf_section_by_name(elf, &ehdr, &shdr, ".note.gnu.build-id", NULL);
865 build_id_data = elf_getdata(sec, NULL);
866 if (build_id_data == NULL)
868 build_id = malloc(BUILD_ID_SIZE);
869 if (build_id == NULL)
871 raw = build_id_data->d_buf + 16;
874 for (i = 0; i < 20; ++i) {
875 sprintf(bid, "%02x", *raw);
879 pr_debug2("%s(%s): %s\n", __func__, self->long_name, build_id);
888 char dso__symtab_origin(const struct dso *self)
890 static const char origin[] = {
891 [DSO__ORIG_KERNEL] = 'k',
892 [DSO__ORIG_JAVA_JIT] = 'j',
893 [DSO__ORIG_FEDORA] = 'f',
894 [DSO__ORIG_UBUNTU] = 'u',
895 [DSO__ORIG_BUILDID] = 'b',
896 [DSO__ORIG_DSO] = 'd',
897 [DSO__ORIG_KMODULE] = 'K',
900 if (self == NULL || self->origin == DSO__ORIG_NOT_FOUND)
902 return origin[self->origin];
905 int dso__load(struct dso *self, struct map *map, symbol_filter_t filter)
908 char *name = malloc(size), *build_id = NULL;
917 self->adjust_symbols = 0;
919 if (strncmp(self->name, "/tmp/perf-", 10) == 0) {
920 ret = dso__load_perf_map(self, map, filter);
921 self->origin = ret > 0 ? DSO__ORIG_JAVA_JIT :
926 self->origin = DSO__ORIG_FEDORA - 1;
931 switch (self->origin) {
932 case DSO__ORIG_FEDORA:
933 snprintf(name, size, "/usr/lib/debug%s.debug",
936 case DSO__ORIG_UBUNTU:
937 snprintf(name, size, "/usr/lib/debug%s",
940 case DSO__ORIG_BUILDID:
941 build_id = dso__read_build_id(self);
942 if (build_id != NULL) {
944 "/usr/lib/debug/.build-id/%.2s/%s.debug",
945 build_id, build_id + 2);
952 snprintf(name, size, "%s", self->long_name);
959 fd = open(name, O_RDONLY);
962 ret = dso__load_sym(self, map, name, fd, filter, 0, 0);
966 * Some people seem to have debuginfo files _WITHOUT_ debug info!?!?
972 int nr_plt = dso__synthesize_plt_symbols(self);
978 if (ret < 0 && strstr(self->name, " (deleted)") != NULL)
983 struct map *kernel_map;
985 static void kernel_maps__insert(struct map *map)
987 maps__insert(&kernel_maps, map);
990 struct symbol *kernel_maps__find_symbol(u64 ip, struct map **mapp)
992 struct map *map = maps__find(&kernel_maps, ip);
998 ip = map->map_ip(map, ip);
999 return map->dso->find_symbol(map->dso, ip);
1005 struct map *kernel_maps__find_by_dso_name(const char *name)
1009 for (nd = rb_first(&kernel_maps); nd; nd = rb_next(nd)) {
1010 struct map *map = rb_entry(nd, struct map, rb_node);
1012 if (map->dso && strcmp(map->dso->name, name) == 0)
1019 static int dso__load_module_sym(struct dso *self, struct map *map,
1020 symbol_filter_t filter)
1022 int err = 0, fd = open(self->long_name, O_RDONLY);
1024 self->loaded = true;
1027 pr_err("%s: cannot open %s\n", __func__, self->long_name);
1031 err = dso__load_sym(self, map, self->long_name, fd, filter, 0, 1);
1037 static int dsos__load_modules_sym_dir(char *dirname, symbol_filter_t filter)
1039 struct dirent *dent;
1040 int nr_symbols = 0, err;
1041 DIR *dir = opendir(dirname);
1044 pr_err("%s: cannot open %s dir\n", __func__, dirname);
1048 while ((dent = readdir(dir)) != NULL) {
1049 char path[PATH_MAX];
1051 if (dent->d_type == DT_DIR) {
1052 if (!strcmp(dent->d_name, ".") ||
1053 !strcmp(dent->d_name, ".."))
1056 snprintf(path, sizeof(path), "%s/%s",
1057 dirname, dent->d_name);
1058 err = dsos__load_modules_sym_dir(path, filter);
1062 char *dot = strrchr(dent->d_name, '.'),
1065 struct rb_node *last;
1067 if (dot == NULL || strcmp(dot, ".ko"))
1069 snprintf(dso_name, sizeof(dso_name), "[%.*s]",
1070 (int)(dot - dent->d_name), dent->d_name);
1072 strxfrchar(dso_name, '-', '_');
1073 map = kernel_maps__find_by_dso_name(dso_name);
1077 snprintf(path, sizeof(path), "%s/%s",
1078 dirname, dent->d_name);
1080 map->dso->long_name = strdup(path);
1081 if (map->dso->long_name == NULL)
1084 err = dso__load_module_sym(map->dso, map, filter);
1087 last = rb_last(&map->dso->syms);
1091 * We do this here as well, even having the
1092 * symbol size found in the symtab because
1093 * misannotated ASM symbols may have the size
1096 dso__fixup_sym_end(map->dso);
1098 sym = rb_entry(last, struct symbol, rb_node);
1099 map->end = map->start + sym->end;
1111 static int dsos__load_modules_sym(symbol_filter_t filter)
1114 char modules_path[PATH_MAX];
1116 if (uname(&uts) < 0)
1119 snprintf(modules_path, sizeof(modules_path), "/lib/modules/%s/kernel",
1122 return dsos__load_modules_sym_dir(modules_path, filter);
1126 * Constructor variant for modules (where we know from /proc/modules where
1127 * they are loaded) and for vmlinux, where only after we load all the
1128 * symbols we'll know where it starts and ends.
1130 static struct map *map__new2(u64 start, struct dso *dso)
1132 struct map *self = malloc(sizeof(*self));
1136 * ->end will be filled after we load all the symbols
1138 map__init(self, start, 0, 0, dso);
1144 static int dsos__load_modules(unsigned int sym_priv_size)
1148 FILE *file = fopen("/proc/modules", "r");
1154 while (!feof(file)) {
1155 char name[PATH_MAX];
1161 line_len = getline(&line, &n, file);
1168 line[--line_len] = '\0'; /* \n */
1170 sep = strrchr(line, 'x');
1174 hex2u64(sep + 1, &start);
1176 sep = strchr(line, ' ');
1182 snprintf(name, sizeof(name), "[%s]", line);
1183 dso = dso__new(name, sym_priv_size);
1186 goto out_delete_line;
1188 map = map__new2(start, dso);
1191 goto out_delete_line;
1194 dso->origin = DSO__ORIG_KMODULE;
1195 kernel_maps__insert(map);
1210 static int dso__load_vmlinux(struct dso *self, struct map *map,
1211 const char *vmlinux, symbol_filter_t filter)
1213 int err, fd = open(vmlinux, O_RDONLY);
1215 self->loaded = true;
1220 err = dso__load_sym(self, map, self->long_name, fd, filter, 1, 0);
1227 int dsos__load_kernel(const char *vmlinux, unsigned int sym_priv_size,
1228 symbol_filter_t filter, int use_modules)
1231 struct dso *dso = dso__new(vmlinux, sym_priv_size);
1236 dso->short_name = "[kernel]";
1237 kernel_map = map__new2(0, dso);
1238 if (kernel_map == NULL)
1239 goto out_delete_dso;
1241 kernel_map->map_ip = kernel_map->unmap_ip = identity__map_ip;
1243 if (use_modules && dsos__load_modules(sym_priv_size) < 0) {
1244 pr_warning("Failed to load list of modules in use! "
1250 err = dso__load_vmlinux(dso, kernel_map, vmlinux, filter);
1251 if (err > 0 && use_modules) {
1252 int syms = dsos__load_modules_sym(filter);
1255 pr_warning("Failed to read module symbols!"
1256 " Continuing...\n");
1263 err = kernel_maps__load_kallsyms(filter, use_modules);
1266 struct rb_node *node = rb_first(&dso->syms);
1267 struct symbol *sym = rb_entry(node, struct symbol, rb_node);
1269 kernel_map->start = sym->start;
1270 node = rb_last(&dso->syms);
1271 sym = rb_entry(node, struct symbol, rb_node);
1272 kernel_map->end = sym->end;
1274 dso->origin = DSO__ORIG_KERNEL;
1275 kernel_maps__insert(kernel_map);
1277 * Now that we have all sorted out, just set the ->end of all
1280 kernel_maps__fixup_end();
1284 kernel_maps__fprintf(stderr);
1297 const char *vmlinux_name = "vmlinux";
1300 static void dsos__add(struct dso *dso)
1302 list_add_tail(&dso->node, &dsos);
1305 static struct dso *dsos__find(const char *name)
1309 list_for_each_entry(pos, &dsos, node)
1310 if (strcmp(pos->name, name) == 0)
1315 struct dso *dsos__findnew(const char *name, unsigned int sym_priv_size)
1317 struct dso *dso = dsos__find(name);
1320 dso = dso__new(name, sym_priv_size);
1328 void dsos__fprintf(FILE *fp)
1332 list_for_each_entry(pos, &dsos, node)
1333 dso__fprintf(pos, fp);
1336 int load_kernel(unsigned int sym_priv_size, symbol_filter_t filter)
1338 if (dsos__load_kernel(vmlinux_name, sym_priv_size, filter,
1342 vdso = dso__new("[vdso]", 0);
1351 void symbol__init(void)
1353 elf_version(EV_CURRENT);