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 static bool want_demangle(bool is_kernel_sym
)
685 return is_kernel_sym
? symbol_conf
.demangle_kernel
: symbol_conf
.demangle
;
688 int dso__load_sym(struct dso
*dso
, struct map
*map
,
689 struct symsrc
*syms_ss
, struct symsrc
*runtime_ss
,
690 symbol_filter_t filter
, int kmodule
)
692 struct kmap
*kmap
= dso
->kernel
? map__kmap(map
) : NULL
;
693 struct map
*curr_map
= map
;
694 struct dso
*curr_dso
= dso
;
695 Elf_Data
*symstrs
, *secstrs
;
701 Elf_Data
*syms
, *opddata
= NULL
;
703 Elf_Scn
*sec
, *sec_strndx
;
706 bool remap_kernel
= false, adjust_kernel_syms
= false;
708 dso
->symtab_type
= syms_ss
->type
;
709 dso
->is_64_bit
= syms_ss
->is_64_bit
;
710 dso
->rel
= syms_ss
->ehdr
.e_type
== ET_REL
;
713 * Modules may already have symbols from kallsyms, but those symbols
714 * have the wrong values for the dso maps, so remove them.
716 if (kmodule
&& syms_ss
->symtab
)
717 symbols__delete(&dso
->symbols
[map
->type
]);
719 if (!syms_ss
->symtab
) {
721 * If the vmlinux is stripped, fail so we will fall back
722 * to using kallsyms. The vmlinux runtime symbols aren't
728 syms_ss
->symtab
= syms_ss
->dynsym
;
729 syms_ss
->symshdr
= syms_ss
->dynshdr
;
733 ehdr
= syms_ss
->ehdr
;
734 sec
= syms_ss
->symtab
;
735 shdr
= syms_ss
->symshdr
;
737 if (runtime_ss
->opdsec
)
738 opddata
= elf_rawdata(runtime_ss
->opdsec
, NULL
);
740 syms
= elf_getdata(sec
, NULL
);
744 sec
= elf_getscn(elf
, shdr
.sh_link
);
748 symstrs
= elf_getdata(sec
, NULL
);
752 sec_strndx
= elf_getscn(runtime_ss
->elf
, runtime_ss
->ehdr
.e_shstrndx
);
753 if (sec_strndx
== NULL
)
756 secstrs
= elf_getdata(sec_strndx
, NULL
);
760 nr_syms
= shdr
.sh_size
/ shdr
.sh_entsize
;
762 memset(&sym
, 0, sizeof(sym
));
765 * The kernel relocation symbol is needed in advance in order to adjust
766 * kernel maps correctly.
768 if (ref_reloc_sym_not_found(kmap
)) {
769 elf_symtab__for_each_symbol(syms
, nr_syms
, idx
, sym
) {
770 const char *elf_name
= elf_sym__name(&sym
, symstrs
);
772 if (strcmp(elf_name
, kmap
->ref_reloc_sym
->name
))
774 kmap
->ref_reloc_sym
->unrelocated_addr
= sym
.st_value
;
775 map
->reloc
= kmap
->ref_reloc_sym
->addr
-
776 kmap
->ref_reloc_sym
->unrelocated_addr
;
781 dso
->adjust_symbols
= runtime_ss
->adjust_symbols
|| ref_reloc(kmap
);
783 * Initial kernel and module mappings do not map to the dso. For
784 * function mappings, flag the fixups.
786 if (map
->type
== MAP__FUNCTION
&& (dso
->kernel
|| kmodule
)) {
788 adjust_kernel_syms
= dso
->adjust_symbols
;
790 elf_symtab__for_each_symbol(syms
, nr_syms
, idx
, sym
) {
792 const char *elf_name
= elf_sym__name(&sym
, symstrs
);
793 char *demangled
= NULL
;
794 int is_label
= elf_sym__is_label(&sym
);
795 const char *section_name
;
796 bool used_opd
= false;
798 if (!is_label
&& !elf_sym__is_a(&sym
, map
->type
))
801 /* Reject ARM ELF "mapping symbols": these aren't unique and
802 * don't identify functions, so will confuse the profile
804 if (ehdr
.e_machine
== EM_ARM
) {
805 if (!strcmp(elf_name
, "$a") ||
806 !strcmp(elf_name
, "$d") ||
807 !strcmp(elf_name
, "$t"))
811 if (runtime_ss
->opdsec
&& sym
.st_shndx
== runtime_ss
->opdidx
) {
812 u32 offset
= sym
.st_value
- syms_ss
->opdshdr
.sh_addr
;
813 u64
*opd
= opddata
->d_buf
+ offset
;
814 sym
.st_value
= DSO__SWAP(dso
, u64
, *opd
);
815 sym
.st_shndx
= elf_addr_to_index(runtime_ss
->elf
,
820 * When loading symbols in a data mapping, ABS symbols (which
821 * has a value of SHN_ABS in its st_shndx) failed at
822 * elf_getscn(). And it marks the loading as a failure so
823 * already loaded symbols cannot be fixed up.
825 * I'm not sure what should be done. Just ignore them for now.
828 if (sym
.st_shndx
== SHN_ABS
)
831 sec
= elf_getscn(runtime_ss
->elf
, sym
.st_shndx
);
835 gelf_getshdr(sec
, &shdr
);
837 if (is_label
&& !elf_sec__is_a(&shdr
, secstrs
, map
->type
))
840 section_name
= elf_sec__name(&shdr
, secstrs
);
842 /* On ARM, symbols for thumb functions have 1 added to
843 * the symbol address as a flag - remove it */
844 if ((ehdr
.e_machine
== EM_ARM
) &&
845 (map
->type
== MAP__FUNCTION
) &&
849 if (dso
->kernel
|| kmodule
) {
850 char dso_name
[PATH_MAX
];
852 /* Adjust symbol to map to file offset */
853 if (adjust_kernel_syms
)
854 sym
.st_value
-= shdr
.sh_addr
- shdr
.sh_offset
;
856 if (strcmp(section_name
,
857 (curr_dso
->short_name
+
858 dso
->short_name_len
)) == 0)
861 if (strcmp(section_name
, ".text") == 0) {
863 * The initial kernel mapping is based on
864 * kallsyms and identity maps. Overwrite it to
865 * map to the kernel dso.
867 if (remap_kernel
&& dso
->kernel
) {
868 remap_kernel
= false;
869 map
->start
= shdr
.sh_addr
+
871 map
->end
= map
->start
+ shdr
.sh_size
;
872 map
->pgoff
= shdr
.sh_offset
;
873 map
->map_ip
= map__map_ip
;
874 map
->unmap_ip
= map__unmap_ip
;
875 /* Ensure maps are correctly ordered */
876 map_groups__remove(kmap
->kmaps
, map
);
877 map_groups__insert(kmap
->kmaps
, map
);
881 * The initial module mapping is based on
882 * /proc/modules mapped to offset zero.
883 * Overwrite it to map to the module dso.
885 if (remap_kernel
&& kmodule
) {
886 remap_kernel
= false;
887 map
->pgoff
= shdr
.sh_offset
;
898 snprintf(dso_name
, sizeof(dso_name
),
899 "%s%s", dso
->short_name
, section_name
);
901 curr_map
= map_groups__find_by_name(kmap
->kmaps
, map
->type
, dso_name
);
902 if (curr_map
== NULL
) {
903 u64 start
= sym
.st_value
;
906 start
+= map
->start
+ shdr
.sh_offset
;
908 curr_dso
= dso__new(dso_name
);
909 if (curr_dso
== NULL
)
911 curr_dso
->kernel
= dso
->kernel
;
912 curr_dso
->long_name
= dso
->long_name
;
913 curr_dso
->long_name_len
= dso
->long_name_len
;
914 curr_map
= map__new2(start
, curr_dso
,
916 if (curr_map
== NULL
) {
917 dso__delete(curr_dso
);
920 if (adjust_kernel_syms
) {
921 curr_map
->start
= shdr
.sh_addr
+
923 curr_map
->end
= curr_map
->start
+
925 curr_map
->pgoff
= shdr
.sh_offset
;
927 curr_map
->map_ip
= identity__map_ip
;
928 curr_map
->unmap_ip
= identity__map_ip
;
930 curr_dso
->symtab_type
= dso
->symtab_type
;
931 map_groups__insert(kmap
->kmaps
, curr_map
);
932 dsos__add(&dso
->node
, curr_dso
);
933 dso__set_loaded(curr_dso
, map
->type
);
935 curr_dso
= curr_map
->dso
;
940 if ((used_opd
&& runtime_ss
->adjust_symbols
)
941 || (!used_opd
&& syms_ss
->adjust_symbols
)) {
942 pr_debug4("%s: adjusting symbol: st_value: %#" PRIx64
" "
943 "sh_addr: %#" PRIx64
" sh_offset: %#" PRIx64
"\n", __func__
,
944 (u64
)sym
.st_value
, (u64
)shdr
.sh_addr
,
945 (u64
)shdr
.sh_offset
);
946 sym
.st_value
-= shdr
.sh_addr
- shdr
.sh_offset
;
950 * We need to figure out if the object was created from C++ sources
951 * DWARF DW_compile_unit has this, but we don't always have access
954 if (want_demangle(dso
->kernel
|| kmodule
)) {
955 int demangle_flags
= DMGL_NO_OPTS
;
957 demangle_flags
= DMGL_PARAMS
| DMGL_ANSI
;
959 demangled
= bfd_demangle(NULL
, elf_name
, demangle_flags
);
960 if (demangled
!= NULL
)
961 elf_name
= demangled
;
963 f
= symbol__new(sym
.st_value
, sym
.st_size
,
964 GELF_ST_BIND(sym
.st_info
), elf_name
);
969 if (filter
&& filter(curr_map
, f
))
972 symbols__insert(&curr_dso
->symbols
[curr_map
->type
], f
);
978 * For misannotated, zeroed, ASM function sizes.
981 symbols__fixup_duplicate(&dso
->symbols
[map
->type
]);
982 symbols__fixup_end(&dso
->symbols
[map
->type
]);
985 * We need to fixup this here too because we create new
986 * maps here, for things like vsyscall sections.
988 __map_groups__fixup_end(kmap
->kmaps
, map
->type
);
996 static int elf_read_maps(Elf
*elf
, bool exe
, mapfn_t mapfn
, void *data
)
1003 if (elf_getphdrnum(elf
, &phdrnum
))
1006 for (i
= 0; i
< phdrnum
; i
++) {
1007 if (gelf_getphdr(elf
, i
, &phdr
) == NULL
)
1009 if (phdr
.p_type
!= PT_LOAD
)
1012 if (!(phdr
.p_flags
& PF_X
))
1015 if (!(phdr
.p_flags
& PF_R
))
1018 sz
= min(phdr
.p_memsz
, phdr
.p_filesz
);
1021 err
= mapfn(phdr
.p_vaddr
, sz
, phdr
.p_offset
, data
);
1028 int file__read_maps(int fd
, bool exe
, mapfn_t mapfn
, void *data
,
1034 elf
= elf_begin(fd
, PERF_ELF_C_READ_MMAP
, NULL
);
1039 *is_64_bit
= (gelf_getclass(elf
) == ELFCLASS64
);
1041 err
= elf_read_maps(elf
, exe
, mapfn
, data
);
1047 enum dso_type
dso__type_fd(int fd
)
1049 enum dso_type dso_type
= DSO__TYPE_UNKNOWN
;
1054 elf
= elf_begin(fd
, PERF_ELF_C_READ_MMAP
, NULL
);
1059 if (ek
!= ELF_K_ELF
)
1062 if (gelf_getclass(elf
) == ELFCLASS64
) {
1063 dso_type
= DSO__TYPE_64BIT
;
1067 if (gelf_getehdr(elf
, &ehdr
) == NULL
)
1070 if (ehdr
.e_machine
== EM_X86_64
)
1071 dso_type
= DSO__TYPE_X32BIT
;
1073 dso_type
= DSO__TYPE_32BIT
;
1080 static int copy_bytes(int from
, off_t from_offs
, int to
, off_t to_offs
, u64 len
)
1085 char *buf
= malloc(page_size
);
1090 if (lseek(to
, to_offs
, SEEK_SET
) != to_offs
)
1093 if (lseek(from
, from_offs
, SEEK_SET
) != from_offs
)
1100 /* Use read because mmap won't work on proc files */
1101 r
= read(from
, buf
, n
);
1107 r
= write(to
, buf
, n
);
1128 static int kcore__open(struct kcore
*kcore
, const char *filename
)
1132 kcore
->fd
= open(filename
, O_RDONLY
);
1133 if (kcore
->fd
== -1)
1136 kcore
->elf
= elf_begin(kcore
->fd
, ELF_C_READ
, NULL
);
1140 kcore
->elfclass
= gelf_getclass(kcore
->elf
);
1141 if (kcore
->elfclass
== ELFCLASSNONE
)
1144 ehdr
= gelf_getehdr(kcore
->elf
, &kcore
->ehdr
);
1151 elf_end(kcore
->elf
);
1157 static int kcore__init(struct kcore
*kcore
, char *filename
, int elfclass
,
1162 kcore
->elfclass
= elfclass
;
1165 kcore
->fd
= mkstemp(filename
);
1167 kcore
->fd
= open(filename
, O_WRONLY
| O_CREAT
| O_EXCL
, 0400);
1168 if (kcore
->fd
== -1)
1171 kcore
->elf
= elf_begin(kcore
->fd
, ELF_C_WRITE
, NULL
);
1175 if (!gelf_newehdr(kcore
->elf
, elfclass
))
1178 ehdr
= gelf_getehdr(kcore
->elf
, &kcore
->ehdr
);
1185 elf_end(kcore
->elf
);
1192 static void kcore__close(struct kcore
*kcore
)
1194 elf_end(kcore
->elf
);
1198 static int kcore__copy_hdr(struct kcore
*from
, struct kcore
*to
, size_t count
)
1200 GElf_Ehdr
*ehdr
= &to
->ehdr
;
1201 GElf_Ehdr
*kehdr
= &from
->ehdr
;
1203 memcpy(ehdr
->e_ident
, kehdr
->e_ident
, EI_NIDENT
);
1204 ehdr
->e_type
= kehdr
->e_type
;
1205 ehdr
->e_machine
= kehdr
->e_machine
;
1206 ehdr
->e_version
= kehdr
->e_version
;
1209 ehdr
->e_flags
= kehdr
->e_flags
;
1210 ehdr
->e_phnum
= count
;
1211 ehdr
->e_shentsize
= 0;
1213 ehdr
->e_shstrndx
= 0;
1215 if (from
->elfclass
== ELFCLASS32
) {
1216 ehdr
->e_phoff
= sizeof(Elf32_Ehdr
);
1217 ehdr
->e_ehsize
= sizeof(Elf32_Ehdr
);
1218 ehdr
->e_phentsize
= sizeof(Elf32_Phdr
);
1220 ehdr
->e_phoff
= sizeof(Elf64_Ehdr
);
1221 ehdr
->e_ehsize
= sizeof(Elf64_Ehdr
);
1222 ehdr
->e_phentsize
= sizeof(Elf64_Phdr
);
1225 if (!gelf_update_ehdr(to
->elf
, ehdr
))
1228 if (!gelf_newphdr(to
->elf
, count
))
1234 static int kcore__add_phdr(struct kcore
*kcore
, int idx
, off_t offset
,
1240 phdr
= gelf_getphdr(kcore
->elf
, idx
, &gphdr
);
1244 phdr
->p_type
= PT_LOAD
;
1245 phdr
->p_flags
= PF_R
| PF_W
| PF_X
;
1246 phdr
->p_offset
= offset
;
1247 phdr
->p_vaddr
= addr
;
1249 phdr
->p_filesz
= len
;
1250 phdr
->p_memsz
= len
;
1251 phdr
->p_align
= page_size
;
1253 if (!gelf_update_phdr(kcore
->elf
, idx
, phdr
))
1259 static off_t
kcore__write(struct kcore
*kcore
)
1261 return elf_update(kcore
->elf
, ELF_C_WRITE
);
1270 struct kcore_copy_info
{
1276 u64 last_module_symbol
;
1277 struct phdr_data kernel_map
;
1278 struct phdr_data modules_map
;
1281 static int kcore_copy__process_kallsyms(void *arg
, const char *name
, char type
,
1284 struct kcore_copy_info
*kci
= arg
;
1286 if (!symbol_type__is_a(type
, MAP__FUNCTION
))
1289 if (strchr(name
, '[')) {
1290 if (start
> kci
->last_module_symbol
)
1291 kci
->last_module_symbol
= start
;
1295 if (!kci
->first_symbol
|| start
< kci
->first_symbol
)
1296 kci
->first_symbol
= start
;
1298 if (!kci
->last_symbol
|| start
> kci
->last_symbol
)
1299 kci
->last_symbol
= start
;
1301 if (!strcmp(name
, "_stext")) {
1306 if (!strcmp(name
, "_etext")) {
1314 static int kcore_copy__parse_kallsyms(struct kcore_copy_info
*kci
,
1317 char kallsyms_filename
[PATH_MAX
];
1319 scnprintf(kallsyms_filename
, PATH_MAX
, "%s/kallsyms", dir
);
1321 if (symbol__restricted_filename(kallsyms_filename
, "/proc/kallsyms"))
1324 if (kallsyms__parse(kallsyms_filename
, kci
,
1325 kcore_copy__process_kallsyms
) < 0)
1331 static int kcore_copy__process_modules(void *arg
,
1332 const char *name __maybe_unused
,
1335 struct kcore_copy_info
*kci
= arg
;
1337 if (!kci
->first_module
|| start
< kci
->first_module
)
1338 kci
->first_module
= start
;
1343 static int kcore_copy__parse_modules(struct kcore_copy_info
*kci
,
1346 char modules_filename
[PATH_MAX
];
1348 scnprintf(modules_filename
, PATH_MAX
, "%s/modules", dir
);
1350 if (symbol__restricted_filename(modules_filename
, "/proc/modules"))
1353 if (modules__parse(modules_filename
, kci
,
1354 kcore_copy__process_modules
) < 0)
1360 static void kcore_copy__map(struct phdr_data
*p
, u64 start
, u64 end
, u64 pgoff
,
1363 if (p
->addr
|| s
< start
|| s
>= end
)
1367 p
->offset
= (s
- start
) + pgoff
;
1368 p
->len
= e
< end
? e
- s
: end
- s
;
1371 static int kcore_copy__read_map(u64 start
, u64 len
, u64 pgoff
, void *data
)
1373 struct kcore_copy_info
*kci
= data
;
1374 u64 end
= start
+ len
;
1376 kcore_copy__map(&kci
->kernel_map
, start
, end
, pgoff
, kci
->stext
,
1379 kcore_copy__map(&kci
->modules_map
, start
, end
, pgoff
, kci
->first_module
,
1380 kci
->last_module_symbol
);
1385 static int kcore_copy__read_maps(struct kcore_copy_info
*kci
, Elf
*elf
)
1387 if (elf_read_maps(elf
, true, kcore_copy__read_map
, kci
) < 0)
1393 static int kcore_copy__calc_maps(struct kcore_copy_info
*kci
, const char *dir
,
1396 if (kcore_copy__parse_kallsyms(kci
, dir
))
1399 if (kcore_copy__parse_modules(kci
, dir
))
1403 kci
->stext
= round_down(kci
->stext
, page_size
);
1405 kci
->stext
= round_down(kci
->first_symbol
, page_size
);
1408 kci
->etext
= round_up(kci
->etext
, page_size
);
1409 } else if (kci
->last_symbol
) {
1410 kci
->etext
= round_up(kci
->last_symbol
, page_size
);
1411 kci
->etext
+= page_size
;
1414 kci
->first_module
= round_down(kci
->first_module
, page_size
);
1416 if (kci
->last_module_symbol
) {
1417 kci
->last_module_symbol
= round_up(kci
->last_module_symbol
,
1419 kci
->last_module_symbol
+= page_size
;
1422 if (!kci
->stext
|| !kci
->etext
)
1425 if (kci
->first_module
&& !kci
->last_module_symbol
)
1428 return kcore_copy__read_maps(kci
, elf
);
1431 static int kcore_copy__copy_file(const char *from_dir
, const char *to_dir
,
1434 char from_filename
[PATH_MAX
];
1435 char to_filename
[PATH_MAX
];
1437 scnprintf(from_filename
, PATH_MAX
, "%s/%s", from_dir
, name
);
1438 scnprintf(to_filename
, PATH_MAX
, "%s/%s", to_dir
, name
);
1440 return copyfile_mode(from_filename
, to_filename
, 0400);
1443 static int kcore_copy__unlink(const char *dir
, const char *name
)
1445 char filename
[PATH_MAX
];
1447 scnprintf(filename
, PATH_MAX
, "%s/%s", dir
, name
);
1449 return unlink(filename
);
1452 static int kcore_copy__compare_fds(int from
, int to
)
1460 buf_from
= malloc(page_size
);
1461 buf_to
= malloc(page_size
);
1462 if (!buf_from
|| !buf_to
)
1466 /* Use read because mmap won't work on proc files */
1467 ret
= read(from
, buf_from
, page_size
);
1476 if (readn(to
, buf_to
, len
) != (int)len
)
1479 if (memcmp(buf_from
, buf_to
, len
))
1490 static int kcore_copy__compare_files(const char *from_filename
,
1491 const char *to_filename
)
1493 int from
, to
, err
= -1;
1495 from
= open(from_filename
, O_RDONLY
);
1499 to
= open(to_filename
, O_RDONLY
);
1501 goto out_close_from
;
1503 err
= kcore_copy__compare_fds(from
, to
);
1511 static int kcore_copy__compare_file(const char *from_dir
, const char *to_dir
,
1514 char from_filename
[PATH_MAX
];
1515 char to_filename
[PATH_MAX
];
1517 scnprintf(from_filename
, PATH_MAX
, "%s/%s", from_dir
, name
);
1518 scnprintf(to_filename
, PATH_MAX
, "%s/%s", to_dir
, name
);
1520 return kcore_copy__compare_files(from_filename
, to_filename
);
1524 * kcore_copy - copy kallsyms, modules and kcore from one directory to another.
1525 * @from_dir: from directory
1526 * @to_dir: to directory
1528 * This function copies kallsyms, modules and kcore files from one directory to
1529 * another. kallsyms and modules are copied entirely. Only code segments are
1530 * copied from kcore. It is assumed that two segments suffice: one for the
1531 * kernel proper and one for all the modules. The code segments are determined
1532 * from kallsyms and modules files. The kernel map starts at _stext or the
1533 * lowest function symbol, and ends at _etext or the highest function symbol.
1534 * The module map starts at the lowest module address and ends at the highest
1535 * module symbol. Start addresses are rounded down to the nearest page. End
1536 * addresses are rounded up to the nearest page. An extra page is added to the
1537 * highest kernel symbol and highest module symbol to, hopefully, encompass that
1538 * symbol too. Because it contains only code sections, the resulting kcore is
1539 * unusual. One significant peculiarity is that the mapping (start -> pgoff)
1540 * is not the same for the kernel map and the modules map. That happens because
1541 * the data is copied adjacently whereas the original kcore has gaps. Finally,
1542 * kallsyms and modules files are compared with their copies to check that
1543 * modules have not been loaded or unloaded while the copies were taking place.
1545 * Return: %0 on success, %-1 on failure.
1547 int kcore_copy(const char *from_dir
, const char *to_dir
)
1550 struct kcore extract
;
1552 int idx
= 0, err
= -1;
1553 off_t offset
= page_size
, sz
, modules_offset
= 0;
1554 struct kcore_copy_info kci
= { .stext
= 0, };
1555 char kcore_filename
[PATH_MAX
];
1556 char extract_filename
[PATH_MAX
];
1558 if (kcore_copy__copy_file(from_dir
, to_dir
, "kallsyms"))
1561 if (kcore_copy__copy_file(from_dir
, to_dir
, "modules"))
1562 goto out_unlink_kallsyms
;
1564 scnprintf(kcore_filename
, PATH_MAX
, "%s/kcore", from_dir
);
1565 scnprintf(extract_filename
, PATH_MAX
, "%s/kcore", to_dir
);
1567 if (kcore__open(&kcore
, kcore_filename
))
1568 goto out_unlink_modules
;
1570 if (kcore_copy__calc_maps(&kci
, from_dir
, kcore
.elf
))
1571 goto out_kcore_close
;
1573 if (kcore__init(&extract
, extract_filename
, kcore
.elfclass
, false))
1574 goto out_kcore_close
;
1576 if (!kci
.modules_map
.addr
)
1579 if (kcore__copy_hdr(&kcore
, &extract
, count
))
1580 goto out_extract_close
;
1582 if (kcore__add_phdr(&extract
, idx
++, offset
, kci
.kernel_map
.addr
,
1583 kci
.kernel_map
.len
))
1584 goto out_extract_close
;
1586 if (kci
.modules_map
.addr
) {
1587 modules_offset
= offset
+ kci
.kernel_map
.len
;
1588 if (kcore__add_phdr(&extract
, idx
, modules_offset
,
1589 kci
.modules_map
.addr
, kci
.modules_map
.len
))
1590 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
, kci
.kernel_map
.offset
, extract
.fd
, offset
,
1598 kci
.kernel_map
.len
))
1599 goto out_extract_close
;
1601 if (modules_offset
&& copy_bytes(kcore
.fd
, kci
.modules_map
.offset
,
1602 extract
.fd
, modules_offset
,
1603 kci
.modules_map
.len
))
1604 goto out_extract_close
;
1606 if (kcore_copy__compare_file(from_dir
, to_dir
, "modules"))
1607 goto out_extract_close
;
1609 if (kcore_copy__compare_file(from_dir
, to_dir
, "kallsyms"))
1610 goto out_extract_close
;
1615 kcore__close(&extract
);
1617 unlink(extract_filename
);
1619 kcore__close(&kcore
);
1622 kcore_copy__unlink(to_dir
, "modules");
1623 out_unlink_kallsyms
:
1625 kcore_copy__unlink(to_dir
, "kallsyms");
1630 int kcore_extract__create(struct kcore_extract
*kce
)
1633 struct kcore extract
;
1635 int idx
= 0, err
= -1;
1636 off_t offset
= page_size
, sz
;
1638 if (kcore__open(&kcore
, kce
->kcore_filename
))
1641 strcpy(kce
->extract_filename
, PERF_KCORE_EXTRACT
);
1642 if (kcore__init(&extract
, kce
->extract_filename
, kcore
.elfclass
, true))
1643 goto out_kcore_close
;
1645 if (kcore__copy_hdr(&kcore
, &extract
, count
))
1646 goto out_extract_close
;
1648 if (kcore__add_phdr(&extract
, idx
, offset
, kce
->addr
, kce
->len
))
1649 goto out_extract_close
;
1651 sz
= kcore__write(&extract
);
1652 if (sz
< 0 || sz
> offset
)
1653 goto out_extract_close
;
1655 if (copy_bytes(kcore
.fd
, kce
->offs
, extract
.fd
, offset
, kce
->len
))
1656 goto out_extract_close
;
1661 kcore__close(&extract
);
1663 unlink(kce
->extract_filename
);
1665 kcore__close(&kcore
);
1670 void kcore_extract__delete(struct kcore_extract
*kce
)
1672 unlink(kce
->extract_filename
);
1675 void symbol__elf_init(void)
1677 elf_version(EV_CURRENT
);