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