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