blob: 79a6a190f41dfedb39f5c9691028c7d53555359f [file] [log] [blame]
Namhyung Kime5a18452012-08-06 13:41:20 +09001#include <fcntl.h>
2#include <stdio.h>
3#include <errno.h>
4#include <string.h>
5#include <unistd.h>
6#include <inttypes.h>
7
8#include "symbol.h"
Stephane Eraniane9c4bcd2015-11-30 10:02:20 +01009#include "demangle-java.h"
Waiman Long8fa7d872014-09-29 16:07:28 -040010#include "machine.h"
Vladimir Nikulichev922d0e42014-04-17 08:27:01 -070011#include "vdso.h"
Arnaldo Carvalho de Meloc506c962013-12-11 09:15:00 -030012#include <symbol/kallsyms.h>
Namhyung Kime5a18452012-08-06 13:41:20 +090013#include "debug.h"
14
David Aherne370a3d2015-02-18 19:33:37 -050015#ifndef EM_AARCH64
16#define EM_AARCH64 183 /* ARM 64 bit */
17#endif
18
Arnaldo Carvalho de Melocc310782016-07-12 11:04:13 -030019typedef Elf64_Nhdr GElf_Nhdr;
David Aherne370a3d2015-02-18 19:33:37 -050020
Arnaldo Carvalho de Meloaaba4e12014-11-24 17:10:52 -030021#ifdef HAVE_CPLUS_DEMANGLE_SUPPORT
22extern char *cplus_demangle(const char *, int);
23
24static inline char *bfd_demangle(void __maybe_unused *v, const char *c, int i)
25{
26 return cplus_demangle(c, i);
27}
28#else
29#ifdef NO_DEMANGLE
30static inline char *bfd_demangle(void __maybe_unused *v,
31 const char __maybe_unused *c,
32 int __maybe_unused i)
33{
34 return NULL;
35}
36#else
37#define PACKAGE 'perf'
38#include <bfd.h>
39#endif
40#endif
41
Ingo Molnar89fe8082013-09-30 12:07:11 +020042#ifndef HAVE_ELF_GETPHDRNUM_SUPPORT
Arnaldo Carvalho de Melo179f36d2015-09-17 11:30:20 -030043static int elf_getphdrnum(Elf *elf, size_t *dst)
Adrian Huntere955d5c2013-09-13 16:49:30 +030044{
45 GElf_Ehdr gehdr;
46 GElf_Ehdr *ehdr;
47
48 ehdr = gelf_getehdr(elf, &gehdr);
49 if (!ehdr)
50 return -1;
51
52 *dst = ehdr->e_phnum;
53
54 return 0;
55}
56#endif
57
Arnaldo Carvalho de Melo2492c462016-07-04 19:35:47 -030058#ifndef HAVE_ELF_GETSHDRSTRNDX_SUPPORT
59static int elf_getshdrstrndx(Elf *elf __maybe_unused, size_t *dst __maybe_unused)
60{
61 pr_err("%s: update your libelf to > 0.140, this one lacks elf_getshdrstrndx().\n", __func__);
62 return -1;
63}
64#endif
65
Namhyung Kime5a18452012-08-06 13:41:20 +090066#ifndef NT_GNU_BUILD_ID
67#define NT_GNU_BUILD_ID 3
68#endif
69
70/**
71 * elf_symtab__for_each_symbol - iterate thru all the symbols
72 *
73 * @syms: struct elf_symtab instance to iterate
74 * @idx: uint32_t idx
75 * @sym: GElf_Sym iterator
76 */
77#define elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) \
78 for (idx = 0, gelf_getsym(syms, idx, &sym);\
79 idx < nr_syms; \
80 idx++, gelf_getsym(syms, idx, &sym))
81
82static inline uint8_t elf_sym__type(const GElf_Sym *sym)
83{
84 return GELF_ST_TYPE(sym->st_info);
85}
86
Vinson Lee4e310502015-02-09 16:29:37 -080087#ifndef STT_GNU_IFUNC
88#define STT_GNU_IFUNC 10
89#endif
90
Namhyung Kime5a18452012-08-06 13:41:20 +090091static inline int elf_sym__is_function(const GElf_Sym *sym)
92{
Adrian Huntera2f3b6b2014-07-14 13:02:33 +030093 return (elf_sym__type(sym) == STT_FUNC ||
94 elf_sym__type(sym) == STT_GNU_IFUNC) &&
Namhyung Kime5a18452012-08-06 13:41:20 +090095 sym->st_name != 0 &&
96 sym->st_shndx != SHN_UNDEF;
97}
98
99static inline bool elf_sym__is_object(const GElf_Sym *sym)
100{
101 return elf_sym__type(sym) == STT_OBJECT &&
102 sym->st_name != 0 &&
103 sym->st_shndx != SHN_UNDEF;
104}
105
106static inline int elf_sym__is_label(const GElf_Sym *sym)
107{
108 return elf_sym__type(sym) == STT_NOTYPE &&
109 sym->st_name != 0 &&
110 sym->st_shndx != SHN_UNDEF &&
111 sym->st_shndx != SHN_ABS;
112}
113
114static bool elf_sym__is_a(GElf_Sym *sym, enum map_type type)
115{
116 switch (type) {
117 case MAP__FUNCTION:
118 return elf_sym__is_function(sym);
119 case MAP__VARIABLE:
120 return elf_sym__is_object(sym);
121 default:
122 return false;
123 }
124}
125
126static inline const char *elf_sym__name(const GElf_Sym *sym,
127 const Elf_Data *symstrs)
128{
129 return symstrs->d_buf + sym->st_name;
130}
131
132static inline const char *elf_sec__name(const GElf_Shdr *shdr,
133 const Elf_Data *secstrs)
134{
135 return secstrs->d_buf + shdr->sh_name;
136}
137
138static inline int elf_sec__is_text(const GElf_Shdr *shdr,
139 const Elf_Data *secstrs)
140{
141 return strstr(elf_sec__name(shdr, secstrs), "text") != NULL;
142}
143
144static inline bool elf_sec__is_data(const GElf_Shdr *shdr,
145 const Elf_Data *secstrs)
146{
147 return strstr(elf_sec__name(shdr, secstrs), "data") != NULL;
148}
149
150static bool elf_sec__is_a(GElf_Shdr *shdr, Elf_Data *secstrs,
151 enum map_type type)
152{
153 switch (type) {
154 case MAP__FUNCTION:
155 return elf_sec__is_text(shdr, secstrs);
156 case MAP__VARIABLE:
157 return elf_sec__is_data(shdr, secstrs);
158 default:
159 return false;
160 }
161}
162
163static size_t elf_addr_to_index(Elf *elf, GElf_Addr addr)
164{
165 Elf_Scn *sec = NULL;
166 GElf_Shdr shdr;
167 size_t cnt = 1;
168
169 while ((sec = elf_nextscn(elf, sec)) != NULL) {
170 gelf_getshdr(sec, &shdr);
171
172 if ((addr >= shdr.sh_addr) &&
173 (addr < (shdr.sh_addr + shdr.sh_size)))
174 return cnt;
175
176 ++cnt;
177 }
178
179 return -1;
180}
181
Masami Hiramatsu99ca4232014-01-16 09:39:49 +0000182Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep,
183 GElf_Shdr *shp, const char *name, size_t *idx)
Namhyung Kime5a18452012-08-06 13:41:20 +0900184{
185 Elf_Scn *sec = NULL;
186 size_t cnt = 1;
187
Cody P Schafer49274652012-08-10 15:22:55 -0700188 /* Elf is corrupted/truncated, avoid calling elf_strptr. */
189 if (!elf_rawdata(elf_getscn(elf, ep->e_shstrndx), NULL))
190 return NULL;
191
Namhyung Kime5a18452012-08-06 13:41:20 +0900192 while ((sec = elf_nextscn(elf, sec)) != NULL) {
193 char *str;
194
195 gelf_getshdr(sec, shp);
196 str = elf_strptr(elf, ep->e_shstrndx, shp->sh_name);
Jiri Olsa155b3a12014-03-02 14:32:07 +0100197 if (str && !strcmp(name, str)) {
Namhyung Kime5a18452012-08-06 13:41:20 +0900198 if (idx)
199 *idx = cnt;
Jiri Olsa155b3a12014-03-02 14:32:07 +0100200 return sec;
Namhyung Kime5a18452012-08-06 13:41:20 +0900201 }
202 ++cnt;
203 }
204
Jiri Olsa155b3a12014-03-02 14:32:07 +0100205 return NULL;
Namhyung Kime5a18452012-08-06 13:41:20 +0900206}
207
208#define elf_section__for_each_rel(reldata, pos, pos_mem, idx, nr_entries) \
209 for (idx = 0, pos = gelf_getrel(reldata, 0, &pos_mem); \
210 idx < nr_entries; \
211 ++idx, pos = gelf_getrel(reldata, idx, &pos_mem))
212
213#define elf_section__for_each_rela(reldata, pos, pos_mem, idx, nr_entries) \
214 for (idx = 0, pos = gelf_getrela(reldata, 0, &pos_mem); \
215 idx < nr_entries; \
216 ++idx, pos = gelf_getrela(reldata, idx, &pos_mem))
217
218/*
219 * We need to check if we have a .dynsym, so that we can handle the
220 * .plt, synthesizing its symbols, that aren't on the symtabs (be it
221 * .dynsym or .symtab).
222 * And always look at the original dso, not at debuginfo packages, that
223 * have the PLT data stripped out (shdr_rel_plt.sh_type == SHT_NOBITS).
224 */
Cody P Schafera44f6052012-08-10 15:22:59 -0700225int dso__synthesize_plt_symbols(struct dso *dso, struct symsrc *ss, struct map *map,
Namhyung Kime5a18452012-08-06 13:41:20 +0900226 symbol_filter_t filter)
227{
228 uint32_t nr_rel_entries, idx;
229 GElf_Sym sym;
230 u64 plt_offset;
231 GElf_Shdr shdr_plt;
232 struct symbol *f;
233 GElf_Shdr shdr_rel_plt, shdr_dynsym;
234 Elf_Data *reldata, *syms, *symstrs;
235 Elf_Scn *scn_plt_rel, *scn_symstrs, *scn_dynsym;
236 size_t dynsym_idx;
237 GElf_Ehdr ehdr;
238 char sympltname[1024];
239 Elf *elf;
Cody P Schafera44f6052012-08-10 15:22:59 -0700240 int nr = 0, symidx, err = 0;
Namhyung Kime5a18452012-08-06 13:41:20 +0900241
David Ahernf47b58b2012-08-19 09:47:14 -0600242 if (!ss->dynsym)
243 return 0;
244
Cody P Schafera44f6052012-08-10 15:22:59 -0700245 elf = ss->elf;
246 ehdr = ss->ehdr;
Namhyung Kime5a18452012-08-06 13:41:20 +0900247
Cody P Schafera44f6052012-08-10 15:22:59 -0700248 scn_dynsym = ss->dynsym;
249 shdr_dynsym = ss->dynshdr;
250 dynsym_idx = ss->dynsym_idx;
Namhyung Kime5a18452012-08-06 13:41:20 +0900251
Namhyung Kime5a18452012-08-06 13:41:20 +0900252 if (scn_dynsym == NULL)
253 goto out_elf_end;
254
255 scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt,
256 ".rela.plt", NULL);
257 if (scn_plt_rel == NULL) {
258 scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt,
259 ".rel.plt", NULL);
260 if (scn_plt_rel == NULL)
261 goto out_elf_end;
262 }
263
264 err = -1;
265
266 if (shdr_rel_plt.sh_link != dynsym_idx)
267 goto out_elf_end;
268
269 if (elf_section_by_name(elf, &ehdr, &shdr_plt, ".plt", NULL) == NULL)
270 goto out_elf_end;
271
272 /*
273 * Fetch the relocation section to find the idxes to the GOT
274 * and the symbols in the .dynsym they refer to.
275 */
276 reldata = elf_getdata(scn_plt_rel, NULL);
277 if (reldata == NULL)
278 goto out_elf_end;
279
280 syms = elf_getdata(scn_dynsym, NULL);
281 if (syms == NULL)
282 goto out_elf_end;
283
284 scn_symstrs = elf_getscn(elf, shdr_dynsym.sh_link);
285 if (scn_symstrs == NULL)
286 goto out_elf_end;
287
288 symstrs = elf_getdata(scn_symstrs, NULL);
289 if (symstrs == NULL)
290 goto out_elf_end;
291
Cody P Schafer52f9ddb2012-08-10 15:22:51 -0700292 if (symstrs->d_size == 0)
293 goto out_elf_end;
294
Namhyung Kime5a18452012-08-06 13:41:20 +0900295 nr_rel_entries = shdr_rel_plt.sh_size / shdr_rel_plt.sh_entsize;
296 plt_offset = shdr_plt.sh_offset;
297
298 if (shdr_rel_plt.sh_type == SHT_RELA) {
299 GElf_Rela pos_mem, *pos;
300
301 elf_section__for_each_rela(reldata, pos, pos_mem, idx,
302 nr_rel_entries) {
303 symidx = GELF_R_SYM(pos->r_info);
304 plt_offset += shdr_plt.sh_entsize;
305 gelf_getsym(syms, symidx, &sym);
306 snprintf(sympltname, sizeof(sympltname),
307 "%s@plt", elf_sym__name(&sym, symstrs));
308
309 f = symbol__new(plt_offset, shdr_plt.sh_entsize,
310 STB_GLOBAL, sympltname);
311 if (!f)
312 goto out_elf_end;
313
314 if (filter && filter(map, f))
315 symbol__delete(f);
316 else {
317 symbols__insert(&dso->symbols[map->type], f);
318 ++nr;
319 }
320 }
321 } else if (shdr_rel_plt.sh_type == SHT_REL) {
322 GElf_Rel pos_mem, *pos;
323 elf_section__for_each_rel(reldata, pos, pos_mem, idx,
324 nr_rel_entries) {
325 symidx = GELF_R_SYM(pos->r_info);
326 plt_offset += shdr_plt.sh_entsize;
327 gelf_getsym(syms, symidx, &sym);
328 snprintf(sympltname, sizeof(sympltname),
329 "%s@plt", elf_sym__name(&sym, symstrs));
330
331 f = symbol__new(plt_offset, shdr_plt.sh_entsize,
332 STB_GLOBAL, sympltname);
333 if (!f)
334 goto out_elf_end;
335
336 if (filter && filter(map, f))
337 symbol__delete(f);
338 else {
339 symbols__insert(&dso->symbols[map->type], f);
340 ++nr;
341 }
342 }
343 }
344
345 err = 0;
346out_elf_end:
Namhyung Kime5a18452012-08-06 13:41:20 +0900347 if (err == 0)
348 return nr;
Namhyung Kime5a18452012-08-06 13:41:20 +0900349 pr_debug("%s: problems reading %s PLT info.\n",
350 __func__, dso->long_name);
351 return 0;
352}
353
354/*
355 * Align offset to 4 bytes as needed for note name and descriptor data.
356 */
357#define NOTE_ALIGN(n) (((n) + 3) & -4U)
358
359static int elf_read_build_id(Elf *elf, void *bf, size_t size)
360{
361 int err = -1;
362 GElf_Ehdr ehdr;
363 GElf_Shdr shdr;
364 Elf_Data *data;
365 Elf_Scn *sec;
366 Elf_Kind ek;
367 void *ptr;
368
369 if (size < BUILD_ID_SIZE)
370 goto out;
371
372 ek = elf_kind(elf);
373 if (ek != ELF_K_ELF)
374 goto out;
375
376 if (gelf_getehdr(elf, &ehdr) == NULL) {
377 pr_err("%s: cannot get elf header.\n", __func__);
378 goto out;
379 }
380
381 /*
382 * Check following sections for notes:
383 * '.note.gnu.build-id'
384 * '.notes'
385 * '.note' (VDSO specific)
386 */
387 do {
388 sec = elf_section_by_name(elf, &ehdr, &shdr,
389 ".note.gnu.build-id", NULL);
390 if (sec)
391 break;
392
393 sec = elf_section_by_name(elf, &ehdr, &shdr,
394 ".notes", NULL);
395 if (sec)
396 break;
397
398 sec = elf_section_by_name(elf, &ehdr, &shdr,
399 ".note", NULL);
400 if (sec)
401 break;
402
403 return err;
404
405 } while (0);
406
407 data = elf_getdata(sec, NULL);
408 if (data == NULL)
409 goto out;
410
411 ptr = data->d_buf;
412 while (ptr < (data->d_buf + data->d_size)) {
413 GElf_Nhdr *nhdr = ptr;
414 size_t namesz = NOTE_ALIGN(nhdr->n_namesz),
415 descsz = NOTE_ALIGN(nhdr->n_descsz);
416 const char *name;
417
418 ptr += sizeof(*nhdr);
419 name = ptr;
420 ptr += namesz;
421 if (nhdr->n_type == NT_GNU_BUILD_ID &&
422 nhdr->n_namesz == sizeof("GNU")) {
423 if (memcmp(name, "GNU", sizeof("GNU")) == 0) {
424 size_t sz = min(size, descsz);
425 memcpy(bf, ptr, sz);
426 memset(bf + sz, 0, size - sz);
427 err = descsz;
428 break;
429 }
430 }
431 ptr += descsz;
432 }
433
434out:
435 return err;
436}
437
438int filename__read_build_id(const char *filename, void *bf, size_t size)
439{
440 int fd, err = -1;
441 Elf *elf;
442
443 if (size < BUILD_ID_SIZE)
444 goto out;
445
446 fd = open(filename, O_RDONLY);
447 if (fd < 0)
448 goto out;
449
450 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
451 if (elf == NULL) {
452 pr_debug2("%s: cannot read %s ELF file.\n", __func__, filename);
453 goto out_close;
454 }
455
456 err = elf_read_build_id(elf, bf, size);
457
458 elf_end(elf);
459out_close:
460 close(fd);
461out:
462 return err;
463}
464
465int sysfs__read_build_id(const char *filename, void *build_id, size_t size)
466{
467 int fd, err = -1;
468
469 if (size < BUILD_ID_SIZE)
470 goto out;
471
472 fd = open(filename, O_RDONLY);
473 if (fd < 0)
474 goto out;
475
476 while (1) {
477 char bf[BUFSIZ];
478 GElf_Nhdr nhdr;
479 size_t namesz, descsz;
480
481 if (read(fd, &nhdr, sizeof(nhdr)) != sizeof(nhdr))
482 break;
483
484 namesz = NOTE_ALIGN(nhdr.n_namesz);
485 descsz = NOTE_ALIGN(nhdr.n_descsz);
486 if (nhdr.n_type == NT_GNU_BUILD_ID &&
487 nhdr.n_namesz == sizeof("GNU")) {
488 if (read(fd, bf, namesz) != (ssize_t)namesz)
489 break;
490 if (memcmp(bf, "GNU", sizeof("GNU")) == 0) {
491 size_t sz = min(descsz, size);
492 if (read(fd, build_id, sz) == (ssize_t)sz) {
493 memset(build_id + sz, 0, size - sz);
494 err = 0;
495 break;
496 }
497 } else if (read(fd, bf, descsz) != (ssize_t)descsz)
498 break;
499 } else {
500 int n = namesz + descsz;
501 if (read(fd, bf, n) != n)
502 break;
503 }
504 }
505 close(fd);
506out:
507 return err;
508}
509
510int filename__read_debuglink(const char *filename, char *debuglink,
511 size_t size)
512{
513 int fd, err = -1;
514 Elf *elf;
515 GElf_Ehdr ehdr;
516 GElf_Shdr shdr;
517 Elf_Data *data;
518 Elf_Scn *sec;
519 Elf_Kind ek;
520
521 fd = open(filename, O_RDONLY);
522 if (fd < 0)
523 goto out;
524
525 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
526 if (elf == NULL) {
527 pr_debug2("%s: cannot read %s ELF file.\n", __func__, filename);
528 goto out_close;
529 }
530
531 ek = elf_kind(elf);
532 if (ek != ELF_K_ELF)
Chenggang Qin784f3392013-10-11 08:27:57 +0800533 goto out_elf_end;
Namhyung Kime5a18452012-08-06 13:41:20 +0900534
535 if (gelf_getehdr(elf, &ehdr) == NULL) {
536 pr_err("%s: cannot get elf header.\n", __func__);
Chenggang Qin784f3392013-10-11 08:27:57 +0800537 goto out_elf_end;
Namhyung Kime5a18452012-08-06 13:41:20 +0900538 }
539
540 sec = elf_section_by_name(elf, &ehdr, &shdr,
541 ".gnu_debuglink", NULL);
542 if (sec == NULL)
Chenggang Qin784f3392013-10-11 08:27:57 +0800543 goto out_elf_end;
Namhyung Kime5a18452012-08-06 13:41:20 +0900544
545 data = elf_getdata(sec, NULL);
546 if (data == NULL)
Chenggang Qin784f3392013-10-11 08:27:57 +0800547 goto out_elf_end;
Namhyung Kime5a18452012-08-06 13:41:20 +0900548
549 /* the start of this section is a zero-terminated string */
550 strncpy(debuglink, data->d_buf, size);
551
Stephane Eranian0d3dc5e2014-02-20 10:32:55 +0900552 err = 0;
553
Chenggang Qin784f3392013-10-11 08:27:57 +0800554out_elf_end:
Namhyung Kime5a18452012-08-06 13:41:20 +0900555 elf_end(elf);
Namhyung Kime5a18452012-08-06 13:41:20 +0900556out_close:
557 close(fd);
558out:
559 return err;
560}
561
562static int dso__swap_init(struct dso *dso, unsigned char eidata)
563{
564 static unsigned int const endian = 1;
565
566 dso->needs_swap = DSO_SWAP__NO;
567
568 switch (eidata) {
569 case ELFDATA2LSB:
570 /* We are big endian, DSO is little endian. */
571 if (*(unsigned char const *)&endian != 1)
572 dso->needs_swap = DSO_SWAP__YES;
573 break;
574
575 case ELFDATA2MSB:
576 /* We are little endian, DSO is big endian. */
577 if (*(unsigned char const *)&endian != 0)
578 dso->needs_swap = DSO_SWAP__YES;
579 break;
580
581 default:
582 pr_err("unrecognized DSO data encoding %d\n", eidata);
583 return -EINVAL;
584 }
585
586 return 0;
587}
588
Namhyung Kimc00c48f2014-11-04 10:14:27 +0900589static int decompress_kmodule(struct dso *dso, const char *name,
590 enum dso_binary_type type)
591{
Jiri Olsa914f85c2015-02-12 22:27:50 +0100592 int fd = -1;
Namhyung Kimc00c48f2014-11-04 10:14:27 +0900593 char tmpbuf[] = "/tmp/perf-kmod-XXXXXX";
Jiri Olsa914f85c2015-02-12 22:27:50 +0100594 struct kmod_path m;
Namhyung Kimc00c48f2014-11-04 10:14:27 +0900595
Namhyung Kim0b064f42015-01-29 17:06:42 +0900596 if (type != DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP &&
597 type != DSO_BINARY_TYPE__GUEST_KMODULE_COMP &&
598 type != DSO_BINARY_TYPE__BUILD_ID_CACHE)
Namhyung Kimc00c48f2014-11-04 10:14:27 +0900599 return -1;
600
Jiri Olsa914f85c2015-02-12 22:27:50 +0100601 if (type == DSO_BINARY_TYPE__BUILD_ID_CACHE)
602 name = dso->long_name;
603
604 if (kmod_path__parse_ext(&m, name) || !m.comp)
605 return -1;
Namhyung Kimc00c48f2014-11-04 10:14:27 +0900606
607 fd = mkstemp(tmpbuf);
Arnaldo Carvalho de Melo18425f12015-03-24 11:49:02 -0300608 if (fd < 0) {
609 dso->load_errno = errno;
Jiri Olsa914f85c2015-02-12 22:27:50 +0100610 goto out;
Arnaldo Carvalho de Melo18425f12015-03-24 11:49:02 -0300611 }
Namhyung Kimc00c48f2014-11-04 10:14:27 +0900612
Jiri Olsa914f85c2015-02-12 22:27:50 +0100613 if (!decompress_to_file(m.ext, name, fd)) {
Arnaldo Carvalho de Melo18425f12015-03-24 11:49:02 -0300614 dso->load_errno = DSO_LOAD_ERRNO__DECOMPRESSION_FAILURE;
Namhyung Kimc00c48f2014-11-04 10:14:27 +0900615 close(fd);
616 fd = -1;
617 }
618
619 unlink(tmpbuf);
620
Jiri Olsa914f85c2015-02-12 22:27:50 +0100621out:
622 free(m.ext);
Namhyung Kimc00c48f2014-11-04 10:14:27 +0900623 return fd;
624}
625
Cody P Schafer3aafe5a2012-08-10 15:23:02 -0700626bool symsrc__possibly_runtime(struct symsrc *ss)
627{
628 return ss->dynsym || ss->opdsec;
629}
630
Cody P Schaferd26cd122012-08-10 15:23:00 -0700631bool symsrc__has_symtab(struct symsrc *ss)
632{
633 return ss->symtab != NULL;
634}
Cody P Schaferb68e2f92012-08-10 15:22:57 -0700635
636void symsrc__destroy(struct symsrc *ss)
Namhyung Kime5a18452012-08-06 13:41:20 +0900637{
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300638 zfree(&ss->name);
Cody P Schaferb68e2f92012-08-10 15:22:57 -0700639 elf_end(ss->elf);
640 close(ss->fd);
641}
642
Naveen N. Raod2332092015-04-28 17:35:35 +0530643bool __weak elf__needs_adjust_symbols(GElf_Ehdr ehdr)
644{
645 return ehdr.e_type == ET_EXEC || ehdr.e_type == ET_REL;
646}
647
Cody P Schaferb68e2f92012-08-10 15:22:57 -0700648int symsrc__init(struct symsrc *ss, struct dso *dso, const char *name,
649 enum dso_binary_type type)
650{
Namhyung Kime5a18452012-08-06 13:41:20 +0900651 int err = -1;
Namhyung Kime5a18452012-08-06 13:41:20 +0900652 GElf_Ehdr ehdr;
Namhyung Kime5a18452012-08-06 13:41:20 +0900653 Elf *elf;
Cody P Schaferb68e2f92012-08-10 15:22:57 -0700654 int fd;
655
Arnaldo Carvalho de Melo18425f12015-03-24 11:49:02 -0300656 if (dso__needs_decompress(dso)) {
Namhyung Kimc00c48f2014-11-04 10:14:27 +0900657 fd = decompress_kmodule(dso, name, type);
Arnaldo Carvalho de Melo18425f12015-03-24 11:49:02 -0300658 if (fd < 0)
659 return -1;
660 } else {
Namhyung Kimc00c48f2014-11-04 10:14:27 +0900661 fd = open(name, O_RDONLY);
Arnaldo Carvalho de Melo18425f12015-03-24 11:49:02 -0300662 if (fd < 0) {
663 dso->load_errno = errno;
664 return -1;
665 }
666 }
Namhyung Kime5a18452012-08-06 13:41:20 +0900667
668 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
669 if (elf == NULL) {
670 pr_debug("%s: cannot read %s ELF file.\n", __func__, name);
Arnaldo Carvalho de Melo18425f12015-03-24 11:49:02 -0300671 dso->load_errno = DSO_LOAD_ERRNO__INVALID_ELF;
Namhyung Kime5a18452012-08-06 13:41:20 +0900672 goto out_close;
673 }
674
675 if (gelf_getehdr(elf, &ehdr) == NULL) {
Arnaldo Carvalho de Melo18425f12015-03-24 11:49:02 -0300676 dso->load_errno = DSO_LOAD_ERRNO__INVALID_ELF;
Namhyung Kime5a18452012-08-06 13:41:20 +0900677 pr_debug("%s: cannot get elf header.\n", __func__);
678 goto out_elf_end;
679 }
680
Arnaldo Carvalho de Melo18425f12015-03-24 11:49:02 -0300681 if (dso__swap_init(dso, ehdr.e_ident[EI_DATA])) {
682 dso->load_errno = DSO_LOAD_ERRNO__INTERNAL_ERROR;
Namhyung Kime5a18452012-08-06 13:41:20 +0900683 goto out_elf_end;
Arnaldo Carvalho de Melo18425f12015-03-24 11:49:02 -0300684 }
Namhyung Kime5a18452012-08-06 13:41:20 +0900685
686 /* Always reject images with a mismatched build-id: */
687 if (dso->has_build_id) {
688 u8 build_id[BUILD_ID_SIZE];
689
Arnaldo Carvalho de Melo18425f12015-03-24 11:49:02 -0300690 if (elf_read_build_id(elf, build_id, BUILD_ID_SIZE) < 0) {
691 dso->load_errno = DSO_LOAD_ERRNO__CANNOT_READ_BUILDID;
Namhyung Kime5a18452012-08-06 13:41:20 +0900692 goto out_elf_end;
Arnaldo Carvalho de Melo18425f12015-03-24 11:49:02 -0300693 }
Namhyung Kime5a18452012-08-06 13:41:20 +0900694
Arnaldo Carvalho de Melo18425f12015-03-24 11:49:02 -0300695 if (!dso__build_id_equal(dso, build_id)) {
Naveen N. Rao468f3d22015-04-25 01:14:46 +0530696 pr_debug("%s: build id mismatch for %s.\n", __func__, name);
Arnaldo Carvalho de Melo18425f12015-03-24 11:49:02 -0300697 dso->load_errno = DSO_LOAD_ERRNO__MISMATCHING_BUILDID;
Namhyung Kime5a18452012-08-06 13:41:20 +0900698 goto out_elf_end;
Arnaldo Carvalho de Melo18425f12015-03-24 11:49:02 -0300699 }
Namhyung Kime5a18452012-08-06 13:41:20 +0900700 }
701
Adrian Hunterc6d8f2a2014-07-14 13:02:41 +0300702 ss->is_64_bit = (gelf_getclass(elf) == ELFCLASS64);
703
Cody P Schaferb68e2f92012-08-10 15:22:57 -0700704 ss->symtab = elf_section_by_name(elf, &ehdr, &ss->symshdr, ".symtab",
705 NULL);
706 if (ss->symshdr.sh_type != SHT_SYMTAB)
707 ss->symtab = NULL;
708
709 ss->dynsym_idx = 0;
710 ss->dynsym = elf_section_by_name(elf, &ehdr, &ss->dynshdr, ".dynsym",
711 &ss->dynsym_idx);
712 if (ss->dynshdr.sh_type != SHT_DYNSYM)
713 ss->dynsym = NULL;
714
715 ss->opdidx = 0;
716 ss->opdsec = elf_section_by_name(elf, &ehdr, &ss->opdshdr, ".opd",
717 &ss->opdidx);
718 if (ss->opdshdr.sh_type != SHT_PROGBITS)
719 ss->opdsec = NULL;
720
Wang Nan99e87f72016-04-07 10:24:31 +0000721 if (dso->kernel == DSO_TYPE_USER)
722 ss->adjust_symbols = true;
723 else
Naveen N. Raod2332092015-04-28 17:35:35 +0530724 ss->adjust_symbols = elf__needs_adjust_symbols(ehdr);
Cody P Schaferb68e2f92012-08-10 15:22:57 -0700725
726 ss->name = strdup(name);
Arnaldo Carvalho de Melo18425f12015-03-24 11:49:02 -0300727 if (!ss->name) {
728 dso->load_errno = errno;
Cody P Schaferb68e2f92012-08-10 15:22:57 -0700729 goto out_elf_end;
Arnaldo Carvalho de Melo18425f12015-03-24 11:49:02 -0300730 }
Cody P Schaferb68e2f92012-08-10 15:22:57 -0700731
732 ss->elf = elf;
733 ss->fd = fd;
734 ss->ehdr = ehdr;
735 ss->type = type;
736
737 return 0;
738
739out_elf_end:
740 elf_end(elf);
741out_close:
742 close(fd);
743 return err;
744}
745
Adrian Hunter39b12f782013-08-07 14:38:47 +0300746/**
747 * ref_reloc_sym_not_found - has kernel relocation symbol been found.
748 * @kmap: kernel maps and relocation reference symbol
749 *
750 * This function returns %true if we are dealing with the kernel maps and the
751 * relocation reference symbol has not yet been found. Otherwise %false is
752 * returned.
753 */
754static bool ref_reloc_sym_not_found(struct kmap *kmap)
755{
756 return kmap && kmap->ref_reloc_sym && kmap->ref_reloc_sym->name &&
757 !kmap->ref_reloc_sym->unrelocated_addr;
758}
759
760/**
761 * ref_reloc - kernel relocation offset.
762 * @kmap: kernel maps and relocation reference symbol
763 *
764 * This function returns the offset of kernel addresses as determined by using
765 * the relocation reference symbol i.e. if the kernel has not been relocated
766 * then the return value is zero.
767 */
768static u64 ref_reloc(struct kmap *kmap)
769{
770 if (kmap && kmap->ref_reloc_sym &&
771 kmap->ref_reloc_sym->unrelocated_addr)
772 return kmap->ref_reloc_sym->addr -
773 kmap->ref_reloc_sym->unrelocated_addr;
774 return 0;
775}
776
Avi Kivity763122a2014-09-13 07:15:05 +0300777static bool want_demangle(bool is_kernel_sym)
778{
779 return is_kernel_sym ? symbol_conf.demangle_kernel : symbol_conf.demangle;
780}
781
Naveen N. Rao0b3c2262016-04-12 14:40:50 +0530782void __weak arch__sym_update(struct symbol *s __maybe_unused,
783 GElf_Sym *sym __maybe_unused) { }
Ananth N Mavinakayanahallic50fc0a2015-04-28 17:35:38 +0530784
Cody P Schafer261360b2012-08-10 15:23:01 -0700785int dso__load_sym(struct dso *dso, struct map *map,
786 struct symsrc *syms_ss, struct symsrc *runtime_ss,
Cody P Schaferd26cd122012-08-10 15:23:00 -0700787 symbol_filter_t filter, int kmodule)
Cody P Schaferb68e2f92012-08-10 15:22:57 -0700788{
789 struct kmap *kmap = dso->kernel ? map__kmap(map) : NULL;
Wang Nanba927322015-04-07 08:22:45 +0000790 struct map_groups *kmaps = kmap ? map__kmaps(map) : NULL;
Cody P Schaferb68e2f92012-08-10 15:22:57 -0700791 struct map *curr_map = map;
792 struct dso *curr_dso = dso;
793 Elf_Data *symstrs, *secstrs;
794 uint32_t nr_syms;
795 int err = -1;
796 uint32_t idx;
797 GElf_Ehdr ehdr;
Cody P Schafer261360b2012-08-10 15:23:01 -0700798 GElf_Shdr shdr;
Wang Nan73cdf0c2016-02-26 09:31:49 +0000799 GElf_Shdr tshdr;
Cody P Schaferb68e2f92012-08-10 15:22:57 -0700800 Elf_Data *syms, *opddata = NULL;
801 GElf_Sym sym;
Cody P Schafer261360b2012-08-10 15:23:01 -0700802 Elf_Scn *sec, *sec_strndx;
Cody P Schaferb68e2f92012-08-10 15:22:57 -0700803 Elf *elf;
804 int nr = 0;
Adrian Hunter39b12f782013-08-07 14:38:47 +0300805 bool remap_kernel = false, adjust_kernel_syms = false;
Cody P Schaferb68e2f92012-08-10 15:22:57 -0700806
Wang Nanba927322015-04-07 08:22:45 +0000807 if (kmap && !kmaps)
808 return -1;
809
Cody P Schafer261360b2012-08-10 15:23:01 -0700810 dso->symtab_type = syms_ss->type;
Adrian Hunterc6d8f2a2014-07-14 13:02:41 +0300811 dso->is_64_bit = syms_ss->is_64_bit;
Adrian Hunter0131c4e2013-08-07 14:38:50 +0300812 dso->rel = syms_ss->ehdr.e_type == ET_REL;
813
814 /*
815 * Modules may already have symbols from kallsyms, but those symbols
816 * have the wrong values for the dso maps, so remove them.
817 */
818 if (kmodule && syms_ss->symtab)
819 symbols__delete(&dso->symbols[map->type]);
Cody P Schafer005f9292012-08-10 15:22:58 -0700820
Cody P Schafer261360b2012-08-10 15:23:01 -0700821 if (!syms_ss->symtab) {
Anton Blanchardd0b0d042014-09-09 08:59:29 +1000822 /*
823 * If the vmlinux is stripped, fail so we will fall back
824 * to using kallsyms. The vmlinux runtime symbols aren't
825 * of much use.
826 */
827 if (dso->kernel)
828 goto out_elf_end;
829
Cody P Schafer261360b2012-08-10 15:23:01 -0700830 syms_ss->symtab = syms_ss->dynsym;
831 syms_ss->symshdr = syms_ss->dynshdr;
Cody P Schaferd26cd122012-08-10 15:23:00 -0700832 }
833
Cody P Schafer261360b2012-08-10 15:23:01 -0700834 elf = syms_ss->elf;
835 ehdr = syms_ss->ehdr;
836 sec = syms_ss->symtab;
837 shdr = syms_ss->symshdr;
Cody P Schaferb68e2f92012-08-10 15:22:57 -0700838
Wang Nan73cdf0c2016-02-26 09:31:49 +0000839 if (elf_section_by_name(elf, &ehdr, &tshdr, ".text", NULL))
840 dso->text_offset = tshdr.sh_addr - tshdr.sh_offset;
841
Cody P Schafer261360b2012-08-10 15:23:01 -0700842 if (runtime_ss->opdsec)
843 opddata = elf_rawdata(runtime_ss->opdsec, NULL);
Namhyung Kime5a18452012-08-06 13:41:20 +0900844
845 syms = elf_getdata(sec, NULL);
846 if (syms == NULL)
847 goto out_elf_end;
848
849 sec = elf_getscn(elf, shdr.sh_link);
850 if (sec == NULL)
851 goto out_elf_end;
852
853 symstrs = elf_getdata(sec, NULL);
854 if (symstrs == NULL)
855 goto out_elf_end;
856
Adrian Hunterf247fb82014-07-31 09:00:46 +0300857 sec_strndx = elf_getscn(runtime_ss->elf, runtime_ss->ehdr.e_shstrndx);
Namhyung Kime5a18452012-08-06 13:41:20 +0900858 if (sec_strndx == NULL)
859 goto out_elf_end;
860
861 secstrs = elf_getdata(sec_strndx, NULL);
862 if (secstrs == NULL)
863 goto out_elf_end;
864
865 nr_syms = shdr.sh_size / shdr.sh_entsize;
866
867 memset(&sym, 0, sizeof(sym));
Adrian Hunter39b12f782013-08-07 14:38:47 +0300868
869 /*
870 * The kernel relocation symbol is needed in advance in order to adjust
871 * kernel maps correctly.
872 */
873 if (ref_reloc_sym_not_found(kmap)) {
874 elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) {
875 const char *elf_name = elf_sym__name(&sym, symstrs);
876
877 if (strcmp(elf_name, kmap->ref_reloc_sym->name))
878 continue;
879 kmap->ref_reloc_sym->unrelocated_addr = sym.st_value;
Adrian Hunter91767532014-01-29 16:14:36 +0200880 map->reloc = kmap->ref_reloc_sym->addr -
881 kmap->ref_reloc_sym->unrelocated_addr;
Adrian Hunter39b12f782013-08-07 14:38:47 +0300882 break;
883 }
884 }
885
Adrian Hunterf0ee3b42015-08-14 15:50:06 +0300886 /*
887 * Handle any relocation of vdso necessary because older kernels
888 * attempted to prelink vdso to its virtual address.
889 */
Wang Nan73cdf0c2016-02-26 09:31:49 +0000890 if (dso__is_vdso(dso))
891 map->reloc = map->start - dso->text_offset;
Adrian Hunterf0ee3b42015-08-14 15:50:06 +0300892
Adrian Hunter39b12f782013-08-07 14:38:47 +0300893 dso->adjust_symbols = runtime_ss->adjust_symbols || ref_reloc(kmap);
894 /*
895 * Initial kernel and module mappings do not map to the dso. For
896 * function mappings, flag the fixups.
897 */
898 if (map->type == MAP__FUNCTION && (dso->kernel || kmodule)) {
899 remap_kernel = true;
900 adjust_kernel_syms = dso->adjust_symbols;
901 }
Namhyung Kime5a18452012-08-06 13:41:20 +0900902 elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) {
903 struct symbol *f;
904 const char *elf_name = elf_sym__name(&sym, symstrs);
905 char *demangled = NULL;
906 int is_label = elf_sym__is_label(&sym);
907 const char *section_name;
Cody P Schafer261360b2012-08-10 15:23:01 -0700908 bool used_opd = false;
Namhyung Kime5a18452012-08-06 13:41:20 +0900909
Namhyung Kime5a18452012-08-06 13:41:20 +0900910 if (!is_label && !elf_sym__is_a(&sym, map->type))
911 continue;
912
913 /* Reject ARM ELF "mapping symbols": these aren't unique and
914 * don't identify functions, so will confuse the profile
915 * output: */
Victor Kamensky4886f2c2015-01-26 22:34:01 -0800916 if (ehdr.e_machine == EM_ARM || ehdr.e_machine == EM_AARCH64) {
917 if (elf_name[0] == '$' && strchr("adtx", elf_name[1])
918 && (elf_name[2] == '\0' || elf_name[2] == '.'))
Namhyung Kime5a18452012-08-06 13:41:20 +0900919 continue;
920 }
921
Cody P Schafer261360b2012-08-10 15:23:01 -0700922 if (runtime_ss->opdsec && sym.st_shndx == runtime_ss->opdidx) {
923 u32 offset = sym.st_value - syms_ss->opdshdr.sh_addr;
Namhyung Kime5a18452012-08-06 13:41:20 +0900924 u64 *opd = opddata->d_buf + offset;
925 sym.st_value = DSO__SWAP(dso, u64, *opd);
Cody P Schafer261360b2012-08-10 15:23:01 -0700926 sym.st_shndx = elf_addr_to_index(runtime_ss->elf,
927 sym.st_value);
928 used_opd = true;
Namhyung Kime5a18452012-08-06 13:41:20 +0900929 }
Namhyung Kim3843b052012-11-21 13:49:44 +0100930 /*
931 * When loading symbols in a data mapping, ABS symbols (which
932 * has a value of SHN_ABS in its st_shndx) failed at
933 * elf_getscn(). And it marks the loading as a failure so
934 * already loaded symbols cannot be fixed up.
935 *
936 * I'm not sure what should be done. Just ignore them for now.
937 * - Namhyung Kim
938 */
939 if (sym.st_shndx == SHN_ABS)
940 continue;
Namhyung Kime5a18452012-08-06 13:41:20 +0900941
Cody P Schafer261360b2012-08-10 15:23:01 -0700942 sec = elf_getscn(runtime_ss->elf, sym.st_shndx);
Namhyung Kime5a18452012-08-06 13:41:20 +0900943 if (!sec)
944 goto out_elf_end;
945
946 gelf_getshdr(sec, &shdr);
947
948 if (is_label && !elf_sec__is_a(&shdr, secstrs, map->type))
949 continue;
950
951 section_name = elf_sec__name(&shdr, secstrs);
952
953 /* On ARM, symbols for thumb functions have 1 added to
954 * the symbol address as a flag - remove it */
955 if ((ehdr.e_machine == EM_ARM) &&
956 (map->type == MAP__FUNCTION) &&
957 (sym.st_value & 1))
958 --sym.st_value;
959
Adrian Hunter39b12f782013-08-07 14:38:47 +0300960 if (dso->kernel || kmodule) {
Namhyung Kime5a18452012-08-06 13:41:20 +0900961 char dso_name[PATH_MAX];
962
Adrian Hunter39b12f782013-08-07 14:38:47 +0300963 /* Adjust symbol to map to file offset */
964 if (adjust_kernel_syms)
965 sym.st_value -= shdr.sh_addr - shdr.sh_offset;
966
Namhyung Kime5a18452012-08-06 13:41:20 +0900967 if (strcmp(section_name,
968 (curr_dso->short_name +
969 dso->short_name_len)) == 0)
970 goto new_symbol;
971
972 if (strcmp(section_name, ".text") == 0) {
Adrian Hunter39b12f782013-08-07 14:38:47 +0300973 /*
974 * The initial kernel mapping is based on
975 * kallsyms and identity maps. Overwrite it to
976 * map to the kernel dso.
977 */
978 if (remap_kernel && dso->kernel) {
979 remap_kernel = false;
980 map->start = shdr.sh_addr +
981 ref_reloc(kmap);
982 map->end = map->start + shdr.sh_size;
983 map->pgoff = shdr.sh_offset;
984 map->map_ip = map__map_ip;
985 map->unmap_ip = map__unmap_ip;
986 /* Ensure maps are correctly ordered */
Wang Nanba927322015-04-07 08:22:45 +0000987 if (kmaps) {
Arnaldo Carvalho de Melo84c2caf2015-05-25 16:59:56 -0300988 map__get(map);
Wang Nanba927322015-04-07 08:22:45 +0000989 map_groups__remove(kmaps, map);
990 map_groups__insert(kmaps, map);
Arnaldo Carvalho de Melo84c2caf2015-05-25 16:59:56 -0300991 map__put(map);
Wang Nanba927322015-04-07 08:22:45 +0000992 }
Adrian Hunter39b12f782013-08-07 14:38:47 +0300993 }
994
Adrian Hunter0131c4e2013-08-07 14:38:50 +0300995 /*
996 * The initial module mapping is based on
997 * /proc/modules mapped to offset zero.
998 * Overwrite it to map to the module dso.
999 */
1000 if (remap_kernel && kmodule) {
1001 remap_kernel = false;
1002 map->pgoff = shdr.sh_offset;
1003 }
1004
Namhyung Kime5a18452012-08-06 13:41:20 +09001005 curr_map = map;
1006 curr_dso = dso;
1007 goto new_symbol;
1008 }
1009
Adrian Hunter0131c4e2013-08-07 14:38:50 +03001010 if (!kmap)
1011 goto new_symbol;
1012
Namhyung Kime5a18452012-08-06 13:41:20 +09001013 snprintf(dso_name, sizeof(dso_name),
1014 "%s%s", dso->short_name, section_name);
1015
Wang Nanba927322015-04-07 08:22:45 +00001016 curr_map = map_groups__find_by_name(kmaps, map->type, dso_name);
Namhyung Kime5a18452012-08-06 13:41:20 +09001017 if (curr_map == NULL) {
1018 u64 start = sym.st_value;
1019
1020 if (kmodule)
1021 start += map->start + shdr.sh_offset;
1022
1023 curr_dso = dso__new(dso_name);
1024 if (curr_dso == NULL)
1025 goto out_elf_end;
1026 curr_dso->kernel = dso->kernel;
1027 curr_dso->long_name = dso->long_name;
1028 curr_dso->long_name_len = dso->long_name_len;
1029 curr_map = map__new2(start, curr_dso,
1030 map->type);
Masami Hiramatsue7a78652015-12-09 11:11:18 +09001031 dso__put(curr_dso);
Namhyung Kime5a18452012-08-06 13:41:20 +09001032 if (curr_map == NULL) {
Namhyung Kime5a18452012-08-06 13:41:20 +09001033 goto out_elf_end;
1034 }
Adrian Hunter39b12f782013-08-07 14:38:47 +03001035 if (adjust_kernel_syms) {
1036 curr_map->start = shdr.sh_addr +
1037 ref_reloc(kmap);
1038 curr_map->end = curr_map->start +
1039 shdr.sh_size;
1040 curr_map->pgoff = shdr.sh_offset;
1041 } else {
1042 curr_map->map_ip = identity__map_ip;
1043 curr_map->unmap_ip = identity__map_ip;
1044 }
Namhyung Kime5a18452012-08-06 13:41:20 +09001045 curr_dso->symtab_type = dso->symtab_type;
Wang Nanba927322015-04-07 08:22:45 +00001046 map_groups__insert(kmaps, curr_map);
Masami Hiramatsue7a78652015-12-09 11:11:18 +09001047 /*
1048 * Add it before we drop the referece to curr_map,
1049 * i.e. while we still are sure to have a reference
1050 * to this DSO via curr_map->dso.
1051 */
1052 dsos__add(&map->groups->machine->dsos, curr_dso);
Masami Hiramatsu8d5c3402015-11-18 15:40:27 +09001053 /* kmaps already got it */
1054 map__put(curr_map);
Namhyung Kime5a18452012-08-06 13:41:20 +09001055 dso__set_loaded(curr_dso, map->type);
1056 } else
1057 curr_dso = curr_map->dso;
1058
1059 goto new_symbol;
1060 }
1061
Cody P Schafer261360b2012-08-10 15:23:01 -07001062 if ((used_opd && runtime_ss->adjust_symbols)
1063 || (!used_opd && syms_ss->adjust_symbols)) {
Namhyung Kime5a18452012-08-06 13:41:20 +09001064 pr_debug4("%s: adjusting symbol: st_value: %#" PRIx64 " "
1065 "sh_addr: %#" PRIx64 " sh_offset: %#" PRIx64 "\n", __func__,
1066 (u64)sym.st_value, (u64)shdr.sh_addr,
1067 (u64)shdr.sh_offset);
1068 sym.st_value -= shdr.sh_addr - shdr.sh_offset;
1069 }
Avi Kivity950b8352014-01-22 21:58:46 +02001070new_symbol:
Namhyung Kime5a18452012-08-06 13:41:20 +09001071 /*
1072 * We need to figure out if the object was created from C++ sources
1073 * DWARF DW_compile_unit has this, but we don't always have access
1074 * to it...
1075 */
Avi Kivity763122a2014-09-13 07:15:05 +03001076 if (want_demangle(dso->kernel || kmodule)) {
Namhyung Kime71e7942014-07-31 14:47:42 +09001077 int demangle_flags = DMGL_NO_OPTS;
1078 if (verbose)
1079 demangle_flags = DMGL_PARAMS | DMGL_ANSI;
1080
1081 demangled = bfd_demangle(NULL, elf_name, demangle_flags);
Stephane Eraniane9c4bcd2015-11-30 10:02:20 +01001082 if (demangled == NULL)
1083 demangled = java_demangle_sym(elf_name, JAVA_DEMANGLE_NORET);
Namhyung Kim328ccda2013-03-25 18:18:18 +09001084 if (demangled != NULL)
1085 elf_name = demangled;
1086 }
Namhyung Kime5a18452012-08-06 13:41:20 +09001087 f = symbol__new(sym.st_value, sym.st_size,
1088 GELF_ST_BIND(sym.st_info), elf_name);
1089 free(demangled);
1090 if (!f)
1091 goto out_elf_end;
1092
Naveen N. Rao0b3c2262016-04-12 14:40:50 +05301093 arch__sym_update(f, &sym);
1094
Namhyung Kime5a18452012-08-06 13:41:20 +09001095 if (filter && filter(curr_map, f))
1096 symbol__delete(f);
1097 else {
1098 symbols__insert(&curr_dso->symbols[curr_map->type], f);
1099 nr++;
1100 }
1101 }
1102
1103 /*
1104 * For misannotated, zeroed, ASM function sizes.
1105 */
1106 if (nr > 0) {
Namhyung Kim680d9262015-03-06 16:31:27 +09001107 if (!symbol_conf.allow_aliases)
1108 symbols__fixup_duplicate(&dso->symbols[map->type]);
Namhyung Kime5a18452012-08-06 13:41:20 +09001109 symbols__fixup_end(&dso->symbols[map->type]);
1110 if (kmap) {
1111 /*
1112 * We need to fixup this here too because we create new
1113 * maps here, for things like vsyscall sections.
1114 */
Wang Nanba927322015-04-07 08:22:45 +00001115 __map_groups__fixup_end(kmaps, map->type);
Namhyung Kime5a18452012-08-06 13:41:20 +09001116 }
1117 }
1118 err = nr;
1119out_elf_end:
Namhyung Kime5a18452012-08-06 13:41:20 +09001120 return err;
1121}
1122
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001123static int elf_read_maps(Elf *elf, bool exe, mapfn_t mapfn, void *data)
1124{
1125 GElf_Phdr phdr;
1126 size_t i, phdrnum;
1127 int err;
1128 u64 sz;
1129
1130 if (elf_getphdrnum(elf, &phdrnum))
1131 return -1;
1132
1133 for (i = 0; i < phdrnum; i++) {
1134 if (gelf_getphdr(elf, i, &phdr) == NULL)
1135 return -1;
1136 if (phdr.p_type != PT_LOAD)
1137 continue;
1138 if (exe) {
1139 if (!(phdr.p_flags & PF_X))
1140 continue;
1141 } else {
1142 if (!(phdr.p_flags & PF_R))
1143 continue;
1144 }
1145 sz = min(phdr.p_memsz, phdr.p_filesz);
1146 if (!sz)
1147 continue;
1148 err = mapfn(phdr.p_vaddr, sz, phdr.p_offset, data);
1149 if (err)
1150 return err;
1151 }
1152 return 0;
1153}
1154
1155int file__read_maps(int fd, bool exe, mapfn_t mapfn, void *data,
1156 bool *is_64_bit)
1157{
1158 int err;
1159 Elf *elf;
1160
1161 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
1162 if (elf == NULL)
1163 return -1;
1164
1165 if (is_64_bit)
1166 *is_64_bit = (gelf_getclass(elf) == ELFCLASS64);
1167
1168 err = elf_read_maps(elf, exe, mapfn, data);
1169
1170 elf_end(elf);
1171 return err;
1172}
1173
Adrian Hunter2b5b8bb2014-07-22 16:17:59 +03001174enum dso_type dso__type_fd(int fd)
1175{
1176 enum dso_type dso_type = DSO__TYPE_UNKNOWN;
1177 GElf_Ehdr ehdr;
1178 Elf_Kind ek;
1179 Elf *elf;
1180
1181 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
1182 if (elf == NULL)
1183 goto out;
1184
1185 ek = elf_kind(elf);
1186 if (ek != ELF_K_ELF)
1187 goto out_end;
1188
1189 if (gelf_getclass(elf) == ELFCLASS64) {
1190 dso_type = DSO__TYPE_64BIT;
1191 goto out_end;
1192 }
1193
1194 if (gelf_getehdr(elf, &ehdr) == NULL)
1195 goto out_end;
1196
1197 if (ehdr.e_machine == EM_X86_64)
1198 dso_type = DSO__TYPE_X32BIT;
1199 else
1200 dso_type = DSO__TYPE_32BIT;
1201out_end:
1202 elf_end(elf);
1203out:
1204 return dso_type;
1205}
1206
Adrian Hunterafba19d2013-10-09 15:01:12 +03001207static int copy_bytes(int from, off_t from_offs, int to, off_t to_offs, u64 len)
1208{
1209 ssize_t r;
1210 size_t n;
1211 int err = -1;
1212 char *buf = malloc(page_size);
1213
1214 if (buf == NULL)
1215 return -1;
1216
1217 if (lseek(to, to_offs, SEEK_SET) != to_offs)
1218 goto out;
1219
1220 if (lseek(from, from_offs, SEEK_SET) != from_offs)
1221 goto out;
1222
1223 while (len) {
1224 n = page_size;
1225 if (len < n)
1226 n = len;
1227 /* Use read because mmap won't work on proc files */
1228 r = read(from, buf, n);
1229 if (r < 0)
1230 goto out;
1231 if (!r)
1232 break;
1233 n = r;
1234 r = write(to, buf, n);
1235 if (r < 0)
1236 goto out;
1237 if ((size_t)r != n)
1238 goto out;
1239 len -= n;
1240 }
1241
1242 err = 0;
1243out:
1244 free(buf);
1245 return err;
1246}
1247
1248struct kcore {
1249 int fd;
1250 int elfclass;
1251 Elf *elf;
1252 GElf_Ehdr ehdr;
1253};
1254
1255static int kcore__open(struct kcore *kcore, const char *filename)
1256{
1257 GElf_Ehdr *ehdr;
1258
1259 kcore->fd = open(filename, O_RDONLY);
1260 if (kcore->fd == -1)
1261 return -1;
1262
1263 kcore->elf = elf_begin(kcore->fd, ELF_C_READ, NULL);
1264 if (!kcore->elf)
1265 goto out_close;
1266
1267 kcore->elfclass = gelf_getclass(kcore->elf);
1268 if (kcore->elfclass == ELFCLASSNONE)
1269 goto out_end;
1270
1271 ehdr = gelf_getehdr(kcore->elf, &kcore->ehdr);
1272 if (!ehdr)
1273 goto out_end;
1274
1275 return 0;
1276
1277out_end:
1278 elf_end(kcore->elf);
1279out_close:
1280 close(kcore->fd);
1281 return -1;
1282}
1283
1284static int kcore__init(struct kcore *kcore, char *filename, int elfclass,
1285 bool temp)
1286{
Adrian Hunterafba19d2013-10-09 15:01:12 +03001287 kcore->elfclass = elfclass;
1288
1289 if (temp)
1290 kcore->fd = mkstemp(filename);
1291 else
1292 kcore->fd = open(filename, O_WRONLY | O_CREAT | O_EXCL, 0400);
1293 if (kcore->fd == -1)
1294 return -1;
1295
1296 kcore->elf = elf_begin(kcore->fd, ELF_C_WRITE, NULL);
1297 if (!kcore->elf)
1298 goto out_close;
1299
1300 if (!gelf_newehdr(kcore->elf, elfclass))
1301 goto out_end;
1302
Adrian Hunterb5cabbc2015-09-24 13:05:22 +03001303 memset(&kcore->ehdr, 0, sizeof(GElf_Ehdr));
Adrian Hunterafba19d2013-10-09 15:01:12 +03001304
1305 return 0;
1306
1307out_end:
1308 elf_end(kcore->elf);
1309out_close:
1310 close(kcore->fd);
1311 unlink(filename);
1312 return -1;
1313}
1314
1315static void kcore__close(struct kcore *kcore)
1316{
1317 elf_end(kcore->elf);
1318 close(kcore->fd);
1319}
1320
1321static int kcore__copy_hdr(struct kcore *from, struct kcore *to, size_t count)
1322{
1323 GElf_Ehdr *ehdr = &to->ehdr;
1324 GElf_Ehdr *kehdr = &from->ehdr;
1325
1326 memcpy(ehdr->e_ident, kehdr->e_ident, EI_NIDENT);
1327 ehdr->e_type = kehdr->e_type;
1328 ehdr->e_machine = kehdr->e_machine;
1329 ehdr->e_version = kehdr->e_version;
1330 ehdr->e_entry = 0;
1331 ehdr->e_shoff = 0;
1332 ehdr->e_flags = kehdr->e_flags;
1333 ehdr->e_phnum = count;
1334 ehdr->e_shentsize = 0;
1335 ehdr->e_shnum = 0;
1336 ehdr->e_shstrndx = 0;
1337
1338 if (from->elfclass == ELFCLASS32) {
1339 ehdr->e_phoff = sizeof(Elf32_Ehdr);
1340 ehdr->e_ehsize = sizeof(Elf32_Ehdr);
1341 ehdr->e_phentsize = sizeof(Elf32_Phdr);
1342 } else {
1343 ehdr->e_phoff = sizeof(Elf64_Ehdr);
1344 ehdr->e_ehsize = sizeof(Elf64_Ehdr);
1345 ehdr->e_phentsize = sizeof(Elf64_Phdr);
1346 }
1347
1348 if (!gelf_update_ehdr(to->elf, ehdr))
1349 return -1;
1350
1351 if (!gelf_newphdr(to->elf, count))
1352 return -1;
1353
1354 return 0;
1355}
1356
1357static int kcore__add_phdr(struct kcore *kcore, int idx, off_t offset,
1358 u64 addr, u64 len)
1359{
Adrian Hunterb5cabbc2015-09-24 13:05:22 +03001360 GElf_Phdr phdr = {
1361 .p_type = PT_LOAD,
1362 .p_flags = PF_R | PF_W | PF_X,
1363 .p_offset = offset,
1364 .p_vaddr = addr,
1365 .p_paddr = 0,
1366 .p_filesz = len,
1367 .p_memsz = len,
1368 .p_align = page_size,
1369 };
Adrian Hunterafba19d2013-10-09 15:01:12 +03001370
Adrian Hunterb5cabbc2015-09-24 13:05:22 +03001371 if (!gelf_update_phdr(kcore->elf, idx, &phdr))
Adrian Hunterafba19d2013-10-09 15:01:12 +03001372 return -1;
1373
1374 return 0;
1375}
1376
1377static off_t kcore__write(struct kcore *kcore)
1378{
1379 return elf_update(kcore->elf, ELF_C_WRITE);
1380}
1381
Adrian Hunterfc1b6912013-10-14 16:57:29 +03001382struct phdr_data {
1383 off_t offset;
1384 u64 addr;
1385 u64 len;
1386};
1387
1388struct kcore_copy_info {
1389 u64 stext;
1390 u64 etext;
1391 u64 first_symbol;
1392 u64 last_symbol;
1393 u64 first_module;
1394 u64 last_module_symbol;
1395 struct phdr_data kernel_map;
1396 struct phdr_data modules_map;
1397};
1398
1399static int kcore_copy__process_kallsyms(void *arg, const char *name, char type,
1400 u64 start)
1401{
1402 struct kcore_copy_info *kci = arg;
1403
1404 if (!symbol_type__is_a(type, MAP__FUNCTION))
1405 return 0;
1406
1407 if (strchr(name, '[')) {
1408 if (start > kci->last_module_symbol)
1409 kci->last_module_symbol = start;
1410 return 0;
1411 }
1412
1413 if (!kci->first_symbol || start < kci->first_symbol)
1414 kci->first_symbol = start;
1415
1416 if (!kci->last_symbol || start > kci->last_symbol)
1417 kci->last_symbol = start;
1418
1419 if (!strcmp(name, "_stext")) {
1420 kci->stext = start;
1421 return 0;
1422 }
1423
1424 if (!strcmp(name, "_etext")) {
1425 kci->etext = start;
1426 return 0;
1427 }
1428
1429 return 0;
1430}
1431
1432static int kcore_copy__parse_kallsyms(struct kcore_copy_info *kci,
1433 const char *dir)
1434{
1435 char kallsyms_filename[PATH_MAX];
1436
1437 scnprintf(kallsyms_filename, PATH_MAX, "%s/kallsyms", dir);
1438
1439 if (symbol__restricted_filename(kallsyms_filename, "/proc/kallsyms"))
1440 return -1;
1441
1442 if (kallsyms__parse(kallsyms_filename, kci,
1443 kcore_copy__process_kallsyms) < 0)
1444 return -1;
1445
1446 return 0;
1447}
1448
1449static int kcore_copy__process_modules(void *arg,
1450 const char *name __maybe_unused,
1451 u64 start)
1452{
1453 struct kcore_copy_info *kci = arg;
1454
1455 if (!kci->first_module || start < kci->first_module)
1456 kci->first_module = start;
1457
1458 return 0;
1459}
1460
1461static int kcore_copy__parse_modules(struct kcore_copy_info *kci,
1462 const char *dir)
1463{
1464 char modules_filename[PATH_MAX];
1465
1466 scnprintf(modules_filename, PATH_MAX, "%s/modules", dir);
1467
1468 if (symbol__restricted_filename(modules_filename, "/proc/modules"))
1469 return -1;
1470
1471 if (modules__parse(modules_filename, kci,
1472 kcore_copy__process_modules) < 0)
1473 return -1;
1474
1475 return 0;
1476}
1477
1478static void kcore_copy__map(struct phdr_data *p, u64 start, u64 end, u64 pgoff,
1479 u64 s, u64 e)
1480{
1481 if (p->addr || s < start || s >= end)
1482 return;
1483
1484 p->addr = s;
1485 p->offset = (s - start) + pgoff;
1486 p->len = e < end ? e - s : end - s;
1487}
1488
1489static int kcore_copy__read_map(u64 start, u64 len, u64 pgoff, void *data)
1490{
1491 struct kcore_copy_info *kci = data;
1492 u64 end = start + len;
1493
1494 kcore_copy__map(&kci->kernel_map, start, end, pgoff, kci->stext,
1495 kci->etext);
1496
1497 kcore_copy__map(&kci->modules_map, start, end, pgoff, kci->first_module,
1498 kci->last_module_symbol);
1499
1500 return 0;
1501}
1502
1503static int kcore_copy__read_maps(struct kcore_copy_info *kci, Elf *elf)
1504{
1505 if (elf_read_maps(elf, true, kcore_copy__read_map, kci) < 0)
1506 return -1;
1507
1508 return 0;
1509}
1510
1511static int kcore_copy__calc_maps(struct kcore_copy_info *kci, const char *dir,
1512 Elf *elf)
1513{
1514 if (kcore_copy__parse_kallsyms(kci, dir))
1515 return -1;
1516
1517 if (kcore_copy__parse_modules(kci, dir))
1518 return -1;
1519
1520 if (kci->stext)
1521 kci->stext = round_down(kci->stext, page_size);
1522 else
1523 kci->stext = round_down(kci->first_symbol, page_size);
1524
1525 if (kci->etext) {
1526 kci->etext = round_up(kci->etext, page_size);
1527 } else if (kci->last_symbol) {
1528 kci->etext = round_up(kci->last_symbol, page_size);
1529 kci->etext += page_size;
1530 }
1531
1532 kci->first_module = round_down(kci->first_module, page_size);
1533
1534 if (kci->last_module_symbol) {
1535 kci->last_module_symbol = round_up(kci->last_module_symbol,
1536 page_size);
1537 kci->last_module_symbol += page_size;
1538 }
1539
1540 if (!kci->stext || !kci->etext)
1541 return -1;
1542
1543 if (kci->first_module && !kci->last_module_symbol)
1544 return -1;
1545
1546 return kcore_copy__read_maps(kci, elf);
1547}
1548
1549static int kcore_copy__copy_file(const char *from_dir, const char *to_dir,
1550 const char *name)
1551{
1552 char from_filename[PATH_MAX];
1553 char to_filename[PATH_MAX];
1554
1555 scnprintf(from_filename, PATH_MAX, "%s/%s", from_dir, name);
1556 scnprintf(to_filename, PATH_MAX, "%s/%s", to_dir, name);
1557
1558 return copyfile_mode(from_filename, to_filename, 0400);
1559}
1560
1561static int kcore_copy__unlink(const char *dir, const char *name)
1562{
1563 char filename[PATH_MAX];
1564
1565 scnprintf(filename, PATH_MAX, "%s/%s", dir, name);
1566
1567 return unlink(filename);
1568}
1569
1570static int kcore_copy__compare_fds(int from, int to)
1571{
1572 char *buf_from;
1573 char *buf_to;
1574 ssize_t ret;
1575 size_t len;
1576 int err = -1;
1577
1578 buf_from = malloc(page_size);
1579 buf_to = malloc(page_size);
1580 if (!buf_from || !buf_to)
1581 goto out;
1582
1583 while (1) {
1584 /* Use read because mmap won't work on proc files */
1585 ret = read(from, buf_from, page_size);
1586 if (ret < 0)
1587 goto out;
1588
1589 if (!ret)
1590 break;
1591
1592 len = ret;
1593
1594 if (readn(to, buf_to, len) != (int)len)
1595 goto out;
1596
1597 if (memcmp(buf_from, buf_to, len))
1598 goto out;
1599 }
1600
1601 err = 0;
1602out:
1603 free(buf_to);
1604 free(buf_from);
1605 return err;
1606}
1607
1608static int kcore_copy__compare_files(const char *from_filename,
1609 const char *to_filename)
1610{
1611 int from, to, err = -1;
1612
1613 from = open(from_filename, O_RDONLY);
1614 if (from < 0)
1615 return -1;
1616
1617 to = open(to_filename, O_RDONLY);
1618 if (to < 0)
1619 goto out_close_from;
1620
1621 err = kcore_copy__compare_fds(from, to);
1622
1623 close(to);
1624out_close_from:
1625 close(from);
1626 return err;
1627}
1628
1629static int kcore_copy__compare_file(const char *from_dir, const char *to_dir,
1630 const char *name)
1631{
1632 char from_filename[PATH_MAX];
1633 char to_filename[PATH_MAX];
1634
1635 scnprintf(from_filename, PATH_MAX, "%s/%s", from_dir, name);
1636 scnprintf(to_filename, PATH_MAX, "%s/%s", to_dir, name);
1637
1638 return kcore_copy__compare_files(from_filename, to_filename);
1639}
1640
1641/**
1642 * kcore_copy - copy kallsyms, modules and kcore from one directory to another.
1643 * @from_dir: from directory
1644 * @to_dir: to directory
1645 *
1646 * This function copies kallsyms, modules and kcore files from one directory to
1647 * another. kallsyms and modules are copied entirely. Only code segments are
1648 * copied from kcore. It is assumed that two segments suffice: one for the
1649 * kernel proper and one for all the modules. The code segments are determined
1650 * from kallsyms and modules files. The kernel map starts at _stext or the
1651 * lowest function symbol, and ends at _etext or the highest function symbol.
1652 * The module map starts at the lowest module address and ends at the highest
1653 * module symbol. Start addresses are rounded down to the nearest page. End
1654 * addresses are rounded up to the nearest page. An extra page is added to the
1655 * highest kernel symbol and highest module symbol to, hopefully, encompass that
1656 * symbol too. Because it contains only code sections, the resulting kcore is
1657 * unusual. One significant peculiarity is that the mapping (start -> pgoff)
1658 * is not the same for the kernel map and the modules map. That happens because
1659 * the data is copied adjacently whereas the original kcore has gaps. Finally,
1660 * kallsyms and modules files are compared with their copies to check that
1661 * modules have not been loaded or unloaded while the copies were taking place.
1662 *
1663 * Return: %0 on success, %-1 on failure.
1664 */
1665int kcore_copy(const char *from_dir, const char *to_dir)
1666{
1667 struct kcore kcore;
1668 struct kcore extract;
1669 size_t count = 2;
1670 int idx = 0, err = -1;
1671 off_t offset = page_size, sz, modules_offset = 0;
1672 struct kcore_copy_info kci = { .stext = 0, };
1673 char kcore_filename[PATH_MAX];
1674 char extract_filename[PATH_MAX];
1675
1676 if (kcore_copy__copy_file(from_dir, to_dir, "kallsyms"))
1677 return -1;
1678
1679 if (kcore_copy__copy_file(from_dir, to_dir, "modules"))
1680 goto out_unlink_kallsyms;
1681
1682 scnprintf(kcore_filename, PATH_MAX, "%s/kcore", from_dir);
1683 scnprintf(extract_filename, PATH_MAX, "%s/kcore", to_dir);
1684
1685 if (kcore__open(&kcore, kcore_filename))
1686 goto out_unlink_modules;
1687
1688 if (kcore_copy__calc_maps(&kci, from_dir, kcore.elf))
1689 goto out_kcore_close;
1690
1691 if (kcore__init(&extract, extract_filename, kcore.elfclass, false))
1692 goto out_kcore_close;
1693
1694 if (!kci.modules_map.addr)
1695 count -= 1;
1696
1697 if (kcore__copy_hdr(&kcore, &extract, count))
1698 goto out_extract_close;
1699
1700 if (kcore__add_phdr(&extract, idx++, offset, kci.kernel_map.addr,
1701 kci.kernel_map.len))
1702 goto out_extract_close;
1703
1704 if (kci.modules_map.addr) {
1705 modules_offset = offset + kci.kernel_map.len;
1706 if (kcore__add_phdr(&extract, idx, modules_offset,
1707 kci.modules_map.addr, kci.modules_map.len))
1708 goto out_extract_close;
1709 }
1710
1711 sz = kcore__write(&extract);
1712 if (sz < 0 || sz > offset)
1713 goto out_extract_close;
1714
1715 if (copy_bytes(kcore.fd, kci.kernel_map.offset, extract.fd, offset,
1716 kci.kernel_map.len))
1717 goto out_extract_close;
1718
1719 if (modules_offset && copy_bytes(kcore.fd, kci.modules_map.offset,
1720 extract.fd, modules_offset,
1721 kci.modules_map.len))
1722 goto out_extract_close;
1723
1724 if (kcore_copy__compare_file(from_dir, to_dir, "modules"))
1725 goto out_extract_close;
1726
1727 if (kcore_copy__compare_file(from_dir, to_dir, "kallsyms"))
1728 goto out_extract_close;
1729
1730 err = 0;
1731
1732out_extract_close:
1733 kcore__close(&extract);
1734 if (err)
1735 unlink(extract_filename);
1736out_kcore_close:
1737 kcore__close(&kcore);
1738out_unlink_modules:
1739 if (err)
1740 kcore_copy__unlink(to_dir, "modules");
1741out_unlink_kallsyms:
1742 if (err)
1743 kcore_copy__unlink(to_dir, "kallsyms");
1744
1745 return err;
1746}
1747
Adrian Hunterafba19d2013-10-09 15:01:12 +03001748int kcore_extract__create(struct kcore_extract *kce)
1749{
1750 struct kcore kcore;
1751 struct kcore extract;
1752 size_t count = 1;
1753 int idx = 0, err = -1;
1754 off_t offset = page_size, sz;
1755
1756 if (kcore__open(&kcore, kce->kcore_filename))
1757 return -1;
1758
1759 strcpy(kce->extract_filename, PERF_KCORE_EXTRACT);
1760 if (kcore__init(&extract, kce->extract_filename, kcore.elfclass, true))
1761 goto out_kcore_close;
1762
1763 if (kcore__copy_hdr(&kcore, &extract, count))
1764 goto out_extract_close;
1765
1766 if (kcore__add_phdr(&extract, idx, offset, kce->addr, kce->len))
1767 goto out_extract_close;
1768
1769 sz = kcore__write(&extract);
1770 if (sz < 0 || sz > offset)
1771 goto out_extract_close;
1772
1773 if (copy_bytes(kcore.fd, kce->offs, extract.fd, offset, kce->len))
1774 goto out_extract_close;
1775
1776 err = 0;
1777
1778out_extract_close:
1779 kcore__close(&extract);
1780 if (err)
1781 unlink(kce->extract_filename);
1782out_kcore_close:
1783 kcore__close(&kcore);
1784
1785 return err;
1786}
1787
1788void kcore_extract__delete(struct kcore_extract *kce)
1789{
1790 unlink(kce->extract_filename);
1791}
1792
Hemant Kumar060fa0c2016-07-01 17:03:46 +09001793/**
1794 * populate_sdt_note : Parse raw data and identify SDT note
1795 * @elf: elf of the opened file
1796 * @data: raw data of a section with description offset applied
1797 * @len: note description size
1798 * @type: type of the note
1799 * @sdt_notes: List to add the SDT note
1800 *
1801 * Responsible for parsing the @data in section .note.stapsdt in @elf and
1802 * if its an SDT note, it appends to @sdt_notes list.
1803 */
1804static int populate_sdt_note(Elf **elf, const char *data, size_t len,
1805 struct list_head *sdt_notes)
1806{
1807 const char *provider, *name;
1808 struct sdt_note *tmp = NULL;
1809 GElf_Ehdr ehdr;
1810 GElf_Addr base_off = 0;
1811 GElf_Shdr shdr;
1812 int ret = -EINVAL;
1813
1814 union {
1815 Elf64_Addr a64[NR_ADDR];
1816 Elf32_Addr a32[NR_ADDR];
1817 } buf;
1818
1819 Elf_Data dst = {
1820 .d_buf = &buf, .d_type = ELF_T_ADDR, .d_version = EV_CURRENT,
1821 .d_size = gelf_fsize((*elf), ELF_T_ADDR, NR_ADDR, EV_CURRENT),
1822 .d_off = 0, .d_align = 0
1823 };
1824 Elf_Data src = {
1825 .d_buf = (void *) data, .d_type = ELF_T_ADDR,
1826 .d_version = EV_CURRENT, .d_size = dst.d_size, .d_off = 0,
1827 .d_align = 0
1828 };
1829
1830 tmp = (struct sdt_note *)calloc(1, sizeof(struct sdt_note));
1831 if (!tmp) {
1832 ret = -ENOMEM;
1833 goto out_err;
1834 }
1835
1836 INIT_LIST_HEAD(&tmp->note_list);
1837
1838 if (len < dst.d_size + 3)
1839 goto out_free_note;
1840
1841 /* Translation from file representation to memory representation */
1842 if (gelf_xlatetom(*elf, &dst, &src,
1843 elf_getident(*elf, NULL)[EI_DATA]) == NULL) {
1844 pr_err("gelf_xlatetom : %s\n", elf_errmsg(-1));
1845 goto out_free_note;
1846 }
1847
1848 /* Populate the fields of sdt_note */
1849 provider = data + dst.d_size;
1850
1851 name = (const char *)memchr(provider, '\0', data + len - provider);
1852 if (name++ == NULL)
1853 goto out_free_note;
1854
1855 tmp->provider = strdup(provider);
1856 if (!tmp->provider) {
1857 ret = -ENOMEM;
1858 goto out_free_note;
1859 }
1860 tmp->name = strdup(name);
1861 if (!tmp->name) {
1862 ret = -ENOMEM;
1863 goto out_free_prov;
1864 }
1865
1866 if (gelf_getclass(*elf) == ELFCLASS32) {
1867 memcpy(&tmp->addr, &buf, 3 * sizeof(Elf32_Addr));
1868 tmp->bit32 = true;
1869 } else {
1870 memcpy(&tmp->addr, &buf, 3 * sizeof(Elf64_Addr));
1871 tmp->bit32 = false;
1872 }
1873
1874 if (!gelf_getehdr(*elf, &ehdr)) {
1875 pr_debug("%s : cannot get elf header.\n", __func__);
1876 ret = -EBADF;
1877 goto out_free_name;
1878 }
1879
1880 /* Adjust the prelink effect :
1881 * Find out the .stapsdt.base section.
1882 * This scn will help us to handle prelinking (if present).
1883 * Compare the retrieved file offset of the base section with the
1884 * base address in the description of the SDT note. If its different,
1885 * then accordingly, adjust the note location.
1886 */
1887 if (elf_section_by_name(*elf, &ehdr, &shdr, SDT_BASE_SCN, NULL)) {
1888 base_off = shdr.sh_offset;
1889 if (base_off) {
1890 if (tmp->bit32)
1891 tmp->addr.a32[0] = tmp->addr.a32[0] + base_off -
1892 tmp->addr.a32[1];
1893 else
1894 tmp->addr.a64[0] = tmp->addr.a64[0] + base_off -
1895 tmp->addr.a64[1];
1896 }
1897 }
1898
1899 list_add_tail(&tmp->note_list, sdt_notes);
1900 return 0;
1901
1902out_free_name:
1903 free(tmp->name);
1904out_free_prov:
1905 free(tmp->provider);
1906out_free_note:
1907 free(tmp);
1908out_err:
1909 return ret;
1910}
1911
1912/**
1913 * construct_sdt_notes_list : constructs a list of SDT notes
1914 * @elf : elf to look into
1915 * @sdt_notes : empty list_head
1916 *
1917 * Scans the sections in 'elf' for the section
1918 * .note.stapsdt. It, then calls populate_sdt_note to find
1919 * out the SDT events and populates the 'sdt_notes'.
1920 */
1921static int construct_sdt_notes_list(Elf *elf, struct list_head *sdt_notes)
1922{
1923 GElf_Ehdr ehdr;
1924 Elf_Scn *scn = NULL;
1925 Elf_Data *data;
1926 GElf_Shdr shdr;
1927 size_t shstrndx, next;
1928 GElf_Nhdr nhdr;
1929 size_t name_off, desc_off, offset;
1930 int ret = 0;
1931
1932 if (gelf_getehdr(elf, &ehdr) == NULL) {
1933 ret = -EBADF;
1934 goto out_ret;
1935 }
1936 if (elf_getshdrstrndx(elf, &shstrndx) != 0) {
1937 ret = -EBADF;
1938 goto out_ret;
1939 }
1940
1941 /* Look for the required section */
1942 scn = elf_section_by_name(elf, &ehdr, &shdr, SDT_NOTE_SCN, NULL);
1943 if (!scn) {
1944 ret = -ENOENT;
1945 goto out_ret;
1946 }
1947
1948 if ((shdr.sh_type != SHT_NOTE) || (shdr.sh_flags & SHF_ALLOC)) {
1949 ret = -ENOENT;
1950 goto out_ret;
1951 }
1952
1953 data = elf_getdata(scn, NULL);
1954
1955 /* Get the SDT notes */
1956 for (offset = 0; (next = gelf_getnote(data, offset, &nhdr, &name_off,
1957 &desc_off)) > 0; offset = next) {
1958 if (nhdr.n_namesz == sizeof(SDT_NOTE_NAME) &&
1959 !memcmp(data->d_buf + name_off, SDT_NOTE_NAME,
1960 sizeof(SDT_NOTE_NAME))) {
1961 /* Check the type of the note */
1962 if (nhdr.n_type != SDT_NOTE_TYPE)
1963 goto out_ret;
1964
1965 ret = populate_sdt_note(&elf, ((data->d_buf) + desc_off),
1966 nhdr.n_descsz, sdt_notes);
1967 if (ret < 0)
1968 goto out_ret;
1969 }
1970 }
1971 if (list_empty(sdt_notes))
1972 ret = -ENOENT;
1973
1974out_ret:
1975 return ret;
1976}
1977
1978/**
1979 * get_sdt_note_list : Wrapper to construct a list of sdt notes
1980 * @head : empty list_head
1981 * @target : file to find SDT notes from
1982 *
1983 * This opens the file, initializes
1984 * the ELF and then calls construct_sdt_notes_list.
1985 */
1986int get_sdt_note_list(struct list_head *head, const char *target)
1987{
1988 Elf *elf;
1989 int fd, ret;
1990
1991 fd = open(target, O_RDONLY);
1992 if (fd < 0)
1993 return -EBADF;
1994
1995 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
1996 if (!elf) {
1997 ret = -EBADF;
1998 goto out_close;
1999 }
2000 ret = construct_sdt_notes_list(elf, head);
2001 elf_end(elf);
2002out_close:
2003 close(fd);
2004 return ret;
2005}
2006
2007/**
2008 * cleanup_sdt_note_list : free the sdt notes' list
2009 * @sdt_notes: sdt notes' list
2010 *
2011 * Free up the SDT notes in @sdt_notes.
2012 * Returns the number of SDT notes free'd.
2013 */
2014int cleanup_sdt_note_list(struct list_head *sdt_notes)
2015{
2016 struct sdt_note *tmp, *pos;
2017 int nr_free = 0;
2018
2019 list_for_each_entry_safe(pos, tmp, sdt_notes, note_list) {
2020 list_del(&pos->note_list);
2021 free(pos->name);
2022 free(pos->provider);
2023 free(pos);
2024 nr_free++;
2025 }
2026 return nr_free;
2027}
2028
2029/**
2030 * sdt_notes__get_count: Counts the number of sdt events
2031 * @start: list_head to sdt_notes list
2032 *
2033 * Returns the number of SDT notes in a list
2034 */
2035int sdt_notes__get_count(struct list_head *start)
2036{
2037 struct sdt_note *sdt_ptr;
2038 int count = 0;
2039
2040 list_for_each_entry(sdt_ptr, start, note_list)
2041 count++;
2042 return count;
2043}
2044
Namhyung Kime5a18452012-08-06 13:41:20 +09002045void symbol__elf_init(void)
2046{
2047 elf_version(EV_CURRENT);
2048}