perf symbols: Fix ELF header errors during "perf kmem record"
[safe/jmp/linux-2.6] / 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         Elf_Kind ek;
925         void *ptr;
926         Elf *elf;
927
928         if (size < BUILD_ID_SIZE)
929                 goto out;
930
931         fd = open(filename, O_RDONLY);
932         if (fd < 0)
933                 goto out;
934
935         elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
936         if (elf == NULL) {
937                 pr_debug2("%s: cannot read %s ELF file.\n", __func__, filename);
938                 goto out_close;
939         }
940
941         ek = elf_kind(elf);
942         if (ek != ELF_K_ELF)
943                 goto out_elf_end;
944
945         if (gelf_getehdr(elf, &ehdr) == NULL) {
946                 pr_err("%s: cannot get elf header.\n", __func__);
947                 goto out_elf_end;
948         }
949
950         sec = elf_section_by_name(elf, &ehdr, &shdr,
951                                   ".note.gnu.build-id", NULL);
952         if (sec == NULL) {
953                 sec = elf_section_by_name(elf, &ehdr, &shdr,
954                                           ".notes", NULL);
955                 if (sec == NULL)
956                         goto out_elf_end;
957         }
958
959         data = elf_getdata(sec, NULL);
960         if (data == NULL)
961                 goto out_elf_end;
962
963         ptr = data->d_buf;
964         while (ptr < (data->d_buf + data->d_size)) {
965                 GElf_Nhdr *nhdr = ptr;
966                 int namesz = NOTE_ALIGN(nhdr->n_namesz),
967                     descsz = NOTE_ALIGN(nhdr->n_descsz);
968                 const char *name;
969
970                 ptr += sizeof(*nhdr);
971                 name = ptr;
972                 ptr += namesz;
973                 if (nhdr->n_type == NT_GNU_BUILD_ID &&
974                     nhdr->n_namesz == sizeof("GNU")) {
975                         if (memcmp(name, "GNU", sizeof("GNU")) == 0) {
976                                 memcpy(bf, ptr, BUILD_ID_SIZE);
977                                 err = BUILD_ID_SIZE;
978                                 break;
979                         }
980                 }
981                 ptr += descsz;
982         }
983 out_elf_end:
984         elf_end(elf);
985 out_close:
986         close(fd);
987 out:
988         return err;
989 }
990
991 int sysfs__read_build_id(const char *filename, void *build_id, size_t size)
992 {
993         int fd, err = -1;
994
995         if (size < BUILD_ID_SIZE)
996                 goto out;
997
998         fd = open(filename, O_RDONLY);
999         if (fd < 0)
1000                 goto out;
1001
1002         while (1) {
1003                 char bf[BUFSIZ];
1004                 GElf_Nhdr nhdr;
1005                 int namesz, descsz;
1006
1007                 if (read(fd, &nhdr, sizeof(nhdr)) != sizeof(nhdr))
1008                         break;
1009
1010                 namesz = NOTE_ALIGN(nhdr.n_namesz);
1011                 descsz = NOTE_ALIGN(nhdr.n_descsz);
1012                 if (nhdr.n_type == NT_GNU_BUILD_ID &&
1013                     nhdr.n_namesz == sizeof("GNU")) {
1014                         if (read(fd, bf, namesz) != namesz)
1015                                 break;
1016                         if (memcmp(bf, "GNU", sizeof("GNU")) == 0) {
1017                                 if (read(fd, build_id,
1018                                     BUILD_ID_SIZE) == BUILD_ID_SIZE) {
1019                                         err = 0;
1020                                         break;
1021                                 }
1022                         } else if (read(fd, bf, descsz) != descsz)
1023                                 break;
1024                 } else {
1025                         int n = namesz + descsz;
1026                         if (read(fd, bf, n) != n)
1027                                 break;
1028                 }
1029         }
1030         close(fd);
1031 out:
1032         return err;
1033 }
1034
1035 char dso__symtab_origin(const struct dso *self)
1036 {
1037         static const char origin[] = {
1038                 [DSO__ORIG_KERNEL] =   'k',
1039                 [DSO__ORIG_JAVA_JIT] = 'j',
1040                 [DSO__ORIG_FEDORA] =   'f',
1041                 [DSO__ORIG_UBUNTU] =   'u',
1042                 [DSO__ORIG_BUILDID] =  'b',
1043                 [DSO__ORIG_DSO] =      'd',
1044                 [DSO__ORIG_KMODULE] =  'K',
1045         };
1046
1047         if (self == NULL || self->origin == DSO__ORIG_NOT_FOUND)
1048                 return '!';
1049         return origin[self->origin];
1050 }
1051
1052 int dso__load(struct dso *self, struct map *map, symbol_filter_t filter)
1053 {
1054         int size = PATH_MAX;
1055         char *name;
1056         u8 build_id[BUILD_ID_SIZE];
1057         int ret = -1;
1058         int fd;
1059
1060         self->loaded = 1;
1061
1062         if (self->kernel)
1063                 return dso__load_kernel_sym(self, map, filter);
1064
1065         name = malloc(size);
1066         if (!name)
1067                 return -1;
1068
1069         self->adjust_symbols = 0;
1070
1071         if (strncmp(self->name, "/tmp/perf-", 10) == 0) {
1072                 ret = dso__load_perf_map(self, map, filter);
1073                 self->origin = ret > 0 ? DSO__ORIG_JAVA_JIT :
1074                                          DSO__ORIG_NOT_FOUND;
1075                 return ret;
1076         }
1077
1078         self->origin = DSO__ORIG_FEDORA - 1;
1079
1080 more:
1081         do {
1082                 self->origin++;
1083                 switch (self->origin) {
1084                 case DSO__ORIG_FEDORA:
1085                         snprintf(name, size, "/usr/lib/debug%s.debug",
1086                                  self->long_name);
1087                         break;
1088                 case DSO__ORIG_UBUNTU:
1089                         snprintf(name, size, "/usr/lib/debug%s",
1090                                  self->long_name);
1091                         break;
1092                 case DSO__ORIG_BUILDID:
1093                         if (filename__read_build_id(self->long_name, build_id,
1094                                                     sizeof(build_id))) {
1095                                 char build_id_hex[BUILD_ID_SIZE * 2 + 1];
1096
1097                                 build_id__sprintf(build_id, sizeof(build_id),
1098                                                   build_id_hex);
1099                                 snprintf(name, size,
1100                                          "/usr/lib/debug/.build-id/%.2s/%s.debug",
1101                                         build_id_hex, build_id_hex + 2);
1102                                 if (self->has_build_id)
1103                                         goto compare_build_id;
1104                                 break;
1105                         }
1106                         self->origin++;
1107                         /* Fall thru */
1108                 case DSO__ORIG_DSO:
1109                         snprintf(name, size, "%s", self->long_name);
1110                         break;
1111
1112                 default:
1113                         goto out;
1114                 }
1115
1116                 if (self->has_build_id) {
1117                         if (filename__read_build_id(name, build_id,
1118                                                     sizeof(build_id)) < 0)
1119                                 goto more;
1120 compare_build_id:
1121                         if (!dso__build_id_equal(self, build_id))
1122                                 goto more;
1123                 }
1124
1125                 fd = open(name, O_RDONLY);
1126         } while (fd < 0);
1127
1128         ret = dso__load_sym(self, map, name, fd, filter, 0, 0);
1129         close(fd);
1130
1131         /*
1132          * Some people seem to have debuginfo files _WITHOUT_ debug info!?!?
1133          */
1134         if (!ret)
1135                 goto more;
1136
1137         if (ret > 0) {
1138                 int nr_plt = dso__synthesize_plt_symbols(self, map, filter);
1139                 if (nr_plt > 0)
1140                         ret += nr_plt;
1141         }
1142 out:
1143         free(name);
1144         if (ret < 0 && strstr(self->name, " (deleted)") != NULL)
1145                 return 0;
1146         return ret;
1147 }
1148
1149 struct map *kernel_map;
1150
1151 static void kernel_maps__insert(struct map *map)
1152 {
1153         maps__insert(&kernel_maps, map);
1154 }
1155
1156 struct symbol *kernel_maps__find_symbol(u64 ip, struct map **mapp,
1157                                         symbol_filter_t filter)
1158 {
1159         struct map *map = maps__find(&kernel_maps, ip);
1160
1161         if (mapp)
1162                 *mapp = map;
1163
1164         if (map) {
1165                 ip = map->map_ip(map, ip);
1166                 return map__find_symbol(map, ip, filter);
1167         }
1168
1169         return NULL;
1170 }
1171
1172 struct map *kernel_maps__find_by_dso_name(const char *name)
1173 {
1174         struct rb_node *nd;
1175
1176         for (nd = rb_first(&kernel_maps); nd; nd = rb_next(nd)) {
1177                 struct map *map = rb_entry(nd, struct map, rb_node);
1178
1179                 if (map->dso && strcmp(map->dso->name, name) == 0)
1180                         return map;
1181         }
1182
1183         return NULL;
1184 }
1185
1186 static int dsos__set_modules_path_dir(char *dirname)
1187 {
1188         struct dirent *dent;
1189         DIR *dir = opendir(dirname);
1190
1191         if (!dir) {
1192                 pr_err("%s: cannot open %s dir\n", __func__, dirname);
1193                 return -1;
1194         }
1195
1196         while ((dent = readdir(dir)) != NULL) {
1197                 char path[PATH_MAX];
1198
1199                 if (dent->d_type == DT_DIR) {
1200                         if (!strcmp(dent->d_name, ".") ||
1201                             !strcmp(dent->d_name, ".."))
1202                                 continue;
1203
1204                         snprintf(path, sizeof(path), "%s/%s",
1205                                  dirname, dent->d_name);
1206                         if (dsos__set_modules_path_dir(path) < 0)
1207                                 goto failure;
1208                 } else {
1209                         char *dot = strrchr(dent->d_name, '.'),
1210                              dso_name[PATH_MAX];
1211                         struct map *map;
1212                         char *long_name;
1213
1214                         if (dot == NULL || strcmp(dot, ".ko"))
1215                                 continue;
1216                         snprintf(dso_name, sizeof(dso_name), "[%.*s]",
1217                                  (int)(dot - dent->d_name), dent->d_name);
1218
1219                         strxfrchar(dso_name, '-', '_');
1220                         map = kernel_maps__find_by_dso_name(dso_name);
1221                         if (map == NULL)
1222                                 continue;
1223
1224                         snprintf(path, sizeof(path), "%s/%s",
1225                                  dirname, dent->d_name);
1226
1227                         long_name = strdup(path);
1228                         if (long_name == NULL)
1229                                 goto failure;
1230                         dso__set_long_name(map->dso, long_name);
1231                 }
1232         }
1233
1234         return 0;
1235 failure:
1236         closedir(dir);
1237         return -1;
1238 }
1239
1240 static int dsos__set_modules_path(void)
1241 {
1242         struct utsname uts;
1243         char modules_path[PATH_MAX];
1244
1245         if (uname(&uts) < 0)
1246                 return -1;
1247
1248         snprintf(modules_path, sizeof(modules_path), "/lib/modules/%s/kernel",
1249                  uts.release);
1250
1251         return dsos__set_modules_path_dir(modules_path);
1252 }
1253
1254 /*
1255  * Constructor variant for modules (where we know from /proc/modules where
1256  * they are loaded) and for vmlinux, where only after we load all the
1257  * symbols we'll know where it starts and ends.
1258  */
1259 static struct map *map__new2(u64 start, struct dso *dso)
1260 {
1261         struct map *self = malloc(sizeof(*self));
1262
1263         if (self != NULL) {
1264                 /*
1265                  * ->end will be filled after we load all the symbols
1266                  */
1267                 map__init(self, start, 0, 0, dso);
1268         }
1269
1270         return self;
1271 }
1272
1273 static int kernel_maps__create_module_maps(void)
1274 {
1275         char *line = NULL;
1276         size_t n;
1277         FILE *file = fopen("/proc/modules", "r");
1278         struct map *map;
1279
1280         if (file == NULL)
1281                 return -1;
1282
1283         while (!feof(file)) {
1284                 char name[PATH_MAX];
1285                 u64 start;
1286                 struct dso *dso;
1287                 char *sep;
1288                 int line_len;
1289
1290                 line_len = getline(&line, &n, file);
1291                 if (line_len < 0)
1292                         break;
1293
1294                 if (!line)
1295                         goto out_failure;
1296
1297                 line[--line_len] = '\0'; /* \n */
1298
1299                 sep = strrchr(line, 'x');
1300                 if (sep == NULL)
1301                         continue;
1302
1303                 hex2u64(sep + 1, &start);
1304
1305                 sep = strchr(line, ' ');
1306                 if (sep == NULL)
1307                         continue;
1308
1309                 *sep = '\0';
1310
1311                 snprintf(name, sizeof(name), "[%s]", line);
1312                 dso = dso__new(name);
1313
1314                 if (dso == NULL)
1315                         goto out_delete_line;
1316
1317                 map = map__new2(start, dso);
1318                 if (map == NULL) {
1319                         dso__delete(dso);
1320                         goto out_delete_line;
1321                 }
1322
1323                 snprintf(name, sizeof(name),
1324                          "/sys/module/%s/notes/.note.gnu.build-id", line);
1325                 if (sysfs__read_build_id(name, dso->build_id,
1326                                          sizeof(dso->build_id)) == 0)
1327                         dso->has_build_id = true;
1328
1329                 dso->origin = DSO__ORIG_KMODULE;
1330                 kernel_maps__insert(map);
1331                 dsos__add(dso);
1332         }
1333
1334         free(line);
1335         fclose(file);
1336
1337         return dsos__set_modules_path();
1338
1339 out_delete_line:
1340         free(line);
1341 out_failure:
1342         return -1;
1343 }
1344
1345 static int dso__load_vmlinux(struct dso *self, struct map *map,
1346                              const char *vmlinux, symbol_filter_t filter)
1347 {
1348         int err = -1, fd;
1349
1350         if (self->has_build_id) {
1351                 u8 build_id[BUILD_ID_SIZE];
1352
1353                 if (filename__read_build_id(vmlinux, build_id,
1354                                             sizeof(build_id)) < 0) {
1355                         pr_debug("No build_id in %s, ignoring it\n", vmlinux);
1356                         return -1;
1357                 }
1358                 if (!dso__build_id_equal(self, build_id)) {
1359                         char expected_build_id[BUILD_ID_SIZE * 2 + 1],
1360                              vmlinux_build_id[BUILD_ID_SIZE * 2 + 1];
1361
1362                         build_id__sprintf(self->build_id,
1363                                           sizeof(self->build_id),
1364                                           expected_build_id);
1365                         build_id__sprintf(build_id, sizeof(build_id),
1366                                           vmlinux_build_id);
1367                         pr_debug("build_id in %s is %s while expected is %s, "
1368                                  "ignoring it\n", vmlinux, vmlinux_build_id,
1369                                  expected_build_id);
1370                         return -1;
1371                 }
1372         }
1373
1374         fd = open(vmlinux, O_RDONLY);
1375         if (fd < 0)
1376                 return -1;
1377
1378         self->loaded = 1;
1379         err = dso__load_sym(self, map, self->long_name, fd, filter, 1, 0);
1380
1381         close(fd);
1382
1383         return err;
1384 }
1385
1386 static int dso__load_kernel_sym(struct dso *self, struct map *map,
1387                                 symbol_filter_t filter)
1388 {
1389         int err = dso__load_vmlinux(self, map, self->name, filter);
1390
1391         if (err <= 0) {
1392                 err = kernel_maps__load_kallsyms(filter);
1393                 if (err > 0)
1394                         dso__set_long_name(self, strdup("[kernel.kallsyms]"));
1395         }
1396
1397         if (err > 0) {
1398                 map__fixup_start(map);
1399                 map__fixup_end(map);
1400         }
1401
1402         return err;
1403 }
1404
1405 LIST_HEAD(dsos);
1406 struct dso      *vdso;
1407
1408 const char      *vmlinux_name = "vmlinux";
1409
1410 static void dsos__add(struct dso *dso)
1411 {
1412         list_add_tail(&dso->node, &dsos);
1413 }
1414
1415 static struct dso *dsos__find(const char *name)
1416 {
1417         struct dso *pos;
1418
1419         list_for_each_entry(pos, &dsos, node)
1420                 if (strcmp(pos->name, name) == 0)
1421                         return pos;
1422         return NULL;
1423 }
1424
1425 struct dso *dsos__findnew(const char *name)
1426 {
1427         struct dso *dso = dsos__find(name);
1428
1429         if (!dso) {
1430                 dso = dso__new(name);
1431                 if (dso != NULL) {
1432                         dsos__add(dso);
1433                         dso__set_basename(dso);
1434                 }
1435         }
1436
1437         return dso;
1438 }
1439
1440 void dsos__fprintf(FILE *fp)
1441 {
1442         struct dso *pos;
1443
1444         list_for_each_entry(pos, &dsos, node)
1445                 dso__fprintf(pos, fp);
1446 }
1447
1448 size_t dsos__fprintf_buildid(FILE *fp)
1449 {
1450         struct dso *pos;
1451         size_t ret = 0;
1452
1453         list_for_each_entry(pos, &dsos, node) {
1454                 ret += dso__fprintf_buildid(pos, fp);
1455                 ret += fprintf(fp, " %s\n", pos->long_name);
1456         }
1457         return ret;
1458 }
1459
1460 static int kernel_maps__create_kernel_map(void)
1461 {
1462         struct dso *kernel = dso__new(vmlinux_name);
1463
1464         if (kernel == NULL)
1465                 return -1;
1466
1467         kernel_map = map__new2(0, kernel);
1468         if (kernel_map == NULL)
1469                 goto out_delete_kernel_dso;
1470
1471         kernel_map->map_ip = kernel_map->unmap_ip = identity__map_ip;
1472
1473         kernel->short_name = "[kernel]";
1474         kernel->kernel = 1;
1475         vdso = dso__new("[vdso]");
1476         if (vdso == NULL)
1477                 goto out_delete_kernel_map;
1478
1479         if (sysfs__read_build_id("/sys/kernel/notes", kernel->build_id,
1480                                  sizeof(kernel->build_id)) == 0)
1481                 kernel->has_build_id = true;
1482
1483         kernel_maps__insert(kernel_map);
1484         dsos__add(kernel);
1485         dsos__add(vdso);
1486
1487         return 0;
1488
1489 out_delete_kernel_map:
1490         map__delete(kernel_map);
1491         kernel_map = NULL;
1492 out_delete_kernel_dso:
1493         dso__delete(kernel);
1494         return -1;
1495 }
1496
1497 int kernel_maps__init(bool use_modules)
1498 {
1499         if (kernel_maps__create_kernel_map() < 0)
1500                 return -1;
1501
1502         if (use_modules && kernel_maps__create_module_maps() < 0)
1503                 pr_warning("Failed to load list of modules in use, "
1504                            "continuing...\n");
1505         /*
1506          * Now that we have all the maps created, just set the ->end of them:
1507          */
1508         kernel_maps__fixup_end();
1509         return 0;
1510 }
1511
1512 void symbol__init(unsigned int priv_size)
1513 {
1514         elf_version(EV_CURRENT);
1515         symbol__priv_size = priv_size;
1516 }