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