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