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