perf: Add Linux perf's symbol-elf.c
[akaros.git] / tools / profile / perf / symbol-elf.c
1 /* Copyright (c) 2011-2015 Linux Perf Authors
2  *
3  * This source code is licensed under the GNU General Public License Version 2.
4  * See the file LICENSE-gpl-2.0.txt for more details. */
5
6 #include <fcntl.h>
7 #include <stdio.h>
8 #include <errno.h>
9 #include <string.h>
10 #include <unistd.h>
11 #include <inttypes.h>
12
13 #include "symbol.h"
14 #include "machine.h"
15 #include "vdso.h"
16 #include <symbol/kallsyms.h>
17 #include "debug.h"
18
19 #ifndef EM_AARCH64
20 #define EM_AARCH64      183  /* ARM 64 bit */
21 #endif
22
23
24 #ifdef HAVE_CPLUS_DEMANGLE_SUPPORT
25 extern char *cplus_demangle(const char *, int);
26
27 static inline char *bfd_demangle(void __maybe_unused *v, const char *c, int i)
28 {
29         return cplus_demangle(c, i);
30 }
31 #else
32 #ifdef NO_DEMANGLE
33 static inline char *bfd_demangle(void __maybe_unused *v,
34                                  const char __maybe_unused *c,
35                                  int __maybe_unused i)
36 {
37         return NULL;
38 }
39 #else
40 #define PACKAGE 'perf'
41 #include <bfd.h>
42 #endif
43 #endif
44
45 #ifndef HAVE_ELF_GETPHDRNUM_SUPPORT
46 static int elf_getphdrnum(Elf *elf, size_t *dst)
47 {
48         GElf_Ehdr gehdr;
49         GElf_Ehdr *ehdr;
50
51         ehdr = gelf_getehdr(elf, &gehdr);
52         if (!ehdr)
53                 return -1;
54
55         *dst = ehdr->e_phnum;
56
57         return 0;
58 }
59 #endif
60
61 #ifndef NT_GNU_BUILD_ID
62 #define NT_GNU_BUILD_ID 3
63 #endif
64
65 /**
66  * elf_symtab__for_each_symbol - iterate thru all the symbols
67  *
68  * @syms: struct elf_symtab instance to iterate
69  * @idx: uint32_t idx
70  * @sym: GElf_Sym iterator
71  */
72 #define elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) \
73         for (idx = 0, gelf_getsym(syms, idx, &sym);\
74              idx < nr_syms; \
75              idx++, gelf_getsym(syms, idx, &sym))
76
77 static inline uint8_t elf_sym__type(const GElf_Sym *sym)
78 {
79         return GELF_ST_TYPE(sym->st_info);
80 }
81
82 #ifndef STT_GNU_IFUNC
83 #define STT_GNU_IFUNC 10
84 #endif
85
86 static inline int elf_sym__is_function(const GElf_Sym *sym)
87 {
88         return (elf_sym__type(sym) == STT_FUNC ||
89                 elf_sym__type(sym) == STT_GNU_IFUNC) &&
90                sym->st_name != 0 &&
91                sym->st_shndx != SHN_UNDEF;
92 }
93
94 static inline bool elf_sym__is_object(const GElf_Sym *sym)
95 {
96         return elf_sym__type(sym) == STT_OBJECT &&
97                 sym->st_name != 0 &&
98                 sym->st_shndx != SHN_UNDEF;
99 }
100
101 static inline int elf_sym__is_label(const GElf_Sym *sym)
102 {
103         return elf_sym__type(sym) == STT_NOTYPE &&
104                 sym->st_name != 0 &&
105                 sym->st_shndx != SHN_UNDEF &&
106                 sym->st_shndx != SHN_ABS;
107 }
108
109 static bool elf_sym__is_a(GElf_Sym *sym, enum map_type type)
110 {
111         switch (type) {
112         case MAP__FUNCTION:
113                 return elf_sym__is_function(sym);
114         case MAP__VARIABLE:
115                 return elf_sym__is_object(sym);
116         default:
117                 return false;
118         }
119 }
120
121 static inline const char *elf_sym__name(const GElf_Sym *sym,
122                                         const Elf_Data *symstrs)
123 {
124         return symstrs->d_buf + sym->st_name;
125 }
126
127 static inline const char *elf_sec__name(const GElf_Shdr *shdr,
128                                         const Elf_Data *secstrs)
129 {
130         return secstrs->d_buf + shdr->sh_name;
131 }
132
133 static inline int elf_sec__is_text(const GElf_Shdr *shdr,
134                                         const Elf_Data *secstrs)
135 {
136         return strstr(elf_sec__name(shdr, secstrs), "text") != NULL;
137 }
138
139 static inline bool elf_sec__is_data(const GElf_Shdr *shdr,
140                                     const Elf_Data *secstrs)
141 {
142         return strstr(elf_sec__name(shdr, secstrs), "data") != NULL;
143 }
144
145 static bool elf_sec__is_a(GElf_Shdr *shdr, Elf_Data *secstrs,
146                           enum map_type type)
147 {
148         switch (type) {
149         case MAP__FUNCTION:
150                 return elf_sec__is_text(shdr, secstrs);
151         case MAP__VARIABLE:
152                 return elf_sec__is_data(shdr, secstrs);
153         default:
154                 return false;
155         }
156 }
157
158 static size_t elf_addr_to_index(Elf *elf, GElf_Addr addr)
159 {
160         Elf_Scn *sec = NULL;
161         GElf_Shdr shdr;
162         size_t cnt = 1;
163
164         while ((sec = elf_nextscn(elf, sec)) != NULL) {
165                 gelf_getshdr(sec, &shdr);
166
167                 if ((addr >= shdr.sh_addr) &&
168                     (addr < (shdr.sh_addr + shdr.sh_size)))
169                         return cnt;
170
171                 ++cnt;
172         }
173
174         return -1;
175 }
176
177 Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep,
178                              GElf_Shdr *shp, const char *name, size_t *idx)
179 {
180         Elf_Scn *sec = NULL;
181         size_t cnt = 1;
182
183         /* Elf is corrupted/truncated, avoid calling elf_strptr. */
184         if (!elf_rawdata(elf_getscn(elf, ep->e_shstrndx), NULL))
185                 return NULL;
186
187         while ((sec = elf_nextscn(elf, sec)) != NULL) {
188                 char *str;
189
190                 gelf_getshdr(sec, shp);
191                 str = elf_strptr(elf, ep->e_shstrndx, shp->sh_name);
192                 if (str && !strcmp(name, str)) {
193                         if (idx)
194                                 *idx = cnt;
195                         return sec;
196                 }
197                 ++cnt;
198         }
199
200         return NULL;
201 }
202
203 #define elf_section__for_each_rel(reldata, pos, pos_mem, idx, nr_entries) \
204         for (idx = 0, pos = gelf_getrel(reldata, 0, &pos_mem); \
205              idx < nr_entries; \
206              ++idx, pos = gelf_getrel(reldata, idx, &pos_mem))
207
208 #define elf_section__for_each_rela(reldata, pos, pos_mem, idx, nr_entries) \
209         for (idx = 0, pos = gelf_getrela(reldata, 0, &pos_mem); \
210              idx < nr_entries; \
211              ++idx, pos = gelf_getrela(reldata, idx, &pos_mem))
212
213 /*
214  * We need to check if we have a .dynsym, so that we can handle the
215  * .plt, synthesizing its symbols, that aren't on the symtabs (be it
216  * .dynsym or .symtab).
217  * And always look at the original dso, not at debuginfo packages, that
218  * have the PLT data stripped out (shdr_rel_plt.sh_type == SHT_NOBITS).
219  */
220 int dso__synthesize_plt_symbols(struct dso *dso, struct symsrc *ss, struct map *map,
221                                 symbol_filter_t filter)
222 {
223         uint32_t nr_rel_entries, idx;
224         GElf_Sym sym;
225         u64 plt_offset;
226         GElf_Shdr shdr_plt;
227         struct symbol *f;
228         GElf_Shdr shdr_rel_plt, shdr_dynsym;
229         Elf_Data *reldata, *syms, *symstrs;
230         Elf_Scn *scn_plt_rel, *scn_symstrs, *scn_dynsym;
231         size_t dynsym_idx;
232         GElf_Ehdr ehdr;
233         char sympltname[1024];
234         Elf *elf;
235         int nr = 0, symidx, err = 0;
236
237         if (!ss->dynsym)
238                 return 0;
239
240         elf = ss->elf;
241         ehdr = ss->ehdr;
242
243         scn_dynsym = ss->dynsym;
244         shdr_dynsym = ss->dynshdr;
245         dynsym_idx = ss->dynsym_idx;
246
247         if (scn_dynsym == NULL)
248                 goto out_elf_end;
249
250         scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt,
251                                           ".rela.plt", NULL);
252         if (scn_plt_rel == NULL) {
253                 scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt,
254                                                   ".rel.plt", NULL);
255                 if (scn_plt_rel == NULL)
256                         goto out_elf_end;
257         }
258
259         err = -1;
260
261         if (shdr_rel_plt.sh_link != dynsym_idx)
262                 goto out_elf_end;
263
264         if (elf_section_by_name(elf, &ehdr, &shdr_plt, ".plt", NULL) == NULL)
265                 goto out_elf_end;
266
267         /*
268          * Fetch the relocation section to find the idxes to the GOT
269          * and the symbols in the .dynsym they refer to.
270          */
271         reldata = elf_getdata(scn_plt_rel, NULL);
272         if (reldata == NULL)
273                 goto out_elf_end;
274
275         syms = elf_getdata(scn_dynsym, NULL);
276         if (syms == NULL)
277                 goto out_elf_end;
278
279         scn_symstrs = elf_getscn(elf, shdr_dynsym.sh_link);
280         if (scn_symstrs == NULL)
281                 goto out_elf_end;
282
283         symstrs = elf_getdata(scn_symstrs, NULL);
284         if (symstrs == NULL)
285                 goto out_elf_end;
286
287         if (symstrs->d_size == 0)
288                 goto out_elf_end;
289
290         nr_rel_entries = shdr_rel_plt.sh_size / shdr_rel_plt.sh_entsize;
291         plt_offset = shdr_plt.sh_offset;
292
293         if (shdr_rel_plt.sh_type == SHT_RELA) {
294                 GElf_Rela pos_mem, *pos;
295
296                 elf_section__for_each_rela(reldata, pos, pos_mem, idx,
297                                            nr_rel_entries) {
298                         symidx = GELF_R_SYM(pos->r_info);
299                         plt_offset += shdr_plt.sh_entsize;
300                         gelf_getsym(syms, symidx, &sym);
301                         snprintf(sympltname, sizeof(sympltname),
302                                  "%s@plt", elf_sym__name(&sym, symstrs));
303
304                         f = symbol__new(plt_offset, shdr_plt.sh_entsize,
305                                         STB_GLOBAL, sympltname);
306                         if (!f)
307                                 goto out_elf_end;
308
309                         if (filter && filter(map, f))
310                                 symbol__delete(f);
311                         else {
312                                 symbols__insert(&dso->symbols[map->type], f);
313                                 ++nr;
314                         }
315                 }
316         } else if (shdr_rel_plt.sh_type == SHT_REL) {
317                 GElf_Rel pos_mem, *pos;
318                 elf_section__for_each_rel(reldata, pos, pos_mem, idx,
319                                           nr_rel_entries) {
320                         symidx = GELF_R_SYM(pos->r_info);
321                         plt_offset += shdr_plt.sh_entsize;
322                         gelf_getsym(syms, symidx, &sym);
323                         snprintf(sympltname, sizeof(sympltname),
324                                  "%s@plt", elf_sym__name(&sym, symstrs));
325
326                         f = symbol__new(plt_offset, shdr_plt.sh_entsize,
327                                         STB_GLOBAL, sympltname);
328                         if (!f)
329                                 goto out_elf_end;
330
331                         if (filter && filter(map, f))
332                                 symbol__delete(f);
333                         else {
334                                 symbols__insert(&dso->symbols[map->type], f);
335                                 ++nr;
336                         }
337                 }
338         }
339
340         err = 0;
341 out_elf_end:
342         if (err == 0)
343                 return nr;
344         pr_debug("%s: problems reading %s PLT info.\n",
345                  __func__, dso->long_name);
346         return 0;
347 }
348
349 /*
350  * Align offset to 4 bytes as needed for note name and descriptor data.
351  */
352 #define NOTE_ALIGN(n) (((n) + 3) & -4U)
353
354 static int elf_read_build_id(Elf *elf, void *bf, size_t size)
355 {
356         int err = -1;
357         GElf_Ehdr ehdr;
358         GElf_Shdr shdr;
359         Elf_Data *data;
360         Elf_Scn *sec;
361         Elf_Kind ek;
362         void *ptr;
363
364         if (size < BUILD_ID_SIZE)
365                 goto out;
366
367         ek = elf_kind(elf);
368         if (ek != ELF_K_ELF)
369                 goto out;
370
371         if (gelf_getehdr(elf, &ehdr) == NULL) {
372                 pr_err("%s: cannot get elf header.\n", __func__);
373                 goto out;
374         }
375
376         /*
377          * Check following sections for notes:
378          *   '.note.gnu.build-id'
379          *   '.notes'
380          *   '.note' (VDSO specific)
381          */
382         do {
383                 sec = elf_section_by_name(elf, &ehdr, &shdr,
384                                           ".note.gnu.build-id", NULL);
385                 if (sec)
386                         break;
387
388                 sec = elf_section_by_name(elf, &ehdr, &shdr,
389                                           ".notes", NULL);
390                 if (sec)
391                         break;
392
393                 sec = elf_section_by_name(elf, &ehdr, &shdr,
394                                           ".note", NULL);
395                 if (sec)
396                         break;
397
398                 return err;
399
400         } while (0);
401
402         data = elf_getdata(sec, NULL);
403         if (data == NULL)
404                 goto out;
405
406         ptr = data->d_buf;
407         while (ptr < (data->d_buf + data->d_size)) {
408                 GElf_Nhdr *nhdr = ptr;
409                 size_t namesz = NOTE_ALIGN(nhdr->n_namesz),
410                        descsz = NOTE_ALIGN(nhdr->n_descsz);
411                 const char *name;
412
413                 ptr += sizeof(*nhdr);
414                 name = ptr;
415                 ptr += namesz;
416                 if (nhdr->n_type == NT_GNU_BUILD_ID &&
417                     nhdr->n_namesz == sizeof("GNU")) {
418                         if (memcmp(name, "GNU", sizeof("GNU")) == 0) {
419                                 size_t sz = min(size, descsz);
420                                 memcpy(bf, ptr, sz);
421                                 memset(bf + sz, 0, size - sz);
422                                 err = descsz;
423                                 break;
424                         }
425                 }
426                 ptr += descsz;
427         }
428
429 out:
430         return err;
431 }
432
433 int filename__read_build_id(const char *filename, void *bf, size_t size)
434 {
435         int fd, err = -1;
436         Elf *elf;
437
438         if (size < BUILD_ID_SIZE)
439                 goto out;
440
441         fd = open(filename, O_RDONLY);
442         if (fd < 0)
443                 goto out;
444
445         elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
446         if (elf == NULL) {
447                 pr_debug2("%s: cannot read %s ELF file.\n", __func__, filename);
448                 goto out_close;
449         }
450
451         err = elf_read_build_id(elf, bf, size);
452
453         elf_end(elf);
454 out_close:
455         close(fd);
456 out:
457         return err;
458 }
459
460 int sysfs__read_build_id(const char *filename, void *build_id, size_t size)
461 {
462         int fd, err = -1;
463
464         if (size < BUILD_ID_SIZE)
465                 goto out;
466
467         fd = open(filename, O_RDONLY);
468         if (fd < 0)
469                 goto out;
470
471         while (1) {
472                 char bf[BUFSIZ];
473                 GElf_Nhdr nhdr;
474                 size_t namesz, descsz;
475
476                 if (read(fd, &nhdr, sizeof(nhdr)) != sizeof(nhdr))
477                         break;
478
479                 namesz = NOTE_ALIGN(nhdr.n_namesz);
480                 descsz = NOTE_ALIGN(nhdr.n_descsz);
481                 if (nhdr.n_type == NT_GNU_BUILD_ID &&
482                     nhdr.n_namesz == sizeof("GNU")) {
483                         if (read(fd, bf, namesz) != (ssize_t)namesz)
484                                 break;
485                         if (memcmp(bf, "GNU", sizeof("GNU")) == 0) {
486                                 size_t sz = min(descsz, size);
487                                 if (read(fd, build_id, sz) == (ssize_t)sz) {
488                                         memset(build_id + sz, 0, size - sz);
489                                         err = 0;
490                                         break;
491                                 }
492                         } else if (read(fd, bf, descsz) != (ssize_t)descsz)
493                                 break;
494                 } else {
495                         int n = namesz + descsz;
496                         if (read(fd, bf, n) != n)
497                                 break;
498                 }
499         }
500         close(fd);
501 out:
502         return err;
503 }
504
505 int filename__read_debuglink(const char *filename, char *debuglink,
506                              size_t size)
507 {
508         int fd, err = -1;
509         Elf *elf;
510         GElf_Ehdr ehdr;
511         GElf_Shdr shdr;
512         Elf_Data *data;
513         Elf_Scn *sec;
514         Elf_Kind ek;
515
516         fd = open(filename, O_RDONLY);
517         if (fd < 0)
518                 goto out;
519
520         elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
521         if (elf == NULL) {
522                 pr_debug2("%s: cannot read %s ELF file.\n", __func__, filename);
523                 goto out_close;
524         }
525
526         ek = elf_kind(elf);
527         if (ek != ELF_K_ELF)
528                 goto out_elf_end;
529
530         if (gelf_getehdr(elf, &ehdr) == NULL) {
531                 pr_err("%s: cannot get elf header.\n", __func__);
532                 goto out_elf_end;
533         }
534
535         sec = elf_section_by_name(elf, &ehdr, &shdr,
536                                   ".gnu_debuglink", NULL);
537         if (sec == NULL)
538                 goto out_elf_end;
539
540         data = elf_getdata(sec, NULL);
541         if (data == NULL)
542                 goto out_elf_end;
543
544         /* the start of this section is a zero-terminated string */
545         strncpy(debuglink, data->d_buf, size);
546
547         err = 0;
548
549 out_elf_end:
550         elf_end(elf);
551 out_close:
552         close(fd);
553 out:
554         return err;
555 }
556
557 static int dso__swap_init(struct dso *dso, unsigned char eidata)
558 {
559         static unsigned int const endian = 1;
560
561         dso->needs_swap = DSO_SWAP__NO;
562
563         switch (eidata) {
564         case ELFDATA2LSB:
565                 /* We are big endian, DSO is little endian. */
566                 if (*(unsigned char const *)&endian != 1)
567                         dso->needs_swap = DSO_SWAP__YES;
568                 break;
569
570         case ELFDATA2MSB:
571                 /* We are little endian, DSO is big endian. */
572                 if (*(unsigned char const *)&endian != 0)
573                         dso->needs_swap = DSO_SWAP__YES;
574                 break;
575
576         default:
577                 pr_err("unrecognized DSO data encoding %d\n", eidata);
578                 return -EINVAL;
579         }
580
581         return 0;
582 }
583
584 static int decompress_kmodule(struct dso *dso, const char *name,
585                               enum dso_binary_type type)
586 {
587         int fd = -1;
588         char tmpbuf[] = "/tmp/perf-kmod-XXXXXX";
589         struct kmod_path m;
590
591         if (type != DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP &&
592             type != DSO_BINARY_TYPE__GUEST_KMODULE_COMP &&
593             type != DSO_BINARY_TYPE__BUILD_ID_CACHE)
594                 return -1;
595
596         if (type == DSO_BINARY_TYPE__BUILD_ID_CACHE)
597                 name = dso->long_name;
598
599         if (kmod_path__parse_ext(&m, name) || !m.comp)
600                 return -1;
601
602         fd = mkstemp(tmpbuf);
603         if (fd < 0) {
604                 dso->load_errno = errno;
605                 goto out;
606         }
607
608         if (!decompress_to_file(m.ext, name, fd)) {
609                 dso->load_errno = DSO_LOAD_ERRNO__DECOMPRESSION_FAILURE;
610                 close(fd);
611                 fd = -1;
612         }
613
614         unlink(tmpbuf);
615
616 out:
617         free(m.ext);
618         return fd;
619 }
620
621 bool symsrc__possibly_runtime(struct symsrc *ss)
622 {
623         return ss->dynsym || ss->opdsec;
624 }
625
626 bool symsrc__has_symtab(struct symsrc *ss)
627 {
628         return ss->symtab != NULL;
629 }
630
631 void symsrc__destroy(struct symsrc *ss)
632 {
633         zfree(&ss->name);
634         elf_end(ss->elf);
635         close(ss->fd);
636 }
637
638 bool __weak elf__needs_adjust_symbols(GElf_Ehdr ehdr)
639 {
640         return ehdr.e_type == ET_EXEC || ehdr.e_type == ET_REL;
641 }
642
643 int symsrc__init(struct symsrc *ss, struct dso *dso, const char *name,
644                  enum dso_binary_type type)
645 {
646         int err = -1;
647         GElf_Ehdr ehdr;
648         Elf *elf;
649         int fd;
650
651         if (dso__needs_decompress(dso)) {
652                 fd = decompress_kmodule(dso, name, type);
653                 if (fd < 0)
654                         return -1;
655         } else {
656                 fd = open(name, O_RDONLY);
657                 if (fd < 0) {
658                         dso->load_errno = errno;
659                         return -1;
660                 }
661         }
662
663         elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
664         if (elf == NULL) {
665                 pr_debug("%s: cannot read %s ELF file.\n", __func__, name);
666                 dso->load_errno = DSO_LOAD_ERRNO__INVALID_ELF;
667                 goto out_close;
668         }
669
670         if (gelf_getehdr(elf, &ehdr) == NULL) {
671                 dso->load_errno = DSO_LOAD_ERRNO__INVALID_ELF;
672                 pr_debug("%s: cannot get elf header.\n", __func__);
673                 goto out_elf_end;
674         }
675
676         if (dso__swap_init(dso, ehdr.e_ident[EI_DATA])) {
677                 dso->load_errno = DSO_LOAD_ERRNO__INTERNAL_ERROR;
678                 goto out_elf_end;
679         }
680
681         /* Always reject images with a mismatched build-id: */
682         if (dso->has_build_id) {
683                 u8 build_id[BUILD_ID_SIZE];
684
685                 if (elf_read_build_id(elf, build_id, BUILD_ID_SIZE) < 0) {
686                         dso->load_errno = DSO_LOAD_ERRNO__CANNOT_READ_BUILDID;
687                         goto out_elf_end;
688                 }
689
690                 if (!dso__build_id_equal(dso, build_id)) {
691                         pr_debug("%s: build id mismatch for %s.\n", __func__, name);
692                         dso->load_errno = DSO_LOAD_ERRNO__MISMATCHING_BUILDID;
693                         goto out_elf_end;
694                 }
695         }
696
697         ss->is_64_bit = (gelf_getclass(elf) == ELFCLASS64);
698
699         ss->symtab = elf_section_by_name(elf, &ehdr, &ss->symshdr, ".symtab",
700                         NULL);
701         if (ss->symshdr.sh_type != SHT_SYMTAB)
702                 ss->symtab = NULL;
703
704         ss->dynsym_idx = 0;
705         ss->dynsym = elf_section_by_name(elf, &ehdr, &ss->dynshdr, ".dynsym",
706                         &ss->dynsym_idx);
707         if (ss->dynshdr.sh_type != SHT_DYNSYM)
708                 ss->dynsym = NULL;
709
710         ss->opdidx = 0;
711         ss->opdsec = elf_section_by_name(elf, &ehdr, &ss->opdshdr, ".opd",
712                         &ss->opdidx);
713         if (ss->opdshdr.sh_type != SHT_PROGBITS)
714                 ss->opdsec = NULL;
715
716         if (dso->kernel == DSO_TYPE_USER) {
717                 GElf_Shdr shdr;
718                 ss->adjust_symbols = (ehdr.e_type == ET_EXEC ||
719                                 ehdr.e_type == ET_REL ||
720                                 dso__is_vdso(dso) ||
721                                 elf_section_by_name(elf, &ehdr, &shdr,
722                                                      ".gnu.prelink_undo",
723                                                      NULL) != NULL);
724         } else {
725                 ss->adjust_symbols = elf__needs_adjust_symbols(ehdr);
726         }
727
728         ss->name   = strdup(name);
729         if (!ss->name) {
730                 dso->load_errno = errno;
731                 goto out_elf_end;
732         }
733
734         ss->elf    = elf;
735         ss->fd     = fd;
736         ss->ehdr   = ehdr;
737         ss->type   = type;
738
739         return 0;
740
741 out_elf_end:
742         elf_end(elf);
743 out_close:
744         close(fd);
745         return err;
746 }
747
748 /**
749  * ref_reloc_sym_not_found - has kernel relocation symbol been found.
750  * @kmap: kernel maps and relocation reference symbol
751  *
752  * This function returns %true if we are dealing with the kernel maps and the
753  * relocation reference symbol has not yet been found.  Otherwise %false is
754  * returned.
755  */
756 static bool ref_reloc_sym_not_found(struct kmap *kmap)
757 {
758         return kmap && kmap->ref_reloc_sym && kmap->ref_reloc_sym->name &&
759                !kmap->ref_reloc_sym->unrelocated_addr;
760 }
761
762 /**
763  * ref_reloc - kernel relocation offset.
764  * @kmap: kernel maps and relocation reference symbol
765  *
766  * This function returns the offset of kernel addresses as determined by using
767  * the relocation reference symbol i.e. if the kernel has not been relocated
768  * then the return value is zero.
769  */
770 static u64 ref_reloc(struct kmap *kmap)
771 {
772         if (kmap && kmap->ref_reloc_sym &&
773             kmap->ref_reloc_sym->unrelocated_addr)
774                 return kmap->ref_reloc_sym->addr -
775                        kmap->ref_reloc_sym->unrelocated_addr;
776         return 0;
777 }
778
779 static bool want_demangle(bool is_kernel_sym)
780 {
781         return is_kernel_sym ? symbol_conf.demangle_kernel : symbol_conf.demangle;
782 }
783
784 void __weak arch__elf_sym_adjust(GElf_Sym *sym __maybe_unused) { }
785
786 int dso__load_sym(struct dso *dso, struct map *map,
787                   struct symsrc *syms_ss, struct symsrc *runtime_ss,
788                   symbol_filter_t filter, int kmodule)
789 {
790         struct kmap *kmap = dso->kernel ? map__kmap(map) : NULL;
791         struct map_groups *kmaps = kmap ? map__kmaps(map) : NULL;
792         struct map *curr_map = map;
793         struct dso *curr_dso = dso;
794         Elf_Data *symstrs, *secstrs;
795         uint32_t nr_syms;
796         int err = -1;
797         uint32_t idx;
798         GElf_Ehdr ehdr;
799         GElf_Shdr shdr;
800         Elf_Data *syms, *opddata = NULL;
801         GElf_Sym sym;
802         Elf_Scn *sec, *sec_strndx;
803         Elf *elf;
804         int nr = 0;
805         bool remap_kernel = false, adjust_kernel_syms = false;
806
807         if (kmap && !kmaps)
808                 return -1;
809
810         dso->symtab_type = syms_ss->type;
811         dso->is_64_bit = syms_ss->is_64_bit;
812         dso->rel = syms_ss->ehdr.e_type == ET_REL;
813
814         /*
815          * Modules may already have symbols from kallsyms, but those symbols
816          * have the wrong values for the dso maps, so remove them.
817          */
818         if (kmodule && syms_ss->symtab)
819                 symbols__delete(&dso->symbols[map->type]);
820
821         if (!syms_ss->symtab) {
822                 /*
823                  * If the vmlinux is stripped, fail so we will fall back
824                  * to using kallsyms. The vmlinux runtime symbols aren't
825                  * of much use.
826                  */
827                 if (dso->kernel)
828                         goto out_elf_end;
829
830                 syms_ss->symtab  = syms_ss->dynsym;
831                 syms_ss->symshdr = syms_ss->dynshdr;
832         }
833
834         elf = syms_ss->elf;
835         ehdr = syms_ss->ehdr;
836         sec = syms_ss->symtab;
837         shdr = syms_ss->symshdr;
838
839         if (runtime_ss->opdsec)
840                 opddata = elf_rawdata(runtime_ss->opdsec, NULL);
841
842         syms = elf_getdata(sec, NULL);
843         if (syms == NULL)
844                 goto out_elf_end;
845
846         sec = elf_getscn(elf, shdr.sh_link);
847         if (sec == NULL)
848                 goto out_elf_end;
849
850         symstrs = elf_getdata(sec, NULL);
851         if (symstrs == NULL)
852                 goto out_elf_end;
853
854         sec_strndx = elf_getscn(runtime_ss->elf, runtime_ss->ehdr.e_shstrndx);
855         if (sec_strndx == NULL)
856                 goto out_elf_end;
857
858         secstrs = elf_getdata(sec_strndx, NULL);
859         if (secstrs == NULL)
860                 goto out_elf_end;
861
862         nr_syms = shdr.sh_size / shdr.sh_entsize;
863
864         memset(&sym, 0, sizeof(sym));
865
866         /*
867          * The kernel relocation symbol is needed in advance in order to adjust
868          * kernel maps correctly.
869          */
870         if (ref_reloc_sym_not_found(kmap)) {
871                 elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) {
872                         const char *elf_name = elf_sym__name(&sym, symstrs);
873
874                         if (strcmp(elf_name, kmap->ref_reloc_sym->name))
875                                 continue;
876                         kmap->ref_reloc_sym->unrelocated_addr = sym.st_value;
877                         map->reloc = kmap->ref_reloc_sym->addr -
878                                      kmap->ref_reloc_sym->unrelocated_addr;
879                         break;
880                 }
881         }
882
883         /*
884          * Handle any relocation of vdso necessary because older kernels
885          * attempted to prelink vdso to its virtual address.
886          */
887         if (dso__is_vdso(dso)) {
888                 GElf_Shdr tshdr;
889
890                 if (elf_section_by_name(elf, &ehdr, &tshdr, ".text", NULL))
891                         map->reloc = map->start - tshdr.sh_addr + tshdr.sh_offset;
892         }
893
894         dso->adjust_symbols = runtime_ss->adjust_symbols || ref_reloc(kmap);
895         /*
896          * Initial kernel and module mappings do not map to the dso.  For
897          * function mappings, flag the fixups.
898          */
899         if (map->type == MAP__FUNCTION && (dso->kernel || kmodule)) {
900                 remap_kernel = true;
901                 adjust_kernel_syms = dso->adjust_symbols;
902         }
903         elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) {
904                 struct symbol *f;
905                 const char *elf_name = elf_sym__name(&sym, symstrs);
906                 char *demangled = NULL;
907                 int is_label = elf_sym__is_label(&sym);
908                 const char *section_name;
909                 bool used_opd = false;
910
911                 if (!is_label && !elf_sym__is_a(&sym, map->type))
912                         continue;
913
914                 /* Reject ARM ELF "mapping symbols": these aren't unique and
915                  * don't identify functions, so will confuse the profile
916                  * output: */
917                 if (ehdr.e_machine == EM_ARM || ehdr.e_machine == EM_AARCH64) {
918                         if (elf_name[0] == '$' && strchr("adtx", elf_name[1])
919                             && (elf_name[2] == '\0' || elf_name[2] == '.'))
920                                 continue;
921                 }
922
923                 if (runtime_ss->opdsec && sym.st_shndx == runtime_ss->opdidx) {
924                         u32 offset = sym.st_value - syms_ss->opdshdr.sh_addr;
925                         u64 *opd = opddata->d_buf + offset;
926                         sym.st_value = DSO__SWAP(dso, u64, *opd);
927                         sym.st_shndx = elf_addr_to_index(runtime_ss->elf,
928                                         sym.st_value);
929                         used_opd = true;
930                 }
931                 /*
932                  * When loading symbols in a data mapping, ABS symbols (which
933                  * has a value of SHN_ABS in its st_shndx) failed at
934                  * elf_getscn().  And it marks the loading as a failure so
935                  * already loaded symbols cannot be fixed up.
936                  *
937                  * I'm not sure what should be done. Just ignore them for now.
938                  * - Namhyung Kim
939                  */
940                 if (sym.st_shndx == SHN_ABS)
941                         continue;
942
943                 sec = elf_getscn(runtime_ss->elf, sym.st_shndx);
944                 if (!sec)
945                         goto out_elf_end;
946
947                 gelf_getshdr(sec, &shdr);
948
949                 if (is_label && !elf_sec__is_a(&shdr, secstrs, map->type))
950                         continue;
951
952                 section_name = elf_sec__name(&shdr, secstrs);
953
954                 /* On ARM, symbols for thumb functions have 1 added to
955                  * the symbol address as a flag - remove it */
956                 if ((ehdr.e_machine == EM_ARM) &&
957                     (map->type == MAP__FUNCTION) &&
958                     (sym.st_value & 1))
959                         --sym.st_value;
960
961                 arch__elf_sym_adjust(&sym);
962
963                 if (dso->kernel || kmodule) {
964                         char dso_name[PATH_MAX];
965
966                         /* Adjust symbol to map to file offset */
967                         if (adjust_kernel_syms)
968                                 sym.st_value -= shdr.sh_addr - shdr.sh_offset;
969
970                         if (strcmp(section_name,
971                                    (curr_dso->short_name +
972                                     dso->short_name_len)) == 0)
973                                 goto new_symbol;
974
975                         if (strcmp(section_name, ".text") == 0) {
976                                 /*
977                                  * The initial kernel mapping is based on
978                                  * kallsyms and identity maps.  Overwrite it to
979                                  * map to the kernel dso.
980                                  */
981                                 if (remap_kernel && dso->kernel) {
982                                         remap_kernel = false;
983                                         map->start = shdr.sh_addr +
984                                                      ref_reloc(kmap);
985                                         map->end = map->start + shdr.sh_size;
986                                         map->pgoff = shdr.sh_offset;
987                                         map->map_ip = map__map_ip;
988                                         map->unmap_ip = map__unmap_ip;
989                                         /* Ensure maps are correctly ordered */
990                                         if (kmaps) {
991                                                 map__get(map);
992                                                 map_groups__remove(kmaps, map);
993                                                 map_groups__insert(kmaps, map);
994                                                 map__put(map);
995                                         }
996                                 }
997
998                                 /*
999                                  * The initial module mapping is based on
1000                                  * /proc/modules mapped to offset zero.
1001                                  * Overwrite it to map to the module dso.
1002                                  */
1003                                 if (remap_kernel && kmodule) {
1004                                         remap_kernel = false;
1005                                         map->pgoff = shdr.sh_offset;
1006                                 }
1007
1008                                 curr_map = map;
1009                                 curr_dso = dso;
1010                                 goto new_symbol;
1011                         }
1012
1013                         if (!kmap)
1014                                 goto new_symbol;
1015
1016                         snprintf(dso_name, sizeof(dso_name),
1017                                  "%s%s", dso->short_name, section_name);
1018
1019                         curr_map = map_groups__find_by_name(kmaps, map->type, dso_name);
1020                         if (curr_map == NULL) {
1021                                 u64 start = sym.st_value;
1022
1023                                 if (kmodule)
1024                                         start += map->start + shdr.sh_offset;
1025
1026                                 curr_dso = dso__new(dso_name);
1027                                 if (curr_dso == NULL)
1028                                         goto out_elf_end;
1029                                 curr_dso->kernel = dso->kernel;
1030                                 curr_dso->long_name = dso->long_name;
1031                                 curr_dso->long_name_len = dso->long_name_len;
1032                                 curr_map = map__new2(start, curr_dso,
1033                                                      map->type);
1034                                 dso__put(curr_dso);
1035                                 if (curr_map == NULL) {
1036                                         goto out_elf_end;
1037                                 }
1038                                 if (adjust_kernel_syms) {
1039                                         curr_map->start = shdr.sh_addr +
1040                                                           ref_reloc(kmap);
1041                                         curr_map->end = curr_map->start +
1042                                                         shdr.sh_size;
1043                                         curr_map->pgoff = shdr.sh_offset;
1044                                 } else {
1045                                         curr_map->map_ip = identity__map_ip;
1046                                         curr_map->unmap_ip = identity__map_ip;
1047                                 }
1048                                 curr_dso->symtab_type = dso->symtab_type;
1049                                 map_groups__insert(kmaps, curr_map);
1050                                 /*
1051                                  * Add it before we drop the referece to curr_map,
1052                                  * i.e. while we still are sure to have a reference
1053                                  * to this DSO via curr_map->dso.
1054                                  */
1055                                 dsos__add(&map->groups->machine->dsos, curr_dso);
1056                                 /* kmaps already got it */
1057                                 map__put(curr_map);
1058                                 dso__set_loaded(curr_dso, map->type);
1059                         } else
1060                                 curr_dso = curr_map->dso;
1061
1062                         goto new_symbol;
1063                 }
1064
1065                 if ((used_opd && runtime_ss->adjust_symbols)
1066                                 || (!used_opd && syms_ss->adjust_symbols)) {
1067                         pr_debug4("%s: adjusting symbol: st_value: %#" PRIx64 " "
1068                                   "sh_addr: %#" PRIx64 " sh_offset: %#" PRIx64 "\n", __func__,
1069                                   (u64)sym.st_value, (u64)shdr.sh_addr,
1070                                   (u64)shdr.sh_offset);
1071                         sym.st_value -= shdr.sh_addr - shdr.sh_offset;
1072                 }
1073 new_symbol:
1074                 /*
1075                  * We need to figure out if the object was created from C++ sources
1076                  * DWARF DW_compile_unit has this, but we don't always have access
1077                  * to it...
1078                  */
1079                 if (want_demangle(dso->kernel || kmodule)) {
1080                         int demangle_flags = DMGL_NO_OPTS;
1081                         if (verbose)
1082                                 demangle_flags = DMGL_PARAMS | DMGL_ANSI;
1083
1084                         demangled = bfd_demangle(NULL, elf_name, demangle_flags);
1085                         if (demangled != NULL)
1086                                 elf_name = demangled;
1087                 }
1088                 f = symbol__new(sym.st_value, sym.st_size,
1089                                 GELF_ST_BIND(sym.st_info), elf_name);
1090                 free(demangled);
1091                 if (!f)
1092                         goto out_elf_end;
1093
1094                 if (filter && filter(curr_map, f))
1095                         symbol__delete(f);
1096                 else {
1097                         symbols__insert(&curr_dso->symbols[curr_map->type], f);
1098                         nr++;
1099                 }
1100         }
1101
1102         /*
1103          * For misannotated, zeroed, ASM function sizes.
1104          */
1105         if (nr > 0) {
1106                 if (!symbol_conf.allow_aliases)
1107                         symbols__fixup_duplicate(&dso->symbols[map->type]);
1108                 symbols__fixup_end(&dso->symbols[map->type]);
1109                 if (kmap) {
1110                         /*
1111                          * We need to fixup this here too because we create new
1112                          * maps here, for things like vsyscall sections.
1113                          */
1114                         __map_groups__fixup_end(kmaps, map->type);
1115                 }
1116         }
1117         err = nr;
1118 out_elf_end:
1119         return err;
1120 }
1121
1122 static int elf_read_maps(Elf *elf, bool exe, mapfn_t mapfn, void *data)
1123 {
1124         GElf_Phdr phdr;
1125         size_t i, phdrnum;
1126         int err;
1127         u64 sz;
1128
1129         if (elf_getphdrnum(elf, &phdrnum))
1130                 return -1;
1131
1132         for (i = 0; i < phdrnum; i++) {
1133                 if (gelf_getphdr(elf, i, &phdr) == NULL)
1134                         return -1;
1135                 if (phdr.p_type != PT_LOAD)
1136                         continue;
1137                 if (exe) {
1138                         if (!(phdr.p_flags & PF_X))
1139                                 continue;
1140                 } else {
1141                         if (!(phdr.p_flags & PF_R))
1142                                 continue;
1143                 }
1144                 sz = min(phdr.p_memsz, phdr.p_filesz);
1145                 if (!sz)
1146                         continue;
1147                 err = mapfn(phdr.p_vaddr, sz, phdr.p_offset, data);
1148                 if (err)
1149                         return err;
1150         }
1151         return 0;
1152 }
1153
1154 int file__read_maps(int fd, bool exe, mapfn_t mapfn, void *data,
1155                     bool *is_64_bit)
1156 {
1157         int err;
1158         Elf *elf;
1159
1160         elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
1161         if (elf == NULL)
1162                 return -1;
1163
1164         if (is_64_bit)
1165                 *is_64_bit = (gelf_getclass(elf) == ELFCLASS64);
1166
1167         err = elf_read_maps(elf, exe, mapfn, data);
1168
1169         elf_end(elf);
1170         return err;
1171 }
1172
1173 enum dso_type dso__type_fd(int fd)
1174 {
1175         enum dso_type dso_type = DSO__TYPE_UNKNOWN;
1176         GElf_Ehdr ehdr;
1177         Elf_Kind ek;
1178         Elf *elf;
1179
1180         elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
1181         if (elf == NULL)
1182                 goto out;
1183
1184         ek = elf_kind(elf);
1185         if (ek != ELF_K_ELF)
1186                 goto out_end;
1187
1188         if (gelf_getclass(elf) == ELFCLASS64) {
1189                 dso_type = DSO__TYPE_64BIT;
1190                 goto out_end;
1191         }
1192
1193         if (gelf_getehdr(elf, &ehdr) == NULL)
1194                 goto out_end;
1195
1196         if (ehdr.e_machine == EM_X86_64)
1197                 dso_type = DSO__TYPE_X32BIT;
1198         else
1199                 dso_type = DSO__TYPE_32BIT;
1200 out_end:
1201         elf_end(elf);
1202 out:
1203         return dso_type;
1204 }
1205
1206 static int copy_bytes(int from, off_t from_offs, int to, off_t to_offs, u64 len)
1207 {
1208         ssize_t r;
1209         size_t n;
1210         int err = -1;
1211         char *buf = malloc(page_size);
1212
1213         if (buf == NULL)
1214                 return -1;
1215
1216         if (lseek(to, to_offs, SEEK_SET) != to_offs)
1217                 goto out;
1218
1219         if (lseek(from, from_offs, SEEK_SET) != from_offs)
1220                 goto out;
1221
1222         while (len) {
1223                 n = page_size;
1224                 if (len < n)
1225                         n = len;
1226                 /* Use read because mmap won't work on proc files */
1227                 r = read(from, buf, n);
1228                 if (r < 0)
1229                         goto out;
1230                 if (!r)
1231                         break;
1232                 n = r;
1233                 r = write(to, buf, n);
1234                 if (r < 0)
1235                         goto out;
1236                 if ((size_t)r != n)
1237                         goto out;
1238                 len -= n;
1239         }
1240
1241         err = 0;
1242 out:
1243         free(buf);
1244         return err;
1245 }
1246
1247 struct kcore {
1248         int fd;
1249         int elfclass;
1250         Elf *elf;
1251         GElf_Ehdr ehdr;
1252 };
1253
1254 static int kcore__open(struct kcore *kcore, const char *filename)
1255 {
1256         GElf_Ehdr *ehdr;
1257
1258         kcore->fd = open(filename, O_RDONLY);
1259         if (kcore->fd == -1)
1260                 return -1;
1261
1262         kcore->elf = elf_begin(kcore->fd, ELF_C_READ, NULL);
1263         if (!kcore->elf)
1264                 goto out_close;
1265
1266         kcore->elfclass = gelf_getclass(kcore->elf);
1267         if (kcore->elfclass == ELFCLASSNONE)
1268                 goto out_end;
1269
1270         ehdr = gelf_getehdr(kcore->elf, &kcore->ehdr);
1271         if (!ehdr)
1272                 goto out_end;
1273
1274         return 0;
1275
1276 out_end:
1277         elf_end(kcore->elf);
1278 out_close:
1279         close(kcore->fd);
1280         return -1;
1281 }
1282
1283 static int kcore__init(struct kcore *kcore, char *filename, int elfclass,
1284                        bool temp)
1285 {
1286         kcore->elfclass = elfclass;
1287
1288         if (temp)
1289                 kcore->fd = mkstemp(filename);
1290         else
1291                 kcore->fd = open(filename, O_WRONLY | O_CREAT | O_EXCL, 0400);
1292         if (kcore->fd == -1)
1293                 return -1;
1294
1295         kcore->elf = elf_begin(kcore->fd, ELF_C_WRITE, NULL);
1296         if (!kcore->elf)
1297                 goto out_close;
1298
1299         if (!gelf_newehdr(kcore->elf, elfclass))
1300                 goto out_end;
1301
1302         memset(&kcore->ehdr, 0, sizeof(GElf_Ehdr));
1303
1304         return 0;
1305
1306 out_end:
1307         elf_end(kcore->elf);
1308 out_close:
1309         close(kcore->fd);
1310         unlink(filename);
1311         return -1;
1312 }
1313
1314 static void kcore__close(struct kcore *kcore)
1315 {
1316         elf_end(kcore->elf);
1317         close(kcore->fd);
1318 }
1319
1320 static int kcore__copy_hdr(struct kcore *from, struct kcore *to, size_t count)
1321 {
1322         GElf_Ehdr *ehdr = &to->ehdr;
1323         GElf_Ehdr *kehdr = &from->ehdr;
1324
1325         memcpy(ehdr->e_ident, kehdr->e_ident, EI_NIDENT);
1326         ehdr->e_type      = kehdr->e_type;
1327         ehdr->e_machine   = kehdr->e_machine;
1328         ehdr->e_version   = kehdr->e_version;
1329         ehdr->e_entry     = 0;
1330         ehdr->e_shoff     = 0;
1331         ehdr->e_flags     = kehdr->e_flags;
1332         ehdr->e_phnum     = count;
1333         ehdr->e_shentsize = 0;
1334         ehdr->e_shnum     = 0;
1335         ehdr->e_shstrndx  = 0;
1336
1337         if (from->elfclass == ELFCLASS32) {
1338                 ehdr->e_phoff     = sizeof(Elf32_Ehdr);
1339                 ehdr->e_ehsize    = sizeof(Elf32_Ehdr);
1340                 ehdr->e_phentsize = sizeof(Elf32_Phdr);
1341         } else {
1342                 ehdr->e_phoff     = sizeof(Elf64_Ehdr);
1343                 ehdr->e_ehsize    = sizeof(Elf64_Ehdr);
1344                 ehdr->e_phentsize = sizeof(Elf64_Phdr);
1345         }
1346
1347         if (!gelf_update_ehdr(to->elf, ehdr))
1348                 return -1;
1349
1350         if (!gelf_newphdr(to->elf, count))
1351                 return -1;
1352
1353         return 0;
1354 }
1355
1356 static int kcore__add_phdr(struct kcore *kcore, int idx, off_t offset,
1357                            u64 addr, u64 len)
1358 {
1359         GElf_Phdr phdr = {
1360                 .p_type         = PT_LOAD,
1361                 .p_flags        = PF_R | PF_W | PF_X,
1362                 .p_offset       = offset,
1363                 .p_vaddr        = addr,
1364                 .p_paddr        = 0,
1365                 .p_filesz       = len,
1366                 .p_memsz        = len,
1367                 .p_align        = page_size,
1368         };
1369
1370         if (!gelf_update_phdr(kcore->elf, idx, &phdr))
1371                 return -1;
1372
1373         return 0;
1374 }
1375
1376 static off_t kcore__write(struct kcore *kcore)
1377 {
1378         return elf_update(kcore->elf, ELF_C_WRITE);
1379 }
1380
1381 struct phdr_data {
1382         off_t offset;
1383         u64 addr;
1384         u64 len;
1385 };
1386
1387 struct kcore_copy_info {
1388         u64 stext;
1389         u64 etext;
1390         u64 first_symbol;
1391         u64 last_symbol;
1392         u64 first_module;
1393         u64 last_module_symbol;
1394         struct phdr_data kernel_map;
1395         struct phdr_data modules_map;
1396 };
1397
1398 static int kcore_copy__process_kallsyms(void *arg, const char *name, char type,
1399                                         u64 start)
1400 {
1401         struct kcore_copy_info *kci = arg;
1402
1403         if (!symbol_type__is_a(type, MAP__FUNCTION))
1404                 return 0;
1405
1406         if (strchr(name, '[')) {
1407                 if (start > kci->last_module_symbol)
1408                         kci->last_module_symbol = start;
1409                 return 0;
1410         }
1411
1412         if (!kci->first_symbol || start < kci->first_symbol)
1413                 kci->first_symbol = start;
1414
1415         if (!kci->last_symbol || start > kci->last_symbol)
1416                 kci->last_symbol = start;
1417
1418         if (!strcmp(name, "_stext")) {
1419                 kci->stext = start;
1420                 return 0;
1421         }
1422
1423         if (!strcmp(name, "_etext")) {
1424                 kci->etext = start;
1425                 return 0;
1426         }
1427
1428         return 0;
1429 }
1430
1431 static int kcore_copy__parse_kallsyms(struct kcore_copy_info *kci,
1432                                       const char *dir)
1433 {
1434         char kallsyms_filename[PATH_MAX];
1435
1436         scnprintf(kallsyms_filename, PATH_MAX, "%s/kallsyms", dir);
1437
1438         if (symbol__restricted_filename(kallsyms_filename, "/proc/kallsyms"))
1439                 return -1;
1440
1441         if (kallsyms__parse(kallsyms_filename, kci,
1442                             kcore_copy__process_kallsyms) < 0)
1443                 return -1;
1444
1445         return 0;
1446 }
1447
1448 static int kcore_copy__process_modules(void *arg,
1449                                        const char *name __maybe_unused,
1450                                        u64 start)
1451 {
1452         struct kcore_copy_info *kci = arg;
1453
1454         if (!kci->first_module || start < kci->first_module)
1455                 kci->first_module = start;
1456
1457         return 0;
1458 }
1459
1460 static int kcore_copy__parse_modules(struct kcore_copy_info *kci,
1461                                      const char *dir)
1462 {
1463         char modules_filename[PATH_MAX];
1464
1465         scnprintf(modules_filename, PATH_MAX, "%s/modules", dir);
1466
1467         if (symbol__restricted_filename(modules_filename, "/proc/modules"))
1468                 return -1;
1469
1470         if (modules__parse(modules_filename, kci,
1471                            kcore_copy__process_modules) < 0)
1472                 return -1;
1473
1474         return 0;
1475 }
1476
1477 static void kcore_copy__map(struct phdr_data *p, u64 start, u64 end, u64 pgoff,
1478                             u64 s, u64 e)
1479 {
1480         if (p->addr || s < start || s >= end)
1481                 return;
1482
1483         p->addr = s;
1484         p->offset = (s - start) + pgoff;
1485         p->len = e < end ? e - s : end - s;
1486 }
1487
1488 static int kcore_copy__read_map(u64 start, u64 len, u64 pgoff, void *data)
1489 {
1490         struct kcore_copy_info *kci = data;
1491         u64 end = start + len;
1492
1493         kcore_copy__map(&kci->kernel_map, start, end, pgoff, kci->stext,
1494                         kci->etext);
1495
1496         kcore_copy__map(&kci->modules_map, start, end, pgoff, kci->first_module,
1497                         kci->last_module_symbol);
1498
1499         return 0;
1500 }
1501
1502 static int kcore_copy__read_maps(struct kcore_copy_info *kci, Elf *elf)
1503 {
1504         if (elf_read_maps(elf, true, kcore_copy__read_map, kci) < 0)
1505                 return -1;
1506
1507         return 0;
1508 }
1509
1510 static int kcore_copy__calc_maps(struct kcore_copy_info *kci, const char *dir,
1511                                  Elf *elf)
1512 {
1513         if (kcore_copy__parse_kallsyms(kci, dir))
1514                 return -1;
1515
1516         if (kcore_copy__parse_modules(kci, dir))
1517                 return -1;
1518
1519         if (kci->stext)
1520                 kci->stext = round_down(kci->stext, page_size);
1521         else
1522                 kci->stext = round_down(kci->first_symbol, page_size);
1523
1524         if (kci->etext) {
1525                 kci->etext = round_up(kci->etext, page_size);
1526         } else if (kci->last_symbol) {
1527                 kci->etext = round_up(kci->last_symbol, page_size);
1528                 kci->etext += page_size;
1529         }
1530
1531         kci->first_module = round_down(kci->first_module, page_size);
1532
1533         if (kci->last_module_symbol) {
1534                 kci->last_module_symbol = round_up(kci->last_module_symbol,
1535                                                    page_size);
1536                 kci->last_module_symbol += page_size;
1537         }
1538
1539         if (!kci->stext || !kci->etext)
1540                 return -1;
1541
1542         if (kci->first_module && !kci->last_module_symbol)
1543                 return -1;
1544
1545         return kcore_copy__read_maps(kci, elf);
1546 }
1547
1548 static int kcore_copy__copy_file(const char *from_dir, const char *to_dir,
1549                                  const char *name)
1550 {
1551         char from_filename[PATH_MAX];
1552         char to_filename[PATH_MAX];
1553
1554         scnprintf(from_filename, PATH_MAX, "%s/%s", from_dir, name);
1555         scnprintf(to_filename, PATH_MAX, "%s/%s", to_dir, name);
1556
1557         return copyfile_mode(from_filename, to_filename, 0400);
1558 }
1559
1560 static int kcore_copy__unlink(const char *dir, const char *name)
1561 {
1562         char filename[PATH_MAX];
1563
1564         scnprintf(filename, PATH_MAX, "%s/%s", dir, name);
1565
1566         return unlink(filename);
1567 }
1568
1569 static int kcore_copy__compare_fds(int from, int to)
1570 {
1571         char *buf_from;
1572         char *buf_to;
1573         ssize_t ret;
1574         size_t len;
1575         int err = -1;
1576
1577         buf_from = malloc(page_size);
1578         buf_to = malloc(page_size);
1579         if (!buf_from || !buf_to)
1580                 goto out;
1581
1582         while (1) {
1583                 /* Use read because mmap won't work on proc files */
1584                 ret = read(from, buf_from, page_size);
1585                 if (ret < 0)
1586                         goto out;
1587
1588                 if (!ret)
1589                         break;
1590
1591                 len = ret;
1592
1593                 if (readn(to, buf_to, len) != (int)len)
1594                         goto out;
1595
1596                 if (memcmp(buf_from, buf_to, len))
1597                         goto out;
1598         }
1599
1600         err = 0;
1601 out:
1602         free(buf_to);
1603         free(buf_from);
1604         return err;
1605 }
1606
1607 static int kcore_copy__compare_files(const char *from_filename,
1608                                      const char *to_filename)
1609 {
1610         int from, to, err = -1;
1611
1612         from = open(from_filename, O_RDONLY);
1613         if (from < 0)
1614                 return -1;
1615
1616         to = open(to_filename, O_RDONLY);
1617         if (to < 0)
1618                 goto out_close_from;
1619
1620         err = kcore_copy__compare_fds(from, to);
1621
1622         close(to);
1623 out_close_from:
1624         close(from);
1625         return err;
1626 }
1627
1628 static int kcore_copy__compare_file(const char *from_dir, const char *to_dir,
1629                                     const char *name)
1630 {
1631         char from_filename[PATH_MAX];
1632         char to_filename[PATH_MAX];
1633
1634         scnprintf(from_filename, PATH_MAX, "%s/%s", from_dir, name);
1635         scnprintf(to_filename, PATH_MAX, "%s/%s", to_dir, name);
1636
1637         return kcore_copy__compare_files(from_filename, to_filename);
1638 }
1639
1640 /**
1641  * kcore_copy - copy kallsyms, modules and kcore from one directory to another.
1642  * @from_dir: from directory
1643  * @to_dir: to directory
1644  *
1645  * This function copies kallsyms, modules and kcore files from one directory to
1646  * another.  kallsyms and modules are copied entirely.  Only code segments are
1647  * copied from kcore.  It is assumed that two segments suffice: one for the
1648  * kernel proper and one for all the modules.  The code segments are determined
1649  * from kallsyms and modules files.  The kernel map starts at _stext or the
1650  * lowest function symbol, and ends at _etext or the highest function symbol.
1651  * The module map starts at the lowest module address and ends at the highest
1652  * module symbol.  Start addresses are rounded down to the nearest page.  End
1653  * addresses are rounded up to the nearest page.  An extra page is added to the
1654  * highest kernel symbol and highest module symbol to, hopefully, encompass that
1655  * symbol too.  Because it contains only code sections, the resulting kcore is
1656  * unusual.  One significant peculiarity is that the mapping (start -> pgoff)
1657  * is not the same for the kernel map and the modules map.  That happens because
1658  * the data is copied adjacently whereas the original kcore has gaps.  Finally,
1659  * kallsyms and modules files are compared with their copies to check that
1660  * modules have not been loaded or unloaded while the copies were taking place.
1661  *
1662  * Return: %0 on success, %-1 on failure.
1663  */
1664 int kcore_copy(const char *from_dir, const char *to_dir)
1665 {
1666         struct kcore kcore;
1667         struct kcore extract;
1668         size_t count = 2;
1669         int idx = 0, err = -1;
1670         off_t offset = page_size, sz, modules_offset = 0;
1671         struct kcore_copy_info kci = { .stext = 0, };
1672         char kcore_filename[PATH_MAX];
1673         char extract_filename[PATH_MAX];
1674
1675         if (kcore_copy__copy_file(from_dir, to_dir, "kallsyms"))
1676                 return -1;
1677
1678         if (kcore_copy__copy_file(from_dir, to_dir, "modules"))
1679                 goto out_unlink_kallsyms;
1680
1681         scnprintf(kcore_filename, PATH_MAX, "%s/kcore", from_dir);
1682         scnprintf(extract_filename, PATH_MAX, "%s/kcore", to_dir);
1683
1684         if (kcore__open(&kcore, kcore_filename))
1685                 goto out_unlink_modules;
1686
1687         if (kcore_copy__calc_maps(&kci, from_dir, kcore.elf))
1688                 goto out_kcore_close;
1689
1690         if (kcore__init(&extract, extract_filename, kcore.elfclass, false))
1691                 goto out_kcore_close;
1692
1693         if (!kci.modules_map.addr)
1694                 count -= 1;
1695
1696         if (kcore__copy_hdr(&kcore, &extract, count))
1697                 goto out_extract_close;
1698
1699         if (kcore__add_phdr(&extract, idx++, offset, kci.kernel_map.addr,
1700                             kci.kernel_map.len))
1701                 goto out_extract_close;
1702
1703         if (kci.modules_map.addr) {
1704                 modules_offset = offset + kci.kernel_map.len;
1705                 if (kcore__add_phdr(&extract, idx, modules_offset,
1706                                     kci.modules_map.addr, kci.modules_map.len))
1707                         goto out_extract_close;
1708         }
1709
1710         sz = kcore__write(&extract);
1711         if (sz < 0 || sz > offset)
1712                 goto out_extract_close;
1713
1714         if (copy_bytes(kcore.fd, kci.kernel_map.offset, extract.fd, offset,
1715                        kci.kernel_map.len))
1716                 goto out_extract_close;
1717
1718         if (modules_offset && copy_bytes(kcore.fd, kci.modules_map.offset,
1719                                          extract.fd, modules_offset,
1720                                          kci.modules_map.len))
1721                 goto out_extract_close;
1722
1723         if (kcore_copy__compare_file(from_dir, to_dir, "modules"))
1724                 goto out_extract_close;
1725
1726         if (kcore_copy__compare_file(from_dir, to_dir, "kallsyms"))
1727                 goto out_extract_close;
1728
1729         err = 0;
1730
1731 out_extract_close:
1732         kcore__close(&extract);
1733         if (err)
1734                 unlink(extract_filename);
1735 out_kcore_close:
1736         kcore__close(&kcore);
1737 out_unlink_modules:
1738         if (err)
1739                 kcore_copy__unlink(to_dir, "modules");
1740 out_unlink_kallsyms:
1741         if (err)
1742                 kcore_copy__unlink(to_dir, "kallsyms");
1743
1744         return err;
1745 }
1746
1747 int kcore_extract__create(struct kcore_extract *kce)
1748 {
1749         struct kcore kcore;
1750         struct kcore extract;
1751         size_t count = 1;
1752         int idx = 0, err = -1;
1753         off_t offset = page_size, sz;
1754
1755         if (kcore__open(&kcore, kce->kcore_filename))
1756                 return -1;
1757
1758         strcpy(kce->extract_filename, PERF_KCORE_EXTRACT);
1759         if (kcore__init(&extract, kce->extract_filename, kcore.elfclass, true))
1760                 goto out_kcore_close;
1761
1762         if (kcore__copy_hdr(&kcore, &extract, count))
1763                 goto out_extract_close;
1764
1765         if (kcore__add_phdr(&extract, idx, offset, kce->addr, kce->len))
1766                 goto out_extract_close;
1767
1768         sz = kcore__write(&extract);
1769         if (sz < 0 || sz > offset)
1770                 goto out_extract_close;
1771
1772         if (copy_bytes(kcore.fd, kce->offs, extract.fd, offset, kce->len))
1773                 goto out_extract_close;
1774
1775         err = 0;
1776
1777 out_extract_close:
1778         kcore__close(&extract);
1779         if (err)
1780                 unlink(kce->extract_filename);
1781 out_kcore_close:
1782         kcore__close(&kcore);
1783
1784         return err;
1785 }
1786
1787 void kcore_extract__delete(struct kcore_extract *kce)
1788 {
1789         unlink(kce->extract_filename);
1790 }
1791
1792 void symbol__elf_init(void)
1793 {
1794         elf_version(EV_CURRENT);
1795 }