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