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