perf kvm: Use strtol for walking guestmount directory
[pandora-kernel.git] / tools / perf / util / symbol.c
1 #include <dirent.h>
2 #include <errno.h>
3 #include <stdlib.h>
4 #include <stdio.h>
5 #include <string.h>
6 #include <sys/types.h>
7 #include <sys/stat.h>
8 #include <sys/param.h>
9 #include <fcntl.h>
10 #include <unistd.h>
11 #include <inttypes.h>
12 #include "build-id.h"
13 #include "util.h"
14 #include "debug.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 KSYM_NAME_LEN
25 #define KSYM_NAME_LEN 256
26 #endif
27
28 #ifndef NT_GNU_BUILD_ID
29 #define NT_GNU_BUILD_ID 3
30 #endif
31
32 static void dso_cache__free(struct rb_root *root);
33 static bool dso__build_id_equal(const struct dso *dso, u8 *build_id);
34 static int elf_read_build_id(Elf *elf, void *bf, size_t size);
35 static void dsos__add(struct list_head *head, struct dso *dso);
36 static struct map *map__new2(u64 start, struct dso *dso, enum map_type type);
37 static int dso__load_kernel_sym(struct dso *dso, struct map *map,
38                                 symbol_filter_t filter);
39 static int dso__load_guest_kernel_sym(struct dso *dso, struct map *map,
40                         symbol_filter_t filter);
41 static int vmlinux_path__nr_entries;
42 static char **vmlinux_path;
43
44 struct symbol_conf symbol_conf = {
45         .exclude_other    = true,
46         .use_modules      = true,
47         .try_vmlinux_path = true,
48         .annotate_src     = true,
49         .symfs            = "",
50 };
51
52 static enum dso_binary_type binary_type_symtab[] = {
53         DSO_BINARY_TYPE__KALLSYMS,
54         DSO_BINARY_TYPE__GUEST_KALLSYMS,
55         DSO_BINARY_TYPE__JAVA_JIT,
56         DSO_BINARY_TYPE__DEBUGLINK,
57         DSO_BINARY_TYPE__BUILD_ID_CACHE,
58         DSO_BINARY_TYPE__FEDORA_DEBUGINFO,
59         DSO_BINARY_TYPE__UBUNTU_DEBUGINFO,
60         DSO_BINARY_TYPE__BUILDID_DEBUGINFO,
61         DSO_BINARY_TYPE__SYSTEM_PATH_DSO,
62         DSO_BINARY_TYPE__GUEST_KMODULE,
63         DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE,
64         DSO_BINARY_TYPE__NOT_FOUND,
65 };
66
67 #define DSO_BINARY_TYPE__SYMTAB_CNT ARRAY_SIZE(binary_type_symtab)
68
69 static enum dso_binary_type binary_type_data[] = {
70         DSO_BINARY_TYPE__BUILD_ID_CACHE,
71         DSO_BINARY_TYPE__SYSTEM_PATH_DSO,
72         DSO_BINARY_TYPE__NOT_FOUND,
73 };
74
75 #define DSO_BINARY_TYPE__DATA_CNT ARRAY_SIZE(binary_type_data)
76
77 int dso__name_len(const struct dso *dso)
78 {
79         if (!dso)
80                 return strlen("[unknown]");
81         if (verbose)
82                 return dso->long_name_len;
83
84         return dso->short_name_len;
85 }
86
87 bool dso__loaded(const struct dso *dso, enum map_type type)
88 {
89         return dso->loaded & (1 << type);
90 }
91
92 bool dso__sorted_by_name(const struct dso *dso, enum map_type type)
93 {
94         return dso->sorted_by_name & (1 << type);
95 }
96
97 static void dso__set_sorted_by_name(struct dso *dso, enum map_type type)
98 {
99         dso->sorted_by_name |= (1 << type);
100 }
101
102 bool symbol_type__is_a(char symbol_type, enum map_type map_type)
103 {
104         symbol_type = toupper(symbol_type);
105
106         switch (map_type) {
107         case MAP__FUNCTION:
108                 return symbol_type == 'T' || symbol_type == 'W';
109         case MAP__VARIABLE:
110                 return symbol_type == 'D';
111         default:
112                 return false;
113         }
114 }
115
116 static int prefix_underscores_count(const char *str)
117 {
118         const char *tail = str;
119
120         while (*tail == '_')
121                 tail++;
122
123         return tail - str;
124 }
125
126 #define SYMBOL_A 0
127 #define SYMBOL_B 1
128
129 static int choose_best_symbol(struct symbol *syma, struct symbol *symb)
130 {
131         s64 a;
132         s64 b;
133
134         /* Prefer a symbol with non zero length */
135         a = syma->end - syma->start;
136         b = symb->end - symb->start;
137         if ((b == 0) && (a > 0))
138                 return SYMBOL_A;
139         else if ((a == 0) && (b > 0))
140                 return SYMBOL_B;
141
142         /* Prefer a non weak symbol over a weak one */
143         a = syma->binding == STB_WEAK;
144         b = symb->binding == STB_WEAK;
145         if (b && !a)
146                 return SYMBOL_A;
147         if (a && !b)
148                 return SYMBOL_B;
149
150         /* Prefer a global symbol over a non global one */
151         a = syma->binding == STB_GLOBAL;
152         b = symb->binding == STB_GLOBAL;
153         if (a && !b)
154                 return SYMBOL_A;
155         if (b && !a)
156                 return SYMBOL_B;
157
158         /* Prefer a symbol with less underscores */
159         a = prefix_underscores_count(syma->name);
160         b = prefix_underscores_count(symb->name);
161         if (b > a)
162                 return SYMBOL_A;
163         else if (a > b)
164                 return SYMBOL_B;
165
166         /* If all else fails, choose the symbol with the longest name */
167         if (strlen(syma->name) >= strlen(symb->name))
168                 return SYMBOL_A;
169         else
170                 return SYMBOL_B;
171 }
172
173 static void symbols__fixup_duplicate(struct rb_root *symbols)
174 {
175         struct rb_node *nd;
176         struct symbol *curr, *next;
177
178         nd = rb_first(symbols);
179
180         while (nd) {
181                 curr = rb_entry(nd, struct symbol, rb_node);
182 again:
183                 nd = rb_next(&curr->rb_node);
184                 next = rb_entry(nd, struct symbol, rb_node);
185
186                 if (!nd)
187                         break;
188
189                 if (curr->start != next->start)
190                         continue;
191
192                 if (choose_best_symbol(curr, next) == SYMBOL_A) {
193                         rb_erase(&next->rb_node, symbols);
194                         goto again;
195                 } else {
196                         nd = rb_next(&curr->rb_node);
197                         rb_erase(&curr->rb_node, symbols);
198                 }
199         }
200 }
201
202 static void symbols__fixup_end(struct rb_root *symbols)
203 {
204         struct rb_node *nd, *prevnd = rb_first(symbols);
205         struct symbol *curr, *prev;
206
207         if (prevnd == NULL)
208                 return;
209
210         curr = rb_entry(prevnd, struct symbol, rb_node);
211
212         for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
213                 prev = curr;
214                 curr = rb_entry(nd, struct symbol, rb_node);
215
216                 if (prev->end == prev->start && prev->end != curr->start)
217                         prev->end = curr->start - 1;
218         }
219
220         /* Last entry */
221         if (curr->end == curr->start)
222                 curr->end = roundup(curr->start, 4096);
223 }
224
225 static void __map_groups__fixup_end(struct map_groups *mg, enum map_type type)
226 {
227         struct map *prev, *curr;
228         struct rb_node *nd, *prevnd = rb_first(&mg->maps[type]);
229
230         if (prevnd == NULL)
231                 return;
232
233         curr = rb_entry(prevnd, struct map, rb_node);
234
235         for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
236                 prev = curr;
237                 curr = rb_entry(nd, struct map, rb_node);
238                 prev->end = curr->start - 1;
239         }
240
241         /*
242          * We still haven't the actual symbols, so guess the
243          * last map final address.
244          */
245         curr->end = ~0ULL;
246 }
247
248 static void map_groups__fixup_end(struct map_groups *mg)
249 {
250         int i;
251         for (i = 0; i < MAP__NR_TYPES; ++i)
252                 __map_groups__fixup_end(mg, i);
253 }
254
255 static struct symbol *symbol__new(u64 start, u64 len, u8 binding,
256                                   const char *name)
257 {
258         size_t namelen = strlen(name) + 1;
259         struct symbol *sym = calloc(1, (symbol_conf.priv_size +
260                                         sizeof(*sym) + namelen));
261         if (sym == NULL)
262                 return NULL;
263
264         if (symbol_conf.priv_size)
265                 sym = ((void *)sym) + symbol_conf.priv_size;
266
267         sym->start   = start;
268         sym->end     = len ? start + len - 1 : start;
269         sym->binding = binding;
270         sym->namelen = namelen - 1;
271
272         pr_debug4("%s: %s %#" PRIx64 "-%#" PRIx64 "\n",
273                   __func__, name, start, sym->end);
274         memcpy(sym->name, name, namelen);
275
276         return sym;
277 }
278
279 void symbol__delete(struct symbol *sym)
280 {
281         free(((void *)sym) - symbol_conf.priv_size);
282 }
283
284 static size_t symbol__fprintf(struct symbol *sym, FILE *fp)
285 {
286         return fprintf(fp, " %" PRIx64 "-%" PRIx64 " %c %s\n",
287                        sym->start, sym->end,
288                        sym->binding == STB_GLOBAL ? 'g' :
289                        sym->binding == STB_LOCAL  ? 'l' : 'w',
290                        sym->name);
291 }
292
293 size_t symbol__fprintf_symname_offs(const struct symbol *sym,
294                                     const struct addr_location *al, FILE *fp)
295 {
296         unsigned long offset;
297         size_t length;
298
299         if (sym && sym->name) {
300                 length = fprintf(fp, "%s", sym->name);
301                 if (al) {
302                         offset = al->addr - sym->start;
303                         length += fprintf(fp, "+0x%lx", offset);
304                 }
305                 return length;
306         } else
307                 return fprintf(fp, "[unknown]");
308 }
309
310 size_t symbol__fprintf_symname(const struct symbol *sym, FILE *fp)
311 {
312         return symbol__fprintf_symname_offs(sym, NULL, fp);
313 }
314
315 void dso__set_long_name(struct dso *dso, char *name)
316 {
317         if (name == NULL)
318                 return;
319         dso->long_name = name;
320         dso->long_name_len = strlen(name);
321 }
322
323 static void dso__set_short_name(struct dso *dso, const char *name)
324 {
325         if (name == NULL)
326                 return;
327         dso->short_name = name;
328         dso->short_name_len = strlen(name);
329 }
330
331 static void dso__set_basename(struct dso *dso)
332 {
333         dso__set_short_name(dso, basename(dso->long_name));
334 }
335
336 struct dso *dso__new(const char *name)
337 {
338         struct dso *dso = calloc(1, sizeof(*dso) + strlen(name) + 1);
339
340         if (dso != NULL) {
341                 int i;
342                 strcpy(dso->name, name);
343                 dso__set_long_name(dso, dso->name);
344                 dso__set_short_name(dso, dso->name);
345                 for (i = 0; i < MAP__NR_TYPES; ++i)
346                         dso->symbols[i] = dso->symbol_names[i] = RB_ROOT;
347                 dso->cache = RB_ROOT;
348                 dso->symtab_type = DSO_BINARY_TYPE__NOT_FOUND;
349                 dso->data_type   = DSO_BINARY_TYPE__NOT_FOUND;
350                 dso->loaded = 0;
351                 dso->sorted_by_name = 0;
352                 dso->has_build_id = 0;
353                 dso->kernel = DSO_TYPE_USER;
354                 dso->needs_swap = DSO_SWAP__UNSET;
355                 INIT_LIST_HEAD(&dso->node);
356         }
357
358         return dso;
359 }
360
361 static void symbols__delete(struct rb_root *symbols)
362 {
363         struct symbol *pos;
364         struct rb_node *next = rb_first(symbols);
365
366         while (next) {
367                 pos = rb_entry(next, struct symbol, rb_node);
368                 next = rb_next(&pos->rb_node);
369                 rb_erase(&pos->rb_node, symbols);
370                 symbol__delete(pos);
371         }
372 }
373
374 void dso__delete(struct dso *dso)
375 {
376         int i;
377         for (i = 0; i < MAP__NR_TYPES; ++i)
378                 symbols__delete(&dso->symbols[i]);
379         if (dso->sname_alloc)
380                 free((char *)dso->short_name);
381         if (dso->lname_alloc)
382                 free(dso->long_name);
383         dso_cache__free(&dso->cache);
384         free(dso);
385 }
386
387 void dso__set_build_id(struct dso *dso, void *build_id)
388 {
389         memcpy(dso->build_id, build_id, sizeof(dso->build_id));
390         dso->has_build_id = 1;
391 }
392
393 static void symbols__insert(struct rb_root *symbols, struct symbol *sym)
394 {
395         struct rb_node **p = &symbols->rb_node;
396         struct rb_node *parent = NULL;
397         const u64 ip = sym->start;
398         struct symbol *s;
399
400         while (*p != NULL) {
401                 parent = *p;
402                 s = rb_entry(parent, struct symbol, rb_node);
403                 if (ip < s->start)
404                         p = &(*p)->rb_left;
405                 else
406                         p = &(*p)->rb_right;
407         }
408         rb_link_node(&sym->rb_node, parent, p);
409         rb_insert_color(&sym->rb_node, symbols);
410 }
411
412 static struct symbol *symbols__find(struct rb_root *symbols, u64 ip)
413 {
414         struct rb_node *n;
415
416         if (symbols == NULL)
417                 return NULL;
418
419         n = symbols->rb_node;
420
421         while (n) {
422                 struct symbol *s = rb_entry(n, struct symbol, rb_node);
423
424                 if (ip < s->start)
425                         n = n->rb_left;
426                 else if (ip > s->end)
427                         n = n->rb_right;
428                 else
429                         return s;
430         }
431
432         return NULL;
433 }
434
435 struct symbol_name_rb_node {
436         struct rb_node  rb_node;
437         struct symbol   sym;
438 };
439
440 static void symbols__insert_by_name(struct rb_root *symbols, struct symbol *sym)
441 {
442         struct rb_node **p = &symbols->rb_node;
443         struct rb_node *parent = NULL;
444         struct symbol_name_rb_node *symn, *s;
445
446         symn = container_of(sym, struct symbol_name_rb_node, sym);
447
448         while (*p != NULL) {
449                 parent = *p;
450                 s = rb_entry(parent, struct symbol_name_rb_node, rb_node);
451                 if (strcmp(sym->name, s->sym.name) < 0)
452                         p = &(*p)->rb_left;
453                 else
454                         p = &(*p)->rb_right;
455         }
456         rb_link_node(&symn->rb_node, parent, p);
457         rb_insert_color(&symn->rb_node, symbols);
458 }
459
460 static void symbols__sort_by_name(struct rb_root *symbols,
461                                   struct rb_root *source)
462 {
463         struct rb_node *nd;
464
465         for (nd = rb_first(source); nd; nd = rb_next(nd)) {
466                 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
467                 symbols__insert_by_name(symbols, pos);
468         }
469 }
470
471 static struct symbol *symbols__find_by_name(struct rb_root *symbols,
472                                             const char *name)
473 {
474         struct rb_node *n;
475
476         if (symbols == NULL)
477                 return NULL;
478
479         n = symbols->rb_node;
480
481         while (n) {
482                 struct symbol_name_rb_node *s;
483                 int cmp;
484
485                 s = rb_entry(n, struct symbol_name_rb_node, rb_node);
486                 cmp = strcmp(name, s->sym.name);
487
488                 if (cmp < 0)
489                         n = n->rb_left;
490                 else if (cmp > 0)
491                         n = n->rb_right;
492                 else
493                         return &s->sym;
494         }
495
496         return NULL;
497 }
498
499 struct symbol *dso__find_symbol(struct dso *dso,
500                                 enum map_type type, u64 addr)
501 {
502         return symbols__find(&dso->symbols[type], addr);
503 }
504
505 struct symbol *dso__find_symbol_by_name(struct dso *dso, enum map_type type,
506                                         const char *name)
507 {
508         return symbols__find_by_name(&dso->symbol_names[type], name);
509 }
510
511 void dso__sort_by_name(struct dso *dso, enum map_type type)
512 {
513         dso__set_sorted_by_name(dso, type);
514         return symbols__sort_by_name(&dso->symbol_names[type],
515                                      &dso->symbols[type]);
516 }
517
518 int build_id__sprintf(const u8 *build_id, int len, char *bf)
519 {
520         char *bid = bf;
521         const u8 *raw = build_id;
522         int i;
523
524         for (i = 0; i < len; ++i) {
525                 sprintf(bid, "%02x", *raw);
526                 ++raw;
527                 bid += 2;
528         }
529
530         return raw - build_id;
531 }
532
533 size_t dso__fprintf_buildid(struct dso *dso, FILE *fp)
534 {
535         char sbuild_id[BUILD_ID_SIZE * 2 + 1];
536
537         build_id__sprintf(dso->build_id, sizeof(dso->build_id), sbuild_id);
538         return fprintf(fp, "%s", sbuild_id);
539 }
540
541 size_t dso__fprintf_symbols_by_name(struct dso *dso,
542                                     enum map_type type, FILE *fp)
543 {
544         size_t ret = 0;
545         struct rb_node *nd;
546         struct symbol_name_rb_node *pos;
547
548         for (nd = rb_first(&dso->symbol_names[type]); nd; nd = rb_next(nd)) {
549                 pos = rb_entry(nd, struct symbol_name_rb_node, rb_node);
550                 fprintf(fp, "%s\n", pos->sym.name);
551         }
552
553         return ret;
554 }
555
556 size_t dso__fprintf(struct dso *dso, enum map_type type, FILE *fp)
557 {
558         struct rb_node *nd;
559         size_t ret = fprintf(fp, "dso: %s (", dso->short_name);
560
561         if (dso->short_name != dso->long_name)
562                 ret += fprintf(fp, "%s, ", dso->long_name);
563         ret += fprintf(fp, "%s, %sloaded, ", map_type__name[type],
564                        dso->loaded ? "" : "NOT ");
565         ret += dso__fprintf_buildid(dso, fp);
566         ret += fprintf(fp, ")\n");
567         for (nd = rb_first(&dso->symbols[type]); nd; nd = rb_next(nd)) {
568                 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
569                 ret += symbol__fprintf(pos, fp);
570         }
571
572         return ret;
573 }
574
575 int kallsyms__parse(const char *filename, void *arg,
576                     int (*process_symbol)(void *arg, const char *name,
577                                           char type, u64 start, u64 end))
578 {
579         char *line = NULL;
580         size_t n;
581         int err = -1;
582         FILE *file = fopen(filename, "r");
583
584         if (file == NULL)
585                 goto out_failure;
586
587         err = 0;
588
589         while (!feof(file)) {
590                 u64 start;
591                 int line_len, len;
592                 char symbol_type;
593                 char *symbol_name;
594
595                 line_len = getline(&line, &n, file);
596                 if (line_len < 0 || !line)
597                         break;
598
599                 line[--line_len] = '\0'; /* \n */
600
601                 len = hex2u64(line, &start);
602
603                 len++;
604                 if (len + 2 >= line_len)
605                         continue;
606
607                 symbol_type = line[len];
608                 len += 2;
609                 symbol_name = line + len;
610                 len = line_len - len;
611
612                 if (len >= KSYM_NAME_LEN) {
613                         err = -1;
614                         break;
615                 }
616
617                 /*
618                  * module symbols are not sorted so we add all
619                  * symbols with zero length and rely on
620                  * symbols__fixup_end() to fix it up.
621                  */
622                 err = process_symbol(arg, symbol_name,
623                                      symbol_type, start, start);
624                 if (err)
625                         break;
626         }
627
628         free(line);
629         fclose(file);
630         return err;
631
632 out_failure:
633         return -1;
634 }
635
636 struct process_kallsyms_args {
637         struct map *map;
638         struct dso *dso;
639 };
640
641 static u8 kallsyms2elf_type(char type)
642 {
643         if (type == 'W')
644                 return STB_WEAK;
645
646         return isupper(type) ? STB_GLOBAL : STB_LOCAL;
647 }
648
649 static int map__process_kallsym_symbol(void *arg, const char *name,
650                                        char type, u64 start, u64 end)
651 {
652         struct symbol *sym;
653         struct process_kallsyms_args *a = arg;
654         struct rb_root *root = &a->dso->symbols[a->map->type];
655
656         if (!symbol_type__is_a(type, a->map->type))
657                 return 0;
658
659         sym = symbol__new(start, end - start + 1,
660                           kallsyms2elf_type(type), name);
661         if (sym == NULL)
662                 return -ENOMEM;
663         /*
664          * We will pass the symbols to the filter later, in
665          * map__split_kallsyms, when we have split the maps per module
666          */
667         symbols__insert(root, sym);
668
669         return 0;
670 }
671
672 /*
673  * Loads the function entries in /proc/kallsyms into kernel_map->dso,
674  * so that we can in the next step set the symbol ->end address and then
675  * call kernel_maps__split_kallsyms.
676  */
677 static int dso__load_all_kallsyms(struct dso *dso, const char *filename,
678                                   struct map *map)
679 {
680         struct process_kallsyms_args args = { .map = map, .dso = dso, };
681         return kallsyms__parse(filename, &args, map__process_kallsym_symbol);
682 }
683
684 /*
685  * Split the symbols into maps, making sure there are no overlaps, i.e. the
686  * kernel range is broken in several maps, named [kernel].N, as we don't have
687  * the original ELF section names vmlinux have.
688  */
689 static int dso__split_kallsyms(struct dso *dso, struct map *map,
690                                symbol_filter_t filter)
691 {
692         struct map_groups *kmaps = map__kmap(map)->kmaps;
693         struct machine *machine = kmaps->machine;
694         struct map *curr_map = map;
695         struct symbol *pos;
696         int count = 0, moved = 0;       
697         struct rb_root *root = &dso->symbols[map->type];
698         struct rb_node *next = rb_first(root);
699         int kernel_range = 0;
700
701         while (next) {
702                 char *module;
703
704                 pos = rb_entry(next, struct symbol, rb_node);
705                 next = rb_next(&pos->rb_node);
706
707                 module = strchr(pos->name, '\t');
708                 if (module) {
709                         if (!symbol_conf.use_modules)
710                                 goto discard_symbol;
711
712                         *module++ = '\0';
713
714                         if (strcmp(curr_map->dso->short_name, module)) {
715                                 if (curr_map != map &&
716                                     dso->kernel == DSO_TYPE_GUEST_KERNEL &&
717                                     machine__is_default_guest(machine)) {
718                                         /*
719                                          * We assume all symbols of a module are
720                                          * continuous in * kallsyms, so curr_map
721                                          * points to a module and all its
722                                          * symbols are in its kmap. Mark it as
723                                          * loaded.
724                                          */
725                                         dso__set_loaded(curr_map->dso,
726                                                         curr_map->type);
727                                 }
728
729                                 curr_map = map_groups__find_by_name(kmaps,
730                                                         map->type, module);
731                                 if (curr_map == NULL) {
732                                         pr_debug("%s/proc/{kallsyms,modules} "
733                                                  "inconsistency while looking "
734                                                  "for \"%s\" module!\n",
735                                                  machine->root_dir, module);
736                                         curr_map = map;
737                                         goto discard_symbol;
738                                 }
739
740                                 if (curr_map->dso->loaded &&
741                                     !machine__is_default_guest(machine))
742                                         goto discard_symbol;
743                         }
744                         /*
745                          * So that we look just like we get from .ko files,
746                          * i.e. not prelinked, relative to map->start.
747                          */
748                         pos->start = curr_map->map_ip(curr_map, pos->start);
749                         pos->end   = curr_map->map_ip(curr_map, pos->end);
750                 } else if (curr_map != map) {
751                         char dso_name[PATH_MAX];
752                         struct dso *ndso;
753
754                         if (count == 0) {
755                                 curr_map = map;
756                                 goto filter_symbol;
757                         }
758
759                         if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
760                                 snprintf(dso_name, sizeof(dso_name),
761                                         "[guest.kernel].%d",
762                                         kernel_range++);
763                         else
764                                 snprintf(dso_name, sizeof(dso_name),
765                                         "[kernel].%d",
766                                         kernel_range++);
767
768                         ndso = dso__new(dso_name);
769                         if (ndso == NULL)
770                                 return -1;
771
772                         ndso->kernel = dso->kernel;
773
774                         curr_map = map__new2(pos->start, ndso, map->type);
775                         if (curr_map == NULL) {
776                                 dso__delete(ndso);
777                                 return -1;
778                         }
779
780                         curr_map->map_ip = curr_map->unmap_ip = identity__map_ip;
781                         map_groups__insert(kmaps, curr_map);
782                         ++kernel_range;
783                 }
784 filter_symbol:
785                 if (filter && filter(curr_map, pos)) {
786 discard_symbol:         rb_erase(&pos->rb_node, root);
787                         symbol__delete(pos);
788                 } else {
789                         if (curr_map != map) {
790                                 rb_erase(&pos->rb_node, root);
791                                 symbols__insert(&curr_map->dso->symbols[curr_map->type], pos);
792                                 ++moved;
793                         } else
794                                 ++count;
795                 }
796         }
797
798         if (curr_map != map &&
799             dso->kernel == DSO_TYPE_GUEST_KERNEL &&
800             machine__is_default_guest(kmaps->machine)) {
801                 dso__set_loaded(curr_map->dso, curr_map->type);
802         }
803
804         return count + moved;
805 }
806
807 static bool symbol__restricted_filename(const char *filename,
808                                         const char *restricted_filename)
809 {
810         bool restricted = false;
811
812         if (symbol_conf.kptr_restrict) {
813                 char *r = realpath(filename, NULL);
814
815                 if (r != NULL) {
816                         restricted = strcmp(r, restricted_filename) == 0;
817                         free(r);
818                         return restricted;
819                 }
820         }
821
822         return restricted;
823 }
824
825 int dso__load_kallsyms(struct dso *dso, const char *filename,
826                        struct map *map, symbol_filter_t filter)
827 {
828         if (symbol__restricted_filename(filename, "/proc/kallsyms"))
829                 return -1;
830
831         if (dso__load_all_kallsyms(dso, filename, map) < 0)
832                 return -1;
833
834         symbols__fixup_duplicate(&dso->symbols[map->type]);
835         symbols__fixup_end(&dso->symbols[map->type]);
836
837         if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
838                 dso->symtab_type = DSO_BINARY_TYPE__GUEST_KALLSYMS;
839         else
840                 dso->symtab_type = DSO_BINARY_TYPE__KALLSYMS;
841
842         return dso__split_kallsyms(dso, map, filter);
843 }
844
845 static int dso__load_perf_map(struct dso *dso, struct map *map,
846                               symbol_filter_t filter)
847 {
848         char *line = NULL;
849         size_t n;
850         FILE *file;
851         int nr_syms = 0;
852
853         file = fopen(dso->long_name, "r");
854         if (file == NULL)
855                 goto out_failure;
856
857         while (!feof(file)) {
858                 u64 start, size;
859                 struct symbol *sym;
860                 int line_len, len;
861
862                 line_len = getline(&line, &n, file);
863                 if (line_len < 0)
864                         break;
865
866                 if (!line)
867                         goto out_failure;
868
869                 line[--line_len] = '\0'; /* \n */
870
871                 len = hex2u64(line, &start);
872
873                 len++;
874                 if (len + 2 >= line_len)
875                         continue;
876
877                 len += hex2u64(line + len, &size);
878
879                 len++;
880                 if (len + 2 >= line_len)
881                         continue;
882
883                 sym = symbol__new(start, size, STB_GLOBAL, line + len);
884
885                 if (sym == NULL)
886                         goto out_delete_line;
887
888                 if (filter && filter(map, sym))
889                         symbol__delete(sym);
890                 else {
891                         symbols__insert(&dso->symbols[map->type], sym);
892                         nr_syms++;
893                 }
894         }
895
896         free(line);
897         fclose(file);
898
899         return nr_syms;
900
901 out_delete_line:
902         free(line);
903 out_failure:
904         return -1;
905 }
906
907 /**
908  * elf_symtab__for_each_symbol - iterate thru all the symbols
909  *
910  * @syms: struct elf_symtab instance to iterate
911  * @idx: uint32_t idx
912  * @sym: GElf_Sym iterator
913  */
914 #define elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) \
915         for (idx = 0, gelf_getsym(syms, idx, &sym);\
916              idx < nr_syms; \
917              idx++, gelf_getsym(syms, idx, &sym))
918
919 static inline uint8_t elf_sym__type(const GElf_Sym *sym)
920 {
921         return GELF_ST_TYPE(sym->st_info);
922 }
923
924 static inline int elf_sym__is_function(const GElf_Sym *sym)
925 {
926         return elf_sym__type(sym) == STT_FUNC &&
927                sym->st_name != 0 &&
928                sym->st_shndx != SHN_UNDEF;
929 }
930
931 static inline bool elf_sym__is_object(const GElf_Sym *sym)
932 {
933         return elf_sym__type(sym) == STT_OBJECT &&
934                 sym->st_name != 0 &&
935                 sym->st_shndx != SHN_UNDEF;
936 }
937
938 static inline int elf_sym__is_label(const GElf_Sym *sym)
939 {
940         return elf_sym__type(sym) == STT_NOTYPE &&
941                 sym->st_name != 0 &&
942                 sym->st_shndx != SHN_UNDEF &&
943                 sym->st_shndx != SHN_ABS;
944 }
945
946 static inline const char *elf_sec__name(const GElf_Shdr *shdr,
947                                         const Elf_Data *secstrs)
948 {
949         return secstrs->d_buf + shdr->sh_name;
950 }
951
952 static inline int elf_sec__is_text(const GElf_Shdr *shdr,
953                                         const Elf_Data *secstrs)
954 {
955         return strstr(elf_sec__name(shdr, secstrs), "text") != NULL;
956 }
957
958 static inline bool elf_sec__is_data(const GElf_Shdr *shdr,
959                                     const Elf_Data *secstrs)
960 {
961         return strstr(elf_sec__name(shdr, secstrs), "data") != NULL;
962 }
963
964 static inline const char *elf_sym__name(const GElf_Sym *sym,
965                                         const Elf_Data *symstrs)
966 {
967         return symstrs->d_buf + sym->st_name;
968 }
969
970 static Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep,
971                                     GElf_Shdr *shp, const char *name,
972                                     size_t *idx)
973 {
974         Elf_Scn *sec = NULL;
975         size_t cnt = 1;
976
977         while ((sec = elf_nextscn(elf, sec)) != NULL) {
978                 char *str;
979
980                 gelf_getshdr(sec, shp);
981                 str = elf_strptr(elf, ep->e_shstrndx, shp->sh_name);
982                 if (!strcmp(name, str)) {
983                         if (idx)
984                                 *idx = cnt;
985                         break;
986                 }
987                 ++cnt;
988         }
989
990         return sec;
991 }
992
993 #define elf_section__for_each_rel(reldata, pos, pos_mem, idx, nr_entries) \
994         for (idx = 0, pos = gelf_getrel(reldata, 0, &pos_mem); \
995              idx < nr_entries; \
996              ++idx, pos = gelf_getrel(reldata, idx, &pos_mem))
997
998 #define elf_section__for_each_rela(reldata, pos, pos_mem, idx, nr_entries) \
999         for (idx = 0, pos = gelf_getrela(reldata, 0, &pos_mem); \
1000              idx < nr_entries; \
1001              ++idx, pos = gelf_getrela(reldata, idx, &pos_mem))
1002
1003 /*
1004  * We need to check if we have a .dynsym, so that we can handle the
1005  * .plt, synthesizing its symbols, that aren't on the symtabs (be it
1006  * .dynsym or .symtab).
1007  * And always look at the original dso, not at debuginfo packages, that
1008  * have the PLT data stripped out (shdr_rel_plt.sh_type == SHT_NOBITS).
1009  */
1010 static int
1011 dso__synthesize_plt_symbols(struct dso *dso, char *name, struct map *map,
1012                             symbol_filter_t filter)
1013 {
1014         uint32_t nr_rel_entries, idx;
1015         GElf_Sym sym;
1016         u64 plt_offset;
1017         GElf_Shdr shdr_plt;
1018         struct symbol *f;
1019         GElf_Shdr shdr_rel_plt, shdr_dynsym;
1020         Elf_Data *reldata, *syms, *symstrs;
1021         Elf_Scn *scn_plt_rel, *scn_symstrs, *scn_dynsym;
1022         size_t dynsym_idx;
1023         GElf_Ehdr ehdr;
1024         char sympltname[1024];
1025         Elf *elf;
1026         int nr = 0, symidx, fd, err = 0;
1027
1028         fd = open(name, O_RDONLY);
1029         if (fd < 0)
1030                 goto out;
1031
1032         elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
1033         if (elf == NULL)
1034                 goto out_close;
1035
1036         if (gelf_getehdr(elf, &ehdr) == NULL)
1037                 goto out_elf_end;
1038
1039         scn_dynsym = elf_section_by_name(elf, &ehdr, &shdr_dynsym,
1040                                          ".dynsym", &dynsym_idx);
1041         if (scn_dynsym == NULL)
1042                 goto out_elf_end;
1043
1044         scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt,
1045                                           ".rela.plt", NULL);
1046         if (scn_plt_rel == NULL) {
1047                 scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt,
1048                                                   ".rel.plt", NULL);
1049                 if (scn_plt_rel == NULL)
1050                         goto out_elf_end;
1051         }
1052
1053         err = -1;
1054
1055         if (shdr_rel_plt.sh_link != dynsym_idx)
1056                 goto out_elf_end;
1057
1058         if (elf_section_by_name(elf, &ehdr, &shdr_plt, ".plt", NULL) == NULL)
1059                 goto out_elf_end;
1060
1061         /*
1062          * Fetch the relocation section to find the idxes to the GOT
1063          * and the symbols in the .dynsym they refer to.
1064          */
1065         reldata = elf_getdata(scn_plt_rel, NULL);
1066         if (reldata == NULL)
1067                 goto out_elf_end;
1068
1069         syms = elf_getdata(scn_dynsym, NULL);
1070         if (syms == NULL)
1071                 goto out_elf_end;
1072
1073         scn_symstrs = elf_getscn(elf, shdr_dynsym.sh_link);
1074         if (scn_symstrs == NULL)
1075                 goto out_elf_end;
1076
1077         symstrs = elf_getdata(scn_symstrs, NULL);
1078         if (symstrs == NULL)
1079                 goto out_elf_end;
1080
1081         nr_rel_entries = shdr_rel_plt.sh_size / shdr_rel_plt.sh_entsize;
1082         plt_offset = shdr_plt.sh_offset;
1083
1084         if (shdr_rel_plt.sh_type == SHT_RELA) {
1085                 GElf_Rela pos_mem, *pos;
1086
1087                 elf_section__for_each_rela(reldata, pos, pos_mem, idx,
1088                                            nr_rel_entries) {
1089                         symidx = GELF_R_SYM(pos->r_info);
1090                         plt_offset += shdr_plt.sh_entsize;
1091                         gelf_getsym(syms, symidx, &sym);
1092                         snprintf(sympltname, sizeof(sympltname),
1093                                  "%s@plt", elf_sym__name(&sym, symstrs));
1094
1095                         f = symbol__new(plt_offset, shdr_plt.sh_entsize,
1096                                         STB_GLOBAL, sympltname);
1097                         if (!f)
1098                                 goto out_elf_end;
1099
1100                         if (filter && filter(map, f))
1101                                 symbol__delete(f);
1102                         else {
1103                                 symbols__insert(&dso->symbols[map->type], f);
1104                                 ++nr;
1105                         }
1106                 }
1107         } else if (shdr_rel_plt.sh_type == SHT_REL) {
1108                 GElf_Rel pos_mem, *pos;
1109                 elf_section__for_each_rel(reldata, pos, pos_mem, idx,
1110                                           nr_rel_entries) {
1111                         symidx = GELF_R_SYM(pos->r_info);
1112                         plt_offset += shdr_plt.sh_entsize;
1113                         gelf_getsym(syms, symidx, &sym);
1114                         snprintf(sympltname, sizeof(sympltname),
1115                                  "%s@plt", elf_sym__name(&sym, symstrs));
1116
1117                         f = symbol__new(plt_offset, shdr_plt.sh_entsize,
1118                                         STB_GLOBAL, sympltname);
1119                         if (!f)
1120                                 goto out_elf_end;
1121
1122                         if (filter && filter(map, f))
1123                                 symbol__delete(f);
1124                         else {
1125                                 symbols__insert(&dso->symbols[map->type], f);
1126                                 ++nr;
1127                         }
1128                 }
1129         }
1130
1131         err = 0;
1132 out_elf_end:
1133         elf_end(elf);
1134 out_close:
1135         close(fd);
1136
1137         if (err == 0)
1138                 return nr;
1139 out:
1140         pr_debug("%s: problems reading %s PLT info.\n",
1141                  __func__, dso->long_name);
1142         return 0;
1143 }
1144
1145 static bool elf_sym__is_a(GElf_Sym *sym, enum map_type type)
1146 {
1147         switch (type) {
1148         case MAP__FUNCTION:
1149                 return elf_sym__is_function(sym);
1150         case MAP__VARIABLE:
1151                 return elf_sym__is_object(sym);
1152         default:
1153                 return false;
1154         }
1155 }
1156
1157 static bool elf_sec__is_a(GElf_Shdr *shdr, Elf_Data *secstrs,
1158                           enum map_type type)
1159 {
1160         switch (type) {
1161         case MAP__FUNCTION:
1162                 return elf_sec__is_text(shdr, secstrs);
1163         case MAP__VARIABLE:
1164                 return elf_sec__is_data(shdr, secstrs);
1165         default:
1166                 return false;
1167         }
1168 }
1169
1170 static size_t elf_addr_to_index(Elf *elf, GElf_Addr addr)
1171 {
1172         Elf_Scn *sec = NULL;
1173         GElf_Shdr shdr;
1174         size_t cnt = 1;
1175
1176         while ((sec = elf_nextscn(elf, sec)) != NULL) {
1177                 gelf_getshdr(sec, &shdr);
1178
1179                 if ((addr >= shdr.sh_addr) &&
1180                     (addr < (shdr.sh_addr + shdr.sh_size)))
1181                         return cnt;
1182
1183                 ++cnt;
1184         }
1185
1186         return -1;
1187 }
1188
1189 static int dso__swap_init(struct dso *dso, unsigned char eidata)
1190 {
1191         static unsigned int const endian = 1;
1192
1193         dso->needs_swap = DSO_SWAP__NO;
1194
1195         switch (eidata) {
1196         case ELFDATA2LSB:
1197                 /* We are big endian, DSO is little endian. */
1198                 if (*(unsigned char const *)&endian != 1)
1199                         dso->needs_swap = DSO_SWAP__YES;
1200                 break;
1201
1202         case ELFDATA2MSB:
1203                 /* We are little endian, DSO is big endian. */
1204                 if (*(unsigned char const *)&endian != 0)
1205                         dso->needs_swap = DSO_SWAP__YES;
1206                 break;
1207
1208         default:
1209                 pr_err("unrecognized DSO data encoding %d\n", eidata);
1210                 return -EINVAL;
1211         }
1212
1213         return 0;
1214 }
1215
1216 static int dso__load_sym(struct dso *dso, struct map *map, const char *name,
1217                          int fd, symbol_filter_t filter, int kmodule,
1218                          int want_symtab)
1219 {
1220         struct kmap *kmap = dso->kernel ? map__kmap(map) : NULL;
1221         struct map *curr_map = map;
1222         struct dso *curr_dso = dso;
1223         Elf_Data *symstrs, *secstrs;
1224         uint32_t nr_syms;
1225         int err = -1;
1226         uint32_t idx;
1227         GElf_Ehdr ehdr;
1228         GElf_Shdr shdr, opdshdr;
1229         Elf_Data *syms, *opddata = NULL;
1230         GElf_Sym sym;
1231         Elf_Scn *sec, *sec_strndx, *opdsec;
1232         Elf *elf;
1233         int nr = 0;
1234         size_t opdidx = 0;
1235
1236         elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
1237         if (elf == NULL) {
1238                 pr_debug("%s: cannot read %s ELF file.\n", __func__, name);
1239                 goto out_close;
1240         }
1241
1242         if (gelf_getehdr(elf, &ehdr) == NULL) {
1243                 pr_debug("%s: cannot get elf header.\n", __func__);
1244                 goto out_elf_end;
1245         }
1246
1247         if (dso__swap_init(dso, ehdr.e_ident[EI_DATA]))
1248                 goto out_elf_end;
1249
1250         /* Always reject images with a mismatched build-id: */
1251         if (dso->has_build_id) {
1252                 u8 build_id[BUILD_ID_SIZE];
1253
1254                 if (elf_read_build_id(elf, build_id, BUILD_ID_SIZE) < 0)
1255                         goto out_elf_end;
1256
1257                 if (!dso__build_id_equal(dso, build_id))
1258                         goto out_elf_end;
1259         }
1260
1261         sec = elf_section_by_name(elf, &ehdr, &shdr, ".symtab", NULL);
1262         if (sec == NULL) {
1263                 if (want_symtab)
1264                         goto out_elf_end;
1265
1266                 sec = elf_section_by_name(elf, &ehdr, &shdr, ".dynsym", NULL);
1267                 if (sec == NULL)
1268                         goto out_elf_end;
1269         }
1270
1271         opdsec = elf_section_by_name(elf, &ehdr, &opdshdr, ".opd", &opdidx);
1272         if (opdshdr.sh_type != SHT_PROGBITS)
1273                 opdsec = NULL;
1274         if (opdsec)
1275                 opddata = elf_rawdata(opdsec, NULL);
1276
1277         syms = elf_getdata(sec, NULL);
1278         if (syms == NULL)
1279                 goto out_elf_end;
1280
1281         sec = elf_getscn(elf, shdr.sh_link);
1282         if (sec == NULL)
1283                 goto out_elf_end;
1284
1285         symstrs = elf_getdata(sec, NULL);
1286         if (symstrs == NULL)
1287                 goto out_elf_end;
1288
1289         sec_strndx = elf_getscn(elf, ehdr.e_shstrndx);
1290         if (sec_strndx == NULL)
1291                 goto out_elf_end;
1292
1293         secstrs = elf_getdata(sec_strndx, NULL);
1294         if (secstrs == NULL)
1295                 goto out_elf_end;
1296
1297         nr_syms = shdr.sh_size / shdr.sh_entsize;
1298
1299         memset(&sym, 0, sizeof(sym));
1300         if (dso->kernel == DSO_TYPE_USER) {
1301                 dso->adjust_symbols = (ehdr.e_type == ET_EXEC ||
1302                                 elf_section_by_name(elf, &ehdr, &shdr,
1303                                                      ".gnu.prelink_undo",
1304                                                      NULL) != NULL);
1305         } else {
1306                 dso->adjust_symbols = 0;
1307         }
1308         elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) {
1309                 struct symbol *f;
1310                 const char *elf_name = elf_sym__name(&sym, symstrs);
1311                 char *demangled = NULL;
1312                 int is_label = elf_sym__is_label(&sym);
1313                 const char *section_name;
1314
1315                 if (kmap && kmap->ref_reloc_sym && kmap->ref_reloc_sym->name &&
1316                     strcmp(elf_name, kmap->ref_reloc_sym->name) == 0)
1317                         kmap->ref_reloc_sym->unrelocated_addr = sym.st_value;
1318
1319                 if (!is_label && !elf_sym__is_a(&sym, map->type))
1320                         continue;
1321
1322                 /* Reject ARM ELF "mapping symbols": these aren't unique and
1323                  * don't identify functions, so will confuse the profile
1324                  * output: */
1325                 if (ehdr.e_machine == EM_ARM) {
1326                         if (!strcmp(elf_name, "$a") ||
1327                             !strcmp(elf_name, "$d") ||
1328                             !strcmp(elf_name, "$t"))
1329                                 continue;
1330                 }
1331
1332                 if (opdsec && sym.st_shndx == opdidx) {
1333                         u32 offset = sym.st_value - opdshdr.sh_addr;
1334                         u64 *opd = opddata->d_buf + offset;
1335                         sym.st_value = DSO__SWAP(dso, u64, *opd);
1336                         sym.st_shndx = elf_addr_to_index(elf, sym.st_value);
1337                 }
1338
1339                 sec = elf_getscn(elf, sym.st_shndx);
1340                 if (!sec)
1341                         goto out_elf_end;
1342
1343                 gelf_getshdr(sec, &shdr);
1344
1345                 if (is_label && !elf_sec__is_a(&shdr, secstrs, map->type))
1346                         continue;
1347
1348                 section_name = elf_sec__name(&shdr, secstrs);
1349
1350                 /* On ARM, symbols for thumb functions have 1 added to
1351                  * the symbol address as a flag - remove it */
1352                 if ((ehdr.e_machine == EM_ARM) &&
1353                     (map->type == MAP__FUNCTION) &&
1354                     (sym.st_value & 1))
1355                         --sym.st_value;
1356
1357                 if (dso->kernel != DSO_TYPE_USER || kmodule) {
1358                         char dso_name[PATH_MAX];
1359
1360                         if (strcmp(section_name,
1361                                    (curr_dso->short_name +
1362                                     dso->short_name_len)) == 0)
1363                                 goto new_symbol;
1364
1365                         if (strcmp(section_name, ".text") == 0) {
1366                                 curr_map = map;
1367                                 curr_dso = dso;
1368                                 goto new_symbol;
1369                         }
1370
1371                         snprintf(dso_name, sizeof(dso_name),
1372                                  "%s%s", dso->short_name, section_name);
1373
1374                         curr_map = map_groups__find_by_name(kmap->kmaps, map->type, dso_name);
1375                         if (curr_map == NULL) {
1376                                 u64 start = sym.st_value;
1377
1378                                 if (kmodule)
1379                                         start += map->start + shdr.sh_offset;
1380
1381                                 curr_dso = dso__new(dso_name);
1382                                 if (curr_dso == NULL)
1383                                         goto out_elf_end;
1384                                 curr_dso->kernel = dso->kernel;
1385                                 curr_dso->long_name = dso->long_name;
1386                                 curr_dso->long_name_len = dso->long_name_len;
1387                                 curr_map = map__new2(start, curr_dso,
1388                                                      map->type);
1389                                 if (curr_map == NULL) {
1390                                         dso__delete(curr_dso);
1391                                         goto out_elf_end;
1392                                 }
1393                                 curr_map->map_ip = identity__map_ip;
1394                                 curr_map->unmap_ip = identity__map_ip;
1395                                 curr_dso->symtab_type = dso->symtab_type;
1396                                 map_groups__insert(kmap->kmaps, curr_map);
1397                                 dsos__add(&dso->node, curr_dso);
1398                                 dso__set_loaded(curr_dso, map->type);
1399                         } else
1400                                 curr_dso = curr_map->dso;
1401
1402                         goto new_symbol;
1403                 }
1404
1405                 if (curr_dso->adjust_symbols) {
1406                         pr_debug4("%s: adjusting symbol: st_value: %#" PRIx64 " "
1407                                   "sh_addr: %#" PRIx64 " sh_offset: %#" PRIx64 "\n", __func__,
1408                                   (u64)sym.st_value, (u64)shdr.sh_addr,
1409                                   (u64)shdr.sh_offset);
1410                         sym.st_value -= shdr.sh_addr - shdr.sh_offset;
1411                 }
1412                 /*
1413                  * We need to figure out if the object was created from C++ sources
1414                  * DWARF DW_compile_unit has this, but we don't always have access
1415                  * to it...
1416                  */
1417                 demangled = bfd_demangle(NULL, elf_name, DMGL_PARAMS | DMGL_ANSI);
1418                 if (demangled != NULL)
1419                         elf_name = demangled;
1420 new_symbol:
1421                 f = symbol__new(sym.st_value, sym.st_size,
1422                                 GELF_ST_BIND(sym.st_info), elf_name);
1423                 free(demangled);
1424                 if (!f)
1425                         goto out_elf_end;
1426
1427                 if (filter && filter(curr_map, f))
1428                         symbol__delete(f);
1429                 else {
1430                         symbols__insert(&curr_dso->symbols[curr_map->type], f);
1431                         nr++;
1432                 }
1433         }
1434
1435         /*
1436          * For misannotated, zeroed, ASM function sizes.
1437          */
1438         if (nr > 0) {
1439                 symbols__fixup_duplicate(&dso->symbols[map->type]);
1440                 symbols__fixup_end(&dso->symbols[map->type]);
1441                 if (kmap) {
1442                         /*
1443                          * We need to fixup this here too because we create new
1444                          * maps here, for things like vsyscall sections.
1445                          */
1446                         __map_groups__fixup_end(kmap->kmaps, map->type);
1447                 }
1448         }
1449         err = nr;
1450 out_elf_end:
1451         elf_end(elf);
1452 out_close:
1453         return err;
1454 }
1455
1456 static bool dso__build_id_equal(const struct dso *dso, u8 *build_id)
1457 {
1458         return memcmp(dso->build_id, build_id, sizeof(dso->build_id)) == 0;
1459 }
1460
1461 bool __dsos__read_build_ids(struct list_head *head, bool with_hits)
1462 {
1463         bool have_build_id = false;
1464         struct dso *pos;
1465
1466         list_for_each_entry(pos, head, node) {
1467                 if (with_hits && !pos->hit)
1468                         continue;
1469                 if (pos->has_build_id) {
1470                         have_build_id = true;
1471                         continue;
1472                 }
1473                 if (filename__read_build_id(pos->long_name, pos->build_id,
1474                                             sizeof(pos->build_id)) > 0) {
1475                         have_build_id     = true;
1476                         pos->has_build_id = true;
1477                 }
1478         }
1479
1480         return have_build_id;
1481 }
1482
1483 /*
1484  * Align offset to 4 bytes as needed for note name and descriptor data.
1485  */
1486 #define NOTE_ALIGN(n) (((n) + 3) & -4U)
1487
1488 static int elf_read_build_id(Elf *elf, void *bf, size_t size)
1489 {
1490         int err = -1;
1491         GElf_Ehdr ehdr;
1492         GElf_Shdr shdr;
1493         Elf_Data *data;
1494         Elf_Scn *sec;
1495         Elf_Kind ek;
1496         void *ptr;
1497
1498         if (size < BUILD_ID_SIZE)
1499                 goto out;
1500
1501         ek = elf_kind(elf);
1502         if (ek != ELF_K_ELF)
1503                 goto out;
1504
1505         if (gelf_getehdr(elf, &ehdr) == NULL) {
1506                 pr_err("%s: cannot get elf header.\n", __func__);
1507                 goto out;
1508         }
1509
1510         /*
1511          * Check following sections for notes:
1512          *   '.note.gnu.build-id'
1513          *   '.notes'
1514          *   '.note' (VDSO specific)
1515          */
1516         do {
1517                 sec = elf_section_by_name(elf, &ehdr, &shdr,
1518                                           ".note.gnu.build-id", NULL);
1519                 if (sec)
1520                         break;
1521
1522                 sec = elf_section_by_name(elf, &ehdr, &shdr,
1523                                           ".notes", NULL);
1524                 if (sec)
1525                         break;
1526
1527                 sec = elf_section_by_name(elf, &ehdr, &shdr,
1528                                           ".note", NULL);
1529                 if (sec)
1530                         break;
1531
1532                 return err;
1533
1534         } while (0);
1535
1536         data = elf_getdata(sec, NULL);
1537         if (data == NULL)
1538                 goto out;
1539
1540         ptr = data->d_buf;
1541         while (ptr < (data->d_buf + data->d_size)) {
1542                 GElf_Nhdr *nhdr = ptr;
1543                 size_t namesz = NOTE_ALIGN(nhdr->n_namesz),
1544                        descsz = NOTE_ALIGN(nhdr->n_descsz);
1545                 const char *name;
1546
1547                 ptr += sizeof(*nhdr);
1548                 name = ptr;
1549                 ptr += namesz;
1550                 if (nhdr->n_type == NT_GNU_BUILD_ID &&
1551                     nhdr->n_namesz == sizeof("GNU")) {
1552                         if (memcmp(name, "GNU", sizeof("GNU")) == 0) {
1553                                 size_t sz = min(size, descsz);
1554                                 memcpy(bf, ptr, sz);
1555                                 memset(bf + sz, 0, size - sz);
1556                                 err = descsz;
1557                                 break;
1558                         }
1559                 }
1560                 ptr += descsz;
1561         }
1562
1563 out:
1564         return err;
1565 }
1566
1567 int filename__read_build_id(const char *filename, void *bf, size_t size)
1568 {
1569         int fd, err = -1;
1570         Elf *elf;
1571
1572         if (size < BUILD_ID_SIZE)
1573                 goto out;
1574
1575         fd = open(filename, O_RDONLY);
1576         if (fd < 0)
1577                 goto out;
1578
1579         elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
1580         if (elf == NULL) {
1581                 pr_debug2("%s: cannot read %s ELF file.\n", __func__, filename);
1582                 goto out_close;
1583         }
1584
1585         err = elf_read_build_id(elf, bf, size);
1586
1587         elf_end(elf);
1588 out_close:
1589         close(fd);
1590 out:
1591         return err;
1592 }
1593
1594 int sysfs__read_build_id(const char *filename, void *build_id, size_t size)
1595 {
1596         int fd, err = -1;
1597
1598         if (size < BUILD_ID_SIZE)
1599                 goto out;
1600
1601         fd = open(filename, O_RDONLY);
1602         if (fd < 0)
1603                 goto out;
1604
1605         while (1) {
1606                 char bf[BUFSIZ];
1607                 GElf_Nhdr nhdr;
1608                 size_t namesz, descsz;
1609
1610                 if (read(fd, &nhdr, sizeof(nhdr)) != sizeof(nhdr))
1611                         break;
1612
1613                 namesz = NOTE_ALIGN(nhdr.n_namesz);
1614                 descsz = NOTE_ALIGN(nhdr.n_descsz);
1615                 if (nhdr.n_type == NT_GNU_BUILD_ID &&
1616                     nhdr.n_namesz == sizeof("GNU")) {
1617                         if (read(fd, bf, namesz) != (ssize_t)namesz)
1618                                 break;
1619                         if (memcmp(bf, "GNU", sizeof("GNU")) == 0) {
1620                                 size_t sz = min(descsz, size);
1621                                 if (read(fd, build_id, sz) == (ssize_t)sz) {
1622                                         memset(build_id + sz, 0, size - sz);
1623                                         err = 0;
1624                                         break;
1625                                 }
1626                         } else if (read(fd, bf, descsz) != (ssize_t)descsz)
1627                                 break;
1628                 } else {
1629                         int n = namesz + descsz;
1630                         if (read(fd, bf, n) != n)
1631                                 break;
1632                 }
1633         }
1634         close(fd);
1635 out:
1636         return err;
1637 }
1638
1639 static int filename__read_debuglink(const char *filename,
1640                                     char *debuglink, size_t size)
1641 {
1642         int fd, err = -1;
1643         Elf *elf;
1644         GElf_Ehdr ehdr;
1645         GElf_Shdr shdr;
1646         Elf_Data *data;
1647         Elf_Scn *sec;
1648         Elf_Kind ek;
1649
1650         fd = open(filename, O_RDONLY);
1651         if (fd < 0)
1652                 goto out;
1653
1654         elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
1655         if (elf == NULL) {
1656                 pr_debug2("%s: cannot read %s ELF file.\n", __func__, filename);
1657                 goto out_close;
1658         }
1659
1660         ek = elf_kind(elf);
1661         if (ek != ELF_K_ELF)
1662                 goto out_close;
1663
1664         if (gelf_getehdr(elf, &ehdr) == NULL) {
1665                 pr_err("%s: cannot get elf header.\n", __func__);
1666                 goto out_close;
1667         }
1668
1669         sec = elf_section_by_name(elf, &ehdr, &shdr,
1670                                   ".gnu_debuglink", NULL);
1671         if (sec == NULL)
1672                 goto out_close;
1673
1674         data = elf_getdata(sec, NULL);
1675         if (data == NULL)
1676                 goto out_close;
1677
1678         /* the start of this section is a zero-terminated string */
1679         strncpy(debuglink, data->d_buf, size);
1680
1681         elf_end(elf);
1682
1683 out_close:
1684         close(fd);
1685 out:
1686         return err;
1687 }
1688
1689 char dso__symtab_origin(const struct dso *dso)
1690 {
1691         static const char origin[] = {
1692                 [DSO_BINARY_TYPE__KALLSYMS]             = 'k',
1693                 [DSO_BINARY_TYPE__JAVA_JIT]             = 'j',
1694                 [DSO_BINARY_TYPE__DEBUGLINK]            = 'l',
1695                 [DSO_BINARY_TYPE__BUILD_ID_CACHE]       = 'B',
1696                 [DSO_BINARY_TYPE__FEDORA_DEBUGINFO]     = 'f',
1697                 [DSO_BINARY_TYPE__UBUNTU_DEBUGINFO]     = 'u',
1698                 [DSO_BINARY_TYPE__BUILDID_DEBUGINFO]    = 'b',
1699                 [DSO_BINARY_TYPE__SYSTEM_PATH_DSO]      = 'd',
1700                 [DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE]  = 'K',
1701                 [DSO_BINARY_TYPE__GUEST_KALLSYMS]       = 'g',
1702                 [DSO_BINARY_TYPE__GUEST_KMODULE]        = 'G',
1703         };
1704
1705         if (dso == NULL || dso->symtab_type == DSO_BINARY_TYPE__NOT_FOUND)
1706                 return '!';
1707         return origin[dso->symtab_type];
1708 }
1709
1710 int dso__binary_type_file(struct dso *dso, enum dso_binary_type type,
1711                           char *root_dir, char *file, size_t size)
1712 {
1713         char build_id_hex[BUILD_ID_SIZE * 2 + 1];
1714         int ret = 0;
1715
1716         switch (type) {
1717         case DSO_BINARY_TYPE__DEBUGLINK: {
1718                 char *debuglink;
1719
1720                 strncpy(file, dso->long_name, size);
1721                 debuglink = file + dso->long_name_len;
1722                 while (debuglink != file && *debuglink != '/')
1723                         debuglink--;
1724                 if (*debuglink == '/')
1725                         debuglink++;
1726                 filename__read_debuglink(dso->long_name, debuglink,
1727                                          size - (debuglink - file));
1728                 }
1729                 break;
1730         case DSO_BINARY_TYPE__BUILD_ID_CACHE:
1731                 /* skip the locally configured cache if a symfs is given */
1732                 if (symbol_conf.symfs[0] ||
1733                     (dso__build_id_filename(dso, file, size) == NULL))
1734                         ret = -1;
1735                 break;
1736
1737         case DSO_BINARY_TYPE__FEDORA_DEBUGINFO:
1738                 snprintf(file, size, "%s/usr/lib/debug%s.debug",
1739                          symbol_conf.symfs, dso->long_name);
1740                 break;
1741
1742         case DSO_BINARY_TYPE__UBUNTU_DEBUGINFO:
1743                 snprintf(file, size, "%s/usr/lib/debug%s",
1744                          symbol_conf.symfs, dso->long_name);
1745                 break;
1746
1747         case DSO_BINARY_TYPE__BUILDID_DEBUGINFO:
1748                 if (!dso->has_build_id) {
1749                         ret = -1;
1750                         break;
1751                 }
1752
1753                 build_id__sprintf(dso->build_id,
1754                                   sizeof(dso->build_id),
1755                                   build_id_hex);
1756                 snprintf(file, size,
1757                          "%s/usr/lib/debug/.build-id/%.2s/%s.debug",
1758                          symbol_conf.symfs, build_id_hex, build_id_hex + 2);
1759                 break;
1760
1761         case DSO_BINARY_TYPE__SYSTEM_PATH_DSO:
1762                 snprintf(file, size, "%s%s",
1763                          symbol_conf.symfs, dso->long_name);
1764                 break;
1765
1766         case DSO_BINARY_TYPE__GUEST_KMODULE:
1767                 snprintf(file, size, "%s%s%s", symbol_conf.symfs,
1768                          root_dir, dso->long_name);
1769                 break;
1770
1771         case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE:
1772                 snprintf(file, size, "%s%s", symbol_conf.symfs,
1773                          dso->long_name);
1774                 break;
1775
1776         default:
1777         case DSO_BINARY_TYPE__KALLSYMS:
1778         case DSO_BINARY_TYPE__GUEST_KALLSYMS:
1779         case DSO_BINARY_TYPE__JAVA_JIT:
1780         case DSO_BINARY_TYPE__NOT_FOUND:
1781                 ret = -1;
1782                 break;
1783         }
1784
1785         return ret;
1786 }
1787
1788 int dso__load(struct dso *dso, struct map *map, symbol_filter_t filter)
1789 {
1790         char *name;
1791         int ret = -1;
1792         int fd;
1793         u_int i;
1794         struct machine *machine;
1795         char *root_dir = (char *) "";
1796         int want_symtab;
1797
1798         dso__set_loaded(dso, map->type);
1799
1800         if (dso->kernel == DSO_TYPE_KERNEL)
1801                 return dso__load_kernel_sym(dso, map, filter);
1802         else if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
1803                 return dso__load_guest_kernel_sym(dso, map, filter);
1804
1805         if (map->groups && map->groups->machine)
1806                 machine = map->groups->machine;
1807         else
1808                 machine = NULL;
1809
1810         name = malloc(PATH_MAX);
1811         if (!name)
1812                 return -1;
1813
1814         dso->adjust_symbols = 0;
1815
1816         if (strncmp(dso->name, "/tmp/perf-", 10) == 0) {
1817                 struct stat st;
1818
1819                 if (lstat(dso->name, &st) < 0)
1820                         return -1;
1821
1822                 if (st.st_uid && (st.st_uid != geteuid())) {
1823                         pr_warning("File %s not owned by current user or root, "
1824                                 "ignoring it.\n", dso->name);
1825                         return -1;
1826                 }
1827
1828                 ret = dso__load_perf_map(dso, map, filter);
1829                 dso->symtab_type = ret > 0 ? DSO_BINARY_TYPE__JAVA_JIT :
1830                                              DSO_BINARY_TYPE__NOT_FOUND;
1831                 return ret;
1832         }
1833
1834         if (machine)
1835                 root_dir = machine->root_dir;
1836
1837         /* Iterate over candidate debug images.
1838          * On the first pass, only load images if they have a full symtab.
1839          * Failing that, do a second pass where we accept .dynsym also
1840          */
1841         want_symtab = 1;
1842 restart:
1843         for (i = 0; i < DSO_BINARY_TYPE__SYMTAB_CNT; i++) {
1844
1845                 dso->symtab_type = binary_type_symtab[i];
1846
1847                 if (dso__binary_type_file(dso, dso->symtab_type,
1848                                           root_dir, name, PATH_MAX))
1849                         continue;
1850
1851                 /* Name is now the name of the next image to try */
1852                 fd = open(name, O_RDONLY);
1853                 if (fd < 0)
1854                         continue;
1855
1856                 ret = dso__load_sym(dso, map, name, fd, filter, 0,
1857                                     want_symtab);
1858                 close(fd);
1859
1860                 /*
1861                  * Some people seem to have debuginfo files _WITHOUT_ debug
1862                  * info!?!?
1863                  */
1864                 if (!ret)
1865                         continue;
1866
1867                 if (ret > 0) {
1868                         int nr_plt;
1869
1870                         nr_plt = dso__synthesize_plt_symbols(dso, name, map, filter);
1871                         if (nr_plt > 0)
1872                                 ret += nr_plt;
1873                         break;
1874                 }
1875         }
1876
1877         /*
1878          * If we wanted a full symtab but no image had one,
1879          * relax our requirements and repeat the search.
1880          */
1881         if (ret <= 0 && want_symtab) {
1882                 want_symtab = 0;
1883                 goto restart;
1884         }
1885
1886         free(name);
1887         if (ret < 0 && strstr(dso->name, " (deleted)") != NULL)
1888                 return 0;
1889         return ret;
1890 }
1891
1892 struct map *map_groups__find_by_name(struct map_groups *mg,
1893                                      enum map_type type, const char *name)
1894 {
1895         struct rb_node *nd;
1896
1897         for (nd = rb_first(&mg->maps[type]); nd; nd = rb_next(nd)) {
1898                 struct map *map = rb_entry(nd, struct map, rb_node);
1899
1900                 if (map->dso && strcmp(map->dso->short_name, name) == 0)
1901                         return map;
1902         }
1903
1904         return NULL;
1905 }
1906
1907 static int dso__kernel_module_get_build_id(struct dso *dso,
1908                                            const char *root_dir)
1909 {
1910         char filename[PATH_MAX];
1911         /*
1912          * kernel module short names are of the form "[module]" and
1913          * we need just "module" here.
1914          */
1915         const char *name = dso->short_name + 1;
1916
1917         snprintf(filename, sizeof(filename),
1918                  "%s/sys/module/%.*s/notes/.note.gnu.build-id",
1919                  root_dir, (int)strlen(name) - 1, name);
1920
1921         if (sysfs__read_build_id(filename, dso->build_id,
1922                                  sizeof(dso->build_id)) == 0)
1923                 dso->has_build_id = true;
1924
1925         return 0;
1926 }
1927
1928 static int map_groups__set_modules_path_dir(struct map_groups *mg,
1929                                 const char *dir_name)
1930 {
1931         struct dirent *dent;
1932         DIR *dir = opendir(dir_name);
1933         int ret = 0;
1934
1935         if (!dir) {
1936                 pr_debug("%s: cannot open %s dir\n", __func__, dir_name);
1937                 return -1;
1938         }
1939
1940         while ((dent = readdir(dir)) != NULL) {
1941                 char path[PATH_MAX];
1942                 struct stat st;
1943
1944                 /*sshfs might return bad dent->d_type, so we have to stat*/
1945                 snprintf(path, sizeof(path), "%s/%s", dir_name, dent->d_name);
1946                 if (stat(path, &st))
1947                         continue;
1948
1949                 if (S_ISDIR(st.st_mode)) {
1950                         if (!strcmp(dent->d_name, ".") ||
1951                             !strcmp(dent->d_name, ".."))
1952                                 continue;
1953
1954                         ret = map_groups__set_modules_path_dir(mg, path);
1955                         if (ret < 0)
1956                                 goto out;
1957                 } else {
1958                         char *dot = strrchr(dent->d_name, '.'),
1959                              dso_name[PATH_MAX];
1960                         struct map *map;
1961                         char *long_name;
1962
1963                         if (dot == NULL || strcmp(dot, ".ko"))
1964                                 continue;
1965                         snprintf(dso_name, sizeof(dso_name), "[%.*s]",
1966                                  (int)(dot - dent->d_name), dent->d_name);
1967
1968                         strxfrchar(dso_name, '-', '_');
1969                         map = map_groups__find_by_name(mg, MAP__FUNCTION,
1970                                                        dso_name);
1971                         if (map == NULL)
1972                                 continue;
1973
1974                         long_name = strdup(path);
1975                         if (long_name == NULL) {
1976                                 ret = -1;
1977                                 goto out;
1978                         }
1979                         dso__set_long_name(map->dso, long_name);
1980                         map->dso->lname_alloc = 1;
1981                         dso__kernel_module_get_build_id(map->dso, "");
1982                 }
1983         }
1984
1985 out:
1986         closedir(dir);
1987         return ret;
1988 }
1989
1990 static char *get_kernel_version(const char *root_dir)
1991 {
1992         char version[PATH_MAX];
1993         FILE *file;
1994         char *name, *tmp;
1995         const char *prefix = "Linux version ";
1996
1997         sprintf(version, "%s/proc/version", root_dir);
1998         file = fopen(version, "r");
1999         if (!file)
2000                 return NULL;
2001
2002         version[0] = '\0';
2003         tmp = fgets(version, sizeof(version), file);
2004         fclose(file);
2005
2006         name = strstr(version, prefix);
2007         if (!name)
2008                 return NULL;
2009         name += strlen(prefix);
2010         tmp = strchr(name, ' ');
2011         if (tmp)
2012                 *tmp = '\0';
2013
2014         return strdup(name);
2015 }
2016
2017 static int machine__set_modules_path(struct machine *machine)
2018 {
2019         char *version;
2020         char modules_path[PATH_MAX];
2021
2022         version = get_kernel_version(machine->root_dir);
2023         if (!version)
2024                 return -1;
2025
2026         snprintf(modules_path, sizeof(modules_path), "%s/lib/modules/%s/kernel",
2027                  machine->root_dir, version);
2028         free(version);
2029
2030         return map_groups__set_modules_path_dir(&machine->kmaps, modules_path);
2031 }
2032
2033 /*
2034  * Constructor variant for modules (where we know from /proc/modules where
2035  * they are loaded) and for vmlinux, where only after we load all the
2036  * symbols we'll know where it starts and ends.
2037  */
2038 static struct map *map__new2(u64 start, struct dso *dso, enum map_type type)
2039 {
2040         struct map *map = calloc(1, (sizeof(*map) +
2041                                      (dso->kernel ? sizeof(struct kmap) : 0)));
2042         if (map != NULL) {
2043                 /*
2044                  * ->end will be filled after we load all the symbols
2045                  */
2046                 map__init(map, type, start, 0, 0, dso);
2047         }
2048
2049         return map;
2050 }
2051
2052 struct map *machine__new_module(struct machine *machine, u64 start,
2053                                 const char *filename)
2054 {
2055         struct map *map;
2056         struct dso *dso = __dsos__findnew(&machine->kernel_dsos, filename);
2057
2058         if (dso == NULL)
2059                 return NULL;
2060
2061         map = map__new2(start, dso, MAP__FUNCTION);
2062         if (map == NULL)
2063                 return NULL;
2064
2065         if (machine__is_host(machine))
2066                 dso->symtab_type = DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE;
2067         else
2068                 dso->symtab_type = DSO_BINARY_TYPE__GUEST_KMODULE;
2069         map_groups__insert(&machine->kmaps, map);
2070         return map;
2071 }
2072
2073 static int machine__create_modules(struct machine *machine)
2074 {
2075         char *line = NULL;
2076         size_t n;
2077         FILE *file;
2078         struct map *map;
2079         const char *modules;
2080         char path[PATH_MAX];
2081
2082         if (machine__is_default_guest(machine))
2083                 modules = symbol_conf.default_guest_modules;
2084         else {
2085                 sprintf(path, "%s/proc/modules", machine->root_dir);
2086                 modules = path;
2087         }
2088
2089         if (symbol__restricted_filename(path, "/proc/modules"))
2090                 return -1;
2091
2092         file = fopen(modules, "r");
2093         if (file == NULL)
2094                 return -1;
2095
2096         while (!feof(file)) {
2097                 char name[PATH_MAX];
2098                 u64 start;
2099                 char *sep;
2100                 int line_len;
2101
2102                 line_len = getline(&line, &n, file);
2103                 if (line_len < 0)
2104                         break;
2105
2106                 if (!line)
2107                         goto out_failure;
2108
2109                 line[--line_len] = '\0'; /* \n */
2110
2111                 sep = strrchr(line, 'x');
2112                 if (sep == NULL)
2113                         continue;
2114
2115                 hex2u64(sep + 1, &start);
2116
2117                 sep = strchr(line, ' ');
2118                 if (sep == NULL)
2119                         continue;
2120
2121                 *sep = '\0';
2122
2123                 snprintf(name, sizeof(name), "[%s]", line);
2124                 map = machine__new_module(machine, start, name);
2125                 if (map == NULL)
2126                         goto out_delete_line;
2127                 dso__kernel_module_get_build_id(map->dso, machine->root_dir);
2128         }
2129
2130         free(line);
2131         fclose(file);
2132
2133         return machine__set_modules_path(machine);
2134
2135 out_delete_line:
2136         free(line);
2137 out_failure:
2138         return -1;
2139 }
2140
2141 int dso__load_vmlinux(struct dso *dso, struct map *map,
2142                       const char *vmlinux, symbol_filter_t filter)
2143 {
2144         int err = -1, fd;
2145         char symfs_vmlinux[PATH_MAX];
2146
2147         snprintf(symfs_vmlinux, sizeof(symfs_vmlinux), "%s%s",
2148                  symbol_conf.symfs, vmlinux);
2149         fd = open(symfs_vmlinux, O_RDONLY);
2150         if (fd < 0)
2151                 return -1;
2152
2153         dso__set_long_name(dso, (char *)vmlinux);
2154         dso__set_loaded(dso, map->type);
2155         err = dso__load_sym(dso, map, symfs_vmlinux, fd, filter, 0, 0);
2156         close(fd);
2157
2158         if (err > 0)
2159                 pr_debug("Using %s for symbols\n", symfs_vmlinux);
2160
2161         return err;
2162 }
2163
2164 int dso__load_vmlinux_path(struct dso *dso, struct map *map,
2165                            symbol_filter_t filter)
2166 {
2167         int i, err = 0;
2168         char *filename;
2169
2170         pr_debug("Looking at the vmlinux_path (%d entries long)\n",
2171                  vmlinux_path__nr_entries + 1);
2172
2173         filename = dso__build_id_filename(dso, NULL, 0);
2174         if (filename != NULL) {
2175                 err = dso__load_vmlinux(dso, map, filename, filter);
2176                 if (err > 0) {
2177                         dso__set_long_name(dso, filename);
2178                         goto out;
2179                 }
2180                 free(filename);
2181         }
2182
2183         for (i = 0; i < vmlinux_path__nr_entries; ++i) {
2184                 err = dso__load_vmlinux(dso, map, vmlinux_path[i], filter);
2185                 if (err > 0) {
2186                         dso__set_long_name(dso, strdup(vmlinux_path[i]));
2187                         break;
2188                 }
2189         }
2190 out:
2191         return err;
2192 }
2193
2194 static int dso__load_kernel_sym(struct dso *dso, struct map *map,
2195                                 symbol_filter_t filter)
2196 {
2197         int err;
2198         const char *kallsyms_filename = NULL;
2199         char *kallsyms_allocated_filename = NULL;
2200         /*
2201          * Step 1: if the user specified a kallsyms or vmlinux filename, use
2202          * it and only it, reporting errors to the user if it cannot be used.
2203          *
2204          * For instance, try to analyse an ARM perf.data file _without_ a
2205          * build-id, or if the user specifies the wrong path to the right
2206          * vmlinux file, obviously we can't fallback to another vmlinux (a
2207          * x86_86 one, on the machine where analysis is being performed, say),
2208          * or worse, /proc/kallsyms.
2209          *
2210          * If the specified file _has_ a build-id and there is a build-id
2211          * section in the perf.data file, we will still do the expected
2212          * validation in dso__load_vmlinux and will bail out if they don't
2213          * match.
2214          */
2215         if (symbol_conf.kallsyms_name != NULL) {
2216                 kallsyms_filename = symbol_conf.kallsyms_name;
2217                 goto do_kallsyms;
2218         }
2219
2220         if (symbol_conf.vmlinux_name != NULL) {
2221                 err = dso__load_vmlinux(dso, map,
2222                                         symbol_conf.vmlinux_name, filter);
2223                 if (err > 0) {
2224                         dso__set_long_name(dso,
2225                                            strdup(symbol_conf.vmlinux_name));
2226                         goto out_fixup;
2227                 }
2228                 return err;
2229         }
2230
2231         if (vmlinux_path != NULL) {
2232                 err = dso__load_vmlinux_path(dso, map, filter);
2233                 if (err > 0)
2234                         goto out_fixup;
2235         }
2236
2237         /* do not try local files if a symfs was given */
2238         if (symbol_conf.symfs[0] != 0)
2239                 return -1;
2240
2241         /*
2242          * Say the kernel DSO was created when processing the build-id header table,
2243          * we have a build-id, so check if it is the same as the running kernel,
2244          * using it if it is.
2245          */
2246         if (dso->has_build_id) {
2247                 u8 kallsyms_build_id[BUILD_ID_SIZE];
2248                 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
2249
2250                 if (sysfs__read_build_id("/sys/kernel/notes", kallsyms_build_id,
2251                                          sizeof(kallsyms_build_id)) == 0) {
2252                         if (dso__build_id_equal(dso, kallsyms_build_id)) {
2253                                 kallsyms_filename = "/proc/kallsyms";
2254                                 goto do_kallsyms;
2255                         }
2256                 }
2257                 /*
2258                  * Now look if we have it on the build-id cache in
2259                  * $HOME/.debug/[kernel.kallsyms].
2260                  */
2261                 build_id__sprintf(dso->build_id, sizeof(dso->build_id),
2262                                   sbuild_id);
2263
2264                 if (asprintf(&kallsyms_allocated_filename,
2265                              "%s/.debug/[kernel.kallsyms]/%s",
2266                              getenv("HOME"), sbuild_id) == -1) {
2267                         pr_err("Not enough memory for kallsyms file lookup\n");
2268                         return -1;
2269                 }
2270
2271                 kallsyms_filename = kallsyms_allocated_filename;
2272
2273                 if (access(kallsyms_filename, F_OK)) {
2274                         pr_err("No kallsyms or vmlinux with build-id %s "
2275                                "was found\n", sbuild_id);
2276                         free(kallsyms_allocated_filename);
2277                         return -1;
2278                 }
2279         } else {
2280                 /*
2281                  * Last resort, if we don't have a build-id and couldn't find
2282                  * any vmlinux file, try the running kernel kallsyms table.
2283                  */
2284                 kallsyms_filename = "/proc/kallsyms";
2285         }
2286
2287 do_kallsyms:
2288         err = dso__load_kallsyms(dso, kallsyms_filename, map, filter);
2289         if (err > 0)
2290                 pr_debug("Using %s for symbols\n", kallsyms_filename);
2291         free(kallsyms_allocated_filename);
2292
2293         if (err > 0) {
2294 out_fixup:
2295                 if (kallsyms_filename != NULL)
2296                         dso__set_long_name(dso, strdup("[kernel.kallsyms]"));
2297                 map__fixup_start(map);
2298                 map__fixup_end(map);
2299         }
2300
2301         return err;
2302 }
2303
2304 static int dso__load_guest_kernel_sym(struct dso *dso, struct map *map,
2305                                       symbol_filter_t filter)
2306 {
2307         int err;
2308         const char *kallsyms_filename = NULL;
2309         struct machine *machine;
2310         char path[PATH_MAX];
2311
2312         if (!map->groups) {
2313                 pr_debug("Guest kernel map hasn't the point to groups\n");
2314                 return -1;
2315         }
2316         machine = map->groups->machine;
2317
2318         if (machine__is_default_guest(machine)) {
2319                 /*
2320                  * if the user specified a vmlinux filename, use it and only
2321                  * it, reporting errors to the user if it cannot be used.
2322                  * Or use file guest_kallsyms inputted by user on commandline
2323                  */
2324                 if (symbol_conf.default_guest_vmlinux_name != NULL) {
2325                         err = dso__load_vmlinux(dso, map,
2326                                 symbol_conf.default_guest_vmlinux_name, filter);
2327                         goto out_try_fixup;
2328                 }
2329
2330                 kallsyms_filename = symbol_conf.default_guest_kallsyms;
2331                 if (!kallsyms_filename)
2332                         return -1;
2333         } else {
2334                 sprintf(path, "%s/proc/kallsyms", machine->root_dir);
2335                 kallsyms_filename = path;
2336         }
2337
2338         err = dso__load_kallsyms(dso, kallsyms_filename, map, filter);
2339         if (err > 0)
2340                 pr_debug("Using %s for symbols\n", kallsyms_filename);
2341
2342 out_try_fixup:
2343         if (err > 0) {
2344                 if (kallsyms_filename != NULL) {
2345                         machine__mmap_name(machine, path, sizeof(path));
2346                         dso__set_long_name(dso, strdup(path));
2347                 }
2348                 map__fixup_start(map);
2349                 map__fixup_end(map);
2350         }
2351
2352         return err;
2353 }
2354
2355 static void dsos__add(struct list_head *head, struct dso *dso)
2356 {
2357         list_add_tail(&dso->node, head);
2358 }
2359
2360 static struct dso *dsos__find(struct list_head *head, const char *name)
2361 {
2362         struct dso *pos;
2363
2364         list_for_each_entry(pos, head, node)
2365                 if (strcmp(pos->long_name, name) == 0)
2366                         return pos;
2367         return NULL;
2368 }
2369
2370 struct dso *__dsos__findnew(struct list_head *head, const char *name)
2371 {
2372         struct dso *dso = dsos__find(head, name);
2373
2374         if (!dso) {
2375                 dso = dso__new(name);
2376                 if (dso != NULL) {
2377                         dsos__add(head, dso);
2378                         dso__set_basename(dso);
2379                 }
2380         }
2381
2382         return dso;
2383 }
2384
2385 size_t __dsos__fprintf(struct list_head *head, FILE *fp)
2386 {
2387         struct dso *pos;
2388         size_t ret = 0;
2389
2390         list_for_each_entry(pos, head, node) {
2391                 int i;
2392                 for (i = 0; i < MAP__NR_TYPES; ++i)
2393                         ret += dso__fprintf(pos, i, fp);
2394         }
2395
2396         return ret;
2397 }
2398
2399 size_t machines__fprintf_dsos(struct rb_root *machines, FILE *fp)
2400 {
2401         struct rb_node *nd;
2402         size_t ret = 0;
2403
2404         for (nd = rb_first(machines); nd; nd = rb_next(nd)) {
2405                 struct machine *pos = rb_entry(nd, struct machine, rb_node);
2406                 ret += __dsos__fprintf(&pos->kernel_dsos, fp);
2407                 ret += __dsos__fprintf(&pos->user_dsos, fp);
2408         }
2409
2410         return ret;
2411 }
2412
2413 static size_t __dsos__fprintf_buildid(struct list_head *head, FILE *fp,
2414                                       bool with_hits)
2415 {
2416         struct dso *pos;
2417         size_t ret = 0;
2418
2419         list_for_each_entry(pos, head, node) {
2420                 if (with_hits && !pos->hit)
2421                         continue;
2422                 ret += dso__fprintf_buildid(pos, fp);
2423                 ret += fprintf(fp, " %s\n", pos->long_name);
2424         }
2425         return ret;
2426 }
2427
2428 size_t machine__fprintf_dsos_buildid(struct machine *machine, FILE *fp,
2429                                      bool with_hits)
2430 {
2431         return __dsos__fprintf_buildid(&machine->kernel_dsos, fp, with_hits) +
2432                __dsos__fprintf_buildid(&machine->user_dsos, fp, with_hits);
2433 }
2434
2435 size_t machines__fprintf_dsos_buildid(struct rb_root *machines,
2436                                       FILE *fp, bool with_hits)
2437 {
2438         struct rb_node *nd;
2439         size_t ret = 0;
2440
2441         for (nd = rb_first(machines); nd; nd = rb_next(nd)) {
2442                 struct machine *pos = rb_entry(nd, struct machine, rb_node);
2443                 ret += machine__fprintf_dsos_buildid(pos, fp, with_hits);
2444         }
2445         return ret;
2446 }
2447
2448 static struct dso*
2449 dso__kernel_findnew(struct machine *machine, const char *name,
2450                     const char *short_name, int dso_type)
2451 {
2452         /*
2453          * The kernel dso could be created by build_id processing.
2454          */
2455         struct dso *dso = __dsos__findnew(&machine->kernel_dsos, name);
2456
2457         /*
2458          * We need to run this in all cases, since during the build_id
2459          * processing we had no idea this was the kernel dso.
2460          */
2461         if (dso != NULL) {
2462                 dso__set_short_name(dso, short_name);
2463                 dso->kernel = dso_type;
2464         }
2465
2466         return dso;
2467 }
2468
2469 void dso__read_running_kernel_build_id(struct dso *dso, struct machine *machine)
2470 {
2471         char path[PATH_MAX];
2472
2473         if (machine__is_default_guest(machine))
2474                 return;
2475         sprintf(path, "%s/sys/kernel/notes", machine->root_dir);
2476         if (sysfs__read_build_id(path, dso->build_id,
2477                                  sizeof(dso->build_id)) == 0)
2478                 dso->has_build_id = true;
2479 }
2480
2481 static struct dso *machine__get_kernel(struct machine *machine)
2482 {
2483         const char *vmlinux_name = NULL;
2484         struct dso *kernel;
2485
2486         if (machine__is_host(machine)) {
2487                 vmlinux_name = symbol_conf.vmlinux_name;
2488                 if (!vmlinux_name)
2489                         vmlinux_name = "[kernel.kallsyms]";
2490
2491                 kernel = dso__kernel_findnew(machine, vmlinux_name,
2492                                              "[kernel]",
2493                                              DSO_TYPE_KERNEL);
2494         } else {
2495                 char bf[PATH_MAX];
2496
2497                 if (machine__is_default_guest(machine))
2498                         vmlinux_name = symbol_conf.default_guest_vmlinux_name;
2499                 if (!vmlinux_name)
2500                         vmlinux_name = machine__mmap_name(machine, bf,
2501                                                           sizeof(bf));
2502
2503                 kernel = dso__kernel_findnew(machine, vmlinux_name,
2504                                              "[guest.kernel]",
2505                                              DSO_TYPE_GUEST_KERNEL);
2506         }
2507
2508         if (kernel != NULL && (!kernel->has_build_id))
2509                 dso__read_running_kernel_build_id(kernel, machine);
2510
2511         return kernel;
2512 }
2513
2514 struct process_args {
2515         u64 start;
2516 };
2517
2518 static int symbol__in_kernel(void *arg, const char *name,
2519                              char type __used, u64 start, u64 end __used)
2520 {
2521         struct process_args *args = arg;
2522
2523         if (strchr(name, '['))
2524                 return 0;
2525
2526         args->start = start;
2527         return 1;
2528 }
2529
2530 /* Figure out the start address of kernel map from /proc/kallsyms */
2531 static u64 machine__get_kernel_start_addr(struct machine *machine)
2532 {
2533         const char *filename;
2534         char path[PATH_MAX];
2535         struct process_args args;
2536
2537         if (machine__is_host(machine)) {
2538                 filename = "/proc/kallsyms";
2539         } else {
2540                 if (machine__is_default_guest(machine))
2541                         filename = (char *)symbol_conf.default_guest_kallsyms;
2542                 else {
2543                         sprintf(path, "%s/proc/kallsyms", machine->root_dir);
2544                         filename = path;
2545                 }
2546         }
2547
2548         if (symbol__restricted_filename(filename, "/proc/kallsyms"))
2549                 return 0;
2550
2551         if (kallsyms__parse(filename, &args, symbol__in_kernel) <= 0)
2552                 return 0;
2553
2554         return args.start;
2555 }
2556
2557 int __machine__create_kernel_maps(struct machine *machine, struct dso *kernel)
2558 {
2559         enum map_type type;
2560         u64 start = machine__get_kernel_start_addr(machine);
2561
2562         for (type = 0; type < MAP__NR_TYPES; ++type) {
2563                 struct kmap *kmap;
2564
2565                 machine->vmlinux_maps[type] = map__new2(start, kernel, type);
2566                 if (machine->vmlinux_maps[type] == NULL)
2567                         return -1;
2568
2569                 machine->vmlinux_maps[type]->map_ip =
2570                         machine->vmlinux_maps[type]->unmap_ip =
2571                                 identity__map_ip;
2572                 kmap = map__kmap(machine->vmlinux_maps[type]);
2573                 kmap->kmaps = &machine->kmaps;
2574                 map_groups__insert(&machine->kmaps,
2575                                    machine->vmlinux_maps[type]);
2576         }
2577
2578         return 0;
2579 }
2580
2581 void machine__destroy_kernel_maps(struct machine *machine)
2582 {
2583         enum map_type type;
2584
2585         for (type = 0; type < MAP__NR_TYPES; ++type) {
2586                 struct kmap *kmap;
2587
2588                 if (machine->vmlinux_maps[type] == NULL)
2589                         continue;
2590
2591                 kmap = map__kmap(machine->vmlinux_maps[type]);
2592                 map_groups__remove(&machine->kmaps,
2593                                    machine->vmlinux_maps[type]);
2594                 if (kmap->ref_reloc_sym) {
2595                         /*
2596                          * ref_reloc_sym is shared among all maps, so free just
2597                          * on one of them.
2598                          */
2599                         if (type == MAP__FUNCTION) {
2600                                 free((char *)kmap->ref_reloc_sym->name);
2601                                 kmap->ref_reloc_sym->name = NULL;
2602                                 free(kmap->ref_reloc_sym);
2603                         }
2604                         kmap->ref_reloc_sym = NULL;
2605                 }
2606
2607                 map__delete(machine->vmlinux_maps[type]);
2608                 machine->vmlinux_maps[type] = NULL;
2609         }
2610 }
2611
2612 int machine__create_kernel_maps(struct machine *machine)
2613 {
2614         struct dso *kernel = machine__get_kernel(machine);
2615
2616         if (kernel == NULL ||
2617             __machine__create_kernel_maps(machine, kernel) < 0)
2618                 return -1;
2619
2620         if (symbol_conf.use_modules && machine__create_modules(machine) < 0) {
2621                 if (machine__is_host(machine))
2622                         pr_debug("Problems creating module maps, "
2623                                  "continuing anyway...\n");
2624                 else
2625                         pr_debug("Problems creating module maps for guest %d, "
2626                                  "continuing anyway...\n", machine->pid);
2627         }
2628
2629         /*
2630          * Now that we have all the maps created, just set the ->end of them:
2631          */
2632         map_groups__fixup_end(&machine->kmaps);
2633         return 0;
2634 }
2635
2636 static void vmlinux_path__exit(void)
2637 {
2638         while (--vmlinux_path__nr_entries >= 0) {
2639                 free(vmlinux_path[vmlinux_path__nr_entries]);
2640                 vmlinux_path[vmlinux_path__nr_entries] = NULL;
2641         }
2642
2643         free(vmlinux_path);
2644         vmlinux_path = NULL;
2645 }
2646
2647 static int vmlinux_path__init(void)
2648 {
2649         struct utsname uts;
2650         char bf[PATH_MAX];
2651
2652         vmlinux_path = malloc(sizeof(char *) * 5);
2653         if (vmlinux_path == NULL)
2654                 return -1;
2655
2656         vmlinux_path[vmlinux_path__nr_entries] = strdup("vmlinux");
2657         if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
2658                 goto out_fail;
2659         ++vmlinux_path__nr_entries;
2660         vmlinux_path[vmlinux_path__nr_entries] = strdup("/boot/vmlinux");
2661         if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
2662                 goto out_fail;
2663         ++vmlinux_path__nr_entries;
2664
2665         /* only try running kernel version if no symfs was given */
2666         if (symbol_conf.symfs[0] != 0)
2667                 return 0;
2668
2669         if (uname(&uts) < 0)
2670                 return -1;
2671
2672         snprintf(bf, sizeof(bf), "/boot/vmlinux-%s", uts.release);
2673         vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
2674         if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
2675                 goto out_fail;
2676         ++vmlinux_path__nr_entries;
2677         snprintf(bf, sizeof(bf), "/lib/modules/%s/build/vmlinux", uts.release);
2678         vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
2679         if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
2680                 goto out_fail;
2681         ++vmlinux_path__nr_entries;
2682         snprintf(bf, sizeof(bf), "/usr/lib/debug/lib/modules/%s/vmlinux",
2683                  uts.release);
2684         vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
2685         if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
2686                 goto out_fail;
2687         ++vmlinux_path__nr_entries;
2688
2689         return 0;
2690
2691 out_fail:
2692         vmlinux_path__exit();
2693         return -1;
2694 }
2695
2696 size_t machine__fprintf_vmlinux_path(struct machine *machine, FILE *fp)
2697 {
2698         int i;
2699         size_t printed = 0;
2700         struct dso *kdso = machine->vmlinux_maps[MAP__FUNCTION]->dso;
2701
2702         if (kdso->has_build_id) {
2703                 char filename[PATH_MAX];
2704                 if (dso__build_id_filename(kdso, filename, sizeof(filename)))
2705                         printed += fprintf(fp, "[0] %s\n", filename);
2706         }
2707
2708         for (i = 0; i < vmlinux_path__nr_entries; ++i)
2709                 printed += fprintf(fp, "[%d] %s\n",
2710                                    i + kdso->has_build_id, vmlinux_path[i]);
2711
2712         return printed;
2713 }
2714
2715 static int setup_list(struct strlist **list, const char *list_str,
2716                       const char *list_name)
2717 {
2718         if (list_str == NULL)
2719                 return 0;
2720
2721         *list = strlist__new(true, list_str);
2722         if (!*list) {
2723                 pr_err("problems parsing %s list\n", list_name);
2724                 return -1;
2725         }
2726         return 0;
2727 }
2728
2729 static bool symbol__read_kptr_restrict(void)
2730 {
2731         bool value = false;
2732
2733         if (geteuid() != 0) {
2734                 FILE *fp = fopen("/proc/sys/kernel/kptr_restrict", "r");
2735                 if (fp != NULL) {
2736                         char line[8];
2737
2738                         if (fgets(line, sizeof(line), fp) != NULL)
2739                                 value = atoi(line) != 0;
2740
2741                         fclose(fp);
2742                 }
2743         }
2744
2745         return value;
2746 }
2747
2748 int symbol__init(void)
2749 {
2750         const char *symfs;
2751
2752         if (symbol_conf.initialized)
2753                 return 0;
2754
2755         symbol_conf.priv_size = ALIGN(symbol_conf.priv_size, sizeof(u64));
2756
2757         elf_version(EV_CURRENT);
2758         if (symbol_conf.sort_by_name)
2759                 symbol_conf.priv_size += (sizeof(struct symbol_name_rb_node) -
2760                                           sizeof(struct symbol));
2761
2762         if (symbol_conf.try_vmlinux_path && vmlinux_path__init() < 0)
2763                 return -1;
2764
2765         if (symbol_conf.field_sep && *symbol_conf.field_sep == '.') {
2766                 pr_err("'.' is the only non valid --field-separator argument\n");
2767                 return -1;
2768         }
2769
2770         if (setup_list(&symbol_conf.dso_list,
2771                        symbol_conf.dso_list_str, "dso") < 0)
2772                 return -1;
2773
2774         if (setup_list(&symbol_conf.comm_list,
2775                        symbol_conf.comm_list_str, "comm") < 0)
2776                 goto out_free_dso_list;
2777
2778         if (setup_list(&symbol_conf.sym_list,
2779                        symbol_conf.sym_list_str, "symbol") < 0)
2780                 goto out_free_comm_list;
2781
2782         /*
2783          * A path to symbols of "/" is identical to ""
2784          * reset here for simplicity.
2785          */
2786         symfs = realpath(symbol_conf.symfs, NULL);
2787         if (symfs == NULL)
2788                 symfs = symbol_conf.symfs;
2789         if (strcmp(symfs, "/") == 0)
2790                 symbol_conf.symfs = "";
2791         if (symfs != symbol_conf.symfs)
2792                 free((void *)symfs);
2793
2794         symbol_conf.kptr_restrict = symbol__read_kptr_restrict();
2795
2796         symbol_conf.initialized = true;
2797         return 0;
2798
2799 out_free_comm_list:
2800         strlist__delete(symbol_conf.comm_list);
2801 out_free_dso_list:
2802         strlist__delete(symbol_conf.dso_list);
2803         return -1;
2804 }
2805
2806 void symbol__exit(void)
2807 {
2808         if (!symbol_conf.initialized)
2809                 return;
2810         strlist__delete(symbol_conf.sym_list);
2811         strlist__delete(symbol_conf.dso_list);
2812         strlist__delete(symbol_conf.comm_list);
2813         vmlinux_path__exit();
2814         symbol_conf.sym_list = symbol_conf.dso_list = symbol_conf.comm_list = NULL;
2815         symbol_conf.initialized = false;
2816 }
2817
2818 int machines__create_kernel_maps(struct rb_root *machines, pid_t pid)
2819 {
2820         struct machine *machine = machines__findnew(machines, pid);
2821
2822         if (machine == NULL)
2823                 return -1;
2824
2825         return machine__create_kernel_maps(machine);
2826 }
2827
2828 static int hex(char ch)
2829 {
2830         if ((ch >= '0') && (ch <= '9'))
2831                 return ch - '0';
2832         if ((ch >= 'a') && (ch <= 'f'))
2833                 return ch - 'a' + 10;
2834         if ((ch >= 'A') && (ch <= 'F'))
2835                 return ch - 'A' + 10;
2836         return -1;
2837 }
2838
2839 /*
2840  * While we find nice hex chars, build a long_val.
2841  * Return number of chars processed.
2842  */
2843 int hex2u64(const char *ptr, u64 *long_val)
2844 {
2845         const char *p = ptr;
2846         *long_val = 0;
2847
2848         while (*p) {
2849                 const int hex_val = hex(*p);
2850
2851                 if (hex_val < 0)
2852                         break;
2853
2854                 *long_val = (*long_val << 4) | hex_val;
2855                 p++;
2856         }
2857
2858         return p - ptr;
2859 }
2860
2861 char *strxfrchar(char *s, char from, char to)
2862 {
2863         char *p = s;
2864
2865         while ((p = strchr(p, from)) != NULL)
2866                 *p++ = to;
2867
2868         return s;
2869 }
2870
2871 int machines__create_guest_kernel_maps(struct rb_root *machines)
2872 {
2873         int ret = 0;
2874         struct dirent **namelist = NULL;
2875         int i, items = 0;
2876         char path[PATH_MAX];
2877         pid_t pid;
2878         char *endp;
2879
2880         if (symbol_conf.default_guest_vmlinux_name ||
2881             symbol_conf.default_guest_modules ||
2882             symbol_conf.default_guest_kallsyms) {
2883                 machines__create_kernel_maps(machines, DEFAULT_GUEST_KERNEL_ID);
2884         }
2885
2886         if (symbol_conf.guestmount) {
2887                 items = scandir(symbol_conf.guestmount, &namelist, NULL, NULL);
2888                 if (items <= 0)
2889                         return -ENOENT;
2890                 for (i = 0; i < items; i++) {
2891                         if (!isdigit(namelist[i]->d_name[0])) {
2892                                 /* Filter out . and .. */
2893                                 continue;
2894                         }
2895                         pid = (pid_t)strtol(namelist[i]->d_name, &endp, 10);
2896                         if ((*endp != '\0') ||
2897                             (endp == namelist[i]->d_name) ||
2898                             (errno == ERANGE)) {
2899                                 pr_debug("invalid directory (%s). Skipping.\n",
2900                                          namelist[i]->d_name);
2901                                 continue;
2902                         }
2903                         sprintf(path, "%s/%s/proc/kallsyms",
2904                                 symbol_conf.guestmount,
2905                                 namelist[i]->d_name);
2906                         ret = access(path, R_OK);
2907                         if (ret) {
2908                                 pr_debug("Can't access file %s\n", path);
2909                                 goto failure;
2910                         }
2911                         machines__create_kernel_maps(machines, pid);
2912                 }
2913 failure:
2914                 free(namelist);
2915         }
2916
2917         return ret;
2918 }
2919
2920 void machines__destroy_guest_kernel_maps(struct rb_root *machines)
2921 {
2922         struct rb_node *next = rb_first(machines);
2923
2924         while (next) {
2925                 struct machine *pos = rb_entry(next, struct machine, rb_node);
2926
2927                 next = rb_next(&pos->rb_node);
2928                 rb_erase(&pos->rb_node, machines);
2929                 machine__delete(pos);
2930         }
2931 }
2932
2933 int machine__load_kallsyms(struct machine *machine, const char *filename,
2934                            enum map_type type, symbol_filter_t filter)
2935 {
2936         struct map *map = machine->vmlinux_maps[type];
2937         int ret = dso__load_kallsyms(map->dso, filename, map, filter);
2938
2939         if (ret > 0) {
2940                 dso__set_loaded(map->dso, type);
2941                 /*
2942                  * Since /proc/kallsyms will have multiple sessions for the
2943                  * kernel, with modules between them, fixup the end of all
2944                  * sections.
2945                  */
2946                 __map_groups__fixup_end(&machine->kmaps, type);
2947         }
2948
2949         return ret;
2950 }
2951
2952 int machine__load_vmlinux_path(struct machine *machine, enum map_type type,
2953                                symbol_filter_t filter)
2954 {
2955         struct map *map = machine->vmlinux_maps[type];
2956         int ret = dso__load_vmlinux_path(map->dso, map, filter);
2957
2958         if (ret > 0) {
2959                 dso__set_loaded(map->dso, type);
2960                 map__reloc_vmlinux(map);
2961         }
2962
2963         return ret;
2964 }
2965
2966 struct map *dso__new_map(const char *name)
2967 {
2968         struct map *map = NULL;
2969         struct dso *dso = dso__new(name);
2970
2971         if (dso)
2972                 map = map__new2(0, dso, MAP__FUNCTION);
2973
2974         return map;
2975 }
2976
2977 static int open_dso(struct dso *dso, struct machine *machine)
2978 {
2979         char *root_dir = (char *) "";
2980         char *name;
2981         int fd;
2982
2983         name = malloc(PATH_MAX);
2984         if (!name)
2985                 return -ENOMEM;
2986
2987         if (machine)
2988                 root_dir = machine->root_dir;
2989
2990         if (dso__binary_type_file(dso, dso->data_type,
2991                                   root_dir, name, PATH_MAX)) {
2992                 free(name);
2993                 return -EINVAL;
2994         }
2995
2996         fd = open(name, O_RDONLY);
2997         free(name);
2998         return fd;
2999 }
3000
3001 int dso__data_fd(struct dso *dso, struct machine *machine)
3002 {
3003         int i = 0;
3004
3005         if (dso->data_type != DSO_BINARY_TYPE__NOT_FOUND)
3006                 return open_dso(dso, machine);
3007
3008         do {
3009                 int fd;
3010
3011                 dso->data_type = binary_type_data[i++];
3012
3013                 fd = open_dso(dso, machine);
3014                 if (fd >= 0)
3015                         return fd;
3016
3017         } while (dso->data_type != DSO_BINARY_TYPE__NOT_FOUND);
3018
3019         return -EINVAL;
3020 }
3021
3022 static void
3023 dso_cache__free(struct rb_root *root)
3024 {
3025         struct rb_node *next = rb_first(root);
3026
3027         while (next) {
3028                 struct dso_cache *cache;
3029
3030                 cache = rb_entry(next, struct dso_cache, rb_node);
3031                 next = rb_next(&cache->rb_node);
3032                 rb_erase(&cache->rb_node, root);
3033                 free(cache);
3034         }
3035 }
3036
3037 static struct dso_cache*
3038 dso_cache__find(struct rb_root *root, u64 offset)
3039 {
3040         struct rb_node **p = &root->rb_node;
3041         struct rb_node *parent = NULL;
3042         struct dso_cache *cache;
3043
3044         while (*p != NULL) {
3045                 u64 end;
3046
3047                 parent = *p;
3048                 cache = rb_entry(parent, struct dso_cache, rb_node);
3049                 end = cache->offset + DSO__DATA_CACHE_SIZE;
3050
3051                 if (offset < cache->offset)
3052                         p = &(*p)->rb_left;
3053                 else if (offset >= end)
3054                         p = &(*p)->rb_right;
3055                 else
3056                         return cache;
3057         }
3058         return NULL;
3059 }
3060
3061 static void
3062 dso_cache__insert(struct rb_root *root, struct dso_cache *new)
3063 {
3064         struct rb_node **p = &root->rb_node;
3065         struct rb_node *parent = NULL;
3066         struct dso_cache *cache;
3067         u64 offset = new->offset;
3068
3069         while (*p != NULL) {
3070                 u64 end;
3071
3072                 parent = *p;
3073                 cache = rb_entry(parent, struct dso_cache, rb_node);
3074                 end = cache->offset + DSO__DATA_CACHE_SIZE;
3075
3076                 if (offset < cache->offset)
3077                         p = &(*p)->rb_left;
3078                 else if (offset >= end)
3079                         p = &(*p)->rb_right;
3080         }
3081
3082         rb_link_node(&new->rb_node, parent, p);
3083         rb_insert_color(&new->rb_node, root);
3084 }
3085
3086 static ssize_t
3087 dso_cache__memcpy(struct dso_cache *cache, u64 offset,
3088                   u8 *data, u64 size)
3089 {
3090         u64 cache_offset = offset - cache->offset;
3091         u64 cache_size   = min(cache->size - cache_offset, size);
3092
3093         memcpy(data, cache->data + cache_offset, cache_size);
3094         return cache_size;
3095 }
3096
3097 static ssize_t
3098 dso_cache__read(struct dso *dso, struct machine *machine,
3099                  u64 offset, u8 *data, ssize_t size)
3100 {
3101         struct dso_cache *cache;
3102         ssize_t ret;
3103         int fd;
3104
3105         fd = dso__data_fd(dso, machine);
3106         if (fd < 0)
3107                 return -1;
3108
3109         do {
3110                 u64 cache_offset;
3111
3112                 ret = -ENOMEM;
3113
3114                 cache = zalloc(sizeof(*cache) + DSO__DATA_CACHE_SIZE);
3115                 if (!cache)
3116                         break;
3117
3118                 cache_offset = offset & DSO__DATA_CACHE_MASK;
3119                 ret = -EINVAL;
3120
3121                 if (-1 == lseek(fd, cache_offset, SEEK_SET))
3122                         break;
3123
3124                 ret = read(fd, cache->data, DSO__DATA_CACHE_SIZE);
3125                 if (ret <= 0)
3126                         break;
3127
3128                 cache->offset = cache_offset;
3129                 cache->size   = ret;
3130                 dso_cache__insert(&dso->cache, cache);
3131
3132                 ret = dso_cache__memcpy(cache, offset, data, size);
3133
3134         } while (0);
3135
3136         if (ret <= 0)
3137                 free(cache);
3138
3139         close(fd);
3140         return ret;
3141 }
3142
3143 static ssize_t dso_cache_read(struct dso *dso, struct machine *machine,
3144                               u64 offset, u8 *data, ssize_t size)
3145 {
3146         struct dso_cache *cache;
3147
3148         cache = dso_cache__find(&dso->cache, offset);
3149         if (cache)
3150                 return dso_cache__memcpy(cache, offset, data, size);
3151         else
3152                 return dso_cache__read(dso, machine, offset, data, size);
3153 }
3154
3155 ssize_t dso__data_read_offset(struct dso *dso, struct machine *machine,
3156                               u64 offset, u8 *data, ssize_t size)
3157 {
3158         ssize_t r = 0;
3159         u8 *p = data;
3160
3161         do {
3162                 ssize_t ret;
3163
3164                 ret = dso_cache_read(dso, machine, offset, p, size);
3165                 if (ret < 0)
3166                         return ret;
3167
3168                 /* Reached EOF, return what we have. */
3169                 if (!ret)
3170                         break;
3171
3172                 BUG_ON(ret > size);
3173
3174                 r      += ret;
3175                 p      += ret;
3176                 offset += ret;
3177                 size   -= ret;
3178
3179         } while (size);
3180
3181         return r;
3182 }
3183
3184 ssize_t dso__data_read_addr(struct dso *dso, struct map *map,
3185                             struct machine *machine, u64 addr,
3186                             u8 *data, ssize_t size)
3187 {
3188         u64 offset = map->map_ip(map, addr);
3189         return dso__data_read_offset(dso, machine, offset, data, size);
3190 }