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