xref: /linux/tools/perf/util/symbol-elf.c (revision d1d10cea0895264cc3769e4d9719baa94f4b250b)
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,bool block)876 static int read_build_id(const char *filename, struct build_id *bid, bool block)
877 {
878 	size_t size = sizeof(bid->data);
879 	int err = -1, fd;
880 	bfd *abfd;
881 
882 	fd = open(filename, block ? O_RDONLY : (O_RDONLY | O_NONBLOCK));
883 	if (fd < 0)
884 		return -1;
885 
886 	abfd = bfd_fdopenr(filename, /*target=*/NULL, fd);
887 	if (!abfd)
888 		return -1;
889 
890 	if (!bfd_check_format(abfd, bfd_object)) {
891 		pr_debug2("%s: cannot read %s bfd file.\n", __func__, filename);
892 		goto out_close;
893 	}
894 
895 	if (!abfd->build_id || abfd->build_id->size > size)
896 		goto out_close;
897 
898 	memcpy(bid->data, abfd->build_id->data, abfd->build_id->size);
899 	memset(bid->data + abfd->build_id->size, 0, size - abfd->build_id->size);
900 	err = bid->size = abfd->build_id->size;
901 
902 out_close:
903 	bfd_close(abfd);
904 	return err;
905 }
906 
907 #else // HAVE_LIBBFD_BUILDID_SUPPORT
908 
read_build_id(const char * filename,struct build_id * bid,bool block)909 static int read_build_id(const char *filename, struct build_id *bid, bool block)
910 {
911 	size_t size = sizeof(bid->data);
912 	int fd, err = -1;
913 	Elf *elf;
914 
915 	if (size < BUILD_ID_SIZE)
916 		goto out;
917 
918 	fd = open(filename, block ? O_RDONLY : (O_RDONLY | O_NONBLOCK));
919 	if (fd < 0)
920 		goto out;
921 
922 	elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
923 	if (elf == NULL) {
924 		pr_debug2("%s: cannot read %s ELF file.\n", __func__, filename);
925 		goto out_close;
926 	}
927 
928 	err = elf_read_build_id(elf, bid->data, size);
929 	if (err > 0)
930 		bid->size = err;
931 
932 	elf_end(elf);
933 out_close:
934 	close(fd);
935 out:
936 	return err;
937 }
938 
939 #endif // HAVE_LIBBFD_BUILDID_SUPPORT
940 
filename__read_build_id(const char * filename,struct build_id * bid,bool block)941 int filename__read_build_id(const char *filename, struct build_id *bid, bool block)
942 {
943 	struct kmod_path m = { .name = NULL, };
944 	char path[PATH_MAX];
945 	int err;
946 
947 	if (!filename)
948 		return -EFAULT;
949 
950 	err = kmod_path__parse(&m, filename);
951 	if (err)
952 		return -1;
953 
954 	if (m.comp) {
955 		int error = 0, fd;
956 
957 		fd = filename__decompress(filename, path, sizeof(path), m.comp, &error);
958 		if (fd < 0) {
959 			pr_debug("Failed to decompress (error %d) %s\n",
960 				 error, filename);
961 			return -1;
962 		}
963 		close(fd);
964 		filename = path;
965 		block = true;
966 	}
967 
968 	err = read_build_id(filename, bid, block);
969 
970 	if (m.comp)
971 		unlink(filename);
972 	return err;
973 }
974 
sysfs__read_build_id(const char * filename,struct build_id * bid)975 int sysfs__read_build_id(const char *filename, struct build_id *bid)
976 {
977 	size_t size = sizeof(bid->data);
978 	int fd, err = -1;
979 
980 	fd = open(filename, O_RDONLY);
981 	if (fd < 0)
982 		goto out;
983 
984 	while (1) {
985 		char bf[BUFSIZ];
986 		GElf_Nhdr nhdr;
987 		size_t namesz, descsz;
988 
989 		if (read(fd, &nhdr, sizeof(nhdr)) != sizeof(nhdr))
990 			break;
991 
992 		namesz = NOTE_ALIGN(nhdr.n_namesz);
993 		descsz = NOTE_ALIGN(nhdr.n_descsz);
994 		if (nhdr.n_type == NT_GNU_BUILD_ID &&
995 		    nhdr.n_namesz == sizeof("GNU")) {
996 			if (read(fd, bf, namesz) != (ssize_t)namesz)
997 				break;
998 			if (memcmp(bf, "GNU", sizeof("GNU")) == 0) {
999 				size_t sz = min(descsz, size);
1000 				if (read(fd, bid->data, sz) == (ssize_t)sz) {
1001 					memset(bid->data + sz, 0, size - sz);
1002 					bid->size = sz;
1003 					err = 0;
1004 					break;
1005 				}
1006 			} else if (read(fd, bf, descsz) != (ssize_t)descsz)
1007 				break;
1008 		} else {
1009 			int n = namesz + descsz;
1010 
1011 			if (n > (int)sizeof(bf)) {
1012 				n = sizeof(bf);
1013 				pr_debug("%s: truncating reading of build id in sysfs file %s: n_namesz=%u, n_descsz=%u.\n",
1014 					 __func__, filename, nhdr.n_namesz, nhdr.n_descsz);
1015 			}
1016 			if (read(fd, bf, n) != n)
1017 				break;
1018 		}
1019 	}
1020 	close(fd);
1021 out:
1022 	return err;
1023 }
1024 
1025 #ifdef HAVE_LIBBFD_SUPPORT
1026 
filename__read_debuglink(const char * filename,char * debuglink,size_t size)1027 int filename__read_debuglink(const char *filename, char *debuglink,
1028 			     size_t size)
1029 {
1030 	int err = -1;
1031 	asection *section;
1032 	bfd *abfd;
1033 
1034 	abfd = bfd_openr(filename, NULL);
1035 	if (!abfd)
1036 		return -1;
1037 
1038 	if (!bfd_check_format(abfd, bfd_object)) {
1039 		pr_debug2("%s: cannot read %s bfd file.\n", __func__, filename);
1040 		goto out_close;
1041 	}
1042 
1043 	section = bfd_get_section_by_name(abfd, ".gnu_debuglink");
1044 	if (!section)
1045 		goto out_close;
1046 
1047 	if (section->size > size)
1048 		goto out_close;
1049 
1050 	if (!bfd_get_section_contents(abfd, section, debuglink, 0,
1051 				      section->size))
1052 		goto out_close;
1053 
1054 	err = 0;
1055 
1056 out_close:
1057 	bfd_close(abfd);
1058 	return err;
1059 }
1060 
1061 #else
1062 
filename__read_debuglink(const char * filename,char * debuglink,size_t size)1063 int filename__read_debuglink(const char *filename, char *debuglink,
1064 			     size_t size)
1065 {
1066 	int fd, err = -1;
1067 	Elf *elf;
1068 	GElf_Ehdr ehdr;
1069 	GElf_Shdr shdr;
1070 	Elf_Data *data;
1071 	Elf_Scn *sec;
1072 	Elf_Kind ek;
1073 
1074 	fd = open(filename, O_RDONLY);
1075 	if (fd < 0)
1076 		goto out;
1077 
1078 	elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
1079 	if (elf == NULL) {
1080 		pr_debug2("%s: cannot read %s ELF file.\n", __func__, filename);
1081 		goto out_close;
1082 	}
1083 
1084 	ek = elf_kind(elf);
1085 	if (ek != ELF_K_ELF)
1086 		goto out_elf_end;
1087 
1088 	if (gelf_getehdr(elf, &ehdr) == NULL) {
1089 		pr_err("%s: cannot get elf header.\n", __func__);
1090 		goto out_elf_end;
1091 	}
1092 
1093 	sec = elf_section_by_name(elf, &ehdr, &shdr,
1094 				  ".gnu_debuglink", NULL);
1095 	if (sec == NULL)
1096 		goto out_elf_end;
1097 
1098 	data = elf_getdata(sec, NULL);
1099 	if (data == NULL)
1100 		goto out_elf_end;
1101 
1102 	/* the start of this section is a zero-terminated string */
1103 	strncpy(debuglink, data->d_buf, size);
1104 
1105 	err = 0;
1106 
1107 out_elf_end:
1108 	elf_end(elf);
1109 out_close:
1110 	close(fd);
1111 out:
1112 	return err;
1113 }
1114 
1115 #endif
1116 
symsrc__possibly_runtime(struct symsrc * ss)1117 bool symsrc__possibly_runtime(struct symsrc *ss)
1118 {
1119 	return ss->dynsym || ss->opdsec;
1120 }
1121 
symsrc__has_symtab(struct symsrc * ss)1122 bool symsrc__has_symtab(struct symsrc *ss)
1123 {
1124 	return ss->symtab != NULL;
1125 }
1126 
symsrc__destroy(struct symsrc * ss)1127 void symsrc__destroy(struct symsrc *ss)
1128 {
1129 	zfree(&ss->name);
1130 	elf_end(ss->elf);
1131 	close(ss->fd);
1132 }
1133 
elf__needs_adjust_symbols(GElf_Ehdr ehdr)1134 bool elf__needs_adjust_symbols(GElf_Ehdr ehdr)
1135 {
1136 	/*
1137 	 * Usually vmlinux is an ELF file with type ET_EXEC for most
1138 	 * architectures; except Arm64 kernel is linked with option
1139 	 * '-share', so need to check type ET_DYN.
1140 	 */
1141 	return ehdr.e_type == ET_EXEC || ehdr.e_type == ET_REL ||
1142 	       ehdr.e_type == ET_DYN;
1143 }
1144 
read_gnu_debugdata(struct dso * dso,Elf * elf,const char * name,int * fd_ret)1145 static Elf *read_gnu_debugdata(struct dso *dso, Elf *elf, const char *name, int *fd_ret)
1146 {
1147 	Elf *elf_embedded;
1148 	GElf_Ehdr ehdr;
1149 	GElf_Shdr shdr;
1150 	Elf_Scn *scn;
1151 	Elf_Data *scn_data;
1152 	FILE *wrapped;
1153 	size_t shndx;
1154 	char temp_filename[] = "/tmp/perf.gnu_debugdata.elf.XXXXXX";
1155 	int ret, temp_fd;
1156 
1157 	if (gelf_getehdr(elf, &ehdr) == NULL) {
1158 		pr_debug("%s: cannot read %s ELF file.\n", __func__, name);
1159 		*dso__load_errno(dso) = DSO_LOAD_ERRNO__INVALID_ELF;
1160 		return NULL;
1161 	}
1162 
1163 	scn = elf_section_by_name(elf, &ehdr, &shdr, ".gnu_debugdata", &shndx);
1164 	if (!scn) {
1165 		*dso__load_errno(dso) = -ENOENT;
1166 		return NULL;
1167 	}
1168 
1169 	if (shdr.sh_type == SHT_NOBITS) {
1170 		pr_debug("%s: .gnu_debugdata of ELF file %s has no data.\n", __func__, name);
1171 		*dso__load_errno(dso) = DSO_LOAD_ERRNO__INVALID_ELF;
1172 		return NULL;
1173 	}
1174 
1175 	scn_data = elf_rawdata(scn, NULL);
1176 	if (!scn_data) {
1177 		pr_debug("%s: error reading .gnu_debugdata of %s: %s\n", __func__,
1178 			 name, elf_errmsg(-1));
1179 		*dso__load_errno(dso) = DSO_LOAD_ERRNO__INVALID_ELF;
1180 		return NULL;
1181 	}
1182 
1183 	wrapped = fmemopen(scn_data->d_buf, scn_data->d_size, "r");
1184 	if (!wrapped) {
1185 		pr_debug("%s: fmemopen: %s\n", __func__, strerror(errno));
1186 		*dso__load_errno(dso) = -errno;
1187 		return NULL;
1188 	}
1189 
1190 	temp_fd = mkstemp(temp_filename);
1191 	if (temp_fd < 0) {
1192 		pr_debug("%s: mkstemp: %s\n", __func__, strerror(errno));
1193 		*dso__load_errno(dso) = -errno;
1194 		fclose(wrapped);
1195 		return NULL;
1196 	}
1197 	unlink(temp_filename);
1198 
1199 	ret = lzma_decompress_stream_to_file(wrapped, temp_fd);
1200 	fclose(wrapped);
1201 	if (ret < 0) {
1202 		*dso__load_errno(dso) = -errno;
1203 		close(temp_fd);
1204 		return NULL;
1205 	}
1206 
1207 	elf_embedded = elf_begin(temp_fd, PERF_ELF_C_READ_MMAP, NULL);
1208 	if (!elf_embedded) {
1209 		pr_debug("%s: error reading .gnu_debugdata of %s: %s\n", __func__,
1210 			 name, elf_errmsg(-1));
1211 		*dso__load_errno(dso) = DSO_LOAD_ERRNO__INVALID_ELF;
1212 		close(temp_fd);
1213 		return NULL;
1214 	}
1215 	pr_debug("%s: using .gnu_debugdata of %s\n", __func__, name);
1216 	*fd_ret = temp_fd;
1217 	return elf_embedded;
1218 }
1219 
symsrc__init(struct symsrc * ss,struct dso * dso,const char * name,enum dso_binary_type type)1220 int symsrc__init(struct symsrc *ss, struct dso *dso, const char *name,
1221 		 enum dso_binary_type type)
1222 {
1223 	GElf_Ehdr ehdr;
1224 	Elf *elf;
1225 	int fd;
1226 
1227 	if (dso__needs_decompress(dso)) {
1228 		fd = dso__decompress_kmodule_fd(dso, name);
1229 		if (fd < 0)
1230 			return -1;
1231 
1232 		type = dso__symtab_type(dso);
1233 	} else {
1234 		fd = open(name, O_RDONLY);
1235 		if (fd < 0) {
1236 			*dso__load_errno(dso) = errno;
1237 			return -1;
1238 		}
1239 	}
1240 
1241 	elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
1242 	if (elf == NULL) {
1243 		pr_debug("%s: cannot read %s ELF file.\n", __func__, name);
1244 		*dso__load_errno(dso) = DSO_LOAD_ERRNO__INVALID_ELF;
1245 		goto out_close;
1246 	}
1247 
1248 	if (type == DSO_BINARY_TYPE__GNU_DEBUGDATA) {
1249 		int new_fd;
1250 		Elf *embedded = read_gnu_debugdata(dso, elf, name, &new_fd);
1251 
1252 		if (!embedded)
1253 			goto out_close;
1254 
1255 		elf_end(elf);
1256 		close(fd);
1257 		fd = new_fd;
1258 		elf = embedded;
1259 	}
1260 
1261 	if (gelf_getehdr(elf, &ehdr) == NULL) {
1262 		*dso__load_errno(dso) = DSO_LOAD_ERRNO__INVALID_ELF;
1263 		pr_debug("%s: cannot get elf header.\n", __func__);
1264 		goto out_elf_end;
1265 	}
1266 
1267 	if (dso__swap_init(dso, ehdr.e_ident[EI_DATA])) {
1268 		*dso__load_errno(dso) = DSO_LOAD_ERRNO__INTERNAL_ERROR;
1269 		goto out_elf_end;
1270 	}
1271 
1272 	/* Always reject images with a mismatched build-id: */
1273 	if (dso__has_build_id(dso) && !symbol_conf.ignore_vmlinux_buildid) {
1274 		u8 build_id[BUILD_ID_SIZE];
1275 		struct build_id bid;
1276 		int size;
1277 
1278 		size = elf_read_build_id(elf, build_id, BUILD_ID_SIZE);
1279 		if (size <= 0) {
1280 			*dso__load_errno(dso) = DSO_LOAD_ERRNO__CANNOT_READ_BUILDID;
1281 			goto out_elf_end;
1282 		}
1283 
1284 		build_id__init(&bid, build_id, size);
1285 		if (!dso__build_id_equal(dso, &bid)) {
1286 			pr_debug("%s: build id mismatch for %s.\n", __func__, name);
1287 			*dso__load_errno(dso) = DSO_LOAD_ERRNO__MISMATCHING_BUILDID;
1288 			goto out_elf_end;
1289 		}
1290 	}
1291 
1292 	ss->is_64_bit = (gelf_getclass(elf) == ELFCLASS64);
1293 
1294 	ss->symtab_idx = 0;
1295 	ss->symtab = elf_section_by_name(elf, &ehdr, &ss->symshdr, ".symtab",
1296 			&ss->symtab_idx);
1297 	if (ss->symshdr.sh_type != SHT_SYMTAB)
1298 		ss->symtab = NULL;
1299 
1300 	ss->dynsym_idx = 0;
1301 	ss->dynsym = elf_section_by_name(elf, &ehdr, &ss->dynshdr, ".dynsym",
1302 			&ss->dynsym_idx);
1303 	if (ss->dynshdr.sh_type != SHT_DYNSYM)
1304 		ss->dynsym = NULL;
1305 
1306 	ss->opdidx = 0;
1307 	ss->opdsec = elf_section_by_name(elf, &ehdr, &ss->opdshdr, ".opd",
1308 			&ss->opdidx);
1309 	if (ss->opdshdr.sh_type != SHT_PROGBITS)
1310 		ss->opdsec = NULL;
1311 
1312 	if (dso__kernel(dso) == DSO_SPACE__USER)
1313 		ss->adjust_symbols = true;
1314 	else
1315 		ss->adjust_symbols = elf__needs_adjust_symbols(ehdr);
1316 
1317 	ss->name   = strdup(name);
1318 	if (!ss->name) {
1319 		*dso__load_errno(dso) = errno;
1320 		goto out_elf_end;
1321 	}
1322 
1323 	ss->elf    = elf;
1324 	ss->fd     = fd;
1325 	ss->ehdr   = ehdr;
1326 	ss->type   = type;
1327 
1328 	return 0;
1329 
1330 out_elf_end:
1331 	elf_end(elf);
1332 out_close:
1333 	close(fd);
1334 	return -1;
1335 }
1336 
is_exe_text(int flags)1337 static bool is_exe_text(int flags)
1338 {
1339 	return (flags & (SHF_ALLOC | SHF_EXECINSTR)) == (SHF_ALLOC | SHF_EXECINSTR);
1340 }
1341 
1342 /*
1343  * Some executable module sections like .noinstr.text might be laid out with
1344  * .text so they can use the same mapping (memory address to file offset).
1345  * Check if that is the case. Refer to kernel layout_sections(). Return the
1346  * maximum offset.
1347  */
max_text_section(Elf * elf,GElf_Ehdr * ehdr)1348 static u64 max_text_section(Elf *elf, GElf_Ehdr *ehdr)
1349 {
1350 	Elf_Scn *sec = NULL;
1351 	GElf_Shdr shdr;
1352 	u64 offs = 0;
1353 
1354 	/* Doesn't work for some arch */
1355 	if (ehdr->e_machine == EM_PARISC ||
1356 	    ehdr->e_machine == EM_ALPHA)
1357 		return 0;
1358 
1359 	/* ELF is corrupted/truncated, avoid calling elf_strptr. */
1360 	if (!elf_rawdata(elf_getscn(elf, ehdr->e_shstrndx), NULL))
1361 		return 0;
1362 
1363 	while ((sec = elf_nextscn(elf, sec)) != NULL) {
1364 		char *sec_name;
1365 
1366 		if (!gelf_getshdr(sec, &shdr))
1367 			break;
1368 
1369 		if (!is_exe_text(shdr.sh_flags))
1370 			continue;
1371 
1372 		/* .init and .exit sections are not placed with .text */
1373 		sec_name = elf_strptr(elf, ehdr->e_shstrndx, shdr.sh_name);
1374 		if (!sec_name ||
1375 		    strstarts(sec_name, ".init") ||
1376 		    strstarts(sec_name, ".exit"))
1377 			break;
1378 
1379 		/* Must be next to previous, assumes .text is first */
1380 		if (offs && PERF_ALIGN(offs, shdr.sh_addralign ?: 1) != shdr.sh_offset)
1381 			break;
1382 
1383 		offs = shdr.sh_offset + shdr.sh_size;
1384 	}
1385 
1386 	return offs;
1387 }
1388 
1389 /**
1390  * ref_reloc_sym_not_found - has kernel relocation symbol been found.
1391  * @kmap: kernel maps and relocation reference symbol
1392  *
1393  * This function returns %true if we are dealing with the kernel maps and the
1394  * relocation reference symbol has not yet been found.  Otherwise %false is
1395  * returned.
1396  */
ref_reloc_sym_not_found(struct kmap * kmap)1397 static bool ref_reloc_sym_not_found(struct kmap *kmap)
1398 {
1399 	return kmap && kmap->ref_reloc_sym && kmap->ref_reloc_sym->name &&
1400 	       !kmap->ref_reloc_sym->unrelocated_addr;
1401 }
1402 
1403 /**
1404  * ref_reloc - kernel relocation offset.
1405  * @kmap: kernel maps and relocation reference symbol
1406  *
1407  * This function returns the offset of kernel addresses as determined by using
1408  * the relocation reference symbol i.e. if the kernel has not been relocated
1409  * then the return value is zero.
1410  */
ref_reloc(struct kmap * kmap)1411 static u64 ref_reloc(struct kmap *kmap)
1412 {
1413 	if (kmap && kmap->ref_reloc_sym &&
1414 	    kmap->ref_reloc_sym->unrelocated_addr)
1415 		return kmap->ref_reloc_sym->addr -
1416 		       kmap->ref_reloc_sym->unrelocated_addr;
1417 	return 0;
1418 }
1419 
arch__sym_update(struct symbol * s __maybe_unused,GElf_Sym * sym __maybe_unused)1420 void __weak arch__sym_update(struct symbol *s __maybe_unused,
1421 		GElf_Sym *sym __maybe_unused) { }
1422 
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)1423 static int dso__process_kernel_symbol(struct dso *dso, struct map *map,
1424 				      GElf_Sym *sym, GElf_Shdr *shdr,
1425 				      struct maps *kmaps, struct kmap *kmap,
1426 				      struct dso **curr_dsop,
1427 				      const char *section_name,
1428 				      bool adjust_kernel_syms, bool kmodule, bool *remap_kernel,
1429 				      u64 max_text_sh_offset)
1430 {
1431 	struct dso *curr_dso = *curr_dsop;
1432 	struct map *curr_map;
1433 	char dso_name[PATH_MAX];
1434 
1435 	/* Adjust symbol to map to file offset */
1436 	if (adjust_kernel_syms)
1437 		sym->st_value -= shdr->sh_addr - shdr->sh_offset;
1438 
1439 	if (strcmp(section_name, (dso__short_name(curr_dso) + dso__short_name_len(dso))) == 0)
1440 		return 0;
1441 
1442 	if (strcmp(section_name, ".text") == 0) {
1443 		/*
1444 		 * The initial kernel mapping is based on
1445 		 * kallsyms and identity maps.  Overwrite it to
1446 		 * map to the kernel dso.
1447 		 */
1448 		if (*remap_kernel && dso__kernel(dso) && !kmodule) {
1449 			*remap_kernel = false;
1450 			map__set_start(map, shdr->sh_addr + ref_reloc(kmap));
1451 			map__set_end(map, map__start(map) + shdr->sh_size);
1452 			map__set_pgoff(map, shdr->sh_offset);
1453 			map__set_mapping_type(map, MAPPING_TYPE__DSO);
1454 			/* Ensure maps are correctly ordered */
1455 			if (kmaps) {
1456 				int err;
1457 				struct map *tmp = map__get(map);
1458 
1459 				maps__remove(kmaps, map);
1460 				err = maps__insert(kmaps, map);
1461 				map__put(tmp);
1462 				if (err)
1463 					return err;
1464 			}
1465 		}
1466 
1467 		/*
1468 		 * The initial module mapping is based on
1469 		 * /proc/modules mapped to offset zero.
1470 		 * Overwrite it to map to the module dso.
1471 		 */
1472 		if (*remap_kernel && kmodule) {
1473 			*remap_kernel = false;
1474 			map__set_pgoff(map, shdr->sh_offset);
1475 		}
1476 
1477 		dso__put(*curr_dsop);
1478 		*curr_dsop = dso__get(dso);
1479 		return 0;
1480 	}
1481 
1482 	if (!kmap)
1483 		return 0;
1484 
1485 	/*
1486 	 * perf does not record module section addresses except for .text, but
1487 	 * some sections can use the same mapping as .text.
1488 	 */
1489 	if (kmodule && adjust_kernel_syms && is_exe_text(shdr->sh_flags) &&
1490 	    shdr->sh_offset <= max_text_sh_offset) {
1491 		dso__put(*curr_dsop);
1492 		*curr_dsop = dso__get(dso);
1493 		return 0;
1494 	}
1495 
1496 	snprintf(dso_name, sizeof(dso_name), "%s%s", dso__short_name(dso), section_name);
1497 
1498 	curr_map = maps__find_by_name(kmaps, dso_name);
1499 	if (curr_map == NULL) {
1500 		u64 start = sym->st_value;
1501 
1502 		if (kmodule)
1503 			start += map__start(map) + shdr->sh_offset;
1504 
1505 		curr_dso = dso__new(dso_name);
1506 		if (curr_dso == NULL)
1507 			return -1;
1508 		dso__set_kernel(curr_dso, dso__kernel(dso));
1509 		RC_CHK_ACCESS(curr_dso)->long_name = dso__long_name(dso);
1510 		RC_CHK_ACCESS(curr_dso)->long_name_len = dso__long_name_len(dso);
1511 		dso__set_binary_type(curr_dso, dso__binary_type(dso));
1512 		dso__set_adjust_symbols(curr_dso, dso__adjust_symbols(dso));
1513 		curr_map = map__new2(start, curr_dso);
1514 		if (curr_map == NULL) {
1515 			dso__put(curr_dso);
1516 			return -1;
1517 		}
1518 		if (dso__kernel(curr_dso))
1519 			map__kmap(curr_map)->kmaps = kmaps;
1520 
1521 		if (adjust_kernel_syms) {
1522 			map__set_start(curr_map, shdr->sh_addr + ref_reloc(kmap));
1523 			map__set_end(curr_map, map__start(curr_map) + shdr->sh_size);
1524 			map__set_pgoff(curr_map, shdr->sh_offset);
1525 		} else {
1526 			map__set_mapping_type(curr_map, MAPPING_TYPE__IDENTITY);
1527 		}
1528 		dso__set_symtab_type(curr_dso, dso__symtab_type(dso));
1529 		if (maps__insert(kmaps, curr_map))
1530 			return -1;
1531 		dsos__add(&maps__machine(kmaps)->dsos, curr_dso);
1532 		dso__set_loaded(curr_dso);
1533 		dso__put(*curr_dsop);
1534 		*curr_dsop = curr_dso;
1535 	} else {
1536 		dso__put(*curr_dsop);
1537 		*curr_dsop = dso__get(map__dso(curr_map));
1538 	}
1539 	map__put(curr_map);
1540 
1541 	return 0;
1542 }
1543 
1544 static int
dso__load_sym_internal(struct dso * dso,struct map * map,struct symsrc * syms_ss,struct symsrc * runtime_ss,int kmodule,int dynsym)1545 dso__load_sym_internal(struct dso *dso, struct map *map, struct symsrc *syms_ss,
1546 		       struct symsrc *runtime_ss, int kmodule, int dynsym)
1547 {
1548 	struct kmap *kmap = dso__kernel(dso) ? map__kmap(map) : NULL;
1549 	struct maps *kmaps = kmap ? map__kmaps(map) : NULL;
1550 	struct dso *curr_dso = NULL;
1551 	Elf_Data *symstrs, *secstrs, *secstrs_run, *secstrs_sym;
1552 	uint32_t nr_syms;
1553 	uint32_t idx;
1554 	GElf_Ehdr ehdr;
1555 	GElf_Shdr shdr;
1556 	GElf_Shdr tshdr;
1557 	Elf_Data *syms, *opddata = NULL;
1558 	GElf_Sym sym;
1559 	Elf_Scn *sec, *sec_strndx;
1560 	Elf *elf;
1561 	int nr = 0;
1562 	bool remap_kernel = false, adjust_kernel_syms = false;
1563 	u64 max_text_sh_offset = 0;
1564 
1565 	if (kmap && !kmaps)
1566 		return -1;
1567 
1568 	elf = syms_ss->elf;
1569 	ehdr = syms_ss->ehdr;
1570 	if (dynsym) {
1571 		sec  = syms_ss->dynsym;
1572 		shdr = syms_ss->dynshdr;
1573 	} else {
1574 		sec =  syms_ss->symtab;
1575 		shdr = syms_ss->symshdr;
1576 	}
1577 
1578 	if (elf_section_by_name(runtime_ss->elf, &runtime_ss->ehdr, &tshdr,
1579 				".text", NULL)) {
1580 		dso__set_text_offset(dso, tshdr.sh_addr - tshdr.sh_offset);
1581 		dso__set_text_end(dso, tshdr.sh_offset + tshdr.sh_size);
1582 	}
1583 
1584 	if (runtime_ss->opdsec)
1585 		opddata = elf_rawdata(runtime_ss->opdsec, NULL);
1586 
1587 	syms = elf_getdata(sec, NULL);
1588 	if (syms == NULL)
1589 		goto out_elf_end;
1590 
1591 	sec = elf_getscn(elf, shdr.sh_link);
1592 	if (sec == NULL)
1593 		goto out_elf_end;
1594 
1595 	symstrs = elf_getdata(sec, NULL);
1596 	if (symstrs == NULL)
1597 		goto out_elf_end;
1598 
1599 	sec_strndx = elf_getscn(runtime_ss->elf, runtime_ss->ehdr.e_shstrndx);
1600 	if (sec_strndx == NULL)
1601 		goto out_elf_end;
1602 
1603 	secstrs_run = elf_getdata(sec_strndx, NULL);
1604 	if (secstrs_run == NULL)
1605 		goto out_elf_end;
1606 
1607 	sec_strndx = elf_getscn(elf, ehdr.e_shstrndx);
1608 	if (sec_strndx == NULL)
1609 		goto out_elf_end;
1610 
1611 	secstrs_sym = elf_getdata(sec_strndx, NULL);
1612 	if (secstrs_sym == NULL)
1613 		goto out_elf_end;
1614 
1615 	nr_syms = shdr.sh_size / shdr.sh_entsize;
1616 
1617 	memset(&sym, 0, sizeof(sym));
1618 
1619 	/*
1620 	 * The kernel relocation symbol is needed in advance in order to adjust
1621 	 * kernel maps correctly.
1622 	 */
1623 	if (ref_reloc_sym_not_found(kmap)) {
1624 		elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) {
1625 			const char *elf_name = elf_sym__name(&sym, symstrs);
1626 
1627 			if (strcmp(elf_name, kmap->ref_reloc_sym->name))
1628 				continue;
1629 			kmap->ref_reloc_sym->unrelocated_addr = sym.st_value;
1630 			map__set_reloc(map, kmap->ref_reloc_sym->addr - kmap->ref_reloc_sym->unrelocated_addr);
1631 			break;
1632 		}
1633 	}
1634 
1635 	/*
1636 	 * Handle any relocation of vdso necessary because older kernels
1637 	 * attempted to prelink vdso to its virtual address.
1638 	 */
1639 	if (dso__is_vdso(dso))
1640 		map__set_reloc(map, map__start(map) - dso__text_offset(dso));
1641 
1642 	dso__set_adjust_symbols(dso, runtime_ss->adjust_symbols || ref_reloc(kmap));
1643 	/*
1644 	 * Initial kernel and module mappings do not map to the dso.
1645 	 * Flag the fixups.
1646 	 */
1647 	if (dso__kernel(dso)) {
1648 		remap_kernel = true;
1649 		adjust_kernel_syms = dso__adjust_symbols(dso);
1650 	}
1651 
1652 	if (kmodule && adjust_kernel_syms)
1653 		max_text_sh_offset = max_text_section(runtime_ss->elf, &runtime_ss->ehdr);
1654 
1655 	curr_dso = dso__get(dso);
1656 	elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) {
1657 		struct symbol *f;
1658 		const char *elf_name = elf_sym__name(&sym, symstrs);
1659 		char *demangled = NULL;
1660 		int is_label = elf_sym__is_label(&sym);
1661 		const char *section_name;
1662 		bool used_opd = false;
1663 
1664 		if (!is_label && !elf_sym__filter(&sym))
1665 			continue;
1666 
1667 		/* Reject ARM ELF "mapping symbols": these aren't unique and
1668 		 * don't identify functions, so will confuse the profile
1669 		 * output: */
1670 		if (ehdr.e_machine == EM_ARM || ehdr.e_machine == EM_AARCH64) {
1671 			if (elf_name[0] == '$' && strchr("adtx", elf_name[1])
1672 			    && (elf_name[2] == '\0' || elf_name[2] == '.'))
1673 				continue;
1674 		}
1675 
1676 		/* Reject RISCV ELF "mapping symbols" */
1677 		if (ehdr.e_machine == EM_RISCV) {
1678 			if (elf_name[0] == '$' && strchr("dx", elf_name[1]))
1679 				continue;
1680 		}
1681 
1682 		if (runtime_ss->opdsec && sym.st_shndx == runtime_ss->opdidx) {
1683 			u32 offset = sym.st_value - syms_ss->opdshdr.sh_addr;
1684 			u64 *opd = opddata->d_buf + offset;
1685 			sym.st_value = DSO__SWAP(dso, u64, *opd);
1686 			sym.st_shndx = elf_addr_to_index(runtime_ss->elf,
1687 					sym.st_value);
1688 			used_opd = true;
1689 		}
1690 
1691 		/*
1692 		 * When loading symbols in a data mapping, ABS symbols (which
1693 		 * has a value of SHN_ABS in its st_shndx) failed at
1694 		 * elf_getscn().  And it marks the loading as a failure so
1695 		 * already loaded symbols cannot be fixed up.
1696 		 *
1697 		 * I'm not sure what should be done. Just ignore them for now.
1698 		 * - Namhyung Kim
1699 		 */
1700 		if (sym.st_shndx == SHN_ABS)
1701 			continue;
1702 
1703 		sec = elf_getscn(syms_ss->elf, sym.st_shndx);
1704 		if (!sec)
1705 			goto out_elf_end;
1706 
1707 		gelf_getshdr(sec, &shdr);
1708 
1709 		/*
1710 		 * If the attribute bit SHF_ALLOC is not set, the section
1711 		 * doesn't occupy memory during process execution.
1712 		 * E.g. ".gnu.warning.*" section is used by linker to generate
1713 		 * warnings when calling deprecated functions, the symbols in
1714 		 * the section aren't loaded to memory during process execution,
1715 		 * so skip them.
1716 		 */
1717 		if (!(shdr.sh_flags & SHF_ALLOC))
1718 			continue;
1719 
1720 		secstrs = secstrs_sym;
1721 
1722 		/*
1723 		 * We have to fallback to runtime when syms' section header has
1724 		 * NOBITS set. NOBITS results in file offset (sh_offset) not
1725 		 * being incremented. So sh_offset used below has different
1726 		 * values for syms (invalid) and runtime (valid).
1727 		 */
1728 		if (shdr.sh_type == SHT_NOBITS) {
1729 			sec = elf_getscn(runtime_ss->elf, sym.st_shndx);
1730 			if (!sec)
1731 				goto out_elf_end;
1732 
1733 			gelf_getshdr(sec, &shdr);
1734 			secstrs = secstrs_run;
1735 		}
1736 
1737 		if (is_label && !elf_sec__filter(&shdr, secstrs))
1738 			continue;
1739 
1740 		section_name = elf_sec__name(&shdr, secstrs);
1741 
1742 		/* On ARM, symbols for thumb functions have 1 added to
1743 		 * the symbol address as a flag - remove it */
1744 		if ((ehdr.e_machine == EM_ARM) &&
1745 		    (GELF_ST_TYPE(sym.st_info) == STT_FUNC) &&
1746 		    (sym.st_value & 1))
1747 			--sym.st_value;
1748 
1749 		if (dso__kernel(dso)) {
1750 			if (dso__process_kernel_symbol(dso, map, &sym, &shdr,
1751 						       kmaps, kmap, &curr_dso,
1752 						       section_name,
1753 						       adjust_kernel_syms,
1754 						       kmodule,
1755 						       &remap_kernel,
1756 						       max_text_sh_offset))
1757 				goto out_elf_end;
1758 		} else if ((used_opd && runtime_ss->adjust_symbols) ||
1759 			   (!used_opd && syms_ss->adjust_symbols)) {
1760 			GElf_Phdr phdr;
1761 
1762 			if (elf_read_program_header(runtime_ss->elf,
1763 						    (u64)sym.st_value, &phdr)) {
1764 				pr_debug4("%s: failed to find program header for "
1765 					   "symbol: %s st_value: %#" PRIx64 "\n",
1766 					   __func__, elf_name, (u64)sym.st_value);
1767 				pr_debug4("%s: adjusting symbol: st_value: %#" PRIx64 " "
1768 					"sh_addr: %#" PRIx64 " sh_offset: %#" PRIx64 "\n",
1769 					__func__, (u64)sym.st_value, (u64)shdr.sh_addr,
1770 					(u64)shdr.sh_offset);
1771 				/*
1772 				 * Fail to find program header, let's rollback
1773 				 * to use shdr.sh_addr and shdr.sh_offset to
1774 				 * calibrate symbol's file address, though this
1775 				 * is not necessary for normal C ELF file, we
1776 				 * still need to handle java JIT symbols in this
1777 				 * case.
1778 				 */
1779 				sym.st_value -= shdr.sh_addr - shdr.sh_offset;
1780 			} else {
1781 				pr_debug4("%s: adjusting symbol: st_value: %#" PRIx64 " "
1782 					"p_vaddr: %#" PRIx64 " p_offset: %#" PRIx64 "\n",
1783 					__func__, (u64)sym.st_value, (u64)phdr.p_vaddr,
1784 					(u64)phdr.p_offset);
1785 				sym.st_value -= phdr.p_vaddr - phdr.p_offset;
1786 			}
1787 		}
1788 
1789 		demangled = dso__demangle_sym(dso, kmodule, elf_name);
1790 		if (demangled != NULL)
1791 			elf_name = demangled;
1792 
1793 		f = symbol__new(sym.st_value, sym.st_size,
1794 				GELF_ST_BIND(sym.st_info),
1795 				GELF_ST_TYPE(sym.st_info), elf_name);
1796 		free(demangled);
1797 		if (!f)
1798 			goto out_elf_end;
1799 
1800 		arch__sym_update(f, &sym);
1801 
1802 		__symbols__insert(dso__symbols(curr_dso), f, dso__kernel(dso));
1803 		nr++;
1804 	}
1805 	dso__put(curr_dso);
1806 
1807 	/*
1808 	 * For misannotated, zeroed, ASM function sizes.
1809 	 */
1810 	if (nr > 0) {
1811 		symbols__fixup_end(dso__symbols(dso), false);
1812 		symbols__fixup_duplicate(dso__symbols(dso));
1813 		if (kmap) {
1814 			/*
1815 			 * We need to fixup this here too because we create new
1816 			 * maps here, for things like vsyscall sections.
1817 			 */
1818 			maps__fixup_end(kmaps);
1819 		}
1820 	}
1821 	return nr;
1822 out_elf_end:
1823 	dso__put(curr_dso);
1824 	return -1;
1825 }
1826 
dso__load_sym(struct dso * dso,struct map * map,struct symsrc * syms_ss,struct symsrc * runtime_ss,int kmodule)1827 int dso__load_sym(struct dso *dso, struct map *map, struct symsrc *syms_ss,
1828 		  struct symsrc *runtime_ss, int kmodule)
1829 {
1830 	int nr = 0;
1831 	int err = -1;
1832 
1833 	dso__set_symtab_type(dso, syms_ss->type);
1834 	dso__set_is_64_bit(dso, syms_ss->is_64_bit);
1835 	dso__set_rel(dso, syms_ss->ehdr.e_type == ET_REL);
1836 
1837 	/*
1838 	 * Modules may already have symbols from kallsyms, but those symbols
1839 	 * have the wrong values for the dso maps, so remove them.
1840 	 */
1841 	if (kmodule && syms_ss->symtab)
1842 		symbols__delete(dso__symbols(dso));
1843 
1844 	if (!syms_ss->symtab) {
1845 		/*
1846 		 * If the vmlinux is stripped, fail so we will fall back
1847 		 * to using kallsyms. The vmlinux runtime symbols aren't
1848 		 * of much use.
1849 		 */
1850 		if (dso__kernel(dso))
1851 			return err;
1852 	} else  {
1853 		err = dso__load_sym_internal(dso, map, syms_ss, runtime_ss,
1854 					     kmodule, 0);
1855 		if (err < 0)
1856 			return err;
1857 		nr = err;
1858 	}
1859 
1860 	if (syms_ss->dynsym) {
1861 		err = dso__load_sym_internal(dso, map, syms_ss, runtime_ss,
1862 					     kmodule, 1);
1863 		if (err < 0)
1864 			return err;
1865 		nr += err;
1866 	}
1867 
1868 	/*
1869 	 * The .gnu_debugdata is a special situation: it contains a symbol
1870 	 * table, but the runtime file may also contain dynsym entries which are
1871 	 * not present there. We need to load both.
1872 	 */
1873 	if (syms_ss->type == DSO_BINARY_TYPE__GNU_DEBUGDATA && runtime_ss->dynsym) {
1874 		err = dso__load_sym_internal(dso, map, runtime_ss, runtime_ss,
1875 					     kmodule, 1);
1876 		if (err < 0)
1877 			return err;
1878 		nr += err;
1879 	}
1880 
1881 	return nr;
1882 }
1883 
elf_read_maps(Elf * elf,bool exe,mapfn_t mapfn,void * data)1884 static int elf_read_maps(Elf *elf, bool exe, mapfn_t mapfn, void *data)
1885 {
1886 	GElf_Phdr phdr;
1887 	size_t i, phdrnum;
1888 	int err;
1889 	u64 sz;
1890 
1891 	if (elf_getphdrnum(elf, &phdrnum))
1892 		return -1;
1893 
1894 	for (i = 0; i < phdrnum; i++) {
1895 		if (gelf_getphdr(elf, i, &phdr) == NULL)
1896 			return -1;
1897 		if (phdr.p_type != PT_LOAD)
1898 			continue;
1899 		if (exe) {
1900 			if (!(phdr.p_flags & PF_X))
1901 				continue;
1902 		} else {
1903 			if (!(phdr.p_flags & PF_R))
1904 				continue;
1905 		}
1906 		sz = min(phdr.p_memsz, phdr.p_filesz);
1907 		if (!sz)
1908 			continue;
1909 		err = mapfn(phdr.p_vaddr, sz, phdr.p_offset, data);
1910 		if (err)
1911 			return err;
1912 	}
1913 	return 0;
1914 }
1915 
file__read_maps(int fd,bool exe,mapfn_t mapfn,void * data,bool * is_64_bit)1916 int file__read_maps(int fd, bool exe, mapfn_t mapfn, void *data,
1917 		    bool *is_64_bit)
1918 {
1919 	int err;
1920 	Elf *elf;
1921 
1922 	elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
1923 	if (elf == NULL)
1924 		return -1;
1925 
1926 	if (is_64_bit)
1927 		*is_64_bit = (gelf_getclass(elf) == ELFCLASS64);
1928 
1929 	err = elf_read_maps(elf, exe, mapfn, data);
1930 
1931 	elf_end(elf);
1932 	return err;
1933 }
1934 
dso__type_fd(int fd)1935 enum dso_type dso__type_fd(int fd)
1936 {
1937 	enum dso_type dso_type = DSO__TYPE_UNKNOWN;
1938 	GElf_Ehdr ehdr;
1939 	Elf_Kind ek;
1940 	Elf *elf;
1941 
1942 	elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
1943 	if (elf == NULL)
1944 		goto out;
1945 
1946 	ek = elf_kind(elf);
1947 	if (ek != ELF_K_ELF)
1948 		goto out_end;
1949 
1950 	if (gelf_getclass(elf) == ELFCLASS64) {
1951 		dso_type = DSO__TYPE_64BIT;
1952 		goto out_end;
1953 	}
1954 
1955 	if (gelf_getehdr(elf, &ehdr) == NULL)
1956 		goto out_end;
1957 
1958 	if (ehdr.e_machine == EM_X86_64)
1959 		dso_type = DSO__TYPE_X32BIT;
1960 	else
1961 		dso_type = DSO__TYPE_32BIT;
1962 out_end:
1963 	elf_end(elf);
1964 out:
1965 	return dso_type;
1966 }
1967 
copy_bytes(int from,off_t from_offs,int to,off_t to_offs,u64 len)1968 static int copy_bytes(int from, off_t from_offs, int to, off_t to_offs, u64 len)
1969 {
1970 	ssize_t r;
1971 	size_t n;
1972 	int err = -1;
1973 	char *buf = malloc(page_size);
1974 
1975 	if (buf == NULL)
1976 		return -1;
1977 
1978 	if (lseek(to, to_offs, SEEK_SET) != to_offs)
1979 		goto out;
1980 
1981 	if (lseek(from, from_offs, SEEK_SET) != from_offs)
1982 		goto out;
1983 
1984 	while (len) {
1985 		n = page_size;
1986 		if (len < n)
1987 			n = len;
1988 		/* Use read because mmap won't work on proc files */
1989 		r = read(from, buf, n);
1990 		if (r < 0)
1991 			goto out;
1992 		if (!r)
1993 			break;
1994 		n = r;
1995 		r = write(to, buf, n);
1996 		if (r < 0)
1997 			goto out;
1998 		if ((size_t)r != n)
1999 			goto out;
2000 		len -= n;
2001 	}
2002 
2003 	err = 0;
2004 out:
2005 	free(buf);
2006 	return err;
2007 }
2008 
2009 struct kcore {
2010 	int fd;
2011 	int elfclass;
2012 	Elf *elf;
2013 	GElf_Ehdr ehdr;
2014 };
2015 
kcore__open(struct kcore * kcore,const char * filename)2016 static int kcore__open(struct kcore *kcore, const char *filename)
2017 {
2018 	GElf_Ehdr *ehdr;
2019 
2020 	kcore->fd = open(filename, O_RDONLY);
2021 	if (kcore->fd == -1)
2022 		return -1;
2023 
2024 	kcore->elf = elf_begin(kcore->fd, ELF_C_READ, NULL);
2025 	if (!kcore->elf)
2026 		goto out_close;
2027 
2028 	kcore->elfclass = gelf_getclass(kcore->elf);
2029 	if (kcore->elfclass == ELFCLASSNONE)
2030 		goto out_end;
2031 
2032 	ehdr = gelf_getehdr(kcore->elf, &kcore->ehdr);
2033 	if (!ehdr)
2034 		goto out_end;
2035 
2036 	return 0;
2037 
2038 out_end:
2039 	elf_end(kcore->elf);
2040 out_close:
2041 	close(kcore->fd);
2042 	return -1;
2043 }
2044 
kcore__init(struct kcore * kcore,char * filename,int elfclass,bool temp)2045 static int kcore__init(struct kcore *kcore, char *filename, int elfclass,
2046 		       bool temp)
2047 {
2048 	kcore->elfclass = elfclass;
2049 
2050 	if (temp)
2051 		kcore->fd = mkstemp(filename);
2052 	else
2053 		kcore->fd = open(filename, O_WRONLY | O_CREAT | O_EXCL, 0400);
2054 	if (kcore->fd == -1)
2055 		return -1;
2056 
2057 	kcore->elf = elf_begin(kcore->fd, ELF_C_WRITE, NULL);
2058 	if (!kcore->elf)
2059 		goto out_close;
2060 
2061 	if (!gelf_newehdr(kcore->elf, elfclass))
2062 		goto out_end;
2063 
2064 	memset(&kcore->ehdr, 0, sizeof(GElf_Ehdr));
2065 
2066 	return 0;
2067 
2068 out_end:
2069 	elf_end(kcore->elf);
2070 out_close:
2071 	close(kcore->fd);
2072 	unlink(filename);
2073 	return -1;
2074 }
2075 
kcore__close(struct kcore * kcore)2076 static void kcore__close(struct kcore *kcore)
2077 {
2078 	elf_end(kcore->elf);
2079 	close(kcore->fd);
2080 }
2081 
kcore__copy_hdr(struct kcore * from,struct kcore * to,size_t count)2082 static int kcore__copy_hdr(struct kcore *from, struct kcore *to, size_t count)
2083 {
2084 	GElf_Ehdr *ehdr = &to->ehdr;
2085 	GElf_Ehdr *kehdr = &from->ehdr;
2086 
2087 	memcpy(ehdr->e_ident, kehdr->e_ident, EI_NIDENT);
2088 	ehdr->e_type      = kehdr->e_type;
2089 	ehdr->e_machine   = kehdr->e_machine;
2090 	ehdr->e_version   = kehdr->e_version;
2091 	ehdr->e_entry     = 0;
2092 	ehdr->e_shoff     = 0;
2093 	ehdr->e_flags     = kehdr->e_flags;
2094 	ehdr->e_phnum     = count;
2095 	ehdr->e_shentsize = 0;
2096 	ehdr->e_shnum     = 0;
2097 	ehdr->e_shstrndx  = 0;
2098 
2099 	if (from->elfclass == ELFCLASS32) {
2100 		ehdr->e_phoff     = sizeof(Elf32_Ehdr);
2101 		ehdr->e_ehsize    = sizeof(Elf32_Ehdr);
2102 		ehdr->e_phentsize = sizeof(Elf32_Phdr);
2103 	} else {
2104 		ehdr->e_phoff     = sizeof(Elf64_Ehdr);
2105 		ehdr->e_ehsize    = sizeof(Elf64_Ehdr);
2106 		ehdr->e_phentsize = sizeof(Elf64_Phdr);
2107 	}
2108 
2109 	if (!gelf_update_ehdr(to->elf, ehdr))
2110 		return -1;
2111 
2112 	if (!gelf_newphdr(to->elf, count))
2113 		return -1;
2114 
2115 	return 0;
2116 }
2117 
kcore__add_phdr(struct kcore * kcore,int idx,off_t offset,u64 addr,u64 len)2118 static int kcore__add_phdr(struct kcore *kcore, int idx, off_t offset,
2119 			   u64 addr, u64 len)
2120 {
2121 	GElf_Phdr phdr = {
2122 		.p_type		= PT_LOAD,
2123 		.p_flags	= PF_R | PF_W | PF_X,
2124 		.p_offset	= offset,
2125 		.p_vaddr	= addr,
2126 		.p_paddr	= 0,
2127 		.p_filesz	= len,
2128 		.p_memsz	= len,
2129 		.p_align	= page_size,
2130 	};
2131 
2132 	if (!gelf_update_phdr(kcore->elf, idx, &phdr))
2133 		return -1;
2134 
2135 	return 0;
2136 }
2137 
kcore__write(struct kcore * kcore)2138 static off_t kcore__write(struct kcore *kcore)
2139 {
2140 	return elf_update(kcore->elf, ELF_C_WRITE);
2141 }
2142 
2143 struct phdr_data {
2144 	off_t offset;
2145 	off_t rel;
2146 	u64 addr;
2147 	u64 len;
2148 	struct list_head node;
2149 	struct phdr_data *remaps;
2150 };
2151 
2152 struct sym_data {
2153 	u64 addr;
2154 	struct list_head node;
2155 };
2156 
2157 struct kcore_copy_info {
2158 	u64 stext;
2159 	u64 etext;
2160 	u64 first_symbol;
2161 	u64 last_symbol;
2162 	u64 first_module;
2163 	u64 first_module_symbol;
2164 	u64 last_module_symbol;
2165 	size_t phnum;
2166 	struct list_head phdrs;
2167 	struct list_head syms;
2168 };
2169 
2170 #define kcore_copy__for_each_phdr(k, p) \
2171 	list_for_each_entry((p), &(k)->phdrs, node)
2172 
phdr_data__new(u64 addr,u64 len,off_t offset)2173 static struct phdr_data *phdr_data__new(u64 addr, u64 len, off_t offset)
2174 {
2175 	struct phdr_data *p = zalloc(sizeof(*p));
2176 
2177 	if (p) {
2178 		p->addr   = addr;
2179 		p->len    = len;
2180 		p->offset = offset;
2181 	}
2182 
2183 	return p;
2184 }
2185 
kcore_copy_info__addnew(struct kcore_copy_info * kci,u64 addr,u64 len,off_t offset)2186 static struct phdr_data *kcore_copy_info__addnew(struct kcore_copy_info *kci,
2187 						 u64 addr, u64 len,
2188 						 off_t offset)
2189 {
2190 	struct phdr_data *p = phdr_data__new(addr, len, offset);
2191 
2192 	if (p)
2193 		list_add_tail(&p->node, &kci->phdrs);
2194 
2195 	return p;
2196 }
2197 
kcore_copy__free_phdrs(struct kcore_copy_info * kci)2198 static void kcore_copy__free_phdrs(struct kcore_copy_info *kci)
2199 {
2200 	struct phdr_data *p, *tmp;
2201 
2202 	list_for_each_entry_safe(p, tmp, &kci->phdrs, node) {
2203 		list_del_init(&p->node);
2204 		free(p);
2205 	}
2206 }
2207 
kcore_copy__new_sym(struct kcore_copy_info * kci,u64 addr)2208 static struct sym_data *kcore_copy__new_sym(struct kcore_copy_info *kci,
2209 					    u64 addr)
2210 {
2211 	struct sym_data *s = zalloc(sizeof(*s));
2212 
2213 	if (s) {
2214 		s->addr = addr;
2215 		list_add_tail(&s->node, &kci->syms);
2216 	}
2217 
2218 	return s;
2219 }
2220 
kcore_copy__free_syms(struct kcore_copy_info * kci)2221 static void kcore_copy__free_syms(struct kcore_copy_info *kci)
2222 {
2223 	struct sym_data *s, *tmp;
2224 
2225 	list_for_each_entry_safe(s, tmp, &kci->syms, node) {
2226 		list_del_init(&s->node);
2227 		free(s);
2228 	}
2229 }
2230 
kcore_copy__process_kallsyms(void * arg,const char * name,char type,u64 start)2231 static int kcore_copy__process_kallsyms(void *arg, const char *name, char type,
2232 					u64 start)
2233 {
2234 	struct kcore_copy_info *kci = arg;
2235 
2236 	if (!kallsyms__is_function(type))
2237 		return 0;
2238 
2239 	if (strchr(name, '[')) {
2240 		if (!kci->first_module_symbol || start < kci->first_module_symbol)
2241 			kci->first_module_symbol = start;
2242 		if (start > kci->last_module_symbol)
2243 			kci->last_module_symbol = start;
2244 		return 0;
2245 	}
2246 
2247 	if (!kci->first_symbol || start < kci->first_symbol)
2248 		kci->first_symbol = start;
2249 
2250 	if (!kci->last_symbol || start > kci->last_symbol)
2251 		kci->last_symbol = start;
2252 
2253 	if (!strcmp(name, "_stext")) {
2254 		kci->stext = start;
2255 		return 0;
2256 	}
2257 
2258 	if (!strcmp(name, "_etext")) {
2259 		kci->etext = start;
2260 		return 0;
2261 	}
2262 
2263 	if (is_entry_trampoline(name) && !kcore_copy__new_sym(kci, start))
2264 		return -1;
2265 
2266 	return 0;
2267 }
2268 
kcore_copy__parse_kallsyms(struct kcore_copy_info * kci,const char * dir)2269 static int kcore_copy__parse_kallsyms(struct kcore_copy_info *kci,
2270 				      const char *dir)
2271 {
2272 	char kallsyms_filename[PATH_MAX];
2273 
2274 	scnprintf(kallsyms_filename, PATH_MAX, "%s/kallsyms", dir);
2275 
2276 	if (symbol__restricted_filename(kallsyms_filename, "/proc/kallsyms"))
2277 		return -1;
2278 
2279 	if (kallsyms__parse(kallsyms_filename, kci,
2280 			    kcore_copy__process_kallsyms) < 0)
2281 		return -1;
2282 
2283 	return 0;
2284 }
2285 
kcore_copy__process_modules(void * arg,const char * name __maybe_unused,u64 start,u64 size __maybe_unused)2286 static int kcore_copy__process_modules(void *arg,
2287 				       const char *name __maybe_unused,
2288 				       u64 start, u64 size __maybe_unused)
2289 {
2290 	struct kcore_copy_info *kci = arg;
2291 
2292 	if (!kci->first_module || start < kci->first_module)
2293 		kci->first_module = start;
2294 
2295 	return 0;
2296 }
2297 
kcore_copy__parse_modules(struct kcore_copy_info * kci,const char * dir)2298 static int kcore_copy__parse_modules(struct kcore_copy_info *kci,
2299 				     const char *dir)
2300 {
2301 	char modules_filename[PATH_MAX];
2302 
2303 	scnprintf(modules_filename, PATH_MAX, "%s/modules", dir);
2304 
2305 	if (symbol__restricted_filename(modules_filename, "/proc/modules"))
2306 		return -1;
2307 
2308 	if (modules__parse(modules_filename, kci,
2309 			   kcore_copy__process_modules) < 0)
2310 		return -1;
2311 
2312 	return 0;
2313 }
2314 
kcore_copy__map(struct kcore_copy_info * kci,u64 start,u64 end,u64 pgoff,u64 s,u64 e)2315 static int kcore_copy__map(struct kcore_copy_info *kci, u64 start, u64 end,
2316 			   u64 pgoff, u64 s, u64 e)
2317 {
2318 	u64 len, offset;
2319 
2320 	if (s < start || s >= end)
2321 		return 0;
2322 
2323 	offset = (s - start) + pgoff;
2324 	len = e < end ? e - s : end - s;
2325 
2326 	return kcore_copy_info__addnew(kci, s, len, offset) ? 0 : -1;
2327 }
2328 
kcore_copy__read_map(u64 start,u64 len,u64 pgoff,void * data)2329 static int kcore_copy__read_map(u64 start, u64 len, u64 pgoff, void *data)
2330 {
2331 	struct kcore_copy_info *kci = data;
2332 	u64 end = start + len;
2333 	struct sym_data *sdat;
2334 
2335 	if (kcore_copy__map(kci, start, end, pgoff, kci->stext, kci->etext))
2336 		return -1;
2337 
2338 	if (kcore_copy__map(kci, start, end, pgoff, kci->first_module,
2339 			    kci->last_module_symbol))
2340 		return -1;
2341 
2342 	list_for_each_entry(sdat, &kci->syms, node) {
2343 		u64 s = round_down(sdat->addr, page_size);
2344 
2345 		if (kcore_copy__map(kci, start, end, pgoff, s, s + len))
2346 			return -1;
2347 	}
2348 
2349 	return 0;
2350 }
2351 
kcore_copy__read_maps(struct kcore_copy_info * kci,Elf * elf)2352 static int kcore_copy__read_maps(struct kcore_copy_info *kci, Elf *elf)
2353 {
2354 	if (elf_read_maps(elf, true, kcore_copy__read_map, kci) < 0)
2355 		return -1;
2356 
2357 	return 0;
2358 }
2359 
kcore_copy__find_remaps(struct kcore_copy_info * kci)2360 static void kcore_copy__find_remaps(struct kcore_copy_info *kci)
2361 {
2362 	struct phdr_data *p, *k = NULL;
2363 	u64 kend;
2364 
2365 	if (!kci->stext)
2366 		return;
2367 
2368 	/* Find phdr that corresponds to the kernel map (contains stext) */
2369 	kcore_copy__for_each_phdr(kci, p) {
2370 		u64 pend = p->addr + p->len - 1;
2371 
2372 		if (p->addr <= kci->stext && pend >= kci->stext) {
2373 			k = p;
2374 			break;
2375 		}
2376 	}
2377 
2378 	if (!k)
2379 		return;
2380 
2381 	kend = k->offset + k->len;
2382 
2383 	/* Find phdrs that remap the kernel */
2384 	kcore_copy__for_each_phdr(kci, p) {
2385 		u64 pend = p->offset + p->len;
2386 
2387 		if (p == k)
2388 			continue;
2389 
2390 		if (p->offset >= k->offset && pend <= kend)
2391 			p->remaps = k;
2392 	}
2393 }
2394 
kcore_copy__layout(struct kcore_copy_info * kci)2395 static void kcore_copy__layout(struct kcore_copy_info *kci)
2396 {
2397 	struct phdr_data *p;
2398 	off_t rel = 0;
2399 
2400 	kcore_copy__find_remaps(kci);
2401 
2402 	kcore_copy__for_each_phdr(kci, p) {
2403 		if (!p->remaps) {
2404 			p->rel = rel;
2405 			rel += p->len;
2406 		}
2407 		kci->phnum += 1;
2408 	}
2409 
2410 	kcore_copy__for_each_phdr(kci, p) {
2411 		struct phdr_data *k = p->remaps;
2412 
2413 		if (k)
2414 			p->rel = p->offset - k->offset + k->rel;
2415 	}
2416 }
2417 
kcore_copy__calc_maps(struct kcore_copy_info * kci,const char * dir,Elf * elf)2418 static int kcore_copy__calc_maps(struct kcore_copy_info *kci, const char *dir,
2419 				 Elf *elf)
2420 {
2421 	if (kcore_copy__parse_kallsyms(kci, dir))
2422 		return -1;
2423 
2424 	if (kcore_copy__parse_modules(kci, dir))
2425 		return -1;
2426 
2427 	if (kci->stext)
2428 		kci->stext = round_down(kci->stext, page_size);
2429 	else
2430 		kci->stext = round_down(kci->first_symbol, page_size);
2431 
2432 	if (kci->etext) {
2433 		kci->etext = round_up(kci->etext, page_size);
2434 	} else if (kci->last_symbol) {
2435 		kci->etext = round_up(kci->last_symbol, page_size);
2436 		kci->etext += page_size;
2437 	}
2438 
2439 	if (kci->first_module_symbol &&
2440 	    (!kci->first_module || kci->first_module_symbol < kci->first_module))
2441 		kci->first_module = kci->first_module_symbol;
2442 
2443 	kci->first_module = round_down(kci->first_module, page_size);
2444 
2445 	if (kci->last_module_symbol) {
2446 		kci->last_module_symbol = round_up(kci->last_module_symbol,
2447 						   page_size);
2448 		kci->last_module_symbol += page_size;
2449 	}
2450 
2451 	if (!kci->stext || !kci->etext)
2452 		return -1;
2453 
2454 	if (kci->first_module && !kci->last_module_symbol)
2455 		return -1;
2456 
2457 	if (kcore_copy__read_maps(kci, elf))
2458 		return -1;
2459 
2460 	kcore_copy__layout(kci);
2461 
2462 	return 0;
2463 }
2464 
kcore_copy__copy_file(const char * from_dir,const char * to_dir,const char * name)2465 static int kcore_copy__copy_file(const char *from_dir, const char *to_dir,
2466 				 const char *name)
2467 {
2468 	char from_filename[PATH_MAX];
2469 	char to_filename[PATH_MAX];
2470 
2471 	scnprintf(from_filename, PATH_MAX, "%s/%s", from_dir, name);
2472 	scnprintf(to_filename, PATH_MAX, "%s/%s", to_dir, name);
2473 
2474 	return copyfile_mode(from_filename, to_filename, 0400);
2475 }
2476 
kcore_copy__unlink(const char * dir,const char * name)2477 static int kcore_copy__unlink(const char *dir, const char *name)
2478 {
2479 	char filename[PATH_MAX];
2480 
2481 	scnprintf(filename, PATH_MAX, "%s/%s", dir, name);
2482 
2483 	return unlink(filename);
2484 }
2485 
kcore_copy__compare_fds(int from,int to)2486 static int kcore_copy__compare_fds(int from, int to)
2487 {
2488 	char *buf_from;
2489 	char *buf_to;
2490 	ssize_t ret;
2491 	size_t len;
2492 	int err = -1;
2493 
2494 	buf_from = malloc(page_size);
2495 	buf_to = malloc(page_size);
2496 	if (!buf_from || !buf_to)
2497 		goto out;
2498 
2499 	while (1) {
2500 		/* Use read because mmap won't work on proc files */
2501 		ret = read(from, buf_from, page_size);
2502 		if (ret < 0)
2503 			goto out;
2504 
2505 		if (!ret)
2506 			break;
2507 
2508 		len = ret;
2509 
2510 		if (readn(to, buf_to, len) != (int)len)
2511 			goto out;
2512 
2513 		if (memcmp(buf_from, buf_to, len))
2514 			goto out;
2515 	}
2516 
2517 	err = 0;
2518 out:
2519 	free(buf_to);
2520 	free(buf_from);
2521 	return err;
2522 }
2523 
kcore_copy__compare_files(const char * from_filename,const char * to_filename)2524 static int kcore_copy__compare_files(const char *from_filename,
2525 				     const char *to_filename)
2526 {
2527 	int from, to, err = -1;
2528 
2529 	from = open(from_filename, O_RDONLY);
2530 	if (from < 0)
2531 		return -1;
2532 
2533 	to = open(to_filename, O_RDONLY);
2534 	if (to < 0)
2535 		goto out_close_from;
2536 
2537 	err = kcore_copy__compare_fds(from, to);
2538 
2539 	close(to);
2540 out_close_from:
2541 	close(from);
2542 	return err;
2543 }
2544 
kcore_copy__compare_file(const char * from_dir,const char * to_dir,const char * name)2545 static int kcore_copy__compare_file(const char *from_dir, const char *to_dir,
2546 				    const char *name)
2547 {
2548 	char from_filename[PATH_MAX];
2549 	char to_filename[PATH_MAX];
2550 
2551 	scnprintf(from_filename, PATH_MAX, "%s/%s", from_dir, name);
2552 	scnprintf(to_filename, PATH_MAX, "%s/%s", to_dir, name);
2553 
2554 	return kcore_copy__compare_files(from_filename, to_filename);
2555 }
2556 
2557 /**
2558  * kcore_copy - copy kallsyms, modules and kcore from one directory to another.
2559  * @from_dir: from directory
2560  * @to_dir: to directory
2561  *
2562  * This function copies kallsyms, modules and kcore files from one directory to
2563  * another.  kallsyms and modules are copied entirely.  Only code segments are
2564  * copied from kcore.  It is assumed that two segments suffice: one for the
2565  * kernel proper and one for all the modules.  The code segments are determined
2566  * from kallsyms and modules files.  The kernel map starts at _stext or the
2567  * lowest function symbol, and ends at _etext or the highest function symbol.
2568  * The module map starts at the lowest module address and ends at the highest
2569  * module symbol.  Start addresses are rounded down to the nearest page.  End
2570  * addresses are rounded up to the nearest page.  An extra page is added to the
2571  * highest kernel symbol and highest module symbol to, hopefully, encompass that
2572  * symbol too.  Because it contains only code sections, the resulting kcore is
2573  * unusual.  One significant peculiarity is that the mapping (start -> pgoff)
2574  * is not the same for the kernel map and the modules map.  That happens because
2575  * the data is copied adjacently whereas the original kcore has gaps.  Finally,
2576  * kallsyms file is compared with its copy to check that modules have not been
2577  * loaded or unloaded while the copies were taking place.
2578  *
2579  * Return: %0 on success, %-1 on failure.
2580  */
kcore_copy(const char * from_dir,const char * to_dir)2581 int kcore_copy(const char *from_dir, const char *to_dir)
2582 {
2583 	struct kcore kcore;
2584 	struct kcore extract;
2585 	int idx = 0, err = -1;
2586 	off_t offset, sz;
2587 	struct kcore_copy_info kci = { .stext = 0, };
2588 	char kcore_filename[PATH_MAX];
2589 	char extract_filename[PATH_MAX];
2590 	struct phdr_data *p;
2591 
2592 	INIT_LIST_HEAD(&kci.phdrs);
2593 	INIT_LIST_HEAD(&kci.syms);
2594 
2595 	if (kcore_copy__copy_file(from_dir, to_dir, "kallsyms"))
2596 		return -1;
2597 
2598 	if (kcore_copy__copy_file(from_dir, to_dir, "modules"))
2599 		goto out_unlink_kallsyms;
2600 
2601 	scnprintf(kcore_filename, PATH_MAX, "%s/kcore", from_dir);
2602 	scnprintf(extract_filename, PATH_MAX, "%s/kcore", to_dir);
2603 
2604 	if (kcore__open(&kcore, kcore_filename))
2605 		goto out_unlink_modules;
2606 
2607 	if (kcore_copy__calc_maps(&kci, from_dir, kcore.elf))
2608 		goto out_kcore_close;
2609 
2610 	if (kcore__init(&extract, extract_filename, kcore.elfclass, false))
2611 		goto out_kcore_close;
2612 
2613 	if (kcore__copy_hdr(&kcore, &extract, kci.phnum))
2614 		goto out_extract_close;
2615 
2616 	offset = gelf_fsize(extract.elf, ELF_T_EHDR, 1, EV_CURRENT) +
2617 		 gelf_fsize(extract.elf, ELF_T_PHDR, kci.phnum, EV_CURRENT);
2618 	offset = round_up(offset, page_size);
2619 
2620 	kcore_copy__for_each_phdr(&kci, p) {
2621 		off_t offs = p->rel + offset;
2622 
2623 		if (kcore__add_phdr(&extract, idx++, offs, p->addr, p->len))
2624 			goto out_extract_close;
2625 	}
2626 
2627 	sz = kcore__write(&extract);
2628 	if (sz < 0 || sz > offset)
2629 		goto out_extract_close;
2630 
2631 	kcore_copy__for_each_phdr(&kci, p) {
2632 		off_t offs = p->rel + offset;
2633 
2634 		if (p->remaps)
2635 			continue;
2636 		if (copy_bytes(kcore.fd, p->offset, extract.fd, offs, p->len))
2637 			goto out_extract_close;
2638 	}
2639 
2640 	if (kcore_copy__compare_file(from_dir, to_dir, "kallsyms"))
2641 		goto out_extract_close;
2642 
2643 	err = 0;
2644 
2645 out_extract_close:
2646 	kcore__close(&extract);
2647 	if (err)
2648 		unlink(extract_filename);
2649 out_kcore_close:
2650 	kcore__close(&kcore);
2651 out_unlink_modules:
2652 	if (err)
2653 		kcore_copy__unlink(to_dir, "modules");
2654 out_unlink_kallsyms:
2655 	if (err)
2656 		kcore_copy__unlink(to_dir, "kallsyms");
2657 
2658 	kcore_copy__free_phdrs(&kci);
2659 	kcore_copy__free_syms(&kci);
2660 
2661 	return err;
2662 }
2663 
kcore_extract__create(struct kcore_extract * kce)2664 int kcore_extract__create(struct kcore_extract *kce)
2665 {
2666 	struct kcore kcore;
2667 	struct kcore extract;
2668 	size_t count = 1;
2669 	int idx = 0, err = -1;
2670 	off_t offset = page_size, sz;
2671 
2672 	if (kcore__open(&kcore, kce->kcore_filename))
2673 		return -1;
2674 
2675 	strcpy(kce->extract_filename, PERF_KCORE_EXTRACT);
2676 	if (kcore__init(&extract, kce->extract_filename, kcore.elfclass, true))
2677 		goto out_kcore_close;
2678 
2679 	if (kcore__copy_hdr(&kcore, &extract, count))
2680 		goto out_extract_close;
2681 
2682 	if (kcore__add_phdr(&extract, idx, offset, kce->addr, kce->len))
2683 		goto out_extract_close;
2684 
2685 	sz = kcore__write(&extract);
2686 	if (sz < 0 || sz > offset)
2687 		goto out_extract_close;
2688 
2689 	if (copy_bytes(kcore.fd, kce->offs, extract.fd, offset, kce->len))
2690 		goto out_extract_close;
2691 
2692 	err = 0;
2693 
2694 out_extract_close:
2695 	kcore__close(&extract);
2696 	if (err)
2697 		unlink(kce->extract_filename);
2698 out_kcore_close:
2699 	kcore__close(&kcore);
2700 
2701 	return err;
2702 }
2703 
kcore_extract__delete(struct kcore_extract * kce)2704 void kcore_extract__delete(struct kcore_extract *kce)
2705 {
2706 	unlink(kce->extract_filename);
2707 }
2708 
2709 #ifdef HAVE_GELF_GETNOTE_SUPPORT
2710 
sdt_adjust_loc(struct sdt_note * tmp,GElf_Addr base_off)2711 static void sdt_adjust_loc(struct sdt_note *tmp, GElf_Addr base_off)
2712 {
2713 	if (!base_off)
2714 		return;
2715 
2716 	if (tmp->bit32)
2717 		tmp->addr.a32[SDT_NOTE_IDX_LOC] =
2718 			tmp->addr.a32[SDT_NOTE_IDX_LOC] + base_off -
2719 			tmp->addr.a32[SDT_NOTE_IDX_BASE];
2720 	else
2721 		tmp->addr.a64[SDT_NOTE_IDX_LOC] =
2722 			tmp->addr.a64[SDT_NOTE_IDX_LOC] + base_off -
2723 			tmp->addr.a64[SDT_NOTE_IDX_BASE];
2724 }
2725 
sdt_adjust_refctr(struct sdt_note * tmp,GElf_Addr base_addr,GElf_Addr base_off)2726 static void sdt_adjust_refctr(struct sdt_note *tmp, GElf_Addr base_addr,
2727 			      GElf_Addr base_off)
2728 {
2729 	if (!base_off)
2730 		return;
2731 
2732 	if (tmp->bit32 && tmp->addr.a32[SDT_NOTE_IDX_REFCTR])
2733 		tmp->addr.a32[SDT_NOTE_IDX_REFCTR] -= (base_addr - base_off);
2734 	else if (tmp->addr.a64[SDT_NOTE_IDX_REFCTR])
2735 		tmp->addr.a64[SDT_NOTE_IDX_REFCTR] -= (base_addr - base_off);
2736 }
2737 
2738 /**
2739  * populate_sdt_note : Parse raw data and identify SDT note
2740  * @elf: elf of the opened file
2741  * @data: raw data of a section with description offset applied
2742  * @len: note description size
2743  * @type: type of the note
2744  * @sdt_notes: List to add the SDT note
2745  *
2746  * Responsible for parsing the @data in section .note.stapsdt in @elf and
2747  * if its an SDT note, it appends to @sdt_notes list.
2748  */
populate_sdt_note(Elf ** elf,const char * data,size_t len,struct list_head * sdt_notes)2749 static int populate_sdt_note(Elf **elf, const char *data, size_t len,
2750 			     struct list_head *sdt_notes)
2751 {
2752 	const char *provider, *name, *args;
2753 	struct sdt_note *tmp = NULL;
2754 	GElf_Ehdr ehdr;
2755 	GElf_Shdr shdr;
2756 	int ret = -EINVAL;
2757 
2758 	union {
2759 		Elf64_Addr a64[NR_ADDR];
2760 		Elf32_Addr a32[NR_ADDR];
2761 	} buf;
2762 
2763 	Elf_Data dst = {
2764 		.d_buf = &buf, .d_type = ELF_T_ADDR, .d_version = EV_CURRENT,
2765 		.d_size = gelf_fsize((*elf), ELF_T_ADDR, NR_ADDR, EV_CURRENT),
2766 		.d_off = 0, .d_align = 0
2767 	};
2768 	Elf_Data src = {
2769 		.d_buf = (void *) data, .d_type = ELF_T_ADDR,
2770 		.d_version = EV_CURRENT, .d_size = dst.d_size, .d_off = 0,
2771 		.d_align = 0
2772 	};
2773 
2774 	tmp = (struct sdt_note *)calloc(1, sizeof(struct sdt_note));
2775 	if (!tmp) {
2776 		ret = -ENOMEM;
2777 		goto out_err;
2778 	}
2779 
2780 	INIT_LIST_HEAD(&tmp->note_list);
2781 
2782 	if (len < dst.d_size + 3)
2783 		goto out_free_note;
2784 
2785 	/* Translation from file representation to memory representation */
2786 	if (gelf_xlatetom(*elf, &dst, &src,
2787 			  elf_getident(*elf, NULL)[EI_DATA]) == NULL) {
2788 		pr_err("gelf_xlatetom : %s\n", elf_errmsg(-1));
2789 		goto out_free_note;
2790 	}
2791 
2792 	/* Populate the fields of sdt_note */
2793 	provider = data + dst.d_size;
2794 
2795 	name = (const char *)memchr(provider, '\0', data + len - provider);
2796 	if (name++ == NULL)
2797 		goto out_free_note;
2798 
2799 	tmp->provider = strdup(provider);
2800 	if (!tmp->provider) {
2801 		ret = -ENOMEM;
2802 		goto out_free_note;
2803 	}
2804 	tmp->name = strdup(name);
2805 	if (!tmp->name) {
2806 		ret = -ENOMEM;
2807 		goto out_free_prov;
2808 	}
2809 
2810 	args = memchr(name, '\0', data + len - name);
2811 
2812 	/*
2813 	 * There is no argument if:
2814 	 * - We reached the end of the note;
2815 	 * - There is not enough room to hold a potential string;
2816 	 * - The argument string is empty or just contains ':'.
2817 	 */
2818 	if (args == NULL || data + len - args < 2 ||
2819 		args[1] == ':' || args[1] == '\0')
2820 		tmp->args = NULL;
2821 	else {
2822 		tmp->args = strdup(++args);
2823 		if (!tmp->args) {
2824 			ret = -ENOMEM;
2825 			goto out_free_name;
2826 		}
2827 	}
2828 
2829 	if (gelf_getclass(*elf) == ELFCLASS32) {
2830 		memcpy(&tmp->addr, &buf, 3 * sizeof(Elf32_Addr));
2831 		tmp->bit32 = true;
2832 	} else {
2833 		memcpy(&tmp->addr, &buf, 3 * sizeof(Elf64_Addr));
2834 		tmp->bit32 = false;
2835 	}
2836 
2837 	if (!gelf_getehdr(*elf, &ehdr)) {
2838 		pr_debug("%s : cannot get elf header.\n", __func__);
2839 		ret = -EBADF;
2840 		goto out_free_args;
2841 	}
2842 
2843 	/* Adjust the prelink effect :
2844 	 * Find out the .stapsdt.base section.
2845 	 * This scn will help us to handle prelinking (if present).
2846 	 * Compare the retrieved file offset of the base section with the
2847 	 * base address in the description of the SDT note. If its different,
2848 	 * then accordingly, adjust the note location.
2849 	 */
2850 	if (elf_section_by_name(*elf, &ehdr, &shdr, SDT_BASE_SCN, NULL))
2851 		sdt_adjust_loc(tmp, shdr.sh_offset);
2852 
2853 	/* Adjust reference counter offset */
2854 	if (elf_section_by_name(*elf, &ehdr, &shdr, SDT_PROBES_SCN, NULL))
2855 		sdt_adjust_refctr(tmp, shdr.sh_addr, shdr.sh_offset);
2856 
2857 	list_add_tail(&tmp->note_list, sdt_notes);
2858 	return 0;
2859 
2860 out_free_args:
2861 	zfree(&tmp->args);
2862 out_free_name:
2863 	zfree(&tmp->name);
2864 out_free_prov:
2865 	zfree(&tmp->provider);
2866 out_free_note:
2867 	free(tmp);
2868 out_err:
2869 	return ret;
2870 }
2871 
2872 /**
2873  * construct_sdt_notes_list : constructs a list of SDT notes
2874  * @elf : elf to look into
2875  * @sdt_notes : empty list_head
2876  *
2877  * Scans the sections in 'elf' for the section
2878  * .note.stapsdt. It, then calls populate_sdt_note to find
2879  * out the SDT events and populates the 'sdt_notes'.
2880  */
construct_sdt_notes_list(Elf * elf,struct list_head * sdt_notes)2881 static int construct_sdt_notes_list(Elf *elf, struct list_head *sdt_notes)
2882 {
2883 	GElf_Ehdr ehdr;
2884 	Elf_Scn *scn = NULL;
2885 	Elf_Data *data;
2886 	GElf_Shdr shdr;
2887 	size_t shstrndx, next;
2888 	GElf_Nhdr nhdr;
2889 	size_t name_off, desc_off, offset;
2890 	int ret = 0;
2891 
2892 	if (gelf_getehdr(elf, &ehdr) == NULL) {
2893 		ret = -EBADF;
2894 		goto out_ret;
2895 	}
2896 	if (elf_getshdrstrndx(elf, &shstrndx) != 0) {
2897 		ret = -EBADF;
2898 		goto out_ret;
2899 	}
2900 
2901 	/* Look for the required section */
2902 	scn = elf_section_by_name(elf, &ehdr, &shdr, SDT_NOTE_SCN, NULL);
2903 	if (!scn) {
2904 		ret = -ENOENT;
2905 		goto out_ret;
2906 	}
2907 
2908 	if ((shdr.sh_type != SHT_NOTE) || (shdr.sh_flags & SHF_ALLOC)) {
2909 		ret = -ENOENT;
2910 		goto out_ret;
2911 	}
2912 
2913 	data = elf_getdata(scn, NULL);
2914 
2915 	/* Get the SDT notes */
2916 	for (offset = 0; (next = gelf_getnote(data, offset, &nhdr, &name_off,
2917 					      &desc_off)) > 0; offset = next) {
2918 		if (nhdr.n_namesz == sizeof(SDT_NOTE_NAME) &&
2919 		    !memcmp(data->d_buf + name_off, SDT_NOTE_NAME,
2920 			    sizeof(SDT_NOTE_NAME))) {
2921 			/* Check the type of the note */
2922 			if (nhdr.n_type != SDT_NOTE_TYPE)
2923 				goto out_ret;
2924 
2925 			ret = populate_sdt_note(&elf, ((data->d_buf) + desc_off),
2926 						nhdr.n_descsz, sdt_notes);
2927 			if (ret < 0)
2928 				goto out_ret;
2929 		}
2930 	}
2931 	if (list_empty(sdt_notes))
2932 		ret = -ENOENT;
2933 
2934 out_ret:
2935 	return ret;
2936 }
2937 
2938 /**
2939  * get_sdt_note_list : Wrapper to construct a list of sdt notes
2940  * @head : empty list_head
2941  * @target : file to find SDT notes from
2942  *
2943  * This opens the file, initializes
2944  * the ELF and then calls construct_sdt_notes_list.
2945  */
get_sdt_note_list(struct list_head * head,const char * target)2946 int get_sdt_note_list(struct list_head *head, const char *target)
2947 {
2948 	Elf *elf;
2949 	int fd, ret;
2950 
2951 	fd = open(target, O_RDONLY);
2952 	if (fd < 0)
2953 		return -EBADF;
2954 
2955 	elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
2956 	if (!elf) {
2957 		ret = -EBADF;
2958 		goto out_close;
2959 	}
2960 	ret = construct_sdt_notes_list(elf, head);
2961 	elf_end(elf);
2962 out_close:
2963 	close(fd);
2964 	return ret;
2965 }
2966 
2967 /**
2968  * cleanup_sdt_note_list : free the sdt notes' list
2969  * @sdt_notes: sdt notes' list
2970  *
2971  * Free up the SDT notes in @sdt_notes.
2972  * Returns the number of SDT notes free'd.
2973  */
cleanup_sdt_note_list(struct list_head * sdt_notes)2974 int cleanup_sdt_note_list(struct list_head *sdt_notes)
2975 {
2976 	struct sdt_note *tmp, *pos;
2977 	int nr_free = 0;
2978 
2979 	list_for_each_entry_safe(pos, tmp, sdt_notes, note_list) {
2980 		list_del_init(&pos->note_list);
2981 		zfree(&pos->args);
2982 		zfree(&pos->name);
2983 		zfree(&pos->provider);
2984 		free(pos);
2985 		nr_free++;
2986 	}
2987 	return nr_free;
2988 }
2989 
2990 /**
2991  * sdt_notes__get_count: Counts the number of sdt events
2992  * @start: list_head to sdt_notes list
2993  *
2994  * Returns the number of SDT notes in a list
2995  */
sdt_notes__get_count(struct list_head * start)2996 int sdt_notes__get_count(struct list_head *start)
2997 {
2998 	struct sdt_note *sdt_ptr;
2999 	int count = 0;
3000 
3001 	list_for_each_entry(sdt_ptr, start, note_list)
3002 		count++;
3003 	return count;
3004 }
3005 #endif
3006 
symbol__elf_init(void)3007 void symbol__elf_init(void)
3008 {
3009 	elf_version(EV_CURRENT);
3010 }
3011