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