b812ace91c60a7c82426ad627d9b451b93afff50
[pandora-kernel.git] / tools / perf / util / symbol.c
1 #define _GNU_SOURCE
2 #include <ctype.h>
3 #include <dirent.h>
4 #include <errno.h>
5 #include <libgen.h>
6 #include <stdlib.h>
7 #include <stdio.h>
8 #include <string.h>
9 #include <sys/types.h>
10 #include <sys/stat.h>
11 #include <sys/param.h>
12 #include <fcntl.h>
13 #include <unistd.h>
14 #include "build-id.h"
15 #include "debug.h"
16 #include "symbol.h"
17 #include "strlist.h"
18
19 #include <libelf.h>
20 #include <gelf.h>
21 #include <elf.h>
22 #include <limits.h>
23 #include <sys/utsname.h>
24
25 #ifndef NT_GNU_BUILD_ID
26 #define NT_GNU_BUILD_ID 3
27 #endif
28
29 static bool dso__build_id_equal(const struct dso *self, u8 *build_id);
30 static int elf_read_build_id(Elf *elf, void *bf, size_t size);
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 int dso__load_kernel_sym(struct dso *self, struct map *map,
34                                 symbol_filter_t filter);
35 static int dso__load_guest_kernel_sym(struct dso *self, struct map *map,
36                         symbol_filter_t filter);
37 static int vmlinux_path__nr_entries;
38 static char **vmlinux_path;
39
40 struct symbol_conf symbol_conf = {
41         .exclude_other    = true,
42         .use_modules      = true,
43         .try_vmlinux_path = true,
44 };
45
46 int dso__name_len(const struct dso *self)
47 {
48         if (verbose)
49                 return self->long_name_len;
50
51         return self->short_name_len;
52 }
53
54 bool dso__loaded(const struct dso *self, enum map_type type)
55 {
56         return self->loaded & (1 << type);
57 }
58
59 bool dso__sorted_by_name(const struct dso *self, enum map_type type)
60 {
61         return self->sorted_by_name & (1 << type);
62 }
63
64 static void dso__set_sorted_by_name(struct dso *self, enum map_type type)
65 {
66         self->sorted_by_name |= (1 << type);
67 }
68
69 bool symbol_type__is_a(char symbol_type, enum map_type map_type)
70 {
71         switch (map_type) {
72         case MAP__FUNCTION:
73                 return symbol_type == 'T' || symbol_type == 'W';
74         case MAP__VARIABLE:
75                 return symbol_type == 'D' || symbol_type == 'd';
76         default:
77                 return false;
78         }
79 }
80
81 static void symbols__fixup_end(struct rb_root *self)
82 {
83         struct rb_node *nd, *prevnd = rb_first(self);
84         struct symbol *curr, *prev;
85
86         if (prevnd == NULL)
87                 return;
88
89         curr = rb_entry(prevnd, struct symbol, rb_node);
90
91         for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
92                 prev = curr;
93                 curr = rb_entry(nd, struct symbol, rb_node);
94
95                 if (prev->end == prev->start)
96                         prev->end = curr->start - 1;
97         }
98
99         /* Last entry */
100         if (curr->end == curr->start)
101                 curr->end = roundup(curr->start, 4096);
102 }
103
104 static void __map_groups__fixup_end(struct map_groups *self, enum map_type type)
105 {
106         struct map *prev, *curr;
107         struct rb_node *nd, *prevnd = rb_first(&self->maps[type]);
108
109         if (prevnd == NULL)
110                 return;
111
112         curr = rb_entry(prevnd, struct map, rb_node);
113
114         for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
115                 prev = curr;
116                 curr = rb_entry(nd, struct map, rb_node);
117                 prev->end = curr->start - 1;
118         }
119
120         /*
121          * We still haven't the actual symbols, so guess the
122          * last map final address.
123          */
124         curr->end = ~0UL;
125 }
126
127 static void map_groups__fixup_end(struct map_groups *self)
128 {
129         int i;
130         for (i = 0; i < MAP__NR_TYPES; ++i)
131                 __map_groups__fixup_end(self, i);
132 }
133
134 static struct symbol *symbol__new(u64 start, u64 len, const char *name)
135 {
136         size_t namelen = strlen(name) + 1;
137         struct symbol *self = calloc(1, (symbol_conf.priv_size +
138                                          sizeof(*self) + namelen));
139         if (self == NULL)
140                 return NULL;
141
142         if (symbol_conf.priv_size)
143                 self = ((void *)self) + symbol_conf.priv_size;
144
145         self->start   = start;
146         self->end     = len ? start + len - 1 : start;
147         self->namelen = namelen - 1;
148
149         pr_debug4("%s: %s %#Lx-%#Lx\n", __func__, name, start, self->end);
150
151         memcpy(self->name, name, namelen);
152
153         return self;
154 }
155
156 void symbol__delete(struct symbol *self)
157 {
158         free(((void *)self) - symbol_conf.priv_size);
159 }
160
161 static size_t symbol__fprintf(struct symbol *self, FILE *fp)
162 {
163         return fprintf(fp, " %llx-%llx %s\n",
164                        self->start, self->end, self->name);
165 }
166
167 void dso__set_long_name(struct dso *self, char *name)
168 {
169         if (name == NULL)
170                 return;
171         self->long_name = name;
172         self->long_name_len = strlen(name);
173 }
174
175 static void dso__set_short_name(struct dso *self, const char *name)
176 {
177         if (name == NULL)
178                 return;
179         self->short_name = name;
180         self->short_name_len = strlen(name);
181 }
182
183 static void dso__set_basename(struct dso *self)
184 {
185         dso__set_short_name(self, basename(self->long_name));
186 }
187
188 struct dso *dso__new(const char *name)
189 {
190         struct dso *self = calloc(1, sizeof(*self) + strlen(name) + 1);
191
192         if (self != NULL) {
193                 int i;
194                 strcpy(self->name, name);
195                 dso__set_long_name(self, self->name);
196                 dso__set_short_name(self, self->name);
197                 for (i = 0; i < MAP__NR_TYPES; ++i)
198                         self->symbols[i] = self->symbol_names[i] = RB_ROOT;
199                 self->slen_calculated = 0;
200                 self->origin = DSO__ORIG_NOT_FOUND;
201                 self->loaded = 0;
202                 self->sorted_by_name = 0;
203                 self->has_build_id = 0;
204                 self->kernel = DSO_TYPE_USER;
205                 INIT_LIST_HEAD(&self->node);
206         }
207
208         return self;
209 }
210
211 static void symbols__delete(struct rb_root *self)
212 {
213         struct symbol *pos;
214         struct rb_node *next = rb_first(self);
215
216         while (next) {
217                 pos = rb_entry(next, struct symbol, rb_node);
218                 next = rb_next(&pos->rb_node);
219                 rb_erase(&pos->rb_node, self);
220                 symbol__delete(pos);
221         }
222 }
223
224 void dso__delete(struct dso *self)
225 {
226         int i;
227         for (i = 0; i < MAP__NR_TYPES; ++i)
228                 symbols__delete(&self->symbols[i]);
229         if (self->sname_alloc)
230                 free((char *)self->short_name);
231         if (self->lname_alloc)
232                 free(self->long_name);
233         free(self);
234 }
235
236 void dso__set_build_id(struct dso *self, void *build_id)
237 {
238         memcpy(self->build_id, build_id, sizeof(self->build_id));
239         self->has_build_id = 1;
240 }
241
242 static void symbols__insert(struct rb_root *self, struct symbol *sym)
243 {
244         struct rb_node **p = &self->rb_node;
245         struct rb_node *parent = NULL;
246         const u64 ip = sym->start;
247         struct symbol *s;
248
249         while (*p != NULL) {
250                 parent = *p;
251                 s = rb_entry(parent, struct symbol, rb_node);
252                 if (ip < s->start)
253                         p = &(*p)->rb_left;
254                 else
255                         p = &(*p)->rb_right;
256         }
257         rb_link_node(&sym->rb_node, parent, p);
258         rb_insert_color(&sym->rb_node, self);
259 }
260
261 static struct symbol *symbols__find(struct rb_root *self, u64 ip)
262 {
263         struct rb_node *n;
264
265         if (self == NULL)
266                 return NULL;
267
268         n = self->rb_node;
269
270         while (n) {
271                 struct symbol *s = rb_entry(n, struct symbol, rb_node);
272
273                 if (ip < s->start)
274                         n = n->rb_left;
275                 else if (ip > s->end)
276                         n = n->rb_right;
277                 else
278                         return s;
279         }
280
281         return NULL;
282 }
283
284 struct symbol_name_rb_node {
285         struct rb_node  rb_node;
286         struct symbol   sym;
287 };
288
289 static void symbols__insert_by_name(struct rb_root *self, struct symbol *sym)
290 {
291         struct rb_node **p = &self->rb_node;
292         struct rb_node *parent = NULL;
293         struct symbol_name_rb_node *symn = ((void *)sym) - sizeof(*parent), *s;
294
295         while (*p != NULL) {
296                 parent = *p;
297                 s = rb_entry(parent, struct symbol_name_rb_node, rb_node);
298                 if (strcmp(sym->name, s->sym.name) < 0)
299                         p = &(*p)->rb_left;
300                 else
301                         p = &(*p)->rb_right;
302         }
303         rb_link_node(&symn->rb_node, parent, p);
304         rb_insert_color(&symn->rb_node, self);
305 }
306
307 static void symbols__sort_by_name(struct rb_root *self, struct rb_root *source)
308 {
309         struct rb_node *nd;
310
311         for (nd = rb_first(source); nd; nd = rb_next(nd)) {
312                 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
313                 symbols__insert_by_name(self, pos);
314         }
315 }
316
317 static struct symbol *symbols__find_by_name(struct rb_root *self, const char *name)
318 {
319         struct rb_node *n;
320
321         if (self == NULL)
322                 return NULL;
323
324         n = self->rb_node;
325
326         while (n) {
327                 struct symbol_name_rb_node *s;
328                 int cmp;
329
330                 s = rb_entry(n, struct symbol_name_rb_node, rb_node);
331                 cmp = strcmp(name, s->sym.name);
332
333                 if (cmp < 0)
334                         n = n->rb_left;
335                 else if (cmp > 0)
336                         n = n->rb_right;
337                 else
338                         return &s->sym;
339         }
340
341         return NULL;
342 }
343
344 struct symbol *dso__find_symbol(struct dso *self,
345                                 enum map_type type, u64 addr)
346 {
347         return symbols__find(&self->symbols[type], addr);
348 }
349
350 struct symbol *dso__find_symbol_by_name(struct dso *self, enum map_type type,
351                                         const char *name)
352 {
353         return symbols__find_by_name(&self->symbol_names[type], name);
354 }
355
356 void dso__sort_by_name(struct dso *self, enum map_type type)
357 {
358         dso__set_sorted_by_name(self, type);
359         return symbols__sort_by_name(&self->symbol_names[type],
360                                      &self->symbols[type]);
361 }
362
363 int build_id__sprintf(const u8 *self, int len, char *bf)
364 {
365         char *bid = bf;
366         const u8 *raw = self;
367         int i;
368
369         for (i = 0; i < len; ++i) {
370                 sprintf(bid, "%02x", *raw);
371                 ++raw;
372                 bid += 2;
373         }
374
375         return raw - self;
376 }
377
378 size_t dso__fprintf_buildid(struct dso *self, FILE *fp)
379 {
380         char sbuild_id[BUILD_ID_SIZE * 2 + 1];
381
382         build_id__sprintf(self->build_id, sizeof(self->build_id), sbuild_id);
383         return fprintf(fp, "%s", sbuild_id);
384 }
385
386 size_t dso__fprintf(struct dso *self, enum map_type type, FILE *fp)
387 {
388         struct rb_node *nd;
389         size_t ret = fprintf(fp, "dso: %s (", self->short_name);
390
391         if (self->short_name != self->long_name)
392                 ret += fprintf(fp, "%s, ", self->long_name);
393         ret += fprintf(fp, "%s, %sloaded, ", map_type__name[type],
394                        self->loaded ? "" : "NOT ");
395         ret += dso__fprintf_buildid(self, fp);
396         ret += fprintf(fp, ")\n");
397         for (nd = rb_first(&self->symbols[type]); nd; nd = rb_next(nd)) {
398                 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
399                 ret += symbol__fprintf(pos, fp);
400         }
401
402         return ret;
403 }
404
405 int kallsyms__parse(const char *filename, void *arg,
406                     int (*process_symbol)(void *arg, const char *name,
407                                                      char type, u64 start))
408 {
409         char *line = NULL;
410         size_t n;
411         int err = 0;
412         FILE *file = fopen(filename, "r");
413
414         if (file == NULL)
415                 goto out_failure;
416
417         while (!feof(file)) {
418                 u64 start;
419                 int line_len, len;
420                 char symbol_type;
421                 char *symbol_name;
422
423                 line_len = getline(&line, &n, file);
424                 if (line_len < 0 || !line)
425                         break;
426
427                 line[--line_len] = '\0'; /* \n */
428
429                 len = hex2u64(line, &start);
430
431                 len++;
432                 if (len + 2 >= line_len)
433                         continue;
434
435                 symbol_type = toupper(line[len]);
436                 symbol_name = line + len + 2;
437
438                 err = process_symbol(arg, symbol_name, symbol_type, start);
439                 if (err)
440                         break;
441         }
442
443         free(line);
444         fclose(file);
445         return err;
446
447 out_failure:
448         return -1;
449 }
450
451 struct process_kallsyms_args {
452         struct map *map;
453         struct dso *dso;
454 };
455
456 static int map__process_kallsym_symbol(void *arg, const char *name,
457                                        char type, u64 start)
458 {
459         struct symbol *sym;
460         struct process_kallsyms_args *a = arg;
461         struct rb_root *root = &a->dso->symbols[a->map->type];
462
463         if (!symbol_type__is_a(type, a->map->type))
464                 return 0;
465
466         /*
467          * Will fix up the end later, when we have all symbols sorted.
468          */
469         sym = symbol__new(start, 0, name);
470
471         if (sym == NULL)
472                 return -ENOMEM;
473         /*
474          * We will pass the symbols to the filter later, in
475          * map__split_kallsyms, when we have split the maps per module
476          */
477         symbols__insert(root, sym);
478
479         return 0;
480 }
481
482 /*
483  * Loads the function entries in /proc/kallsyms into kernel_map->dso,
484  * so that we can in the next step set the symbol ->end address and then
485  * call kernel_maps__split_kallsyms.
486  */
487 static int dso__load_all_kallsyms(struct dso *self, const char *filename,
488                                   struct map *map)
489 {
490         struct process_kallsyms_args args = { .map = map, .dso = self, };
491         return kallsyms__parse(filename, &args, map__process_kallsym_symbol);
492 }
493
494 /*
495  * Split the symbols into maps, making sure there are no overlaps, i.e. the
496  * kernel range is broken in several maps, named [kernel].N, as we don't have
497  * the original ELF section names vmlinux have.
498  */
499 static int dso__split_kallsyms(struct dso *self, struct map *map,
500                                symbol_filter_t filter)
501 {
502         struct map_groups *kmaps = map__kmap(map)->kmaps;
503         struct machine *machine = kmaps->machine;
504         struct map *curr_map = map;
505         struct symbol *pos;
506         int count = 0;
507         struct rb_root *root = &self->symbols[map->type];
508         struct rb_node *next = rb_first(root);
509         int kernel_range = 0;
510
511         while (next) {
512                 char *module;
513
514                 pos = rb_entry(next, struct symbol, rb_node);
515                 next = rb_next(&pos->rb_node);
516
517                 module = strchr(pos->name, '\t');
518                 if (module) {
519                         if (!symbol_conf.use_modules)
520                                 goto discard_symbol;
521
522                         *module++ = '\0';
523
524                         if (strcmp(curr_map->dso->short_name, module)) {
525                                 if (curr_map != map &&
526                                     self->kernel == DSO_TYPE_GUEST_KERNEL &&
527                                     machine__is_default_guest(machine)) {
528                                         /*
529                                          * We assume all symbols of a module are
530                                          * continuous in * kallsyms, so curr_map
531                                          * points to a module and all its
532                                          * symbols are in its kmap. Mark it as
533                                          * loaded.
534                                          */
535                                         dso__set_loaded(curr_map->dso,
536                                                         curr_map->type);
537                                 }
538
539                                 curr_map = map_groups__find_by_name(kmaps,
540                                                         map->type, module);
541                                 if (curr_map == NULL) {
542                                         pr_debug("%s/proc/{kallsyms,modules} "
543                                                  "inconsistency while looking "
544                                                  "for \"%s\" module!\n",
545                                                  machine->root_dir, module);
546                                         curr_map = map;
547                                         goto discard_symbol;
548                                 }
549
550                                 if (curr_map->dso->loaded &&
551                                     !machine__is_default_guest(machine))
552                                         goto discard_symbol;
553                         }
554                         /*
555                          * So that we look just like we get from .ko files,
556                          * i.e. not prelinked, relative to map->start.
557                          */
558                         pos->start = curr_map->map_ip(curr_map, pos->start);
559                         pos->end   = curr_map->map_ip(curr_map, pos->end);
560                 } else if (curr_map != map) {
561                         char dso_name[PATH_MAX];
562                         struct dso *dso;
563
564                         if (self->kernel == DSO_TYPE_GUEST_KERNEL)
565                                 snprintf(dso_name, sizeof(dso_name),
566                                         "[guest.kernel].%d",
567                                         kernel_range++);
568                         else
569                                 snprintf(dso_name, sizeof(dso_name),
570                                         "[kernel].%d",
571                                         kernel_range++);
572
573                         dso = dso__new(dso_name);
574                         if (dso == NULL)
575                                 return -1;
576
577                         dso->kernel = self->kernel;
578
579                         curr_map = map__new2(pos->start, dso, map->type);
580                         if (curr_map == NULL) {
581                                 dso__delete(dso);
582                                 return -1;
583                         }
584
585                         curr_map->map_ip = curr_map->unmap_ip = identity__map_ip;
586                         map_groups__insert(kmaps, curr_map);
587                         ++kernel_range;
588                 }
589
590                 if (filter && filter(curr_map, pos)) {
591 discard_symbol:         rb_erase(&pos->rb_node, root);
592                         symbol__delete(pos);
593                 } else {
594                         if (curr_map != map) {
595                                 rb_erase(&pos->rb_node, root);
596                                 symbols__insert(&curr_map->dso->symbols[curr_map->type], pos);
597                         }
598                         count++;
599                 }
600         }
601
602         if (curr_map != map &&
603             self->kernel == DSO_TYPE_GUEST_KERNEL &&
604             machine__is_default_guest(kmaps->machine)) {
605                 dso__set_loaded(curr_map->dso, curr_map->type);
606         }
607
608         return count;
609 }
610
611 int dso__load_kallsyms(struct dso *self, const char *filename,
612                        struct map *map, symbol_filter_t filter)
613 {
614         if (dso__load_all_kallsyms(self, filename, map) < 0)
615                 return -1;
616
617         symbols__fixup_end(&self->symbols[map->type]);
618         if (self->kernel == DSO_TYPE_GUEST_KERNEL)
619                 self->origin = DSO__ORIG_GUEST_KERNEL;
620         else
621                 self->origin = DSO__ORIG_KERNEL;
622
623         return dso__split_kallsyms(self, map, filter);
624 }
625
626 static int dso__load_perf_map(struct dso *self, struct map *map,
627                               symbol_filter_t filter)
628 {
629         char *line = NULL;
630         size_t n;
631         FILE *file;
632         int nr_syms = 0;
633
634         file = fopen(self->long_name, "r");
635         if (file == NULL)
636                 goto out_failure;
637
638         while (!feof(file)) {
639                 u64 start, size;
640                 struct symbol *sym;
641                 int line_len, len;
642
643                 line_len = getline(&line, &n, file);
644                 if (line_len < 0)
645                         break;
646
647                 if (!line)
648                         goto out_failure;
649
650                 line[--line_len] = '\0'; /* \n */
651
652                 len = hex2u64(line, &start);
653
654                 len++;
655                 if (len + 2 >= line_len)
656                         continue;
657
658                 len += hex2u64(line + len, &size);
659
660                 len++;
661                 if (len + 2 >= line_len)
662                         continue;
663
664                 sym = symbol__new(start, size, line + len);
665
666                 if (sym == NULL)
667                         goto out_delete_line;
668
669                 if (filter && filter(map, sym))
670                         symbol__delete(sym);
671                 else {
672                         symbols__insert(&self->symbols[map->type], sym);
673                         nr_syms++;
674                 }
675         }
676
677         free(line);
678         fclose(file);
679
680         return nr_syms;
681
682 out_delete_line:
683         free(line);
684 out_failure:
685         return -1;
686 }
687
688 /**
689  * elf_symtab__for_each_symbol - iterate thru all the symbols
690  *
691  * @self: struct elf_symtab instance to iterate
692  * @idx: uint32_t idx
693  * @sym: GElf_Sym iterator
694  */
695 #define elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) \
696         for (idx = 0, gelf_getsym(syms, idx, &sym);\
697              idx < nr_syms; \
698              idx++, gelf_getsym(syms, idx, &sym))
699
700 static inline uint8_t elf_sym__type(const GElf_Sym *sym)
701 {
702         return GELF_ST_TYPE(sym->st_info);
703 }
704
705 static inline int elf_sym__is_function(const GElf_Sym *sym)
706 {
707         return elf_sym__type(sym) == STT_FUNC &&
708                sym->st_name != 0 &&
709                sym->st_shndx != SHN_UNDEF;
710 }
711
712 static inline bool elf_sym__is_object(const GElf_Sym *sym)
713 {
714         return elf_sym__type(sym) == STT_OBJECT &&
715                 sym->st_name != 0 &&
716                 sym->st_shndx != SHN_UNDEF;
717 }
718
719 static inline int elf_sym__is_label(const GElf_Sym *sym)
720 {
721         return elf_sym__type(sym) == STT_NOTYPE &&
722                 sym->st_name != 0 &&
723                 sym->st_shndx != SHN_UNDEF &&
724                 sym->st_shndx != SHN_ABS;
725 }
726
727 static inline const char *elf_sec__name(const GElf_Shdr *shdr,
728                                         const Elf_Data *secstrs)
729 {
730         return secstrs->d_buf + shdr->sh_name;
731 }
732
733 static inline int elf_sec__is_text(const GElf_Shdr *shdr,
734                                         const Elf_Data *secstrs)
735 {
736         return strstr(elf_sec__name(shdr, secstrs), "text") != NULL;
737 }
738
739 static inline bool elf_sec__is_data(const GElf_Shdr *shdr,
740                                     const Elf_Data *secstrs)
741 {
742         return strstr(elf_sec__name(shdr, secstrs), "data") != NULL;
743 }
744
745 static inline const char *elf_sym__name(const GElf_Sym *sym,
746                                         const Elf_Data *symstrs)
747 {
748         return symstrs->d_buf + sym->st_name;
749 }
750
751 static Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep,
752                                     GElf_Shdr *shp, const char *name,
753                                     size_t *idx)
754 {
755         Elf_Scn *sec = NULL;
756         size_t cnt = 1;
757
758         while ((sec = elf_nextscn(elf, sec)) != NULL) {
759                 char *str;
760
761                 gelf_getshdr(sec, shp);
762                 str = elf_strptr(elf, ep->e_shstrndx, shp->sh_name);
763                 if (!strcmp(name, str)) {
764                         if (idx)
765                                 *idx = cnt;
766                         break;
767                 }
768                 ++cnt;
769         }
770
771         return sec;
772 }
773
774 #define elf_section__for_each_rel(reldata, pos, pos_mem, idx, nr_entries) \
775         for (idx = 0, pos = gelf_getrel(reldata, 0, &pos_mem); \
776              idx < nr_entries; \
777              ++idx, pos = gelf_getrel(reldata, idx, &pos_mem))
778
779 #define elf_section__for_each_rela(reldata, pos, pos_mem, idx, nr_entries) \
780         for (idx = 0, pos = gelf_getrela(reldata, 0, &pos_mem); \
781              idx < nr_entries; \
782              ++idx, pos = gelf_getrela(reldata, idx, &pos_mem))
783
784 /*
785  * We need to check if we have a .dynsym, so that we can handle the
786  * .plt, synthesizing its symbols, that aren't on the symtabs (be it
787  * .dynsym or .symtab).
788  * And always look at the original dso, not at debuginfo packages, that
789  * have the PLT data stripped out (shdr_rel_plt.sh_type == SHT_NOBITS).
790  */
791 static int dso__synthesize_plt_symbols(struct  dso *self, struct map *map,
792                                        symbol_filter_t filter)
793 {
794         uint32_t nr_rel_entries, idx;
795         GElf_Sym sym;
796         u64 plt_offset;
797         GElf_Shdr shdr_plt;
798         struct symbol *f;
799         GElf_Shdr shdr_rel_plt, shdr_dynsym;
800         Elf_Data *reldata, *syms, *symstrs;
801         Elf_Scn *scn_plt_rel, *scn_symstrs, *scn_dynsym;
802         size_t dynsym_idx;
803         GElf_Ehdr ehdr;
804         char sympltname[1024];
805         Elf *elf;
806         int nr = 0, symidx, fd, err = 0;
807
808         fd = open(self->long_name, O_RDONLY);
809         if (fd < 0)
810                 goto out;
811
812         elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
813         if (elf == NULL)
814                 goto out_close;
815
816         if (gelf_getehdr(elf, &ehdr) == NULL)
817                 goto out_elf_end;
818
819         scn_dynsym = elf_section_by_name(elf, &ehdr, &shdr_dynsym,
820                                          ".dynsym", &dynsym_idx);
821         if (scn_dynsym == NULL)
822                 goto out_elf_end;
823
824         scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt,
825                                           ".rela.plt", NULL);
826         if (scn_plt_rel == NULL) {
827                 scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt,
828                                                   ".rel.plt", NULL);
829                 if (scn_plt_rel == NULL)
830                         goto out_elf_end;
831         }
832
833         err = -1;
834
835         if (shdr_rel_plt.sh_link != dynsym_idx)
836                 goto out_elf_end;
837
838         if (elf_section_by_name(elf, &ehdr, &shdr_plt, ".plt", NULL) == NULL)
839                 goto out_elf_end;
840
841         /*
842          * Fetch the relocation section to find the idxes to the GOT
843          * and the symbols in the .dynsym they refer to.
844          */
845         reldata = elf_getdata(scn_plt_rel, NULL);
846         if (reldata == NULL)
847                 goto out_elf_end;
848
849         syms = elf_getdata(scn_dynsym, NULL);
850         if (syms == NULL)
851                 goto out_elf_end;
852
853         scn_symstrs = elf_getscn(elf, shdr_dynsym.sh_link);
854         if (scn_symstrs == NULL)
855                 goto out_elf_end;
856
857         symstrs = elf_getdata(scn_symstrs, NULL);
858         if (symstrs == NULL)
859                 goto out_elf_end;
860
861         nr_rel_entries = shdr_rel_plt.sh_size / shdr_rel_plt.sh_entsize;
862         plt_offset = shdr_plt.sh_offset;
863
864         if (shdr_rel_plt.sh_type == SHT_RELA) {
865                 GElf_Rela pos_mem, *pos;
866
867                 elf_section__for_each_rela(reldata, pos, pos_mem, idx,
868                                            nr_rel_entries) {
869                         symidx = GELF_R_SYM(pos->r_info);
870                         plt_offset += shdr_plt.sh_entsize;
871                         gelf_getsym(syms, symidx, &sym);
872                         snprintf(sympltname, sizeof(sympltname),
873                                  "%s@plt", elf_sym__name(&sym, symstrs));
874
875                         f = symbol__new(plt_offset, shdr_plt.sh_entsize,
876                                         sympltname);
877                         if (!f)
878                                 goto out_elf_end;
879
880                         if (filter && filter(map, f))
881                                 symbol__delete(f);
882                         else {
883                                 symbols__insert(&self->symbols[map->type], f);
884                                 ++nr;
885                         }
886                 }
887         } else if (shdr_rel_plt.sh_type == SHT_REL) {
888                 GElf_Rel pos_mem, *pos;
889                 elf_section__for_each_rel(reldata, pos, pos_mem, idx,
890                                           nr_rel_entries) {
891                         symidx = GELF_R_SYM(pos->r_info);
892                         plt_offset += shdr_plt.sh_entsize;
893                         gelf_getsym(syms, symidx, &sym);
894                         snprintf(sympltname, sizeof(sympltname),
895                                  "%s@plt", elf_sym__name(&sym, symstrs));
896
897                         f = symbol__new(plt_offset, shdr_plt.sh_entsize,
898                                         sympltname);
899                         if (!f)
900                                 goto out_elf_end;
901
902                         if (filter && filter(map, f))
903                                 symbol__delete(f);
904                         else {
905                                 symbols__insert(&self->symbols[map->type], f);
906                                 ++nr;
907                         }
908                 }
909         }
910
911         err = 0;
912 out_elf_end:
913         elf_end(elf);
914 out_close:
915         close(fd);
916
917         if (err == 0)
918                 return nr;
919 out:
920         pr_debug("%s: problems reading %s PLT info.\n",
921                  __func__, self->long_name);
922         return 0;
923 }
924
925 static bool elf_sym__is_a(GElf_Sym *self, enum map_type type)
926 {
927         switch (type) {
928         case MAP__FUNCTION:
929                 return elf_sym__is_function(self);
930         case MAP__VARIABLE:
931                 return elf_sym__is_object(self);
932         default:
933                 return false;
934         }
935 }
936
937 static bool elf_sec__is_a(GElf_Shdr *self, Elf_Data *secstrs, enum map_type type)
938 {
939         switch (type) {
940         case MAP__FUNCTION:
941                 return elf_sec__is_text(self, secstrs);
942         case MAP__VARIABLE:
943                 return elf_sec__is_data(self, secstrs);
944         default:
945                 return false;
946         }
947 }
948
949 static size_t elf_addr_to_index(Elf *elf, GElf_Addr addr)
950 {
951         Elf_Scn *sec = NULL;
952         GElf_Shdr shdr;
953         size_t cnt = 1;
954
955         while ((sec = elf_nextscn(elf, sec)) != NULL) {
956                 gelf_getshdr(sec, &shdr);
957
958                 if ((addr >= shdr.sh_addr) &&
959                     (addr < (shdr.sh_addr + shdr.sh_size)))
960                         return cnt;
961
962                 ++cnt;
963         }
964
965         return -1;
966 }
967
968 static int dso__load_sym(struct dso *self, struct map *map, const char *name,
969                          int fd, symbol_filter_t filter, int kmodule)
970 {
971         struct kmap *kmap = self->kernel ? map__kmap(map) : NULL;
972         struct map *curr_map = map;
973         struct dso *curr_dso = self;
974         Elf_Data *symstrs, *secstrs;
975         uint32_t nr_syms;
976         int err = -1;
977         uint32_t idx;
978         GElf_Ehdr ehdr;
979         GElf_Shdr shdr, opdshdr;
980         Elf_Data *syms, *opddata = NULL;
981         GElf_Sym sym;
982         Elf_Scn *sec, *sec_strndx, *opdsec;
983         Elf *elf;
984         int nr = 0;
985         size_t opdidx = 0;
986
987         elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
988         if (elf == NULL) {
989                 pr_err("%s: cannot read %s ELF file.\n", __func__, name);
990                 goto out_close;
991         }
992
993         if (gelf_getehdr(elf, &ehdr) == NULL) {
994                 pr_err("%s: cannot get elf header.\n", __func__);
995                 goto out_elf_end;
996         }
997
998         if (self->has_build_id) {
999                 u8 build_id[BUILD_ID_SIZE];
1000
1001                 if (elf_read_build_id(elf, build_id,
1002                                       BUILD_ID_SIZE) != BUILD_ID_SIZE)
1003                         goto out_elf_end;
1004
1005                 if (!dso__build_id_equal(self, build_id))
1006                         goto out_elf_end;
1007         }
1008
1009         sec = elf_section_by_name(elf, &ehdr, &shdr, ".symtab", NULL);
1010         if (sec == NULL) {
1011                 sec = elf_section_by_name(elf, &ehdr, &shdr, ".dynsym", NULL);
1012                 if (sec == NULL)
1013                         goto out_elf_end;
1014         }
1015
1016         opdsec = elf_section_by_name(elf, &ehdr, &opdshdr, ".opd", &opdidx);
1017         if (opdsec)
1018                 opddata = elf_rawdata(opdsec, NULL);
1019
1020         syms = elf_getdata(sec, NULL);
1021         if (syms == NULL)
1022                 goto out_elf_end;
1023
1024         sec = elf_getscn(elf, shdr.sh_link);
1025         if (sec == NULL)
1026                 goto out_elf_end;
1027
1028         symstrs = elf_getdata(sec, NULL);
1029         if (symstrs == NULL)
1030                 goto out_elf_end;
1031
1032         sec_strndx = elf_getscn(elf, ehdr.e_shstrndx);
1033         if (sec_strndx == NULL)
1034                 goto out_elf_end;
1035
1036         secstrs = elf_getdata(sec_strndx, NULL);
1037         if (secstrs == NULL)
1038                 goto out_elf_end;
1039
1040         nr_syms = shdr.sh_size / shdr.sh_entsize;
1041
1042         memset(&sym, 0, sizeof(sym));
1043         if (self->kernel == DSO_TYPE_USER) {
1044                 self->adjust_symbols = (ehdr.e_type == ET_EXEC ||
1045                                 elf_section_by_name(elf, &ehdr, &shdr,
1046                                                      ".gnu.prelink_undo",
1047                                                      NULL) != NULL);
1048         } else self->adjust_symbols = 0;
1049
1050         elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) {
1051                 struct symbol *f;
1052                 const char *elf_name = elf_sym__name(&sym, symstrs);
1053                 char *demangled = NULL;
1054                 int is_label = elf_sym__is_label(&sym);
1055                 const char *section_name;
1056
1057                 if (kmap && kmap->ref_reloc_sym && kmap->ref_reloc_sym->name &&
1058                     strcmp(elf_name, kmap->ref_reloc_sym->name) == 0)
1059                         kmap->ref_reloc_sym->unrelocated_addr = sym.st_value;
1060
1061                 if (!is_label && !elf_sym__is_a(&sym, map->type))
1062                         continue;
1063
1064                 if (opdsec && sym.st_shndx == opdidx) {
1065                         u32 offset = sym.st_value - opdshdr.sh_addr;
1066                         u64 *opd = opddata->d_buf + offset;
1067                         sym.st_value = *opd;
1068                         sym.st_shndx = elf_addr_to_index(elf, sym.st_value);
1069                 }
1070
1071                 sec = elf_getscn(elf, sym.st_shndx);
1072                 if (!sec)
1073                         goto out_elf_end;
1074
1075                 gelf_getshdr(sec, &shdr);
1076
1077                 if (is_label && !elf_sec__is_a(&shdr, secstrs, map->type))
1078                         continue;
1079
1080                 section_name = elf_sec__name(&shdr, secstrs);
1081
1082                 if (self->kernel != DSO_TYPE_USER || kmodule) {
1083                         char dso_name[PATH_MAX];
1084
1085                         if (strcmp(section_name,
1086                                    (curr_dso->short_name +
1087                                     self->short_name_len)) == 0)
1088                                 goto new_symbol;
1089
1090                         if (strcmp(section_name, ".text") == 0) {
1091                                 curr_map = map;
1092                                 curr_dso = self;
1093                                 goto new_symbol;
1094                         }
1095
1096                         snprintf(dso_name, sizeof(dso_name),
1097                                  "%s%s", self->short_name, section_name);
1098
1099                         curr_map = map_groups__find_by_name(kmap->kmaps, map->type, dso_name);
1100                         if (curr_map == NULL) {
1101                                 u64 start = sym.st_value;
1102
1103                                 if (kmodule)
1104                                         start += map->start + shdr.sh_offset;
1105
1106                                 curr_dso = dso__new(dso_name);
1107                                 if (curr_dso == NULL)
1108                                         goto out_elf_end;
1109                                 curr_dso->kernel = self->kernel;
1110                                 curr_map = map__new2(start, curr_dso,
1111                                                      map->type);
1112                                 if (curr_map == NULL) {
1113                                         dso__delete(curr_dso);
1114                                         goto out_elf_end;
1115                                 }
1116                                 curr_map->map_ip = identity__map_ip;
1117                                 curr_map->unmap_ip = identity__map_ip;
1118                                 curr_dso->origin = self->origin;
1119                                 map_groups__insert(kmap->kmaps, curr_map);
1120                                 dsos__add(&self->node, curr_dso);
1121                                 dso__set_loaded(curr_dso, map->type);
1122                         } else
1123                                 curr_dso = curr_map->dso;
1124
1125                         goto new_symbol;
1126                 }
1127
1128                 if (curr_dso->adjust_symbols) {
1129                         pr_debug4("%s: adjusting symbol: st_value: %#Lx "
1130                                   "sh_addr: %#Lx sh_offset: %#Lx\n", __func__,
1131                                   (u64)sym.st_value, (u64)shdr.sh_addr,
1132                                   (u64)shdr.sh_offset);
1133                         sym.st_value -= shdr.sh_addr - shdr.sh_offset;
1134                 }
1135                 /*
1136                  * We need to figure out if the object was created from C++ sources
1137                  * DWARF DW_compile_unit has this, but we don't always have access
1138                  * to it...
1139                  */
1140                 demangled = bfd_demangle(NULL, elf_name, DMGL_PARAMS | DMGL_ANSI);
1141                 if (demangled != NULL)
1142                         elf_name = demangled;
1143 new_symbol:
1144                 f = symbol__new(sym.st_value, sym.st_size, elf_name);
1145                 free(demangled);
1146                 if (!f)
1147                         goto out_elf_end;
1148
1149                 if (filter && filter(curr_map, f))
1150                         symbol__delete(f);
1151                 else {
1152                         symbols__insert(&curr_dso->symbols[curr_map->type], f);
1153                         nr++;
1154                 }
1155         }
1156
1157         /*
1158          * For misannotated, zeroed, ASM function sizes.
1159          */
1160         if (nr > 0) {
1161                 symbols__fixup_end(&self->symbols[map->type]);
1162                 if (kmap) {
1163                         /*
1164                          * We need to fixup this here too because we create new
1165                          * maps here, for things like vsyscall sections.
1166                          */
1167                         __map_groups__fixup_end(kmap->kmaps, map->type);
1168                 }
1169         }
1170         err = nr;
1171 out_elf_end:
1172         elf_end(elf);
1173 out_close:
1174         return err;
1175 }
1176
1177 static bool dso__build_id_equal(const struct dso *self, u8 *build_id)
1178 {
1179         return memcmp(self->build_id, build_id, sizeof(self->build_id)) == 0;
1180 }
1181
1182 bool __dsos__read_build_ids(struct list_head *head, bool with_hits)
1183 {
1184         bool have_build_id = false;
1185         struct dso *pos;
1186
1187         list_for_each_entry(pos, head, node) {
1188                 if (with_hits && !pos->hit)
1189                         continue;
1190                 if (pos->has_build_id) {
1191                         have_build_id = true;
1192                         continue;
1193                 }
1194                 if (filename__read_build_id(pos->long_name, pos->build_id,
1195                                             sizeof(pos->build_id)) > 0) {
1196                         have_build_id     = true;
1197                         pos->has_build_id = true;
1198                 }
1199         }
1200
1201         return have_build_id;
1202 }
1203
1204 /*
1205  * Align offset to 4 bytes as needed for note name and descriptor data.
1206  */
1207 #define NOTE_ALIGN(n) (((n) + 3) & -4U)
1208
1209 static int elf_read_build_id(Elf *elf, void *bf, size_t size)
1210 {
1211         int err = -1;
1212         GElf_Ehdr ehdr;
1213         GElf_Shdr shdr;
1214         Elf_Data *data;
1215         Elf_Scn *sec;
1216         Elf_Kind ek;
1217         void *ptr;
1218
1219         if (size < BUILD_ID_SIZE)
1220                 goto out;
1221
1222         ek = elf_kind(elf);
1223         if (ek != ELF_K_ELF)
1224                 goto out;
1225
1226         if (gelf_getehdr(elf, &ehdr) == NULL) {
1227                 pr_err("%s: cannot get elf header.\n", __func__);
1228                 goto out;
1229         }
1230
1231         sec = elf_section_by_name(elf, &ehdr, &shdr,
1232                                   ".note.gnu.build-id", NULL);
1233         if (sec == NULL) {
1234                 sec = elf_section_by_name(elf, &ehdr, &shdr,
1235                                           ".notes", NULL);
1236                 if (sec == NULL)
1237                         goto out;
1238         }
1239
1240         data = elf_getdata(sec, NULL);
1241         if (data == NULL)
1242                 goto out;
1243
1244         ptr = data->d_buf;
1245         while (ptr < (data->d_buf + data->d_size)) {
1246                 GElf_Nhdr *nhdr = ptr;
1247                 int namesz = NOTE_ALIGN(nhdr->n_namesz),
1248                     descsz = NOTE_ALIGN(nhdr->n_descsz);
1249                 const char *name;
1250
1251                 ptr += sizeof(*nhdr);
1252                 name = ptr;
1253                 ptr += namesz;
1254                 if (nhdr->n_type == NT_GNU_BUILD_ID &&
1255                     nhdr->n_namesz == sizeof("GNU")) {
1256                         if (memcmp(name, "GNU", sizeof("GNU")) == 0) {
1257                                 memcpy(bf, ptr, BUILD_ID_SIZE);
1258                                 err = BUILD_ID_SIZE;
1259                                 break;
1260                         }
1261                 }
1262                 ptr += descsz;
1263         }
1264
1265 out:
1266         return err;
1267 }
1268
1269 int filename__read_build_id(const char *filename, void *bf, size_t size)
1270 {
1271         int fd, err = -1;
1272         Elf *elf;
1273
1274         if (size < BUILD_ID_SIZE)
1275                 goto out;
1276
1277         fd = open(filename, O_RDONLY);
1278         if (fd < 0)
1279                 goto out;
1280
1281         elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
1282         if (elf == NULL) {
1283                 pr_debug2("%s: cannot read %s ELF file.\n", __func__, filename);
1284                 goto out_close;
1285         }
1286
1287         err = elf_read_build_id(elf, bf, size);
1288
1289         elf_end(elf);
1290 out_close:
1291         close(fd);
1292 out:
1293         return err;
1294 }
1295
1296 int sysfs__read_build_id(const char *filename, void *build_id, size_t size)
1297 {
1298         int fd, err = -1;
1299
1300         if (size < BUILD_ID_SIZE)
1301                 goto out;
1302
1303         fd = open(filename, O_RDONLY);
1304         if (fd < 0)
1305                 goto out;
1306
1307         while (1) {
1308                 char bf[BUFSIZ];
1309                 GElf_Nhdr nhdr;
1310                 int namesz, descsz;
1311
1312                 if (read(fd, &nhdr, sizeof(nhdr)) != sizeof(nhdr))
1313                         break;
1314
1315                 namesz = NOTE_ALIGN(nhdr.n_namesz);
1316                 descsz = NOTE_ALIGN(nhdr.n_descsz);
1317                 if (nhdr.n_type == NT_GNU_BUILD_ID &&
1318                     nhdr.n_namesz == sizeof("GNU")) {
1319                         if (read(fd, bf, namesz) != namesz)
1320                                 break;
1321                         if (memcmp(bf, "GNU", sizeof("GNU")) == 0) {
1322                                 if (read(fd, build_id,
1323                                     BUILD_ID_SIZE) == BUILD_ID_SIZE) {
1324                                         err = 0;
1325                                         break;
1326                                 }
1327                         } else if (read(fd, bf, descsz) != descsz)
1328                                 break;
1329                 } else {
1330                         int n = namesz + descsz;
1331                         if (read(fd, bf, n) != n)
1332                                 break;
1333                 }
1334         }
1335         close(fd);
1336 out:
1337         return err;
1338 }
1339
1340 char dso__symtab_origin(const struct dso *self)
1341 {
1342         static const char origin[] = {
1343                 [DSO__ORIG_KERNEL] =   'k',
1344                 [DSO__ORIG_JAVA_JIT] = 'j',
1345                 [DSO__ORIG_BUILD_ID_CACHE] = 'B',
1346                 [DSO__ORIG_FEDORA] =   'f',
1347                 [DSO__ORIG_UBUNTU] =   'u',
1348                 [DSO__ORIG_BUILDID] =  'b',
1349                 [DSO__ORIG_DSO] =      'd',
1350                 [DSO__ORIG_KMODULE] =  'K',
1351                 [DSO__ORIG_GUEST_KERNEL] =  'g',
1352                 [DSO__ORIG_GUEST_KMODULE] =  'G',
1353         };
1354
1355         if (self == NULL || self->origin == DSO__ORIG_NOT_FOUND)
1356                 return '!';
1357         return origin[self->origin];
1358 }
1359
1360 int dso__load(struct dso *self, struct map *map, symbol_filter_t filter)
1361 {
1362         int size = PATH_MAX;
1363         char *name;
1364         int ret = -1;
1365         int fd;
1366         struct machine *machine;
1367         const char *root_dir;
1368
1369         dso__set_loaded(self, map->type);
1370
1371         if (self->kernel == DSO_TYPE_KERNEL)
1372                 return dso__load_kernel_sym(self, map, filter);
1373         else if (self->kernel == DSO_TYPE_GUEST_KERNEL)
1374                 return dso__load_guest_kernel_sym(self, map, filter);
1375
1376         if (map->groups && map->groups->machine)
1377                 machine = map->groups->machine;
1378         else
1379                 machine = NULL;
1380
1381         name = malloc(size);
1382         if (!name)
1383                 return -1;
1384
1385         self->adjust_symbols = 0;
1386
1387         if (strncmp(self->name, "/tmp/perf-", 10) == 0) {
1388                 ret = dso__load_perf_map(self, map, filter);
1389                 self->origin = ret > 0 ? DSO__ORIG_JAVA_JIT :
1390                                          DSO__ORIG_NOT_FOUND;
1391                 return ret;
1392         }
1393
1394         self->origin = DSO__ORIG_BUILD_ID_CACHE;
1395         if (dso__build_id_filename(self, name, size) != NULL)
1396                 goto open_file;
1397 more:
1398         do {
1399                 self->origin++;
1400                 switch (self->origin) {
1401                 case DSO__ORIG_FEDORA:
1402                         snprintf(name, size, "/usr/lib/debug%s.debug",
1403                                  self->long_name);
1404                         break;
1405                 case DSO__ORIG_UBUNTU:
1406                         snprintf(name, size, "/usr/lib/debug%s",
1407                                  self->long_name);
1408                         break;
1409                 case DSO__ORIG_BUILDID:
1410                         if (self->has_build_id) {
1411                                 char build_id_hex[BUILD_ID_SIZE * 2 + 1];
1412                                 build_id__sprintf(self->build_id,
1413                                                   sizeof(self->build_id),
1414                                                   build_id_hex);
1415                                 snprintf(name, size,
1416                                          "/usr/lib/debug/.build-id/%.2s/%s.debug",
1417                                         build_id_hex, build_id_hex + 2);
1418                                 break;
1419                         }
1420                         self->origin++;
1421                         /* Fall thru */
1422                 case DSO__ORIG_DSO:
1423                         snprintf(name, size, "%s", self->long_name);
1424                         break;
1425                 case DSO__ORIG_GUEST_KMODULE:
1426                         if (map->groups && map->groups->machine)
1427                                 root_dir = map->groups->machine->root_dir;
1428                         else
1429                                 root_dir = "";
1430                         snprintf(name, size, "%s%s", root_dir, self->long_name);
1431                         break;
1432
1433                 default:
1434                         goto out;
1435                 }
1436 open_file:
1437                 fd = open(name, O_RDONLY);
1438         } while (fd < 0);
1439
1440         ret = dso__load_sym(self, map, name, fd, filter, 0);
1441         close(fd);
1442
1443         /*
1444          * Some people seem to have debuginfo files _WITHOUT_ debug info!?!?
1445          */
1446         if (!ret)
1447                 goto more;
1448
1449         if (ret > 0) {
1450                 int nr_plt = dso__synthesize_plt_symbols(self, map, filter);
1451                 if (nr_plt > 0)
1452                         ret += nr_plt;
1453         }
1454 out:
1455         free(name);
1456         if (ret < 0 && strstr(self->name, " (deleted)") != NULL)
1457                 return 0;
1458         return ret;
1459 }
1460
1461 struct map *map_groups__find_by_name(struct map_groups *self,
1462                                      enum map_type type, const char *name)
1463 {
1464         struct rb_node *nd;
1465
1466         for (nd = rb_first(&self->maps[type]); nd; nd = rb_next(nd)) {
1467                 struct map *map = rb_entry(nd, struct map, rb_node);
1468
1469                 if (map->dso && strcmp(map->dso->short_name, name) == 0)
1470                         return map;
1471         }
1472
1473         return NULL;
1474 }
1475
1476 static int dso__kernel_module_get_build_id(struct dso *self,
1477                                 const char *root_dir)
1478 {
1479         char filename[PATH_MAX];
1480         /*
1481          * kernel module short names are of the form "[module]" and
1482          * we need just "module" here.
1483          */
1484         const char *name = self->short_name + 1;
1485
1486         snprintf(filename, sizeof(filename),
1487                  "%s/sys/module/%.*s/notes/.note.gnu.build-id",
1488                  root_dir, (int)strlen(name) - 1, name);
1489
1490         if (sysfs__read_build_id(filename, self->build_id,
1491                                  sizeof(self->build_id)) == 0)
1492                 self->has_build_id = true;
1493
1494         return 0;
1495 }
1496
1497 static int map_groups__set_modules_path_dir(struct map_groups *self,
1498                                 const char *dir_name)
1499 {
1500         struct dirent *dent;
1501         DIR *dir = opendir(dir_name);
1502
1503         if (!dir) {
1504                 pr_debug("%s: cannot open %s dir\n", __func__, dir_name);
1505                 return -1;
1506         }
1507
1508         while ((dent = readdir(dir)) != NULL) {
1509                 char path[PATH_MAX];
1510                 struct stat st;
1511
1512                 /*sshfs might return bad dent->d_type, so we have to stat*/
1513                 sprintf(path, "%s/%s", dir_name, dent->d_name);
1514                 if (stat(path, &st))
1515                         continue;
1516
1517                 if (S_ISDIR(st.st_mode)) {
1518                         if (!strcmp(dent->d_name, ".") ||
1519                             !strcmp(dent->d_name, ".."))
1520                                 continue;
1521
1522                         snprintf(path, sizeof(path), "%s/%s",
1523                                  dir_name, dent->d_name);
1524                         if (map_groups__set_modules_path_dir(self, path) < 0)
1525                                 goto failure;
1526                 } else {
1527                         char *dot = strrchr(dent->d_name, '.'),
1528                              dso_name[PATH_MAX];
1529                         struct map *map;
1530                         char *long_name;
1531
1532                         if (dot == NULL || strcmp(dot, ".ko"))
1533                                 continue;
1534                         snprintf(dso_name, sizeof(dso_name), "[%.*s]",
1535                                  (int)(dot - dent->d_name), dent->d_name);
1536
1537                         strxfrchar(dso_name, '-', '_');
1538                         map = map_groups__find_by_name(self, MAP__FUNCTION, dso_name);
1539                         if (map == NULL)
1540                                 continue;
1541
1542                         snprintf(path, sizeof(path), "%s/%s",
1543                                  dir_name, dent->d_name);
1544
1545                         long_name = strdup(path);
1546                         if (long_name == NULL)
1547                                 goto failure;
1548                         dso__set_long_name(map->dso, long_name);
1549                         map->dso->lname_alloc = 1;
1550                         dso__kernel_module_get_build_id(map->dso, "");
1551                 }
1552         }
1553
1554         return 0;
1555 failure:
1556         closedir(dir);
1557         return -1;
1558 }
1559
1560 static char *get_kernel_version(const char *root_dir)
1561 {
1562         char version[PATH_MAX];
1563         FILE *file;
1564         char *name, *tmp;
1565         const char *prefix = "Linux version ";
1566
1567         sprintf(version, "%s/proc/version", root_dir);
1568         file = fopen(version, "r");
1569         if (!file)
1570                 return NULL;
1571
1572         version[0] = '\0';
1573         tmp = fgets(version, sizeof(version), file);
1574         fclose(file);
1575
1576         name = strstr(version, prefix);
1577         if (!name)
1578                 return NULL;
1579         name += strlen(prefix);
1580         tmp = strchr(name, ' ');
1581         if (tmp)
1582                 *tmp = '\0';
1583
1584         return strdup(name);
1585 }
1586
1587 static int machine__set_modules_path(struct machine *self)
1588 {
1589         char *version;
1590         char modules_path[PATH_MAX];
1591
1592         version = get_kernel_version(self->root_dir);
1593         if (!version)
1594                 return -1;
1595
1596         snprintf(modules_path, sizeof(modules_path), "%s/lib/modules/%s/kernel",
1597                  self->root_dir, version);
1598         free(version);
1599
1600         return map_groups__set_modules_path_dir(&self->kmaps, modules_path);
1601 }
1602
1603 /*
1604  * Constructor variant for modules (where we know from /proc/modules where
1605  * they are loaded) and for vmlinux, where only after we load all the
1606  * symbols we'll know where it starts and ends.
1607  */
1608 static struct map *map__new2(u64 start, struct dso *dso, enum map_type type)
1609 {
1610         struct map *self = calloc(1, (sizeof(*self) +
1611                                       (dso->kernel ? sizeof(struct kmap) : 0)));
1612         if (self != NULL) {
1613                 /*
1614                  * ->end will be filled after we load all the symbols
1615                  */
1616                 map__init(self, type, start, 0, 0, dso);
1617         }
1618
1619         return self;
1620 }
1621
1622 struct map *machine__new_module(struct machine *self, u64 start,
1623                                 const char *filename)
1624 {
1625         struct map *map;
1626         struct dso *dso = __dsos__findnew(&self->kernel_dsos, filename);
1627
1628         if (dso == NULL)
1629                 return NULL;
1630
1631         map = map__new2(start, dso, MAP__FUNCTION);
1632         if (map == NULL)
1633                 return NULL;
1634
1635         if (machine__is_host(self))
1636                 dso->origin = DSO__ORIG_KMODULE;
1637         else
1638                 dso->origin = DSO__ORIG_GUEST_KMODULE;
1639         map_groups__insert(&self->kmaps, map);
1640         return map;
1641 }
1642
1643 static int machine__create_modules(struct machine *self)
1644 {
1645         char *line = NULL;
1646         size_t n;
1647         FILE *file;
1648         struct map *map;
1649         const char *modules;
1650         char path[PATH_MAX];
1651
1652         if (machine__is_default_guest(self))
1653                 modules = symbol_conf.default_guest_modules;
1654         else {
1655                 sprintf(path, "%s/proc/modules", self->root_dir);
1656                 modules = path;
1657         }
1658
1659         file = fopen(modules, "r");
1660         if (file == NULL)
1661                 return -1;
1662
1663         while (!feof(file)) {
1664                 char name[PATH_MAX];
1665                 u64 start;
1666                 char *sep;
1667                 int line_len;
1668
1669                 line_len = getline(&line, &n, file);
1670                 if (line_len < 0)
1671                         break;
1672
1673                 if (!line)
1674                         goto out_failure;
1675
1676                 line[--line_len] = '\0'; /* \n */
1677
1678                 sep = strrchr(line, 'x');
1679                 if (sep == NULL)
1680                         continue;
1681
1682                 hex2u64(sep + 1, &start);
1683
1684                 sep = strchr(line, ' ');
1685                 if (sep == NULL)
1686                         continue;
1687
1688                 *sep = '\0';
1689
1690                 snprintf(name, sizeof(name), "[%s]", line);
1691                 map = machine__new_module(self, start, name);
1692                 if (map == NULL)
1693                         goto out_delete_line;
1694                 dso__kernel_module_get_build_id(map->dso, self->root_dir);
1695         }
1696
1697         free(line);
1698         fclose(file);
1699
1700         return machine__set_modules_path(self);
1701
1702 out_delete_line:
1703         free(line);
1704 out_failure:
1705         return -1;
1706 }
1707
1708 static int dso__load_vmlinux(struct dso *self, struct map *map,
1709                              const char *vmlinux, symbol_filter_t filter)
1710 {
1711         int err = -1, fd;
1712
1713         if (self->has_build_id) {
1714                 u8 build_id[BUILD_ID_SIZE];
1715
1716                 if (filename__read_build_id(vmlinux, build_id,
1717                                             sizeof(build_id)) < 0) {
1718                         pr_debug("No build_id in %s, ignoring it\n", vmlinux);
1719                         return -1;
1720                 }
1721                 if (!dso__build_id_equal(self, build_id)) {
1722                         char expected_build_id[BUILD_ID_SIZE * 2 + 1],
1723                              vmlinux_build_id[BUILD_ID_SIZE * 2 + 1];
1724
1725                         build_id__sprintf(self->build_id,
1726                                           sizeof(self->build_id),
1727                                           expected_build_id);
1728                         build_id__sprintf(build_id, sizeof(build_id),
1729                                           vmlinux_build_id);
1730                         pr_debug("build_id in %s is %s while expected is %s, "
1731                                  "ignoring it\n", vmlinux, vmlinux_build_id,
1732                                  expected_build_id);
1733                         return -1;
1734                 }
1735         }
1736
1737         fd = open(vmlinux, O_RDONLY);
1738         if (fd < 0)
1739                 return -1;
1740
1741         dso__set_loaded(self, map->type);
1742         err = dso__load_sym(self, map, vmlinux, fd, filter, 0);
1743         close(fd);
1744
1745         if (err > 0)
1746                 pr_debug("Using %s for symbols\n", vmlinux);
1747
1748         return err;
1749 }
1750
1751 int dso__load_vmlinux_path(struct dso *self, struct map *map,
1752                            symbol_filter_t filter)
1753 {
1754         int i, err = 0;
1755         char *filename;
1756
1757         pr_debug("Looking at the vmlinux_path (%d entries long)\n",
1758                  vmlinux_path__nr_entries + 1);
1759
1760         filename = dso__build_id_filename(self, NULL, 0);
1761         if (filename != NULL) {
1762                 err = dso__load_vmlinux(self, map, filename, filter);
1763                 if (err > 0) {
1764                         dso__set_long_name(self, filename);
1765                         goto out;
1766                 }
1767                 free(filename);
1768         }
1769
1770         for (i = 0; i < vmlinux_path__nr_entries; ++i) {
1771                 err = dso__load_vmlinux(self, map, vmlinux_path[i], filter);
1772                 if (err > 0) {
1773                         dso__set_long_name(self, strdup(vmlinux_path[i]));
1774                         break;
1775                 }
1776         }
1777 out:
1778         return err;
1779 }
1780
1781 static int dso__load_kernel_sym(struct dso *self, struct map *map,
1782                                 symbol_filter_t filter)
1783 {
1784         int err;
1785         const char *kallsyms_filename = NULL;
1786         char *kallsyms_allocated_filename = NULL;
1787         /*
1788          * Step 1: if the user specified a vmlinux filename, use it and only
1789          * it, reporting errors to the user if it cannot be used.
1790          *
1791          * For instance, try to analyse an ARM perf.data file _without_ a
1792          * build-id, or if the user specifies the wrong path to the right
1793          * vmlinux file, obviously we can't fallback to another vmlinux (a
1794          * x86_86 one, on the machine where analysis is being performed, say),
1795          * or worse, /proc/kallsyms.
1796          *
1797          * If the specified file _has_ a build-id and there is a build-id
1798          * section in the perf.data file, we will still do the expected
1799          * validation in dso__load_vmlinux and will bail out if they don't
1800          * match.
1801          */
1802         if (symbol_conf.vmlinux_name != NULL) {
1803                 err = dso__load_vmlinux(self, map,
1804                                         symbol_conf.vmlinux_name, filter);
1805                 if (err > 0) {
1806                         dso__set_long_name(self,
1807                                            strdup(symbol_conf.vmlinux_name));
1808                         goto out_fixup;
1809                 }
1810                 return err;
1811         }
1812
1813         if (vmlinux_path != NULL) {
1814                 err = dso__load_vmlinux_path(self, map, filter);
1815                 if (err > 0)
1816                         goto out_fixup;
1817         }
1818
1819         /*
1820          * Say the kernel DSO was created when processing the build-id header table,
1821          * we have a build-id, so check if it is the same as the running kernel,
1822          * using it if it is.
1823          */
1824         if (self->has_build_id) {
1825                 u8 kallsyms_build_id[BUILD_ID_SIZE];
1826                 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
1827
1828                 if (sysfs__read_build_id("/sys/kernel/notes", kallsyms_build_id,
1829                                          sizeof(kallsyms_build_id)) == 0) {
1830                         if (dso__build_id_equal(self, kallsyms_build_id)) {
1831                                 kallsyms_filename = "/proc/kallsyms";
1832                                 goto do_kallsyms;
1833                         }
1834                 }
1835                 /*
1836                  * Now look if we have it on the build-id cache in
1837                  * $HOME/.debug/[kernel.kallsyms].
1838                  */
1839                 build_id__sprintf(self->build_id, sizeof(self->build_id),
1840                                   sbuild_id);
1841
1842                 if (asprintf(&kallsyms_allocated_filename,
1843                              "%s/.debug/[kernel.kallsyms]/%s",
1844                              getenv("HOME"), sbuild_id) == -1) {
1845                         pr_err("Not enough memory for kallsyms file lookup\n");
1846                         return -1;
1847                 }
1848
1849                 kallsyms_filename = kallsyms_allocated_filename;
1850
1851                 if (access(kallsyms_filename, F_OK)) {
1852                         pr_err("No kallsyms or vmlinux with build-id %s "
1853                                "was found\n", sbuild_id);
1854                         free(kallsyms_allocated_filename);
1855                         return -1;
1856                 }
1857         } else {
1858                 /*
1859                  * Last resort, if we don't have a build-id and couldn't find
1860                  * any vmlinux file, try the running kernel kallsyms table.
1861                  */
1862                 kallsyms_filename = "/proc/kallsyms";
1863         }
1864
1865 do_kallsyms:
1866         err = dso__load_kallsyms(self, kallsyms_filename, map, filter);
1867         if (err > 0)
1868                 pr_debug("Using %s for symbols\n", kallsyms_filename);
1869         free(kallsyms_allocated_filename);
1870
1871         if (err > 0) {
1872 out_fixup:
1873                 if (kallsyms_filename != NULL)
1874                         dso__set_long_name(self, strdup("[kernel.kallsyms]"));
1875                 map__fixup_start(map);
1876                 map__fixup_end(map);
1877         }
1878
1879         return err;
1880 }
1881
1882 static int dso__load_guest_kernel_sym(struct dso *self, struct map *map,
1883                                 symbol_filter_t filter)
1884 {
1885         int err;
1886         const char *kallsyms_filename = NULL;
1887         struct machine *machine;
1888         char path[PATH_MAX];
1889
1890         if (!map->groups) {
1891                 pr_debug("Guest kernel map hasn't the point to groups\n");
1892                 return -1;
1893         }
1894         machine = map->groups->machine;
1895
1896         if (machine__is_default_guest(machine)) {
1897                 /*
1898                  * if the user specified a vmlinux filename, use it and only
1899                  * it, reporting errors to the user if it cannot be used.
1900                  * Or use file guest_kallsyms inputted by user on commandline
1901                  */
1902                 if (symbol_conf.default_guest_vmlinux_name != NULL) {
1903                         err = dso__load_vmlinux(self, map,
1904                                 symbol_conf.default_guest_vmlinux_name, filter);
1905                         goto out_try_fixup;
1906                 }
1907
1908                 kallsyms_filename = symbol_conf.default_guest_kallsyms;
1909                 if (!kallsyms_filename)
1910                         return -1;
1911         } else {
1912                 sprintf(path, "%s/proc/kallsyms", machine->root_dir);
1913                 kallsyms_filename = path;
1914         }
1915
1916         err = dso__load_kallsyms(self, kallsyms_filename, map, filter);
1917         if (err > 0)
1918                 pr_debug("Using %s for symbols\n", kallsyms_filename);
1919
1920 out_try_fixup:
1921         if (err > 0) {
1922                 if (kallsyms_filename != NULL) {
1923                         machine__mmap_name(machine, path, sizeof(path));
1924                         dso__set_long_name(self, strdup(path));
1925                 }
1926                 map__fixup_start(map);
1927                 map__fixup_end(map);
1928         }
1929
1930         return err;
1931 }
1932
1933 static void dsos__add(struct list_head *head, struct dso *dso)
1934 {
1935         list_add_tail(&dso->node, head);
1936 }
1937
1938 static struct dso *dsos__find(struct list_head *head, const char *name)
1939 {
1940         struct dso *pos;
1941
1942         list_for_each_entry(pos, head, node)
1943                 if (strcmp(pos->long_name, name) == 0)
1944                         return pos;
1945         return NULL;
1946 }
1947
1948 struct dso *__dsos__findnew(struct list_head *head, const char *name)
1949 {
1950         struct dso *dso = dsos__find(head, name);
1951
1952         if (!dso) {
1953                 dso = dso__new(name);
1954                 if (dso != NULL) {
1955                         dsos__add(head, dso);
1956                         dso__set_basename(dso);
1957                 }
1958         }
1959
1960         return dso;
1961 }
1962
1963 size_t __dsos__fprintf(struct list_head *head, FILE *fp)
1964 {
1965         struct dso *pos;
1966         size_t ret = 0;
1967
1968         list_for_each_entry(pos, head, node) {
1969                 int i;
1970                 for (i = 0; i < MAP__NR_TYPES; ++i)
1971                         ret += dso__fprintf(pos, i, fp);
1972         }
1973
1974         return ret;
1975 }
1976
1977 size_t machines__fprintf_dsos(struct rb_root *self, FILE *fp)
1978 {
1979         struct rb_node *nd;
1980         size_t ret = 0;
1981
1982         for (nd = rb_first(self); nd; nd = rb_next(nd)) {
1983                 struct machine *pos = rb_entry(nd, struct machine, rb_node);
1984                 ret += __dsos__fprintf(&pos->kernel_dsos, fp);
1985                 ret += __dsos__fprintf(&pos->user_dsos, fp);
1986         }
1987
1988         return ret;
1989 }
1990
1991 static size_t __dsos__fprintf_buildid(struct list_head *head, FILE *fp,
1992                                       bool with_hits)
1993 {
1994         struct dso *pos;
1995         size_t ret = 0;
1996
1997         list_for_each_entry(pos, head, node) {
1998                 if (with_hits && !pos->hit)
1999                         continue;
2000                 ret += dso__fprintf_buildid(pos, fp);
2001                 ret += fprintf(fp, " %s\n", pos->long_name);
2002         }
2003         return ret;
2004 }
2005
2006 size_t machine__fprintf_dsos_buildid(struct machine *self, FILE *fp, bool with_hits)
2007 {
2008         return __dsos__fprintf_buildid(&self->kernel_dsos, fp, with_hits) +
2009                __dsos__fprintf_buildid(&self->user_dsos, fp, with_hits);
2010 }
2011
2012 size_t machines__fprintf_dsos_buildid(struct rb_root *self, FILE *fp, bool with_hits)
2013 {
2014         struct rb_node *nd;
2015         size_t ret = 0;
2016
2017         for (nd = rb_first(self); nd; nd = rb_next(nd)) {
2018                 struct machine *pos = rb_entry(nd, struct machine, rb_node);
2019                 ret += machine__fprintf_dsos_buildid(pos, fp, with_hits);
2020         }
2021         return ret;
2022 }
2023
2024 struct dso *dso__new_kernel(const char *name)
2025 {
2026         struct dso *self = dso__new(name ?: "[kernel.kallsyms]");
2027
2028         if (self != NULL) {
2029                 dso__set_short_name(self, "[kernel]");
2030                 self->kernel = DSO_TYPE_KERNEL;
2031         }
2032
2033         return self;
2034 }
2035
2036 static struct dso *dso__new_guest_kernel(struct machine *machine,
2037                                         const char *name)
2038 {
2039         char bf[PATH_MAX];
2040         struct dso *self = dso__new(name ?: machine__mmap_name(machine, bf, sizeof(bf)));
2041
2042         if (self != NULL) {
2043                 dso__set_short_name(self, "[guest.kernel]");
2044                 self->kernel = DSO_TYPE_GUEST_KERNEL;
2045         }
2046
2047         return self;
2048 }
2049
2050 void dso__read_running_kernel_build_id(struct dso *self, struct machine *machine)
2051 {
2052         char path[PATH_MAX];
2053
2054         if (machine__is_default_guest(machine))
2055                 return;
2056         sprintf(path, "%s/sys/kernel/notes", machine->root_dir);
2057         if (sysfs__read_build_id(path, self->build_id,
2058                                  sizeof(self->build_id)) == 0)
2059                 self->has_build_id = true;
2060 }
2061
2062 static struct dso *machine__create_kernel(struct machine *self)
2063 {
2064         const char *vmlinux_name = NULL;
2065         struct dso *kernel;
2066
2067         if (machine__is_host(self)) {
2068                 vmlinux_name = symbol_conf.vmlinux_name;
2069                 kernel = dso__new_kernel(vmlinux_name);
2070         } else {
2071                 if (machine__is_default_guest(self))
2072                         vmlinux_name = symbol_conf.default_guest_vmlinux_name;
2073                 kernel = dso__new_guest_kernel(self, vmlinux_name);
2074         }
2075
2076         if (kernel != NULL) {
2077                 dso__read_running_kernel_build_id(kernel, self);
2078                 dsos__add(&self->kernel_dsos, kernel);
2079         }
2080         return kernel;
2081 }
2082
2083 int __machine__create_kernel_maps(struct machine *self, struct dso *kernel)
2084 {
2085         enum map_type type;
2086
2087         for (type = 0; type < MAP__NR_TYPES; ++type) {
2088                 struct kmap *kmap;
2089
2090                 self->vmlinux_maps[type] = map__new2(0, kernel, type);
2091                 if (self->vmlinux_maps[type] == NULL)
2092                         return -1;
2093
2094                 self->vmlinux_maps[type]->map_ip =
2095                         self->vmlinux_maps[type]->unmap_ip = identity__map_ip;
2096
2097                 kmap = map__kmap(self->vmlinux_maps[type]);
2098                 kmap->kmaps = &self->kmaps;
2099                 map_groups__insert(&self->kmaps, self->vmlinux_maps[type]);
2100         }
2101
2102         return 0;
2103 }
2104
2105 int machine__create_kernel_maps(struct machine *self)
2106 {
2107         struct dso *kernel = machine__create_kernel(self);
2108
2109         if (kernel == NULL ||
2110             __machine__create_kernel_maps(self, kernel) < 0)
2111                 return -1;
2112
2113         if (symbol_conf.use_modules && machine__create_modules(self) < 0)
2114                 pr_debug("Problems creating module maps, continuing anyway...\n");
2115         /*
2116          * Now that we have all the maps created, just set the ->end of them:
2117          */
2118         map_groups__fixup_end(&self->kmaps);
2119         return 0;
2120 }
2121
2122 static void vmlinux_path__exit(void)
2123 {
2124         while (--vmlinux_path__nr_entries >= 0) {
2125                 free(vmlinux_path[vmlinux_path__nr_entries]);
2126                 vmlinux_path[vmlinux_path__nr_entries] = NULL;
2127         }
2128
2129         free(vmlinux_path);
2130         vmlinux_path = NULL;
2131 }
2132
2133 static int vmlinux_path__init(void)
2134 {
2135         struct utsname uts;
2136         char bf[PATH_MAX];
2137
2138         if (uname(&uts) < 0)
2139                 return -1;
2140
2141         vmlinux_path = malloc(sizeof(char *) * 5);
2142         if (vmlinux_path == NULL)
2143                 return -1;
2144
2145         vmlinux_path[vmlinux_path__nr_entries] = strdup("vmlinux");
2146         if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
2147                 goto out_fail;
2148         ++vmlinux_path__nr_entries;
2149         vmlinux_path[vmlinux_path__nr_entries] = strdup("/boot/vmlinux");
2150         if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
2151                 goto out_fail;
2152         ++vmlinux_path__nr_entries;
2153         snprintf(bf, sizeof(bf), "/boot/vmlinux-%s", uts.release);
2154         vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
2155         if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
2156                 goto out_fail;
2157         ++vmlinux_path__nr_entries;
2158         snprintf(bf, sizeof(bf), "/lib/modules/%s/build/vmlinux", uts.release);
2159         vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
2160         if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
2161                 goto out_fail;
2162         ++vmlinux_path__nr_entries;
2163         snprintf(bf, sizeof(bf), "/usr/lib/debug/lib/modules/%s/vmlinux",
2164                  uts.release);
2165         vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
2166         if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
2167                 goto out_fail;
2168         ++vmlinux_path__nr_entries;
2169
2170         return 0;
2171
2172 out_fail:
2173         vmlinux_path__exit();
2174         return -1;
2175 }
2176
2177 size_t machine__fprintf_vmlinux_path(struct machine *self, FILE *fp)
2178 {
2179         int i;
2180         size_t printed = 0;
2181         struct dso *kdso = self->vmlinux_maps[MAP__FUNCTION]->dso;
2182
2183         if (kdso->has_build_id) {
2184                 char filename[PATH_MAX];
2185                 if (dso__build_id_filename(kdso, filename, sizeof(filename)))
2186                         printed += fprintf(fp, "[0] %s\n", filename);
2187         }
2188
2189         for (i = 0; i < vmlinux_path__nr_entries; ++i)
2190                 printed += fprintf(fp, "[%d] %s\n",
2191                                    i + kdso->has_build_id, vmlinux_path[i]);
2192
2193         return printed;
2194 }
2195
2196 static int setup_list(struct strlist **list, const char *list_str,
2197                       const char *list_name)
2198 {
2199         if (list_str == NULL)
2200                 return 0;
2201
2202         *list = strlist__new(true, list_str);
2203         if (!*list) {
2204                 pr_err("problems parsing %s list\n", list_name);
2205                 return -1;
2206         }
2207         return 0;
2208 }
2209
2210 int symbol__init(void)
2211 {
2212         elf_version(EV_CURRENT);
2213         if (symbol_conf.sort_by_name)
2214                 symbol_conf.priv_size += (sizeof(struct symbol_name_rb_node) -
2215                                           sizeof(struct symbol));
2216
2217         if (symbol_conf.try_vmlinux_path && vmlinux_path__init() < 0)
2218                 return -1;
2219
2220         if (symbol_conf.field_sep && *symbol_conf.field_sep == '.') {
2221                 pr_err("'.' is the only non valid --field-separator argument\n");
2222                 return -1;
2223         }
2224
2225         if (setup_list(&symbol_conf.dso_list,
2226                        symbol_conf.dso_list_str, "dso") < 0)
2227                 return -1;
2228
2229         if (setup_list(&symbol_conf.comm_list,
2230                        symbol_conf.comm_list_str, "comm") < 0)
2231                 goto out_free_dso_list;
2232
2233         if (setup_list(&symbol_conf.sym_list,
2234                        symbol_conf.sym_list_str, "symbol") < 0)
2235                 goto out_free_comm_list;
2236
2237         return 0;
2238
2239 out_free_dso_list:
2240         strlist__delete(symbol_conf.dso_list);
2241 out_free_comm_list:
2242         strlist__delete(symbol_conf.comm_list);
2243         return -1;
2244 }
2245
2246 int machines__create_kernel_maps(struct rb_root *self, pid_t pid)
2247 {
2248         struct machine *machine = machines__findnew(self, pid);
2249
2250         if (machine == NULL)
2251                 return -1;
2252
2253         return machine__create_kernel_maps(machine);
2254 }
2255
2256 static int hex(char ch)
2257 {
2258         if ((ch >= '0') && (ch <= '9'))
2259                 return ch - '0';
2260         if ((ch >= 'a') && (ch <= 'f'))
2261                 return ch - 'a' + 10;
2262         if ((ch >= 'A') && (ch <= 'F'))
2263                 return ch - 'A' + 10;
2264         return -1;
2265 }
2266
2267 /*
2268  * While we find nice hex chars, build a long_val.
2269  * Return number of chars processed.
2270  */
2271 int hex2u64(const char *ptr, u64 *long_val)
2272 {
2273         const char *p = ptr;
2274         *long_val = 0;
2275
2276         while (*p) {
2277                 const int hex_val = hex(*p);
2278
2279                 if (hex_val < 0)
2280                         break;
2281
2282                 *long_val = (*long_val << 4) | hex_val;
2283                 p++;
2284         }
2285
2286         return p - ptr;
2287 }
2288
2289 char *strxfrchar(char *s, char from, char to)
2290 {
2291         char *p = s;
2292
2293         while ((p = strchr(p, from)) != NULL)
2294                 *p++ = to;
2295
2296         return s;
2297 }
2298
2299 int machines__create_guest_kernel_maps(struct rb_root *self)
2300 {
2301         int ret = 0;
2302         struct dirent **namelist = NULL;
2303         int i, items = 0;
2304         char path[PATH_MAX];
2305         pid_t pid;
2306
2307         if (symbol_conf.default_guest_vmlinux_name ||
2308             symbol_conf.default_guest_modules ||
2309             symbol_conf.default_guest_kallsyms) {
2310                 machines__create_kernel_maps(self, DEFAULT_GUEST_KERNEL_ID);
2311         }
2312
2313         if (symbol_conf.guestmount) {
2314                 items = scandir(symbol_conf.guestmount, &namelist, NULL, NULL);
2315                 if (items <= 0)
2316                         return -ENOENT;
2317                 for (i = 0; i < items; i++) {
2318                         if (!isdigit(namelist[i]->d_name[0])) {
2319                                 /* Filter out . and .. */
2320                                 continue;
2321                         }
2322                         pid = atoi(namelist[i]->d_name);
2323                         sprintf(path, "%s/%s/proc/kallsyms",
2324                                 symbol_conf.guestmount,
2325                                 namelist[i]->d_name);
2326                         ret = access(path, R_OK);
2327                         if (ret) {
2328                                 pr_debug("Can't access file %s\n", path);
2329                                 goto failure;
2330                         }
2331                         machines__create_kernel_maps(self, pid);
2332                 }
2333 failure:
2334                 free(namelist);
2335         }
2336
2337         return ret;
2338 }
2339
2340 int machine__load_kallsyms(struct machine *self, const char *filename,
2341                            enum map_type type, symbol_filter_t filter)
2342 {
2343         struct map *map = self->vmlinux_maps[type];
2344         int ret = dso__load_kallsyms(map->dso, filename, map, filter);
2345
2346         if (ret > 0) {
2347                 dso__set_loaded(map->dso, type);
2348                 /*
2349                  * Since /proc/kallsyms will have multiple sessions for the
2350                  * kernel, with modules between them, fixup the end of all
2351                  * sections.
2352                  */
2353                 __map_groups__fixup_end(&self->kmaps, type);
2354         }
2355
2356         return ret;
2357 }
2358
2359 int machine__load_vmlinux_path(struct machine *self, enum map_type type,
2360                                symbol_filter_t filter)
2361 {
2362         struct map *map = self->vmlinux_maps[type];
2363         int ret = dso__load_vmlinux_path(map->dso, map, filter);
2364
2365         if (ret > 0) {
2366                 dso__set_loaded(map->dso, type);
2367                 map__reloc_vmlinux(map);
2368         }
2369
2370         return ret;
2371 }