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