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