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 "compress.h"
11 #include "dso.h"
12 #include "map.h"
13 #include "maps.h"
14 #include "symbol.h"
15 #include "symsrc.h"
16 #include "demangle-cxx.h"
17 #include "demangle-ocaml.h"
18 #include "demangle-java.h"
19 #include "demangle-rust.h"
20 #include "machine.h"
21 #include "vdso.h"
22 #include "debug.h"
23 #include "util/copyfile.h"
24 #include <linux/ctype.h>
25 #include <linux/kernel.h>
26 #include <linux/zalloc.h>
27 #include <linux/string.h>
28 #include <symbol/kallsyms.h>
29 #include <internal/lib.h>
30 
31 #ifdef HAVE_LIBBFD_SUPPORT
32 #define PACKAGE 'perf'
33 #include <bfd.h>
34 #endif
35 
36 #if defined(HAVE_LIBBFD_SUPPORT) || defined(HAVE_CPLUS_DEMANGLE_SUPPORT)
37 #ifndef DMGL_PARAMS
38 #define DMGL_PARAMS     (1 << 0)  /* Include function args */
39 #define DMGL_ANSI       (1 << 1)  /* Include const, volatile, etc */
40 #endif
41 #endif
42 
43 #ifndef EM_AARCH64
44 #define EM_AARCH64	183  /* ARM 64 bit */
45 #endif
46 
47 #ifndef EM_LOONGARCH
48 #define EM_LOONGARCH	258
49 #endif
50 
51 #ifndef ELF32_ST_VISIBILITY
52 #define ELF32_ST_VISIBILITY(o)	((o) & 0x03)
53 #endif
54 
55 /* For ELF64 the definitions are the same.  */
56 #ifndef ELF64_ST_VISIBILITY
57 #define ELF64_ST_VISIBILITY(o)	ELF32_ST_VISIBILITY (o)
58 #endif
59 
60 /* How to extract information held in the st_other field.  */
61 #ifndef GELF_ST_VISIBILITY
62 #define GELF_ST_VISIBILITY(val)	ELF64_ST_VISIBILITY (val)
63 #endif
64 
65 typedef Elf64_Nhdr GElf_Nhdr;
66 
67 
68 #ifndef HAVE_ELF_GETPHDRNUM_SUPPORT
elf_getphdrnum(Elf * elf,size_t * dst)69 static int elf_getphdrnum(Elf *elf, size_t *dst)
70 {
71 	GElf_Ehdr gehdr;
72 	GElf_Ehdr *ehdr;
73 
74 	ehdr = gelf_getehdr(elf, &gehdr);
75 	if (!ehdr)
76 		return -1;
77 
78 	*dst = ehdr->e_phnum;
79 
80 	return 0;
81 }
82 #endif
83 
84 #ifndef HAVE_ELF_GETSHDRSTRNDX_SUPPORT
elf_getshdrstrndx(Elf * elf __maybe_unused,size_t * dst __maybe_unused)85 static int elf_getshdrstrndx(Elf *elf __maybe_unused, size_t *dst __maybe_unused)
86 {
87 	pr_err("%s: update your libelf to > 0.140, this one lacks elf_getshdrstrndx().\n", __func__);
88 	return -1;
89 }
90 #endif
91 
92 #ifndef NT_GNU_BUILD_ID
93 #define NT_GNU_BUILD_ID 3
94 #endif
95 
96 /**
97  * elf_symtab__for_each_symbol - iterate thru all the symbols
98  *
99  * @syms: struct elf_symtab instance to iterate
100  * @idx: uint32_t idx
101  * @sym: GElf_Sym iterator
102  */
103 #define elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) \
104 	for (idx = 0, gelf_getsym(syms, idx, &sym);\
105 	     idx < nr_syms; \
106 	     idx++, gelf_getsym(syms, idx, &sym))
107 
elf_sym__type(const GElf_Sym * sym)108 static inline uint8_t elf_sym__type(const GElf_Sym *sym)
109 {
110 	return GELF_ST_TYPE(sym->st_info);
111 }
112 
elf_sym__visibility(const GElf_Sym * sym)113 static inline uint8_t elf_sym__visibility(const GElf_Sym *sym)
114 {
115 	return GELF_ST_VISIBILITY(sym->st_other);
116 }
117 
118 #ifndef STT_GNU_IFUNC
119 #define STT_GNU_IFUNC 10
120 #endif
121 
elf_sym__is_function(const GElf_Sym * sym)122 static inline int elf_sym__is_function(const GElf_Sym *sym)
123 {
124 	return (elf_sym__type(sym) == STT_FUNC ||
125 		elf_sym__type(sym) == STT_GNU_IFUNC) &&
126 	       sym->st_name != 0 &&
127 	       sym->st_shndx != SHN_UNDEF;
128 }
129 
elf_sym__is_object(const GElf_Sym * sym)130 static inline bool elf_sym__is_object(const GElf_Sym *sym)
131 {
132 	return elf_sym__type(sym) == STT_OBJECT &&
133 		sym->st_name != 0 &&
134 		sym->st_shndx != SHN_UNDEF;
135 }
136 
elf_sym__is_label(const GElf_Sym * sym)137 static inline int elf_sym__is_label(const GElf_Sym *sym)
138 {
139 	return elf_sym__type(sym) == STT_NOTYPE &&
140 		sym->st_name != 0 &&
141 		sym->st_shndx != SHN_UNDEF &&
142 		sym->st_shndx != SHN_ABS &&
143 		elf_sym__visibility(sym) != STV_HIDDEN &&
144 		elf_sym__visibility(sym) != STV_INTERNAL;
145 }
146 
elf_sym__filter(GElf_Sym * sym)147 static bool elf_sym__filter(GElf_Sym *sym)
148 {
149 	return elf_sym__is_function(sym) || elf_sym__is_object(sym);
150 }
151 
elf_sym__name(const GElf_Sym * sym,const Elf_Data * symstrs)152 static inline const char *elf_sym__name(const GElf_Sym *sym,
153 					const Elf_Data *symstrs)
154 {
155 	return symstrs->d_buf + sym->st_name;
156 }
157 
elf_sec__name(const GElf_Shdr * shdr,const Elf_Data * secstrs)158 static inline const char *elf_sec__name(const GElf_Shdr *shdr,
159 					const Elf_Data *secstrs)
160 {
161 	return secstrs->d_buf + shdr->sh_name;
162 }
163 
elf_sec__is_text(const GElf_Shdr * shdr,const Elf_Data * secstrs)164 static inline int elf_sec__is_text(const GElf_Shdr *shdr,
165 					const Elf_Data *secstrs)
166 {
167 	return strstr(elf_sec__name(shdr, secstrs), "text") != NULL;
168 }
169 
elf_sec__is_data(const GElf_Shdr * shdr,const Elf_Data * secstrs)170 static inline bool elf_sec__is_data(const GElf_Shdr *shdr,
171 				    const Elf_Data *secstrs)
172 {
173 	return strstr(elf_sec__name(shdr, secstrs), "data") != NULL;
174 }
175 
elf_sec__filter(GElf_Shdr * shdr,Elf_Data * secstrs)176 static bool elf_sec__filter(GElf_Shdr *shdr, Elf_Data *secstrs)
177 {
178 	return elf_sec__is_text(shdr, secstrs) ||
179 	       elf_sec__is_data(shdr, secstrs);
180 }
181 
elf_addr_to_index(Elf * elf,GElf_Addr addr)182 static size_t elf_addr_to_index(Elf *elf, GElf_Addr addr)
183 {
184 	Elf_Scn *sec = NULL;
185 	GElf_Shdr shdr;
186 	size_t cnt = 1;
187 
188 	while ((sec = elf_nextscn(elf, sec)) != NULL) {
189 		gelf_getshdr(sec, &shdr);
190 
191 		if ((addr >= shdr.sh_addr) &&
192 		    (addr < (shdr.sh_addr + shdr.sh_size)))
193 			return cnt;
194 
195 		++cnt;
196 	}
197 
198 	return -1;
199 }
200 
elf_section_by_name(Elf * elf,GElf_Ehdr * ep,GElf_Shdr * shp,const char * name,size_t * idx)201 Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep,
202 			     GElf_Shdr *shp, const char *name, size_t *idx)
203 {
204 	Elf_Scn *sec = NULL;
205 	size_t cnt = 1;
206 
207 	/* ELF is corrupted/truncated, avoid calling elf_strptr. */
208 	if (!elf_rawdata(elf_getscn(elf, ep->e_shstrndx), NULL))
209 		return NULL;
210 
211 	while ((sec = elf_nextscn(elf, sec)) != NULL) {
212 		char *str;
213 
214 		gelf_getshdr(sec, shp);
215 		str = elf_strptr(elf, ep->e_shstrndx, shp->sh_name);
216 		if (str && !strcmp(name, str)) {
217 			if (idx)
218 				*idx = cnt;
219 			return sec;
220 		}
221 		++cnt;
222 	}
223 
224 	return NULL;
225 }
226 
filename__has_section(const char * filename,const char * sec)227 bool filename__has_section(const char *filename, const char *sec)
228 {
229 	int fd;
230 	Elf *elf;
231 	GElf_Ehdr ehdr;
232 	GElf_Shdr shdr;
233 	bool found = false;
234 
235 	fd = open(filename, O_RDONLY);
236 	if (fd < 0)
237 		return false;
238 
239 	elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
240 	if (elf == NULL)
241 		goto out;
242 
243 	if (gelf_getehdr(elf, &ehdr) == NULL)
244 		goto elf_out;
245 
246 	found = !!elf_section_by_name(elf, &ehdr, &shdr, sec, NULL);
247 
248 elf_out:
249 	elf_end(elf);
250 out:
251 	close(fd);
252 	return found;
253 }
254 
elf_read_program_header(Elf * elf,u64 vaddr,GElf_Phdr * phdr)255 static int elf_read_program_header(Elf *elf, u64 vaddr, GElf_Phdr *phdr)
256 {
257 	size_t i, phdrnum;
258 	u64 sz;
259 
260 	if (elf_getphdrnum(elf, &phdrnum))
261 		return -1;
262 
263 	for (i = 0; i < phdrnum; i++) {
264 		if (gelf_getphdr(elf, i, phdr) == NULL)
265 			return -1;
266 
267 		if (phdr->p_type != PT_LOAD)
268 			continue;
269 
270 		sz = max(phdr->p_memsz, phdr->p_filesz);
271 		if (!sz)
272 			continue;
273 
274 		if (vaddr >= phdr->p_vaddr && (vaddr < phdr->p_vaddr + sz))
275 			return 0;
276 	}
277 
278 	/* Not found any valid program header */
279 	return -1;
280 }
281 
want_demangle(bool is_kernel_sym)282 static bool want_demangle(bool is_kernel_sym)
283 {
284 	return is_kernel_sym ? symbol_conf.demangle_kernel : symbol_conf.demangle;
285 }
286 
287 /*
288  * Demangle C++ function signature, typically replaced by demangle-cxx.cpp
289  * version.
290  */
291 #ifndef HAVE_CXA_DEMANGLE_SUPPORT
cxx_demangle_sym(const char * str __maybe_unused,bool params __maybe_unused,bool modifiers __maybe_unused)292 char *cxx_demangle_sym(const char *str __maybe_unused, bool params __maybe_unused,
293 		       bool modifiers __maybe_unused)
294 {
295 #ifdef HAVE_LIBBFD_SUPPORT
296 	int flags = (params ? DMGL_PARAMS : 0) | (modifiers ? DMGL_ANSI : 0);
297 
298 	return bfd_demangle(NULL, str, flags);
299 #elif defined(HAVE_CPLUS_DEMANGLE_SUPPORT)
300 	int flags = (params ? DMGL_PARAMS : 0) | (modifiers ? DMGL_ANSI : 0);
301 
302 	return cplus_demangle(str, flags);
303 #else
304 	return NULL;
305 #endif
306 }
307 #endif /* !HAVE_CXA_DEMANGLE_SUPPORT */
308 
demangle_sym(struct dso * dso,int kmodule,const char * elf_name)309 static char *demangle_sym(struct dso *dso, int kmodule, const char *elf_name)
310 {
311 	char *demangled = NULL;
312 
313 	/*
314 	 * We need to figure out if the object was created from C++ sources
315 	 * DWARF DW_compile_unit has this, but we don't always have access
316 	 * to it...
317 	 */
318 	if (!want_demangle(dso__kernel(dso) || kmodule))
319 		return demangled;
320 
321 	demangled = cxx_demangle_sym(elf_name, verbose > 0, verbose > 0);
322 	if (demangled == NULL) {
323 		demangled = ocaml_demangle_sym(elf_name);
324 		if (demangled == NULL) {
325 			demangled = java_demangle_sym(elf_name, JAVA_DEMANGLE_NORET);
326 		}
327 	}
328 	else if (rust_is_mangled(demangled))
329 		/*
330 		    * Input to Rust demangling is the BFD-demangled
331 		    * name which it Rust-demangles in place.
332 		    */
333 		rust_demangle_sym(demangled);
334 
335 	return demangled;
336 }
337 
338 struct rel_info {
339 	u32		nr_entries;
340 	u32		*sorted;
341 	bool		is_rela;
342 	Elf_Data	*reldata;
343 	GElf_Rela	rela;
344 	GElf_Rel	rel;
345 };
346 
get_rel_symidx(struct rel_info * ri,u32 idx)347 static u32 get_rel_symidx(struct rel_info *ri, u32 idx)
348 {
349 	idx = ri->sorted ? ri->sorted[idx] : idx;
350 	if (ri->is_rela) {
351 		gelf_getrela(ri->reldata, idx, &ri->rela);
352 		return GELF_R_SYM(ri->rela.r_info);
353 	}
354 	gelf_getrel(ri->reldata, idx, &ri->rel);
355 	return GELF_R_SYM(ri->rel.r_info);
356 }
357 
get_rel_offset(struct rel_info * ri,u32 x)358 static u64 get_rel_offset(struct rel_info *ri, u32 x)
359 {
360 	if (ri->is_rela) {
361 		GElf_Rela rela;
362 
363 		gelf_getrela(ri->reldata, x, &rela);
364 		return rela.r_offset;
365 	} else {
366 		GElf_Rel rel;
367 
368 		gelf_getrel(ri->reldata, x, &rel);
369 		return rel.r_offset;
370 	}
371 }
372 
rel_cmp(const void * a,const void * b,void * r)373 static int rel_cmp(const void *a, const void *b, void *r)
374 {
375 	struct rel_info *ri = r;
376 	u64 a_offset = get_rel_offset(ri, *(const u32 *)a);
377 	u64 b_offset = get_rel_offset(ri, *(const u32 *)b);
378 
379 	return a_offset < b_offset ? -1 : (a_offset > b_offset ? 1 : 0);
380 }
381 
sort_rel(struct rel_info * ri)382 static int sort_rel(struct rel_info *ri)
383 {
384 	size_t sz = sizeof(ri->sorted[0]);
385 	u32 i;
386 
387 	ri->sorted = calloc(ri->nr_entries, sz);
388 	if (!ri->sorted)
389 		return -1;
390 	for (i = 0; i < ri->nr_entries; i++)
391 		ri->sorted[i] = i;
392 	qsort_r(ri->sorted, ri->nr_entries, sz, rel_cmp, ri);
393 	return 0;
394 }
395 
396 /*
397  * For x86_64, the GNU linker is putting IFUNC information in the relocation
398  * addend.
399  */
addend_may_be_ifunc(GElf_Ehdr * ehdr,struct rel_info * ri)400 static bool addend_may_be_ifunc(GElf_Ehdr *ehdr, struct rel_info *ri)
401 {
402 	return ehdr->e_machine == EM_X86_64 && ri->is_rela &&
403 	       GELF_R_TYPE(ri->rela.r_info) == R_X86_64_IRELATIVE;
404 }
405 
get_ifunc_name(Elf * elf,struct dso * dso,GElf_Ehdr * ehdr,struct rel_info * ri,char * buf,size_t buf_sz)406 static bool get_ifunc_name(Elf *elf, struct dso *dso, GElf_Ehdr *ehdr,
407 			   struct rel_info *ri, char *buf, size_t buf_sz)
408 {
409 	u64 addr = ri->rela.r_addend;
410 	struct symbol *sym;
411 	GElf_Phdr phdr;
412 
413 	if (!addend_may_be_ifunc(ehdr, ri))
414 		return false;
415 
416 	if (elf_read_program_header(elf, addr, &phdr))
417 		return false;
418 
419 	addr -= phdr.p_vaddr - phdr.p_offset;
420 
421 	sym = dso__find_symbol_nocache(dso, addr);
422 
423 	/* Expecting the address to be an IFUNC or IFUNC alias */
424 	if (!sym || sym->start != addr || (sym->type != STT_GNU_IFUNC && !sym->ifunc_alias))
425 		return false;
426 
427 	snprintf(buf, buf_sz, "%s@plt", sym->name);
428 
429 	return true;
430 }
431 
exit_rel(struct rel_info * ri)432 static void exit_rel(struct rel_info *ri)
433 {
434 	zfree(&ri->sorted);
435 }
436 
get_plt_sizes(struct dso * dso,GElf_Ehdr * ehdr,GElf_Shdr * shdr_plt,u64 * plt_header_size,u64 * plt_entry_size)437 static bool get_plt_sizes(struct dso *dso, GElf_Ehdr *ehdr, GElf_Shdr *shdr_plt,
438 			  u64 *plt_header_size, u64 *plt_entry_size)
439 {
440 	switch (ehdr->e_machine) {
441 	case EM_ARM:
442 		*plt_header_size = 20;
443 		*plt_entry_size = 12;
444 		return true;
445 	case EM_AARCH64:
446 		*plt_header_size = 32;
447 		*plt_entry_size = 16;
448 		return true;
449 	case EM_LOONGARCH:
450 		*plt_header_size = 32;
451 		*plt_entry_size = 16;
452 		return true;
453 	case EM_SPARC:
454 		*plt_header_size = 48;
455 		*plt_entry_size = 12;
456 		return true;
457 	case EM_SPARCV9:
458 		*plt_header_size = 128;
459 		*plt_entry_size = 32;
460 		return true;
461 	case EM_386:
462 	case EM_X86_64:
463 		*plt_entry_size = shdr_plt->sh_entsize;
464 		/* Size is 8 or 16, if not, assume alignment indicates size */
465 		if (*plt_entry_size != 8 && *plt_entry_size != 16)
466 			*plt_entry_size = shdr_plt->sh_addralign == 8 ? 8 : 16;
467 		*plt_header_size = *plt_entry_size;
468 		break;
469 	default: /* FIXME: s390/alpha/mips/parisc/poperpc/sh/xtensa need to be checked */
470 		*plt_header_size = shdr_plt->sh_entsize;
471 		*plt_entry_size = shdr_plt->sh_entsize;
472 		break;
473 	}
474 	if (*plt_entry_size)
475 		return true;
476 	pr_debug("Missing PLT entry size for %s\n", dso__long_name(dso));
477 	return false;
478 }
479 
machine_is_x86(GElf_Half e_machine)480 static bool machine_is_x86(GElf_Half e_machine)
481 {
482 	return e_machine == EM_386 || e_machine == EM_X86_64;
483 }
484 
485 struct rela_dyn {
486 	GElf_Addr	offset;
487 	u32		sym_idx;
488 };
489 
490 struct rela_dyn_info {
491 	struct dso	*dso;
492 	Elf_Data	*plt_got_data;
493 	u32		nr_entries;
494 	struct rela_dyn	*sorted;
495 	Elf_Data	*dynsym_data;
496 	Elf_Data	*dynstr_data;
497 	Elf_Data	*rela_dyn_data;
498 };
499 
exit_rela_dyn(struct rela_dyn_info * di)500 static void exit_rela_dyn(struct rela_dyn_info *di)
501 {
502 	zfree(&di->sorted);
503 }
504 
cmp_offset(const void * a,const void * b)505 static int cmp_offset(const void *a, const void *b)
506 {
507 	const struct rela_dyn *va = a;
508 	const struct rela_dyn *vb = b;
509 
510 	return va->offset < vb->offset ? -1 : (va->offset > vb->offset ? 1 : 0);
511 }
512 
sort_rela_dyn(struct rela_dyn_info * di)513 static int sort_rela_dyn(struct rela_dyn_info *di)
514 {
515 	u32 i, n;
516 
517 	di->sorted = calloc(di->nr_entries, sizeof(di->sorted[0]));
518 	if (!di->sorted)
519 		return -1;
520 
521 	/* Get data for sorting: the offset and symbol index */
522 	for (i = 0, n = 0; i < di->nr_entries; i++) {
523 		GElf_Rela rela;
524 		u32 sym_idx;
525 
526 		gelf_getrela(di->rela_dyn_data, i, &rela);
527 		sym_idx = GELF_R_SYM(rela.r_info);
528 		if (sym_idx) {
529 			di->sorted[n].sym_idx = sym_idx;
530 			di->sorted[n].offset = rela.r_offset;
531 			n += 1;
532 		}
533 	}
534 
535 	/* Sort by offset */
536 	di->nr_entries = n;
537 	qsort(di->sorted, n, sizeof(di->sorted[0]), cmp_offset);
538 
539 	return 0;
540 }
541 
get_rela_dyn_info(Elf * elf,GElf_Ehdr * ehdr,struct rela_dyn_info * di,Elf_Scn * scn)542 static void get_rela_dyn_info(Elf *elf, GElf_Ehdr *ehdr, struct rela_dyn_info *di, Elf_Scn *scn)
543 {
544 	GElf_Shdr rela_dyn_shdr;
545 	GElf_Shdr shdr;
546 
547 	di->plt_got_data = elf_getdata(scn, NULL);
548 
549 	scn = elf_section_by_name(elf, ehdr, &rela_dyn_shdr, ".rela.dyn", NULL);
550 	if (!scn || !rela_dyn_shdr.sh_link || !rela_dyn_shdr.sh_entsize)
551 		return;
552 
553 	di->nr_entries = rela_dyn_shdr.sh_size / rela_dyn_shdr.sh_entsize;
554 	di->rela_dyn_data = elf_getdata(scn, NULL);
555 
556 	scn = elf_getscn(elf, rela_dyn_shdr.sh_link);
557 	if (!scn || !gelf_getshdr(scn, &shdr) || !shdr.sh_link)
558 		return;
559 
560 	di->dynsym_data = elf_getdata(scn, NULL);
561 	di->dynstr_data = elf_getdata(elf_getscn(elf, shdr.sh_link), NULL);
562 
563 	if (!di->plt_got_data || !di->dynstr_data || !di->dynsym_data || !di->rela_dyn_data)
564 		return;
565 
566 	/* Sort into offset order */
567 	sort_rela_dyn(di);
568 }
569 
570 /* Get instruction displacement from a plt entry for x86_64 */
get_x86_64_plt_disp(const u8 * p)571 static u32 get_x86_64_plt_disp(const u8 *p)
572 {
573 	u8 endbr64[] = {0xf3, 0x0f, 0x1e, 0xfa};
574 	int n = 0;
575 
576 	/* Skip endbr64 */
577 	if (!memcmp(p, endbr64, sizeof(endbr64)))
578 		n += sizeof(endbr64);
579 	/* Skip bnd prefix */
580 	if (p[n] == 0xf2)
581 		n += 1;
582 	/* jmp with 4-byte displacement */
583 	if (p[n] == 0xff && p[n + 1] == 0x25) {
584 		u32 disp;
585 
586 		n += 2;
587 		/* Also add offset from start of entry to end of instruction */
588 		memcpy(&disp, p + n, sizeof(disp));
589 		return n + 4 + le32toh(disp);
590 	}
591 	return 0;
592 }
593 
get_plt_got_name(GElf_Shdr * shdr,size_t i,struct rela_dyn_info * di,char * buf,size_t buf_sz)594 static bool get_plt_got_name(GElf_Shdr *shdr, size_t i,
595 			     struct rela_dyn_info *di,
596 			     char *buf, size_t buf_sz)
597 {
598 	struct rela_dyn vi, *vr;
599 	const char *sym_name;
600 	char *demangled;
601 	GElf_Sym sym;
602 	bool result;
603 	u32 disp;
604 
605 	if (!di->sorted)
606 		return false;
607 
608 	disp = get_x86_64_plt_disp(di->plt_got_data->d_buf + i);
609 	if (!disp)
610 		return false;
611 
612 	/* Compute target offset of the .plt.got entry */
613 	vi.offset = shdr->sh_offset + di->plt_got_data->d_off + i + disp;
614 
615 	/* Find that offset in .rela.dyn (sorted by offset) */
616 	vr = bsearch(&vi, di->sorted, di->nr_entries, sizeof(di->sorted[0]), cmp_offset);
617 	if (!vr)
618 		return false;
619 
620 	/* Get the associated symbol */
621 	gelf_getsym(di->dynsym_data, vr->sym_idx, &sym);
622 	sym_name = elf_sym__name(&sym, di->dynstr_data);
623 	demangled = demangle_sym(di->dso, 0, sym_name);
624 	if (demangled != NULL)
625 		sym_name = demangled;
626 
627 	snprintf(buf, buf_sz, "%s@plt", sym_name);
628 
629 	result = *sym_name;
630 
631 	free(demangled);
632 
633 	return result;
634 }
635 
dso__synthesize_plt_got_symbols(struct dso * dso,Elf * elf,GElf_Ehdr * ehdr,char * buf,size_t buf_sz)636 static int dso__synthesize_plt_got_symbols(struct dso *dso, Elf *elf,
637 					   GElf_Ehdr *ehdr,
638 					   char *buf, size_t buf_sz)
639 {
640 	struct rela_dyn_info di = { .dso = dso };
641 	struct symbol *sym;
642 	GElf_Shdr shdr;
643 	Elf_Scn *scn;
644 	int err = -1;
645 	size_t i;
646 
647 	scn = elf_section_by_name(elf, ehdr, &shdr, ".plt.got", NULL);
648 	if (!scn || !shdr.sh_entsize)
649 		return 0;
650 
651 	if (ehdr->e_machine == EM_X86_64)
652 		get_rela_dyn_info(elf, ehdr, &di, scn);
653 
654 	for (i = 0; i < shdr.sh_size; i += shdr.sh_entsize) {
655 		if (!get_plt_got_name(&shdr, i, &di, buf, buf_sz))
656 			snprintf(buf, buf_sz, "offset_%#" PRIx64 "@plt", (u64)shdr.sh_offset + i);
657 		sym = symbol__new(shdr.sh_offset + i, shdr.sh_entsize, STB_GLOBAL, STT_FUNC, buf);
658 		if (!sym)
659 			goto out;
660 		symbols__insert(dso__symbols(dso), sym);
661 	}
662 	err = 0;
663 out:
664 	exit_rela_dyn(&di);
665 	return err;
666 }
667 
668 /*
669  * We need to check if we have a .dynsym, so that we can handle the
670  * .plt, synthesizing its symbols, that aren't on the symtabs (be it
671  * .dynsym or .symtab).
672  * And always look at the original dso, not at debuginfo packages, that
673  * have the PLT data stripped out (shdr_rel_plt.sh_type == SHT_NOBITS).
674  */
dso__synthesize_plt_symbols(struct dso * dso,struct symsrc * ss)675 int dso__synthesize_plt_symbols(struct dso *dso, struct symsrc *ss)
676 {
677 	uint32_t idx;
678 	GElf_Sym sym;
679 	u64 plt_offset, plt_header_size, plt_entry_size;
680 	GElf_Shdr shdr_plt, plt_sec_shdr;
681 	struct symbol *f, *plt_sym;
682 	GElf_Shdr shdr_rel_plt, shdr_dynsym;
683 	Elf_Data *syms, *symstrs;
684 	Elf_Scn *scn_plt_rel, *scn_symstrs, *scn_dynsym;
685 	GElf_Ehdr ehdr;
686 	char sympltname[1024];
687 	Elf *elf;
688 	int nr = 0, err = -1;
689 	struct rel_info ri = { .is_rela = false };
690 	bool lazy_plt;
691 
692 	elf = ss->elf;
693 	ehdr = ss->ehdr;
694 
695 	if (!elf_section_by_name(elf, &ehdr, &shdr_plt, ".plt", NULL))
696 		return 0;
697 
698 	/*
699 	 * A symbol from a previous section (e.g. .init) can have been expanded
700 	 * by symbols__fixup_end() to overlap .plt. Truncate it before adding
701 	 * a symbol for .plt header.
702 	 */
703 	f = dso__find_symbol_nocache(dso, shdr_plt.sh_offset);
704 	if (f && f->start < shdr_plt.sh_offset && f->end > shdr_plt.sh_offset)
705 		f->end = shdr_plt.sh_offset;
706 
707 	if (!get_plt_sizes(dso, &ehdr, &shdr_plt, &plt_header_size, &plt_entry_size))
708 		return 0;
709 
710 	/* Add a symbol for .plt header */
711 	plt_sym = symbol__new(shdr_plt.sh_offset, plt_header_size, STB_GLOBAL, STT_FUNC, ".plt");
712 	if (!plt_sym)
713 		goto out_elf_end;
714 	symbols__insert(dso__symbols(dso), plt_sym);
715 
716 	/* Only x86 has .plt.got */
717 	if (machine_is_x86(ehdr.e_machine) &&
718 	    dso__synthesize_plt_got_symbols(dso, elf, &ehdr, sympltname, sizeof(sympltname)))
719 		goto out_elf_end;
720 
721 	/* Only x86 has .plt.sec */
722 	if (machine_is_x86(ehdr.e_machine) &&
723 	    elf_section_by_name(elf, &ehdr, &plt_sec_shdr, ".plt.sec", NULL)) {
724 		if (!get_plt_sizes(dso, &ehdr, &plt_sec_shdr, &plt_header_size, &plt_entry_size))
725 			return 0;
726 		/* Extend .plt symbol to entire .plt */
727 		plt_sym->end = plt_sym->start + shdr_plt.sh_size;
728 		/* Use .plt.sec offset */
729 		plt_offset = plt_sec_shdr.sh_offset;
730 		lazy_plt = false;
731 	} else {
732 		plt_offset = shdr_plt.sh_offset;
733 		lazy_plt = true;
734 	}
735 
736 	scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt,
737 					  ".rela.plt", NULL);
738 	if (scn_plt_rel == NULL) {
739 		scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt,
740 						  ".rel.plt", NULL);
741 		if (scn_plt_rel == NULL)
742 			return 0;
743 	}
744 
745 	if (shdr_rel_plt.sh_type != SHT_RELA &&
746 	    shdr_rel_plt.sh_type != SHT_REL)
747 		return 0;
748 
749 	if (!shdr_rel_plt.sh_link)
750 		return 0;
751 
752 	if (shdr_rel_plt.sh_link == ss->dynsym_idx) {
753 		scn_dynsym = ss->dynsym;
754 		shdr_dynsym = ss->dynshdr;
755 	} else if (shdr_rel_plt.sh_link == ss->symtab_idx) {
756 		/*
757 		 * A static executable can have a .plt due to IFUNCs, in which
758 		 * case .symtab is used not .dynsym.
759 		 */
760 		scn_dynsym = ss->symtab;
761 		shdr_dynsym = ss->symshdr;
762 	} else {
763 		goto out_elf_end;
764 	}
765 
766 	if (!scn_dynsym)
767 		return 0;
768 
769 	/*
770 	 * Fetch the relocation section to find the idxes to the GOT
771 	 * and the symbols in the .dynsym they refer to.
772 	 */
773 	ri.reldata = elf_getdata(scn_plt_rel, NULL);
774 	if (!ri.reldata)
775 		goto out_elf_end;
776 
777 	syms = elf_getdata(scn_dynsym, NULL);
778 	if (syms == NULL)
779 		goto out_elf_end;
780 
781 	scn_symstrs = elf_getscn(elf, shdr_dynsym.sh_link);
782 	if (scn_symstrs == NULL)
783 		goto out_elf_end;
784 
785 	symstrs = elf_getdata(scn_symstrs, NULL);
786 	if (symstrs == NULL)
787 		goto out_elf_end;
788 
789 	if (symstrs->d_size == 0)
790 		goto out_elf_end;
791 
792 	ri.nr_entries = shdr_rel_plt.sh_size / shdr_rel_plt.sh_entsize;
793 
794 	ri.is_rela = shdr_rel_plt.sh_type == SHT_RELA;
795 
796 	if (lazy_plt) {
797 		/*
798 		 * Assume a .plt with the same number of entries as the number
799 		 * of relocation entries is not lazy and does not have a header.
800 		 */
801 		if (ri.nr_entries * plt_entry_size == shdr_plt.sh_size)
802 			dso__delete_symbol(dso, plt_sym);
803 		else
804 			plt_offset += plt_header_size;
805 	}
806 
807 	/*
808 	 * x86 doesn't insert IFUNC relocations in .plt order, so sort to get
809 	 * back in order.
810 	 */
811 	if (machine_is_x86(ehdr.e_machine) && sort_rel(&ri))
812 		goto out_elf_end;
813 
814 	for (idx = 0; idx < ri.nr_entries; idx++) {
815 		const char *elf_name = NULL;
816 		char *demangled = NULL;
817 
818 		gelf_getsym(syms, get_rel_symidx(&ri, idx), &sym);
819 
820 		elf_name = elf_sym__name(&sym, symstrs);
821 		demangled = demangle_sym(dso, 0, elf_name);
822 		if (demangled)
823 			elf_name = demangled;
824 		if (*elf_name)
825 			snprintf(sympltname, sizeof(sympltname), "%s@plt", elf_name);
826 		else if (!get_ifunc_name(elf, dso, &ehdr, &ri, sympltname, sizeof(sympltname)))
827 			snprintf(sympltname, sizeof(sympltname),
828 				 "offset_%#" PRIx64 "@plt", plt_offset);
829 		free(demangled);
830 
831 		f = symbol__new(plt_offset, plt_entry_size, STB_GLOBAL, STT_FUNC, sympltname);
832 		if (!f)
833 			goto out_elf_end;
834 
835 		plt_offset += plt_entry_size;
836 		symbols__insert(dso__symbols(dso), f);
837 		++nr;
838 	}
839 
840 	err = 0;
841 out_elf_end:
842 	exit_rel(&ri);
843 	if (err == 0)
844 		return nr;
845 	pr_debug("%s: problems reading %s PLT info.\n",
846 		 __func__, dso__long_name(dso));
847 	return 0;
848 }
849 
dso__demangle_sym(struct dso * dso,int kmodule,const char * elf_name)850 char *dso__demangle_sym(struct dso *dso, int kmodule, const char *elf_name)
851 {
852 	return demangle_sym(dso, kmodule, elf_name);
853 }
854 
855 /*
856  * Align offset to 4 bytes as needed for note name and descriptor data.
857  */
858 #define NOTE_ALIGN(n) (((n) + 3) & -4U)
859 
elf_read_build_id(Elf * elf,void * bf,size_t size)860 static int elf_read_build_id(Elf *elf, void *bf, size_t size)
861 {
862 	int err = -1;
863 	GElf_Ehdr ehdr;
864 	GElf_Shdr shdr;
865 	Elf_Data *data;
866 	Elf_Scn *sec;
867 	Elf_Kind ek;
868 	void *ptr;
869 
870 	if (size < BUILD_ID_SIZE)
871 		goto out;
872 
873 	ek = elf_kind(elf);
874 	if (ek != ELF_K_ELF)
875 		goto out;
876 
877 	if (gelf_getehdr(elf, &ehdr) == NULL) {
878 		pr_err("%s: cannot get elf header.\n", __func__);
879 		goto out;
880 	}
881 
882 	/*
883 	 * Check following sections for notes:
884 	 *   '.note.gnu.build-id'
885 	 *   '.notes'
886 	 *   '.note' (VDSO specific)
887 	 */
888 	do {
889 		sec = elf_section_by_name(elf, &ehdr, &shdr,
890 					  ".note.gnu.build-id", NULL);
891 		if (sec)
892 			break;
893 
894 		sec = elf_section_by_name(elf, &ehdr, &shdr,
895 					  ".notes", NULL);
896 		if (sec)
897 			break;
898 
899 		sec = elf_section_by_name(elf, &ehdr, &shdr,
900 					  ".note", NULL);
901 		if (sec)
902 			break;
903 
904 		return err;
905 
906 	} while (0);
907 
908 	data = elf_getdata(sec, NULL);
909 	if (data == NULL)
910 		goto out;
911 
912 	ptr = data->d_buf;
913 	while (ptr < (data->d_buf + data->d_size)) {
914 		GElf_Nhdr *nhdr = ptr;
915 		size_t namesz = NOTE_ALIGN(nhdr->n_namesz),
916 		       descsz = NOTE_ALIGN(nhdr->n_descsz);
917 		const char *name;
918 
919 		ptr += sizeof(*nhdr);
920 		name = ptr;
921 		ptr += namesz;
922 		if (nhdr->n_type == NT_GNU_BUILD_ID &&
923 		    nhdr->n_namesz == sizeof("GNU")) {
924 			if (memcmp(name, "GNU", sizeof("GNU")) == 0) {
925 				size_t sz = min(size, descsz);
926 				memcpy(bf, ptr, sz);
927 				memset(bf + sz, 0, size - sz);
928 				err = sz;
929 				break;
930 			}
931 		}
932 		ptr += descsz;
933 	}
934 
935 out:
936 	return err;
937 }
938 
939 #ifdef HAVE_LIBBFD_BUILDID_SUPPORT
940 
read_build_id(const char * filename,struct build_id * bid)941 static int read_build_id(const char *filename, struct build_id *bid)
942 {
943 	size_t size = sizeof(bid->data);
944 	int err = -1;
945 	bfd *abfd;
946 
947 	abfd = bfd_openr(filename, NULL);
948 	if (!abfd)
949 		return -1;
950 
951 	if (!bfd_check_format(abfd, bfd_object)) {
952 		pr_debug2("%s: cannot read %s bfd file.\n", __func__, filename);
953 		goto out_close;
954 	}
955 
956 	if (!abfd->build_id || abfd->build_id->size > size)
957 		goto out_close;
958 
959 	memcpy(bid->data, abfd->build_id->data, abfd->build_id->size);
960 	memset(bid->data + abfd->build_id->size, 0, size - abfd->build_id->size);
961 	err = bid->size = abfd->build_id->size;
962 
963 out_close:
964 	bfd_close(abfd);
965 	return err;
966 }
967 
968 #else // HAVE_LIBBFD_BUILDID_SUPPORT
969 
read_build_id(const char * filename,struct build_id * bid)970 static int read_build_id(const char *filename, struct build_id *bid)
971 {
972 	size_t size = sizeof(bid->data);
973 	int fd, err = -1;
974 	Elf *elf;
975 
976 	if (size < BUILD_ID_SIZE)
977 		goto out;
978 
979 	fd = open(filename, O_RDONLY);
980 	if (fd < 0)
981 		goto out;
982 
983 	elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
984 	if (elf == NULL) {
985 		pr_debug2("%s: cannot read %s ELF file.\n", __func__, filename);
986 		goto out_close;
987 	}
988 
989 	err = elf_read_build_id(elf, bid->data, size);
990 	if (err > 0)
991 		bid->size = err;
992 
993 	elf_end(elf);
994 out_close:
995 	close(fd);
996 out:
997 	return err;
998 }
999 
1000 #endif // HAVE_LIBBFD_BUILDID_SUPPORT
1001 
filename__read_build_id(const char * filename,struct build_id * bid)1002 int filename__read_build_id(const char *filename, struct build_id *bid)
1003 {
1004 	struct kmod_path m = { .name = NULL, };
1005 	char path[PATH_MAX];
1006 	int err;
1007 
1008 	if (!filename)
1009 		return -EFAULT;
1010 
1011 	err = kmod_path__parse(&m, filename);
1012 	if (err)
1013 		return -1;
1014 
1015 	if (m.comp) {
1016 		int error = 0, fd;
1017 
1018 		fd = filename__decompress(filename, path, sizeof(path), m.comp, &error);
1019 		if (fd < 0) {
1020 			pr_debug("Failed to decompress (error %d) %s\n",
1021 				 error, filename);
1022 			return -1;
1023 		}
1024 		close(fd);
1025 		filename = path;
1026 	}
1027 
1028 	err = read_build_id(filename, bid);
1029 
1030 	if (m.comp)
1031 		unlink(filename);
1032 	return err;
1033 }
1034 
sysfs__read_build_id(const char * filename,struct build_id * bid)1035 int sysfs__read_build_id(const char *filename, struct build_id *bid)
1036 {
1037 	size_t size = sizeof(bid->data);
1038 	int fd, err = -1;
1039 
1040 	fd = open(filename, O_RDONLY);
1041 	if (fd < 0)
1042 		goto out;
1043 
1044 	while (1) {
1045 		char bf[BUFSIZ];
1046 		GElf_Nhdr nhdr;
1047 		size_t namesz, descsz;
1048 
1049 		if (read(fd, &nhdr, sizeof(nhdr)) != sizeof(nhdr))
1050 			break;
1051 
1052 		namesz = NOTE_ALIGN(nhdr.n_namesz);
1053 		descsz = NOTE_ALIGN(nhdr.n_descsz);
1054 		if (nhdr.n_type == NT_GNU_BUILD_ID &&
1055 		    nhdr.n_namesz == sizeof("GNU")) {
1056 			if (read(fd, bf, namesz) != (ssize_t)namesz)
1057 				break;
1058 			if (memcmp(bf, "GNU", sizeof("GNU")) == 0) {
1059 				size_t sz = min(descsz, size);
1060 				if (read(fd, bid->data, sz) == (ssize_t)sz) {
1061 					memset(bid->data + sz, 0, size - sz);
1062 					bid->size = sz;
1063 					err = 0;
1064 					break;
1065 				}
1066 			} else if (read(fd, bf, descsz) != (ssize_t)descsz)
1067 				break;
1068 		} else {
1069 			int n = namesz + descsz;
1070 
1071 			if (n > (int)sizeof(bf)) {
1072 				n = sizeof(bf);
1073 				pr_debug("%s: truncating reading of build id in sysfs file %s: n_namesz=%u, n_descsz=%u.\n",
1074 					 __func__, filename, nhdr.n_namesz, nhdr.n_descsz);
1075 			}
1076 			if (read(fd, bf, n) != n)
1077 				break;
1078 		}
1079 	}
1080 	close(fd);
1081 out:
1082 	return err;
1083 }
1084 
1085 #ifdef HAVE_LIBBFD_SUPPORT
1086 
filename__read_debuglink(const char * filename,char * debuglink,size_t size)1087 int filename__read_debuglink(const char *filename, char *debuglink,
1088 			     size_t size)
1089 {
1090 	int err = -1;
1091 	asection *section;
1092 	bfd *abfd;
1093 
1094 	abfd = bfd_openr(filename, NULL);
1095 	if (!abfd)
1096 		return -1;
1097 
1098 	if (!bfd_check_format(abfd, bfd_object)) {
1099 		pr_debug2("%s: cannot read %s bfd file.\n", __func__, filename);
1100 		goto out_close;
1101 	}
1102 
1103 	section = bfd_get_section_by_name(abfd, ".gnu_debuglink");
1104 	if (!section)
1105 		goto out_close;
1106 
1107 	if (section->size > size)
1108 		goto out_close;
1109 
1110 	if (!bfd_get_section_contents(abfd, section, debuglink, 0,
1111 				      section->size))
1112 		goto out_close;
1113 
1114 	err = 0;
1115 
1116 out_close:
1117 	bfd_close(abfd);
1118 	return err;
1119 }
1120 
1121 #else
1122 
filename__read_debuglink(const char * filename,char * debuglink,size_t size)1123 int filename__read_debuglink(const char *filename, char *debuglink,
1124 			     size_t size)
1125 {
1126 	int fd, err = -1;
1127 	Elf *elf;
1128 	GElf_Ehdr ehdr;
1129 	GElf_Shdr shdr;
1130 	Elf_Data *data;
1131 	Elf_Scn *sec;
1132 	Elf_Kind ek;
1133 
1134 	fd = open(filename, O_RDONLY);
1135 	if (fd < 0)
1136 		goto out;
1137 
1138 	elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
1139 	if (elf == NULL) {
1140 		pr_debug2("%s: cannot read %s ELF file.\n", __func__, filename);
1141 		goto out_close;
1142 	}
1143 
1144 	ek = elf_kind(elf);
1145 	if (ek != ELF_K_ELF)
1146 		goto out_elf_end;
1147 
1148 	if (gelf_getehdr(elf, &ehdr) == NULL) {
1149 		pr_err("%s: cannot get elf header.\n", __func__);
1150 		goto out_elf_end;
1151 	}
1152 
1153 	sec = elf_section_by_name(elf, &ehdr, &shdr,
1154 				  ".gnu_debuglink", NULL);
1155 	if (sec == NULL)
1156 		goto out_elf_end;
1157 
1158 	data = elf_getdata(sec, NULL);
1159 	if (data == NULL)
1160 		goto out_elf_end;
1161 
1162 	/* the start of this section is a zero-terminated string */
1163 	strncpy(debuglink, data->d_buf, size);
1164 
1165 	err = 0;
1166 
1167 out_elf_end:
1168 	elf_end(elf);
1169 out_close:
1170 	close(fd);
1171 out:
1172 	return err;
1173 }
1174 
1175 #endif
1176 
symsrc__possibly_runtime(struct symsrc * ss)1177 bool symsrc__possibly_runtime(struct symsrc *ss)
1178 {
1179 	return ss->dynsym || ss->opdsec;
1180 }
1181 
symsrc__has_symtab(struct symsrc * ss)1182 bool symsrc__has_symtab(struct symsrc *ss)
1183 {
1184 	return ss->symtab != NULL;
1185 }
1186 
symsrc__destroy(struct symsrc * ss)1187 void symsrc__destroy(struct symsrc *ss)
1188 {
1189 	zfree(&ss->name);
1190 	elf_end(ss->elf);
1191 	close(ss->fd);
1192 }
1193 
elf__needs_adjust_symbols(GElf_Ehdr ehdr)1194 bool elf__needs_adjust_symbols(GElf_Ehdr ehdr)
1195 {
1196 	/*
1197 	 * Usually vmlinux is an ELF file with type ET_EXEC for most
1198 	 * architectures; except Arm64 kernel is linked with option
1199 	 * '-share', so need to check type ET_DYN.
1200 	 */
1201 	return ehdr.e_type == ET_EXEC || ehdr.e_type == ET_REL ||
1202 	       ehdr.e_type == ET_DYN;
1203 }
1204 
read_gnu_debugdata(struct dso * dso,Elf * elf,const char * name,int * fd_ret)1205 static Elf *read_gnu_debugdata(struct dso *dso, Elf *elf, const char *name, int *fd_ret)
1206 {
1207 	Elf *elf_embedded;
1208 	GElf_Ehdr ehdr;
1209 	GElf_Shdr shdr;
1210 	Elf_Scn *scn;
1211 	Elf_Data *scn_data;
1212 	FILE *wrapped;
1213 	size_t shndx;
1214 	char temp_filename[] = "/tmp/perf.gnu_debugdata.elf.XXXXXX";
1215 	int ret, temp_fd;
1216 
1217 	if (gelf_getehdr(elf, &ehdr) == NULL) {
1218 		pr_debug("%s: cannot read %s ELF file.\n", __func__, name);
1219 		*dso__load_errno(dso) = DSO_LOAD_ERRNO__INVALID_ELF;
1220 		return NULL;
1221 	}
1222 
1223 	scn = elf_section_by_name(elf, &ehdr, &shdr, ".gnu_debugdata", &shndx);
1224 	if (!scn) {
1225 		*dso__load_errno(dso) = -ENOENT;
1226 		return NULL;
1227 	}
1228 
1229 	if (shdr.sh_type == SHT_NOBITS) {
1230 		pr_debug("%s: .gnu_debugdata of ELF file %s has no data.\n", __func__, name);
1231 		*dso__load_errno(dso) = DSO_LOAD_ERRNO__INVALID_ELF;
1232 		return NULL;
1233 	}
1234 
1235 	scn_data = elf_rawdata(scn, NULL);
1236 	if (!scn_data) {
1237 		pr_debug("%s: error reading .gnu_debugdata of %s: %s\n", __func__,
1238 			 name, elf_errmsg(-1));
1239 		*dso__load_errno(dso) = DSO_LOAD_ERRNO__INVALID_ELF;
1240 		return NULL;
1241 	}
1242 
1243 	wrapped = fmemopen(scn_data->d_buf, scn_data->d_size, "r");
1244 	if (!wrapped) {
1245 		pr_debug("%s: fmemopen: %s\n", __func__, strerror(errno));
1246 		*dso__load_errno(dso) = -errno;
1247 		return NULL;
1248 	}
1249 
1250 	temp_fd = mkstemp(temp_filename);
1251 	if (temp_fd < 0) {
1252 		pr_debug("%s: mkstemp: %s\n", __func__, strerror(errno));
1253 		*dso__load_errno(dso) = -errno;
1254 		fclose(wrapped);
1255 		return NULL;
1256 	}
1257 	unlink(temp_filename);
1258 
1259 	ret = lzma_decompress_stream_to_file(wrapped, temp_fd);
1260 	fclose(wrapped);
1261 	if (ret < 0) {
1262 		*dso__load_errno(dso) = -errno;
1263 		close(temp_fd);
1264 		return NULL;
1265 	}
1266 
1267 	elf_embedded = elf_begin(temp_fd, PERF_ELF_C_READ_MMAP, NULL);
1268 	if (!elf_embedded) {
1269 		pr_debug("%s: error reading .gnu_debugdata of %s: %s\n", __func__,
1270 			 name, elf_errmsg(-1));
1271 		*dso__load_errno(dso) = DSO_LOAD_ERRNO__INVALID_ELF;
1272 		close(temp_fd);
1273 		return NULL;
1274 	}
1275 	pr_debug("%s: using .gnu_debugdata of %s\n", __func__, name);
1276 	*fd_ret = temp_fd;
1277 	return elf_embedded;
1278 }
1279 
symsrc__init(struct symsrc * ss,struct dso * dso,const char * name,enum dso_binary_type type)1280 int symsrc__init(struct symsrc *ss, struct dso *dso, const char *name,
1281 		 enum dso_binary_type type)
1282 {
1283 	GElf_Ehdr ehdr;
1284 	Elf *elf;
1285 	int fd;
1286 
1287 	if (dso__needs_decompress(dso)) {
1288 		fd = dso__decompress_kmodule_fd(dso, name);
1289 		if (fd < 0)
1290 			return -1;
1291 
1292 		type = dso__symtab_type(dso);
1293 	} else {
1294 		fd = open(name, O_RDONLY);
1295 		if (fd < 0) {
1296 			*dso__load_errno(dso) = errno;
1297 			return -1;
1298 		}
1299 	}
1300 
1301 	elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
1302 	if (elf == NULL) {
1303 		pr_debug("%s: cannot read %s ELF file.\n", __func__, name);
1304 		*dso__load_errno(dso) = DSO_LOAD_ERRNO__INVALID_ELF;
1305 		goto out_close;
1306 	}
1307 
1308 	if (type == DSO_BINARY_TYPE__GNU_DEBUGDATA) {
1309 		int new_fd;
1310 		Elf *embedded = read_gnu_debugdata(dso, elf, name, &new_fd);
1311 
1312 		if (!embedded)
1313 			goto out_close;
1314 
1315 		elf_end(elf);
1316 		close(fd);
1317 		fd = new_fd;
1318 		elf = embedded;
1319 	}
1320 
1321 	if (gelf_getehdr(elf, &ehdr) == NULL) {
1322 		*dso__load_errno(dso) = DSO_LOAD_ERRNO__INVALID_ELF;
1323 		pr_debug("%s: cannot get elf header.\n", __func__);
1324 		goto out_elf_end;
1325 	}
1326 
1327 	if (dso__swap_init(dso, ehdr.e_ident[EI_DATA])) {
1328 		*dso__load_errno(dso) = DSO_LOAD_ERRNO__INTERNAL_ERROR;
1329 		goto out_elf_end;
1330 	}
1331 
1332 	/* Always reject images with a mismatched build-id: */
1333 	if (dso__has_build_id(dso) && !symbol_conf.ignore_vmlinux_buildid) {
1334 		u8 build_id[BUILD_ID_SIZE];
1335 		struct build_id bid;
1336 		int size;
1337 
1338 		size = elf_read_build_id(elf, build_id, BUILD_ID_SIZE);
1339 		if (size <= 0) {
1340 			*dso__load_errno(dso) = DSO_LOAD_ERRNO__CANNOT_READ_BUILDID;
1341 			goto out_elf_end;
1342 		}
1343 
1344 		build_id__init(&bid, build_id, size);
1345 		if (!dso__build_id_equal(dso, &bid)) {
1346 			pr_debug("%s: build id mismatch for %s.\n", __func__, name);
1347 			*dso__load_errno(dso) = DSO_LOAD_ERRNO__MISMATCHING_BUILDID;
1348 			goto out_elf_end;
1349 		}
1350 	}
1351 
1352 	ss->is_64_bit = (gelf_getclass(elf) == ELFCLASS64);
1353 
1354 	ss->symtab_idx = 0;
1355 	ss->symtab = elf_section_by_name(elf, &ehdr, &ss->symshdr, ".symtab",
1356 			&ss->symtab_idx);
1357 	if (ss->symshdr.sh_type != SHT_SYMTAB)
1358 		ss->symtab = NULL;
1359 
1360 	ss->dynsym_idx = 0;
1361 	ss->dynsym = elf_section_by_name(elf, &ehdr, &ss->dynshdr, ".dynsym",
1362 			&ss->dynsym_idx);
1363 	if (ss->dynshdr.sh_type != SHT_DYNSYM)
1364 		ss->dynsym = NULL;
1365 
1366 	ss->opdidx = 0;
1367 	ss->opdsec = elf_section_by_name(elf, &ehdr, &ss->opdshdr, ".opd",
1368 			&ss->opdidx);
1369 	if (ss->opdshdr.sh_type != SHT_PROGBITS)
1370 		ss->opdsec = NULL;
1371 
1372 	if (dso__kernel(dso) == DSO_SPACE__USER)
1373 		ss->adjust_symbols = true;
1374 	else
1375 		ss->adjust_symbols = elf__needs_adjust_symbols(ehdr);
1376 
1377 	ss->name   = strdup(name);
1378 	if (!ss->name) {
1379 		*dso__load_errno(dso) = errno;
1380 		goto out_elf_end;
1381 	}
1382 
1383 	ss->elf    = elf;
1384 	ss->fd     = fd;
1385 	ss->ehdr   = ehdr;
1386 	ss->type   = type;
1387 
1388 	return 0;
1389 
1390 out_elf_end:
1391 	elf_end(elf);
1392 out_close:
1393 	close(fd);
1394 	return -1;
1395 }
1396 
is_exe_text(int flags)1397 static bool is_exe_text(int flags)
1398 {
1399 	return (flags & (SHF_ALLOC | SHF_EXECINSTR)) == (SHF_ALLOC | SHF_EXECINSTR);
1400 }
1401 
1402 /*
1403  * Some executable module sections like .noinstr.text might be laid out with
1404  * .text so they can use the same mapping (memory address to file offset).
1405  * Check if that is the case. Refer to kernel layout_sections(). Return the
1406  * maximum offset.
1407  */
max_text_section(Elf * elf,GElf_Ehdr * ehdr)1408 static u64 max_text_section(Elf *elf, GElf_Ehdr *ehdr)
1409 {
1410 	Elf_Scn *sec = NULL;
1411 	GElf_Shdr shdr;
1412 	u64 offs = 0;
1413 
1414 	/* Doesn't work for some arch */
1415 	if (ehdr->e_machine == EM_PARISC ||
1416 	    ehdr->e_machine == EM_ALPHA)
1417 		return 0;
1418 
1419 	/* ELF is corrupted/truncated, avoid calling elf_strptr. */
1420 	if (!elf_rawdata(elf_getscn(elf, ehdr->e_shstrndx), NULL))
1421 		return 0;
1422 
1423 	while ((sec = elf_nextscn(elf, sec)) != NULL) {
1424 		char *sec_name;
1425 
1426 		if (!gelf_getshdr(sec, &shdr))
1427 			break;
1428 
1429 		if (!is_exe_text(shdr.sh_flags))
1430 			continue;
1431 
1432 		/* .init and .exit sections are not placed with .text */
1433 		sec_name = elf_strptr(elf, ehdr->e_shstrndx, shdr.sh_name);
1434 		if (!sec_name ||
1435 		    strstarts(sec_name, ".init") ||
1436 		    strstarts(sec_name, ".exit"))
1437 			break;
1438 
1439 		/* Must be next to previous, assumes .text is first */
1440 		if (offs && PERF_ALIGN(offs, shdr.sh_addralign ?: 1) != shdr.sh_offset)
1441 			break;
1442 
1443 		offs = shdr.sh_offset + shdr.sh_size;
1444 	}
1445 
1446 	return offs;
1447 }
1448 
1449 /**
1450  * ref_reloc_sym_not_found - has kernel relocation symbol been found.
1451  * @kmap: kernel maps and relocation reference symbol
1452  *
1453  * This function returns %true if we are dealing with the kernel maps and the
1454  * relocation reference symbol has not yet been found.  Otherwise %false is
1455  * returned.
1456  */
ref_reloc_sym_not_found(struct kmap * kmap)1457 static bool ref_reloc_sym_not_found(struct kmap *kmap)
1458 {
1459 	return kmap && kmap->ref_reloc_sym && kmap->ref_reloc_sym->name &&
1460 	       !kmap->ref_reloc_sym->unrelocated_addr;
1461 }
1462 
1463 /**
1464  * ref_reloc - kernel relocation offset.
1465  * @kmap: kernel maps and relocation reference symbol
1466  *
1467  * This function returns the offset of kernel addresses as determined by using
1468  * the relocation reference symbol i.e. if the kernel has not been relocated
1469  * then the return value is zero.
1470  */
ref_reloc(struct kmap * kmap)1471 static u64 ref_reloc(struct kmap *kmap)
1472 {
1473 	if (kmap && kmap->ref_reloc_sym &&
1474 	    kmap->ref_reloc_sym->unrelocated_addr)
1475 		return kmap->ref_reloc_sym->addr -
1476 		       kmap->ref_reloc_sym->unrelocated_addr;
1477 	return 0;
1478 }
1479 
arch__sym_update(struct symbol * s __maybe_unused,GElf_Sym * sym __maybe_unused)1480 void __weak arch__sym_update(struct symbol *s __maybe_unused,
1481 		GElf_Sym *sym __maybe_unused) { }
1482 
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,const char * section_name,bool adjust_kernel_syms,bool kmodule,bool * remap_kernel,u64 max_text_sh_offset)1483 static int dso__process_kernel_symbol(struct dso *dso, struct map *map,
1484 				      GElf_Sym *sym, GElf_Shdr *shdr,
1485 				      struct maps *kmaps, struct kmap *kmap,
1486 				      struct dso **curr_dsop,
1487 				      const char *section_name,
1488 				      bool adjust_kernel_syms, bool kmodule, bool *remap_kernel,
1489 				      u64 max_text_sh_offset)
1490 {
1491 	struct dso *curr_dso = *curr_dsop;
1492 	struct map *curr_map;
1493 	char dso_name[PATH_MAX];
1494 
1495 	/* Adjust symbol to map to file offset */
1496 	if (adjust_kernel_syms)
1497 		sym->st_value -= shdr->sh_addr - shdr->sh_offset;
1498 
1499 	if (strcmp(section_name, (dso__short_name(curr_dso) + dso__short_name_len(dso))) == 0)
1500 		return 0;
1501 
1502 	if (strcmp(section_name, ".text") == 0) {
1503 		/*
1504 		 * The initial kernel mapping is based on
1505 		 * kallsyms and identity maps.  Overwrite it to
1506 		 * map to the kernel dso.
1507 		 */
1508 		if (*remap_kernel && dso__kernel(dso) && !kmodule) {
1509 			*remap_kernel = false;
1510 			map__set_start(map, shdr->sh_addr + ref_reloc(kmap));
1511 			map__set_end(map, map__start(map) + shdr->sh_size);
1512 			map__set_pgoff(map, shdr->sh_offset);
1513 			map__set_mapping_type(map, MAPPING_TYPE__DSO);
1514 			/* Ensure maps are correctly ordered */
1515 			if (kmaps) {
1516 				int err;
1517 				struct map *tmp = map__get(map);
1518 
1519 				maps__remove(kmaps, map);
1520 				err = maps__insert(kmaps, map);
1521 				map__put(tmp);
1522 				if (err)
1523 					return err;
1524 			}
1525 		}
1526 
1527 		/*
1528 		 * The initial module mapping is based on
1529 		 * /proc/modules mapped to offset zero.
1530 		 * Overwrite it to map to the module dso.
1531 		 */
1532 		if (*remap_kernel && kmodule) {
1533 			*remap_kernel = false;
1534 			map__set_pgoff(map, shdr->sh_offset);
1535 		}
1536 
1537 		dso__put(*curr_dsop);
1538 		*curr_dsop = dso__get(dso);
1539 		return 0;
1540 	}
1541 
1542 	if (!kmap)
1543 		return 0;
1544 
1545 	/*
1546 	 * perf does not record module section addresses except for .text, but
1547 	 * some sections can use the same mapping as .text.
1548 	 */
1549 	if (kmodule && adjust_kernel_syms && is_exe_text(shdr->sh_flags) &&
1550 	    shdr->sh_offset <= max_text_sh_offset) {
1551 		dso__put(*curr_dsop);
1552 		*curr_dsop = dso__get(dso);
1553 		return 0;
1554 	}
1555 
1556 	snprintf(dso_name, sizeof(dso_name), "%s%s", dso__short_name(dso), section_name);
1557 
1558 	curr_map = maps__find_by_name(kmaps, dso_name);
1559 	if (curr_map == NULL) {
1560 		u64 start = sym->st_value;
1561 
1562 		if (kmodule)
1563 			start += map__start(map) + shdr->sh_offset;
1564 
1565 		curr_dso = dso__new(dso_name);
1566 		if (curr_dso == NULL)
1567 			return -1;
1568 		dso__set_kernel(curr_dso, dso__kernel(dso));
1569 		RC_CHK_ACCESS(curr_dso)->long_name = dso__long_name(dso);
1570 		RC_CHK_ACCESS(curr_dso)->long_name_len = dso__long_name_len(dso);
1571 		dso__set_binary_type(curr_dso, dso__binary_type(dso));
1572 		dso__set_adjust_symbols(curr_dso, dso__adjust_symbols(dso));
1573 		curr_map = map__new2(start, curr_dso);
1574 		if (curr_map == NULL) {
1575 			dso__put(curr_dso);
1576 			return -1;
1577 		}
1578 		if (dso__kernel(curr_dso))
1579 			map__kmap(curr_map)->kmaps = kmaps;
1580 
1581 		if (adjust_kernel_syms) {
1582 			map__set_start(curr_map, shdr->sh_addr + ref_reloc(kmap));
1583 			map__set_end(curr_map, map__start(curr_map) + shdr->sh_size);
1584 			map__set_pgoff(curr_map, shdr->sh_offset);
1585 		} else {
1586 			map__set_mapping_type(curr_map, MAPPING_TYPE__IDENTITY);
1587 		}
1588 		dso__set_symtab_type(curr_dso, dso__symtab_type(dso));
1589 		if (maps__insert(kmaps, curr_map))
1590 			return -1;
1591 		dsos__add(&maps__machine(kmaps)->dsos, curr_dso);
1592 		dso__set_loaded(curr_dso);
1593 		dso__put(*curr_dsop);
1594 		*curr_dsop = curr_dso;
1595 	} else {
1596 		dso__put(*curr_dsop);
1597 		*curr_dsop = dso__get(map__dso(curr_map));
1598 	}
1599 	map__put(curr_map);
1600 
1601 	return 0;
1602 }
1603 
1604 static int
dso__load_sym_internal(struct dso * dso,struct map * map,struct symsrc * syms_ss,struct symsrc * runtime_ss,int kmodule,int dynsym)1605 dso__load_sym_internal(struct dso *dso, struct map *map, struct symsrc *syms_ss,
1606 		       struct symsrc *runtime_ss, int kmodule, int dynsym)
1607 {
1608 	struct kmap *kmap = dso__kernel(dso) ? map__kmap(map) : NULL;
1609 	struct maps *kmaps = kmap ? map__kmaps(map) : NULL;
1610 	struct dso *curr_dso = NULL;
1611 	Elf_Data *symstrs, *secstrs, *secstrs_run, *secstrs_sym;
1612 	uint32_t nr_syms;
1613 	uint32_t idx;
1614 	GElf_Ehdr ehdr;
1615 	GElf_Shdr shdr;
1616 	GElf_Shdr tshdr;
1617 	Elf_Data *syms, *opddata = NULL;
1618 	GElf_Sym sym;
1619 	Elf_Scn *sec, *sec_strndx;
1620 	Elf *elf;
1621 	int nr = 0;
1622 	bool remap_kernel = false, adjust_kernel_syms = false;
1623 	u64 max_text_sh_offset = 0;
1624 
1625 	if (kmap && !kmaps)
1626 		return -1;
1627 
1628 	elf = syms_ss->elf;
1629 	ehdr = syms_ss->ehdr;
1630 	if (dynsym) {
1631 		sec  = syms_ss->dynsym;
1632 		shdr = syms_ss->dynshdr;
1633 	} else {
1634 		sec =  syms_ss->symtab;
1635 		shdr = syms_ss->symshdr;
1636 	}
1637 
1638 	if (elf_section_by_name(runtime_ss->elf, &runtime_ss->ehdr, &tshdr,
1639 				".text", NULL)) {
1640 		dso__set_text_offset(dso, tshdr.sh_addr - tshdr.sh_offset);
1641 		dso__set_text_end(dso, tshdr.sh_offset + tshdr.sh_size);
1642 	}
1643 
1644 	if (runtime_ss->opdsec)
1645 		opddata = elf_rawdata(runtime_ss->opdsec, NULL);
1646 
1647 	syms = elf_getdata(sec, NULL);
1648 	if (syms == NULL)
1649 		goto out_elf_end;
1650 
1651 	sec = elf_getscn(elf, shdr.sh_link);
1652 	if (sec == NULL)
1653 		goto out_elf_end;
1654 
1655 	symstrs = elf_getdata(sec, NULL);
1656 	if (symstrs == NULL)
1657 		goto out_elf_end;
1658 
1659 	sec_strndx = elf_getscn(runtime_ss->elf, runtime_ss->ehdr.e_shstrndx);
1660 	if (sec_strndx == NULL)
1661 		goto out_elf_end;
1662 
1663 	secstrs_run = elf_getdata(sec_strndx, NULL);
1664 	if (secstrs_run == NULL)
1665 		goto out_elf_end;
1666 
1667 	sec_strndx = elf_getscn(elf, ehdr.e_shstrndx);
1668 	if (sec_strndx == NULL)
1669 		goto out_elf_end;
1670 
1671 	secstrs_sym = elf_getdata(sec_strndx, NULL);
1672 	if (secstrs_sym == NULL)
1673 		goto out_elf_end;
1674 
1675 	nr_syms = shdr.sh_size / shdr.sh_entsize;
1676 
1677 	memset(&sym, 0, sizeof(sym));
1678 
1679 	/*
1680 	 * The kernel relocation symbol is needed in advance in order to adjust
1681 	 * kernel maps correctly.
1682 	 */
1683 	if (ref_reloc_sym_not_found(kmap)) {
1684 		elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) {
1685 			const char *elf_name = elf_sym__name(&sym, symstrs);
1686 
1687 			if (strcmp(elf_name, kmap->ref_reloc_sym->name))
1688 				continue;
1689 			kmap->ref_reloc_sym->unrelocated_addr = sym.st_value;
1690 			map__set_reloc(map, kmap->ref_reloc_sym->addr - kmap->ref_reloc_sym->unrelocated_addr);
1691 			break;
1692 		}
1693 	}
1694 
1695 	/*
1696 	 * Handle any relocation of vdso necessary because older kernels
1697 	 * attempted to prelink vdso to its virtual address.
1698 	 */
1699 	if (dso__is_vdso(dso))
1700 		map__set_reloc(map, map__start(map) - dso__text_offset(dso));
1701 
1702 	dso__set_adjust_symbols(dso, runtime_ss->adjust_symbols || ref_reloc(kmap));
1703 	/*
1704 	 * Initial kernel and module mappings do not map to the dso.
1705 	 * Flag the fixups.
1706 	 */
1707 	if (dso__kernel(dso)) {
1708 		remap_kernel = true;
1709 		adjust_kernel_syms = dso__adjust_symbols(dso);
1710 	}
1711 
1712 	if (kmodule && adjust_kernel_syms)
1713 		max_text_sh_offset = max_text_section(runtime_ss->elf, &runtime_ss->ehdr);
1714 
1715 	curr_dso = dso__get(dso);
1716 	elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) {
1717 		struct symbol *f;
1718 		const char *elf_name = elf_sym__name(&sym, symstrs);
1719 		char *demangled = NULL;
1720 		int is_label = elf_sym__is_label(&sym);
1721 		const char *section_name;
1722 		bool used_opd = false;
1723 
1724 		if (!is_label && !elf_sym__filter(&sym))
1725 			continue;
1726 
1727 		/* Reject ARM ELF "mapping symbols": these aren't unique and
1728 		 * don't identify functions, so will confuse the profile
1729 		 * output: */
1730 		if (ehdr.e_machine == EM_ARM || ehdr.e_machine == EM_AARCH64) {
1731 			if (elf_name[0] == '$' && strchr("adtx", elf_name[1])
1732 			    && (elf_name[2] == '\0' || elf_name[2] == '.'))
1733 				continue;
1734 		}
1735 
1736 		if (runtime_ss->opdsec && sym.st_shndx == runtime_ss->opdidx) {
1737 			u32 offset = sym.st_value - syms_ss->opdshdr.sh_addr;
1738 			u64 *opd = opddata->d_buf + offset;
1739 			sym.st_value = DSO__SWAP(dso, u64, *opd);
1740 			sym.st_shndx = elf_addr_to_index(runtime_ss->elf,
1741 					sym.st_value);
1742 			used_opd = true;
1743 		}
1744 
1745 		/*
1746 		 * When loading symbols in a data mapping, ABS symbols (which
1747 		 * has a value of SHN_ABS in its st_shndx) failed at
1748 		 * elf_getscn().  And it marks the loading as a failure so
1749 		 * already loaded symbols cannot be fixed up.
1750 		 *
1751 		 * I'm not sure what should be done. Just ignore them for now.
1752 		 * - Namhyung Kim
1753 		 */
1754 		if (sym.st_shndx == SHN_ABS)
1755 			continue;
1756 
1757 		sec = elf_getscn(syms_ss->elf, sym.st_shndx);
1758 		if (!sec)
1759 			goto out_elf_end;
1760 
1761 		gelf_getshdr(sec, &shdr);
1762 
1763 		/*
1764 		 * If the attribute bit SHF_ALLOC is not set, the section
1765 		 * doesn't occupy memory during process execution.
1766 		 * E.g. ".gnu.warning.*" section is used by linker to generate
1767 		 * warnings when calling deprecated functions, the symbols in
1768 		 * the section aren't loaded to memory during process execution,
1769 		 * so skip them.
1770 		 */
1771 		if (!(shdr.sh_flags & SHF_ALLOC))
1772 			continue;
1773 
1774 		secstrs = secstrs_sym;
1775 
1776 		/*
1777 		 * We have to fallback to runtime when syms' section header has
1778 		 * NOBITS set. NOBITS results in file offset (sh_offset) not
1779 		 * being incremented. So sh_offset used below has different
1780 		 * values for syms (invalid) and runtime (valid).
1781 		 */
1782 		if (shdr.sh_type == SHT_NOBITS) {
1783 			sec = elf_getscn(runtime_ss->elf, sym.st_shndx);
1784 			if (!sec)
1785 				goto out_elf_end;
1786 
1787 			gelf_getshdr(sec, &shdr);
1788 			secstrs = secstrs_run;
1789 		}
1790 
1791 		if (is_label && !elf_sec__filter(&shdr, secstrs))
1792 			continue;
1793 
1794 		section_name = elf_sec__name(&shdr, secstrs);
1795 
1796 		/* On ARM, symbols for thumb functions have 1 added to
1797 		 * the symbol address as a flag - remove it */
1798 		if ((ehdr.e_machine == EM_ARM) &&
1799 		    (GELF_ST_TYPE(sym.st_info) == STT_FUNC) &&
1800 		    (sym.st_value & 1))
1801 			--sym.st_value;
1802 
1803 		if (dso__kernel(dso)) {
1804 			if (dso__process_kernel_symbol(dso, map, &sym, &shdr,
1805 						       kmaps, kmap, &curr_dso,
1806 						       section_name,
1807 						       adjust_kernel_syms,
1808 						       kmodule,
1809 						       &remap_kernel,
1810 						       max_text_sh_offset))
1811 				goto out_elf_end;
1812 		} else if ((used_opd && runtime_ss->adjust_symbols) ||
1813 			   (!used_opd && syms_ss->adjust_symbols)) {
1814 			GElf_Phdr phdr;
1815 
1816 			if (elf_read_program_header(runtime_ss->elf,
1817 						    (u64)sym.st_value, &phdr)) {
1818 				pr_debug4("%s: failed to find program header for "
1819 					   "symbol: %s st_value: %#" PRIx64 "\n",
1820 					   __func__, elf_name, (u64)sym.st_value);
1821 				pr_debug4("%s: adjusting symbol: st_value: %#" PRIx64 " "
1822 					"sh_addr: %#" PRIx64 " sh_offset: %#" PRIx64 "\n",
1823 					__func__, (u64)sym.st_value, (u64)shdr.sh_addr,
1824 					(u64)shdr.sh_offset);
1825 				/*
1826 				 * Fail to find program header, let's rollback
1827 				 * to use shdr.sh_addr and shdr.sh_offset to
1828 				 * calibrate symbol's file address, though this
1829 				 * is not necessary for normal C ELF file, we
1830 				 * still need to handle java JIT symbols in this
1831 				 * case.
1832 				 */
1833 				sym.st_value -= shdr.sh_addr - shdr.sh_offset;
1834 			} else {
1835 				pr_debug4("%s: adjusting symbol: st_value: %#" PRIx64 " "
1836 					"p_vaddr: %#" PRIx64 " p_offset: %#" PRIx64 "\n",
1837 					__func__, (u64)sym.st_value, (u64)phdr.p_vaddr,
1838 					(u64)phdr.p_offset);
1839 				sym.st_value -= phdr.p_vaddr - phdr.p_offset;
1840 			}
1841 		}
1842 
1843 		demangled = demangle_sym(dso, kmodule, elf_name);
1844 		if (demangled != NULL)
1845 			elf_name = demangled;
1846 
1847 		f = symbol__new(sym.st_value, sym.st_size,
1848 				GELF_ST_BIND(sym.st_info),
1849 				GELF_ST_TYPE(sym.st_info), elf_name);
1850 		free(demangled);
1851 		if (!f)
1852 			goto out_elf_end;
1853 
1854 		arch__sym_update(f, &sym);
1855 
1856 		__symbols__insert(dso__symbols(curr_dso), f, dso__kernel(dso));
1857 		nr++;
1858 	}
1859 	dso__put(curr_dso);
1860 
1861 	/*
1862 	 * For misannotated, zeroed, ASM function sizes.
1863 	 */
1864 	if (nr > 0) {
1865 		symbols__fixup_end(dso__symbols(dso), false);
1866 		symbols__fixup_duplicate(dso__symbols(dso));
1867 		if (kmap) {
1868 			/*
1869 			 * We need to fixup this here too because we create new
1870 			 * maps here, for things like vsyscall sections.
1871 			 */
1872 			maps__fixup_end(kmaps);
1873 		}
1874 	}
1875 	return nr;
1876 out_elf_end:
1877 	dso__put(curr_dso);
1878 	return -1;
1879 }
1880 
dso__load_sym(struct dso * dso,struct map * map,struct symsrc * syms_ss,struct symsrc * runtime_ss,int kmodule)1881 int dso__load_sym(struct dso *dso, struct map *map, struct symsrc *syms_ss,
1882 		  struct symsrc *runtime_ss, int kmodule)
1883 {
1884 	int nr = 0;
1885 	int err = -1;
1886 
1887 	dso__set_symtab_type(dso, syms_ss->type);
1888 	dso__set_is_64_bit(dso, syms_ss->is_64_bit);
1889 	dso__set_rel(dso, syms_ss->ehdr.e_type == ET_REL);
1890 
1891 	/*
1892 	 * Modules may already have symbols from kallsyms, but those symbols
1893 	 * have the wrong values for the dso maps, so remove them.
1894 	 */
1895 	if (kmodule && syms_ss->symtab)
1896 		symbols__delete(dso__symbols(dso));
1897 
1898 	if (!syms_ss->symtab) {
1899 		/*
1900 		 * If the vmlinux is stripped, fail so we will fall back
1901 		 * to using kallsyms. The vmlinux runtime symbols aren't
1902 		 * of much use.
1903 		 */
1904 		if (dso__kernel(dso))
1905 			return err;
1906 	} else  {
1907 		err = dso__load_sym_internal(dso, map, syms_ss, runtime_ss,
1908 					     kmodule, 0);
1909 		if (err < 0)
1910 			return err;
1911 		nr = err;
1912 	}
1913 
1914 	if (syms_ss->dynsym) {
1915 		err = dso__load_sym_internal(dso, map, syms_ss, runtime_ss,
1916 					     kmodule, 1);
1917 		if (err < 0)
1918 			return err;
1919 		nr += err;
1920 	}
1921 
1922 	/*
1923 	 * The .gnu_debugdata is a special situation: it contains a symbol
1924 	 * table, but the runtime file may also contain dynsym entries which are
1925 	 * not present there. We need to load both.
1926 	 */
1927 	if (syms_ss->type == DSO_BINARY_TYPE__GNU_DEBUGDATA && runtime_ss->dynsym) {
1928 		err = dso__load_sym_internal(dso, map, runtime_ss, runtime_ss,
1929 					     kmodule, 1);
1930 		if (err < 0)
1931 			return err;
1932 		nr += err;
1933 	}
1934 
1935 	return nr;
1936 }
1937 
elf_read_maps(Elf * elf,bool exe,mapfn_t mapfn,void * data)1938 static int elf_read_maps(Elf *elf, bool exe, mapfn_t mapfn, void *data)
1939 {
1940 	GElf_Phdr phdr;
1941 	size_t i, phdrnum;
1942 	int err;
1943 	u64 sz;
1944 
1945 	if (elf_getphdrnum(elf, &phdrnum))
1946 		return -1;
1947 
1948 	for (i = 0; i < phdrnum; i++) {
1949 		if (gelf_getphdr(elf, i, &phdr) == NULL)
1950 			return -1;
1951 		if (phdr.p_type != PT_LOAD)
1952 			continue;
1953 		if (exe) {
1954 			if (!(phdr.p_flags & PF_X))
1955 				continue;
1956 		} else {
1957 			if (!(phdr.p_flags & PF_R))
1958 				continue;
1959 		}
1960 		sz = min(phdr.p_memsz, phdr.p_filesz);
1961 		if (!sz)
1962 			continue;
1963 		err = mapfn(phdr.p_vaddr, sz, phdr.p_offset, data);
1964 		if (err)
1965 			return err;
1966 	}
1967 	return 0;
1968 }
1969 
file__read_maps(int fd,bool exe,mapfn_t mapfn,void * data,bool * is_64_bit)1970 int file__read_maps(int fd, bool exe, mapfn_t mapfn, void *data,
1971 		    bool *is_64_bit)
1972 {
1973 	int err;
1974 	Elf *elf;
1975 
1976 	elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
1977 	if (elf == NULL)
1978 		return -1;
1979 
1980 	if (is_64_bit)
1981 		*is_64_bit = (gelf_getclass(elf) == ELFCLASS64);
1982 
1983 	err = elf_read_maps(elf, exe, mapfn, data);
1984 
1985 	elf_end(elf);
1986 	return err;
1987 }
1988 
dso__type_fd(int fd)1989 enum dso_type dso__type_fd(int fd)
1990 {
1991 	enum dso_type dso_type = DSO__TYPE_UNKNOWN;
1992 	GElf_Ehdr ehdr;
1993 	Elf_Kind ek;
1994 	Elf *elf;
1995 
1996 	elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
1997 	if (elf == NULL)
1998 		goto out;
1999 
2000 	ek = elf_kind(elf);
2001 	if (ek != ELF_K_ELF)
2002 		goto out_end;
2003 
2004 	if (gelf_getclass(elf) == ELFCLASS64) {
2005 		dso_type = DSO__TYPE_64BIT;
2006 		goto out_end;
2007 	}
2008 
2009 	if (gelf_getehdr(elf, &ehdr) == NULL)
2010 		goto out_end;
2011 
2012 	if (ehdr.e_machine == EM_X86_64)
2013 		dso_type = DSO__TYPE_X32BIT;
2014 	else
2015 		dso_type = DSO__TYPE_32BIT;
2016 out_end:
2017 	elf_end(elf);
2018 out:
2019 	return dso_type;
2020 }
2021 
copy_bytes(int from,off_t from_offs,int to,off_t to_offs,u64 len)2022 static int copy_bytes(int from, off_t from_offs, int to, off_t to_offs, u64 len)
2023 {
2024 	ssize_t r;
2025 	size_t n;
2026 	int err = -1;
2027 	char *buf = malloc(page_size);
2028 
2029 	if (buf == NULL)
2030 		return -1;
2031 
2032 	if (lseek(to, to_offs, SEEK_SET) != to_offs)
2033 		goto out;
2034 
2035 	if (lseek(from, from_offs, SEEK_SET) != from_offs)
2036 		goto out;
2037 
2038 	while (len) {
2039 		n = page_size;
2040 		if (len < n)
2041 			n = len;
2042 		/* Use read because mmap won't work on proc files */
2043 		r = read(from, buf, n);
2044 		if (r < 0)
2045 			goto out;
2046 		if (!r)
2047 			break;
2048 		n = r;
2049 		r = write(to, buf, n);
2050 		if (r < 0)
2051 			goto out;
2052 		if ((size_t)r != n)
2053 			goto out;
2054 		len -= n;
2055 	}
2056 
2057 	err = 0;
2058 out:
2059 	free(buf);
2060 	return err;
2061 }
2062 
2063 struct kcore {
2064 	int fd;
2065 	int elfclass;
2066 	Elf *elf;
2067 	GElf_Ehdr ehdr;
2068 };
2069 
kcore__open(struct kcore * kcore,const char * filename)2070 static int kcore__open(struct kcore *kcore, const char *filename)
2071 {
2072 	GElf_Ehdr *ehdr;
2073 
2074 	kcore->fd = open(filename, O_RDONLY);
2075 	if (kcore->fd == -1)
2076 		return -1;
2077 
2078 	kcore->elf = elf_begin(kcore->fd, ELF_C_READ, NULL);
2079 	if (!kcore->elf)
2080 		goto out_close;
2081 
2082 	kcore->elfclass = gelf_getclass(kcore->elf);
2083 	if (kcore->elfclass == ELFCLASSNONE)
2084 		goto out_end;
2085 
2086 	ehdr = gelf_getehdr(kcore->elf, &kcore->ehdr);
2087 	if (!ehdr)
2088 		goto out_end;
2089 
2090 	return 0;
2091 
2092 out_end:
2093 	elf_end(kcore->elf);
2094 out_close:
2095 	close(kcore->fd);
2096 	return -1;
2097 }
2098 
kcore__init(struct kcore * kcore,char * filename,int elfclass,bool temp)2099 static int kcore__init(struct kcore *kcore, char *filename, int elfclass,
2100 		       bool temp)
2101 {
2102 	kcore->elfclass = elfclass;
2103 
2104 	if (temp)
2105 		kcore->fd = mkstemp(filename);
2106 	else
2107 		kcore->fd = open(filename, O_WRONLY | O_CREAT | O_EXCL, 0400);
2108 	if (kcore->fd == -1)
2109 		return -1;
2110 
2111 	kcore->elf = elf_begin(kcore->fd, ELF_C_WRITE, NULL);
2112 	if (!kcore->elf)
2113 		goto out_close;
2114 
2115 	if (!gelf_newehdr(kcore->elf, elfclass))
2116 		goto out_end;
2117 
2118 	memset(&kcore->ehdr, 0, sizeof(GElf_Ehdr));
2119 
2120 	return 0;
2121 
2122 out_end:
2123 	elf_end(kcore->elf);
2124 out_close:
2125 	close(kcore->fd);
2126 	unlink(filename);
2127 	return -1;
2128 }
2129 
kcore__close(struct kcore * kcore)2130 static void kcore__close(struct kcore *kcore)
2131 {
2132 	elf_end(kcore->elf);
2133 	close(kcore->fd);
2134 }
2135 
kcore__copy_hdr(struct kcore * from,struct kcore * to,size_t count)2136 static int kcore__copy_hdr(struct kcore *from, struct kcore *to, size_t count)
2137 {
2138 	GElf_Ehdr *ehdr = &to->ehdr;
2139 	GElf_Ehdr *kehdr = &from->ehdr;
2140 
2141 	memcpy(ehdr->e_ident, kehdr->e_ident, EI_NIDENT);
2142 	ehdr->e_type      = kehdr->e_type;
2143 	ehdr->e_machine   = kehdr->e_machine;
2144 	ehdr->e_version   = kehdr->e_version;
2145 	ehdr->e_entry     = 0;
2146 	ehdr->e_shoff     = 0;
2147 	ehdr->e_flags     = kehdr->e_flags;
2148 	ehdr->e_phnum     = count;
2149 	ehdr->e_shentsize = 0;
2150 	ehdr->e_shnum     = 0;
2151 	ehdr->e_shstrndx  = 0;
2152 
2153 	if (from->elfclass == ELFCLASS32) {
2154 		ehdr->e_phoff     = sizeof(Elf32_Ehdr);
2155 		ehdr->e_ehsize    = sizeof(Elf32_Ehdr);
2156 		ehdr->e_phentsize = sizeof(Elf32_Phdr);
2157 	} else {
2158 		ehdr->e_phoff     = sizeof(Elf64_Ehdr);
2159 		ehdr->e_ehsize    = sizeof(Elf64_Ehdr);
2160 		ehdr->e_phentsize = sizeof(Elf64_Phdr);
2161 	}
2162 
2163 	if (!gelf_update_ehdr(to->elf, ehdr))
2164 		return -1;
2165 
2166 	if (!gelf_newphdr(to->elf, count))
2167 		return -1;
2168 
2169 	return 0;
2170 }
2171 
kcore__add_phdr(struct kcore * kcore,int idx,off_t offset,u64 addr,u64 len)2172 static int kcore__add_phdr(struct kcore *kcore, int idx, off_t offset,
2173 			   u64 addr, u64 len)
2174 {
2175 	GElf_Phdr phdr = {
2176 		.p_type		= PT_LOAD,
2177 		.p_flags	= PF_R | PF_W | PF_X,
2178 		.p_offset	= offset,
2179 		.p_vaddr	= addr,
2180 		.p_paddr	= 0,
2181 		.p_filesz	= len,
2182 		.p_memsz	= len,
2183 		.p_align	= page_size,
2184 	};
2185 
2186 	if (!gelf_update_phdr(kcore->elf, idx, &phdr))
2187 		return -1;
2188 
2189 	return 0;
2190 }
2191 
kcore__write(struct kcore * kcore)2192 static off_t kcore__write(struct kcore *kcore)
2193 {
2194 	return elf_update(kcore->elf, ELF_C_WRITE);
2195 }
2196 
2197 struct phdr_data {
2198 	off_t offset;
2199 	off_t rel;
2200 	u64 addr;
2201 	u64 len;
2202 	struct list_head node;
2203 	struct phdr_data *remaps;
2204 };
2205 
2206 struct sym_data {
2207 	u64 addr;
2208 	struct list_head node;
2209 };
2210 
2211 struct kcore_copy_info {
2212 	u64 stext;
2213 	u64 etext;
2214 	u64 first_symbol;
2215 	u64 last_symbol;
2216 	u64 first_module;
2217 	u64 first_module_symbol;
2218 	u64 last_module_symbol;
2219 	size_t phnum;
2220 	struct list_head phdrs;
2221 	struct list_head syms;
2222 };
2223 
2224 #define kcore_copy__for_each_phdr(k, p) \
2225 	list_for_each_entry((p), &(k)->phdrs, node)
2226 
phdr_data__new(u64 addr,u64 len,off_t offset)2227 static struct phdr_data *phdr_data__new(u64 addr, u64 len, off_t offset)
2228 {
2229 	struct phdr_data *p = zalloc(sizeof(*p));
2230 
2231 	if (p) {
2232 		p->addr   = addr;
2233 		p->len    = len;
2234 		p->offset = offset;
2235 	}
2236 
2237 	return p;
2238 }
2239 
kcore_copy_info__addnew(struct kcore_copy_info * kci,u64 addr,u64 len,off_t offset)2240 static struct phdr_data *kcore_copy_info__addnew(struct kcore_copy_info *kci,
2241 						 u64 addr, u64 len,
2242 						 off_t offset)
2243 {
2244 	struct phdr_data *p = phdr_data__new(addr, len, offset);
2245 
2246 	if (p)
2247 		list_add_tail(&p->node, &kci->phdrs);
2248 
2249 	return p;
2250 }
2251 
kcore_copy__free_phdrs(struct kcore_copy_info * kci)2252 static void kcore_copy__free_phdrs(struct kcore_copy_info *kci)
2253 {
2254 	struct phdr_data *p, *tmp;
2255 
2256 	list_for_each_entry_safe(p, tmp, &kci->phdrs, node) {
2257 		list_del_init(&p->node);
2258 		free(p);
2259 	}
2260 }
2261 
kcore_copy__new_sym(struct kcore_copy_info * kci,u64 addr)2262 static struct sym_data *kcore_copy__new_sym(struct kcore_copy_info *kci,
2263 					    u64 addr)
2264 {
2265 	struct sym_data *s = zalloc(sizeof(*s));
2266 
2267 	if (s) {
2268 		s->addr = addr;
2269 		list_add_tail(&s->node, &kci->syms);
2270 	}
2271 
2272 	return s;
2273 }
2274 
kcore_copy__free_syms(struct kcore_copy_info * kci)2275 static void kcore_copy__free_syms(struct kcore_copy_info *kci)
2276 {
2277 	struct sym_data *s, *tmp;
2278 
2279 	list_for_each_entry_safe(s, tmp, &kci->syms, node) {
2280 		list_del_init(&s->node);
2281 		free(s);
2282 	}
2283 }
2284 
kcore_copy__process_kallsyms(void * arg,const char * name,char type,u64 start)2285 static int kcore_copy__process_kallsyms(void *arg, const char *name, char type,
2286 					u64 start)
2287 {
2288 	struct kcore_copy_info *kci = arg;
2289 
2290 	if (!kallsyms__is_function(type))
2291 		return 0;
2292 
2293 	if (strchr(name, '[')) {
2294 		if (!kci->first_module_symbol || start < kci->first_module_symbol)
2295 			kci->first_module_symbol = start;
2296 		if (start > kci->last_module_symbol)
2297 			kci->last_module_symbol = start;
2298 		return 0;
2299 	}
2300 
2301 	if (!kci->first_symbol || start < kci->first_symbol)
2302 		kci->first_symbol = start;
2303 
2304 	if (!kci->last_symbol || start > kci->last_symbol)
2305 		kci->last_symbol = start;
2306 
2307 	if (!strcmp(name, "_stext")) {
2308 		kci->stext = start;
2309 		return 0;
2310 	}
2311 
2312 	if (!strcmp(name, "_etext")) {
2313 		kci->etext = start;
2314 		return 0;
2315 	}
2316 
2317 	if (is_entry_trampoline(name) && !kcore_copy__new_sym(kci, start))
2318 		return -1;
2319 
2320 	return 0;
2321 }
2322 
kcore_copy__parse_kallsyms(struct kcore_copy_info * kci,const char * dir)2323 static int kcore_copy__parse_kallsyms(struct kcore_copy_info *kci,
2324 				      const char *dir)
2325 {
2326 	char kallsyms_filename[PATH_MAX];
2327 
2328 	scnprintf(kallsyms_filename, PATH_MAX, "%s/kallsyms", dir);
2329 
2330 	if (symbol__restricted_filename(kallsyms_filename, "/proc/kallsyms"))
2331 		return -1;
2332 
2333 	if (kallsyms__parse(kallsyms_filename, kci,
2334 			    kcore_copy__process_kallsyms) < 0)
2335 		return -1;
2336 
2337 	return 0;
2338 }
2339 
kcore_copy__process_modules(void * arg,const char * name __maybe_unused,u64 start,u64 size __maybe_unused)2340 static int kcore_copy__process_modules(void *arg,
2341 				       const char *name __maybe_unused,
2342 				       u64 start, u64 size __maybe_unused)
2343 {
2344 	struct kcore_copy_info *kci = arg;
2345 
2346 	if (!kci->first_module || start < kci->first_module)
2347 		kci->first_module = start;
2348 
2349 	return 0;
2350 }
2351 
kcore_copy__parse_modules(struct kcore_copy_info * kci,const char * dir)2352 static int kcore_copy__parse_modules(struct kcore_copy_info *kci,
2353 				     const char *dir)
2354 {
2355 	char modules_filename[PATH_MAX];
2356 
2357 	scnprintf(modules_filename, PATH_MAX, "%s/modules", dir);
2358 
2359 	if (symbol__restricted_filename(modules_filename, "/proc/modules"))
2360 		return -1;
2361 
2362 	if (modules__parse(modules_filename, kci,
2363 			   kcore_copy__process_modules) < 0)
2364 		return -1;
2365 
2366 	return 0;
2367 }
2368 
kcore_copy__map(struct kcore_copy_info * kci,u64 start,u64 end,u64 pgoff,u64 s,u64 e)2369 static int kcore_copy__map(struct kcore_copy_info *kci, u64 start, u64 end,
2370 			   u64 pgoff, u64 s, u64 e)
2371 {
2372 	u64 len, offset;
2373 
2374 	if (s < start || s >= end)
2375 		return 0;
2376 
2377 	offset = (s - start) + pgoff;
2378 	len = e < end ? e - s : end - s;
2379 
2380 	return kcore_copy_info__addnew(kci, s, len, offset) ? 0 : -1;
2381 }
2382 
kcore_copy__read_map(u64 start,u64 len,u64 pgoff,void * data)2383 static int kcore_copy__read_map(u64 start, u64 len, u64 pgoff, void *data)
2384 {
2385 	struct kcore_copy_info *kci = data;
2386 	u64 end = start + len;
2387 	struct sym_data *sdat;
2388 
2389 	if (kcore_copy__map(kci, start, end, pgoff, kci->stext, kci->etext))
2390 		return -1;
2391 
2392 	if (kcore_copy__map(kci, start, end, pgoff, kci->first_module,
2393 			    kci->last_module_symbol))
2394 		return -1;
2395 
2396 	list_for_each_entry(sdat, &kci->syms, node) {
2397 		u64 s = round_down(sdat->addr, page_size);
2398 
2399 		if (kcore_copy__map(kci, start, end, pgoff, s, s + len))
2400 			return -1;
2401 	}
2402 
2403 	return 0;
2404 }
2405 
kcore_copy__read_maps(struct kcore_copy_info * kci,Elf * elf)2406 static int kcore_copy__read_maps(struct kcore_copy_info *kci, Elf *elf)
2407 {
2408 	if (elf_read_maps(elf, true, kcore_copy__read_map, kci) < 0)
2409 		return -1;
2410 
2411 	return 0;
2412 }
2413 
kcore_copy__find_remaps(struct kcore_copy_info * kci)2414 static void kcore_copy__find_remaps(struct kcore_copy_info *kci)
2415 {
2416 	struct phdr_data *p, *k = NULL;
2417 	u64 kend;
2418 
2419 	if (!kci->stext)
2420 		return;
2421 
2422 	/* Find phdr that corresponds to the kernel map (contains stext) */
2423 	kcore_copy__for_each_phdr(kci, p) {
2424 		u64 pend = p->addr + p->len - 1;
2425 
2426 		if (p->addr <= kci->stext && pend >= kci->stext) {
2427 			k = p;
2428 			break;
2429 		}
2430 	}
2431 
2432 	if (!k)
2433 		return;
2434 
2435 	kend = k->offset + k->len;
2436 
2437 	/* Find phdrs that remap the kernel */
2438 	kcore_copy__for_each_phdr(kci, p) {
2439 		u64 pend = p->offset + p->len;
2440 
2441 		if (p == k)
2442 			continue;
2443 
2444 		if (p->offset >= k->offset && pend <= kend)
2445 			p->remaps = k;
2446 	}
2447 }
2448 
kcore_copy__layout(struct kcore_copy_info * kci)2449 static void kcore_copy__layout(struct kcore_copy_info *kci)
2450 {
2451 	struct phdr_data *p;
2452 	off_t rel = 0;
2453 
2454 	kcore_copy__find_remaps(kci);
2455 
2456 	kcore_copy__for_each_phdr(kci, p) {
2457 		if (!p->remaps) {
2458 			p->rel = rel;
2459 			rel += p->len;
2460 		}
2461 		kci->phnum += 1;
2462 	}
2463 
2464 	kcore_copy__for_each_phdr(kci, p) {
2465 		struct phdr_data *k = p->remaps;
2466 
2467 		if (k)
2468 			p->rel = p->offset - k->offset + k->rel;
2469 	}
2470 }
2471 
kcore_copy__calc_maps(struct kcore_copy_info * kci,const char * dir,Elf * elf)2472 static int kcore_copy__calc_maps(struct kcore_copy_info *kci, const char *dir,
2473 				 Elf *elf)
2474 {
2475 	if (kcore_copy__parse_kallsyms(kci, dir))
2476 		return -1;
2477 
2478 	if (kcore_copy__parse_modules(kci, dir))
2479 		return -1;
2480 
2481 	if (kci->stext)
2482 		kci->stext = round_down(kci->stext, page_size);
2483 	else
2484 		kci->stext = round_down(kci->first_symbol, page_size);
2485 
2486 	if (kci->etext) {
2487 		kci->etext = round_up(kci->etext, page_size);
2488 	} else if (kci->last_symbol) {
2489 		kci->etext = round_up(kci->last_symbol, page_size);
2490 		kci->etext += page_size;
2491 	}
2492 
2493 	if (kci->first_module_symbol &&
2494 	    (!kci->first_module || kci->first_module_symbol < kci->first_module))
2495 		kci->first_module = kci->first_module_symbol;
2496 
2497 	kci->first_module = round_down(kci->first_module, page_size);
2498 
2499 	if (kci->last_module_symbol) {
2500 		kci->last_module_symbol = round_up(kci->last_module_symbol,
2501 						   page_size);
2502 		kci->last_module_symbol += page_size;
2503 	}
2504 
2505 	if (!kci->stext || !kci->etext)
2506 		return -1;
2507 
2508 	if (kci->first_module && !kci->last_module_symbol)
2509 		return -1;
2510 
2511 	if (kcore_copy__read_maps(kci, elf))
2512 		return -1;
2513 
2514 	kcore_copy__layout(kci);
2515 
2516 	return 0;
2517 }
2518 
kcore_copy__copy_file(const char * from_dir,const char * to_dir,const char * name)2519 static int kcore_copy__copy_file(const char *from_dir, const char *to_dir,
2520 				 const char *name)
2521 {
2522 	char from_filename[PATH_MAX];
2523 	char to_filename[PATH_MAX];
2524 
2525 	scnprintf(from_filename, PATH_MAX, "%s/%s", from_dir, name);
2526 	scnprintf(to_filename, PATH_MAX, "%s/%s", to_dir, name);
2527 
2528 	return copyfile_mode(from_filename, to_filename, 0400);
2529 }
2530 
kcore_copy__unlink(const char * dir,const char * name)2531 static int kcore_copy__unlink(const char *dir, const char *name)
2532 {
2533 	char filename[PATH_MAX];
2534 
2535 	scnprintf(filename, PATH_MAX, "%s/%s", dir, name);
2536 
2537 	return unlink(filename);
2538 }
2539 
kcore_copy__compare_fds(int from,int to)2540 static int kcore_copy__compare_fds(int from, int to)
2541 {
2542 	char *buf_from;
2543 	char *buf_to;
2544 	ssize_t ret;
2545 	size_t len;
2546 	int err = -1;
2547 
2548 	buf_from = malloc(page_size);
2549 	buf_to = malloc(page_size);
2550 	if (!buf_from || !buf_to)
2551 		goto out;
2552 
2553 	while (1) {
2554 		/* Use read because mmap won't work on proc files */
2555 		ret = read(from, buf_from, page_size);
2556 		if (ret < 0)
2557 			goto out;
2558 
2559 		if (!ret)
2560 			break;
2561 
2562 		len = ret;
2563 
2564 		if (readn(to, buf_to, len) != (int)len)
2565 			goto out;
2566 
2567 		if (memcmp(buf_from, buf_to, len))
2568 			goto out;
2569 	}
2570 
2571 	err = 0;
2572 out:
2573 	free(buf_to);
2574 	free(buf_from);
2575 	return err;
2576 }
2577 
kcore_copy__compare_files(const char * from_filename,const char * to_filename)2578 static int kcore_copy__compare_files(const char *from_filename,
2579 				     const char *to_filename)
2580 {
2581 	int from, to, err = -1;
2582 
2583 	from = open(from_filename, O_RDONLY);
2584 	if (from < 0)
2585 		return -1;
2586 
2587 	to = open(to_filename, O_RDONLY);
2588 	if (to < 0)
2589 		goto out_close_from;
2590 
2591 	err = kcore_copy__compare_fds(from, to);
2592 
2593 	close(to);
2594 out_close_from:
2595 	close(from);
2596 	return err;
2597 }
2598 
kcore_copy__compare_file(const char * from_dir,const char * to_dir,const char * name)2599 static int kcore_copy__compare_file(const char *from_dir, const char *to_dir,
2600 				    const char *name)
2601 {
2602 	char from_filename[PATH_MAX];
2603 	char to_filename[PATH_MAX];
2604 
2605 	scnprintf(from_filename, PATH_MAX, "%s/%s", from_dir, name);
2606 	scnprintf(to_filename, PATH_MAX, "%s/%s", to_dir, name);
2607 
2608 	return kcore_copy__compare_files(from_filename, to_filename);
2609 }
2610 
2611 /**
2612  * kcore_copy - copy kallsyms, modules and kcore from one directory to another.
2613  * @from_dir: from directory
2614  * @to_dir: to directory
2615  *
2616  * This function copies kallsyms, modules and kcore files from one directory to
2617  * another.  kallsyms and modules are copied entirely.  Only code segments are
2618  * copied from kcore.  It is assumed that two segments suffice: one for the
2619  * kernel proper and one for all the modules.  The code segments are determined
2620  * from kallsyms and modules files.  The kernel map starts at _stext or the
2621  * lowest function symbol, and ends at _etext or the highest function symbol.
2622  * The module map starts at the lowest module address and ends at the highest
2623  * module symbol.  Start addresses are rounded down to the nearest page.  End
2624  * addresses are rounded up to the nearest page.  An extra page is added to the
2625  * highest kernel symbol and highest module symbol to, hopefully, encompass that
2626  * symbol too.  Because it contains only code sections, the resulting kcore is
2627  * unusual.  One significant peculiarity is that the mapping (start -> pgoff)
2628  * is not the same for the kernel map and the modules map.  That happens because
2629  * the data is copied adjacently whereas the original kcore has gaps.  Finally,
2630  * kallsyms file is compared with its copy to check that modules have not been
2631  * loaded or unloaded while the copies were taking place.
2632  *
2633  * Return: %0 on success, %-1 on failure.
2634  */
kcore_copy(const char * from_dir,const char * to_dir)2635 int kcore_copy(const char *from_dir, const char *to_dir)
2636 {
2637 	struct kcore kcore;
2638 	struct kcore extract;
2639 	int idx = 0, err = -1;
2640 	off_t offset, sz;
2641 	struct kcore_copy_info kci = { .stext = 0, };
2642 	char kcore_filename[PATH_MAX];
2643 	char extract_filename[PATH_MAX];
2644 	struct phdr_data *p;
2645 
2646 	INIT_LIST_HEAD(&kci.phdrs);
2647 	INIT_LIST_HEAD(&kci.syms);
2648 
2649 	if (kcore_copy__copy_file(from_dir, to_dir, "kallsyms"))
2650 		return -1;
2651 
2652 	if (kcore_copy__copy_file(from_dir, to_dir, "modules"))
2653 		goto out_unlink_kallsyms;
2654 
2655 	scnprintf(kcore_filename, PATH_MAX, "%s/kcore", from_dir);
2656 	scnprintf(extract_filename, PATH_MAX, "%s/kcore", to_dir);
2657 
2658 	if (kcore__open(&kcore, kcore_filename))
2659 		goto out_unlink_modules;
2660 
2661 	if (kcore_copy__calc_maps(&kci, from_dir, kcore.elf))
2662 		goto out_kcore_close;
2663 
2664 	if (kcore__init(&extract, extract_filename, kcore.elfclass, false))
2665 		goto out_kcore_close;
2666 
2667 	if (kcore__copy_hdr(&kcore, &extract, kci.phnum))
2668 		goto out_extract_close;
2669 
2670 	offset = gelf_fsize(extract.elf, ELF_T_EHDR, 1, EV_CURRENT) +
2671 		 gelf_fsize(extract.elf, ELF_T_PHDR, kci.phnum, EV_CURRENT);
2672 	offset = round_up(offset, page_size);
2673 
2674 	kcore_copy__for_each_phdr(&kci, p) {
2675 		off_t offs = p->rel + offset;
2676 
2677 		if (kcore__add_phdr(&extract, idx++, offs, p->addr, p->len))
2678 			goto out_extract_close;
2679 	}
2680 
2681 	sz = kcore__write(&extract);
2682 	if (sz < 0 || sz > offset)
2683 		goto out_extract_close;
2684 
2685 	kcore_copy__for_each_phdr(&kci, p) {
2686 		off_t offs = p->rel + offset;
2687 
2688 		if (p->remaps)
2689 			continue;
2690 		if (copy_bytes(kcore.fd, p->offset, extract.fd, offs, p->len))
2691 			goto out_extract_close;
2692 	}
2693 
2694 	if (kcore_copy__compare_file(from_dir, to_dir, "kallsyms"))
2695 		goto out_extract_close;
2696 
2697 	err = 0;
2698 
2699 out_extract_close:
2700 	kcore__close(&extract);
2701 	if (err)
2702 		unlink(extract_filename);
2703 out_kcore_close:
2704 	kcore__close(&kcore);
2705 out_unlink_modules:
2706 	if (err)
2707 		kcore_copy__unlink(to_dir, "modules");
2708 out_unlink_kallsyms:
2709 	if (err)
2710 		kcore_copy__unlink(to_dir, "kallsyms");
2711 
2712 	kcore_copy__free_phdrs(&kci);
2713 	kcore_copy__free_syms(&kci);
2714 
2715 	return err;
2716 }
2717 
kcore_extract__create(struct kcore_extract * kce)2718 int kcore_extract__create(struct kcore_extract *kce)
2719 {
2720 	struct kcore kcore;
2721 	struct kcore extract;
2722 	size_t count = 1;
2723 	int idx = 0, err = -1;
2724 	off_t offset = page_size, sz;
2725 
2726 	if (kcore__open(&kcore, kce->kcore_filename))
2727 		return -1;
2728 
2729 	strcpy(kce->extract_filename, PERF_KCORE_EXTRACT);
2730 	if (kcore__init(&extract, kce->extract_filename, kcore.elfclass, true))
2731 		goto out_kcore_close;
2732 
2733 	if (kcore__copy_hdr(&kcore, &extract, count))
2734 		goto out_extract_close;
2735 
2736 	if (kcore__add_phdr(&extract, idx, offset, kce->addr, kce->len))
2737 		goto out_extract_close;
2738 
2739 	sz = kcore__write(&extract);
2740 	if (sz < 0 || sz > offset)
2741 		goto out_extract_close;
2742 
2743 	if (copy_bytes(kcore.fd, kce->offs, extract.fd, offset, kce->len))
2744 		goto out_extract_close;
2745 
2746 	err = 0;
2747 
2748 out_extract_close:
2749 	kcore__close(&extract);
2750 	if (err)
2751 		unlink(kce->extract_filename);
2752 out_kcore_close:
2753 	kcore__close(&kcore);
2754 
2755 	return err;
2756 }
2757 
kcore_extract__delete(struct kcore_extract * kce)2758 void kcore_extract__delete(struct kcore_extract *kce)
2759 {
2760 	unlink(kce->extract_filename);
2761 }
2762 
2763 #ifdef HAVE_GELF_GETNOTE_SUPPORT
2764 
sdt_adjust_loc(struct sdt_note * tmp,GElf_Addr base_off)2765 static void sdt_adjust_loc(struct sdt_note *tmp, GElf_Addr base_off)
2766 {
2767 	if (!base_off)
2768 		return;
2769 
2770 	if (tmp->bit32)
2771 		tmp->addr.a32[SDT_NOTE_IDX_LOC] =
2772 			tmp->addr.a32[SDT_NOTE_IDX_LOC] + base_off -
2773 			tmp->addr.a32[SDT_NOTE_IDX_BASE];
2774 	else
2775 		tmp->addr.a64[SDT_NOTE_IDX_LOC] =
2776 			tmp->addr.a64[SDT_NOTE_IDX_LOC] + base_off -
2777 			tmp->addr.a64[SDT_NOTE_IDX_BASE];
2778 }
2779 
sdt_adjust_refctr(struct sdt_note * tmp,GElf_Addr base_addr,GElf_Addr base_off)2780 static void sdt_adjust_refctr(struct sdt_note *tmp, GElf_Addr base_addr,
2781 			      GElf_Addr base_off)
2782 {
2783 	if (!base_off)
2784 		return;
2785 
2786 	if (tmp->bit32 && tmp->addr.a32[SDT_NOTE_IDX_REFCTR])
2787 		tmp->addr.a32[SDT_NOTE_IDX_REFCTR] -= (base_addr - base_off);
2788 	else if (tmp->addr.a64[SDT_NOTE_IDX_REFCTR])
2789 		tmp->addr.a64[SDT_NOTE_IDX_REFCTR] -= (base_addr - base_off);
2790 }
2791 
2792 /**
2793  * populate_sdt_note : Parse raw data and identify SDT note
2794  * @elf: elf of the opened file
2795  * @data: raw data of a section with description offset applied
2796  * @len: note description size
2797  * @type: type of the note
2798  * @sdt_notes: List to add the SDT note
2799  *
2800  * Responsible for parsing the @data in section .note.stapsdt in @elf and
2801  * if its an SDT note, it appends to @sdt_notes list.
2802  */
populate_sdt_note(Elf ** elf,const char * data,size_t len,struct list_head * sdt_notes)2803 static int populate_sdt_note(Elf **elf, const char *data, size_t len,
2804 			     struct list_head *sdt_notes)
2805 {
2806 	const char *provider, *name, *args;
2807 	struct sdt_note *tmp = NULL;
2808 	GElf_Ehdr ehdr;
2809 	GElf_Shdr shdr;
2810 	int ret = -EINVAL;
2811 
2812 	union {
2813 		Elf64_Addr a64[NR_ADDR];
2814 		Elf32_Addr a32[NR_ADDR];
2815 	} buf;
2816 
2817 	Elf_Data dst = {
2818 		.d_buf = &buf, .d_type = ELF_T_ADDR, .d_version = EV_CURRENT,
2819 		.d_size = gelf_fsize((*elf), ELF_T_ADDR, NR_ADDR, EV_CURRENT),
2820 		.d_off = 0, .d_align = 0
2821 	};
2822 	Elf_Data src = {
2823 		.d_buf = (void *) data, .d_type = ELF_T_ADDR,
2824 		.d_version = EV_CURRENT, .d_size = dst.d_size, .d_off = 0,
2825 		.d_align = 0
2826 	};
2827 
2828 	tmp = (struct sdt_note *)calloc(1, sizeof(struct sdt_note));
2829 	if (!tmp) {
2830 		ret = -ENOMEM;
2831 		goto out_err;
2832 	}
2833 
2834 	INIT_LIST_HEAD(&tmp->note_list);
2835 
2836 	if (len < dst.d_size + 3)
2837 		goto out_free_note;
2838 
2839 	/* Translation from file representation to memory representation */
2840 	if (gelf_xlatetom(*elf, &dst, &src,
2841 			  elf_getident(*elf, NULL)[EI_DATA]) == NULL) {
2842 		pr_err("gelf_xlatetom : %s\n", elf_errmsg(-1));
2843 		goto out_free_note;
2844 	}
2845 
2846 	/* Populate the fields of sdt_note */
2847 	provider = data + dst.d_size;
2848 
2849 	name = (const char *)memchr(provider, '\0', data + len - provider);
2850 	if (name++ == NULL)
2851 		goto out_free_note;
2852 
2853 	tmp->provider = strdup(provider);
2854 	if (!tmp->provider) {
2855 		ret = -ENOMEM;
2856 		goto out_free_note;
2857 	}
2858 	tmp->name = strdup(name);
2859 	if (!tmp->name) {
2860 		ret = -ENOMEM;
2861 		goto out_free_prov;
2862 	}
2863 
2864 	args = memchr(name, '\0', data + len - name);
2865 
2866 	/*
2867 	 * There is no argument if:
2868 	 * - We reached the end of the note;
2869 	 * - There is not enough room to hold a potential string;
2870 	 * - The argument string is empty or just contains ':'.
2871 	 */
2872 	if (args == NULL || data + len - args < 2 ||
2873 		args[1] == ':' || args[1] == '\0')
2874 		tmp->args = NULL;
2875 	else {
2876 		tmp->args = strdup(++args);
2877 		if (!tmp->args) {
2878 			ret = -ENOMEM;
2879 			goto out_free_name;
2880 		}
2881 	}
2882 
2883 	if (gelf_getclass(*elf) == ELFCLASS32) {
2884 		memcpy(&tmp->addr, &buf, 3 * sizeof(Elf32_Addr));
2885 		tmp->bit32 = true;
2886 	} else {
2887 		memcpy(&tmp->addr, &buf, 3 * sizeof(Elf64_Addr));
2888 		tmp->bit32 = false;
2889 	}
2890 
2891 	if (!gelf_getehdr(*elf, &ehdr)) {
2892 		pr_debug("%s : cannot get elf header.\n", __func__);
2893 		ret = -EBADF;
2894 		goto out_free_args;
2895 	}
2896 
2897 	/* Adjust the prelink effect :
2898 	 * Find out the .stapsdt.base section.
2899 	 * This scn will help us to handle prelinking (if present).
2900 	 * Compare the retrieved file offset of the base section with the
2901 	 * base address in the description of the SDT note. If its different,
2902 	 * then accordingly, adjust the note location.
2903 	 */
2904 	if (elf_section_by_name(*elf, &ehdr, &shdr, SDT_BASE_SCN, NULL))
2905 		sdt_adjust_loc(tmp, shdr.sh_offset);
2906 
2907 	/* Adjust reference counter offset */
2908 	if (elf_section_by_name(*elf, &ehdr, &shdr, SDT_PROBES_SCN, NULL))
2909 		sdt_adjust_refctr(tmp, shdr.sh_addr, shdr.sh_offset);
2910 
2911 	list_add_tail(&tmp->note_list, sdt_notes);
2912 	return 0;
2913 
2914 out_free_args:
2915 	zfree(&tmp->args);
2916 out_free_name:
2917 	zfree(&tmp->name);
2918 out_free_prov:
2919 	zfree(&tmp->provider);
2920 out_free_note:
2921 	free(tmp);
2922 out_err:
2923 	return ret;
2924 }
2925 
2926 /**
2927  * construct_sdt_notes_list : constructs a list of SDT notes
2928  * @elf : elf to look into
2929  * @sdt_notes : empty list_head
2930  *
2931  * Scans the sections in 'elf' for the section
2932  * .note.stapsdt. It, then calls populate_sdt_note to find
2933  * out the SDT events and populates the 'sdt_notes'.
2934  */
construct_sdt_notes_list(Elf * elf,struct list_head * sdt_notes)2935 static int construct_sdt_notes_list(Elf *elf, struct list_head *sdt_notes)
2936 {
2937 	GElf_Ehdr ehdr;
2938 	Elf_Scn *scn = NULL;
2939 	Elf_Data *data;
2940 	GElf_Shdr shdr;
2941 	size_t shstrndx, next;
2942 	GElf_Nhdr nhdr;
2943 	size_t name_off, desc_off, offset;
2944 	int ret = 0;
2945 
2946 	if (gelf_getehdr(elf, &ehdr) == NULL) {
2947 		ret = -EBADF;
2948 		goto out_ret;
2949 	}
2950 	if (elf_getshdrstrndx(elf, &shstrndx) != 0) {
2951 		ret = -EBADF;
2952 		goto out_ret;
2953 	}
2954 
2955 	/* Look for the required section */
2956 	scn = elf_section_by_name(elf, &ehdr, &shdr, SDT_NOTE_SCN, NULL);
2957 	if (!scn) {
2958 		ret = -ENOENT;
2959 		goto out_ret;
2960 	}
2961 
2962 	if ((shdr.sh_type != SHT_NOTE) || (shdr.sh_flags & SHF_ALLOC)) {
2963 		ret = -ENOENT;
2964 		goto out_ret;
2965 	}
2966 
2967 	data = elf_getdata(scn, NULL);
2968 
2969 	/* Get the SDT notes */
2970 	for (offset = 0; (next = gelf_getnote(data, offset, &nhdr, &name_off,
2971 					      &desc_off)) > 0; offset = next) {
2972 		if (nhdr.n_namesz == sizeof(SDT_NOTE_NAME) &&
2973 		    !memcmp(data->d_buf + name_off, SDT_NOTE_NAME,
2974 			    sizeof(SDT_NOTE_NAME))) {
2975 			/* Check the type of the note */
2976 			if (nhdr.n_type != SDT_NOTE_TYPE)
2977 				goto out_ret;
2978 
2979 			ret = populate_sdt_note(&elf, ((data->d_buf) + desc_off),
2980 						nhdr.n_descsz, sdt_notes);
2981 			if (ret < 0)
2982 				goto out_ret;
2983 		}
2984 	}
2985 	if (list_empty(sdt_notes))
2986 		ret = -ENOENT;
2987 
2988 out_ret:
2989 	return ret;
2990 }
2991 
2992 /**
2993  * get_sdt_note_list : Wrapper to construct a list of sdt notes
2994  * @head : empty list_head
2995  * @target : file to find SDT notes from
2996  *
2997  * This opens the file, initializes
2998  * the ELF and then calls construct_sdt_notes_list.
2999  */
get_sdt_note_list(struct list_head * head,const char * target)3000 int get_sdt_note_list(struct list_head *head, const char *target)
3001 {
3002 	Elf *elf;
3003 	int fd, ret;
3004 
3005 	fd = open(target, O_RDONLY);
3006 	if (fd < 0)
3007 		return -EBADF;
3008 
3009 	elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
3010 	if (!elf) {
3011 		ret = -EBADF;
3012 		goto out_close;
3013 	}
3014 	ret = construct_sdt_notes_list(elf, head);
3015 	elf_end(elf);
3016 out_close:
3017 	close(fd);
3018 	return ret;
3019 }
3020 
3021 /**
3022  * cleanup_sdt_note_list : free the sdt notes' list
3023  * @sdt_notes: sdt notes' list
3024  *
3025  * Free up the SDT notes in @sdt_notes.
3026  * Returns the number of SDT notes free'd.
3027  */
cleanup_sdt_note_list(struct list_head * sdt_notes)3028 int cleanup_sdt_note_list(struct list_head *sdt_notes)
3029 {
3030 	struct sdt_note *tmp, *pos;
3031 	int nr_free = 0;
3032 
3033 	list_for_each_entry_safe(pos, tmp, sdt_notes, note_list) {
3034 		list_del_init(&pos->note_list);
3035 		zfree(&pos->args);
3036 		zfree(&pos->name);
3037 		zfree(&pos->provider);
3038 		free(pos);
3039 		nr_free++;
3040 	}
3041 	return nr_free;
3042 }
3043 
3044 /**
3045  * sdt_notes__get_count: Counts the number of sdt events
3046  * @start: list_head to sdt_notes list
3047  *
3048  * Returns the number of SDT notes in a list
3049  */
sdt_notes__get_count(struct list_head * start)3050 int sdt_notes__get_count(struct list_head *start)
3051 {
3052 	struct sdt_note *sdt_ptr;
3053 	int count = 0;
3054 
3055 	list_for_each_entry(sdt_ptr, start, note_list)
3056 		count++;
3057 	return count;
3058 }
3059 #endif
3060 
symbol__elf_init(void)3061 void symbol__elf_init(void)
3062 {
3063 	elf_version(EV_CURRENT);
3064 }
3065