Merge tag 'perf-core-for-mingo-20160920' of git://git.kernel.org/pub/scm/linux/kernel...
[cascardo/linux.git] / tools / perf / util / symbol.c
1 #include <dirent.h>
2 #include <errno.h>
3 #include <stdlib.h>
4 #include <stdio.h>
5 #include <string.h>
6 #include <sys/types.h>
7 #include <sys/stat.h>
8 #include <sys/param.h>
9 #include <fcntl.h>
10 #include <unistd.h>
11 #include <inttypes.h>
12 #include "annotate.h"
13 #include "build-id.h"
14 #include "util.h"
15 #include "debug.h"
16 #include "machine.h"
17 #include "symbol.h"
18 #include "strlist.h"
19 #include "intlist.h"
20 #include "header.h"
21
22 #include <elf.h>
23 #include <limits.h>
24 #include <symbol/kallsyms.h>
25 #include <sys/utsname.h>
26
27 static int dso__load_kernel_sym(struct dso *dso, struct map *map);
28 static int dso__load_guest_kernel_sym(struct dso *dso, struct map *map);
29 static bool symbol__is_idle(const char *name);
30
31 int vmlinux_path__nr_entries;
32 char **vmlinux_path;
33
34 struct symbol_conf symbol_conf = {
35         .use_modules            = true,
36         .try_vmlinux_path       = true,
37         .annotate_src           = true,
38         .demangle               = true,
39         .demangle_kernel        = false,
40         .cumulate_callchain     = true,
41         .show_hist_headers      = true,
42         .symfs                  = "",
43         .event_group            = true,
44 };
45
46 static enum dso_binary_type binary_type_symtab[] = {
47         DSO_BINARY_TYPE__KALLSYMS,
48         DSO_BINARY_TYPE__GUEST_KALLSYMS,
49         DSO_BINARY_TYPE__JAVA_JIT,
50         DSO_BINARY_TYPE__DEBUGLINK,
51         DSO_BINARY_TYPE__BUILD_ID_CACHE,
52         DSO_BINARY_TYPE__FEDORA_DEBUGINFO,
53         DSO_BINARY_TYPE__UBUNTU_DEBUGINFO,
54         DSO_BINARY_TYPE__BUILDID_DEBUGINFO,
55         DSO_BINARY_TYPE__SYSTEM_PATH_DSO,
56         DSO_BINARY_TYPE__GUEST_KMODULE,
57         DSO_BINARY_TYPE__GUEST_KMODULE_COMP,
58         DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE,
59         DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP,
60         DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO,
61         DSO_BINARY_TYPE__NOT_FOUND,
62 };
63
64 #define DSO_BINARY_TYPE__SYMTAB_CNT ARRAY_SIZE(binary_type_symtab)
65
66 bool symbol_type__is_a(char symbol_type, enum map_type map_type)
67 {
68         symbol_type = toupper(symbol_type);
69
70         switch (map_type) {
71         case MAP__FUNCTION:
72                 return symbol_type == 'T' || symbol_type == 'W';
73         case MAP__VARIABLE:
74                 return symbol_type == 'D';
75         default:
76                 return false;
77         }
78 }
79
80 static int prefix_underscores_count(const char *str)
81 {
82         const char *tail = str;
83
84         while (*tail == '_')
85                 tail++;
86
87         return tail - str;
88 }
89
90 int __weak arch__choose_best_symbol(struct symbol *syma,
91                                     struct symbol *symb __maybe_unused)
92 {
93         /* Avoid "SyS" kernel syscall aliases */
94         if (strlen(syma->name) >= 3 && !strncmp(syma->name, "SyS", 3))
95                 return SYMBOL_B;
96         if (strlen(syma->name) >= 10 && !strncmp(syma->name, "compat_SyS", 10))
97                 return SYMBOL_B;
98
99         return SYMBOL_A;
100 }
101
102 static int choose_best_symbol(struct symbol *syma, struct symbol *symb)
103 {
104         s64 a;
105         s64 b;
106         size_t na, nb;
107
108         /* Prefer a symbol with non zero length */
109         a = syma->end - syma->start;
110         b = symb->end - symb->start;
111         if ((b == 0) && (a > 0))
112                 return SYMBOL_A;
113         else if ((a == 0) && (b > 0))
114                 return SYMBOL_B;
115
116         /* Prefer a non weak symbol over a weak one */
117         a = syma->binding == STB_WEAK;
118         b = symb->binding == STB_WEAK;
119         if (b && !a)
120                 return SYMBOL_A;
121         if (a && !b)
122                 return SYMBOL_B;
123
124         /* Prefer a global symbol over a non global one */
125         a = syma->binding == STB_GLOBAL;
126         b = symb->binding == STB_GLOBAL;
127         if (a && !b)
128                 return SYMBOL_A;
129         if (b && !a)
130                 return SYMBOL_B;
131
132         /* Prefer a symbol with less underscores */
133         a = prefix_underscores_count(syma->name);
134         b = prefix_underscores_count(symb->name);
135         if (b > a)
136                 return SYMBOL_A;
137         else if (a > b)
138                 return SYMBOL_B;
139
140         /* Choose the symbol with the longest name */
141         na = strlen(syma->name);
142         nb = strlen(symb->name);
143         if (na > nb)
144                 return SYMBOL_A;
145         else if (na < nb)
146                 return SYMBOL_B;
147
148         return arch__choose_best_symbol(syma, symb);
149 }
150
151 void symbols__fixup_duplicate(struct rb_root *symbols)
152 {
153         struct rb_node *nd;
154         struct symbol *curr, *next;
155
156         if (symbol_conf.allow_aliases)
157                 return;
158
159         nd = rb_first(symbols);
160
161         while (nd) {
162                 curr = rb_entry(nd, struct symbol, rb_node);
163 again:
164                 nd = rb_next(&curr->rb_node);
165                 next = rb_entry(nd, struct symbol, rb_node);
166
167                 if (!nd)
168                         break;
169
170                 if (curr->start != next->start)
171                         continue;
172
173                 if (choose_best_symbol(curr, next) == SYMBOL_A) {
174                         rb_erase(&next->rb_node, symbols);
175                         symbol__delete(next);
176                         goto again;
177                 } else {
178                         nd = rb_next(&curr->rb_node);
179                         rb_erase(&curr->rb_node, symbols);
180                         symbol__delete(curr);
181                 }
182         }
183 }
184
185 void symbols__fixup_end(struct rb_root *symbols)
186 {
187         struct rb_node *nd, *prevnd = rb_first(symbols);
188         struct symbol *curr, *prev;
189
190         if (prevnd == NULL)
191                 return;
192
193         curr = rb_entry(prevnd, struct symbol, rb_node);
194
195         for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
196                 prev = curr;
197                 curr = rb_entry(nd, struct symbol, rb_node);
198
199                 if (prev->end == prev->start && prev->end != curr->start)
200                         prev->end = curr->start;
201         }
202
203         /* Last entry */
204         if (curr->end == curr->start)
205                 curr->end = roundup(curr->start, 4096);
206 }
207
208 void __map_groups__fixup_end(struct map_groups *mg, enum map_type type)
209 {
210         struct maps *maps = &mg->maps[type];
211         struct map *next, *curr;
212
213         pthread_rwlock_wrlock(&maps->lock);
214
215         curr = maps__first(maps);
216         if (curr == NULL)
217                 goto out_unlock;
218
219         for (next = map__next(curr); next; next = map__next(curr)) {
220                 curr->end = next->start;
221                 curr = next;
222         }
223
224         /*
225          * We still haven't the actual symbols, so guess the
226          * last map final address.
227          */
228         curr->end = ~0ULL;
229
230 out_unlock:
231         pthread_rwlock_unlock(&maps->lock);
232 }
233
234 struct symbol *symbol__new(u64 start, u64 len, u8 binding, const char *name)
235 {
236         size_t namelen = strlen(name) + 1;
237         struct symbol *sym = calloc(1, (symbol_conf.priv_size +
238                                         sizeof(*sym) + namelen));
239         if (sym == NULL)
240                 return NULL;
241
242         if (symbol_conf.priv_size) {
243                 if (symbol_conf.init_annotation) {
244                         struct annotation *notes = (void *)sym;
245                         pthread_mutex_init(&notes->lock, NULL);
246                 }
247                 sym = ((void *)sym) + symbol_conf.priv_size;
248         }
249
250         sym->start   = start;
251         sym->end     = len ? start + len : start;
252         sym->binding = binding;
253         sym->namelen = namelen - 1;
254
255         pr_debug4("%s: %s %#" PRIx64 "-%#" PRIx64 "\n",
256                   __func__, name, start, sym->end);
257         memcpy(sym->name, name, namelen);
258
259         return sym;
260 }
261
262 void symbol__delete(struct symbol *sym)
263 {
264         free(((void *)sym) - symbol_conf.priv_size);
265 }
266
267 void symbols__delete(struct rb_root *symbols)
268 {
269         struct symbol *pos;
270         struct rb_node *next = rb_first(symbols);
271
272         while (next) {
273                 pos = rb_entry(next, struct symbol, rb_node);
274                 next = rb_next(&pos->rb_node);
275                 rb_erase(&pos->rb_node, symbols);
276                 symbol__delete(pos);
277         }
278 }
279
280 void __symbols__insert(struct rb_root *symbols, struct symbol *sym, bool kernel)
281 {
282         struct rb_node **p = &symbols->rb_node;
283         struct rb_node *parent = NULL;
284         const u64 ip = sym->start;
285         struct symbol *s;
286
287         if (kernel) {
288                 const char *name = sym->name;
289                 /*
290                  * ppc64 uses function descriptors and appends a '.' to the
291                  * start of every instruction address. Remove it.
292                  */
293                 if (name[0] == '.')
294                         name++;
295                 sym->idle = symbol__is_idle(name);
296         }
297
298         while (*p != NULL) {
299                 parent = *p;
300                 s = rb_entry(parent, struct symbol, rb_node);
301                 if (ip < s->start)
302                         p = &(*p)->rb_left;
303                 else
304                         p = &(*p)->rb_right;
305         }
306         rb_link_node(&sym->rb_node, parent, p);
307         rb_insert_color(&sym->rb_node, symbols);
308 }
309
310 void symbols__insert(struct rb_root *symbols, struct symbol *sym)
311 {
312         __symbols__insert(symbols, sym, false);
313 }
314
315 static struct symbol *symbols__find(struct rb_root *symbols, u64 ip)
316 {
317         struct rb_node *n;
318
319         if (symbols == NULL)
320                 return NULL;
321
322         n = symbols->rb_node;
323
324         while (n) {
325                 struct symbol *s = rb_entry(n, struct symbol, rb_node);
326
327                 if (ip < s->start)
328                         n = n->rb_left;
329                 else if (ip > s->end || (ip == s->end && ip != s->start))
330                         n = n->rb_right;
331                 else
332                         return s;
333         }
334
335         return NULL;
336 }
337
338 static struct symbol *symbols__first(struct rb_root *symbols)
339 {
340         struct rb_node *n = rb_first(symbols);
341
342         if (n)
343                 return rb_entry(n, struct symbol, rb_node);
344
345         return NULL;
346 }
347
348 static struct symbol *symbols__next(struct symbol *sym)
349 {
350         struct rb_node *n = rb_next(&sym->rb_node);
351
352         if (n)
353                 return rb_entry(n, struct symbol, rb_node);
354
355         return NULL;
356 }
357
358 static void symbols__insert_by_name(struct rb_root *symbols, struct symbol *sym)
359 {
360         struct rb_node **p = &symbols->rb_node;
361         struct rb_node *parent = NULL;
362         struct symbol_name_rb_node *symn, *s;
363
364         symn = container_of(sym, struct symbol_name_rb_node, sym);
365
366         while (*p != NULL) {
367                 parent = *p;
368                 s = rb_entry(parent, struct symbol_name_rb_node, rb_node);
369                 if (strcmp(sym->name, s->sym.name) < 0)
370                         p = &(*p)->rb_left;
371                 else
372                         p = &(*p)->rb_right;
373         }
374         rb_link_node(&symn->rb_node, parent, p);
375         rb_insert_color(&symn->rb_node, symbols);
376 }
377
378 static void symbols__sort_by_name(struct rb_root *symbols,
379                                   struct rb_root *source)
380 {
381         struct rb_node *nd;
382
383         for (nd = rb_first(source); nd; nd = rb_next(nd)) {
384                 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
385                 symbols__insert_by_name(symbols, pos);
386         }
387 }
388
389 static struct symbol *symbols__find_by_name(struct rb_root *symbols,
390                                             const char *name)
391 {
392         struct rb_node *n;
393         struct symbol_name_rb_node *s = NULL;
394
395         if (symbols == NULL)
396                 return NULL;
397
398         n = symbols->rb_node;
399
400         while (n) {
401                 int cmp;
402
403                 s = rb_entry(n, struct symbol_name_rb_node, rb_node);
404                 cmp = arch__compare_symbol_names(name, s->sym.name);
405
406                 if (cmp < 0)
407                         n = n->rb_left;
408                 else if (cmp > 0)
409                         n = n->rb_right;
410                 else
411                         break;
412         }
413
414         if (n == NULL)
415                 return NULL;
416
417         /* return first symbol that has same name (if any) */
418         for (n = rb_prev(n); n; n = rb_prev(n)) {
419                 struct symbol_name_rb_node *tmp;
420
421                 tmp = rb_entry(n, struct symbol_name_rb_node, rb_node);
422                 if (arch__compare_symbol_names(tmp->sym.name, s->sym.name))
423                         break;
424
425                 s = tmp;
426         }
427
428         return &s->sym;
429 }
430
431 void dso__reset_find_symbol_cache(struct dso *dso)
432 {
433         enum map_type type;
434
435         for (type = MAP__FUNCTION; type <= MAP__VARIABLE; ++type) {
436                 dso->last_find_result[type].addr   = 0;
437                 dso->last_find_result[type].symbol = NULL;
438         }
439 }
440
441 void dso__insert_symbol(struct dso *dso, enum map_type type, struct symbol *sym)
442 {
443         __symbols__insert(&dso->symbols[type], sym, dso->kernel);
444
445         /* update the symbol cache if necessary */
446         if (dso->last_find_result[type].addr >= sym->start &&
447             (dso->last_find_result[type].addr < sym->end ||
448             sym->start == sym->end)) {
449                 dso->last_find_result[type].symbol = sym;
450         }
451 }
452
453 struct symbol *dso__find_symbol(struct dso *dso,
454                                 enum map_type type, u64 addr)
455 {
456         if (dso->last_find_result[type].addr != addr) {
457                 dso->last_find_result[type].addr   = addr;
458                 dso->last_find_result[type].symbol = symbols__find(&dso->symbols[type], addr);
459         }
460
461         return dso->last_find_result[type].symbol;
462 }
463
464 struct symbol *dso__first_symbol(struct dso *dso, enum map_type type)
465 {
466         return symbols__first(&dso->symbols[type]);
467 }
468
469 struct symbol *dso__next_symbol(struct symbol *sym)
470 {
471         return symbols__next(sym);
472 }
473
474 struct symbol *symbol__next_by_name(struct symbol *sym)
475 {
476         struct symbol_name_rb_node *s = container_of(sym, struct symbol_name_rb_node, sym);
477         struct rb_node *n = rb_next(&s->rb_node);
478
479         return n ? &rb_entry(n, struct symbol_name_rb_node, rb_node)->sym : NULL;
480 }
481
482  /*
483   * Teturns first symbol that matched with @name.
484   */
485 struct symbol *dso__find_symbol_by_name(struct dso *dso, enum map_type type,
486                                         const char *name)
487 {
488         return symbols__find_by_name(&dso->symbol_names[type], name);
489 }
490
491 void dso__sort_by_name(struct dso *dso, enum map_type type)
492 {
493         dso__set_sorted_by_name(dso, type);
494         return symbols__sort_by_name(&dso->symbol_names[type],
495                                      &dso->symbols[type]);
496 }
497
498 int modules__parse(const char *filename, void *arg,
499                    int (*process_module)(void *arg, const char *name,
500                                          u64 start))
501 {
502         char *line = NULL;
503         size_t n;
504         FILE *file;
505         int err = 0;
506
507         file = fopen(filename, "r");
508         if (file == NULL)
509                 return -1;
510
511         while (1) {
512                 char name[PATH_MAX];
513                 u64 start;
514                 char *sep;
515                 ssize_t line_len;
516
517                 line_len = getline(&line, &n, file);
518                 if (line_len < 0) {
519                         if (feof(file))
520                                 break;
521                         err = -1;
522                         goto out;
523                 }
524
525                 if (!line) {
526                         err = -1;
527                         goto out;
528                 }
529
530                 line[--line_len] = '\0'; /* \n */
531
532                 sep = strrchr(line, 'x');
533                 if (sep == NULL)
534                         continue;
535
536                 hex2u64(sep + 1, &start);
537
538                 sep = strchr(line, ' ');
539                 if (sep == NULL)
540                         continue;
541
542                 *sep = '\0';
543
544                 scnprintf(name, sizeof(name), "[%s]", line);
545
546                 err = process_module(arg, name, start);
547                 if (err)
548                         break;
549         }
550 out:
551         free(line);
552         fclose(file);
553         return err;
554 }
555
556 struct process_kallsyms_args {
557         struct map *map;
558         struct dso *dso;
559 };
560
561 /*
562  * These are symbols in the kernel image, so make sure that
563  * sym is from a kernel DSO.
564  */
565 static bool symbol__is_idle(const char *name)
566 {
567         const char * const idle_symbols[] = {
568                 "cpu_idle",
569                 "cpu_startup_entry",
570                 "intel_idle",
571                 "default_idle",
572                 "native_safe_halt",
573                 "enter_idle",
574                 "exit_idle",
575                 "mwait_idle",
576                 "mwait_idle_with_hints",
577                 "poll_idle",
578                 "ppc64_runlatch_off",
579                 "pseries_dedicated_idle_sleep",
580                 NULL
581         };
582         int i;
583
584         for (i = 0; idle_symbols[i]; i++) {
585                 if (!strcmp(idle_symbols[i], name))
586                         return true;
587         }
588
589         return false;
590 }
591
592 static int map__process_kallsym_symbol(void *arg, const char *name,
593                                        char type, u64 start)
594 {
595         struct symbol *sym;
596         struct process_kallsyms_args *a = arg;
597         struct rb_root *root = &a->dso->symbols[a->map->type];
598
599         if (!symbol_type__is_a(type, a->map->type))
600                 return 0;
601
602         /*
603          * module symbols are not sorted so we add all
604          * symbols, setting length to 0, and rely on
605          * symbols__fixup_end() to fix it up.
606          */
607         sym = symbol__new(start, 0, kallsyms2elf_binding(type), name);
608         if (sym == NULL)
609                 return -ENOMEM;
610         /*
611          * We will pass the symbols to the filter later, in
612          * map__split_kallsyms, when we have split the maps per module
613          */
614         __symbols__insert(root, sym, !strchr(name, '['));
615
616         return 0;
617 }
618
619 /*
620  * Loads the function entries in /proc/kallsyms into kernel_map->dso,
621  * so that we can in the next step set the symbol ->end address and then
622  * call kernel_maps__split_kallsyms.
623  */
624 static int dso__load_all_kallsyms(struct dso *dso, const char *filename,
625                                   struct map *map)
626 {
627         struct process_kallsyms_args args = { .map = map, .dso = dso, };
628         return kallsyms__parse(filename, &args, map__process_kallsym_symbol);
629 }
630
631 static int dso__split_kallsyms_for_kcore(struct dso *dso, struct map *map)
632 {
633         struct map_groups *kmaps = map__kmaps(map);
634         struct map *curr_map;
635         struct symbol *pos;
636         int count = 0;
637         struct rb_root old_root = dso->symbols[map->type];
638         struct rb_root *root = &dso->symbols[map->type];
639         struct rb_node *next = rb_first(root);
640
641         if (!kmaps)
642                 return -1;
643
644         *root = RB_ROOT;
645
646         while (next) {
647                 char *module;
648
649                 pos = rb_entry(next, struct symbol, rb_node);
650                 next = rb_next(&pos->rb_node);
651
652                 rb_erase_init(&pos->rb_node, &old_root);
653
654                 module = strchr(pos->name, '\t');
655                 if (module)
656                         *module = '\0';
657
658                 curr_map = map_groups__find(kmaps, map->type, pos->start);
659
660                 if (!curr_map) {
661                         symbol__delete(pos);
662                         continue;
663                 }
664
665                 pos->start -= curr_map->start - curr_map->pgoff;
666                 if (pos->end)
667                         pos->end -= curr_map->start - curr_map->pgoff;
668                 symbols__insert(&curr_map->dso->symbols[curr_map->type], pos);
669                 ++count;
670         }
671
672         /* Symbols have been adjusted */
673         dso->adjust_symbols = 1;
674
675         return count;
676 }
677
678 /*
679  * Split the symbols into maps, making sure there are no overlaps, i.e. the
680  * kernel range is broken in several maps, named [kernel].N, as we don't have
681  * the original ELF section names vmlinux have.
682  */
683 static int dso__split_kallsyms(struct dso *dso, struct map *map, u64 delta)
684 {
685         struct map_groups *kmaps = map__kmaps(map);
686         struct machine *machine;
687         struct map *curr_map = map;
688         struct symbol *pos;
689         int count = 0, moved = 0;
690         struct rb_root *root = &dso->symbols[map->type];
691         struct rb_node *next = rb_first(root);
692         int kernel_range = 0;
693
694         if (!kmaps)
695                 return -1;
696
697         machine = kmaps->machine;
698
699         while (next) {
700                 char *module;
701
702                 pos = rb_entry(next, struct symbol, rb_node);
703                 next = rb_next(&pos->rb_node);
704
705                 module = strchr(pos->name, '\t');
706                 if (module) {
707                         if (!symbol_conf.use_modules)
708                                 goto discard_symbol;
709
710                         *module++ = '\0';
711
712                         if (strcmp(curr_map->dso->short_name, module)) {
713                                 if (curr_map != map &&
714                                     dso->kernel == DSO_TYPE_GUEST_KERNEL &&
715                                     machine__is_default_guest(machine)) {
716                                         /*
717                                          * We assume all symbols of a module are
718                                          * continuous in * kallsyms, so curr_map
719                                          * points to a module and all its
720                                          * symbols are in its kmap. Mark it as
721                                          * loaded.
722                                          */
723                                         dso__set_loaded(curr_map->dso,
724                                                         curr_map->type);
725                                 }
726
727                                 curr_map = map_groups__find_by_name(kmaps,
728                                                         map->type, module);
729                                 if (curr_map == NULL) {
730                                         pr_debug("%s/proc/{kallsyms,modules} "
731                                                  "inconsistency while looking "
732                                                  "for \"%s\" module!\n",
733                                                  machine->root_dir, module);
734                                         curr_map = map;
735                                         goto discard_symbol;
736                                 }
737
738                                 if (curr_map->dso->loaded &&
739                                     !machine__is_default_guest(machine))
740                                         goto discard_symbol;
741                         }
742                         /*
743                          * So that we look just like we get from .ko files,
744                          * i.e. not prelinked, relative to map->start.
745                          */
746                         pos->start = curr_map->map_ip(curr_map, pos->start);
747                         pos->end   = curr_map->map_ip(curr_map, pos->end);
748                 } else if (curr_map != map) {
749                         char dso_name[PATH_MAX];
750                         struct dso *ndso;
751
752                         if (delta) {
753                                 /* Kernel was relocated at boot time */
754                                 pos->start -= delta;
755                                 pos->end -= delta;
756                         }
757
758                         if (count == 0) {
759                                 curr_map = map;
760                                 goto add_symbol;
761                         }
762
763                         if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
764                                 snprintf(dso_name, sizeof(dso_name),
765                                         "[guest.kernel].%d",
766                                         kernel_range++);
767                         else
768                                 snprintf(dso_name, sizeof(dso_name),
769                                         "[kernel].%d",
770                                         kernel_range++);
771
772                         ndso = dso__new(dso_name);
773                         if (ndso == NULL)
774                                 return -1;
775
776                         ndso->kernel = dso->kernel;
777
778                         curr_map = map__new2(pos->start, ndso, map->type);
779                         if (curr_map == NULL) {
780                                 dso__put(ndso);
781                                 return -1;
782                         }
783
784                         curr_map->map_ip = curr_map->unmap_ip = identity__map_ip;
785                         map_groups__insert(kmaps, curr_map);
786                         ++kernel_range;
787                 } else if (delta) {
788                         /* Kernel was relocated at boot time */
789                         pos->start -= delta;
790                         pos->end -= delta;
791                 }
792 add_symbol:
793                 if (curr_map != map) {
794                         rb_erase(&pos->rb_node, root);
795                         symbols__insert(&curr_map->dso->symbols[curr_map->type], pos);
796                         ++moved;
797                 } else
798                         ++count;
799
800                 continue;
801 discard_symbol:
802                 rb_erase(&pos->rb_node, root);
803                 symbol__delete(pos);
804         }
805
806         if (curr_map != map &&
807             dso->kernel == DSO_TYPE_GUEST_KERNEL &&
808             machine__is_default_guest(kmaps->machine)) {
809                 dso__set_loaded(curr_map->dso, curr_map->type);
810         }
811
812         return count + moved;
813 }
814
815 bool symbol__restricted_filename(const char *filename,
816                                  const char *restricted_filename)
817 {
818         bool restricted = false;
819
820         if (symbol_conf.kptr_restrict) {
821                 char *r = realpath(filename, NULL);
822
823                 if (r != NULL) {
824                         restricted = strcmp(r, restricted_filename) == 0;
825                         free(r);
826                         return restricted;
827                 }
828         }
829
830         return restricted;
831 }
832
833 struct module_info {
834         struct rb_node rb_node;
835         char *name;
836         u64 start;
837 };
838
839 static void add_module(struct module_info *mi, struct rb_root *modules)
840 {
841         struct rb_node **p = &modules->rb_node;
842         struct rb_node *parent = NULL;
843         struct module_info *m;
844
845         while (*p != NULL) {
846                 parent = *p;
847                 m = rb_entry(parent, struct module_info, rb_node);
848                 if (strcmp(mi->name, m->name) < 0)
849                         p = &(*p)->rb_left;
850                 else
851                         p = &(*p)->rb_right;
852         }
853         rb_link_node(&mi->rb_node, parent, p);
854         rb_insert_color(&mi->rb_node, modules);
855 }
856
857 static void delete_modules(struct rb_root *modules)
858 {
859         struct module_info *mi;
860         struct rb_node *next = rb_first(modules);
861
862         while (next) {
863                 mi = rb_entry(next, struct module_info, rb_node);
864                 next = rb_next(&mi->rb_node);
865                 rb_erase(&mi->rb_node, modules);
866                 zfree(&mi->name);
867                 free(mi);
868         }
869 }
870
871 static struct module_info *find_module(const char *name,
872                                        struct rb_root *modules)
873 {
874         struct rb_node *n = modules->rb_node;
875
876         while (n) {
877                 struct module_info *m;
878                 int cmp;
879
880                 m = rb_entry(n, struct module_info, rb_node);
881                 cmp = strcmp(name, m->name);
882                 if (cmp < 0)
883                         n = n->rb_left;
884                 else if (cmp > 0)
885                         n = n->rb_right;
886                 else
887                         return m;
888         }
889
890         return NULL;
891 }
892
893 static int __read_proc_modules(void *arg, const char *name, u64 start)
894 {
895         struct rb_root *modules = arg;
896         struct module_info *mi;
897
898         mi = zalloc(sizeof(struct module_info));
899         if (!mi)
900                 return -ENOMEM;
901
902         mi->name = strdup(name);
903         mi->start = start;
904
905         if (!mi->name) {
906                 free(mi);
907                 return -ENOMEM;
908         }
909
910         add_module(mi, modules);
911
912         return 0;
913 }
914
915 static int read_proc_modules(const char *filename, struct rb_root *modules)
916 {
917         if (symbol__restricted_filename(filename, "/proc/modules"))
918                 return -1;
919
920         if (modules__parse(filename, modules, __read_proc_modules)) {
921                 delete_modules(modules);
922                 return -1;
923         }
924
925         return 0;
926 }
927
928 int compare_proc_modules(const char *from, const char *to)
929 {
930         struct rb_root from_modules = RB_ROOT;
931         struct rb_root to_modules = RB_ROOT;
932         struct rb_node *from_node, *to_node;
933         struct module_info *from_m, *to_m;
934         int ret = -1;
935
936         if (read_proc_modules(from, &from_modules))
937                 return -1;
938
939         if (read_proc_modules(to, &to_modules))
940                 goto out_delete_from;
941
942         from_node = rb_first(&from_modules);
943         to_node = rb_first(&to_modules);
944         while (from_node) {
945                 if (!to_node)
946                         break;
947
948                 from_m = rb_entry(from_node, struct module_info, rb_node);
949                 to_m = rb_entry(to_node, struct module_info, rb_node);
950
951                 if (from_m->start != to_m->start ||
952                     strcmp(from_m->name, to_m->name))
953                         break;
954
955                 from_node = rb_next(from_node);
956                 to_node = rb_next(to_node);
957         }
958
959         if (!from_node && !to_node)
960                 ret = 0;
961
962         delete_modules(&to_modules);
963 out_delete_from:
964         delete_modules(&from_modules);
965
966         return ret;
967 }
968
969 static int do_validate_kcore_modules(const char *filename, struct map *map,
970                                   struct map_groups *kmaps)
971 {
972         struct rb_root modules = RB_ROOT;
973         struct map *old_map;
974         int err;
975
976         err = read_proc_modules(filename, &modules);
977         if (err)
978                 return err;
979
980         old_map = map_groups__first(kmaps, map->type);
981         while (old_map) {
982                 struct map *next = map_groups__next(old_map);
983                 struct module_info *mi;
984
985                 if (old_map == map || old_map->start == map->start) {
986                         /* The kernel map */
987                         old_map = next;
988                         continue;
989                 }
990
991                 /* Module must be in memory at the same address */
992                 mi = find_module(old_map->dso->short_name, &modules);
993                 if (!mi || mi->start != old_map->start) {
994                         err = -EINVAL;
995                         goto out;
996                 }
997
998                 old_map = next;
999         }
1000 out:
1001         delete_modules(&modules);
1002         return err;
1003 }
1004
1005 /*
1006  * If kallsyms is referenced by name then we look for filename in the same
1007  * directory.
1008  */
1009 static bool filename_from_kallsyms_filename(char *filename,
1010                                             const char *base_name,
1011                                             const char *kallsyms_filename)
1012 {
1013         char *name;
1014
1015         strcpy(filename, kallsyms_filename);
1016         name = strrchr(filename, '/');
1017         if (!name)
1018                 return false;
1019
1020         name += 1;
1021
1022         if (!strcmp(name, "kallsyms")) {
1023                 strcpy(name, base_name);
1024                 return true;
1025         }
1026
1027         return false;
1028 }
1029
1030 static int validate_kcore_modules(const char *kallsyms_filename,
1031                                   struct map *map)
1032 {
1033         struct map_groups *kmaps = map__kmaps(map);
1034         char modules_filename[PATH_MAX];
1035
1036         if (!kmaps)
1037                 return -EINVAL;
1038
1039         if (!filename_from_kallsyms_filename(modules_filename, "modules",
1040                                              kallsyms_filename))
1041                 return -EINVAL;
1042
1043         if (do_validate_kcore_modules(modules_filename, map, kmaps))
1044                 return -EINVAL;
1045
1046         return 0;
1047 }
1048
1049 static int validate_kcore_addresses(const char *kallsyms_filename,
1050                                     struct map *map)
1051 {
1052         struct kmap *kmap = map__kmap(map);
1053
1054         if (!kmap)
1055                 return -EINVAL;
1056
1057         if (kmap->ref_reloc_sym && kmap->ref_reloc_sym->name) {
1058                 u64 start;
1059
1060                 start = kallsyms__get_function_start(kallsyms_filename,
1061                                                      kmap->ref_reloc_sym->name);
1062                 if (start != kmap->ref_reloc_sym->addr)
1063                         return -EINVAL;
1064         }
1065
1066         return validate_kcore_modules(kallsyms_filename, map);
1067 }
1068
1069 struct kcore_mapfn_data {
1070         struct dso *dso;
1071         enum map_type type;
1072         struct list_head maps;
1073 };
1074
1075 static int kcore_mapfn(u64 start, u64 len, u64 pgoff, void *data)
1076 {
1077         struct kcore_mapfn_data *md = data;
1078         struct map *map;
1079
1080         map = map__new2(start, md->dso, md->type);
1081         if (map == NULL)
1082                 return -ENOMEM;
1083
1084         map->end = map->start + len;
1085         map->pgoff = pgoff;
1086
1087         list_add(&map->node, &md->maps);
1088
1089         return 0;
1090 }
1091
1092 static int dso__load_kcore(struct dso *dso, struct map *map,
1093                            const char *kallsyms_filename)
1094 {
1095         struct map_groups *kmaps = map__kmaps(map);
1096         struct machine *machine;
1097         struct kcore_mapfn_data md;
1098         struct map *old_map, *new_map, *replacement_map = NULL;
1099         bool is_64_bit;
1100         int err, fd;
1101         char kcore_filename[PATH_MAX];
1102         struct symbol *sym;
1103
1104         if (!kmaps)
1105                 return -EINVAL;
1106
1107         machine = kmaps->machine;
1108
1109         /* This function requires that the map is the kernel map */
1110         if (map != machine->vmlinux_maps[map->type])
1111                 return -EINVAL;
1112
1113         if (!filename_from_kallsyms_filename(kcore_filename, "kcore",
1114                                              kallsyms_filename))
1115                 return -EINVAL;
1116
1117         /* Modules and kernel must be present at their original addresses */
1118         if (validate_kcore_addresses(kallsyms_filename, map))
1119                 return -EINVAL;
1120
1121         md.dso = dso;
1122         md.type = map->type;
1123         INIT_LIST_HEAD(&md.maps);
1124
1125         fd = open(kcore_filename, O_RDONLY);
1126         if (fd < 0) {
1127                 pr_debug("Failed to open %s. Note /proc/kcore requires CAP_SYS_RAWIO capability to access.\n",
1128                          kcore_filename);
1129                 return -EINVAL;
1130         }
1131
1132         /* Read new maps into temporary lists */
1133         err = file__read_maps(fd, md.type == MAP__FUNCTION, kcore_mapfn, &md,
1134                               &is_64_bit);
1135         if (err)
1136                 goto out_err;
1137         dso->is_64_bit = is_64_bit;
1138
1139         if (list_empty(&md.maps)) {
1140                 err = -EINVAL;
1141                 goto out_err;
1142         }
1143
1144         /* Remove old maps */
1145         old_map = map_groups__first(kmaps, map->type);
1146         while (old_map) {
1147                 struct map *next = map_groups__next(old_map);
1148
1149                 if (old_map != map)
1150                         map_groups__remove(kmaps, old_map);
1151                 old_map = next;
1152         }
1153
1154         /* Find the kernel map using the first symbol */
1155         sym = dso__first_symbol(dso, map->type);
1156         list_for_each_entry(new_map, &md.maps, node) {
1157                 if (sym && sym->start >= new_map->start &&
1158                     sym->start < new_map->end) {
1159                         replacement_map = new_map;
1160                         break;
1161                 }
1162         }
1163
1164         if (!replacement_map)
1165                 replacement_map = list_entry(md.maps.next, struct map, node);
1166
1167         /* Add new maps */
1168         while (!list_empty(&md.maps)) {
1169                 new_map = list_entry(md.maps.next, struct map, node);
1170                 list_del_init(&new_map->node);
1171                 if (new_map == replacement_map) {
1172                         map->start      = new_map->start;
1173                         map->end        = new_map->end;
1174                         map->pgoff      = new_map->pgoff;
1175                         map->map_ip     = new_map->map_ip;
1176                         map->unmap_ip   = new_map->unmap_ip;
1177                         /* Ensure maps are correctly ordered */
1178                         map__get(map);
1179                         map_groups__remove(kmaps, map);
1180                         map_groups__insert(kmaps, map);
1181                         map__put(map);
1182                 } else {
1183                         map_groups__insert(kmaps, new_map);
1184                 }
1185
1186                 map__put(new_map);
1187         }
1188
1189         /*
1190          * Set the data type and long name so that kcore can be read via
1191          * dso__data_read_addr().
1192          */
1193         if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
1194                 dso->binary_type = DSO_BINARY_TYPE__GUEST_KCORE;
1195         else
1196                 dso->binary_type = DSO_BINARY_TYPE__KCORE;
1197         dso__set_long_name(dso, strdup(kcore_filename), true);
1198
1199         close(fd);
1200
1201         if (map->type == MAP__FUNCTION)
1202                 pr_debug("Using %s for kernel object code\n", kcore_filename);
1203         else
1204                 pr_debug("Using %s for kernel data\n", kcore_filename);
1205
1206         return 0;
1207
1208 out_err:
1209         while (!list_empty(&md.maps)) {
1210                 map = list_entry(md.maps.next, struct map, node);
1211                 list_del_init(&map->node);
1212                 map__put(map);
1213         }
1214         close(fd);
1215         return -EINVAL;
1216 }
1217
1218 /*
1219  * If the kernel is relocated at boot time, kallsyms won't match.  Compute the
1220  * delta based on the relocation reference symbol.
1221  */
1222 static int kallsyms__delta(struct map *map, const char *filename, u64 *delta)
1223 {
1224         struct kmap *kmap = map__kmap(map);
1225         u64 addr;
1226
1227         if (!kmap)
1228                 return -1;
1229
1230         if (!kmap->ref_reloc_sym || !kmap->ref_reloc_sym->name)
1231                 return 0;
1232
1233         addr = kallsyms__get_function_start(filename,
1234                                             kmap->ref_reloc_sym->name);
1235         if (!addr)
1236                 return -1;
1237
1238         *delta = addr - kmap->ref_reloc_sym->addr;
1239         return 0;
1240 }
1241
1242 int __dso__load_kallsyms(struct dso *dso, const char *filename,
1243                          struct map *map, bool no_kcore)
1244 {
1245         u64 delta = 0;
1246
1247         if (symbol__restricted_filename(filename, "/proc/kallsyms"))
1248                 return -1;
1249
1250         if (dso__load_all_kallsyms(dso, filename, map) < 0)
1251                 return -1;
1252
1253         if (kallsyms__delta(map, filename, &delta))
1254                 return -1;
1255
1256         symbols__fixup_end(&dso->symbols[map->type]);
1257         symbols__fixup_duplicate(&dso->symbols[map->type]);
1258
1259         if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
1260                 dso->symtab_type = DSO_BINARY_TYPE__GUEST_KALLSYMS;
1261         else
1262                 dso->symtab_type = DSO_BINARY_TYPE__KALLSYMS;
1263
1264         if (!no_kcore && !dso__load_kcore(dso, map, filename))
1265                 return dso__split_kallsyms_for_kcore(dso, map);
1266         else
1267                 return dso__split_kallsyms(dso, map, delta);
1268 }
1269
1270 int dso__load_kallsyms(struct dso *dso, const char *filename,
1271                        struct map *map)
1272 {
1273         return __dso__load_kallsyms(dso, filename, map, false);
1274 }
1275
1276 static int dso__load_perf_map(struct dso *dso, struct map *map)
1277 {
1278         char *line = NULL;
1279         size_t n;
1280         FILE *file;
1281         int nr_syms = 0;
1282
1283         file = fopen(dso->long_name, "r");
1284         if (file == NULL)
1285                 goto out_failure;
1286
1287         while (!feof(file)) {
1288                 u64 start, size;
1289                 struct symbol *sym;
1290                 int line_len, len;
1291
1292                 line_len = getline(&line, &n, file);
1293                 if (line_len < 0)
1294                         break;
1295
1296                 if (!line)
1297                         goto out_failure;
1298
1299                 line[--line_len] = '\0'; /* \n */
1300
1301                 len = hex2u64(line, &start);
1302
1303                 len++;
1304                 if (len + 2 >= line_len)
1305                         continue;
1306
1307                 len += hex2u64(line + len, &size);
1308
1309                 len++;
1310                 if (len + 2 >= line_len)
1311                         continue;
1312
1313                 sym = symbol__new(start, size, STB_GLOBAL, line + len);
1314
1315                 if (sym == NULL)
1316                         goto out_delete_line;
1317
1318                 symbols__insert(&dso->symbols[map->type], sym);
1319                 nr_syms++;
1320         }
1321
1322         free(line);
1323         fclose(file);
1324
1325         return nr_syms;
1326
1327 out_delete_line:
1328         free(line);
1329 out_failure:
1330         return -1;
1331 }
1332
1333 static bool dso__is_compatible_symtab_type(struct dso *dso, bool kmod,
1334                                            enum dso_binary_type type)
1335 {
1336         switch (type) {
1337         case DSO_BINARY_TYPE__JAVA_JIT:
1338         case DSO_BINARY_TYPE__DEBUGLINK:
1339         case DSO_BINARY_TYPE__SYSTEM_PATH_DSO:
1340         case DSO_BINARY_TYPE__FEDORA_DEBUGINFO:
1341         case DSO_BINARY_TYPE__UBUNTU_DEBUGINFO:
1342         case DSO_BINARY_TYPE__BUILDID_DEBUGINFO:
1343         case DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO:
1344                 return !kmod && dso->kernel == DSO_TYPE_USER;
1345
1346         case DSO_BINARY_TYPE__KALLSYMS:
1347         case DSO_BINARY_TYPE__VMLINUX:
1348         case DSO_BINARY_TYPE__KCORE:
1349                 return dso->kernel == DSO_TYPE_KERNEL;
1350
1351         case DSO_BINARY_TYPE__GUEST_KALLSYMS:
1352         case DSO_BINARY_TYPE__GUEST_VMLINUX:
1353         case DSO_BINARY_TYPE__GUEST_KCORE:
1354                 return dso->kernel == DSO_TYPE_GUEST_KERNEL;
1355
1356         case DSO_BINARY_TYPE__GUEST_KMODULE:
1357         case DSO_BINARY_TYPE__GUEST_KMODULE_COMP:
1358         case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE:
1359         case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP:
1360                 /*
1361                  * kernel modules know their symtab type - it's set when
1362                  * creating a module dso in machine__findnew_module_map().
1363                  */
1364                 return kmod && dso->symtab_type == type;
1365
1366         case DSO_BINARY_TYPE__BUILD_ID_CACHE:
1367                 return true;
1368
1369         case DSO_BINARY_TYPE__NOT_FOUND:
1370         default:
1371                 return false;
1372         }
1373 }
1374
1375 int dso__load(struct dso *dso, struct map *map)
1376 {
1377         char *name;
1378         int ret = -1;
1379         u_int i;
1380         struct machine *machine;
1381         char *root_dir = (char *) "";
1382         int ss_pos = 0;
1383         struct symsrc ss_[2];
1384         struct symsrc *syms_ss = NULL, *runtime_ss = NULL;
1385         bool kmod;
1386         unsigned char build_id[BUILD_ID_SIZE];
1387
1388         pthread_mutex_lock(&dso->lock);
1389
1390         /* check again under the dso->lock */
1391         if (dso__loaded(dso, map->type)) {
1392                 ret = 1;
1393                 goto out;
1394         }
1395
1396         if (dso->kernel) {
1397                 if (dso->kernel == DSO_TYPE_KERNEL)
1398                         ret = dso__load_kernel_sym(dso, map);
1399                 else if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
1400                         ret = dso__load_guest_kernel_sym(dso, map);
1401
1402                 goto out;
1403         }
1404
1405         if (map->groups && map->groups->machine)
1406                 machine = map->groups->machine;
1407         else
1408                 machine = NULL;
1409
1410         dso->adjust_symbols = 0;
1411
1412         if (strncmp(dso->name, "/tmp/perf-", 10) == 0) {
1413                 struct stat st;
1414
1415                 if (lstat(dso->name, &st) < 0)
1416                         goto out;
1417
1418                 if (!symbol_conf.force && st.st_uid && (st.st_uid != geteuid())) {
1419                         pr_warning("File %s not owned by current user or root, "
1420                                    "ignoring it (use -f to override).\n", dso->name);
1421                         goto out;
1422                 }
1423
1424                 ret = dso__load_perf_map(dso, map);
1425                 dso->symtab_type = ret > 0 ? DSO_BINARY_TYPE__JAVA_JIT :
1426                                              DSO_BINARY_TYPE__NOT_FOUND;
1427                 goto out;
1428         }
1429
1430         if (machine)
1431                 root_dir = machine->root_dir;
1432
1433         name = malloc(PATH_MAX);
1434         if (!name)
1435                 goto out;
1436
1437         kmod = dso->symtab_type == DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE ||
1438                 dso->symtab_type == DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP ||
1439                 dso->symtab_type == DSO_BINARY_TYPE__GUEST_KMODULE ||
1440                 dso->symtab_type == DSO_BINARY_TYPE__GUEST_KMODULE_COMP;
1441
1442
1443         /*
1444          * Read the build id if possible. This is required for
1445          * DSO_BINARY_TYPE__BUILDID_DEBUGINFO to work
1446          */
1447         if (is_regular_file(dso->long_name) &&
1448             filename__read_build_id(dso->long_name, build_id, BUILD_ID_SIZE) > 0)
1449                 dso__set_build_id(dso, build_id);
1450
1451         /*
1452          * Iterate over candidate debug images.
1453          * Keep track of "interesting" ones (those which have a symtab, dynsym,
1454          * and/or opd section) for processing.
1455          */
1456         for (i = 0; i < DSO_BINARY_TYPE__SYMTAB_CNT; i++) {
1457                 struct symsrc *ss = &ss_[ss_pos];
1458                 bool next_slot = false;
1459
1460                 enum dso_binary_type symtab_type = binary_type_symtab[i];
1461
1462                 if (!dso__is_compatible_symtab_type(dso, kmod, symtab_type))
1463                         continue;
1464
1465                 if (dso__read_binary_type_filename(dso, symtab_type,
1466                                                    root_dir, name, PATH_MAX))
1467                         continue;
1468
1469                 if (!is_regular_file(name))
1470                         continue;
1471
1472                 /* Name is now the name of the next image to try */
1473                 if (symsrc__init(ss, dso, name, symtab_type) < 0)
1474                         continue;
1475
1476                 if (!syms_ss && symsrc__has_symtab(ss)) {
1477                         syms_ss = ss;
1478                         next_slot = true;
1479                         if (!dso->symsrc_filename)
1480                                 dso->symsrc_filename = strdup(name);
1481                 }
1482
1483                 if (!runtime_ss && symsrc__possibly_runtime(ss)) {
1484                         runtime_ss = ss;
1485                         next_slot = true;
1486                 }
1487
1488                 if (next_slot) {
1489                         ss_pos++;
1490
1491                         if (syms_ss && runtime_ss)
1492                                 break;
1493                 } else {
1494                         symsrc__destroy(ss);
1495                 }
1496
1497         }
1498
1499         if (!runtime_ss && !syms_ss)
1500                 goto out_free;
1501
1502         if (runtime_ss && !syms_ss) {
1503                 syms_ss = runtime_ss;
1504         }
1505
1506         /* We'll have to hope for the best */
1507         if (!runtime_ss && syms_ss)
1508                 runtime_ss = syms_ss;
1509
1510         if (syms_ss && syms_ss->type == DSO_BINARY_TYPE__BUILD_ID_CACHE)
1511                 if (dso__build_id_is_kmod(dso, name, PATH_MAX))
1512                         kmod = true;
1513
1514         if (syms_ss)
1515                 ret = dso__load_sym(dso, map, syms_ss, runtime_ss, kmod);
1516         else
1517                 ret = -1;
1518
1519         if (ret > 0) {
1520                 int nr_plt;
1521
1522                 nr_plt = dso__synthesize_plt_symbols(dso, runtime_ss, map);
1523                 if (nr_plt > 0)
1524                         ret += nr_plt;
1525         }
1526
1527         for (; ss_pos > 0; ss_pos--)
1528                 symsrc__destroy(&ss_[ss_pos - 1]);
1529 out_free:
1530         free(name);
1531         if (ret < 0 && strstr(dso->name, " (deleted)") != NULL)
1532                 ret = 0;
1533 out:
1534         dso__set_loaded(dso, map->type);
1535         pthread_mutex_unlock(&dso->lock);
1536
1537         return ret;
1538 }
1539
1540 struct map *map_groups__find_by_name(struct map_groups *mg,
1541                                      enum map_type type, const char *name)
1542 {
1543         struct maps *maps = &mg->maps[type];
1544         struct map *map;
1545
1546         pthread_rwlock_rdlock(&maps->lock);
1547
1548         for (map = maps__first(maps); map; map = map__next(map)) {
1549                 if (map->dso && strcmp(map->dso->short_name, name) == 0)
1550                         goto out_unlock;
1551         }
1552
1553         map = NULL;
1554
1555 out_unlock:
1556         pthread_rwlock_unlock(&maps->lock);
1557         return map;
1558 }
1559
1560 int dso__load_vmlinux(struct dso *dso, struct map *map,
1561                       const char *vmlinux, bool vmlinux_allocated)
1562 {
1563         int err = -1;
1564         struct symsrc ss;
1565         char symfs_vmlinux[PATH_MAX];
1566         enum dso_binary_type symtab_type;
1567
1568         if (vmlinux[0] == '/')
1569                 snprintf(symfs_vmlinux, sizeof(symfs_vmlinux), "%s", vmlinux);
1570         else
1571                 symbol__join_symfs(symfs_vmlinux, vmlinux);
1572
1573         if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
1574                 symtab_type = DSO_BINARY_TYPE__GUEST_VMLINUX;
1575         else
1576                 symtab_type = DSO_BINARY_TYPE__VMLINUX;
1577
1578         if (symsrc__init(&ss, dso, symfs_vmlinux, symtab_type))
1579                 return -1;
1580
1581         err = dso__load_sym(dso, map, &ss, &ss, 0);
1582         symsrc__destroy(&ss);
1583
1584         if (err > 0) {
1585                 if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
1586                         dso->binary_type = DSO_BINARY_TYPE__GUEST_VMLINUX;
1587                 else
1588                         dso->binary_type = DSO_BINARY_TYPE__VMLINUX;
1589                 dso__set_long_name(dso, vmlinux, vmlinux_allocated);
1590                 dso__set_loaded(dso, map->type);
1591                 pr_debug("Using %s for symbols\n", symfs_vmlinux);
1592         }
1593
1594         return err;
1595 }
1596
1597 int dso__load_vmlinux_path(struct dso *dso, struct map *map)
1598 {
1599         int i, err = 0;
1600         char *filename = NULL;
1601
1602         pr_debug("Looking at the vmlinux_path (%d entries long)\n",
1603                  vmlinux_path__nr_entries + 1);
1604
1605         for (i = 0; i < vmlinux_path__nr_entries; ++i) {
1606                 err = dso__load_vmlinux(dso, map, vmlinux_path[i], false);
1607                 if (err > 0)
1608                         goto out;
1609         }
1610
1611         if (!symbol_conf.ignore_vmlinux_buildid)
1612                 filename = dso__build_id_filename(dso, NULL, 0);
1613         if (filename != NULL) {
1614                 err = dso__load_vmlinux(dso, map, filename, true);
1615                 if (err > 0)
1616                         goto out;
1617                 free(filename);
1618         }
1619 out:
1620         return err;
1621 }
1622
1623 static bool visible_dir_filter(const char *name, struct dirent *d)
1624 {
1625         if (d->d_type != DT_DIR)
1626                 return false;
1627         return lsdir_no_dot_filter(name, d);
1628 }
1629
1630 static int find_matching_kcore(struct map *map, char *dir, size_t dir_sz)
1631 {
1632         char kallsyms_filename[PATH_MAX];
1633         int ret = -1;
1634         struct strlist *dirs;
1635         struct str_node *nd;
1636
1637         dirs = lsdir(dir, visible_dir_filter);
1638         if (!dirs)
1639                 return -1;
1640
1641         strlist__for_each_entry(nd, dirs) {
1642                 scnprintf(kallsyms_filename, sizeof(kallsyms_filename),
1643                           "%s/%s/kallsyms", dir, nd->s);
1644                 if (!validate_kcore_addresses(kallsyms_filename, map)) {
1645                         strlcpy(dir, kallsyms_filename, dir_sz);
1646                         ret = 0;
1647                         break;
1648                 }
1649         }
1650
1651         strlist__delete(dirs);
1652
1653         return ret;
1654 }
1655
1656 /*
1657  * Use open(O_RDONLY) to check readability directly instead of access(R_OK)
1658  * since access(R_OK) only checks with real UID/GID but open() use effective
1659  * UID/GID and actual capabilities (e.g. /proc/kcore requires CAP_SYS_RAWIO).
1660  */
1661 static bool filename__readable(const char *file)
1662 {
1663         int fd = open(file, O_RDONLY);
1664         if (fd < 0)
1665                 return false;
1666         close(fd);
1667         return true;
1668 }
1669
1670 static char *dso__find_kallsyms(struct dso *dso, struct map *map)
1671 {
1672         u8 host_build_id[BUILD_ID_SIZE];
1673         char sbuild_id[SBUILD_ID_SIZE];
1674         bool is_host = false;
1675         char path[PATH_MAX];
1676
1677         if (!dso->has_build_id) {
1678                 /*
1679                  * Last resort, if we don't have a build-id and couldn't find
1680                  * any vmlinux file, try the running kernel kallsyms table.
1681                  */
1682                 goto proc_kallsyms;
1683         }
1684
1685         if (sysfs__read_build_id("/sys/kernel/notes", host_build_id,
1686                                  sizeof(host_build_id)) == 0)
1687                 is_host = dso__build_id_equal(dso, host_build_id);
1688
1689         /* Try a fast path for /proc/kallsyms if possible */
1690         if (is_host) {
1691                 /*
1692                  * Do not check the build-id cache, unless we know we cannot use
1693                  * /proc/kcore or module maps don't match to /proc/kallsyms.
1694                  * To check readability of /proc/kcore, do not use access(R_OK)
1695                  * since /proc/kcore requires CAP_SYS_RAWIO to read and access
1696                  * can't check it.
1697                  */
1698                 if (filename__readable("/proc/kcore") &&
1699                     !validate_kcore_addresses("/proc/kallsyms", map))
1700                         goto proc_kallsyms;
1701         }
1702
1703         build_id__sprintf(dso->build_id, sizeof(dso->build_id), sbuild_id);
1704
1705         /* Find kallsyms in build-id cache with kcore */
1706         scnprintf(path, sizeof(path), "%s/%s/%s",
1707                   buildid_dir, DSO__NAME_KCORE, sbuild_id);
1708
1709         if (!find_matching_kcore(map, path, sizeof(path)))
1710                 return strdup(path);
1711
1712         /* Use current /proc/kallsyms if possible */
1713         if (is_host) {
1714 proc_kallsyms:
1715                 return strdup("/proc/kallsyms");
1716         }
1717
1718         /* Finally, find a cache of kallsyms */
1719         if (!build_id_cache__kallsyms_path(sbuild_id, path, sizeof(path))) {
1720                 pr_err("No kallsyms or vmlinux with build-id %s was found\n",
1721                        sbuild_id);
1722                 return NULL;
1723         }
1724
1725         return strdup(path);
1726 }
1727
1728 static int dso__load_kernel_sym(struct dso *dso, struct map *map)
1729 {
1730         int err;
1731         const char *kallsyms_filename = NULL;
1732         char *kallsyms_allocated_filename = NULL;
1733         /*
1734          * Step 1: if the user specified a kallsyms or vmlinux filename, use
1735          * it and only it, reporting errors to the user if it cannot be used.
1736          *
1737          * For instance, try to analyse an ARM perf.data file _without_ a
1738          * build-id, or if the user specifies the wrong path to the right
1739          * vmlinux file, obviously we can't fallback to another vmlinux (a
1740          * x86_86 one, on the machine where analysis is being performed, say),
1741          * or worse, /proc/kallsyms.
1742          *
1743          * If the specified file _has_ a build-id and there is a build-id
1744          * section in the perf.data file, we will still do the expected
1745          * validation in dso__load_vmlinux and will bail out if they don't
1746          * match.
1747          */
1748         if (symbol_conf.kallsyms_name != NULL) {
1749                 kallsyms_filename = symbol_conf.kallsyms_name;
1750                 goto do_kallsyms;
1751         }
1752
1753         if (!symbol_conf.ignore_vmlinux && symbol_conf.vmlinux_name != NULL) {
1754                 return dso__load_vmlinux(dso, map, symbol_conf.vmlinux_name, false);
1755         }
1756
1757         if (!symbol_conf.ignore_vmlinux && vmlinux_path != NULL) {
1758                 err = dso__load_vmlinux_path(dso, map);
1759                 if (err > 0)
1760                         return err;
1761         }
1762
1763         /* do not try local files if a symfs was given */
1764         if (symbol_conf.symfs[0] != 0)
1765                 return -1;
1766
1767         kallsyms_allocated_filename = dso__find_kallsyms(dso, map);
1768         if (!kallsyms_allocated_filename)
1769                 return -1;
1770
1771         kallsyms_filename = kallsyms_allocated_filename;
1772
1773 do_kallsyms:
1774         err = dso__load_kallsyms(dso, kallsyms_filename, map);
1775         if (err > 0)
1776                 pr_debug("Using %s for symbols\n", kallsyms_filename);
1777         free(kallsyms_allocated_filename);
1778
1779         if (err > 0 && !dso__is_kcore(dso)) {
1780                 dso->binary_type = DSO_BINARY_TYPE__KALLSYMS;
1781                 dso__set_long_name(dso, DSO__NAME_KALLSYMS, false);
1782                 map__fixup_start(map);
1783                 map__fixup_end(map);
1784         }
1785
1786         return err;
1787 }
1788
1789 static int dso__load_guest_kernel_sym(struct dso *dso, struct map *map)
1790 {
1791         int err;
1792         const char *kallsyms_filename = NULL;
1793         struct machine *machine;
1794         char path[PATH_MAX];
1795
1796         if (!map->groups) {
1797                 pr_debug("Guest kernel map hasn't the point to groups\n");
1798                 return -1;
1799         }
1800         machine = map->groups->machine;
1801
1802         if (machine__is_default_guest(machine)) {
1803                 /*
1804                  * if the user specified a vmlinux filename, use it and only
1805                  * it, reporting errors to the user if it cannot be used.
1806                  * Or use file guest_kallsyms inputted by user on commandline
1807                  */
1808                 if (symbol_conf.default_guest_vmlinux_name != NULL) {
1809                         err = dso__load_vmlinux(dso, map,
1810                                                 symbol_conf.default_guest_vmlinux_name,
1811                                                 false);
1812                         return err;
1813                 }
1814
1815                 kallsyms_filename = symbol_conf.default_guest_kallsyms;
1816                 if (!kallsyms_filename)
1817                         return -1;
1818         } else {
1819                 sprintf(path, "%s/proc/kallsyms", machine->root_dir);
1820                 kallsyms_filename = path;
1821         }
1822
1823         err = dso__load_kallsyms(dso, kallsyms_filename, map);
1824         if (err > 0)
1825                 pr_debug("Using %s for symbols\n", kallsyms_filename);
1826         if (err > 0 && !dso__is_kcore(dso)) {
1827                 dso->binary_type = DSO_BINARY_TYPE__GUEST_KALLSYMS;
1828                 machine__mmap_name(machine, path, sizeof(path));
1829                 dso__set_long_name(dso, strdup(path), true);
1830                 map__fixup_start(map);
1831                 map__fixup_end(map);
1832         }
1833
1834         return err;
1835 }
1836
1837 static void vmlinux_path__exit(void)
1838 {
1839         while (--vmlinux_path__nr_entries >= 0)
1840                 zfree(&vmlinux_path[vmlinux_path__nr_entries]);
1841         vmlinux_path__nr_entries = 0;
1842
1843         zfree(&vmlinux_path);
1844 }
1845
1846 static const char * const vmlinux_paths[] = {
1847         "vmlinux",
1848         "/boot/vmlinux"
1849 };
1850
1851 static const char * const vmlinux_paths_upd[] = {
1852         "/boot/vmlinux-%s",
1853         "/usr/lib/debug/boot/vmlinux-%s",
1854         "/lib/modules/%s/build/vmlinux",
1855         "/usr/lib/debug/lib/modules/%s/vmlinux",
1856         "/usr/lib/debug/boot/vmlinux-%s.debug"
1857 };
1858
1859 static int vmlinux_path__add(const char *new_entry)
1860 {
1861         vmlinux_path[vmlinux_path__nr_entries] = strdup(new_entry);
1862         if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1863                 return -1;
1864         ++vmlinux_path__nr_entries;
1865
1866         return 0;
1867 }
1868
1869 static int vmlinux_path__init(struct perf_env *env)
1870 {
1871         struct utsname uts;
1872         char bf[PATH_MAX];
1873         char *kernel_version;
1874         unsigned int i;
1875
1876         vmlinux_path = malloc(sizeof(char *) * (ARRAY_SIZE(vmlinux_paths) +
1877                               ARRAY_SIZE(vmlinux_paths_upd)));
1878         if (vmlinux_path == NULL)
1879                 return -1;
1880
1881         for (i = 0; i < ARRAY_SIZE(vmlinux_paths); i++)
1882                 if (vmlinux_path__add(vmlinux_paths[i]) < 0)
1883                         goto out_fail;
1884
1885         /* only try kernel version if no symfs was given */
1886         if (symbol_conf.symfs[0] != 0)
1887                 return 0;
1888
1889         if (env) {
1890                 kernel_version = env->os_release;
1891         } else {
1892                 if (uname(&uts) < 0)
1893                         goto out_fail;
1894
1895                 kernel_version = uts.release;
1896         }
1897
1898         for (i = 0; i < ARRAY_SIZE(vmlinux_paths_upd); i++) {
1899                 snprintf(bf, sizeof(bf), vmlinux_paths_upd[i], kernel_version);
1900                 if (vmlinux_path__add(bf) < 0)
1901                         goto out_fail;
1902         }
1903
1904         return 0;
1905
1906 out_fail:
1907         vmlinux_path__exit();
1908         return -1;
1909 }
1910
1911 int setup_list(struct strlist **list, const char *list_str,
1912                       const char *list_name)
1913 {
1914         if (list_str == NULL)
1915                 return 0;
1916
1917         *list = strlist__new(list_str, NULL);
1918         if (!*list) {
1919                 pr_err("problems parsing %s list\n", list_name);
1920                 return -1;
1921         }
1922
1923         symbol_conf.has_filter = true;
1924         return 0;
1925 }
1926
1927 int setup_intlist(struct intlist **list, const char *list_str,
1928                   const char *list_name)
1929 {
1930         if (list_str == NULL)
1931                 return 0;
1932
1933         *list = intlist__new(list_str);
1934         if (!*list) {
1935                 pr_err("problems parsing %s list\n", list_name);
1936                 return -1;
1937         }
1938         return 0;
1939 }
1940
1941 static bool symbol__read_kptr_restrict(void)
1942 {
1943         bool value = false;
1944         FILE *fp = fopen("/proc/sys/kernel/kptr_restrict", "r");
1945
1946         if (fp != NULL) {
1947                 char line[8];
1948
1949                 if (fgets(line, sizeof(line), fp) != NULL)
1950                         value = (geteuid() != 0) ?
1951                                         (atoi(line) != 0) :
1952                                         (atoi(line) == 2);
1953
1954                 fclose(fp);
1955         }
1956
1957         return value;
1958 }
1959
1960 int symbol__annotation_init(void)
1961 {
1962         if (symbol_conf.initialized) {
1963                 pr_err("Annotation needs to be init before symbol__init()\n");
1964                 return -1;
1965         }
1966
1967         if (symbol_conf.init_annotation) {
1968                 pr_warning("Annotation being initialized multiple times\n");
1969                 return 0;
1970         }
1971
1972         symbol_conf.priv_size += sizeof(struct annotation);
1973         symbol_conf.init_annotation = true;
1974         return 0;
1975 }
1976
1977 int symbol__init(struct perf_env *env)
1978 {
1979         const char *symfs;
1980
1981         if (symbol_conf.initialized)
1982                 return 0;
1983
1984         symbol_conf.priv_size = PERF_ALIGN(symbol_conf.priv_size, sizeof(u64));
1985
1986         symbol__elf_init();
1987
1988         if (symbol_conf.sort_by_name)
1989                 symbol_conf.priv_size += (sizeof(struct symbol_name_rb_node) -
1990                                           sizeof(struct symbol));
1991
1992         if (symbol_conf.try_vmlinux_path && vmlinux_path__init(env) < 0)
1993                 return -1;
1994
1995         if (symbol_conf.field_sep && *symbol_conf.field_sep == '.') {
1996                 pr_err("'.' is the only non valid --field-separator argument\n");
1997                 return -1;
1998         }
1999
2000         if (setup_list(&symbol_conf.dso_list,
2001                        symbol_conf.dso_list_str, "dso") < 0)
2002                 return -1;
2003
2004         if (setup_list(&symbol_conf.comm_list,
2005                        symbol_conf.comm_list_str, "comm") < 0)
2006                 goto out_free_dso_list;
2007
2008         if (setup_intlist(&symbol_conf.pid_list,
2009                        symbol_conf.pid_list_str, "pid") < 0)
2010                 goto out_free_comm_list;
2011
2012         if (setup_intlist(&symbol_conf.tid_list,
2013                        symbol_conf.tid_list_str, "tid") < 0)
2014                 goto out_free_pid_list;
2015
2016         if (setup_list(&symbol_conf.sym_list,
2017                        symbol_conf.sym_list_str, "symbol") < 0)
2018                 goto out_free_tid_list;
2019
2020         /*
2021          * A path to symbols of "/" is identical to ""
2022          * reset here for simplicity.
2023          */
2024         symfs = realpath(symbol_conf.symfs, NULL);
2025         if (symfs == NULL)
2026                 symfs = symbol_conf.symfs;
2027         if (strcmp(symfs, "/") == 0)
2028                 symbol_conf.symfs = "";
2029         if (symfs != symbol_conf.symfs)
2030                 free((void *)symfs);
2031
2032         symbol_conf.kptr_restrict = symbol__read_kptr_restrict();
2033
2034         symbol_conf.initialized = true;
2035         return 0;
2036
2037 out_free_tid_list:
2038         intlist__delete(symbol_conf.tid_list);
2039 out_free_pid_list:
2040         intlist__delete(symbol_conf.pid_list);
2041 out_free_comm_list:
2042         strlist__delete(symbol_conf.comm_list);
2043 out_free_dso_list:
2044         strlist__delete(symbol_conf.dso_list);
2045         return -1;
2046 }
2047
2048 void symbol__exit(void)
2049 {
2050         if (!symbol_conf.initialized)
2051                 return;
2052         strlist__delete(symbol_conf.sym_list);
2053         strlist__delete(symbol_conf.dso_list);
2054         strlist__delete(symbol_conf.comm_list);
2055         intlist__delete(symbol_conf.tid_list);
2056         intlist__delete(symbol_conf.pid_list);
2057         vmlinux_path__exit();
2058         symbol_conf.sym_list = symbol_conf.dso_list = symbol_conf.comm_list = NULL;
2059         symbol_conf.initialized = false;
2060 }
2061
2062 int symbol__config_symfs(const struct option *opt __maybe_unused,
2063                          const char *dir, int unset __maybe_unused)
2064 {
2065         char *bf = NULL;
2066         int ret;
2067
2068         symbol_conf.symfs = strdup(dir);
2069         if (symbol_conf.symfs == NULL)
2070                 return -ENOMEM;
2071
2072         /* skip the locally configured cache if a symfs is given, and
2073          * config buildid dir to symfs/.debug
2074          */
2075         ret = asprintf(&bf, "%s/%s", dir, ".debug");
2076         if (ret < 0)
2077                 return -ENOMEM;
2078
2079         set_buildid_dir(bf);
2080
2081         free(bf);
2082         return 0;
2083 }