blob: 848d1a0f8dc09562c6c982ef1390649160862c3a [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Namhyung Kime5a18452012-08-06 13:41:20 +09002#include <fcntl.h>
3#include <stdio.h>
4#include <errno.h>
5#include <string.h>
6#include <unistd.h>
7#include <inttypes.h>
8
Arnaldo Carvalho de Melo1101f692019-01-27 13:42:37 +01009#include "map.h"
Namhyung Kime5a18452012-08-06 13:41:20 +090010#include "symbol.h"
Stephane Eraniane9c4bcd2015-11-30 10:02:20 +010011#include "demangle-java.h"
David Tolnaycae15db2016-07-09 00:20:00 -070012#include "demangle-rust.h"
Waiman Long8fa7d872014-09-29 16:07:28 -040013#include "machine.h"
Vladimir Nikulichev922d0e42014-04-17 08:27:01 -070014#include "vdso.h"
Namhyung Kime5a18452012-08-06 13:41:20 +090015#include "debug.h"
Arnaldo Carvalho de Melo3d689ed2017-04-17 16:10:49 -030016#include "sane_ctype.h"
17#include <symbol/kallsyms.h>
Namhyung Kime5a18452012-08-06 13:41:20 +090018
David Aherne370a3d2015-02-18 19:33:37 -050019#ifndef EM_AARCH64
20#define EM_AARCH64 183 /* ARM 64 bit */
21#endif
22
Arnaldo Carvalho de Melocc310782016-07-12 11:04:13 -030023typedef Elf64_Nhdr GElf_Nhdr;
David Aherne370a3d2015-02-18 19:33:37 -050024
Arnaldo Carvalho de Meloaaba4e12014-11-24 17:10:52 -030025#ifdef HAVE_CPLUS_DEMANGLE_SUPPORT
26extern char *cplus_demangle(const char *, int);
27
28static inline char *bfd_demangle(void __maybe_unused *v, const char *c, int i)
29{
30 return cplus_demangle(c, i);
31}
32#else
33#ifdef NO_DEMANGLE
34static inline char *bfd_demangle(void __maybe_unused *v,
35 const char __maybe_unused *c,
36 int __maybe_unused i)
37{
38 return NULL;
39}
40#else
41#define PACKAGE 'perf'
42#include <bfd.h>
43#endif
44#endif
45
Ingo Molnar89fe8082013-09-30 12:07:11 +020046#ifndef HAVE_ELF_GETPHDRNUM_SUPPORT
Arnaldo Carvalho de Melo179f36d2015-09-17 11:30:20 -030047static int elf_getphdrnum(Elf *elf, size_t *dst)
Adrian Huntere955d5c2013-09-13 16:49:30 +030048{
49 GElf_Ehdr gehdr;
50 GElf_Ehdr *ehdr;
51
52 ehdr = gelf_getehdr(elf, &gehdr);
53 if (!ehdr)
54 return -1;
55
56 *dst = ehdr->e_phnum;
57
58 return 0;
59}
60#endif
61
Arnaldo Carvalho de Melo2492c462016-07-04 19:35:47 -030062#ifndef HAVE_ELF_GETSHDRSTRNDX_SUPPORT
63static int elf_getshdrstrndx(Elf *elf __maybe_unused, size_t *dst __maybe_unused)
64{
65 pr_err("%s: update your libelf to > 0.140, this one lacks elf_getshdrstrndx().\n", __func__);
66 return -1;
67}
68#endif
69
Namhyung Kime5a18452012-08-06 13:41:20 +090070#ifndef NT_GNU_BUILD_ID
71#define NT_GNU_BUILD_ID 3
72#endif
73
74/**
75 * elf_symtab__for_each_symbol - iterate thru all the symbols
76 *
77 * @syms: struct elf_symtab instance to iterate
78 * @idx: uint32_t idx
79 * @sym: GElf_Sym iterator
80 */
81#define elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) \
82 for (idx = 0, gelf_getsym(syms, idx, &sym);\
83 idx < nr_syms; \
84 idx++, gelf_getsym(syms, idx, &sym))
85
86static inline uint8_t elf_sym__type(const GElf_Sym *sym)
87{
88 return GELF_ST_TYPE(sym->st_info);
89}
90
Vinson Lee4e310502015-02-09 16:29:37 -080091#ifndef STT_GNU_IFUNC
92#define STT_GNU_IFUNC 10
93#endif
94
Namhyung Kime5a18452012-08-06 13:41:20 +090095static inline int elf_sym__is_function(const GElf_Sym *sym)
96{
Adrian Huntera2f3b6b2014-07-14 13:02:33 +030097 return (elf_sym__type(sym) == STT_FUNC ||
98 elf_sym__type(sym) == STT_GNU_IFUNC) &&
Namhyung Kime5a18452012-08-06 13:41:20 +090099 sym->st_name != 0 &&
100 sym->st_shndx != SHN_UNDEF;
101}
102
103static inline bool elf_sym__is_object(const GElf_Sym *sym)
104{
105 return elf_sym__type(sym) == STT_OBJECT &&
106 sym->st_name != 0 &&
107 sym->st_shndx != SHN_UNDEF;
108}
109
110static inline int elf_sym__is_label(const GElf_Sym *sym)
111{
112 return elf_sym__type(sym) == STT_NOTYPE &&
113 sym->st_name != 0 &&
114 sym->st_shndx != SHN_UNDEF &&
115 sym->st_shndx != SHN_ABS;
116}
117
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -0300118static bool elf_sym__filter(GElf_Sym *sym)
Namhyung Kime5a18452012-08-06 13:41:20 +0900119{
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -0300120 return elf_sym__is_function(sym) || elf_sym__is_object(sym);
Namhyung Kime5a18452012-08-06 13:41:20 +0900121}
122
123static inline const char *elf_sym__name(const GElf_Sym *sym,
124 const Elf_Data *symstrs)
125{
126 return symstrs->d_buf + sym->st_name;
127}
128
129static inline const char *elf_sec__name(const GElf_Shdr *shdr,
130 const Elf_Data *secstrs)
131{
132 return secstrs->d_buf + shdr->sh_name;
133}
134
135static inline int elf_sec__is_text(const GElf_Shdr *shdr,
136 const Elf_Data *secstrs)
137{
138 return strstr(elf_sec__name(shdr, secstrs), "text") != NULL;
139}
140
141static inline bool elf_sec__is_data(const GElf_Shdr *shdr,
142 const Elf_Data *secstrs)
143{
144 return strstr(elf_sec__name(shdr, secstrs), "data") != NULL;
145}
146
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -0300147static bool elf_sec__filter(GElf_Shdr *shdr, Elf_Data *secstrs)
Namhyung Kime5a18452012-08-06 13:41:20 +0900148{
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -0300149 return elf_sec__is_text(shdr, secstrs) ||
150 elf_sec__is_data(shdr, secstrs);
Namhyung Kime5a18452012-08-06 13:41:20 +0900151}
152
153static size_t elf_addr_to_index(Elf *elf, GElf_Addr addr)
154{
155 Elf_Scn *sec = NULL;
156 GElf_Shdr shdr;
157 size_t cnt = 1;
158
159 while ((sec = elf_nextscn(elf, sec)) != NULL) {
160 gelf_getshdr(sec, &shdr);
161
162 if ((addr >= shdr.sh_addr) &&
163 (addr < (shdr.sh_addr + shdr.sh_size)))
164 return cnt;
165
166 ++cnt;
167 }
168
169 return -1;
170}
171
Masami Hiramatsu99ca4232014-01-16 09:39:49 +0000172Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep,
173 GElf_Shdr *shp, const char *name, size_t *idx)
Namhyung Kime5a18452012-08-06 13:41:20 +0900174{
175 Elf_Scn *sec = NULL;
176 size_t cnt = 1;
177
Cody P Schafer49274652012-08-10 15:22:55 -0700178 /* Elf is corrupted/truncated, avoid calling elf_strptr. */
179 if (!elf_rawdata(elf_getscn(elf, ep->e_shstrndx), NULL))
180 return NULL;
181
Namhyung Kime5a18452012-08-06 13:41:20 +0900182 while ((sec = elf_nextscn(elf, sec)) != NULL) {
183 char *str;
184
185 gelf_getshdr(sec, shp);
186 str = elf_strptr(elf, ep->e_shstrndx, shp->sh_name);
Jiri Olsa155b3a12014-03-02 14:32:07 +0100187 if (str && !strcmp(name, str)) {
Namhyung Kime5a18452012-08-06 13:41:20 +0900188 if (idx)
189 *idx = cnt;
Jiri Olsa155b3a12014-03-02 14:32:07 +0100190 return sec;
Namhyung Kime5a18452012-08-06 13:41:20 +0900191 }
192 ++cnt;
193 }
194
Jiri Olsa155b3a12014-03-02 14:32:07 +0100195 return NULL;
Namhyung Kime5a18452012-08-06 13:41:20 +0900196}
197
Milian Wolff2a8d41b2016-08-30 13:41:02 +0200198static bool want_demangle(bool is_kernel_sym)
199{
200 return is_kernel_sym ? symbol_conf.demangle_kernel : symbol_conf.demangle;
201}
202
203static char *demangle_sym(struct dso *dso, int kmodule, const char *elf_name)
204{
Namhyung Kimbb963e12017-02-17 17:17:38 +0900205 int demangle_flags = verbose > 0 ? (DMGL_PARAMS | DMGL_ANSI) : DMGL_NO_OPTS;
Milian Wolff2a8d41b2016-08-30 13:41:02 +0200206 char *demangled = NULL;
207
208 /*
209 * We need to figure out if the object was created from C++ sources
210 * DWARF DW_compile_unit has this, but we don't always have access
211 * to it...
212 */
213 if (!want_demangle(dso->kernel || kmodule))
214 return demangled;
215
216 demangled = bfd_demangle(NULL, elf_name, demangle_flags);
217 if (demangled == NULL)
218 demangled = java_demangle_sym(elf_name, JAVA_DEMANGLE_NORET);
219 else if (rust_is_mangled(demangled))
220 /*
221 * Input to Rust demangling is the BFD-demangled
222 * name which it Rust-demangles in place.
223 */
224 rust_demangle_sym(demangled);
225
226 return demangled;
227}
228
Namhyung Kime5a18452012-08-06 13:41:20 +0900229#define elf_section__for_each_rel(reldata, pos, pos_mem, idx, nr_entries) \
230 for (idx = 0, pos = gelf_getrel(reldata, 0, &pos_mem); \
231 idx < nr_entries; \
232 ++idx, pos = gelf_getrel(reldata, idx, &pos_mem))
233
234#define elf_section__for_each_rela(reldata, pos, pos_mem, idx, nr_entries) \
235 for (idx = 0, pos = gelf_getrela(reldata, 0, &pos_mem); \
236 idx < nr_entries; \
237 ++idx, pos = gelf_getrela(reldata, idx, &pos_mem))
238
239/*
240 * We need to check if we have a .dynsym, so that we can handle the
241 * .plt, synthesizing its symbols, that aren't on the symtabs (be it
242 * .dynsym or .symtab).
243 * And always look at the original dso, not at debuginfo packages, that
244 * have the PLT data stripped out (shdr_rel_plt.sh_type == SHT_NOBITS).
245 */
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -0300246int dso__synthesize_plt_symbols(struct dso *dso, struct symsrc *ss)
Namhyung Kime5a18452012-08-06 13:41:20 +0900247{
248 uint32_t nr_rel_entries, idx;
249 GElf_Sym sym;
Li Binb2f76052017-06-05 08:34:09 +0800250 u64 plt_offset, plt_header_size, plt_entry_size;
Namhyung Kime5a18452012-08-06 13:41:20 +0900251 GElf_Shdr shdr_plt;
252 struct symbol *f;
253 GElf_Shdr shdr_rel_plt, shdr_dynsym;
254 Elf_Data *reldata, *syms, *symstrs;
255 Elf_Scn *scn_plt_rel, *scn_symstrs, *scn_dynsym;
256 size_t dynsym_idx;
257 GElf_Ehdr ehdr;
258 char sympltname[1024];
259 Elf *elf;
Cody P Schafera44f6052012-08-10 15:22:59 -0700260 int nr = 0, symidx, err = 0;
Namhyung Kime5a18452012-08-06 13:41:20 +0900261
David Ahernf47b58b2012-08-19 09:47:14 -0600262 if (!ss->dynsym)
263 return 0;
264
Cody P Schafera44f6052012-08-10 15:22:59 -0700265 elf = ss->elf;
266 ehdr = ss->ehdr;
Namhyung Kime5a18452012-08-06 13:41:20 +0900267
Cody P Schafera44f6052012-08-10 15:22:59 -0700268 scn_dynsym = ss->dynsym;
269 shdr_dynsym = ss->dynshdr;
270 dynsym_idx = ss->dynsym_idx;
Namhyung Kime5a18452012-08-06 13:41:20 +0900271
Namhyung Kime5a18452012-08-06 13:41:20 +0900272 if (scn_dynsym == NULL)
273 goto out_elf_end;
274
275 scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt,
276 ".rela.plt", NULL);
277 if (scn_plt_rel == NULL) {
278 scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt,
279 ".rel.plt", NULL);
280 if (scn_plt_rel == NULL)
281 goto out_elf_end;
282 }
283
284 err = -1;
285
286 if (shdr_rel_plt.sh_link != dynsym_idx)
287 goto out_elf_end;
288
289 if (elf_section_by_name(elf, &ehdr, &shdr_plt, ".plt", NULL) == NULL)
290 goto out_elf_end;
291
292 /*
293 * Fetch the relocation section to find the idxes to the GOT
294 * and the symbols in the .dynsym they refer to.
295 */
296 reldata = elf_getdata(scn_plt_rel, NULL);
297 if (reldata == NULL)
298 goto out_elf_end;
299
300 syms = elf_getdata(scn_dynsym, NULL);
301 if (syms == NULL)
302 goto out_elf_end;
303
304 scn_symstrs = elf_getscn(elf, shdr_dynsym.sh_link);
305 if (scn_symstrs == NULL)
306 goto out_elf_end;
307
308 symstrs = elf_getdata(scn_symstrs, NULL);
309 if (symstrs == NULL)
310 goto out_elf_end;
311
Cody P Schafer52f9ddb2012-08-10 15:22:51 -0700312 if (symstrs->d_size == 0)
313 goto out_elf_end;
314
Namhyung Kime5a18452012-08-06 13:41:20 +0900315 nr_rel_entries = shdr_rel_plt.sh_size / shdr_rel_plt.sh_entsize;
316 plt_offset = shdr_plt.sh_offset;
Li Binb2f76052017-06-05 08:34:09 +0800317 switch (ehdr.e_machine) {
318 case EM_ARM:
319 plt_header_size = 20;
320 plt_entry_size = 12;
321 break;
322
323 case EM_AARCH64:
324 plt_header_size = 32;
325 plt_entry_size = 16;
326 break;
327
David Millerd6afa562018-10-17 12:08:59 -0700328 case EM_SPARC:
329 plt_header_size = 48;
330 plt_entry_size = 12;
331 break;
332
333 case EM_SPARCV9:
334 plt_header_size = 128;
335 plt_entry_size = 32;
336 break;
337
338 default: /* FIXME: s390/alpha/mips/parisc/poperpc/sh/xtensa need to be checked */
Li Binb2f76052017-06-05 08:34:09 +0800339 plt_header_size = shdr_plt.sh_entsize;
340 plt_entry_size = shdr_plt.sh_entsize;
341 break;
342 }
343 plt_offset += plt_header_size;
Namhyung Kime5a18452012-08-06 13:41:20 +0900344
345 if (shdr_rel_plt.sh_type == SHT_RELA) {
346 GElf_Rela pos_mem, *pos;
347
348 elf_section__for_each_rela(reldata, pos, pos_mem, idx,
349 nr_rel_entries) {
Milian Wolff2a8d41b2016-08-30 13:41:02 +0200350 const char *elf_name = NULL;
351 char *demangled = NULL;
Namhyung Kime5a18452012-08-06 13:41:20 +0900352 symidx = GELF_R_SYM(pos->r_info);
Namhyung Kime5a18452012-08-06 13:41:20 +0900353 gelf_getsym(syms, symidx, &sym);
Milian Wolff2a8d41b2016-08-30 13:41:02 +0200354
355 elf_name = elf_sym__name(&sym, symstrs);
356 demangled = demangle_sym(dso, 0, elf_name);
357 if (demangled != NULL)
358 elf_name = demangled;
Namhyung Kime5a18452012-08-06 13:41:20 +0900359 snprintf(sympltname, sizeof(sympltname),
Milian Wolff2a8d41b2016-08-30 13:41:02 +0200360 "%s@plt", elf_name);
361 free(demangled);
Namhyung Kime5a18452012-08-06 13:41:20 +0900362
Li Binb2f76052017-06-05 08:34:09 +0800363 f = symbol__new(plt_offset, plt_entry_size,
Arnaldo Carvalho de Meloaf30bff2018-04-26 11:09:10 -0300364 STB_GLOBAL, STT_FUNC, sympltname);
Namhyung Kime5a18452012-08-06 13:41:20 +0900365 if (!f)
366 goto out_elf_end;
367
Li Binb2f76052017-06-05 08:34:09 +0800368 plt_offset += plt_entry_size;
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -0300369 symbols__insert(&dso->symbols, f);
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -0300370 ++nr;
Namhyung Kime5a18452012-08-06 13:41:20 +0900371 }
372 } else if (shdr_rel_plt.sh_type == SHT_REL) {
373 GElf_Rel pos_mem, *pos;
374 elf_section__for_each_rel(reldata, pos, pos_mem, idx,
375 nr_rel_entries) {
Milian Wolff2a8d41b2016-08-30 13:41:02 +0200376 const char *elf_name = NULL;
377 char *demangled = NULL;
Namhyung Kime5a18452012-08-06 13:41:20 +0900378 symidx = GELF_R_SYM(pos->r_info);
Namhyung Kime5a18452012-08-06 13:41:20 +0900379 gelf_getsym(syms, symidx, &sym);
Milian Wolff2a8d41b2016-08-30 13:41:02 +0200380
381 elf_name = elf_sym__name(&sym, symstrs);
382 demangled = demangle_sym(dso, 0, elf_name);
383 if (demangled != NULL)
384 elf_name = demangled;
Namhyung Kime5a18452012-08-06 13:41:20 +0900385 snprintf(sympltname, sizeof(sympltname),
Milian Wolff2a8d41b2016-08-30 13:41:02 +0200386 "%s@plt", elf_name);
387 free(demangled);
Namhyung Kime5a18452012-08-06 13:41:20 +0900388
Li Binb2f76052017-06-05 08:34:09 +0800389 f = symbol__new(plt_offset, plt_entry_size,
Arnaldo Carvalho de Meloaf30bff2018-04-26 11:09:10 -0300390 STB_GLOBAL, STT_FUNC, sympltname);
Namhyung Kime5a18452012-08-06 13:41:20 +0900391 if (!f)
392 goto out_elf_end;
393
Li Binb2f76052017-06-05 08:34:09 +0800394 plt_offset += plt_entry_size;
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -0300395 symbols__insert(&dso->symbols, f);
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -0300396 ++nr;
Namhyung Kime5a18452012-08-06 13:41:20 +0900397 }
398 }
399
400 err = 0;
401out_elf_end:
Namhyung Kime5a18452012-08-06 13:41:20 +0900402 if (err == 0)
403 return nr;
Namhyung Kime5a18452012-08-06 13:41:20 +0900404 pr_debug("%s: problems reading %s PLT info.\n",
405 __func__, dso->long_name);
406 return 0;
407}
408
Milian Wolff80c345b2017-08-06 23:24:34 +0200409char *dso__demangle_sym(struct dso *dso, int kmodule, const char *elf_name)
Jin Yaoa64489c2017-03-26 04:34:26 +0800410{
411 return demangle_sym(dso, kmodule, elf_name);
412}
413
Namhyung Kime5a18452012-08-06 13:41:20 +0900414/*
415 * Align offset to 4 bytes as needed for note name and descriptor data.
416 */
417#define NOTE_ALIGN(n) (((n) + 3) & -4U)
418
419static int elf_read_build_id(Elf *elf, void *bf, size_t size)
420{
421 int err = -1;
422 GElf_Ehdr ehdr;
423 GElf_Shdr shdr;
424 Elf_Data *data;
425 Elf_Scn *sec;
426 Elf_Kind ek;
427 void *ptr;
428
429 if (size < BUILD_ID_SIZE)
430 goto out;
431
432 ek = elf_kind(elf);
433 if (ek != ELF_K_ELF)
434 goto out;
435
436 if (gelf_getehdr(elf, &ehdr) == NULL) {
437 pr_err("%s: cannot get elf header.\n", __func__);
438 goto out;
439 }
440
441 /*
442 * Check following sections for notes:
443 * '.note.gnu.build-id'
444 * '.notes'
445 * '.note' (VDSO specific)
446 */
447 do {
448 sec = elf_section_by_name(elf, &ehdr, &shdr,
449 ".note.gnu.build-id", NULL);
450 if (sec)
451 break;
452
453 sec = elf_section_by_name(elf, &ehdr, &shdr,
454 ".notes", NULL);
455 if (sec)
456 break;
457
458 sec = elf_section_by_name(elf, &ehdr, &shdr,
459 ".note", NULL);
460 if (sec)
461 break;
462
463 return err;
464
465 } while (0);
466
467 data = elf_getdata(sec, NULL);
468 if (data == NULL)
469 goto out;
470
471 ptr = data->d_buf;
472 while (ptr < (data->d_buf + data->d_size)) {
473 GElf_Nhdr *nhdr = ptr;
474 size_t namesz = NOTE_ALIGN(nhdr->n_namesz),
475 descsz = NOTE_ALIGN(nhdr->n_descsz);
476 const char *name;
477
478 ptr += sizeof(*nhdr);
479 name = ptr;
480 ptr += namesz;
481 if (nhdr->n_type == NT_GNU_BUILD_ID &&
482 nhdr->n_namesz == sizeof("GNU")) {
483 if (memcmp(name, "GNU", sizeof("GNU")) == 0) {
484 size_t sz = min(size, descsz);
485 memcpy(bf, ptr, sz);
486 memset(bf + sz, 0, size - sz);
487 err = descsz;
488 break;
489 }
490 }
491 ptr += descsz;
492 }
493
494out:
495 return err;
496}
497
498int filename__read_build_id(const char *filename, void *bf, size_t size)
499{
500 int fd, err = -1;
501 Elf *elf;
502
503 if (size < BUILD_ID_SIZE)
504 goto out;
505
506 fd = open(filename, O_RDONLY);
507 if (fd < 0)
508 goto out;
509
510 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
511 if (elf == NULL) {
512 pr_debug2("%s: cannot read %s ELF file.\n", __func__, filename);
513 goto out_close;
514 }
515
516 err = elf_read_build_id(elf, bf, size);
517
518 elf_end(elf);
519out_close:
520 close(fd);
521out:
522 return err;
523}
524
525int sysfs__read_build_id(const char *filename, void *build_id, size_t size)
526{
527 int fd, err = -1;
528
529 if (size < BUILD_ID_SIZE)
530 goto out;
531
532 fd = open(filename, O_RDONLY);
533 if (fd < 0)
534 goto out;
535
536 while (1) {
537 char bf[BUFSIZ];
538 GElf_Nhdr nhdr;
539 size_t namesz, descsz;
540
541 if (read(fd, &nhdr, sizeof(nhdr)) != sizeof(nhdr))
542 break;
543
544 namesz = NOTE_ALIGN(nhdr.n_namesz);
545 descsz = NOTE_ALIGN(nhdr.n_descsz);
546 if (nhdr.n_type == NT_GNU_BUILD_ID &&
547 nhdr.n_namesz == sizeof("GNU")) {
548 if (read(fd, bf, namesz) != (ssize_t)namesz)
549 break;
550 if (memcmp(bf, "GNU", sizeof("GNU")) == 0) {
551 size_t sz = min(descsz, size);
552 if (read(fd, build_id, sz) == (ssize_t)sz) {
553 memset(build_id + sz, 0, size - sz);
554 err = 0;
555 break;
556 }
557 } else if (read(fd, bf, descsz) != (ssize_t)descsz)
558 break;
559 } else {
560 int n = namesz + descsz;
Arnaldo Carvalho de Melo7934c982017-01-03 15:19:21 -0300561
562 if (n > (int)sizeof(bf)) {
563 n = sizeof(bf);
564 pr_debug("%s: truncating reading of build id in sysfs file %s: n_namesz=%u, n_descsz=%u.\n",
565 __func__, filename, nhdr.n_namesz, nhdr.n_descsz);
566 }
Namhyung Kime5a18452012-08-06 13:41:20 +0900567 if (read(fd, bf, n) != n)
568 break;
569 }
570 }
571 close(fd);
572out:
573 return err;
574}
575
576int filename__read_debuglink(const char *filename, char *debuglink,
577 size_t size)
578{
579 int fd, err = -1;
580 Elf *elf;
581 GElf_Ehdr ehdr;
582 GElf_Shdr shdr;
583 Elf_Data *data;
584 Elf_Scn *sec;
585 Elf_Kind ek;
586
587 fd = open(filename, O_RDONLY);
588 if (fd < 0)
589 goto out;
590
591 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
592 if (elf == NULL) {
593 pr_debug2("%s: cannot read %s ELF file.\n", __func__, filename);
594 goto out_close;
595 }
596
597 ek = elf_kind(elf);
598 if (ek != ELF_K_ELF)
Chenggang Qin784f3392013-10-11 08:27:57 +0800599 goto out_elf_end;
Namhyung Kime5a18452012-08-06 13:41:20 +0900600
601 if (gelf_getehdr(elf, &ehdr) == NULL) {
602 pr_err("%s: cannot get elf header.\n", __func__);
Chenggang Qin784f3392013-10-11 08:27:57 +0800603 goto out_elf_end;
Namhyung Kime5a18452012-08-06 13:41:20 +0900604 }
605
606 sec = elf_section_by_name(elf, &ehdr, &shdr,
607 ".gnu_debuglink", NULL);
608 if (sec == NULL)
Chenggang Qin784f3392013-10-11 08:27:57 +0800609 goto out_elf_end;
Namhyung Kime5a18452012-08-06 13:41:20 +0900610
611 data = elf_getdata(sec, NULL);
612 if (data == NULL)
Chenggang Qin784f3392013-10-11 08:27:57 +0800613 goto out_elf_end;
Namhyung Kime5a18452012-08-06 13:41:20 +0900614
615 /* the start of this section is a zero-terminated string */
616 strncpy(debuglink, data->d_buf, size);
617
Stephane Eranian0d3dc5e2014-02-20 10:32:55 +0900618 err = 0;
619
Chenggang Qin784f3392013-10-11 08:27:57 +0800620out_elf_end:
Namhyung Kime5a18452012-08-06 13:41:20 +0900621 elf_end(elf);
Namhyung Kime5a18452012-08-06 13:41:20 +0900622out_close:
623 close(fd);
624out:
625 return err;
626}
627
628static int dso__swap_init(struct dso *dso, unsigned char eidata)
629{
630 static unsigned int const endian = 1;
631
632 dso->needs_swap = DSO_SWAP__NO;
633
634 switch (eidata) {
635 case ELFDATA2LSB:
636 /* We are big endian, DSO is little endian. */
637 if (*(unsigned char const *)&endian != 1)
638 dso->needs_swap = DSO_SWAP__YES;
639 break;
640
641 case ELFDATA2MSB:
642 /* We are little endian, DSO is big endian. */
643 if (*(unsigned char const *)&endian != 0)
644 dso->needs_swap = DSO_SWAP__YES;
645 break;
646
647 default:
648 pr_err("unrecognized DSO data encoding %d\n", eidata);
649 return -EINVAL;
650 }
651
652 return 0;
653}
654
Cody P Schafer3aafe5a2012-08-10 15:23:02 -0700655bool symsrc__possibly_runtime(struct symsrc *ss)
656{
657 return ss->dynsym || ss->opdsec;
658}
659
Cody P Schaferd26cd122012-08-10 15:23:00 -0700660bool symsrc__has_symtab(struct symsrc *ss)
661{
662 return ss->symtab != NULL;
663}
Cody P Schaferb68e2f92012-08-10 15:22:57 -0700664
665void symsrc__destroy(struct symsrc *ss)
Namhyung Kime5a18452012-08-06 13:41:20 +0900666{
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300667 zfree(&ss->name);
Cody P Schaferb68e2f92012-08-10 15:22:57 -0700668 elf_end(ss->elf);
669 close(ss->fd);
670}
671
Naveen N. Raod2332092015-04-28 17:35:35 +0530672bool __weak elf__needs_adjust_symbols(GElf_Ehdr ehdr)
673{
674 return ehdr.e_type == ET_EXEC || ehdr.e_type == ET_REL;
675}
676
Cody P Schaferb68e2f92012-08-10 15:22:57 -0700677int symsrc__init(struct symsrc *ss, struct dso *dso, const char *name,
678 enum dso_binary_type type)
679{
Namhyung Kime5a18452012-08-06 13:41:20 +0900680 int err = -1;
Namhyung Kime5a18452012-08-06 13:41:20 +0900681 GElf_Ehdr ehdr;
Namhyung Kime5a18452012-08-06 13:41:20 +0900682 Elf *elf;
Cody P Schaferb68e2f92012-08-10 15:22:57 -0700683 int fd;
684
Arnaldo Carvalho de Melo18425f12015-03-24 11:49:02 -0300685 if (dso__needs_decompress(dso)) {
Namhyung Kim42b3fa62017-06-08 16:31:03 +0900686 fd = dso__decompress_kmodule_fd(dso, name);
Arnaldo Carvalho de Melo18425f12015-03-24 11:49:02 -0300687 if (fd < 0)
688 return -1;
Namhyung Kimc25ec422017-06-08 16:31:08 +0900689
690 type = dso->symtab_type;
Arnaldo Carvalho de Melo18425f12015-03-24 11:49:02 -0300691 } else {
Namhyung Kimc00c48f2014-11-04 10:14:27 +0900692 fd = open(name, O_RDONLY);
Arnaldo Carvalho de Melo18425f12015-03-24 11:49:02 -0300693 if (fd < 0) {
694 dso->load_errno = errno;
695 return -1;
696 }
697 }
Namhyung Kime5a18452012-08-06 13:41:20 +0900698
699 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
700 if (elf == NULL) {
701 pr_debug("%s: cannot read %s ELF file.\n", __func__, name);
Arnaldo Carvalho de Melo18425f12015-03-24 11:49:02 -0300702 dso->load_errno = DSO_LOAD_ERRNO__INVALID_ELF;
Namhyung Kime5a18452012-08-06 13:41:20 +0900703 goto out_close;
704 }
705
706 if (gelf_getehdr(elf, &ehdr) == NULL) {
Arnaldo Carvalho de Melo18425f12015-03-24 11:49:02 -0300707 dso->load_errno = DSO_LOAD_ERRNO__INVALID_ELF;
Namhyung Kime5a18452012-08-06 13:41:20 +0900708 pr_debug("%s: cannot get elf header.\n", __func__);
709 goto out_elf_end;
710 }
711
Arnaldo Carvalho de Melo18425f12015-03-24 11:49:02 -0300712 if (dso__swap_init(dso, ehdr.e_ident[EI_DATA])) {
713 dso->load_errno = DSO_LOAD_ERRNO__INTERNAL_ERROR;
Namhyung Kime5a18452012-08-06 13:41:20 +0900714 goto out_elf_end;
Arnaldo Carvalho de Melo18425f12015-03-24 11:49:02 -0300715 }
Namhyung Kime5a18452012-08-06 13:41:20 +0900716
717 /* Always reject images with a mismatched build-id: */
Masami Hiramatsu428aff82016-08-26 01:24:42 +0900718 if (dso->has_build_id && !symbol_conf.ignore_vmlinux_buildid) {
Namhyung Kime5a18452012-08-06 13:41:20 +0900719 u8 build_id[BUILD_ID_SIZE];
720
Arnaldo Carvalho de Melo18425f12015-03-24 11:49:02 -0300721 if (elf_read_build_id(elf, build_id, BUILD_ID_SIZE) < 0) {
722 dso->load_errno = DSO_LOAD_ERRNO__CANNOT_READ_BUILDID;
Namhyung Kime5a18452012-08-06 13:41:20 +0900723 goto out_elf_end;
Arnaldo Carvalho de Melo18425f12015-03-24 11:49:02 -0300724 }
Namhyung Kime5a18452012-08-06 13:41:20 +0900725
Arnaldo Carvalho de Melo18425f12015-03-24 11:49:02 -0300726 if (!dso__build_id_equal(dso, build_id)) {
Naveen N. Rao468f3d22015-04-25 01:14:46 +0530727 pr_debug("%s: build id mismatch for %s.\n", __func__, name);
Arnaldo Carvalho de Melo18425f12015-03-24 11:49:02 -0300728 dso->load_errno = DSO_LOAD_ERRNO__MISMATCHING_BUILDID;
Namhyung Kime5a18452012-08-06 13:41:20 +0900729 goto out_elf_end;
Arnaldo Carvalho de Melo18425f12015-03-24 11:49:02 -0300730 }
Namhyung Kime5a18452012-08-06 13:41:20 +0900731 }
732
Adrian Hunterc6d8f2a2014-07-14 13:02:41 +0300733 ss->is_64_bit = (gelf_getclass(elf) == ELFCLASS64);
734
Cody P Schaferb68e2f92012-08-10 15:22:57 -0700735 ss->symtab = elf_section_by_name(elf, &ehdr, &ss->symshdr, ".symtab",
736 NULL);
737 if (ss->symshdr.sh_type != SHT_SYMTAB)
738 ss->symtab = NULL;
739
740 ss->dynsym_idx = 0;
741 ss->dynsym = elf_section_by_name(elf, &ehdr, &ss->dynshdr, ".dynsym",
742 &ss->dynsym_idx);
743 if (ss->dynshdr.sh_type != SHT_DYNSYM)
744 ss->dynsym = NULL;
745
746 ss->opdidx = 0;
747 ss->opdsec = elf_section_by_name(elf, &ehdr, &ss->opdshdr, ".opd",
748 &ss->opdidx);
749 if (ss->opdshdr.sh_type != SHT_PROGBITS)
750 ss->opdsec = NULL;
751
Wang Nan99e87f72016-04-07 10:24:31 +0000752 if (dso->kernel == DSO_TYPE_USER)
753 ss->adjust_symbols = true;
754 else
Naveen N. Raod2332092015-04-28 17:35:35 +0530755 ss->adjust_symbols = elf__needs_adjust_symbols(ehdr);
Cody P Schaferb68e2f92012-08-10 15:22:57 -0700756
757 ss->name = strdup(name);
Arnaldo Carvalho de Melo18425f12015-03-24 11:49:02 -0300758 if (!ss->name) {
759 dso->load_errno = errno;
Cody P Schaferb68e2f92012-08-10 15:22:57 -0700760 goto out_elf_end;
Arnaldo Carvalho de Melo18425f12015-03-24 11:49:02 -0300761 }
Cody P Schaferb68e2f92012-08-10 15:22:57 -0700762
763 ss->elf = elf;
764 ss->fd = fd;
765 ss->ehdr = ehdr;
766 ss->type = type;
767
768 return 0;
769
770out_elf_end:
771 elf_end(elf);
772out_close:
773 close(fd);
774 return err;
775}
776
Adrian Hunter39b12f782013-08-07 14:38:47 +0300777/**
778 * ref_reloc_sym_not_found - has kernel relocation symbol been found.
779 * @kmap: kernel maps and relocation reference symbol
780 *
781 * This function returns %true if we are dealing with the kernel maps and the
782 * relocation reference symbol has not yet been found. Otherwise %false is
783 * returned.
784 */
785static bool ref_reloc_sym_not_found(struct kmap *kmap)
786{
787 return kmap && kmap->ref_reloc_sym && kmap->ref_reloc_sym->name &&
788 !kmap->ref_reloc_sym->unrelocated_addr;
789}
790
791/**
792 * ref_reloc - kernel relocation offset.
793 * @kmap: kernel maps and relocation reference symbol
794 *
795 * This function returns the offset of kernel addresses as determined by using
796 * the relocation reference symbol i.e. if the kernel has not been relocated
797 * then the return value is zero.
798 */
799static u64 ref_reloc(struct kmap *kmap)
800{
801 if (kmap && kmap->ref_reloc_sym &&
802 kmap->ref_reloc_sym->unrelocated_addr)
803 return kmap->ref_reloc_sym->addr -
804 kmap->ref_reloc_sym->unrelocated_addr;
805 return 0;
806}
807
Naveen N. Rao0b3c2262016-04-12 14:40:50 +0530808void __weak arch__sym_update(struct symbol *s __maybe_unused,
809 GElf_Sym *sym __maybe_unused) { }
Ananth N Mavinakayanahallic50fc0a2015-04-28 17:35:38 +0530810
Arnaldo Carvalho de Melo4e0d1e82018-04-27 15:15:24 -0300811static int dso__process_kernel_symbol(struct dso *dso, struct map *map,
812 GElf_Sym *sym, GElf_Shdr *shdr,
813 struct map_groups *kmaps, struct kmap *kmap,
814 struct dso **curr_dsop, struct map **curr_mapp,
815 const char *section_name,
816 bool adjust_kernel_syms, bool kmodule, bool *remap_kernel)
817{
818 struct dso *curr_dso = *curr_dsop;
819 struct map *curr_map;
820 char dso_name[PATH_MAX];
821
822 /* Adjust symbol to map to file offset */
823 if (adjust_kernel_syms)
824 sym->st_value -= shdr->sh_addr - shdr->sh_offset;
825
826 if (strcmp(section_name, (curr_dso->short_name + dso->short_name_len)) == 0)
827 return 0;
828
829 if (strcmp(section_name, ".text") == 0) {
830 /*
831 * The initial kernel mapping is based on
832 * kallsyms and identity maps. Overwrite it to
833 * map to the kernel dso.
834 */
835 if (*remap_kernel && dso->kernel) {
836 *remap_kernel = false;
837 map->start = shdr->sh_addr + ref_reloc(kmap);
838 map->end = map->start + shdr->sh_size;
839 map->pgoff = shdr->sh_offset;
840 map->map_ip = map__map_ip;
841 map->unmap_ip = map__unmap_ip;
842 /* Ensure maps are correctly ordered */
843 if (kmaps) {
844 map__get(map);
845 map_groups__remove(kmaps, map);
846 map_groups__insert(kmaps, map);
847 map__put(map);
848 }
849 }
850
851 /*
852 * The initial module mapping is based on
853 * /proc/modules mapped to offset zero.
854 * Overwrite it to map to the module dso.
855 */
856 if (*remap_kernel && kmodule) {
857 *remap_kernel = false;
858 map->pgoff = shdr->sh_offset;
859 }
860
861 *curr_mapp = map;
862 *curr_dsop = dso;
863 return 0;
864 }
865
866 if (!kmap)
867 return 0;
868
869 snprintf(dso_name, sizeof(dso_name), "%s%s", dso->short_name, section_name);
870
871 curr_map = map_groups__find_by_name(kmaps, dso_name);
872 if (curr_map == NULL) {
873 u64 start = sym->st_value;
874
875 if (kmodule)
876 start += map->start + shdr->sh_offset;
877
878 curr_dso = dso__new(dso_name);
879 if (curr_dso == NULL)
880 return -1;
881 curr_dso->kernel = dso->kernel;
882 curr_dso->long_name = dso->long_name;
883 curr_dso->long_name_len = dso->long_name_len;
884 curr_map = map__new2(start, curr_dso);
885 dso__put(curr_dso);
886 if (curr_map == NULL)
887 return -1;
888
889 if (adjust_kernel_syms) {
890 curr_map->start = shdr->sh_addr + ref_reloc(kmap);
891 curr_map->end = curr_map->start + shdr->sh_size;
892 curr_map->pgoff = shdr->sh_offset;
893 } else {
894 curr_map->map_ip = curr_map->unmap_ip = identity__map_ip;
895 }
896 curr_dso->symtab_type = dso->symtab_type;
897 map_groups__insert(kmaps, curr_map);
898 /*
899 * Add it before we drop the referece to curr_map, i.e. while
900 * we still are sure to have a reference to this DSO via
901 * *curr_map->dso.
902 */
903 dsos__add(&map->groups->machine->dsos, curr_dso);
904 /* kmaps already got it */
905 map__put(curr_map);
906 dso__set_loaded(curr_dso);
907 *curr_mapp = curr_map;
908 *curr_dsop = curr_dso;
909 } else
910 *curr_dsop = curr_map->dso;
911
912 return 0;
913}
914
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -0300915int dso__load_sym(struct dso *dso, struct map *map, struct symsrc *syms_ss,
916 struct symsrc *runtime_ss, int kmodule)
Cody P Schaferb68e2f92012-08-10 15:22:57 -0700917{
918 struct kmap *kmap = dso->kernel ? map__kmap(map) : NULL;
Wang Nanba927322015-04-07 08:22:45 +0000919 struct map_groups *kmaps = kmap ? map__kmaps(map) : NULL;
Cody P Schaferb68e2f92012-08-10 15:22:57 -0700920 struct map *curr_map = map;
921 struct dso *curr_dso = dso;
922 Elf_Data *symstrs, *secstrs;
923 uint32_t nr_syms;
924 int err = -1;
925 uint32_t idx;
926 GElf_Ehdr ehdr;
Cody P Schafer261360b2012-08-10 15:23:01 -0700927 GElf_Shdr shdr;
Wang Nan73cdf0c2016-02-26 09:31:49 +0000928 GElf_Shdr tshdr;
Cody P Schaferb68e2f92012-08-10 15:22:57 -0700929 Elf_Data *syms, *opddata = NULL;
930 GElf_Sym sym;
Cody P Schafer261360b2012-08-10 15:23:01 -0700931 Elf_Scn *sec, *sec_strndx;
Cody P Schaferb68e2f92012-08-10 15:22:57 -0700932 Elf *elf;
933 int nr = 0;
Adrian Hunter39b12f782013-08-07 14:38:47 +0300934 bool remap_kernel = false, adjust_kernel_syms = false;
Cody P Schaferb68e2f92012-08-10 15:22:57 -0700935
Wang Nanba927322015-04-07 08:22:45 +0000936 if (kmap && !kmaps)
937 return -1;
938
Cody P Schafer261360b2012-08-10 15:23:01 -0700939 dso->symtab_type = syms_ss->type;
Adrian Hunterc6d8f2a2014-07-14 13:02:41 +0300940 dso->is_64_bit = syms_ss->is_64_bit;
Adrian Hunter0131c4e2013-08-07 14:38:50 +0300941 dso->rel = syms_ss->ehdr.e_type == ET_REL;
942
943 /*
944 * Modules may already have symbols from kallsyms, but those symbols
945 * have the wrong values for the dso maps, so remove them.
946 */
947 if (kmodule && syms_ss->symtab)
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -0300948 symbols__delete(&dso->symbols);
Cody P Schafer005f9292012-08-10 15:22:58 -0700949
Cody P Schafer261360b2012-08-10 15:23:01 -0700950 if (!syms_ss->symtab) {
Anton Blanchardd0b0d042014-09-09 08:59:29 +1000951 /*
952 * If the vmlinux is stripped, fail so we will fall back
953 * to using kallsyms. The vmlinux runtime symbols aren't
954 * of much use.
955 */
956 if (dso->kernel)
957 goto out_elf_end;
958
Cody P Schafer261360b2012-08-10 15:23:01 -0700959 syms_ss->symtab = syms_ss->dynsym;
960 syms_ss->symshdr = syms_ss->dynshdr;
Cody P Schaferd26cd122012-08-10 15:23:00 -0700961 }
962
Cody P Schafer261360b2012-08-10 15:23:01 -0700963 elf = syms_ss->elf;
964 ehdr = syms_ss->ehdr;
965 sec = syms_ss->symtab;
966 shdr = syms_ss->symshdr;
Cody P Schaferb68e2f92012-08-10 15:22:57 -0700967
Anton Blanchard50de1a02016-08-13 11:55:33 +1000968 if (elf_section_by_name(runtime_ss->elf, &runtime_ss->ehdr, &tshdr,
969 ".text", NULL))
Wang Nan73cdf0c2016-02-26 09:31:49 +0000970 dso->text_offset = tshdr.sh_addr - tshdr.sh_offset;
971
Cody P Schafer261360b2012-08-10 15:23:01 -0700972 if (runtime_ss->opdsec)
973 opddata = elf_rawdata(runtime_ss->opdsec, NULL);
Namhyung Kime5a18452012-08-06 13:41:20 +0900974
975 syms = elf_getdata(sec, NULL);
976 if (syms == NULL)
977 goto out_elf_end;
978
979 sec = elf_getscn(elf, shdr.sh_link);
980 if (sec == NULL)
981 goto out_elf_end;
982
983 symstrs = elf_getdata(sec, NULL);
984 if (symstrs == NULL)
985 goto out_elf_end;
986
Adrian Hunterf247fb82014-07-31 09:00:46 +0300987 sec_strndx = elf_getscn(runtime_ss->elf, runtime_ss->ehdr.e_shstrndx);
Namhyung Kime5a18452012-08-06 13:41:20 +0900988 if (sec_strndx == NULL)
989 goto out_elf_end;
990
991 secstrs = elf_getdata(sec_strndx, NULL);
992 if (secstrs == NULL)
993 goto out_elf_end;
994
995 nr_syms = shdr.sh_size / shdr.sh_entsize;
996
997 memset(&sym, 0, sizeof(sym));
Adrian Hunter39b12f782013-08-07 14:38:47 +0300998
999 /*
1000 * The kernel relocation symbol is needed in advance in order to adjust
1001 * kernel maps correctly.
1002 */
1003 if (ref_reloc_sym_not_found(kmap)) {
1004 elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) {
1005 const char *elf_name = elf_sym__name(&sym, symstrs);
1006
1007 if (strcmp(elf_name, kmap->ref_reloc_sym->name))
1008 continue;
1009 kmap->ref_reloc_sym->unrelocated_addr = sym.st_value;
Adrian Hunter91767532014-01-29 16:14:36 +02001010 map->reloc = kmap->ref_reloc_sym->addr -
1011 kmap->ref_reloc_sym->unrelocated_addr;
Adrian Hunter39b12f782013-08-07 14:38:47 +03001012 break;
1013 }
1014 }
1015
Adrian Hunterf0ee3b42015-08-14 15:50:06 +03001016 /*
1017 * Handle any relocation of vdso necessary because older kernels
1018 * attempted to prelink vdso to its virtual address.
1019 */
Wang Nan73cdf0c2016-02-26 09:31:49 +00001020 if (dso__is_vdso(dso))
1021 map->reloc = map->start - dso->text_offset;
Adrian Hunterf0ee3b42015-08-14 15:50:06 +03001022
Adrian Hunter39b12f782013-08-07 14:38:47 +03001023 dso->adjust_symbols = runtime_ss->adjust_symbols || ref_reloc(kmap);
1024 /*
Arnaldo Carvalho de Melod1fd8d92018-04-26 12:36:37 -03001025 * Initial kernel and module mappings do not map to the dso.
1026 * Flag the fixups.
Adrian Hunter39b12f782013-08-07 14:38:47 +03001027 */
Arnaldo Carvalho de Melod1fd8d92018-04-26 12:36:37 -03001028 if (dso->kernel || kmodule) {
Adrian Hunter39b12f782013-08-07 14:38:47 +03001029 remap_kernel = true;
1030 adjust_kernel_syms = dso->adjust_symbols;
1031 }
Namhyung Kime5a18452012-08-06 13:41:20 +09001032 elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) {
1033 struct symbol *f;
1034 const char *elf_name = elf_sym__name(&sym, symstrs);
1035 char *demangled = NULL;
1036 int is_label = elf_sym__is_label(&sym);
1037 const char *section_name;
Cody P Schafer261360b2012-08-10 15:23:01 -07001038 bool used_opd = false;
Namhyung Kime5a18452012-08-06 13:41:20 +09001039
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -03001040 if (!is_label && !elf_sym__filter(&sym))
Namhyung Kime5a18452012-08-06 13:41:20 +09001041 continue;
1042
1043 /* Reject ARM ELF "mapping symbols": these aren't unique and
1044 * don't identify functions, so will confuse the profile
1045 * output: */
Victor Kamensky4886f2c2015-01-26 22:34:01 -08001046 if (ehdr.e_machine == EM_ARM || ehdr.e_machine == EM_AARCH64) {
1047 if (elf_name[0] == '$' && strchr("adtx", elf_name[1])
1048 && (elf_name[2] == '\0' || elf_name[2] == '.'))
Namhyung Kime5a18452012-08-06 13:41:20 +09001049 continue;
1050 }
1051
Cody P Schafer261360b2012-08-10 15:23:01 -07001052 if (runtime_ss->opdsec && sym.st_shndx == runtime_ss->opdidx) {
1053 u32 offset = sym.st_value - syms_ss->opdshdr.sh_addr;
Namhyung Kime5a18452012-08-06 13:41:20 +09001054 u64 *opd = opddata->d_buf + offset;
1055 sym.st_value = DSO__SWAP(dso, u64, *opd);
Cody P Schafer261360b2012-08-10 15:23:01 -07001056 sym.st_shndx = elf_addr_to_index(runtime_ss->elf,
1057 sym.st_value);
1058 used_opd = true;
Namhyung Kime5a18452012-08-06 13:41:20 +09001059 }
Namhyung Kim3843b052012-11-21 13:49:44 +01001060 /*
1061 * When loading symbols in a data mapping, ABS symbols (which
1062 * has a value of SHN_ABS in its st_shndx) failed at
1063 * elf_getscn(). And it marks the loading as a failure so
1064 * already loaded symbols cannot be fixed up.
1065 *
1066 * I'm not sure what should be done. Just ignore them for now.
1067 * - Namhyung Kim
1068 */
1069 if (sym.st_shndx == SHN_ABS)
1070 continue;
Namhyung Kime5a18452012-08-06 13:41:20 +09001071
Cody P Schafer261360b2012-08-10 15:23:01 -07001072 sec = elf_getscn(runtime_ss->elf, sym.st_shndx);
Namhyung Kime5a18452012-08-06 13:41:20 +09001073 if (!sec)
1074 goto out_elf_end;
1075
1076 gelf_getshdr(sec, &shdr);
1077
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -03001078 if (is_label && !elf_sec__filter(&shdr, secstrs))
Namhyung Kime5a18452012-08-06 13:41:20 +09001079 continue;
1080
1081 section_name = elf_sec__name(&shdr, secstrs);
1082
1083 /* On ARM, symbols for thumb functions have 1 added to
1084 * the symbol address as a flag - remove it */
1085 if ((ehdr.e_machine == EM_ARM) &&
Arnaldo Carvalho de Melo18231d72018-04-26 12:45:17 -03001086 (GELF_ST_TYPE(sym.st_info) == STT_FUNC) &&
Namhyung Kime5a18452012-08-06 13:41:20 +09001087 (sym.st_value & 1))
1088 --sym.st_value;
1089
Adrian Hunter39b12f782013-08-07 14:38:47 +03001090 if (dso->kernel || kmodule) {
Arnaldo Carvalho de Melo4e0d1e82018-04-27 15:15:24 -03001091 if (dso__process_kernel_symbol(dso, map, &sym, &shdr, kmaps, kmap, &curr_dso, &curr_map,
1092 section_name, adjust_kernel_syms, kmodule, &remap_kernel))
1093 goto out_elf_end;
Arnaldo Carvalho de Melo857140e2018-04-27 10:53:14 -03001094 } else if ((used_opd && runtime_ss->adjust_symbols) ||
1095 (!used_opd && syms_ss->adjust_symbols)) {
Namhyung Kime5a18452012-08-06 13:41:20 +09001096 pr_debug4("%s: adjusting symbol: st_value: %#" PRIx64 " "
1097 "sh_addr: %#" PRIx64 " sh_offset: %#" PRIx64 "\n", __func__,
1098 (u64)sym.st_value, (u64)shdr.sh_addr,
1099 (u64)shdr.sh_offset);
1100 sym.st_value -= shdr.sh_addr - shdr.sh_offset;
1101 }
Arnaldo Carvalho de Melo4e0d1e82018-04-27 15:15:24 -03001102
Milian Wolff2a8d41b2016-08-30 13:41:02 +02001103 demangled = demangle_sym(dso, kmodule, elf_name);
1104 if (demangled != NULL)
1105 elf_name = demangled;
Namhyung Kime71e7942014-07-31 14:47:42 +09001106
Namhyung Kime5a18452012-08-06 13:41:20 +09001107 f = symbol__new(sym.st_value, sym.st_size,
Arnaldo Carvalho de Meloaf30bff2018-04-26 11:09:10 -03001108 GELF_ST_BIND(sym.st_info),
1109 GELF_ST_TYPE(sym.st_info), elf_name);
Namhyung Kime5a18452012-08-06 13:41:20 +09001110 free(demangled);
1111 if (!f)
1112 goto out_elf_end;
1113
Naveen N. Rao0b3c2262016-04-12 14:40:50 +05301114 arch__sym_update(f, &sym);
1115
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -03001116 __symbols__insert(&curr_dso->symbols, f, dso->kernel);
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03001117 nr++;
Namhyung Kime5a18452012-08-06 13:41:20 +09001118 }
1119
1120 /*
1121 * For misannotated, zeroed, ASM function sizes.
1122 */
1123 if (nr > 0) {
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -03001124 symbols__fixup_end(&dso->symbols);
1125 symbols__fixup_duplicate(&dso->symbols);
Namhyung Kime5a18452012-08-06 13:41:20 +09001126 if (kmap) {
1127 /*
1128 * We need to fixup this here too because we create new
1129 * maps here, for things like vsyscall sections.
1130 */
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -03001131 map_groups__fixup_end(kmaps);
Namhyung Kime5a18452012-08-06 13:41:20 +09001132 }
1133 }
1134 err = nr;
1135out_elf_end:
Namhyung Kime5a18452012-08-06 13:41:20 +09001136 return err;
1137}
1138
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001139static int elf_read_maps(Elf *elf, bool exe, mapfn_t mapfn, void *data)
1140{
1141 GElf_Phdr phdr;
1142 size_t i, phdrnum;
1143 int err;
1144 u64 sz;
1145
1146 if (elf_getphdrnum(elf, &phdrnum))
1147 return -1;
1148
1149 for (i = 0; i < phdrnum; i++) {
1150 if (gelf_getphdr(elf, i, &phdr) == NULL)
1151 return -1;
1152 if (phdr.p_type != PT_LOAD)
1153 continue;
1154 if (exe) {
1155 if (!(phdr.p_flags & PF_X))
1156 continue;
1157 } else {
1158 if (!(phdr.p_flags & PF_R))
1159 continue;
1160 }
1161 sz = min(phdr.p_memsz, phdr.p_filesz);
1162 if (!sz)
1163 continue;
1164 err = mapfn(phdr.p_vaddr, sz, phdr.p_offset, data);
1165 if (err)
1166 return err;
1167 }
1168 return 0;
1169}
1170
1171int file__read_maps(int fd, bool exe, mapfn_t mapfn, void *data,
1172 bool *is_64_bit)
1173{
1174 int err;
1175 Elf *elf;
1176
1177 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
1178 if (elf == NULL)
1179 return -1;
1180
1181 if (is_64_bit)
1182 *is_64_bit = (gelf_getclass(elf) == ELFCLASS64);
1183
1184 err = elf_read_maps(elf, exe, mapfn, data);
1185
1186 elf_end(elf);
1187 return err;
1188}
1189
Adrian Hunter2b5b8bb2014-07-22 16:17:59 +03001190enum dso_type dso__type_fd(int fd)
1191{
1192 enum dso_type dso_type = DSO__TYPE_UNKNOWN;
1193 GElf_Ehdr ehdr;
1194 Elf_Kind ek;
1195 Elf *elf;
1196
1197 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
1198 if (elf == NULL)
1199 goto out;
1200
1201 ek = elf_kind(elf);
1202 if (ek != ELF_K_ELF)
1203 goto out_end;
1204
1205 if (gelf_getclass(elf) == ELFCLASS64) {
1206 dso_type = DSO__TYPE_64BIT;
1207 goto out_end;
1208 }
1209
1210 if (gelf_getehdr(elf, &ehdr) == NULL)
1211 goto out_end;
1212
1213 if (ehdr.e_machine == EM_X86_64)
1214 dso_type = DSO__TYPE_X32BIT;
1215 else
1216 dso_type = DSO__TYPE_32BIT;
1217out_end:
1218 elf_end(elf);
1219out:
1220 return dso_type;
1221}
1222
Adrian Hunterafba19d2013-10-09 15:01:12 +03001223static int copy_bytes(int from, off_t from_offs, int to, off_t to_offs, u64 len)
1224{
1225 ssize_t r;
1226 size_t n;
1227 int err = -1;
1228 char *buf = malloc(page_size);
1229
1230 if (buf == NULL)
1231 return -1;
1232
1233 if (lseek(to, to_offs, SEEK_SET) != to_offs)
1234 goto out;
1235
1236 if (lseek(from, from_offs, SEEK_SET) != from_offs)
1237 goto out;
1238
1239 while (len) {
1240 n = page_size;
1241 if (len < n)
1242 n = len;
1243 /* Use read because mmap won't work on proc files */
1244 r = read(from, buf, n);
1245 if (r < 0)
1246 goto out;
1247 if (!r)
1248 break;
1249 n = r;
1250 r = write(to, buf, n);
1251 if (r < 0)
1252 goto out;
1253 if ((size_t)r != n)
1254 goto out;
1255 len -= n;
1256 }
1257
1258 err = 0;
1259out:
1260 free(buf);
1261 return err;
1262}
1263
1264struct kcore {
1265 int fd;
1266 int elfclass;
1267 Elf *elf;
1268 GElf_Ehdr ehdr;
1269};
1270
1271static int kcore__open(struct kcore *kcore, const char *filename)
1272{
1273 GElf_Ehdr *ehdr;
1274
1275 kcore->fd = open(filename, O_RDONLY);
1276 if (kcore->fd == -1)
1277 return -1;
1278
1279 kcore->elf = elf_begin(kcore->fd, ELF_C_READ, NULL);
1280 if (!kcore->elf)
1281 goto out_close;
1282
1283 kcore->elfclass = gelf_getclass(kcore->elf);
1284 if (kcore->elfclass == ELFCLASSNONE)
1285 goto out_end;
1286
1287 ehdr = gelf_getehdr(kcore->elf, &kcore->ehdr);
1288 if (!ehdr)
1289 goto out_end;
1290
1291 return 0;
1292
1293out_end:
1294 elf_end(kcore->elf);
1295out_close:
1296 close(kcore->fd);
1297 return -1;
1298}
1299
1300static int kcore__init(struct kcore *kcore, char *filename, int elfclass,
1301 bool temp)
1302{
Adrian Hunterafba19d2013-10-09 15:01:12 +03001303 kcore->elfclass = elfclass;
1304
1305 if (temp)
1306 kcore->fd = mkstemp(filename);
1307 else
1308 kcore->fd = open(filename, O_WRONLY | O_CREAT | O_EXCL, 0400);
1309 if (kcore->fd == -1)
1310 return -1;
1311
1312 kcore->elf = elf_begin(kcore->fd, ELF_C_WRITE, NULL);
1313 if (!kcore->elf)
1314 goto out_close;
1315
1316 if (!gelf_newehdr(kcore->elf, elfclass))
1317 goto out_end;
1318
Adrian Hunterb5cabbc2015-09-24 13:05:22 +03001319 memset(&kcore->ehdr, 0, sizeof(GElf_Ehdr));
Adrian Hunterafba19d2013-10-09 15:01:12 +03001320
1321 return 0;
1322
1323out_end:
1324 elf_end(kcore->elf);
1325out_close:
1326 close(kcore->fd);
1327 unlink(filename);
1328 return -1;
1329}
1330
1331static void kcore__close(struct kcore *kcore)
1332{
1333 elf_end(kcore->elf);
1334 close(kcore->fd);
1335}
1336
1337static int kcore__copy_hdr(struct kcore *from, struct kcore *to, size_t count)
1338{
1339 GElf_Ehdr *ehdr = &to->ehdr;
1340 GElf_Ehdr *kehdr = &from->ehdr;
1341
1342 memcpy(ehdr->e_ident, kehdr->e_ident, EI_NIDENT);
1343 ehdr->e_type = kehdr->e_type;
1344 ehdr->e_machine = kehdr->e_machine;
1345 ehdr->e_version = kehdr->e_version;
1346 ehdr->e_entry = 0;
1347 ehdr->e_shoff = 0;
1348 ehdr->e_flags = kehdr->e_flags;
1349 ehdr->e_phnum = count;
1350 ehdr->e_shentsize = 0;
1351 ehdr->e_shnum = 0;
1352 ehdr->e_shstrndx = 0;
1353
1354 if (from->elfclass == ELFCLASS32) {
1355 ehdr->e_phoff = sizeof(Elf32_Ehdr);
1356 ehdr->e_ehsize = sizeof(Elf32_Ehdr);
1357 ehdr->e_phentsize = sizeof(Elf32_Phdr);
1358 } else {
1359 ehdr->e_phoff = sizeof(Elf64_Ehdr);
1360 ehdr->e_ehsize = sizeof(Elf64_Ehdr);
1361 ehdr->e_phentsize = sizeof(Elf64_Phdr);
1362 }
1363
1364 if (!gelf_update_ehdr(to->elf, ehdr))
1365 return -1;
1366
1367 if (!gelf_newphdr(to->elf, count))
1368 return -1;
1369
1370 return 0;
1371}
1372
1373static int kcore__add_phdr(struct kcore *kcore, int idx, off_t offset,
1374 u64 addr, u64 len)
1375{
Adrian Hunterb5cabbc2015-09-24 13:05:22 +03001376 GElf_Phdr phdr = {
1377 .p_type = PT_LOAD,
1378 .p_flags = PF_R | PF_W | PF_X,
1379 .p_offset = offset,
1380 .p_vaddr = addr,
1381 .p_paddr = 0,
1382 .p_filesz = len,
1383 .p_memsz = len,
1384 .p_align = page_size,
1385 };
Adrian Hunterafba19d2013-10-09 15:01:12 +03001386
Adrian Hunterb5cabbc2015-09-24 13:05:22 +03001387 if (!gelf_update_phdr(kcore->elf, idx, &phdr))
Adrian Hunterafba19d2013-10-09 15:01:12 +03001388 return -1;
1389
1390 return 0;
1391}
1392
1393static off_t kcore__write(struct kcore *kcore)
1394{
1395 return elf_update(kcore->elf, ELF_C_WRITE);
1396}
1397
Adrian Hunterfc1b6912013-10-14 16:57:29 +03001398struct phdr_data {
1399 off_t offset;
Adrian Hunter15acef62018-05-22 13:54:41 +03001400 off_t rel;
Adrian Hunterfc1b6912013-10-14 16:57:29 +03001401 u64 addr;
1402 u64 len;
Adrian Hunterf6838202018-05-22 13:54:38 +03001403 struct list_head node;
Adrian Hunter22916fd2018-05-22 13:54:45 +03001404 struct phdr_data *remaps;
Adrian Hunterfc1b6912013-10-14 16:57:29 +03001405};
1406
Adrian Huntera1a3a062018-05-22 13:54:44 +03001407struct sym_data {
1408 u64 addr;
1409 struct list_head node;
1410};
1411
Adrian Hunterfc1b6912013-10-14 16:57:29 +03001412struct kcore_copy_info {
1413 u64 stext;
1414 u64 etext;
1415 u64 first_symbol;
1416 u64 last_symbol;
1417 u64 first_module;
1418 u64 last_module_symbol;
Adrian Hunter6e979572018-05-22 13:54:39 +03001419 size_t phnum;
Adrian Hunterf6838202018-05-22 13:54:38 +03001420 struct list_head phdrs;
Adrian Huntera1a3a062018-05-22 13:54:44 +03001421 struct list_head syms;
Adrian Hunterfc1b6912013-10-14 16:57:29 +03001422};
1423
Adrian Hunter15acef62018-05-22 13:54:41 +03001424#define kcore_copy__for_each_phdr(k, p) \
1425 list_for_each_entry((p), &(k)->phdrs, node)
1426
Adrian Hunterb4503cd2018-05-22 13:54:43 +03001427static struct phdr_data *phdr_data__new(u64 addr, u64 len, off_t offset)
1428{
1429 struct phdr_data *p = zalloc(sizeof(*p));
1430
1431 if (p) {
1432 p->addr = addr;
1433 p->len = len;
1434 p->offset = offset;
1435 }
1436
1437 return p;
1438}
1439
1440static struct phdr_data *kcore_copy_info__addnew(struct kcore_copy_info *kci,
1441 u64 addr, u64 len,
1442 off_t offset)
1443{
1444 struct phdr_data *p = phdr_data__new(addr, len, offset);
1445
1446 if (p)
1447 list_add_tail(&p->node, &kci->phdrs);
1448
1449 return p;
1450}
1451
1452static void kcore_copy__free_phdrs(struct kcore_copy_info *kci)
1453{
1454 struct phdr_data *p, *tmp;
1455
1456 list_for_each_entry_safe(p, tmp, &kci->phdrs, node) {
1457 list_del(&p->node);
1458 free(p);
1459 }
1460}
1461
Adrian Huntera1a3a062018-05-22 13:54:44 +03001462static struct sym_data *kcore_copy__new_sym(struct kcore_copy_info *kci,
1463 u64 addr)
1464{
1465 struct sym_data *s = zalloc(sizeof(*s));
1466
1467 if (s) {
1468 s->addr = addr;
1469 list_add_tail(&s->node, &kci->syms);
1470 }
1471
1472 return s;
1473}
1474
1475static void kcore_copy__free_syms(struct kcore_copy_info *kci)
1476{
1477 struct sym_data *s, *tmp;
1478
1479 list_for_each_entry_safe(s, tmp, &kci->syms, node) {
1480 list_del(&s->node);
1481 free(s);
1482 }
1483}
1484
Adrian Hunterfc1b6912013-10-14 16:57:29 +03001485static int kcore_copy__process_kallsyms(void *arg, const char *name, char type,
1486 u64 start)
1487{
1488 struct kcore_copy_info *kci = arg;
1489
Arnaldo Carvalho de Meloe85e0e02018-04-25 17:16:31 -03001490 if (!kallsyms__is_function(type))
Adrian Hunterfc1b6912013-10-14 16:57:29 +03001491 return 0;
1492
1493 if (strchr(name, '[')) {
1494 if (start > kci->last_module_symbol)
1495 kci->last_module_symbol = start;
1496 return 0;
1497 }
1498
1499 if (!kci->first_symbol || start < kci->first_symbol)
1500 kci->first_symbol = start;
1501
1502 if (!kci->last_symbol || start > kci->last_symbol)
1503 kci->last_symbol = start;
1504
1505 if (!strcmp(name, "_stext")) {
1506 kci->stext = start;
1507 return 0;
1508 }
1509
1510 if (!strcmp(name, "_etext")) {
1511 kci->etext = start;
1512 return 0;
1513 }
1514
Adrian Huntera1a3a062018-05-22 13:54:44 +03001515 if (is_entry_trampoline(name) && !kcore_copy__new_sym(kci, start))
1516 return -1;
1517
Adrian Hunterfc1b6912013-10-14 16:57:29 +03001518 return 0;
1519}
1520
1521static int kcore_copy__parse_kallsyms(struct kcore_copy_info *kci,
1522 const char *dir)
1523{
1524 char kallsyms_filename[PATH_MAX];
1525
1526 scnprintf(kallsyms_filename, PATH_MAX, "%s/kallsyms", dir);
1527
1528 if (symbol__restricted_filename(kallsyms_filename, "/proc/kallsyms"))
1529 return -1;
1530
1531 if (kallsyms__parse(kallsyms_filename, kci,
1532 kcore_copy__process_kallsyms) < 0)
1533 return -1;
1534
1535 return 0;
1536}
1537
1538static int kcore_copy__process_modules(void *arg,
1539 const char *name __maybe_unused,
Thomas Richter9ad46522017-08-03 15:49:02 +02001540 u64 start, u64 size __maybe_unused)
Adrian Hunterfc1b6912013-10-14 16:57:29 +03001541{
1542 struct kcore_copy_info *kci = arg;
1543
1544 if (!kci->first_module || start < kci->first_module)
1545 kci->first_module = start;
1546
1547 return 0;
1548}
1549
1550static int kcore_copy__parse_modules(struct kcore_copy_info *kci,
1551 const char *dir)
1552{
1553 char modules_filename[PATH_MAX];
1554
1555 scnprintf(modules_filename, PATH_MAX, "%s/modules", dir);
1556
1557 if (symbol__restricted_filename(modules_filename, "/proc/modules"))
1558 return -1;
1559
1560 if (modules__parse(modules_filename, kci,
1561 kcore_copy__process_modules) < 0)
1562 return -1;
1563
1564 return 0;
1565}
1566
Adrian Hunterb4503cd2018-05-22 13:54:43 +03001567static int kcore_copy__map(struct kcore_copy_info *kci, u64 start, u64 end,
1568 u64 pgoff, u64 s, u64 e)
Adrian Hunterfc1b6912013-10-14 16:57:29 +03001569{
Adrian Hunterb4503cd2018-05-22 13:54:43 +03001570 u64 len, offset;
Adrian Hunterfc1b6912013-10-14 16:57:29 +03001571
Adrian Hunterb4503cd2018-05-22 13:54:43 +03001572 if (s < start || s >= end)
1573 return 0;
1574
1575 offset = (s - start) + pgoff;
1576 len = e < end ? e - s : end - s;
1577
1578 return kcore_copy_info__addnew(kci, s, len, offset) ? 0 : -1;
Adrian Hunterfc1b6912013-10-14 16:57:29 +03001579}
1580
1581static int kcore_copy__read_map(u64 start, u64 len, u64 pgoff, void *data)
1582{
1583 struct kcore_copy_info *kci = data;
1584 u64 end = start + len;
Adrian Huntera1a3a062018-05-22 13:54:44 +03001585 struct sym_data *sdat;
Adrian Hunterfc1b6912013-10-14 16:57:29 +03001586
Adrian Hunterb4503cd2018-05-22 13:54:43 +03001587 if (kcore_copy__map(kci, start, end, pgoff, kci->stext, kci->etext))
1588 return -1;
Adrian Hunterfc1b6912013-10-14 16:57:29 +03001589
Adrian Hunterb4503cd2018-05-22 13:54:43 +03001590 if (kcore_copy__map(kci, start, end, pgoff, kci->first_module,
1591 kci->last_module_symbol))
1592 return -1;
Adrian Hunterfc1b6912013-10-14 16:57:29 +03001593
Adrian Huntera1a3a062018-05-22 13:54:44 +03001594 list_for_each_entry(sdat, &kci->syms, node) {
1595 u64 s = round_down(sdat->addr, page_size);
1596
1597 if (kcore_copy__map(kci, start, end, pgoff, s, s + len))
1598 return -1;
1599 }
1600
Adrian Hunterfc1b6912013-10-14 16:57:29 +03001601 return 0;
1602}
1603
1604static int kcore_copy__read_maps(struct kcore_copy_info *kci, Elf *elf)
1605{
1606 if (elf_read_maps(elf, true, kcore_copy__read_map, kci) < 0)
1607 return -1;
1608
1609 return 0;
1610}
1611
Adrian Hunter22916fd2018-05-22 13:54:45 +03001612static void kcore_copy__find_remaps(struct kcore_copy_info *kci)
1613{
1614 struct phdr_data *p, *k = NULL;
1615 u64 kend;
1616
1617 if (!kci->stext)
1618 return;
1619
1620 /* Find phdr that corresponds to the kernel map (contains stext) */
1621 kcore_copy__for_each_phdr(kci, p) {
1622 u64 pend = p->addr + p->len - 1;
1623
1624 if (p->addr <= kci->stext && pend >= kci->stext) {
1625 k = p;
1626 break;
1627 }
1628 }
1629
1630 if (!k)
1631 return;
1632
1633 kend = k->offset + k->len;
1634
1635 /* Find phdrs that remap the kernel */
1636 kcore_copy__for_each_phdr(kci, p) {
1637 u64 pend = p->offset + p->len;
1638
1639 if (p == k)
1640 continue;
1641
1642 if (p->offset >= k->offset && pend <= kend)
1643 p->remaps = k;
1644 }
1645}
1646
Adrian Hunter15acef62018-05-22 13:54:41 +03001647static void kcore_copy__layout(struct kcore_copy_info *kci)
1648{
1649 struct phdr_data *p;
1650 off_t rel = 0;
1651
Adrian Hunter22916fd2018-05-22 13:54:45 +03001652 kcore_copy__find_remaps(kci);
1653
Adrian Hunter15acef62018-05-22 13:54:41 +03001654 kcore_copy__for_each_phdr(kci, p) {
Adrian Hunter22916fd2018-05-22 13:54:45 +03001655 if (!p->remaps) {
1656 p->rel = rel;
1657 rel += p->len;
1658 }
Adrian Hunter15acef62018-05-22 13:54:41 +03001659 kci->phnum += 1;
1660 }
Adrian Hunter22916fd2018-05-22 13:54:45 +03001661
1662 kcore_copy__for_each_phdr(kci, p) {
1663 struct phdr_data *k = p->remaps;
1664
1665 if (k)
1666 p->rel = p->offset - k->offset + k->rel;
1667 }
Adrian Hunter15acef62018-05-22 13:54:41 +03001668}
1669
Adrian Hunterfc1b6912013-10-14 16:57:29 +03001670static int kcore_copy__calc_maps(struct kcore_copy_info *kci, const char *dir,
1671 Elf *elf)
1672{
1673 if (kcore_copy__parse_kallsyms(kci, dir))
1674 return -1;
1675
1676 if (kcore_copy__parse_modules(kci, dir))
1677 return -1;
1678
1679 if (kci->stext)
1680 kci->stext = round_down(kci->stext, page_size);
1681 else
1682 kci->stext = round_down(kci->first_symbol, page_size);
1683
1684 if (kci->etext) {
1685 kci->etext = round_up(kci->etext, page_size);
1686 } else if (kci->last_symbol) {
1687 kci->etext = round_up(kci->last_symbol, page_size);
1688 kci->etext += page_size;
1689 }
1690
1691 kci->first_module = round_down(kci->first_module, page_size);
1692
1693 if (kci->last_module_symbol) {
1694 kci->last_module_symbol = round_up(kci->last_module_symbol,
1695 page_size);
1696 kci->last_module_symbol += page_size;
1697 }
1698
1699 if (!kci->stext || !kci->etext)
1700 return -1;
1701
1702 if (kci->first_module && !kci->last_module_symbol)
1703 return -1;
1704
Adrian Hunter15acef62018-05-22 13:54:41 +03001705 if (kcore_copy__read_maps(kci, elf))
1706 return -1;
1707
1708 kcore_copy__layout(kci);
1709
1710 return 0;
Adrian Hunterfc1b6912013-10-14 16:57:29 +03001711}
1712
1713static int kcore_copy__copy_file(const char *from_dir, const char *to_dir,
1714 const char *name)
1715{
1716 char from_filename[PATH_MAX];
1717 char to_filename[PATH_MAX];
1718
1719 scnprintf(from_filename, PATH_MAX, "%s/%s", from_dir, name);
1720 scnprintf(to_filename, PATH_MAX, "%s/%s", to_dir, name);
1721
1722 return copyfile_mode(from_filename, to_filename, 0400);
1723}
1724
1725static int kcore_copy__unlink(const char *dir, const char *name)
1726{
1727 char filename[PATH_MAX];
1728
1729 scnprintf(filename, PATH_MAX, "%s/%s", dir, name);
1730
1731 return unlink(filename);
1732}
1733
1734static int kcore_copy__compare_fds(int from, int to)
1735{
1736 char *buf_from;
1737 char *buf_to;
1738 ssize_t ret;
1739 size_t len;
1740 int err = -1;
1741
1742 buf_from = malloc(page_size);
1743 buf_to = malloc(page_size);
1744 if (!buf_from || !buf_to)
1745 goto out;
1746
1747 while (1) {
1748 /* Use read because mmap won't work on proc files */
1749 ret = read(from, buf_from, page_size);
1750 if (ret < 0)
1751 goto out;
1752
1753 if (!ret)
1754 break;
1755
1756 len = ret;
1757
1758 if (readn(to, buf_to, len) != (int)len)
1759 goto out;
1760
1761 if (memcmp(buf_from, buf_to, len))
1762 goto out;
1763 }
1764
1765 err = 0;
1766out:
1767 free(buf_to);
1768 free(buf_from);
1769 return err;
1770}
1771
1772static int kcore_copy__compare_files(const char *from_filename,
1773 const char *to_filename)
1774{
1775 int from, to, err = -1;
1776
1777 from = open(from_filename, O_RDONLY);
1778 if (from < 0)
1779 return -1;
1780
1781 to = open(to_filename, O_RDONLY);
1782 if (to < 0)
1783 goto out_close_from;
1784
1785 err = kcore_copy__compare_fds(from, to);
1786
1787 close(to);
1788out_close_from:
1789 close(from);
1790 return err;
1791}
1792
1793static int kcore_copy__compare_file(const char *from_dir, const char *to_dir,
1794 const char *name)
1795{
1796 char from_filename[PATH_MAX];
1797 char to_filename[PATH_MAX];
1798
1799 scnprintf(from_filename, PATH_MAX, "%s/%s", from_dir, name);
1800 scnprintf(to_filename, PATH_MAX, "%s/%s", to_dir, name);
1801
1802 return kcore_copy__compare_files(from_filename, to_filename);
1803}
1804
1805/**
1806 * kcore_copy - copy kallsyms, modules and kcore from one directory to another.
1807 * @from_dir: from directory
1808 * @to_dir: to directory
1809 *
1810 * This function copies kallsyms, modules and kcore files from one directory to
1811 * another. kallsyms and modules are copied entirely. Only code segments are
1812 * copied from kcore. It is assumed that two segments suffice: one for the
1813 * kernel proper and one for all the modules. The code segments are determined
1814 * from kallsyms and modules files. The kernel map starts at _stext or the
1815 * lowest function symbol, and ends at _etext or the highest function symbol.
1816 * The module map starts at the lowest module address and ends at the highest
1817 * module symbol. Start addresses are rounded down to the nearest page. End
1818 * addresses are rounded up to the nearest page. An extra page is added to the
1819 * highest kernel symbol and highest module symbol to, hopefully, encompass that
1820 * symbol too. Because it contains only code sections, the resulting kcore is
1821 * unusual. One significant peculiarity is that the mapping (start -> pgoff)
1822 * is not the same for the kernel map and the modules map. That happens because
1823 * the data is copied adjacently whereas the original kcore has gaps. Finally,
1824 * kallsyms and modules files are compared with their copies to check that
1825 * modules have not been loaded or unloaded while the copies were taking place.
1826 *
1827 * Return: %0 on success, %-1 on failure.
1828 */
1829int kcore_copy(const char *from_dir, const char *to_dir)
1830{
1831 struct kcore kcore;
1832 struct kcore extract;
Adrian Hunterfc1b6912013-10-14 16:57:29 +03001833 int idx = 0, err = -1;
Adrian Hunterd2c95982018-05-22 13:54:42 +03001834 off_t offset, sz;
Adrian Hunterfc1b6912013-10-14 16:57:29 +03001835 struct kcore_copy_info kci = { .stext = 0, };
1836 char kcore_filename[PATH_MAX];
1837 char extract_filename[PATH_MAX];
Adrian Hunterd2c95982018-05-22 13:54:42 +03001838 struct phdr_data *p;
Adrian Hunterfc1b6912013-10-14 16:57:29 +03001839
Adrian Hunterf6838202018-05-22 13:54:38 +03001840 INIT_LIST_HEAD(&kci.phdrs);
Adrian Huntera1a3a062018-05-22 13:54:44 +03001841 INIT_LIST_HEAD(&kci.syms);
Adrian Hunterf6838202018-05-22 13:54:38 +03001842
Adrian Hunterfc1b6912013-10-14 16:57:29 +03001843 if (kcore_copy__copy_file(from_dir, to_dir, "kallsyms"))
1844 return -1;
1845
1846 if (kcore_copy__copy_file(from_dir, to_dir, "modules"))
1847 goto out_unlink_kallsyms;
1848
1849 scnprintf(kcore_filename, PATH_MAX, "%s/kcore", from_dir);
1850 scnprintf(extract_filename, PATH_MAX, "%s/kcore", to_dir);
1851
1852 if (kcore__open(&kcore, kcore_filename))
1853 goto out_unlink_modules;
1854
1855 if (kcore_copy__calc_maps(&kci, from_dir, kcore.elf))
1856 goto out_kcore_close;
1857
1858 if (kcore__init(&extract, extract_filename, kcore.elfclass, false))
1859 goto out_kcore_close;
1860
Adrian Hunter6e979572018-05-22 13:54:39 +03001861 if (kcore__copy_hdr(&kcore, &extract, kci.phnum))
Adrian Hunterfc1b6912013-10-14 16:57:29 +03001862 goto out_extract_close;
1863
Adrian Hunterc9dd1d82018-05-22 13:54:40 +03001864 offset = gelf_fsize(extract.elf, ELF_T_EHDR, 1, EV_CURRENT) +
1865 gelf_fsize(extract.elf, ELF_T_PHDR, kci.phnum, EV_CURRENT);
1866 offset = round_up(offset, page_size);
1867
Adrian Hunterd2c95982018-05-22 13:54:42 +03001868 kcore_copy__for_each_phdr(&kci, p) {
1869 off_t offs = p->rel + offset;
Adrian Hunterfc1b6912013-10-14 16:57:29 +03001870
Adrian Hunterd2c95982018-05-22 13:54:42 +03001871 if (kcore__add_phdr(&extract, idx++, offs, p->addr, p->len))
Adrian Hunterfc1b6912013-10-14 16:57:29 +03001872 goto out_extract_close;
1873 }
1874
1875 sz = kcore__write(&extract);
1876 if (sz < 0 || sz > offset)
1877 goto out_extract_close;
1878
Adrian Hunterd2c95982018-05-22 13:54:42 +03001879 kcore_copy__for_each_phdr(&kci, p) {
1880 off_t offs = p->rel + offset;
Adrian Hunterfc1b6912013-10-14 16:57:29 +03001881
Adrian Hunter22916fd2018-05-22 13:54:45 +03001882 if (p->remaps)
1883 continue;
Adrian Hunterd2c95982018-05-22 13:54:42 +03001884 if (copy_bytes(kcore.fd, p->offset, extract.fd, offs, p->len))
1885 goto out_extract_close;
1886 }
Adrian Hunterfc1b6912013-10-14 16:57:29 +03001887
1888 if (kcore_copy__compare_file(from_dir, to_dir, "modules"))
1889 goto out_extract_close;
1890
1891 if (kcore_copy__compare_file(from_dir, to_dir, "kallsyms"))
1892 goto out_extract_close;
1893
1894 err = 0;
1895
1896out_extract_close:
1897 kcore__close(&extract);
1898 if (err)
1899 unlink(extract_filename);
1900out_kcore_close:
1901 kcore__close(&kcore);
1902out_unlink_modules:
1903 if (err)
1904 kcore_copy__unlink(to_dir, "modules");
1905out_unlink_kallsyms:
1906 if (err)
1907 kcore_copy__unlink(to_dir, "kallsyms");
1908
Adrian Hunterb4503cd2018-05-22 13:54:43 +03001909 kcore_copy__free_phdrs(&kci);
Adrian Huntera1a3a062018-05-22 13:54:44 +03001910 kcore_copy__free_syms(&kci);
Adrian Hunterb4503cd2018-05-22 13:54:43 +03001911
Adrian Hunterfc1b6912013-10-14 16:57:29 +03001912 return err;
1913}
1914
Adrian Hunterafba19d2013-10-09 15:01:12 +03001915int kcore_extract__create(struct kcore_extract *kce)
1916{
1917 struct kcore kcore;
1918 struct kcore extract;
1919 size_t count = 1;
1920 int idx = 0, err = -1;
1921 off_t offset = page_size, sz;
1922
1923 if (kcore__open(&kcore, kce->kcore_filename))
1924 return -1;
1925
1926 strcpy(kce->extract_filename, PERF_KCORE_EXTRACT);
1927 if (kcore__init(&extract, kce->extract_filename, kcore.elfclass, true))
1928 goto out_kcore_close;
1929
1930 if (kcore__copy_hdr(&kcore, &extract, count))
1931 goto out_extract_close;
1932
1933 if (kcore__add_phdr(&extract, idx, offset, kce->addr, kce->len))
1934 goto out_extract_close;
1935
1936 sz = kcore__write(&extract);
1937 if (sz < 0 || sz > offset)
1938 goto out_extract_close;
1939
1940 if (copy_bytes(kcore.fd, kce->offs, extract.fd, offset, kce->len))
1941 goto out_extract_close;
1942
1943 err = 0;
1944
1945out_extract_close:
1946 kcore__close(&extract);
1947 if (err)
1948 unlink(kce->extract_filename);
1949out_kcore_close:
1950 kcore__close(&kcore);
1951
1952 return err;
1953}
1954
1955void kcore_extract__delete(struct kcore_extract *kce)
1956{
1957 unlink(kce->extract_filename);
1958}
1959
Arnaldo Carvalho de Melo1c1a3a42016-07-12 12:19:09 -03001960#ifdef HAVE_GELF_GETNOTE_SUPPORT
Ravi Bangoria5a5e3d32018-08-20 10:12:50 +05301961
1962static void sdt_adjust_loc(struct sdt_note *tmp, GElf_Addr base_off)
1963{
1964 if (!base_off)
1965 return;
1966
1967 if (tmp->bit32)
1968 tmp->addr.a32[SDT_NOTE_IDX_LOC] =
1969 tmp->addr.a32[SDT_NOTE_IDX_LOC] + base_off -
1970 tmp->addr.a32[SDT_NOTE_IDX_BASE];
1971 else
1972 tmp->addr.a64[SDT_NOTE_IDX_LOC] =
1973 tmp->addr.a64[SDT_NOTE_IDX_LOC] + base_off -
1974 tmp->addr.a64[SDT_NOTE_IDX_BASE];
1975}
1976
1977static void sdt_adjust_refctr(struct sdt_note *tmp, GElf_Addr base_addr,
1978 GElf_Addr base_off)
1979{
1980 if (!base_off)
1981 return;
1982
1983 if (tmp->bit32 && tmp->addr.a32[SDT_NOTE_IDX_REFCTR])
1984 tmp->addr.a32[SDT_NOTE_IDX_REFCTR] -= (base_addr - base_off);
1985 else if (tmp->addr.a64[SDT_NOTE_IDX_REFCTR])
1986 tmp->addr.a64[SDT_NOTE_IDX_REFCTR] -= (base_addr - base_off);
1987}
1988
Hemant Kumar060fa0c2016-07-01 17:03:46 +09001989/**
1990 * populate_sdt_note : Parse raw data and identify SDT note
1991 * @elf: elf of the opened file
1992 * @data: raw data of a section with description offset applied
1993 * @len: note description size
1994 * @type: type of the note
1995 * @sdt_notes: List to add the SDT note
1996 *
1997 * Responsible for parsing the @data in section .note.stapsdt in @elf and
1998 * if its an SDT note, it appends to @sdt_notes list.
1999 */
2000static int populate_sdt_note(Elf **elf, const char *data, size_t len,
2001 struct list_head *sdt_notes)
2002{
Alexis Berlemontbe881842016-12-14 01:07:31 +01002003 const char *provider, *name, *args;
Hemant Kumar060fa0c2016-07-01 17:03:46 +09002004 struct sdt_note *tmp = NULL;
2005 GElf_Ehdr ehdr;
Hemant Kumar060fa0c2016-07-01 17:03:46 +09002006 GElf_Shdr shdr;
2007 int ret = -EINVAL;
2008
2009 union {
2010 Elf64_Addr a64[NR_ADDR];
2011 Elf32_Addr a32[NR_ADDR];
2012 } buf;
2013
2014 Elf_Data dst = {
2015 .d_buf = &buf, .d_type = ELF_T_ADDR, .d_version = EV_CURRENT,
2016 .d_size = gelf_fsize((*elf), ELF_T_ADDR, NR_ADDR, EV_CURRENT),
2017 .d_off = 0, .d_align = 0
2018 };
2019 Elf_Data src = {
2020 .d_buf = (void *) data, .d_type = ELF_T_ADDR,
2021 .d_version = EV_CURRENT, .d_size = dst.d_size, .d_off = 0,
2022 .d_align = 0
2023 };
2024
2025 tmp = (struct sdt_note *)calloc(1, sizeof(struct sdt_note));
2026 if (!tmp) {
2027 ret = -ENOMEM;
2028 goto out_err;
2029 }
2030
2031 INIT_LIST_HEAD(&tmp->note_list);
2032
2033 if (len < dst.d_size + 3)
2034 goto out_free_note;
2035
2036 /* Translation from file representation to memory representation */
2037 if (gelf_xlatetom(*elf, &dst, &src,
2038 elf_getident(*elf, NULL)[EI_DATA]) == NULL) {
2039 pr_err("gelf_xlatetom : %s\n", elf_errmsg(-1));
2040 goto out_free_note;
2041 }
2042
2043 /* Populate the fields of sdt_note */
2044 provider = data + dst.d_size;
2045
2046 name = (const char *)memchr(provider, '\0', data + len - provider);
2047 if (name++ == NULL)
2048 goto out_free_note;
2049
2050 tmp->provider = strdup(provider);
2051 if (!tmp->provider) {
2052 ret = -ENOMEM;
2053 goto out_free_note;
2054 }
2055 tmp->name = strdup(name);
2056 if (!tmp->name) {
2057 ret = -ENOMEM;
2058 goto out_free_prov;
2059 }
2060
Alexis Berlemontbe881842016-12-14 01:07:31 +01002061 args = memchr(name, '\0', data + len - name);
2062
2063 /*
2064 * There is no argument if:
2065 * - We reached the end of the note;
2066 * - There is not enough room to hold a potential string;
2067 * - The argument string is empty or just contains ':'.
2068 */
2069 if (args == NULL || data + len - args < 2 ||
2070 args[1] == ':' || args[1] == '\0')
2071 tmp->args = NULL;
2072 else {
2073 tmp->args = strdup(++args);
2074 if (!tmp->args) {
2075 ret = -ENOMEM;
2076 goto out_free_name;
2077 }
2078 }
2079
Hemant Kumar060fa0c2016-07-01 17:03:46 +09002080 if (gelf_getclass(*elf) == ELFCLASS32) {
2081 memcpy(&tmp->addr, &buf, 3 * sizeof(Elf32_Addr));
2082 tmp->bit32 = true;
2083 } else {
2084 memcpy(&tmp->addr, &buf, 3 * sizeof(Elf64_Addr));
2085 tmp->bit32 = false;
2086 }
2087
2088 if (!gelf_getehdr(*elf, &ehdr)) {
2089 pr_debug("%s : cannot get elf header.\n", __func__);
2090 ret = -EBADF;
Alexis Berlemontbe881842016-12-14 01:07:31 +01002091 goto out_free_args;
Hemant Kumar060fa0c2016-07-01 17:03:46 +09002092 }
2093
2094 /* Adjust the prelink effect :
2095 * Find out the .stapsdt.base section.
2096 * This scn will help us to handle prelinking (if present).
2097 * Compare the retrieved file offset of the base section with the
2098 * base address in the description of the SDT note. If its different,
2099 * then accordingly, adjust the note location.
2100 */
Ravi Bangoria5a5e3d32018-08-20 10:12:50 +05302101 if (elf_section_by_name(*elf, &ehdr, &shdr, SDT_BASE_SCN, NULL))
2102 sdt_adjust_loc(tmp, shdr.sh_offset);
2103
2104 /* Adjust reference counter offset */
2105 if (elf_section_by_name(*elf, &ehdr, &shdr, SDT_PROBES_SCN, NULL))
2106 sdt_adjust_refctr(tmp, shdr.sh_addr, shdr.sh_offset);
Hemant Kumar060fa0c2016-07-01 17:03:46 +09002107
2108 list_add_tail(&tmp->note_list, sdt_notes);
2109 return 0;
2110
Alexis Berlemontbe881842016-12-14 01:07:31 +01002111out_free_args:
2112 free(tmp->args);
Hemant Kumar060fa0c2016-07-01 17:03:46 +09002113out_free_name:
2114 free(tmp->name);
2115out_free_prov:
2116 free(tmp->provider);
2117out_free_note:
2118 free(tmp);
2119out_err:
2120 return ret;
2121}
2122
2123/**
2124 * construct_sdt_notes_list : constructs a list of SDT notes
2125 * @elf : elf to look into
2126 * @sdt_notes : empty list_head
2127 *
2128 * Scans the sections in 'elf' for the section
2129 * .note.stapsdt. It, then calls populate_sdt_note to find
2130 * out the SDT events and populates the 'sdt_notes'.
2131 */
2132static int construct_sdt_notes_list(Elf *elf, struct list_head *sdt_notes)
2133{
2134 GElf_Ehdr ehdr;
2135 Elf_Scn *scn = NULL;
2136 Elf_Data *data;
2137 GElf_Shdr shdr;
2138 size_t shstrndx, next;
2139 GElf_Nhdr nhdr;
2140 size_t name_off, desc_off, offset;
2141 int ret = 0;
2142
2143 if (gelf_getehdr(elf, &ehdr) == NULL) {
2144 ret = -EBADF;
2145 goto out_ret;
2146 }
2147 if (elf_getshdrstrndx(elf, &shstrndx) != 0) {
2148 ret = -EBADF;
2149 goto out_ret;
2150 }
2151
2152 /* Look for the required section */
2153 scn = elf_section_by_name(elf, &ehdr, &shdr, SDT_NOTE_SCN, NULL);
2154 if (!scn) {
2155 ret = -ENOENT;
2156 goto out_ret;
2157 }
2158
2159 if ((shdr.sh_type != SHT_NOTE) || (shdr.sh_flags & SHF_ALLOC)) {
2160 ret = -ENOENT;
2161 goto out_ret;
2162 }
2163
2164 data = elf_getdata(scn, NULL);
2165
2166 /* Get the SDT notes */
2167 for (offset = 0; (next = gelf_getnote(data, offset, &nhdr, &name_off,
2168 &desc_off)) > 0; offset = next) {
2169 if (nhdr.n_namesz == sizeof(SDT_NOTE_NAME) &&
2170 !memcmp(data->d_buf + name_off, SDT_NOTE_NAME,
2171 sizeof(SDT_NOTE_NAME))) {
2172 /* Check the type of the note */
2173 if (nhdr.n_type != SDT_NOTE_TYPE)
2174 goto out_ret;
2175
2176 ret = populate_sdt_note(&elf, ((data->d_buf) + desc_off),
2177 nhdr.n_descsz, sdt_notes);
2178 if (ret < 0)
2179 goto out_ret;
2180 }
2181 }
2182 if (list_empty(sdt_notes))
2183 ret = -ENOENT;
2184
2185out_ret:
2186 return ret;
2187}
2188
2189/**
2190 * get_sdt_note_list : Wrapper to construct a list of sdt notes
2191 * @head : empty list_head
2192 * @target : file to find SDT notes from
2193 *
2194 * This opens the file, initializes
2195 * the ELF and then calls construct_sdt_notes_list.
2196 */
2197int get_sdt_note_list(struct list_head *head, const char *target)
2198{
2199 Elf *elf;
2200 int fd, ret;
2201
2202 fd = open(target, O_RDONLY);
2203 if (fd < 0)
2204 return -EBADF;
2205
2206 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
2207 if (!elf) {
2208 ret = -EBADF;
2209 goto out_close;
2210 }
2211 ret = construct_sdt_notes_list(elf, head);
2212 elf_end(elf);
2213out_close:
2214 close(fd);
2215 return ret;
2216}
2217
2218/**
2219 * cleanup_sdt_note_list : free the sdt notes' list
2220 * @sdt_notes: sdt notes' list
2221 *
2222 * Free up the SDT notes in @sdt_notes.
2223 * Returns the number of SDT notes free'd.
2224 */
2225int cleanup_sdt_note_list(struct list_head *sdt_notes)
2226{
2227 struct sdt_note *tmp, *pos;
2228 int nr_free = 0;
2229
2230 list_for_each_entry_safe(pos, tmp, sdt_notes, note_list) {
2231 list_del(&pos->note_list);
2232 free(pos->name);
2233 free(pos->provider);
2234 free(pos);
2235 nr_free++;
2236 }
2237 return nr_free;
2238}
2239
2240/**
2241 * sdt_notes__get_count: Counts the number of sdt events
2242 * @start: list_head to sdt_notes list
2243 *
2244 * Returns the number of SDT notes in a list
2245 */
2246int sdt_notes__get_count(struct list_head *start)
2247{
2248 struct sdt_note *sdt_ptr;
2249 int count = 0;
2250
2251 list_for_each_entry(sdt_ptr, start, note_list)
2252 count++;
2253 return count;
2254}
Arnaldo Carvalho de Melo1c1a3a42016-07-12 12:19:09 -03002255#endif
Hemant Kumar060fa0c2016-07-01 17:03:46 +09002256
Namhyung Kime5a18452012-08-06 13:41:20 +09002257void symbol__elf_init(void)
2258{
2259 elf_version(EV_CURRENT);
2260}