1 // SPDX-License-Identifier: GPL-2.0
2 #include <fcntl.h>
3 #include <stdio.h>
4 #include <errno.h>
5 #include <stdlib.h>
6 #include <string.h>
7 #include <unistd.h>
8 #include <inttypes.h>
9 
10 #include "dso.h"
11 #include "map.h"
12 #include "maps.h"
13 #include "symbol.h"
14 #include "symsrc.h"
15 #include "demangle-java.h"
16 #include "demangle-rust.h"
17 #include "machine.h"
18 #include "vdso.h"
19 #include "debug.h"
20 #include "util/copyfile.h"
21 #include <linux/ctype.h>
22 #include <linux/kernel.h>
23 #include <linux/zalloc.h>
24 #include <symbol/kallsyms.h>
25 #include <internal/lib.h>
26 
27 #ifndef EM_AARCH64
28 #define EM_AARCH64	183  /* ARM 64 bit */
29 #endif
30 
31 #ifndef ELF32_ST_VISIBILITY
32 #define ELF32_ST_VISIBILITY(o)	((o) & 0x03)
33 #endif
34 
35 /* For ELF64 the definitions are the same.  */
36 #ifndef ELF64_ST_VISIBILITY
37 #define ELF64_ST_VISIBILITY(o)	ELF32_ST_VISIBILITY (o)
38 #endif
39 
40 /* How to extract information held in the st_other field.  */
41 #ifndef GELF_ST_VISIBILITY
42 #define GELF_ST_VISIBILITY(val)	ELF64_ST_VISIBILITY (val)
43 #endif
44 
45 typedef Elf64_Nhdr GElf_Nhdr;
46 
47 #ifndef DMGL_PARAMS
48 #define DMGL_NO_OPTS     0              /* For readability... */
49 #define DMGL_PARAMS      (1 << 0)       /* Include function args */
50 #define DMGL_ANSI        (1 << 1)       /* Include const, volatile, etc */
51 #endif
52 
53 #ifdef HAVE_LIBBFD_SUPPORT
54 #define PACKAGE 'perf'
55 #include <bfd.h>
56 #else
57 #ifdef HAVE_CPLUS_DEMANGLE_SUPPORT
58 extern char *cplus_demangle(const char *, int);
59 
bfd_demangle(void __maybe_unused * v,const char * c,int i)60 static inline char *bfd_demangle(void __maybe_unused *v, const char *c, int i)
61 {
62 	return cplus_demangle(c, i);
63 }
64 #else
65 #ifdef NO_DEMANGLE
bfd_demangle(void __maybe_unused * v,const char __maybe_unused * c,int __maybe_unused i)66 static inline char *bfd_demangle(void __maybe_unused *v,
67 				 const char __maybe_unused *c,
68 				 int __maybe_unused i)
69 {
70 	return NULL;
71 }
72 #endif
73 #endif
74 #endif
75 
76 #ifndef HAVE_ELF_GETPHDRNUM_SUPPORT
elf_getphdrnum(Elf * elf,size_t * dst)77 static int elf_getphdrnum(Elf *elf, size_t *dst)
78 {
79 	GElf_Ehdr gehdr;
80 	GElf_Ehdr *ehdr;
81 
82 	ehdr = gelf_getehdr(elf, &gehdr);
83 	if (!ehdr)
84 		return -1;
85 
86 	*dst = ehdr->e_phnum;
87 
88 	return 0;
89 }
90 #endif
91 
92 #ifndef HAVE_ELF_GETSHDRSTRNDX_SUPPORT
elf_getshdrstrndx(Elf * elf __maybe_unused,size_t * dst __maybe_unused)93 static int elf_getshdrstrndx(Elf *elf __maybe_unused, size_t *dst __maybe_unused)
94 {
95 	pr_err("%s: update your libelf to > 0.140, this one lacks elf_getshdrstrndx().\n", __func__);
96 	return -1;
97 }
98 #endif
99 
100 #ifndef NT_GNU_BUILD_ID
101 #define NT_GNU_BUILD_ID 3
102 #endif
103 
104 /**
105  * elf_symtab__for_each_symbol - iterate thru all the symbols
106  *
107  * @syms: struct elf_symtab instance to iterate
108  * @idx: uint32_t idx
109  * @sym: GElf_Sym iterator
110  */
111 #define elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) \
112 	for (idx = 0, gelf_getsym(syms, idx, &sym);\
113 	     idx < nr_syms; \
114 	     idx++, gelf_getsym(syms, idx, &sym))
115 
elf_sym__type(const GElf_Sym * sym)116 static inline uint8_t elf_sym__type(const GElf_Sym *sym)
117 {
118 	return GELF_ST_TYPE(sym->st_info);
119 }
120 
elf_sym__visibility(const GElf_Sym * sym)121 static inline uint8_t elf_sym__visibility(const GElf_Sym *sym)
122 {
123 	return GELF_ST_VISIBILITY(sym->st_other);
124 }
125 
126 #ifndef STT_GNU_IFUNC
127 #define STT_GNU_IFUNC 10
128 #endif
129 
elf_sym__is_function(const GElf_Sym * sym)130 static inline int elf_sym__is_function(const GElf_Sym *sym)
131 {
132 	return (elf_sym__type(sym) == STT_FUNC ||
133 		elf_sym__type(sym) == STT_GNU_IFUNC) &&
134 	       sym->st_name != 0 &&
135 	       sym->st_shndx != SHN_UNDEF;
136 }
137 
elf_sym__is_object(const GElf_Sym * sym)138 static inline bool elf_sym__is_object(const GElf_Sym *sym)
139 {
140 	return elf_sym__type(sym) == STT_OBJECT &&
141 		sym->st_name != 0 &&
142 		sym->st_shndx != SHN_UNDEF;
143 }
144 
elf_sym__is_label(const GElf_Sym * sym)145 static inline int elf_sym__is_label(const GElf_Sym *sym)
146 {
147 	return elf_sym__type(sym) == STT_NOTYPE &&
148 		sym->st_name != 0 &&
149 		sym->st_shndx != SHN_UNDEF &&
150 		sym->st_shndx != SHN_ABS &&
151 		elf_sym__visibility(sym) != STV_HIDDEN &&
152 		elf_sym__visibility(sym) != STV_INTERNAL;
153 }
154 
elf_sym__filter(GElf_Sym * sym)155 static bool elf_sym__filter(GElf_Sym *sym)
156 {
157 	return elf_sym__is_function(sym) || elf_sym__is_object(sym);
158 }
159 
elf_sym__name(const GElf_Sym * sym,const Elf_Data * symstrs)160 static inline const char *elf_sym__name(const GElf_Sym *sym,
161 					const Elf_Data *symstrs)
162 {
163 	return symstrs->d_buf + sym->st_name;
164 }
165 
elf_sec__name(const GElf_Shdr * shdr,const Elf_Data * secstrs)166 static inline const char *elf_sec__name(const GElf_Shdr *shdr,
167 					const Elf_Data *secstrs)
168 {
169 	return secstrs->d_buf + shdr->sh_name;
170 }
171 
elf_sec__is_text(const GElf_Shdr * shdr,const Elf_Data * secstrs)172 static inline int elf_sec__is_text(const GElf_Shdr *shdr,
173 					const Elf_Data *secstrs)
174 {
175 	return strstr(elf_sec__name(shdr, secstrs), "text") != NULL;
176 }
177 
elf_sec__is_data(const GElf_Shdr * shdr,const Elf_Data * secstrs)178 static inline bool elf_sec__is_data(const GElf_Shdr *shdr,
179 				    const Elf_Data *secstrs)
180 {
181 	return strstr(elf_sec__name(shdr, secstrs), "data") != NULL;
182 }
183 
elf_sec__filter(GElf_Shdr * shdr,Elf_Data * secstrs)184 static bool elf_sec__filter(GElf_Shdr *shdr, Elf_Data *secstrs)
185 {
186 	return elf_sec__is_text(shdr, secstrs) ||
187 	       elf_sec__is_data(shdr, secstrs);
188 }
189 
elf_addr_to_index(Elf * elf,GElf_Addr addr)190 static size_t elf_addr_to_index(Elf *elf, GElf_Addr addr)
191 {
192 	Elf_Scn *sec = NULL;
193 	GElf_Shdr shdr;
194 	size_t cnt = 1;
195 
196 	while ((sec = elf_nextscn(elf, sec)) != NULL) {
197 		gelf_getshdr(sec, &shdr);
198 
199 		if ((addr >= shdr.sh_addr) &&
200 		    (addr < (shdr.sh_addr + shdr.sh_size)))
201 			return cnt;
202 
203 		++cnt;
204 	}
205 
206 	return -1;
207 }
208 
elf_section_by_name(Elf * elf,GElf_Ehdr * ep,GElf_Shdr * shp,const char * name,size_t * idx)209 Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep,
210 			     GElf_Shdr *shp, const char *name, size_t *idx)
211 {
212 	Elf_Scn *sec = NULL;
213 	size_t cnt = 1;
214 
215 	/* Elf is corrupted/truncated, avoid calling elf_strptr. */
216 	if (!elf_rawdata(elf_getscn(elf, ep->e_shstrndx), NULL))
217 		return NULL;
218 
219 	while ((sec = elf_nextscn(elf, sec)) != NULL) {
220 		char *str;
221 
222 		gelf_getshdr(sec, shp);
223 		str = elf_strptr(elf, ep->e_shstrndx, shp->sh_name);
224 		if (str && !strcmp(name, str)) {
225 			if (idx)
226 				*idx = cnt;
227 			return sec;
228 		}
229 		++cnt;
230 	}
231 
232 	return NULL;
233 }
234 
want_demangle(bool is_kernel_sym)235 static bool want_demangle(bool is_kernel_sym)
236 {
237 	return is_kernel_sym ? symbol_conf.demangle_kernel : symbol_conf.demangle;
238 }
239 
demangle_sym(struct dso * dso,int kmodule,const char * elf_name)240 static char *demangle_sym(struct dso *dso, int kmodule, const char *elf_name)
241 {
242 	int demangle_flags = verbose > 0 ? (DMGL_PARAMS | DMGL_ANSI) : DMGL_NO_OPTS;
243 	char *demangled = NULL;
244 
245 	/*
246 	 * We need to figure out if the object was created from C++ sources
247 	 * DWARF DW_compile_unit has this, but we don't always have access
248 	 * to it...
249 	 */
250 	if (!want_demangle(dso->kernel || kmodule))
251 	    return demangled;
252 
253 	demangled = bfd_demangle(NULL, elf_name, demangle_flags);
254 	if (demangled == NULL)
255 		demangled = java_demangle_sym(elf_name, JAVA_DEMANGLE_NORET);
256 	else if (rust_is_mangled(demangled))
257 		/*
258 		    * Input to Rust demangling is the BFD-demangled
259 		    * name which it Rust-demangles in place.
260 		    */
261 		rust_demangle_sym(demangled);
262 
263 	return demangled;
264 }
265 
266 #define elf_section__for_each_rel(reldata, pos, pos_mem, idx, nr_entries) \
267 	for (idx = 0, pos = gelf_getrel(reldata, 0, &pos_mem); \
268 	     idx < nr_entries; \
269 	     ++idx, pos = gelf_getrel(reldata, idx, &pos_mem))
270 
271 #define elf_section__for_each_rela(reldata, pos, pos_mem, idx, nr_entries) \
272 	for (idx = 0, pos = gelf_getrela(reldata, 0, &pos_mem); \
273 	     idx < nr_entries; \
274 	     ++idx, pos = gelf_getrela(reldata, idx, &pos_mem))
275 
276 /*
277  * We need to check if we have a .dynsym, so that we can handle the
278  * .plt, synthesizing its symbols, that aren't on the symtabs (be it
279  * .dynsym or .symtab).
280  * And always look at the original dso, not at debuginfo packages, that
281  * have the PLT data stripped out (shdr_rel_plt.sh_type == SHT_NOBITS).
282  */
dso__synthesize_plt_symbols(struct dso * dso,struct symsrc * ss)283 int dso__synthesize_plt_symbols(struct dso *dso, struct symsrc *ss)
284 {
285 	uint32_t nr_rel_entries, idx;
286 	GElf_Sym sym;
287 	u64 plt_offset, plt_header_size, plt_entry_size;
288 	GElf_Shdr shdr_plt;
289 	struct symbol *f;
290 	GElf_Shdr shdr_rel_plt, shdr_dynsym;
291 	Elf_Data *reldata, *syms, *symstrs;
292 	Elf_Scn *scn_plt_rel, *scn_symstrs, *scn_dynsym;
293 	size_t dynsym_idx;
294 	GElf_Ehdr ehdr;
295 	char sympltname[1024];
296 	Elf *elf;
297 	int nr = 0, symidx, err = 0;
298 
299 	if (!ss->dynsym)
300 		return 0;
301 
302 	elf = ss->elf;
303 	ehdr = ss->ehdr;
304 
305 	scn_dynsym = ss->dynsym;
306 	shdr_dynsym = ss->dynshdr;
307 	dynsym_idx = ss->dynsym_idx;
308 
309 	if (scn_dynsym == NULL)
310 		goto out_elf_end;
311 
312 	scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt,
313 					  ".rela.plt", NULL);
314 	if (scn_plt_rel == NULL) {
315 		scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt,
316 						  ".rel.plt", NULL);
317 		if (scn_plt_rel == NULL)
318 			goto out_elf_end;
319 	}
320 
321 	err = -1;
322 
323 	if (shdr_rel_plt.sh_link != dynsym_idx)
324 		goto out_elf_end;
325 
326 	if (elf_section_by_name(elf, &ehdr, &shdr_plt, ".plt", NULL) == NULL)
327 		goto out_elf_end;
328 
329 	/*
330 	 * Fetch the relocation section to find the idxes to the GOT
331 	 * and the symbols in the .dynsym they refer to.
332 	 */
333 	reldata = elf_getdata(scn_plt_rel, NULL);
334 	if (reldata == NULL)
335 		goto out_elf_end;
336 
337 	syms = elf_getdata(scn_dynsym, NULL);
338 	if (syms == NULL)
339 		goto out_elf_end;
340 
341 	scn_symstrs = elf_getscn(elf, shdr_dynsym.sh_link);
342 	if (scn_symstrs == NULL)
343 		goto out_elf_end;
344 
345 	symstrs = elf_getdata(scn_symstrs, NULL);
346 	if (symstrs == NULL)
347 		goto out_elf_end;
348 
349 	if (symstrs->d_size == 0)
350 		goto out_elf_end;
351 
352 	nr_rel_entries = shdr_rel_plt.sh_size / shdr_rel_plt.sh_entsize;
353 	plt_offset = shdr_plt.sh_offset;
354 	switch (ehdr.e_machine) {
355 		case EM_ARM:
356 			plt_header_size = 20;
357 			plt_entry_size = 12;
358 			break;
359 
360 		case EM_AARCH64:
361 			plt_header_size = 32;
362 			plt_entry_size = 16;
363 			break;
364 
365 		case EM_SPARC:
366 			plt_header_size = 48;
367 			plt_entry_size = 12;
368 			break;
369 
370 		case EM_SPARCV9:
371 			plt_header_size = 128;
372 			plt_entry_size = 32;
373 			break;
374 
375 		default: /* FIXME: s390/alpha/mips/parisc/poperpc/sh/xtensa need to be checked */
376 			plt_header_size = shdr_plt.sh_entsize;
377 			plt_entry_size = shdr_plt.sh_entsize;
378 			break;
379 	}
380 	plt_offset += plt_header_size;
381 
382 	if (shdr_rel_plt.sh_type == SHT_RELA) {
383 		GElf_Rela pos_mem, *pos;
384 
385 		elf_section__for_each_rela(reldata, pos, pos_mem, idx,
386 					   nr_rel_entries) {
387 			const char *elf_name = NULL;
388 			char *demangled = NULL;
389 			symidx = GELF_R_SYM(pos->r_info);
390 			gelf_getsym(syms, symidx, &sym);
391 
392 			elf_name = elf_sym__name(&sym, symstrs);
393 			demangled = demangle_sym(dso, 0, elf_name);
394 			if (demangled != NULL)
395 				elf_name = demangled;
396 			snprintf(sympltname, sizeof(sympltname),
397 				 "%s@plt", elf_name);
398 			free(demangled);
399 
400 			f = symbol__new(plt_offset, plt_entry_size,
401 					STB_GLOBAL, STT_FUNC, sympltname);
402 			if (!f)
403 				goto out_elf_end;
404 
405 			plt_offset += plt_entry_size;
406 			symbols__insert(&dso->symbols, f);
407 			++nr;
408 		}
409 	} else if (shdr_rel_plt.sh_type == SHT_REL) {
410 		GElf_Rel pos_mem, *pos;
411 		elf_section__for_each_rel(reldata, pos, pos_mem, idx,
412 					  nr_rel_entries) {
413 			const char *elf_name = NULL;
414 			char *demangled = NULL;
415 			symidx = GELF_R_SYM(pos->r_info);
416 			gelf_getsym(syms, symidx, &sym);
417 
418 			elf_name = elf_sym__name(&sym, symstrs);
419 			demangled = demangle_sym(dso, 0, elf_name);
420 			if (demangled != NULL)
421 				elf_name = demangled;
422 			snprintf(sympltname, sizeof(sympltname),
423 				 "%s@plt", elf_name);
424 			free(demangled);
425 
426 			f = symbol__new(plt_offset, plt_entry_size,
427 					STB_GLOBAL, STT_FUNC, sympltname);
428 			if (!f)
429 				goto out_elf_end;
430 
431 			plt_offset += plt_entry_size;
432 			symbols__insert(&dso->symbols, f);
433 			++nr;
434 		}
435 	}
436 
437 	err = 0;
438 out_elf_end:
439 	if (err == 0)
440 		return nr;
441 	pr_debug("%s: problems reading %s PLT info.\n",
442 		 __func__, dso->long_name);
443 	return 0;
444 }
445 
dso__demangle_sym(struct dso * dso,int kmodule,const char * elf_name)446 char *dso__demangle_sym(struct dso *dso, int kmodule, const char *elf_name)
447 {
448 	return demangle_sym(dso, kmodule, elf_name);
449 }
450 
451 /*
452  * Align offset to 4 bytes as needed for note name and descriptor data.
453  */
454 #define NOTE_ALIGN(n) (((n) + 3) & -4U)
455 
elf_read_build_id(Elf * elf,void * bf,size_t size)456 static int elf_read_build_id(Elf *elf, void *bf, size_t size)
457 {
458 	int err = -1;
459 	GElf_Ehdr ehdr;
460 	GElf_Shdr shdr;
461 	Elf_Data *data;
462 	Elf_Scn *sec;
463 	Elf_Kind ek;
464 	void *ptr;
465 
466 	if (size < BUILD_ID_SIZE)
467 		goto out;
468 
469 	ek = elf_kind(elf);
470 	if (ek != ELF_K_ELF)
471 		goto out;
472 
473 	if (gelf_getehdr(elf, &ehdr) == NULL) {
474 		pr_err("%s: cannot get elf header.\n", __func__);
475 		goto out;
476 	}
477 
478 	/*
479 	 * Check following sections for notes:
480 	 *   '.note.gnu.build-id'
481 	 *   '.notes'
482 	 *   '.note' (VDSO specific)
483 	 */
484 	do {
485 		sec = elf_section_by_name(elf, &ehdr, &shdr,
486 					  ".note.gnu.build-id", NULL);
487 		if (sec)
488 			break;
489 
490 		sec = elf_section_by_name(elf, &ehdr, &shdr,
491 					  ".notes", NULL);
492 		if (sec)
493 			break;
494 
495 		sec = elf_section_by_name(elf, &ehdr, &shdr,
496 					  ".note", NULL);
497 		if (sec)
498 			break;
499 
500 		return err;
501 
502 	} while (0);
503 
504 	data = elf_getdata(sec, NULL);
505 	if (data == NULL)
506 		goto out;
507 
508 	ptr = data->d_buf;
509 	while (ptr < (data->d_buf + data->d_size)) {
510 		GElf_Nhdr *nhdr = ptr;
511 		size_t namesz = NOTE_ALIGN(nhdr->n_namesz),
512 		       descsz = NOTE_ALIGN(nhdr->n_descsz);
513 		const char *name;
514 
515 		ptr += sizeof(*nhdr);
516 		name = ptr;
517 		ptr += namesz;
518 		if (nhdr->n_type == NT_GNU_BUILD_ID &&
519 		    nhdr->n_namesz == sizeof("GNU")) {
520 			if (memcmp(name, "GNU", sizeof("GNU")) == 0) {
521 				size_t sz = min(size, descsz);
522 				memcpy(bf, ptr, sz);
523 				memset(bf + sz, 0, size - sz);
524 				err = descsz;
525 				break;
526 			}
527 		}
528 		ptr += descsz;
529 	}
530 
531 out:
532 	return err;
533 }
534 
535 #ifdef HAVE_LIBBFD_BUILDID_SUPPORT
536 
filename__read_build_id(const char * filename,struct build_id * bid)537 int filename__read_build_id(const char *filename, struct build_id *bid)
538 {
539 	size_t size = sizeof(bid->data);
540 	int err = -1;
541 	bfd *abfd;
542 
543 	abfd = bfd_openr(filename, NULL);
544 	if (!abfd)
545 		return -1;
546 
547 	if (!bfd_check_format(abfd, bfd_object)) {
548 		pr_debug2("%s: cannot read %s bfd file.\n", __func__, filename);
549 		goto out_close;
550 	}
551 
552 	if (!abfd->build_id || abfd->build_id->size > size)
553 		goto out_close;
554 
555 	memcpy(bid->data, abfd->build_id->data, abfd->build_id->size);
556 	memset(bid->data + abfd->build_id->size, 0, size - abfd->build_id->size);
557 	err = bid->size = abfd->build_id->size;
558 
559 out_close:
560 	bfd_close(abfd);
561 	return err;
562 }
563 
564 #else // HAVE_LIBBFD_BUILDID_SUPPORT
565 
filename__read_build_id(const char * filename,struct build_id * bid)566 int filename__read_build_id(const char *filename, struct build_id *bid)
567 {
568 	size_t size = sizeof(bid->data);
569 	int fd, err = -1;
570 	Elf *elf;
571 
572 	if (size < BUILD_ID_SIZE)
573 		goto out;
574 
575 	fd = open(filename, O_RDONLY);
576 	if (fd < 0)
577 		goto out;
578 
579 	elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
580 	if (elf == NULL) {
581 		pr_debug2("%s: cannot read %s ELF file.\n", __func__, filename);
582 		goto out_close;
583 	}
584 
585 	err = elf_read_build_id(elf, bid->data, size);
586 	if (err > 0)
587 		bid->size = err;
588 
589 	elf_end(elf);
590 out_close:
591 	close(fd);
592 out:
593 	return err;
594 }
595 
596 #endif // HAVE_LIBBFD_BUILDID_SUPPORT
597 
sysfs__read_build_id(const char * filename,struct build_id * bid)598 int sysfs__read_build_id(const char *filename, struct build_id *bid)
599 {
600 	size_t size = sizeof(bid->data);
601 	int fd, err = -1;
602 
603 	fd = open(filename, O_RDONLY);
604 	if (fd < 0)
605 		goto out;
606 
607 	while (1) {
608 		char bf[BUFSIZ];
609 		GElf_Nhdr nhdr;
610 		size_t namesz, descsz;
611 
612 		if (read(fd, &nhdr, sizeof(nhdr)) != sizeof(nhdr))
613 			break;
614 
615 		namesz = NOTE_ALIGN(nhdr.n_namesz);
616 		descsz = NOTE_ALIGN(nhdr.n_descsz);
617 		if (nhdr.n_type == NT_GNU_BUILD_ID &&
618 		    nhdr.n_namesz == sizeof("GNU")) {
619 			if (read(fd, bf, namesz) != (ssize_t)namesz)
620 				break;
621 			if (memcmp(bf, "GNU", sizeof("GNU")) == 0) {
622 				size_t sz = min(descsz, size);
623 				if (read(fd, bid->data, sz) == (ssize_t)sz) {
624 					memset(bid->data + sz, 0, size - sz);
625 					bid->size = sz;
626 					err = 0;
627 					break;
628 				}
629 			} else if (read(fd, bf, descsz) != (ssize_t)descsz)
630 				break;
631 		} else {
632 			int n = namesz + descsz;
633 
634 			if (n > (int)sizeof(bf)) {
635 				n = sizeof(bf);
636 				pr_debug("%s: truncating reading of build id in sysfs file %s: n_namesz=%u, n_descsz=%u.\n",
637 					 __func__, filename, nhdr.n_namesz, nhdr.n_descsz);
638 			}
639 			if (read(fd, bf, n) != n)
640 				break;
641 		}
642 	}
643 	close(fd);
644 out:
645 	return err;
646 }
647 
648 #ifdef HAVE_LIBBFD_SUPPORT
649 
filename__read_debuglink(const char * filename,char * debuglink,size_t size)650 int filename__read_debuglink(const char *filename, char *debuglink,
651 			     size_t size)
652 {
653 	int err = -1;
654 	asection *section;
655 	bfd *abfd;
656 
657 	abfd = bfd_openr(filename, NULL);
658 	if (!abfd)
659 		return -1;
660 
661 	if (!bfd_check_format(abfd, bfd_object)) {
662 		pr_debug2("%s: cannot read %s bfd file.\n", __func__, filename);
663 		goto out_close;
664 	}
665 
666 	section = bfd_get_section_by_name(abfd, ".gnu_debuglink");
667 	if (!section)
668 		goto out_close;
669 
670 	if (section->size > size)
671 		goto out_close;
672 
673 	if (!bfd_get_section_contents(abfd, section, debuglink, 0,
674 				      section->size))
675 		goto out_close;
676 
677 	err = 0;
678 
679 out_close:
680 	bfd_close(abfd);
681 	return err;
682 }
683 
684 #else
685 
filename__read_debuglink(const char * filename,char * debuglink,size_t size)686 int filename__read_debuglink(const char *filename, char *debuglink,
687 			     size_t size)
688 {
689 	int fd, err = -1;
690 	Elf *elf;
691 	GElf_Ehdr ehdr;
692 	GElf_Shdr shdr;
693 	Elf_Data *data;
694 	Elf_Scn *sec;
695 	Elf_Kind ek;
696 
697 	fd = open(filename, O_RDONLY);
698 	if (fd < 0)
699 		goto out;
700 
701 	elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
702 	if (elf == NULL) {
703 		pr_debug2("%s: cannot read %s ELF file.\n", __func__, filename);
704 		goto out_close;
705 	}
706 
707 	ek = elf_kind(elf);
708 	if (ek != ELF_K_ELF)
709 		goto out_elf_end;
710 
711 	if (gelf_getehdr(elf, &ehdr) == NULL) {
712 		pr_err("%s: cannot get elf header.\n", __func__);
713 		goto out_elf_end;
714 	}
715 
716 	sec = elf_section_by_name(elf, &ehdr, &shdr,
717 				  ".gnu_debuglink", NULL);
718 	if (sec == NULL)
719 		goto out_elf_end;
720 
721 	data = elf_getdata(sec, NULL);
722 	if (data == NULL)
723 		goto out_elf_end;
724 
725 	/* the start of this section is a zero-terminated string */
726 	strncpy(debuglink, data->d_buf, size);
727 
728 	err = 0;
729 
730 out_elf_end:
731 	elf_end(elf);
732 out_close:
733 	close(fd);
734 out:
735 	return err;
736 }
737 
738 #endif
739 
dso__swap_init(struct dso * dso,unsigned char eidata)740 static int dso__swap_init(struct dso *dso, unsigned char eidata)
741 {
742 	static unsigned int const endian = 1;
743 
744 	dso->needs_swap = DSO_SWAP__NO;
745 
746 	switch (eidata) {
747 	case ELFDATA2LSB:
748 		/* We are big endian, DSO is little endian. */
749 		if (*(unsigned char const *)&endian != 1)
750 			dso->needs_swap = DSO_SWAP__YES;
751 		break;
752 
753 	case ELFDATA2MSB:
754 		/* We are little endian, DSO is big endian. */
755 		if (*(unsigned char const *)&endian != 0)
756 			dso->needs_swap = DSO_SWAP__YES;
757 		break;
758 
759 	default:
760 		pr_err("unrecognized DSO data encoding %d\n", eidata);
761 		return -EINVAL;
762 	}
763 
764 	return 0;
765 }
766 
symsrc__possibly_runtime(struct symsrc * ss)767 bool symsrc__possibly_runtime(struct symsrc *ss)
768 {
769 	return ss->dynsym || ss->opdsec;
770 }
771 
symsrc__has_symtab(struct symsrc * ss)772 bool symsrc__has_symtab(struct symsrc *ss)
773 {
774 	return ss->symtab != NULL;
775 }
776 
symsrc__destroy(struct symsrc * ss)777 void symsrc__destroy(struct symsrc *ss)
778 {
779 	zfree(&ss->name);
780 	elf_end(ss->elf);
781 	close(ss->fd);
782 }
783 
elf__needs_adjust_symbols(GElf_Ehdr ehdr)784 bool elf__needs_adjust_symbols(GElf_Ehdr ehdr)
785 {
786 	/*
787 	 * Usually vmlinux is an ELF file with type ET_EXEC for most
788 	 * architectures; except Arm64 kernel is linked with option
789 	 * '-share', so need to check type ET_DYN.
790 	 */
791 	return ehdr.e_type == ET_EXEC || ehdr.e_type == ET_REL ||
792 	       ehdr.e_type == ET_DYN;
793 }
794 
symsrc__init(struct symsrc * ss,struct dso * dso,const char * name,enum dso_binary_type type)795 int symsrc__init(struct symsrc *ss, struct dso *dso, const char *name,
796 		 enum dso_binary_type type)
797 {
798 	GElf_Ehdr ehdr;
799 	Elf *elf;
800 	int fd;
801 
802 	if (dso__needs_decompress(dso)) {
803 		fd = dso__decompress_kmodule_fd(dso, name);
804 		if (fd < 0)
805 			return -1;
806 
807 		type = dso->symtab_type;
808 	} else {
809 		fd = open(name, O_RDONLY);
810 		if (fd < 0) {
811 			dso->load_errno = errno;
812 			return -1;
813 		}
814 	}
815 
816 	elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
817 	if (elf == NULL) {
818 		pr_debug("%s: cannot read %s ELF file.\n", __func__, name);
819 		dso->load_errno = DSO_LOAD_ERRNO__INVALID_ELF;
820 		goto out_close;
821 	}
822 
823 	if (gelf_getehdr(elf, &ehdr) == NULL) {
824 		dso->load_errno = DSO_LOAD_ERRNO__INVALID_ELF;
825 		pr_debug("%s: cannot get elf header.\n", __func__);
826 		goto out_elf_end;
827 	}
828 
829 	if (dso__swap_init(dso, ehdr.e_ident[EI_DATA])) {
830 		dso->load_errno = DSO_LOAD_ERRNO__INTERNAL_ERROR;
831 		goto out_elf_end;
832 	}
833 
834 	/* Always reject images with a mismatched build-id: */
835 	if (dso->has_build_id && !symbol_conf.ignore_vmlinux_buildid) {
836 		u8 build_id[BUILD_ID_SIZE];
837 		struct build_id bid;
838 		int size;
839 
840 		size = elf_read_build_id(elf, build_id, BUILD_ID_SIZE);
841 		if (size <= 0) {
842 			dso->load_errno = DSO_LOAD_ERRNO__CANNOT_READ_BUILDID;
843 			goto out_elf_end;
844 		}
845 
846 		build_id__init(&bid, build_id, size);
847 		if (!dso__build_id_equal(dso, &bid)) {
848 			pr_debug("%s: build id mismatch for %s.\n", __func__, name);
849 			dso->load_errno = DSO_LOAD_ERRNO__MISMATCHING_BUILDID;
850 			goto out_elf_end;
851 		}
852 	}
853 
854 	ss->is_64_bit = (gelf_getclass(elf) == ELFCLASS64);
855 
856 	ss->symtab = elf_section_by_name(elf, &ehdr, &ss->symshdr, ".symtab",
857 			NULL);
858 	if (ss->symshdr.sh_type != SHT_SYMTAB)
859 		ss->symtab = NULL;
860 
861 	ss->dynsym_idx = 0;
862 	ss->dynsym = elf_section_by_name(elf, &ehdr, &ss->dynshdr, ".dynsym",
863 			&ss->dynsym_idx);
864 	if (ss->dynshdr.sh_type != SHT_DYNSYM)
865 		ss->dynsym = NULL;
866 
867 	ss->opdidx = 0;
868 	ss->opdsec = elf_section_by_name(elf, &ehdr, &ss->opdshdr, ".opd",
869 			&ss->opdidx);
870 	if (ss->opdshdr.sh_type != SHT_PROGBITS)
871 		ss->opdsec = NULL;
872 
873 	if (dso->kernel == DSO_SPACE__USER)
874 		ss->adjust_symbols = true;
875 	else
876 		ss->adjust_symbols = elf__needs_adjust_symbols(ehdr);
877 
878 	ss->name   = strdup(name);
879 	if (!ss->name) {
880 		dso->load_errno = errno;
881 		goto out_elf_end;
882 	}
883 
884 	ss->elf    = elf;
885 	ss->fd     = fd;
886 	ss->ehdr   = ehdr;
887 	ss->type   = type;
888 
889 	return 0;
890 
891 out_elf_end:
892 	elf_end(elf);
893 out_close:
894 	close(fd);
895 	return -1;
896 }
897 
898 /**
899  * ref_reloc_sym_not_found - has kernel relocation symbol been found.
900  * @kmap: kernel maps and relocation reference symbol
901  *
902  * This function returns %true if we are dealing with the kernel maps and the
903  * relocation reference symbol has not yet been found.  Otherwise %false is
904  * returned.
905  */
ref_reloc_sym_not_found(struct kmap * kmap)906 static bool ref_reloc_sym_not_found(struct kmap *kmap)
907 {
908 	return kmap && kmap->ref_reloc_sym && kmap->ref_reloc_sym->name &&
909 	       !kmap->ref_reloc_sym->unrelocated_addr;
910 }
911 
912 /**
913  * ref_reloc - kernel relocation offset.
914  * @kmap: kernel maps and relocation reference symbol
915  *
916  * This function returns the offset of kernel addresses as determined by using
917  * the relocation reference symbol i.e. if the kernel has not been relocated
918  * then the return value is zero.
919  */
ref_reloc(struct kmap * kmap)920 static u64 ref_reloc(struct kmap *kmap)
921 {
922 	if (kmap && kmap->ref_reloc_sym &&
923 	    kmap->ref_reloc_sym->unrelocated_addr)
924 		return kmap->ref_reloc_sym->addr -
925 		       kmap->ref_reloc_sym->unrelocated_addr;
926 	return 0;
927 }
928 
arch__sym_update(struct symbol * s __maybe_unused,GElf_Sym * sym __maybe_unused)929 void __weak arch__sym_update(struct symbol *s __maybe_unused,
930 		GElf_Sym *sym __maybe_unused) { }
931 
dso__process_kernel_symbol(struct dso * dso,struct map * map,GElf_Sym * sym,GElf_Shdr * shdr,struct maps * kmaps,struct kmap * kmap,struct dso ** curr_dsop,struct map ** curr_mapp,const char * section_name,bool adjust_kernel_syms,bool kmodule,bool * remap_kernel)932 static int dso__process_kernel_symbol(struct dso *dso, struct map *map,
933 				      GElf_Sym *sym, GElf_Shdr *shdr,
934 				      struct maps *kmaps, struct kmap *kmap,
935 				      struct dso **curr_dsop, struct map **curr_mapp,
936 				      const char *section_name,
937 				      bool adjust_kernel_syms, bool kmodule, bool *remap_kernel)
938 {
939 	struct dso *curr_dso = *curr_dsop;
940 	struct map *curr_map;
941 	char dso_name[PATH_MAX];
942 
943 	/* Adjust symbol to map to file offset */
944 	if (adjust_kernel_syms)
945 		sym->st_value -= shdr->sh_addr - shdr->sh_offset;
946 
947 	if (strcmp(section_name, (curr_dso->short_name + dso->short_name_len)) == 0)
948 		return 0;
949 
950 	if (strcmp(section_name, ".text") == 0) {
951 		/*
952 		 * The initial kernel mapping is based on
953 		 * kallsyms and identity maps.  Overwrite it to
954 		 * map to the kernel dso.
955 		 */
956 		if (*remap_kernel && dso->kernel && !kmodule) {
957 			*remap_kernel = false;
958 			map->start = shdr->sh_addr + ref_reloc(kmap);
959 			map->end = map->start + shdr->sh_size;
960 			map->pgoff = shdr->sh_offset;
961 			map->map_ip = map__map_ip;
962 			map->unmap_ip = map__unmap_ip;
963 			/* Ensure maps are correctly ordered */
964 			if (kmaps) {
965 				map__get(map);
966 				maps__remove(kmaps, map);
967 				maps__insert(kmaps, map);
968 				map__put(map);
969 			}
970 		}
971 
972 		/*
973 		 * The initial module mapping is based on
974 		 * /proc/modules mapped to offset zero.
975 		 * Overwrite it to map to the module dso.
976 		 */
977 		if (*remap_kernel && kmodule) {
978 			*remap_kernel = false;
979 			map->pgoff = shdr->sh_offset;
980 		}
981 
982 		*curr_mapp = map;
983 		*curr_dsop = dso;
984 		return 0;
985 	}
986 
987 	if (!kmap)
988 		return 0;
989 
990 	snprintf(dso_name, sizeof(dso_name), "%s%s", dso->short_name, section_name);
991 
992 	curr_map = maps__find_by_name(kmaps, dso_name);
993 	if (curr_map == NULL) {
994 		u64 start = sym->st_value;
995 
996 		if (kmodule)
997 			start += map->start + shdr->sh_offset;
998 
999 		curr_dso = dso__new(dso_name);
1000 		if (curr_dso == NULL)
1001 			return -1;
1002 		curr_dso->kernel = dso->kernel;
1003 		curr_dso->long_name = dso->long_name;
1004 		curr_dso->long_name_len = dso->long_name_len;
1005 		curr_map = map__new2(start, curr_dso);
1006 		dso__put(curr_dso);
1007 		if (curr_map == NULL)
1008 			return -1;
1009 
1010 		if (curr_dso->kernel)
1011 			map__kmap(curr_map)->kmaps = kmaps;
1012 
1013 		if (adjust_kernel_syms) {
1014 			curr_map->start  = shdr->sh_addr + ref_reloc(kmap);
1015 			curr_map->end	 = curr_map->start + shdr->sh_size;
1016 			curr_map->pgoff	 = shdr->sh_offset;
1017 		} else {
1018 			curr_map->map_ip = curr_map->unmap_ip = identity__map_ip;
1019 		}
1020 		curr_dso->symtab_type = dso->symtab_type;
1021 		maps__insert(kmaps, curr_map);
1022 		/*
1023 		 * Add it before we drop the referece to curr_map, i.e. while
1024 		 * we still are sure to have a reference to this DSO via
1025 		 * *curr_map->dso.
1026 		 */
1027 		dsos__add(&kmaps->machine->dsos, curr_dso);
1028 		/* kmaps already got it */
1029 		map__put(curr_map);
1030 		dso__set_loaded(curr_dso);
1031 		*curr_mapp = curr_map;
1032 		*curr_dsop = curr_dso;
1033 	} else
1034 		*curr_dsop = curr_map->dso;
1035 
1036 	return 0;
1037 }
1038 
dso__load_sym(struct dso * dso,struct map * map,struct symsrc * syms_ss,struct symsrc * runtime_ss,int kmodule)1039 int dso__load_sym(struct dso *dso, struct map *map, struct symsrc *syms_ss,
1040 		  struct symsrc *runtime_ss, int kmodule)
1041 {
1042 	struct kmap *kmap = dso->kernel ? map__kmap(map) : NULL;
1043 	struct maps *kmaps = kmap ? map__kmaps(map) : NULL;
1044 	struct map *curr_map = map;
1045 	struct dso *curr_dso = dso;
1046 	Elf_Data *symstrs, *secstrs;
1047 	uint32_t nr_syms;
1048 	int err = -1;
1049 	uint32_t idx;
1050 	GElf_Ehdr ehdr;
1051 	GElf_Shdr shdr;
1052 	GElf_Shdr tshdr;
1053 	Elf_Data *syms, *opddata = NULL;
1054 	GElf_Sym sym;
1055 	Elf_Scn *sec, *sec_strndx;
1056 	Elf *elf;
1057 	int nr = 0;
1058 	bool remap_kernel = false, adjust_kernel_syms = false;
1059 
1060 	if (kmap && !kmaps)
1061 		return -1;
1062 
1063 	dso->symtab_type = syms_ss->type;
1064 	dso->is_64_bit = syms_ss->is_64_bit;
1065 	dso->rel = syms_ss->ehdr.e_type == ET_REL;
1066 
1067 	/*
1068 	 * Modules may already have symbols from kallsyms, but those symbols
1069 	 * have the wrong values for the dso maps, so remove them.
1070 	 */
1071 	if (kmodule && syms_ss->symtab)
1072 		symbols__delete(&dso->symbols);
1073 
1074 	if (!syms_ss->symtab) {
1075 		/*
1076 		 * If the vmlinux is stripped, fail so we will fall back
1077 		 * to using kallsyms. The vmlinux runtime symbols aren't
1078 		 * of much use.
1079 		 */
1080 		if (dso->kernel)
1081 			goto out_elf_end;
1082 
1083 		syms_ss->symtab  = syms_ss->dynsym;
1084 		syms_ss->symshdr = syms_ss->dynshdr;
1085 	}
1086 
1087 	elf = syms_ss->elf;
1088 	ehdr = syms_ss->ehdr;
1089 	sec = syms_ss->symtab;
1090 	shdr = syms_ss->symshdr;
1091 
1092 	if (elf_section_by_name(runtime_ss->elf, &runtime_ss->ehdr, &tshdr,
1093 				".text", NULL))
1094 		dso->text_offset = tshdr.sh_addr - tshdr.sh_offset;
1095 
1096 	if (runtime_ss->opdsec)
1097 		opddata = elf_rawdata(runtime_ss->opdsec, NULL);
1098 
1099 	syms = elf_getdata(sec, NULL);
1100 	if (syms == NULL)
1101 		goto out_elf_end;
1102 
1103 	sec = elf_getscn(elf, shdr.sh_link);
1104 	if (sec == NULL)
1105 		goto out_elf_end;
1106 
1107 	symstrs = elf_getdata(sec, NULL);
1108 	if (symstrs == NULL)
1109 		goto out_elf_end;
1110 
1111 	sec_strndx = elf_getscn(runtime_ss->elf, runtime_ss->ehdr.e_shstrndx);
1112 	if (sec_strndx == NULL)
1113 		goto out_elf_end;
1114 
1115 	secstrs = elf_getdata(sec_strndx, NULL);
1116 	if (secstrs == NULL)
1117 		goto out_elf_end;
1118 
1119 	nr_syms = shdr.sh_size / shdr.sh_entsize;
1120 
1121 	memset(&sym, 0, sizeof(sym));
1122 
1123 	/*
1124 	 * The kernel relocation symbol is needed in advance in order to adjust
1125 	 * kernel maps correctly.
1126 	 */
1127 	if (ref_reloc_sym_not_found(kmap)) {
1128 		elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) {
1129 			const char *elf_name = elf_sym__name(&sym, symstrs);
1130 
1131 			if (strcmp(elf_name, kmap->ref_reloc_sym->name))
1132 				continue;
1133 			kmap->ref_reloc_sym->unrelocated_addr = sym.st_value;
1134 			map->reloc = kmap->ref_reloc_sym->addr -
1135 				     kmap->ref_reloc_sym->unrelocated_addr;
1136 			break;
1137 		}
1138 	}
1139 
1140 	/*
1141 	 * Handle any relocation of vdso necessary because older kernels
1142 	 * attempted to prelink vdso to its virtual address.
1143 	 */
1144 	if (dso__is_vdso(dso))
1145 		map->reloc = map->start - dso->text_offset;
1146 
1147 	dso->adjust_symbols = runtime_ss->adjust_symbols || ref_reloc(kmap);
1148 	/*
1149 	 * Initial kernel and module mappings do not map to the dso.
1150 	 * Flag the fixups.
1151 	 */
1152 	if (dso->kernel) {
1153 		remap_kernel = true;
1154 		adjust_kernel_syms = dso->adjust_symbols;
1155 	}
1156 	elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) {
1157 		struct symbol *f;
1158 		const char *elf_name = elf_sym__name(&sym, symstrs);
1159 		char *demangled = NULL;
1160 		int is_label = elf_sym__is_label(&sym);
1161 		const char *section_name;
1162 		bool used_opd = false;
1163 
1164 		if (!is_label && !elf_sym__filter(&sym))
1165 			continue;
1166 
1167 		/* Reject ARM ELF "mapping symbols": these aren't unique and
1168 		 * don't identify functions, so will confuse the profile
1169 		 * output: */
1170 		if (ehdr.e_machine == EM_ARM || ehdr.e_machine == EM_AARCH64) {
1171 			if (elf_name[0] == '$' && strchr("adtx", elf_name[1])
1172 			    && (elf_name[2] == '\0' || elf_name[2] == '.'))
1173 				continue;
1174 		}
1175 
1176 		if (runtime_ss->opdsec && sym.st_shndx == runtime_ss->opdidx) {
1177 			u32 offset = sym.st_value - syms_ss->opdshdr.sh_addr;
1178 			u64 *opd = opddata->d_buf + offset;
1179 			sym.st_value = DSO__SWAP(dso, u64, *opd);
1180 			sym.st_shndx = elf_addr_to_index(runtime_ss->elf,
1181 					sym.st_value);
1182 			used_opd = true;
1183 		}
1184 		/*
1185 		 * When loading symbols in a data mapping, ABS symbols (which
1186 		 * has a value of SHN_ABS in its st_shndx) failed at
1187 		 * elf_getscn().  And it marks the loading as a failure so
1188 		 * already loaded symbols cannot be fixed up.
1189 		 *
1190 		 * I'm not sure what should be done. Just ignore them for now.
1191 		 * - Namhyung Kim
1192 		 */
1193 		if (sym.st_shndx == SHN_ABS)
1194 			continue;
1195 
1196 		sec = elf_getscn(runtime_ss->elf, sym.st_shndx);
1197 		if (!sec)
1198 			goto out_elf_end;
1199 
1200 		gelf_getshdr(sec, &shdr);
1201 
1202 		if (is_label && !elf_sec__filter(&shdr, secstrs))
1203 			continue;
1204 
1205 		section_name = elf_sec__name(&shdr, secstrs);
1206 
1207 		/* On ARM, symbols for thumb functions have 1 added to
1208 		 * the symbol address as a flag - remove it */
1209 		if ((ehdr.e_machine == EM_ARM) &&
1210 		    (GELF_ST_TYPE(sym.st_info) == STT_FUNC) &&
1211 		    (sym.st_value & 1))
1212 			--sym.st_value;
1213 
1214 		if (dso->kernel) {
1215 			if (dso__process_kernel_symbol(dso, map, &sym, &shdr, kmaps, kmap, &curr_dso, &curr_map,
1216 						       section_name, adjust_kernel_syms, kmodule, &remap_kernel))
1217 				goto out_elf_end;
1218 		} else if ((used_opd && runtime_ss->adjust_symbols) ||
1219 			   (!used_opd && syms_ss->adjust_symbols)) {
1220 			pr_debug4("%s: adjusting symbol: st_value: %#" PRIx64 " "
1221 				  "sh_addr: %#" PRIx64 " sh_offset: %#" PRIx64 "\n", __func__,
1222 				  (u64)sym.st_value, (u64)shdr.sh_addr,
1223 				  (u64)shdr.sh_offset);
1224 			sym.st_value -= shdr.sh_addr - shdr.sh_offset;
1225 		}
1226 
1227 		demangled = demangle_sym(dso, kmodule, elf_name);
1228 		if (demangled != NULL)
1229 			elf_name = demangled;
1230 
1231 		f = symbol__new(sym.st_value, sym.st_size,
1232 				GELF_ST_BIND(sym.st_info),
1233 				GELF_ST_TYPE(sym.st_info), elf_name);
1234 		free(demangled);
1235 		if (!f)
1236 			goto out_elf_end;
1237 
1238 		arch__sym_update(f, &sym);
1239 
1240 		__symbols__insert(&curr_dso->symbols, f, dso->kernel);
1241 		nr++;
1242 	}
1243 
1244 	/*
1245 	 * For misannotated, zeroed, ASM function sizes.
1246 	 */
1247 	if (nr > 0) {
1248 		symbols__fixup_end(&dso->symbols);
1249 		symbols__fixup_duplicate(&dso->symbols);
1250 		if (kmap) {
1251 			/*
1252 			 * We need to fixup this here too because we create new
1253 			 * maps here, for things like vsyscall sections.
1254 			 */
1255 			maps__fixup_end(kmaps);
1256 		}
1257 	}
1258 	err = nr;
1259 out_elf_end:
1260 	return err;
1261 }
1262 
elf_read_maps(Elf * elf,bool exe,mapfn_t mapfn,void * data)1263 static int elf_read_maps(Elf *elf, bool exe, mapfn_t mapfn, void *data)
1264 {
1265 	GElf_Phdr phdr;
1266 	size_t i, phdrnum;
1267 	int err;
1268 	u64 sz;
1269 
1270 	if (elf_getphdrnum(elf, &phdrnum))
1271 		return -1;
1272 
1273 	for (i = 0; i < phdrnum; i++) {
1274 		if (gelf_getphdr(elf, i, &phdr) == NULL)
1275 			return -1;
1276 		if (phdr.p_type != PT_LOAD)
1277 			continue;
1278 		if (exe) {
1279 			if (!(phdr.p_flags & PF_X))
1280 				continue;
1281 		} else {
1282 			if (!(phdr.p_flags & PF_R))
1283 				continue;
1284 		}
1285 		sz = min(phdr.p_memsz, phdr.p_filesz);
1286 		if (!sz)
1287 			continue;
1288 		err = mapfn(phdr.p_vaddr, sz, phdr.p_offset, data);
1289 		if (err)
1290 			return err;
1291 	}
1292 	return 0;
1293 }
1294 
file__read_maps(int fd,bool exe,mapfn_t mapfn,void * data,bool * is_64_bit)1295 int file__read_maps(int fd, bool exe, mapfn_t mapfn, void *data,
1296 		    bool *is_64_bit)
1297 {
1298 	int err;
1299 	Elf *elf;
1300 
1301 	elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
1302 	if (elf == NULL)
1303 		return -1;
1304 
1305 	if (is_64_bit)
1306 		*is_64_bit = (gelf_getclass(elf) == ELFCLASS64);
1307 
1308 	err = elf_read_maps(elf, exe, mapfn, data);
1309 
1310 	elf_end(elf);
1311 	return err;
1312 }
1313 
dso__type_fd(int fd)1314 enum dso_type dso__type_fd(int fd)
1315 {
1316 	enum dso_type dso_type = DSO__TYPE_UNKNOWN;
1317 	GElf_Ehdr ehdr;
1318 	Elf_Kind ek;
1319 	Elf *elf;
1320 
1321 	elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
1322 	if (elf == NULL)
1323 		goto out;
1324 
1325 	ek = elf_kind(elf);
1326 	if (ek != ELF_K_ELF)
1327 		goto out_end;
1328 
1329 	if (gelf_getclass(elf) == ELFCLASS64) {
1330 		dso_type = DSO__TYPE_64BIT;
1331 		goto out_end;
1332 	}
1333 
1334 	if (gelf_getehdr(elf, &ehdr) == NULL)
1335 		goto out_end;
1336 
1337 	if (ehdr.e_machine == EM_X86_64)
1338 		dso_type = DSO__TYPE_X32BIT;
1339 	else
1340 		dso_type = DSO__TYPE_32BIT;
1341 out_end:
1342 	elf_end(elf);
1343 out:
1344 	return dso_type;
1345 }
1346 
copy_bytes(int from,off_t from_offs,int to,off_t to_offs,u64 len)1347 static int copy_bytes(int from, off_t from_offs, int to, off_t to_offs, u64 len)
1348 {
1349 	ssize_t r;
1350 	size_t n;
1351 	int err = -1;
1352 	char *buf = malloc(page_size);
1353 
1354 	if (buf == NULL)
1355 		return -1;
1356 
1357 	if (lseek(to, to_offs, SEEK_SET) != to_offs)
1358 		goto out;
1359 
1360 	if (lseek(from, from_offs, SEEK_SET) != from_offs)
1361 		goto out;
1362 
1363 	while (len) {
1364 		n = page_size;
1365 		if (len < n)
1366 			n = len;
1367 		/* Use read because mmap won't work on proc files */
1368 		r = read(from, buf, n);
1369 		if (r < 0)
1370 			goto out;
1371 		if (!r)
1372 			break;
1373 		n = r;
1374 		r = write(to, buf, n);
1375 		if (r < 0)
1376 			goto out;
1377 		if ((size_t)r != n)
1378 			goto out;
1379 		len -= n;
1380 	}
1381 
1382 	err = 0;
1383 out:
1384 	free(buf);
1385 	return err;
1386 }
1387 
1388 struct kcore {
1389 	int fd;
1390 	int elfclass;
1391 	Elf *elf;
1392 	GElf_Ehdr ehdr;
1393 };
1394 
kcore__open(struct kcore * kcore,const char * filename)1395 static int kcore__open(struct kcore *kcore, const char *filename)
1396 {
1397 	GElf_Ehdr *ehdr;
1398 
1399 	kcore->fd = open(filename, O_RDONLY);
1400 	if (kcore->fd == -1)
1401 		return -1;
1402 
1403 	kcore->elf = elf_begin(kcore->fd, ELF_C_READ, NULL);
1404 	if (!kcore->elf)
1405 		goto out_close;
1406 
1407 	kcore->elfclass = gelf_getclass(kcore->elf);
1408 	if (kcore->elfclass == ELFCLASSNONE)
1409 		goto out_end;
1410 
1411 	ehdr = gelf_getehdr(kcore->elf, &kcore->ehdr);
1412 	if (!ehdr)
1413 		goto out_end;
1414 
1415 	return 0;
1416 
1417 out_end:
1418 	elf_end(kcore->elf);
1419 out_close:
1420 	close(kcore->fd);
1421 	return -1;
1422 }
1423 
kcore__init(struct kcore * kcore,char * filename,int elfclass,bool temp)1424 static int kcore__init(struct kcore *kcore, char *filename, int elfclass,
1425 		       bool temp)
1426 {
1427 	kcore->elfclass = elfclass;
1428 
1429 	if (temp)
1430 		kcore->fd = mkstemp(filename);
1431 	else
1432 		kcore->fd = open(filename, O_WRONLY | O_CREAT | O_EXCL, 0400);
1433 	if (kcore->fd == -1)
1434 		return -1;
1435 
1436 	kcore->elf = elf_begin(kcore->fd, ELF_C_WRITE, NULL);
1437 	if (!kcore->elf)
1438 		goto out_close;
1439 
1440 	if (!gelf_newehdr(kcore->elf, elfclass))
1441 		goto out_end;
1442 
1443 	memset(&kcore->ehdr, 0, sizeof(GElf_Ehdr));
1444 
1445 	return 0;
1446 
1447 out_end:
1448 	elf_end(kcore->elf);
1449 out_close:
1450 	close(kcore->fd);
1451 	unlink(filename);
1452 	return -1;
1453 }
1454 
kcore__close(struct kcore * kcore)1455 static void kcore__close(struct kcore *kcore)
1456 {
1457 	elf_end(kcore->elf);
1458 	close(kcore->fd);
1459 }
1460 
kcore__copy_hdr(struct kcore * from,struct kcore * to,size_t count)1461 static int kcore__copy_hdr(struct kcore *from, struct kcore *to, size_t count)
1462 {
1463 	GElf_Ehdr *ehdr = &to->ehdr;
1464 	GElf_Ehdr *kehdr = &from->ehdr;
1465 
1466 	memcpy(ehdr->e_ident, kehdr->e_ident, EI_NIDENT);
1467 	ehdr->e_type      = kehdr->e_type;
1468 	ehdr->e_machine   = kehdr->e_machine;
1469 	ehdr->e_version   = kehdr->e_version;
1470 	ehdr->e_entry     = 0;
1471 	ehdr->e_shoff     = 0;
1472 	ehdr->e_flags     = kehdr->e_flags;
1473 	ehdr->e_phnum     = count;
1474 	ehdr->e_shentsize = 0;
1475 	ehdr->e_shnum     = 0;
1476 	ehdr->e_shstrndx  = 0;
1477 
1478 	if (from->elfclass == ELFCLASS32) {
1479 		ehdr->e_phoff     = sizeof(Elf32_Ehdr);
1480 		ehdr->e_ehsize    = sizeof(Elf32_Ehdr);
1481 		ehdr->e_phentsize = sizeof(Elf32_Phdr);
1482 	} else {
1483 		ehdr->e_phoff     = sizeof(Elf64_Ehdr);
1484 		ehdr->e_ehsize    = sizeof(Elf64_Ehdr);
1485 		ehdr->e_phentsize = sizeof(Elf64_Phdr);
1486 	}
1487 
1488 	if (!gelf_update_ehdr(to->elf, ehdr))
1489 		return -1;
1490 
1491 	if (!gelf_newphdr(to->elf, count))
1492 		return -1;
1493 
1494 	return 0;
1495 }
1496 
kcore__add_phdr(struct kcore * kcore,int idx,off_t offset,u64 addr,u64 len)1497 static int kcore__add_phdr(struct kcore *kcore, int idx, off_t offset,
1498 			   u64 addr, u64 len)
1499 {
1500 	GElf_Phdr phdr = {
1501 		.p_type		= PT_LOAD,
1502 		.p_flags	= PF_R | PF_W | PF_X,
1503 		.p_offset	= offset,
1504 		.p_vaddr	= addr,
1505 		.p_paddr	= 0,
1506 		.p_filesz	= len,
1507 		.p_memsz	= len,
1508 		.p_align	= page_size,
1509 	};
1510 
1511 	if (!gelf_update_phdr(kcore->elf, idx, &phdr))
1512 		return -1;
1513 
1514 	return 0;
1515 }
1516 
kcore__write(struct kcore * kcore)1517 static off_t kcore__write(struct kcore *kcore)
1518 {
1519 	return elf_update(kcore->elf, ELF_C_WRITE);
1520 }
1521 
1522 struct phdr_data {
1523 	off_t offset;
1524 	off_t rel;
1525 	u64 addr;
1526 	u64 len;
1527 	struct list_head node;
1528 	struct phdr_data *remaps;
1529 };
1530 
1531 struct sym_data {
1532 	u64 addr;
1533 	struct list_head node;
1534 };
1535 
1536 struct kcore_copy_info {
1537 	u64 stext;
1538 	u64 etext;
1539 	u64 first_symbol;
1540 	u64 last_symbol;
1541 	u64 first_module;
1542 	u64 first_module_symbol;
1543 	u64 last_module_symbol;
1544 	size_t phnum;
1545 	struct list_head phdrs;
1546 	struct list_head syms;
1547 };
1548 
1549 #define kcore_copy__for_each_phdr(k, p) \
1550 	list_for_each_entry((p), &(k)->phdrs, node)
1551 
phdr_data__new(u64 addr,u64 len,off_t offset)1552 static struct phdr_data *phdr_data__new(u64 addr, u64 len, off_t offset)
1553 {
1554 	struct phdr_data *p = zalloc(sizeof(*p));
1555 
1556 	if (p) {
1557 		p->addr   = addr;
1558 		p->len    = len;
1559 		p->offset = offset;
1560 	}
1561 
1562 	return p;
1563 }
1564 
kcore_copy_info__addnew(struct kcore_copy_info * kci,u64 addr,u64 len,off_t offset)1565 static struct phdr_data *kcore_copy_info__addnew(struct kcore_copy_info *kci,
1566 						 u64 addr, u64 len,
1567 						 off_t offset)
1568 {
1569 	struct phdr_data *p = phdr_data__new(addr, len, offset);
1570 
1571 	if (p)
1572 		list_add_tail(&p->node, &kci->phdrs);
1573 
1574 	return p;
1575 }
1576 
kcore_copy__free_phdrs(struct kcore_copy_info * kci)1577 static void kcore_copy__free_phdrs(struct kcore_copy_info *kci)
1578 {
1579 	struct phdr_data *p, *tmp;
1580 
1581 	list_for_each_entry_safe(p, tmp, &kci->phdrs, node) {
1582 		list_del_init(&p->node);
1583 		free(p);
1584 	}
1585 }
1586 
kcore_copy__new_sym(struct kcore_copy_info * kci,u64 addr)1587 static struct sym_data *kcore_copy__new_sym(struct kcore_copy_info *kci,
1588 					    u64 addr)
1589 {
1590 	struct sym_data *s = zalloc(sizeof(*s));
1591 
1592 	if (s) {
1593 		s->addr = addr;
1594 		list_add_tail(&s->node, &kci->syms);
1595 	}
1596 
1597 	return s;
1598 }
1599 
kcore_copy__free_syms(struct kcore_copy_info * kci)1600 static void kcore_copy__free_syms(struct kcore_copy_info *kci)
1601 {
1602 	struct sym_data *s, *tmp;
1603 
1604 	list_for_each_entry_safe(s, tmp, &kci->syms, node) {
1605 		list_del_init(&s->node);
1606 		free(s);
1607 	}
1608 }
1609 
kcore_copy__process_kallsyms(void * arg,const char * name,char type,u64 start)1610 static int kcore_copy__process_kallsyms(void *arg, const char *name, char type,
1611 					u64 start)
1612 {
1613 	struct kcore_copy_info *kci = arg;
1614 
1615 	if (!kallsyms__is_function(type))
1616 		return 0;
1617 
1618 	if (strchr(name, '[')) {
1619 		if (!kci->first_module_symbol || start < kci->first_module_symbol)
1620 			kci->first_module_symbol = start;
1621 		if (start > kci->last_module_symbol)
1622 			kci->last_module_symbol = start;
1623 		return 0;
1624 	}
1625 
1626 	if (!kci->first_symbol || start < kci->first_symbol)
1627 		kci->first_symbol = start;
1628 
1629 	if (!kci->last_symbol || start > kci->last_symbol)
1630 		kci->last_symbol = start;
1631 
1632 	if (!strcmp(name, "_stext")) {
1633 		kci->stext = start;
1634 		return 0;
1635 	}
1636 
1637 	if (!strcmp(name, "_etext")) {
1638 		kci->etext = start;
1639 		return 0;
1640 	}
1641 
1642 	if (is_entry_trampoline(name) && !kcore_copy__new_sym(kci, start))
1643 		return -1;
1644 
1645 	return 0;
1646 }
1647 
kcore_copy__parse_kallsyms(struct kcore_copy_info * kci,const char * dir)1648 static int kcore_copy__parse_kallsyms(struct kcore_copy_info *kci,
1649 				      const char *dir)
1650 {
1651 	char kallsyms_filename[PATH_MAX];
1652 
1653 	scnprintf(kallsyms_filename, PATH_MAX, "%s/kallsyms", dir);
1654 
1655 	if (symbol__restricted_filename(kallsyms_filename, "/proc/kallsyms"))
1656 		return -1;
1657 
1658 	if (kallsyms__parse(kallsyms_filename, kci,
1659 			    kcore_copy__process_kallsyms) < 0)
1660 		return -1;
1661 
1662 	return 0;
1663 }
1664 
kcore_copy__process_modules(void * arg,const char * name __maybe_unused,u64 start,u64 size __maybe_unused)1665 static int kcore_copy__process_modules(void *arg,
1666 				       const char *name __maybe_unused,
1667 				       u64 start, u64 size __maybe_unused)
1668 {
1669 	struct kcore_copy_info *kci = arg;
1670 
1671 	if (!kci->first_module || start < kci->first_module)
1672 		kci->first_module = start;
1673 
1674 	return 0;
1675 }
1676 
kcore_copy__parse_modules(struct kcore_copy_info * kci,const char * dir)1677 static int kcore_copy__parse_modules(struct kcore_copy_info *kci,
1678 				     const char *dir)
1679 {
1680 	char modules_filename[PATH_MAX];
1681 
1682 	scnprintf(modules_filename, PATH_MAX, "%s/modules", dir);
1683 
1684 	if (symbol__restricted_filename(modules_filename, "/proc/modules"))
1685 		return -1;
1686 
1687 	if (modules__parse(modules_filename, kci,
1688 			   kcore_copy__process_modules) < 0)
1689 		return -1;
1690 
1691 	return 0;
1692 }
1693 
kcore_copy__map(struct kcore_copy_info * kci,u64 start,u64 end,u64 pgoff,u64 s,u64 e)1694 static int kcore_copy__map(struct kcore_copy_info *kci, u64 start, u64 end,
1695 			   u64 pgoff, u64 s, u64 e)
1696 {
1697 	u64 len, offset;
1698 
1699 	if (s < start || s >= end)
1700 		return 0;
1701 
1702 	offset = (s - start) + pgoff;
1703 	len = e < end ? e - s : end - s;
1704 
1705 	return kcore_copy_info__addnew(kci, s, len, offset) ? 0 : -1;
1706 }
1707 
kcore_copy__read_map(u64 start,u64 len,u64 pgoff,void * data)1708 static int kcore_copy__read_map(u64 start, u64 len, u64 pgoff, void *data)
1709 {
1710 	struct kcore_copy_info *kci = data;
1711 	u64 end = start + len;
1712 	struct sym_data *sdat;
1713 
1714 	if (kcore_copy__map(kci, start, end, pgoff, kci->stext, kci->etext))
1715 		return -1;
1716 
1717 	if (kcore_copy__map(kci, start, end, pgoff, kci->first_module,
1718 			    kci->last_module_symbol))
1719 		return -1;
1720 
1721 	list_for_each_entry(sdat, &kci->syms, node) {
1722 		u64 s = round_down(sdat->addr, page_size);
1723 
1724 		if (kcore_copy__map(kci, start, end, pgoff, s, s + len))
1725 			return -1;
1726 	}
1727 
1728 	return 0;
1729 }
1730 
kcore_copy__read_maps(struct kcore_copy_info * kci,Elf * elf)1731 static int kcore_copy__read_maps(struct kcore_copy_info *kci, Elf *elf)
1732 {
1733 	if (elf_read_maps(elf, true, kcore_copy__read_map, kci) < 0)
1734 		return -1;
1735 
1736 	return 0;
1737 }
1738 
kcore_copy__find_remaps(struct kcore_copy_info * kci)1739 static void kcore_copy__find_remaps(struct kcore_copy_info *kci)
1740 {
1741 	struct phdr_data *p, *k = NULL;
1742 	u64 kend;
1743 
1744 	if (!kci->stext)
1745 		return;
1746 
1747 	/* Find phdr that corresponds to the kernel map (contains stext) */
1748 	kcore_copy__for_each_phdr(kci, p) {
1749 		u64 pend = p->addr + p->len - 1;
1750 
1751 		if (p->addr <= kci->stext && pend >= kci->stext) {
1752 			k = p;
1753 			break;
1754 		}
1755 	}
1756 
1757 	if (!k)
1758 		return;
1759 
1760 	kend = k->offset + k->len;
1761 
1762 	/* Find phdrs that remap the kernel */
1763 	kcore_copy__for_each_phdr(kci, p) {
1764 		u64 pend = p->offset + p->len;
1765 
1766 		if (p == k)
1767 			continue;
1768 
1769 		if (p->offset >= k->offset && pend <= kend)
1770 			p->remaps = k;
1771 	}
1772 }
1773 
kcore_copy__layout(struct kcore_copy_info * kci)1774 static void kcore_copy__layout(struct kcore_copy_info *kci)
1775 {
1776 	struct phdr_data *p;
1777 	off_t rel = 0;
1778 
1779 	kcore_copy__find_remaps(kci);
1780 
1781 	kcore_copy__for_each_phdr(kci, p) {
1782 		if (!p->remaps) {
1783 			p->rel = rel;
1784 			rel += p->len;
1785 		}
1786 		kci->phnum += 1;
1787 	}
1788 
1789 	kcore_copy__for_each_phdr(kci, p) {
1790 		struct phdr_data *k = p->remaps;
1791 
1792 		if (k)
1793 			p->rel = p->offset - k->offset + k->rel;
1794 	}
1795 }
1796 
kcore_copy__calc_maps(struct kcore_copy_info * kci,const char * dir,Elf * elf)1797 static int kcore_copy__calc_maps(struct kcore_copy_info *kci, const char *dir,
1798 				 Elf *elf)
1799 {
1800 	if (kcore_copy__parse_kallsyms(kci, dir))
1801 		return -1;
1802 
1803 	if (kcore_copy__parse_modules(kci, dir))
1804 		return -1;
1805 
1806 	if (kci->stext)
1807 		kci->stext = round_down(kci->stext, page_size);
1808 	else
1809 		kci->stext = round_down(kci->first_symbol, page_size);
1810 
1811 	if (kci->etext) {
1812 		kci->etext = round_up(kci->etext, page_size);
1813 	} else if (kci->last_symbol) {
1814 		kci->etext = round_up(kci->last_symbol, page_size);
1815 		kci->etext += page_size;
1816 	}
1817 
1818 	if (kci->first_module_symbol &&
1819 	    (!kci->first_module || kci->first_module_symbol < kci->first_module))
1820 		kci->first_module = kci->first_module_symbol;
1821 
1822 	kci->first_module = round_down(kci->first_module, page_size);
1823 
1824 	if (kci->last_module_symbol) {
1825 		kci->last_module_symbol = round_up(kci->last_module_symbol,
1826 						   page_size);
1827 		kci->last_module_symbol += page_size;
1828 	}
1829 
1830 	if (!kci->stext || !kci->etext)
1831 		return -1;
1832 
1833 	if (kci->first_module && !kci->last_module_symbol)
1834 		return -1;
1835 
1836 	if (kcore_copy__read_maps(kci, elf))
1837 		return -1;
1838 
1839 	kcore_copy__layout(kci);
1840 
1841 	return 0;
1842 }
1843 
kcore_copy__copy_file(const char * from_dir,const char * to_dir,const char * name)1844 static int kcore_copy__copy_file(const char *from_dir, const char *to_dir,
1845 				 const char *name)
1846 {
1847 	char from_filename[PATH_MAX];
1848 	char to_filename[PATH_MAX];
1849 
1850 	scnprintf(from_filename, PATH_MAX, "%s/%s", from_dir, name);
1851 	scnprintf(to_filename, PATH_MAX, "%s/%s", to_dir, name);
1852 
1853 	return copyfile_mode(from_filename, to_filename, 0400);
1854 }
1855 
kcore_copy__unlink(const char * dir,const char * name)1856 static int kcore_copy__unlink(const char *dir, const char *name)
1857 {
1858 	char filename[PATH_MAX];
1859 
1860 	scnprintf(filename, PATH_MAX, "%s/%s", dir, name);
1861 
1862 	return unlink(filename);
1863 }
1864 
kcore_copy__compare_fds(int from,int to)1865 static int kcore_copy__compare_fds(int from, int to)
1866 {
1867 	char *buf_from;
1868 	char *buf_to;
1869 	ssize_t ret;
1870 	size_t len;
1871 	int err = -1;
1872 
1873 	buf_from = malloc(page_size);
1874 	buf_to = malloc(page_size);
1875 	if (!buf_from || !buf_to)
1876 		goto out;
1877 
1878 	while (1) {
1879 		/* Use read because mmap won't work on proc files */
1880 		ret = read(from, buf_from, page_size);
1881 		if (ret < 0)
1882 			goto out;
1883 
1884 		if (!ret)
1885 			break;
1886 
1887 		len = ret;
1888 
1889 		if (readn(to, buf_to, len) != (int)len)
1890 			goto out;
1891 
1892 		if (memcmp(buf_from, buf_to, len))
1893 			goto out;
1894 	}
1895 
1896 	err = 0;
1897 out:
1898 	free(buf_to);
1899 	free(buf_from);
1900 	return err;
1901 }
1902 
kcore_copy__compare_files(const char * from_filename,const char * to_filename)1903 static int kcore_copy__compare_files(const char *from_filename,
1904 				     const char *to_filename)
1905 {
1906 	int from, to, err = -1;
1907 
1908 	from = open(from_filename, O_RDONLY);
1909 	if (from < 0)
1910 		return -1;
1911 
1912 	to = open(to_filename, O_RDONLY);
1913 	if (to < 0)
1914 		goto out_close_from;
1915 
1916 	err = kcore_copy__compare_fds(from, to);
1917 
1918 	close(to);
1919 out_close_from:
1920 	close(from);
1921 	return err;
1922 }
1923 
kcore_copy__compare_file(const char * from_dir,const char * to_dir,const char * name)1924 static int kcore_copy__compare_file(const char *from_dir, const char *to_dir,
1925 				    const char *name)
1926 {
1927 	char from_filename[PATH_MAX];
1928 	char to_filename[PATH_MAX];
1929 
1930 	scnprintf(from_filename, PATH_MAX, "%s/%s", from_dir, name);
1931 	scnprintf(to_filename, PATH_MAX, "%s/%s", to_dir, name);
1932 
1933 	return kcore_copy__compare_files(from_filename, to_filename);
1934 }
1935 
1936 /**
1937  * kcore_copy - copy kallsyms, modules and kcore from one directory to another.
1938  * @from_dir: from directory
1939  * @to_dir: to directory
1940  *
1941  * This function copies kallsyms, modules and kcore files from one directory to
1942  * another.  kallsyms and modules are copied entirely.  Only code segments are
1943  * copied from kcore.  It is assumed that two segments suffice: one for the
1944  * kernel proper and one for all the modules.  The code segments are determined
1945  * from kallsyms and modules files.  The kernel map starts at _stext or the
1946  * lowest function symbol, and ends at _etext or the highest function symbol.
1947  * The module map starts at the lowest module address and ends at the highest
1948  * module symbol.  Start addresses are rounded down to the nearest page.  End
1949  * addresses are rounded up to the nearest page.  An extra page is added to the
1950  * highest kernel symbol and highest module symbol to, hopefully, encompass that
1951  * symbol too.  Because it contains only code sections, the resulting kcore is
1952  * unusual.  One significant peculiarity is that the mapping (start -> pgoff)
1953  * is not the same for the kernel map and the modules map.  That happens because
1954  * the data is copied adjacently whereas the original kcore has gaps.  Finally,
1955  * kallsyms and modules files are compared with their copies to check that
1956  * modules have not been loaded or unloaded while the copies were taking place.
1957  *
1958  * Return: %0 on success, %-1 on failure.
1959  */
kcore_copy(const char * from_dir,const char * to_dir)1960 int kcore_copy(const char *from_dir, const char *to_dir)
1961 {
1962 	struct kcore kcore;
1963 	struct kcore extract;
1964 	int idx = 0, err = -1;
1965 	off_t offset, sz;
1966 	struct kcore_copy_info kci = { .stext = 0, };
1967 	char kcore_filename[PATH_MAX];
1968 	char extract_filename[PATH_MAX];
1969 	struct phdr_data *p;
1970 
1971 	INIT_LIST_HEAD(&kci.phdrs);
1972 	INIT_LIST_HEAD(&kci.syms);
1973 
1974 	if (kcore_copy__copy_file(from_dir, to_dir, "kallsyms"))
1975 		return -1;
1976 
1977 	if (kcore_copy__copy_file(from_dir, to_dir, "modules"))
1978 		goto out_unlink_kallsyms;
1979 
1980 	scnprintf(kcore_filename, PATH_MAX, "%s/kcore", from_dir);
1981 	scnprintf(extract_filename, PATH_MAX, "%s/kcore", to_dir);
1982 
1983 	if (kcore__open(&kcore, kcore_filename))
1984 		goto out_unlink_modules;
1985 
1986 	if (kcore_copy__calc_maps(&kci, from_dir, kcore.elf))
1987 		goto out_kcore_close;
1988 
1989 	if (kcore__init(&extract, extract_filename, kcore.elfclass, false))
1990 		goto out_kcore_close;
1991 
1992 	if (kcore__copy_hdr(&kcore, &extract, kci.phnum))
1993 		goto out_extract_close;
1994 
1995 	offset = gelf_fsize(extract.elf, ELF_T_EHDR, 1, EV_CURRENT) +
1996 		 gelf_fsize(extract.elf, ELF_T_PHDR, kci.phnum, EV_CURRENT);
1997 	offset = round_up(offset, page_size);
1998 
1999 	kcore_copy__for_each_phdr(&kci, p) {
2000 		off_t offs = p->rel + offset;
2001 
2002 		if (kcore__add_phdr(&extract, idx++, offs, p->addr, p->len))
2003 			goto out_extract_close;
2004 	}
2005 
2006 	sz = kcore__write(&extract);
2007 	if (sz < 0 || sz > offset)
2008 		goto out_extract_close;
2009 
2010 	kcore_copy__for_each_phdr(&kci, p) {
2011 		off_t offs = p->rel + offset;
2012 
2013 		if (p->remaps)
2014 			continue;
2015 		if (copy_bytes(kcore.fd, p->offset, extract.fd, offs, p->len))
2016 			goto out_extract_close;
2017 	}
2018 
2019 	if (kcore_copy__compare_file(from_dir, to_dir, "modules"))
2020 		goto out_extract_close;
2021 
2022 	if (kcore_copy__compare_file(from_dir, to_dir, "kallsyms"))
2023 		goto out_extract_close;
2024 
2025 	err = 0;
2026 
2027 out_extract_close:
2028 	kcore__close(&extract);
2029 	if (err)
2030 		unlink(extract_filename);
2031 out_kcore_close:
2032 	kcore__close(&kcore);
2033 out_unlink_modules:
2034 	if (err)
2035 		kcore_copy__unlink(to_dir, "modules");
2036 out_unlink_kallsyms:
2037 	if (err)
2038 		kcore_copy__unlink(to_dir, "kallsyms");
2039 
2040 	kcore_copy__free_phdrs(&kci);
2041 	kcore_copy__free_syms(&kci);
2042 
2043 	return err;
2044 }
2045 
kcore_extract__create(struct kcore_extract * kce)2046 int kcore_extract__create(struct kcore_extract *kce)
2047 {
2048 	struct kcore kcore;
2049 	struct kcore extract;
2050 	size_t count = 1;
2051 	int idx = 0, err = -1;
2052 	off_t offset = page_size, sz;
2053 
2054 	if (kcore__open(&kcore, kce->kcore_filename))
2055 		return -1;
2056 
2057 	strcpy(kce->extract_filename, PERF_KCORE_EXTRACT);
2058 	if (kcore__init(&extract, kce->extract_filename, kcore.elfclass, true))
2059 		goto out_kcore_close;
2060 
2061 	if (kcore__copy_hdr(&kcore, &extract, count))
2062 		goto out_extract_close;
2063 
2064 	if (kcore__add_phdr(&extract, idx, offset, kce->addr, kce->len))
2065 		goto out_extract_close;
2066 
2067 	sz = kcore__write(&extract);
2068 	if (sz < 0 || sz > offset)
2069 		goto out_extract_close;
2070 
2071 	if (copy_bytes(kcore.fd, kce->offs, extract.fd, offset, kce->len))
2072 		goto out_extract_close;
2073 
2074 	err = 0;
2075 
2076 out_extract_close:
2077 	kcore__close(&extract);
2078 	if (err)
2079 		unlink(kce->extract_filename);
2080 out_kcore_close:
2081 	kcore__close(&kcore);
2082 
2083 	return err;
2084 }
2085 
kcore_extract__delete(struct kcore_extract * kce)2086 void kcore_extract__delete(struct kcore_extract *kce)
2087 {
2088 	unlink(kce->extract_filename);
2089 }
2090 
2091 #ifdef HAVE_GELF_GETNOTE_SUPPORT
2092 
sdt_adjust_loc(struct sdt_note * tmp,GElf_Addr base_off)2093 static void sdt_adjust_loc(struct sdt_note *tmp, GElf_Addr base_off)
2094 {
2095 	if (!base_off)
2096 		return;
2097 
2098 	if (tmp->bit32)
2099 		tmp->addr.a32[SDT_NOTE_IDX_LOC] =
2100 			tmp->addr.a32[SDT_NOTE_IDX_LOC] + base_off -
2101 			tmp->addr.a32[SDT_NOTE_IDX_BASE];
2102 	else
2103 		tmp->addr.a64[SDT_NOTE_IDX_LOC] =
2104 			tmp->addr.a64[SDT_NOTE_IDX_LOC] + base_off -
2105 			tmp->addr.a64[SDT_NOTE_IDX_BASE];
2106 }
2107 
sdt_adjust_refctr(struct sdt_note * tmp,GElf_Addr base_addr,GElf_Addr base_off)2108 static void sdt_adjust_refctr(struct sdt_note *tmp, GElf_Addr base_addr,
2109 			      GElf_Addr base_off)
2110 {
2111 	if (!base_off)
2112 		return;
2113 
2114 	if (tmp->bit32 && tmp->addr.a32[SDT_NOTE_IDX_REFCTR])
2115 		tmp->addr.a32[SDT_NOTE_IDX_REFCTR] -= (base_addr - base_off);
2116 	else if (tmp->addr.a64[SDT_NOTE_IDX_REFCTR])
2117 		tmp->addr.a64[SDT_NOTE_IDX_REFCTR] -= (base_addr - base_off);
2118 }
2119 
2120 /**
2121  * populate_sdt_note : Parse raw data and identify SDT note
2122  * @elf: elf of the opened file
2123  * @data: raw data of a section with description offset applied
2124  * @len: note description size
2125  * @type: type of the note
2126  * @sdt_notes: List to add the SDT note
2127  *
2128  * Responsible for parsing the @data in section .note.stapsdt in @elf and
2129  * if its an SDT note, it appends to @sdt_notes list.
2130  */
populate_sdt_note(Elf ** elf,const char * data,size_t len,struct list_head * sdt_notes)2131 static int populate_sdt_note(Elf **elf, const char *data, size_t len,
2132 			     struct list_head *sdt_notes)
2133 {
2134 	const char *provider, *name, *args;
2135 	struct sdt_note *tmp = NULL;
2136 	GElf_Ehdr ehdr;
2137 	GElf_Shdr shdr;
2138 	int ret = -EINVAL;
2139 
2140 	union {
2141 		Elf64_Addr a64[NR_ADDR];
2142 		Elf32_Addr a32[NR_ADDR];
2143 	} buf;
2144 
2145 	Elf_Data dst = {
2146 		.d_buf = &buf, .d_type = ELF_T_ADDR, .d_version = EV_CURRENT,
2147 		.d_size = gelf_fsize((*elf), ELF_T_ADDR, NR_ADDR, EV_CURRENT),
2148 		.d_off = 0, .d_align = 0
2149 	};
2150 	Elf_Data src = {
2151 		.d_buf = (void *) data, .d_type = ELF_T_ADDR,
2152 		.d_version = EV_CURRENT, .d_size = dst.d_size, .d_off = 0,
2153 		.d_align = 0
2154 	};
2155 
2156 	tmp = (struct sdt_note *)calloc(1, sizeof(struct sdt_note));
2157 	if (!tmp) {
2158 		ret = -ENOMEM;
2159 		goto out_err;
2160 	}
2161 
2162 	INIT_LIST_HEAD(&tmp->note_list);
2163 
2164 	if (len < dst.d_size + 3)
2165 		goto out_free_note;
2166 
2167 	/* Translation from file representation to memory representation */
2168 	if (gelf_xlatetom(*elf, &dst, &src,
2169 			  elf_getident(*elf, NULL)[EI_DATA]) == NULL) {
2170 		pr_err("gelf_xlatetom : %s\n", elf_errmsg(-1));
2171 		goto out_free_note;
2172 	}
2173 
2174 	/* Populate the fields of sdt_note */
2175 	provider = data + dst.d_size;
2176 
2177 	name = (const char *)memchr(provider, '\0', data + len - provider);
2178 	if (name++ == NULL)
2179 		goto out_free_note;
2180 
2181 	tmp->provider = strdup(provider);
2182 	if (!tmp->provider) {
2183 		ret = -ENOMEM;
2184 		goto out_free_note;
2185 	}
2186 	tmp->name = strdup(name);
2187 	if (!tmp->name) {
2188 		ret = -ENOMEM;
2189 		goto out_free_prov;
2190 	}
2191 
2192 	args = memchr(name, '\0', data + len - name);
2193 
2194 	/*
2195 	 * There is no argument if:
2196 	 * - We reached the end of the note;
2197 	 * - There is not enough room to hold a potential string;
2198 	 * - The argument string is empty or just contains ':'.
2199 	 */
2200 	if (args == NULL || data + len - args < 2 ||
2201 		args[1] == ':' || args[1] == '\0')
2202 		tmp->args = NULL;
2203 	else {
2204 		tmp->args = strdup(++args);
2205 		if (!tmp->args) {
2206 			ret = -ENOMEM;
2207 			goto out_free_name;
2208 		}
2209 	}
2210 
2211 	if (gelf_getclass(*elf) == ELFCLASS32) {
2212 		memcpy(&tmp->addr, &buf, 3 * sizeof(Elf32_Addr));
2213 		tmp->bit32 = true;
2214 	} else {
2215 		memcpy(&tmp->addr, &buf, 3 * sizeof(Elf64_Addr));
2216 		tmp->bit32 = false;
2217 	}
2218 
2219 	if (!gelf_getehdr(*elf, &ehdr)) {
2220 		pr_debug("%s : cannot get elf header.\n", __func__);
2221 		ret = -EBADF;
2222 		goto out_free_args;
2223 	}
2224 
2225 	/* Adjust the prelink effect :
2226 	 * Find out the .stapsdt.base section.
2227 	 * This scn will help us to handle prelinking (if present).
2228 	 * Compare the retrieved file offset of the base section with the
2229 	 * base address in the description of the SDT note. If its different,
2230 	 * then accordingly, adjust the note location.
2231 	 */
2232 	if (elf_section_by_name(*elf, &ehdr, &shdr, SDT_BASE_SCN, NULL))
2233 		sdt_adjust_loc(tmp, shdr.sh_offset);
2234 
2235 	/* Adjust reference counter offset */
2236 	if (elf_section_by_name(*elf, &ehdr, &shdr, SDT_PROBES_SCN, NULL))
2237 		sdt_adjust_refctr(tmp, shdr.sh_addr, shdr.sh_offset);
2238 
2239 	list_add_tail(&tmp->note_list, sdt_notes);
2240 	return 0;
2241 
2242 out_free_args:
2243 	zfree(&tmp->args);
2244 out_free_name:
2245 	zfree(&tmp->name);
2246 out_free_prov:
2247 	zfree(&tmp->provider);
2248 out_free_note:
2249 	free(tmp);
2250 out_err:
2251 	return ret;
2252 }
2253 
2254 /**
2255  * construct_sdt_notes_list : constructs a list of SDT notes
2256  * @elf : elf to look into
2257  * @sdt_notes : empty list_head
2258  *
2259  * Scans the sections in 'elf' for the section
2260  * .note.stapsdt. It, then calls populate_sdt_note to find
2261  * out the SDT events and populates the 'sdt_notes'.
2262  */
construct_sdt_notes_list(Elf * elf,struct list_head * sdt_notes)2263 static int construct_sdt_notes_list(Elf *elf, struct list_head *sdt_notes)
2264 {
2265 	GElf_Ehdr ehdr;
2266 	Elf_Scn *scn = NULL;
2267 	Elf_Data *data;
2268 	GElf_Shdr shdr;
2269 	size_t shstrndx, next;
2270 	GElf_Nhdr nhdr;
2271 	size_t name_off, desc_off, offset;
2272 	int ret = 0;
2273 
2274 	if (gelf_getehdr(elf, &ehdr) == NULL) {
2275 		ret = -EBADF;
2276 		goto out_ret;
2277 	}
2278 	if (elf_getshdrstrndx(elf, &shstrndx) != 0) {
2279 		ret = -EBADF;
2280 		goto out_ret;
2281 	}
2282 
2283 	/* Look for the required section */
2284 	scn = elf_section_by_name(elf, &ehdr, &shdr, SDT_NOTE_SCN, NULL);
2285 	if (!scn) {
2286 		ret = -ENOENT;
2287 		goto out_ret;
2288 	}
2289 
2290 	if ((shdr.sh_type != SHT_NOTE) || (shdr.sh_flags & SHF_ALLOC)) {
2291 		ret = -ENOENT;
2292 		goto out_ret;
2293 	}
2294 
2295 	data = elf_getdata(scn, NULL);
2296 
2297 	/* Get the SDT notes */
2298 	for (offset = 0; (next = gelf_getnote(data, offset, &nhdr, &name_off,
2299 					      &desc_off)) > 0; offset = next) {
2300 		if (nhdr.n_namesz == sizeof(SDT_NOTE_NAME) &&
2301 		    !memcmp(data->d_buf + name_off, SDT_NOTE_NAME,
2302 			    sizeof(SDT_NOTE_NAME))) {
2303 			/* Check the type of the note */
2304 			if (nhdr.n_type != SDT_NOTE_TYPE)
2305 				goto out_ret;
2306 
2307 			ret = populate_sdt_note(&elf, ((data->d_buf) + desc_off),
2308 						nhdr.n_descsz, sdt_notes);
2309 			if (ret < 0)
2310 				goto out_ret;
2311 		}
2312 	}
2313 	if (list_empty(sdt_notes))
2314 		ret = -ENOENT;
2315 
2316 out_ret:
2317 	return ret;
2318 }
2319 
2320 /**
2321  * get_sdt_note_list : Wrapper to construct a list of sdt notes
2322  * @head : empty list_head
2323  * @target : file to find SDT notes from
2324  *
2325  * This opens the file, initializes
2326  * the ELF and then calls construct_sdt_notes_list.
2327  */
get_sdt_note_list(struct list_head * head,const char * target)2328 int get_sdt_note_list(struct list_head *head, const char *target)
2329 {
2330 	Elf *elf;
2331 	int fd, ret;
2332 
2333 	fd = open(target, O_RDONLY);
2334 	if (fd < 0)
2335 		return -EBADF;
2336 
2337 	elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
2338 	if (!elf) {
2339 		ret = -EBADF;
2340 		goto out_close;
2341 	}
2342 	ret = construct_sdt_notes_list(elf, head);
2343 	elf_end(elf);
2344 out_close:
2345 	close(fd);
2346 	return ret;
2347 }
2348 
2349 /**
2350  * cleanup_sdt_note_list : free the sdt notes' list
2351  * @sdt_notes: sdt notes' list
2352  *
2353  * Free up the SDT notes in @sdt_notes.
2354  * Returns the number of SDT notes free'd.
2355  */
cleanup_sdt_note_list(struct list_head * sdt_notes)2356 int cleanup_sdt_note_list(struct list_head *sdt_notes)
2357 {
2358 	struct sdt_note *tmp, *pos;
2359 	int nr_free = 0;
2360 
2361 	list_for_each_entry_safe(pos, tmp, sdt_notes, note_list) {
2362 		list_del_init(&pos->note_list);
2363 		zfree(&pos->name);
2364 		zfree(&pos->provider);
2365 		free(pos);
2366 		nr_free++;
2367 	}
2368 	return nr_free;
2369 }
2370 
2371 /**
2372  * sdt_notes__get_count: Counts the number of sdt events
2373  * @start: list_head to sdt_notes list
2374  *
2375  * Returns the number of SDT notes in a list
2376  */
sdt_notes__get_count(struct list_head * start)2377 int sdt_notes__get_count(struct list_head *start)
2378 {
2379 	struct sdt_note *sdt_ptr;
2380 	int count = 0;
2381 
2382 	list_for_each_entry(sdt_ptr, start, note_list)
2383 		count++;
2384 	return count;
2385 }
2386 #endif
2387 
symbol__elf_init(void)2388 void symbol__elf_init(void)
2389 {
2390 	elf_version(EV_CURRENT);
2391 }
2392