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