1 // SPDX-License-Identifier: GPL-2.0
10 #include "demangle-java.h"
11 #include "demangle-rust.h"
15 #include "sane_ctype.h"
16 #include <symbol/kallsyms.h>
19 #define EM_AARCH64 183 /* ARM 64 bit */
22 typedef Elf64_Nhdr GElf_Nhdr;
24 #ifdef HAVE_CPLUS_DEMANGLE_SUPPORT
25 extern char *cplus_demangle(const char *, int);
27 static inline char *bfd_demangle(void __maybe_unused *v, const char *c, int i)
29 return cplus_demangle(c, i);
33 static inline char *bfd_demangle(void __maybe_unused *v,
34 const char __maybe_unused *c,
40 #define PACKAGE 'perf'
45 #ifndef HAVE_ELF_GETPHDRNUM_SUPPORT
46 static int elf_getphdrnum(Elf *elf, size_t *dst)
51 ehdr = gelf_getehdr(elf, &gehdr);
61 #ifndef HAVE_ELF_GETSHDRSTRNDX_SUPPORT
62 static int elf_getshdrstrndx(Elf *elf __maybe_unused, size_t *dst __maybe_unused)
64 pr_err("%s: update your libelf to > 0.140, this one lacks elf_getshdrstrndx().\n", __func__);
69 #ifndef NT_GNU_BUILD_ID
70 #define NT_GNU_BUILD_ID 3
74 * elf_symtab__for_each_symbol - iterate thru all the symbols
76 * @syms: struct elf_symtab instance to iterate
78 * @sym: GElf_Sym iterator
80 #define elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) \
81 for (idx = 0, gelf_getsym(syms, idx, &sym);\
83 idx++, gelf_getsym(syms, idx, &sym))
85 static inline uint8_t elf_sym__type(const GElf_Sym *sym)
87 return GELF_ST_TYPE(sym->st_info);
90 static inline uint8_t elf_sym__visibility(const GElf_Sym *sym)
92 return GELF_ST_VISIBILITY(sym->st_other);
96 #define STT_GNU_IFUNC 10
99 static inline int elf_sym__is_function(const GElf_Sym *sym)
101 return (elf_sym__type(sym) == STT_FUNC ||
102 elf_sym__type(sym) == STT_GNU_IFUNC) &&
104 sym->st_shndx != SHN_UNDEF;
107 static inline bool elf_sym__is_object(const GElf_Sym *sym)
109 return elf_sym__type(sym) == STT_OBJECT &&
111 sym->st_shndx != SHN_UNDEF;
114 static inline int elf_sym__is_label(const GElf_Sym *sym)
116 return elf_sym__type(sym) == STT_NOTYPE &&
118 sym->st_shndx != SHN_UNDEF &&
119 sym->st_shndx != SHN_ABS &&
120 elf_sym__visibility(sym) != STV_HIDDEN &&
121 elf_sym__visibility(sym) != STV_INTERNAL;
124 static bool elf_sym__filter(GElf_Sym *sym)
126 return elf_sym__is_function(sym) || elf_sym__is_object(sym);
129 static inline const char *elf_sym__name(const GElf_Sym *sym,
130 const Elf_Data *symstrs)
132 return symstrs->d_buf + sym->st_name;
135 static inline const char *elf_sec__name(const GElf_Shdr *shdr,
136 const Elf_Data *secstrs)
138 return secstrs->d_buf + shdr->sh_name;
141 static inline int elf_sec__is_text(const GElf_Shdr *shdr,
142 const Elf_Data *secstrs)
144 return strstr(elf_sec__name(shdr, secstrs), "text") != NULL;
147 static inline bool elf_sec__is_data(const GElf_Shdr *shdr,
148 const Elf_Data *secstrs)
150 return strstr(elf_sec__name(shdr, secstrs), "data") != NULL;
153 static bool elf_sec__filter(GElf_Shdr *shdr, Elf_Data *secstrs)
155 return elf_sec__is_text(shdr, secstrs) ||
156 elf_sec__is_data(shdr, secstrs);
159 static size_t elf_addr_to_index(Elf *elf, GElf_Addr addr)
165 while ((sec = elf_nextscn(elf, sec)) != NULL) {
166 gelf_getshdr(sec, &shdr);
168 if ((addr >= shdr.sh_addr) &&
169 (addr < (shdr.sh_addr + shdr.sh_size)))
178 Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep,
179 GElf_Shdr *shp, const char *name, size_t *idx)
184 /* Elf is corrupted/truncated, avoid calling elf_strptr. */
185 if (!elf_rawdata(elf_getscn(elf, ep->e_shstrndx), NULL))
188 while ((sec = elf_nextscn(elf, sec)) != NULL) {
191 gelf_getshdr(sec, shp);
192 str = elf_strptr(elf, ep->e_shstrndx, shp->sh_name);
193 if (str && !strcmp(name, str)) {
204 static bool want_demangle(bool is_kernel_sym)
206 return is_kernel_sym ? symbol_conf.demangle_kernel : symbol_conf.demangle;
209 static char *demangle_sym(struct dso *dso, int kmodule, const char *elf_name)
211 int demangle_flags = verbose > 0 ? (DMGL_PARAMS | DMGL_ANSI) : DMGL_NO_OPTS;
212 char *demangled = NULL;
215 * We need to figure out if the object was created from C++ sources
216 * DWARF DW_compile_unit has this, but we don't always have access
219 if (!want_demangle(dso->kernel || kmodule))
222 demangled = bfd_demangle(NULL, elf_name, demangle_flags);
223 if (demangled == NULL)
224 demangled = java_demangle_sym(elf_name, JAVA_DEMANGLE_NORET);
225 else if (rust_is_mangled(demangled))
227 * Input to Rust demangling is the BFD-demangled
228 * name which it Rust-demangles in place.
230 rust_demangle_sym(demangled);
235 #define elf_section__for_each_rel(reldata, pos, pos_mem, idx, nr_entries) \
236 for (idx = 0, pos = gelf_getrel(reldata, 0, &pos_mem); \
238 ++idx, pos = gelf_getrel(reldata, idx, &pos_mem))
240 #define elf_section__for_each_rela(reldata, pos, pos_mem, idx, nr_entries) \
241 for (idx = 0, pos = gelf_getrela(reldata, 0, &pos_mem); \
243 ++idx, pos = gelf_getrela(reldata, idx, &pos_mem))
246 * We need to check if we have a .dynsym, so that we can handle the
247 * .plt, synthesizing its symbols, that aren't on the symtabs (be it
248 * .dynsym or .symtab).
249 * And always look at the original dso, not at debuginfo packages, that
250 * have the PLT data stripped out (shdr_rel_plt.sh_type == SHT_NOBITS).
252 int dso__synthesize_plt_symbols(struct dso *dso, struct symsrc *ss)
254 uint32_t nr_rel_entries, idx;
256 u64 plt_offset, plt_header_size, plt_entry_size;
259 GElf_Shdr shdr_rel_plt, shdr_dynsym;
260 Elf_Data *reldata, *syms, *symstrs;
261 Elf_Scn *scn_plt_rel, *scn_symstrs, *scn_dynsym;
264 char sympltname[1024];
266 int nr = 0, symidx, err = 0;
274 scn_dynsym = ss->dynsym;
275 shdr_dynsym = ss->dynshdr;
276 dynsym_idx = ss->dynsym_idx;
278 if (scn_dynsym == NULL)
281 scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt,
283 if (scn_plt_rel == NULL) {
284 scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt,
286 if (scn_plt_rel == NULL)
292 if (shdr_rel_plt.sh_link != dynsym_idx)
295 if (elf_section_by_name(elf, &ehdr, &shdr_plt, ".plt", NULL) == NULL)
299 * Fetch the relocation section to find the idxes to the GOT
300 * and the symbols in the .dynsym they refer to.
302 reldata = elf_getdata(scn_plt_rel, NULL);
306 syms = elf_getdata(scn_dynsym, NULL);
310 scn_symstrs = elf_getscn(elf, shdr_dynsym.sh_link);
311 if (scn_symstrs == NULL)
314 symstrs = elf_getdata(scn_symstrs, NULL);
318 if (symstrs->d_size == 0)
321 nr_rel_entries = shdr_rel_plt.sh_size / shdr_rel_plt.sh_entsize;
322 plt_offset = shdr_plt.sh_offset;
323 switch (ehdr.e_machine) {
325 plt_header_size = 20;
330 plt_header_size = 32;
335 plt_header_size = 48;
340 plt_header_size = 128;
344 default: /* FIXME: s390/alpha/mips/parisc/poperpc/sh/xtensa need to be checked */
345 plt_header_size = shdr_plt.sh_entsize;
346 plt_entry_size = shdr_plt.sh_entsize;
349 plt_offset += plt_header_size;
351 if (shdr_rel_plt.sh_type == SHT_RELA) {
352 GElf_Rela pos_mem, *pos;
354 elf_section__for_each_rela(reldata, pos, pos_mem, idx,
356 const char *elf_name = NULL;
357 char *demangled = NULL;
358 symidx = GELF_R_SYM(pos->r_info);
359 gelf_getsym(syms, symidx, &sym);
361 elf_name = elf_sym__name(&sym, symstrs);
362 demangled = demangle_sym(dso, 0, elf_name);
363 if (demangled != NULL)
364 elf_name = demangled;
365 snprintf(sympltname, sizeof(sympltname),
369 f = symbol__new(plt_offset, plt_entry_size,
370 STB_GLOBAL, STT_FUNC, sympltname);
374 plt_offset += plt_entry_size;
375 symbols__insert(&dso->symbols, f);
378 } else if (shdr_rel_plt.sh_type == SHT_REL) {
379 GElf_Rel pos_mem, *pos;
380 elf_section__for_each_rel(reldata, pos, pos_mem, idx,
382 const char *elf_name = NULL;
383 char *demangled = NULL;
384 symidx = GELF_R_SYM(pos->r_info);
385 gelf_getsym(syms, symidx, &sym);
387 elf_name = elf_sym__name(&sym, symstrs);
388 demangled = demangle_sym(dso, 0, elf_name);
389 if (demangled != NULL)
390 elf_name = demangled;
391 snprintf(sympltname, sizeof(sympltname),
395 f = symbol__new(plt_offset, plt_entry_size,
396 STB_GLOBAL, STT_FUNC, sympltname);
400 plt_offset += plt_entry_size;
401 symbols__insert(&dso->symbols, f);
410 pr_debug("%s: problems reading %s PLT info.\n",
411 __func__, dso->long_name);
415 char *dso__demangle_sym(struct dso *dso, int kmodule, const char *elf_name)
417 return demangle_sym(dso, kmodule, elf_name);
421 * Align offset to 4 bytes as needed for note name and descriptor data.
423 #define NOTE_ALIGN(n) (((n) + 3) & -4U)
425 static int elf_read_build_id(Elf *elf, void *bf, size_t size)
435 if (size < BUILD_ID_SIZE)
442 if (gelf_getehdr(elf, &ehdr) == NULL) {
443 pr_err("%s: cannot get elf header.\n", __func__);
448 * Check following sections for notes:
449 * '.note.gnu.build-id'
451 * '.note' (VDSO specific)
454 sec = elf_section_by_name(elf, &ehdr, &shdr,
455 ".note.gnu.build-id", NULL);
459 sec = elf_section_by_name(elf, &ehdr, &shdr,
464 sec = elf_section_by_name(elf, &ehdr, &shdr,
473 data = elf_getdata(sec, NULL);
478 while (ptr < (data->d_buf + data->d_size)) {
479 GElf_Nhdr *nhdr = ptr;
480 size_t namesz = NOTE_ALIGN(nhdr->n_namesz),
481 descsz = NOTE_ALIGN(nhdr->n_descsz);
484 ptr += sizeof(*nhdr);
487 if (nhdr->n_type == NT_GNU_BUILD_ID &&
488 nhdr->n_namesz == sizeof("GNU")) {
489 if (memcmp(name, "GNU", sizeof("GNU")) == 0) {
490 size_t sz = min(size, descsz);
492 memset(bf + sz, 0, size - sz);
504 int filename__read_build_id(const char *filename, void *bf, size_t size)
509 if (size < BUILD_ID_SIZE)
512 fd = open(filename, O_RDONLY);
516 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
518 pr_debug2("%s: cannot read %s ELF file.\n", __func__, filename);
522 err = elf_read_build_id(elf, bf, size);
531 int sysfs__read_build_id(const char *filename, void *build_id, size_t size)
535 if (size < BUILD_ID_SIZE)
538 fd = open(filename, O_RDONLY);
545 size_t namesz, descsz;
547 if (read(fd, &nhdr, sizeof(nhdr)) != sizeof(nhdr))
550 namesz = NOTE_ALIGN(nhdr.n_namesz);
551 descsz = NOTE_ALIGN(nhdr.n_descsz);
552 if (nhdr.n_type == NT_GNU_BUILD_ID &&
553 nhdr.n_namesz == sizeof("GNU")) {
554 if (read(fd, bf, namesz) != (ssize_t)namesz)
556 if (memcmp(bf, "GNU", sizeof("GNU")) == 0) {
557 size_t sz = min(descsz, size);
558 if (read(fd, build_id, sz) == (ssize_t)sz) {
559 memset(build_id + sz, 0, size - sz);
563 } else if (read(fd, bf, descsz) != (ssize_t)descsz)
566 int n = namesz + descsz;
568 if (n > (int)sizeof(bf)) {
570 pr_debug("%s: truncating reading of build id in sysfs file %s: n_namesz=%u, n_descsz=%u.\n",
571 __func__, filename, nhdr.n_namesz, nhdr.n_descsz);
573 if (read(fd, bf, n) != n)
582 int filename__read_debuglink(const char *filename, char *debuglink,
593 fd = open(filename, O_RDONLY);
597 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
599 pr_debug2("%s: cannot read %s ELF file.\n", __func__, filename);
607 if (gelf_getehdr(elf, &ehdr) == NULL) {
608 pr_err("%s: cannot get elf header.\n", __func__);
612 sec = elf_section_by_name(elf, &ehdr, &shdr,
613 ".gnu_debuglink", NULL);
617 data = elf_getdata(sec, NULL);
621 /* the start of this section is a zero-terminated string */
622 strncpy(debuglink, data->d_buf, size);
634 static int dso__swap_init(struct dso *dso, unsigned char eidata)
636 static unsigned int const endian = 1;
638 dso->needs_swap = DSO_SWAP__NO;
642 /* We are big endian, DSO is little endian. */
643 if (*(unsigned char const *)&endian != 1)
644 dso->needs_swap = DSO_SWAP__YES;
648 /* We are little endian, DSO is big endian. */
649 if (*(unsigned char const *)&endian != 0)
650 dso->needs_swap = DSO_SWAP__YES;
654 pr_err("unrecognized DSO data encoding %d\n", eidata);
661 bool symsrc__possibly_runtime(struct symsrc *ss)
663 return ss->dynsym || ss->opdsec;
666 bool symsrc__has_symtab(struct symsrc *ss)
668 return ss->symtab != NULL;
671 void symsrc__destroy(struct symsrc *ss)
678 bool __weak elf__needs_adjust_symbols(GElf_Ehdr ehdr)
680 return ehdr.e_type == ET_EXEC || ehdr.e_type == ET_REL;
683 int symsrc__init(struct symsrc *ss, struct dso *dso, const char *name,
684 enum dso_binary_type type)
691 if (dso__needs_decompress(dso)) {
692 fd = dso__decompress_kmodule_fd(dso, name);
696 type = dso->symtab_type;
698 fd = open(name, O_RDONLY);
700 dso->load_errno = errno;
705 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
707 pr_debug("%s: cannot read %s ELF file.\n", __func__, name);
708 dso->load_errno = DSO_LOAD_ERRNO__INVALID_ELF;
712 if (gelf_getehdr(elf, &ehdr) == NULL) {
713 dso->load_errno = DSO_LOAD_ERRNO__INVALID_ELF;
714 pr_debug("%s: cannot get elf header.\n", __func__);
718 if (dso__swap_init(dso, ehdr.e_ident[EI_DATA])) {
719 dso->load_errno = DSO_LOAD_ERRNO__INTERNAL_ERROR;
723 /* Always reject images with a mismatched build-id: */
724 if (dso->has_build_id && !symbol_conf.ignore_vmlinux_buildid) {
725 u8 build_id[BUILD_ID_SIZE];
727 if (elf_read_build_id(elf, build_id, BUILD_ID_SIZE) < 0) {
728 dso->load_errno = DSO_LOAD_ERRNO__CANNOT_READ_BUILDID;
732 if (!dso__build_id_equal(dso, build_id)) {
733 pr_debug("%s: build id mismatch for %s.\n", __func__, name);
734 dso->load_errno = DSO_LOAD_ERRNO__MISMATCHING_BUILDID;
739 ss->is_64_bit = (gelf_getclass(elf) == ELFCLASS64);
741 ss->symtab = elf_section_by_name(elf, &ehdr, &ss->symshdr, ".symtab",
743 if (ss->symshdr.sh_type != SHT_SYMTAB)
747 ss->dynsym = elf_section_by_name(elf, &ehdr, &ss->dynshdr, ".dynsym",
749 if (ss->dynshdr.sh_type != SHT_DYNSYM)
753 ss->opdsec = elf_section_by_name(elf, &ehdr, &ss->opdshdr, ".opd",
755 if (ss->opdshdr.sh_type != SHT_PROGBITS)
758 if (dso->kernel == DSO_TYPE_USER)
759 ss->adjust_symbols = true;
761 ss->adjust_symbols = elf__needs_adjust_symbols(ehdr);
763 ss->name = strdup(name);
765 dso->load_errno = errno;
784 * ref_reloc_sym_not_found - has kernel relocation symbol been found.
785 * @kmap: kernel maps and relocation reference symbol
787 * This function returns %true if we are dealing with the kernel maps and the
788 * relocation reference symbol has not yet been found. Otherwise %false is
791 static bool ref_reloc_sym_not_found(struct kmap *kmap)
793 return kmap && kmap->ref_reloc_sym && kmap->ref_reloc_sym->name &&
794 !kmap->ref_reloc_sym->unrelocated_addr;
798 * ref_reloc - kernel relocation offset.
799 * @kmap: kernel maps and relocation reference symbol
801 * This function returns the offset of kernel addresses as determined by using
802 * the relocation reference symbol i.e. if the kernel has not been relocated
803 * then the return value is zero.
805 static u64 ref_reloc(struct kmap *kmap)
807 if (kmap && kmap->ref_reloc_sym &&
808 kmap->ref_reloc_sym->unrelocated_addr)
809 return kmap->ref_reloc_sym->addr -
810 kmap->ref_reloc_sym->unrelocated_addr;
814 void __weak arch__sym_update(struct symbol *s __maybe_unused,
815 GElf_Sym *sym __maybe_unused) { }
817 static int dso__process_kernel_symbol(struct dso *dso, struct map *map,
818 GElf_Sym *sym, GElf_Shdr *shdr,
819 struct map_groups *kmaps, struct kmap *kmap,
820 struct dso **curr_dsop, struct map **curr_mapp,
821 const char *section_name,
822 bool adjust_kernel_syms, bool kmodule, bool *remap_kernel)
824 struct dso *curr_dso = *curr_dsop;
825 struct map *curr_map;
826 char dso_name[PATH_MAX];
828 /* Adjust symbol to map to file offset */
829 if (adjust_kernel_syms)
830 sym->st_value -= shdr->sh_addr - shdr->sh_offset;
832 if (strcmp(section_name, (curr_dso->short_name + dso->short_name_len)) == 0)
835 if (strcmp(section_name, ".text") == 0) {
837 * The initial kernel mapping is based on
838 * kallsyms and identity maps. Overwrite it to
839 * map to the kernel dso.
841 if (*remap_kernel && dso->kernel) {
842 *remap_kernel = false;
843 map->start = shdr->sh_addr + ref_reloc(kmap);
844 map->end = map->start + shdr->sh_size;
845 map->pgoff = shdr->sh_offset;
846 map->map_ip = map__map_ip;
847 map->unmap_ip = map__unmap_ip;
848 /* Ensure maps are correctly ordered */
851 map_groups__remove(kmaps, map);
852 map_groups__insert(kmaps, map);
858 * The initial module mapping is based on
859 * /proc/modules mapped to offset zero.
860 * Overwrite it to map to the module dso.
862 if (*remap_kernel && kmodule) {
863 *remap_kernel = false;
864 map->pgoff = shdr->sh_offset;
875 snprintf(dso_name, sizeof(dso_name), "%s%s", dso->short_name, section_name);
877 curr_map = map_groups__find_by_name(kmaps, dso_name);
878 if (curr_map == NULL) {
879 u64 start = sym->st_value;
882 start += map->start + shdr->sh_offset;
884 curr_dso = dso__new(dso_name);
885 if (curr_dso == NULL)
887 curr_dso->kernel = dso->kernel;
888 curr_dso->long_name = dso->long_name;
889 curr_dso->long_name_len = dso->long_name_len;
890 curr_map = map__new2(start, curr_dso);
892 if (curr_map == NULL)
895 if (adjust_kernel_syms) {
896 curr_map->start = shdr->sh_addr + ref_reloc(kmap);
897 curr_map->end = curr_map->start + shdr->sh_size;
898 curr_map->pgoff = shdr->sh_offset;
900 curr_map->map_ip = curr_map->unmap_ip = identity__map_ip;
902 curr_dso->symtab_type = dso->symtab_type;
903 map_groups__insert(kmaps, curr_map);
905 * Add it before we drop the referece to curr_map, i.e. while
906 * we still are sure to have a reference to this DSO via
909 dsos__add(&map->groups->machine->dsos, curr_dso);
910 /* kmaps already got it */
912 dso__set_loaded(curr_dso);
913 *curr_mapp = curr_map;
914 *curr_dsop = curr_dso;
916 *curr_dsop = curr_map->dso;
921 int dso__load_sym(struct dso *dso, struct map *map, struct symsrc *syms_ss,
922 struct symsrc *runtime_ss, int kmodule)
924 struct kmap *kmap = dso->kernel ? map__kmap(map) : NULL;
925 struct map_groups *kmaps = kmap ? map__kmaps(map) : NULL;
926 struct map *curr_map = map;
927 struct dso *curr_dso = dso;
928 Elf_Data *symstrs, *secstrs;
935 Elf_Data *syms, *opddata = NULL;
937 Elf_Scn *sec, *sec_strndx;
940 bool remap_kernel = false, adjust_kernel_syms = false;
945 dso->symtab_type = syms_ss->type;
946 dso->is_64_bit = syms_ss->is_64_bit;
947 dso->rel = syms_ss->ehdr.e_type == ET_REL;
950 * Modules may already have symbols from kallsyms, but those symbols
951 * have the wrong values for the dso maps, so remove them.
953 if (kmodule && syms_ss->symtab)
954 symbols__delete(&dso->symbols);
956 if (!syms_ss->symtab) {
958 * If the vmlinux is stripped, fail so we will fall back
959 * to using kallsyms. The vmlinux runtime symbols aren't
965 syms_ss->symtab = syms_ss->dynsym;
966 syms_ss->symshdr = syms_ss->dynshdr;
970 ehdr = syms_ss->ehdr;
971 sec = syms_ss->symtab;
972 shdr = syms_ss->symshdr;
974 if (elf_section_by_name(runtime_ss->elf, &runtime_ss->ehdr, &tshdr,
976 dso->text_offset = tshdr.sh_addr - tshdr.sh_offset;
978 if (runtime_ss->opdsec)
979 opddata = elf_rawdata(runtime_ss->opdsec, NULL);
981 syms = elf_getdata(sec, NULL);
985 sec = elf_getscn(elf, shdr.sh_link);
989 symstrs = elf_getdata(sec, NULL);
993 sec_strndx = elf_getscn(runtime_ss->elf, runtime_ss->ehdr.e_shstrndx);
994 if (sec_strndx == NULL)
997 secstrs = elf_getdata(sec_strndx, NULL);
1001 nr_syms = shdr.sh_size / shdr.sh_entsize;
1003 memset(&sym, 0, sizeof(sym));
1006 * The kernel relocation symbol is needed in advance in order to adjust
1007 * kernel maps correctly.
1009 if (ref_reloc_sym_not_found(kmap)) {
1010 elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) {
1011 const char *elf_name = elf_sym__name(&sym, symstrs);
1013 if (strcmp(elf_name, kmap->ref_reloc_sym->name))
1015 kmap->ref_reloc_sym->unrelocated_addr = sym.st_value;
1016 map->reloc = kmap->ref_reloc_sym->addr -
1017 kmap->ref_reloc_sym->unrelocated_addr;
1023 * Handle any relocation of vdso necessary because older kernels
1024 * attempted to prelink vdso to its virtual address.
1026 if (dso__is_vdso(dso))
1027 map->reloc = map->start - dso->text_offset;
1029 dso->adjust_symbols = runtime_ss->adjust_symbols || ref_reloc(kmap);
1031 * Initial kernel and module mappings do not map to the dso.
1034 if (dso->kernel || kmodule) {
1035 remap_kernel = true;
1036 adjust_kernel_syms = dso->adjust_symbols;
1038 elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) {
1040 const char *elf_name = elf_sym__name(&sym, symstrs);
1041 char *demangled = NULL;
1042 int is_label = elf_sym__is_label(&sym);
1043 const char *section_name;
1044 bool used_opd = false;
1046 if (!is_label && !elf_sym__filter(&sym))
1049 /* Reject ARM ELF "mapping symbols": these aren't unique and
1050 * don't identify functions, so will confuse the profile
1052 if (ehdr.e_machine == EM_ARM || ehdr.e_machine == EM_AARCH64) {
1053 if (elf_name[0] == '$' && strchr("adtx", elf_name[1])
1054 && (elf_name[2] == '\0' || elf_name[2] == '.'))
1058 if (runtime_ss->opdsec && sym.st_shndx == runtime_ss->opdidx) {
1059 u32 offset = sym.st_value - syms_ss->opdshdr.sh_addr;
1060 u64 *opd = opddata->d_buf + offset;
1061 sym.st_value = DSO__SWAP(dso, u64, *opd);
1062 sym.st_shndx = elf_addr_to_index(runtime_ss->elf,
1067 * When loading symbols in a data mapping, ABS symbols (which
1068 * has a value of SHN_ABS in its st_shndx) failed at
1069 * elf_getscn(). And it marks the loading as a failure so
1070 * already loaded symbols cannot be fixed up.
1072 * I'm not sure what should be done. Just ignore them for now.
1075 if (sym.st_shndx == SHN_ABS)
1078 sec = elf_getscn(runtime_ss->elf, sym.st_shndx);
1082 gelf_getshdr(sec, &shdr);
1084 if (is_label && !elf_sec__filter(&shdr, secstrs))
1087 section_name = elf_sec__name(&shdr, secstrs);
1089 /* On ARM, symbols for thumb functions have 1 added to
1090 * the symbol address as a flag - remove it */
1091 if ((ehdr.e_machine == EM_ARM) &&
1092 (GELF_ST_TYPE(sym.st_info) == STT_FUNC) &&
1096 if (dso->kernel || kmodule) {
1097 if (dso__process_kernel_symbol(dso, map, &sym, &shdr, kmaps, kmap, &curr_dso, &curr_map,
1098 section_name, adjust_kernel_syms, kmodule, &remap_kernel))
1100 } else if ((used_opd && runtime_ss->adjust_symbols) ||
1101 (!used_opd && syms_ss->adjust_symbols)) {
1102 pr_debug4("%s: adjusting symbol: st_value: %#" PRIx64 " "
1103 "sh_addr: %#" PRIx64 " sh_offset: %#" PRIx64 "\n", __func__,
1104 (u64)sym.st_value, (u64)shdr.sh_addr,
1105 (u64)shdr.sh_offset);
1106 sym.st_value -= shdr.sh_addr - shdr.sh_offset;
1109 demangled = demangle_sym(dso, kmodule, elf_name);
1110 if (demangled != NULL)
1111 elf_name = demangled;
1113 f = symbol__new(sym.st_value, sym.st_size,
1114 GELF_ST_BIND(sym.st_info),
1115 GELF_ST_TYPE(sym.st_info), elf_name);
1120 arch__sym_update(f, &sym);
1122 __symbols__insert(&curr_dso->symbols, f, dso->kernel);
1127 * For misannotated, zeroed, ASM function sizes.
1130 symbols__fixup_end(&dso->symbols);
1131 symbols__fixup_duplicate(&dso->symbols);
1134 * We need to fixup this here too because we create new
1135 * maps here, for things like vsyscall sections.
1137 map_groups__fixup_end(kmaps);
1145 static int elf_read_maps(Elf *elf, bool exe, mapfn_t mapfn, void *data)
1152 if (elf_getphdrnum(elf, &phdrnum))
1155 for (i = 0; i < phdrnum; i++) {
1156 if (gelf_getphdr(elf, i, &phdr) == NULL)
1158 if (phdr.p_type != PT_LOAD)
1161 if (!(phdr.p_flags & PF_X))
1164 if (!(phdr.p_flags & PF_R))
1167 sz = min(phdr.p_memsz, phdr.p_filesz);
1170 err = mapfn(phdr.p_vaddr, sz, phdr.p_offset, data);
1177 int file__read_maps(int fd, bool exe, mapfn_t mapfn, void *data,
1183 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
1188 *is_64_bit = (gelf_getclass(elf) == ELFCLASS64);
1190 err = elf_read_maps(elf, exe, mapfn, data);
1196 enum dso_type dso__type_fd(int fd)
1198 enum dso_type dso_type = DSO__TYPE_UNKNOWN;
1203 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
1208 if (ek != ELF_K_ELF)
1211 if (gelf_getclass(elf) == ELFCLASS64) {
1212 dso_type = DSO__TYPE_64BIT;
1216 if (gelf_getehdr(elf, &ehdr) == NULL)
1219 if (ehdr.e_machine == EM_X86_64)
1220 dso_type = DSO__TYPE_X32BIT;
1222 dso_type = DSO__TYPE_32BIT;
1229 static int copy_bytes(int from, off_t from_offs, int to, off_t to_offs, u64 len)
1234 char *buf = malloc(page_size);
1239 if (lseek(to, to_offs, SEEK_SET) != to_offs)
1242 if (lseek(from, from_offs, SEEK_SET) != from_offs)
1249 /* Use read because mmap won't work on proc files */
1250 r = read(from, buf, n);
1256 r = write(to, buf, n);
1277 static int kcore__open(struct kcore *kcore, const char *filename)
1281 kcore->fd = open(filename, O_RDONLY);
1282 if (kcore->fd == -1)
1285 kcore->elf = elf_begin(kcore->fd, ELF_C_READ, NULL);
1289 kcore->elfclass = gelf_getclass(kcore->elf);
1290 if (kcore->elfclass == ELFCLASSNONE)
1293 ehdr = gelf_getehdr(kcore->elf, &kcore->ehdr);
1300 elf_end(kcore->elf);
1306 static int kcore__init(struct kcore *kcore, char *filename, int elfclass,
1309 kcore->elfclass = elfclass;
1312 kcore->fd = mkstemp(filename);
1314 kcore->fd = open(filename, O_WRONLY | O_CREAT | O_EXCL, 0400);
1315 if (kcore->fd == -1)
1318 kcore->elf = elf_begin(kcore->fd, ELF_C_WRITE, NULL);
1322 if (!gelf_newehdr(kcore->elf, elfclass))
1325 memset(&kcore->ehdr, 0, sizeof(GElf_Ehdr));
1330 elf_end(kcore->elf);
1337 static void kcore__close(struct kcore *kcore)
1339 elf_end(kcore->elf);
1343 static int kcore__copy_hdr(struct kcore *from, struct kcore *to, size_t count)
1345 GElf_Ehdr *ehdr = &to->ehdr;
1346 GElf_Ehdr *kehdr = &from->ehdr;
1348 memcpy(ehdr->e_ident, kehdr->e_ident, EI_NIDENT);
1349 ehdr->e_type = kehdr->e_type;
1350 ehdr->e_machine = kehdr->e_machine;
1351 ehdr->e_version = kehdr->e_version;
1354 ehdr->e_flags = kehdr->e_flags;
1355 ehdr->e_phnum = count;
1356 ehdr->e_shentsize = 0;
1358 ehdr->e_shstrndx = 0;
1360 if (from->elfclass == ELFCLASS32) {
1361 ehdr->e_phoff = sizeof(Elf32_Ehdr);
1362 ehdr->e_ehsize = sizeof(Elf32_Ehdr);
1363 ehdr->e_phentsize = sizeof(Elf32_Phdr);
1365 ehdr->e_phoff = sizeof(Elf64_Ehdr);
1366 ehdr->e_ehsize = sizeof(Elf64_Ehdr);
1367 ehdr->e_phentsize = sizeof(Elf64_Phdr);
1370 if (!gelf_update_ehdr(to->elf, ehdr))
1373 if (!gelf_newphdr(to->elf, count))
1379 static int kcore__add_phdr(struct kcore *kcore, int idx, off_t offset,
1384 .p_flags = PF_R | PF_W | PF_X,
1390 .p_align = page_size,
1393 if (!gelf_update_phdr(kcore->elf, idx, &phdr))
1399 static off_t kcore__write(struct kcore *kcore)
1401 return elf_update(kcore->elf, ELF_C_WRITE);
1409 struct list_head node;
1410 struct phdr_data *remaps;
1415 struct list_head node;
1418 struct kcore_copy_info {
1424 u64 first_module_symbol;
1425 u64 last_module_symbol;
1427 struct list_head phdrs;
1428 struct list_head syms;
1431 #define kcore_copy__for_each_phdr(k, p) \
1432 list_for_each_entry((p), &(k)->phdrs, node)
1434 static struct phdr_data *phdr_data__new(u64 addr, u64 len, off_t offset)
1436 struct phdr_data *p = zalloc(sizeof(*p));
1447 static struct phdr_data *kcore_copy_info__addnew(struct kcore_copy_info *kci,
1451 struct phdr_data *p = phdr_data__new(addr, len, offset);
1454 list_add_tail(&p->node, &kci->phdrs);
1459 static void kcore_copy__free_phdrs(struct kcore_copy_info *kci)
1461 struct phdr_data *p, *tmp;
1463 list_for_each_entry_safe(p, tmp, &kci->phdrs, node) {
1469 static struct sym_data *kcore_copy__new_sym(struct kcore_copy_info *kci,
1472 struct sym_data *s = zalloc(sizeof(*s));
1476 list_add_tail(&s->node, &kci->syms);
1482 static void kcore_copy__free_syms(struct kcore_copy_info *kci)
1484 struct sym_data *s, *tmp;
1486 list_for_each_entry_safe(s, tmp, &kci->syms, node) {
1492 static int kcore_copy__process_kallsyms(void *arg, const char *name, char type,
1495 struct kcore_copy_info *kci = arg;
1497 if (!kallsyms__is_function(type))
1500 if (strchr(name, '[')) {
1501 if (!kci->first_module_symbol || start < kci->first_module_symbol)
1502 kci->first_module_symbol = start;
1503 if (start > kci->last_module_symbol)
1504 kci->last_module_symbol = start;
1508 if (!kci->first_symbol || start < kci->first_symbol)
1509 kci->first_symbol = start;
1511 if (!kci->last_symbol || start > kci->last_symbol)
1512 kci->last_symbol = start;
1514 if (!strcmp(name, "_stext")) {
1519 if (!strcmp(name, "_etext")) {
1524 if (is_entry_trampoline(name) && !kcore_copy__new_sym(kci, start))
1530 static int kcore_copy__parse_kallsyms(struct kcore_copy_info *kci,
1533 char kallsyms_filename[PATH_MAX];
1535 scnprintf(kallsyms_filename, PATH_MAX, "%s/kallsyms", dir);
1537 if (symbol__restricted_filename(kallsyms_filename, "/proc/kallsyms"))
1540 if (kallsyms__parse(kallsyms_filename, kci,
1541 kcore_copy__process_kallsyms) < 0)
1547 static int kcore_copy__process_modules(void *arg,
1548 const char *name __maybe_unused,
1549 u64 start, u64 size __maybe_unused)
1551 struct kcore_copy_info *kci = arg;
1553 if (!kci->first_module || start < kci->first_module)
1554 kci->first_module = start;
1559 static int kcore_copy__parse_modules(struct kcore_copy_info *kci,
1562 char modules_filename[PATH_MAX];
1564 scnprintf(modules_filename, PATH_MAX, "%s/modules", dir);
1566 if (symbol__restricted_filename(modules_filename, "/proc/modules"))
1569 if (modules__parse(modules_filename, kci,
1570 kcore_copy__process_modules) < 0)
1576 static int kcore_copy__map(struct kcore_copy_info *kci, u64 start, u64 end,
1577 u64 pgoff, u64 s, u64 e)
1581 if (s < start || s >= end)
1584 offset = (s - start) + pgoff;
1585 len = e < end ? e - s : end - s;
1587 return kcore_copy_info__addnew(kci, s, len, offset) ? 0 : -1;
1590 static int kcore_copy__read_map(u64 start, u64 len, u64 pgoff, void *data)
1592 struct kcore_copy_info *kci = data;
1593 u64 end = start + len;
1594 struct sym_data *sdat;
1596 if (kcore_copy__map(kci, start, end, pgoff, kci->stext, kci->etext))
1599 if (kcore_copy__map(kci, start, end, pgoff, kci->first_module,
1600 kci->last_module_symbol))
1603 list_for_each_entry(sdat, &kci->syms, node) {
1604 u64 s = round_down(sdat->addr, page_size);
1606 if (kcore_copy__map(kci, start, end, pgoff, s, s + len))
1613 static int kcore_copy__read_maps(struct kcore_copy_info *kci, Elf *elf)
1615 if (elf_read_maps(elf, true, kcore_copy__read_map, kci) < 0)
1621 static void kcore_copy__find_remaps(struct kcore_copy_info *kci)
1623 struct phdr_data *p, *k = NULL;
1629 /* Find phdr that corresponds to the kernel map (contains stext) */
1630 kcore_copy__for_each_phdr(kci, p) {
1631 u64 pend = p->addr + p->len - 1;
1633 if (p->addr <= kci->stext && pend >= kci->stext) {
1642 kend = k->offset + k->len;
1644 /* Find phdrs that remap the kernel */
1645 kcore_copy__for_each_phdr(kci, p) {
1646 u64 pend = p->offset + p->len;
1651 if (p->offset >= k->offset && pend <= kend)
1656 static void kcore_copy__layout(struct kcore_copy_info *kci)
1658 struct phdr_data *p;
1661 kcore_copy__find_remaps(kci);
1663 kcore_copy__for_each_phdr(kci, p) {
1671 kcore_copy__for_each_phdr(kci, p) {
1672 struct phdr_data *k = p->remaps;
1675 p->rel = p->offset - k->offset + k->rel;
1679 static int kcore_copy__calc_maps(struct kcore_copy_info *kci, const char *dir,
1682 if (kcore_copy__parse_kallsyms(kci, dir))
1685 if (kcore_copy__parse_modules(kci, dir))
1689 kci->stext = round_down(kci->stext, page_size);
1691 kci->stext = round_down(kci->first_symbol, page_size);
1694 kci->etext = round_up(kci->etext, page_size);
1695 } else if (kci->last_symbol) {
1696 kci->etext = round_up(kci->last_symbol, page_size);
1697 kci->etext += page_size;
1700 if (kci->first_module_symbol &&
1701 (!kci->first_module || kci->first_module_symbol < kci->first_module))
1702 kci->first_module = kci->first_module_symbol;
1704 kci->first_module = round_down(kci->first_module, page_size);
1706 if (kci->last_module_symbol) {
1707 kci->last_module_symbol = round_up(kci->last_module_symbol,
1709 kci->last_module_symbol += page_size;
1712 if (!kci->stext || !kci->etext)
1715 if (kci->first_module && !kci->last_module_symbol)
1718 if (kcore_copy__read_maps(kci, elf))
1721 kcore_copy__layout(kci);
1726 static int kcore_copy__copy_file(const char *from_dir, const char *to_dir,
1729 char from_filename[PATH_MAX];
1730 char to_filename[PATH_MAX];
1732 scnprintf(from_filename, PATH_MAX, "%s/%s", from_dir, name);
1733 scnprintf(to_filename, PATH_MAX, "%s/%s", to_dir, name);
1735 return copyfile_mode(from_filename, to_filename, 0400);
1738 static int kcore_copy__unlink(const char *dir, const char *name)
1740 char filename[PATH_MAX];
1742 scnprintf(filename, PATH_MAX, "%s/%s", dir, name);
1744 return unlink(filename);
1747 static int kcore_copy__compare_fds(int from, int to)
1755 buf_from = malloc(page_size);
1756 buf_to = malloc(page_size);
1757 if (!buf_from || !buf_to)
1761 /* Use read because mmap won't work on proc files */
1762 ret = read(from, buf_from, page_size);
1771 if (readn(to, buf_to, len) != (int)len)
1774 if (memcmp(buf_from, buf_to, len))
1785 static int kcore_copy__compare_files(const char *from_filename,
1786 const char *to_filename)
1788 int from, to, err = -1;
1790 from = open(from_filename, O_RDONLY);
1794 to = open(to_filename, O_RDONLY);
1796 goto out_close_from;
1798 err = kcore_copy__compare_fds(from, to);
1806 static int kcore_copy__compare_file(const char *from_dir, const char *to_dir,
1809 char from_filename[PATH_MAX];
1810 char to_filename[PATH_MAX];
1812 scnprintf(from_filename, PATH_MAX, "%s/%s", from_dir, name);
1813 scnprintf(to_filename, PATH_MAX, "%s/%s", to_dir, name);
1815 return kcore_copy__compare_files(from_filename, to_filename);
1819 * kcore_copy - copy kallsyms, modules and kcore from one directory to another.
1820 * @from_dir: from directory
1821 * @to_dir: to directory
1823 * This function copies kallsyms, modules and kcore files from one directory to
1824 * another. kallsyms and modules are copied entirely. Only code segments are
1825 * copied from kcore. It is assumed that two segments suffice: one for the
1826 * kernel proper and one for all the modules. The code segments are determined
1827 * from kallsyms and modules files. The kernel map starts at _stext or the
1828 * lowest function symbol, and ends at _etext or the highest function symbol.
1829 * The module map starts at the lowest module address and ends at the highest
1830 * module symbol. Start addresses are rounded down to the nearest page. End
1831 * addresses are rounded up to the nearest page. An extra page is added to the
1832 * highest kernel symbol and highest module symbol to, hopefully, encompass that
1833 * symbol too. Because it contains only code sections, the resulting kcore is
1834 * unusual. One significant peculiarity is that the mapping (start -> pgoff)
1835 * is not the same for the kernel map and the modules map. That happens because
1836 * the data is copied adjacently whereas the original kcore has gaps. Finally,
1837 * kallsyms and modules files are compared with their copies to check that
1838 * modules have not been loaded or unloaded while the copies were taking place.
1840 * Return: %0 on success, %-1 on failure.
1842 int kcore_copy(const char *from_dir, const char *to_dir)
1845 struct kcore extract;
1846 int idx = 0, err = -1;
1848 struct kcore_copy_info kci = { .stext = 0, };
1849 char kcore_filename[PATH_MAX];
1850 char extract_filename[PATH_MAX];
1851 struct phdr_data *p;
1853 INIT_LIST_HEAD(&kci.phdrs);
1854 INIT_LIST_HEAD(&kci.syms);
1856 if (kcore_copy__copy_file(from_dir, to_dir, "kallsyms"))
1859 if (kcore_copy__copy_file(from_dir, to_dir, "modules"))
1860 goto out_unlink_kallsyms;
1862 scnprintf(kcore_filename, PATH_MAX, "%s/kcore", from_dir);
1863 scnprintf(extract_filename, PATH_MAX, "%s/kcore", to_dir);
1865 if (kcore__open(&kcore, kcore_filename))
1866 goto out_unlink_modules;
1868 if (kcore_copy__calc_maps(&kci, from_dir, kcore.elf))
1869 goto out_kcore_close;
1871 if (kcore__init(&extract, extract_filename, kcore.elfclass, false))
1872 goto out_kcore_close;
1874 if (kcore__copy_hdr(&kcore, &extract, kci.phnum))
1875 goto out_extract_close;
1877 offset = gelf_fsize(extract.elf, ELF_T_EHDR, 1, EV_CURRENT) +
1878 gelf_fsize(extract.elf, ELF_T_PHDR, kci.phnum, EV_CURRENT);
1879 offset = round_up(offset, page_size);
1881 kcore_copy__for_each_phdr(&kci, p) {
1882 off_t offs = p->rel + offset;
1884 if (kcore__add_phdr(&extract, idx++, offs, p->addr, p->len))
1885 goto out_extract_close;
1888 sz = kcore__write(&extract);
1889 if (sz < 0 || sz > offset)
1890 goto out_extract_close;
1892 kcore_copy__for_each_phdr(&kci, p) {
1893 off_t offs = p->rel + offset;
1897 if (copy_bytes(kcore.fd, p->offset, extract.fd, offs, p->len))
1898 goto out_extract_close;
1901 if (kcore_copy__compare_file(from_dir, to_dir, "modules"))
1902 goto out_extract_close;
1904 if (kcore_copy__compare_file(from_dir, to_dir, "kallsyms"))
1905 goto out_extract_close;
1910 kcore__close(&extract);
1912 unlink(extract_filename);
1914 kcore__close(&kcore);
1917 kcore_copy__unlink(to_dir, "modules");
1918 out_unlink_kallsyms:
1920 kcore_copy__unlink(to_dir, "kallsyms");
1922 kcore_copy__free_phdrs(&kci);
1923 kcore_copy__free_syms(&kci);
1928 int kcore_extract__create(struct kcore_extract *kce)
1931 struct kcore extract;
1933 int idx = 0, err = -1;
1934 off_t offset = page_size, sz;
1936 if (kcore__open(&kcore, kce->kcore_filename))
1939 strcpy(kce->extract_filename, PERF_KCORE_EXTRACT);
1940 if (kcore__init(&extract, kce->extract_filename, kcore.elfclass, true))
1941 goto out_kcore_close;
1943 if (kcore__copy_hdr(&kcore, &extract, count))
1944 goto out_extract_close;
1946 if (kcore__add_phdr(&extract, idx, offset, kce->addr, kce->len))
1947 goto out_extract_close;
1949 sz = kcore__write(&extract);
1950 if (sz < 0 || sz > offset)
1951 goto out_extract_close;
1953 if (copy_bytes(kcore.fd, kce->offs, extract.fd, offset, kce->len))
1954 goto out_extract_close;
1959 kcore__close(&extract);
1961 unlink(kce->extract_filename);
1963 kcore__close(&kcore);
1968 void kcore_extract__delete(struct kcore_extract *kce)
1970 unlink(kce->extract_filename);
1973 #ifdef HAVE_GELF_GETNOTE_SUPPORT
1975 * populate_sdt_note : Parse raw data and identify SDT note
1976 * @elf: elf of the opened file
1977 * @data: raw data of a section with description offset applied
1978 * @len: note description size
1979 * @type: type of the note
1980 * @sdt_notes: List to add the SDT note
1982 * Responsible for parsing the @data in section .note.stapsdt in @elf and
1983 * if its an SDT note, it appends to @sdt_notes list.
1985 static int populate_sdt_note(Elf **elf, const char *data, size_t len,
1986 struct list_head *sdt_notes)
1988 const char *provider, *name, *args;
1989 struct sdt_note *tmp = NULL;
1991 GElf_Addr base_off = 0;
1996 Elf64_Addr a64[NR_ADDR];
1997 Elf32_Addr a32[NR_ADDR];
2001 .d_buf = &buf, .d_type = ELF_T_ADDR, .d_version = EV_CURRENT,
2002 .d_size = gelf_fsize((*elf), ELF_T_ADDR, NR_ADDR, EV_CURRENT),
2003 .d_off = 0, .d_align = 0
2006 .d_buf = (void *) data, .d_type = ELF_T_ADDR,
2007 .d_version = EV_CURRENT, .d_size = dst.d_size, .d_off = 0,
2011 tmp = (struct sdt_note *)calloc(1, sizeof(struct sdt_note));
2017 INIT_LIST_HEAD(&tmp->note_list);
2019 if (len < dst.d_size + 3)
2022 /* Translation from file representation to memory representation */
2023 if (gelf_xlatetom(*elf, &dst, &src,
2024 elf_getident(*elf, NULL)[EI_DATA]) == NULL) {
2025 pr_err("gelf_xlatetom : %s\n", elf_errmsg(-1));
2029 /* Populate the fields of sdt_note */
2030 provider = data + dst.d_size;
2032 name = (const char *)memchr(provider, '\0', data + len - provider);
2036 tmp->provider = strdup(provider);
2037 if (!tmp->provider) {
2041 tmp->name = strdup(name);
2047 args = memchr(name, '\0', data + len - name);
2050 * There is no argument if:
2051 * - We reached the end of the note;
2052 * - There is not enough room to hold a potential string;
2053 * - The argument string is empty or just contains ':'.
2055 if (args == NULL || data + len - args < 2 ||
2056 args[1] == ':' || args[1] == '\0')
2059 tmp->args = strdup(++args);
2066 if (gelf_getclass(*elf) == ELFCLASS32) {
2067 memcpy(&tmp->addr, &buf, 3 * sizeof(Elf32_Addr));
2070 memcpy(&tmp->addr, &buf, 3 * sizeof(Elf64_Addr));
2074 if (!gelf_getehdr(*elf, &ehdr)) {
2075 pr_debug("%s : cannot get elf header.\n", __func__);
2080 /* Adjust the prelink effect :
2081 * Find out the .stapsdt.base section.
2082 * This scn will help us to handle prelinking (if present).
2083 * Compare the retrieved file offset of the base section with the
2084 * base address in the description of the SDT note. If its different,
2085 * then accordingly, adjust the note location.
2087 if (elf_section_by_name(*elf, &ehdr, &shdr, SDT_BASE_SCN, NULL)) {
2088 base_off = shdr.sh_offset;
2091 tmp->addr.a32[0] = tmp->addr.a32[0] + base_off -
2094 tmp->addr.a64[0] = tmp->addr.a64[0] + base_off -
2099 list_add_tail(&tmp->note_list, sdt_notes);
2107 free(tmp->provider);
2115 * construct_sdt_notes_list : constructs a list of SDT notes
2116 * @elf : elf to look into
2117 * @sdt_notes : empty list_head
2119 * Scans the sections in 'elf' for the section
2120 * .note.stapsdt. It, then calls populate_sdt_note to find
2121 * out the SDT events and populates the 'sdt_notes'.
2123 static int construct_sdt_notes_list(Elf *elf, struct list_head *sdt_notes)
2126 Elf_Scn *scn = NULL;
2129 size_t shstrndx, next;
2131 size_t name_off, desc_off, offset;
2134 if (gelf_getehdr(elf, &ehdr) == NULL) {
2138 if (elf_getshdrstrndx(elf, &shstrndx) != 0) {
2143 /* Look for the required section */
2144 scn = elf_section_by_name(elf, &ehdr, &shdr, SDT_NOTE_SCN, NULL);
2150 if ((shdr.sh_type != SHT_NOTE) || (shdr.sh_flags & SHF_ALLOC)) {
2155 data = elf_getdata(scn, NULL);
2157 /* Get the SDT notes */
2158 for (offset = 0; (next = gelf_getnote(data, offset, &nhdr, &name_off,
2159 &desc_off)) > 0; offset = next) {
2160 if (nhdr.n_namesz == sizeof(SDT_NOTE_NAME) &&
2161 !memcmp(data->d_buf + name_off, SDT_NOTE_NAME,
2162 sizeof(SDT_NOTE_NAME))) {
2163 /* Check the type of the note */
2164 if (nhdr.n_type != SDT_NOTE_TYPE)
2167 ret = populate_sdt_note(&elf, ((data->d_buf) + desc_off),
2168 nhdr.n_descsz, sdt_notes);
2173 if (list_empty(sdt_notes))
2181 * get_sdt_note_list : Wrapper to construct a list of sdt notes
2182 * @head : empty list_head
2183 * @target : file to find SDT notes from
2185 * This opens the file, initializes
2186 * the ELF and then calls construct_sdt_notes_list.
2188 int get_sdt_note_list(struct list_head *head, const char *target)
2193 fd = open(target, O_RDONLY);
2197 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
2202 ret = construct_sdt_notes_list(elf, head);
2210 * cleanup_sdt_note_list : free the sdt notes' list
2211 * @sdt_notes: sdt notes' list
2213 * Free up the SDT notes in @sdt_notes.
2214 * Returns the number of SDT notes free'd.
2216 int cleanup_sdt_note_list(struct list_head *sdt_notes)
2218 struct sdt_note *tmp, *pos;
2221 list_for_each_entry_safe(pos, tmp, sdt_notes, note_list) {
2222 list_del(&pos->note_list);
2224 free(pos->provider);
2232 * sdt_notes__get_count: Counts the number of sdt events
2233 * @start: list_head to sdt_notes list
2235 * Returns the number of SDT notes in a list
2237 int sdt_notes__get_count(struct list_head *start)
2239 struct sdt_note *sdt_ptr;
2242 list_for_each_entry(sdt_ptr, start, note_list)
2248 void symbol__elf_init(void)
2250 elf_version(EV_CURRENT);