10 #include <symbol/kallsyms.h>
13 #ifndef HAVE_ELF_GETPHDRNUM_SUPPORT
14 static int elf_getphdrnum(Elf
*elf
, size_t *dst
)
19 ehdr
= gelf_getehdr(elf
, &gehdr
);
29 #ifndef NT_GNU_BUILD_ID
30 #define NT_GNU_BUILD_ID 3
34 * elf_symtab__for_each_symbol - iterate thru all the symbols
36 * @syms: struct elf_symtab instance to iterate
38 * @sym: GElf_Sym iterator
40 #define elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) \
41 for (idx = 0, gelf_getsym(syms, idx, &sym);\
43 idx++, gelf_getsym(syms, idx, &sym))
45 static inline uint8_t elf_sym__type(const GElf_Sym
*sym
)
47 return GELF_ST_TYPE(sym
->st_info
);
50 static inline int elf_sym__is_function(const GElf_Sym
*sym
)
52 return (elf_sym__type(sym
) == STT_FUNC
||
53 elf_sym__type(sym
) == STT_GNU_IFUNC
) &&
55 sym
->st_shndx
!= SHN_UNDEF
;
58 static inline bool elf_sym__is_object(const GElf_Sym
*sym
)
60 return elf_sym__type(sym
) == STT_OBJECT
&&
62 sym
->st_shndx
!= SHN_UNDEF
;
65 static inline int elf_sym__is_label(const GElf_Sym
*sym
)
67 return elf_sym__type(sym
) == STT_NOTYPE
&&
69 sym
->st_shndx
!= SHN_UNDEF
&&
70 sym
->st_shndx
!= SHN_ABS
;
73 static bool elf_sym__is_a(GElf_Sym
*sym
, enum map_type type
)
77 return elf_sym__is_function(sym
);
79 return elf_sym__is_object(sym
);
85 static inline const char *elf_sym__name(const GElf_Sym
*sym
,
86 const Elf_Data
*symstrs
)
88 return symstrs
->d_buf
+ sym
->st_name
;
91 static inline const char *elf_sec__name(const GElf_Shdr
*shdr
,
92 const Elf_Data
*secstrs
)
94 return secstrs
->d_buf
+ shdr
->sh_name
;
97 static inline int elf_sec__is_text(const GElf_Shdr
*shdr
,
98 const Elf_Data
*secstrs
)
100 return strstr(elf_sec__name(shdr
, secstrs
), "text") != NULL
;
103 static inline bool elf_sec__is_data(const GElf_Shdr
*shdr
,
104 const Elf_Data
*secstrs
)
106 return strstr(elf_sec__name(shdr
, secstrs
), "data") != NULL
;
109 static bool elf_sec__is_a(GElf_Shdr
*shdr
, Elf_Data
*secstrs
,
114 return elf_sec__is_text(shdr
, secstrs
);
116 return elf_sec__is_data(shdr
, secstrs
);
122 static size_t elf_addr_to_index(Elf
*elf
, GElf_Addr addr
)
128 while ((sec
= elf_nextscn(elf
, sec
)) != NULL
) {
129 gelf_getshdr(sec
, &shdr
);
131 if ((addr
>= shdr
.sh_addr
) &&
132 (addr
< (shdr
.sh_addr
+ shdr
.sh_size
)))
141 Elf_Scn
*elf_section_by_name(Elf
*elf
, GElf_Ehdr
*ep
,
142 GElf_Shdr
*shp
, const char *name
, size_t *idx
)
147 /* Elf is corrupted/truncated, avoid calling elf_strptr. */
148 if (!elf_rawdata(elf_getscn(elf
, ep
->e_shstrndx
), NULL
))
151 while ((sec
= elf_nextscn(elf
, sec
)) != NULL
) {
154 gelf_getshdr(sec
, shp
);
155 str
= elf_strptr(elf
, ep
->e_shstrndx
, shp
->sh_name
);
156 if (str
&& !strcmp(name
, str
)) {
167 #define elf_section__for_each_rel(reldata, pos, pos_mem, idx, nr_entries) \
168 for (idx = 0, pos = gelf_getrel(reldata, 0, &pos_mem); \
170 ++idx, pos = gelf_getrel(reldata, idx, &pos_mem))
172 #define elf_section__for_each_rela(reldata, pos, pos_mem, idx, nr_entries) \
173 for (idx = 0, pos = gelf_getrela(reldata, 0, &pos_mem); \
175 ++idx, pos = gelf_getrela(reldata, idx, &pos_mem))
178 * We need to check if we have a .dynsym, so that we can handle the
179 * .plt, synthesizing its symbols, that aren't on the symtabs (be it
180 * .dynsym or .symtab).
181 * And always look at the original dso, not at debuginfo packages, that
182 * have the PLT data stripped out (shdr_rel_plt.sh_type == SHT_NOBITS).
184 int dso__synthesize_plt_symbols(struct dso
*dso
, struct symsrc
*ss
, struct map
*map
,
185 symbol_filter_t filter
)
187 uint32_t nr_rel_entries
, idx
;
192 GElf_Shdr shdr_rel_plt
, shdr_dynsym
;
193 Elf_Data
*reldata
, *syms
, *symstrs
;
194 Elf_Scn
*scn_plt_rel
, *scn_symstrs
, *scn_dynsym
;
197 char sympltname
[1024];
199 int nr
= 0, symidx
, err
= 0;
207 scn_dynsym
= ss
->dynsym
;
208 shdr_dynsym
= ss
->dynshdr
;
209 dynsym_idx
= ss
->dynsym_idx
;
211 if (scn_dynsym
== NULL
)
214 scn_plt_rel
= elf_section_by_name(elf
, &ehdr
, &shdr_rel_plt
,
216 if (scn_plt_rel
== NULL
) {
217 scn_plt_rel
= elf_section_by_name(elf
, &ehdr
, &shdr_rel_plt
,
219 if (scn_plt_rel
== NULL
)
225 if (shdr_rel_plt
.sh_link
!= dynsym_idx
)
228 if (elf_section_by_name(elf
, &ehdr
, &shdr_plt
, ".plt", NULL
) == NULL
)
232 * Fetch the relocation section to find the idxes to the GOT
233 * and the symbols in the .dynsym they refer to.
235 reldata
= elf_getdata(scn_plt_rel
, NULL
);
239 syms
= elf_getdata(scn_dynsym
, NULL
);
243 scn_symstrs
= elf_getscn(elf
, shdr_dynsym
.sh_link
);
244 if (scn_symstrs
== NULL
)
247 symstrs
= elf_getdata(scn_symstrs
, NULL
);
251 if (symstrs
->d_size
== 0)
254 nr_rel_entries
= shdr_rel_plt
.sh_size
/ shdr_rel_plt
.sh_entsize
;
255 plt_offset
= shdr_plt
.sh_offset
;
257 if (shdr_rel_plt
.sh_type
== SHT_RELA
) {
258 GElf_Rela pos_mem
, *pos
;
260 elf_section__for_each_rela(reldata
, pos
, pos_mem
, idx
,
262 symidx
= GELF_R_SYM(pos
->r_info
);
263 plt_offset
+= shdr_plt
.sh_entsize
;
264 gelf_getsym(syms
, symidx
, &sym
);
265 snprintf(sympltname
, sizeof(sympltname
),
266 "%s@plt", elf_sym__name(&sym
, symstrs
));
268 f
= symbol__new(plt_offset
, shdr_plt
.sh_entsize
,
269 STB_GLOBAL
, sympltname
);
273 if (filter
&& filter(map
, f
))
276 symbols__insert(&dso
->symbols
[map
->type
], f
);
280 } else if (shdr_rel_plt
.sh_type
== SHT_REL
) {
281 GElf_Rel pos_mem
, *pos
;
282 elf_section__for_each_rel(reldata
, pos
, pos_mem
, idx
,
284 symidx
= GELF_R_SYM(pos
->r_info
);
285 plt_offset
+= shdr_plt
.sh_entsize
;
286 gelf_getsym(syms
, symidx
, &sym
);
287 snprintf(sympltname
, sizeof(sympltname
),
288 "%s@plt", elf_sym__name(&sym
, symstrs
));
290 f
= symbol__new(plt_offset
, shdr_plt
.sh_entsize
,
291 STB_GLOBAL
, sympltname
);
295 if (filter
&& filter(map
, f
))
298 symbols__insert(&dso
->symbols
[map
->type
], f
);
308 pr_debug("%s: problems reading %s PLT info.\n",
309 __func__
, dso
->long_name
);
314 * Align offset to 4 bytes as needed for note name and descriptor data.
316 #define NOTE_ALIGN(n) (((n) + 3) & -4U)
318 static int elf_read_build_id(Elf
*elf
, void *bf
, size_t size
)
328 if (size
< BUILD_ID_SIZE
)
335 if (gelf_getehdr(elf
, &ehdr
) == NULL
) {
336 pr_err("%s: cannot get elf header.\n", __func__
);
341 * Check following sections for notes:
342 * '.note.gnu.build-id'
344 * '.note' (VDSO specific)
347 sec
= elf_section_by_name(elf
, &ehdr
, &shdr
,
348 ".note.gnu.build-id", NULL
);
352 sec
= elf_section_by_name(elf
, &ehdr
, &shdr
,
357 sec
= elf_section_by_name(elf
, &ehdr
, &shdr
,
366 data
= elf_getdata(sec
, NULL
);
371 while (ptr
< (data
->d_buf
+ data
->d_size
)) {
372 GElf_Nhdr
*nhdr
= ptr
;
373 size_t namesz
= NOTE_ALIGN(nhdr
->n_namesz
),
374 descsz
= NOTE_ALIGN(nhdr
->n_descsz
);
377 ptr
+= sizeof(*nhdr
);
380 if (nhdr
->n_type
== NT_GNU_BUILD_ID
&&
381 nhdr
->n_namesz
== sizeof("GNU")) {
382 if (memcmp(name
, "GNU", sizeof("GNU")) == 0) {
383 size_t sz
= min(size
, descsz
);
385 memset(bf
+ sz
, 0, size
- sz
);
397 int filename__read_build_id(const char *filename
, void *bf
, size_t size
)
402 if (size
< BUILD_ID_SIZE
)
405 fd
= open(filename
, O_RDONLY
);
409 elf
= elf_begin(fd
, PERF_ELF_C_READ_MMAP
, NULL
);
411 pr_debug2("%s: cannot read %s ELF file.\n", __func__
, filename
);
415 err
= elf_read_build_id(elf
, bf
, size
);
424 int sysfs__read_build_id(const char *filename
, void *build_id
, size_t size
)
428 if (size
< BUILD_ID_SIZE
)
431 fd
= open(filename
, O_RDONLY
);
438 size_t namesz
, descsz
;
440 if (read(fd
, &nhdr
, sizeof(nhdr
)) != sizeof(nhdr
))
443 namesz
= NOTE_ALIGN(nhdr
.n_namesz
);
444 descsz
= NOTE_ALIGN(nhdr
.n_descsz
);
445 if (nhdr
.n_type
== NT_GNU_BUILD_ID
&&
446 nhdr
.n_namesz
== sizeof("GNU")) {
447 if (read(fd
, bf
, namesz
) != (ssize_t
)namesz
)
449 if (memcmp(bf
, "GNU", sizeof("GNU")) == 0) {
450 size_t sz
= min(descsz
, size
);
451 if (read(fd
, build_id
, sz
) == (ssize_t
)sz
) {
452 memset(build_id
+ sz
, 0, size
- sz
);
456 } else if (read(fd
, bf
, descsz
) != (ssize_t
)descsz
)
459 int n
= namesz
+ descsz
;
460 if (read(fd
, bf
, n
) != n
)
469 int filename__read_debuglink(const char *filename
, char *debuglink
,
480 fd
= open(filename
, O_RDONLY
);
484 elf
= elf_begin(fd
, PERF_ELF_C_READ_MMAP
, NULL
);
486 pr_debug2("%s: cannot read %s ELF file.\n", __func__
, filename
);
494 if (gelf_getehdr(elf
, &ehdr
) == NULL
) {
495 pr_err("%s: cannot get elf header.\n", __func__
);
499 sec
= elf_section_by_name(elf
, &ehdr
, &shdr
,
500 ".gnu_debuglink", NULL
);
504 data
= elf_getdata(sec
, NULL
);
508 /* the start of this section is a zero-terminated string */
509 strncpy(debuglink
, data
->d_buf
, size
);
521 static int dso__swap_init(struct dso
*dso
, unsigned char eidata
)
523 static unsigned int const endian
= 1;
525 dso
->needs_swap
= DSO_SWAP__NO
;
529 /* We are big endian, DSO is little endian. */
530 if (*(unsigned char const *)&endian
!= 1)
531 dso
->needs_swap
= DSO_SWAP__YES
;
535 /* We are little endian, DSO is big endian. */
536 if (*(unsigned char const *)&endian
!= 0)
537 dso
->needs_swap
= DSO_SWAP__YES
;
541 pr_err("unrecognized DSO data encoding %d\n", eidata
);
548 bool symsrc__possibly_runtime(struct symsrc
*ss
)
550 return ss
->dynsym
|| ss
->opdsec
;
553 bool symsrc__has_symtab(struct symsrc
*ss
)
555 return ss
->symtab
!= NULL
;
558 void symsrc__destroy(struct symsrc
*ss
)
565 int symsrc__init(struct symsrc
*ss
, struct dso
*dso
, const char *name
,
566 enum dso_binary_type type
)
573 fd
= open(name
, O_RDONLY
);
577 elf
= elf_begin(fd
, PERF_ELF_C_READ_MMAP
, NULL
);
579 pr_debug("%s: cannot read %s ELF file.\n", __func__
, name
);
583 if (gelf_getehdr(elf
, &ehdr
) == NULL
) {
584 pr_debug("%s: cannot get elf header.\n", __func__
);
588 if (dso__swap_init(dso
, ehdr
.e_ident
[EI_DATA
]))
591 /* Always reject images with a mismatched build-id: */
592 if (dso
->has_build_id
) {
593 u8 build_id
[BUILD_ID_SIZE
];
595 if (elf_read_build_id(elf
, build_id
, BUILD_ID_SIZE
) < 0)
598 if (!dso__build_id_equal(dso
, build_id
))
602 ss
->is_64_bit
= (gelf_getclass(elf
) == ELFCLASS64
);
604 ss
->symtab
= elf_section_by_name(elf
, &ehdr
, &ss
->symshdr
, ".symtab",
606 if (ss
->symshdr
.sh_type
!= SHT_SYMTAB
)
610 ss
->dynsym
= elf_section_by_name(elf
, &ehdr
, &ss
->dynshdr
, ".dynsym",
612 if (ss
->dynshdr
.sh_type
!= SHT_DYNSYM
)
616 ss
->opdsec
= elf_section_by_name(elf
, &ehdr
, &ss
->opdshdr
, ".opd",
618 if (ss
->opdshdr
.sh_type
!= SHT_PROGBITS
)
621 if (dso
->kernel
== DSO_TYPE_USER
) {
623 ss
->adjust_symbols
= (ehdr
.e_type
== ET_EXEC
||
624 ehdr
.e_type
== ET_REL
||
626 elf_section_by_name(elf
, &ehdr
, &shdr
,
630 ss
->adjust_symbols
= ehdr
.e_type
== ET_EXEC
||
631 ehdr
.e_type
== ET_REL
;
634 ss
->name
= strdup(name
);
653 * ref_reloc_sym_not_found - has kernel relocation symbol been found.
654 * @kmap: kernel maps and relocation reference symbol
656 * This function returns %true if we are dealing with the kernel maps and the
657 * relocation reference symbol has not yet been found. Otherwise %false is
660 static bool ref_reloc_sym_not_found(struct kmap
*kmap
)
662 return kmap
&& kmap
->ref_reloc_sym
&& kmap
->ref_reloc_sym
->name
&&
663 !kmap
->ref_reloc_sym
->unrelocated_addr
;
667 * ref_reloc - kernel relocation offset.
668 * @kmap: kernel maps and relocation reference symbol
670 * This function returns the offset of kernel addresses as determined by using
671 * the relocation reference symbol i.e. if the kernel has not been relocated
672 * then the return value is zero.
674 static u64
ref_reloc(struct kmap
*kmap
)
676 if (kmap
&& kmap
->ref_reloc_sym
&&
677 kmap
->ref_reloc_sym
->unrelocated_addr
)
678 return kmap
->ref_reloc_sym
->addr
-
679 kmap
->ref_reloc_sym
->unrelocated_addr
;
683 int dso__load_sym(struct dso
*dso
, struct map
*map
,
684 struct symsrc
*syms_ss
, struct symsrc
*runtime_ss
,
685 symbol_filter_t filter
, int kmodule
)
687 struct kmap
*kmap
= dso
->kernel
? map__kmap(map
) : NULL
;
688 struct map
*curr_map
= map
;
689 struct dso
*curr_dso
= dso
;
690 Elf_Data
*symstrs
, *secstrs
;
696 Elf_Data
*syms
, *opddata
= NULL
;
698 Elf_Scn
*sec
, *sec_strndx
;
701 bool remap_kernel
= false, adjust_kernel_syms
= false;
703 dso
->symtab_type
= syms_ss
->type
;
704 dso
->is_64_bit
= syms_ss
->is_64_bit
;
705 dso
->rel
= syms_ss
->ehdr
.e_type
== ET_REL
;
708 * Modules may already have symbols from kallsyms, but those symbols
709 * have the wrong values for the dso maps, so remove them.
711 if (kmodule
&& syms_ss
->symtab
)
712 symbols__delete(&dso
->symbols
[map
->type
]);
714 if (!syms_ss
->symtab
) {
715 syms_ss
->symtab
= syms_ss
->dynsym
;
716 syms_ss
->symshdr
= syms_ss
->dynshdr
;
720 ehdr
= syms_ss
->ehdr
;
721 sec
= syms_ss
->symtab
;
722 shdr
= syms_ss
->symshdr
;
724 if (runtime_ss
->opdsec
)
725 opddata
= elf_rawdata(runtime_ss
->opdsec
, NULL
);
727 syms
= elf_getdata(sec
, NULL
);
731 sec
= elf_getscn(elf
, shdr
.sh_link
);
735 symstrs
= elf_getdata(sec
, NULL
);
739 sec_strndx
= elf_getscn(elf
, ehdr
.e_shstrndx
);
740 if (sec_strndx
== NULL
)
743 secstrs
= elf_getdata(sec_strndx
, NULL
);
747 nr_syms
= shdr
.sh_size
/ shdr
.sh_entsize
;
749 memset(&sym
, 0, sizeof(sym
));
752 * The kernel relocation symbol is needed in advance in order to adjust
753 * kernel maps correctly.
755 if (ref_reloc_sym_not_found(kmap
)) {
756 elf_symtab__for_each_symbol(syms
, nr_syms
, idx
, sym
) {
757 const char *elf_name
= elf_sym__name(&sym
, symstrs
);
759 if (strcmp(elf_name
, kmap
->ref_reloc_sym
->name
))
761 kmap
->ref_reloc_sym
->unrelocated_addr
= sym
.st_value
;
762 map
->reloc
= kmap
->ref_reloc_sym
->addr
-
763 kmap
->ref_reloc_sym
->unrelocated_addr
;
768 dso
->adjust_symbols
= runtime_ss
->adjust_symbols
|| ref_reloc(kmap
);
770 * Initial kernel and module mappings do not map to the dso. For
771 * function mappings, flag the fixups.
773 if (map
->type
== MAP__FUNCTION
&& (dso
->kernel
|| kmodule
)) {
775 adjust_kernel_syms
= dso
->adjust_symbols
;
777 elf_symtab__for_each_symbol(syms
, nr_syms
, idx
, sym
) {
779 const char *elf_name
= elf_sym__name(&sym
, symstrs
);
780 char *demangled
= NULL
;
781 int is_label
= elf_sym__is_label(&sym
);
782 const char *section_name
;
783 bool used_opd
= false;
785 if (!is_label
&& !elf_sym__is_a(&sym
, map
->type
))
788 /* Reject ARM ELF "mapping symbols": these aren't unique and
789 * don't identify functions, so will confuse the profile
791 if (ehdr
.e_machine
== EM_ARM
) {
792 if (!strcmp(elf_name
, "$a") ||
793 !strcmp(elf_name
, "$d") ||
794 !strcmp(elf_name
, "$t"))
798 if (runtime_ss
->opdsec
&& sym
.st_shndx
== runtime_ss
->opdidx
) {
799 u32 offset
= sym
.st_value
- syms_ss
->opdshdr
.sh_addr
;
800 u64
*opd
= opddata
->d_buf
+ offset
;
801 sym
.st_value
= DSO__SWAP(dso
, u64
, *opd
);
802 sym
.st_shndx
= elf_addr_to_index(runtime_ss
->elf
,
807 * When loading symbols in a data mapping, ABS symbols (which
808 * has a value of SHN_ABS in its st_shndx) failed at
809 * elf_getscn(). And it marks the loading as a failure so
810 * already loaded symbols cannot be fixed up.
812 * I'm not sure what should be done. Just ignore them for now.
815 if (sym
.st_shndx
== SHN_ABS
)
818 sec
= elf_getscn(runtime_ss
->elf
, sym
.st_shndx
);
822 gelf_getshdr(sec
, &shdr
);
824 if (is_label
&& !elf_sec__is_a(&shdr
, secstrs
, map
->type
))
827 section_name
= elf_sec__name(&shdr
, secstrs
);
829 /* On ARM, symbols for thumb functions have 1 added to
830 * the symbol address as a flag - remove it */
831 if ((ehdr
.e_machine
== EM_ARM
) &&
832 (map
->type
== MAP__FUNCTION
) &&
836 if (dso
->kernel
|| kmodule
) {
837 char dso_name
[PATH_MAX
];
839 /* Adjust symbol to map to file offset */
840 if (adjust_kernel_syms
)
841 sym
.st_value
-= shdr
.sh_addr
- shdr
.sh_offset
;
843 if (strcmp(section_name
,
844 (curr_dso
->short_name
+
845 dso
->short_name_len
)) == 0)
848 if (strcmp(section_name
, ".text") == 0) {
850 * The initial kernel mapping is based on
851 * kallsyms and identity maps. Overwrite it to
852 * map to the kernel dso.
854 if (remap_kernel
&& dso
->kernel
) {
855 remap_kernel
= false;
856 map
->start
= shdr
.sh_addr
+
858 map
->end
= map
->start
+ shdr
.sh_size
;
859 map
->pgoff
= shdr
.sh_offset
;
860 map
->map_ip
= map__map_ip
;
861 map
->unmap_ip
= map__unmap_ip
;
862 /* Ensure maps are correctly ordered */
863 map_groups__remove(kmap
->kmaps
, map
);
864 map_groups__insert(kmap
->kmaps
, map
);
868 * The initial module mapping is based on
869 * /proc/modules mapped to offset zero.
870 * Overwrite it to map to the module dso.
872 if (remap_kernel
&& kmodule
) {
873 remap_kernel
= false;
874 map
->pgoff
= shdr
.sh_offset
;
885 snprintf(dso_name
, sizeof(dso_name
),
886 "%s%s", dso
->short_name
, section_name
);
888 curr_map
= map_groups__find_by_name(kmap
->kmaps
, map
->type
, dso_name
);
889 if (curr_map
== NULL
) {
890 u64 start
= sym
.st_value
;
893 start
+= map
->start
+ shdr
.sh_offset
;
895 curr_dso
= dso__new(dso_name
);
896 if (curr_dso
== NULL
)
898 curr_dso
->kernel
= dso
->kernel
;
899 curr_dso
->long_name
= dso
->long_name
;
900 curr_dso
->long_name_len
= dso
->long_name_len
;
901 curr_map
= map__new2(start
, curr_dso
,
903 if (curr_map
== NULL
) {
904 dso__delete(curr_dso
);
907 if (adjust_kernel_syms
) {
908 curr_map
->start
= shdr
.sh_addr
+
910 curr_map
->end
= curr_map
->start
+
912 curr_map
->pgoff
= shdr
.sh_offset
;
914 curr_map
->map_ip
= identity__map_ip
;
915 curr_map
->unmap_ip
= identity__map_ip
;
917 curr_dso
->symtab_type
= dso
->symtab_type
;
918 map_groups__insert(kmap
->kmaps
, curr_map
);
919 dsos__add(&dso
->node
, curr_dso
);
920 dso__set_loaded(curr_dso
, map
->type
);
922 curr_dso
= curr_map
->dso
;
927 if ((used_opd
&& runtime_ss
->adjust_symbols
)
928 || (!used_opd
&& syms_ss
->adjust_symbols
)) {
929 pr_debug4("%s: adjusting symbol: st_value: %#" PRIx64
" "
930 "sh_addr: %#" PRIx64
" sh_offset: %#" PRIx64
"\n", __func__
,
931 (u64
)sym
.st_value
, (u64
)shdr
.sh_addr
,
932 (u64
)shdr
.sh_offset
);
933 sym
.st_value
-= shdr
.sh_addr
- shdr
.sh_offset
;
937 * We need to figure out if the object was created from C++ sources
938 * DWARF DW_compile_unit has this, but we don't always have access
941 if (symbol_conf
.demangle
) {
942 demangled
= bfd_demangle(NULL
, elf_name
,
943 DMGL_PARAMS
| DMGL_ANSI
);
944 if (demangled
!= NULL
)
945 elf_name
= demangled
;
947 f
= symbol__new(sym
.st_value
, sym
.st_size
,
948 GELF_ST_BIND(sym
.st_info
), elf_name
);
953 if (filter
&& filter(curr_map
, f
))
956 symbols__insert(&curr_dso
->symbols
[curr_map
->type
], f
);
962 * For misannotated, zeroed, ASM function sizes.
965 symbols__fixup_duplicate(&dso
->symbols
[map
->type
]);
966 symbols__fixup_end(&dso
->symbols
[map
->type
]);
969 * We need to fixup this here too because we create new
970 * maps here, for things like vsyscall sections.
972 __map_groups__fixup_end(kmap
->kmaps
, map
->type
);
980 static int elf_read_maps(Elf
*elf
, bool exe
, mapfn_t mapfn
, void *data
)
987 if (elf_getphdrnum(elf
, &phdrnum
))
990 for (i
= 0; i
< phdrnum
; i
++) {
991 if (gelf_getphdr(elf
, i
, &phdr
) == NULL
)
993 if (phdr
.p_type
!= PT_LOAD
)
996 if (!(phdr
.p_flags
& PF_X
))
999 if (!(phdr
.p_flags
& PF_R
))
1002 sz
= min(phdr
.p_memsz
, phdr
.p_filesz
);
1005 err
= mapfn(phdr
.p_vaddr
, sz
, phdr
.p_offset
, data
);
1012 int file__read_maps(int fd
, bool exe
, mapfn_t mapfn
, void *data
,
1018 elf
= elf_begin(fd
, PERF_ELF_C_READ_MMAP
, NULL
);
1023 *is_64_bit
= (gelf_getclass(elf
) == ELFCLASS64
);
1025 err
= elf_read_maps(elf
, exe
, mapfn
, data
);
1031 enum dso_type
dso__type_fd(int fd
)
1033 enum dso_type dso_type
= DSO__TYPE_UNKNOWN
;
1038 elf
= elf_begin(fd
, PERF_ELF_C_READ_MMAP
, NULL
);
1043 if (ek
!= ELF_K_ELF
)
1046 if (gelf_getclass(elf
) == ELFCLASS64
) {
1047 dso_type
= DSO__TYPE_64BIT
;
1051 if (gelf_getehdr(elf
, &ehdr
) == NULL
)
1054 if (ehdr
.e_machine
== EM_X86_64
)
1055 dso_type
= DSO__TYPE_X32BIT
;
1057 dso_type
= DSO__TYPE_32BIT
;
1064 static int copy_bytes(int from
, off_t from_offs
, int to
, off_t to_offs
, u64 len
)
1069 char *buf
= malloc(page_size
);
1074 if (lseek(to
, to_offs
, SEEK_SET
) != to_offs
)
1077 if (lseek(from
, from_offs
, SEEK_SET
) != from_offs
)
1084 /* Use read because mmap won't work on proc files */
1085 r
= read(from
, buf
, n
);
1091 r
= write(to
, buf
, n
);
1112 static int kcore__open(struct kcore
*kcore
, const char *filename
)
1116 kcore
->fd
= open(filename
, O_RDONLY
);
1117 if (kcore
->fd
== -1)
1120 kcore
->elf
= elf_begin(kcore
->fd
, ELF_C_READ
, NULL
);
1124 kcore
->elfclass
= gelf_getclass(kcore
->elf
);
1125 if (kcore
->elfclass
== ELFCLASSNONE
)
1128 ehdr
= gelf_getehdr(kcore
->elf
, &kcore
->ehdr
);
1135 elf_end(kcore
->elf
);
1141 static int kcore__init(struct kcore
*kcore
, char *filename
, int elfclass
,
1146 kcore
->elfclass
= elfclass
;
1149 kcore
->fd
= mkstemp(filename
);
1151 kcore
->fd
= open(filename
, O_WRONLY
| O_CREAT
| O_EXCL
, 0400);
1152 if (kcore
->fd
== -1)
1155 kcore
->elf
= elf_begin(kcore
->fd
, ELF_C_WRITE
, NULL
);
1159 if (!gelf_newehdr(kcore
->elf
, elfclass
))
1162 ehdr
= gelf_getehdr(kcore
->elf
, &kcore
->ehdr
);
1169 elf_end(kcore
->elf
);
1176 static void kcore__close(struct kcore
*kcore
)
1178 elf_end(kcore
->elf
);
1182 static int kcore__copy_hdr(struct kcore
*from
, struct kcore
*to
, size_t count
)
1184 GElf_Ehdr
*ehdr
= &to
->ehdr
;
1185 GElf_Ehdr
*kehdr
= &from
->ehdr
;
1187 memcpy(ehdr
->e_ident
, kehdr
->e_ident
, EI_NIDENT
);
1188 ehdr
->e_type
= kehdr
->e_type
;
1189 ehdr
->e_machine
= kehdr
->e_machine
;
1190 ehdr
->e_version
= kehdr
->e_version
;
1193 ehdr
->e_flags
= kehdr
->e_flags
;
1194 ehdr
->e_phnum
= count
;
1195 ehdr
->e_shentsize
= 0;
1197 ehdr
->e_shstrndx
= 0;
1199 if (from
->elfclass
== ELFCLASS32
) {
1200 ehdr
->e_phoff
= sizeof(Elf32_Ehdr
);
1201 ehdr
->e_ehsize
= sizeof(Elf32_Ehdr
);
1202 ehdr
->e_phentsize
= sizeof(Elf32_Phdr
);
1204 ehdr
->e_phoff
= sizeof(Elf64_Ehdr
);
1205 ehdr
->e_ehsize
= sizeof(Elf64_Ehdr
);
1206 ehdr
->e_phentsize
= sizeof(Elf64_Phdr
);
1209 if (!gelf_update_ehdr(to
->elf
, ehdr
))
1212 if (!gelf_newphdr(to
->elf
, count
))
1218 static int kcore__add_phdr(struct kcore
*kcore
, int idx
, off_t offset
,
1224 phdr
= gelf_getphdr(kcore
->elf
, idx
, &gphdr
);
1228 phdr
->p_type
= PT_LOAD
;
1229 phdr
->p_flags
= PF_R
| PF_W
| PF_X
;
1230 phdr
->p_offset
= offset
;
1231 phdr
->p_vaddr
= addr
;
1233 phdr
->p_filesz
= len
;
1234 phdr
->p_memsz
= len
;
1235 phdr
->p_align
= page_size
;
1237 if (!gelf_update_phdr(kcore
->elf
, idx
, phdr
))
1243 static off_t
kcore__write(struct kcore
*kcore
)
1245 return elf_update(kcore
->elf
, ELF_C_WRITE
);
1254 struct kcore_copy_info
{
1260 u64 last_module_symbol
;
1261 struct phdr_data kernel_map
;
1262 struct phdr_data modules_map
;
1265 static int kcore_copy__process_kallsyms(void *arg
, const char *name
, char type
,
1268 struct kcore_copy_info
*kci
= arg
;
1270 if (!symbol_type__is_a(type
, MAP__FUNCTION
))
1273 if (strchr(name
, '[')) {
1274 if (start
> kci
->last_module_symbol
)
1275 kci
->last_module_symbol
= start
;
1279 if (!kci
->first_symbol
|| start
< kci
->first_symbol
)
1280 kci
->first_symbol
= start
;
1282 if (!kci
->last_symbol
|| start
> kci
->last_symbol
)
1283 kci
->last_symbol
= start
;
1285 if (!strcmp(name
, "_stext")) {
1290 if (!strcmp(name
, "_etext")) {
1298 static int kcore_copy__parse_kallsyms(struct kcore_copy_info
*kci
,
1301 char kallsyms_filename
[PATH_MAX
];
1303 scnprintf(kallsyms_filename
, PATH_MAX
, "%s/kallsyms", dir
);
1305 if (symbol__restricted_filename(kallsyms_filename
, "/proc/kallsyms"))
1308 if (kallsyms__parse(kallsyms_filename
, kci
,
1309 kcore_copy__process_kallsyms
) < 0)
1315 static int kcore_copy__process_modules(void *arg
,
1316 const char *name __maybe_unused
,
1319 struct kcore_copy_info
*kci
= arg
;
1321 if (!kci
->first_module
|| start
< kci
->first_module
)
1322 kci
->first_module
= start
;
1327 static int kcore_copy__parse_modules(struct kcore_copy_info
*kci
,
1330 char modules_filename
[PATH_MAX
];
1332 scnprintf(modules_filename
, PATH_MAX
, "%s/modules", dir
);
1334 if (symbol__restricted_filename(modules_filename
, "/proc/modules"))
1337 if (modules__parse(modules_filename
, kci
,
1338 kcore_copy__process_modules
) < 0)
1344 static void kcore_copy__map(struct phdr_data
*p
, u64 start
, u64 end
, u64 pgoff
,
1347 if (p
->addr
|| s
< start
|| s
>= end
)
1351 p
->offset
= (s
- start
) + pgoff
;
1352 p
->len
= e
< end
? e
- s
: end
- s
;
1355 static int kcore_copy__read_map(u64 start
, u64 len
, u64 pgoff
, void *data
)
1357 struct kcore_copy_info
*kci
= data
;
1358 u64 end
= start
+ len
;
1360 kcore_copy__map(&kci
->kernel_map
, start
, end
, pgoff
, kci
->stext
,
1363 kcore_copy__map(&kci
->modules_map
, start
, end
, pgoff
, kci
->first_module
,
1364 kci
->last_module_symbol
);
1369 static int kcore_copy__read_maps(struct kcore_copy_info
*kci
, Elf
*elf
)
1371 if (elf_read_maps(elf
, true, kcore_copy__read_map
, kci
) < 0)
1377 static int kcore_copy__calc_maps(struct kcore_copy_info
*kci
, const char *dir
,
1380 if (kcore_copy__parse_kallsyms(kci
, dir
))
1383 if (kcore_copy__parse_modules(kci
, dir
))
1387 kci
->stext
= round_down(kci
->stext
, page_size
);
1389 kci
->stext
= round_down(kci
->first_symbol
, page_size
);
1392 kci
->etext
= round_up(kci
->etext
, page_size
);
1393 } else if (kci
->last_symbol
) {
1394 kci
->etext
= round_up(kci
->last_symbol
, page_size
);
1395 kci
->etext
+= page_size
;
1398 kci
->first_module
= round_down(kci
->first_module
, page_size
);
1400 if (kci
->last_module_symbol
) {
1401 kci
->last_module_symbol
= round_up(kci
->last_module_symbol
,
1403 kci
->last_module_symbol
+= page_size
;
1406 if (!kci
->stext
|| !kci
->etext
)
1409 if (kci
->first_module
&& !kci
->last_module_symbol
)
1412 return kcore_copy__read_maps(kci
, elf
);
1415 static int kcore_copy__copy_file(const char *from_dir
, const char *to_dir
,
1418 char from_filename
[PATH_MAX
];
1419 char to_filename
[PATH_MAX
];
1421 scnprintf(from_filename
, PATH_MAX
, "%s/%s", from_dir
, name
);
1422 scnprintf(to_filename
, PATH_MAX
, "%s/%s", to_dir
, name
);
1424 return copyfile_mode(from_filename
, to_filename
, 0400);
1427 static int kcore_copy__unlink(const char *dir
, const char *name
)
1429 char filename
[PATH_MAX
];
1431 scnprintf(filename
, PATH_MAX
, "%s/%s", dir
, name
);
1433 return unlink(filename
);
1436 static int kcore_copy__compare_fds(int from
, int to
)
1444 buf_from
= malloc(page_size
);
1445 buf_to
= malloc(page_size
);
1446 if (!buf_from
|| !buf_to
)
1450 /* Use read because mmap won't work on proc files */
1451 ret
= read(from
, buf_from
, page_size
);
1460 if (readn(to
, buf_to
, len
) != (int)len
)
1463 if (memcmp(buf_from
, buf_to
, len
))
1474 static int kcore_copy__compare_files(const char *from_filename
,
1475 const char *to_filename
)
1477 int from
, to
, err
= -1;
1479 from
= open(from_filename
, O_RDONLY
);
1483 to
= open(to_filename
, O_RDONLY
);
1485 goto out_close_from
;
1487 err
= kcore_copy__compare_fds(from
, to
);
1495 static int kcore_copy__compare_file(const char *from_dir
, const char *to_dir
,
1498 char from_filename
[PATH_MAX
];
1499 char to_filename
[PATH_MAX
];
1501 scnprintf(from_filename
, PATH_MAX
, "%s/%s", from_dir
, name
);
1502 scnprintf(to_filename
, PATH_MAX
, "%s/%s", to_dir
, name
);
1504 return kcore_copy__compare_files(from_filename
, to_filename
);
1508 * kcore_copy - copy kallsyms, modules and kcore from one directory to another.
1509 * @from_dir: from directory
1510 * @to_dir: to directory
1512 * This function copies kallsyms, modules and kcore files from one directory to
1513 * another. kallsyms and modules are copied entirely. Only code segments are
1514 * copied from kcore. It is assumed that two segments suffice: one for the
1515 * kernel proper and one for all the modules. The code segments are determined
1516 * from kallsyms and modules files. The kernel map starts at _stext or the
1517 * lowest function symbol, and ends at _etext or the highest function symbol.
1518 * The module map starts at the lowest module address and ends at the highest
1519 * module symbol. Start addresses are rounded down to the nearest page. End
1520 * addresses are rounded up to the nearest page. An extra page is added to the
1521 * highest kernel symbol and highest module symbol to, hopefully, encompass that
1522 * symbol too. Because it contains only code sections, the resulting kcore is
1523 * unusual. One significant peculiarity is that the mapping (start -> pgoff)
1524 * is not the same for the kernel map and the modules map. That happens because
1525 * the data is copied adjacently whereas the original kcore has gaps. Finally,
1526 * kallsyms and modules files are compared with their copies to check that
1527 * modules have not been loaded or unloaded while the copies were taking place.
1529 * Return: %0 on success, %-1 on failure.
1531 int kcore_copy(const char *from_dir
, const char *to_dir
)
1534 struct kcore extract
;
1536 int idx
= 0, err
= -1;
1537 off_t offset
= page_size
, sz
, modules_offset
= 0;
1538 struct kcore_copy_info kci
= { .stext
= 0, };
1539 char kcore_filename
[PATH_MAX
];
1540 char extract_filename
[PATH_MAX
];
1542 if (kcore_copy__copy_file(from_dir
, to_dir
, "kallsyms"))
1545 if (kcore_copy__copy_file(from_dir
, to_dir
, "modules"))
1546 goto out_unlink_kallsyms
;
1548 scnprintf(kcore_filename
, PATH_MAX
, "%s/kcore", from_dir
);
1549 scnprintf(extract_filename
, PATH_MAX
, "%s/kcore", to_dir
);
1551 if (kcore__open(&kcore
, kcore_filename
))
1552 goto out_unlink_modules
;
1554 if (kcore_copy__calc_maps(&kci
, from_dir
, kcore
.elf
))
1555 goto out_kcore_close
;
1557 if (kcore__init(&extract
, extract_filename
, kcore
.elfclass
, false))
1558 goto out_kcore_close
;
1560 if (!kci
.modules_map
.addr
)
1563 if (kcore__copy_hdr(&kcore
, &extract
, count
))
1564 goto out_extract_close
;
1566 if (kcore__add_phdr(&extract
, idx
++, offset
, kci
.kernel_map
.addr
,
1567 kci
.kernel_map
.len
))
1568 goto out_extract_close
;
1570 if (kci
.modules_map
.addr
) {
1571 modules_offset
= offset
+ kci
.kernel_map
.len
;
1572 if (kcore__add_phdr(&extract
, idx
, modules_offset
,
1573 kci
.modules_map
.addr
, kci
.modules_map
.len
))
1574 goto out_extract_close
;
1577 sz
= kcore__write(&extract
);
1578 if (sz
< 0 || sz
> offset
)
1579 goto out_extract_close
;
1581 if (copy_bytes(kcore
.fd
, kci
.kernel_map
.offset
, extract
.fd
, offset
,
1582 kci
.kernel_map
.len
))
1583 goto out_extract_close
;
1585 if (modules_offset
&& copy_bytes(kcore
.fd
, kci
.modules_map
.offset
,
1586 extract
.fd
, modules_offset
,
1587 kci
.modules_map
.len
))
1588 goto out_extract_close
;
1590 if (kcore_copy__compare_file(from_dir
, to_dir
, "modules"))
1591 goto out_extract_close
;
1593 if (kcore_copy__compare_file(from_dir
, to_dir
, "kallsyms"))
1594 goto out_extract_close
;
1599 kcore__close(&extract
);
1601 unlink(extract_filename
);
1603 kcore__close(&kcore
);
1606 kcore_copy__unlink(to_dir
, "modules");
1607 out_unlink_kallsyms
:
1609 kcore_copy__unlink(to_dir
, "kallsyms");
1614 int kcore_extract__create(struct kcore_extract
*kce
)
1617 struct kcore extract
;
1619 int idx
= 0, err
= -1;
1620 off_t offset
= page_size
, sz
;
1622 if (kcore__open(&kcore
, kce
->kcore_filename
))
1625 strcpy(kce
->extract_filename
, PERF_KCORE_EXTRACT
);
1626 if (kcore__init(&extract
, kce
->extract_filename
, kcore
.elfclass
, true))
1627 goto out_kcore_close
;
1629 if (kcore__copy_hdr(&kcore
, &extract
, count
))
1630 goto out_extract_close
;
1632 if (kcore__add_phdr(&extract
, idx
, offset
, kce
->addr
, kce
->len
))
1633 goto out_extract_close
;
1635 sz
= kcore__write(&extract
);
1636 if (sz
< 0 || sz
> offset
)
1637 goto out_extract_close
;
1639 if (copy_bytes(kcore
.fd
, kce
->offs
, extract
.fd
, offset
, kce
->len
))
1640 goto out_extract_close
;
1645 kcore__close(&extract
);
1647 unlink(kce
->extract_filename
);
1649 kcore__close(&kcore
);
1654 void kcore_extract__delete(struct kcore_extract
*kce
)
1656 unlink(kce
->extract_filename
);
1659 void symbol__elf_init(void)
1661 elf_version(EV_CURRENT
);