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