11 #ifndef NT_GNU_BUILD_ID
12 #define NT_GNU_BUILD_ID 3
16 * elf_symtab__for_each_symbol - iterate thru all the symbols
18 * @syms: struct elf_symtab instance to iterate
20 * @sym: GElf_Sym iterator
22 #define elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) \
23 for (idx = 0, gelf_getsym(syms, idx, &sym);\
25 idx++, gelf_getsym(syms, idx, &sym))
27 static inline uint8_t elf_sym__type(const GElf_Sym *sym)
29 return GELF_ST_TYPE(sym->st_info);
32 static inline int elf_sym__is_function(const GElf_Sym *sym)
34 return elf_sym__type(sym) == STT_FUNC &&
36 sym->st_shndx != SHN_UNDEF;
39 static inline bool elf_sym__is_object(const GElf_Sym *sym)
41 return elf_sym__type(sym) == STT_OBJECT &&
43 sym->st_shndx != SHN_UNDEF;
46 static inline int elf_sym__is_label(const GElf_Sym *sym)
48 return elf_sym__type(sym) == STT_NOTYPE &&
50 sym->st_shndx != SHN_UNDEF &&
51 sym->st_shndx != SHN_ABS;
54 static bool elf_sym__is_a(GElf_Sym *sym, enum map_type type)
58 return elf_sym__is_function(sym);
60 return elf_sym__is_object(sym);
66 static inline const char *elf_sym__name(const GElf_Sym *sym,
67 const Elf_Data *symstrs)
69 return symstrs->d_buf + sym->st_name;
72 static inline const char *elf_sec__name(const GElf_Shdr *shdr,
73 const Elf_Data *secstrs)
75 return secstrs->d_buf + shdr->sh_name;
78 static inline int elf_sec__is_text(const GElf_Shdr *shdr,
79 const Elf_Data *secstrs)
81 return strstr(elf_sec__name(shdr, secstrs), "text") != NULL;
84 static inline bool elf_sec__is_data(const GElf_Shdr *shdr,
85 const Elf_Data *secstrs)
87 return strstr(elf_sec__name(shdr, secstrs), "data") != NULL;
90 static bool elf_sec__is_a(GElf_Shdr *shdr, Elf_Data *secstrs,
95 return elf_sec__is_text(shdr, secstrs);
97 return elf_sec__is_data(shdr, secstrs);
103 static size_t elf_addr_to_index(Elf *elf, GElf_Addr addr)
109 while ((sec = elf_nextscn(elf, sec)) != NULL) {
110 gelf_getshdr(sec, &shdr);
112 if ((addr >= shdr.sh_addr) &&
113 (addr < (shdr.sh_addr + shdr.sh_size)))
122 static Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep,
123 GElf_Shdr *shp, const char *name,
129 /* Elf is corrupted/truncated, avoid calling elf_strptr. */
130 if (!elf_rawdata(elf_getscn(elf, ep->e_shstrndx), NULL))
133 while ((sec = elf_nextscn(elf, sec)) != NULL) {
136 gelf_getshdr(sec, shp);
137 str = elf_strptr(elf, ep->e_shstrndx, shp->sh_name);
138 if (!strcmp(name, str)) {
149 #define elf_section__for_each_rel(reldata, pos, pos_mem, idx, nr_entries) \
150 for (idx = 0, pos = gelf_getrel(reldata, 0, &pos_mem); \
152 ++idx, pos = gelf_getrel(reldata, idx, &pos_mem))
154 #define elf_section__for_each_rela(reldata, pos, pos_mem, idx, nr_entries) \
155 for (idx = 0, pos = gelf_getrela(reldata, 0, &pos_mem); \
157 ++idx, pos = gelf_getrela(reldata, idx, &pos_mem))
160 * We need to check if we have a .dynsym, so that we can handle the
161 * .plt, synthesizing its symbols, that aren't on the symtabs (be it
162 * .dynsym or .symtab).
163 * And always look at the original dso, not at debuginfo packages, that
164 * have the PLT data stripped out (shdr_rel_plt.sh_type == SHT_NOBITS).
166 int dso__synthesize_plt_symbols(struct dso *dso, struct symsrc *ss, struct map *map,
167 symbol_filter_t filter)
169 uint32_t nr_rel_entries, idx;
174 GElf_Shdr shdr_rel_plt, shdr_dynsym;
175 Elf_Data *reldata, *syms, *symstrs;
176 Elf_Scn *scn_plt_rel, *scn_symstrs, *scn_dynsym;
179 char sympltname[1024];
181 int nr = 0, symidx, err = 0;
189 scn_dynsym = ss->dynsym;
190 shdr_dynsym = ss->dynshdr;
191 dynsym_idx = ss->dynsym_idx;
193 if (scn_dynsym == NULL)
196 scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt,
198 if (scn_plt_rel == NULL) {
199 scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt,
201 if (scn_plt_rel == NULL)
207 if (shdr_rel_plt.sh_link != dynsym_idx)
210 if (elf_section_by_name(elf, &ehdr, &shdr_plt, ".plt", NULL) == NULL)
214 * Fetch the relocation section to find the idxes to the GOT
215 * and the symbols in the .dynsym they refer to.
217 reldata = elf_getdata(scn_plt_rel, NULL);
221 syms = elf_getdata(scn_dynsym, NULL);
225 scn_symstrs = elf_getscn(elf, shdr_dynsym.sh_link);
226 if (scn_symstrs == NULL)
229 symstrs = elf_getdata(scn_symstrs, NULL);
233 if (symstrs->d_size == 0)
236 nr_rel_entries = shdr_rel_plt.sh_size / shdr_rel_plt.sh_entsize;
237 plt_offset = shdr_plt.sh_offset;
239 if (shdr_rel_plt.sh_type == SHT_RELA) {
240 GElf_Rela pos_mem, *pos;
242 elf_section__for_each_rela(reldata, pos, pos_mem, idx,
244 symidx = GELF_R_SYM(pos->r_info);
245 plt_offset += shdr_plt.sh_entsize;
246 gelf_getsym(syms, symidx, &sym);
247 snprintf(sympltname, sizeof(sympltname),
248 "%s@plt", elf_sym__name(&sym, symstrs));
250 f = symbol__new(plt_offset, shdr_plt.sh_entsize,
251 STB_GLOBAL, sympltname);
255 if (filter && filter(map, f))
258 symbols__insert(&dso->symbols[map->type], f);
262 } else if (shdr_rel_plt.sh_type == SHT_REL) {
263 GElf_Rel pos_mem, *pos;
264 elf_section__for_each_rel(reldata, pos, pos_mem, idx,
266 symidx = GELF_R_SYM(pos->r_info);
267 plt_offset += shdr_plt.sh_entsize;
268 gelf_getsym(syms, symidx, &sym);
269 snprintf(sympltname, sizeof(sympltname),
270 "%s@plt", elf_sym__name(&sym, symstrs));
272 f = symbol__new(plt_offset, shdr_plt.sh_entsize,
273 STB_GLOBAL, sympltname);
277 if (filter && filter(map, f))
280 symbols__insert(&dso->symbols[map->type], f);
290 pr_debug("%s: problems reading %s PLT info.\n",
291 __func__, dso->long_name);
296 * Align offset to 4 bytes as needed for note name and descriptor data.
298 #define NOTE_ALIGN(n) (((n) + 3) & -4U)
300 static int elf_read_build_id(Elf *elf, void *bf, size_t size)
310 if (size < BUILD_ID_SIZE)
317 if (gelf_getehdr(elf, &ehdr) == NULL) {
318 pr_err("%s: cannot get elf header.\n", __func__);
323 * Check following sections for notes:
324 * '.note.gnu.build-id'
326 * '.note' (VDSO specific)
329 sec = elf_section_by_name(elf, &ehdr, &shdr,
330 ".note.gnu.build-id", NULL);
334 sec = elf_section_by_name(elf, &ehdr, &shdr,
339 sec = elf_section_by_name(elf, &ehdr, &shdr,
348 data = elf_getdata(sec, NULL);
353 while (ptr < (data->d_buf + data->d_size)) {
354 GElf_Nhdr *nhdr = ptr;
355 size_t namesz = NOTE_ALIGN(nhdr->n_namesz),
356 descsz = NOTE_ALIGN(nhdr->n_descsz);
359 ptr += sizeof(*nhdr);
362 if (nhdr->n_type == NT_GNU_BUILD_ID &&
363 nhdr->n_namesz == sizeof("GNU")) {
364 if (memcmp(name, "GNU", sizeof("GNU")) == 0) {
365 size_t sz = min(size, descsz);
367 memset(bf + sz, 0, size - sz);
379 int filename__read_build_id(const char *filename, void *bf, size_t size)
384 if (size < BUILD_ID_SIZE)
387 fd = open(filename, O_RDONLY);
391 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
393 pr_debug2("%s: cannot read %s ELF file.\n", __func__, filename);
397 err = elf_read_build_id(elf, bf, size);
406 int sysfs__read_build_id(const char *filename, void *build_id, size_t size)
410 if (size < BUILD_ID_SIZE)
413 fd = open(filename, O_RDONLY);
420 size_t namesz, descsz;
422 if (read(fd, &nhdr, sizeof(nhdr)) != sizeof(nhdr))
425 namesz = NOTE_ALIGN(nhdr.n_namesz);
426 descsz = NOTE_ALIGN(nhdr.n_descsz);
427 if (nhdr.n_type == NT_GNU_BUILD_ID &&
428 nhdr.n_namesz == sizeof("GNU")) {
429 if (read(fd, bf, namesz) != (ssize_t)namesz)
431 if (memcmp(bf, "GNU", sizeof("GNU")) == 0) {
432 size_t sz = min(descsz, size);
433 if (read(fd, build_id, sz) == (ssize_t)sz) {
434 memset(build_id + sz, 0, size - sz);
438 } else if (read(fd, bf, descsz) != (ssize_t)descsz)
441 int n = namesz + descsz;
442 if (read(fd, bf, n) != n)
451 int filename__read_debuglink(const char *filename, char *debuglink,
462 fd = open(filename, O_RDONLY);
466 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
468 pr_debug2("%s: cannot read %s ELF file.\n", __func__, filename);
476 if (gelf_getehdr(elf, &ehdr) == NULL) {
477 pr_err("%s: cannot get elf header.\n", __func__);
481 sec = elf_section_by_name(elf, &ehdr, &shdr,
482 ".gnu_debuglink", NULL);
486 data = elf_getdata(sec, NULL);
490 /* the start of this section is a zero-terminated string */
491 strncpy(debuglink, data->d_buf, size);
501 static int dso__swap_init(struct dso *dso, unsigned char eidata)
503 static unsigned int const endian = 1;
505 dso->needs_swap = DSO_SWAP__NO;
509 /* We are big endian, DSO is little endian. */
510 if (*(unsigned char const *)&endian != 1)
511 dso->needs_swap = DSO_SWAP__YES;
515 /* We are little endian, DSO is big endian. */
516 if (*(unsigned char const *)&endian != 0)
517 dso->needs_swap = DSO_SWAP__YES;
521 pr_err("unrecognized DSO data encoding %d\n", eidata);
528 bool symsrc__possibly_runtime(struct symsrc *ss)
530 return ss->dynsym || ss->opdsec;
533 bool symsrc__has_symtab(struct symsrc *ss)
535 return ss->symtab != NULL;
538 void symsrc__destroy(struct symsrc *ss)
545 int symsrc__init(struct symsrc *ss, struct dso *dso, const char *name,
546 enum dso_binary_type type)
553 fd = open(name, O_RDONLY);
557 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
559 pr_debug("%s: cannot read %s ELF file.\n", __func__, name);
563 if (gelf_getehdr(elf, &ehdr) == NULL) {
564 pr_debug("%s: cannot get elf header.\n", __func__);
568 if (dso__swap_init(dso, ehdr.e_ident[EI_DATA]))
571 /* Always reject images with a mismatched build-id: */
572 if (dso->has_build_id) {
573 u8 build_id[BUILD_ID_SIZE];
575 if (elf_read_build_id(elf, build_id, BUILD_ID_SIZE) < 0)
578 if (!dso__build_id_equal(dso, build_id))
582 ss->symtab = elf_section_by_name(elf, &ehdr, &ss->symshdr, ".symtab",
584 if (ss->symshdr.sh_type != SHT_SYMTAB)
588 ss->dynsym = elf_section_by_name(elf, &ehdr, &ss->dynshdr, ".dynsym",
590 if (ss->dynshdr.sh_type != SHT_DYNSYM)
594 ss->opdsec = elf_section_by_name(elf, &ehdr, &ss->opdshdr, ".opd",
596 if (ss->opdshdr.sh_type != SHT_PROGBITS)
599 if (dso->kernel == DSO_TYPE_USER) {
601 ss->adjust_symbols = (ehdr.e_type == ET_EXEC ||
602 ehdr.e_type == ET_REL ||
603 elf_section_by_name(elf, &ehdr, &shdr,
607 ss->adjust_symbols = ehdr.e_type == ET_EXEC ||
608 ehdr.e_type == ET_REL;
611 ss->name = strdup(name);
630 * ref_reloc_sym_not_found - has kernel relocation symbol been found.
631 * @kmap: kernel maps and relocation reference symbol
633 * This function returns %true if we are dealing with the kernel maps and the
634 * relocation reference symbol has not yet been found. Otherwise %false is
637 static bool ref_reloc_sym_not_found(struct kmap *kmap)
639 return kmap && kmap->ref_reloc_sym && kmap->ref_reloc_sym->name &&
640 !kmap->ref_reloc_sym->unrelocated_addr;
644 * ref_reloc - kernel relocation offset.
645 * @kmap: kernel maps and relocation reference symbol
647 * This function returns the offset of kernel addresses as determined by using
648 * the relocation reference symbol i.e. if the kernel has not been relocated
649 * then the return value is zero.
651 static u64 ref_reloc(struct kmap *kmap)
653 if (kmap && kmap->ref_reloc_sym &&
654 kmap->ref_reloc_sym->unrelocated_addr)
655 return kmap->ref_reloc_sym->addr -
656 kmap->ref_reloc_sym->unrelocated_addr;
660 int dso__load_sym(struct dso *dso, struct map *map,
661 struct symsrc *syms_ss, struct symsrc *runtime_ss,
662 symbol_filter_t filter, int kmodule)
664 struct kmap *kmap = dso->kernel ? map__kmap(map) : NULL;
665 struct map *curr_map = map;
666 struct dso *curr_dso = dso;
667 Elf_Data *symstrs, *secstrs;
673 Elf_Data *syms, *opddata = NULL;
675 Elf_Scn *sec, *sec_strndx;
678 bool remap_kernel = false, adjust_kernel_syms = false;
680 dso->symtab_type = syms_ss->type;
681 dso->rel = syms_ss->ehdr.e_type == ET_REL;
684 * Modules may already have symbols from kallsyms, but those symbols
685 * have the wrong values for the dso maps, so remove them.
687 if (kmodule && syms_ss->symtab)
688 symbols__delete(&dso->symbols[map->type]);
690 if (!syms_ss->symtab) {
691 syms_ss->symtab = syms_ss->dynsym;
692 syms_ss->symshdr = syms_ss->dynshdr;
696 ehdr = syms_ss->ehdr;
697 sec = syms_ss->symtab;
698 shdr = syms_ss->symshdr;
700 if (runtime_ss->opdsec)
701 opddata = elf_rawdata(runtime_ss->opdsec, NULL);
703 syms = elf_getdata(sec, NULL);
707 sec = elf_getscn(elf, shdr.sh_link);
711 symstrs = elf_getdata(sec, NULL);
715 sec_strndx = elf_getscn(elf, ehdr.e_shstrndx);
716 if (sec_strndx == NULL)
719 secstrs = elf_getdata(sec_strndx, NULL);
723 nr_syms = shdr.sh_size / shdr.sh_entsize;
725 memset(&sym, 0, sizeof(sym));
728 * The kernel relocation symbol is needed in advance in order to adjust
729 * kernel maps correctly.
731 if (ref_reloc_sym_not_found(kmap)) {
732 elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) {
733 const char *elf_name = elf_sym__name(&sym, symstrs);
735 if (strcmp(elf_name, kmap->ref_reloc_sym->name))
737 kmap->ref_reloc_sym->unrelocated_addr = sym.st_value;
742 dso->adjust_symbols = runtime_ss->adjust_symbols || ref_reloc(kmap);
744 * Initial kernel and module mappings do not map to the dso. For
745 * function mappings, flag the fixups.
747 if (map->type == MAP__FUNCTION && (dso->kernel || kmodule)) {
749 adjust_kernel_syms = dso->adjust_symbols;
751 elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) {
753 const char *elf_name = elf_sym__name(&sym, symstrs);
754 char *demangled = NULL;
755 int is_label = elf_sym__is_label(&sym);
756 const char *section_name;
757 bool used_opd = false;
759 if (!is_label && !elf_sym__is_a(&sym, map->type))
762 /* Reject ARM ELF "mapping symbols": these aren't unique and
763 * don't identify functions, so will confuse the profile
765 if (ehdr.e_machine == EM_ARM) {
766 if (!strcmp(elf_name, "$a") ||
767 !strcmp(elf_name, "$d") ||
768 !strcmp(elf_name, "$t"))
772 if (runtime_ss->opdsec && sym.st_shndx == runtime_ss->opdidx) {
773 u32 offset = sym.st_value - syms_ss->opdshdr.sh_addr;
774 u64 *opd = opddata->d_buf + offset;
775 sym.st_value = DSO__SWAP(dso, u64, *opd);
776 sym.st_shndx = elf_addr_to_index(runtime_ss->elf,
781 * When loading symbols in a data mapping, ABS symbols (which
782 * has a value of SHN_ABS in its st_shndx) failed at
783 * elf_getscn(). And it marks the loading as a failure so
784 * already loaded symbols cannot be fixed up.
786 * I'm not sure what should be done. Just ignore them for now.
789 if (sym.st_shndx == SHN_ABS)
792 sec = elf_getscn(runtime_ss->elf, sym.st_shndx);
796 gelf_getshdr(sec, &shdr);
798 if (is_label && !elf_sec__is_a(&shdr, secstrs, map->type))
801 section_name = elf_sec__name(&shdr, secstrs);
803 /* On ARM, symbols for thumb functions have 1 added to
804 * the symbol address as a flag - remove it */
805 if ((ehdr.e_machine == EM_ARM) &&
806 (map->type == MAP__FUNCTION) &&
810 if (dso->kernel || kmodule) {
811 char dso_name[PATH_MAX];
813 /* Adjust symbol to map to file offset */
814 if (adjust_kernel_syms)
815 sym.st_value -= shdr.sh_addr - shdr.sh_offset;
817 if (strcmp(section_name,
818 (curr_dso->short_name +
819 dso->short_name_len)) == 0)
822 if (strcmp(section_name, ".text") == 0) {
824 * The initial kernel mapping is based on
825 * kallsyms and identity maps. Overwrite it to
826 * map to the kernel dso.
828 if (remap_kernel && dso->kernel) {
829 remap_kernel = false;
830 map->start = shdr.sh_addr +
832 map->end = map->start + shdr.sh_size;
833 map->pgoff = shdr.sh_offset;
834 map->map_ip = map__map_ip;
835 map->unmap_ip = map__unmap_ip;
836 /* Ensure maps are correctly ordered */
837 map_groups__remove(kmap->kmaps, map);
838 map_groups__insert(kmap->kmaps, map);
842 * The initial module mapping is based on
843 * /proc/modules mapped to offset zero.
844 * Overwrite it to map to the module dso.
846 if (remap_kernel && kmodule) {
847 remap_kernel = false;
848 map->pgoff = shdr.sh_offset;
859 snprintf(dso_name, sizeof(dso_name),
860 "%s%s", dso->short_name, section_name);
862 curr_map = map_groups__find_by_name(kmap->kmaps, map->type, dso_name);
863 if (curr_map == NULL) {
864 u64 start = sym.st_value;
867 start += map->start + shdr.sh_offset;
869 curr_dso = dso__new(dso_name);
870 if (curr_dso == NULL)
872 curr_dso->kernel = dso->kernel;
873 curr_dso->long_name = dso->long_name;
874 curr_dso->long_name_len = dso->long_name_len;
875 curr_map = map__new2(start, curr_dso,
877 if (curr_map == NULL) {
878 dso__delete(curr_dso);
881 if (adjust_kernel_syms) {
882 curr_map->start = shdr.sh_addr +
884 curr_map->end = curr_map->start +
886 curr_map->pgoff = shdr.sh_offset;
888 curr_map->map_ip = identity__map_ip;
889 curr_map->unmap_ip = identity__map_ip;
891 curr_dso->symtab_type = dso->symtab_type;
892 map_groups__insert(kmap->kmaps, curr_map);
893 dsos__add(&dso->node, curr_dso);
894 dso__set_loaded(curr_dso, map->type);
896 curr_dso = curr_map->dso;
901 if ((used_opd && runtime_ss->adjust_symbols)
902 || (!used_opd && syms_ss->adjust_symbols)) {
903 pr_debug4("%s: adjusting symbol: st_value: %#" PRIx64 " "
904 "sh_addr: %#" PRIx64 " sh_offset: %#" PRIx64 "\n", __func__,
905 (u64)sym.st_value, (u64)shdr.sh_addr,
906 (u64)shdr.sh_offset);
907 sym.st_value -= shdr.sh_addr - shdr.sh_offset;
910 * We need to figure out if the object was created from C++ sources
911 * DWARF DW_compile_unit has this, but we don't always have access
914 if (symbol_conf.demangle) {
915 demangled = bfd_demangle(NULL, elf_name,
916 DMGL_PARAMS | DMGL_ANSI);
917 if (demangled != NULL)
918 elf_name = demangled;
921 f = symbol__new(sym.st_value, sym.st_size,
922 GELF_ST_BIND(sym.st_info), elf_name);
927 if (filter && filter(curr_map, f))
930 symbols__insert(&curr_dso->symbols[curr_map->type], f);
936 * For misannotated, zeroed, ASM function sizes.
939 symbols__fixup_duplicate(&dso->symbols[map->type]);
940 symbols__fixup_end(&dso->symbols[map->type]);
943 * We need to fixup this here too because we create new
944 * maps here, for things like vsyscall sections.
946 __map_groups__fixup_end(kmap->kmaps, map->type);
954 static int elf_read_maps(Elf *elf, bool exe, mapfn_t mapfn, void *data)
961 if (elf_getphdrnum(elf, &phdrnum))
964 for (i = 0; i < phdrnum; i++) {
965 if (gelf_getphdr(elf, i, &phdr) == NULL)
967 if (phdr.p_type != PT_LOAD)
970 if (!(phdr.p_flags & PF_X))
973 if (!(phdr.p_flags & PF_R))
976 sz = min(phdr.p_memsz, phdr.p_filesz);
979 err = mapfn(phdr.p_vaddr, sz, phdr.p_offset, data);
986 int file__read_maps(int fd, bool exe, mapfn_t mapfn, void *data,
992 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
997 *is_64_bit = (gelf_getclass(elf) == ELFCLASS64);
999 err = elf_read_maps(elf, exe, mapfn, data);
1005 void symbol__elf_init(void)
1007 elf_version(EV_CURRENT);