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