Merge tag 'fixes-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/rusty...
[linux/fpc-iii.git] / tools / perf / util / symbol-elf.c
blob06fcd1bf98b6034e39ef9af8dd0b4111d9208535
1 #include <fcntl.h>
2 #include <stdio.h>
3 #include <errno.h>
4 #include <string.h>
5 #include <unistd.h>
6 #include <inttypes.h>
8 #include "symbol.h"
9 #include "machine.h"
10 #include "vdso.h"
11 #include <symbol/kallsyms.h>
12 #include "debug.h"
14 #ifdef HAVE_CPLUS_DEMANGLE_SUPPORT
15 extern char *cplus_demangle(const char *, int);
17 static inline char *bfd_demangle(void __maybe_unused *v, const char *c, int i)
19 return cplus_demangle(c, i);
21 #else
22 #ifdef NO_DEMANGLE
23 static inline char *bfd_demangle(void __maybe_unused *v,
24 const char __maybe_unused *c,
25 int __maybe_unused i)
27 return NULL;
29 #else
30 #define PACKAGE 'perf'
31 #include <bfd.h>
32 #endif
33 #endif
35 #ifndef HAVE_ELF_GETPHDRNUM_SUPPORT
36 static int elf_getphdrnum(Elf *elf, size_t *dst)
38 GElf_Ehdr gehdr;
39 GElf_Ehdr *ehdr;
41 ehdr = gelf_getehdr(elf, &gehdr);
42 if (!ehdr)
43 return -1;
45 *dst = ehdr->e_phnum;
47 return 0;
49 #endif
51 #ifndef NT_GNU_BUILD_ID
52 #define NT_GNU_BUILD_ID 3
53 #endif
55 /**
56 * elf_symtab__for_each_symbol - iterate thru all the symbols
58 * @syms: struct elf_symtab instance to iterate
59 * @idx: uint32_t idx
60 * @sym: GElf_Sym iterator
62 #define elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) \
63 for (idx = 0, gelf_getsym(syms, idx, &sym);\
64 idx < nr_syms; \
65 idx++, gelf_getsym(syms, idx, &sym))
67 static inline uint8_t elf_sym__type(const GElf_Sym *sym)
69 return GELF_ST_TYPE(sym->st_info);
72 static inline int elf_sym__is_function(const GElf_Sym *sym)
74 return (elf_sym__type(sym) == STT_FUNC ||
75 elf_sym__type(sym) == STT_GNU_IFUNC) &&
76 sym->st_name != 0 &&
77 sym->st_shndx != SHN_UNDEF;
80 static inline bool elf_sym__is_object(const GElf_Sym *sym)
82 return elf_sym__type(sym) == STT_OBJECT &&
83 sym->st_name != 0 &&
84 sym->st_shndx != SHN_UNDEF;
87 static inline int elf_sym__is_label(const GElf_Sym *sym)
89 return elf_sym__type(sym) == STT_NOTYPE &&
90 sym->st_name != 0 &&
91 sym->st_shndx != SHN_UNDEF &&
92 sym->st_shndx != SHN_ABS;
95 static bool elf_sym__is_a(GElf_Sym *sym, enum map_type type)
97 switch (type) {
98 case MAP__FUNCTION:
99 return elf_sym__is_function(sym);
100 case MAP__VARIABLE:
101 return elf_sym__is_object(sym);
102 default:
103 return false;
107 static inline const char *elf_sym__name(const GElf_Sym *sym,
108 const Elf_Data *symstrs)
110 return symstrs->d_buf + sym->st_name;
113 static inline const char *elf_sec__name(const GElf_Shdr *shdr,
114 const Elf_Data *secstrs)
116 return secstrs->d_buf + shdr->sh_name;
119 static inline int elf_sec__is_text(const GElf_Shdr *shdr,
120 const Elf_Data *secstrs)
122 return strstr(elf_sec__name(shdr, secstrs), "text") != NULL;
125 static inline bool elf_sec__is_data(const GElf_Shdr *shdr,
126 const Elf_Data *secstrs)
128 return strstr(elf_sec__name(shdr, secstrs), "data") != NULL;
131 static bool elf_sec__is_a(GElf_Shdr *shdr, Elf_Data *secstrs,
132 enum map_type type)
134 switch (type) {
135 case MAP__FUNCTION:
136 return elf_sec__is_text(shdr, secstrs);
137 case MAP__VARIABLE:
138 return elf_sec__is_data(shdr, secstrs);
139 default:
140 return false;
144 static size_t elf_addr_to_index(Elf *elf, GElf_Addr addr)
146 Elf_Scn *sec = NULL;
147 GElf_Shdr shdr;
148 size_t cnt = 1;
150 while ((sec = elf_nextscn(elf, sec)) != NULL) {
151 gelf_getshdr(sec, &shdr);
153 if ((addr >= shdr.sh_addr) &&
154 (addr < (shdr.sh_addr + shdr.sh_size)))
155 return cnt;
157 ++cnt;
160 return -1;
163 Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep,
164 GElf_Shdr *shp, const char *name, size_t *idx)
166 Elf_Scn *sec = NULL;
167 size_t cnt = 1;
169 /* Elf is corrupted/truncated, avoid calling elf_strptr. */
170 if (!elf_rawdata(elf_getscn(elf, ep->e_shstrndx), NULL))
171 return NULL;
173 while ((sec = elf_nextscn(elf, sec)) != NULL) {
174 char *str;
176 gelf_getshdr(sec, shp);
177 str = elf_strptr(elf, ep->e_shstrndx, shp->sh_name);
178 if (str && !strcmp(name, str)) {
179 if (idx)
180 *idx = cnt;
181 return sec;
183 ++cnt;
186 return NULL;
189 #define elf_section__for_each_rel(reldata, pos, pos_mem, idx, nr_entries) \
190 for (idx = 0, pos = gelf_getrel(reldata, 0, &pos_mem); \
191 idx < nr_entries; \
192 ++idx, pos = gelf_getrel(reldata, idx, &pos_mem))
194 #define elf_section__for_each_rela(reldata, pos, pos_mem, idx, nr_entries) \
195 for (idx = 0, pos = gelf_getrela(reldata, 0, &pos_mem); \
196 idx < nr_entries; \
197 ++idx, pos = gelf_getrela(reldata, idx, &pos_mem))
200 * We need to check if we have a .dynsym, so that we can handle the
201 * .plt, synthesizing its symbols, that aren't on the symtabs (be it
202 * .dynsym or .symtab).
203 * And always look at the original dso, not at debuginfo packages, that
204 * have the PLT data stripped out (shdr_rel_plt.sh_type == SHT_NOBITS).
206 int dso__synthesize_plt_symbols(struct dso *dso, struct symsrc *ss, struct map *map,
207 symbol_filter_t filter)
209 uint32_t nr_rel_entries, idx;
210 GElf_Sym sym;
211 u64 plt_offset;
212 GElf_Shdr shdr_plt;
213 struct symbol *f;
214 GElf_Shdr shdr_rel_plt, shdr_dynsym;
215 Elf_Data *reldata, *syms, *symstrs;
216 Elf_Scn *scn_plt_rel, *scn_symstrs, *scn_dynsym;
217 size_t dynsym_idx;
218 GElf_Ehdr ehdr;
219 char sympltname[1024];
220 Elf *elf;
221 int nr = 0, symidx, err = 0;
223 if (!ss->dynsym)
224 return 0;
226 elf = ss->elf;
227 ehdr = ss->ehdr;
229 scn_dynsym = ss->dynsym;
230 shdr_dynsym = ss->dynshdr;
231 dynsym_idx = ss->dynsym_idx;
233 if (scn_dynsym == NULL)
234 goto out_elf_end;
236 scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt,
237 ".rela.plt", NULL);
238 if (scn_plt_rel == NULL) {
239 scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt,
240 ".rel.plt", NULL);
241 if (scn_plt_rel == NULL)
242 goto out_elf_end;
245 err = -1;
247 if (shdr_rel_plt.sh_link != dynsym_idx)
248 goto out_elf_end;
250 if (elf_section_by_name(elf, &ehdr, &shdr_plt, ".plt", NULL) == NULL)
251 goto out_elf_end;
254 * Fetch the relocation section to find the idxes to the GOT
255 * and the symbols in the .dynsym they refer to.
257 reldata = elf_getdata(scn_plt_rel, NULL);
258 if (reldata == NULL)
259 goto out_elf_end;
261 syms = elf_getdata(scn_dynsym, NULL);
262 if (syms == NULL)
263 goto out_elf_end;
265 scn_symstrs = elf_getscn(elf, shdr_dynsym.sh_link);
266 if (scn_symstrs == NULL)
267 goto out_elf_end;
269 symstrs = elf_getdata(scn_symstrs, NULL);
270 if (symstrs == NULL)
271 goto out_elf_end;
273 if (symstrs->d_size == 0)
274 goto out_elf_end;
276 nr_rel_entries = shdr_rel_plt.sh_size / shdr_rel_plt.sh_entsize;
277 plt_offset = shdr_plt.sh_offset;
279 if (shdr_rel_plt.sh_type == SHT_RELA) {
280 GElf_Rela pos_mem, *pos;
282 elf_section__for_each_rela(reldata, pos, pos_mem, idx,
283 nr_rel_entries) {
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);
292 if (!f)
293 goto out_elf_end;
295 if (filter && filter(map, f))
296 symbol__delete(f);
297 else {
298 symbols__insert(&dso->symbols[map->type], f);
299 ++nr;
302 } else if (shdr_rel_plt.sh_type == SHT_REL) {
303 GElf_Rel pos_mem, *pos;
304 elf_section__for_each_rel(reldata, pos, pos_mem, idx,
305 nr_rel_entries) {
306 symidx = GELF_R_SYM(pos->r_info);
307 plt_offset += shdr_plt.sh_entsize;
308 gelf_getsym(syms, symidx, &sym);
309 snprintf(sympltname, sizeof(sympltname),
310 "%s@plt", elf_sym__name(&sym, symstrs));
312 f = symbol__new(plt_offset, shdr_plt.sh_entsize,
313 STB_GLOBAL, sympltname);
314 if (!f)
315 goto out_elf_end;
317 if (filter && filter(map, f))
318 symbol__delete(f);
319 else {
320 symbols__insert(&dso->symbols[map->type], f);
321 ++nr;
326 err = 0;
327 out_elf_end:
328 if (err == 0)
329 return nr;
330 pr_debug("%s: problems reading %s PLT info.\n",
331 __func__, dso->long_name);
332 return 0;
336 * Align offset to 4 bytes as needed for note name and descriptor data.
338 #define NOTE_ALIGN(n) (((n) + 3) & -4U)
340 static int elf_read_build_id(Elf *elf, void *bf, size_t size)
342 int err = -1;
343 GElf_Ehdr ehdr;
344 GElf_Shdr shdr;
345 Elf_Data *data;
346 Elf_Scn *sec;
347 Elf_Kind ek;
348 void *ptr;
350 if (size < BUILD_ID_SIZE)
351 goto out;
353 ek = elf_kind(elf);
354 if (ek != ELF_K_ELF)
355 goto out;
357 if (gelf_getehdr(elf, &ehdr) == NULL) {
358 pr_err("%s: cannot get elf header.\n", __func__);
359 goto out;
363 * Check following sections for notes:
364 * '.note.gnu.build-id'
365 * '.notes'
366 * '.note' (VDSO specific)
368 do {
369 sec = elf_section_by_name(elf, &ehdr, &shdr,
370 ".note.gnu.build-id", NULL);
371 if (sec)
372 break;
374 sec = elf_section_by_name(elf, &ehdr, &shdr,
375 ".notes", NULL);
376 if (sec)
377 break;
379 sec = elf_section_by_name(elf, &ehdr, &shdr,
380 ".note", NULL);
381 if (sec)
382 break;
384 return err;
386 } while (0);
388 data = elf_getdata(sec, NULL);
389 if (data == NULL)
390 goto out;
392 ptr = data->d_buf;
393 while (ptr < (data->d_buf + data->d_size)) {
394 GElf_Nhdr *nhdr = ptr;
395 size_t namesz = NOTE_ALIGN(nhdr->n_namesz),
396 descsz = NOTE_ALIGN(nhdr->n_descsz);
397 const char *name;
399 ptr += sizeof(*nhdr);
400 name = ptr;
401 ptr += namesz;
402 if (nhdr->n_type == NT_GNU_BUILD_ID &&
403 nhdr->n_namesz == sizeof("GNU")) {
404 if (memcmp(name, "GNU", sizeof("GNU")) == 0) {
405 size_t sz = min(size, descsz);
406 memcpy(bf, ptr, sz);
407 memset(bf + sz, 0, size - sz);
408 err = descsz;
409 break;
412 ptr += descsz;
415 out:
416 return err;
419 int filename__read_build_id(const char *filename, void *bf, size_t size)
421 int fd, err = -1;
422 Elf *elf;
424 if (size < BUILD_ID_SIZE)
425 goto out;
427 fd = open(filename, O_RDONLY);
428 if (fd < 0)
429 goto out;
431 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
432 if (elf == NULL) {
433 pr_debug2("%s: cannot read %s ELF file.\n", __func__, filename);
434 goto out_close;
437 err = elf_read_build_id(elf, bf, size);
439 elf_end(elf);
440 out_close:
441 close(fd);
442 out:
443 return err;
446 int sysfs__read_build_id(const char *filename, void *build_id, size_t size)
448 int fd, err = -1;
450 if (size < BUILD_ID_SIZE)
451 goto out;
453 fd = open(filename, O_RDONLY);
454 if (fd < 0)
455 goto out;
457 while (1) {
458 char bf[BUFSIZ];
459 GElf_Nhdr nhdr;
460 size_t namesz, descsz;
462 if (read(fd, &nhdr, sizeof(nhdr)) != sizeof(nhdr))
463 break;
465 namesz = NOTE_ALIGN(nhdr.n_namesz);
466 descsz = NOTE_ALIGN(nhdr.n_descsz);
467 if (nhdr.n_type == NT_GNU_BUILD_ID &&
468 nhdr.n_namesz == sizeof("GNU")) {
469 if (read(fd, bf, namesz) != (ssize_t)namesz)
470 break;
471 if (memcmp(bf, "GNU", sizeof("GNU")) == 0) {
472 size_t sz = min(descsz, size);
473 if (read(fd, build_id, sz) == (ssize_t)sz) {
474 memset(build_id + sz, 0, size - sz);
475 err = 0;
476 break;
478 } else if (read(fd, bf, descsz) != (ssize_t)descsz)
479 break;
480 } else {
481 int n = namesz + descsz;
482 if (read(fd, bf, n) != n)
483 break;
486 close(fd);
487 out:
488 return err;
491 int filename__read_debuglink(const char *filename, char *debuglink,
492 size_t size)
494 int fd, err = -1;
495 Elf *elf;
496 GElf_Ehdr ehdr;
497 GElf_Shdr shdr;
498 Elf_Data *data;
499 Elf_Scn *sec;
500 Elf_Kind ek;
502 fd = open(filename, O_RDONLY);
503 if (fd < 0)
504 goto out;
506 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
507 if (elf == NULL) {
508 pr_debug2("%s: cannot read %s ELF file.\n", __func__, filename);
509 goto out_close;
512 ek = elf_kind(elf);
513 if (ek != ELF_K_ELF)
514 goto out_elf_end;
516 if (gelf_getehdr(elf, &ehdr) == NULL) {
517 pr_err("%s: cannot get elf header.\n", __func__);
518 goto out_elf_end;
521 sec = elf_section_by_name(elf, &ehdr, &shdr,
522 ".gnu_debuglink", NULL);
523 if (sec == NULL)
524 goto out_elf_end;
526 data = elf_getdata(sec, NULL);
527 if (data == NULL)
528 goto out_elf_end;
530 /* the start of this section is a zero-terminated string */
531 strncpy(debuglink, data->d_buf, size);
533 err = 0;
535 out_elf_end:
536 elf_end(elf);
537 out_close:
538 close(fd);
539 out:
540 return err;
543 static int dso__swap_init(struct dso *dso, unsigned char eidata)
545 static unsigned int const endian = 1;
547 dso->needs_swap = DSO_SWAP__NO;
549 switch (eidata) {
550 case ELFDATA2LSB:
551 /* We are big endian, DSO is little endian. */
552 if (*(unsigned char const *)&endian != 1)
553 dso->needs_swap = DSO_SWAP__YES;
554 break;
556 case ELFDATA2MSB:
557 /* We are little endian, DSO is big endian. */
558 if (*(unsigned char const *)&endian != 0)
559 dso->needs_swap = DSO_SWAP__YES;
560 break;
562 default:
563 pr_err("unrecognized DSO data encoding %d\n", eidata);
564 return -EINVAL;
567 return 0;
570 static int decompress_kmodule(struct dso *dso, const char *name,
571 enum dso_binary_type type)
573 int fd;
574 const char *ext = strrchr(name, '.');
575 char tmpbuf[] = "/tmp/perf-kmod-XXXXXX";
577 if ((type != DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP &&
578 type != DSO_BINARY_TYPE__GUEST_KMODULE_COMP) ||
579 type != dso->symtab_type)
580 return -1;
582 if (!ext || !is_supported_compression(ext + 1))
583 return -1;
585 fd = mkstemp(tmpbuf);
586 if (fd < 0)
587 return -1;
589 if (!decompress_to_file(ext + 1, name, fd)) {
590 close(fd);
591 fd = -1;
594 unlink(tmpbuf);
596 return fd;
599 bool symsrc__possibly_runtime(struct symsrc *ss)
601 return ss->dynsym || ss->opdsec;
604 bool symsrc__has_symtab(struct symsrc *ss)
606 return ss->symtab != NULL;
609 void symsrc__destroy(struct symsrc *ss)
611 zfree(&ss->name);
612 elf_end(ss->elf);
613 close(ss->fd);
616 int symsrc__init(struct symsrc *ss, struct dso *dso, const char *name,
617 enum dso_binary_type type)
619 int err = -1;
620 GElf_Ehdr ehdr;
621 Elf *elf;
622 int fd;
624 if (dso__needs_decompress(dso))
625 fd = decompress_kmodule(dso, name, type);
626 else
627 fd = open(name, O_RDONLY);
629 if (fd < 0)
630 return -1;
632 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
633 if (elf == NULL) {
634 pr_debug("%s: cannot read %s ELF file.\n", __func__, name);
635 goto out_close;
638 if (gelf_getehdr(elf, &ehdr) == NULL) {
639 pr_debug("%s: cannot get elf header.\n", __func__);
640 goto out_elf_end;
643 if (dso__swap_init(dso, ehdr.e_ident[EI_DATA]))
644 goto out_elf_end;
646 /* Always reject images with a mismatched build-id: */
647 if (dso->has_build_id) {
648 u8 build_id[BUILD_ID_SIZE];
650 if (elf_read_build_id(elf, build_id, BUILD_ID_SIZE) < 0)
651 goto out_elf_end;
653 if (!dso__build_id_equal(dso, build_id))
654 goto out_elf_end;
657 ss->is_64_bit = (gelf_getclass(elf) == ELFCLASS64);
659 ss->symtab = elf_section_by_name(elf, &ehdr, &ss->symshdr, ".symtab",
660 NULL);
661 if (ss->symshdr.sh_type != SHT_SYMTAB)
662 ss->symtab = NULL;
664 ss->dynsym_idx = 0;
665 ss->dynsym = elf_section_by_name(elf, &ehdr, &ss->dynshdr, ".dynsym",
666 &ss->dynsym_idx);
667 if (ss->dynshdr.sh_type != SHT_DYNSYM)
668 ss->dynsym = NULL;
670 ss->opdidx = 0;
671 ss->opdsec = elf_section_by_name(elf, &ehdr, &ss->opdshdr, ".opd",
672 &ss->opdidx);
673 if (ss->opdshdr.sh_type != SHT_PROGBITS)
674 ss->opdsec = NULL;
676 if (dso->kernel == DSO_TYPE_USER) {
677 GElf_Shdr shdr;
678 ss->adjust_symbols = (ehdr.e_type == ET_EXEC ||
679 ehdr.e_type == ET_REL ||
680 dso__is_vdso(dso) ||
681 elf_section_by_name(elf, &ehdr, &shdr,
682 ".gnu.prelink_undo",
683 NULL) != NULL);
684 } else {
685 ss->adjust_symbols = ehdr.e_type == ET_EXEC ||
686 ehdr.e_type == ET_REL;
689 ss->name = strdup(name);
690 if (!ss->name)
691 goto out_elf_end;
693 ss->elf = elf;
694 ss->fd = fd;
695 ss->ehdr = ehdr;
696 ss->type = type;
698 return 0;
700 out_elf_end:
701 elf_end(elf);
702 out_close:
703 close(fd);
704 return err;
708 * ref_reloc_sym_not_found - has kernel relocation symbol been found.
709 * @kmap: kernel maps and relocation reference symbol
711 * This function returns %true if we are dealing with the kernel maps and the
712 * relocation reference symbol has not yet been found. Otherwise %false is
713 * returned.
715 static bool ref_reloc_sym_not_found(struct kmap *kmap)
717 return kmap && kmap->ref_reloc_sym && kmap->ref_reloc_sym->name &&
718 !kmap->ref_reloc_sym->unrelocated_addr;
722 * ref_reloc - kernel relocation offset.
723 * @kmap: kernel maps and relocation reference symbol
725 * This function returns the offset of kernel addresses as determined by using
726 * the relocation reference symbol i.e. if the kernel has not been relocated
727 * then the return value is zero.
729 static u64 ref_reloc(struct kmap *kmap)
731 if (kmap && kmap->ref_reloc_sym &&
732 kmap->ref_reloc_sym->unrelocated_addr)
733 return kmap->ref_reloc_sym->addr -
734 kmap->ref_reloc_sym->unrelocated_addr;
735 return 0;
738 static bool want_demangle(bool is_kernel_sym)
740 return is_kernel_sym ? symbol_conf.demangle_kernel : symbol_conf.demangle;
743 int dso__load_sym(struct dso *dso, struct map *map,
744 struct symsrc *syms_ss, struct symsrc *runtime_ss,
745 symbol_filter_t filter, int kmodule)
747 struct kmap *kmap = dso->kernel ? map__kmap(map) : NULL;
748 struct map *curr_map = map;
749 struct dso *curr_dso = dso;
750 Elf_Data *symstrs, *secstrs;
751 uint32_t nr_syms;
752 int err = -1;
753 uint32_t idx;
754 GElf_Ehdr ehdr;
755 GElf_Shdr shdr;
756 Elf_Data *syms, *opddata = NULL;
757 GElf_Sym sym;
758 Elf_Scn *sec, *sec_strndx;
759 Elf *elf;
760 int nr = 0;
761 bool remap_kernel = false, adjust_kernel_syms = false;
763 dso->symtab_type = syms_ss->type;
764 dso->is_64_bit = syms_ss->is_64_bit;
765 dso->rel = syms_ss->ehdr.e_type == ET_REL;
768 * Modules may already have symbols from kallsyms, but those symbols
769 * have the wrong values for the dso maps, so remove them.
771 if (kmodule && syms_ss->symtab)
772 symbols__delete(&dso->symbols[map->type]);
774 if (!syms_ss->symtab) {
776 * If the vmlinux is stripped, fail so we will fall back
777 * to using kallsyms. The vmlinux runtime symbols aren't
778 * of much use.
780 if (dso->kernel)
781 goto out_elf_end;
783 syms_ss->symtab = syms_ss->dynsym;
784 syms_ss->symshdr = syms_ss->dynshdr;
787 elf = syms_ss->elf;
788 ehdr = syms_ss->ehdr;
789 sec = syms_ss->symtab;
790 shdr = syms_ss->symshdr;
792 if (runtime_ss->opdsec)
793 opddata = elf_rawdata(runtime_ss->opdsec, NULL);
795 syms = elf_getdata(sec, NULL);
796 if (syms == NULL)
797 goto out_elf_end;
799 sec = elf_getscn(elf, shdr.sh_link);
800 if (sec == NULL)
801 goto out_elf_end;
803 symstrs = elf_getdata(sec, NULL);
804 if (symstrs == NULL)
805 goto out_elf_end;
807 sec_strndx = elf_getscn(runtime_ss->elf, runtime_ss->ehdr.e_shstrndx);
808 if (sec_strndx == NULL)
809 goto out_elf_end;
811 secstrs = elf_getdata(sec_strndx, NULL);
812 if (secstrs == NULL)
813 goto out_elf_end;
815 nr_syms = shdr.sh_size / shdr.sh_entsize;
817 memset(&sym, 0, sizeof(sym));
820 * The kernel relocation symbol is needed in advance in order to adjust
821 * kernel maps correctly.
823 if (ref_reloc_sym_not_found(kmap)) {
824 elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) {
825 const char *elf_name = elf_sym__name(&sym, symstrs);
827 if (strcmp(elf_name, kmap->ref_reloc_sym->name))
828 continue;
829 kmap->ref_reloc_sym->unrelocated_addr = sym.st_value;
830 map->reloc = kmap->ref_reloc_sym->addr -
831 kmap->ref_reloc_sym->unrelocated_addr;
832 break;
836 dso->adjust_symbols = runtime_ss->adjust_symbols || ref_reloc(kmap);
838 * Initial kernel and module mappings do not map to the dso. For
839 * function mappings, flag the fixups.
841 if (map->type == MAP__FUNCTION && (dso->kernel || kmodule)) {
842 remap_kernel = true;
843 adjust_kernel_syms = dso->adjust_symbols;
845 elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) {
846 struct symbol *f;
847 const char *elf_name = elf_sym__name(&sym, symstrs);
848 char *demangled = NULL;
849 int is_label = elf_sym__is_label(&sym);
850 const char *section_name;
851 bool used_opd = false;
853 if (!is_label && !elf_sym__is_a(&sym, map->type))
854 continue;
856 /* Reject ARM ELF "mapping symbols": these aren't unique and
857 * don't identify functions, so will confuse the profile
858 * output: */
859 if (ehdr.e_machine == EM_ARM) {
860 if (!strcmp(elf_name, "$a") ||
861 !strcmp(elf_name, "$d") ||
862 !strcmp(elf_name, "$t"))
863 continue;
866 if (runtime_ss->opdsec && sym.st_shndx == runtime_ss->opdidx) {
867 u32 offset = sym.st_value - syms_ss->opdshdr.sh_addr;
868 u64 *opd = opddata->d_buf + offset;
869 sym.st_value = DSO__SWAP(dso, u64, *opd);
870 sym.st_shndx = elf_addr_to_index(runtime_ss->elf,
871 sym.st_value);
872 used_opd = true;
875 * When loading symbols in a data mapping, ABS symbols (which
876 * has a value of SHN_ABS in its st_shndx) failed at
877 * elf_getscn(). And it marks the loading as a failure so
878 * already loaded symbols cannot be fixed up.
880 * I'm not sure what should be done. Just ignore them for now.
881 * - Namhyung Kim
883 if (sym.st_shndx == SHN_ABS)
884 continue;
886 sec = elf_getscn(runtime_ss->elf, sym.st_shndx);
887 if (!sec)
888 goto out_elf_end;
890 gelf_getshdr(sec, &shdr);
892 if (is_label && !elf_sec__is_a(&shdr, secstrs, map->type))
893 continue;
895 section_name = elf_sec__name(&shdr, secstrs);
897 /* On ARM, symbols for thumb functions have 1 added to
898 * the symbol address as a flag - remove it */
899 if ((ehdr.e_machine == EM_ARM) &&
900 (map->type == MAP__FUNCTION) &&
901 (sym.st_value & 1))
902 --sym.st_value;
904 if (dso->kernel || kmodule) {
905 char dso_name[PATH_MAX];
907 /* Adjust symbol to map to file offset */
908 if (adjust_kernel_syms)
909 sym.st_value -= shdr.sh_addr - shdr.sh_offset;
911 if (strcmp(section_name,
912 (curr_dso->short_name +
913 dso->short_name_len)) == 0)
914 goto new_symbol;
916 if (strcmp(section_name, ".text") == 0) {
918 * The initial kernel mapping is based on
919 * kallsyms and identity maps. Overwrite it to
920 * map to the kernel dso.
922 if (remap_kernel && dso->kernel) {
923 remap_kernel = false;
924 map->start = shdr.sh_addr +
925 ref_reloc(kmap);
926 map->end = map->start + shdr.sh_size;
927 map->pgoff = shdr.sh_offset;
928 map->map_ip = map__map_ip;
929 map->unmap_ip = map__unmap_ip;
930 /* Ensure maps are correctly ordered */
931 map_groups__remove(kmap->kmaps, map);
932 map_groups__insert(kmap->kmaps, map);
936 * The initial module mapping is based on
937 * /proc/modules mapped to offset zero.
938 * Overwrite it to map to the module dso.
940 if (remap_kernel && kmodule) {
941 remap_kernel = false;
942 map->pgoff = shdr.sh_offset;
945 curr_map = map;
946 curr_dso = dso;
947 goto new_symbol;
950 if (!kmap)
951 goto new_symbol;
953 snprintf(dso_name, sizeof(dso_name),
954 "%s%s", dso->short_name, section_name);
956 curr_map = map_groups__find_by_name(kmap->kmaps, map->type, dso_name);
957 if (curr_map == NULL) {
958 u64 start = sym.st_value;
960 if (kmodule)
961 start += map->start + shdr.sh_offset;
963 curr_dso = dso__new(dso_name);
964 if (curr_dso == NULL)
965 goto out_elf_end;
966 curr_dso->kernel = dso->kernel;
967 curr_dso->long_name = dso->long_name;
968 curr_dso->long_name_len = dso->long_name_len;
969 curr_map = map__new2(start, curr_dso,
970 map->type);
971 if (curr_map == NULL) {
972 dso__delete(curr_dso);
973 goto out_elf_end;
975 if (adjust_kernel_syms) {
976 curr_map->start = shdr.sh_addr +
977 ref_reloc(kmap);
978 curr_map->end = curr_map->start +
979 shdr.sh_size;
980 curr_map->pgoff = shdr.sh_offset;
981 } else {
982 curr_map->map_ip = identity__map_ip;
983 curr_map->unmap_ip = identity__map_ip;
985 curr_dso->symtab_type = dso->symtab_type;
986 map_groups__insert(kmap->kmaps, curr_map);
988 * The new DSO should go to the kernel DSOS
990 dsos__add(&map->groups->machine->kernel_dsos,
991 curr_dso);
992 dso__set_loaded(curr_dso, map->type);
993 } else
994 curr_dso = curr_map->dso;
996 goto new_symbol;
999 if ((used_opd && runtime_ss->adjust_symbols)
1000 || (!used_opd && syms_ss->adjust_symbols)) {
1001 pr_debug4("%s: adjusting symbol: st_value: %#" PRIx64 " "
1002 "sh_addr: %#" PRIx64 " sh_offset: %#" PRIx64 "\n", __func__,
1003 (u64)sym.st_value, (u64)shdr.sh_addr,
1004 (u64)shdr.sh_offset);
1005 sym.st_value -= shdr.sh_addr - shdr.sh_offset;
1007 new_symbol:
1009 * We need to figure out if the object was created from C++ sources
1010 * DWARF DW_compile_unit has this, but we don't always have access
1011 * to it...
1013 if (want_demangle(dso->kernel || kmodule)) {
1014 int demangle_flags = DMGL_NO_OPTS;
1015 if (verbose)
1016 demangle_flags = DMGL_PARAMS | DMGL_ANSI;
1018 demangled = bfd_demangle(NULL, elf_name, demangle_flags);
1019 if (demangled != NULL)
1020 elf_name = demangled;
1022 f = symbol__new(sym.st_value, sym.st_size,
1023 GELF_ST_BIND(sym.st_info), elf_name);
1024 free(demangled);
1025 if (!f)
1026 goto out_elf_end;
1028 if (filter && filter(curr_map, f))
1029 symbol__delete(f);
1030 else {
1031 symbols__insert(&curr_dso->symbols[curr_map->type], f);
1032 nr++;
1037 * For misannotated, zeroed, ASM function sizes.
1039 if (nr > 0) {
1040 symbols__fixup_duplicate(&dso->symbols[map->type]);
1041 symbols__fixup_end(&dso->symbols[map->type]);
1042 if (kmap) {
1044 * We need to fixup this here too because we create new
1045 * maps here, for things like vsyscall sections.
1047 __map_groups__fixup_end(kmap->kmaps, map->type);
1050 err = nr;
1051 out_elf_end:
1052 return err;
1055 static int elf_read_maps(Elf *elf, bool exe, mapfn_t mapfn, void *data)
1057 GElf_Phdr phdr;
1058 size_t i, phdrnum;
1059 int err;
1060 u64 sz;
1062 if (elf_getphdrnum(elf, &phdrnum))
1063 return -1;
1065 for (i = 0; i < phdrnum; i++) {
1066 if (gelf_getphdr(elf, i, &phdr) == NULL)
1067 return -1;
1068 if (phdr.p_type != PT_LOAD)
1069 continue;
1070 if (exe) {
1071 if (!(phdr.p_flags & PF_X))
1072 continue;
1073 } else {
1074 if (!(phdr.p_flags & PF_R))
1075 continue;
1077 sz = min(phdr.p_memsz, phdr.p_filesz);
1078 if (!sz)
1079 continue;
1080 err = mapfn(phdr.p_vaddr, sz, phdr.p_offset, data);
1081 if (err)
1082 return err;
1084 return 0;
1087 int file__read_maps(int fd, bool exe, mapfn_t mapfn, void *data,
1088 bool *is_64_bit)
1090 int err;
1091 Elf *elf;
1093 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
1094 if (elf == NULL)
1095 return -1;
1097 if (is_64_bit)
1098 *is_64_bit = (gelf_getclass(elf) == ELFCLASS64);
1100 err = elf_read_maps(elf, exe, mapfn, data);
1102 elf_end(elf);
1103 return err;
1106 enum dso_type dso__type_fd(int fd)
1108 enum dso_type dso_type = DSO__TYPE_UNKNOWN;
1109 GElf_Ehdr ehdr;
1110 Elf_Kind ek;
1111 Elf *elf;
1113 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
1114 if (elf == NULL)
1115 goto out;
1117 ek = elf_kind(elf);
1118 if (ek != ELF_K_ELF)
1119 goto out_end;
1121 if (gelf_getclass(elf) == ELFCLASS64) {
1122 dso_type = DSO__TYPE_64BIT;
1123 goto out_end;
1126 if (gelf_getehdr(elf, &ehdr) == NULL)
1127 goto out_end;
1129 if (ehdr.e_machine == EM_X86_64)
1130 dso_type = DSO__TYPE_X32BIT;
1131 else
1132 dso_type = DSO__TYPE_32BIT;
1133 out_end:
1134 elf_end(elf);
1135 out:
1136 return dso_type;
1139 static int copy_bytes(int from, off_t from_offs, int to, off_t to_offs, u64 len)
1141 ssize_t r;
1142 size_t n;
1143 int err = -1;
1144 char *buf = malloc(page_size);
1146 if (buf == NULL)
1147 return -1;
1149 if (lseek(to, to_offs, SEEK_SET) != to_offs)
1150 goto out;
1152 if (lseek(from, from_offs, SEEK_SET) != from_offs)
1153 goto out;
1155 while (len) {
1156 n = page_size;
1157 if (len < n)
1158 n = len;
1159 /* Use read because mmap won't work on proc files */
1160 r = read(from, buf, n);
1161 if (r < 0)
1162 goto out;
1163 if (!r)
1164 break;
1165 n = r;
1166 r = write(to, buf, n);
1167 if (r < 0)
1168 goto out;
1169 if ((size_t)r != n)
1170 goto out;
1171 len -= n;
1174 err = 0;
1175 out:
1176 free(buf);
1177 return err;
1180 struct kcore {
1181 int fd;
1182 int elfclass;
1183 Elf *elf;
1184 GElf_Ehdr ehdr;
1187 static int kcore__open(struct kcore *kcore, const char *filename)
1189 GElf_Ehdr *ehdr;
1191 kcore->fd = open(filename, O_RDONLY);
1192 if (kcore->fd == -1)
1193 return -1;
1195 kcore->elf = elf_begin(kcore->fd, ELF_C_READ, NULL);
1196 if (!kcore->elf)
1197 goto out_close;
1199 kcore->elfclass = gelf_getclass(kcore->elf);
1200 if (kcore->elfclass == ELFCLASSNONE)
1201 goto out_end;
1203 ehdr = gelf_getehdr(kcore->elf, &kcore->ehdr);
1204 if (!ehdr)
1205 goto out_end;
1207 return 0;
1209 out_end:
1210 elf_end(kcore->elf);
1211 out_close:
1212 close(kcore->fd);
1213 return -1;
1216 static int kcore__init(struct kcore *kcore, char *filename, int elfclass,
1217 bool temp)
1219 GElf_Ehdr *ehdr;
1221 kcore->elfclass = elfclass;
1223 if (temp)
1224 kcore->fd = mkstemp(filename);
1225 else
1226 kcore->fd = open(filename, O_WRONLY | O_CREAT | O_EXCL, 0400);
1227 if (kcore->fd == -1)
1228 return -1;
1230 kcore->elf = elf_begin(kcore->fd, ELF_C_WRITE, NULL);
1231 if (!kcore->elf)
1232 goto out_close;
1234 if (!gelf_newehdr(kcore->elf, elfclass))
1235 goto out_end;
1237 ehdr = gelf_getehdr(kcore->elf, &kcore->ehdr);
1238 if (!ehdr)
1239 goto out_end;
1241 return 0;
1243 out_end:
1244 elf_end(kcore->elf);
1245 out_close:
1246 close(kcore->fd);
1247 unlink(filename);
1248 return -1;
1251 static void kcore__close(struct kcore *kcore)
1253 elf_end(kcore->elf);
1254 close(kcore->fd);
1257 static int kcore__copy_hdr(struct kcore *from, struct kcore *to, size_t count)
1259 GElf_Ehdr *ehdr = &to->ehdr;
1260 GElf_Ehdr *kehdr = &from->ehdr;
1262 memcpy(ehdr->e_ident, kehdr->e_ident, EI_NIDENT);
1263 ehdr->e_type = kehdr->e_type;
1264 ehdr->e_machine = kehdr->e_machine;
1265 ehdr->e_version = kehdr->e_version;
1266 ehdr->e_entry = 0;
1267 ehdr->e_shoff = 0;
1268 ehdr->e_flags = kehdr->e_flags;
1269 ehdr->e_phnum = count;
1270 ehdr->e_shentsize = 0;
1271 ehdr->e_shnum = 0;
1272 ehdr->e_shstrndx = 0;
1274 if (from->elfclass == ELFCLASS32) {
1275 ehdr->e_phoff = sizeof(Elf32_Ehdr);
1276 ehdr->e_ehsize = sizeof(Elf32_Ehdr);
1277 ehdr->e_phentsize = sizeof(Elf32_Phdr);
1278 } else {
1279 ehdr->e_phoff = sizeof(Elf64_Ehdr);
1280 ehdr->e_ehsize = sizeof(Elf64_Ehdr);
1281 ehdr->e_phentsize = sizeof(Elf64_Phdr);
1284 if (!gelf_update_ehdr(to->elf, ehdr))
1285 return -1;
1287 if (!gelf_newphdr(to->elf, count))
1288 return -1;
1290 return 0;
1293 static int kcore__add_phdr(struct kcore *kcore, int idx, off_t offset,
1294 u64 addr, u64 len)
1296 GElf_Phdr gphdr;
1297 GElf_Phdr *phdr;
1299 phdr = gelf_getphdr(kcore->elf, idx, &gphdr);
1300 if (!phdr)
1301 return -1;
1303 phdr->p_type = PT_LOAD;
1304 phdr->p_flags = PF_R | PF_W | PF_X;
1305 phdr->p_offset = offset;
1306 phdr->p_vaddr = addr;
1307 phdr->p_paddr = 0;
1308 phdr->p_filesz = len;
1309 phdr->p_memsz = len;
1310 phdr->p_align = page_size;
1312 if (!gelf_update_phdr(kcore->elf, idx, phdr))
1313 return -1;
1315 return 0;
1318 static off_t kcore__write(struct kcore *kcore)
1320 return elf_update(kcore->elf, ELF_C_WRITE);
1323 struct phdr_data {
1324 off_t offset;
1325 u64 addr;
1326 u64 len;
1329 struct kcore_copy_info {
1330 u64 stext;
1331 u64 etext;
1332 u64 first_symbol;
1333 u64 last_symbol;
1334 u64 first_module;
1335 u64 last_module_symbol;
1336 struct phdr_data kernel_map;
1337 struct phdr_data modules_map;
1340 static int kcore_copy__process_kallsyms(void *arg, const char *name, char type,
1341 u64 start)
1343 struct kcore_copy_info *kci = arg;
1345 if (!symbol_type__is_a(type, MAP__FUNCTION))
1346 return 0;
1348 if (strchr(name, '[')) {
1349 if (start > kci->last_module_symbol)
1350 kci->last_module_symbol = start;
1351 return 0;
1354 if (!kci->first_symbol || start < kci->first_symbol)
1355 kci->first_symbol = start;
1357 if (!kci->last_symbol || start > kci->last_symbol)
1358 kci->last_symbol = start;
1360 if (!strcmp(name, "_stext")) {
1361 kci->stext = start;
1362 return 0;
1365 if (!strcmp(name, "_etext")) {
1366 kci->etext = start;
1367 return 0;
1370 return 0;
1373 static int kcore_copy__parse_kallsyms(struct kcore_copy_info *kci,
1374 const char *dir)
1376 char kallsyms_filename[PATH_MAX];
1378 scnprintf(kallsyms_filename, PATH_MAX, "%s/kallsyms", dir);
1380 if (symbol__restricted_filename(kallsyms_filename, "/proc/kallsyms"))
1381 return -1;
1383 if (kallsyms__parse(kallsyms_filename, kci,
1384 kcore_copy__process_kallsyms) < 0)
1385 return -1;
1387 return 0;
1390 static int kcore_copy__process_modules(void *arg,
1391 const char *name __maybe_unused,
1392 u64 start)
1394 struct kcore_copy_info *kci = arg;
1396 if (!kci->first_module || start < kci->first_module)
1397 kci->first_module = start;
1399 return 0;
1402 static int kcore_copy__parse_modules(struct kcore_copy_info *kci,
1403 const char *dir)
1405 char modules_filename[PATH_MAX];
1407 scnprintf(modules_filename, PATH_MAX, "%s/modules", dir);
1409 if (symbol__restricted_filename(modules_filename, "/proc/modules"))
1410 return -1;
1412 if (modules__parse(modules_filename, kci,
1413 kcore_copy__process_modules) < 0)
1414 return -1;
1416 return 0;
1419 static void kcore_copy__map(struct phdr_data *p, u64 start, u64 end, u64 pgoff,
1420 u64 s, u64 e)
1422 if (p->addr || s < start || s >= end)
1423 return;
1425 p->addr = s;
1426 p->offset = (s - start) + pgoff;
1427 p->len = e < end ? e - s : end - s;
1430 static int kcore_copy__read_map(u64 start, u64 len, u64 pgoff, void *data)
1432 struct kcore_copy_info *kci = data;
1433 u64 end = start + len;
1435 kcore_copy__map(&kci->kernel_map, start, end, pgoff, kci->stext,
1436 kci->etext);
1438 kcore_copy__map(&kci->modules_map, start, end, pgoff, kci->first_module,
1439 kci->last_module_symbol);
1441 return 0;
1444 static int kcore_copy__read_maps(struct kcore_copy_info *kci, Elf *elf)
1446 if (elf_read_maps(elf, true, kcore_copy__read_map, kci) < 0)
1447 return -1;
1449 return 0;
1452 static int kcore_copy__calc_maps(struct kcore_copy_info *kci, const char *dir,
1453 Elf *elf)
1455 if (kcore_copy__parse_kallsyms(kci, dir))
1456 return -1;
1458 if (kcore_copy__parse_modules(kci, dir))
1459 return -1;
1461 if (kci->stext)
1462 kci->stext = round_down(kci->stext, page_size);
1463 else
1464 kci->stext = round_down(kci->first_symbol, page_size);
1466 if (kci->etext) {
1467 kci->etext = round_up(kci->etext, page_size);
1468 } else if (kci->last_symbol) {
1469 kci->etext = round_up(kci->last_symbol, page_size);
1470 kci->etext += page_size;
1473 kci->first_module = round_down(kci->first_module, page_size);
1475 if (kci->last_module_symbol) {
1476 kci->last_module_symbol = round_up(kci->last_module_symbol,
1477 page_size);
1478 kci->last_module_symbol += page_size;
1481 if (!kci->stext || !kci->etext)
1482 return -1;
1484 if (kci->first_module && !kci->last_module_symbol)
1485 return -1;
1487 return kcore_copy__read_maps(kci, elf);
1490 static int kcore_copy__copy_file(const char *from_dir, const char *to_dir,
1491 const char *name)
1493 char from_filename[PATH_MAX];
1494 char to_filename[PATH_MAX];
1496 scnprintf(from_filename, PATH_MAX, "%s/%s", from_dir, name);
1497 scnprintf(to_filename, PATH_MAX, "%s/%s", to_dir, name);
1499 return copyfile_mode(from_filename, to_filename, 0400);
1502 static int kcore_copy__unlink(const char *dir, const char *name)
1504 char filename[PATH_MAX];
1506 scnprintf(filename, PATH_MAX, "%s/%s", dir, name);
1508 return unlink(filename);
1511 static int kcore_copy__compare_fds(int from, int to)
1513 char *buf_from;
1514 char *buf_to;
1515 ssize_t ret;
1516 size_t len;
1517 int err = -1;
1519 buf_from = malloc(page_size);
1520 buf_to = malloc(page_size);
1521 if (!buf_from || !buf_to)
1522 goto out;
1524 while (1) {
1525 /* Use read because mmap won't work on proc files */
1526 ret = read(from, buf_from, page_size);
1527 if (ret < 0)
1528 goto out;
1530 if (!ret)
1531 break;
1533 len = ret;
1535 if (readn(to, buf_to, len) != (int)len)
1536 goto out;
1538 if (memcmp(buf_from, buf_to, len))
1539 goto out;
1542 err = 0;
1543 out:
1544 free(buf_to);
1545 free(buf_from);
1546 return err;
1549 static int kcore_copy__compare_files(const char *from_filename,
1550 const char *to_filename)
1552 int from, to, err = -1;
1554 from = open(from_filename, O_RDONLY);
1555 if (from < 0)
1556 return -1;
1558 to = open(to_filename, O_RDONLY);
1559 if (to < 0)
1560 goto out_close_from;
1562 err = kcore_copy__compare_fds(from, to);
1564 close(to);
1565 out_close_from:
1566 close(from);
1567 return err;
1570 static int kcore_copy__compare_file(const char *from_dir, const char *to_dir,
1571 const char *name)
1573 char from_filename[PATH_MAX];
1574 char to_filename[PATH_MAX];
1576 scnprintf(from_filename, PATH_MAX, "%s/%s", from_dir, name);
1577 scnprintf(to_filename, PATH_MAX, "%s/%s", to_dir, name);
1579 return kcore_copy__compare_files(from_filename, to_filename);
1583 * kcore_copy - copy kallsyms, modules and kcore from one directory to another.
1584 * @from_dir: from directory
1585 * @to_dir: to directory
1587 * This function copies kallsyms, modules and kcore files from one directory to
1588 * another. kallsyms and modules are copied entirely. Only code segments are
1589 * copied from kcore. It is assumed that two segments suffice: one for the
1590 * kernel proper and one for all the modules. The code segments are determined
1591 * from kallsyms and modules files. The kernel map starts at _stext or the
1592 * lowest function symbol, and ends at _etext or the highest function symbol.
1593 * The module map starts at the lowest module address and ends at the highest
1594 * module symbol. Start addresses are rounded down to the nearest page. End
1595 * addresses are rounded up to the nearest page. An extra page is added to the
1596 * highest kernel symbol and highest module symbol to, hopefully, encompass that
1597 * symbol too. Because it contains only code sections, the resulting kcore is
1598 * unusual. One significant peculiarity is that the mapping (start -> pgoff)
1599 * is not the same for the kernel map and the modules map. That happens because
1600 * the data is copied adjacently whereas the original kcore has gaps. Finally,
1601 * kallsyms and modules files are compared with their copies to check that
1602 * modules have not been loaded or unloaded while the copies were taking place.
1604 * Return: %0 on success, %-1 on failure.
1606 int kcore_copy(const char *from_dir, const char *to_dir)
1608 struct kcore kcore;
1609 struct kcore extract;
1610 size_t count = 2;
1611 int idx = 0, err = -1;
1612 off_t offset = page_size, sz, modules_offset = 0;
1613 struct kcore_copy_info kci = { .stext = 0, };
1614 char kcore_filename[PATH_MAX];
1615 char extract_filename[PATH_MAX];
1617 if (kcore_copy__copy_file(from_dir, to_dir, "kallsyms"))
1618 return -1;
1620 if (kcore_copy__copy_file(from_dir, to_dir, "modules"))
1621 goto out_unlink_kallsyms;
1623 scnprintf(kcore_filename, PATH_MAX, "%s/kcore", from_dir);
1624 scnprintf(extract_filename, PATH_MAX, "%s/kcore", to_dir);
1626 if (kcore__open(&kcore, kcore_filename))
1627 goto out_unlink_modules;
1629 if (kcore_copy__calc_maps(&kci, from_dir, kcore.elf))
1630 goto out_kcore_close;
1632 if (kcore__init(&extract, extract_filename, kcore.elfclass, false))
1633 goto out_kcore_close;
1635 if (!kci.modules_map.addr)
1636 count -= 1;
1638 if (kcore__copy_hdr(&kcore, &extract, count))
1639 goto out_extract_close;
1641 if (kcore__add_phdr(&extract, idx++, offset, kci.kernel_map.addr,
1642 kci.kernel_map.len))
1643 goto out_extract_close;
1645 if (kci.modules_map.addr) {
1646 modules_offset = offset + kci.kernel_map.len;
1647 if (kcore__add_phdr(&extract, idx, modules_offset,
1648 kci.modules_map.addr, kci.modules_map.len))
1649 goto out_extract_close;
1652 sz = kcore__write(&extract);
1653 if (sz < 0 || sz > offset)
1654 goto out_extract_close;
1656 if (copy_bytes(kcore.fd, kci.kernel_map.offset, extract.fd, offset,
1657 kci.kernel_map.len))
1658 goto out_extract_close;
1660 if (modules_offset && copy_bytes(kcore.fd, kci.modules_map.offset,
1661 extract.fd, modules_offset,
1662 kci.modules_map.len))
1663 goto out_extract_close;
1665 if (kcore_copy__compare_file(from_dir, to_dir, "modules"))
1666 goto out_extract_close;
1668 if (kcore_copy__compare_file(from_dir, to_dir, "kallsyms"))
1669 goto out_extract_close;
1671 err = 0;
1673 out_extract_close:
1674 kcore__close(&extract);
1675 if (err)
1676 unlink(extract_filename);
1677 out_kcore_close:
1678 kcore__close(&kcore);
1679 out_unlink_modules:
1680 if (err)
1681 kcore_copy__unlink(to_dir, "modules");
1682 out_unlink_kallsyms:
1683 if (err)
1684 kcore_copy__unlink(to_dir, "kallsyms");
1686 return err;
1689 int kcore_extract__create(struct kcore_extract *kce)
1691 struct kcore kcore;
1692 struct kcore extract;
1693 size_t count = 1;
1694 int idx = 0, err = -1;
1695 off_t offset = page_size, sz;
1697 if (kcore__open(&kcore, kce->kcore_filename))
1698 return -1;
1700 strcpy(kce->extract_filename, PERF_KCORE_EXTRACT);
1701 if (kcore__init(&extract, kce->extract_filename, kcore.elfclass, true))
1702 goto out_kcore_close;
1704 if (kcore__copy_hdr(&kcore, &extract, count))
1705 goto out_extract_close;
1707 if (kcore__add_phdr(&extract, idx, offset, kce->addr, kce->len))
1708 goto out_extract_close;
1710 sz = kcore__write(&extract);
1711 if (sz < 0 || sz > offset)
1712 goto out_extract_close;
1714 if (copy_bytes(kcore.fd, kce->offs, extract.fd, offset, kce->len))
1715 goto out_extract_close;
1717 err = 0;
1719 out_extract_close:
1720 kcore__close(&extract);
1721 if (err)
1722 unlink(kce->extract_filename);
1723 out_kcore_close:
1724 kcore__close(&kcore);
1726 return err;
1729 void kcore_extract__delete(struct kcore_extract *kce)
1731 unlink(kce->extract_filename);
1734 void symbol__elf_init(void)
1736 elf_version(EV_CURRENT);