perf symbols: only set vmlinux longname & mark loaded if really loaded
[linux-2.6-microblaze.git] / tools / perf / util / symbol.c
index 50958bb..96dbf28 100644 (file)
@@ -15,8 +15,6 @@
 #include "symbol.h"
 #include "strlist.h"
 
-#include <libelf.h>
-#include <gelf.h>
 #include <elf.h>
 #include <limits.h>
 #include <sys/utsname.h>
 #define KSYM_NAME_LEN 256
 #endif
 
-#ifndef NT_GNU_BUILD_ID
-#define NT_GNU_BUILD_ID 3
-#endif
-
-static bool dso__build_id_equal(const struct dso *dso, u8 *build_id);
-static int elf_read_build_id(Elf *elf, void *bf, size_t size);
-static void dsos__add(struct list_head *head, struct dso *dso);
-static struct map *map__new2(u64 start, struct dso *dso, enum map_type type);
+static void dso_cache__free(struct rb_root *root);
 static int dso__load_kernel_sym(struct dso *dso, struct map *map,
                                symbol_filter_t filter);
 static int dso__load_guest_kernel_sym(struct dso *dso, struct map *map,
@@ -48,6 +39,31 @@ struct symbol_conf symbol_conf = {
        .symfs            = "",
 };
 
+static enum dso_binary_type binary_type_symtab[] = {
+       DSO_BINARY_TYPE__KALLSYMS,
+       DSO_BINARY_TYPE__GUEST_KALLSYMS,
+       DSO_BINARY_TYPE__JAVA_JIT,
+       DSO_BINARY_TYPE__DEBUGLINK,
+       DSO_BINARY_TYPE__BUILD_ID_CACHE,
+       DSO_BINARY_TYPE__FEDORA_DEBUGINFO,
+       DSO_BINARY_TYPE__UBUNTU_DEBUGINFO,
+       DSO_BINARY_TYPE__BUILDID_DEBUGINFO,
+       DSO_BINARY_TYPE__SYSTEM_PATH_DSO,
+       DSO_BINARY_TYPE__GUEST_KMODULE,
+       DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE,
+       DSO_BINARY_TYPE__NOT_FOUND,
+};
+
+#define DSO_BINARY_TYPE__SYMTAB_CNT ARRAY_SIZE(binary_type_symtab)
+
+static enum dso_binary_type binary_type_data[] = {
+       DSO_BINARY_TYPE__BUILD_ID_CACHE,
+       DSO_BINARY_TYPE__SYSTEM_PATH_DSO,
+       DSO_BINARY_TYPE__NOT_FOUND,
+};
+
+#define DSO_BINARY_TYPE__DATA_CNT ARRAY_SIZE(binary_type_data)
+
 int dso__name_len(const struct dso *dso)
 {
        if (!dso)
@@ -144,7 +160,7 @@ static int choose_best_symbol(struct symbol *syma, struct symbol *symb)
                return SYMBOL_B;
 }
 
-static void symbols__fixup_duplicate(struct rb_root *symbols)
+void symbols__fixup_duplicate(struct rb_root *symbols)
 {
        struct rb_node *nd;
        struct symbol *curr, *next;
@@ -173,7 +189,7 @@ again:
        }
 }
 
-static void symbols__fixup_end(struct rb_root *symbols)
+void symbols__fixup_end(struct rb_root *symbols)
 {
        struct rb_node *nd, *prevnd = rb_first(symbols);
        struct symbol *curr, *prev;
@@ -196,7 +212,7 @@ static void symbols__fixup_end(struct rb_root *symbols)
                curr->end = roundup(curr->start, 4096);
 }
 
-static void __map_groups__fixup_end(struct map_groups *mg, enum map_type type)
+void __map_groups__fixup_end(struct map_groups *mg, enum map_type type)
 {
        struct map *prev, *curr;
        struct rb_node *nd, *prevnd = rb_first(&mg->maps[type]);
@@ -226,8 +242,7 @@ static void map_groups__fixup_end(struct map_groups *mg)
                __map_groups__fixup_end(mg, i);
 }
 
-static struct symbol *symbol__new(u64 start, u64 len, u8 binding,
-                                 const char *name)
+struct symbol *symbol__new(u64 start, u64 len, u8 binding, const char *name)
 {
        size_t namelen = strlen(name) + 1;
        struct symbol *sym = calloc(1, (symbol_conf.priv_size +
@@ -318,7 +333,9 @@ struct dso *dso__new(const char *name)
                dso__set_short_name(dso, dso->name);
                for (i = 0; i < MAP__NR_TYPES; ++i)
                        dso->symbols[i] = dso->symbol_names[i] = RB_ROOT;
-               dso->symtab_type = SYMTAB__NOT_FOUND;
+               dso->cache = RB_ROOT;
+               dso->symtab_type = DSO_BINARY_TYPE__NOT_FOUND;
+               dso->data_type   = DSO_BINARY_TYPE__NOT_FOUND;
                dso->loaded = 0;
                dso->sorted_by_name = 0;
                dso->has_build_id = 0;
@@ -352,6 +369,7 @@ void dso__delete(struct dso *dso)
                free((char *)dso->short_name);
        if (dso->lname_alloc)
                free(dso->long_name);
+       dso_cache__free(&dso->cache);
        free(dso);
 }
 
@@ -361,7 +379,7 @@ void dso__set_build_id(struct dso *dso, void *build_id)
        dso->has_build_id = 1;
 }
 
-static void symbols__insert(struct rb_root *symbols, struct symbol *sym)
+void symbols__insert(struct rb_root *symbols, struct symbol *sym)
 {
        struct rb_node **p = &symbols->rb_node;
        struct rb_node *parent = NULL;
@@ -545,7 +563,7 @@ size_t dso__fprintf(struct dso *dso, enum map_type type, FILE *fp)
 
 int kallsyms__parse(const char *filename, void *arg,
                    int (*process_symbol)(void *arg, const char *name,
-                                         char type, u64 start, u64 end))
+                                         char type, u64 start))
 {
        char *line = NULL;
        size_t n;
@@ -585,13 +603,8 @@ int kallsyms__parse(const char *filename, void *arg,
                        break;
                }
 
-               /*
-                * module symbols are not sorted so we add all
-                * symbols with zero length and rely on
-                * symbols__fixup_end() to fix it up.
-                */
                err = process_symbol(arg, symbol_name,
-                                    symbol_type, start, start);
+                                    symbol_type, start);
                if (err)
                        break;
        }
@@ -618,7 +631,7 @@ static u8 kallsyms2elf_type(char type)
 }
 
 static int map__process_kallsym_symbol(void *arg, const char *name,
-                                      char type, u64 start, u64 end)
+                                      char type, u64 start)
 {
        struct symbol *sym;
        struct process_kallsyms_args *a = arg;
@@ -627,8 +640,12 @@ static int map__process_kallsym_symbol(void *arg, const char *name,
        if (!symbol_type__is_a(type, a->map->type))
                return 0;
 
-       sym = symbol__new(start, end - start + 1,
-                         kallsyms2elf_type(type), name);
+       /*
+        * module symbols are not sorted so we add all
+        * symbols, setting length to 0, and rely on
+        * symbols__fixup_end() to fix it up.
+        */
+       sym = symbol__new(start, 0, kallsyms2elf_type(type), name);
        if (sym == NULL)
                return -ENOMEM;
        /*
@@ -806,9 +823,9 @@ int dso__load_kallsyms(struct dso *dso, const char *filename,
        symbols__fixup_end(&dso->symbols[map->type]);
 
        if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
-               dso->symtab_type = SYMTAB__GUEST_KALLSYMS;
+               dso->symtab_type = DSO_BINARY_TYPE__GUEST_KALLSYMS;
        else
-               dso->symtab_type = SYMTAB__KALLSYMS;
+               dso->symtab_type = DSO_BINARY_TYPE__KALLSYMS;
 
        return dso__split_kallsyms(dso, map, filter);
 }
@@ -875,556 +892,7 @@ out_failure:
        return -1;
 }
 
-/**
- * elf_symtab__for_each_symbol - iterate thru all the symbols
- *
- * @syms: struct elf_symtab instance to iterate
- * @idx: uint32_t idx
- * @sym: GElf_Sym iterator
- */
-#define elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) \
-       for (idx = 0, gelf_getsym(syms, idx, &sym);\
-            idx < nr_syms; \
-            idx++, gelf_getsym(syms, idx, &sym))
-
-static inline uint8_t elf_sym__type(const GElf_Sym *sym)
-{
-       return GELF_ST_TYPE(sym->st_info);
-}
-
-static inline int elf_sym__is_function(const GElf_Sym *sym)
-{
-       return elf_sym__type(sym) == STT_FUNC &&
-              sym->st_name != 0 &&
-              sym->st_shndx != SHN_UNDEF;
-}
-
-static inline bool elf_sym__is_object(const GElf_Sym *sym)
-{
-       return elf_sym__type(sym) == STT_OBJECT &&
-               sym->st_name != 0 &&
-               sym->st_shndx != SHN_UNDEF;
-}
-
-static inline int elf_sym__is_label(const GElf_Sym *sym)
-{
-       return elf_sym__type(sym) == STT_NOTYPE &&
-               sym->st_name != 0 &&
-               sym->st_shndx != SHN_UNDEF &&
-               sym->st_shndx != SHN_ABS;
-}
-
-static inline const char *elf_sec__name(const GElf_Shdr *shdr,
-                                       const Elf_Data *secstrs)
-{
-       return secstrs->d_buf + shdr->sh_name;
-}
-
-static inline int elf_sec__is_text(const GElf_Shdr *shdr,
-                                       const Elf_Data *secstrs)
-{
-       return strstr(elf_sec__name(shdr, secstrs), "text") != NULL;
-}
-
-static inline bool elf_sec__is_data(const GElf_Shdr *shdr,
-                                   const Elf_Data *secstrs)
-{
-       return strstr(elf_sec__name(shdr, secstrs), "data") != NULL;
-}
-
-static inline const char *elf_sym__name(const GElf_Sym *sym,
-                                       const Elf_Data *symstrs)
-{
-       return symstrs->d_buf + sym->st_name;
-}
-
-static Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep,
-                                   GElf_Shdr *shp, const char *name,
-                                   size_t *idx)
-{
-       Elf_Scn *sec = NULL;
-       size_t cnt = 1;
-
-       while ((sec = elf_nextscn(elf, sec)) != NULL) {
-               char *str;
-
-               gelf_getshdr(sec, shp);
-               str = elf_strptr(elf, ep->e_shstrndx, shp->sh_name);
-               if (!strcmp(name, str)) {
-                       if (idx)
-                               *idx = cnt;
-                       break;
-               }
-               ++cnt;
-       }
-
-       return sec;
-}
-
-#define elf_section__for_each_rel(reldata, pos, pos_mem, idx, nr_entries) \
-       for (idx = 0, pos = gelf_getrel(reldata, 0, &pos_mem); \
-            idx < nr_entries; \
-            ++idx, pos = gelf_getrel(reldata, idx, &pos_mem))
-
-#define elf_section__for_each_rela(reldata, pos, pos_mem, idx, nr_entries) \
-       for (idx = 0, pos = gelf_getrela(reldata, 0, &pos_mem); \
-            idx < nr_entries; \
-            ++idx, pos = gelf_getrela(reldata, idx, &pos_mem))
-
-/*
- * We need to check if we have a .dynsym, so that we can handle the
- * .plt, synthesizing its symbols, that aren't on the symtabs (be it
- * .dynsym or .symtab).
- * And always look at the original dso, not at debuginfo packages, that
- * have the PLT data stripped out (shdr_rel_plt.sh_type == SHT_NOBITS).
- */
-static int
-dso__synthesize_plt_symbols(struct dso *dso, char *name, struct map *map,
-                           symbol_filter_t filter)
-{
-       uint32_t nr_rel_entries, idx;
-       GElf_Sym sym;
-       u64 plt_offset;
-       GElf_Shdr shdr_plt;
-       struct symbol *f;
-       GElf_Shdr shdr_rel_plt, shdr_dynsym;
-       Elf_Data *reldata, *syms, *symstrs;
-       Elf_Scn *scn_plt_rel, *scn_symstrs, *scn_dynsym;
-       size_t dynsym_idx;
-       GElf_Ehdr ehdr;
-       char sympltname[1024];
-       Elf *elf;
-       int nr = 0, symidx, fd, err = 0;
-
-       fd = open(name, O_RDONLY);
-       if (fd < 0)
-               goto out;
-
-       elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
-       if (elf == NULL)
-               goto out_close;
-
-       if (gelf_getehdr(elf, &ehdr) == NULL)
-               goto out_elf_end;
-
-       scn_dynsym = elf_section_by_name(elf, &ehdr, &shdr_dynsym,
-                                        ".dynsym", &dynsym_idx);
-       if (scn_dynsym == NULL)
-               goto out_elf_end;
-
-       scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt,
-                                         ".rela.plt", NULL);
-       if (scn_plt_rel == NULL) {
-               scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt,
-                                                 ".rel.plt", NULL);
-               if (scn_plt_rel == NULL)
-                       goto out_elf_end;
-       }
-
-       err = -1;
-
-       if (shdr_rel_plt.sh_link != dynsym_idx)
-               goto out_elf_end;
-
-       if (elf_section_by_name(elf, &ehdr, &shdr_plt, ".plt", NULL) == NULL)
-               goto out_elf_end;
-
-       /*
-        * Fetch the relocation section to find the idxes to the GOT
-        * and the symbols in the .dynsym they refer to.
-        */
-       reldata = elf_getdata(scn_plt_rel, NULL);
-       if (reldata == NULL)
-               goto out_elf_end;
-
-       syms = elf_getdata(scn_dynsym, NULL);
-       if (syms == NULL)
-               goto out_elf_end;
-
-       scn_symstrs = elf_getscn(elf, shdr_dynsym.sh_link);
-       if (scn_symstrs == NULL)
-               goto out_elf_end;
-
-       symstrs = elf_getdata(scn_symstrs, NULL);
-       if (symstrs == NULL)
-               goto out_elf_end;
-
-       nr_rel_entries = shdr_rel_plt.sh_size / shdr_rel_plt.sh_entsize;
-       plt_offset = shdr_plt.sh_offset;
-
-       if (shdr_rel_plt.sh_type == SHT_RELA) {
-               GElf_Rela pos_mem, *pos;
-
-               elf_section__for_each_rela(reldata, pos, pos_mem, idx,
-                                          nr_rel_entries) {
-                       symidx = GELF_R_SYM(pos->r_info);
-                       plt_offset += shdr_plt.sh_entsize;
-                       gelf_getsym(syms, symidx, &sym);
-                       snprintf(sympltname, sizeof(sympltname),
-                                "%s@plt", elf_sym__name(&sym, symstrs));
-
-                       f = symbol__new(plt_offset, shdr_plt.sh_entsize,
-                                       STB_GLOBAL, sympltname);
-                       if (!f)
-                               goto out_elf_end;
-
-                       if (filter && filter(map, f))
-                               symbol__delete(f);
-                       else {
-                               symbols__insert(&dso->symbols[map->type], f);
-                               ++nr;
-                       }
-               }
-       } else if (shdr_rel_plt.sh_type == SHT_REL) {
-               GElf_Rel pos_mem, *pos;
-               elf_section__for_each_rel(reldata, pos, pos_mem, idx,
-                                         nr_rel_entries) {
-                       symidx = GELF_R_SYM(pos->r_info);
-                       plt_offset += shdr_plt.sh_entsize;
-                       gelf_getsym(syms, symidx, &sym);
-                       snprintf(sympltname, sizeof(sympltname),
-                                "%s@plt", elf_sym__name(&sym, symstrs));
-
-                       f = symbol__new(plt_offset, shdr_plt.sh_entsize,
-                                       STB_GLOBAL, sympltname);
-                       if (!f)
-                               goto out_elf_end;
-
-                       if (filter && filter(map, f))
-                               symbol__delete(f);
-                       else {
-                               symbols__insert(&dso->symbols[map->type], f);
-                               ++nr;
-                       }
-               }
-       }
-
-       err = 0;
-out_elf_end:
-       elf_end(elf);
-out_close:
-       close(fd);
-
-       if (err == 0)
-               return nr;
-out:
-       pr_debug("%s: problems reading %s PLT info.\n",
-                __func__, dso->long_name);
-       return 0;
-}
-
-static bool elf_sym__is_a(GElf_Sym *sym, enum map_type type)
-{
-       switch (type) {
-       case MAP__FUNCTION:
-               return elf_sym__is_function(sym);
-       case MAP__VARIABLE:
-               return elf_sym__is_object(sym);
-       default:
-               return false;
-       }
-}
-
-static bool elf_sec__is_a(GElf_Shdr *shdr, Elf_Data *secstrs,
-                         enum map_type type)
-{
-       switch (type) {
-       case MAP__FUNCTION:
-               return elf_sec__is_text(shdr, secstrs);
-       case MAP__VARIABLE:
-               return elf_sec__is_data(shdr, secstrs);
-       default:
-               return false;
-       }
-}
-
-static size_t elf_addr_to_index(Elf *elf, GElf_Addr addr)
-{
-       Elf_Scn *sec = NULL;
-       GElf_Shdr shdr;
-       size_t cnt = 1;
-
-       while ((sec = elf_nextscn(elf, sec)) != NULL) {
-               gelf_getshdr(sec, &shdr);
-
-               if ((addr >= shdr.sh_addr) &&
-                   (addr < (shdr.sh_addr + shdr.sh_size)))
-                       return cnt;
-
-               ++cnt;
-       }
-
-       return -1;
-}
-
-static int dso__swap_init(struct dso *dso, unsigned char eidata)
-{
-       static unsigned int const endian = 1;
-
-       dso->needs_swap = DSO_SWAP__NO;
-
-       switch (eidata) {
-       case ELFDATA2LSB:
-               /* We are big endian, DSO is little endian. */
-               if (*(unsigned char const *)&endian != 1)
-                       dso->needs_swap = DSO_SWAP__YES;
-               break;
-
-       case ELFDATA2MSB:
-               /* We are little endian, DSO is big endian. */
-               if (*(unsigned char const *)&endian != 0)
-                       dso->needs_swap = DSO_SWAP__YES;
-               break;
-
-       default:
-               pr_err("unrecognized DSO data encoding %d\n", eidata);
-               return -EINVAL;
-       }
-
-       return 0;
-}
-
-static int dso__load_sym(struct dso *dso, struct map *map, const char *name,
-                        int fd, symbol_filter_t filter, int kmodule,
-                        int want_symtab)
-{
-       struct kmap *kmap = dso->kernel ? map__kmap(map) : NULL;
-       struct map *curr_map = map;
-       struct dso *curr_dso = dso;
-       Elf_Data *symstrs, *secstrs;
-       uint32_t nr_syms;
-       int err = -1;
-       uint32_t idx;
-       GElf_Ehdr ehdr;
-       GElf_Shdr shdr, opdshdr;
-       Elf_Data *syms, *opddata = NULL;
-       GElf_Sym sym;
-       Elf_Scn *sec, *sec_strndx, *opdsec;
-       Elf *elf;
-       int nr = 0;
-       size_t opdidx = 0;
-
-       elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
-       if (elf == NULL) {
-               pr_debug("%s: cannot read %s ELF file.\n", __func__, name);
-               goto out_close;
-       }
-
-       if (gelf_getehdr(elf, &ehdr) == NULL) {
-               pr_debug("%s: cannot get elf header.\n", __func__);
-               goto out_elf_end;
-       }
-
-       if (dso__swap_init(dso, ehdr.e_ident[EI_DATA]))
-               goto out_elf_end;
-
-       /* Always reject images with a mismatched build-id: */
-       if (dso->has_build_id) {
-               u8 build_id[BUILD_ID_SIZE];
-
-               if (elf_read_build_id(elf, build_id, BUILD_ID_SIZE) < 0)
-                       goto out_elf_end;
-
-               if (!dso__build_id_equal(dso, build_id))
-                       goto out_elf_end;
-       }
-
-       sec = elf_section_by_name(elf, &ehdr, &shdr, ".symtab", NULL);
-       if (sec == NULL) {
-               if (want_symtab)
-                       goto out_elf_end;
-
-               sec = elf_section_by_name(elf, &ehdr, &shdr, ".dynsym", NULL);
-               if (sec == NULL)
-                       goto out_elf_end;
-       }
-
-       opdsec = elf_section_by_name(elf, &ehdr, &opdshdr, ".opd", &opdidx);
-       if (opdshdr.sh_type != SHT_PROGBITS)
-               opdsec = NULL;
-       if (opdsec)
-               opddata = elf_rawdata(opdsec, NULL);
-
-       syms = elf_getdata(sec, NULL);
-       if (syms == NULL)
-               goto out_elf_end;
-
-       sec = elf_getscn(elf, shdr.sh_link);
-       if (sec == NULL)
-               goto out_elf_end;
-
-       symstrs = elf_getdata(sec, NULL);
-       if (symstrs == NULL)
-               goto out_elf_end;
-
-       sec_strndx = elf_getscn(elf, ehdr.e_shstrndx);
-       if (sec_strndx == NULL)
-               goto out_elf_end;
-
-       secstrs = elf_getdata(sec_strndx, NULL);
-       if (secstrs == NULL)
-               goto out_elf_end;
-
-       nr_syms = shdr.sh_size / shdr.sh_entsize;
-
-       memset(&sym, 0, sizeof(sym));
-       if (dso->kernel == DSO_TYPE_USER) {
-               dso->adjust_symbols = (ehdr.e_type == ET_EXEC ||
-                               elf_section_by_name(elf, &ehdr, &shdr,
-                                                    ".gnu.prelink_undo",
-                                                    NULL) != NULL);
-       } else {
-               dso->adjust_symbols = 0;
-       }
-       elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) {
-               struct symbol *f;
-               const char *elf_name = elf_sym__name(&sym, symstrs);
-               char *demangled = NULL;
-               int is_label = elf_sym__is_label(&sym);
-               const char *section_name;
-
-               if (kmap && kmap->ref_reloc_sym && kmap->ref_reloc_sym->name &&
-                   strcmp(elf_name, kmap->ref_reloc_sym->name) == 0)
-                       kmap->ref_reloc_sym->unrelocated_addr = sym.st_value;
-
-               if (!is_label && !elf_sym__is_a(&sym, map->type))
-                       continue;
-
-               /* Reject ARM ELF "mapping symbols": these aren't unique and
-                * don't identify functions, so will confuse the profile
-                * output: */
-               if (ehdr.e_machine == EM_ARM) {
-                       if (!strcmp(elf_name, "$a") ||
-                           !strcmp(elf_name, "$d") ||
-                           !strcmp(elf_name, "$t"))
-                               continue;
-               }
-
-               if (opdsec && sym.st_shndx == opdidx) {
-                       u32 offset = sym.st_value - opdshdr.sh_addr;
-                       u64 *opd = opddata->d_buf + offset;
-                       sym.st_value = DSO__SWAP(dso, u64, *opd);
-                       sym.st_shndx = elf_addr_to_index(elf, sym.st_value);
-               }
-
-               sec = elf_getscn(elf, sym.st_shndx);
-               if (!sec)
-                       goto out_elf_end;
-
-               gelf_getshdr(sec, &shdr);
-
-               if (is_label && !elf_sec__is_a(&shdr, secstrs, map->type))
-                       continue;
-
-               section_name = elf_sec__name(&shdr, secstrs);
-
-               /* On ARM, symbols for thumb functions have 1 added to
-                * the symbol address as a flag - remove it */
-               if ((ehdr.e_machine == EM_ARM) &&
-                   (map->type == MAP__FUNCTION) &&
-                   (sym.st_value & 1))
-                       --sym.st_value;
-
-               if (dso->kernel != DSO_TYPE_USER || kmodule) {
-                       char dso_name[PATH_MAX];
-
-                       if (strcmp(section_name,
-                                  (curr_dso->short_name +
-                                   dso->short_name_len)) == 0)
-                               goto new_symbol;
-
-                       if (strcmp(section_name, ".text") == 0) {
-                               curr_map = map;
-                               curr_dso = dso;
-                               goto new_symbol;
-                       }
-
-                       snprintf(dso_name, sizeof(dso_name),
-                                "%s%s", dso->short_name, section_name);
-
-                       curr_map = map_groups__find_by_name(kmap->kmaps, map->type, dso_name);
-                       if (curr_map == NULL) {
-                               u64 start = sym.st_value;
-
-                               if (kmodule)
-                                       start += map->start + shdr.sh_offset;
-
-                               curr_dso = dso__new(dso_name);
-                               if (curr_dso == NULL)
-                                       goto out_elf_end;
-                               curr_dso->kernel = dso->kernel;
-                               curr_dso->long_name = dso->long_name;
-                               curr_dso->long_name_len = dso->long_name_len;
-                               curr_map = map__new2(start, curr_dso,
-                                                    map->type);
-                               if (curr_map == NULL) {
-                                       dso__delete(curr_dso);
-                                       goto out_elf_end;
-                               }
-                               curr_map->map_ip = identity__map_ip;
-                               curr_map->unmap_ip = identity__map_ip;
-                               curr_dso->symtab_type = dso->symtab_type;
-                               map_groups__insert(kmap->kmaps, curr_map);
-                               dsos__add(&dso->node, curr_dso);
-                               dso__set_loaded(curr_dso, map->type);
-                       } else
-                               curr_dso = curr_map->dso;
-
-                       goto new_symbol;
-               }
-
-               if (curr_dso->adjust_symbols) {
-                       pr_debug4("%s: adjusting symbol: st_value: %#" PRIx64 " "
-                                 "sh_addr: %#" PRIx64 " sh_offset: %#" PRIx64 "\n", __func__,
-                                 (u64)sym.st_value, (u64)shdr.sh_addr,
-                                 (u64)shdr.sh_offset);
-                       sym.st_value -= shdr.sh_addr - shdr.sh_offset;
-               }
-               /*
-                * We need to figure out if the object was created from C++ sources
-                * DWARF DW_compile_unit has this, but we don't always have access
-                * to it...
-                */
-               demangled = bfd_demangle(NULL, elf_name, DMGL_PARAMS | DMGL_ANSI);
-               if (demangled != NULL)
-                       elf_name = demangled;
-new_symbol:
-               f = symbol__new(sym.st_value, sym.st_size,
-                               GELF_ST_BIND(sym.st_info), elf_name);
-               free(demangled);
-               if (!f)
-                       goto out_elf_end;
-
-               if (filter && filter(curr_map, f))
-                       symbol__delete(f);
-               else {
-                       symbols__insert(&curr_dso->symbols[curr_map->type], f);
-                       nr++;
-               }
-       }
-
-       /*
-        * For misannotated, zeroed, ASM function sizes.
-        */
-       if (nr > 0) {
-               symbols__fixup_duplicate(&dso->symbols[map->type]);
-               symbols__fixup_end(&dso->symbols[map->type]);
-               if (kmap) {
-                       /*
-                        * We need to fixup this here too because we create new
-                        * maps here, for things like vsyscall sections.
-                        */
-                       __map_groups__fixup_end(kmap->kmaps, map->type);
-               }
-       }
-       err = nr;
-out_elf_end:
-       elf_end(elf);
-out_close:
-       return err;
-}
-
-static bool dso__build_id_equal(const struct dso *dso, u8 *build_id)
+bool dso__build_id_equal(const struct dso *dso, u8 *build_id)
 {
        return memcmp(dso->build_id, build_id, sizeof(dso->build_id)) == 0;
 }
@@ -1451,241 +919,113 @@ bool __dsos__read_build_ids(struct list_head *head, bool with_hits)
        return have_build_id;
 }
 
-/*
- * Align offset to 4 bytes as needed for note name and descriptor data.
- */
-#define NOTE_ALIGN(n) (((n) + 3) & -4U)
-
-static int elf_read_build_id(Elf *elf, void *bf, size_t size)
-{
-       int err = -1;
-       GElf_Ehdr ehdr;
-       GElf_Shdr shdr;
-       Elf_Data *data;
-       Elf_Scn *sec;
-       Elf_Kind ek;
-       void *ptr;
-
-       if (size < BUILD_ID_SIZE)
-               goto out;
-
-       ek = elf_kind(elf);
-       if (ek != ELF_K_ELF)
-               goto out;
-
-       if (gelf_getehdr(elf, &ehdr) == NULL) {
-               pr_err("%s: cannot get elf header.\n", __func__);
-               goto out;
-       }
-
-       /*
-        * Check following sections for notes:
-        *   '.note.gnu.build-id'
-        *   '.notes'
-        *   '.note' (VDSO specific)
-        */
-       do {
-               sec = elf_section_by_name(elf, &ehdr, &shdr,
-                                         ".note.gnu.build-id", NULL);
-               if (sec)
-                       break;
-
-               sec = elf_section_by_name(elf, &ehdr, &shdr,
-                                         ".notes", NULL);
-               if (sec)
-                       break;
-
-               sec = elf_section_by_name(elf, &ehdr, &shdr,
-                                         ".note", NULL);
-               if (sec)
-                       break;
-
-               return err;
-
-       } while (0);
-
-       data = elf_getdata(sec, NULL);
-       if (data == NULL)
-               goto out;
-
-       ptr = data->d_buf;
-       while (ptr < (data->d_buf + data->d_size)) {
-               GElf_Nhdr *nhdr = ptr;
-               size_t namesz = NOTE_ALIGN(nhdr->n_namesz),
-                      descsz = NOTE_ALIGN(nhdr->n_descsz);
-               const char *name;
-
-               ptr += sizeof(*nhdr);
-               name = ptr;
-               ptr += namesz;
-               if (nhdr->n_type == NT_GNU_BUILD_ID &&
-                   nhdr->n_namesz == sizeof("GNU")) {
-                       if (memcmp(name, "GNU", sizeof("GNU")) == 0) {
-                               size_t sz = min(size, descsz);
-                               memcpy(bf, ptr, sz);
-                               memset(bf + sz, 0, size - sz);
-                               err = descsz;
-                               break;
-                       }
-               }
-               ptr += descsz;
-       }
-
-out:
-       return err;
-}
-
-int filename__read_build_id(const char *filename, void *bf, size_t size)
+char dso__symtab_origin(const struct dso *dso)
 {
-       int fd, err = -1;
-       Elf *elf;
-
-       if (size < BUILD_ID_SIZE)
-               goto out;
-
-       fd = open(filename, O_RDONLY);
-       if (fd < 0)
-               goto out;
-
-       elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
-       if (elf == NULL) {
-               pr_debug2("%s: cannot read %s ELF file.\n", __func__, filename);
-               goto out_close;
-       }
-
-       err = elf_read_build_id(elf, bf, size);
+       static const char origin[] = {
+               [DSO_BINARY_TYPE__KALLSYMS]             = 'k',
+               [DSO_BINARY_TYPE__JAVA_JIT]             = 'j',
+               [DSO_BINARY_TYPE__DEBUGLINK]            = 'l',
+               [DSO_BINARY_TYPE__BUILD_ID_CACHE]       = 'B',
+               [DSO_BINARY_TYPE__FEDORA_DEBUGINFO]     = 'f',
+               [DSO_BINARY_TYPE__UBUNTU_DEBUGINFO]     = 'u',
+               [DSO_BINARY_TYPE__BUILDID_DEBUGINFO]    = 'b',
+               [DSO_BINARY_TYPE__SYSTEM_PATH_DSO]      = 'd',
+               [DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE]  = 'K',
+               [DSO_BINARY_TYPE__GUEST_KALLSYMS]       = 'g',
+               [DSO_BINARY_TYPE__GUEST_KMODULE]        = 'G',
+       };
 
-       elf_end(elf);
-out_close:
-       close(fd);
-out:
-       return err;
+       if (dso == NULL || dso->symtab_type == DSO_BINARY_TYPE__NOT_FOUND)
+               return '!';
+       return origin[dso->symtab_type];
 }
 
-int sysfs__read_build_id(const char *filename, void *build_id, size_t size)
+int dso__binary_type_file(struct dso *dso, enum dso_binary_type type,
+                         char *root_dir, char *file, size_t size)
 {
-       int fd, err = -1;
+       char build_id_hex[BUILD_ID_SIZE * 2 + 1];
+       int ret = 0;
 
-       if (size < BUILD_ID_SIZE)
-               goto out;
+       switch (type) {
+       case DSO_BINARY_TYPE__DEBUGLINK: {
+               char *debuglink;
+
+               strncpy(file, dso->long_name, size);
+               debuglink = file + dso->long_name_len;
+               while (debuglink != file && *debuglink != '/')
+                       debuglink--;
+               if (*debuglink == '/')
+                       debuglink++;
+               filename__read_debuglink(dso->long_name, debuglink,
+                                        size - (debuglink - file));
+               }
+               break;
+       case DSO_BINARY_TYPE__BUILD_ID_CACHE:
+               /* skip the locally configured cache if a symfs is given */
+               if (symbol_conf.symfs[0] ||
+                   (dso__build_id_filename(dso, file, size) == NULL))
+                       ret = -1;
+               break;
 
-       fd = open(filename, O_RDONLY);
-       if (fd < 0)
-               goto out;
+       case DSO_BINARY_TYPE__FEDORA_DEBUGINFO:
+               snprintf(file, size, "%s/usr/lib/debug%s.debug",
+                        symbol_conf.symfs, dso->long_name);
+               break;
 
-       while (1) {
-               char bf[BUFSIZ];
-               GElf_Nhdr nhdr;
-               size_t namesz, descsz;
+       case DSO_BINARY_TYPE__UBUNTU_DEBUGINFO:
+               snprintf(file, size, "%s/usr/lib/debug%s",
+                        symbol_conf.symfs, dso->long_name);
+               break;
 
-               if (read(fd, &nhdr, sizeof(nhdr)) != sizeof(nhdr))
+       case DSO_BINARY_TYPE__BUILDID_DEBUGINFO:
+               if (!dso->has_build_id) {
+                       ret = -1;
                        break;
-
-               namesz = NOTE_ALIGN(nhdr.n_namesz);
-               descsz = NOTE_ALIGN(nhdr.n_descsz);
-               if (nhdr.n_type == NT_GNU_BUILD_ID &&
-                   nhdr.n_namesz == sizeof("GNU")) {
-                       if (read(fd, bf, namesz) != (ssize_t)namesz)
-                               break;
-                       if (memcmp(bf, "GNU", sizeof("GNU")) == 0) {
-                               size_t sz = min(descsz, size);
-                               if (read(fd, build_id, sz) == (ssize_t)sz) {
-                                       memset(build_id + sz, 0, size - sz);
-                                       err = 0;
-                                       break;
-                               }
-                       } else if (read(fd, bf, descsz) != (ssize_t)descsz)
-                               break;
-               } else {
-                       int n = namesz + descsz;
-                       if (read(fd, bf, n) != n)
-                               break;
                }
-       }
-       close(fd);
-out:
-       return err;
-}
 
-static int filename__read_debuglink(const char *filename,
-                                   char *debuglink, size_t size)
-{
-       int fd, err = -1;
-       Elf *elf;
-       GElf_Ehdr ehdr;
-       GElf_Shdr shdr;
-       Elf_Data *data;
-       Elf_Scn *sec;
-       Elf_Kind ek;
+               build_id__sprintf(dso->build_id,
+                                 sizeof(dso->build_id),
+                                 build_id_hex);
+               snprintf(file, size,
+                        "%s/usr/lib/debug/.build-id/%.2s/%s.debug",
+                        symbol_conf.symfs, build_id_hex, build_id_hex + 2);
+               break;
 
-       fd = open(filename, O_RDONLY);
-       if (fd < 0)
-               goto out;
+       case DSO_BINARY_TYPE__SYSTEM_PATH_DSO:
+               snprintf(file, size, "%s%s",
+                        symbol_conf.symfs, dso->long_name);
+               break;
 
-       elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
-       if (elf == NULL) {
-               pr_debug2("%s: cannot read %s ELF file.\n", __func__, filename);
-               goto out_close;
-       }
+       case DSO_BINARY_TYPE__GUEST_KMODULE:
+               snprintf(file, size, "%s%s%s", symbol_conf.symfs,
+                        root_dir, dso->long_name);
+               break;
 
-       ek = elf_kind(elf);
-       if (ek != ELF_K_ELF)
-               goto out_close;
+       case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE:
+               snprintf(file, size, "%s%s", symbol_conf.symfs,
+                        dso->long_name);
+               break;
 
-       if (gelf_getehdr(elf, &ehdr) == NULL) {
-               pr_err("%s: cannot get elf header.\n", __func__);
-               goto out_close;
+       default:
+       case DSO_BINARY_TYPE__KALLSYMS:
+       case DSO_BINARY_TYPE__GUEST_KALLSYMS:
+       case DSO_BINARY_TYPE__JAVA_JIT:
+       case DSO_BINARY_TYPE__NOT_FOUND:
+               ret = -1;
+               break;
        }
 
-       sec = elf_section_by_name(elf, &ehdr, &shdr,
-                                 ".gnu_debuglink", NULL);
-       if (sec == NULL)
-               goto out_close;
-
-       data = elf_getdata(sec, NULL);
-       if (data == NULL)
-               goto out_close;
-
-       /* the start of this section is a zero-terminated string */
-       strncpy(debuglink, data->d_buf, size);
-
-       elf_end(elf);
-
-out_close:
-       close(fd);
-out:
-       return err;
-}
-
-char dso__symtab_origin(const struct dso *dso)
-{
-       static const char origin[] = {
-               [SYMTAB__KALLSYMS]            = 'k',
-               [SYMTAB__JAVA_JIT]            = 'j',
-               [SYMTAB__DEBUGLINK]           = 'l',
-               [SYMTAB__BUILD_ID_CACHE]      = 'B',
-               [SYMTAB__FEDORA_DEBUGINFO]    = 'f',
-               [SYMTAB__UBUNTU_DEBUGINFO]    = 'u',
-               [SYMTAB__BUILDID_DEBUGINFO]   = 'b',
-               [SYMTAB__SYSTEM_PATH_DSO]     = 'd',
-               [SYMTAB__SYSTEM_PATH_KMODULE] = 'K',
-               [SYMTAB__GUEST_KALLSYMS]      =  'g',
-               [SYMTAB__GUEST_KMODULE]       =  'G',
-       };
-
-       if (dso == NULL || dso->symtab_type == SYMTAB__NOT_FOUND)
-               return '!';
-       return origin[dso->symtab_type];
+       return ret;
 }
 
 int dso__load(struct dso *dso, struct map *map, symbol_filter_t filter)
 {
-       int size = PATH_MAX;
        char *name;
        int ret = -1;
        int fd;
+       u_int i;
        struct machine *machine;
-       const char *root_dir;
+       char *root_dir = (char *) "";
        int want_symtab;
 
        dso__set_loaded(dso, map->type);
@@ -1700,7 +1040,7 @@ int dso__load(struct dso *dso, struct map *map, symbol_filter_t filter)
        else
                machine = NULL;
 
-       name = malloc(size);
+       name = malloc(PATH_MAX);
        if (!name)
                return -1;
 
@@ -1719,81 +1059,27 @@ int dso__load(struct dso *dso, struct map *map, symbol_filter_t filter)
                }
 
                ret = dso__load_perf_map(dso, map, filter);
-               dso->symtab_type = ret > 0 ? SYMTAB__JAVA_JIT :
-                                             SYMTAB__NOT_FOUND;
+               dso->symtab_type = ret > 0 ? DSO_BINARY_TYPE__JAVA_JIT :
+                                            DSO_BINARY_TYPE__NOT_FOUND;
                return ret;
        }
 
+       if (machine)
+               root_dir = machine->root_dir;
+
        /* Iterate over candidate debug images.
         * On the first pass, only load images if they have a full symtab.
         * Failing that, do a second pass where we accept .dynsym also
         */
        want_symtab = 1;
 restart:
-       for (dso->symtab_type = SYMTAB__DEBUGLINK;
-            dso->symtab_type != SYMTAB__NOT_FOUND;
-            dso->symtab_type++) {
-               switch (dso->symtab_type) {
-               case SYMTAB__DEBUGLINK: {
-                       char *debuglink;
-                       strncpy(name, dso->long_name, size);
-                       debuglink = name + dso->long_name_len;
-                       while (debuglink != name && *debuglink != '/')
-                               debuglink--;
-                       if (*debuglink == '/')
-                               debuglink++;
-                       filename__read_debuglink(dso->long_name, debuglink,
-                                                size - (debuglink - name));
-                       }
-                       break;
-               case SYMTAB__BUILD_ID_CACHE:
-                       /* skip the locally configured cache if a symfs is given */
-                       if (symbol_conf.symfs[0] ||
-                           (dso__build_id_filename(dso, name, size) == NULL)) {
-                               continue;
-                       }
-                       break;
-               case SYMTAB__FEDORA_DEBUGINFO:
-                       snprintf(name, size, "%s/usr/lib/debug%s.debug",
-                                symbol_conf.symfs, dso->long_name);
-                       break;
-               case SYMTAB__UBUNTU_DEBUGINFO:
-                       snprintf(name, size, "%s/usr/lib/debug%s",
-                                symbol_conf.symfs, dso->long_name);
-                       break;
-               case SYMTAB__BUILDID_DEBUGINFO: {
-                       char build_id_hex[BUILD_ID_SIZE * 2 + 1];
+       for (i = 0; i < DSO_BINARY_TYPE__SYMTAB_CNT; i++) {
 
-                       if (!dso->has_build_id)
-                               continue;
+               dso->symtab_type = binary_type_symtab[i];
 
-                       build_id__sprintf(dso->build_id,
-                                         sizeof(dso->build_id),
-                                         build_id_hex);
-                       snprintf(name, size,
-                                "%s/usr/lib/debug/.build-id/%.2s/%s.debug",
-                                symbol_conf.symfs, build_id_hex, build_id_hex + 2);
-                       }
-                       break;
-               case SYMTAB__SYSTEM_PATH_DSO:
-                       snprintf(name, size, "%s%s",
-                            symbol_conf.symfs, dso->long_name);
-                       break;
-               case SYMTAB__GUEST_KMODULE:
-                       if (map->groups && machine)
-                               root_dir = machine->root_dir;
-                       else
-                               root_dir = "";
-                       snprintf(name, size, "%s%s%s", symbol_conf.symfs,
-                                root_dir, dso->long_name);
-                       break;
-
-               case SYMTAB__SYSTEM_PATH_KMODULE:
-                       snprintf(name, size, "%s%s", symbol_conf.symfs,
-                                dso->long_name);
-                       break;
-               default:;
-               }
+               if (dso__binary_type_file(dso, dso->symtab_type,
+                                         root_dir, name, PATH_MAX))
+                       continue;
 
                /* Name is now the name of the next image to try */
                fd = open(name, O_RDONLY);
@@ -1977,25 +1263,6 @@ static int machine__set_modules_path(struct machine *machine)
        return map_groups__set_modules_path_dir(&machine->kmaps, modules_path);
 }
 
-/*
- * Constructor variant for modules (where we know from /proc/modules where
- * they are loaded) and for vmlinux, where only after we load all the
- * symbols we'll know where it starts and ends.
- */
-static struct map *map__new2(u64 start, struct dso *dso, enum map_type type)
-{
-       struct map *map = calloc(1, (sizeof(*map) +
-                                    (dso->kernel ? sizeof(struct kmap) : 0)));
-       if (map != NULL) {
-               /*
-                * ->end will be filled after we load all the symbols
-                */
-               map__init(map, type, start, 0, 0, dso);
-       }
-
-       return map;
-}
-
 struct map *machine__new_module(struct machine *machine, u64 start,
                                const char *filename)
 {
@@ -2010,9 +1277,9 @@ struct map *machine__new_module(struct machine *machine, u64 start,
                return NULL;
 
        if (machine__is_host(machine))
-               dso->symtab_type = SYMTAB__SYSTEM_PATH_KMODULE;
+               dso->symtab_type = DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE;
        else
-               dso->symtab_type = SYMTAB__GUEST_KMODULE;
+               dso->symtab_type = DSO_BINARY_TYPE__GUEST_KMODULE;
        map_groups__insert(&machine->kmaps, map);
        return map;
 }
@@ -2097,13 +1364,14 @@ int dso__load_vmlinux(struct dso *dso, struct map *map,
        if (fd < 0)
                return -1;
 
-       dso__set_long_name(dso, (char *)vmlinux);
-       dso__set_loaded(dso, map->type);
        err = dso__load_sym(dso, map, symfs_vmlinux, fd, filter, 0, 0);
        close(fd);
 
-       if (err > 0)
+       if (err > 0) {
+               dso__set_long_name(dso, (char *)vmlinux);
+               dso__set_loaded(dso, map->type);
                pr_debug("Using %s for symbols\n", symfs_vmlinux);
+       }
 
        return err;
 }
@@ -2120,10 +1388,8 @@ int dso__load_vmlinux_path(struct dso *dso, struct map *map,
        filename = dso__build_id_filename(dso, NULL, 0);
        if (filename != NULL) {
                err = dso__load_vmlinux(dso, map, filename, filter);
-               if (err > 0) {
-                       dso__set_long_name(dso, filename);
+               if (err > 0)
                        goto out;
-               }
                free(filename);
        }
 
@@ -2238,9 +1504,8 @@ do_kallsyms:
        free(kallsyms_allocated_filename);
 
        if (err > 0) {
+               dso__set_long_name(dso, strdup("[kernel.kallsyms]"));
 out_fixup:
-               if (kallsyms_filename != NULL)
-                       dso__set_long_name(dso, strdup("[kernel.kallsyms]"));
                map__fixup_start(map);
                map__fixup_end(map);
        }
@@ -2299,7 +1564,7 @@ out_try_fixup:
        return err;
 }
 
-static void dsos__add(struct list_head *head, struct dso *dso)
+void dsos__add(struct list_head *head, struct dso *dso)
 {
        list_add_tail(&dso->node, head);
 }
@@ -2463,7 +1728,7 @@ struct process_args {
 };
 
 static int symbol__in_kernel(void *arg, const char *name,
-                            char type __used, u64 start, u64 end __used)
+                            char type __used, u64 start)
 {
        struct process_args *args = arg;
 
@@ -2564,8 +1829,15 @@ int machine__create_kernel_maps(struct machine *machine)
            __machine__create_kernel_maps(machine, kernel) < 0)
                return -1;
 
-       if (symbol_conf.use_modules && machine__create_modules(machine) < 0)
-               pr_debug("Problems creating module maps, continuing anyway...\n");
+       if (symbol_conf.use_modules && machine__create_modules(machine) < 0) {
+               if (machine__is_host(machine))
+                       pr_debug("Problems creating module maps, "
+                                "continuing anyway...\n");
+               else
+                       pr_debug("Problems creating module maps for guest %d, "
+                                "continuing anyway...\n", machine->pid);
+       }
+
        /*
         * Now that we have all the maps created, just set the ->end of them:
         */
@@ -2694,7 +1966,8 @@ int symbol__init(void)
 
        symbol_conf.priv_size = ALIGN(symbol_conf.priv_size, sizeof(u64));
 
-       elf_version(EV_CURRENT);
+       symbol__elf_init();
+
        if (symbol_conf.sort_by_name)
                symbol_conf.priv_size += (sizeof(struct symbol_name_rb_node) -
                                          sizeof(struct symbol));
@@ -2815,6 +2088,7 @@ int machines__create_guest_kernel_maps(struct rb_root *machines)
        int i, items = 0;
        char path[PATH_MAX];
        pid_t pid;
+       char *endp;
 
        if (symbol_conf.default_guest_vmlinux_name ||
            symbol_conf.default_guest_modules ||
@@ -2831,7 +2105,14 @@ int machines__create_guest_kernel_maps(struct rb_root *machines)
                                /* Filter out . and .. */
                                continue;
                        }
-                       pid = atoi(namelist[i]->d_name);
+                       pid = (pid_t)strtol(namelist[i]->d_name, &endp, 10);
+                       if ((*endp != '\0') ||
+                           (endp == namelist[i]->d_name) ||
+                           (errno == ERANGE)) {
+                               pr_debug("invalid directory (%s). Skipping.\n",
+                                        namelist[i]->d_name);
+                               continue;
+                       }
                        sprintf(path, "%s/%s/proc/kallsyms",
                                symbol_conf.guestmount,
                                namelist[i]->d_name);
@@ -2905,3 +2186,218 @@ struct map *dso__new_map(const char *name)
 
        return map;
 }
+
+static int open_dso(struct dso *dso, struct machine *machine)
+{
+       char *root_dir = (char *) "";
+       char *name;
+       int fd;
+
+       name = malloc(PATH_MAX);
+       if (!name)
+               return -ENOMEM;
+
+       if (machine)
+               root_dir = machine->root_dir;
+
+       if (dso__binary_type_file(dso, dso->data_type,
+                                 root_dir, name, PATH_MAX)) {
+               free(name);
+               return -EINVAL;
+       }
+
+       fd = open(name, O_RDONLY);
+       free(name);
+       return fd;
+}
+
+int dso__data_fd(struct dso *dso, struct machine *machine)
+{
+       int i = 0;
+
+       if (dso->data_type != DSO_BINARY_TYPE__NOT_FOUND)
+               return open_dso(dso, machine);
+
+       do {
+               int fd;
+
+               dso->data_type = binary_type_data[i++];
+
+               fd = open_dso(dso, machine);
+               if (fd >= 0)
+                       return fd;
+
+       } while (dso->data_type != DSO_BINARY_TYPE__NOT_FOUND);
+
+       return -EINVAL;
+}
+
+static void
+dso_cache__free(struct rb_root *root)
+{
+       struct rb_node *next = rb_first(root);
+
+       while (next) {
+               struct dso_cache *cache;
+
+               cache = rb_entry(next, struct dso_cache, rb_node);
+               next = rb_next(&cache->rb_node);
+               rb_erase(&cache->rb_node, root);
+               free(cache);
+       }
+}
+
+static struct dso_cache*
+dso_cache__find(struct rb_root *root, u64 offset)
+{
+       struct rb_node **p = &root->rb_node;
+       struct rb_node *parent = NULL;
+       struct dso_cache *cache;
+
+       while (*p != NULL) {
+               u64 end;
+
+               parent = *p;
+               cache = rb_entry(parent, struct dso_cache, rb_node);
+               end = cache->offset + DSO__DATA_CACHE_SIZE;
+
+               if (offset < cache->offset)
+                       p = &(*p)->rb_left;
+               else if (offset >= end)
+                       p = &(*p)->rb_right;
+               else
+                       return cache;
+       }
+       return NULL;
+}
+
+static void
+dso_cache__insert(struct rb_root *root, struct dso_cache *new)
+{
+       struct rb_node **p = &root->rb_node;
+       struct rb_node *parent = NULL;
+       struct dso_cache *cache;
+       u64 offset = new->offset;
+
+       while (*p != NULL) {
+               u64 end;
+
+               parent = *p;
+               cache = rb_entry(parent, struct dso_cache, rb_node);
+               end = cache->offset + DSO__DATA_CACHE_SIZE;
+
+               if (offset < cache->offset)
+                       p = &(*p)->rb_left;
+               else if (offset >= end)
+                       p = &(*p)->rb_right;
+       }
+
+       rb_link_node(&new->rb_node, parent, p);
+       rb_insert_color(&new->rb_node, root);
+}
+
+static ssize_t
+dso_cache__memcpy(struct dso_cache *cache, u64 offset,
+                 u8 *data, u64 size)
+{
+       u64 cache_offset = offset - cache->offset;
+       u64 cache_size   = min(cache->size - cache_offset, size);
+
+       memcpy(data, cache->data + cache_offset, cache_size);
+       return cache_size;
+}
+
+static ssize_t
+dso_cache__read(struct dso *dso, struct machine *machine,
+                u64 offset, u8 *data, ssize_t size)
+{
+       struct dso_cache *cache;
+       ssize_t ret;
+       int fd;
+
+       fd = dso__data_fd(dso, machine);
+       if (fd < 0)
+               return -1;
+
+       do {
+               u64 cache_offset;
+
+               ret = -ENOMEM;
+
+               cache = zalloc(sizeof(*cache) + DSO__DATA_CACHE_SIZE);
+               if (!cache)
+                       break;
+
+               cache_offset = offset & DSO__DATA_CACHE_MASK;
+               ret = -EINVAL;
+
+               if (-1 == lseek(fd, cache_offset, SEEK_SET))
+                       break;
+
+               ret = read(fd, cache->data, DSO__DATA_CACHE_SIZE);
+               if (ret <= 0)
+                       break;
+
+               cache->offset = cache_offset;
+               cache->size   = ret;
+               dso_cache__insert(&dso->cache, cache);
+
+               ret = dso_cache__memcpy(cache, offset, data, size);
+
+       } while (0);
+
+       if (ret <= 0)
+               free(cache);
+
+       close(fd);
+       return ret;
+}
+
+static ssize_t dso_cache_read(struct dso *dso, struct machine *machine,
+                             u64 offset, u8 *data, ssize_t size)
+{
+       struct dso_cache *cache;
+
+       cache = dso_cache__find(&dso->cache, offset);
+       if (cache)
+               return dso_cache__memcpy(cache, offset, data, size);
+       else
+               return dso_cache__read(dso, machine, offset, data, size);
+}
+
+ssize_t dso__data_read_offset(struct dso *dso, struct machine *machine,
+                             u64 offset, u8 *data, ssize_t size)
+{
+       ssize_t r = 0;
+       u8 *p = data;
+
+       do {
+               ssize_t ret;
+
+               ret = dso_cache_read(dso, machine, offset, p, size);
+               if (ret < 0)
+                       return ret;
+
+               /* Reached EOF, return what we have. */
+               if (!ret)
+                       break;
+
+               BUG_ON(ret > size);
+
+               r      += ret;
+               p      += ret;
+               offset += ret;
+               size   -= ret;
+
+       } while (size);
+
+       return r;
+}
+
+ssize_t dso__data_read_addr(struct dso *dso, struct map *map,
+                           struct machine *machine, u64 addr,
+                           u8 *data, ssize_t size)
+{
+       u64 offset = map->map_ip(map, addr);
+       return dso__data_read_offset(dso, machine, offset, data, size);
+}