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