11 #include <symbol/kallsyms.h>
14 #ifdef HAVE_CPLUS_DEMANGLE_SUPPORT
15 extern char *cplus_demangle(const char *, int);
17 static inline char *bfd_demangle(void __maybe_unused *v, const char *c, int i)
19 return cplus_demangle(c, i);
23 static inline char *bfd_demangle(void __maybe_unused *v,
24 const char __maybe_unused *c,
30 #define PACKAGE 'perf'
35 #ifndef HAVE_ELF_GETPHDRNUM_SUPPORT
36 static int elf_getphdrnum(Elf *elf, size_t *dst)
41 ehdr = gelf_getehdr(elf, &gehdr);
51 #ifndef NT_GNU_BUILD_ID
52 #define NT_GNU_BUILD_ID 3
56 * elf_symtab__for_each_symbol - iterate thru all the symbols
58 * @syms: struct elf_symtab instance to iterate
60 * @sym: GElf_Sym iterator
62 #define elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) \
63 for (idx = 0, gelf_getsym(syms, idx, &sym);\
65 idx++, gelf_getsym(syms, idx, &sym))
67 static inline uint8_t elf_sym__type(const GElf_Sym *sym)
69 return GELF_ST_TYPE(sym->st_info);
72 static inline int elf_sym__is_function(const GElf_Sym *sym)
74 return (elf_sym__type(sym) == STT_FUNC ||
75 elf_sym__type(sym) == STT_GNU_IFUNC) &&
77 sym->st_shndx != SHN_UNDEF;
80 static inline bool elf_sym__is_object(const GElf_Sym *sym)
82 return elf_sym__type(sym) == STT_OBJECT &&
84 sym->st_shndx != SHN_UNDEF;
87 static inline int elf_sym__is_label(const GElf_Sym *sym)
89 return elf_sym__type(sym) == STT_NOTYPE &&
91 sym->st_shndx != SHN_UNDEF &&
92 sym->st_shndx != SHN_ABS;
95 static bool elf_sym__is_a(GElf_Sym *sym, enum map_type type)
99 return elf_sym__is_function(sym);
101 return elf_sym__is_object(sym);
107 static inline const char *elf_sym__name(const GElf_Sym *sym,
108 const Elf_Data *symstrs)
110 return symstrs->d_buf + sym->st_name;
113 static inline const char *elf_sec__name(const GElf_Shdr *shdr,
114 const Elf_Data *secstrs)
116 return secstrs->d_buf + shdr->sh_name;
119 static inline int elf_sec__is_text(const GElf_Shdr *shdr,
120 const Elf_Data *secstrs)
122 return strstr(elf_sec__name(shdr, secstrs), "text") != NULL;
125 static inline bool elf_sec__is_data(const GElf_Shdr *shdr,
126 const Elf_Data *secstrs)
128 return strstr(elf_sec__name(shdr, secstrs), "data") != NULL;
131 static bool elf_sec__is_a(GElf_Shdr *shdr, Elf_Data *secstrs,
136 return elf_sec__is_text(shdr, secstrs);
138 return elf_sec__is_data(shdr, secstrs);
144 static size_t elf_addr_to_index(Elf *elf, GElf_Addr addr)
150 while ((sec = elf_nextscn(elf, sec)) != NULL) {
151 gelf_getshdr(sec, &shdr);
153 if ((addr >= shdr.sh_addr) &&
154 (addr < (shdr.sh_addr + shdr.sh_size)))
163 Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep,
164 GElf_Shdr *shp, const char *name, size_t *idx)
169 /* Elf is corrupted/truncated, avoid calling elf_strptr. */
170 if (!elf_rawdata(elf_getscn(elf, ep->e_shstrndx), NULL))
173 while ((sec = elf_nextscn(elf, sec)) != NULL) {
176 gelf_getshdr(sec, shp);
177 str = elf_strptr(elf, ep->e_shstrndx, shp->sh_name);
178 if (str && !strcmp(name, str)) {
189 #define elf_section__for_each_rel(reldata, pos, pos_mem, idx, nr_entries) \
190 for (idx = 0, pos = gelf_getrel(reldata, 0, &pos_mem); \
192 ++idx, pos = gelf_getrel(reldata, idx, &pos_mem))
194 #define elf_section__for_each_rela(reldata, pos, pos_mem, idx, nr_entries) \
195 for (idx = 0, pos = gelf_getrela(reldata, 0, &pos_mem); \
197 ++idx, pos = gelf_getrela(reldata, idx, &pos_mem))
200 * We need to check if we have a .dynsym, so that we can handle the
201 * .plt, synthesizing its symbols, that aren't on the symtabs (be it
202 * .dynsym or .symtab).
203 * And always look at the original dso, not at debuginfo packages, that
204 * have the PLT data stripped out (shdr_rel_plt.sh_type == SHT_NOBITS).
206 int dso__synthesize_plt_symbols(struct dso *dso, struct symsrc *ss, struct map *map,
207 symbol_filter_t filter)
209 uint32_t nr_rel_entries, idx;
214 GElf_Shdr shdr_rel_plt, shdr_dynsym;
215 Elf_Data *reldata, *syms, *symstrs;
216 Elf_Scn *scn_plt_rel, *scn_symstrs, *scn_dynsym;
219 char sympltname[1024];
221 int nr = 0, symidx, err = 0;
229 scn_dynsym = ss->dynsym;
230 shdr_dynsym = ss->dynshdr;
231 dynsym_idx = ss->dynsym_idx;
233 if (scn_dynsym == NULL)
236 scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt,
238 if (scn_plt_rel == NULL) {
239 scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt,
241 if (scn_plt_rel == NULL)
247 if (shdr_rel_plt.sh_link != dynsym_idx)
250 if (elf_section_by_name(elf, &ehdr, &shdr_plt, ".plt", NULL) == NULL)
254 * Fetch the relocation section to find the idxes to the GOT
255 * and the symbols in the .dynsym they refer to.
257 reldata = elf_getdata(scn_plt_rel, NULL);
261 syms = elf_getdata(scn_dynsym, NULL);
265 scn_symstrs = elf_getscn(elf, shdr_dynsym.sh_link);
266 if (scn_symstrs == NULL)
269 symstrs = elf_getdata(scn_symstrs, NULL);
273 if (symstrs->d_size == 0)
276 nr_rel_entries = shdr_rel_plt.sh_size / shdr_rel_plt.sh_entsize;
277 plt_offset = shdr_plt.sh_offset;
279 if (shdr_rel_plt.sh_type == SHT_RELA) {
280 GElf_Rela pos_mem, *pos;
282 elf_section__for_each_rela(reldata, pos, pos_mem, idx,
284 symidx = GELF_R_SYM(pos->r_info);
285 plt_offset += shdr_plt.sh_entsize;
286 gelf_getsym(syms, symidx, &sym);
287 snprintf(sympltname, sizeof(sympltname),
288 "%s@plt", elf_sym__name(&sym, symstrs));
290 f = symbol__new(plt_offset, shdr_plt.sh_entsize,
291 STB_GLOBAL, sympltname);
295 if (filter && filter(map, f))
298 symbols__insert(&dso->symbols[map->type], f);
302 } else if (shdr_rel_plt.sh_type == SHT_REL) {
303 GElf_Rel pos_mem, *pos;
304 elf_section__for_each_rel(reldata, pos, pos_mem, idx,
306 symidx = GELF_R_SYM(pos->r_info);
307 plt_offset += shdr_plt.sh_entsize;
308 gelf_getsym(syms, symidx, &sym);
309 snprintf(sympltname, sizeof(sympltname),
310 "%s@plt", elf_sym__name(&sym, symstrs));
312 f = symbol__new(plt_offset, shdr_plt.sh_entsize,
313 STB_GLOBAL, sympltname);
317 if (filter && filter(map, f))
320 symbols__insert(&dso->symbols[map->type], f);
330 pr_debug("%s: problems reading %s PLT info.\n",
331 __func__, dso->long_name);
336 * Align offset to 4 bytes as needed for note name and descriptor data.
338 #define NOTE_ALIGN(n) (((n) + 3) & -4U)
340 static int elf_read_build_id(Elf *elf, void *bf, size_t size)
350 if (size < BUILD_ID_SIZE)
357 if (gelf_getehdr(elf, &ehdr) == NULL) {
358 pr_err("%s: cannot get elf header.\n", __func__);
363 * Check following sections for notes:
364 * '.note.gnu.build-id'
366 * '.note' (VDSO specific)
369 sec = elf_section_by_name(elf, &ehdr, &shdr,
370 ".note.gnu.build-id", NULL);
374 sec = elf_section_by_name(elf, &ehdr, &shdr,
379 sec = elf_section_by_name(elf, &ehdr, &shdr,
388 data = elf_getdata(sec, NULL);
393 while (ptr < (data->d_buf + data->d_size)) {
394 GElf_Nhdr *nhdr = ptr;
395 size_t namesz = NOTE_ALIGN(nhdr->n_namesz),
396 descsz = NOTE_ALIGN(nhdr->n_descsz);
399 ptr += sizeof(*nhdr);
402 if (nhdr->n_type == NT_GNU_BUILD_ID &&
403 nhdr->n_namesz == sizeof("GNU")) {
404 if (memcmp(name, "GNU", sizeof("GNU")) == 0) {
405 size_t sz = min(size, descsz);
407 memset(bf + sz, 0, size - sz);
419 int filename__read_build_id(const char *filename, void *bf, size_t size)
424 if (size < BUILD_ID_SIZE)
427 fd = open(filename, O_RDONLY);
431 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
433 pr_debug2("%s: cannot read %s ELF file.\n", __func__, filename);
437 err = elf_read_build_id(elf, bf, size);
446 int sysfs__read_build_id(const char *filename, void *build_id, size_t size)
450 if (size < BUILD_ID_SIZE)
453 fd = open(filename, O_RDONLY);
460 size_t namesz, descsz;
462 if (read(fd, &nhdr, sizeof(nhdr)) != sizeof(nhdr))
465 namesz = NOTE_ALIGN(nhdr.n_namesz);
466 descsz = NOTE_ALIGN(nhdr.n_descsz);
467 if (nhdr.n_type == NT_GNU_BUILD_ID &&
468 nhdr.n_namesz == sizeof("GNU")) {
469 if (read(fd, bf, namesz) != (ssize_t)namesz)
471 if (memcmp(bf, "GNU", sizeof("GNU")) == 0) {
472 size_t sz = min(descsz, size);
473 if (read(fd, build_id, sz) == (ssize_t)sz) {
474 memset(build_id + sz, 0, size - sz);
478 } else if (read(fd, bf, descsz) != (ssize_t)descsz)
481 int n = namesz + descsz;
482 if (read(fd, bf, n) != n)
491 int filename__read_debuglink(const char *filename, char *debuglink,
502 fd = open(filename, O_RDONLY);
506 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
508 pr_debug2("%s: cannot read %s ELF file.\n", __func__, filename);
516 if (gelf_getehdr(elf, &ehdr) == NULL) {
517 pr_err("%s: cannot get elf header.\n", __func__);
521 sec = elf_section_by_name(elf, &ehdr, &shdr,
522 ".gnu_debuglink", NULL);
526 data = elf_getdata(sec, NULL);
530 /* the start of this section is a zero-terminated string */
531 strncpy(debuglink, data->d_buf, size);
543 static int dso__swap_init(struct dso *dso, unsigned char eidata)
545 static unsigned int const endian = 1;
547 dso->needs_swap = DSO_SWAP__NO;
551 /* We are big endian, DSO is little endian. */
552 if (*(unsigned char const *)&endian != 1)
553 dso->needs_swap = DSO_SWAP__YES;
557 /* We are little endian, DSO is big endian. */
558 if (*(unsigned char const *)&endian != 0)
559 dso->needs_swap = DSO_SWAP__YES;
563 pr_err("unrecognized DSO data encoding %d\n", eidata);
570 static int decompress_kmodule(struct dso *dso, const char *name,
571 enum dso_binary_type type)
574 const char *ext = strrchr(name, '.');
575 char tmpbuf[] = "/tmp/perf-kmod-XXXXXX";
577 if (type != DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP &&
578 type != DSO_BINARY_TYPE__GUEST_KMODULE_COMP &&
579 type != DSO_BINARY_TYPE__BUILD_ID_CACHE)
582 if (!ext || !is_supported_compression(ext + 1)) {
583 ext = strrchr(dso->name, '.');
584 if (!ext || !is_supported_compression(ext + 1))
588 fd = mkstemp(tmpbuf);
592 if (!decompress_to_file(ext + 1, name, fd)) {
602 bool symsrc__possibly_runtime(struct symsrc *ss)
604 return ss->dynsym || ss->opdsec;
607 bool symsrc__has_symtab(struct symsrc *ss)
609 return ss->symtab != NULL;
612 void symsrc__destroy(struct symsrc *ss)
619 int symsrc__init(struct symsrc *ss, struct dso *dso, const char *name,
620 enum dso_binary_type type)
627 if (dso__needs_decompress(dso))
628 fd = decompress_kmodule(dso, name, type);
630 fd = open(name, O_RDONLY);
635 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
637 pr_debug("%s: cannot read %s ELF file.\n", __func__, name);
641 if (gelf_getehdr(elf, &ehdr) == NULL) {
642 pr_debug("%s: cannot get elf header.\n", __func__);
646 if (dso__swap_init(dso, ehdr.e_ident[EI_DATA]))
649 /* Always reject images with a mismatched build-id: */
650 if (dso->has_build_id) {
651 u8 build_id[BUILD_ID_SIZE];
653 if (elf_read_build_id(elf, build_id, BUILD_ID_SIZE) < 0)
656 if (!dso__build_id_equal(dso, build_id))
660 ss->is_64_bit = (gelf_getclass(elf) == ELFCLASS64);
662 ss->symtab = elf_section_by_name(elf, &ehdr, &ss->symshdr, ".symtab",
664 if (ss->symshdr.sh_type != SHT_SYMTAB)
668 ss->dynsym = elf_section_by_name(elf, &ehdr, &ss->dynshdr, ".dynsym",
670 if (ss->dynshdr.sh_type != SHT_DYNSYM)
674 ss->opdsec = elf_section_by_name(elf, &ehdr, &ss->opdshdr, ".opd",
676 if (ss->opdshdr.sh_type != SHT_PROGBITS)
679 if (dso->kernel == DSO_TYPE_USER) {
681 ss->adjust_symbols = (ehdr.e_type == ET_EXEC ||
682 ehdr.e_type == ET_REL ||
684 elf_section_by_name(elf, &ehdr, &shdr,
688 ss->adjust_symbols = ehdr.e_type == ET_EXEC ||
689 ehdr.e_type == ET_REL;
692 ss->name = strdup(name);
711 * ref_reloc_sym_not_found - has kernel relocation symbol been found.
712 * @kmap: kernel maps and relocation reference symbol
714 * This function returns %true if we are dealing with the kernel maps and the
715 * relocation reference symbol has not yet been found. Otherwise %false is
718 static bool ref_reloc_sym_not_found(struct kmap *kmap)
720 return kmap && kmap->ref_reloc_sym && kmap->ref_reloc_sym->name &&
721 !kmap->ref_reloc_sym->unrelocated_addr;
725 * ref_reloc - kernel relocation offset.
726 * @kmap: kernel maps and relocation reference symbol
728 * This function returns the offset of kernel addresses as determined by using
729 * the relocation reference symbol i.e. if the kernel has not been relocated
730 * then the return value is zero.
732 static u64 ref_reloc(struct kmap *kmap)
734 if (kmap && kmap->ref_reloc_sym &&
735 kmap->ref_reloc_sym->unrelocated_addr)
736 return kmap->ref_reloc_sym->addr -
737 kmap->ref_reloc_sym->unrelocated_addr;
741 static bool want_demangle(bool is_kernel_sym)
743 return is_kernel_sym ? symbol_conf.demangle_kernel : symbol_conf.demangle;
746 int dso__load_sym(struct dso *dso, struct map *map,
747 struct symsrc *syms_ss, struct symsrc *runtime_ss,
748 symbol_filter_t filter, int kmodule)
750 struct kmap *kmap = dso->kernel ? map__kmap(map) : NULL;
751 struct map *curr_map = map;
752 struct dso *curr_dso = dso;
753 Elf_Data *symstrs, *secstrs;
759 Elf_Data *syms, *opddata = NULL;
761 Elf_Scn *sec, *sec_strndx;
764 bool remap_kernel = false, adjust_kernel_syms = false;
766 dso->symtab_type = syms_ss->type;
767 dso->is_64_bit = syms_ss->is_64_bit;
768 dso->rel = syms_ss->ehdr.e_type == ET_REL;
771 * Modules may already have symbols from kallsyms, but those symbols
772 * have the wrong values for the dso maps, so remove them.
774 if (kmodule && syms_ss->symtab)
775 symbols__delete(&dso->symbols[map->type]);
777 if (!syms_ss->symtab) {
779 * If the vmlinux is stripped, fail so we will fall back
780 * to using kallsyms. The vmlinux runtime symbols aren't
786 syms_ss->symtab = syms_ss->dynsym;
787 syms_ss->symshdr = syms_ss->dynshdr;
791 ehdr = syms_ss->ehdr;
792 sec = syms_ss->symtab;
793 shdr = syms_ss->symshdr;
795 if (runtime_ss->opdsec)
796 opddata = elf_rawdata(runtime_ss->opdsec, NULL);
798 syms = elf_getdata(sec, NULL);
802 sec = elf_getscn(elf, shdr.sh_link);
806 symstrs = elf_getdata(sec, NULL);
810 sec_strndx = elf_getscn(runtime_ss->elf, runtime_ss->ehdr.e_shstrndx);
811 if (sec_strndx == NULL)
814 secstrs = elf_getdata(sec_strndx, NULL);
818 nr_syms = shdr.sh_size / shdr.sh_entsize;
820 memset(&sym, 0, sizeof(sym));
823 * The kernel relocation symbol is needed in advance in order to adjust
824 * kernel maps correctly.
826 if (ref_reloc_sym_not_found(kmap)) {
827 elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) {
828 const char *elf_name = elf_sym__name(&sym, symstrs);
830 if (strcmp(elf_name, kmap->ref_reloc_sym->name))
832 kmap->ref_reloc_sym->unrelocated_addr = sym.st_value;
833 map->reloc = kmap->ref_reloc_sym->addr -
834 kmap->ref_reloc_sym->unrelocated_addr;
839 dso->adjust_symbols = runtime_ss->adjust_symbols || ref_reloc(kmap);
841 * Initial kernel and module mappings do not map to the dso. For
842 * function mappings, flag the fixups.
844 if (map->type == MAP__FUNCTION && (dso->kernel || kmodule)) {
846 adjust_kernel_syms = dso->adjust_symbols;
848 elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) {
850 const char *elf_name = elf_sym__name(&sym, symstrs);
851 char *demangled = NULL;
852 int is_label = elf_sym__is_label(&sym);
853 const char *section_name;
854 bool used_opd = false;
856 if (!is_label && !elf_sym__is_a(&sym, map->type))
859 /* Reject ARM ELF "mapping symbols": these aren't unique and
860 * don't identify functions, so will confuse the profile
862 if (ehdr.e_machine == EM_ARM || ehdr.e_machine == EM_AARCH64) {
863 if (elf_name[0] == '$' && strchr("adtx", elf_name[1])
864 && (elf_name[2] == '\0' || elf_name[2] == '.'))
868 if (runtime_ss->opdsec && sym.st_shndx == runtime_ss->opdidx) {
869 u32 offset = sym.st_value - syms_ss->opdshdr.sh_addr;
870 u64 *opd = opddata->d_buf + offset;
871 sym.st_value = DSO__SWAP(dso, u64, *opd);
872 sym.st_shndx = elf_addr_to_index(runtime_ss->elf,
877 * When loading symbols in a data mapping, ABS symbols (which
878 * has a value of SHN_ABS in its st_shndx) failed at
879 * elf_getscn(). And it marks the loading as a failure so
880 * already loaded symbols cannot be fixed up.
882 * I'm not sure what should be done. Just ignore them for now.
885 if (sym.st_shndx == SHN_ABS)
888 sec = elf_getscn(runtime_ss->elf, sym.st_shndx);
892 gelf_getshdr(sec, &shdr);
894 if (is_label && !elf_sec__is_a(&shdr, secstrs, map->type))
897 section_name = elf_sec__name(&shdr, secstrs);
899 /* On ARM, symbols for thumb functions have 1 added to
900 * the symbol address as a flag - remove it */
901 if ((ehdr.e_machine == EM_ARM) &&
902 (map->type == MAP__FUNCTION) &&
906 if (dso->kernel || kmodule) {
907 char dso_name[PATH_MAX];
909 /* Adjust symbol to map to file offset */
910 if (adjust_kernel_syms)
911 sym.st_value -= shdr.sh_addr - shdr.sh_offset;
913 if (strcmp(section_name,
914 (curr_dso->short_name +
915 dso->short_name_len)) == 0)
918 if (strcmp(section_name, ".text") == 0) {
920 * The initial kernel mapping is based on
921 * kallsyms and identity maps. Overwrite it to
922 * map to the kernel dso.
924 if (remap_kernel && dso->kernel) {
925 remap_kernel = false;
926 map->start = shdr.sh_addr +
928 map->end = map->start + shdr.sh_size;
929 map->pgoff = shdr.sh_offset;
930 map->map_ip = map__map_ip;
931 map->unmap_ip = map__unmap_ip;
932 /* Ensure maps are correctly ordered */
933 map_groups__remove(kmap->kmaps, map);
934 map_groups__insert(kmap->kmaps, map);
938 * The initial module mapping is based on
939 * /proc/modules mapped to offset zero.
940 * Overwrite it to map to the module dso.
942 if (remap_kernel && kmodule) {
943 remap_kernel = false;
944 map->pgoff = shdr.sh_offset;
955 snprintf(dso_name, sizeof(dso_name),
956 "%s%s", dso->short_name, section_name);
958 curr_map = map_groups__find_by_name(kmap->kmaps, map->type, dso_name);
959 if (curr_map == NULL) {
960 u64 start = sym.st_value;
963 start += map->start + shdr.sh_offset;
965 curr_dso = dso__new(dso_name);
966 if (curr_dso == NULL)
968 curr_dso->kernel = dso->kernel;
969 curr_dso->long_name = dso->long_name;
970 curr_dso->long_name_len = dso->long_name_len;
971 curr_map = map__new2(start, curr_dso,
973 if (curr_map == NULL) {
974 dso__delete(curr_dso);
977 if (adjust_kernel_syms) {
978 curr_map->start = shdr.sh_addr +
980 curr_map->end = curr_map->start +
982 curr_map->pgoff = shdr.sh_offset;
984 curr_map->map_ip = identity__map_ip;
985 curr_map->unmap_ip = identity__map_ip;
987 curr_dso->symtab_type = dso->symtab_type;
988 map_groups__insert(kmap->kmaps, curr_map);
990 * The new DSO should go to the kernel DSOS
992 dsos__add(&map->groups->machine->kernel_dsos,
994 dso__set_loaded(curr_dso, map->type);
996 curr_dso = curr_map->dso;
1001 if ((used_opd && runtime_ss->adjust_symbols)
1002 || (!used_opd && syms_ss->adjust_symbols)) {
1003 pr_debug4("%s: adjusting symbol: st_value: %#" PRIx64 " "
1004 "sh_addr: %#" PRIx64 " sh_offset: %#" PRIx64 "\n", __func__,
1005 (u64)sym.st_value, (u64)shdr.sh_addr,
1006 (u64)shdr.sh_offset);
1007 sym.st_value -= shdr.sh_addr - shdr.sh_offset;
1011 * We need to figure out if the object was created from C++ sources
1012 * DWARF DW_compile_unit has this, but we don't always have access
1015 if (want_demangle(dso->kernel || kmodule)) {
1016 int demangle_flags = DMGL_NO_OPTS;
1018 demangle_flags = DMGL_PARAMS | DMGL_ANSI;
1020 demangled = bfd_demangle(NULL, elf_name, demangle_flags);
1021 if (demangled != NULL)
1022 elf_name = demangled;
1024 f = symbol__new(sym.st_value, sym.st_size,
1025 GELF_ST_BIND(sym.st_info), elf_name);
1030 if (filter && filter(curr_map, f))
1033 symbols__insert(&curr_dso->symbols[curr_map->type], f);
1039 * For misannotated, zeroed, ASM function sizes.
1042 symbols__fixup_duplicate(&dso->symbols[map->type]);
1043 symbols__fixup_end(&dso->symbols[map->type]);
1046 * We need to fixup this here too because we create new
1047 * maps here, for things like vsyscall sections.
1049 __map_groups__fixup_end(kmap->kmaps, map->type);
1057 static int elf_read_maps(Elf *elf, bool exe, mapfn_t mapfn, void *data)
1064 if (elf_getphdrnum(elf, &phdrnum))
1067 for (i = 0; i < phdrnum; i++) {
1068 if (gelf_getphdr(elf, i, &phdr) == NULL)
1070 if (phdr.p_type != PT_LOAD)
1073 if (!(phdr.p_flags & PF_X))
1076 if (!(phdr.p_flags & PF_R))
1079 sz = min(phdr.p_memsz, phdr.p_filesz);
1082 err = mapfn(phdr.p_vaddr, sz, phdr.p_offset, data);
1089 int file__read_maps(int fd, bool exe, mapfn_t mapfn, void *data,
1095 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
1100 *is_64_bit = (gelf_getclass(elf) == ELFCLASS64);
1102 err = elf_read_maps(elf, exe, mapfn, data);
1108 enum dso_type dso__type_fd(int fd)
1110 enum dso_type dso_type = DSO__TYPE_UNKNOWN;
1115 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
1120 if (ek != ELF_K_ELF)
1123 if (gelf_getclass(elf) == ELFCLASS64) {
1124 dso_type = DSO__TYPE_64BIT;
1128 if (gelf_getehdr(elf, &ehdr) == NULL)
1131 if (ehdr.e_machine == EM_X86_64)
1132 dso_type = DSO__TYPE_X32BIT;
1134 dso_type = DSO__TYPE_32BIT;
1141 static int copy_bytes(int from, off_t from_offs, int to, off_t to_offs, u64 len)
1146 char *buf = malloc(page_size);
1151 if (lseek(to, to_offs, SEEK_SET) != to_offs)
1154 if (lseek(from, from_offs, SEEK_SET) != from_offs)
1161 /* Use read because mmap won't work on proc files */
1162 r = read(from, buf, n);
1168 r = write(to, buf, n);
1189 static int kcore__open(struct kcore *kcore, const char *filename)
1193 kcore->fd = open(filename, O_RDONLY);
1194 if (kcore->fd == -1)
1197 kcore->elf = elf_begin(kcore->fd, ELF_C_READ, NULL);
1201 kcore->elfclass = gelf_getclass(kcore->elf);
1202 if (kcore->elfclass == ELFCLASSNONE)
1205 ehdr = gelf_getehdr(kcore->elf, &kcore->ehdr);
1212 elf_end(kcore->elf);
1218 static int kcore__init(struct kcore *kcore, char *filename, int elfclass,
1223 kcore->elfclass = elfclass;
1226 kcore->fd = mkstemp(filename);
1228 kcore->fd = open(filename, O_WRONLY | O_CREAT | O_EXCL, 0400);
1229 if (kcore->fd == -1)
1232 kcore->elf = elf_begin(kcore->fd, ELF_C_WRITE, NULL);
1236 if (!gelf_newehdr(kcore->elf, elfclass))
1239 ehdr = gelf_getehdr(kcore->elf, &kcore->ehdr);
1246 elf_end(kcore->elf);
1253 static void kcore__close(struct kcore *kcore)
1255 elf_end(kcore->elf);
1259 static int kcore__copy_hdr(struct kcore *from, struct kcore *to, size_t count)
1261 GElf_Ehdr *ehdr = &to->ehdr;
1262 GElf_Ehdr *kehdr = &from->ehdr;
1264 memcpy(ehdr->e_ident, kehdr->e_ident, EI_NIDENT);
1265 ehdr->e_type = kehdr->e_type;
1266 ehdr->e_machine = kehdr->e_machine;
1267 ehdr->e_version = kehdr->e_version;
1270 ehdr->e_flags = kehdr->e_flags;
1271 ehdr->e_phnum = count;
1272 ehdr->e_shentsize = 0;
1274 ehdr->e_shstrndx = 0;
1276 if (from->elfclass == ELFCLASS32) {
1277 ehdr->e_phoff = sizeof(Elf32_Ehdr);
1278 ehdr->e_ehsize = sizeof(Elf32_Ehdr);
1279 ehdr->e_phentsize = sizeof(Elf32_Phdr);
1281 ehdr->e_phoff = sizeof(Elf64_Ehdr);
1282 ehdr->e_ehsize = sizeof(Elf64_Ehdr);
1283 ehdr->e_phentsize = sizeof(Elf64_Phdr);
1286 if (!gelf_update_ehdr(to->elf, ehdr))
1289 if (!gelf_newphdr(to->elf, count))
1295 static int kcore__add_phdr(struct kcore *kcore, int idx, off_t offset,
1301 phdr = gelf_getphdr(kcore->elf, idx, &gphdr);
1305 phdr->p_type = PT_LOAD;
1306 phdr->p_flags = PF_R | PF_W | PF_X;
1307 phdr->p_offset = offset;
1308 phdr->p_vaddr = addr;
1310 phdr->p_filesz = len;
1311 phdr->p_memsz = len;
1312 phdr->p_align = page_size;
1314 if (!gelf_update_phdr(kcore->elf, idx, phdr))
1320 static off_t kcore__write(struct kcore *kcore)
1322 return elf_update(kcore->elf, ELF_C_WRITE);
1331 struct kcore_copy_info {
1337 u64 last_module_symbol;
1338 struct phdr_data kernel_map;
1339 struct phdr_data modules_map;
1342 static int kcore_copy__process_kallsyms(void *arg, const char *name, char type,
1345 struct kcore_copy_info *kci = arg;
1347 if (!symbol_type__is_a(type, MAP__FUNCTION))
1350 if (strchr(name, '[')) {
1351 if (start > kci->last_module_symbol)
1352 kci->last_module_symbol = start;
1356 if (!kci->first_symbol || start < kci->first_symbol)
1357 kci->first_symbol = start;
1359 if (!kci->last_symbol || start > kci->last_symbol)
1360 kci->last_symbol = start;
1362 if (!strcmp(name, "_stext")) {
1367 if (!strcmp(name, "_etext")) {
1375 static int kcore_copy__parse_kallsyms(struct kcore_copy_info *kci,
1378 char kallsyms_filename[PATH_MAX];
1380 scnprintf(kallsyms_filename, PATH_MAX, "%s/kallsyms", dir);
1382 if (symbol__restricted_filename(kallsyms_filename, "/proc/kallsyms"))
1385 if (kallsyms__parse(kallsyms_filename, kci,
1386 kcore_copy__process_kallsyms) < 0)
1392 static int kcore_copy__process_modules(void *arg,
1393 const char *name __maybe_unused,
1396 struct kcore_copy_info *kci = arg;
1398 if (!kci->first_module || start < kci->first_module)
1399 kci->first_module = start;
1404 static int kcore_copy__parse_modules(struct kcore_copy_info *kci,
1407 char modules_filename[PATH_MAX];
1409 scnprintf(modules_filename, PATH_MAX, "%s/modules", dir);
1411 if (symbol__restricted_filename(modules_filename, "/proc/modules"))
1414 if (modules__parse(modules_filename, kci,
1415 kcore_copy__process_modules) < 0)
1421 static void kcore_copy__map(struct phdr_data *p, u64 start, u64 end, u64 pgoff,
1424 if (p->addr || s < start || s >= end)
1428 p->offset = (s - start) + pgoff;
1429 p->len = e < end ? e - s : end - s;
1432 static int kcore_copy__read_map(u64 start, u64 len, u64 pgoff, void *data)
1434 struct kcore_copy_info *kci = data;
1435 u64 end = start + len;
1437 kcore_copy__map(&kci->kernel_map, start, end, pgoff, kci->stext,
1440 kcore_copy__map(&kci->modules_map, start, end, pgoff, kci->first_module,
1441 kci->last_module_symbol);
1446 static int kcore_copy__read_maps(struct kcore_copy_info *kci, Elf *elf)
1448 if (elf_read_maps(elf, true, kcore_copy__read_map, kci) < 0)
1454 static int kcore_copy__calc_maps(struct kcore_copy_info *kci, const char *dir,
1457 if (kcore_copy__parse_kallsyms(kci, dir))
1460 if (kcore_copy__parse_modules(kci, dir))
1464 kci->stext = round_down(kci->stext, page_size);
1466 kci->stext = round_down(kci->first_symbol, page_size);
1469 kci->etext = round_up(kci->etext, page_size);
1470 } else if (kci->last_symbol) {
1471 kci->etext = round_up(kci->last_symbol, page_size);
1472 kci->etext += page_size;
1475 kci->first_module = round_down(kci->first_module, page_size);
1477 if (kci->last_module_symbol) {
1478 kci->last_module_symbol = round_up(kci->last_module_symbol,
1480 kci->last_module_symbol += page_size;
1483 if (!kci->stext || !kci->etext)
1486 if (kci->first_module && !kci->last_module_symbol)
1489 return kcore_copy__read_maps(kci, elf);
1492 static int kcore_copy__copy_file(const char *from_dir, const char *to_dir,
1495 char from_filename[PATH_MAX];
1496 char to_filename[PATH_MAX];
1498 scnprintf(from_filename, PATH_MAX, "%s/%s", from_dir, name);
1499 scnprintf(to_filename, PATH_MAX, "%s/%s", to_dir, name);
1501 return copyfile_mode(from_filename, to_filename, 0400);
1504 static int kcore_copy__unlink(const char *dir, const char *name)
1506 char filename[PATH_MAX];
1508 scnprintf(filename, PATH_MAX, "%s/%s", dir, name);
1510 return unlink(filename);
1513 static int kcore_copy__compare_fds(int from, int to)
1521 buf_from = malloc(page_size);
1522 buf_to = malloc(page_size);
1523 if (!buf_from || !buf_to)
1527 /* Use read because mmap won't work on proc files */
1528 ret = read(from, buf_from, page_size);
1537 if (readn(to, buf_to, len) != (int)len)
1540 if (memcmp(buf_from, buf_to, len))
1551 static int kcore_copy__compare_files(const char *from_filename,
1552 const char *to_filename)
1554 int from, to, err = -1;
1556 from = open(from_filename, O_RDONLY);
1560 to = open(to_filename, O_RDONLY);
1562 goto out_close_from;
1564 err = kcore_copy__compare_fds(from, to);
1572 static int kcore_copy__compare_file(const char *from_dir, const char *to_dir,
1575 char from_filename[PATH_MAX];
1576 char to_filename[PATH_MAX];
1578 scnprintf(from_filename, PATH_MAX, "%s/%s", from_dir, name);
1579 scnprintf(to_filename, PATH_MAX, "%s/%s", to_dir, name);
1581 return kcore_copy__compare_files(from_filename, to_filename);
1585 * kcore_copy - copy kallsyms, modules and kcore from one directory to another.
1586 * @from_dir: from directory
1587 * @to_dir: to directory
1589 * This function copies kallsyms, modules and kcore files from one directory to
1590 * another. kallsyms and modules are copied entirely. Only code segments are
1591 * copied from kcore. It is assumed that two segments suffice: one for the
1592 * kernel proper and one for all the modules. The code segments are determined
1593 * from kallsyms and modules files. The kernel map starts at _stext or the
1594 * lowest function symbol, and ends at _etext or the highest function symbol.
1595 * The module map starts at the lowest module address and ends at the highest
1596 * module symbol. Start addresses are rounded down to the nearest page. End
1597 * addresses are rounded up to the nearest page. An extra page is added to the
1598 * highest kernel symbol and highest module symbol to, hopefully, encompass that
1599 * symbol too. Because it contains only code sections, the resulting kcore is
1600 * unusual. One significant peculiarity is that the mapping (start -> pgoff)
1601 * is not the same for the kernel map and the modules map. That happens because
1602 * the data is copied adjacently whereas the original kcore has gaps. Finally,
1603 * kallsyms and modules files are compared with their copies to check that
1604 * modules have not been loaded or unloaded while the copies were taking place.
1606 * Return: %0 on success, %-1 on failure.
1608 int kcore_copy(const char *from_dir, const char *to_dir)
1611 struct kcore extract;
1613 int idx = 0, err = -1;
1614 off_t offset = page_size, sz, modules_offset = 0;
1615 struct kcore_copy_info kci = { .stext = 0, };
1616 char kcore_filename[PATH_MAX];
1617 char extract_filename[PATH_MAX];
1619 if (kcore_copy__copy_file(from_dir, to_dir, "kallsyms"))
1622 if (kcore_copy__copy_file(from_dir, to_dir, "modules"))
1623 goto out_unlink_kallsyms;
1625 scnprintf(kcore_filename, PATH_MAX, "%s/kcore", from_dir);
1626 scnprintf(extract_filename, PATH_MAX, "%s/kcore", to_dir);
1628 if (kcore__open(&kcore, kcore_filename))
1629 goto out_unlink_modules;
1631 if (kcore_copy__calc_maps(&kci, from_dir, kcore.elf))
1632 goto out_kcore_close;
1634 if (kcore__init(&extract, extract_filename, kcore.elfclass, false))
1635 goto out_kcore_close;
1637 if (!kci.modules_map.addr)
1640 if (kcore__copy_hdr(&kcore, &extract, count))
1641 goto out_extract_close;
1643 if (kcore__add_phdr(&extract, idx++, offset, kci.kernel_map.addr,
1644 kci.kernel_map.len))
1645 goto out_extract_close;
1647 if (kci.modules_map.addr) {
1648 modules_offset = offset + kci.kernel_map.len;
1649 if (kcore__add_phdr(&extract, idx, modules_offset,
1650 kci.modules_map.addr, kci.modules_map.len))
1651 goto out_extract_close;
1654 sz = kcore__write(&extract);
1655 if (sz < 0 || sz > offset)
1656 goto out_extract_close;
1658 if (copy_bytes(kcore.fd, kci.kernel_map.offset, extract.fd, offset,
1659 kci.kernel_map.len))
1660 goto out_extract_close;
1662 if (modules_offset && copy_bytes(kcore.fd, kci.modules_map.offset,
1663 extract.fd, modules_offset,
1664 kci.modules_map.len))
1665 goto out_extract_close;
1667 if (kcore_copy__compare_file(from_dir, to_dir, "modules"))
1668 goto out_extract_close;
1670 if (kcore_copy__compare_file(from_dir, to_dir, "kallsyms"))
1671 goto out_extract_close;
1676 kcore__close(&extract);
1678 unlink(extract_filename);
1680 kcore__close(&kcore);
1683 kcore_copy__unlink(to_dir, "modules");
1684 out_unlink_kallsyms:
1686 kcore_copy__unlink(to_dir, "kallsyms");
1691 int kcore_extract__create(struct kcore_extract *kce)
1694 struct kcore extract;
1696 int idx = 0, err = -1;
1697 off_t offset = page_size, sz;
1699 if (kcore__open(&kcore, kce->kcore_filename))
1702 strcpy(kce->extract_filename, PERF_KCORE_EXTRACT);
1703 if (kcore__init(&extract, kce->extract_filename, kcore.elfclass, true))
1704 goto out_kcore_close;
1706 if (kcore__copy_hdr(&kcore, &extract, count))
1707 goto out_extract_close;
1709 if (kcore__add_phdr(&extract, idx, offset, kce->addr, kce->len))
1710 goto out_extract_close;
1712 sz = kcore__write(&extract);
1713 if (sz < 0 || sz > offset)
1714 goto out_extract_close;
1716 if (copy_bytes(kcore.fd, kce->offs, extract.fd, offset, kce->len))
1717 goto out_extract_close;
1722 kcore__close(&extract);
1724 unlink(kce->extract_filename);
1726 kcore__close(&kcore);
1731 void kcore_extract__delete(struct kcore_extract *kce)
1733 unlink(kce->extract_filename);
1736 void symbol__elf_init(void)
1738 elf_version(EV_CURRENT);