perf tests: Adjust the vmlinux symtab matches kallsyms test
[linux-2.6-block.git] / tools / perf / util / symbol-elf.c
CommitLineData
e5a1845f
NK
1#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"
9#include "debug.h"
10
11#ifndef NT_GNU_BUILD_ID
12#define NT_GNU_BUILD_ID 3
13#endif
14
15/**
16 * elf_symtab__for_each_symbol - iterate thru all the symbols
17 *
18 * @syms: struct elf_symtab instance to iterate
19 * @idx: uint32_t idx
20 * @sym: GElf_Sym iterator
21 */
22#define elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) \
23 for (idx = 0, gelf_getsym(syms, idx, &sym);\
24 idx < nr_syms; \
25 idx++, gelf_getsym(syms, idx, &sym))
26
27static inline uint8_t elf_sym__type(const GElf_Sym *sym)
28{
29 return GELF_ST_TYPE(sym->st_info);
30}
31
32static inline int elf_sym__is_function(const GElf_Sym *sym)
33{
34 return elf_sym__type(sym) == STT_FUNC &&
35 sym->st_name != 0 &&
36 sym->st_shndx != SHN_UNDEF;
37}
38
39static inline bool elf_sym__is_object(const GElf_Sym *sym)
40{
41 return elf_sym__type(sym) == STT_OBJECT &&
42 sym->st_name != 0 &&
43 sym->st_shndx != SHN_UNDEF;
44}
45
46static inline int elf_sym__is_label(const GElf_Sym *sym)
47{
48 return elf_sym__type(sym) == STT_NOTYPE &&
49 sym->st_name != 0 &&
50 sym->st_shndx != SHN_UNDEF &&
51 sym->st_shndx != SHN_ABS;
52}
53
54static bool elf_sym__is_a(GElf_Sym *sym, enum map_type type)
55{
56 switch (type) {
57 case MAP__FUNCTION:
58 return elf_sym__is_function(sym);
59 case MAP__VARIABLE:
60 return elf_sym__is_object(sym);
61 default:
62 return false;
63 }
64}
65
66static inline const char *elf_sym__name(const GElf_Sym *sym,
67 const Elf_Data *symstrs)
68{
69 return symstrs->d_buf + sym->st_name;
70}
71
72static inline const char *elf_sec__name(const GElf_Shdr *shdr,
73 const Elf_Data *secstrs)
74{
75 return secstrs->d_buf + shdr->sh_name;
76}
77
78static inline int elf_sec__is_text(const GElf_Shdr *shdr,
79 const Elf_Data *secstrs)
80{
81 return strstr(elf_sec__name(shdr, secstrs), "text") != NULL;
82}
83
84static inline bool elf_sec__is_data(const GElf_Shdr *shdr,
85 const Elf_Data *secstrs)
86{
87 return strstr(elf_sec__name(shdr, secstrs), "data") != NULL;
88}
89
90static bool elf_sec__is_a(GElf_Shdr *shdr, Elf_Data *secstrs,
91 enum map_type type)
92{
93 switch (type) {
94 case MAP__FUNCTION:
95 return elf_sec__is_text(shdr, secstrs);
96 case MAP__VARIABLE:
97 return elf_sec__is_data(shdr, secstrs);
98 default:
99 return false;
100 }
101}
102
103static size_t elf_addr_to_index(Elf *elf, GElf_Addr addr)
104{
105 Elf_Scn *sec = NULL;
106 GElf_Shdr shdr;
107 size_t cnt = 1;
108
109 while ((sec = elf_nextscn(elf, sec)) != NULL) {
110 gelf_getshdr(sec, &shdr);
111
112 if ((addr >= shdr.sh_addr) &&
113 (addr < (shdr.sh_addr + shdr.sh_size)))
114 return cnt;
115
116 ++cnt;
117 }
118
119 return -1;
120}
121
122static Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep,
123 GElf_Shdr *shp, const char *name,
124 size_t *idx)
125{
126 Elf_Scn *sec = NULL;
127 size_t cnt = 1;
128
49274654
CS
129 /* Elf is corrupted/truncated, avoid calling elf_strptr. */
130 if (!elf_rawdata(elf_getscn(elf, ep->e_shstrndx), NULL))
131 return NULL;
132
e5a1845f
NK
133 while ((sec = elf_nextscn(elf, sec)) != NULL) {
134 char *str;
135
136 gelf_getshdr(sec, shp);
137 str = elf_strptr(elf, ep->e_shstrndx, shp->sh_name);
138 if (!strcmp(name, str)) {
139 if (idx)
140 *idx = cnt;
141 break;
142 }
143 ++cnt;
144 }
145
146 return sec;
147}
148
149#define elf_section__for_each_rel(reldata, pos, pos_mem, idx, nr_entries) \
150 for (idx = 0, pos = gelf_getrel(reldata, 0, &pos_mem); \
151 idx < nr_entries; \
152 ++idx, pos = gelf_getrel(reldata, idx, &pos_mem))
153
154#define elf_section__for_each_rela(reldata, pos, pos_mem, idx, nr_entries) \
155 for (idx = 0, pos = gelf_getrela(reldata, 0, &pos_mem); \
156 idx < nr_entries; \
157 ++idx, pos = gelf_getrela(reldata, idx, &pos_mem))
158
159/*
160 * We need to check if we have a .dynsym, so that we can handle the
161 * .plt, synthesizing its symbols, that aren't on the symtabs (be it
162 * .dynsym or .symtab).
163 * And always look at the original dso, not at debuginfo packages, that
164 * have the PLT data stripped out (shdr_rel_plt.sh_type == SHT_NOBITS).
165 */
a44f605b 166int dso__synthesize_plt_symbols(struct dso *dso, struct symsrc *ss, struct map *map,
e5a1845f
NK
167 symbol_filter_t filter)
168{
169 uint32_t nr_rel_entries, idx;
170 GElf_Sym sym;
171 u64 plt_offset;
172 GElf_Shdr shdr_plt;
173 struct symbol *f;
174 GElf_Shdr shdr_rel_plt, shdr_dynsym;
175 Elf_Data *reldata, *syms, *symstrs;
176 Elf_Scn *scn_plt_rel, *scn_symstrs, *scn_dynsym;
177 size_t dynsym_idx;
178 GElf_Ehdr ehdr;
179 char sympltname[1024];
180 Elf *elf;
a44f605b 181 int nr = 0, symidx, err = 0;
e5a1845f 182
f47b58b7
DA
183 if (!ss->dynsym)
184 return 0;
185
a44f605b
CS
186 elf = ss->elf;
187 ehdr = ss->ehdr;
e5a1845f 188
a44f605b
CS
189 scn_dynsym = ss->dynsym;
190 shdr_dynsym = ss->dynshdr;
191 dynsym_idx = ss->dynsym_idx;
e5a1845f 192
e5a1845f
NK
193 if (scn_dynsym == NULL)
194 goto out_elf_end;
195
196 scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt,
197 ".rela.plt", NULL);
198 if (scn_plt_rel == NULL) {
199 scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt,
200 ".rel.plt", NULL);
201 if (scn_plt_rel == NULL)
202 goto out_elf_end;
203 }
204
205 err = -1;
206
207 if (shdr_rel_plt.sh_link != dynsym_idx)
208 goto out_elf_end;
209
210 if (elf_section_by_name(elf, &ehdr, &shdr_plt, ".plt", NULL) == NULL)
211 goto out_elf_end;
212
213 /*
214 * Fetch the relocation section to find the idxes to the GOT
215 * and the symbols in the .dynsym they refer to.
216 */
217 reldata = elf_getdata(scn_plt_rel, NULL);
218 if (reldata == NULL)
219 goto out_elf_end;
220
221 syms = elf_getdata(scn_dynsym, NULL);
222 if (syms == NULL)
223 goto out_elf_end;
224
225 scn_symstrs = elf_getscn(elf, shdr_dynsym.sh_link);
226 if (scn_symstrs == NULL)
227 goto out_elf_end;
228
229 symstrs = elf_getdata(scn_symstrs, NULL);
230 if (symstrs == NULL)
231 goto out_elf_end;
232
52f9ddba
CS
233 if (symstrs->d_size == 0)
234 goto out_elf_end;
235
e5a1845f
NK
236 nr_rel_entries = shdr_rel_plt.sh_size / shdr_rel_plt.sh_entsize;
237 plt_offset = shdr_plt.sh_offset;
238
239 if (shdr_rel_plt.sh_type == SHT_RELA) {
240 GElf_Rela pos_mem, *pos;
241
242 elf_section__for_each_rela(reldata, pos, pos_mem, idx,
243 nr_rel_entries) {
244 symidx = GELF_R_SYM(pos->r_info);
245 plt_offset += shdr_plt.sh_entsize;
246 gelf_getsym(syms, symidx, &sym);
247 snprintf(sympltname, sizeof(sympltname),
248 "%s@plt", elf_sym__name(&sym, symstrs));
249
250 f = symbol__new(plt_offset, shdr_plt.sh_entsize,
251 STB_GLOBAL, sympltname);
252 if (!f)
253 goto out_elf_end;
254
255 if (filter && filter(map, f))
256 symbol__delete(f);
257 else {
258 symbols__insert(&dso->symbols[map->type], f);
259 ++nr;
260 }
261 }
262 } else if (shdr_rel_plt.sh_type == SHT_REL) {
263 GElf_Rel pos_mem, *pos;
264 elf_section__for_each_rel(reldata, pos, pos_mem, idx,
265 nr_rel_entries) {
266 symidx = GELF_R_SYM(pos->r_info);
267 plt_offset += shdr_plt.sh_entsize;
268 gelf_getsym(syms, symidx, &sym);
269 snprintf(sympltname, sizeof(sympltname),
270 "%s@plt", elf_sym__name(&sym, symstrs));
271
272 f = symbol__new(plt_offset, shdr_plt.sh_entsize,
273 STB_GLOBAL, sympltname);
274 if (!f)
275 goto out_elf_end;
276
277 if (filter && filter(map, f))
278 symbol__delete(f);
279 else {
280 symbols__insert(&dso->symbols[map->type], f);
281 ++nr;
282 }
283 }
284 }
285
286 err = 0;
287out_elf_end:
e5a1845f
NK
288 if (err == 0)
289 return nr;
e5a1845f
NK
290 pr_debug("%s: problems reading %s PLT info.\n",
291 __func__, dso->long_name);
292 return 0;
293}
294
295/*
296 * Align offset to 4 bytes as needed for note name and descriptor data.
297 */
298#define NOTE_ALIGN(n) (((n) + 3) & -4U)
299
300static int elf_read_build_id(Elf *elf, void *bf, size_t size)
301{
302 int err = -1;
303 GElf_Ehdr ehdr;
304 GElf_Shdr shdr;
305 Elf_Data *data;
306 Elf_Scn *sec;
307 Elf_Kind ek;
308 void *ptr;
309
310 if (size < BUILD_ID_SIZE)
311 goto out;
312
313 ek = elf_kind(elf);
314 if (ek != ELF_K_ELF)
315 goto out;
316
317 if (gelf_getehdr(elf, &ehdr) == NULL) {
318 pr_err("%s: cannot get elf header.\n", __func__);
319 goto out;
320 }
321
322 /*
323 * Check following sections for notes:
324 * '.note.gnu.build-id'
325 * '.notes'
326 * '.note' (VDSO specific)
327 */
328 do {
329 sec = elf_section_by_name(elf, &ehdr, &shdr,
330 ".note.gnu.build-id", NULL);
331 if (sec)
332 break;
333
334 sec = elf_section_by_name(elf, &ehdr, &shdr,
335 ".notes", NULL);
336 if (sec)
337 break;
338
339 sec = elf_section_by_name(elf, &ehdr, &shdr,
340 ".note", NULL);
341 if (sec)
342 break;
343
344 return err;
345
346 } while (0);
347
348 data = elf_getdata(sec, NULL);
349 if (data == NULL)
350 goto out;
351
352 ptr = data->d_buf;
353 while (ptr < (data->d_buf + data->d_size)) {
354 GElf_Nhdr *nhdr = ptr;
355 size_t namesz = NOTE_ALIGN(nhdr->n_namesz),
356 descsz = NOTE_ALIGN(nhdr->n_descsz);
357 const char *name;
358
359 ptr += sizeof(*nhdr);
360 name = ptr;
361 ptr += namesz;
362 if (nhdr->n_type == NT_GNU_BUILD_ID &&
363 nhdr->n_namesz == sizeof("GNU")) {
364 if (memcmp(name, "GNU", sizeof("GNU")) == 0) {
365 size_t sz = min(size, descsz);
366 memcpy(bf, ptr, sz);
367 memset(bf + sz, 0, size - sz);
368 err = descsz;
369 break;
370 }
371 }
372 ptr += descsz;
373 }
374
375out:
376 return err;
377}
378
379int filename__read_build_id(const char *filename, void *bf, size_t size)
380{
381 int fd, err = -1;
382 Elf *elf;
383
384 if (size < BUILD_ID_SIZE)
385 goto out;
386
387 fd = open(filename, O_RDONLY);
388 if (fd < 0)
389 goto out;
390
391 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
392 if (elf == NULL) {
393 pr_debug2("%s: cannot read %s ELF file.\n", __func__, filename);
394 goto out_close;
395 }
396
397 err = elf_read_build_id(elf, bf, size);
398
399 elf_end(elf);
400out_close:
401 close(fd);
402out:
403 return err;
404}
405
406int sysfs__read_build_id(const char *filename, void *build_id, size_t size)
407{
408 int fd, err = -1;
409
410 if (size < BUILD_ID_SIZE)
411 goto out;
412
413 fd = open(filename, O_RDONLY);
414 if (fd < 0)
415 goto out;
416
417 while (1) {
418 char bf[BUFSIZ];
419 GElf_Nhdr nhdr;
420 size_t namesz, descsz;
421
422 if (read(fd, &nhdr, sizeof(nhdr)) != sizeof(nhdr))
423 break;
424
425 namesz = NOTE_ALIGN(nhdr.n_namesz);
426 descsz = NOTE_ALIGN(nhdr.n_descsz);
427 if (nhdr.n_type == NT_GNU_BUILD_ID &&
428 nhdr.n_namesz == sizeof("GNU")) {
429 if (read(fd, bf, namesz) != (ssize_t)namesz)
430 break;
431 if (memcmp(bf, "GNU", sizeof("GNU")) == 0) {
432 size_t sz = min(descsz, size);
433 if (read(fd, build_id, sz) == (ssize_t)sz) {
434 memset(build_id + sz, 0, size - sz);
435 err = 0;
436 break;
437 }
438 } else if (read(fd, bf, descsz) != (ssize_t)descsz)
439 break;
440 } else {
441 int n = namesz + descsz;
442 if (read(fd, bf, n) != n)
443 break;
444 }
445 }
446 close(fd);
447out:
448 return err;
449}
450
451int filename__read_debuglink(const char *filename, char *debuglink,
452 size_t size)
453{
454 int fd, err = -1;
455 Elf *elf;
456 GElf_Ehdr ehdr;
457 GElf_Shdr shdr;
458 Elf_Data *data;
459 Elf_Scn *sec;
460 Elf_Kind ek;
461
462 fd = open(filename, O_RDONLY);
463 if (fd < 0)
464 goto out;
465
466 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
467 if (elf == NULL) {
468 pr_debug2("%s: cannot read %s ELF file.\n", __func__, filename);
469 goto out_close;
470 }
471
472 ek = elf_kind(elf);
473 if (ek != ELF_K_ELF)
474 goto out_close;
475
476 if (gelf_getehdr(elf, &ehdr) == NULL) {
477 pr_err("%s: cannot get elf header.\n", __func__);
478 goto out_close;
479 }
480
481 sec = elf_section_by_name(elf, &ehdr, &shdr,
482 ".gnu_debuglink", NULL);
483 if (sec == NULL)
484 goto out_close;
485
486 data = elf_getdata(sec, NULL);
487 if (data == NULL)
488 goto out_close;
489
490 /* the start of this section is a zero-terminated string */
491 strncpy(debuglink, data->d_buf, size);
492
493 elf_end(elf);
494
495out_close:
496 close(fd);
497out:
498 return err;
499}
500
501static int dso__swap_init(struct dso *dso, unsigned char eidata)
502{
503 static unsigned int const endian = 1;
504
505 dso->needs_swap = DSO_SWAP__NO;
506
507 switch (eidata) {
508 case ELFDATA2LSB:
509 /* We are big endian, DSO is little endian. */
510 if (*(unsigned char const *)&endian != 1)
511 dso->needs_swap = DSO_SWAP__YES;
512 break;
513
514 case ELFDATA2MSB:
515 /* We are little endian, DSO is big endian. */
516 if (*(unsigned char const *)&endian != 0)
517 dso->needs_swap = DSO_SWAP__YES;
518 break;
519
520 default:
521 pr_err("unrecognized DSO data encoding %d\n", eidata);
522 return -EINVAL;
523 }
524
525 return 0;
526}
527
3aafe5ae
CS
528bool symsrc__possibly_runtime(struct symsrc *ss)
529{
530 return ss->dynsym || ss->opdsec;
531}
532
d26cd12b
CS
533bool symsrc__has_symtab(struct symsrc *ss)
534{
535 return ss->symtab != NULL;
536}
b68e2f91
CS
537
538void symsrc__destroy(struct symsrc *ss)
539{
540 free(ss->name);
541 elf_end(ss->elf);
542 close(ss->fd);
543}
544
545int symsrc__init(struct symsrc *ss, struct dso *dso, const char *name,
546 enum dso_binary_type type)
e5a1845f 547{
e5a1845f 548 int err = -1;
e5a1845f 549 GElf_Ehdr ehdr;
e5a1845f 550 Elf *elf;
b68e2f91
CS
551 int fd;
552
553 fd = open(name, O_RDONLY);
554 if (fd < 0)
555 return -1;
e5a1845f
NK
556
557 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
558 if (elf == NULL) {
559 pr_debug("%s: cannot read %s ELF file.\n", __func__, name);
560 goto out_close;
561 }
562
563 if (gelf_getehdr(elf, &ehdr) == NULL) {
564 pr_debug("%s: cannot get elf header.\n", __func__);
565 goto out_elf_end;
566 }
567
568 if (dso__swap_init(dso, ehdr.e_ident[EI_DATA]))
569 goto out_elf_end;
570
571 /* Always reject images with a mismatched build-id: */
572 if (dso->has_build_id) {
573 u8 build_id[BUILD_ID_SIZE];
574
575 if (elf_read_build_id(elf, build_id, BUILD_ID_SIZE) < 0)
576 goto out_elf_end;
577
578 if (!dso__build_id_equal(dso, build_id))
579 goto out_elf_end;
580 }
581
b68e2f91
CS
582 ss->symtab = elf_section_by_name(elf, &ehdr, &ss->symshdr, ".symtab",
583 NULL);
584 if (ss->symshdr.sh_type != SHT_SYMTAB)
585 ss->symtab = NULL;
586
587 ss->dynsym_idx = 0;
588 ss->dynsym = elf_section_by_name(elf, &ehdr, &ss->dynshdr, ".dynsym",
589 &ss->dynsym_idx);
590 if (ss->dynshdr.sh_type != SHT_DYNSYM)
591 ss->dynsym = NULL;
592
593 ss->opdidx = 0;
594 ss->opdsec = elf_section_by_name(elf, &ehdr, &ss->opdshdr, ".opd",
595 &ss->opdidx);
596 if (ss->opdshdr.sh_type != SHT_PROGBITS)
597 ss->opdsec = NULL;
598
599 if (dso->kernel == DSO_TYPE_USER) {
600 GElf_Shdr shdr;
601 ss->adjust_symbols = (ehdr.e_type == ET_EXEC ||
602 elf_section_by_name(elf, &ehdr, &shdr,
603 ".gnu.prelink_undo",
604 NULL) != NULL);
605 } else {
39b12f78 606 ss->adjust_symbols = ehdr.e_type == ET_EXEC;
b68e2f91
CS
607 }
608
609 ss->name = strdup(name);
610 if (!ss->name)
611 goto out_elf_end;
612
613 ss->elf = elf;
614 ss->fd = fd;
615 ss->ehdr = ehdr;
616 ss->type = type;
617
618 return 0;
619
620out_elf_end:
621 elf_end(elf);
622out_close:
623 close(fd);
624 return err;
625}
626
39b12f78
AH
627/**
628 * ref_reloc_sym_not_found - has kernel relocation symbol been found.
629 * @kmap: kernel maps and relocation reference symbol
630 *
631 * This function returns %true if we are dealing with the kernel maps and the
632 * relocation reference symbol has not yet been found. Otherwise %false is
633 * returned.
634 */
635static bool ref_reloc_sym_not_found(struct kmap *kmap)
636{
637 return kmap && kmap->ref_reloc_sym && kmap->ref_reloc_sym->name &&
638 !kmap->ref_reloc_sym->unrelocated_addr;
639}
640
641/**
642 * ref_reloc - kernel relocation offset.
643 * @kmap: kernel maps and relocation reference symbol
644 *
645 * This function returns the offset of kernel addresses as determined by using
646 * the relocation reference symbol i.e. if the kernel has not been relocated
647 * then the return value is zero.
648 */
649static u64 ref_reloc(struct kmap *kmap)
650{
651 if (kmap && kmap->ref_reloc_sym &&
652 kmap->ref_reloc_sym->unrelocated_addr)
653 return kmap->ref_reloc_sym->addr -
654 kmap->ref_reloc_sym->unrelocated_addr;
655 return 0;
656}
657
261360b6
CS
658int dso__load_sym(struct dso *dso, struct map *map,
659 struct symsrc *syms_ss, struct symsrc *runtime_ss,
d26cd12b 660 symbol_filter_t filter, int kmodule)
b68e2f91
CS
661{
662 struct kmap *kmap = dso->kernel ? map__kmap(map) : NULL;
663 struct map *curr_map = map;
664 struct dso *curr_dso = dso;
665 Elf_Data *symstrs, *secstrs;
666 uint32_t nr_syms;
667 int err = -1;
668 uint32_t idx;
669 GElf_Ehdr ehdr;
261360b6 670 GElf_Shdr shdr;
b68e2f91
CS
671 Elf_Data *syms, *opddata = NULL;
672 GElf_Sym sym;
261360b6 673 Elf_Scn *sec, *sec_strndx;
b68e2f91
CS
674 Elf *elf;
675 int nr = 0;
39b12f78 676 bool remap_kernel = false, adjust_kernel_syms = false;
b68e2f91 677
261360b6 678 dso->symtab_type = syms_ss->type;
005f9294 679
261360b6
CS
680 if (!syms_ss->symtab) {
681 syms_ss->symtab = syms_ss->dynsym;
682 syms_ss->symshdr = syms_ss->dynshdr;
d26cd12b
CS
683 }
684
261360b6
CS
685 elf = syms_ss->elf;
686 ehdr = syms_ss->ehdr;
687 sec = syms_ss->symtab;
688 shdr = syms_ss->symshdr;
b68e2f91 689
261360b6
CS
690 if (runtime_ss->opdsec)
691 opddata = elf_rawdata(runtime_ss->opdsec, NULL);
e5a1845f
NK
692
693 syms = elf_getdata(sec, NULL);
694 if (syms == NULL)
695 goto out_elf_end;
696
697 sec = elf_getscn(elf, shdr.sh_link);
698 if (sec == NULL)
699 goto out_elf_end;
700
701 symstrs = elf_getdata(sec, NULL);
702 if (symstrs == NULL)
703 goto out_elf_end;
704
705 sec_strndx = elf_getscn(elf, ehdr.e_shstrndx);
706 if (sec_strndx == NULL)
707 goto out_elf_end;
708
709 secstrs = elf_getdata(sec_strndx, NULL);
710 if (secstrs == NULL)
711 goto out_elf_end;
712
713 nr_syms = shdr.sh_size / shdr.sh_entsize;
714
715 memset(&sym, 0, sizeof(sym));
39b12f78
AH
716
717 /*
718 * The kernel relocation symbol is needed in advance in order to adjust
719 * kernel maps correctly.
720 */
721 if (ref_reloc_sym_not_found(kmap)) {
722 elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) {
723 const char *elf_name = elf_sym__name(&sym, symstrs);
724
725 if (strcmp(elf_name, kmap->ref_reloc_sym->name))
726 continue;
727 kmap->ref_reloc_sym->unrelocated_addr = sym.st_value;
728 break;
729 }
730 }
731
732 dso->adjust_symbols = runtime_ss->adjust_symbols || ref_reloc(kmap);
733 /*
734 * Initial kernel and module mappings do not map to the dso. For
735 * function mappings, flag the fixups.
736 */
737 if (map->type == MAP__FUNCTION && (dso->kernel || kmodule)) {
738 remap_kernel = true;
739 adjust_kernel_syms = dso->adjust_symbols;
740 }
e5a1845f
NK
741 elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) {
742 struct symbol *f;
743 const char *elf_name = elf_sym__name(&sym, symstrs);
744 char *demangled = NULL;
745 int is_label = elf_sym__is_label(&sym);
746 const char *section_name;
261360b6 747 bool used_opd = false;
e5a1845f 748
e5a1845f
NK
749 if (!is_label && !elf_sym__is_a(&sym, map->type))
750 continue;
751
752 /* Reject ARM ELF "mapping symbols": these aren't unique and
753 * don't identify functions, so will confuse the profile
754 * output: */
755 if (ehdr.e_machine == EM_ARM) {
756 if (!strcmp(elf_name, "$a") ||
757 !strcmp(elf_name, "$d") ||
758 !strcmp(elf_name, "$t"))
759 continue;
760 }
761
261360b6
CS
762 if (runtime_ss->opdsec && sym.st_shndx == runtime_ss->opdidx) {
763 u32 offset = sym.st_value - syms_ss->opdshdr.sh_addr;
e5a1845f
NK
764 u64 *opd = opddata->d_buf + offset;
765 sym.st_value = DSO__SWAP(dso, u64, *opd);
261360b6
CS
766 sym.st_shndx = elf_addr_to_index(runtime_ss->elf,
767 sym.st_value);
768 used_opd = true;
e5a1845f 769 }
3843b05d
NK
770 /*
771 * When loading symbols in a data mapping, ABS symbols (which
772 * has a value of SHN_ABS in its st_shndx) failed at
773 * elf_getscn(). And it marks the loading as a failure so
774 * already loaded symbols cannot be fixed up.
775 *
776 * I'm not sure what should be done. Just ignore them for now.
777 * - Namhyung Kim
778 */
779 if (sym.st_shndx == SHN_ABS)
780 continue;
e5a1845f 781
261360b6 782 sec = elf_getscn(runtime_ss->elf, sym.st_shndx);
e5a1845f
NK
783 if (!sec)
784 goto out_elf_end;
785
786 gelf_getshdr(sec, &shdr);
787
788 if (is_label && !elf_sec__is_a(&shdr, secstrs, map->type))
789 continue;
790
791 section_name = elf_sec__name(&shdr, secstrs);
792
793 /* On ARM, symbols for thumb functions have 1 added to
794 * the symbol address as a flag - remove it */
795 if ((ehdr.e_machine == EM_ARM) &&
796 (map->type == MAP__FUNCTION) &&
797 (sym.st_value & 1))
798 --sym.st_value;
799
39b12f78 800 if (dso->kernel || kmodule) {
e5a1845f
NK
801 char dso_name[PATH_MAX];
802
39b12f78
AH
803 /* Adjust symbol to map to file offset */
804 if (adjust_kernel_syms)
805 sym.st_value -= shdr.sh_addr - shdr.sh_offset;
806
e5a1845f
NK
807 if (strcmp(section_name,
808 (curr_dso->short_name +
809 dso->short_name_len)) == 0)
810 goto new_symbol;
811
812 if (strcmp(section_name, ".text") == 0) {
39b12f78
AH
813 /*
814 * The initial kernel mapping is based on
815 * kallsyms and identity maps. Overwrite it to
816 * map to the kernel dso.
817 */
818 if (remap_kernel && dso->kernel) {
819 remap_kernel = false;
820 map->start = shdr.sh_addr +
821 ref_reloc(kmap);
822 map->end = map->start + shdr.sh_size;
823 map->pgoff = shdr.sh_offset;
824 map->map_ip = map__map_ip;
825 map->unmap_ip = map__unmap_ip;
826 /* Ensure maps are correctly ordered */
827 map_groups__remove(kmap->kmaps, map);
828 map_groups__insert(kmap->kmaps, map);
829 }
830
e5a1845f
NK
831 curr_map = map;
832 curr_dso = dso;
833 goto new_symbol;
834 }
835
836 snprintf(dso_name, sizeof(dso_name),
837 "%s%s", dso->short_name, section_name);
838
839 curr_map = map_groups__find_by_name(kmap->kmaps, map->type, dso_name);
840 if (curr_map == NULL) {
841 u64 start = sym.st_value;
842
843 if (kmodule)
844 start += map->start + shdr.sh_offset;
845
846 curr_dso = dso__new(dso_name);
847 if (curr_dso == NULL)
848 goto out_elf_end;
849 curr_dso->kernel = dso->kernel;
850 curr_dso->long_name = dso->long_name;
851 curr_dso->long_name_len = dso->long_name_len;
852 curr_map = map__new2(start, curr_dso,
853 map->type);
854 if (curr_map == NULL) {
855 dso__delete(curr_dso);
856 goto out_elf_end;
857 }
39b12f78
AH
858 if (adjust_kernel_syms) {
859 curr_map->start = shdr.sh_addr +
860 ref_reloc(kmap);
861 curr_map->end = curr_map->start +
862 shdr.sh_size;
863 curr_map->pgoff = shdr.sh_offset;
864 } else {
865 curr_map->map_ip = identity__map_ip;
866 curr_map->unmap_ip = identity__map_ip;
867 }
e5a1845f
NK
868 curr_dso->symtab_type = dso->symtab_type;
869 map_groups__insert(kmap->kmaps, curr_map);
870 dsos__add(&dso->node, curr_dso);
871 dso__set_loaded(curr_dso, map->type);
872 } else
873 curr_dso = curr_map->dso;
874
875 goto new_symbol;
876 }
877
261360b6
CS
878 if ((used_opd && runtime_ss->adjust_symbols)
879 || (!used_opd && syms_ss->adjust_symbols)) {
e5a1845f
NK
880 pr_debug4("%s: adjusting symbol: st_value: %#" PRIx64 " "
881 "sh_addr: %#" PRIx64 " sh_offset: %#" PRIx64 "\n", __func__,
882 (u64)sym.st_value, (u64)shdr.sh_addr,
883 (u64)shdr.sh_offset);
884 sym.st_value -= shdr.sh_addr - shdr.sh_offset;
885 }
886 /*
887 * We need to figure out if the object was created from C++ sources
888 * DWARF DW_compile_unit has this, but we don't always have access
889 * to it...
890 */
328ccdac
NK
891 if (symbol_conf.demangle) {
892 demangled = bfd_demangle(NULL, elf_name,
893 DMGL_PARAMS | DMGL_ANSI);
894 if (demangled != NULL)
895 elf_name = demangled;
896 }
e5a1845f
NK
897new_symbol:
898 f = symbol__new(sym.st_value, sym.st_size,
899 GELF_ST_BIND(sym.st_info), elf_name);
900 free(demangled);
901 if (!f)
902 goto out_elf_end;
903
904 if (filter && filter(curr_map, f))
905 symbol__delete(f);
906 else {
907 symbols__insert(&curr_dso->symbols[curr_map->type], f);
908 nr++;
909 }
910 }
911
912 /*
913 * For misannotated, zeroed, ASM function sizes.
914 */
915 if (nr > 0) {
916 symbols__fixup_duplicate(&dso->symbols[map->type]);
917 symbols__fixup_end(&dso->symbols[map->type]);
918 if (kmap) {
919 /*
920 * We need to fixup this here too because we create new
921 * maps here, for things like vsyscall sections.
922 */
923 __map_groups__fixup_end(kmap->kmaps, map->type);
924 }
925 }
926 err = nr;
927out_elf_end:
e5a1845f
NK
928 return err;
929}
930
931void symbol__elf_init(void)
932{
933 elf_version(EV_CURRENT);
934}