14 #ifndef NT_GNU_BUILD_ID
15 #define NT_GNU_BUILD_ID 3
19 * elf_symtab__for_each_symbol - iterate thru all the symbols
21 * @syms: struct elf_symtab instance to iterate
23 * @sym: GElf_Sym iterator
25 #define elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) \
26 for (idx = 0, gelf_getsym(syms, idx, &sym);\
28 idx++, gelf_getsym(syms, idx, &sym))
30 static inline uint8_t elf_sym__type(const GElf_Sym *sym)
32 return GELF_ST_TYPE(sym->st_info);
35 static inline int elf_sym__is_function(const GElf_Sym *sym)
37 return elf_sym__type(sym) == STT_FUNC &&
39 sym->st_shndx != SHN_UNDEF;
42 static inline bool elf_sym__is_object(const GElf_Sym *sym)
44 return elf_sym__type(sym) == STT_OBJECT &&
46 sym->st_shndx != SHN_UNDEF;
49 static inline int elf_sym__is_label(const GElf_Sym *sym)
51 return elf_sym__type(sym) == STT_NOTYPE &&
53 sym->st_shndx != SHN_UNDEF &&
54 sym->st_shndx != SHN_ABS;
57 static bool elf_sym__is_a(GElf_Sym *sym, enum map_type type)
61 return elf_sym__is_function(sym);
63 return elf_sym__is_object(sym);
69 static inline const char *elf_sym__name(const GElf_Sym *sym,
70 const Elf_Data *symstrs)
72 return symstrs->d_buf + sym->st_name;
75 static inline const char *elf_sec__name(const GElf_Shdr *shdr,
76 const Elf_Data *secstrs)
78 return secstrs->d_buf + shdr->sh_name;
81 static inline int elf_sec__is_text(const GElf_Shdr *shdr,
82 const Elf_Data *secstrs)
84 return strstr(elf_sec__name(shdr, secstrs), "text") != NULL;
87 static inline bool elf_sec__is_data(const GElf_Shdr *shdr,
88 const Elf_Data *secstrs)
90 return strstr(elf_sec__name(shdr, secstrs), "data") != NULL;
93 static bool elf_sec__is_a(GElf_Shdr *shdr, Elf_Data *secstrs,
98 return elf_sec__is_text(shdr, secstrs);
100 return elf_sec__is_data(shdr, secstrs);
106 static size_t elf_addr_to_index(Elf *elf, GElf_Addr addr)
112 while ((sec = elf_nextscn(elf, sec)) != NULL) {
113 gelf_getshdr(sec, &shdr);
115 if ((addr >= shdr.sh_addr) &&
116 (addr < (shdr.sh_addr + shdr.sh_size)))
125 static Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep,
126 GElf_Shdr *shp, const char *name,
132 /* Elf is corrupted/truncated, avoid calling elf_strptr. */
133 if (!elf_rawdata(elf_getscn(elf, ep->e_shstrndx), NULL))
136 while ((sec = elf_nextscn(elf, sec)) != NULL) {
139 gelf_getshdr(sec, shp);
140 str = elf_strptr(elf, ep->e_shstrndx, shp->sh_name);
141 if (!strcmp(name, str)) {
152 #define elf_section__for_each_rel(reldata, pos, pos_mem, idx, nr_entries) \
153 for (idx = 0, pos = gelf_getrel(reldata, 0, &pos_mem); \
155 ++idx, pos = gelf_getrel(reldata, idx, &pos_mem))
157 #define elf_section__for_each_rela(reldata, pos, pos_mem, idx, nr_entries) \
158 for (idx = 0, pos = gelf_getrela(reldata, 0, &pos_mem); \
160 ++idx, pos = gelf_getrela(reldata, idx, &pos_mem))
163 * We need to check if we have a .dynsym, so that we can handle the
164 * .plt, synthesizing its symbols, that aren't on the symtabs (be it
165 * .dynsym or .symtab).
166 * And always look at the original dso, not at debuginfo packages, that
167 * have the PLT data stripped out (shdr_rel_plt.sh_type == SHT_NOBITS).
169 int dso__synthesize_plt_symbols(struct dso *dso, char *name, struct map *map,
170 symbol_filter_t filter)
172 uint32_t nr_rel_entries, idx;
177 GElf_Shdr shdr_rel_plt, shdr_dynsym;
178 Elf_Data *reldata, *syms, *symstrs;
179 Elf_Scn *scn_plt_rel, *scn_symstrs, *scn_dynsym;
182 char sympltname[1024];
184 int nr = 0, symidx, fd, err = 0;
186 fd = open(name, O_RDONLY);
190 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
194 if (gelf_getehdr(elf, &ehdr) == NULL)
197 scn_dynsym = elf_section_by_name(elf, &ehdr, &shdr_dynsym,
198 ".dynsym", &dynsym_idx);
199 if (scn_dynsym == NULL)
202 scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt,
204 if (scn_plt_rel == NULL) {
205 scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt,
207 if (scn_plt_rel == NULL)
213 if (shdr_rel_plt.sh_link != dynsym_idx)
216 if (elf_section_by_name(elf, &ehdr, &shdr_plt, ".plt", NULL) == NULL)
220 * Fetch the relocation section to find the idxes to the GOT
221 * and the symbols in the .dynsym they refer to.
223 reldata = elf_getdata(scn_plt_rel, NULL);
227 syms = elf_getdata(scn_dynsym, NULL);
231 scn_symstrs = elf_getscn(elf, shdr_dynsym.sh_link);
232 if (scn_symstrs == NULL)
235 symstrs = elf_getdata(scn_symstrs, NULL);
239 if (symstrs->d_size == 0)
242 nr_rel_entries = shdr_rel_plt.sh_size / shdr_rel_plt.sh_entsize;
243 plt_offset = shdr_plt.sh_offset;
245 if (shdr_rel_plt.sh_type == SHT_RELA) {
246 GElf_Rela pos_mem, *pos;
248 elf_section__for_each_rela(reldata, pos, pos_mem, idx,
250 symidx = GELF_R_SYM(pos->r_info);
251 plt_offset += shdr_plt.sh_entsize;
252 gelf_getsym(syms, symidx, &sym);
253 snprintf(sympltname, sizeof(sympltname),
254 "%s@plt", elf_sym__name(&sym, symstrs));
256 f = symbol__new(plt_offset, shdr_plt.sh_entsize,
257 STB_GLOBAL, sympltname);
261 if (filter && filter(map, f))
264 symbols__insert(&dso->symbols[map->type], f);
268 } else if (shdr_rel_plt.sh_type == SHT_REL) {
269 GElf_Rel pos_mem, *pos;
270 elf_section__for_each_rel(reldata, pos, pos_mem, idx,
272 symidx = GELF_R_SYM(pos->r_info);
273 plt_offset += shdr_plt.sh_entsize;
274 gelf_getsym(syms, symidx, &sym);
275 snprintf(sympltname, sizeof(sympltname),
276 "%s@plt", elf_sym__name(&sym, symstrs));
278 f = symbol__new(plt_offset, shdr_plt.sh_entsize,
279 STB_GLOBAL, sympltname);
283 if (filter && filter(map, f))
286 symbols__insert(&dso->symbols[map->type], f);
301 pr_debug("%s: problems reading %s PLT info.\n",
302 __func__, dso->long_name);
307 * Align offset to 4 bytes as needed for note name and descriptor data.
309 #define NOTE_ALIGN(n) (((n) + 3) & -4U)
311 static int elf_read_build_id(Elf *elf, void *bf, size_t size)
321 if (size < BUILD_ID_SIZE)
328 if (gelf_getehdr(elf, &ehdr) == NULL) {
329 pr_err("%s: cannot get elf header.\n", __func__);
334 * Check following sections for notes:
335 * '.note.gnu.build-id'
337 * '.note' (VDSO specific)
340 sec = elf_section_by_name(elf, &ehdr, &shdr,
341 ".note.gnu.build-id", NULL);
345 sec = elf_section_by_name(elf, &ehdr, &shdr,
350 sec = elf_section_by_name(elf, &ehdr, &shdr,
359 data = elf_getdata(sec, NULL);
364 while (ptr < (data->d_buf + data->d_size)) {
365 GElf_Nhdr *nhdr = ptr;
366 size_t namesz = NOTE_ALIGN(nhdr->n_namesz),
367 descsz = NOTE_ALIGN(nhdr->n_descsz);
370 ptr += sizeof(*nhdr);
373 if (nhdr->n_type == NT_GNU_BUILD_ID &&
374 nhdr->n_namesz == sizeof("GNU")) {
375 if (memcmp(name, "GNU", sizeof("GNU")) == 0) {
376 size_t sz = min(size, descsz);
378 memset(bf + sz, 0, size - sz);
390 int filename__read_build_id(const char *filename, void *bf, size_t size)
395 if (size < BUILD_ID_SIZE)
398 fd = open(filename, O_RDONLY);
402 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
404 pr_debug2("%s: cannot read %s ELF file.\n", __func__, filename);
408 err = elf_read_build_id(elf, bf, size);
417 int sysfs__read_build_id(const char *filename, void *build_id, size_t size)
421 if (size < BUILD_ID_SIZE)
424 fd = open(filename, O_RDONLY);
431 size_t namesz, descsz;
433 if (read(fd, &nhdr, sizeof(nhdr)) != sizeof(nhdr))
436 namesz = NOTE_ALIGN(nhdr.n_namesz);
437 descsz = NOTE_ALIGN(nhdr.n_descsz);
438 if (nhdr.n_type == NT_GNU_BUILD_ID &&
439 nhdr.n_namesz == sizeof("GNU")) {
440 if (read(fd, bf, namesz) != (ssize_t)namesz)
442 if (memcmp(bf, "GNU", sizeof("GNU")) == 0) {
443 size_t sz = min(descsz, size);
444 if (read(fd, build_id, sz) == (ssize_t)sz) {
445 memset(build_id + sz, 0, size - sz);
449 } else if (read(fd, bf, descsz) != (ssize_t)descsz)
452 int n = namesz + descsz;
453 if (read(fd, bf, n) != n)
462 int filename__read_debuglink(const char *filename, char *debuglink,
473 fd = open(filename, O_RDONLY);
477 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
479 pr_debug2("%s: cannot read %s ELF file.\n", __func__, filename);
487 if (gelf_getehdr(elf, &ehdr) == NULL) {
488 pr_err("%s: cannot get elf header.\n", __func__);
492 sec = elf_section_by_name(elf, &ehdr, &shdr,
493 ".gnu_debuglink", NULL);
497 data = elf_getdata(sec, NULL);
501 /* the start of this section is a zero-terminated string */
502 strncpy(debuglink, data->d_buf, size);
512 static int dso__swap_init(struct dso *dso, unsigned char eidata)
514 static unsigned int const endian = 1;
516 dso->needs_swap = DSO_SWAP__NO;
520 /* We are big endian, DSO is little endian. */
521 if (*(unsigned char const *)&endian != 1)
522 dso->needs_swap = DSO_SWAP__YES;
526 /* We are little endian, DSO is big endian. */
527 if (*(unsigned char const *)&endian != 0)
528 dso->needs_swap = DSO_SWAP__YES;
532 pr_err("unrecognized DSO data encoding %d\n", eidata);
539 int dso__load_sym(struct dso *dso, struct map *map, const char *name, int fd,
540 symbol_filter_t filter, int kmodule, int want_symtab)
542 struct kmap *kmap = dso->kernel ? map__kmap(map) : NULL;
543 struct map *curr_map = map;
544 struct dso *curr_dso = dso;
545 Elf_Data *symstrs, *secstrs;
550 GElf_Shdr shdr, opdshdr;
551 Elf_Data *syms, *opddata = NULL;
553 Elf_Scn *sec, *sec_strndx, *opdsec;
558 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
560 pr_debug("%s: cannot read %s ELF file.\n", __func__, name);
564 if (gelf_getehdr(elf, &ehdr) == NULL) {
565 pr_debug("%s: cannot get elf header.\n", __func__);
569 if (dso__swap_init(dso, ehdr.e_ident[EI_DATA]))
572 /* Always reject images with a mismatched build-id: */
573 if (dso->has_build_id) {
574 u8 build_id[BUILD_ID_SIZE];
576 if (elf_read_build_id(elf, build_id, BUILD_ID_SIZE) < 0)
579 if (!dso__build_id_equal(dso, build_id))
583 sec = elf_section_by_name(elf, &ehdr, &shdr, ".symtab", NULL);
588 sec = elf_section_by_name(elf, &ehdr, &shdr, ".dynsym", NULL);
593 opdsec = elf_section_by_name(elf, &ehdr, &opdshdr, ".opd", &opdidx);
594 if (opdshdr.sh_type != SHT_PROGBITS)
597 opddata = elf_rawdata(opdsec, NULL);
599 syms = elf_getdata(sec, NULL);
603 sec = elf_getscn(elf, shdr.sh_link);
607 symstrs = elf_getdata(sec, NULL);
611 sec_strndx = elf_getscn(elf, ehdr.e_shstrndx);
612 if (sec_strndx == NULL)
615 secstrs = elf_getdata(sec_strndx, NULL);
619 nr_syms = shdr.sh_size / shdr.sh_entsize;
621 memset(&sym, 0, sizeof(sym));
622 if (dso->kernel == DSO_TYPE_USER) {
623 dso->adjust_symbols = (ehdr.e_type == ET_EXEC ||
624 elf_section_by_name(elf, &ehdr, &shdr,
628 dso->adjust_symbols = 0;
630 elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) {
632 const char *elf_name = elf_sym__name(&sym, symstrs);
633 char *demangled = NULL;
634 int is_label = elf_sym__is_label(&sym);
635 const char *section_name;
637 if (kmap && kmap->ref_reloc_sym && kmap->ref_reloc_sym->name &&
638 strcmp(elf_name, kmap->ref_reloc_sym->name) == 0)
639 kmap->ref_reloc_sym->unrelocated_addr = sym.st_value;
641 if (!is_label && !elf_sym__is_a(&sym, map->type))
644 /* Reject ARM ELF "mapping symbols": these aren't unique and
645 * don't identify functions, so will confuse the profile
647 if (ehdr.e_machine == EM_ARM) {
648 if (!strcmp(elf_name, "$a") ||
649 !strcmp(elf_name, "$d") ||
650 !strcmp(elf_name, "$t"))
654 if (opdsec && sym.st_shndx == opdidx) {
655 u32 offset = sym.st_value - opdshdr.sh_addr;
656 u64 *opd = opddata->d_buf + offset;
657 sym.st_value = DSO__SWAP(dso, u64, *opd);
658 sym.st_shndx = elf_addr_to_index(elf, sym.st_value);
661 sec = elf_getscn(elf, sym.st_shndx);
665 gelf_getshdr(sec, &shdr);
667 if (is_label && !elf_sec__is_a(&shdr, secstrs, map->type))
670 section_name = elf_sec__name(&shdr, secstrs);
672 /* On ARM, symbols for thumb functions have 1 added to
673 * the symbol address as a flag - remove it */
674 if ((ehdr.e_machine == EM_ARM) &&
675 (map->type == MAP__FUNCTION) &&
679 if (dso->kernel != DSO_TYPE_USER || kmodule) {
680 char dso_name[PATH_MAX];
682 if (strcmp(section_name,
683 (curr_dso->short_name +
684 dso->short_name_len)) == 0)
687 if (strcmp(section_name, ".text") == 0) {
693 snprintf(dso_name, sizeof(dso_name),
694 "%s%s", dso->short_name, section_name);
696 curr_map = map_groups__find_by_name(kmap->kmaps, map->type, dso_name);
697 if (curr_map == NULL) {
698 u64 start = sym.st_value;
701 start += map->start + shdr.sh_offset;
703 curr_dso = dso__new(dso_name);
704 if (curr_dso == NULL)
706 curr_dso->kernel = dso->kernel;
707 curr_dso->long_name = dso->long_name;
708 curr_dso->long_name_len = dso->long_name_len;
709 curr_map = map__new2(start, curr_dso,
711 if (curr_map == NULL) {
712 dso__delete(curr_dso);
715 curr_map->map_ip = identity__map_ip;
716 curr_map->unmap_ip = identity__map_ip;
717 curr_dso->symtab_type = dso->symtab_type;
718 map_groups__insert(kmap->kmaps, curr_map);
719 dsos__add(&dso->node, curr_dso);
720 dso__set_loaded(curr_dso, map->type);
722 curr_dso = curr_map->dso;
727 if (curr_dso->adjust_symbols && sym.st_value) {
728 pr_debug4("%s: adjusting symbol: st_value: %#" PRIx64 " "
729 "sh_addr: %#" PRIx64 " sh_offset: %#" PRIx64 "\n", __func__,
730 (u64)sym.st_value, (u64)shdr.sh_addr,
731 (u64)shdr.sh_offset);
732 sym.st_value -= shdr.sh_addr - shdr.sh_offset;
735 * We need to figure out if the object was created from C++ sources
736 * DWARF DW_compile_unit has this, but we don't always have access
739 demangled = bfd_demangle(NULL, elf_name, DMGL_PARAMS | DMGL_ANSI);
740 if (demangled != NULL)
741 elf_name = demangled;
743 f = symbol__new(sym.st_value, sym.st_size,
744 GELF_ST_BIND(sym.st_info), elf_name);
749 if (filter && filter(curr_map, f))
752 symbols__insert(&curr_dso->symbols[curr_map->type], f);
758 * For misannotated, zeroed, ASM function sizes.
761 symbols__fixup_duplicate(&dso->symbols[map->type]);
762 symbols__fixup_end(&dso->symbols[map->type]);
765 * We need to fixup this here too because we create new
766 * maps here, for things like vsyscall sections.
768 __map_groups__fixup_end(kmap->kmaps, map->type);
778 void symbol__elf_init(void)
780 elf_version(EV_CURRENT);