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