blob: 94a156df22d5a8d3ea0aa8fd1c01991f72c26faa [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>
Arnaldo Carvalho de Melo215a0d32019-07-04 11:21:24 -03005#include <stdlib.h>
Namhyung Kime5a18452012-08-06 13:41:20 +09006#include <string.h>
7#include <unistd.h>
8#include <inttypes.h>
9
Arnaldo Carvalho de Melo09aa3b02019-09-10 16:17:19 +010010#include "dso.h"
Arnaldo Carvalho de Melo1101f692019-01-27 13:42:37 +010011#include "map.h"
Arnaldo Carvalho de Meloc54d2412019-11-25 22:24:10 -030012#include "maps.h"
Namhyung Kime5a18452012-08-06 13:41:20 +090013#include "symbol.h"
Arnaldo Carvalho de Melob1d1b092019-08-30 10:26:37 -030014#include "symsrc.h"
Stephane Eraniane9c4bcd2015-11-30 10:02:20 +010015#include "demangle-java.h"
David Tolnaycae15db2016-07-09 00:20:00 -070016#include "demangle-rust.h"
Waiman Long8fa7d872014-09-29 16:07:28 -040017#include "machine.h"
Vladimir Nikulichev922d0e42014-04-17 08:27:01 -070018#include "vdso.h"
Namhyung Kime5a18452012-08-06 13:41:20 +090019#include "debug.h"
Arnaldo Carvalho de Melo32ff3fe2019-09-24 15:14:12 -030020#include "util/copyfile.h"
Arnaldo Carvalho de Melo3052ba52019-06-25 17:27:31 -030021#include <linux/ctype.h>
Arnaldo Carvalho de Melofb71c86c2019-09-03 10:56:06 -030022#include <linux/kernel.h>
Arnaldo Carvalho de Melo7f7c5362019-07-04 11:32:27 -030023#include <linux/zalloc.h>
Arnaldo Carvalho de Melo3d689ed2017-04-17 16:10:49 -030024#include <symbol/kallsyms.h>
Arnaldo Carvalho de Melofb71c86c2019-09-03 10:56:06 -030025#include <internal/lib.h>
Namhyung Kime5a18452012-08-06 13:41:20 +090026
David Aherne370a3d2015-02-18 19:33:37 -050027#ifndef EM_AARCH64
28#define EM_AARCH64 183 /* ARM 64 bit */
29#endif
30
Arnaldo Carvalho de Melo843cf702019-02-04 15:48:03 -030031#ifndef ELF32_ST_VISIBILITY
32#define ELF32_ST_VISIBILITY(o) ((o) & 0x03)
33#endif
34
35/* For ELF64 the definitions are the same. */
36#ifndef ELF64_ST_VISIBILITY
37#define ELF64_ST_VISIBILITY(o) ELF32_ST_VISIBILITY (o)
38#endif
39
40/* How to extract information held in the st_other field. */
41#ifndef GELF_ST_VISIBILITY
42#define GELF_ST_VISIBILITY(val) ELF64_ST_VISIBILITY (val)
43#endif
44
Arnaldo Carvalho de Melocc310782016-07-12 11:04:13 -030045typedef Elf64_Nhdr GElf_Nhdr;
David Aherne370a3d2015-02-18 19:33:37 -050046
Arnaldo Carvalho de Melo9bea81b2019-08-30 10:01:50 -030047#ifndef DMGL_PARAMS
48#define DMGL_NO_OPTS 0 /* For readability... */
49#define DMGL_PARAMS (1 << 0) /* Include function args */
50#define DMGL_ANSI (1 << 1) /* Include const, volatile, etc */
51#endif
52
Remi Bernonba0509d2020-08-21 18:52:36 +020053#ifdef HAVE_LIBBFD_SUPPORT
54#define PACKAGE 'perf'
55#include <bfd.h>
56#else
Arnaldo Carvalho de Meloaaba4e12014-11-24 17:10:52 -030057#ifdef HAVE_CPLUS_DEMANGLE_SUPPORT
58extern char *cplus_demangle(const char *, int);
59
60static inline char *bfd_demangle(void __maybe_unused *v, const char *c, int i)
61{
62 return cplus_demangle(c, i);
63}
64#else
65#ifdef NO_DEMANGLE
66static inline char *bfd_demangle(void __maybe_unused *v,
67 const char __maybe_unused *c,
68 int __maybe_unused i)
69{
70 return NULL;
71}
Remi Bernonba0509d2020-08-21 18:52:36 +020072#endif
Arnaldo Carvalho de Meloaaba4e12014-11-24 17:10:52 -030073#endif
74#endif
75
Ingo Molnar89fe8082013-09-30 12:07:11 +020076#ifndef HAVE_ELF_GETPHDRNUM_SUPPORT
Arnaldo Carvalho de Melo179f36d2015-09-17 11:30:20 -030077static int elf_getphdrnum(Elf *elf, size_t *dst)
Adrian Huntere955d5c2013-09-13 16:49:30 +030078{
79 GElf_Ehdr gehdr;
80 GElf_Ehdr *ehdr;
81
82 ehdr = gelf_getehdr(elf, &gehdr);
83 if (!ehdr)
84 return -1;
85
86 *dst = ehdr->e_phnum;
87
88 return 0;
89}
90#endif
91
Arnaldo Carvalho de Melo2492c462016-07-04 19:35:47 -030092#ifndef HAVE_ELF_GETSHDRSTRNDX_SUPPORT
93static int elf_getshdrstrndx(Elf *elf __maybe_unused, size_t *dst __maybe_unused)
94{
95 pr_err("%s: update your libelf to > 0.140, this one lacks elf_getshdrstrndx().\n", __func__);
96 return -1;
97}
98#endif
99
Namhyung Kime5a18452012-08-06 13:41:20 +0900100#ifndef NT_GNU_BUILD_ID
101#define NT_GNU_BUILD_ID 3
102#endif
103
104/**
105 * elf_symtab__for_each_symbol - iterate thru all the symbols
106 *
107 * @syms: struct elf_symtab instance to iterate
108 * @idx: uint32_t idx
109 * @sym: GElf_Sym iterator
110 */
111#define elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) \
112 for (idx = 0, gelf_getsym(syms, idx, &sym);\
113 idx < nr_syms; \
114 idx++, gelf_getsym(syms, idx, &sym))
115
116static inline uint8_t elf_sym__type(const GElf_Sym *sym)
117{
118 return GELF_ST_TYPE(sym->st_info);
119}
120
Jiri Olsa59a17702019-01-28 14:35:26 +0100121static inline uint8_t elf_sym__visibility(const GElf_Sym *sym)
122{
123 return GELF_ST_VISIBILITY(sym->st_other);
124}
125
Vinson Lee4e310502015-02-09 16:29:37 -0800126#ifndef STT_GNU_IFUNC
127#define STT_GNU_IFUNC 10
128#endif
129
Namhyung Kime5a18452012-08-06 13:41:20 +0900130static inline int elf_sym__is_function(const GElf_Sym *sym)
131{
Adrian Huntera2f3b6b2014-07-14 13:02:33 +0300132 return (elf_sym__type(sym) == STT_FUNC ||
133 elf_sym__type(sym) == STT_GNU_IFUNC) &&
Namhyung Kime5a18452012-08-06 13:41:20 +0900134 sym->st_name != 0 &&
135 sym->st_shndx != SHN_UNDEF;
136}
137
138static inline bool elf_sym__is_object(const GElf_Sym *sym)
139{
140 return elf_sym__type(sym) == STT_OBJECT &&
141 sym->st_name != 0 &&
142 sym->st_shndx != SHN_UNDEF;
143}
144
145static inline int elf_sym__is_label(const GElf_Sym *sym)
146{
147 return elf_sym__type(sym) == STT_NOTYPE &&
148 sym->st_name != 0 &&
149 sym->st_shndx != SHN_UNDEF &&
Jiri Olsa59a17702019-01-28 14:35:26 +0100150 sym->st_shndx != SHN_ABS &&
151 elf_sym__visibility(sym) != STV_HIDDEN &&
152 elf_sym__visibility(sym) != STV_INTERNAL;
Namhyung Kime5a18452012-08-06 13:41:20 +0900153}
154
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -0300155static bool elf_sym__filter(GElf_Sym *sym)
Namhyung Kime5a18452012-08-06 13:41:20 +0900156{
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -0300157 return elf_sym__is_function(sym) || elf_sym__is_object(sym);
Namhyung Kime5a18452012-08-06 13:41:20 +0900158}
159
160static inline const char *elf_sym__name(const GElf_Sym *sym,
161 const Elf_Data *symstrs)
162{
163 return symstrs->d_buf + sym->st_name;
164}
165
166static inline const char *elf_sec__name(const GElf_Shdr *shdr,
167 const Elf_Data *secstrs)
168{
169 return secstrs->d_buf + shdr->sh_name;
170}
171
172static inline int elf_sec__is_text(const GElf_Shdr *shdr,
173 const Elf_Data *secstrs)
174{
175 return strstr(elf_sec__name(shdr, secstrs), "text") != NULL;
176}
177
178static inline bool elf_sec__is_data(const GElf_Shdr *shdr,
179 const Elf_Data *secstrs)
180{
181 return strstr(elf_sec__name(shdr, secstrs), "data") != NULL;
182}
183
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -0300184static bool elf_sec__filter(GElf_Shdr *shdr, Elf_Data *secstrs)
Namhyung Kime5a18452012-08-06 13:41:20 +0900185{
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -0300186 return elf_sec__is_text(shdr, secstrs) ||
187 elf_sec__is_data(shdr, secstrs);
Namhyung Kime5a18452012-08-06 13:41:20 +0900188}
189
190static size_t elf_addr_to_index(Elf *elf, GElf_Addr addr)
191{
192 Elf_Scn *sec = NULL;
193 GElf_Shdr shdr;
194 size_t cnt = 1;
195
196 while ((sec = elf_nextscn(elf, sec)) != NULL) {
197 gelf_getshdr(sec, &shdr);
198
199 if ((addr >= shdr.sh_addr) &&
200 (addr < (shdr.sh_addr + shdr.sh_size)))
201 return cnt;
202
203 ++cnt;
204 }
205
206 return -1;
207}
208
Masami Hiramatsu99ca4232014-01-16 09:39:49 +0000209Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep,
210 GElf_Shdr *shp, const char *name, size_t *idx)
Namhyung Kime5a18452012-08-06 13:41:20 +0900211{
212 Elf_Scn *sec = NULL;
213 size_t cnt = 1;
214
Cody P Schafer49274652012-08-10 15:22:55 -0700215 /* Elf is corrupted/truncated, avoid calling elf_strptr. */
216 if (!elf_rawdata(elf_getscn(elf, ep->e_shstrndx), NULL))
217 return NULL;
218
Namhyung Kime5a18452012-08-06 13:41:20 +0900219 while ((sec = elf_nextscn(elf, sec)) != NULL) {
220 char *str;
221
222 gelf_getshdr(sec, shp);
223 str = elf_strptr(elf, ep->e_shstrndx, shp->sh_name);
Jiri Olsa155b3a12014-03-02 14:32:07 +0100224 if (str && !strcmp(name, str)) {
Namhyung Kime5a18452012-08-06 13:41:20 +0900225 if (idx)
226 *idx = cnt;
Jiri Olsa155b3a12014-03-02 14:32:07 +0100227 return sec;
Namhyung Kime5a18452012-08-06 13:41:20 +0900228 }
229 ++cnt;
230 }
231
Jiri Olsa155b3a12014-03-02 14:32:07 +0100232 return NULL;
Namhyung Kime5a18452012-08-06 13:41:20 +0900233}
234
Milian Wolff2a8d41b2016-08-30 13:41:02 +0200235static bool want_demangle(bool is_kernel_sym)
236{
237 return is_kernel_sym ? symbol_conf.demangle_kernel : symbol_conf.demangle;
238}
239
240static char *demangle_sym(struct dso *dso, int kmodule, const char *elf_name)
241{
Namhyung Kimbb963e12017-02-17 17:17:38 +0900242 int demangle_flags = verbose > 0 ? (DMGL_PARAMS | DMGL_ANSI) : DMGL_NO_OPTS;
Milian Wolff2a8d41b2016-08-30 13:41:02 +0200243 char *demangled = NULL;
244
245 /*
246 * We need to figure out if the object was created from C++ sources
247 * DWARF DW_compile_unit has this, but we don't always have access
248 * to it...
249 */
250 if (!want_demangle(dso->kernel || kmodule))
251 return demangled;
252
253 demangled = bfd_demangle(NULL, elf_name, demangle_flags);
254 if (demangled == NULL)
255 demangled = java_demangle_sym(elf_name, JAVA_DEMANGLE_NORET);
256 else if (rust_is_mangled(demangled))
257 /*
258 * Input to Rust demangling is the BFD-demangled
259 * name which it Rust-demangles in place.
260 */
261 rust_demangle_sym(demangled);
262
263 return demangled;
264}
265
Namhyung Kime5a18452012-08-06 13:41:20 +0900266#define elf_section__for_each_rel(reldata, pos, pos_mem, idx, nr_entries) \
267 for (idx = 0, pos = gelf_getrel(reldata, 0, &pos_mem); \
268 idx < nr_entries; \
269 ++idx, pos = gelf_getrel(reldata, idx, &pos_mem))
270
271#define elf_section__for_each_rela(reldata, pos, pos_mem, idx, nr_entries) \
272 for (idx = 0, pos = gelf_getrela(reldata, 0, &pos_mem); \
273 idx < nr_entries; \
274 ++idx, pos = gelf_getrela(reldata, idx, &pos_mem))
275
276/*
277 * We need to check if we have a .dynsym, so that we can handle the
278 * .plt, synthesizing its symbols, that aren't on the symtabs (be it
279 * .dynsym or .symtab).
280 * And always look at the original dso, not at debuginfo packages, that
281 * have the PLT data stripped out (shdr_rel_plt.sh_type == SHT_NOBITS).
282 */
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -0300283int dso__synthesize_plt_symbols(struct dso *dso, struct symsrc *ss)
Namhyung Kime5a18452012-08-06 13:41:20 +0900284{
285 uint32_t nr_rel_entries, idx;
286 GElf_Sym sym;
Li Binb2f76052017-06-05 08:34:09 +0800287 u64 plt_offset, plt_header_size, plt_entry_size;
Namhyung Kime5a18452012-08-06 13:41:20 +0900288 GElf_Shdr shdr_plt;
289 struct symbol *f;
290 GElf_Shdr shdr_rel_plt, shdr_dynsym;
291 Elf_Data *reldata, *syms, *symstrs;
292 Elf_Scn *scn_plt_rel, *scn_symstrs, *scn_dynsym;
293 size_t dynsym_idx;
294 GElf_Ehdr ehdr;
295 char sympltname[1024];
296 Elf *elf;
Cody P Schafera44f6052012-08-10 15:22:59 -0700297 int nr = 0, symidx, err = 0;
Namhyung Kime5a18452012-08-06 13:41:20 +0900298
David Ahernf47b58b2012-08-19 09:47:14 -0600299 if (!ss->dynsym)
300 return 0;
301
Cody P Schafera44f6052012-08-10 15:22:59 -0700302 elf = ss->elf;
303 ehdr = ss->ehdr;
Namhyung Kime5a18452012-08-06 13:41:20 +0900304
Cody P Schafera44f6052012-08-10 15:22:59 -0700305 scn_dynsym = ss->dynsym;
306 shdr_dynsym = ss->dynshdr;
307 dynsym_idx = ss->dynsym_idx;
Namhyung Kime5a18452012-08-06 13:41:20 +0900308
Namhyung Kime5a18452012-08-06 13:41:20 +0900309 if (scn_dynsym == NULL)
310 goto out_elf_end;
311
312 scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt,
313 ".rela.plt", NULL);
314 if (scn_plt_rel == NULL) {
315 scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt,
316 ".rel.plt", NULL);
317 if (scn_plt_rel == NULL)
318 goto out_elf_end;
319 }
320
321 err = -1;
322
323 if (shdr_rel_plt.sh_link != dynsym_idx)
324 goto out_elf_end;
325
326 if (elf_section_by_name(elf, &ehdr, &shdr_plt, ".plt", NULL) == NULL)
327 goto out_elf_end;
328
329 /*
330 * Fetch the relocation section to find the idxes to the GOT
331 * and the symbols in the .dynsym they refer to.
332 */
333 reldata = elf_getdata(scn_plt_rel, NULL);
334 if (reldata == NULL)
335 goto out_elf_end;
336
337 syms = elf_getdata(scn_dynsym, NULL);
338 if (syms == NULL)
339 goto out_elf_end;
340
341 scn_symstrs = elf_getscn(elf, shdr_dynsym.sh_link);
342 if (scn_symstrs == NULL)
343 goto out_elf_end;
344
345 symstrs = elf_getdata(scn_symstrs, NULL);
346 if (symstrs == NULL)
347 goto out_elf_end;
348
Cody P Schafer52f9ddb2012-08-10 15:22:51 -0700349 if (symstrs->d_size == 0)
350 goto out_elf_end;
351
Namhyung Kime5a18452012-08-06 13:41:20 +0900352 nr_rel_entries = shdr_rel_plt.sh_size / shdr_rel_plt.sh_entsize;
353 plt_offset = shdr_plt.sh_offset;
Li Binb2f76052017-06-05 08:34:09 +0800354 switch (ehdr.e_machine) {
355 case EM_ARM:
356 plt_header_size = 20;
357 plt_entry_size = 12;
358 break;
359
360 case EM_AARCH64:
361 plt_header_size = 32;
362 plt_entry_size = 16;
363 break;
364
David Millerd6afa562018-10-17 12:08:59 -0700365 case EM_SPARC:
366 plt_header_size = 48;
367 plt_entry_size = 12;
368 break;
369
370 case EM_SPARCV9:
371 plt_header_size = 128;
372 plt_entry_size = 32;
373 break;
374
375 default: /* FIXME: s390/alpha/mips/parisc/poperpc/sh/xtensa need to be checked */
Li Binb2f76052017-06-05 08:34:09 +0800376 plt_header_size = shdr_plt.sh_entsize;
377 plt_entry_size = shdr_plt.sh_entsize;
378 break;
379 }
380 plt_offset += plt_header_size;
Namhyung Kime5a18452012-08-06 13:41:20 +0900381
382 if (shdr_rel_plt.sh_type == SHT_RELA) {
383 GElf_Rela pos_mem, *pos;
384
385 elf_section__for_each_rela(reldata, pos, pos_mem, idx,
386 nr_rel_entries) {
Milian Wolff2a8d41b2016-08-30 13:41:02 +0200387 const char *elf_name = NULL;
388 char *demangled = NULL;
Namhyung Kime5a18452012-08-06 13:41:20 +0900389 symidx = GELF_R_SYM(pos->r_info);
Namhyung Kime5a18452012-08-06 13:41:20 +0900390 gelf_getsym(syms, symidx, &sym);
Milian Wolff2a8d41b2016-08-30 13:41:02 +0200391
392 elf_name = elf_sym__name(&sym, symstrs);
393 demangled = demangle_sym(dso, 0, elf_name);
394 if (demangled != NULL)
395 elf_name = demangled;
Namhyung Kime5a18452012-08-06 13:41:20 +0900396 snprintf(sympltname, sizeof(sympltname),
Milian Wolff2a8d41b2016-08-30 13:41:02 +0200397 "%s@plt", elf_name);
398 free(demangled);
Namhyung Kime5a18452012-08-06 13:41:20 +0900399
Li Binb2f76052017-06-05 08:34:09 +0800400 f = symbol__new(plt_offset, plt_entry_size,
Arnaldo Carvalho de Meloaf30bff2018-04-26 11:09:10 -0300401 STB_GLOBAL, STT_FUNC, sympltname);
Namhyung Kime5a18452012-08-06 13:41:20 +0900402 if (!f)
403 goto out_elf_end;
404
Li Binb2f76052017-06-05 08:34:09 +0800405 plt_offset += plt_entry_size;
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -0300406 symbols__insert(&dso->symbols, f);
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -0300407 ++nr;
Namhyung Kime5a18452012-08-06 13:41:20 +0900408 }
409 } else if (shdr_rel_plt.sh_type == SHT_REL) {
410 GElf_Rel pos_mem, *pos;
411 elf_section__for_each_rel(reldata, pos, pos_mem, idx,
412 nr_rel_entries) {
Milian Wolff2a8d41b2016-08-30 13:41:02 +0200413 const char *elf_name = NULL;
414 char *demangled = NULL;
Namhyung Kime5a18452012-08-06 13:41:20 +0900415 symidx = GELF_R_SYM(pos->r_info);
Namhyung Kime5a18452012-08-06 13:41:20 +0900416 gelf_getsym(syms, symidx, &sym);
Milian Wolff2a8d41b2016-08-30 13:41:02 +0200417
418 elf_name = elf_sym__name(&sym, symstrs);
419 demangled = demangle_sym(dso, 0, elf_name);
420 if (demangled != NULL)
421 elf_name = demangled;
Namhyung Kime5a18452012-08-06 13:41:20 +0900422 snprintf(sympltname, sizeof(sympltname),
Milian Wolff2a8d41b2016-08-30 13:41:02 +0200423 "%s@plt", elf_name);
424 free(demangled);
Namhyung Kime5a18452012-08-06 13:41:20 +0900425
Li Binb2f76052017-06-05 08:34:09 +0800426 f = symbol__new(plt_offset, plt_entry_size,
Arnaldo Carvalho de Meloaf30bff2018-04-26 11:09:10 -0300427 STB_GLOBAL, STT_FUNC, sympltname);
Namhyung Kime5a18452012-08-06 13:41:20 +0900428 if (!f)
429 goto out_elf_end;
430
Li Binb2f76052017-06-05 08:34:09 +0800431 plt_offset += plt_entry_size;
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -0300432 symbols__insert(&dso->symbols, f);
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -0300433 ++nr;
Namhyung Kime5a18452012-08-06 13:41:20 +0900434 }
435 }
436
437 err = 0;
438out_elf_end:
Namhyung Kime5a18452012-08-06 13:41:20 +0900439 if (err == 0)
440 return nr;
Namhyung Kime5a18452012-08-06 13:41:20 +0900441 pr_debug("%s: problems reading %s PLT info.\n",
442 __func__, dso->long_name);
443 return 0;
444}
445
Milian Wolff80c345b2017-08-06 23:24:34 +0200446char *dso__demangle_sym(struct dso *dso, int kmodule, const char *elf_name)
Jin Yaoa64489c2017-03-26 04:34:26 +0800447{
448 return demangle_sym(dso, kmodule, elf_name);
449}
450
Namhyung Kime5a18452012-08-06 13:41:20 +0900451/*
452 * Align offset to 4 bytes as needed for note name and descriptor data.
453 */
454#define NOTE_ALIGN(n) (((n) + 3) & -4U)
455
456static int elf_read_build_id(Elf *elf, void *bf, size_t size)
457{
458 int err = -1;
459 GElf_Ehdr ehdr;
460 GElf_Shdr shdr;
461 Elf_Data *data;
462 Elf_Scn *sec;
463 Elf_Kind ek;
464 void *ptr;
465
466 if (size < BUILD_ID_SIZE)
467 goto out;
468
469 ek = elf_kind(elf);
470 if (ek != ELF_K_ELF)
471 goto out;
472
473 if (gelf_getehdr(elf, &ehdr) == NULL) {
474 pr_err("%s: cannot get elf header.\n", __func__);
475 goto out;
476 }
477
478 /*
479 * Check following sections for notes:
480 * '.note.gnu.build-id'
481 * '.notes'
482 * '.note' (VDSO specific)
483 */
484 do {
485 sec = elf_section_by_name(elf, &ehdr, &shdr,
486 ".note.gnu.build-id", NULL);
487 if (sec)
488 break;
489
490 sec = elf_section_by_name(elf, &ehdr, &shdr,
491 ".notes", NULL);
492 if (sec)
493 break;
494
495 sec = elf_section_by_name(elf, &ehdr, &shdr,
496 ".note", NULL);
497 if (sec)
498 break;
499
500 return err;
501
502 } while (0);
503
504 data = elf_getdata(sec, NULL);
505 if (data == NULL)
506 goto out;
507
508 ptr = data->d_buf;
509 while (ptr < (data->d_buf + data->d_size)) {
510 GElf_Nhdr *nhdr = ptr;
511 size_t namesz = NOTE_ALIGN(nhdr->n_namesz),
512 descsz = NOTE_ALIGN(nhdr->n_descsz);
513 const char *name;
514
515 ptr += sizeof(*nhdr);
516 name = ptr;
517 ptr += namesz;
518 if (nhdr->n_type == NT_GNU_BUILD_ID &&
519 nhdr->n_namesz == sizeof("GNU")) {
520 if (memcmp(name, "GNU", sizeof("GNU")) == 0) {
521 size_t sz = min(size, descsz);
522 memcpy(bf, ptr, sz);
523 memset(bf + sz, 0, size - sz);
524 err = descsz;
525 break;
526 }
527 }
528 ptr += descsz;
529 }
530
531out:
532 return err;
533}
534
Remi Bernonba0509d2020-08-21 18:52:36 +0200535#ifdef HAVE_LIBBFD_BUILDID_SUPPORT
536
537int filename__read_build_id(const char *filename, void *bf, size_t size)
538{
539 int err = -1;
540 bfd *abfd;
541
542 abfd = bfd_openr(filename, NULL);
543 if (!abfd)
544 return -1;
545
546 if (!bfd_check_format(abfd, bfd_object)) {
547 pr_debug2("%s: cannot read %s bfd file.\n", __func__, filename);
548 goto out_close;
549 }
550
551 if (!abfd->build_id || abfd->build_id->size > size)
552 goto out_close;
553
554 memcpy(bf, abfd->build_id->data, abfd->build_id->size);
555 memset(bf + abfd->build_id->size, 0, size - abfd->build_id->size);
556 err = abfd->build_id->size;
557
558out_close:
559 bfd_close(abfd);
560 return err;
561}
562
563#else // HAVE_LIBBFD_BUILDID_SUPPORT
564
Namhyung Kime5a18452012-08-06 13:41:20 +0900565int filename__read_build_id(const char *filename, void *bf, size_t size)
566{
567 int fd, err = -1;
568 Elf *elf;
569
570 if (size < BUILD_ID_SIZE)
571 goto out;
572
573 fd = open(filename, O_RDONLY);
574 if (fd < 0)
575 goto out;
576
577 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
578 if (elf == NULL) {
579 pr_debug2("%s: cannot read %s ELF file.\n", __func__, filename);
580 goto out_close;
581 }
582
583 err = elf_read_build_id(elf, bf, size);
584
585 elf_end(elf);
586out_close:
587 close(fd);
588out:
589 return err;
590}
591
Remi Bernonba0509d2020-08-21 18:52:36 +0200592#endif // HAVE_LIBBFD_BUILDID_SUPPORT
593
Namhyung Kime5a18452012-08-06 13:41:20 +0900594int sysfs__read_build_id(const char *filename, void *build_id, size_t size)
595{
596 int fd, err = -1;
597
598 if (size < BUILD_ID_SIZE)
599 goto out;
600
601 fd = open(filename, O_RDONLY);
602 if (fd < 0)
603 goto out;
604
605 while (1) {
606 char bf[BUFSIZ];
607 GElf_Nhdr nhdr;
608 size_t namesz, descsz;
609
610 if (read(fd, &nhdr, sizeof(nhdr)) != sizeof(nhdr))
611 break;
612
613 namesz = NOTE_ALIGN(nhdr.n_namesz);
614 descsz = NOTE_ALIGN(nhdr.n_descsz);
615 if (nhdr.n_type == NT_GNU_BUILD_ID &&
616 nhdr.n_namesz == sizeof("GNU")) {
617 if (read(fd, bf, namesz) != (ssize_t)namesz)
618 break;
619 if (memcmp(bf, "GNU", sizeof("GNU")) == 0) {
620 size_t sz = min(descsz, size);
621 if (read(fd, build_id, sz) == (ssize_t)sz) {
622 memset(build_id + sz, 0, size - sz);
623 err = 0;
624 break;
625 }
626 } else if (read(fd, bf, descsz) != (ssize_t)descsz)
627 break;
628 } else {
629 int n = namesz + descsz;
Arnaldo Carvalho de Melo7934c982017-01-03 15:19:21 -0300630
631 if (n > (int)sizeof(bf)) {
632 n = sizeof(bf);
633 pr_debug("%s: truncating reading of build id in sysfs file %s: n_namesz=%u, n_descsz=%u.\n",
634 __func__, filename, nhdr.n_namesz, nhdr.n_descsz);
635 }
Namhyung Kime5a18452012-08-06 13:41:20 +0900636 if (read(fd, bf, n) != n)
637 break;
638 }
639 }
640 close(fd);
641out:
642 return err;
643}
644
Remi Bernonba0509d2020-08-21 18:52:36 +0200645#ifdef HAVE_LIBBFD_SUPPORT
646
647int filename__read_debuglink(const char *filename, char *debuglink,
648 size_t size)
649{
650 int err = -1;
651 asection *section;
652 bfd *abfd;
653
654 abfd = bfd_openr(filename, NULL);
655 if (!abfd)
656 return -1;
657
658 if (!bfd_check_format(abfd, bfd_object)) {
659 pr_debug2("%s: cannot read %s bfd file.\n", __func__, filename);
660 goto out_close;
661 }
662
663 section = bfd_get_section_by_name(abfd, ".gnu_debuglink");
664 if (!section)
665 goto out_close;
666
667 if (section->size > size)
668 goto out_close;
669
670 if (!bfd_get_section_contents(abfd, section, debuglink, 0,
671 section->size))
672 goto out_close;
673
674 err = 0;
675
676out_close:
677 bfd_close(abfd);
678 return err;
679}
680
681#else
682
Namhyung Kime5a18452012-08-06 13:41:20 +0900683int filename__read_debuglink(const char *filename, char *debuglink,
684 size_t size)
685{
686 int fd, err = -1;
687 Elf *elf;
688 GElf_Ehdr ehdr;
689 GElf_Shdr shdr;
690 Elf_Data *data;
691 Elf_Scn *sec;
692 Elf_Kind ek;
693
694 fd = open(filename, O_RDONLY);
695 if (fd < 0)
696 goto out;
697
698 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
699 if (elf == NULL) {
700 pr_debug2("%s: cannot read %s ELF file.\n", __func__, filename);
701 goto out_close;
702 }
703
704 ek = elf_kind(elf);
705 if (ek != ELF_K_ELF)
Chenggang Qin784f3392013-10-11 08:27:57 +0800706 goto out_elf_end;
Namhyung Kime5a18452012-08-06 13:41:20 +0900707
708 if (gelf_getehdr(elf, &ehdr) == NULL) {
709 pr_err("%s: cannot get elf header.\n", __func__);
Chenggang Qin784f3392013-10-11 08:27:57 +0800710 goto out_elf_end;
Namhyung Kime5a18452012-08-06 13:41:20 +0900711 }
712
713 sec = elf_section_by_name(elf, &ehdr, &shdr,
714 ".gnu_debuglink", NULL);
715 if (sec == NULL)
Chenggang Qin784f3392013-10-11 08:27:57 +0800716 goto out_elf_end;
Namhyung Kime5a18452012-08-06 13:41:20 +0900717
718 data = elf_getdata(sec, NULL);
719 if (data == NULL)
Chenggang Qin784f3392013-10-11 08:27:57 +0800720 goto out_elf_end;
Namhyung Kime5a18452012-08-06 13:41:20 +0900721
722 /* the start of this section is a zero-terminated string */
723 strncpy(debuglink, data->d_buf, size);
724
Stephane Eranian0d3dc5e2014-02-20 10:32:55 +0900725 err = 0;
726
Chenggang Qin784f3392013-10-11 08:27:57 +0800727out_elf_end:
Namhyung Kime5a18452012-08-06 13:41:20 +0900728 elf_end(elf);
Namhyung Kime5a18452012-08-06 13:41:20 +0900729out_close:
730 close(fd);
731out:
732 return err;
733}
734
Remi Bernonba0509d2020-08-21 18:52:36 +0200735#endif
736
Namhyung Kime5a18452012-08-06 13:41:20 +0900737static int dso__swap_init(struct dso *dso, unsigned char eidata)
738{
739 static unsigned int const endian = 1;
740
741 dso->needs_swap = DSO_SWAP__NO;
742
743 switch (eidata) {
744 case ELFDATA2LSB:
745 /* We are big endian, DSO is little endian. */
746 if (*(unsigned char const *)&endian != 1)
747 dso->needs_swap = DSO_SWAP__YES;
748 break;
749
750 case ELFDATA2MSB:
751 /* We are little endian, DSO is big endian. */
752 if (*(unsigned char const *)&endian != 0)
753 dso->needs_swap = DSO_SWAP__YES;
754 break;
755
756 default:
757 pr_err("unrecognized DSO data encoding %d\n", eidata);
758 return -EINVAL;
759 }
760
761 return 0;
762}
763
Cody P Schafer3aafe5a2012-08-10 15:23:02 -0700764bool symsrc__possibly_runtime(struct symsrc *ss)
765{
766 return ss->dynsym || ss->opdsec;
767}
768
Cody P Schaferd26cd122012-08-10 15:23:00 -0700769bool symsrc__has_symtab(struct symsrc *ss)
770{
771 return ss->symtab != NULL;
772}
Cody P Schaferb68e2f92012-08-10 15:22:57 -0700773
774void symsrc__destroy(struct symsrc *ss)
Namhyung Kime5a18452012-08-06 13:41:20 +0900775{
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300776 zfree(&ss->name);
Cody P Schaferb68e2f92012-08-10 15:22:57 -0700777 elf_end(ss->elf);
778 close(ss->fd);
779}
780
Leo Yan7eec00a2020-03-06 09:57:58 +0800781bool elf__needs_adjust_symbols(GElf_Ehdr ehdr)
Naveen N. Raod2332092015-04-28 17:35:35 +0530782{
Leo Yan7eec00a2020-03-06 09:57:58 +0800783 /*
784 * Usually vmlinux is an ELF file with type ET_EXEC for most
785 * architectures; except Arm64 kernel is linked with option
786 * '-share', so need to check type ET_DYN.
787 */
788 return ehdr.e_type == ET_EXEC || ehdr.e_type == ET_REL ||
789 ehdr.e_type == ET_DYN;
Naveen N. Raod2332092015-04-28 17:35:35 +0530790}
791
Cody P Schaferb68e2f92012-08-10 15:22:57 -0700792int symsrc__init(struct symsrc *ss, struct dso *dso, const char *name,
793 enum dso_binary_type type)
794{
Namhyung Kime5a18452012-08-06 13:41:20 +0900795 GElf_Ehdr ehdr;
Namhyung Kime5a18452012-08-06 13:41:20 +0900796 Elf *elf;
Cody P Schaferb68e2f92012-08-10 15:22:57 -0700797 int fd;
798
Arnaldo Carvalho de Melo18425f12015-03-24 11:49:02 -0300799 if (dso__needs_decompress(dso)) {
Namhyung Kim42b3fa62017-06-08 16:31:03 +0900800 fd = dso__decompress_kmodule_fd(dso, name);
Arnaldo Carvalho de Melo18425f12015-03-24 11:49:02 -0300801 if (fd < 0)
802 return -1;
Namhyung Kimc25ec422017-06-08 16:31:08 +0900803
804 type = dso->symtab_type;
Arnaldo Carvalho de Melo18425f12015-03-24 11:49:02 -0300805 } else {
Namhyung Kimc00c48f2014-11-04 10:14:27 +0900806 fd = open(name, O_RDONLY);
Arnaldo Carvalho de Melo18425f12015-03-24 11:49:02 -0300807 if (fd < 0) {
808 dso->load_errno = errno;
809 return -1;
810 }
811 }
Namhyung Kime5a18452012-08-06 13:41:20 +0900812
813 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
814 if (elf == NULL) {
815 pr_debug("%s: cannot read %s ELF file.\n", __func__, name);
Arnaldo Carvalho de Melo18425f12015-03-24 11:49:02 -0300816 dso->load_errno = DSO_LOAD_ERRNO__INVALID_ELF;
Namhyung Kime5a18452012-08-06 13:41:20 +0900817 goto out_close;
818 }
819
820 if (gelf_getehdr(elf, &ehdr) == NULL) {
Arnaldo Carvalho de Melo18425f12015-03-24 11:49:02 -0300821 dso->load_errno = DSO_LOAD_ERRNO__INVALID_ELF;
Namhyung Kime5a18452012-08-06 13:41:20 +0900822 pr_debug("%s: cannot get elf header.\n", __func__);
823 goto out_elf_end;
824 }
825
Arnaldo Carvalho de Melo18425f12015-03-24 11:49:02 -0300826 if (dso__swap_init(dso, ehdr.e_ident[EI_DATA])) {
827 dso->load_errno = DSO_LOAD_ERRNO__INTERNAL_ERROR;
Namhyung Kime5a18452012-08-06 13:41:20 +0900828 goto out_elf_end;
Arnaldo Carvalho de Melo18425f12015-03-24 11:49:02 -0300829 }
Namhyung Kime5a18452012-08-06 13:41:20 +0900830
831 /* Always reject images with a mismatched build-id: */
Masami Hiramatsu428aff82016-08-26 01:24:42 +0900832 if (dso->has_build_id && !symbol_conf.ignore_vmlinux_buildid) {
Namhyung Kime5a18452012-08-06 13:41:20 +0900833 u8 build_id[BUILD_ID_SIZE];
834
Arnaldo Carvalho de Melo18425f12015-03-24 11:49:02 -0300835 if (elf_read_build_id(elf, build_id, BUILD_ID_SIZE) < 0) {
836 dso->load_errno = DSO_LOAD_ERRNO__CANNOT_READ_BUILDID;
Namhyung Kime5a18452012-08-06 13:41:20 +0900837 goto out_elf_end;
Arnaldo Carvalho de Melo18425f12015-03-24 11:49:02 -0300838 }
Namhyung Kime5a18452012-08-06 13:41:20 +0900839
Arnaldo Carvalho de Melo18425f12015-03-24 11:49:02 -0300840 if (!dso__build_id_equal(dso, build_id)) {
Naveen N. Rao468f3d22015-04-25 01:14:46 +0530841 pr_debug("%s: build id mismatch for %s.\n", __func__, name);
Arnaldo Carvalho de Melo18425f12015-03-24 11:49:02 -0300842 dso->load_errno = DSO_LOAD_ERRNO__MISMATCHING_BUILDID;
Namhyung Kime5a18452012-08-06 13:41:20 +0900843 goto out_elf_end;
Arnaldo Carvalho de Melo18425f12015-03-24 11:49:02 -0300844 }
Namhyung Kime5a18452012-08-06 13:41:20 +0900845 }
846
Adrian Hunterc6d8f2a2014-07-14 13:02:41 +0300847 ss->is_64_bit = (gelf_getclass(elf) == ELFCLASS64);
848
Cody P Schaferb68e2f92012-08-10 15:22:57 -0700849 ss->symtab = elf_section_by_name(elf, &ehdr, &ss->symshdr, ".symtab",
850 NULL);
851 if (ss->symshdr.sh_type != SHT_SYMTAB)
852 ss->symtab = NULL;
853
854 ss->dynsym_idx = 0;
855 ss->dynsym = elf_section_by_name(elf, &ehdr, &ss->dynshdr, ".dynsym",
856 &ss->dynsym_idx);
857 if (ss->dynshdr.sh_type != SHT_DYNSYM)
858 ss->dynsym = NULL;
859
860 ss->opdidx = 0;
861 ss->opdsec = elf_section_by_name(elf, &ehdr, &ss->opdshdr, ".opd",
862 &ss->opdidx);
863 if (ss->opdshdr.sh_type != SHT_PROGBITS)
864 ss->opdsec = NULL;
865
Jiri Olsa1c695c82020-08-08 14:21:54 +0200866 if (dso->kernel == DSO_SPACE__USER)
Wang Nan99e87f72016-04-07 10:24:31 +0000867 ss->adjust_symbols = true;
868 else
Naveen N. Raod2332092015-04-28 17:35:35 +0530869 ss->adjust_symbols = elf__needs_adjust_symbols(ehdr);
Cody P Schaferb68e2f92012-08-10 15:22:57 -0700870
871 ss->name = strdup(name);
Arnaldo Carvalho de Melo18425f12015-03-24 11:49:02 -0300872 if (!ss->name) {
873 dso->load_errno = errno;
Cody P Schaferb68e2f92012-08-10 15:22:57 -0700874 goto out_elf_end;
Arnaldo Carvalho de Melo18425f12015-03-24 11:49:02 -0300875 }
Cody P Schaferb68e2f92012-08-10 15:22:57 -0700876
877 ss->elf = elf;
878 ss->fd = fd;
879 ss->ehdr = ehdr;
880 ss->type = type;
881
882 return 0;
883
884out_elf_end:
885 elf_end(elf);
886out_close:
887 close(fd);
Leo Yane5f177a2019-05-30 17:38:01 +0800888 return -1;
Cody P Schaferb68e2f92012-08-10 15:22:57 -0700889}
890
Adrian Hunter39b12f782013-08-07 14:38:47 +0300891/**
892 * ref_reloc_sym_not_found - has kernel relocation symbol been found.
893 * @kmap: kernel maps and relocation reference symbol
894 *
895 * This function returns %true if we are dealing with the kernel maps and the
896 * relocation reference symbol has not yet been found. Otherwise %false is
897 * returned.
898 */
899static bool ref_reloc_sym_not_found(struct kmap *kmap)
900{
901 return kmap && kmap->ref_reloc_sym && kmap->ref_reloc_sym->name &&
902 !kmap->ref_reloc_sym->unrelocated_addr;
903}
904
905/**
906 * ref_reloc - kernel relocation offset.
907 * @kmap: kernel maps and relocation reference symbol
908 *
909 * This function returns the offset of kernel addresses as determined by using
910 * the relocation reference symbol i.e. if the kernel has not been relocated
911 * then the return value is zero.
912 */
913static u64 ref_reloc(struct kmap *kmap)
914{
915 if (kmap && kmap->ref_reloc_sym &&
916 kmap->ref_reloc_sym->unrelocated_addr)
917 return kmap->ref_reloc_sym->addr -
918 kmap->ref_reloc_sym->unrelocated_addr;
919 return 0;
920}
921
Naveen N. Rao0b3c2262016-04-12 14:40:50 +0530922void __weak arch__sym_update(struct symbol *s __maybe_unused,
923 GElf_Sym *sym __maybe_unused) { }
Ananth N Mavinakayanahallic50fc0a2015-04-28 17:35:38 +0530924
Arnaldo Carvalho de Melo4e0d1e82018-04-27 15:15:24 -0300925static int dso__process_kernel_symbol(struct dso *dso, struct map *map,
926 GElf_Sym *sym, GElf_Shdr *shdr,
Arnaldo Carvalho de Melo79b6bb72019-11-25 21:58:33 -0300927 struct maps *kmaps, struct kmap *kmap,
Arnaldo Carvalho de Melo4e0d1e82018-04-27 15:15:24 -0300928 struct dso **curr_dsop, struct map **curr_mapp,
929 const char *section_name,
930 bool adjust_kernel_syms, bool kmodule, bool *remap_kernel)
931{
932 struct dso *curr_dso = *curr_dsop;
933 struct map *curr_map;
934 char dso_name[PATH_MAX];
935
936 /* Adjust symbol to map to file offset */
937 if (adjust_kernel_syms)
938 sym->st_value -= shdr->sh_addr - shdr->sh_offset;
939
940 if (strcmp(section_name, (curr_dso->short_name + dso->short_name_len)) == 0)
941 return 0;
942
943 if (strcmp(section_name, ".text") == 0) {
944 /*
945 * The initial kernel mapping is based on
946 * kallsyms and identity maps. Overwrite it to
947 * map to the kernel dso.
948 */
Jiri Olsab2fe96a2020-08-08 14:26:56 +0200949 if (*remap_kernel && dso->kernel && !kmodule) {
Arnaldo Carvalho de Melo4e0d1e82018-04-27 15:15:24 -0300950 *remap_kernel = false;
951 map->start = shdr->sh_addr + ref_reloc(kmap);
952 map->end = map->start + shdr->sh_size;
953 map->pgoff = shdr->sh_offset;
954 map->map_ip = map__map_ip;
955 map->unmap_ip = map__unmap_ip;
956 /* Ensure maps are correctly ordered */
957 if (kmaps) {
958 map__get(map);
Arnaldo Carvalho de Melo79b6bb72019-11-25 21:58:33 -0300959 maps__remove(kmaps, map);
960 maps__insert(kmaps, map);
Arnaldo Carvalho de Melo4e0d1e82018-04-27 15:15:24 -0300961 map__put(map);
962 }
963 }
964
965 /*
966 * The initial module mapping is based on
967 * /proc/modules mapped to offset zero.
968 * Overwrite it to map to the module dso.
969 */
970 if (*remap_kernel && kmodule) {
971 *remap_kernel = false;
972 map->pgoff = shdr->sh_offset;
973 }
974
975 *curr_mapp = map;
976 *curr_dsop = dso;
977 return 0;
978 }
979
980 if (!kmap)
981 return 0;
982
983 snprintf(dso_name, sizeof(dso_name), "%s%s", dso->short_name, section_name);
984
Arnaldo Carvalho de Melo79b6bb72019-11-25 21:58:33 -0300985 curr_map = maps__find_by_name(kmaps, dso_name);
Arnaldo Carvalho de Melo4e0d1e82018-04-27 15:15:24 -0300986 if (curr_map == NULL) {
987 u64 start = sym->st_value;
988
989 if (kmodule)
990 start += map->start + shdr->sh_offset;
991
992 curr_dso = dso__new(dso_name);
993 if (curr_dso == NULL)
994 return -1;
995 curr_dso->kernel = dso->kernel;
996 curr_dso->long_name = dso->long_name;
997 curr_dso->long_name_len = dso->long_name_len;
998 curr_map = map__new2(start, curr_dso);
999 dso__put(curr_dso);
1000 if (curr_map == NULL)
1001 return -1;
1002
Arnaldo Carvalho de Meloa75af862019-12-18 15:23:14 -03001003 if (curr_dso->kernel)
1004 map__kmap(curr_map)->kmaps = kmaps;
1005
Arnaldo Carvalho de Melo4e0d1e82018-04-27 15:15:24 -03001006 if (adjust_kernel_syms) {
1007 curr_map->start = shdr->sh_addr + ref_reloc(kmap);
1008 curr_map->end = curr_map->start + shdr->sh_size;
1009 curr_map->pgoff = shdr->sh_offset;
1010 } else {
1011 curr_map->map_ip = curr_map->unmap_ip = identity__map_ip;
1012 }
1013 curr_dso->symtab_type = dso->symtab_type;
Arnaldo Carvalho de Melo79b6bb72019-11-25 21:58:33 -03001014 maps__insert(kmaps, curr_map);
Arnaldo Carvalho de Melo4e0d1e82018-04-27 15:15:24 -03001015 /*
1016 * Add it before we drop the referece to curr_map, i.e. while
1017 * we still are sure to have a reference to this DSO via
1018 * *curr_map->dso.
1019 */
Arnaldo Carvalho de Melof2baa0602019-11-04 16:09:48 -03001020 dsos__add(&kmaps->machine->dsos, curr_dso);
Arnaldo Carvalho de Melo4e0d1e82018-04-27 15:15:24 -03001021 /* kmaps already got it */
1022 map__put(curr_map);
1023 dso__set_loaded(curr_dso);
1024 *curr_mapp = curr_map;
1025 *curr_dsop = curr_dso;
1026 } else
1027 *curr_dsop = curr_map->dso;
1028
1029 return 0;
1030}
1031
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03001032int dso__load_sym(struct dso *dso, struct map *map, struct symsrc *syms_ss,
1033 struct symsrc *runtime_ss, int kmodule)
Cody P Schaferb68e2f92012-08-10 15:22:57 -07001034{
1035 struct kmap *kmap = dso->kernel ? map__kmap(map) : NULL;
Arnaldo Carvalho de Melo79b6bb72019-11-25 21:58:33 -03001036 struct maps *kmaps = kmap ? map__kmaps(map) : NULL;
Cody P Schaferb68e2f92012-08-10 15:22:57 -07001037 struct map *curr_map = map;
1038 struct dso *curr_dso = dso;
1039 Elf_Data *symstrs, *secstrs;
1040 uint32_t nr_syms;
1041 int err = -1;
1042 uint32_t idx;
1043 GElf_Ehdr ehdr;
Cody P Schafer261360b2012-08-10 15:23:01 -07001044 GElf_Shdr shdr;
Wang Nan73cdf0c2016-02-26 09:31:49 +00001045 GElf_Shdr tshdr;
Cody P Schaferb68e2f92012-08-10 15:22:57 -07001046 Elf_Data *syms, *opddata = NULL;
1047 GElf_Sym sym;
Cody P Schafer261360b2012-08-10 15:23:01 -07001048 Elf_Scn *sec, *sec_strndx;
Cody P Schaferb68e2f92012-08-10 15:22:57 -07001049 Elf *elf;
1050 int nr = 0;
Adrian Hunter39b12f782013-08-07 14:38:47 +03001051 bool remap_kernel = false, adjust_kernel_syms = false;
Cody P Schaferb68e2f92012-08-10 15:22:57 -07001052
Wang Nanba927322015-04-07 08:22:45 +00001053 if (kmap && !kmaps)
1054 return -1;
1055
Cody P Schafer261360b2012-08-10 15:23:01 -07001056 dso->symtab_type = syms_ss->type;
Adrian Hunterc6d8f2a2014-07-14 13:02:41 +03001057 dso->is_64_bit = syms_ss->is_64_bit;
Adrian Hunter0131c4e2013-08-07 14:38:50 +03001058 dso->rel = syms_ss->ehdr.e_type == ET_REL;
1059
1060 /*
1061 * Modules may already have symbols from kallsyms, but those symbols
1062 * have the wrong values for the dso maps, so remove them.
1063 */
1064 if (kmodule && syms_ss->symtab)
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -03001065 symbols__delete(&dso->symbols);
Cody P Schafer005f9292012-08-10 15:22:58 -07001066
Cody P Schafer261360b2012-08-10 15:23:01 -07001067 if (!syms_ss->symtab) {
Anton Blanchardd0b0d042014-09-09 08:59:29 +10001068 /*
1069 * If the vmlinux is stripped, fail so we will fall back
1070 * to using kallsyms. The vmlinux runtime symbols aren't
1071 * of much use.
1072 */
1073 if (dso->kernel)
1074 goto out_elf_end;
1075
Cody P Schafer261360b2012-08-10 15:23:01 -07001076 syms_ss->symtab = syms_ss->dynsym;
1077 syms_ss->symshdr = syms_ss->dynshdr;
Cody P Schaferd26cd122012-08-10 15:23:00 -07001078 }
1079
Cody P Schafer261360b2012-08-10 15:23:01 -07001080 elf = syms_ss->elf;
1081 ehdr = syms_ss->ehdr;
1082 sec = syms_ss->symtab;
1083 shdr = syms_ss->symshdr;
Cody P Schaferb68e2f92012-08-10 15:22:57 -07001084
Anton Blanchard50de1a02016-08-13 11:55:33 +10001085 if (elf_section_by_name(runtime_ss->elf, &runtime_ss->ehdr, &tshdr,
1086 ".text", NULL))
Wang Nan73cdf0c2016-02-26 09:31:49 +00001087 dso->text_offset = tshdr.sh_addr - tshdr.sh_offset;
1088
Cody P Schafer261360b2012-08-10 15:23:01 -07001089 if (runtime_ss->opdsec)
1090 opddata = elf_rawdata(runtime_ss->opdsec, NULL);
Namhyung Kime5a18452012-08-06 13:41:20 +09001091
1092 syms = elf_getdata(sec, NULL);
1093 if (syms == NULL)
1094 goto out_elf_end;
1095
1096 sec = elf_getscn(elf, shdr.sh_link);
1097 if (sec == NULL)
1098 goto out_elf_end;
1099
1100 symstrs = elf_getdata(sec, NULL);
1101 if (symstrs == NULL)
1102 goto out_elf_end;
1103
Adrian Hunterf247fb82014-07-31 09:00:46 +03001104 sec_strndx = elf_getscn(runtime_ss->elf, runtime_ss->ehdr.e_shstrndx);
Namhyung Kime5a18452012-08-06 13:41:20 +09001105 if (sec_strndx == NULL)
1106 goto out_elf_end;
1107
1108 secstrs = elf_getdata(sec_strndx, NULL);
1109 if (secstrs == NULL)
1110 goto out_elf_end;
1111
1112 nr_syms = shdr.sh_size / shdr.sh_entsize;
1113
1114 memset(&sym, 0, sizeof(sym));
Adrian Hunter39b12f782013-08-07 14:38:47 +03001115
1116 /*
1117 * The kernel relocation symbol is needed in advance in order to adjust
1118 * kernel maps correctly.
1119 */
1120 if (ref_reloc_sym_not_found(kmap)) {
1121 elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) {
1122 const char *elf_name = elf_sym__name(&sym, symstrs);
1123
1124 if (strcmp(elf_name, kmap->ref_reloc_sym->name))
1125 continue;
1126 kmap->ref_reloc_sym->unrelocated_addr = sym.st_value;
Adrian Hunter91767532014-01-29 16:14:36 +02001127 map->reloc = kmap->ref_reloc_sym->addr -
1128 kmap->ref_reloc_sym->unrelocated_addr;
Adrian Hunter39b12f782013-08-07 14:38:47 +03001129 break;
1130 }
1131 }
1132
Adrian Hunterf0ee3b42015-08-14 15:50:06 +03001133 /*
1134 * Handle any relocation of vdso necessary because older kernels
1135 * attempted to prelink vdso to its virtual address.
1136 */
Wang Nan73cdf0c2016-02-26 09:31:49 +00001137 if (dso__is_vdso(dso))
1138 map->reloc = map->start - dso->text_offset;
Adrian Hunterf0ee3b42015-08-14 15:50:06 +03001139
Adrian Hunter39b12f782013-08-07 14:38:47 +03001140 dso->adjust_symbols = runtime_ss->adjust_symbols || ref_reloc(kmap);
1141 /*
Arnaldo Carvalho de Melod1fd8d92018-04-26 12:36:37 -03001142 * Initial kernel and module mappings do not map to the dso.
1143 * Flag the fixups.
Adrian Hunter39b12f782013-08-07 14:38:47 +03001144 */
Jiri Olsab2fe96a2020-08-08 14:26:56 +02001145 if (dso->kernel) {
Adrian Hunter39b12f782013-08-07 14:38:47 +03001146 remap_kernel = true;
1147 adjust_kernel_syms = dso->adjust_symbols;
1148 }
Namhyung Kime5a18452012-08-06 13:41:20 +09001149 elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) {
1150 struct symbol *f;
1151 const char *elf_name = elf_sym__name(&sym, symstrs);
1152 char *demangled = NULL;
1153 int is_label = elf_sym__is_label(&sym);
1154 const char *section_name;
Cody P Schafer261360b2012-08-10 15:23:01 -07001155 bool used_opd = false;
Namhyung Kime5a18452012-08-06 13:41:20 +09001156
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -03001157 if (!is_label && !elf_sym__filter(&sym))
Namhyung Kime5a18452012-08-06 13:41:20 +09001158 continue;
1159
1160 /* Reject ARM ELF "mapping symbols": these aren't unique and
1161 * don't identify functions, so will confuse the profile
1162 * output: */
Victor Kamensky4886f2c2015-01-26 22:34:01 -08001163 if (ehdr.e_machine == EM_ARM || ehdr.e_machine == EM_AARCH64) {
1164 if (elf_name[0] == '$' && strchr("adtx", elf_name[1])
1165 && (elf_name[2] == '\0' || elf_name[2] == '.'))
Namhyung Kime5a18452012-08-06 13:41:20 +09001166 continue;
1167 }
1168
Cody P Schafer261360b2012-08-10 15:23:01 -07001169 if (runtime_ss->opdsec && sym.st_shndx == runtime_ss->opdidx) {
1170 u32 offset = sym.st_value - syms_ss->opdshdr.sh_addr;
Namhyung Kime5a18452012-08-06 13:41:20 +09001171 u64 *opd = opddata->d_buf + offset;
1172 sym.st_value = DSO__SWAP(dso, u64, *opd);
Cody P Schafer261360b2012-08-10 15:23:01 -07001173 sym.st_shndx = elf_addr_to_index(runtime_ss->elf,
1174 sym.st_value);
1175 used_opd = true;
Namhyung Kime5a18452012-08-06 13:41:20 +09001176 }
Namhyung Kim3843b052012-11-21 13:49:44 +01001177 /*
1178 * When loading symbols in a data mapping, ABS symbols (which
1179 * has a value of SHN_ABS in its st_shndx) failed at
1180 * elf_getscn(). And it marks the loading as a failure so
1181 * already loaded symbols cannot be fixed up.
1182 *
1183 * I'm not sure what should be done. Just ignore them for now.
1184 * - Namhyung Kim
1185 */
1186 if (sym.st_shndx == SHN_ABS)
1187 continue;
Namhyung Kime5a18452012-08-06 13:41:20 +09001188
Cody P Schafer261360b2012-08-10 15:23:01 -07001189 sec = elf_getscn(runtime_ss->elf, sym.st_shndx);
Namhyung Kime5a18452012-08-06 13:41:20 +09001190 if (!sec)
1191 goto out_elf_end;
1192
1193 gelf_getshdr(sec, &shdr);
1194
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -03001195 if (is_label && !elf_sec__filter(&shdr, secstrs))
Namhyung Kime5a18452012-08-06 13:41:20 +09001196 continue;
1197
1198 section_name = elf_sec__name(&shdr, secstrs);
1199
1200 /* On ARM, symbols for thumb functions have 1 added to
1201 * the symbol address as a flag - remove it */
1202 if ((ehdr.e_machine == EM_ARM) &&
Arnaldo Carvalho de Melo18231d72018-04-26 12:45:17 -03001203 (GELF_ST_TYPE(sym.st_info) == STT_FUNC) &&
Namhyung Kime5a18452012-08-06 13:41:20 +09001204 (sym.st_value & 1))
1205 --sym.st_value;
1206
Jiri Olsab2fe96a2020-08-08 14:26:56 +02001207 if (dso->kernel) {
Arnaldo Carvalho de Melo4e0d1e82018-04-27 15:15:24 -03001208 if (dso__process_kernel_symbol(dso, map, &sym, &shdr, kmaps, kmap, &curr_dso, &curr_map,
1209 section_name, adjust_kernel_syms, kmodule, &remap_kernel))
1210 goto out_elf_end;
Arnaldo Carvalho de Melo857140e2018-04-27 10:53:14 -03001211 } else if ((used_opd && runtime_ss->adjust_symbols) ||
1212 (!used_opd && syms_ss->adjust_symbols)) {
Namhyung Kime5a18452012-08-06 13:41:20 +09001213 pr_debug4("%s: adjusting symbol: st_value: %#" PRIx64 " "
1214 "sh_addr: %#" PRIx64 " sh_offset: %#" PRIx64 "\n", __func__,
1215 (u64)sym.st_value, (u64)shdr.sh_addr,
1216 (u64)shdr.sh_offset);
1217 sym.st_value -= shdr.sh_addr - shdr.sh_offset;
1218 }
Arnaldo Carvalho de Melo4e0d1e82018-04-27 15:15:24 -03001219
Milian Wolff2a8d41b2016-08-30 13:41:02 +02001220 demangled = demangle_sym(dso, kmodule, elf_name);
1221 if (demangled != NULL)
1222 elf_name = demangled;
Namhyung Kime71e7942014-07-31 14:47:42 +09001223
Namhyung Kime5a18452012-08-06 13:41:20 +09001224 f = symbol__new(sym.st_value, sym.st_size,
Arnaldo Carvalho de Meloaf30bff2018-04-26 11:09:10 -03001225 GELF_ST_BIND(sym.st_info),
1226 GELF_ST_TYPE(sym.st_info), elf_name);
Namhyung Kime5a18452012-08-06 13:41:20 +09001227 free(demangled);
1228 if (!f)
1229 goto out_elf_end;
1230
Naveen N. Rao0b3c2262016-04-12 14:40:50 +05301231 arch__sym_update(f, &sym);
1232
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -03001233 __symbols__insert(&curr_dso->symbols, f, dso->kernel);
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03001234 nr++;
Namhyung Kime5a18452012-08-06 13:41:20 +09001235 }
1236
1237 /*
1238 * For misannotated, zeroed, ASM function sizes.
1239 */
1240 if (nr > 0) {
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -03001241 symbols__fixup_end(&dso->symbols);
1242 symbols__fixup_duplicate(&dso->symbols);
Namhyung Kime5a18452012-08-06 13:41:20 +09001243 if (kmap) {
1244 /*
1245 * We need to fixup this here too because we create new
1246 * maps here, for things like vsyscall sections.
1247 */
Arnaldo Carvalho de Melo79b6bb72019-11-25 21:58:33 -03001248 maps__fixup_end(kmaps);
Namhyung Kime5a18452012-08-06 13:41:20 +09001249 }
1250 }
1251 err = nr;
1252out_elf_end:
Namhyung Kime5a18452012-08-06 13:41:20 +09001253 return err;
1254}
1255
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001256static int elf_read_maps(Elf *elf, bool exe, mapfn_t mapfn, void *data)
1257{
1258 GElf_Phdr phdr;
1259 size_t i, phdrnum;
1260 int err;
1261 u64 sz;
1262
1263 if (elf_getphdrnum(elf, &phdrnum))
1264 return -1;
1265
1266 for (i = 0; i < phdrnum; i++) {
1267 if (gelf_getphdr(elf, i, &phdr) == NULL)
1268 return -1;
1269 if (phdr.p_type != PT_LOAD)
1270 continue;
1271 if (exe) {
1272 if (!(phdr.p_flags & PF_X))
1273 continue;
1274 } else {
1275 if (!(phdr.p_flags & PF_R))
1276 continue;
1277 }
1278 sz = min(phdr.p_memsz, phdr.p_filesz);
1279 if (!sz)
1280 continue;
1281 err = mapfn(phdr.p_vaddr, sz, phdr.p_offset, data);
1282 if (err)
1283 return err;
1284 }
1285 return 0;
1286}
1287
1288int file__read_maps(int fd, bool exe, mapfn_t mapfn, void *data,
1289 bool *is_64_bit)
1290{
1291 int err;
1292 Elf *elf;
1293
1294 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
1295 if (elf == NULL)
1296 return -1;
1297
1298 if (is_64_bit)
1299 *is_64_bit = (gelf_getclass(elf) == ELFCLASS64);
1300
1301 err = elf_read_maps(elf, exe, mapfn, data);
1302
1303 elf_end(elf);
1304 return err;
1305}
1306
Adrian Hunter2b5b8bb2014-07-22 16:17:59 +03001307enum dso_type dso__type_fd(int fd)
1308{
1309 enum dso_type dso_type = DSO__TYPE_UNKNOWN;
1310 GElf_Ehdr ehdr;
1311 Elf_Kind ek;
1312 Elf *elf;
1313
1314 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
1315 if (elf == NULL)
1316 goto out;
1317
1318 ek = elf_kind(elf);
1319 if (ek != ELF_K_ELF)
1320 goto out_end;
1321
1322 if (gelf_getclass(elf) == ELFCLASS64) {
1323 dso_type = DSO__TYPE_64BIT;
1324 goto out_end;
1325 }
1326
1327 if (gelf_getehdr(elf, &ehdr) == NULL)
1328 goto out_end;
1329
1330 if (ehdr.e_machine == EM_X86_64)
1331 dso_type = DSO__TYPE_X32BIT;
1332 else
1333 dso_type = DSO__TYPE_32BIT;
1334out_end:
1335 elf_end(elf);
1336out:
1337 return dso_type;
1338}
1339
Adrian Hunterafba19d2013-10-09 15:01:12 +03001340static int copy_bytes(int from, off_t from_offs, int to, off_t to_offs, u64 len)
1341{
1342 ssize_t r;
1343 size_t n;
1344 int err = -1;
1345 char *buf = malloc(page_size);
1346
1347 if (buf == NULL)
1348 return -1;
1349
1350 if (lseek(to, to_offs, SEEK_SET) != to_offs)
1351 goto out;
1352
1353 if (lseek(from, from_offs, SEEK_SET) != from_offs)
1354 goto out;
1355
1356 while (len) {
1357 n = page_size;
1358 if (len < n)
1359 n = len;
1360 /* Use read because mmap won't work on proc files */
1361 r = read(from, buf, n);
1362 if (r < 0)
1363 goto out;
1364 if (!r)
1365 break;
1366 n = r;
1367 r = write(to, buf, n);
1368 if (r < 0)
1369 goto out;
1370 if ((size_t)r != n)
1371 goto out;
1372 len -= n;
1373 }
1374
1375 err = 0;
1376out:
1377 free(buf);
1378 return err;
1379}
1380
1381struct kcore {
1382 int fd;
1383 int elfclass;
1384 Elf *elf;
1385 GElf_Ehdr ehdr;
1386};
1387
1388static int kcore__open(struct kcore *kcore, const char *filename)
1389{
1390 GElf_Ehdr *ehdr;
1391
1392 kcore->fd = open(filename, O_RDONLY);
1393 if (kcore->fd == -1)
1394 return -1;
1395
1396 kcore->elf = elf_begin(kcore->fd, ELF_C_READ, NULL);
1397 if (!kcore->elf)
1398 goto out_close;
1399
1400 kcore->elfclass = gelf_getclass(kcore->elf);
1401 if (kcore->elfclass == ELFCLASSNONE)
1402 goto out_end;
1403
1404 ehdr = gelf_getehdr(kcore->elf, &kcore->ehdr);
1405 if (!ehdr)
1406 goto out_end;
1407
1408 return 0;
1409
1410out_end:
1411 elf_end(kcore->elf);
1412out_close:
1413 close(kcore->fd);
1414 return -1;
1415}
1416
1417static int kcore__init(struct kcore *kcore, char *filename, int elfclass,
1418 bool temp)
1419{
Adrian Hunterafba19d2013-10-09 15:01:12 +03001420 kcore->elfclass = elfclass;
1421
1422 if (temp)
1423 kcore->fd = mkstemp(filename);
1424 else
1425 kcore->fd = open(filename, O_WRONLY | O_CREAT | O_EXCL, 0400);
1426 if (kcore->fd == -1)
1427 return -1;
1428
1429 kcore->elf = elf_begin(kcore->fd, ELF_C_WRITE, NULL);
1430 if (!kcore->elf)
1431 goto out_close;
1432
1433 if (!gelf_newehdr(kcore->elf, elfclass))
1434 goto out_end;
1435
Adrian Hunterb5cabbc2015-09-24 13:05:22 +03001436 memset(&kcore->ehdr, 0, sizeof(GElf_Ehdr));
Adrian Hunterafba19d2013-10-09 15:01:12 +03001437
1438 return 0;
1439
1440out_end:
1441 elf_end(kcore->elf);
1442out_close:
1443 close(kcore->fd);
1444 unlink(filename);
1445 return -1;
1446}
1447
1448static void kcore__close(struct kcore *kcore)
1449{
1450 elf_end(kcore->elf);
1451 close(kcore->fd);
1452}
1453
1454static int kcore__copy_hdr(struct kcore *from, struct kcore *to, size_t count)
1455{
1456 GElf_Ehdr *ehdr = &to->ehdr;
1457 GElf_Ehdr *kehdr = &from->ehdr;
1458
1459 memcpy(ehdr->e_ident, kehdr->e_ident, EI_NIDENT);
1460 ehdr->e_type = kehdr->e_type;
1461 ehdr->e_machine = kehdr->e_machine;
1462 ehdr->e_version = kehdr->e_version;
1463 ehdr->e_entry = 0;
1464 ehdr->e_shoff = 0;
1465 ehdr->e_flags = kehdr->e_flags;
1466 ehdr->e_phnum = count;
1467 ehdr->e_shentsize = 0;
1468 ehdr->e_shnum = 0;
1469 ehdr->e_shstrndx = 0;
1470
1471 if (from->elfclass == ELFCLASS32) {
1472 ehdr->e_phoff = sizeof(Elf32_Ehdr);
1473 ehdr->e_ehsize = sizeof(Elf32_Ehdr);
1474 ehdr->e_phentsize = sizeof(Elf32_Phdr);
1475 } else {
1476 ehdr->e_phoff = sizeof(Elf64_Ehdr);
1477 ehdr->e_ehsize = sizeof(Elf64_Ehdr);
1478 ehdr->e_phentsize = sizeof(Elf64_Phdr);
1479 }
1480
1481 if (!gelf_update_ehdr(to->elf, ehdr))
1482 return -1;
1483
1484 if (!gelf_newphdr(to->elf, count))
1485 return -1;
1486
1487 return 0;
1488}
1489
1490static int kcore__add_phdr(struct kcore *kcore, int idx, off_t offset,
1491 u64 addr, u64 len)
1492{
Adrian Hunterb5cabbc2015-09-24 13:05:22 +03001493 GElf_Phdr phdr = {
1494 .p_type = PT_LOAD,
1495 .p_flags = PF_R | PF_W | PF_X,
1496 .p_offset = offset,
1497 .p_vaddr = addr,
1498 .p_paddr = 0,
1499 .p_filesz = len,
1500 .p_memsz = len,
1501 .p_align = page_size,
1502 };
Adrian Hunterafba19d2013-10-09 15:01:12 +03001503
Adrian Hunterb5cabbc2015-09-24 13:05:22 +03001504 if (!gelf_update_phdr(kcore->elf, idx, &phdr))
Adrian Hunterafba19d2013-10-09 15:01:12 +03001505 return -1;
1506
1507 return 0;
1508}
1509
1510static off_t kcore__write(struct kcore *kcore)
1511{
1512 return elf_update(kcore->elf, ELF_C_WRITE);
1513}
1514
Adrian Hunterfc1b6912013-10-14 16:57:29 +03001515struct phdr_data {
1516 off_t offset;
Adrian Hunter15acef62018-05-22 13:54:41 +03001517 off_t rel;
Adrian Hunterfc1b6912013-10-14 16:57:29 +03001518 u64 addr;
1519 u64 len;
Adrian Hunterf6838202018-05-22 13:54:38 +03001520 struct list_head node;
Adrian Hunter22916fd2018-05-22 13:54:45 +03001521 struct phdr_data *remaps;
Adrian Hunterfc1b6912013-10-14 16:57:29 +03001522};
1523
Adrian Huntera1a3a062018-05-22 13:54:44 +03001524struct sym_data {
1525 u64 addr;
1526 struct list_head node;
1527};
1528
Adrian Hunterfc1b6912013-10-14 16:57:29 +03001529struct kcore_copy_info {
1530 u64 stext;
1531 u64 etext;
1532 u64 first_symbol;
1533 u64 last_symbol;
1534 u64 first_module;
Adrian Hunter61f82e32020-05-12 15:19:16 +03001535 u64 first_module_symbol;
Adrian Hunterfc1b6912013-10-14 16:57:29 +03001536 u64 last_module_symbol;
Adrian Hunter6e979572018-05-22 13:54:39 +03001537 size_t phnum;
Adrian Hunterf6838202018-05-22 13:54:38 +03001538 struct list_head phdrs;
Adrian Huntera1a3a062018-05-22 13:54:44 +03001539 struct list_head syms;
Adrian Hunterfc1b6912013-10-14 16:57:29 +03001540};
1541
Adrian Hunter15acef62018-05-22 13:54:41 +03001542#define kcore_copy__for_each_phdr(k, p) \
1543 list_for_each_entry((p), &(k)->phdrs, node)
1544
Adrian Hunterb4503cd2018-05-22 13:54:43 +03001545static struct phdr_data *phdr_data__new(u64 addr, u64 len, off_t offset)
1546{
1547 struct phdr_data *p = zalloc(sizeof(*p));
1548
1549 if (p) {
1550 p->addr = addr;
1551 p->len = len;
1552 p->offset = offset;
1553 }
1554
1555 return p;
1556}
1557
1558static struct phdr_data *kcore_copy_info__addnew(struct kcore_copy_info *kci,
1559 u64 addr, u64 len,
1560 off_t offset)
1561{
1562 struct phdr_data *p = phdr_data__new(addr, len, offset);
1563
1564 if (p)
1565 list_add_tail(&p->node, &kci->phdrs);
1566
1567 return p;
1568}
1569
1570static void kcore_copy__free_phdrs(struct kcore_copy_info *kci)
1571{
1572 struct phdr_data *p, *tmp;
1573
1574 list_for_each_entry_safe(p, tmp, &kci->phdrs, node) {
Arnaldo Carvalho de Meloe56fbc92019-07-04 12:13:46 -03001575 list_del_init(&p->node);
Adrian Hunterb4503cd2018-05-22 13:54:43 +03001576 free(p);
1577 }
1578}
1579
Adrian Huntera1a3a062018-05-22 13:54:44 +03001580static struct sym_data *kcore_copy__new_sym(struct kcore_copy_info *kci,
1581 u64 addr)
1582{
1583 struct sym_data *s = zalloc(sizeof(*s));
1584
1585 if (s) {
1586 s->addr = addr;
1587 list_add_tail(&s->node, &kci->syms);
1588 }
1589
1590 return s;
1591}
1592
1593static void kcore_copy__free_syms(struct kcore_copy_info *kci)
1594{
1595 struct sym_data *s, *tmp;
1596
1597 list_for_each_entry_safe(s, tmp, &kci->syms, node) {
Arnaldo Carvalho de Meloe56fbc92019-07-04 12:13:46 -03001598 list_del_init(&s->node);
Adrian Huntera1a3a062018-05-22 13:54:44 +03001599 free(s);
1600 }
1601}
1602
Adrian Hunterfc1b6912013-10-14 16:57:29 +03001603static int kcore_copy__process_kallsyms(void *arg, const char *name, char type,
1604 u64 start)
1605{
1606 struct kcore_copy_info *kci = arg;
1607
Arnaldo Carvalho de Meloe85e0e02018-04-25 17:16:31 -03001608 if (!kallsyms__is_function(type))
Adrian Hunterfc1b6912013-10-14 16:57:29 +03001609 return 0;
1610
1611 if (strchr(name, '[')) {
Adrian Hunter61f82e32020-05-12 15:19:16 +03001612 if (!kci->first_module_symbol || start < kci->first_module_symbol)
1613 kci->first_module_symbol = start;
Adrian Hunterfc1b6912013-10-14 16:57:29 +03001614 if (start > kci->last_module_symbol)
1615 kci->last_module_symbol = start;
1616 return 0;
1617 }
1618
1619 if (!kci->first_symbol || start < kci->first_symbol)
1620 kci->first_symbol = start;
1621
1622 if (!kci->last_symbol || start > kci->last_symbol)
1623 kci->last_symbol = start;
1624
1625 if (!strcmp(name, "_stext")) {
1626 kci->stext = start;
1627 return 0;
1628 }
1629
1630 if (!strcmp(name, "_etext")) {
1631 kci->etext = start;
1632 return 0;
1633 }
1634
Adrian Huntera1a3a062018-05-22 13:54:44 +03001635 if (is_entry_trampoline(name) && !kcore_copy__new_sym(kci, start))
1636 return -1;
1637
Adrian Hunterfc1b6912013-10-14 16:57:29 +03001638 return 0;
1639}
1640
1641static int kcore_copy__parse_kallsyms(struct kcore_copy_info *kci,
1642 const char *dir)
1643{
1644 char kallsyms_filename[PATH_MAX];
1645
1646 scnprintf(kallsyms_filename, PATH_MAX, "%s/kallsyms", dir);
1647
1648 if (symbol__restricted_filename(kallsyms_filename, "/proc/kallsyms"))
1649 return -1;
1650
1651 if (kallsyms__parse(kallsyms_filename, kci,
1652 kcore_copy__process_kallsyms) < 0)
1653 return -1;
1654
1655 return 0;
1656}
1657
1658static int kcore_copy__process_modules(void *arg,
1659 const char *name __maybe_unused,
Thomas Richter9ad46522017-08-03 15:49:02 +02001660 u64 start, u64 size __maybe_unused)
Adrian Hunterfc1b6912013-10-14 16:57:29 +03001661{
1662 struct kcore_copy_info *kci = arg;
1663
1664 if (!kci->first_module || start < kci->first_module)
1665 kci->first_module = start;
1666
1667 return 0;
1668}
1669
1670static int kcore_copy__parse_modules(struct kcore_copy_info *kci,
1671 const char *dir)
1672{
1673 char modules_filename[PATH_MAX];
1674
1675 scnprintf(modules_filename, PATH_MAX, "%s/modules", dir);
1676
1677 if (symbol__restricted_filename(modules_filename, "/proc/modules"))
1678 return -1;
1679
1680 if (modules__parse(modules_filename, kci,
1681 kcore_copy__process_modules) < 0)
1682 return -1;
1683
1684 return 0;
1685}
1686
Adrian Hunterb4503cd2018-05-22 13:54:43 +03001687static int kcore_copy__map(struct kcore_copy_info *kci, u64 start, u64 end,
1688 u64 pgoff, u64 s, u64 e)
Adrian Hunterfc1b6912013-10-14 16:57:29 +03001689{
Adrian Hunterb4503cd2018-05-22 13:54:43 +03001690 u64 len, offset;
Adrian Hunterfc1b6912013-10-14 16:57:29 +03001691
Adrian Hunterb4503cd2018-05-22 13:54:43 +03001692 if (s < start || s >= end)
1693 return 0;
1694
1695 offset = (s - start) + pgoff;
1696 len = e < end ? e - s : end - s;
1697
1698 return kcore_copy_info__addnew(kci, s, len, offset) ? 0 : -1;
Adrian Hunterfc1b6912013-10-14 16:57:29 +03001699}
1700
1701static int kcore_copy__read_map(u64 start, u64 len, u64 pgoff, void *data)
1702{
1703 struct kcore_copy_info *kci = data;
1704 u64 end = start + len;
Adrian Huntera1a3a062018-05-22 13:54:44 +03001705 struct sym_data *sdat;
Adrian Hunterfc1b6912013-10-14 16:57:29 +03001706
Adrian Hunterb4503cd2018-05-22 13:54:43 +03001707 if (kcore_copy__map(kci, start, end, pgoff, kci->stext, kci->etext))
1708 return -1;
Adrian Hunterfc1b6912013-10-14 16:57:29 +03001709
Adrian Hunterb4503cd2018-05-22 13:54:43 +03001710 if (kcore_copy__map(kci, start, end, pgoff, kci->first_module,
1711 kci->last_module_symbol))
1712 return -1;
Adrian Hunterfc1b6912013-10-14 16:57:29 +03001713
Adrian Huntera1a3a062018-05-22 13:54:44 +03001714 list_for_each_entry(sdat, &kci->syms, node) {
1715 u64 s = round_down(sdat->addr, page_size);
1716
1717 if (kcore_copy__map(kci, start, end, pgoff, s, s + len))
1718 return -1;
1719 }
1720
Adrian Hunterfc1b6912013-10-14 16:57:29 +03001721 return 0;
1722}
1723
1724static int kcore_copy__read_maps(struct kcore_copy_info *kci, Elf *elf)
1725{
1726 if (elf_read_maps(elf, true, kcore_copy__read_map, kci) < 0)
1727 return -1;
1728
1729 return 0;
1730}
1731
Adrian Hunter22916fd2018-05-22 13:54:45 +03001732static void kcore_copy__find_remaps(struct kcore_copy_info *kci)
1733{
1734 struct phdr_data *p, *k = NULL;
1735 u64 kend;
1736
1737 if (!kci->stext)
1738 return;
1739
1740 /* Find phdr that corresponds to the kernel map (contains stext) */
1741 kcore_copy__for_each_phdr(kci, p) {
1742 u64 pend = p->addr + p->len - 1;
1743
1744 if (p->addr <= kci->stext && pend >= kci->stext) {
1745 k = p;
1746 break;
1747 }
1748 }
1749
1750 if (!k)
1751 return;
1752
1753 kend = k->offset + k->len;
1754
1755 /* Find phdrs that remap the kernel */
1756 kcore_copy__for_each_phdr(kci, p) {
1757 u64 pend = p->offset + p->len;
1758
1759 if (p == k)
1760 continue;
1761
1762 if (p->offset >= k->offset && pend <= kend)
1763 p->remaps = k;
1764 }
1765}
1766
Adrian Hunter15acef62018-05-22 13:54:41 +03001767static void kcore_copy__layout(struct kcore_copy_info *kci)
1768{
1769 struct phdr_data *p;
1770 off_t rel = 0;
1771
Adrian Hunter22916fd2018-05-22 13:54:45 +03001772 kcore_copy__find_remaps(kci);
1773
Adrian Hunter15acef62018-05-22 13:54:41 +03001774 kcore_copy__for_each_phdr(kci, p) {
Adrian Hunter22916fd2018-05-22 13:54:45 +03001775 if (!p->remaps) {
1776 p->rel = rel;
1777 rel += p->len;
1778 }
Adrian Hunter15acef62018-05-22 13:54:41 +03001779 kci->phnum += 1;
1780 }
Adrian Hunter22916fd2018-05-22 13:54:45 +03001781
1782 kcore_copy__for_each_phdr(kci, p) {
1783 struct phdr_data *k = p->remaps;
1784
1785 if (k)
1786 p->rel = p->offset - k->offset + k->rel;
1787 }
Adrian Hunter15acef62018-05-22 13:54:41 +03001788}
1789
Adrian Hunterfc1b6912013-10-14 16:57:29 +03001790static int kcore_copy__calc_maps(struct kcore_copy_info *kci, const char *dir,
1791 Elf *elf)
1792{
1793 if (kcore_copy__parse_kallsyms(kci, dir))
1794 return -1;
1795
1796 if (kcore_copy__parse_modules(kci, dir))
1797 return -1;
1798
1799 if (kci->stext)
1800 kci->stext = round_down(kci->stext, page_size);
1801 else
1802 kci->stext = round_down(kci->first_symbol, page_size);
1803
1804 if (kci->etext) {
1805 kci->etext = round_up(kci->etext, page_size);
1806 } else if (kci->last_symbol) {
1807 kci->etext = round_up(kci->last_symbol, page_size);
1808 kci->etext += page_size;
1809 }
1810
Adrian Hunter61f82e32020-05-12 15:19:16 +03001811 if (kci->first_module_symbol &&
1812 (!kci->first_module || kci->first_module_symbol < kci->first_module))
1813 kci->first_module = kci->first_module_symbol;
1814
Adrian Hunterfc1b6912013-10-14 16:57:29 +03001815 kci->first_module = round_down(kci->first_module, page_size);
1816
1817 if (kci->last_module_symbol) {
1818 kci->last_module_symbol = round_up(kci->last_module_symbol,
1819 page_size);
1820 kci->last_module_symbol += page_size;
1821 }
1822
1823 if (!kci->stext || !kci->etext)
1824 return -1;
1825
1826 if (kci->first_module && !kci->last_module_symbol)
1827 return -1;
1828
Adrian Hunter15acef62018-05-22 13:54:41 +03001829 if (kcore_copy__read_maps(kci, elf))
1830 return -1;
1831
1832 kcore_copy__layout(kci);
1833
1834 return 0;
Adrian Hunterfc1b6912013-10-14 16:57:29 +03001835}
1836
1837static int kcore_copy__copy_file(const char *from_dir, const char *to_dir,
1838 const char *name)
1839{
1840 char from_filename[PATH_MAX];
1841 char to_filename[PATH_MAX];
1842
1843 scnprintf(from_filename, PATH_MAX, "%s/%s", from_dir, name);
1844 scnprintf(to_filename, PATH_MAX, "%s/%s", to_dir, name);
1845
1846 return copyfile_mode(from_filename, to_filename, 0400);
1847}
1848
1849static int kcore_copy__unlink(const char *dir, const char *name)
1850{
1851 char filename[PATH_MAX];
1852
1853 scnprintf(filename, PATH_MAX, "%s/%s", dir, name);
1854
1855 return unlink(filename);
1856}
1857
1858static int kcore_copy__compare_fds(int from, int to)
1859{
1860 char *buf_from;
1861 char *buf_to;
1862 ssize_t ret;
1863 size_t len;
1864 int err = -1;
1865
1866 buf_from = malloc(page_size);
1867 buf_to = malloc(page_size);
1868 if (!buf_from || !buf_to)
1869 goto out;
1870
1871 while (1) {
1872 /* Use read because mmap won't work on proc files */
1873 ret = read(from, buf_from, page_size);
1874 if (ret < 0)
1875 goto out;
1876
1877 if (!ret)
1878 break;
1879
1880 len = ret;
1881
1882 if (readn(to, buf_to, len) != (int)len)
1883 goto out;
1884
1885 if (memcmp(buf_from, buf_to, len))
1886 goto out;
1887 }
1888
1889 err = 0;
1890out:
1891 free(buf_to);
1892 free(buf_from);
1893 return err;
1894}
1895
1896static int kcore_copy__compare_files(const char *from_filename,
1897 const char *to_filename)
1898{
1899 int from, to, err = -1;
1900
1901 from = open(from_filename, O_RDONLY);
1902 if (from < 0)
1903 return -1;
1904
1905 to = open(to_filename, O_RDONLY);
1906 if (to < 0)
1907 goto out_close_from;
1908
1909 err = kcore_copy__compare_fds(from, to);
1910
1911 close(to);
1912out_close_from:
1913 close(from);
1914 return err;
1915}
1916
1917static int kcore_copy__compare_file(const char *from_dir, const char *to_dir,
1918 const char *name)
1919{
1920 char from_filename[PATH_MAX];
1921 char to_filename[PATH_MAX];
1922
1923 scnprintf(from_filename, PATH_MAX, "%s/%s", from_dir, name);
1924 scnprintf(to_filename, PATH_MAX, "%s/%s", to_dir, name);
1925
1926 return kcore_copy__compare_files(from_filename, to_filename);
1927}
1928
1929/**
1930 * kcore_copy - copy kallsyms, modules and kcore from one directory to another.
1931 * @from_dir: from directory
1932 * @to_dir: to directory
1933 *
1934 * This function copies kallsyms, modules and kcore files from one directory to
1935 * another. kallsyms and modules are copied entirely. Only code segments are
1936 * copied from kcore. It is assumed that two segments suffice: one for the
1937 * kernel proper and one for all the modules. The code segments are determined
1938 * from kallsyms and modules files. The kernel map starts at _stext or the
1939 * lowest function symbol, and ends at _etext or the highest function symbol.
1940 * The module map starts at the lowest module address and ends at the highest
1941 * module symbol. Start addresses are rounded down to the nearest page. End
1942 * addresses are rounded up to the nearest page. An extra page is added to the
1943 * highest kernel symbol and highest module symbol to, hopefully, encompass that
1944 * symbol too. Because it contains only code sections, the resulting kcore is
1945 * unusual. One significant peculiarity is that the mapping (start -> pgoff)
1946 * is not the same for the kernel map and the modules map. That happens because
1947 * the data is copied adjacently whereas the original kcore has gaps. Finally,
1948 * kallsyms and modules files are compared with their copies to check that
1949 * modules have not been loaded or unloaded while the copies were taking place.
1950 *
1951 * Return: %0 on success, %-1 on failure.
1952 */
1953int kcore_copy(const char *from_dir, const char *to_dir)
1954{
1955 struct kcore kcore;
1956 struct kcore extract;
Adrian Hunterfc1b6912013-10-14 16:57:29 +03001957 int idx = 0, err = -1;
Adrian Hunterd2c95982018-05-22 13:54:42 +03001958 off_t offset, sz;
Adrian Hunterfc1b6912013-10-14 16:57:29 +03001959 struct kcore_copy_info kci = { .stext = 0, };
1960 char kcore_filename[PATH_MAX];
1961 char extract_filename[PATH_MAX];
Adrian Hunterd2c95982018-05-22 13:54:42 +03001962 struct phdr_data *p;
Adrian Hunterfc1b6912013-10-14 16:57:29 +03001963
Adrian Hunterf6838202018-05-22 13:54:38 +03001964 INIT_LIST_HEAD(&kci.phdrs);
Adrian Huntera1a3a062018-05-22 13:54:44 +03001965 INIT_LIST_HEAD(&kci.syms);
Adrian Hunterf6838202018-05-22 13:54:38 +03001966
Adrian Hunterfc1b6912013-10-14 16:57:29 +03001967 if (kcore_copy__copy_file(from_dir, to_dir, "kallsyms"))
1968 return -1;
1969
1970 if (kcore_copy__copy_file(from_dir, to_dir, "modules"))
1971 goto out_unlink_kallsyms;
1972
1973 scnprintf(kcore_filename, PATH_MAX, "%s/kcore", from_dir);
1974 scnprintf(extract_filename, PATH_MAX, "%s/kcore", to_dir);
1975
1976 if (kcore__open(&kcore, kcore_filename))
1977 goto out_unlink_modules;
1978
1979 if (kcore_copy__calc_maps(&kci, from_dir, kcore.elf))
1980 goto out_kcore_close;
1981
1982 if (kcore__init(&extract, extract_filename, kcore.elfclass, false))
1983 goto out_kcore_close;
1984
Adrian Hunter6e979572018-05-22 13:54:39 +03001985 if (kcore__copy_hdr(&kcore, &extract, kci.phnum))
Adrian Hunterfc1b6912013-10-14 16:57:29 +03001986 goto out_extract_close;
1987
Adrian Hunterc9dd1d82018-05-22 13:54:40 +03001988 offset = gelf_fsize(extract.elf, ELF_T_EHDR, 1, EV_CURRENT) +
1989 gelf_fsize(extract.elf, ELF_T_PHDR, kci.phnum, EV_CURRENT);
1990 offset = round_up(offset, page_size);
1991
Adrian Hunterd2c95982018-05-22 13:54:42 +03001992 kcore_copy__for_each_phdr(&kci, p) {
1993 off_t offs = p->rel + offset;
Adrian Hunterfc1b6912013-10-14 16:57:29 +03001994
Adrian Hunterd2c95982018-05-22 13:54:42 +03001995 if (kcore__add_phdr(&extract, idx++, offs, p->addr, p->len))
Adrian Hunterfc1b6912013-10-14 16:57:29 +03001996 goto out_extract_close;
1997 }
1998
1999 sz = kcore__write(&extract);
2000 if (sz < 0 || sz > offset)
2001 goto out_extract_close;
2002
Adrian Hunterd2c95982018-05-22 13:54:42 +03002003 kcore_copy__for_each_phdr(&kci, p) {
2004 off_t offs = p->rel + offset;
Adrian Hunterfc1b6912013-10-14 16:57:29 +03002005
Adrian Hunter22916fd2018-05-22 13:54:45 +03002006 if (p->remaps)
2007 continue;
Adrian Hunterd2c95982018-05-22 13:54:42 +03002008 if (copy_bytes(kcore.fd, p->offset, extract.fd, offs, p->len))
2009 goto out_extract_close;
2010 }
Adrian Hunterfc1b6912013-10-14 16:57:29 +03002011
2012 if (kcore_copy__compare_file(from_dir, to_dir, "modules"))
2013 goto out_extract_close;
2014
2015 if (kcore_copy__compare_file(from_dir, to_dir, "kallsyms"))
2016 goto out_extract_close;
2017
2018 err = 0;
2019
2020out_extract_close:
2021 kcore__close(&extract);
2022 if (err)
2023 unlink(extract_filename);
2024out_kcore_close:
2025 kcore__close(&kcore);
2026out_unlink_modules:
2027 if (err)
2028 kcore_copy__unlink(to_dir, "modules");
2029out_unlink_kallsyms:
2030 if (err)
2031 kcore_copy__unlink(to_dir, "kallsyms");
2032
Adrian Hunterb4503cd2018-05-22 13:54:43 +03002033 kcore_copy__free_phdrs(&kci);
Adrian Huntera1a3a062018-05-22 13:54:44 +03002034 kcore_copy__free_syms(&kci);
Adrian Hunterb4503cd2018-05-22 13:54:43 +03002035
Adrian Hunterfc1b6912013-10-14 16:57:29 +03002036 return err;
2037}
2038
Adrian Hunterafba19d2013-10-09 15:01:12 +03002039int kcore_extract__create(struct kcore_extract *kce)
2040{
2041 struct kcore kcore;
2042 struct kcore extract;
2043 size_t count = 1;
2044 int idx = 0, err = -1;
2045 off_t offset = page_size, sz;
2046
2047 if (kcore__open(&kcore, kce->kcore_filename))
2048 return -1;
2049
2050 strcpy(kce->extract_filename, PERF_KCORE_EXTRACT);
2051 if (kcore__init(&extract, kce->extract_filename, kcore.elfclass, true))
2052 goto out_kcore_close;
2053
2054 if (kcore__copy_hdr(&kcore, &extract, count))
2055 goto out_extract_close;
2056
2057 if (kcore__add_phdr(&extract, idx, offset, kce->addr, kce->len))
2058 goto out_extract_close;
2059
2060 sz = kcore__write(&extract);
2061 if (sz < 0 || sz > offset)
2062 goto out_extract_close;
2063
2064 if (copy_bytes(kcore.fd, kce->offs, extract.fd, offset, kce->len))
2065 goto out_extract_close;
2066
2067 err = 0;
2068
2069out_extract_close:
2070 kcore__close(&extract);
2071 if (err)
2072 unlink(kce->extract_filename);
2073out_kcore_close:
2074 kcore__close(&kcore);
2075
2076 return err;
2077}
2078
2079void kcore_extract__delete(struct kcore_extract *kce)
2080{
2081 unlink(kce->extract_filename);
2082}
2083
Arnaldo Carvalho de Melo1c1a3a42016-07-12 12:19:09 -03002084#ifdef HAVE_GELF_GETNOTE_SUPPORT
Ravi Bangoria5a5e3d32018-08-20 10:12:50 +05302085
2086static void sdt_adjust_loc(struct sdt_note *tmp, GElf_Addr base_off)
2087{
2088 if (!base_off)
2089 return;
2090
2091 if (tmp->bit32)
2092 tmp->addr.a32[SDT_NOTE_IDX_LOC] =
2093 tmp->addr.a32[SDT_NOTE_IDX_LOC] + base_off -
2094 tmp->addr.a32[SDT_NOTE_IDX_BASE];
2095 else
2096 tmp->addr.a64[SDT_NOTE_IDX_LOC] =
2097 tmp->addr.a64[SDT_NOTE_IDX_LOC] + base_off -
2098 tmp->addr.a64[SDT_NOTE_IDX_BASE];
2099}
2100
2101static void sdt_adjust_refctr(struct sdt_note *tmp, GElf_Addr base_addr,
2102 GElf_Addr base_off)
2103{
2104 if (!base_off)
2105 return;
2106
2107 if (tmp->bit32 && tmp->addr.a32[SDT_NOTE_IDX_REFCTR])
2108 tmp->addr.a32[SDT_NOTE_IDX_REFCTR] -= (base_addr - base_off);
2109 else if (tmp->addr.a64[SDT_NOTE_IDX_REFCTR])
2110 tmp->addr.a64[SDT_NOTE_IDX_REFCTR] -= (base_addr - base_off);
2111}
2112
Hemant Kumar060fa0c2016-07-01 17:03:46 +09002113/**
2114 * populate_sdt_note : Parse raw data and identify SDT note
2115 * @elf: elf of the opened file
2116 * @data: raw data of a section with description offset applied
2117 * @len: note description size
2118 * @type: type of the note
2119 * @sdt_notes: List to add the SDT note
2120 *
2121 * Responsible for parsing the @data in section .note.stapsdt in @elf and
2122 * if its an SDT note, it appends to @sdt_notes list.
2123 */
2124static int populate_sdt_note(Elf **elf, const char *data, size_t len,
2125 struct list_head *sdt_notes)
2126{
Alexis Berlemontbe881842016-12-14 01:07:31 +01002127 const char *provider, *name, *args;
Hemant Kumar060fa0c2016-07-01 17:03:46 +09002128 struct sdt_note *tmp = NULL;
2129 GElf_Ehdr ehdr;
Hemant Kumar060fa0c2016-07-01 17:03:46 +09002130 GElf_Shdr shdr;
2131 int ret = -EINVAL;
2132
2133 union {
2134 Elf64_Addr a64[NR_ADDR];
2135 Elf32_Addr a32[NR_ADDR];
2136 } buf;
2137
2138 Elf_Data dst = {
2139 .d_buf = &buf, .d_type = ELF_T_ADDR, .d_version = EV_CURRENT,
2140 .d_size = gelf_fsize((*elf), ELF_T_ADDR, NR_ADDR, EV_CURRENT),
2141 .d_off = 0, .d_align = 0
2142 };
2143 Elf_Data src = {
2144 .d_buf = (void *) data, .d_type = ELF_T_ADDR,
2145 .d_version = EV_CURRENT, .d_size = dst.d_size, .d_off = 0,
2146 .d_align = 0
2147 };
2148
2149 tmp = (struct sdt_note *)calloc(1, sizeof(struct sdt_note));
2150 if (!tmp) {
2151 ret = -ENOMEM;
2152 goto out_err;
2153 }
2154
2155 INIT_LIST_HEAD(&tmp->note_list);
2156
2157 if (len < dst.d_size + 3)
2158 goto out_free_note;
2159
2160 /* Translation from file representation to memory representation */
2161 if (gelf_xlatetom(*elf, &dst, &src,
2162 elf_getident(*elf, NULL)[EI_DATA]) == NULL) {
2163 pr_err("gelf_xlatetom : %s\n", elf_errmsg(-1));
2164 goto out_free_note;
2165 }
2166
2167 /* Populate the fields of sdt_note */
2168 provider = data + dst.d_size;
2169
2170 name = (const char *)memchr(provider, '\0', data + len - provider);
2171 if (name++ == NULL)
2172 goto out_free_note;
2173
2174 tmp->provider = strdup(provider);
2175 if (!tmp->provider) {
2176 ret = -ENOMEM;
2177 goto out_free_note;
2178 }
2179 tmp->name = strdup(name);
2180 if (!tmp->name) {
2181 ret = -ENOMEM;
2182 goto out_free_prov;
2183 }
2184
Alexis Berlemontbe881842016-12-14 01:07:31 +01002185 args = memchr(name, '\0', data + len - name);
2186
2187 /*
2188 * There is no argument if:
2189 * - We reached the end of the note;
2190 * - There is not enough room to hold a potential string;
2191 * - The argument string is empty or just contains ':'.
2192 */
2193 if (args == NULL || data + len - args < 2 ||
2194 args[1] == ':' || args[1] == '\0')
2195 tmp->args = NULL;
2196 else {
2197 tmp->args = strdup(++args);
2198 if (!tmp->args) {
2199 ret = -ENOMEM;
2200 goto out_free_name;
2201 }
2202 }
2203
Hemant Kumar060fa0c2016-07-01 17:03:46 +09002204 if (gelf_getclass(*elf) == ELFCLASS32) {
2205 memcpy(&tmp->addr, &buf, 3 * sizeof(Elf32_Addr));
2206 tmp->bit32 = true;
2207 } else {
2208 memcpy(&tmp->addr, &buf, 3 * sizeof(Elf64_Addr));
2209 tmp->bit32 = false;
2210 }
2211
2212 if (!gelf_getehdr(*elf, &ehdr)) {
2213 pr_debug("%s : cannot get elf header.\n", __func__);
2214 ret = -EBADF;
Alexis Berlemontbe881842016-12-14 01:07:31 +01002215 goto out_free_args;
Hemant Kumar060fa0c2016-07-01 17:03:46 +09002216 }
2217
2218 /* Adjust the prelink effect :
2219 * Find out the .stapsdt.base section.
2220 * This scn will help us to handle prelinking (if present).
2221 * Compare the retrieved file offset of the base section with the
2222 * base address in the description of the SDT note. If its different,
2223 * then accordingly, adjust the note location.
2224 */
Ravi Bangoria5a5e3d32018-08-20 10:12:50 +05302225 if (elf_section_by_name(*elf, &ehdr, &shdr, SDT_BASE_SCN, NULL))
2226 sdt_adjust_loc(tmp, shdr.sh_offset);
2227
2228 /* Adjust reference counter offset */
2229 if (elf_section_by_name(*elf, &ehdr, &shdr, SDT_PROBES_SCN, NULL))
2230 sdt_adjust_refctr(tmp, shdr.sh_addr, shdr.sh_offset);
Hemant Kumar060fa0c2016-07-01 17:03:46 +09002231
2232 list_add_tail(&tmp->note_list, sdt_notes);
2233 return 0;
2234
Alexis Berlemontbe881842016-12-14 01:07:31 +01002235out_free_args:
Arnaldo Carvalho de Melod8f9da22019-07-04 12:06:20 -03002236 zfree(&tmp->args);
Hemant Kumar060fa0c2016-07-01 17:03:46 +09002237out_free_name:
Arnaldo Carvalho de Melod8f9da22019-07-04 12:06:20 -03002238 zfree(&tmp->name);
Hemant Kumar060fa0c2016-07-01 17:03:46 +09002239out_free_prov:
Arnaldo Carvalho de Melod8f9da22019-07-04 12:06:20 -03002240 zfree(&tmp->provider);
Hemant Kumar060fa0c2016-07-01 17:03:46 +09002241out_free_note:
2242 free(tmp);
2243out_err:
2244 return ret;
2245}
2246
2247/**
2248 * construct_sdt_notes_list : constructs a list of SDT notes
2249 * @elf : elf to look into
2250 * @sdt_notes : empty list_head
2251 *
2252 * Scans the sections in 'elf' for the section
2253 * .note.stapsdt. It, then calls populate_sdt_note to find
2254 * out the SDT events and populates the 'sdt_notes'.
2255 */
2256static int construct_sdt_notes_list(Elf *elf, struct list_head *sdt_notes)
2257{
2258 GElf_Ehdr ehdr;
2259 Elf_Scn *scn = NULL;
2260 Elf_Data *data;
2261 GElf_Shdr shdr;
2262 size_t shstrndx, next;
2263 GElf_Nhdr nhdr;
2264 size_t name_off, desc_off, offset;
2265 int ret = 0;
2266
2267 if (gelf_getehdr(elf, &ehdr) == NULL) {
2268 ret = -EBADF;
2269 goto out_ret;
2270 }
2271 if (elf_getshdrstrndx(elf, &shstrndx) != 0) {
2272 ret = -EBADF;
2273 goto out_ret;
2274 }
2275
2276 /* Look for the required section */
2277 scn = elf_section_by_name(elf, &ehdr, &shdr, SDT_NOTE_SCN, NULL);
2278 if (!scn) {
2279 ret = -ENOENT;
2280 goto out_ret;
2281 }
2282
2283 if ((shdr.sh_type != SHT_NOTE) || (shdr.sh_flags & SHF_ALLOC)) {
2284 ret = -ENOENT;
2285 goto out_ret;
2286 }
2287
2288 data = elf_getdata(scn, NULL);
2289
2290 /* Get the SDT notes */
2291 for (offset = 0; (next = gelf_getnote(data, offset, &nhdr, &name_off,
2292 &desc_off)) > 0; offset = next) {
2293 if (nhdr.n_namesz == sizeof(SDT_NOTE_NAME) &&
2294 !memcmp(data->d_buf + name_off, SDT_NOTE_NAME,
2295 sizeof(SDT_NOTE_NAME))) {
2296 /* Check the type of the note */
2297 if (nhdr.n_type != SDT_NOTE_TYPE)
2298 goto out_ret;
2299
2300 ret = populate_sdt_note(&elf, ((data->d_buf) + desc_off),
2301 nhdr.n_descsz, sdt_notes);
2302 if (ret < 0)
2303 goto out_ret;
2304 }
2305 }
2306 if (list_empty(sdt_notes))
2307 ret = -ENOENT;
2308
2309out_ret:
2310 return ret;
2311}
2312
2313/**
2314 * get_sdt_note_list : Wrapper to construct a list of sdt notes
2315 * @head : empty list_head
2316 * @target : file to find SDT notes from
2317 *
2318 * This opens the file, initializes
2319 * the ELF and then calls construct_sdt_notes_list.
2320 */
2321int get_sdt_note_list(struct list_head *head, const char *target)
2322{
2323 Elf *elf;
2324 int fd, ret;
2325
2326 fd = open(target, O_RDONLY);
2327 if (fd < 0)
2328 return -EBADF;
2329
2330 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
2331 if (!elf) {
2332 ret = -EBADF;
2333 goto out_close;
2334 }
2335 ret = construct_sdt_notes_list(elf, head);
2336 elf_end(elf);
2337out_close:
2338 close(fd);
2339 return ret;
2340}
2341
2342/**
2343 * cleanup_sdt_note_list : free the sdt notes' list
2344 * @sdt_notes: sdt notes' list
2345 *
2346 * Free up the SDT notes in @sdt_notes.
2347 * Returns the number of SDT notes free'd.
2348 */
2349int cleanup_sdt_note_list(struct list_head *sdt_notes)
2350{
2351 struct sdt_note *tmp, *pos;
2352 int nr_free = 0;
2353
2354 list_for_each_entry_safe(pos, tmp, sdt_notes, note_list) {
Arnaldo Carvalho de Meloe56fbc92019-07-04 12:13:46 -03002355 list_del_init(&pos->note_list);
Arnaldo Carvalho de Melod8f9da22019-07-04 12:06:20 -03002356 zfree(&pos->name);
2357 zfree(&pos->provider);
Hemant Kumar060fa0c2016-07-01 17:03:46 +09002358 free(pos);
2359 nr_free++;
2360 }
2361 return nr_free;
2362}
2363
2364/**
2365 * sdt_notes__get_count: Counts the number of sdt events
2366 * @start: list_head to sdt_notes list
2367 *
2368 * Returns the number of SDT notes in a list
2369 */
2370int sdt_notes__get_count(struct list_head *start)
2371{
2372 struct sdt_note *sdt_ptr;
2373 int count = 0;
2374
2375 list_for_each_entry(sdt_ptr, start, note_list)
2376 count++;
2377 return count;
2378}
Arnaldo Carvalho de Melo1c1a3a42016-07-12 12:19:09 -03002379#endif
Hemant Kumar060fa0c2016-07-01 17:03:46 +09002380
Namhyung Kime5a18452012-08-06 13:41:20 +09002381void symbol__elf_init(void)
2382{
2383 elf_version(EV_CURRENT);
2384}