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 bool want_demangle(bool is_kernel_sym)
237 return is_kernel_sym ? symbol_conf.demangle_kernel : symbol_conf.demangle;
240 static char *demangle_sym(struct dso *dso, int kmodule, const char *elf_name)
242 int demangle_flags = verbose > 0 ? (DMGL_PARAMS | DMGL_ANSI) : DMGL_NO_OPTS;
243 char *demangled = NULL;
246 * We need to figure out if the object was created from C++ sources
247 * DWARF DW_compile_unit has this, but we don't always have access
250 if (!want_demangle(dso->kernel || kmodule))
253 demangled = bfd_demangle(NULL, elf_name, demangle_flags);
254 if (demangled == NULL)
255 demangled = java_demangle_sym(elf_name, JAVA_DEMANGLE_NORET);
256 else if (rust_is_mangled(demangled))
258 * Input to Rust demangling is the BFD-demangled
259 * name which it Rust-demangles in place.
261 rust_demangle_sym(demangled);
266 #define elf_section__for_each_rel(reldata, pos, pos_mem, idx, nr_entries) \
267 for (idx = 0, pos = gelf_getrel(reldata, 0, &pos_mem); \
269 ++idx, pos = gelf_getrel(reldata, idx, &pos_mem))
271 #define elf_section__for_each_rela(reldata, pos, pos_mem, idx, nr_entries) \
272 for (idx = 0, pos = gelf_getrela(reldata, 0, &pos_mem); \
274 ++idx, pos = gelf_getrela(reldata, idx, &pos_mem))
277 * We need to check if we have a .dynsym, so that we can handle the
278 * .plt, synthesizing its symbols, that aren't on the symtabs (be it
279 * .dynsym or .symtab).
280 * And always look at the original dso, not at debuginfo packages, that
281 * have the PLT data stripped out (shdr_rel_plt.sh_type == SHT_NOBITS).
283 int dso__synthesize_plt_symbols(struct dso *dso, struct symsrc *ss)
285 uint32_t nr_rel_entries, idx;
287 u64 plt_offset, plt_header_size, plt_entry_size;
290 GElf_Shdr shdr_rel_plt, shdr_dynsym;
291 Elf_Data *reldata, *syms, *symstrs;
292 Elf_Scn *scn_plt_rel, *scn_symstrs, *scn_dynsym;
295 char sympltname[1024];
297 int nr = 0, symidx, err = 0;
305 scn_dynsym = ss->dynsym;
306 shdr_dynsym = ss->dynshdr;
307 dynsym_idx = ss->dynsym_idx;
309 if (scn_dynsym == NULL)
312 scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt,
314 if (scn_plt_rel == NULL) {
315 scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt,
317 if (scn_plt_rel == NULL)
323 if (shdr_rel_plt.sh_link != dynsym_idx)
326 if (elf_section_by_name(elf, &ehdr, &shdr_plt, ".plt", NULL) == NULL)
330 * Fetch the relocation section to find the idxes to the GOT
331 * and the symbols in the .dynsym they refer to.
333 reldata = elf_getdata(scn_plt_rel, NULL);
337 syms = elf_getdata(scn_dynsym, NULL);
341 scn_symstrs = elf_getscn(elf, shdr_dynsym.sh_link);
342 if (scn_symstrs == NULL)
345 symstrs = elf_getdata(scn_symstrs, NULL);
349 if (symstrs->d_size == 0)
352 nr_rel_entries = shdr_rel_plt.sh_size / shdr_rel_plt.sh_entsize;
353 plt_offset = shdr_plt.sh_offset;
354 switch (ehdr.e_machine) {
356 plt_header_size = 20;
361 plt_header_size = 32;
366 plt_header_size = 48;
371 plt_header_size = 128;
375 default: /* FIXME: s390/alpha/mips/parisc/poperpc/sh/xtensa need to be checked */
376 plt_header_size = shdr_plt.sh_entsize;
377 plt_entry_size = shdr_plt.sh_entsize;
380 plt_offset += plt_header_size;
382 if (shdr_rel_plt.sh_type == SHT_RELA) {
383 GElf_Rela pos_mem, *pos;
385 elf_section__for_each_rela(reldata, pos, pos_mem, idx,
387 const char *elf_name = NULL;
388 char *demangled = NULL;
389 symidx = GELF_R_SYM(pos->r_info);
390 gelf_getsym(syms, symidx, &sym);
392 elf_name = elf_sym__name(&sym, symstrs);
393 demangled = demangle_sym(dso, 0, elf_name);
394 if (demangled != NULL)
395 elf_name = demangled;
396 snprintf(sympltname, sizeof(sympltname),
400 f = symbol__new(plt_offset, plt_entry_size,
401 STB_GLOBAL, STT_FUNC, sympltname);
405 plt_offset += plt_entry_size;
406 symbols__insert(&dso->symbols, f);
409 } else if (shdr_rel_plt.sh_type == SHT_REL) {
410 GElf_Rel pos_mem, *pos;
411 elf_section__for_each_rel(reldata, pos, pos_mem, idx,
413 const char *elf_name = NULL;
414 char *demangled = NULL;
415 symidx = GELF_R_SYM(pos->r_info);
416 gelf_getsym(syms, symidx, &sym);
418 elf_name = elf_sym__name(&sym, symstrs);
419 demangled = demangle_sym(dso, 0, elf_name);
420 if (demangled != NULL)
421 elf_name = demangled;
422 snprintf(sympltname, sizeof(sympltname),
426 f = symbol__new(plt_offset, plt_entry_size,
427 STB_GLOBAL, STT_FUNC, sympltname);
431 plt_offset += plt_entry_size;
432 symbols__insert(&dso->symbols, f);
441 pr_debug("%s: problems reading %s PLT info.\n",
442 __func__, dso->long_name);
446 char *dso__demangle_sym(struct dso *dso, int kmodule, const char *elf_name)
448 return demangle_sym(dso, kmodule, elf_name);
452 * Align offset to 4 bytes as needed for note name and descriptor data.
454 #define NOTE_ALIGN(n) (((n) + 3) & -4U)
456 static int elf_read_build_id(Elf *elf, void *bf, size_t size)
466 if (size < BUILD_ID_SIZE)
473 if (gelf_getehdr(elf, &ehdr) == NULL) {
474 pr_err("%s: cannot get elf header.\n", __func__);
479 * Check following sections for notes:
480 * '.note.gnu.build-id'
482 * '.note' (VDSO specific)
485 sec = elf_section_by_name(elf, &ehdr, &shdr,
486 ".note.gnu.build-id", NULL);
490 sec = elf_section_by_name(elf, &ehdr, &shdr,
495 sec = elf_section_by_name(elf, &ehdr, &shdr,
504 data = elf_getdata(sec, NULL);
509 while (ptr < (data->d_buf + data->d_size)) {
510 GElf_Nhdr *nhdr = ptr;
511 size_t namesz = NOTE_ALIGN(nhdr->n_namesz),
512 descsz = NOTE_ALIGN(nhdr->n_descsz);
515 ptr += sizeof(*nhdr);
518 if (nhdr->n_type == NT_GNU_BUILD_ID &&
519 nhdr->n_namesz == sizeof("GNU")) {
520 if (memcmp(name, "GNU", sizeof("GNU")) == 0) {
521 size_t sz = min(size, descsz);
523 memset(bf + sz, 0, size - sz);
535 #ifdef HAVE_LIBBFD_BUILDID_SUPPORT
537 static int read_build_id(const char *filename, struct build_id *bid)
539 size_t size = sizeof(bid->data);
543 abfd = bfd_openr(filename, NULL);
547 if (!bfd_check_format(abfd, bfd_object)) {
548 pr_debug2("%s: cannot read %s bfd file.\n", __func__, filename);
552 if (!abfd->build_id || abfd->build_id->size > size)
555 memcpy(bid->data, abfd->build_id->data, abfd->build_id->size);
556 memset(bid->data + abfd->build_id->size, 0, size - abfd->build_id->size);
557 err = bid->size = abfd->build_id->size;
564 #else // HAVE_LIBBFD_BUILDID_SUPPORT
566 static int read_build_id(const char *filename, struct build_id *bid)
568 size_t size = sizeof(bid->data);
572 if (size < BUILD_ID_SIZE)
575 fd = open(filename, O_RDONLY);
579 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
581 pr_debug2("%s: cannot read %s ELF file.\n", __func__, filename);
585 err = elf_read_build_id(elf, bid->data, size);
596 #endif // HAVE_LIBBFD_BUILDID_SUPPORT
598 int filename__read_build_id(const char *filename, struct build_id *bid)
600 struct kmod_path m = { .name = NULL, };
607 err = kmod_path__parse(&m, filename);
614 fd = filename__decompress(filename, path, sizeof(path), m.comp, &error);
616 pr_debug("Failed to decompress (error %d) %s\n",
624 err = read_build_id(filename, bid);
631 int sysfs__read_build_id(const char *filename, struct build_id *bid)
633 size_t size = sizeof(bid->data);
636 fd = open(filename, O_RDONLY);
643 size_t namesz, descsz;
645 if (read(fd, &nhdr, sizeof(nhdr)) != sizeof(nhdr))
648 namesz = NOTE_ALIGN(nhdr.n_namesz);
649 descsz = NOTE_ALIGN(nhdr.n_descsz);
650 if (nhdr.n_type == NT_GNU_BUILD_ID &&
651 nhdr.n_namesz == sizeof("GNU")) {
652 if (read(fd, bf, namesz) != (ssize_t)namesz)
654 if (memcmp(bf, "GNU", sizeof("GNU")) == 0) {
655 size_t sz = min(descsz, size);
656 if (read(fd, bid->data, sz) == (ssize_t)sz) {
657 memset(bid->data + sz, 0, size - sz);
662 } else if (read(fd, bf, descsz) != (ssize_t)descsz)
665 int n = namesz + descsz;
667 if (n > (int)sizeof(bf)) {
669 pr_debug("%s: truncating reading of build id in sysfs file %s: n_namesz=%u, n_descsz=%u.\n",
670 __func__, filename, nhdr.n_namesz, nhdr.n_descsz);
672 if (read(fd, bf, n) != n)
681 #ifdef HAVE_LIBBFD_SUPPORT
683 int filename__read_debuglink(const char *filename, char *debuglink,
690 abfd = bfd_openr(filename, NULL);
694 if (!bfd_check_format(abfd, bfd_object)) {
695 pr_debug2("%s: cannot read %s bfd file.\n", __func__, filename);
699 section = bfd_get_section_by_name(abfd, ".gnu_debuglink");
703 if (section->size > size)
706 if (!bfd_get_section_contents(abfd, section, debuglink, 0,
719 int filename__read_debuglink(const char *filename, char *debuglink,
730 fd = open(filename, O_RDONLY);
734 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
736 pr_debug2("%s: cannot read %s ELF file.\n", __func__, filename);
744 if (gelf_getehdr(elf, &ehdr) == NULL) {
745 pr_err("%s: cannot get elf header.\n", __func__);
749 sec = elf_section_by_name(elf, &ehdr, &shdr,
750 ".gnu_debuglink", NULL);
754 data = elf_getdata(sec, NULL);
758 /* the start of this section is a zero-terminated string */
759 strncpy(debuglink, data->d_buf, size);
773 static int dso__swap_init(struct dso *dso, unsigned char eidata)
775 static unsigned int const endian = 1;
777 dso->needs_swap = DSO_SWAP__NO;
781 /* We are big endian, DSO is little endian. */
782 if (*(unsigned char const *)&endian != 1)
783 dso->needs_swap = DSO_SWAP__YES;
787 /* We are little endian, DSO is big endian. */
788 if (*(unsigned char const *)&endian != 0)
789 dso->needs_swap = DSO_SWAP__YES;
793 pr_err("unrecognized DSO data encoding %d\n", eidata);
800 bool symsrc__possibly_runtime(struct symsrc *ss)
802 return ss->dynsym || ss->opdsec;
805 bool symsrc__has_symtab(struct symsrc *ss)
807 return ss->symtab != NULL;
810 void symsrc__destroy(struct symsrc *ss)
817 bool elf__needs_adjust_symbols(GElf_Ehdr ehdr)
820 * Usually vmlinux is an ELF file with type ET_EXEC for most
821 * architectures; except Arm64 kernel is linked with option
822 * '-share', so need to check type ET_DYN.
824 return ehdr.e_type == ET_EXEC || ehdr.e_type == ET_REL ||
825 ehdr.e_type == ET_DYN;
828 int symsrc__init(struct symsrc *ss, struct dso *dso, const char *name,
829 enum dso_binary_type type)
835 if (dso__needs_decompress(dso)) {
836 fd = dso__decompress_kmodule_fd(dso, name);
840 type = dso->symtab_type;
842 fd = open(name, O_RDONLY);
844 dso->load_errno = errno;
849 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
851 pr_debug("%s: cannot read %s ELF file.\n", __func__, name);
852 dso->load_errno = DSO_LOAD_ERRNO__INVALID_ELF;
856 if (gelf_getehdr(elf, &ehdr) == NULL) {
857 dso->load_errno = DSO_LOAD_ERRNO__INVALID_ELF;
858 pr_debug("%s: cannot get elf header.\n", __func__);
862 if (dso__swap_init(dso, ehdr.e_ident[EI_DATA])) {
863 dso->load_errno = DSO_LOAD_ERRNO__INTERNAL_ERROR;
867 /* Always reject images with a mismatched build-id: */
868 if (dso->has_build_id && !symbol_conf.ignore_vmlinux_buildid) {
869 u8 build_id[BUILD_ID_SIZE];
873 size = elf_read_build_id(elf, build_id, BUILD_ID_SIZE);
875 dso->load_errno = DSO_LOAD_ERRNO__CANNOT_READ_BUILDID;
879 build_id__init(&bid, build_id, size);
880 if (!dso__build_id_equal(dso, &bid)) {
881 pr_debug("%s: build id mismatch for %s.\n", __func__, name);
882 dso->load_errno = DSO_LOAD_ERRNO__MISMATCHING_BUILDID;
887 ss->is_64_bit = (gelf_getclass(elf) == ELFCLASS64);
889 ss->symtab = elf_section_by_name(elf, &ehdr, &ss->symshdr, ".symtab",
891 if (ss->symshdr.sh_type != SHT_SYMTAB)
895 ss->dynsym = elf_section_by_name(elf, &ehdr, &ss->dynshdr, ".dynsym",
897 if (ss->dynshdr.sh_type != SHT_DYNSYM)
901 ss->opdsec = elf_section_by_name(elf, &ehdr, &ss->opdshdr, ".opd",
903 if (ss->opdshdr.sh_type != SHT_PROGBITS)
906 if (dso->kernel == DSO_SPACE__USER)
907 ss->adjust_symbols = true;
909 ss->adjust_symbols = elf__needs_adjust_symbols(ehdr);
911 ss->name = strdup(name);
913 dso->load_errno = errno;
932 * ref_reloc_sym_not_found - has kernel relocation symbol been found.
933 * @kmap: kernel maps and relocation reference symbol
935 * This function returns %true if we are dealing with the kernel maps and the
936 * relocation reference symbol has not yet been found. Otherwise %false is
939 static bool ref_reloc_sym_not_found(struct kmap *kmap)
941 return kmap && kmap->ref_reloc_sym && kmap->ref_reloc_sym->name &&
942 !kmap->ref_reloc_sym->unrelocated_addr;
946 * ref_reloc - kernel relocation offset.
947 * @kmap: kernel maps and relocation reference symbol
949 * This function returns the offset of kernel addresses as determined by using
950 * the relocation reference symbol i.e. if the kernel has not been relocated
951 * then the return value is zero.
953 static u64 ref_reloc(struct kmap *kmap)
955 if (kmap && kmap->ref_reloc_sym &&
956 kmap->ref_reloc_sym->unrelocated_addr)
957 return kmap->ref_reloc_sym->addr -
958 kmap->ref_reloc_sym->unrelocated_addr;
962 void __weak arch__sym_update(struct symbol *s __maybe_unused,
963 GElf_Sym *sym __maybe_unused) { }
965 static int dso__process_kernel_symbol(struct dso *dso, struct map *map,
966 GElf_Sym *sym, GElf_Shdr *shdr,
967 struct maps *kmaps, struct kmap *kmap,
968 struct dso **curr_dsop, struct map **curr_mapp,
969 const char *section_name,
970 bool adjust_kernel_syms, bool kmodule, bool *remap_kernel)
972 struct dso *curr_dso = *curr_dsop;
973 struct map *curr_map;
974 char dso_name[PATH_MAX];
976 /* Adjust symbol to map to file offset */
977 if (adjust_kernel_syms)
978 sym->st_value -= shdr->sh_addr - shdr->sh_offset;
980 if (strcmp(section_name, (curr_dso->short_name + dso->short_name_len)) == 0)
983 if (strcmp(section_name, ".text") == 0) {
985 * The initial kernel mapping is based on
986 * kallsyms and identity maps. Overwrite it to
987 * map to the kernel dso.
989 if (*remap_kernel && dso->kernel && !kmodule) {
990 *remap_kernel = false;
991 map->start = shdr->sh_addr + ref_reloc(kmap);
992 map->end = map->start + shdr->sh_size;
993 map->pgoff = shdr->sh_offset;
994 map->map_ip = map__map_ip;
995 map->unmap_ip = map__unmap_ip;
996 /* Ensure maps are correctly ordered */
999 maps__remove(kmaps, map);
1000 maps__insert(kmaps, map);
1006 * The initial module mapping is based on
1007 * /proc/modules mapped to offset zero.
1008 * Overwrite it to map to the module dso.
1010 if (*remap_kernel && kmodule) {
1011 *remap_kernel = false;
1012 map->pgoff = shdr->sh_offset;
1023 snprintf(dso_name, sizeof(dso_name), "%s%s", dso->short_name, section_name);
1025 curr_map = maps__find_by_name(kmaps, dso_name);
1026 if (curr_map == NULL) {
1027 u64 start = sym->st_value;
1030 start += map->start + shdr->sh_offset;
1032 curr_dso = dso__new(dso_name);
1033 if (curr_dso == NULL)
1035 curr_dso->kernel = dso->kernel;
1036 curr_dso->long_name = dso->long_name;
1037 curr_dso->long_name_len = dso->long_name_len;
1038 curr_map = map__new2(start, curr_dso);
1040 if (curr_map == NULL)
1043 if (curr_dso->kernel)
1044 map__kmap(curr_map)->kmaps = kmaps;
1046 if (adjust_kernel_syms) {
1047 curr_map->start = shdr->sh_addr + ref_reloc(kmap);
1048 curr_map->end = curr_map->start + shdr->sh_size;
1049 curr_map->pgoff = shdr->sh_offset;
1051 curr_map->map_ip = curr_map->unmap_ip = identity__map_ip;
1053 curr_dso->symtab_type = dso->symtab_type;
1054 maps__insert(kmaps, curr_map);
1056 * Add it before we drop the referece to curr_map, i.e. while
1057 * we still are sure to have a reference to this DSO via
1060 dsos__add(&kmaps->machine->dsos, curr_dso);
1061 /* kmaps already got it */
1063 dso__set_loaded(curr_dso);
1064 *curr_mapp = curr_map;
1065 *curr_dsop = curr_dso;
1067 *curr_dsop = curr_map->dso;
1072 int dso__load_sym(struct dso *dso, struct map *map, struct symsrc *syms_ss,
1073 struct symsrc *runtime_ss, int kmodule)
1075 struct kmap *kmap = dso->kernel ? map__kmap(map) : NULL;
1076 struct maps *kmaps = kmap ? map__kmaps(map) : NULL;
1077 struct map *curr_map = map;
1078 struct dso *curr_dso = dso;
1079 Elf_Data *symstrs, *secstrs;
1086 Elf_Data *syms, *opddata = NULL;
1088 Elf_Scn *sec, *sec_strndx;
1091 bool remap_kernel = false, adjust_kernel_syms = false;
1096 dso->symtab_type = syms_ss->type;
1097 dso->is_64_bit = syms_ss->is_64_bit;
1098 dso->rel = syms_ss->ehdr.e_type == ET_REL;
1101 * Modules may already have symbols from kallsyms, but those symbols
1102 * have the wrong values for the dso maps, so remove them.
1104 if (kmodule && syms_ss->symtab)
1105 symbols__delete(&dso->symbols);
1107 if (!syms_ss->symtab) {
1109 * If the vmlinux is stripped, fail so we will fall back
1110 * to using kallsyms. The vmlinux runtime symbols aren't
1116 syms_ss->symtab = syms_ss->dynsym;
1117 syms_ss->symshdr = syms_ss->dynshdr;
1121 ehdr = syms_ss->ehdr;
1122 sec = syms_ss->symtab;
1123 shdr = syms_ss->symshdr;
1125 if (elf_section_by_name(runtime_ss->elf, &runtime_ss->ehdr, &tshdr,
1127 dso->text_offset = tshdr.sh_addr - tshdr.sh_offset;
1129 if (runtime_ss->opdsec)
1130 opddata = elf_rawdata(runtime_ss->opdsec, NULL);
1132 syms = elf_getdata(sec, NULL);
1136 sec = elf_getscn(elf, shdr.sh_link);
1140 symstrs = elf_getdata(sec, NULL);
1141 if (symstrs == NULL)
1144 sec_strndx = elf_getscn(runtime_ss->elf, runtime_ss->ehdr.e_shstrndx);
1145 if (sec_strndx == NULL)
1148 secstrs = elf_getdata(sec_strndx, NULL);
1149 if (secstrs == NULL)
1152 nr_syms = shdr.sh_size / shdr.sh_entsize;
1154 memset(&sym, 0, sizeof(sym));
1157 * The kernel relocation symbol is needed in advance in order to adjust
1158 * kernel maps correctly.
1160 if (ref_reloc_sym_not_found(kmap)) {
1161 elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) {
1162 const char *elf_name = elf_sym__name(&sym, symstrs);
1164 if (strcmp(elf_name, kmap->ref_reloc_sym->name))
1166 kmap->ref_reloc_sym->unrelocated_addr = sym.st_value;
1167 map->reloc = kmap->ref_reloc_sym->addr -
1168 kmap->ref_reloc_sym->unrelocated_addr;
1174 * Handle any relocation of vdso necessary because older kernels
1175 * attempted to prelink vdso to its virtual address.
1177 if (dso__is_vdso(dso))
1178 map->reloc = map->start - dso->text_offset;
1180 dso->adjust_symbols = runtime_ss->adjust_symbols || ref_reloc(kmap);
1182 * Initial kernel and module mappings do not map to the dso.
1186 remap_kernel = true;
1187 adjust_kernel_syms = dso->adjust_symbols;
1189 elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) {
1191 const char *elf_name = elf_sym__name(&sym, symstrs);
1192 char *demangled = NULL;
1193 int is_label = elf_sym__is_label(&sym);
1194 const char *section_name;
1195 bool used_opd = false;
1197 if (!is_label && !elf_sym__filter(&sym))
1200 /* Reject ARM ELF "mapping symbols": these aren't unique and
1201 * don't identify functions, so will confuse the profile
1203 if (ehdr.e_machine == EM_ARM || ehdr.e_machine == EM_AARCH64) {
1204 if (elf_name[0] == '$' && strchr("adtx", elf_name[1])
1205 && (elf_name[2] == '\0' || elf_name[2] == '.'))
1209 if (runtime_ss->opdsec && sym.st_shndx == runtime_ss->opdidx) {
1210 u32 offset = sym.st_value - syms_ss->opdshdr.sh_addr;
1211 u64 *opd = opddata->d_buf + offset;
1212 sym.st_value = DSO__SWAP(dso, u64, *opd);
1213 sym.st_shndx = elf_addr_to_index(runtime_ss->elf,
1218 * When loading symbols in a data mapping, ABS symbols (which
1219 * has a value of SHN_ABS in its st_shndx) failed at
1220 * elf_getscn(). And it marks the loading as a failure so
1221 * already loaded symbols cannot be fixed up.
1223 * I'm not sure what should be done. Just ignore them for now.
1226 if (sym.st_shndx == SHN_ABS)
1229 sec = elf_getscn(runtime_ss->elf, sym.st_shndx);
1233 gelf_getshdr(sec, &shdr);
1235 if (is_label && !elf_sec__filter(&shdr, secstrs))
1238 section_name = elf_sec__name(&shdr, secstrs);
1240 /* On ARM, symbols for thumb functions have 1 added to
1241 * the symbol address as a flag - remove it */
1242 if ((ehdr.e_machine == EM_ARM) &&
1243 (GELF_ST_TYPE(sym.st_info) == STT_FUNC) &&
1248 if (dso__process_kernel_symbol(dso, map, &sym, &shdr, kmaps, kmap, &curr_dso, &curr_map,
1249 section_name, adjust_kernel_syms, kmodule, &remap_kernel))
1251 } else if ((used_opd && runtime_ss->adjust_symbols) ||
1252 (!used_opd && syms_ss->adjust_symbols)) {
1253 pr_debug4("%s: adjusting symbol: st_value: %#" PRIx64 " "
1254 "sh_addr: %#" PRIx64 " sh_offset: %#" PRIx64 "\n", __func__,
1255 (u64)sym.st_value, (u64)shdr.sh_addr,
1256 (u64)shdr.sh_offset);
1257 sym.st_value -= shdr.sh_addr - shdr.sh_offset;
1260 demangled = demangle_sym(dso, kmodule, elf_name);
1261 if (demangled != NULL)
1262 elf_name = demangled;
1264 f = symbol__new(sym.st_value, sym.st_size,
1265 GELF_ST_BIND(sym.st_info),
1266 GELF_ST_TYPE(sym.st_info), elf_name);
1271 arch__sym_update(f, &sym);
1273 __symbols__insert(&curr_dso->symbols, f, dso->kernel);
1278 * For misannotated, zeroed, ASM function sizes.
1281 symbols__fixup_end(&dso->symbols);
1282 symbols__fixup_duplicate(&dso->symbols);
1285 * We need to fixup this here too because we create new
1286 * maps here, for things like vsyscall sections.
1288 maps__fixup_end(kmaps);
1296 static int elf_read_maps(Elf *elf, bool exe, mapfn_t mapfn, void *data)
1303 if (elf_getphdrnum(elf, &phdrnum))
1306 for (i = 0; i < phdrnum; i++) {
1307 if (gelf_getphdr(elf, i, &phdr) == NULL)
1309 if (phdr.p_type != PT_LOAD)
1312 if (!(phdr.p_flags & PF_X))
1315 if (!(phdr.p_flags & PF_R))
1318 sz = min(phdr.p_memsz, phdr.p_filesz);
1321 err = mapfn(phdr.p_vaddr, sz, phdr.p_offset, data);
1328 int file__read_maps(int fd, bool exe, mapfn_t mapfn, void *data,
1334 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
1339 *is_64_bit = (gelf_getclass(elf) == ELFCLASS64);
1341 err = elf_read_maps(elf, exe, mapfn, data);
1347 enum dso_type dso__type_fd(int fd)
1349 enum dso_type dso_type = DSO__TYPE_UNKNOWN;
1354 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
1359 if (ek != ELF_K_ELF)
1362 if (gelf_getclass(elf) == ELFCLASS64) {
1363 dso_type = DSO__TYPE_64BIT;
1367 if (gelf_getehdr(elf, &ehdr) == NULL)
1370 if (ehdr.e_machine == EM_X86_64)
1371 dso_type = DSO__TYPE_X32BIT;
1373 dso_type = DSO__TYPE_32BIT;
1380 static int copy_bytes(int from, off_t from_offs, int to, off_t to_offs, u64 len)
1385 char *buf = malloc(page_size);
1390 if (lseek(to, to_offs, SEEK_SET) != to_offs)
1393 if (lseek(from, from_offs, SEEK_SET) != from_offs)
1400 /* Use read because mmap won't work on proc files */
1401 r = read(from, buf, n);
1407 r = write(to, buf, n);
1428 static int kcore__open(struct kcore *kcore, const char *filename)
1432 kcore->fd = open(filename, O_RDONLY);
1433 if (kcore->fd == -1)
1436 kcore->elf = elf_begin(kcore->fd, ELF_C_READ, NULL);
1440 kcore->elfclass = gelf_getclass(kcore->elf);
1441 if (kcore->elfclass == ELFCLASSNONE)
1444 ehdr = gelf_getehdr(kcore->elf, &kcore->ehdr);
1451 elf_end(kcore->elf);
1457 static int kcore__init(struct kcore *kcore, char *filename, int elfclass,
1460 kcore->elfclass = elfclass;
1463 kcore->fd = mkstemp(filename);
1465 kcore->fd = open(filename, O_WRONLY | O_CREAT | O_EXCL, 0400);
1466 if (kcore->fd == -1)
1469 kcore->elf = elf_begin(kcore->fd, ELF_C_WRITE, NULL);
1473 if (!gelf_newehdr(kcore->elf, elfclass))
1476 memset(&kcore->ehdr, 0, sizeof(GElf_Ehdr));
1481 elf_end(kcore->elf);
1488 static void kcore__close(struct kcore *kcore)
1490 elf_end(kcore->elf);
1494 static int kcore__copy_hdr(struct kcore *from, struct kcore *to, size_t count)
1496 GElf_Ehdr *ehdr = &to->ehdr;
1497 GElf_Ehdr *kehdr = &from->ehdr;
1499 memcpy(ehdr->e_ident, kehdr->e_ident, EI_NIDENT);
1500 ehdr->e_type = kehdr->e_type;
1501 ehdr->e_machine = kehdr->e_machine;
1502 ehdr->e_version = kehdr->e_version;
1505 ehdr->e_flags = kehdr->e_flags;
1506 ehdr->e_phnum = count;
1507 ehdr->e_shentsize = 0;
1509 ehdr->e_shstrndx = 0;
1511 if (from->elfclass == ELFCLASS32) {
1512 ehdr->e_phoff = sizeof(Elf32_Ehdr);
1513 ehdr->e_ehsize = sizeof(Elf32_Ehdr);
1514 ehdr->e_phentsize = sizeof(Elf32_Phdr);
1516 ehdr->e_phoff = sizeof(Elf64_Ehdr);
1517 ehdr->e_ehsize = sizeof(Elf64_Ehdr);
1518 ehdr->e_phentsize = sizeof(Elf64_Phdr);
1521 if (!gelf_update_ehdr(to->elf, ehdr))
1524 if (!gelf_newphdr(to->elf, count))
1530 static int kcore__add_phdr(struct kcore *kcore, int idx, off_t offset,
1535 .p_flags = PF_R | PF_W | PF_X,
1541 .p_align = page_size,
1544 if (!gelf_update_phdr(kcore->elf, idx, &phdr))
1550 static off_t kcore__write(struct kcore *kcore)
1552 return elf_update(kcore->elf, ELF_C_WRITE);
1560 struct list_head node;
1561 struct phdr_data *remaps;
1566 struct list_head node;
1569 struct kcore_copy_info {
1575 u64 first_module_symbol;
1576 u64 last_module_symbol;
1578 struct list_head phdrs;
1579 struct list_head syms;
1582 #define kcore_copy__for_each_phdr(k, p) \
1583 list_for_each_entry((p), &(k)->phdrs, node)
1585 static struct phdr_data *phdr_data__new(u64 addr, u64 len, off_t offset)
1587 struct phdr_data *p = zalloc(sizeof(*p));
1598 static struct phdr_data *kcore_copy_info__addnew(struct kcore_copy_info *kci,
1602 struct phdr_data *p = phdr_data__new(addr, len, offset);
1605 list_add_tail(&p->node, &kci->phdrs);
1610 static void kcore_copy__free_phdrs(struct kcore_copy_info *kci)
1612 struct phdr_data *p, *tmp;
1614 list_for_each_entry_safe(p, tmp, &kci->phdrs, node) {
1615 list_del_init(&p->node);
1620 static struct sym_data *kcore_copy__new_sym(struct kcore_copy_info *kci,
1623 struct sym_data *s = zalloc(sizeof(*s));
1627 list_add_tail(&s->node, &kci->syms);
1633 static void kcore_copy__free_syms(struct kcore_copy_info *kci)
1635 struct sym_data *s, *tmp;
1637 list_for_each_entry_safe(s, tmp, &kci->syms, node) {
1638 list_del_init(&s->node);
1643 static int kcore_copy__process_kallsyms(void *arg, const char *name, char type,
1646 struct kcore_copy_info *kci = arg;
1648 if (!kallsyms__is_function(type))
1651 if (strchr(name, '[')) {
1652 if (!kci->first_module_symbol || start < kci->first_module_symbol)
1653 kci->first_module_symbol = start;
1654 if (start > kci->last_module_symbol)
1655 kci->last_module_symbol = start;
1659 if (!kci->first_symbol || start < kci->first_symbol)
1660 kci->first_symbol = start;
1662 if (!kci->last_symbol || start > kci->last_symbol)
1663 kci->last_symbol = start;
1665 if (!strcmp(name, "_stext")) {
1670 if (!strcmp(name, "_etext")) {
1675 if (is_entry_trampoline(name) && !kcore_copy__new_sym(kci, start))
1681 static int kcore_copy__parse_kallsyms(struct kcore_copy_info *kci,
1684 char kallsyms_filename[PATH_MAX];
1686 scnprintf(kallsyms_filename, PATH_MAX, "%s/kallsyms", dir);
1688 if (symbol__restricted_filename(kallsyms_filename, "/proc/kallsyms"))
1691 if (kallsyms__parse(kallsyms_filename, kci,
1692 kcore_copy__process_kallsyms) < 0)
1698 static int kcore_copy__process_modules(void *arg,
1699 const char *name __maybe_unused,
1700 u64 start, u64 size __maybe_unused)
1702 struct kcore_copy_info *kci = arg;
1704 if (!kci->first_module || start < kci->first_module)
1705 kci->first_module = start;
1710 static int kcore_copy__parse_modules(struct kcore_copy_info *kci,
1713 char modules_filename[PATH_MAX];
1715 scnprintf(modules_filename, PATH_MAX, "%s/modules", dir);
1717 if (symbol__restricted_filename(modules_filename, "/proc/modules"))
1720 if (modules__parse(modules_filename, kci,
1721 kcore_copy__process_modules) < 0)
1727 static int kcore_copy__map(struct kcore_copy_info *kci, u64 start, u64 end,
1728 u64 pgoff, u64 s, u64 e)
1732 if (s < start || s >= end)
1735 offset = (s - start) + pgoff;
1736 len = e < end ? e - s : end - s;
1738 return kcore_copy_info__addnew(kci, s, len, offset) ? 0 : -1;
1741 static int kcore_copy__read_map(u64 start, u64 len, u64 pgoff, void *data)
1743 struct kcore_copy_info *kci = data;
1744 u64 end = start + len;
1745 struct sym_data *sdat;
1747 if (kcore_copy__map(kci, start, end, pgoff, kci->stext, kci->etext))
1750 if (kcore_copy__map(kci, start, end, pgoff, kci->first_module,
1751 kci->last_module_symbol))
1754 list_for_each_entry(sdat, &kci->syms, node) {
1755 u64 s = round_down(sdat->addr, page_size);
1757 if (kcore_copy__map(kci, start, end, pgoff, s, s + len))
1764 static int kcore_copy__read_maps(struct kcore_copy_info *kci, Elf *elf)
1766 if (elf_read_maps(elf, true, kcore_copy__read_map, kci) < 0)
1772 static void kcore_copy__find_remaps(struct kcore_copy_info *kci)
1774 struct phdr_data *p, *k = NULL;
1780 /* Find phdr that corresponds to the kernel map (contains stext) */
1781 kcore_copy__for_each_phdr(kci, p) {
1782 u64 pend = p->addr + p->len - 1;
1784 if (p->addr <= kci->stext && pend >= kci->stext) {
1793 kend = k->offset + k->len;
1795 /* Find phdrs that remap the kernel */
1796 kcore_copy__for_each_phdr(kci, p) {
1797 u64 pend = p->offset + p->len;
1802 if (p->offset >= k->offset && pend <= kend)
1807 static void kcore_copy__layout(struct kcore_copy_info *kci)
1809 struct phdr_data *p;
1812 kcore_copy__find_remaps(kci);
1814 kcore_copy__for_each_phdr(kci, p) {
1822 kcore_copy__for_each_phdr(kci, p) {
1823 struct phdr_data *k = p->remaps;
1826 p->rel = p->offset - k->offset + k->rel;
1830 static int kcore_copy__calc_maps(struct kcore_copy_info *kci, const char *dir,
1833 if (kcore_copy__parse_kallsyms(kci, dir))
1836 if (kcore_copy__parse_modules(kci, dir))
1840 kci->stext = round_down(kci->stext, page_size);
1842 kci->stext = round_down(kci->first_symbol, page_size);
1845 kci->etext = round_up(kci->etext, page_size);
1846 } else if (kci->last_symbol) {
1847 kci->etext = round_up(kci->last_symbol, page_size);
1848 kci->etext += page_size;
1851 if (kci->first_module_symbol &&
1852 (!kci->first_module || kci->first_module_symbol < kci->first_module))
1853 kci->first_module = kci->first_module_symbol;
1855 kci->first_module = round_down(kci->first_module, page_size);
1857 if (kci->last_module_symbol) {
1858 kci->last_module_symbol = round_up(kci->last_module_symbol,
1860 kci->last_module_symbol += page_size;
1863 if (!kci->stext || !kci->etext)
1866 if (kci->first_module && !kci->last_module_symbol)
1869 if (kcore_copy__read_maps(kci, elf))
1872 kcore_copy__layout(kci);
1877 static int kcore_copy__copy_file(const char *from_dir, const char *to_dir,
1880 char from_filename[PATH_MAX];
1881 char to_filename[PATH_MAX];
1883 scnprintf(from_filename, PATH_MAX, "%s/%s", from_dir, name);
1884 scnprintf(to_filename, PATH_MAX, "%s/%s", to_dir, name);
1886 return copyfile_mode(from_filename, to_filename, 0400);
1889 static int kcore_copy__unlink(const char *dir, const char *name)
1891 char filename[PATH_MAX];
1893 scnprintf(filename, PATH_MAX, "%s/%s", dir, name);
1895 return unlink(filename);
1898 static int kcore_copy__compare_fds(int from, int to)
1906 buf_from = malloc(page_size);
1907 buf_to = malloc(page_size);
1908 if (!buf_from || !buf_to)
1912 /* Use read because mmap won't work on proc files */
1913 ret = read(from, buf_from, page_size);
1922 if (readn(to, buf_to, len) != (int)len)
1925 if (memcmp(buf_from, buf_to, len))
1936 static int kcore_copy__compare_files(const char *from_filename,
1937 const char *to_filename)
1939 int from, to, err = -1;
1941 from = open(from_filename, O_RDONLY);
1945 to = open(to_filename, O_RDONLY);
1947 goto out_close_from;
1949 err = kcore_copy__compare_fds(from, to);
1957 static int kcore_copy__compare_file(const char *from_dir, const char *to_dir,
1960 char from_filename[PATH_MAX];
1961 char to_filename[PATH_MAX];
1963 scnprintf(from_filename, PATH_MAX, "%s/%s", from_dir, name);
1964 scnprintf(to_filename, PATH_MAX, "%s/%s", to_dir, name);
1966 return kcore_copy__compare_files(from_filename, to_filename);
1970 * kcore_copy - copy kallsyms, modules and kcore from one directory to another.
1971 * @from_dir: from directory
1972 * @to_dir: to directory
1974 * This function copies kallsyms, modules and kcore files from one directory to
1975 * another. kallsyms and modules are copied entirely. Only code segments are
1976 * copied from kcore. It is assumed that two segments suffice: one for the
1977 * kernel proper and one for all the modules. The code segments are determined
1978 * from kallsyms and modules files. The kernel map starts at _stext or the
1979 * lowest function symbol, and ends at _etext or the highest function symbol.
1980 * The module map starts at the lowest module address and ends at the highest
1981 * module symbol. Start addresses are rounded down to the nearest page. End
1982 * addresses are rounded up to the nearest page. An extra page is added to the
1983 * highest kernel symbol and highest module symbol to, hopefully, encompass that
1984 * symbol too. Because it contains only code sections, the resulting kcore is
1985 * unusual. One significant peculiarity is that the mapping (start -> pgoff)
1986 * is not the same for the kernel map and the modules map. That happens because
1987 * the data is copied adjacently whereas the original kcore has gaps. Finally,
1988 * kallsyms and modules files are compared with their copies to check that
1989 * modules have not been loaded or unloaded while the copies were taking place.
1991 * Return: %0 on success, %-1 on failure.
1993 int kcore_copy(const char *from_dir, const char *to_dir)
1996 struct kcore extract;
1997 int idx = 0, err = -1;
1999 struct kcore_copy_info kci = { .stext = 0, };
2000 char kcore_filename[PATH_MAX];
2001 char extract_filename[PATH_MAX];
2002 struct phdr_data *p;
2004 INIT_LIST_HEAD(&kci.phdrs);
2005 INIT_LIST_HEAD(&kci.syms);
2007 if (kcore_copy__copy_file(from_dir, to_dir, "kallsyms"))
2010 if (kcore_copy__copy_file(from_dir, to_dir, "modules"))
2011 goto out_unlink_kallsyms;
2013 scnprintf(kcore_filename, PATH_MAX, "%s/kcore", from_dir);
2014 scnprintf(extract_filename, PATH_MAX, "%s/kcore", to_dir);
2016 if (kcore__open(&kcore, kcore_filename))
2017 goto out_unlink_modules;
2019 if (kcore_copy__calc_maps(&kci, from_dir, kcore.elf))
2020 goto out_kcore_close;
2022 if (kcore__init(&extract, extract_filename, kcore.elfclass, false))
2023 goto out_kcore_close;
2025 if (kcore__copy_hdr(&kcore, &extract, kci.phnum))
2026 goto out_extract_close;
2028 offset = gelf_fsize(extract.elf, ELF_T_EHDR, 1, EV_CURRENT) +
2029 gelf_fsize(extract.elf, ELF_T_PHDR, kci.phnum, EV_CURRENT);
2030 offset = round_up(offset, page_size);
2032 kcore_copy__for_each_phdr(&kci, p) {
2033 off_t offs = p->rel + offset;
2035 if (kcore__add_phdr(&extract, idx++, offs, p->addr, p->len))
2036 goto out_extract_close;
2039 sz = kcore__write(&extract);
2040 if (sz < 0 || sz > offset)
2041 goto out_extract_close;
2043 kcore_copy__for_each_phdr(&kci, p) {
2044 off_t offs = p->rel + offset;
2048 if (copy_bytes(kcore.fd, p->offset, extract.fd, offs, p->len))
2049 goto out_extract_close;
2052 if (kcore_copy__compare_file(from_dir, to_dir, "modules"))
2053 goto out_extract_close;
2055 if (kcore_copy__compare_file(from_dir, to_dir, "kallsyms"))
2056 goto out_extract_close;
2061 kcore__close(&extract);
2063 unlink(extract_filename);
2065 kcore__close(&kcore);
2068 kcore_copy__unlink(to_dir, "modules");
2069 out_unlink_kallsyms:
2071 kcore_copy__unlink(to_dir, "kallsyms");
2073 kcore_copy__free_phdrs(&kci);
2074 kcore_copy__free_syms(&kci);
2079 int kcore_extract__create(struct kcore_extract *kce)
2082 struct kcore extract;
2084 int idx = 0, err = -1;
2085 off_t offset = page_size, sz;
2087 if (kcore__open(&kcore, kce->kcore_filename))
2090 strcpy(kce->extract_filename, PERF_KCORE_EXTRACT);
2091 if (kcore__init(&extract, kce->extract_filename, kcore.elfclass, true))
2092 goto out_kcore_close;
2094 if (kcore__copy_hdr(&kcore, &extract, count))
2095 goto out_extract_close;
2097 if (kcore__add_phdr(&extract, idx, offset, kce->addr, kce->len))
2098 goto out_extract_close;
2100 sz = kcore__write(&extract);
2101 if (sz < 0 || sz > offset)
2102 goto out_extract_close;
2104 if (copy_bytes(kcore.fd, kce->offs, extract.fd, offset, kce->len))
2105 goto out_extract_close;
2110 kcore__close(&extract);
2112 unlink(kce->extract_filename);
2114 kcore__close(&kcore);
2119 void kcore_extract__delete(struct kcore_extract *kce)
2121 unlink(kce->extract_filename);
2124 #ifdef HAVE_GELF_GETNOTE_SUPPORT
2126 static void sdt_adjust_loc(struct sdt_note *tmp, GElf_Addr base_off)
2132 tmp->addr.a32[SDT_NOTE_IDX_LOC] =
2133 tmp->addr.a32[SDT_NOTE_IDX_LOC] + base_off -
2134 tmp->addr.a32[SDT_NOTE_IDX_BASE];
2136 tmp->addr.a64[SDT_NOTE_IDX_LOC] =
2137 tmp->addr.a64[SDT_NOTE_IDX_LOC] + base_off -
2138 tmp->addr.a64[SDT_NOTE_IDX_BASE];
2141 static void sdt_adjust_refctr(struct sdt_note *tmp, GElf_Addr base_addr,
2147 if (tmp->bit32 && tmp->addr.a32[SDT_NOTE_IDX_REFCTR])
2148 tmp->addr.a32[SDT_NOTE_IDX_REFCTR] -= (base_addr - base_off);
2149 else if (tmp->addr.a64[SDT_NOTE_IDX_REFCTR])
2150 tmp->addr.a64[SDT_NOTE_IDX_REFCTR] -= (base_addr - base_off);
2154 * populate_sdt_note : Parse raw data and identify SDT note
2155 * @elf: elf of the opened file
2156 * @data: raw data of a section with description offset applied
2157 * @len: note description size
2158 * @type: type of the note
2159 * @sdt_notes: List to add the SDT note
2161 * Responsible for parsing the @data in section .note.stapsdt in @elf and
2162 * if its an SDT note, it appends to @sdt_notes list.
2164 static int populate_sdt_note(Elf **elf, const char *data, size_t len,
2165 struct list_head *sdt_notes)
2167 const char *provider, *name, *args;
2168 struct sdt_note *tmp = NULL;
2174 Elf64_Addr a64[NR_ADDR];
2175 Elf32_Addr a32[NR_ADDR];
2179 .d_buf = &buf, .d_type = ELF_T_ADDR, .d_version = EV_CURRENT,
2180 .d_size = gelf_fsize((*elf), ELF_T_ADDR, NR_ADDR, EV_CURRENT),
2181 .d_off = 0, .d_align = 0
2184 .d_buf = (void *) data, .d_type = ELF_T_ADDR,
2185 .d_version = EV_CURRENT, .d_size = dst.d_size, .d_off = 0,
2189 tmp = (struct sdt_note *)calloc(1, sizeof(struct sdt_note));
2195 INIT_LIST_HEAD(&tmp->note_list);
2197 if (len < dst.d_size + 3)
2200 /* Translation from file representation to memory representation */
2201 if (gelf_xlatetom(*elf, &dst, &src,
2202 elf_getident(*elf, NULL)[EI_DATA]) == NULL) {
2203 pr_err("gelf_xlatetom : %s\n", elf_errmsg(-1));
2207 /* Populate the fields of sdt_note */
2208 provider = data + dst.d_size;
2210 name = (const char *)memchr(provider, '\0', data + len - provider);
2214 tmp->provider = strdup(provider);
2215 if (!tmp->provider) {
2219 tmp->name = strdup(name);
2225 args = memchr(name, '\0', data + len - name);
2228 * There is no argument if:
2229 * - We reached the end of the note;
2230 * - There is not enough room to hold a potential string;
2231 * - The argument string is empty or just contains ':'.
2233 if (args == NULL || data + len - args < 2 ||
2234 args[1] == ':' || args[1] == '\0')
2237 tmp->args = strdup(++args);
2244 if (gelf_getclass(*elf) == ELFCLASS32) {
2245 memcpy(&tmp->addr, &buf, 3 * sizeof(Elf32_Addr));
2248 memcpy(&tmp->addr, &buf, 3 * sizeof(Elf64_Addr));
2252 if (!gelf_getehdr(*elf, &ehdr)) {
2253 pr_debug("%s : cannot get elf header.\n", __func__);
2258 /* Adjust the prelink effect :
2259 * Find out the .stapsdt.base section.
2260 * This scn will help us to handle prelinking (if present).
2261 * Compare the retrieved file offset of the base section with the
2262 * base address in the description of the SDT note. If its different,
2263 * then accordingly, adjust the note location.
2265 if (elf_section_by_name(*elf, &ehdr, &shdr, SDT_BASE_SCN, NULL))
2266 sdt_adjust_loc(tmp, shdr.sh_offset);
2268 /* Adjust reference counter offset */
2269 if (elf_section_by_name(*elf, &ehdr, &shdr, SDT_PROBES_SCN, NULL))
2270 sdt_adjust_refctr(tmp, shdr.sh_addr, shdr.sh_offset);
2272 list_add_tail(&tmp->note_list, sdt_notes);
2280 zfree(&tmp->provider);
2288 * construct_sdt_notes_list : constructs a list of SDT notes
2289 * @elf : elf to look into
2290 * @sdt_notes : empty list_head
2292 * Scans the sections in 'elf' for the section
2293 * .note.stapsdt. It, then calls populate_sdt_note to find
2294 * out the SDT events and populates the 'sdt_notes'.
2296 static int construct_sdt_notes_list(Elf *elf, struct list_head *sdt_notes)
2299 Elf_Scn *scn = NULL;
2302 size_t shstrndx, next;
2304 size_t name_off, desc_off, offset;
2307 if (gelf_getehdr(elf, &ehdr) == NULL) {
2311 if (elf_getshdrstrndx(elf, &shstrndx) != 0) {
2316 /* Look for the required section */
2317 scn = elf_section_by_name(elf, &ehdr, &shdr, SDT_NOTE_SCN, NULL);
2323 if ((shdr.sh_type != SHT_NOTE) || (shdr.sh_flags & SHF_ALLOC)) {
2328 data = elf_getdata(scn, NULL);
2330 /* Get the SDT notes */
2331 for (offset = 0; (next = gelf_getnote(data, offset, &nhdr, &name_off,
2332 &desc_off)) > 0; offset = next) {
2333 if (nhdr.n_namesz == sizeof(SDT_NOTE_NAME) &&
2334 !memcmp(data->d_buf + name_off, SDT_NOTE_NAME,
2335 sizeof(SDT_NOTE_NAME))) {
2336 /* Check the type of the note */
2337 if (nhdr.n_type != SDT_NOTE_TYPE)
2340 ret = populate_sdt_note(&elf, ((data->d_buf) + desc_off),
2341 nhdr.n_descsz, sdt_notes);
2346 if (list_empty(sdt_notes))
2354 * get_sdt_note_list : Wrapper to construct a list of sdt notes
2355 * @head : empty list_head
2356 * @target : file to find SDT notes from
2358 * This opens the file, initializes
2359 * the ELF and then calls construct_sdt_notes_list.
2361 int get_sdt_note_list(struct list_head *head, const char *target)
2366 fd = open(target, O_RDONLY);
2370 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
2375 ret = construct_sdt_notes_list(elf, head);
2383 * cleanup_sdt_note_list : free the sdt notes' list
2384 * @sdt_notes: sdt notes' list
2386 * Free up the SDT notes in @sdt_notes.
2387 * Returns the number of SDT notes free'd.
2389 int cleanup_sdt_note_list(struct list_head *sdt_notes)
2391 struct sdt_note *tmp, *pos;
2394 list_for_each_entry_safe(pos, tmp, sdt_notes, note_list) {
2395 list_del_init(&pos->note_list);
2397 zfree(&pos->provider);
2405 * sdt_notes__get_count: Counts the number of sdt events
2406 * @start: list_head to sdt_notes list
2408 * Returns the number of SDT notes in a list
2410 int sdt_notes__get_count(struct list_head *start)
2412 struct sdt_note *sdt_ptr;
2415 list_for_each_entry(sdt_ptr, start, note_list)
2421 void symbol__elf_init(void)
2423 elf_version(EV_CURRENT);