14 #ifndef NT_GNU_BUILD_ID
15 #define NT_GNU_BUILD_ID 3
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 &&
42 static inline bool elf_sym__is_object(
const GElf_Sym *sym)
49 static inline int elf_sym__is_label(
const GElf_Sym *sym)
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,
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);
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))
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,
err = 0;
192 scn_dynsym = ss->dynsym;
193 shdr_dynsym = ss->dynshdr;
194 dynsym_idx = ss->dynsym_idx;
196 if (scn_dynsym ==
NULL)
199 scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt,
201 if (scn_plt_rel ==
NULL) {
202 scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt,
204 if (scn_plt_rel ==
NULL)
210 if (shdr_rel_plt.sh_link != dynsym_idx)
213 if (elf_section_by_name(elf, &ehdr, &shdr_plt,
".plt",
NULL) ==
NULL)
220 reldata = elf_getdata(scn_plt_rel,
NULL);
224 syms = elf_getdata(scn_dynsym,
NULL);
228 scn_symstrs = elf_getscn(elf, shdr_dynsym.sh_link);
229 if (scn_symstrs ==
NULL)
232 symstrs = elf_getdata(scn_symstrs,
NULL);
236 if (symstrs->d_size == 0)
239 nr_rel_entries = shdr_rel_plt.sh_size / shdr_rel_plt.sh_entsize;
240 plt_offset = shdr_plt.sh_offset;
242 if (shdr_rel_plt.sh_type ==
SHT_RELA) {
243 GElf_Rela pos_mem, *
pos;
247 symidx = GELF_R_SYM(pos->r_info);
248 plt_offset += shdr_plt.sh_entsize;
249 gelf_getsym(syms, symidx, &sym);
250 snprintf(sympltname,
sizeof(sympltname),
251 "%s@plt", elf_sym__name(&sym, symstrs));
258 if (filter &&
filter(map, f))
265 }
else if (shdr_rel_plt.sh_type ==
SHT_REL) {
266 GElf_Rel pos_mem, *
pos;
269 symidx = GELF_R_SYM(pos->r_info);
270 plt_offset += shdr_plt.sh_entsize;
271 gelf_getsym(syms, symidx, &sym);
272 snprintf(sympltname,
sizeof(sympltname),
273 "%s@plt", elf_sym__name(&sym, symstrs));
280 if (filter &&
filter(map, f))
293 pr_debug(
"%s: problems reading %s PLT info.\n",
301 #define NOTE_ALIGN(n) (((n) + 3) & -4U)
303 static int elf_read_build_id(Elf *elf,
void *
bf,
size_t size)
320 if (gelf_getehdr(elf, &ehdr) ==
NULL) {
321 pr_err(
"%s: cannot get elf header.\n", __func__);
332 sec = elf_section_by_name(elf, &ehdr, &shdr,
333 ".note.gnu.build-id",
NULL);
337 sec = elf_section_by_name(elf, &ehdr, &shdr,
342 sec = elf_section_by_name(elf, &ehdr, &shdr,
351 data = elf_getdata(sec,
NULL);
356 while (ptr < (data->d_buf + data->d_size)) {
357 GElf_Nhdr *nhdr =
ptr;
362 ptr +=
sizeof(*nhdr);
366 nhdr->n_namesz ==
sizeof(
"GNU")) {
367 if (
memcmp(name,
"GNU",
sizeof(
"GNU")) == 0) {
368 size_t sz =
min(size, descsz);
370 memset(bf + sz, 0, size - sz);
396 pr_debug2(
"%s: cannot read %s ELF file.\n", __func__, filename);
400 err = elf_read_build_id(elf, bf, size);
423 size_t namesz, descsz;
425 if (
read(fd, &nhdr,
sizeof(nhdr)) !=
sizeof(nhdr))
431 nhdr.n_namesz ==
sizeof(
"GNU")) {
434 if (
memcmp(bf,
"GNU",
sizeof(
"GNU")) == 0) {
435 size_t sz =
min(descsz, size);
437 memset(build_id + sz, 0, size - sz);
441 }
else if (
read(fd, bf, descsz) != (
ssize_t)descsz)
444 int n = namesz + descsz;
445 if (
read(fd, bf, n) != n)
471 pr_debug2(
"%s: cannot read %s ELF file.\n", __func__, filename);
479 if (gelf_getehdr(elf, &ehdr) ==
NULL) {
480 pr_err(
"%s: cannot get elf header.\n", __func__);
484 sec = elf_section_by_name(elf, &ehdr, &shdr,
485 ".gnu_debuglink",
NULL);
489 data = elf_getdata(sec,
NULL);
494 strncpy(debuglink, data->d_buf, size);
504 static int dso__swap_init(
struct dso *
dso,
unsigned char eidata)
506 static unsigned int const endian = 1;
513 if (*(
unsigned char const *)&endian != 1)
519 if (*(
unsigned char const *)&endian != 0)
524 pr_err(
"unrecognized DSO data encoding %d\n", eidata);
533 return ss->dynsym || ss->opdsec;
538 return ss->symtab !=
NULL;
562 pr_debug(
"%s: cannot read %s ELF file.\n", __func__, name);
566 if (gelf_getehdr(elf, &ehdr) ==
NULL) {
567 pr_debug(
"%s: cannot get elf header.\n", __func__);
571 if (dso__swap_init(dso, ehdr.e_ident[
EI_DATA]))
585 ss->symtab = elf_section_by_name(elf, &ehdr, &ss->symshdr,
".symtab",
591 ss->dynsym = elf_section_by_name(elf, &ehdr, &ss->dynshdr,
".dynsym",
597 ss->opdsec = elf_section_by_name(elf, &ehdr, &ss->opdshdr,
".opd",
604 ss->adjust_symbols = (ehdr.e_type ==
ET_EXEC ||
605 elf_section_by_name(elf, &ehdr, &shdr,
609 ss->adjust_symbols = 0;
612 ss->
name = strdup(name);
635 struct map *curr_map =
map;
636 struct dso *curr_dso = dso;
637 Elf_Data *symstrs, *secstrs;
643 Elf_Data *syms, *opddata =
NULL;
645 Elf_Scn *
sec, *sec_strndx;
651 if (!syms_ss->symtab) {
652 syms_ss->symtab = syms_ss->dynsym;
653 syms_ss->symshdr = syms_ss->dynshdr;
657 ehdr = syms_ss->ehdr;
658 sec = syms_ss->symtab;
659 shdr = syms_ss->symshdr;
661 if (runtime_ss->opdsec)
662 opddata = elf_rawdata(runtime_ss->opdsec,
NULL);
664 syms = elf_getdata(sec,
NULL);
668 sec = elf_getscn(elf, shdr.sh_link);
672 symstrs = elf_getdata(sec,
NULL);
676 sec_strndx = elf_getscn(elf, ehdr.e_shstrndx);
677 if (sec_strndx ==
NULL)
680 secstrs = elf_getdata(sec_strndx,
NULL);
684 nr_syms = shdr.sh_size / shdr.sh_entsize;
686 memset(&sym, 0,
sizeof(sym));
690 const char *elf_name = elf_sym__name(&sym, symstrs);
691 char *demangled =
NULL;
692 int is_label = elf_sym__is_label(&sym);
693 const char *section_name;
694 bool used_opd =
false;
700 if (!is_label && !elf_sym__is_a(&sym, map->
type))
706 if (ehdr.e_machine ==
EM_ARM) {
707 if (!
strcmp(elf_name,
"$a") ||
708 !
strcmp(elf_name,
"$d") ||
713 if (runtime_ss->opdsec && sym.st_shndx == runtime_ss->opdidx) {
714 u32 offset = sym.st_value - syms_ss->opdshdr.sh_addr;
717 sym.st_shndx = elf_addr_to_index(runtime_ss->elf,
722 sec = elf_getscn(runtime_ss->elf, sym.st_shndx);
726 gelf_getshdr(sec, &shdr);
728 if (is_label && !elf_sec__is_a(&shdr, secstrs, map->
type))
731 section_name = elf_sec__name(&shdr, secstrs);
735 if ((ehdr.e_machine ==
EM_ARM) &&
748 if (
strcmp(section_name,
".text") == 0) {
754 snprintf(dso_name,
sizeof(dso_name),
758 if (curr_map ==
NULL) {
762 start += map->
start + shdr.sh_offset;
765 if (curr_dso ==
NULL)
772 if (curr_map ==
NULL) {
776 curr_map->
map_ip = identity__map_ip;
777 curr_map->
unmap_ip = identity__map_ip;
779 map_groups__insert(kmap->
kmaps, curr_map);
781 dso__set_loaded(curr_dso, map->
type);
783 curr_dso = curr_map->
dso;
788 if ((used_opd && runtime_ss->adjust_symbols)
789 || (!used_opd && syms_ss->adjust_symbols)) {
791 "sh_addr: %#" PRIx64 " sh_offset: %#" PRIx64 "\n", __func__,
792 (
u64)sym.st_value, (
u64)shdr.sh_addr,
793 (
u64)shdr.sh_offset);
794 sym.st_value -= shdr.sh_addr - shdr.sh_offset;
802 if (demangled !=
NULL)
803 elf_name = demangled;
806 GELF_ST_BIND(sym.st_info), elf_name);
811 if (filter &&
filter(curr_map, f))