perf symbols: Old versions of elf.h don't have NT_GNU_BUILD_ID
[deliverable/linux.git] / tools / perf / util / symbol.c
1 #include "util.h"
2 #include "../perf.h"
3 #include "string.h"
4 #include "symbol.h"
5 #include "thread.h"
6
7 #include "debug.h"
8
9 #include <libelf.h>
10 #include <gelf.h>
11 #include <elf.h>
12 #include <limits.h>
13 #include <sys/utsname.h>
14
15 #ifndef NT_GNU_BUILD_ID
16 #define NT_GNU_BUILD_ID 3
17 #endif
18
19 enum dso_origin {
20 DSO__ORIG_KERNEL = 0,
21 DSO__ORIG_JAVA_JIT,
22 DSO__ORIG_FEDORA,
23 DSO__ORIG_UBUNTU,
24 DSO__ORIG_BUILDID,
25 DSO__ORIG_DSO,
26 DSO__ORIG_KMODULE,
27 DSO__ORIG_NOT_FOUND,
28 };
29
30 static void dsos__add(struct dso *dso);
31 static struct dso *dsos__find(const char *name);
32 static struct map *map__new2(u64 start, struct dso *dso);
33 static void kernel_maps__insert(struct map *map);
34 static int dso__load_kernel_sym(struct dso *self, struct map *map,
35 symbol_filter_t filter);
36 unsigned int symbol__priv_size;
37
38 static struct rb_root kernel_maps;
39
40 static void dso__fixup_sym_end(struct dso *self)
41 {
42 struct rb_node *nd, *prevnd = rb_first(&self->syms);
43 struct symbol *curr, *prev;
44
45 if (prevnd == NULL)
46 return;
47
48 curr = rb_entry(prevnd, struct symbol, rb_node);
49
50 for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
51 prev = curr;
52 curr = rb_entry(nd, struct symbol, rb_node);
53
54 if (prev->end == prev->start)
55 prev->end = curr->start - 1;
56 }
57
58 /* Last entry */
59 if (curr->end == curr->start)
60 curr->end = roundup(curr->start, 4096);
61 }
62
63 static void kernel_maps__fixup_end(void)
64 {
65 struct map *prev, *curr;
66 struct rb_node *nd, *prevnd = rb_first(&kernel_maps);
67
68 if (prevnd == NULL)
69 return;
70
71 curr = rb_entry(prevnd, struct map, rb_node);
72
73 for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
74 prev = curr;
75 curr = rb_entry(nd, struct map, rb_node);
76 prev->end = curr->start - 1;
77 }
78
79 /*
80 * We still haven't the actual symbols, so guess the
81 * last map final address.
82 */
83 curr->end = ~0UL;
84 }
85
86 static struct symbol *symbol__new(u64 start, u64 len, const char *name)
87 {
88 size_t namelen = strlen(name) + 1;
89 struct symbol *self = calloc(1, (symbol__priv_size +
90 sizeof(*self) + namelen));
91 if (!self)
92 return NULL;
93
94 if (symbol__priv_size) {
95 memset(self, 0, symbol__priv_size);
96 self = ((void *)self) + symbol__priv_size;
97 }
98 self->start = start;
99 self->end = len ? start + len - 1 : start;
100
101 pr_debug3("%s: %s %#Lx-%#Lx\n", __func__, name, start, self->end);
102
103 memcpy(self->name, name, namelen);
104
105 return self;
106 }
107
108 static void symbol__delete(struct symbol *self)
109 {
110 free(((void *)self) - symbol__priv_size);
111 }
112
113 static size_t symbol__fprintf(struct symbol *self, FILE *fp)
114 {
115 return fprintf(fp, " %llx-%llx %s\n",
116 self->start, self->end, self->name);
117 }
118
119 static void dso__set_long_name(struct dso *self, char *name)
120 {
121 if (name == NULL)
122 return;
123 self->long_name = name;
124 self->long_name_len = strlen(name);
125 }
126
127 static void dso__set_basename(struct dso *self)
128 {
129 self->short_name = basename(self->long_name);
130 }
131
132 struct dso *dso__new(const char *name)
133 {
134 struct dso *self = malloc(sizeof(*self) + strlen(name) + 1);
135
136 if (self != NULL) {
137 strcpy(self->name, name);
138 dso__set_long_name(self, self->name);
139 self->short_name = self->name;
140 self->syms = RB_ROOT;
141 self->find_symbol = dso__find_symbol;
142 self->slen_calculated = 0;
143 self->origin = DSO__ORIG_NOT_FOUND;
144 self->loaded = 0;
145 self->has_build_id = 0;
146 }
147
148 return self;
149 }
150
151 static void dso__delete_symbols(struct dso *self)
152 {
153 struct symbol *pos;
154 struct rb_node *next = rb_first(&self->syms);
155
156 while (next) {
157 pos = rb_entry(next, struct symbol, rb_node);
158 next = rb_next(&pos->rb_node);
159 rb_erase(&pos->rb_node, &self->syms);
160 symbol__delete(pos);
161 }
162 }
163
164 void dso__delete(struct dso *self)
165 {
166 dso__delete_symbols(self);
167 if (self->long_name != self->name)
168 free(self->long_name);
169 free(self);
170 }
171
172 void dso__set_build_id(struct dso *self, void *build_id)
173 {
174 memcpy(self->build_id, build_id, sizeof(self->build_id));
175 self->has_build_id = 1;
176 }
177
178 static void dso__insert_symbol(struct dso *self, struct symbol *sym)
179 {
180 struct rb_node **p = &self->syms.rb_node;
181 struct rb_node *parent = NULL;
182 const u64 ip = sym->start;
183 struct symbol *s;
184
185 while (*p != NULL) {
186 parent = *p;
187 s = rb_entry(parent, struct symbol, rb_node);
188 if (ip < s->start)
189 p = &(*p)->rb_left;
190 else
191 p = &(*p)->rb_right;
192 }
193 rb_link_node(&sym->rb_node, parent, p);
194 rb_insert_color(&sym->rb_node, &self->syms);
195 }
196
197 struct symbol *dso__find_symbol(struct dso *self, u64 ip)
198 {
199 struct rb_node *n;
200
201 if (self == NULL)
202 return NULL;
203
204 n = self->syms.rb_node;
205
206 while (n) {
207 struct symbol *s = rb_entry(n, struct symbol, rb_node);
208
209 if (ip < s->start)
210 n = n->rb_left;
211 else if (ip > s->end)
212 n = n->rb_right;
213 else
214 return s;
215 }
216
217 return NULL;
218 }
219
220 int build_id__sprintf(u8 *self, int len, char *bf)
221 {
222 char *bid = bf;
223 u8 *raw = self;
224 int i;
225
226 for (i = 0; i < len; ++i) {
227 sprintf(bid, "%02x", *raw);
228 ++raw;
229 bid += 2;
230 }
231
232 return raw - self;
233 }
234
235 size_t dso__fprintf_buildid(struct dso *self, FILE *fp)
236 {
237 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
238
239 build_id__sprintf(self->build_id, sizeof(self->build_id), sbuild_id);
240 return fprintf(fp, "%s", sbuild_id);
241 }
242
243 size_t dso__fprintf(struct dso *self, FILE *fp)
244 {
245 struct rb_node *nd;
246 size_t ret = fprintf(fp, "dso: %s (", self->short_name);
247
248 ret += dso__fprintf_buildid(self, fp);
249 ret += fprintf(fp, ")\n");
250
251 for (nd = rb_first(&self->syms); nd; nd = rb_next(nd)) {
252 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
253 ret += symbol__fprintf(pos, fp);
254 }
255
256 return ret;
257 }
258
259 /*
260 * Loads the function entries in /proc/kallsyms into kernel_map->dso,
261 * so that we can in the next step set the symbol ->end address and then
262 * call kernel_maps__split_kallsyms.
263 */
264 static int kernel_maps__load_all_kallsyms(void)
265 {
266 char *line = NULL;
267 size_t n;
268 FILE *file = fopen("/proc/kallsyms", "r");
269
270 if (file == NULL)
271 goto out_failure;
272
273 while (!feof(file)) {
274 u64 start;
275 struct symbol *sym;
276 int line_len, len;
277 char symbol_type;
278 char *symbol_name;
279
280 line_len = getline(&line, &n, file);
281 if (line_len < 0)
282 break;
283
284 if (!line)
285 goto out_failure;
286
287 line[--line_len] = '\0'; /* \n */
288
289 len = hex2u64(line, &start);
290
291 len++;
292 if (len + 2 >= line_len)
293 continue;
294
295 symbol_type = toupper(line[len]);
296 /*
297 * We're interested only in code ('T'ext)
298 */
299 if (symbol_type != 'T' && symbol_type != 'W')
300 continue;
301
302 symbol_name = line + len + 2;
303 /*
304 * Will fix up the end later, when we have all symbols sorted.
305 */
306 sym = symbol__new(start, 0, symbol_name);
307
308 if (sym == NULL)
309 goto out_delete_line;
310
311 /*
312 * We will pass the symbols to the filter later, in
313 * kernel_maps__split_kallsyms, when we have split the
314 * maps per module
315 */
316 dso__insert_symbol(kernel_map->dso, sym);
317 }
318
319 free(line);
320 fclose(file);
321
322 return 0;
323
324 out_delete_line:
325 free(line);
326 out_failure:
327 return -1;
328 }
329
330 /*
331 * Split the symbols into maps, making sure there are no overlaps, i.e. the
332 * kernel range is broken in several maps, named [kernel].N, as we don't have
333 * the original ELF section names vmlinux have.
334 */
335 static int kernel_maps__split_kallsyms(symbol_filter_t filter)
336 {
337 struct map *map = kernel_map;
338 struct symbol *pos;
339 int count = 0;
340 struct rb_node *next = rb_first(&kernel_map->dso->syms);
341 int kernel_range = 0;
342
343 while (next) {
344 char *module;
345
346 pos = rb_entry(next, struct symbol, rb_node);
347 next = rb_next(&pos->rb_node);
348
349 module = strchr(pos->name, '\t');
350 if (module) {
351 *module++ = '\0';
352
353 if (strcmp(map->dso->name, module)) {
354 map = kernel_maps__find_by_dso_name(module);
355 if (!map) {
356 pr_err("/proc/{kallsyms,modules} "
357 "inconsistency!\n");
358 return -1;
359 }
360 }
361 /*
362 * So that we look just like we get from .ko files,
363 * i.e. not prelinked, relative to map->start.
364 */
365 pos->start = map->map_ip(map, pos->start);
366 pos->end = map->map_ip(map, pos->end);
367 } else if (map != kernel_map) {
368 char dso_name[PATH_MAX];
369 struct dso *dso;
370
371 snprintf(dso_name, sizeof(dso_name), "[kernel].%d",
372 kernel_range++);
373
374 dso = dso__new(dso_name);
375 if (dso == NULL)
376 return -1;
377
378 map = map__new2(pos->start, dso);
379 if (map == NULL) {
380 dso__delete(dso);
381 return -1;
382 }
383
384 map->map_ip = map->unmap_ip = identity__map_ip;
385 kernel_maps__insert(map);
386 ++kernel_range;
387 }
388
389 if (filter && filter(map, pos)) {
390 rb_erase(&pos->rb_node, &kernel_map->dso->syms);
391 symbol__delete(pos);
392 } else {
393 if (map != kernel_map) {
394 rb_erase(&pos->rb_node, &kernel_map->dso->syms);
395 dso__insert_symbol(map->dso, pos);
396 }
397 count++;
398 }
399 }
400
401 return count;
402 }
403
404
405 static int kernel_maps__load_kallsyms(symbol_filter_t filter)
406 {
407 if (kernel_maps__load_all_kallsyms())
408 return -1;
409
410 dso__fixup_sym_end(kernel_map->dso);
411 kernel_map->dso->origin = DSO__ORIG_KERNEL;
412
413 return kernel_maps__split_kallsyms(filter);
414 }
415
416 size_t kernel_maps__fprintf(FILE *fp)
417 {
418 size_t printed = fprintf(fp, "Kernel maps:\n");
419 struct rb_node *nd;
420
421 for (nd = rb_first(&kernel_maps); nd; nd = rb_next(nd)) {
422 struct map *pos = rb_entry(nd, struct map, rb_node);
423
424 printed += fprintf(fp, "Map:");
425 printed += map__fprintf(pos, fp);
426 if (verbose > 1) {
427 printed += dso__fprintf(pos->dso, fp);
428 printed += fprintf(fp, "--\n");
429 }
430 }
431
432 return printed + fprintf(fp, "END kernel maps\n");
433 }
434
435 static int dso__load_perf_map(struct dso *self, struct map *map,
436 symbol_filter_t filter)
437 {
438 char *line = NULL;
439 size_t n;
440 FILE *file;
441 int nr_syms = 0;
442
443 file = fopen(self->long_name, "r");
444 if (file == NULL)
445 goto out_failure;
446
447 while (!feof(file)) {
448 u64 start, size;
449 struct symbol *sym;
450 int line_len, len;
451
452 line_len = getline(&line, &n, file);
453 if (line_len < 0)
454 break;
455
456 if (!line)
457 goto out_failure;
458
459 line[--line_len] = '\0'; /* \n */
460
461 len = hex2u64(line, &start);
462
463 len++;
464 if (len + 2 >= line_len)
465 continue;
466
467 len += hex2u64(line + len, &size);
468
469 len++;
470 if (len + 2 >= line_len)
471 continue;
472
473 sym = symbol__new(start, size, line + len);
474
475 if (sym == NULL)
476 goto out_delete_line;
477
478 if (filter && filter(map, sym))
479 symbol__delete(sym);
480 else {
481 dso__insert_symbol(self, sym);
482 nr_syms++;
483 }
484 }
485
486 free(line);
487 fclose(file);
488
489 return nr_syms;
490
491 out_delete_line:
492 free(line);
493 out_failure:
494 return -1;
495 }
496
497 /**
498 * elf_symtab__for_each_symbol - iterate thru all the symbols
499 *
500 * @self: struct elf_symtab instance to iterate
501 * @idx: uint32_t idx
502 * @sym: GElf_Sym iterator
503 */
504 #define elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) \
505 for (idx = 0, gelf_getsym(syms, idx, &sym);\
506 idx < nr_syms; \
507 idx++, gelf_getsym(syms, idx, &sym))
508
509 static inline uint8_t elf_sym__type(const GElf_Sym *sym)
510 {
511 return GELF_ST_TYPE(sym->st_info);
512 }
513
514 static inline int elf_sym__is_function(const GElf_Sym *sym)
515 {
516 return elf_sym__type(sym) == STT_FUNC &&
517 sym->st_name != 0 &&
518 sym->st_shndx != SHN_UNDEF;
519 }
520
521 static inline int elf_sym__is_label(const GElf_Sym *sym)
522 {
523 return elf_sym__type(sym) == STT_NOTYPE &&
524 sym->st_name != 0 &&
525 sym->st_shndx != SHN_UNDEF &&
526 sym->st_shndx != SHN_ABS;
527 }
528
529 static inline const char *elf_sec__name(const GElf_Shdr *shdr,
530 const Elf_Data *secstrs)
531 {
532 return secstrs->d_buf + shdr->sh_name;
533 }
534
535 static inline int elf_sec__is_text(const GElf_Shdr *shdr,
536 const Elf_Data *secstrs)
537 {
538 return strstr(elf_sec__name(shdr, secstrs), "text") != NULL;
539 }
540
541 static inline const char *elf_sym__name(const GElf_Sym *sym,
542 const Elf_Data *symstrs)
543 {
544 return symstrs->d_buf + sym->st_name;
545 }
546
547 static Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep,
548 GElf_Shdr *shp, const char *name,
549 size_t *idx)
550 {
551 Elf_Scn *sec = NULL;
552 size_t cnt = 1;
553
554 while ((sec = elf_nextscn(elf, sec)) != NULL) {
555 char *str;
556
557 gelf_getshdr(sec, shp);
558 str = elf_strptr(elf, ep->e_shstrndx, shp->sh_name);
559 if (!strcmp(name, str)) {
560 if (idx)
561 *idx = cnt;
562 break;
563 }
564 ++cnt;
565 }
566
567 return sec;
568 }
569
570 #define elf_section__for_each_rel(reldata, pos, pos_mem, idx, nr_entries) \
571 for (idx = 0, pos = gelf_getrel(reldata, 0, &pos_mem); \
572 idx < nr_entries; \
573 ++idx, pos = gelf_getrel(reldata, idx, &pos_mem))
574
575 #define elf_section__for_each_rela(reldata, pos, pos_mem, idx, nr_entries) \
576 for (idx = 0, pos = gelf_getrela(reldata, 0, &pos_mem); \
577 idx < nr_entries; \
578 ++idx, pos = gelf_getrela(reldata, idx, &pos_mem))
579
580 /*
581 * We need to check if we have a .dynsym, so that we can handle the
582 * .plt, synthesizing its symbols, that aren't on the symtabs (be it
583 * .dynsym or .symtab).
584 * And always look at the original dso, not at debuginfo packages, that
585 * have the PLT data stripped out (shdr_rel_plt.sh_type == SHT_NOBITS).
586 */
587 static int dso__synthesize_plt_symbols(struct dso *self, struct map *map,
588 symbol_filter_t filter)
589 {
590 uint32_t nr_rel_entries, idx;
591 GElf_Sym sym;
592 u64 plt_offset;
593 GElf_Shdr shdr_plt;
594 struct symbol *f;
595 GElf_Shdr shdr_rel_plt, shdr_dynsym;
596 Elf_Data *reldata, *syms, *symstrs;
597 Elf_Scn *scn_plt_rel, *scn_symstrs, *scn_dynsym;
598 size_t dynsym_idx;
599 GElf_Ehdr ehdr;
600 char sympltname[1024];
601 Elf *elf;
602 int nr = 0, symidx, fd, err = 0;
603
604 fd = open(self->long_name, O_RDONLY);
605 if (fd < 0)
606 goto out;
607
608 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
609 if (elf == NULL)
610 goto out_close;
611
612 if (gelf_getehdr(elf, &ehdr) == NULL)
613 goto out_elf_end;
614
615 scn_dynsym = elf_section_by_name(elf, &ehdr, &shdr_dynsym,
616 ".dynsym", &dynsym_idx);
617 if (scn_dynsym == NULL)
618 goto out_elf_end;
619
620 scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt,
621 ".rela.plt", NULL);
622 if (scn_plt_rel == NULL) {
623 scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt,
624 ".rel.plt", NULL);
625 if (scn_plt_rel == NULL)
626 goto out_elf_end;
627 }
628
629 err = -1;
630
631 if (shdr_rel_plt.sh_link != dynsym_idx)
632 goto out_elf_end;
633
634 if (elf_section_by_name(elf, &ehdr, &shdr_plt, ".plt", NULL) == NULL)
635 goto out_elf_end;
636
637 /*
638 * Fetch the relocation section to find the idxes to the GOT
639 * and the symbols in the .dynsym they refer to.
640 */
641 reldata = elf_getdata(scn_plt_rel, NULL);
642 if (reldata == NULL)
643 goto out_elf_end;
644
645 syms = elf_getdata(scn_dynsym, NULL);
646 if (syms == NULL)
647 goto out_elf_end;
648
649 scn_symstrs = elf_getscn(elf, shdr_dynsym.sh_link);
650 if (scn_symstrs == NULL)
651 goto out_elf_end;
652
653 symstrs = elf_getdata(scn_symstrs, NULL);
654 if (symstrs == NULL)
655 goto out_elf_end;
656
657 nr_rel_entries = shdr_rel_plt.sh_size / shdr_rel_plt.sh_entsize;
658 plt_offset = shdr_plt.sh_offset;
659
660 if (shdr_rel_plt.sh_type == SHT_RELA) {
661 GElf_Rela pos_mem, *pos;
662
663 elf_section__for_each_rela(reldata, pos, pos_mem, idx,
664 nr_rel_entries) {
665 symidx = GELF_R_SYM(pos->r_info);
666 plt_offset += shdr_plt.sh_entsize;
667 gelf_getsym(syms, symidx, &sym);
668 snprintf(sympltname, sizeof(sympltname),
669 "%s@plt", elf_sym__name(&sym, symstrs));
670
671 f = symbol__new(plt_offset, shdr_plt.sh_entsize,
672 sympltname);
673 if (!f)
674 goto out_elf_end;
675
676 if (filter && filter(map, f))
677 symbol__delete(f);
678 else {
679 dso__insert_symbol(self, f);
680 ++nr;
681 }
682 }
683 } else if (shdr_rel_plt.sh_type == SHT_REL) {
684 GElf_Rel pos_mem, *pos;
685 elf_section__for_each_rel(reldata, pos, pos_mem, idx,
686 nr_rel_entries) {
687 symidx = GELF_R_SYM(pos->r_info);
688 plt_offset += shdr_plt.sh_entsize;
689 gelf_getsym(syms, symidx, &sym);
690 snprintf(sympltname, sizeof(sympltname),
691 "%s@plt", elf_sym__name(&sym, symstrs));
692
693 f = symbol__new(plt_offset, shdr_plt.sh_entsize,
694 sympltname);
695 if (!f)
696 goto out_elf_end;
697
698 if (filter && filter(map, f))
699 symbol__delete(f);
700 else {
701 dso__insert_symbol(self, f);
702 ++nr;
703 }
704 }
705 }
706
707 err = 0;
708 out_elf_end:
709 elf_end(elf);
710 out_close:
711 close(fd);
712
713 if (err == 0)
714 return nr;
715 out:
716 pr_warning("%s: problems reading %s PLT info.\n",
717 __func__, self->long_name);
718 return 0;
719 }
720
721 static int dso__load_sym(struct dso *self, struct map *map, const char *name,
722 int fd, symbol_filter_t filter, int kernel,
723 int kmodule)
724 {
725 struct map *curr_map = map;
726 struct dso *curr_dso = self;
727 size_t dso_name_len = strlen(self->short_name);
728 Elf_Data *symstrs, *secstrs;
729 uint32_t nr_syms;
730 int err = -1;
731 uint32_t idx;
732 GElf_Ehdr ehdr;
733 GElf_Shdr shdr;
734 Elf_Data *syms;
735 GElf_Sym sym;
736 Elf_Scn *sec, *sec_strndx;
737 Elf *elf;
738 int nr = 0;
739
740 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
741 if (elf == NULL) {
742 pr_err("%s: cannot read %s ELF file.\n", __func__, name);
743 goto out_close;
744 }
745
746 if (gelf_getehdr(elf, &ehdr) == NULL) {
747 pr_err("%s: cannot get elf header.\n", __func__);
748 goto out_elf_end;
749 }
750
751 sec = elf_section_by_name(elf, &ehdr, &shdr, ".symtab", NULL);
752 if (sec == NULL) {
753 sec = elf_section_by_name(elf, &ehdr, &shdr, ".dynsym", NULL);
754 if (sec == NULL)
755 goto out_elf_end;
756 }
757
758 syms = elf_getdata(sec, NULL);
759 if (syms == NULL)
760 goto out_elf_end;
761
762 sec = elf_getscn(elf, shdr.sh_link);
763 if (sec == NULL)
764 goto out_elf_end;
765
766 symstrs = elf_getdata(sec, NULL);
767 if (symstrs == NULL)
768 goto out_elf_end;
769
770 sec_strndx = elf_getscn(elf, ehdr.e_shstrndx);
771 if (sec_strndx == NULL)
772 goto out_elf_end;
773
774 secstrs = elf_getdata(sec_strndx, NULL);
775 if (secstrs == NULL)
776 goto out_elf_end;
777
778 nr_syms = shdr.sh_size / shdr.sh_entsize;
779
780 memset(&sym, 0, sizeof(sym));
781 if (!kernel) {
782 self->adjust_symbols = (ehdr.e_type == ET_EXEC ||
783 elf_section_by_name(elf, &ehdr, &shdr,
784 ".gnu.prelink_undo",
785 NULL) != NULL);
786 } else self->adjust_symbols = 0;
787
788 elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) {
789 struct symbol *f;
790 const char *elf_name;
791 char *demangled = NULL;
792 int is_label = elf_sym__is_label(&sym);
793 const char *section_name;
794
795 if (!is_label && !elf_sym__is_function(&sym))
796 continue;
797
798 sec = elf_getscn(elf, sym.st_shndx);
799 if (!sec)
800 goto out_elf_end;
801
802 gelf_getshdr(sec, &shdr);
803
804 if (is_label && !elf_sec__is_text(&shdr, secstrs))
805 continue;
806
807 elf_name = elf_sym__name(&sym, symstrs);
808 section_name = elf_sec__name(&shdr, secstrs);
809
810 if (kernel || kmodule) {
811 char dso_name[PATH_MAX];
812
813 if (strcmp(section_name,
814 curr_dso->short_name + dso_name_len) == 0)
815 goto new_symbol;
816
817 if (strcmp(section_name, ".text") == 0) {
818 curr_map = map;
819 curr_dso = self;
820 goto new_symbol;
821 }
822
823 snprintf(dso_name, sizeof(dso_name),
824 "%s%s", self->short_name, section_name);
825
826 curr_map = kernel_maps__find_by_dso_name(dso_name);
827 if (curr_map == NULL) {
828 u64 start = sym.st_value;
829
830 if (kmodule)
831 start += map->start + shdr.sh_offset;
832
833 curr_dso = dso__new(dso_name);
834 if (curr_dso == NULL)
835 goto out_elf_end;
836 curr_map = map__new2(start, curr_dso);
837 if (curr_map == NULL) {
838 dso__delete(curr_dso);
839 goto out_elf_end;
840 }
841 curr_map->map_ip = identity__map_ip;
842 curr_map->unmap_ip = identity__map_ip;
843 curr_dso->origin = DSO__ORIG_KERNEL;
844 kernel_maps__insert(curr_map);
845 dsos__add(curr_dso);
846 } else
847 curr_dso = curr_map->dso;
848
849 goto new_symbol;
850 }
851
852 if (curr_dso->adjust_symbols) {
853 pr_debug2("adjusting symbol: st_value: %Lx sh_addr: "
854 "%Lx sh_offset: %Lx\n", (u64)sym.st_value,
855 (u64)shdr.sh_addr, (u64)shdr.sh_offset);
856 sym.st_value -= shdr.sh_addr - shdr.sh_offset;
857 }
858 /*
859 * We need to figure out if the object was created from C++ sources
860 * DWARF DW_compile_unit has this, but we don't always have access
861 * to it...
862 */
863 demangled = bfd_demangle(NULL, elf_name, DMGL_PARAMS | DMGL_ANSI);
864 if (demangled != NULL)
865 elf_name = demangled;
866 new_symbol:
867 f = symbol__new(sym.st_value, sym.st_size, elf_name);
868 free(demangled);
869 if (!f)
870 goto out_elf_end;
871
872 if (filter && filter(curr_map, f))
873 symbol__delete(f);
874 else {
875 dso__insert_symbol(curr_dso, f);
876 nr++;
877 }
878 }
879
880 /*
881 * For misannotated, zeroed, ASM function sizes.
882 */
883 if (nr > 0)
884 dso__fixup_sym_end(self);
885 err = nr;
886 out_elf_end:
887 elf_end(elf);
888 out_close:
889 return err;
890 }
891
892 static bool dso__build_id_equal(const struct dso *self, u8 *build_id)
893 {
894 return memcmp(self->build_id, build_id, sizeof(self->build_id)) == 0;
895 }
896
897 bool dsos__read_build_ids(void)
898 {
899 bool have_build_id = false;
900 struct dso *pos;
901
902 list_for_each_entry(pos, &dsos, node)
903 if (filename__read_build_id(pos->long_name, pos->build_id,
904 sizeof(pos->build_id)) > 0) {
905 have_build_id = true;
906 pos->has_build_id = true;
907 }
908
909 return have_build_id;
910 }
911
912 /*
913 * Align offset to 4 bytes as needed for note name and descriptor data.
914 */
915 #define NOTE_ALIGN(n) (((n) + 3) & -4U)
916
917 int filename__read_build_id(const char *filename, void *bf, size_t size)
918 {
919 int fd, err = -1;
920 GElf_Ehdr ehdr;
921 GElf_Shdr shdr;
922 Elf_Data *data;
923 Elf_Scn *sec;
924 void *ptr;
925 Elf *elf;
926
927 if (size < BUILD_ID_SIZE)
928 goto out;
929
930 fd = open(filename, O_RDONLY);
931 if (fd < 0)
932 goto out;
933
934 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
935 if (elf == NULL) {
936 pr_debug2("%s: cannot read %s ELF file.\n", __func__, filename);
937 goto out_close;
938 }
939
940 if (gelf_getehdr(elf, &ehdr) == NULL) {
941 pr_err("%s: cannot get elf header.\n", __func__);
942 goto out_elf_end;
943 }
944
945 sec = elf_section_by_name(elf, &ehdr, &shdr,
946 ".note.gnu.build-id", NULL);
947 if (sec == NULL) {
948 sec = elf_section_by_name(elf, &ehdr, &shdr,
949 ".notes", NULL);
950 if (sec == NULL)
951 goto out_elf_end;
952 }
953
954 data = elf_getdata(sec, NULL);
955 if (data == NULL)
956 goto out_elf_end;
957
958 ptr = data->d_buf;
959 while (ptr < (data->d_buf + data->d_size)) {
960 GElf_Nhdr *nhdr = ptr;
961 int namesz = NOTE_ALIGN(nhdr->n_namesz),
962 descsz = NOTE_ALIGN(nhdr->n_descsz);
963 const char *name;
964
965 ptr += sizeof(*nhdr);
966 name = ptr;
967 ptr += namesz;
968 if (nhdr->n_type == NT_GNU_BUILD_ID &&
969 nhdr->n_namesz == sizeof("GNU")) {
970 if (memcmp(name, "GNU", sizeof("GNU")) == 0) {
971 memcpy(bf, ptr, BUILD_ID_SIZE);
972 err = BUILD_ID_SIZE;
973 break;
974 }
975 }
976 ptr += descsz;
977 }
978 out_elf_end:
979 elf_end(elf);
980 out_close:
981 close(fd);
982 out:
983 return err;
984 }
985
986 int sysfs__read_build_id(const char *filename, void *build_id, size_t size)
987 {
988 int fd, err = -1;
989
990 if (size < BUILD_ID_SIZE)
991 goto out;
992
993 fd = open(filename, O_RDONLY);
994 if (fd < 0)
995 goto out;
996
997 while (1) {
998 char bf[BUFSIZ];
999 GElf_Nhdr nhdr;
1000 int namesz, descsz;
1001
1002 if (read(fd, &nhdr, sizeof(nhdr)) != sizeof(nhdr))
1003 break;
1004
1005 namesz = NOTE_ALIGN(nhdr.n_namesz);
1006 descsz = NOTE_ALIGN(nhdr.n_descsz);
1007 if (nhdr.n_type == NT_GNU_BUILD_ID &&
1008 nhdr.n_namesz == sizeof("GNU")) {
1009 if (read(fd, bf, namesz) != namesz)
1010 break;
1011 if (memcmp(bf, "GNU", sizeof("GNU")) == 0) {
1012 if (read(fd, build_id,
1013 BUILD_ID_SIZE) == BUILD_ID_SIZE) {
1014 err = 0;
1015 break;
1016 }
1017 } else if (read(fd, bf, descsz) != descsz)
1018 break;
1019 } else {
1020 int n = namesz + descsz;
1021 if (read(fd, bf, n) != n)
1022 break;
1023 }
1024 }
1025 close(fd);
1026 out:
1027 return err;
1028 }
1029
1030 char dso__symtab_origin(const struct dso *self)
1031 {
1032 static const char origin[] = {
1033 [DSO__ORIG_KERNEL] = 'k',
1034 [DSO__ORIG_JAVA_JIT] = 'j',
1035 [DSO__ORIG_FEDORA] = 'f',
1036 [DSO__ORIG_UBUNTU] = 'u',
1037 [DSO__ORIG_BUILDID] = 'b',
1038 [DSO__ORIG_DSO] = 'd',
1039 [DSO__ORIG_KMODULE] = 'K',
1040 };
1041
1042 if (self == NULL || self->origin == DSO__ORIG_NOT_FOUND)
1043 return '!';
1044 return origin[self->origin];
1045 }
1046
1047 int dso__load(struct dso *self, struct map *map, symbol_filter_t filter)
1048 {
1049 int size = PATH_MAX;
1050 char *name;
1051 u8 build_id[BUILD_ID_SIZE];
1052 int ret = -1;
1053 int fd;
1054
1055 self->loaded = 1;
1056
1057 if (self->kernel)
1058 return dso__load_kernel_sym(self, map, filter);
1059
1060 name = malloc(size);
1061 if (!name)
1062 return -1;
1063
1064 self->adjust_symbols = 0;
1065
1066 if (strncmp(self->name, "/tmp/perf-", 10) == 0) {
1067 ret = dso__load_perf_map(self, map, filter);
1068 self->origin = ret > 0 ? DSO__ORIG_JAVA_JIT :
1069 DSO__ORIG_NOT_FOUND;
1070 return ret;
1071 }
1072
1073 self->origin = DSO__ORIG_FEDORA - 1;
1074
1075 more:
1076 do {
1077 self->origin++;
1078 switch (self->origin) {
1079 case DSO__ORIG_FEDORA:
1080 snprintf(name, size, "/usr/lib/debug%s.debug",
1081 self->long_name);
1082 break;
1083 case DSO__ORIG_UBUNTU:
1084 snprintf(name, size, "/usr/lib/debug%s",
1085 self->long_name);
1086 break;
1087 case DSO__ORIG_BUILDID:
1088 if (filename__read_build_id(self->long_name, build_id,
1089 sizeof(build_id))) {
1090 char build_id_hex[BUILD_ID_SIZE * 2 + 1];
1091
1092 build_id__sprintf(build_id, sizeof(build_id),
1093 build_id_hex);
1094 snprintf(name, size,
1095 "/usr/lib/debug/.build-id/%.2s/%s.debug",
1096 build_id_hex, build_id_hex + 2);
1097 if (self->has_build_id)
1098 goto compare_build_id;
1099 break;
1100 }
1101 self->origin++;
1102 /* Fall thru */
1103 case DSO__ORIG_DSO:
1104 snprintf(name, size, "%s", self->long_name);
1105 break;
1106
1107 default:
1108 goto out;
1109 }
1110
1111 if (self->has_build_id) {
1112 if (filename__read_build_id(name, build_id,
1113 sizeof(build_id)) < 0)
1114 goto more;
1115 compare_build_id:
1116 if (!dso__build_id_equal(self, build_id))
1117 goto more;
1118 }
1119
1120 fd = open(name, O_RDONLY);
1121 } while (fd < 0);
1122
1123 ret = dso__load_sym(self, map, name, fd, filter, 0, 0);
1124 close(fd);
1125
1126 /*
1127 * Some people seem to have debuginfo files _WITHOUT_ debug info!?!?
1128 */
1129 if (!ret)
1130 goto more;
1131
1132 if (ret > 0) {
1133 int nr_plt = dso__synthesize_plt_symbols(self, map, filter);
1134 if (nr_plt > 0)
1135 ret += nr_plt;
1136 }
1137 out:
1138 free(name);
1139 if (ret < 0 && strstr(self->name, " (deleted)") != NULL)
1140 return 0;
1141 return ret;
1142 }
1143
1144 struct map *kernel_map;
1145
1146 static void kernel_maps__insert(struct map *map)
1147 {
1148 maps__insert(&kernel_maps, map);
1149 }
1150
1151 struct symbol *kernel_maps__find_symbol(u64 ip, struct map **mapp,
1152 symbol_filter_t filter)
1153 {
1154 struct map *map = maps__find(&kernel_maps, ip);
1155
1156 if (mapp)
1157 *mapp = map;
1158
1159 if (map) {
1160 ip = map->map_ip(map, ip);
1161 return map__find_symbol(map, ip, filter);
1162 }
1163
1164 return NULL;
1165 }
1166
1167 struct map *kernel_maps__find_by_dso_name(const char *name)
1168 {
1169 struct rb_node *nd;
1170
1171 for (nd = rb_first(&kernel_maps); nd; nd = rb_next(nd)) {
1172 struct map *map = rb_entry(nd, struct map, rb_node);
1173
1174 if (map->dso && strcmp(map->dso->name, name) == 0)
1175 return map;
1176 }
1177
1178 return NULL;
1179 }
1180
1181 static int dsos__set_modules_path_dir(char *dirname)
1182 {
1183 struct dirent *dent;
1184 DIR *dir = opendir(dirname);
1185
1186 if (!dir) {
1187 pr_err("%s: cannot open %s dir\n", __func__, dirname);
1188 return -1;
1189 }
1190
1191 while ((dent = readdir(dir)) != NULL) {
1192 char path[PATH_MAX];
1193
1194 if (dent->d_type == DT_DIR) {
1195 if (!strcmp(dent->d_name, ".") ||
1196 !strcmp(dent->d_name, ".."))
1197 continue;
1198
1199 snprintf(path, sizeof(path), "%s/%s",
1200 dirname, dent->d_name);
1201 if (dsos__set_modules_path_dir(path) < 0)
1202 goto failure;
1203 } else {
1204 char *dot = strrchr(dent->d_name, '.'),
1205 dso_name[PATH_MAX];
1206 struct map *map;
1207 char *long_name;
1208
1209 if (dot == NULL || strcmp(dot, ".ko"))
1210 continue;
1211 snprintf(dso_name, sizeof(dso_name), "[%.*s]",
1212 (int)(dot - dent->d_name), dent->d_name);
1213
1214 strxfrchar(dso_name, '-', '_');
1215 map = kernel_maps__find_by_dso_name(dso_name);
1216 if (map == NULL)
1217 continue;
1218
1219 snprintf(path, sizeof(path), "%s/%s",
1220 dirname, dent->d_name);
1221
1222 long_name = strdup(path);
1223 if (long_name == NULL)
1224 goto failure;
1225 dso__set_long_name(map->dso, long_name);
1226 }
1227 }
1228
1229 return 0;
1230 failure:
1231 closedir(dir);
1232 return -1;
1233 }
1234
1235 static int dsos__set_modules_path(void)
1236 {
1237 struct utsname uts;
1238 char modules_path[PATH_MAX];
1239
1240 if (uname(&uts) < 0)
1241 return -1;
1242
1243 snprintf(modules_path, sizeof(modules_path), "/lib/modules/%s/kernel",
1244 uts.release);
1245
1246 return dsos__set_modules_path_dir(modules_path);
1247 }
1248
1249 /*
1250 * Constructor variant for modules (where we know from /proc/modules where
1251 * they are loaded) and for vmlinux, where only after we load all the
1252 * symbols we'll know where it starts and ends.
1253 */
1254 static struct map *map__new2(u64 start, struct dso *dso)
1255 {
1256 struct map *self = malloc(sizeof(*self));
1257
1258 if (self != NULL) {
1259 /*
1260 * ->end will be filled after we load all the symbols
1261 */
1262 map__init(self, start, 0, 0, dso);
1263 }
1264
1265 return self;
1266 }
1267
1268 static int kernel_maps__create_module_maps(void)
1269 {
1270 char *line = NULL;
1271 size_t n;
1272 FILE *file = fopen("/proc/modules", "r");
1273 struct map *map;
1274
1275 if (file == NULL)
1276 return -1;
1277
1278 while (!feof(file)) {
1279 char name[PATH_MAX];
1280 u64 start;
1281 struct dso *dso;
1282 char *sep;
1283 int line_len;
1284
1285 line_len = getline(&line, &n, file);
1286 if (line_len < 0)
1287 break;
1288
1289 if (!line)
1290 goto out_failure;
1291
1292 line[--line_len] = '\0'; /* \n */
1293
1294 sep = strrchr(line, 'x');
1295 if (sep == NULL)
1296 continue;
1297
1298 hex2u64(sep + 1, &start);
1299
1300 sep = strchr(line, ' ');
1301 if (sep == NULL)
1302 continue;
1303
1304 *sep = '\0';
1305
1306 snprintf(name, sizeof(name), "[%s]", line);
1307 dso = dso__new(name);
1308
1309 if (dso == NULL)
1310 goto out_delete_line;
1311
1312 map = map__new2(start, dso);
1313 if (map == NULL) {
1314 dso__delete(dso);
1315 goto out_delete_line;
1316 }
1317
1318 snprintf(name, sizeof(name),
1319 "/sys/module/%s/notes/.note.gnu.build-id", line);
1320 if (sysfs__read_build_id(name, dso->build_id,
1321 sizeof(dso->build_id)) == 0)
1322 dso->has_build_id = true;
1323
1324 dso->origin = DSO__ORIG_KMODULE;
1325 kernel_maps__insert(map);
1326 dsos__add(dso);
1327 }
1328
1329 free(line);
1330 fclose(file);
1331
1332 return dsos__set_modules_path();
1333
1334 out_delete_line:
1335 free(line);
1336 out_failure:
1337 return -1;
1338 }
1339
1340 static int dso__load_vmlinux(struct dso *self, struct map *map,
1341 const char *vmlinux, symbol_filter_t filter)
1342 {
1343 int err = -1, fd;
1344
1345 if (self->has_build_id) {
1346 u8 build_id[BUILD_ID_SIZE];
1347
1348 if (filename__read_build_id(vmlinux, build_id,
1349 sizeof(build_id)) < 0) {
1350 pr_debug("No build_id in %s, ignoring it\n", vmlinux);
1351 return -1;
1352 }
1353 if (!dso__build_id_equal(self, build_id)) {
1354 char expected_build_id[BUILD_ID_SIZE * 2 + 1],
1355 vmlinux_build_id[BUILD_ID_SIZE * 2 + 1];
1356
1357 build_id__sprintf(self->build_id,
1358 sizeof(self->build_id),
1359 expected_build_id);
1360 build_id__sprintf(build_id, sizeof(build_id),
1361 vmlinux_build_id);
1362 pr_debug("build_id in %s is %s while expected is %s, "
1363 "ignoring it\n", vmlinux, vmlinux_build_id,
1364 expected_build_id);
1365 return -1;
1366 }
1367 }
1368
1369 fd = open(vmlinux, O_RDONLY);
1370 if (fd < 0)
1371 return -1;
1372
1373 self->loaded = 1;
1374 err = dso__load_sym(self, map, self->long_name, fd, filter, 1, 0);
1375
1376 close(fd);
1377
1378 return err;
1379 }
1380
1381 static int dso__load_kernel_sym(struct dso *self, struct map *map,
1382 symbol_filter_t filter)
1383 {
1384 int err = dso__load_vmlinux(self, map, self->name, filter);
1385
1386 if (err <= 0) {
1387 err = kernel_maps__load_kallsyms(filter);
1388 if (err > 0)
1389 dso__set_long_name(self, strdup("[kernel.kallsyms]"));
1390 }
1391
1392 if (err > 0) {
1393 map__fixup_start(map);
1394 map__fixup_end(map);
1395 }
1396
1397 return err;
1398 }
1399
1400 LIST_HEAD(dsos);
1401 struct dso *vdso;
1402
1403 const char *vmlinux_name = "vmlinux";
1404
1405 static void dsos__add(struct dso *dso)
1406 {
1407 list_add_tail(&dso->node, &dsos);
1408 }
1409
1410 static struct dso *dsos__find(const char *name)
1411 {
1412 struct dso *pos;
1413
1414 list_for_each_entry(pos, &dsos, node)
1415 if (strcmp(pos->name, name) == 0)
1416 return pos;
1417 return NULL;
1418 }
1419
1420 struct dso *dsos__findnew(const char *name)
1421 {
1422 struct dso *dso = dsos__find(name);
1423
1424 if (!dso) {
1425 dso = dso__new(name);
1426 if (dso != NULL) {
1427 dsos__add(dso);
1428 dso__set_basename(dso);
1429 }
1430 }
1431
1432 return dso;
1433 }
1434
1435 void dsos__fprintf(FILE *fp)
1436 {
1437 struct dso *pos;
1438
1439 list_for_each_entry(pos, &dsos, node)
1440 dso__fprintf(pos, fp);
1441 }
1442
1443 size_t dsos__fprintf_buildid(FILE *fp)
1444 {
1445 struct dso *pos;
1446 size_t ret = 0;
1447
1448 list_for_each_entry(pos, &dsos, node) {
1449 ret += dso__fprintf_buildid(pos, fp);
1450 ret += fprintf(fp, " %s\n", pos->long_name);
1451 }
1452 return ret;
1453 }
1454
1455 static int kernel_maps__create_kernel_map(void)
1456 {
1457 struct dso *kernel = dso__new(vmlinux_name);
1458
1459 if (kernel == NULL)
1460 return -1;
1461
1462 kernel_map = map__new2(0, kernel);
1463 if (kernel_map == NULL)
1464 goto out_delete_kernel_dso;
1465
1466 kernel_map->map_ip = kernel_map->unmap_ip = identity__map_ip;
1467
1468 kernel->short_name = "[kernel]";
1469 kernel->kernel = 1;
1470 vdso = dso__new("[vdso]");
1471 if (vdso == NULL)
1472 goto out_delete_kernel_map;
1473
1474 if (sysfs__read_build_id("/sys/kernel/notes", kernel->build_id,
1475 sizeof(kernel->build_id)) == 0)
1476 kernel->has_build_id = true;
1477
1478 kernel_maps__insert(kernel_map);
1479 dsos__add(kernel);
1480 dsos__add(vdso);
1481
1482 return 0;
1483
1484 out_delete_kernel_map:
1485 map__delete(kernel_map);
1486 kernel_map = NULL;
1487 out_delete_kernel_dso:
1488 dso__delete(kernel);
1489 return -1;
1490 }
1491
1492 int kernel_maps__init(bool use_modules)
1493 {
1494 if (kernel_maps__create_kernel_map() < 0)
1495 return -1;
1496
1497 if (use_modules && kernel_maps__create_module_maps() < 0)
1498 pr_warning("Failed to load list of modules in use, "
1499 "continuing...\n");
1500 /*
1501 * Now that we have all the maps created, just set the ->end of them:
1502 */
1503 kernel_maps__fixup_end();
1504 return 0;
1505 }
1506
1507 void symbol__init(unsigned int priv_size)
1508 {
1509 elf_version(EV_CURRENT);
1510 symbol__priv_size = priv_size;
1511 }
This page took 0.113608 seconds and 5 git commands to generate.