1 // SPDX-License-Identifier: GPL-2.0
15 #include "demangle-java.h"
16 #include "demangle-rust.h"
20 #include "util/copyfile.h"
21 #include <linux/ctype.h>
22 #include <linux/kernel.h>
23 #include <linux/zalloc.h>
24 #include <symbol/kallsyms.h>
25 #include <internal/lib.h>
28 #define EM_AARCH64 183 /* ARM 64 bit */
31 #ifndef ELF32_ST_VISIBILITY
32 #define ELF32_ST_VISIBILITY(o) ((o) & 0x03)
35 /* For ELF64 the definitions are the same. */
36 #ifndef ELF64_ST_VISIBILITY
37 #define ELF64_ST_VISIBILITY(o) ELF32_ST_VISIBILITY (o)
40 /* How to extract information held in the st_other field. */
41 #ifndef GELF_ST_VISIBILITY
42 #define GELF_ST_VISIBILITY(val) ELF64_ST_VISIBILITY (val)
45 typedef Elf64_Nhdr GElf_Nhdr;
48 #define DMGL_NO_OPTS 0 /* For readability... */
49 #define DMGL_PARAMS (1 << 0) /* Include function args */
50 #define DMGL_ANSI (1 << 1) /* Include const, volatile, etc */
53 #ifdef HAVE_LIBBFD_SUPPORT
54 #define PACKAGE 'perf'
57 #ifdef HAVE_CPLUS_DEMANGLE_SUPPORT
58 extern char *cplus_demangle(const char *, int);
60 static inline char *bfd_demangle(void __maybe_unused *v, const char *c, int i)
62 return cplus_demangle(c, i);
66 static inline char *bfd_demangle(void __maybe_unused *v,
67 const char __maybe_unused *c,
76 #ifndef HAVE_ELF_GETPHDRNUM_SUPPORT
77 static int elf_getphdrnum(Elf *elf, size_t *dst)
82 ehdr = gelf_getehdr(elf, &gehdr);
92 #ifndef HAVE_ELF_GETSHDRSTRNDX_SUPPORT
93 static int elf_getshdrstrndx(Elf *elf __maybe_unused, size_t *dst __maybe_unused)
95 pr_err("%s: update your libelf to > 0.140, this one lacks elf_getshdrstrndx().\n", __func__);
100 #ifndef NT_GNU_BUILD_ID
101 #define NT_GNU_BUILD_ID 3
105 * elf_symtab__for_each_symbol - iterate thru all the symbols
107 * @syms: struct elf_symtab instance to iterate
109 * @sym: GElf_Sym iterator
111 #define elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) \
112 for (idx = 0, gelf_getsym(syms, idx, &sym);\
114 idx++, gelf_getsym(syms, idx, &sym))
116 static inline uint8_t elf_sym__type(const GElf_Sym *sym)
118 return GELF_ST_TYPE(sym->st_info);
121 static inline uint8_t elf_sym__visibility(const GElf_Sym *sym)
123 return GELF_ST_VISIBILITY(sym->st_other);
126 #ifndef STT_GNU_IFUNC
127 #define STT_GNU_IFUNC 10
130 static inline int elf_sym__is_function(const GElf_Sym *sym)
132 return (elf_sym__type(sym) == STT_FUNC ||
133 elf_sym__type(sym) == STT_GNU_IFUNC) &&
135 sym->st_shndx != SHN_UNDEF;
138 static inline bool elf_sym__is_object(const GElf_Sym *sym)
140 return elf_sym__type(sym) == STT_OBJECT &&
142 sym->st_shndx != SHN_UNDEF;
145 static inline int elf_sym__is_label(const GElf_Sym *sym)
147 return elf_sym__type(sym) == STT_NOTYPE &&
149 sym->st_shndx != SHN_UNDEF &&
150 sym->st_shndx != SHN_ABS &&
151 elf_sym__visibility(sym) != STV_HIDDEN &&
152 elf_sym__visibility(sym) != STV_INTERNAL;
155 static bool elf_sym__filter(GElf_Sym *sym)
157 return elf_sym__is_function(sym) || elf_sym__is_object(sym);
160 static inline const char *elf_sym__name(const GElf_Sym *sym,
161 const Elf_Data *symstrs)
163 return symstrs->d_buf + sym->st_name;
166 static inline const char *elf_sec__name(const GElf_Shdr *shdr,
167 const Elf_Data *secstrs)
169 return secstrs->d_buf + shdr->sh_name;
172 static inline int elf_sec__is_text(const GElf_Shdr *shdr,
173 const Elf_Data *secstrs)
175 return strstr(elf_sec__name(shdr, secstrs), "text") != NULL;
178 static inline bool elf_sec__is_data(const GElf_Shdr *shdr,
179 const Elf_Data *secstrs)
181 return strstr(elf_sec__name(shdr, secstrs), "data") != NULL;
184 static bool elf_sec__filter(GElf_Shdr *shdr, Elf_Data *secstrs)
186 return elf_sec__is_text(shdr, secstrs) ||
187 elf_sec__is_data(shdr, secstrs);
190 static size_t elf_addr_to_index(Elf *elf, GElf_Addr addr)
196 while ((sec = elf_nextscn(elf, sec)) != NULL) {
197 gelf_getshdr(sec, &shdr);
199 if ((addr >= shdr.sh_addr) &&
200 (addr < (shdr.sh_addr + shdr.sh_size)))
209 Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep,
210 GElf_Shdr *shp, const char *name, size_t *idx)
215 /* Elf is corrupted/truncated, avoid calling elf_strptr. */
216 if (!elf_rawdata(elf_getscn(elf, ep->e_shstrndx), NULL))
219 while ((sec = elf_nextscn(elf, sec)) != NULL) {
222 gelf_getshdr(sec, shp);
223 str = elf_strptr(elf, ep->e_shstrndx, shp->sh_name);
224 if (str && !strcmp(name, str)) {
235 static int elf_read_program_header(Elf *elf, u64 vaddr, GElf_Phdr *phdr)
240 if (elf_getphdrnum(elf, &phdrnum))
243 for (i = 0; i < phdrnum; i++) {
244 if (gelf_getphdr(elf, i, phdr) == NULL)
247 if (phdr->p_type != PT_LOAD)
250 sz = max(phdr->p_memsz, phdr->p_filesz);
254 if (vaddr >= phdr->p_vaddr && (vaddr < phdr->p_vaddr + sz))
258 /* Not found any valid program header */
262 static bool want_demangle(bool is_kernel_sym)
264 return is_kernel_sym ? symbol_conf.demangle_kernel : symbol_conf.demangle;
267 static char *demangle_sym(struct dso *dso, int kmodule, const char *elf_name)
269 int demangle_flags = verbose > 0 ? (DMGL_PARAMS | DMGL_ANSI) : DMGL_NO_OPTS;
270 char *demangled = NULL;
273 * We need to figure out if the object was created from C++ sources
274 * DWARF DW_compile_unit has this, but we don't always have access
277 if (!want_demangle(dso->kernel || kmodule))
280 demangled = bfd_demangle(NULL, elf_name, demangle_flags);
281 if (demangled == NULL)
282 demangled = java_demangle_sym(elf_name, JAVA_DEMANGLE_NORET);
283 else if (rust_is_mangled(demangled))
285 * Input to Rust demangling is the BFD-demangled
286 * name which it Rust-demangles in place.
288 rust_demangle_sym(demangled);
293 #define elf_section__for_each_rel(reldata, pos, pos_mem, idx, nr_entries) \
294 for (idx = 0, pos = gelf_getrel(reldata, 0, &pos_mem); \
296 ++idx, pos = gelf_getrel(reldata, idx, &pos_mem))
298 #define elf_section__for_each_rela(reldata, pos, pos_mem, idx, nr_entries) \
299 for (idx = 0, pos = gelf_getrela(reldata, 0, &pos_mem); \
301 ++idx, pos = gelf_getrela(reldata, idx, &pos_mem))
304 * We need to check if we have a .dynsym, so that we can handle the
305 * .plt, synthesizing its symbols, that aren't on the symtabs (be it
306 * .dynsym or .symtab).
307 * And always look at the original dso, not at debuginfo packages, that
308 * have the PLT data stripped out (shdr_rel_plt.sh_type == SHT_NOBITS).
310 int dso__synthesize_plt_symbols(struct dso *dso, struct symsrc *ss)
312 uint32_t nr_rel_entries, idx;
314 u64 plt_offset, plt_header_size, plt_entry_size;
317 GElf_Shdr shdr_rel_plt, shdr_dynsym;
318 Elf_Data *reldata, *syms, *symstrs;
319 Elf_Scn *scn_plt_rel, *scn_symstrs, *scn_dynsym;
322 char sympltname[1024];
324 int nr = 0, symidx, err = 0;
332 scn_dynsym = ss->dynsym;
333 shdr_dynsym = ss->dynshdr;
334 dynsym_idx = ss->dynsym_idx;
336 if (scn_dynsym == NULL)
339 scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt,
341 if (scn_plt_rel == NULL) {
342 scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt,
344 if (scn_plt_rel == NULL)
350 if (shdr_rel_plt.sh_link != dynsym_idx)
353 if (elf_section_by_name(elf, &ehdr, &shdr_plt, ".plt", NULL) == NULL)
357 * Fetch the relocation section to find the idxes to the GOT
358 * and the symbols in the .dynsym they refer to.
360 reldata = elf_getdata(scn_plt_rel, NULL);
364 syms = elf_getdata(scn_dynsym, NULL);
368 scn_symstrs = elf_getscn(elf, shdr_dynsym.sh_link);
369 if (scn_symstrs == NULL)
372 symstrs = elf_getdata(scn_symstrs, NULL);
376 if (symstrs->d_size == 0)
379 nr_rel_entries = shdr_rel_plt.sh_size / shdr_rel_plt.sh_entsize;
380 plt_offset = shdr_plt.sh_offset;
381 switch (ehdr.e_machine) {
383 plt_header_size = 20;
388 plt_header_size = 32;
393 plt_header_size = 48;
398 plt_header_size = 128;
402 default: /* FIXME: s390/alpha/mips/parisc/poperpc/sh/xtensa need to be checked */
403 plt_header_size = shdr_plt.sh_entsize;
404 plt_entry_size = shdr_plt.sh_entsize;
407 plt_offset += plt_header_size;
409 if (shdr_rel_plt.sh_type == SHT_RELA) {
410 GElf_Rela pos_mem, *pos;
412 elf_section__for_each_rela(reldata, pos, pos_mem, idx,
414 const char *elf_name = NULL;
415 char *demangled = NULL;
416 symidx = GELF_R_SYM(pos->r_info);
417 gelf_getsym(syms, symidx, &sym);
419 elf_name = elf_sym__name(&sym, symstrs);
420 demangled = demangle_sym(dso, 0, elf_name);
421 if (demangled != NULL)
422 elf_name = demangled;
423 snprintf(sympltname, sizeof(sympltname),
427 f = symbol__new(plt_offset, plt_entry_size,
428 STB_GLOBAL, STT_FUNC, sympltname);
432 plt_offset += plt_entry_size;
433 symbols__insert(&dso->symbols, f);
436 } else if (shdr_rel_plt.sh_type == SHT_REL) {
437 GElf_Rel pos_mem, *pos;
438 elf_section__for_each_rel(reldata, pos, pos_mem, idx,
440 const char *elf_name = NULL;
441 char *demangled = NULL;
442 symidx = GELF_R_SYM(pos->r_info);
443 gelf_getsym(syms, symidx, &sym);
445 elf_name = elf_sym__name(&sym, symstrs);
446 demangled = demangle_sym(dso, 0, elf_name);
447 if (demangled != NULL)
448 elf_name = demangled;
449 snprintf(sympltname, sizeof(sympltname),
453 f = symbol__new(plt_offset, plt_entry_size,
454 STB_GLOBAL, STT_FUNC, sympltname);
458 plt_offset += plt_entry_size;
459 symbols__insert(&dso->symbols, f);
468 pr_debug("%s: problems reading %s PLT info.\n",
469 __func__, dso->long_name);
473 char *dso__demangle_sym(struct dso *dso, int kmodule, const char *elf_name)
475 return demangle_sym(dso, kmodule, elf_name);
479 * Align offset to 4 bytes as needed for note name and descriptor data.
481 #define NOTE_ALIGN(n) (((n) + 3) & -4U)
483 static int elf_read_build_id(Elf *elf, void *bf, size_t size)
493 if (size < BUILD_ID_SIZE)
500 if (gelf_getehdr(elf, &ehdr) == NULL) {
501 pr_err("%s: cannot get elf header.\n", __func__);
506 * Check following sections for notes:
507 * '.note.gnu.build-id'
509 * '.note' (VDSO specific)
512 sec = elf_section_by_name(elf, &ehdr, &shdr,
513 ".note.gnu.build-id", NULL);
517 sec = elf_section_by_name(elf, &ehdr, &shdr,
522 sec = elf_section_by_name(elf, &ehdr, &shdr,
531 data = elf_getdata(sec, NULL);
536 while (ptr < (data->d_buf + data->d_size)) {
537 GElf_Nhdr *nhdr = ptr;
538 size_t namesz = NOTE_ALIGN(nhdr->n_namesz),
539 descsz = NOTE_ALIGN(nhdr->n_descsz);
542 ptr += sizeof(*nhdr);
545 if (nhdr->n_type == NT_GNU_BUILD_ID &&
546 nhdr->n_namesz == sizeof("GNU")) {
547 if (memcmp(name, "GNU", sizeof("GNU")) == 0) {
548 size_t sz = min(size, descsz);
550 memset(bf + sz, 0, size - sz);
562 #ifdef HAVE_LIBBFD_BUILDID_SUPPORT
564 int filename__read_build_id(const char *filename, struct build_id *bid)
566 size_t size = sizeof(bid->data);
570 abfd = bfd_openr(filename, NULL);
574 if (!bfd_check_format(abfd, bfd_object)) {
575 pr_debug2("%s: cannot read %s bfd file.\n", __func__, filename);
579 if (!abfd->build_id || abfd->build_id->size > size)
582 memcpy(bid->data, abfd->build_id->data, abfd->build_id->size);
583 memset(bid->data + abfd->build_id->size, 0, size - abfd->build_id->size);
584 err = bid->size = abfd->build_id->size;
591 #else // HAVE_LIBBFD_BUILDID_SUPPORT
593 int filename__read_build_id(const char *filename, struct build_id *bid)
595 size_t size = sizeof(bid->data);
599 if (size < BUILD_ID_SIZE)
602 fd = open(filename, O_RDONLY);
606 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
608 pr_debug2("%s: cannot read %s ELF file.\n", __func__, filename);
612 err = elf_read_build_id(elf, bid->data, size);
623 #endif // HAVE_LIBBFD_BUILDID_SUPPORT
625 int sysfs__read_build_id(const char *filename, struct build_id *bid)
627 size_t size = sizeof(bid->data);
630 fd = open(filename, O_RDONLY);
637 size_t namesz, descsz;
639 if (read(fd, &nhdr, sizeof(nhdr)) != sizeof(nhdr))
642 namesz = NOTE_ALIGN(nhdr.n_namesz);
643 descsz = NOTE_ALIGN(nhdr.n_descsz);
644 if (nhdr.n_type == NT_GNU_BUILD_ID &&
645 nhdr.n_namesz == sizeof("GNU")) {
646 if (read(fd, bf, namesz) != (ssize_t)namesz)
648 if (memcmp(bf, "GNU", sizeof("GNU")) == 0) {
649 size_t sz = min(descsz, size);
650 if (read(fd, bid->data, sz) == (ssize_t)sz) {
651 memset(bid->data + sz, 0, size - sz);
656 } else if (read(fd, bf, descsz) != (ssize_t)descsz)
659 int n = namesz + descsz;
661 if (n > (int)sizeof(bf)) {
663 pr_debug("%s: truncating reading of build id in sysfs file %s: n_namesz=%u, n_descsz=%u.\n",
664 __func__, filename, nhdr.n_namesz, nhdr.n_descsz);
666 if (read(fd, bf, n) != n)
675 #ifdef HAVE_LIBBFD_SUPPORT
677 int filename__read_debuglink(const char *filename, char *debuglink,
684 abfd = bfd_openr(filename, NULL);
688 if (!bfd_check_format(abfd, bfd_object)) {
689 pr_debug2("%s: cannot read %s bfd file.\n", __func__, filename);
693 section = bfd_get_section_by_name(abfd, ".gnu_debuglink");
697 if (section->size > size)
700 if (!bfd_get_section_contents(abfd, section, debuglink, 0,
713 int filename__read_debuglink(const char *filename, char *debuglink,
724 fd = open(filename, O_RDONLY);
728 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
730 pr_debug2("%s: cannot read %s ELF file.\n", __func__, filename);
738 if (gelf_getehdr(elf, &ehdr) == NULL) {
739 pr_err("%s: cannot get elf header.\n", __func__);
743 sec = elf_section_by_name(elf, &ehdr, &shdr,
744 ".gnu_debuglink", NULL);
748 data = elf_getdata(sec, NULL);
752 /* the start of this section is a zero-terminated string */
753 strncpy(debuglink, data->d_buf, size);
767 static int dso__swap_init(struct dso *dso, unsigned char eidata)
769 static unsigned int const endian = 1;
771 dso->needs_swap = DSO_SWAP__NO;
775 /* We are big endian, DSO is little endian. */
776 if (*(unsigned char const *)&endian != 1)
777 dso->needs_swap = DSO_SWAP__YES;
781 /* We are little endian, DSO is big endian. */
782 if (*(unsigned char const *)&endian != 0)
783 dso->needs_swap = DSO_SWAP__YES;
787 pr_err("unrecognized DSO data encoding %d\n", eidata);
794 bool symsrc__possibly_runtime(struct symsrc *ss)
796 return ss->dynsym || ss->opdsec;
799 bool symsrc__has_symtab(struct symsrc *ss)
801 return ss->symtab != NULL;
804 void symsrc__destroy(struct symsrc *ss)
811 bool elf__needs_adjust_symbols(GElf_Ehdr ehdr)
814 * Usually vmlinux is an ELF file with type ET_EXEC for most
815 * architectures; except Arm64 kernel is linked with option
816 * '-share', so need to check type ET_DYN.
818 return ehdr.e_type == ET_EXEC || ehdr.e_type == ET_REL ||
819 ehdr.e_type == ET_DYN;
822 int symsrc__init(struct symsrc *ss, struct dso *dso, const char *name,
823 enum dso_binary_type type)
829 if (dso__needs_decompress(dso)) {
830 fd = dso__decompress_kmodule_fd(dso, name);
834 type = dso->symtab_type;
836 fd = open(name, O_RDONLY);
838 dso->load_errno = errno;
843 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
845 pr_debug("%s: cannot read %s ELF file.\n", __func__, name);
846 dso->load_errno = DSO_LOAD_ERRNO__INVALID_ELF;
850 if (gelf_getehdr(elf, &ehdr) == NULL) {
851 dso->load_errno = DSO_LOAD_ERRNO__INVALID_ELF;
852 pr_debug("%s: cannot get elf header.\n", __func__);
856 if (dso__swap_init(dso, ehdr.e_ident[EI_DATA])) {
857 dso->load_errno = DSO_LOAD_ERRNO__INTERNAL_ERROR;
861 /* Always reject images with a mismatched build-id: */
862 if (dso->has_build_id && !symbol_conf.ignore_vmlinux_buildid) {
863 u8 build_id[BUILD_ID_SIZE];
867 size = elf_read_build_id(elf, build_id, BUILD_ID_SIZE);
869 dso->load_errno = DSO_LOAD_ERRNO__CANNOT_READ_BUILDID;
873 build_id__init(&bid, build_id, size);
874 if (!dso__build_id_equal(dso, &bid)) {
875 pr_debug("%s: build id mismatch for %s.\n", __func__, name);
876 dso->load_errno = DSO_LOAD_ERRNO__MISMATCHING_BUILDID;
881 ss->is_64_bit = (gelf_getclass(elf) == ELFCLASS64);
883 ss->symtab = elf_section_by_name(elf, &ehdr, &ss->symshdr, ".symtab",
885 if (ss->symshdr.sh_type != SHT_SYMTAB)
889 ss->dynsym = elf_section_by_name(elf, &ehdr, &ss->dynshdr, ".dynsym",
891 if (ss->dynshdr.sh_type != SHT_DYNSYM)
895 ss->opdsec = elf_section_by_name(elf, &ehdr, &ss->opdshdr, ".opd",
897 if (ss->opdshdr.sh_type != SHT_PROGBITS)
900 if (dso->kernel == DSO_SPACE__USER)
901 ss->adjust_symbols = true;
903 ss->adjust_symbols = elf__needs_adjust_symbols(ehdr);
905 ss->name = strdup(name);
907 dso->load_errno = errno;
926 * ref_reloc_sym_not_found - has kernel relocation symbol been found.
927 * @kmap: kernel maps and relocation reference symbol
929 * This function returns %true if we are dealing with the kernel maps and the
930 * relocation reference symbol has not yet been found. Otherwise %false is
933 static bool ref_reloc_sym_not_found(struct kmap *kmap)
935 return kmap && kmap->ref_reloc_sym && kmap->ref_reloc_sym->name &&
936 !kmap->ref_reloc_sym->unrelocated_addr;
940 * ref_reloc - kernel relocation offset.
941 * @kmap: kernel maps and relocation reference symbol
943 * This function returns the offset of kernel addresses as determined by using
944 * the relocation reference symbol i.e. if the kernel has not been relocated
945 * then the return value is zero.
947 static u64 ref_reloc(struct kmap *kmap)
949 if (kmap && kmap->ref_reloc_sym &&
950 kmap->ref_reloc_sym->unrelocated_addr)
951 return kmap->ref_reloc_sym->addr -
952 kmap->ref_reloc_sym->unrelocated_addr;
956 void __weak arch__sym_update(struct symbol *s __maybe_unused,
957 GElf_Sym *sym __maybe_unused) { }
959 static int dso__process_kernel_symbol(struct dso *dso, struct map *map,
960 GElf_Sym *sym, GElf_Shdr *shdr,
961 struct maps *kmaps, struct kmap *kmap,
962 struct dso **curr_dsop, struct map **curr_mapp,
963 const char *section_name,
964 bool adjust_kernel_syms, bool kmodule, bool *remap_kernel)
966 struct dso *curr_dso = *curr_dsop;
967 struct map *curr_map;
968 char dso_name[PATH_MAX];
970 /* Adjust symbol to map to file offset */
971 if (adjust_kernel_syms)
972 sym->st_value -= shdr->sh_addr - shdr->sh_offset;
974 if (strcmp(section_name, (curr_dso->short_name + dso->short_name_len)) == 0)
977 if (strcmp(section_name, ".text") == 0) {
979 * The initial kernel mapping is based on
980 * kallsyms and identity maps. Overwrite it to
981 * map to the kernel dso.
983 if (*remap_kernel && dso->kernel && !kmodule) {
984 *remap_kernel = false;
985 map->start = shdr->sh_addr + ref_reloc(kmap);
986 map->end = map->start + shdr->sh_size;
987 map->pgoff = shdr->sh_offset;
988 map->map_ip = map__map_ip;
989 map->unmap_ip = map__unmap_ip;
990 /* Ensure maps are correctly ordered */
993 maps__remove(kmaps, map);
994 maps__insert(kmaps, map);
1000 * The initial module mapping is based on
1001 * /proc/modules mapped to offset zero.
1002 * Overwrite it to map to the module dso.
1004 if (*remap_kernel && kmodule) {
1005 *remap_kernel = false;
1006 map->pgoff = shdr->sh_offset;
1017 snprintf(dso_name, sizeof(dso_name), "%s%s", dso->short_name, section_name);
1019 curr_map = maps__find_by_name(kmaps, dso_name);
1020 if (curr_map == NULL) {
1021 u64 start = sym->st_value;
1024 start += map->start + shdr->sh_offset;
1026 curr_dso = dso__new(dso_name);
1027 if (curr_dso == NULL)
1029 curr_dso->kernel = dso->kernel;
1030 curr_dso->long_name = dso->long_name;
1031 curr_dso->long_name_len = dso->long_name_len;
1032 curr_map = map__new2(start, curr_dso);
1034 if (curr_map == NULL)
1037 if (curr_dso->kernel)
1038 map__kmap(curr_map)->kmaps = kmaps;
1040 if (adjust_kernel_syms) {
1041 curr_map->start = shdr->sh_addr + ref_reloc(kmap);
1042 curr_map->end = curr_map->start + shdr->sh_size;
1043 curr_map->pgoff = shdr->sh_offset;
1045 curr_map->map_ip = curr_map->unmap_ip = identity__map_ip;
1047 curr_dso->symtab_type = dso->symtab_type;
1048 maps__insert(kmaps, curr_map);
1050 * Add it before we drop the referece to curr_map, i.e. while
1051 * we still are sure to have a reference to this DSO via
1054 dsos__add(&kmaps->machine->dsos, curr_dso);
1055 /* kmaps already got it */
1057 dso__set_loaded(curr_dso);
1058 *curr_mapp = curr_map;
1059 *curr_dsop = curr_dso;
1061 *curr_dsop = curr_map->dso;
1066 int dso__load_sym(struct dso *dso, struct map *map, struct symsrc *syms_ss,
1067 struct symsrc *runtime_ss, int kmodule)
1069 struct kmap *kmap = dso->kernel ? map__kmap(map) : NULL;
1070 struct maps *kmaps = kmap ? map__kmaps(map) : NULL;
1071 struct map *curr_map = map;
1072 struct dso *curr_dso = dso;
1073 Elf_Data *symstrs, *secstrs;
1080 Elf_Data *syms, *opddata = NULL;
1082 Elf_Scn *sec, *sec_strndx;
1085 bool remap_kernel = false, adjust_kernel_syms = false;
1090 dso->symtab_type = syms_ss->type;
1091 dso->is_64_bit = syms_ss->is_64_bit;
1092 dso->rel = syms_ss->ehdr.e_type == ET_REL;
1095 * Modules may already have symbols from kallsyms, but those symbols
1096 * have the wrong values for the dso maps, so remove them.
1098 if (kmodule && syms_ss->symtab)
1099 symbols__delete(&dso->symbols);
1101 if (!syms_ss->symtab) {
1103 * If the vmlinux is stripped, fail so we will fall back
1104 * to using kallsyms. The vmlinux runtime symbols aren't
1110 syms_ss->symtab = syms_ss->dynsym;
1111 syms_ss->symshdr = syms_ss->dynshdr;
1115 ehdr = syms_ss->ehdr;
1116 sec = syms_ss->symtab;
1117 shdr = syms_ss->symshdr;
1119 if (elf_section_by_name(runtime_ss->elf, &runtime_ss->ehdr, &tshdr,
1121 dso->text_offset = tshdr.sh_addr - tshdr.sh_offset;
1123 if (runtime_ss->opdsec)
1124 opddata = elf_rawdata(runtime_ss->opdsec, NULL);
1126 syms = elf_getdata(sec, NULL);
1130 sec = elf_getscn(elf, shdr.sh_link);
1134 symstrs = elf_getdata(sec, NULL);
1135 if (symstrs == NULL)
1138 sec_strndx = elf_getscn(runtime_ss->elf, runtime_ss->ehdr.e_shstrndx);
1139 if (sec_strndx == NULL)
1142 secstrs = elf_getdata(sec_strndx, NULL);
1143 if (secstrs == NULL)
1146 nr_syms = shdr.sh_size / shdr.sh_entsize;
1148 memset(&sym, 0, sizeof(sym));
1151 * The kernel relocation symbol is needed in advance in order to adjust
1152 * kernel maps correctly.
1154 if (ref_reloc_sym_not_found(kmap)) {
1155 elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) {
1156 const char *elf_name = elf_sym__name(&sym, symstrs);
1158 if (strcmp(elf_name, kmap->ref_reloc_sym->name))
1160 kmap->ref_reloc_sym->unrelocated_addr = sym.st_value;
1161 map->reloc = kmap->ref_reloc_sym->addr -
1162 kmap->ref_reloc_sym->unrelocated_addr;
1168 * Handle any relocation of vdso necessary because older kernels
1169 * attempted to prelink vdso to its virtual address.
1171 if (dso__is_vdso(dso))
1172 map->reloc = map->start - dso->text_offset;
1174 dso->adjust_symbols = runtime_ss->adjust_symbols || ref_reloc(kmap);
1176 * Initial kernel and module mappings do not map to the dso.
1180 remap_kernel = true;
1181 adjust_kernel_syms = dso->adjust_symbols;
1183 elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) {
1185 const char *elf_name = elf_sym__name(&sym, symstrs);
1186 char *demangled = NULL;
1187 int is_label = elf_sym__is_label(&sym);
1188 const char *section_name;
1189 bool used_opd = false;
1191 if (!is_label && !elf_sym__filter(&sym))
1194 /* Reject ARM ELF "mapping symbols": these aren't unique and
1195 * don't identify functions, so will confuse the profile
1197 if (ehdr.e_machine == EM_ARM || ehdr.e_machine == EM_AARCH64) {
1198 if (elf_name[0] == '$' && strchr("adtx", elf_name[1])
1199 && (elf_name[2] == '\0' || elf_name[2] == '.'))
1203 if (runtime_ss->opdsec && sym.st_shndx == runtime_ss->opdidx) {
1204 u32 offset = sym.st_value - syms_ss->opdshdr.sh_addr;
1205 u64 *opd = opddata->d_buf + offset;
1206 sym.st_value = DSO__SWAP(dso, u64, *opd);
1207 sym.st_shndx = elf_addr_to_index(runtime_ss->elf,
1213 * When loading symbols in a data mapping, ABS symbols (which
1214 * has a value of SHN_ABS in its st_shndx) failed at
1215 * elf_getscn(). And it marks the loading as a failure so
1216 * already loaded symbols cannot be fixed up.
1218 * I'm not sure what should be done. Just ignore them for now.
1221 if (sym.st_shndx == SHN_ABS)
1224 sec = elf_getscn(runtime_ss->elf, sym.st_shndx);
1228 gelf_getshdr(sec, &shdr);
1230 if (is_label && !elf_sec__filter(&shdr, secstrs))
1233 section_name = elf_sec__name(&shdr, secstrs);
1235 /* On ARM, symbols for thumb functions have 1 added to
1236 * the symbol address as a flag - remove it */
1237 if ((ehdr.e_machine == EM_ARM) &&
1238 (GELF_ST_TYPE(sym.st_info) == STT_FUNC) &&
1243 if (dso__process_kernel_symbol(dso, map, &sym, &shdr, kmaps, kmap, &curr_dso, &curr_map,
1244 section_name, adjust_kernel_syms, kmodule, &remap_kernel))
1246 } else if ((used_opd && runtime_ss->adjust_symbols) ||
1247 (!used_opd && syms_ss->adjust_symbols)) {
1250 if (elf_read_program_header(runtime_ss->elf,
1251 (u64)sym.st_value, &phdr)) {
1252 pr_debug4("%s: failed to find program header for "
1253 "symbol: %s st_value: %#" PRIx64 "\n",
1254 __func__, elf_name, (u64)sym.st_value);
1255 pr_debug4("%s: adjusting symbol: st_value: %#" PRIx64 " "
1256 "sh_addr: %#" PRIx64 " sh_offset: %#" PRIx64 "\n",
1257 __func__, (u64)sym.st_value, (u64)shdr.sh_addr,
1258 (u64)shdr.sh_offset);
1260 * Fail to find program header, let's rollback
1261 * to use shdr.sh_addr and shdr.sh_offset to
1262 * calibrate symbol's file address, though this
1263 * is not necessary for normal C ELF file, we
1264 * still need to handle java JIT symbols in this
1267 sym.st_value -= shdr.sh_addr - shdr.sh_offset;
1269 pr_debug4("%s: adjusting symbol: st_value: %#" PRIx64 " "
1270 "p_vaddr: %#" PRIx64 " p_offset: %#" PRIx64 "\n",
1271 __func__, (u64)sym.st_value, (u64)phdr.p_vaddr,
1272 (u64)phdr.p_offset);
1273 sym.st_value -= phdr.p_vaddr - phdr.p_offset;
1277 demangled = demangle_sym(dso, kmodule, elf_name);
1278 if (demangled != NULL)
1279 elf_name = demangled;
1281 f = symbol__new(sym.st_value, sym.st_size,
1282 GELF_ST_BIND(sym.st_info),
1283 GELF_ST_TYPE(sym.st_info), elf_name);
1288 arch__sym_update(f, &sym);
1290 __symbols__insert(&curr_dso->symbols, f, dso->kernel);
1295 * For misannotated, zeroed, ASM function sizes.
1298 symbols__fixup_end(&dso->symbols, false);
1299 symbols__fixup_duplicate(&dso->symbols);
1302 * We need to fixup this here too because we create new
1303 * maps here, for things like vsyscall sections.
1305 maps__fixup_end(kmaps);
1313 static int elf_read_maps(Elf *elf, bool exe, mapfn_t mapfn, void *data)
1320 if (elf_getphdrnum(elf, &phdrnum))
1323 for (i = 0; i < phdrnum; i++) {
1324 if (gelf_getphdr(elf, i, &phdr) == NULL)
1326 if (phdr.p_type != PT_LOAD)
1329 if (!(phdr.p_flags & PF_X))
1332 if (!(phdr.p_flags & PF_R))
1335 sz = min(phdr.p_memsz, phdr.p_filesz);
1338 err = mapfn(phdr.p_vaddr, sz, phdr.p_offset, data);
1345 int file__read_maps(int fd, bool exe, mapfn_t mapfn, void *data,
1351 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
1356 *is_64_bit = (gelf_getclass(elf) == ELFCLASS64);
1358 err = elf_read_maps(elf, exe, mapfn, data);
1364 enum dso_type dso__type_fd(int fd)
1366 enum dso_type dso_type = DSO__TYPE_UNKNOWN;
1371 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
1376 if (ek != ELF_K_ELF)
1379 if (gelf_getclass(elf) == ELFCLASS64) {
1380 dso_type = DSO__TYPE_64BIT;
1384 if (gelf_getehdr(elf, &ehdr) == NULL)
1387 if (ehdr.e_machine == EM_X86_64)
1388 dso_type = DSO__TYPE_X32BIT;
1390 dso_type = DSO__TYPE_32BIT;
1397 static int copy_bytes(int from, off_t from_offs, int to, off_t to_offs, u64 len)
1402 char *buf = malloc(page_size);
1407 if (lseek(to, to_offs, SEEK_SET) != to_offs)
1410 if (lseek(from, from_offs, SEEK_SET) != from_offs)
1417 /* Use read because mmap won't work on proc files */
1418 r = read(from, buf, n);
1424 r = write(to, buf, n);
1445 static int kcore__open(struct kcore *kcore, const char *filename)
1449 kcore->fd = open(filename, O_RDONLY);
1450 if (kcore->fd == -1)
1453 kcore->elf = elf_begin(kcore->fd, ELF_C_READ, NULL);
1457 kcore->elfclass = gelf_getclass(kcore->elf);
1458 if (kcore->elfclass == ELFCLASSNONE)
1461 ehdr = gelf_getehdr(kcore->elf, &kcore->ehdr);
1468 elf_end(kcore->elf);
1474 static int kcore__init(struct kcore *kcore, char *filename, int elfclass,
1477 kcore->elfclass = elfclass;
1480 kcore->fd = mkstemp(filename);
1482 kcore->fd = open(filename, O_WRONLY | O_CREAT | O_EXCL, 0400);
1483 if (kcore->fd == -1)
1486 kcore->elf = elf_begin(kcore->fd, ELF_C_WRITE, NULL);
1490 if (!gelf_newehdr(kcore->elf, elfclass))
1493 memset(&kcore->ehdr, 0, sizeof(GElf_Ehdr));
1498 elf_end(kcore->elf);
1505 static void kcore__close(struct kcore *kcore)
1507 elf_end(kcore->elf);
1511 static int kcore__copy_hdr(struct kcore *from, struct kcore *to, size_t count)
1513 GElf_Ehdr *ehdr = &to->ehdr;
1514 GElf_Ehdr *kehdr = &from->ehdr;
1516 memcpy(ehdr->e_ident, kehdr->e_ident, EI_NIDENT);
1517 ehdr->e_type = kehdr->e_type;
1518 ehdr->e_machine = kehdr->e_machine;
1519 ehdr->e_version = kehdr->e_version;
1522 ehdr->e_flags = kehdr->e_flags;
1523 ehdr->e_phnum = count;
1524 ehdr->e_shentsize = 0;
1526 ehdr->e_shstrndx = 0;
1528 if (from->elfclass == ELFCLASS32) {
1529 ehdr->e_phoff = sizeof(Elf32_Ehdr);
1530 ehdr->e_ehsize = sizeof(Elf32_Ehdr);
1531 ehdr->e_phentsize = sizeof(Elf32_Phdr);
1533 ehdr->e_phoff = sizeof(Elf64_Ehdr);
1534 ehdr->e_ehsize = sizeof(Elf64_Ehdr);
1535 ehdr->e_phentsize = sizeof(Elf64_Phdr);
1538 if (!gelf_update_ehdr(to->elf, ehdr))
1541 if (!gelf_newphdr(to->elf, count))
1547 static int kcore__add_phdr(struct kcore *kcore, int idx, off_t offset,
1552 .p_flags = PF_R | PF_W | PF_X,
1558 .p_align = page_size,
1561 if (!gelf_update_phdr(kcore->elf, idx, &phdr))
1567 static off_t kcore__write(struct kcore *kcore)
1569 return elf_update(kcore->elf, ELF_C_WRITE);
1577 struct list_head node;
1578 struct phdr_data *remaps;
1583 struct list_head node;
1586 struct kcore_copy_info {
1592 u64 first_module_symbol;
1593 u64 last_module_symbol;
1595 struct list_head phdrs;
1596 struct list_head syms;
1599 #define kcore_copy__for_each_phdr(k, p) \
1600 list_for_each_entry((p), &(k)->phdrs, node)
1602 static struct phdr_data *phdr_data__new(u64 addr, u64 len, off_t offset)
1604 struct phdr_data *p = zalloc(sizeof(*p));
1615 static struct phdr_data *kcore_copy_info__addnew(struct kcore_copy_info *kci,
1619 struct phdr_data *p = phdr_data__new(addr, len, offset);
1622 list_add_tail(&p->node, &kci->phdrs);
1627 static void kcore_copy__free_phdrs(struct kcore_copy_info *kci)
1629 struct phdr_data *p, *tmp;
1631 list_for_each_entry_safe(p, tmp, &kci->phdrs, node) {
1632 list_del_init(&p->node);
1637 static struct sym_data *kcore_copy__new_sym(struct kcore_copy_info *kci,
1640 struct sym_data *s = zalloc(sizeof(*s));
1644 list_add_tail(&s->node, &kci->syms);
1650 static void kcore_copy__free_syms(struct kcore_copy_info *kci)
1652 struct sym_data *s, *tmp;
1654 list_for_each_entry_safe(s, tmp, &kci->syms, node) {
1655 list_del_init(&s->node);
1660 static int kcore_copy__process_kallsyms(void *arg, const char *name, char type,
1663 struct kcore_copy_info *kci = arg;
1665 if (!kallsyms__is_function(type))
1668 if (strchr(name, '[')) {
1669 if (!kci->first_module_symbol || start < kci->first_module_symbol)
1670 kci->first_module_symbol = start;
1671 if (start > kci->last_module_symbol)
1672 kci->last_module_symbol = start;
1676 if (!kci->first_symbol || start < kci->first_symbol)
1677 kci->first_symbol = start;
1679 if (!kci->last_symbol || start > kci->last_symbol)
1680 kci->last_symbol = start;
1682 if (!strcmp(name, "_stext")) {
1687 if (!strcmp(name, "_etext")) {
1692 if (is_entry_trampoline(name) && !kcore_copy__new_sym(kci, start))
1698 static int kcore_copy__parse_kallsyms(struct kcore_copy_info *kci,
1701 char kallsyms_filename[PATH_MAX];
1703 scnprintf(kallsyms_filename, PATH_MAX, "%s/kallsyms", dir);
1705 if (symbol__restricted_filename(kallsyms_filename, "/proc/kallsyms"))
1708 if (kallsyms__parse(kallsyms_filename, kci,
1709 kcore_copy__process_kallsyms) < 0)
1715 static int kcore_copy__process_modules(void *arg,
1716 const char *name __maybe_unused,
1717 u64 start, u64 size __maybe_unused)
1719 struct kcore_copy_info *kci = arg;
1721 if (!kci->first_module || start < kci->first_module)
1722 kci->first_module = start;
1727 static int kcore_copy__parse_modules(struct kcore_copy_info *kci,
1730 char modules_filename[PATH_MAX];
1732 scnprintf(modules_filename, PATH_MAX, "%s/modules", dir);
1734 if (symbol__restricted_filename(modules_filename, "/proc/modules"))
1737 if (modules__parse(modules_filename, kci,
1738 kcore_copy__process_modules) < 0)
1744 static int kcore_copy__map(struct kcore_copy_info *kci, u64 start, u64 end,
1745 u64 pgoff, u64 s, u64 e)
1749 if (s < start || s >= end)
1752 offset = (s - start) + pgoff;
1753 len = e < end ? e - s : end - s;
1755 return kcore_copy_info__addnew(kci, s, len, offset) ? 0 : -1;
1758 static int kcore_copy__read_map(u64 start, u64 len, u64 pgoff, void *data)
1760 struct kcore_copy_info *kci = data;
1761 u64 end = start + len;
1762 struct sym_data *sdat;
1764 if (kcore_copy__map(kci, start, end, pgoff, kci->stext, kci->etext))
1767 if (kcore_copy__map(kci, start, end, pgoff, kci->first_module,
1768 kci->last_module_symbol))
1771 list_for_each_entry(sdat, &kci->syms, node) {
1772 u64 s = round_down(sdat->addr, page_size);
1774 if (kcore_copy__map(kci, start, end, pgoff, s, s + len))
1781 static int kcore_copy__read_maps(struct kcore_copy_info *kci, Elf *elf)
1783 if (elf_read_maps(elf, true, kcore_copy__read_map, kci) < 0)
1789 static void kcore_copy__find_remaps(struct kcore_copy_info *kci)
1791 struct phdr_data *p, *k = NULL;
1797 /* Find phdr that corresponds to the kernel map (contains stext) */
1798 kcore_copy__for_each_phdr(kci, p) {
1799 u64 pend = p->addr + p->len - 1;
1801 if (p->addr <= kci->stext && pend >= kci->stext) {
1810 kend = k->offset + k->len;
1812 /* Find phdrs that remap the kernel */
1813 kcore_copy__for_each_phdr(kci, p) {
1814 u64 pend = p->offset + p->len;
1819 if (p->offset >= k->offset && pend <= kend)
1824 static void kcore_copy__layout(struct kcore_copy_info *kci)
1826 struct phdr_data *p;
1829 kcore_copy__find_remaps(kci);
1831 kcore_copy__for_each_phdr(kci, p) {
1839 kcore_copy__for_each_phdr(kci, p) {
1840 struct phdr_data *k = p->remaps;
1843 p->rel = p->offset - k->offset + k->rel;
1847 static int kcore_copy__calc_maps(struct kcore_copy_info *kci, const char *dir,
1850 if (kcore_copy__parse_kallsyms(kci, dir))
1853 if (kcore_copy__parse_modules(kci, dir))
1857 kci->stext = round_down(kci->stext, page_size);
1859 kci->stext = round_down(kci->first_symbol, page_size);
1862 kci->etext = round_up(kci->etext, page_size);
1863 } else if (kci->last_symbol) {
1864 kci->etext = round_up(kci->last_symbol, page_size);
1865 kci->etext += page_size;
1868 if (kci->first_module_symbol &&
1869 (!kci->first_module || kci->first_module_symbol < kci->first_module))
1870 kci->first_module = kci->first_module_symbol;
1872 kci->first_module = round_down(kci->first_module, page_size);
1874 if (kci->last_module_symbol) {
1875 kci->last_module_symbol = round_up(kci->last_module_symbol,
1877 kci->last_module_symbol += page_size;
1880 if (!kci->stext || !kci->etext)
1883 if (kci->first_module && !kci->last_module_symbol)
1886 if (kcore_copy__read_maps(kci, elf))
1889 kcore_copy__layout(kci);
1894 static int kcore_copy__copy_file(const char *from_dir, const char *to_dir,
1897 char from_filename[PATH_MAX];
1898 char to_filename[PATH_MAX];
1900 scnprintf(from_filename, PATH_MAX, "%s/%s", from_dir, name);
1901 scnprintf(to_filename, PATH_MAX, "%s/%s", to_dir, name);
1903 return copyfile_mode(from_filename, to_filename, 0400);
1906 static int kcore_copy__unlink(const char *dir, const char *name)
1908 char filename[PATH_MAX];
1910 scnprintf(filename, PATH_MAX, "%s/%s", dir, name);
1912 return unlink(filename);
1915 static int kcore_copy__compare_fds(int from, int to)
1923 buf_from = malloc(page_size);
1924 buf_to = malloc(page_size);
1925 if (!buf_from || !buf_to)
1929 /* Use read because mmap won't work on proc files */
1930 ret = read(from, buf_from, page_size);
1939 if (readn(to, buf_to, len) != (int)len)
1942 if (memcmp(buf_from, buf_to, len))
1953 static int kcore_copy__compare_files(const char *from_filename,
1954 const char *to_filename)
1956 int from, to, err = -1;
1958 from = open(from_filename, O_RDONLY);
1962 to = open(to_filename, O_RDONLY);
1964 goto out_close_from;
1966 err = kcore_copy__compare_fds(from, to);
1974 static int kcore_copy__compare_file(const char *from_dir, const char *to_dir,
1977 char from_filename[PATH_MAX];
1978 char to_filename[PATH_MAX];
1980 scnprintf(from_filename, PATH_MAX, "%s/%s", from_dir, name);
1981 scnprintf(to_filename, PATH_MAX, "%s/%s", to_dir, name);
1983 return kcore_copy__compare_files(from_filename, to_filename);
1987 * kcore_copy - copy kallsyms, modules and kcore from one directory to another.
1988 * @from_dir: from directory
1989 * @to_dir: to directory
1991 * This function copies kallsyms, modules and kcore files from one directory to
1992 * another. kallsyms and modules are copied entirely. Only code segments are
1993 * copied from kcore. It is assumed that two segments suffice: one for the
1994 * kernel proper and one for all the modules. The code segments are determined
1995 * from kallsyms and modules files. The kernel map starts at _stext or the
1996 * lowest function symbol, and ends at _etext or the highest function symbol.
1997 * The module map starts at the lowest module address and ends at the highest
1998 * module symbol. Start addresses are rounded down to the nearest page. End
1999 * addresses are rounded up to the nearest page. An extra page is added to the
2000 * highest kernel symbol and highest module symbol to, hopefully, encompass that
2001 * symbol too. Because it contains only code sections, the resulting kcore is
2002 * unusual. One significant peculiarity is that the mapping (start -> pgoff)
2003 * is not the same for the kernel map and the modules map. That happens because
2004 * the data is copied adjacently whereas the original kcore has gaps. Finally,
2005 * kallsyms file is compared with its copy to check that modules have not been
2006 * loaded or unloaded while the copies were taking place.
2008 * Return: %0 on success, %-1 on failure.
2010 int kcore_copy(const char *from_dir, const char *to_dir)
2013 struct kcore extract;
2014 int idx = 0, err = -1;
2016 struct kcore_copy_info kci = { .stext = 0, };
2017 char kcore_filename[PATH_MAX];
2018 char extract_filename[PATH_MAX];
2019 struct phdr_data *p;
2021 INIT_LIST_HEAD(&kci.phdrs);
2022 INIT_LIST_HEAD(&kci.syms);
2024 if (kcore_copy__copy_file(from_dir, to_dir, "kallsyms"))
2027 if (kcore_copy__copy_file(from_dir, to_dir, "modules"))
2028 goto out_unlink_kallsyms;
2030 scnprintf(kcore_filename, PATH_MAX, "%s/kcore", from_dir);
2031 scnprintf(extract_filename, PATH_MAX, "%s/kcore", to_dir);
2033 if (kcore__open(&kcore, kcore_filename))
2034 goto out_unlink_modules;
2036 if (kcore_copy__calc_maps(&kci, from_dir, kcore.elf))
2037 goto out_kcore_close;
2039 if (kcore__init(&extract, extract_filename, kcore.elfclass, false))
2040 goto out_kcore_close;
2042 if (kcore__copy_hdr(&kcore, &extract, kci.phnum))
2043 goto out_extract_close;
2045 offset = gelf_fsize(extract.elf, ELF_T_EHDR, 1, EV_CURRENT) +
2046 gelf_fsize(extract.elf, ELF_T_PHDR, kci.phnum, EV_CURRENT);
2047 offset = round_up(offset, page_size);
2049 kcore_copy__for_each_phdr(&kci, p) {
2050 off_t offs = p->rel + offset;
2052 if (kcore__add_phdr(&extract, idx++, offs, p->addr, p->len))
2053 goto out_extract_close;
2056 sz = kcore__write(&extract);
2057 if (sz < 0 || sz > offset)
2058 goto out_extract_close;
2060 kcore_copy__for_each_phdr(&kci, p) {
2061 off_t offs = p->rel + offset;
2065 if (copy_bytes(kcore.fd, p->offset, extract.fd, offs, p->len))
2066 goto out_extract_close;
2069 if (kcore_copy__compare_file(from_dir, to_dir, "kallsyms"))
2070 goto out_extract_close;
2075 kcore__close(&extract);
2077 unlink(extract_filename);
2079 kcore__close(&kcore);
2082 kcore_copy__unlink(to_dir, "modules");
2083 out_unlink_kallsyms:
2085 kcore_copy__unlink(to_dir, "kallsyms");
2087 kcore_copy__free_phdrs(&kci);
2088 kcore_copy__free_syms(&kci);
2093 int kcore_extract__create(struct kcore_extract *kce)
2096 struct kcore extract;
2098 int idx = 0, err = -1;
2099 off_t offset = page_size, sz;
2101 if (kcore__open(&kcore, kce->kcore_filename))
2104 strcpy(kce->extract_filename, PERF_KCORE_EXTRACT);
2105 if (kcore__init(&extract, kce->extract_filename, kcore.elfclass, true))
2106 goto out_kcore_close;
2108 if (kcore__copy_hdr(&kcore, &extract, count))
2109 goto out_extract_close;
2111 if (kcore__add_phdr(&extract, idx, offset, kce->addr, kce->len))
2112 goto out_extract_close;
2114 sz = kcore__write(&extract);
2115 if (sz < 0 || sz > offset)
2116 goto out_extract_close;
2118 if (copy_bytes(kcore.fd, kce->offs, extract.fd, offset, kce->len))
2119 goto out_extract_close;
2124 kcore__close(&extract);
2126 unlink(kce->extract_filename);
2128 kcore__close(&kcore);
2133 void kcore_extract__delete(struct kcore_extract *kce)
2135 unlink(kce->extract_filename);
2138 #ifdef HAVE_GELF_GETNOTE_SUPPORT
2140 static void sdt_adjust_loc(struct sdt_note *tmp, GElf_Addr base_off)
2146 tmp->addr.a32[SDT_NOTE_IDX_LOC] =
2147 tmp->addr.a32[SDT_NOTE_IDX_LOC] + base_off -
2148 tmp->addr.a32[SDT_NOTE_IDX_BASE];
2150 tmp->addr.a64[SDT_NOTE_IDX_LOC] =
2151 tmp->addr.a64[SDT_NOTE_IDX_LOC] + base_off -
2152 tmp->addr.a64[SDT_NOTE_IDX_BASE];
2155 static void sdt_adjust_refctr(struct sdt_note *tmp, GElf_Addr base_addr,
2161 if (tmp->bit32 && tmp->addr.a32[SDT_NOTE_IDX_REFCTR])
2162 tmp->addr.a32[SDT_NOTE_IDX_REFCTR] -= (base_addr - base_off);
2163 else if (tmp->addr.a64[SDT_NOTE_IDX_REFCTR])
2164 tmp->addr.a64[SDT_NOTE_IDX_REFCTR] -= (base_addr - base_off);
2168 * populate_sdt_note : Parse raw data and identify SDT note
2169 * @elf: elf of the opened file
2170 * @data: raw data of a section with description offset applied
2171 * @len: note description size
2172 * @type: type of the note
2173 * @sdt_notes: List to add the SDT note
2175 * Responsible for parsing the @data in section .note.stapsdt in @elf and
2176 * if its an SDT note, it appends to @sdt_notes list.
2178 static int populate_sdt_note(Elf **elf, const char *data, size_t len,
2179 struct list_head *sdt_notes)
2181 const char *provider, *name, *args;
2182 struct sdt_note *tmp = NULL;
2188 Elf64_Addr a64[NR_ADDR];
2189 Elf32_Addr a32[NR_ADDR];
2193 .d_buf = &buf, .d_type = ELF_T_ADDR, .d_version = EV_CURRENT,
2194 .d_size = gelf_fsize((*elf), ELF_T_ADDR, NR_ADDR, EV_CURRENT),
2195 .d_off = 0, .d_align = 0
2198 .d_buf = (void *) data, .d_type = ELF_T_ADDR,
2199 .d_version = EV_CURRENT, .d_size = dst.d_size, .d_off = 0,
2203 tmp = (struct sdt_note *)calloc(1, sizeof(struct sdt_note));
2209 INIT_LIST_HEAD(&tmp->note_list);
2211 if (len < dst.d_size + 3)
2214 /* Translation from file representation to memory representation */
2215 if (gelf_xlatetom(*elf, &dst, &src,
2216 elf_getident(*elf, NULL)[EI_DATA]) == NULL) {
2217 pr_err("gelf_xlatetom : %s\n", elf_errmsg(-1));
2221 /* Populate the fields of sdt_note */
2222 provider = data + dst.d_size;
2224 name = (const char *)memchr(provider, '\0', data + len - provider);
2228 tmp->provider = strdup(provider);
2229 if (!tmp->provider) {
2233 tmp->name = strdup(name);
2239 args = memchr(name, '\0', data + len - name);
2242 * There is no argument if:
2243 * - We reached the end of the note;
2244 * - There is not enough room to hold a potential string;
2245 * - The argument string is empty or just contains ':'.
2247 if (args == NULL || data + len - args < 2 ||
2248 args[1] == ':' || args[1] == '\0')
2251 tmp->args = strdup(++args);
2258 if (gelf_getclass(*elf) == ELFCLASS32) {
2259 memcpy(&tmp->addr, &buf, 3 * sizeof(Elf32_Addr));
2262 memcpy(&tmp->addr, &buf, 3 * sizeof(Elf64_Addr));
2266 if (!gelf_getehdr(*elf, &ehdr)) {
2267 pr_debug("%s : cannot get elf header.\n", __func__);
2272 /* Adjust the prelink effect :
2273 * Find out the .stapsdt.base section.
2274 * This scn will help us to handle prelinking (if present).
2275 * Compare the retrieved file offset of the base section with the
2276 * base address in the description of the SDT note. If its different,
2277 * then accordingly, adjust the note location.
2279 if (elf_section_by_name(*elf, &ehdr, &shdr, SDT_BASE_SCN, NULL))
2280 sdt_adjust_loc(tmp, shdr.sh_offset);
2282 /* Adjust reference counter offset */
2283 if (elf_section_by_name(*elf, &ehdr, &shdr, SDT_PROBES_SCN, NULL))
2284 sdt_adjust_refctr(tmp, shdr.sh_addr, shdr.sh_offset);
2286 list_add_tail(&tmp->note_list, sdt_notes);
2294 zfree(&tmp->provider);
2302 * construct_sdt_notes_list : constructs a list of SDT notes
2303 * @elf : elf to look into
2304 * @sdt_notes : empty list_head
2306 * Scans the sections in 'elf' for the section
2307 * .note.stapsdt. It, then calls populate_sdt_note to find
2308 * out the SDT events and populates the 'sdt_notes'.
2310 static int construct_sdt_notes_list(Elf *elf, struct list_head *sdt_notes)
2313 Elf_Scn *scn = NULL;
2316 size_t shstrndx, next;
2318 size_t name_off, desc_off, offset;
2321 if (gelf_getehdr(elf, &ehdr) == NULL) {
2325 if (elf_getshdrstrndx(elf, &shstrndx) != 0) {
2330 /* Look for the required section */
2331 scn = elf_section_by_name(elf, &ehdr, &shdr, SDT_NOTE_SCN, NULL);
2337 if ((shdr.sh_type != SHT_NOTE) || (shdr.sh_flags & SHF_ALLOC)) {
2342 data = elf_getdata(scn, NULL);
2344 /* Get the SDT notes */
2345 for (offset = 0; (next = gelf_getnote(data, offset, &nhdr, &name_off,
2346 &desc_off)) > 0; offset = next) {
2347 if (nhdr.n_namesz == sizeof(SDT_NOTE_NAME) &&
2348 !memcmp(data->d_buf + name_off, SDT_NOTE_NAME,
2349 sizeof(SDT_NOTE_NAME))) {
2350 /* Check the type of the note */
2351 if (nhdr.n_type != SDT_NOTE_TYPE)
2354 ret = populate_sdt_note(&elf, ((data->d_buf) + desc_off),
2355 nhdr.n_descsz, sdt_notes);
2360 if (list_empty(sdt_notes))
2368 * get_sdt_note_list : Wrapper to construct a list of sdt notes
2369 * @head : empty list_head
2370 * @target : file to find SDT notes from
2372 * This opens the file, initializes
2373 * the ELF and then calls construct_sdt_notes_list.
2375 int get_sdt_note_list(struct list_head *head, const char *target)
2380 fd = open(target, O_RDONLY);
2384 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
2389 ret = construct_sdt_notes_list(elf, head);
2397 * cleanup_sdt_note_list : free the sdt notes' list
2398 * @sdt_notes: sdt notes' list
2400 * Free up the SDT notes in @sdt_notes.
2401 * Returns the number of SDT notes free'd.
2403 int cleanup_sdt_note_list(struct list_head *sdt_notes)
2405 struct sdt_note *tmp, *pos;
2408 list_for_each_entry_safe(pos, tmp, sdt_notes, note_list) {
2409 list_del_init(&pos->note_list);
2412 zfree(&pos->provider);
2420 * sdt_notes__get_count: Counts the number of sdt events
2421 * @start: list_head to sdt_notes list
2423 * Returns the number of SDT notes in a list
2425 int sdt_notes__get_count(struct list_head *start)
2427 struct sdt_note *sdt_ptr;
2430 list_for_each_entry(sdt_ptr, start, note_list)
2436 void symbol__elf_init(void)
2438 elf_version(EV_CURRENT);