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