9 #include <symbol/kallsyms.h>
12 #ifndef HAVE_ELF_GETPHDRNUM_SUPPORT
13 static int elf_getphdrnum(Elf
*elf
, size_t *dst
)
18 ehdr
= gelf_getehdr(elf
, &gehdr
);
28 #ifndef NT_GNU_BUILD_ID
29 #define NT_GNU_BUILD_ID 3
33 * elf_symtab__for_each_symbol - iterate thru all the symbols
35 * @syms: struct elf_symtab instance to iterate
37 * @sym: GElf_Sym iterator
39 #define elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) \
40 for (idx = 0, gelf_getsym(syms, idx, &sym);\
42 idx++, gelf_getsym(syms, idx, &sym))
44 static inline uint8_t elf_sym__type(const GElf_Sym
*sym
)
46 return GELF_ST_TYPE(sym
->st_info
);
49 static inline int elf_sym__is_function(const GElf_Sym
*sym
)
51 return elf_sym__type(sym
) == STT_FUNC
&&
53 sym
->st_shndx
!= SHN_UNDEF
;
56 static inline bool elf_sym__is_object(const GElf_Sym
*sym
)
58 return elf_sym__type(sym
) == STT_OBJECT
&&
60 sym
->st_shndx
!= SHN_UNDEF
;
63 static inline int elf_sym__is_label(const GElf_Sym
*sym
)
65 return elf_sym__type(sym
) == STT_NOTYPE
&&
67 sym
->st_shndx
!= SHN_UNDEF
&&
68 sym
->st_shndx
!= SHN_ABS
;
71 static bool elf_sym__is_a(GElf_Sym
*sym
, enum map_type type
)
75 return elf_sym__is_function(sym
);
77 return elf_sym__is_object(sym
);
83 static inline const char *elf_sym__name(const GElf_Sym
*sym
,
84 const Elf_Data
*symstrs
)
86 return symstrs
->d_buf
+ sym
->st_name
;
89 static inline const char *elf_sec__name(const GElf_Shdr
*shdr
,
90 const Elf_Data
*secstrs
)
92 return secstrs
->d_buf
+ shdr
->sh_name
;
95 static inline int elf_sec__is_text(const GElf_Shdr
*shdr
,
96 const Elf_Data
*secstrs
)
98 return strstr(elf_sec__name(shdr
, secstrs
), "text") != NULL
;
101 static inline bool elf_sec__is_data(const GElf_Shdr
*shdr
,
102 const Elf_Data
*secstrs
)
104 return strstr(elf_sec__name(shdr
, secstrs
), "data") != NULL
;
107 static bool elf_sec__is_a(GElf_Shdr
*shdr
, Elf_Data
*secstrs
,
112 return elf_sec__is_text(shdr
, secstrs
);
114 return elf_sec__is_data(shdr
, secstrs
);
120 static size_t elf_addr_to_index(Elf
*elf
, GElf_Addr addr
)
126 while ((sec
= elf_nextscn(elf
, sec
)) != NULL
) {
127 gelf_getshdr(sec
, &shdr
);
129 if ((addr
>= shdr
.sh_addr
) &&
130 (addr
< (shdr
.sh_addr
+ shdr
.sh_size
)))
139 static Elf_Scn
*elf_section_by_name(Elf
*elf
, GElf_Ehdr
*ep
,
140 GElf_Shdr
*shp
, const char *name
,
146 /* Elf is corrupted/truncated, avoid calling elf_strptr. */
147 if (!elf_rawdata(elf_getscn(elf
, ep
->e_shstrndx
), NULL
))
150 while ((sec
= elf_nextscn(elf
, sec
)) != NULL
) {
153 gelf_getshdr(sec
, shp
);
154 str
= elf_strptr(elf
, ep
->e_shstrndx
, shp
->sh_name
);
155 if (!strcmp(name
, str
)) {
166 #define elf_section__for_each_rel(reldata, pos, pos_mem, idx, nr_entries) \
167 for (idx = 0, pos = gelf_getrel(reldata, 0, &pos_mem); \
169 ++idx, pos = gelf_getrel(reldata, idx, &pos_mem))
171 #define elf_section__for_each_rela(reldata, pos, pos_mem, idx, nr_entries) \
172 for (idx = 0, pos = gelf_getrela(reldata, 0, &pos_mem); \
174 ++idx, pos = gelf_getrela(reldata, idx, &pos_mem))
177 * We need to check if we have a .dynsym, so that we can handle the
178 * .plt, synthesizing its symbols, that aren't on the symtabs (be it
179 * .dynsym or .symtab).
180 * And always look at the original dso, not at debuginfo packages, that
181 * have the PLT data stripped out (shdr_rel_plt.sh_type == SHT_NOBITS).
183 int dso__synthesize_plt_symbols(struct dso
*dso
, struct symsrc
*ss
, struct map
*map
,
184 symbol_filter_t filter
)
186 uint32_t nr_rel_entries
, idx
;
191 GElf_Shdr shdr_rel_plt
, shdr_dynsym
;
192 Elf_Data
*reldata
, *syms
, *symstrs
;
193 Elf_Scn
*scn_plt_rel
, *scn_symstrs
, *scn_dynsym
;
196 char sympltname
[1024];
198 int nr
= 0, symidx
, err
= 0;
206 scn_dynsym
= ss
->dynsym
;
207 shdr_dynsym
= ss
->dynshdr
;
208 dynsym_idx
= ss
->dynsym_idx
;
210 if (scn_dynsym
== NULL
)
213 scn_plt_rel
= elf_section_by_name(elf
, &ehdr
, &shdr_rel_plt
,
215 if (scn_plt_rel
== NULL
) {
216 scn_plt_rel
= elf_section_by_name(elf
, &ehdr
, &shdr_rel_plt
,
218 if (scn_plt_rel
== NULL
)
224 if (shdr_rel_plt
.sh_link
!= dynsym_idx
)
227 if (elf_section_by_name(elf
, &ehdr
, &shdr_plt
, ".plt", NULL
) == NULL
)
231 * Fetch the relocation section to find the idxes to the GOT
232 * and the symbols in the .dynsym they refer to.
234 reldata
= elf_getdata(scn_plt_rel
, NULL
);
238 syms
= elf_getdata(scn_dynsym
, NULL
);
242 scn_symstrs
= elf_getscn(elf
, shdr_dynsym
.sh_link
);
243 if (scn_symstrs
== NULL
)
246 symstrs
= elf_getdata(scn_symstrs
, NULL
);
250 if (symstrs
->d_size
== 0)
253 nr_rel_entries
= shdr_rel_plt
.sh_size
/ shdr_rel_plt
.sh_entsize
;
254 plt_offset
= shdr_plt
.sh_offset
;
256 if (shdr_rel_plt
.sh_type
== SHT_RELA
) {
257 GElf_Rela pos_mem
, *pos
;
259 elf_section__for_each_rela(reldata
, pos
, pos_mem
, idx
,
261 symidx
= GELF_R_SYM(pos
->r_info
);
262 plt_offset
+= shdr_plt
.sh_entsize
;
263 gelf_getsym(syms
, symidx
, &sym
);
264 snprintf(sympltname
, sizeof(sympltname
),
265 "%s@plt", elf_sym__name(&sym
, symstrs
));
267 f
= symbol__new(plt_offset
, shdr_plt
.sh_entsize
,
268 STB_GLOBAL
, sympltname
);
272 if (filter
&& filter(map
, f
))
275 symbols__insert(&dso
->symbols
[map
->type
], f
);
279 } else if (shdr_rel_plt
.sh_type
== SHT_REL
) {
280 GElf_Rel pos_mem
, *pos
;
281 elf_section__for_each_rel(reldata
, pos
, pos_mem
, idx
,
283 symidx
= GELF_R_SYM(pos
->r_info
);
284 plt_offset
+= shdr_plt
.sh_entsize
;
285 gelf_getsym(syms
, symidx
, &sym
);
286 snprintf(sympltname
, sizeof(sympltname
),
287 "%s@plt", elf_sym__name(&sym
, symstrs
));
289 f
= symbol__new(plt_offset
, shdr_plt
.sh_entsize
,
290 STB_GLOBAL
, sympltname
);
294 if (filter
&& filter(map
, f
))
297 symbols__insert(&dso
->symbols
[map
->type
], f
);
307 pr_debug("%s: problems reading %s PLT info.\n",
308 __func__
, dso
->long_name
);
313 * Align offset to 4 bytes as needed for note name and descriptor data.
315 #define NOTE_ALIGN(n) (((n) + 3) & -4U)
317 static int elf_read_build_id(Elf
*elf
, void *bf
, size_t size
)
327 if (size
< BUILD_ID_SIZE
)
334 if (gelf_getehdr(elf
, &ehdr
) == NULL
) {
335 pr_err("%s: cannot get elf header.\n", __func__
);
340 * Check following sections for notes:
341 * '.note.gnu.build-id'
343 * '.note' (VDSO specific)
346 sec
= elf_section_by_name(elf
, &ehdr
, &shdr
,
347 ".note.gnu.build-id", NULL
);
351 sec
= elf_section_by_name(elf
, &ehdr
, &shdr
,
356 sec
= elf_section_by_name(elf
, &ehdr
, &shdr
,
365 data
= elf_getdata(sec
, NULL
);
370 while (ptr
< (data
->d_buf
+ data
->d_size
)) {
371 GElf_Nhdr
*nhdr
= ptr
;
372 size_t namesz
= NOTE_ALIGN(nhdr
->n_namesz
),
373 descsz
= NOTE_ALIGN(nhdr
->n_descsz
);
376 ptr
+= sizeof(*nhdr
);
379 if (nhdr
->n_type
== NT_GNU_BUILD_ID
&&
380 nhdr
->n_namesz
== sizeof("GNU")) {
381 if (memcmp(name
, "GNU", sizeof("GNU")) == 0) {
382 size_t sz
= min(size
, descsz
);
384 memset(bf
+ sz
, 0, size
- sz
);
396 int filename__read_build_id(const char *filename
, void *bf
, size_t size
)
401 if (size
< BUILD_ID_SIZE
)
404 fd
= open(filename
, O_RDONLY
);
408 elf
= elf_begin(fd
, PERF_ELF_C_READ_MMAP
, NULL
);
410 pr_debug2("%s: cannot read %s ELF file.\n", __func__
, filename
);
414 err
= elf_read_build_id(elf
, bf
, size
);
423 int sysfs__read_build_id(const char *filename
, void *build_id
, size_t size
)
427 if (size
< BUILD_ID_SIZE
)
430 fd
= open(filename
, O_RDONLY
);
437 size_t namesz
, descsz
;
439 if (read(fd
, &nhdr
, sizeof(nhdr
)) != sizeof(nhdr
))
442 namesz
= NOTE_ALIGN(nhdr
.n_namesz
);
443 descsz
= NOTE_ALIGN(nhdr
.n_descsz
);
444 if (nhdr
.n_type
== NT_GNU_BUILD_ID
&&
445 nhdr
.n_namesz
== sizeof("GNU")) {
446 if (read(fd
, bf
, namesz
) != (ssize_t
)namesz
)
448 if (memcmp(bf
, "GNU", sizeof("GNU")) == 0) {
449 size_t sz
= min(descsz
, size
);
450 if (read(fd
, build_id
, sz
) == (ssize_t
)sz
) {
451 memset(build_id
+ sz
, 0, size
- sz
);
455 } else if (read(fd
, bf
, descsz
) != (ssize_t
)descsz
)
458 int n
= namesz
+ descsz
;
459 if (read(fd
, bf
, n
) != n
)
468 int filename__read_debuglink(const char *filename
, char *debuglink
,
479 fd
= open(filename
, O_RDONLY
);
483 elf
= elf_begin(fd
, PERF_ELF_C_READ_MMAP
, NULL
);
485 pr_debug2("%s: cannot read %s ELF file.\n", __func__
, filename
);
493 if (gelf_getehdr(elf
, &ehdr
) == NULL
) {
494 pr_err("%s: cannot get elf header.\n", __func__
);
498 sec
= elf_section_by_name(elf
, &ehdr
, &shdr
,
499 ".gnu_debuglink", NULL
);
503 data
= elf_getdata(sec
, NULL
);
507 /* the start of this section is a zero-terminated string */
508 strncpy(debuglink
, data
->d_buf
, size
);
518 static int dso__swap_init(struct dso
*dso
, unsigned char eidata
)
520 static unsigned int const endian
= 1;
522 dso
->needs_swap
= DSO_SWAP__NO
;
526 /* We are big endian, DSO is little endian. */
527 if (*(unsigned char const *)&endian
!= 1)
528 dso
->needs_swap
= DSO_SWAP__YES
;
532 /* We are little endian, DSO is big endian. */
533 if (*(unsigned char const *)&endian
!= 0)
534 dso
->needs_swap
= DSO_SWAP__YES
;
538 pr_err("unrecognized DSO data encoding %d\n", eidata
);
545 bool symsrc__possibly_runtime(struct symsrc
*ss
)
547 return ss
->dynsym
|| ss
->opdsec
;
550 bool symsrc__has_symtab(struct symsrc
*ss
)
552 return ss
->symtab
!= NULL
;
555 void symsrc__destroy(struct symsrc
*ss
)
562 int symsrc__init(struct symsrc
*ss
, struct dso
*dso
, const char *name
,
563 enum dso_binary_type type
)
570 fd
= open(name
, O_RDONLY
);
574 elf
= elf_begin(fd
, PERF_ELF_C_READ_MMAP
, NULL
);
576 pr_debug("%s: cannot read %s ELF file.\n", __func__
, name
);
580 if (gelf_getehdr(elf
, &ehdr
) == NULL
) {
581 pr_debug("%s: cannot get elf header.\n", __func__
);
585 if (dso__swap_init(dso
, ehdr
.e_ident
[EI_DATA
]))
588 /* Always reject images with a mismatched build-id: */
589 if (dso
->has_build_id
) {
590 u8 build_id
[BUILD_ID_SIZE
];
592 if (elf_read_build_id(elf
, build_id
, BUILD_ID_SIZE
) < 0)
595 if (!dso__build_id_equal(dso
, build_id
))
599 ss
->symtab
= elf_section_by_name(elf
, &ehdr
, &ss
->symshdr
, ".symtab",
601 if (ss
->symshdr
.sh_type
!= SHT_SYMTAB
)
605 ss
->dynsym
= elf_section_by_name(elf
, &ehdr
, &ss
->dynshdr
, ".dynsym",
607 if (ss
->dynshdr
.sh_type
!= SHT_DYNSYM
)
611 ss
->opdsec
= elf_section_by_name(elf
, &ehdr
, &ss
->opdshdr
, ".opd",
613 if (ss
->opdshdr
.sh_type
!= SHT_PROGBITS
)
616 if (dso
->kernel
== DSO_TYPE_USER
) {
618 ss
->adjust_symbols
= (ehdr
.e_type
== ET_EXEC
||
619 ehdr
.e_type
== ET_REL
||
620 elf_section_by_name(elf
, &ehdr
, &shdr
,
624 ss
->adjust_symbols
= ehdr
.e_type
== ET_EXEC
||
625 ehdr
.e_type
== ET_REL
;
628 ss
->name
= strdup(name
);
647 * ref_reloc_sym_not_found - has kernel relocation symbol been found.
648 * @kmap: kernel maps and relocation reference symbol
650 * This function returns %true if we are dealing with the kernel maps and the
651 * relocation reference symbol has not yet been found. Otherwise %false is
654 static bool ref_reloc_sym_not_found(struct kmap
*kmap
)
656 return kmap
&& kmap
->ref_reloc_sym
&& kmap
->ref_reloc_sym
->name
&&
657 !kmap
->ref_reloc_sym
->unrelocated_addr
;
661 * ref_reloc - kernel relocation offset.
662 * @kmap: kernel maps and relocation reference symbol
664 * This function returns the offset of kernel addresses as determined by using
665 * the relocation reference symbol i.e. if the kernel has not been relocated
666 * then the return value is zero.
668 static u64
ref_reloc(struct kmap
*kmap
)
670 if (kmap
&& kmap
->ref_reloc_sym
&&
671 kmap
->ref_reloc_sym
->unrelocated_addr
)
672 return kmap
->ref_reloc_sym
->addr
-
673 kmap
->ref_reloc_sym
->unrelocated_addr
;
677 int dso__load_sym(struct dso
*dso
, struct map
*map
,
678 struct symsrc
*syms_ss
, struct symsrc
*runtime_ss
,
679 symbol_filter_t filter
, int kmodule
)
681 struct kmap
*kmap
= dso
->kernel
? map__kmap(map
) : NULL
;
682 struct map
*curr_map
= map
;
683 struct dso
*curr_dso
= dso
;
684 Elf_Data
*symstrs
, *secstrs
;
690 Elf_Data
*syms
, *opddata
= NULL
;
692 Elf_Scn
*sec
, *sec_strndx
;
695 bool remap_kernel
= false, adjust_kernel_syms
= false;
697 dso
->symtab_type
= syms_ss
->type
;
698 dso
->rel
= syms_ss
->ehdr
.e_type
== ET_REL
;
701 * Modules may already have symbols from kallsyms, but those symbols
702 * have the wrong values for the dso maps, so remove them.
704 if (kmodule
&& syms_ss
->symtab
)
705 symbols__delete(&dso
->symbols
[map
->type
]);
707 if (!syms_ss
->symtab
) {
708 syms_ss
->symtab
= syms_ss
->dynsym
;
709 syms_ss
->symshdr
= syms_ss
->dynshdr
;
713 ehdr
= syms_ss
->ehdr
;
714 sec
= syms_ss
->symtab
;
715 shdr
= syms_ss
->symshdr
;
717 if (runtime_ss
->opdsec
)
718 opddata
= elf_rawdata(runtime_ss
->opdsec
, NULL
);
720 syms
= elf_getdata(sec
, NULL
);
724 sec
= elf_getscn(elf
, shdr
.sh_link
);
728 symstrs
= elf_getdata(sec
, NULL
);
732 sec_strndx
= elf_getscn(elf
, ehdr
.e_shstrndx
);
733 if (sec_strndx
== NULL
)
736 secstrs
= elf_getdata(sec_strndx
, NULL
);
740 nr_syms
= shdr
.sh_size
/ shdr
.sh_entsize
;
742 memset(&sym
, 0, sizeof(sym
));
745 * The kernel relocation symbol is needed in advance in order to adjust
746 * kernel maps correctly.
748 if (ref_reloc_sym_not_found(kmap
)) {
749 elf_symtab__for_each_symbol(syms
, nr_syms
, idx
, sym
) {
750 const char *elf_name
= elf_sym__name(&sym
, symstrs
);
752 if (strcmp(elf_name
, kmap
->ref_reloc_sym
->name
))
754 kmap
->ref_reloc_sym
->unrelocated_addr
= sym
.st_value
;
759 dso
->adjust_symbols
= runtime_ss
->adjust_symbols
|| ref_reloc(kmap
);
761 * Initial kernel and module mappings do not map to the dso. For
762 * function mappings, flag the fixups.
764 if (map
->type
== MAP__FUNCTION
&& (dso
->kernel
|| kmodule
)) {
766 adjust_kernel_syms
= dso
->adjust_symbols
;
768 elf_symtab__for_each_symbol(syms
, nr_syms
, idx
, sym
) {
770 const char *elf_name
= elf_sym__name(&sym
, symstrs
);
771 char *demangled
= NULL
;
772 int is_label
= elf_sym__is_label(&sym
);
773 const char *section_name
;
774 bool used_opd
= false;
776 if (!is_label
&& !elf_sym__is_a(&sym
, map
->type
))
779 /* Reject ARM ELF "mapping symbols": these aren't unique and
780 * don't identify functions, so will confuse the profile
782 if (ehdr
.e_machine
== EM_ARM
) {
783 if (!strcmp(elf_name
, "$a") ||
784 !strcmp(elf_name
, "$d") ||
785 !strcmp(elf_name
, "$t"))
789 if (runtime_ss
->opdsec
&& sym
.st_shndx
== runtime_ss
->opdidx
) {
790 u32 offset
= sym
.st_value
- syms_ss
->opdshdr
.sh_addr
;
791 u64
*opd
= opddata
->d_buf
+ offset
;
792 sym
.st_value
= DSO__SWAP(dso
, u64
, *opd
);
793 sym
.st_shndx
= elf_addr_to_index(runtime_ss
->elf
,
798 * When loading symbols in a data mapping, ABS symbols (which
799 * has a value of SHN_ABS in its st_shndx) failed at
800 * elf_getscn(). And it marks the loading as a failure so
801 * already loaded symbols cannot be fixed up.
803 * I'm not sure what should be done. Just ignore them for now.
806 if (sym
.st_shndx
== SHN_ABS
)
809 sec
= elf_getscn(runtime_ss
->elf
, sym
.st_shndx
);
813 gelf_getshdr(sec
, &shdr
);
815 if (is_label
&& !elf_sec__is_a(&shdr
, secstrs
, map
->type
))
818 section_name
= elf_sec__name(&shdr
, secstrs
);
820 /* On ARM, symbols for thumb functions have 1 added to
821 * the symbol address as a flag - remove it */
822 if ((ehdr
.e_machine
== EM_ARM
) &&
823 (map
->type
== MAP__FUNCTION
) &&
827 if (dso
->kernel
|| kmodule
) {
828 char dso_name
[PATH_MAX
];
830 /* Adjust symbol to map to file offset */
831 if (adjust_kernel_syms
)
832 sym
.st_value
-= shdr
.sh_addr
- shdr
.sh_offset
;
834 if (strcmp(section_name
,
835 (curr_dso
->short_name
+
836 dso
->short_name_len
)) == 0)
839 if (strcmp(section_name
, ".text") == 0) {
841 * The initial kernel mapping is based on
842 * kallsyms and identity maps. Overwrite it to
843 * map to the kernel dso.
845 if (remap_kernel
&& dso
->kernel
) {
846 remap_kernel
= false;
847 map
->start
= shdr
.sh_addr
+
849 map
->end
= map
->start
+ shdr
.sh_size
;
850 map
->pgoff
= shdr
.sh_offset
;
851 map
->map_ip
= map__map_ip
;
852 map
->unmap_ip
= map__unmap_ip
;
853 /* Ensure maps are correctly ordered */
854 map_groups__remove(kmap
->kmaps
, map
);
855 map_groups__insert(kmap
->kmaps
, map
);
859 * The initial module mapping is based on
860 * /proc/modules mapped to offset zero.
861 * Overwrite it to map to the module dso.
863 if (remap_kernel
&& kmodule
) {
864 remap_kernel
= false;
865 map
->pgoff
= shdr
.sh_offset
;
876 snprintf(dso_name
, sizeof(dso_name
),
877 "%s%s", dso
->short_name
, section_name
);
879 curr_map
= map_groups__find_by_name(kmap
->kmaps
, map
->type
, dso_name
);
880 if (curr_map
== NULL
) {
881 u64 start
= sym
.st_value
;
884 start
+= map
->start
+ shdr
.sh_offset
;
886 curr_dso
= dso__new(dso_name
);
887 if (curr_dso
== NULL
)
889 curr_dso
->kernel
= dso
->kernel
;
890 curr_dso
->long_name
= dso
->long_name
;
891 curr_dso
->long_name_len
= dso
->long_name_len
;
892 curr_map
= map__new2(start
, curr_dso
,
894 if (curr_map
== NULL
) {
895 dso__delete(curr_dso
);
898 if (adjust_kernel_syms
) {
899 curr_map
->start
= shdr
.sh_addr
+
901 curr_map
->end
= curr_map
->start
+
903 curr_map
->pgoff
= shdr
.sh_offset
;
905 curr_map
->map_ip
= identity__map_ip
;
906 curr_map
->unmap_ip
= identity__map_ip
;
908 curr_dso
->symtab_type
= dso
->symtab_type
;
909 map_groups__insert(kmap
->kmaps
, curr_map
);
910 dsos__add(&dso
->node
, curr_dso
);
911 dso__set_loaded(curr_dso
, map
->type
);
913 curr_dso
= curr_map
->dso
;
918 if ((used_opd
&& runtime_ss
->adjust_symbols
)
919 || (!used_opd
&& syms_ss
->adjust_symbols
)) {
920 pr_debug4("%s: adjusting symbol: st_value: %#" PRIx64
" "
921 "sh_addr: %#" PRIx64
" sh_offset: %#" PRIx64
"\n", __func__
,
922 (u64
)sym
.st_value
, (u64
)shdr
.sh_addr
,
923 (u64
)shdr
.sh_offset
);
924 sym
.st_value
-= shdr
.sh_addr
- shdr
.sh_offset
;
927 * We need to figure out if the object was created from C++ sources
928 * DWARF DW_compile_unit has this, but we don't always have access
931 if (symbol_conf
.demangle
) {
932 demangled
= bfd_demangle(NULL
, elf_name
,
933 DMGL_PARAMS
| DMGL_ANSI
);
934 if (demangled
!= NULL
)
935 elf_name
= demangled
;
938 f
= symbol__new(sym
.st_value
, sym
.st_size
,
939 GELF_ST_BIND(sym
.st_info
), elf_name
);
944 if (filter
&& filter(curr_map
, f
))
947 symbols__insert(&curr_dso
->symbols
[curr_map
->type
], f
);
953 * For misannotated, zeroed, ASM function sizes.
956 symbols__fixup_duplicate(&dso
->symbols
[map
->type
]);
957 symbols__fixup_end(&dso
->symbols
[map
->type
]);
960 * We need to fixup this here too because we create new
961 * maps here, for things like vsyscall sections.
963 __map_groups__fixup_end(kmap
->kmaps
, map
->type
);
971 static int elf_read_maps(Elf
*elf
, bool exe
, mapfn_t mapfn
, void *data
)
978 if (elf_getphdrnum(elf
, &phdrnum
))
981 for (i
= 0; i
< phdrnum
; i
++) {
982 if (gelf_getphdr(elf
, i
, &phdr
) == NULL
)
984 if (phdr
.p_type
!= PT_LOAD
)
987 if (!(phdr
.p_flags
& PF_X
))
990 if (!(phdr
.p_flags
& PF_R
))
993 sz
= min(phdr
.p_memsz
, phdr
.p_filesz
);
996 err
= mapfn(phdr
.p_vaddr
, sz
, phdr
.p_offset
, data
);
1003 int file__read_maps(int fd
, bool exe
, mapfn_t mapfn
, void *data
,
1009 elf
= elf_begin(fd
, PERF_ELF_C_READ_MMAP
, NULL
);
1014 *is_64_bit
= (gelf_getclass(elf
) == ELFCLASS64
);
1016 err
= elf_read_maps(elf
, exe
, mapfn
, data
);
1022 static int copy_bytes(int from
, off_t from_offs
, int to
, off_t to_offs
, u64 len
)
1027 char *buf
= malloc(page_size
);
1032 if (lseek(to
, to_offs
, SEEK_SET
) != to_offs
)
1035 if (lseek(from
, from_offs
, SEEK_SET
) != from_offs
)
1042 /* Use read because mmap won't work on proc files */
1043 r
= read(from
, buf
, n
);
1049 r
= write(to
, buf
, n
);
1070 static int kcore__open(struct kcore
*kcore
, const char *filename
)
1074 kcore
->fd
= open(filename
, O_RDONLY
);
1075 if (kcore
->fd
== -1)
1078 kcore
->elf
= elf_begin(kcore
->fd
, ELF_C_READ
, NULL
);
1082 kcore
->elfclass
= gelf_getclass(kcore
->elf
);
1083 if (kcore
->elfclass
== ELFCLASSNONE
)
1086 ehdr
= gelf_getehdr(kcore
->elf
, &kcore
->ehdr
);
1093 elf_end(kcore
->elf
);
1099 static int kcore__init(struct kcore
*kcore
, char *filename
, int elfclass
,
1104 kcore
->elfclass
= elfclass
;
1107 kcore
->fd
= mkstemp(filename
);
1109 kcore
->fd
= open(filename
, O_WRONLY
| O_CREAT
| O_EXCL
, 0400);
1110 if (kcore
->fd
== -1)
1113 kcore
->elf
= elf_begin(kcore
->fd
, ELF_C_WRITE
, NULL
);
1117 if (!gelf_newehdr(kcore
->elf
, elfclass
))
1120 ehdr
= gelf_getehdr(kcore
->elf
, &kcore
->ehdr
);
1127 elf_end(kcore
->elf
);
1134 static void kcore__close(struct kcore
*kcore
)
1136 elf_end(kcore
->elf
);
1140 static int kcore__copy_hdr(struct kcore
*from
, struct kcore
*to
, size_t count
)
1142 GElf_Ehdr
*ehdr
= &to
->ehdr
;
1143 GElf_Ehdr
*kehdr
= &from
->ehdr
;
1145 memcpy(ehdr
->e_ident
, kehdr
->e_ident
, EI_NIDENT
);
1146 ehdr
->e_type
= kehdr
->e_type
;
1147 ehdr
->e_machine
= kehdr
->e_machine
;
1148 ehdr
->e_version
= kehdr
->e_version
;
1151 ehdr
->e_flags
= kehdr
->e_flags
;
1152 ehdr
->e_phnum
= count
;
1153 ehdr
->e_shentsize
= 0;
1155 ehdr
->e_shstrndx
= 0;
1157 if (from
->elfclass
== ELFCLASS32
) {
1158 ehdr
->e_phoff
= sizeof(Elf32_Ehdr
);
1159 ehdr
->e_ehsize
= sizeof(Elf32_Ehdr
);
1160 ehdr
->e_phentsize
= sizeof(Elf32_Phdr
);
1162 ehdr
->e_phoff
= sizeof(Elf64_Ehdr
);
1163 ehdr
->e_ehsize
= sizeof(Elf64_Ehdr
);
1164 ehdr
->e_phentsize
= sizeof(Elf64_Phdr
);
1167 if (!gelf_update_ehdr(to
->elf
, ehdr
))
1170 if (!gelf_newphdr(to
->elf
, count
))
1176 static int kcore__add_phdr(struct kcore
*kcore
, int idx
, off_t offset
,
1182 phdr
= gelf_getphdr(kcore
->elf
, idx
, &gphdr
);
1186 phdr
->p_type
= PT_LOAD
;
1187 phdr
->p_flags
= PF_R
| PF_W
| PF_X
;
1188 phdr
->p_offset
= offset
;
1189 phdr
->p_vaddr
= addr
;
1191 phdr
->p_filesz
= len
;
1192 phdr
->p_memsz
= len
;
1193 phdr
->p_align
= page_size
;
1195 if (!gelf_update_phdr(kcore
->elf
, idx
, phdr
))
1201 static off_t
kcore__write(struct kcore
*kcore
)
1203 return elf_update(kcore
->elf
, ELF_C_WRITE
);
1212 struct kcore_copy_info
{
1218 u64 last_module_symbol
;
1219 struct phdr_data kernel_map
;
1220 struct phdr_data modules_map
;
1223 static int kcore_copy__process_kallsyms(void *arg
, const char *name
, char type
,
1226 struct kcore_copy_info
*kci
= arg
;
1228 if (!symbol_type__is_a(type
, MAP__FUNCTION
))
1231 if (strchr(name
, '[')) {
1232 if (start
> kci
->last_module_symbol
)
1233 kci
->last_module_symbol
= start
;
1237 if (!kci
->first_symbol
|| start
< kci
->first_symbol
)
1238 kci
->first_symbol
= start
;
1240 if (!kci
->last_symbol
|| start
> kci
->last_symbol
)
1241 kci
->last_symbol
= start
;
1243 if (!strcmp(name
, "_stext")) {
1248 if (!strcmp(name
, "_etext")) {
1256 static int kcore_copy__parse_kallsyms(struct kcore_copy_info
*kci
,
1259 char kallsyms_filename
[PATH_MAX
];
1261 scnprintf(kallsyms_filename
, PATH_MAX
, "%s/kallsyms", dir
);
1263 if (symbol__restricted_filename(kallsyms_filename
, "/proc/kallsyms"))
1266 if (kallsyms__parse(kallsyms_filename
, kci
,
1267 kcore_copy__process_kallsyms
) < 0)
1273 static int kcore_copy__process_modules(void *arg
,
1274 const char *name __maybe_unused
,
1277 struct kcore_copy_info
*kci
= arg
;
1279 if (!kci
->first_module
|| start
< kci
->first_module
)
1280 kci
->first_module
= start
;
1285 static int kcore_copy__parse_modules(struct kcore_copy_info
*kci
,
1288 char modules_filename
[PATH_MAX
];
1290 scnprintf(modules_filename
, PATH_MAX
, "%s/modules", dir
);
1292 if (symbol__restricted_filename(modules_filename
, "/proc/modules"))
1295 if (modules__parse(modules_filename
, kci
,
1296 kcore_copy__process_modules
) < 0)
1302 static void kcore_copy__map(struct phdr_data
*p
, u64 start
, u64 end
, u64 pgoff
,
1305 if (p
->addr
|| s
< start
|| s
>= end
)
1309 p
->offset
= (s
- start
) + pgoff
;
1310 p
->len
= e
< end
? e
- s
: end
- s
;
1313 static int kcore_copy__read_map(u64 start
, u64 len
, u64 pgoff
, void *data
)
1315 struct kcore_copy_info
*kci
= data
;
1316 u64 end
= start
+ len
;
1318 kcore_copy__map(&kci
->kernel_map
, start
, end
, pgoff
, kci
->stext
,
1321 kcore_copy__map(&kci
->modules_map
, start
, end
, pgoff
, kci
->first_module
,
1322 kci
->last_module_symbol
);
1327 static int kcore_copy__read_maps(struct kcore_copy_info
*kci
, Elf
*elf
)
1329 if (elf_read_maps(elf
, true, kcore_copy__read_map
, kci
) < 0)
1335 static int kcore_copy__calc_maps(struct kcore_copy_info
*kci
, const char *dir
,
1338 if (kcore_copy__parse_kallsyms(kci
, dir
))
1341 if (kcore_copy__parse_modules(kci
, dir
))
1345 kci
->stext
= round_down(kci
->stext
, page_size
);
1347 kci
->stext
= round_down(kci
->first_symbol
, page_size
);
1350 kci
->etext
= round_up(kci
->etext
, page_size
);
1351 } else if (kci
->last_symbol
) {
1352 kci
->etext
= round_up(kci
->last_symbol
, page_size
);
1353 kci
->etext
+= page_size
;
1356 kci
->first_module
= round_down(kci
->first_module
, page_size
);
1358 if (kci
->last_module_symbol
) {
1359 kci
->last_module_symbol
= round_up(kci
->last_module_symbol
,
1361 kci
->last_module_symbol
+= page_size
;
1364 if (!kci
->stext
|| !kci
->etext
)
1367 if (kci
->first_module
&& !kci
->last_module_symbol
)
1370 return kcore_copy__read_maps(kci
, elf
);
1373 static int kcore_copy__copy_file(const char *from_dir
, const char *to_dir
,
1376 char from_filename
[PATH_MAX
];
1377 char to_filename
[PATH_MAX
];
1379 scnprintf(from_filename
, PATH_MAX
, "%s/%s", from_dir
, name
);
1380 scnprintf(to_filename
, PATH_MAX
, "%s/%s", to_dir
, name
);
1382 return copyfile_mode(from_filename
, to_filename
, 0400);
1385 static int kcore_copy__unlink(const char *dir
, const char *name
)
1387 char filename
[PATH_MAX
];
1389 scnprintf(filename
, PATH_MAX
, "%s/%s", dir
, name
);
1391 return unlink(filename
);
1394 static int kcore_copy__compare_fds(int from
, int to
)
1402 buf_from
= malloc(page_size
);
1403 buf_to
= malloc(page_size
);
1404 if (!buf_from
|| !buf_to
)
1408 /* Use read because mmap won't work on proc files */
1409 ret
= read(from
, buf_from
, page_size
);
1418 if (readn(to
, buf_to
, len
) != (int)len
)
1421 if (memcmp(buf_from
, buf_to
, len
))
1432 static int kcore_copy__compare_files(const char *from_filename
,
1433 const char *to_filename
)
1435 int from
, to
, err
= -1;
1437 from
= open(from_filename
, O_RDONLY
);
1441 to
= open(to_filename
, O_RDONLY
);
1443 goto out_close_from
;
1445 err
= kcore_copy__compare_fds(from
, to
);
1453 static int kcore_copy__compare_file(const char *from_dir
, const char *to_dir
,
1456 char from_filename
[PATH_MAX
];
1457 char to_filename
[PATH_MAX
];
1459 scnprintf(from_filename
, PATH_MAX
, "%s/%s", from_dir
, name
);
1460 scnprintf(to_filename
, PATH_MAX
, "%s/%s", to_dir
, name
);
1462 return kcore_copy__compare_files(from_filename
, to_filename
);
1466 * kcore_copy - copy kallsyms, modules and kcore from one directory to another.
1467 * @from_dir: from directory
1468 * @to_dir: to directory
1470 * This function copies kallsyms, modules and kcore files from one directory to
1471 * another. kallsyms and modules are copied entirely. Only code segments are
1472 * copied from kcore. It is assumed that two segments suffice: one for the
1473 * kernel proper and one for all the modules. The code segments are determined
1474 * from kallsyms and modules files. The kernel map starts at _stext or the
1475 * lowest function symbol, and ends at _etext or the highest function symbol.
1476 * The module map starts at the lowest module address and ends at the highest
1477 * module symbol. Start addresses are rounded down to the nearest page. End
1478 * addresses are rounded up to the nearest page. An extra page is added to the
1479 * highest kernel symbol and highest module symbol to, hopefully, encompass that
1480 * symbol too. Because it contains only code sections, the resulting kcore is
1481 * unusual. One significant peculiarity is that the mapping (start -> pgoff)
1482 * is not the same for the kernel map and the modules map. That happens because
1483 * the data is copied adjacently whereas the original kcore has gaps. Finally,
1484 * kallsyms and modules files are compared with their copies to check that
1485 * modules have not been loaded or unloaded while the copies were taking place.
1487 * Return: %0 on success, %-1 on failure.
1489 int kcore_copy(const char *from_dir
, const char *to_dir
)
1492 struct kcore extract
;
1494 int idx
= 0, err
= -1;
1495 off_t offset
= page_size
, sz
, modules_offset
= 0;
1496 struct kcore_copy_info kci
= { .stext
= 0, };
1497 char kcore_filename
[PATH_MAX
];
1498 char extract_filename
[PATH_MAX
];
1500 if (kcore_copy__copy_file(from_dir
, to_dir
, "kallsyms"))
1503 if (kcore_copy__copy_file(from_dir
, to_dir
, "modules"))
1504 goto out_unlink_kallsyms
;
1506 scnprintf(kcore_filename
, PATH_MAX
, "%s/kcore", from_dir
);
1507 scnprintf(extract_filename
, PATH_MAX
, "%s/kcore", to_dir
);
1509 if (kcore__open(&kcore
, kcore_filename
))
1510 goto out_unlink_modules
;
1512 if (kcore_copy__calc_maps(&kci
, from_dir
, kcore
.elf
))
1513 goto out_kcore_close
;
1515 if (kcore__init(&extract
, extract_filename
, kcore
.elfclass
, false))
1516 goto out_kcore_close
;
1518 if (!kci
.modules_map
.addr
)
1521 if (kcore__copy_hdr(&kcore
, &extract
, count
))
1522 goto out_extract_close
;
1524 if (kcore__add_phdr(&extract
, idx
++, offset
, kci
.kernel_map
.addr
,
1525 kci
.kernel_map
.len
))
1526 goto out_extract_close
;
1528 if (kci
.modules_map
.addr
) {
1529 modules_offset
= offset
+ kci
.kernel_map
.len
;
1530 if (kcore__add_phdr(&extract
, idx
, modules_offset
,
1531 kci
.modules_map
.addr
, kci
.modules_map
.len
))
1532 goto out_extract_close
;
1535 sz
= kcore__write(&extract
);
1536 if (sz
< 0 || sz
> offset
)
1537 goto out_extract_close
;
1539 if (copy_bytes(kcore
.fd
, kci
.kernel_map
.offset
, extract
.fd
, offset
,
1540 kci
.kernel_map
.len
))
1541 goto out_extract_close
;
1543 if (modules_offset
&& copy_bytes(kcore
.fd
, kci
.modules_map
.offset
,
1544 extract
.fd
, modules_offset
,
1545 kci
.modules_map
.len
))
1546 goto out_extract_close
;
1548 if (kcore_copy__compare_file(from_dir
, to_dir
, "modules"))
1549 goto out_extract_close
;
1551 if (kcore_copy__compare_file(from_dir
, to_dir
, "kallsyms"))
1552 goto out_extract_close
;
1557 kcore__close(&extract
);
1559 unlink(extract_filename
);
1561 kcore__close(&kcore
);
1564 kcore_copy__unlink(to_dir
, "modules");
1565 out_unlink_kallsyms
:
1567 kcore_copy__unlink(to_dir
, "kallsyms");
1572 int kcore_extract__create(struct kcore_extract
*kce
)
1575 struct kcore extract
;
1577 int idx
= 0, err
= -1;
1578 off_t offset
= page_size
, sz
;
1580 if (kcore__open(&kcore
, kce
->kcore_filename
))
1583 strcpy(kce
->extract_filename
, PERF_KCORE_EXTRACT
);
1584 if (kcore__init(&extract
, kce
->extract_filename
, kcore
.elfclass
, true))
1585 goto out_kcore_close
;
1587 if (kcore__copy_hdr(&kcore
, &extract
, count
))
1588 goto out_extract_close
;
1590 if (kcore__add_phdr(&extract
, idx
, offset
, kce
->addr
, kce
->len
))
1591 goto out_extract_close
;
1593 sz
= kcore__write(&extract
);
1594 if (sz
< 0 || sz
> offset
)
1595 goto out_extract_close
;
1597 if (copy_bytes(kcore
.fd
, kce
->offs
, extract
.fd
, offset
, kce
->len
))
1598 goto out_extract_close
;
1603 kcore__close(&extract
);
1605 unlink(kce
->extract_filename
);
1607 kcore__close(&kcore
);
1612 void kcore_extract__delete(struct kcore_extract
*kce
)
1614 unlink(kce
->extract_filename
);
1617 void symbol__elf_init(void)
1619 elf_version(EV_CURRENT
);