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 Elf_Scn
*elf_section_by_name(Elf
*elf
, GElf_Ehdr
*ep
,
140 GElf_Shdr
*shp
, const char *name
, size_t *idx
)
145 /* Elf is corrupted/truncated, avoid calling elf_strptr. */
146 if (!elf_rawdata(elf_getscn(elf
, ep
->e_shstrndx
), NULL
))
149 while ((sec
= elf_nextscn(elf
, sec
)) != NULL
) {
152 gelf_getshdr(sec
, shp
);
153 str
= elf_strptr(elf
, ep
->e_shstrndx
, shp
->sh_name
);
154 if (!strcmp(name
, str
)) {
165 #define elf_section__for_each_rel(reldata, pos, pos_mem, idx, nr_entries) \
166 for (idx = 0, pos = gelf_getrel(reldata, 0, &pos_mem); \
168 ++idx, pos = gelf_getrel(reldata, idx, &pos_mem))
170 #define elf_section__for_each_rela(reldata, pos, pos_mem, idx, nr_entries) \
171 for (idx = 0, pos = gelf_getrela(reldata, 0, &pos_mem); \
173 ++idx, pos = gelf_getrela(reldata, idx, &pos_mem))
176 * We need to check if we have a .dynsym, so that we can handle the
177 * .plt, synthesizing its symbols, that aren't on the symtabs (be it
178 * .dynsym or .symtab).
179 * And always look at the original dso, not at debuginfo packages, that
180 * have the PLT data stripped out (shdr_rel_plt.sh_type == SHT_NOBITS).
182 int dso__synthesize_plt_symbols(struct dso
*dso
, struct symsrc
*ss
, struct map
*map
,
183 symbol_filter_t filter
)
185 uint32_t nr_rel_entries
, idx
;
190 GElf_Shdr shdr_rel_plt
, shdr_dynsym
;
191 Elf_Data
*reldata
, *syms
, *symstrs
;
192 Elf_Scn
*scn_plt_rel
, *scn_symstrs
, *scn_dynsym
;
195 char sympltname
[1024];
197 int nr
= 0, symidx
, err
= 0;
205 scn_dynsym
= ss
->dynsym
;
206 shdr_dynsym
= ss
->dynshdr
;
207 dynsym_idx
= ss
->dynsym_idx
;
209 if (scn_dynsym
== NULL
)
212 scn_plt_rel
= elf_section_by_name(elf
, &ehdr
, &shdr_rel_plt
,
214 if (scn_plt_rel
== NULL
) {
215 scn_plt_rel
= elf_section_by_name(elf
, &ehdr
, &shdr_rel_plt
,
217 if (scn_plt_rel
== NULL
)
223 if (shdr_rel_plt
.sh_link
!= dynsym_idx
)
226 if (elf_section_by_name(elf
, &ehdr
, &shdr_plt
, ".plt", NULL
) == NULL
)
230 * Fetch the relocation section to find the idxes to the GOT
231 * and the symbols in the .dynsym they refer to.
233 reldata
= elf_getdata(scn_plt_rel
, NULL
);
237 syms
= elf_getdata(scn_dynsym
, NULL
);
241 scn_symstrs
= elf_getscn(elf
, shdr_dynsym
.sh_link
);
242 if (scn_symstrs
== NULL
)
245 symstrs
= elf_getdata(scn_symstrs
, NULL
);
249 if (symstrs
->d_size
== 0)
252 nr_rel_entries
= shdr_rel_plt
.sh_size
/ shdr_rel_plt
.sh_entsize
;
253 plt_offset
= shdr_plt
.sh_offset
;
255 if (shdr_rel_plt
.sh_type
== SHT_RELA
) {
256 GElf_Rela pos_mem
, *pos
;
258 elf_section__for_each_rela(reldata
, pos
, pos_mem
, idx
,
260 symidx
= GELF_R_SYM(pos
->r_info
);
261 plt_offset
+= shdr_plt
.sh_entsize
;
262 gelf_getsym(syms
, symidx
, &sym
);
263 snprintf(sympltname
, sizeof(sympltname
),
264 "%s@plt", elf_sym__name(&sym
, symstrs
));
266 f
= symbol__new(plt_offset
, shdr_plt
.sh_entsize
,
267 STB_GLOBAL
, sympltname
);
271 if (filter
&& filter(map
, f
))
274 symbols__insert(&dso
->symbols
[map
->type
], f
);
278 } else if (shdr_rel_plt
.sh_type
== SHT_REL
) {
279 GElf_Rel pos_mem
, *pos
;
280 elf_section__for_each_rel(reldata
, pos
, pos_mem
, idx
,
282 symidx
= GELF_R_SYM(pos
->r_info
);
283 plt_offset
+= shdr_plt
.sh_entsize
;
284 gelf_getsym(syms
, symidx
, &sym
);
285 snprintf(sympltname
, sizeof(sympltname
),
286 "%s@plt", elf_sym__name(&sym
, symstrs
));
288 f
= symbol__new(plt_offset
, shdr_plt
.sh_entsize
,
289 STB_GLOBAL
, sympltname
);
293 if (filter
&& filter(map
, f
))
296 symbols__insert(&dso
->symbols
[map
->type
], f
);
306 pr_debug("%s: problems reading %s PLT info.\n",
307 __func__
, dso
->long_name
);
312 * Align offset to 4 bytes as needed for note name and descriptor data.
314 #define NOTE_ALIGN(n) (((n) + 3) & -4U)
316 static int elf_read_build_id(Elf
*elf
, void *bf
, size_t size
)
326 if (size
< BUILD_ID_SIZE
)
333 if (gelf_getehdr(elf
, &ehdr
) == NULL
) {
334 pr_err("%s: cannot get elf header.\n", __func__
);
339 * Check following sections for notes:
340 * '.note.gnu.build-id'
342 * '.note' (VDSO specific)
345 sec
= elf_section_by_name(elf
, &ehdr
, &shdr
,
346 ".note.gnu.build-id", NULL
);
350 sec
= elf_section_by_name(elf
, &ehdr
, &shdr
,
355 sec
= elf_section_by_name(elf
, &ehdr
, &shdr
,
364 data
= elf_getdata(sec
, NULL
);
369 while (ptr
< (data
->d_buf
+ data
->d_size
)) {
370 GElf_Nhdr
*nhdr
= ptr
;
371 size_t namesz
= NOTE_ALIGN(nhdr
->n_namesz
),
372 descsz
= NOTE_ALIGN(nhdr
->n_descsz
);
375 ptr
+= sizeof(*nhdr
);
378 if (nhdr
->n_type
== NT_GNU_BUILD_ID
&&
379 nhdr
->n_namesz
== sizeof("GNU")) {
380 if (memcmp(name
, "GNU", sizeof("GNU")) == 0) {
381 size_t sz
= min(size
, descsz
);
383 memset(bf
+ sz
, 0, size
- sz
);
395 int filename__read_build_id(const char *filename
, void *bf
, size_t size
)
400 if (size
< BUILD_ID_SIZE
)
403 fd
= open(filename
, O_RDONLY
);
407 elf
= elf_begin(fd
, PERF_ELF_C_READ_MMAP
, NULL
);
409 pr_debug2("%s: cannot read %s ELF file.\n", __func__
, filename
);
413 err
= elf_read_build_id(elf
, bf
, size
);
422 int sysfs__read_build_id(const char *filename
, void *build_id
, size_t size
)
426 if (size
< BUILD_ID_SIZE
)
429 fd
= open(filename
, O_RDONLY
);
436 size_t namesz
, descsz
;
438 if (read(fd
, &nhdr
, sizeof(nhdr
)) != sizeof(nhdr
))
441 namesz
= NOTE_ALIGN(nhdr
.n_namesz
);
442 descsz
= NOTE_ALIGN(nhdr
.n_descsz
);
443 if (nhdr
.n_type
== NT_GNU_BUILD_ID
&&
444 nhdr
.n_namesz
== sizeof("GNU")) {
445 if (read(fd
, bf
, namesz
) != (ssize_t
)namesz
)
447 if (memcmp(bf
, "GNU", sizeof("GNU")) == 0) {
448 size_t sz
= min(descsz
, size
);
449 if (read(fd
, build_id
, sz
) == (ssize_t
)sz
) {
450 memset(build_id
+ sz
, 0, size
- sz
);
454 } else if (read(fd
, bf
, descsz
) != (ssize_t
)descsz
)
457 int n
= namesz
+ descsz
;
458 if (read(fd
, bf
, n
) != n
)
467 int filename__read_debuglink(const char *filename
, char *debuglink
,
478 fd
= open(filename
, O_RDONLY
);
482 elf
= elf_begin(fd
, PERF_ELF_C_READ_MMAP
, NULL
);
484 pr_debug2("%s: cannot read %s ELF file.\n", __func__
, filename
);
492 if (gelf_getehdr(elf
, &ehdr
) == NULL
) {
493 pr_err("%s: cannot get elf header.\n", __func__
);
497 sec
= elf_section_by_name(elf
, &ehdr
, &shdr
,
498 ".gnu_debuglink", NULL
);
502 data
= elf_getdata(sec
, NULL
);
506 /* the start of this section is a zero-terminated string */
507 strncpy(debuglink
, data
->d_buf
, size
);
517 static int dso__swap_init(struct dso
*dso
, unsigned char eidata
)
519 static unsigned int const endian
= 1;
521 dso
->needs_swap
= DSO_SWAP__NO
;
525 /* We are big endian, DSO is little endian. */
526 if (*(unsigned char const *)&endian
!= 1)
527 dso
->needs_swap
= DSO_SWAP__YES
;
531 /* We are little endian, DSO is big endian. */
532 if (*(unsigned char const *)&endian
!= 0)
533 dso
->needs_swap
= DSO_SWAP__YES
;
537 pr_err("unrecognized DSO data encoding %d\n", eidata
);
544 bool symsrc__possibly_runtime(struct symsrc
*ss
)
546 return ss
->dynsym
|| ss
->opdsec
;
549 bool symsrc__has_symtab(struct symsrc
*ss
)
551 return ss
->symtab
!= NULL
;
554 void symsrc__destroy(struct symsrc
*ss
)
561 int symsrc__init(struct symsrc
*ss
, struct dso
*dso
, const char *name
,
562 enum dso_binary_type type
)
569 fd
= open(name
, O_RDONLY
);
573 elf
= elf_begin(fd
, PERF_ELF_C_READ_MMAP
, NULL
);
575 pr_debug("%s: cannot read %s ELF file.\n", __func__
, name
);
579 if (gelf_getehdr(elf
, &ehdr
) == NULL
) {
580 pr_debug("%s: cannot get elf header.\n", __func__
);
584 if (dso__swap_init(dso
, ehdr
.e_ident
[EI_DATA
]))
587 /* Always reject images with a mismatched build-id: */
588 if (dso
->has_build_id
) {
589 u8 build_id
[BUILD_ID_SIZE
];
591 if (elf_read_build_id(elf
, build_id
, BUILD_ID_SIZE
) < 0)
594 if (!dso__build_id_equal(dso
, build_id
))
598 ss
->symtab
= elf_section_by_name(elf
, &ehdr
, &ss
->symshdr
, ".symtab",
600 if (ss
->symshdr
.sh_type
!= SHT_SYMTAB
)
604 ss
->dynsym
= elf_section_by_name(elf
, &ehdr
, &ss
->dynshdr
, ".dynsym",
606 if (ss
->dynshdr
.sh_type
!= SHT_DYNSYM
)
610 ss
->opdsec
= elf_section_by_name(elf
, &ehdr
, &ss
->opdshdr
, ".opd",
612 if (ss
->opdshdr
.sh_type
!= SHT_PROGBITS
)
615 if (dso
->kernel
== DSO_TYPE_USER
) {
617 ss
->adjust_symbols
= (ehdr
.e_type
== ET_EXEC
||
618 ehdr
.e_type
== ET_REL
||
619 elf_section_by_name(elf
, &ehdr
, &shdr
,
623 ss
->adjust_symbols
= ehdr
.e_type
== ET_EXEC
||
624 ehdr
.e_type
== ET_REL
;
627 ss
->name
= strdup(name
);
646 * ref_reloc_sym_not_found - has kernel relocation symbol been found.
647 * @kmap: kernel maps and relocation reference symbol
649 * This function returns %true if we are dealing with the kernel maps and the
650 * relocation reference symbol has not yet been found. Otherwise %false is
653 static bool ref_reloc_sym_not_found(struct kmap
*kmap
)
655 return kmap
&& kmap
->ref_reloc_sym
&& kmap
->ref_reloc_sym
->name
&&
656 !kmap
->ref_reloc_sym
->unrelocated_addr
;
660 * ref_reloc - kernel relocation offset.
661 * @kmap: kernel maps and relocation reference symbol
663 * This function returns the offset of kernel addresses as determined by using
664 * the relocation reference symbol i.e. if the kernel has not been relocated
665 * then the return value is zero.
667 static u64
ref_reloc(struct kmap
*kmap
)
669 if (kmap
&& kmap
->ref_reloc_sym
&&
670 kmap
->ref_reloc_sym
->unrelocated_addr
)
671 return kmap
->ref_reloc_sym
->addr
-
672 kmap
->ref_reloc_sym
->unrelocated_addr
;
676 int dso__load_sym(struct dso
*dso
, struct map
*map
,
677 struct symsrc
*syms_ss
, struct symsrc
*runtime_ss
,
678 symbol_filter_t filter
, int kmodule
)
680 struct kmap
*kmap
= dso
->kernel
? map__kmap(map
) : NULL
;
681 struct map
*curr_map
= map
;
682 struct dso
*curr_dso
= dso
;
683 Elf_Data
*symstrs
, *secstrs
;
689 Elf_Data
*syms
, *opddata
= NULL
;
691 Elf_Scn
*sec
, *sec_strndx
;
694 bool remap_kernel
= false, adjust_kernel_syms
= false;
696 dso
->symtab_type
= syms_ss
->type
;
697 dso
->rel
= syms_ss
->ehdr
.e_type
== ET_REL
;
700 * Modules may already have symbols from kallsyms, but those symbols
701 * have the wrong values for the dso maps, so remove them.
703 if (kmodule
&& syms_ss
->symtab
)
704 symbols__delete(&dso
->symbols
[map
->type
]);
706 if (!syms_ss
->symtab
) {
707 syms_ss
->symtab
= syms_ss
->dynsym
;
708 syms_ss
->symshdr
= syms_ss
->dynshdr
;
712 ehdr
= syms_ss
->ehdr
;
713 sec
= syms_ss
->symtab
;
714 shdr
= syms_ss
->symshdr
;
716 if (runtime_ss
->opdsec
)
717 opddata
= elf_rawdata(runtime_ss
->opdsec
, NULL
);
719 syms
= elf_getdata(sec
, NULL
);
723 sec
= elf_getscn(elf
, shdr
.sh_link
);
727 symstrs
= elf_getdata(sec
, NULL
);
731 sec_strndx
= elf_getscn(elf
, ehdr
.e_shstrndx
);
732 if (sec_strndx
== NULL
)
735 secstrs
= elf_getdata(sec_strndx
, NULL
);
739 nr_syms
= shdr
.sh_size
/ shdr
.sh_entsize
;
741 memset(&sym
, 0, sizeof(sym
));
744 * The kernel relocation symbol is needed in advance in order to adjust
745 * kernel maps correctly.
747 if (ref_reloc_sym_not_found(kmap
)) {
748 elf_symtab__for_each_symbol(syms
, nr_syms
, idx
, sym
) {
749 const char *elf_name
= elf_sym__name(&sym
, symstrs
);
751 if (strcmp(elf_name
, kmap
->ref_reloc_sym
->name
))
753 kmap
->ref_reloc_sym
->unrelocated_addr
= sym
.st_value
;
758 dso
->adjust_symbols
= runtime_ss
->adjust_symbols
|| ref_reloc(kmap
);
760 * Initial kernel and module mappings do not map to the dso. For
761 * function mappings, flag the fixups.
763 if (map
->type
== MAP__FUNCTION
&& (dso
->kernel
|| kmodule
)) {
765 adjust_kernel_syms
= dso
->adjust_symbols
;
767 elf_symtab__for_each_symbol(syms
, nr_syms
, idx
, sym
) {
769 const char *elf_name
= elf_sym__name(&sym
, symstrs
);
770 char *demangled
= NULL
;
771 int is_label
= elf_sym__is_label(&sym
);
772 const char *section_name
;
773 bool used_opd
= false;
775 if (!is_label
&& !elf_sym__is_a(&sym
, map
->type
))
778 /* Reject ARM ELF "mapping symbols": these aren't unique and
779 * don't identify functions, so will confuse the profile
781 if (ehdr
.e_machine
== EM_ARM
) {
782 if (!strcmp(elf_name
, "$a") ||
783 !strcmp(elf_name
, "$d") ||
784 !strcmp(elf_name
, "$t"))
788 if (runtime_ss
->opdsec
&& sym
.st_shndx
== runtime_ss
->opdidx
) {
789 u32 offset
= sym
.st_value
- syms_ss
->opdshdr
.sh_addr
;
790 u64
*opd
= opddata
->d_buf
+ offset
;
791 sym
.st_value
= DSO__SWAP(dso
, u64
, *opd
);
792 sym
.st_shndx
= elf_addr_to_index(runtime_ss
->elf
,
797 * When loading symbols in a data mapping, ABS symbols (which
798 * has a value of SHN_ABS in its st_shndx) failed at
799 * elf_getscn(). And it marks the loading as a failure so
800 * already loaded symbols cannot be fixed up.
802 * I'm not sure what should be done. Just ignore them for now.
805 if (sym
.st_shndx
== SHN_ABS
)
808 sec
= elf_getscn(runtime_ss
->elf
, sym
.st_shndx
);
812 gelf_getshdr(sec
, &shdr
);
814 if (is_label
&& !elf_sec__is_a(&shdr
, secstrs
, map
->type
))
817 section_name
= elf_sec__name(&shdr
, secstrs
);
819 /* On ARM, symbols for thumb functions have 1 added to
820 * the symbol address as a flag - remove it */
821 if ((ehdr
.e_machine
== EM_ARM
) &&
822 (map
->type
== MAP__FUNCTION
) &&
826 if (dso
->kernel
|| kmodule
) {
827 char dso_name
[PATH_MAX
];
829 /* Adjust symbol to map to file offset */
830 if (adjust_kernel_syms
)
831 sym
.st_value
-= shdr
.sh_addr
- shdr
.sh_offset
;
833 if (strcmp(section_name
,
834 (curr_dso
->short_name
+
835 dso
->short_name_len
)) == 0)
838 if (strcmp(section_name
, ".text") == 0) {
840 * The initial kernel mapping is based on
841 * kallsyms and identity maps. Overwrite it to
842 * map to the kernel dso.
844 if (remap_kernel
&& dso
->kernel
) {
845 remap_kernel
= false;
846 map
->start
= shdr
.sh_addr
+
848 map
->end
= map
->start
+ shdr
.sh_size
;
849 map
->pgoff
= shdr
.sh_offset
;
850 map
->map_ip
= map__map_ip
;
851 map
->unmap_ip
= map__unmap_ip
;
852 /* Ensure maps are correctly ordered */
853 map_groups__remove(kmap
->kmaps
, map
);
854 map_groups__insert(kmap
->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
),
876 "%s%s", dso
->short_name
, section_name
);
878 curr_map
= map_groups__find_by_name(kmap
->kmaps
, map
->type
, dso_name
);
879 if (curr_map
== NULL
) {
880 u64 start
= sym
.st_value
;
883 start
+= map
->start
+ shdr
.sh_offset
;
885 curr_dso
= dso__new(dso_name
);
886 if (curr_dso
== NULL
)
888 curr_dso
->kernel
= dso
->kernel
;
889 curr_dso
->long_name
= dso
->long_name
;
890 curr_dso
->long_name_len
= dso
->long_name_len
;
891 curr_map
= map__new2(start
, curr_dso
,
893 if (curr_map
== NULL
) {
894 dso__delete(curr_dso
);
897 if (adjust_kernel_syms
) {
898 curr_map
->start
= shdr
.sh_addr
+
900 curr_map
->end
= curr_map
->start
+
902 curr_map
->pgoff
= shdr
.sh_offset
;
904 curr_map
->map_ip
= identity__map_ip
;
905 curr_map
->unmap_ip
= identity__map_ip
;
907 curr_dso
->symtab_type
= dso
->symtab_type
;
908 map_groups__insert(kmap
->kmaps
, curr_map
);
909 dsos__add(&dso
->node
, curr_dso
);
910 dso__set_loaded(curr_dso
, map
->type
);
912 curr_dso
= curr_map
->dso
;
917 if ((used_opd
&& runtime_ss
->adjust_symbols
)
918 || (!used_opd
&& syms_ss
->adjust_symbols
)) {
919 pr_debug4("%s: adjusting symbol: st_value: %#" PRIx64
" "
920 "sh_addr: %#" PRIx64
" sh_offset: %#" PRIx64
"\n", __func__
,
921 (u64
)sym
.st_value
, (u64
)shdr
.sh_addr
,
922 (u64
)shdr
.sh_offset
);
923 sym
.st_value
-= shdr
.sh_addr
- shdr
.sh_offset
;
926 * We need to figure out if the object was created from C++ sources
927 * DWARF DW_compile_unit has this, but we don't always have access
930 if (symbol_conf
.demangle
) {
931 demangled
= bfd_demangle(NULL
, elf_name
,
932 DMGL_PARAMS
| DMGL_ANSI
);
933 if (demangled
!= NULL
)
934 elf_name
= demangled
;
937 f
= symbol__new(sym
.st_value
, sym
.st_size
,
938 GELF_ST_BIND(sym
.st_info
), elf_name
);
943 if (filter
&& filter(curr_map
, f
))
946 symbols__insert(&curr_dso
->symbols
[curr_map
->type
], f
);
952 * For misannotated, zeroed, ASM function sizes.
955 symbols__fixup_duplicate(&dso
->symbols
[map
->type
]);
956 symbols__fixup_end(&dso
->symbols
[map
->type
]);
959 * We need to fixup this here too because we create new
960 * maps here, for things like vsyscall sections.
962 __map_groups__fixup_end(kmap
->kmaps
, map
->type
);
970 static int elf_read_maps(Elf
*elf
, bool exe
, mapfn_t mapfn
, void *data
)
977 if (elf_getphdrnum(elf
, &phdrnum
))
980 for (i
= 0; i
< phdrnum
; i
++) {
981 if (gelf_getphdr(elf
, i
, &phdr
) == NULL
)
983 if (phdr
.p_type
!= PT_LOAD
)
986 if (!(phdr
.p_flags
& PF_X
))
989 if (!(phdr
.p_flags
& PF_R
))
992 sz
= min(phdr
.p_memsz
, phdr
.p_filesz
);
995 err
= mapfn(phdr
.p_vaddr
, sz
, phdr
.p_offset
, data
);
1002 int file__read_maps(int fd
, bool exe
, mapfn_t mapfn
, void *data
,
1008 elf
= elf_begin(fd
, PERF_ELF_C_READ_MMAP
, NULL
);
1013 *is_64_bit
= (gelf_getclass(elf
) == ELFCLASS64
);
1015 err
= elf_read_maps(elf
, exe
, mapfn
, data
);
1021 static int copy_bytes(int from
, off_t from_offs
, int to
, off_t to_offs
, u64 len
)
1026 char *buf
= malloc(page_size
);
1031 if (lseek(to
, to_offs
, SEEK_SET
) != to_offs
)
1034 if (lseek(from
, from_offs
, SEEK_SET
) != from_offs
)
1041 /* Use read because mmap won't work on proc files */
1042 r
= read(from
, buf
, n
);
1048 r
= write(to
, buf
, n
);
1069 static int kcore__open(struct kcore
*kcore
, const char *filename
)
1073 kcore
->fd
= open(filename
, O_RDONLY
);
1074 if (kcore
->fd
== -1)
1077 kcore
->elf
= elf_begin(kcore
->fd
, ELF_C_READ
, NULL
);
1081 kcore
->elfclass
= gelf_getclass(kcore
->elf
);
1082 if (kcore
->elfclass
== ELFCLASSNONE
)
1085 ehdr
= gelf_getehdr(kcore
->elf
, &kcore
->ehdr
);
1092 elf_end(kcore
->elf
);
1098 static int kcore__init(struct kcore
*kcore
, char *filename
, int elfclass
,
1103 kcore
->elfclass
= elfclass
;
1106 kcore
->fd
= mkstemp(filename
);
1108 kcore
->fd
= open(filename
, O_WRONLY
| O_CREAT
| O_EXCL
, 0400);
1109 if (kcore
->fd
== -1)
1112 kcore
->elf
= elf_begin(kcore
->fd
, ELF_C_WRITE
, NULL
);
1116 if (!gelf_newehdr(kcore
->elf
, elfclass
))
1119 ehdr
= gelf_getehdr(kcore
->elf
, &kcore
->ehdr
);
1126 elf_end(kcore
->elf
);
1133 static void kcore__close(struct kcore
*kcore
)
1135 elf_end(kcore
->elf
);
1139 static int kcore__copy_hdr(struct kcore
*from
, struct kcore
*to
, size_t count
)
1141 GElf_Ehdr
*ehdr
= &to
->ehdr
;
1142 GElf_Ehdr
*kehdr
= &from
->ehdr
;
1144 memcpy(ehdr
->e_ident
, kehdr
->e_ident
, EI_NIDENT
);
1145 ehdr
->e_type
= kehdr
->e_type
;
1146 ehdr
->e_machine
= kehdr
->e_machine
;
1147 ehdr
->e_version
= kehdr
->e_version
;
1150 ehdr
->e_flags
= kehdr
->e_flags
;
1151 ehdr
->e_phnum
= count
;
1152 ehdr
->e_shentsize
= 0;
1154 ehdr
->e_shstrndx
= 0;
1156 if (from
->elfclass
== ELFCLASS32
) {
1157 ehdr
->e_phoff
= sizeof(Elf32_Ehdr
);
1158 ehdr
->e_ehsize
= sizeof(Elf32_Ehdr
);
1159 ehdr
->e_phentsize
= sizeof(Elf32_Phdr
);
1161 ehdr
->e_phoff
= sizeof(Elf64_Ehdr
);
1162 ehdr
->e_ehsize
= sizeof(Elf64_Ehdr
);
1163 ehdr
->e_phentsize
= sizeof(Elf64_Phdr
);
1166 if (!gelf_update_ehdr(to
->elf
, ehdr
))
1169 if (!gelf_newphdr(to
->elf
, count
))
1175 static int kcore__add_phdr(struct kcore
*kcore
, int idx
, off_t offset
,
1181 phdr
= gelf_getphdr(kcore
->elf
, idx
, &gphdr
);
1185 phdr
->p_type
= PT_LOAD
;
1186 phdr
->p_flags
= PF_R
| PF_W
| PF_X
;
1187 phdr
->p_offset
= offset
;
1188 phdr
->p_vaddr
= addr
;
1190 phdr
->p_filesz
= len
;
1191 phdr
->p_memsz
= len
;
1192 phdr
->p_align
= page_size
;
1194 if (!gelf_update_phdr(kcore
->elf
, idx
, phdr
))
1200 static off_t
kcore__write(struct kcore
*kcore
)
1202 return elf_update(kcore
->elf
, ELF_C_WRITE
);
1211 struct kcore_copy_info
{
1217 u64 last_module_symbol
;
1218 struct phdr_data kernel_map
;
1219 struct phdr_data modules_map
;
1222 static int kcore_copy__process_kallsyms(void *arg
, const char *name
, char type
,
1225 struct kcore_copy_info
*kci
= arg
;
1227 if (!symbol_type__is_a(type
, MAP__FUNCTION
))
1230 if (strchr(name
, '[')) {
1231 if (start
> kci
->last_module_symbol
)
1232 kci
->last_module_symbol
= start
;
1236 if (!kci
->first_symbol
|| start
< kci
->first_symbol
)
1237 kci
->first_symbol
= start
;
1239 if (!kci
->last_symbol
|| start
> kci
->last_symbol
)
1240 kci
->last_symbol
= start
;
1242 if (!strcmp(name
, "_stext")) {
1247 if (!strcmp(name
, "_etext")) {
1255 static int kcore_copy__parse_kallsyms(struct kcore_copy_info
*kci
,
1258 char kallsyms_filename
[PATH_MAX
];
1260 scnprintf(kallsyms_filename
, PATH_MAX
, "%s/kallsyms", dir
);
1262 if (symbol__restricted_filename(kallsyms_filename
, "/proc/kallsyms"))
1265 if (kallsyms__parse(kallsyms_filename
, kci
,
1266 kcore_copy__process_kallsyms
) < 0)
1272 static int kcore_copy__process_modules(void *arg
,
1273 const char *name __maybe_unused
,
1276 struct kcore_copy_info
*kci
= arg
;
1278 if (!kci
->first_module
|| start
< kci
->first_module
)
1279 kci
->first_module
= start
;
1284 static int kcore_copy__parse_modules(struct kcore_copy_info
*kci
,
1287 char modules_filename
[PATH_MAX
];
1289 scnprintf(modules_filename
, PATH_MAX
, "%s/modules", dir
);
1291 if (symbol__restricted_filename(modules_filename
, "/proc/modules"))
1294 if (modules__parse(modules_filename
, kci
,
1295 kcore_copy__process_modules
) < 0)
1301 static void kcore_copy__map(struct phdr_data
*p
, u64 start
, u64 end
, u64 pgoff
,
1304 if (p
->addr
|| s
< start
|| s
>= end
)
1308 p
->offset
= (s
- start
) + pgoff
;
1309 p
->len
= e
< end
? e
- s
: end
- s
;
1312 static int kcore_copy__read_map(u64 start
, u64 len
, u64 pgoff
, void *data
)
1314 struct kcore_copy_info
*kci
= data
;
1315 u64 end
= start
+ len
;
1317 kcore_copy__map(&kci
->kernel_map
, start
, end
, pgoff
, kci
->stext
,
1320 kcore_copy__map(&kci
->modules_map
, start
, end
, pgoff
, kci
->first_module
,
1321 kci
->last_module_symbol
);
1326 static int kcore_copy__read_maps(struct kcore_copy_info
*kci
, Elf
*elf
)
1328 if (elf_read_maps(elf
, true, kcore_copy__read_map
, kci
) < 0)
1334 static int kcore_copy__calc_maps(struct kcore_copy_info
*kci
, const char *dir
,
1337 if (kcore_copy__parse_kallsyms(kci
, dir
))
1340 if (kcore_copy__parse_modules(kci
, dir
))
1344 kci
->stext
= round_down(kci
->stext
, page_size
);
1346 kci
->stext
= round_down(kci
->first_symbol
, page_size
);
1349 kci
->etext
= round_up(kci
->etext
, page_size
);
1350 } else if (kci
->last_symbol
) {
1351 kci
->etext
= round_up(kci
->last_symbol
, page_size
);
1352 kci
->etext
+= page_size
;
1355 kci
->first_module
= round_down(kci
->first_module
, page_size
);
1357 if (kci
->last_module_symbol
) {
1358 kci
->last_module_symbol
= round_up(kci
->last_module_symbol
,
1360 kci
->last_module_symbol
+= page_size
;
1363 if (!kci
->stext
|| !kci
->etext
)
1366 if (kci
->first_module
&& !kci
->last_module_symbol
)
1369 return kcore_copy__read_maps(kci
, elf
);
1372 static int kcore_copy__copy_file(const char *from_dir
, const char *to_dir
,
1375 char from_filename
[PATH_MAX
];
1376 char to_filename
[PATH_MAX
];
1378 scnprintf(from_filename
, PATH_MAX
, "%s/%s", from_dir
, name
);
1379 scnprintf(to_filename
, PATH_MAX
, "%s/%s", to_dir
, name
);
1381 return copyfile_mode(from_filename
, to_filename
, 0400);
1384 static int kcore_copy__unlink(const char *dir
, const char *name
)
1386 char filename
[PATH_MAX
];
1388 scnprintf(filename
, PATH_MAX
, "%s/%s", dir
, name
);
1390 return unlink(filename
);
1393 static int kcore_copy__compare_fds(int from
, int to
)
1401 buf_from
= malloc(page_size
);
1402 buf_to
= malloc(page_size
);
1403 if (!buf_from
|| !buf_to
)
1407 /* Use read because mmap won't work on proc files */
1408 ret
= read(from
, buf_from
, page_size
);
1417 if (readn(to
, buf_to
, len
) != (int)len
)
1420 if (memcmp(buf_from
, buf_to
, len
))
1431 static int kcore_copy__compare_files(const char *from_filename
,
1432 const char *to_filename
)
1434 int from
, to
, err
= -1;
1436 from
= open(from_filename
, O_RDONLY
);
1440 to
= open(to_filename
, O_RDONLY
);
1442 goto out_close_from
;
1444 err
= kcore_copy__compare_fds(from
, to
);
1452 static int kcore_copy__compare_file(const char *from_dir
, const char *to_dir
,
1455 char from_filename
[PATH_MAX
];
1456 char to_filename
[PATH_MAX
];
1458 scnprintf(from_filename
, PATH_MAX
, "%s/%s", from_dir
, name
);
1459 scnprintf(to_filename
, PATH_MAX
, "%s/%s", to_dir
, name
);
1461 return kcore_copy__compare_files(from_filename
, to_filename
);
1465 * kcore_copy - copy kallsyms, modules and kcore from one directory to another.
1466 * @from_dir: from directory
1467 * @to_dir: to directory
1469 * This function copies kallsyms, modules and kcore files from one directory to
1470 * another. kallsyms and modules are copied entirely. Only code segments are
1471 * copied from kcore. It is assumed that two segments suffice: one for the
1472 * kernel proper and one for all the modules. The code segments are determined
1473 * from kallsyms and modules files. The kernel map starts at _stext or the
1474 * lowest function symbol, and ends at _etext or the highest function symbol.
1475 * The module map starts at the lowest module address and ends at the highest
1476 * module symbol. Start addresses are rounded down to the nearest page. End
1477 * addresses are rounded up to the nearest page. An extra page is added to the
1478 * highest kernel symbol and highest module symbol to, hopefully, encompass that
1479 * symbol too. Because it contains only code sections, the resulting kcore is
1480 * unusual. One significant peculiarity is that the mapping (start -> pgoff)
1481 * is not the same for the kernel map and the modules map. That happens because
1482 * the data is copied adjacently whereas the original kcore has gaps. Finally,
1483 * kallsyms and modules files are compared with their copies to check that
1484 * modules have not been loaded or unloaded while the copies were taking place.
1486 * Return: %0 on success, %-1 on failure.
1488 int kcore_copy(const char *from_dir
, const char *to_dir
)
1491 struct kcore extract
;
1493 int idx
= 0, err
= -1;
1494 off_t offset
= page_size
, sz
, modules_offset
= 0;
1495 struct kcore_copy_info kci
= { .stext
= 0, };
1496 char kcore_filename
[PATH_MAX
];
1497 char extract_filename
[PATH_MAX
];
1499 if (kcore_copy__copy_file(from_dir
, to_dir
, "kallsyms"))
1502 if (kcore_copy__copy_file(from_dir
, to_dir
, "modules"))
1503 goto out_unlink_kallsyms
;
1505 scnprintf(kcore_filename
, PATH_MAX
, "%s/kcore", from_dir
);
1506 scnprintf(extract_filename
, PATH_MAX
, "%s/kcore", to_dir
);
1508 if (kcore__open(&kcore
, kcore_filename
))
1509 goto out_unlink_modules
;
1511 if (kcore_copy__calc_maps(&kci
, from_dir
, kcore
.elf
))
1512 goto out_kcore_close
;
1514 if (kcore__init(&extract
, extract_filename
, kcore
.elfclass
, false))
1515 goto out_kcore_close
;
1517 if (!kci
.modules_map
.addr
)
1520 if (kcore__copy_hdr(&kcore
, &extract
, count
))
1521 goto out_extract_close
;
1523 if (kcore__add_phdr(&extract
, idx
++, offset
, kci
.kernel_map
.addr
,
1524 kci
.kernel_map
.len
))
1525 goto out_extract_close
;
1527 if (kci
.modules_map
.addr
) {
1528 modules_offset
= offset
+ kci
.kernel_map
.len
;
1529 if (kcore__add_phdr(&extract
, idx
, modules_offset
,
1530 kci
.modules_map
.addr
, kci
.modules_map
.len
))
1531 goto out_extract_close
;
1534 sz
= kcore__write(&extract
);
1535 if (sz
< 0 || sz
> offset
)
1536 goto out_extract_close
;
1538 if (copy_bytes(kcore
.fd
, kci
.kernel_map
.offset
, extract
.fd
, offset
,
1539 kci
.kernel_map
.len
))
1540 goto out_extract_close
;
1542 if (modules_offset
&& copy_bytes(kcore
.fd
, kci
.modules_map
.offset
,
1543 extract
.fd
, modules_offset
,
1544 kci
.modules_map
.len
))
1545 goto out_extract_close
;
1547 if (kcore_copy__compare_file(from_dir
, to_dir
, "modules"))
1548 goto out_extract_close
;
1550 if (kcore_copy__compare_file(from_dir
, to_dir
, "kallsyms"))
1551 goto out_extract_close
;
1556 kcore__close(&extract
);
1558 unlink(extract_filename
);
1560 kcore__close(&kcore
);
1563 kcore_copy__unlink(to_dir
, "modules");
1564 out_unlink_kallsyms
:
1566 kcore_copy__unlink(to_dir
, "kallsyms");
1571 int kcore_extract__create(struct kcore_extract
*kce
)
1574 struct kcore extract
;
1576 int idx
= 0, err
= -1;
1577 off_t offset
= page_size
, sz
;
1579 if (kcore__open(&kcore
, kce
->kcore_filename
))
1582 strcpy(kce
->extract_filename
, PERF_KCORE_EXTRACT
);
1583 if (kcore__init(&extract
, kce
->extract_filename
, kcore
.elfclass
, true))
1584 goto out_kcore_close
;
1586 if (kcore__copy_hdr(&kcore
, &extract
, count
))
1587 goto out_extract_close
;
1589 if (kcore__add_phdr(&extract
, idx
, offset
, kce
->addr
, kce
->len
))
1590 goto out_extract_close
;
1592 sz
= kcore__write(&extract
);
1593 if (sz
< 0 || sz
> offset
)
1594 goto out_extract_close
;
1596 if (copy_bytes(kcore
.fd
, kce
->offs
, extract
.fd
, offset
, kce
->len
))
1597 goto out_extract_close
;
1602 kcore__close(&extract
);
1604 unlink(kce
->extract_filename
);
1606 kcore__close(&kcore
);
1611 void kcore_extract__delete(struct kcore_extract
*kce
)
1613 unlink(kce
->extract_filename
);
1616 void symbol__elf_init(void)
1618 elf_version(EV_CURRENT
);