~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/tools/perf/util/symbol.c

Version: ~ [ linux-6.11.5 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.58 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.114 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.169 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.228 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.284 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.322 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.9 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 // SPDX-License-Identifier: GPL-2.0
  2 #include <dirent.h>
  3 #include <errno.h>
  4 #include <stdlib.h>
  5 #include <stdio.h>
  6 #include <string.h>
  7 #include <linux/capability.h>
  8 #include <linux/kernel.h>
  9 #include <linux/mman.h>
 10 #include <linux/string.h>
 11 #include <linux/time64.h>
 12 #include <sys/types.h>
 13 #include <sys/stat.h>
 14 #include <sys/param.h>
 15 #include <fcntl.h>
 16 #include <unistd.h>
 17 #include <inttypes.h>
 18 #include "annotate.h"
 19 #include "build-id.h"
 20 #include "cap.h"
 21 #include "dso.h"
 22 #include "util.h" // lsdir()
 23 #include "debug.h"
 24 #include "event.h"
 25 #include "machine.h"
 26 #include "map.h"
 27 #include "symbol.h"
 28 #include "map_symbol.h"
 29 #include "mem-events.h"
 30 #include "mem-info.h"
 31 #include "symsrc.h"
 32 #include "strlist.h"
 33 #include "intlist.h"
 34 #include "namespaces.h"
 35 #include "header.h"
 36 #include "path.h"
 37 #include <linux/ctype.h>
 38 #include <linux/zalloc.h>
 39 
 40 #include <elf.h>
 41 #include <limits.h>
 42 #include <symbol/kallsyms.h>
 43 #include <sys/utsname.h>
 44 
 45 static int dso__load_kernel_sym(struct dso *dso, struct map *map);
 46 static int dso__load_guest_kernel_sym(struct dso *dso, struct map *map);
 47 static bool symbol__is_idle(const char *name);
 48 
 49 int vmlinux_path__nr_entries;
 50 char **vmlinux_path;
 51 
 52 struct symbol_conf symbol_conf = {
 53         .nanosecs               = false,
 54         .use_modules            = true,
 55         .try_vmlinux_path       = true,
 56         .demangle               = true,
 57         .demangle_kernel        = false,
 58         .cumulate_callchain     = true,
 59         .time_quantum           = 100 * NSEC_PER_MSEC, /* 100ms */
 60         .show_hist_headers      = true,
 61         .symfs                  = "",
 62         .event_group            = true,
 63         .inline_name            = true,
 64         .res_sample             = 0,
 65 };
 66 
 67 struct map_list_node {
 68         struct list_head node;
 69         struct map *map;
 70 };
 71 
 72 static struct map_list_node *map_list_node__new(void)
 73 {
 74         return malloc(sizeof(struct map_list_node));
 75 }
 76 
 77 static enum dso_binary_type binary_type_symtab[] = {
 78         DSO_BINARY_TYPE__KALLSYMS,
 79         DSO_BINARY_TYPE__GUEST_KALLSYMS,
 80         DSO_BINARY_TYPE__JAVA_JIT,
 81         DSO_BINARY_TYPE__DEBUGLINK,
 82         DSO_BINARY_TYPE__BUILD_ID_CACHE,
 83         DSO_BINARY_TYPE__BUILD_ID_CACHE_DEBUGINFO,
 84         DSO_BINARY_TYPE__FEDORA_DEBUGINFO,
 85         DSO_BINARY_TYPE__UBUNTU_DEBUGINFO,
 86         DSO_BINARY_TYPE__BUILDID_DEBUGINFO,
 87         DSO_BINARY_TYPE__SYSTEM_PATH_DSO,
 88         DSO_BINARY_TYPE__GUEST_KMODULE,
 89         DSO_BINARY_TYPE__GUEST_KMODULE_COMP,
 90         DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE,
 91         DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP,
 92         DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO,
 93         DSO_BINARY_TYPE__MIXEDUP_UBUNTU_DEBUGINFO,
 94         DSO_BINARY_TYPE__NOT_FOUND,
 95 };
 96 
 97 #define DSO_BINARY_TYPE__SYMTAB_CNT ARRAY_SIZE(binary_type_symtab)
 98 
 99 static bool symbol_type__filter(char symbol_type)
100 {
101         symbol_type = toupper(symbol_type);
102         return symbol_type == 'T' || symbol_type == 'W' || symbol_type == 'D' || symbol_type == 'B';
103 }
104 
105 static int prefix_underscores_count(const char *str)
106 {
107         const char *tail = str;
108 
109         while (*tail == '_')
110                 tail++;
111 
112         return tail - str;
113 }
114 
115 const char * __weak arch__normalize_symbol_name(const char *name)
116 {
117         return name;
118 }
119 
120 int __weak arch__compare_symbol_names(const char *namea, const char *nameb)
121 {
122         return strcmp(namea, nameb);
123 }
124 
125 int __weak arch__compare_symbol_names_n(const char *namea, const char *nameb,
126                                         unsigned int n)
127 {
128         return strncmp(namea, nameb, n);
129 }
130 
131 int __weak arch__choose_best_symbol(struct symbol *syma,
132                                     struct symbol *symb __maybe_unused)
133 {
134         /* Avoid "SyS" kernel syscall aliases */
135         if (strlen(syma->name) >= 3 && !strncmp(syma->name, "SyS", 3))
136                 return SYMBOL_B;
137         if (strlen(syma->name) >= 10 && !strncmp(syma->name, "compat_SyS", 10))
138                 return SYMBOL_B;
139 
140         return SYMBOL_A;
141 }
142 
143 static int choose_best_symbol(struct symbol *syma, struct symbol *symb)
144 {
145         s64 a;
146         s64 b;
147         size_t na, nb;
148 
149         /* Prefer a symbol with non zero length */
150         a = syma->end - syma->start;
151         b = symb->end - symb->start;
152         if ((b == 0) && (a > 0))
153                 return SYMBOL_A;
154         else if ((a == 0) && (b > 0))
155                 return SYMBOL_B;
156 
157         /* Prefer a non weak symbol over a weak one */
158         a = syma->binding == STB_WEAK;
159         b = symb->binding == STB_WEAK;
160         if (b && !a)
161                 return SYMBOL_A;
162         if (a && !b)
163                 return SYMBOL_B;
164 
165         /* Prefer a global symbol over a non global one */
166         a = syma->binding == STB_GLOBAL;
167         b = symb->binding == STB_GLOBAL;
168         if (a && !b)
169                 return SYMBOL_A;
170         if (b && !a)
171                 return SYMBOL_B;
172 
173         /* Prefer a symbol with less underscores */
174         a = prefix_underscores_count(syma->name);
175         b = prefix_underscores_count(symb->name);
176         if (b > a)
177                 return SYMBOL_A;
178         else if (a > b)
179                 return SYMBOL_B;
180 
181         /* Choose the symbol with the longest name */
182         na = strlen(syma->name);
183         nb = strlen(symb->name);
184         if (na > nb)
185                 return SYMBOL_A;
186         else if (na < nb)
187                 return SYMBOL_B;
188 
189         return arch__choose_best_symbol(syma, symb);
190 }
191 
192 void symbols__fixup_duplicate(struct rb_root_cached *symbols)
193 {
194         struct rb_node *nd;
195         struct symbol *curr, *next;
196 
197         if (symbol_conf.allow_aliases)
198                 return;
199 
200         nd = rb_first_cached(symbols);
201 
202         while (nd) {
203                 curr = rb_entry(nd, struct symbol, rb_node);
204 again:
205                 nd = rb_next(&curr->rb_node);
206                 if (!nd)
207                         break;
208 
209                 next = rb_entry(nd, struct symbol, rb_node);
210                 if (curr->start != next->start)
211                         continue;
212 
213                 if (choose_best_symbol(curr, next) == SYMBOL_A) {
214                         if (next->type == STT_GNU_IFUNC)
215                                 curr->ifunc_alias = true;
216                         rb_erase_cached(&next->rb_node, symbols);
217                         symbol__delete(next);
218                         goto again;
219                 } else {
220                         if (curr->type == STT_GNU_IFUNC)
221                                 next->ifunc_alias = true;
222                         nd = rb_next(&curr->rb_node);
223                         rb_erase_cached(&curr->rb_node, symbols);
224                         symbol__delete(curr);
225                 }
226         }
227 }
228 
229 /* Update zero-sized symbols using the address of the next symbol */
230 void symbols__fixup_end(struct rb_root_cached *symbols, bool is_kallsyms)
231 {
232         struct rb_node *nd, *prevnd = rb_first_cached(symbols);
233         struct symbol *curr, *prev;
234 
235         if (prevnd == NULL)
236                 return;
237 
238         curr = rb_entry(prevnd, struct symbol, rb_node);
239 
240         for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
241                 prev = curr;
242                 curr = rb_entry(nd, struct symbol, rb_node);
243 
244                 /*
245                  * On some architecture kernel text segment start is located at
246                  * some low memory address, while modules are located at high
247                  * memory addresses (or vice versa).  The gap between end of
248                  * kernel text segment and beginning of first module's text
249                  * segment is very big.  Therefore do not fill this gap and do
250                  * not assign it to the kernel dso map (kallsyms).
251                  *
252                  * Also BPF code can be allocated separately from text segments
253                  * and modules.  So the last entry in a module should not fill
254                  * the gap too.
255                  *
256                  * In kallsyms, it determines module symbols using '[' character
257                  * like in:
258                  *   ffffffffc1937000 T hdmi_driver_init  [snd_hda_codec_hdmi]
259                  */
260                 if (prev->end == prev->start) {
261                         const char *prev_mod;
262                         const char *curr_mod;
263 
264                         if (!is_kallsyms) {
265                                 prev->end = curr->start;
266                                 continue;
267                         }
268 
269                         prev_mod = strchr(prev->name, '[');
270                         curr_mod = strchr(curr->name, '[');
271 
272                         /* Last kernel/module symbol mapped to end of page */
273                         if (!prev_mod != !curr_mod)
274                                 prev->end = roundup(prev->end + 4096, 4096);
275                         /* Last symbol in the previous module */
276                         else if (prev_mod && strcmp(prev_mod, curr_mod))
277                                 prev->end = roundup(prev->end + 4096, 4096);
278                         else
279                                 prev->end = curr->start;
280 
281                         pr_debug4("%s sym:%s end:%#" PRIx64 "\n",
282                                   __func__, prev->name, prev->end);
283                 }
284         }
285 
286         /* Last entry */
287         if (curr->end == curr->start)
288                 curr->end = roundup(curr->start, 4096) + 4096;
289 }
290 
291 struct symbol *symbol__new(u64 start, u64 len, u8 binding, u8 type, const char *name)
292 {
293         size_t namelen = strlen(name) + 1;
294         struct symbol *sym = calloc(1, (symbol_conf.priv_size +
295                                         sizeof(*sym) + namelen));
296         if (sym == NULL)
297                 return NULL;
298 
299         if (symbol_conf.priv_size) {
300                 if (symbol_conf.init_annotation) {
301                         struct annotation *notes = (void *)sym;
302                         annotation__init(notes);
303                 }
304                 sym = ((void *)sym) + symbol_conf.priv_size;
305         }
306 
307         sym->start   = start;
308         sym->end     = len ? start + len : start;
309         sym->type    = type;
310         sym->binding = binding;
311         sym->namelen = namelen - 1;
312 
313         pr_debug4("%s: %s %#" PRIx64 "-%#" PRIx64 "\n",
314                   __func__, name, start, sym->end);
315         memcpy(sym->name, name, namelen);
316 
317         return sym;
318 }
319 
320 void symbol__delete(struct symbol *sym)
321 {
322         if (symbol_conf.priv_size) {
323                 if (symbol_conf.init_annotation) {
324                         struct annotation *notes = symbol__annotation(sym);
325 
326                         annotation__exit(notes);
327                 }
328         }
329         free(((void *)sym) - symbol_conf.priv_size);
330 }
331 
332 void symbols__delete(struct rb_root_cached *symbols)
333 {
334         struct symbol *pos;
335         struct rb_node *next = rb_first_cached(symbols);
336 
337         while (next) {
338                 pos = rb_entry(next, struct symbol, rb_node);
339                 next = rb_next(&pos->rb_node);
340                 rb_erase_cached(&pos->rb_node, symbols);
341                 symbol__delete(pos);
342         }
343 }
344 
345 void __symbols__insert(struct rb_root_cached *symbols,
346                        struct symbol *sym, bool kernel)
347 {
348         struct rb_node **p = &symbols->rb_root.rb_node;
349         struct rb_node *parent = NULL;
350         const u64 ip = sym->start;
351         struct symbol *s;
352         bool leftmost = true;
353 
354         if (kernel) {
355                 const char *name = sym->name;
356                 /*
357                  * ppc64 uses function descriptors and appends a '.' to the
358                  * start of every instruction address. Remove it.
359                  */
360                 if (name[0] == '.')
361                         name++;
362                 sym->idle = symbol__is_idle(name);
363         }
364 
365         while (*p != NULL) {
366                 parent = *p;
367                 s = rb_entry(parent, struct symbol, rb_node);
368                 if (ip < s->start)
369                         p = &(*p)->rb_left;
370                 else {
371                         p = &(*p)->rb_right;
372                         leftmost = false;
373                 }
374         }
375         rb_link_node(&sym->rb_node, parent, p);
376         rb_insert_color_cached(&sym->rb_node, symbols, leftmost);
377 }
378 
379 void symbols__insert(struct rb_root_cached *symbols, struct symbol *sym)
380 {
381         __symbols__insert(symbols, sym, false);
382 }
383 
384 static struct symbol *symbols__find(struct rb_root_cached *symbols, u64 ip)
385 {
386         struct rb_node *n;
387 
388         if (symbols == NULL)
389                 return NULL;
390 
391         n = symbols->rb_root.rb_node;
392 
393         while (n) {
394                 struct symbol *s = rb_entry(n, struct symbol, rb_node);
395 
396                 if (ip < s->start)
397                         n = n->rb_left;
398                 else if (ip > s->end || (ip == s->end && ip != s->start))
399                         n = n->rb_right;
400                 else
401                         return s;
402         }
403 
404         return NULL;
405 }
406 
407 static struct symbol *symbols__first(struct rb_root_cached *symbols)
408 {
409         struct rb_node *n = rb_first_cached(symbols);
410 
411         if (n)
412                 return rb_entry(n, struct symbol, rb_node);
413 
414         return NULL;
415 }
416 
417 static struct symbol *symbols__last(struct rb_root_cached *symbols)
418 {
419         struct rb_node *n = rb_last(&symbols->rb_root);
420 
421         if (n)
422                 return rb_entry(n, struct symbol, rb_node);
423 
424         return NULL;
425 }
426 
427 static struct symbol *symbols__next(struct symbol *sym)
428 {
429         struct rb_node *n = rb_next(&sym->rb_node);
430 
431         if (n)
432                 return rb_entry(n, struct symbol, rb_node);
433 
434         return NULL;
435 }
436 
437 static int symbols__sort_name_cmp(const void *vlhs, const void *vrhs)
438 {
439         const struct symbol *lhs = *((const struct symbol **)vlhs);
440         const struct symbol *rhs = *((const struct symbol **)vrhs);
441 
442         return strcmp(lhs->name, rhs->name);
443 }
444 
445 static struct symbol **symbols__sort_by_name(struct rb_root_cached *source, size_t *len)
446 {
447         struct rb_node *nd;
448         struct symbol **result;
449         size_t i = 0, size = 0;
450 
451         for (nd = rb_first_cached(source); nd; nd = rb_next(nd))
452                 size++;
453 
454         result = malloc(sizeof(*result) * size);
455         if (!result)
456                 return NULL;
457 
458         for (nd = rb_first_cached(source); nd; nd = rb_next(nd)) {
459                 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
460 
461                 result[i++] = pos;
462         }
463         qsort(result, size, sizeof(*result), symbols__sort_name_cmp);
464         *len = size;
465         return result;
466 }
467 
468 int symbol__match_symbol_name(const char *name, const char *str,
469                               enum symbol_tag_include includes)
470 {
471         const char *versioning;
472 
473         if (includes == SYMBOL_TAG_INCLUDE__DEFAULT_ONLY &&
474             (versioning = strstr(name, "@@"))) {
475                 int len = strlen(str);
476 
477                 if (len < versioning - name)
478                         len = versioning - name;
479 
480                 return arch__compare_symbol_names_n(name, str, len);
481         } else
482                 return arch__compare_symbol_names(name, str);
483 }
484 
485 static struct symbol *symbols__find_by_name(struct symbol *symbols[],
486                                             size_t symbols_len,
487                                             const char *name,
488                                             enum symbol_tag_include includes,
489                                             size_t *found_idx)
490 {
491         size_t i, lower = 0, upper = symbols_len;
492         struct symbol *s = NULL;
493 
494         if (found_idx)
495                 *found_idx = SIZE_MAX;
496 
497         if (!symbols_len)
498                 return NULL;
499 
500         while (lower < upper) {
501                 int cmp;
502 
503                 i = (lower + upper) / 2;
504                 cmp = symbol__match_symbol_name(symbols[i]->name, name, includes);
505 
506                 if (cmp > 0)
507                         upper = i;
508                 else if (cmp < 0)
509                         lower = i + 1;
510                 else {
511                         if (found_idx)
512                                 *found_idx = i;
513                         s = symbols[i];
514                         break;
515                 }
516         }
517         if (s && includes != SYMBOL_TAG_INCLUDE__DEFAULT_ONLY) {
518                 /* return first symbol that has same name (if any) */
519                 for (; i > 0; i--) {
520                         struct symbol *tmp = symbols[i - 1];
521 
522                         if (!arch__compare_symbol_names(tmp->name, s->name)) {
523                                 if (found_idx)
524                                         *found_idx = i - 1;
525                                 s = tmp;
526                         } else
527                                 break;
528                 }
529         }
530         assert(!found_idx || !s || s == symbols[*found_idx]);
531         return s;
532 }
533 
534 void dso__reset_find_symbol_cache(struct dso *dso)
535 {
536         dso__set_last_find_result_addr(dso, 0);
537         dso__set_last_find_result_symbol(dso, NULL);
538 }
539 
540 void dso__insert_symbol(struct dso *dso, struct symbol *sym)
541 {
542         __symbols__insert(dso__symbols(dso), sym, dso__kernel(dso));
543 
544         /* update the symbol cache if necessary */
545         if (dso__last_find_result_addr(dso) >= sym->start &&
546             (dso__last_find_result_addr(dso) < sym->end ||
547             sym->start == sym->end)) {
548                 dso__set_last_find_result_symbol(dso, sym);
549         }
550 }
551 
552 void dso__delete_symbol(struct dso *dso, struct symbol *sym)
553 {
554         rb_erase_cached(&sym->rb_node, dso__symbols(dso));
555         symbol__delete(sym);
556         dso__reset_find_symbol_cache(dso);
557 }
558 
559 struct symbol *dso__find_symbol(struct dso *dso, u64 addr)
560 {
561         if (dso__last_find_result_addr(dso) != addr || dso__last_find_result_symbol(dso) == NULL) {
562                 dso__set_last_find_result_addr(dso, addr);
563                 dso__set_last_find_result_symbol(dso, symbols__find(dso__symbols(dso), addr));
564         }
565 
566         return dso__last_find_result_symbol(dso);
567 }
568 
569 struct symbol *dso__find_symbol_nocache(struct dso *dso, u64 addr)
570 {
571         return symbols__find(dso__symbols(dso), addr);
572 }
573 
574 struct symbol *dso__first_symbol(struct dso *dso)
575 {
576         return symbols__first(dso__symbols(dso));
577 }
578 
579 struct symbol *dso__last_symbol(struct dso *dso)
580 {
581         return symbols__last(dso__symbols(dso));
582 }
583 
584 struct symbol *dso__next_symbol(struct symbol *sym)
585 {
586         return symbols__next(sym);
587 }
588 
589 struct symbol *dso__next_symbol_by_name(struct dso *dso, size_t *idx)
590 {
591         if (*idx + 1 >= dso__symbol_names_len(dso))
592                 return NULL;
593 
594         ++*idx;
595         return dso__symbol_names(dso)[*idx];
596 }
597 
598  /*
599   * Returns first symbol that matched with @name.
600   */
601 struct symbol *dso__find_symbol_by_name(struct dso *dso, const char *name, size_t *idx)
602 {
603         struct symbol *s = symbols__find_by_name(dso__symbol_names(dso),
604                                                  dso__symbol_names_len(dso),
605                                                  name, SYMBOL_TAG_INCLUDE__NONE, idx);
606         if (!s) {
607                 s = symbols__find_by_name(dso__symbol_names(dso), dso__symbol_names_len(dso),
608                                           name, SYMBOL_TAG_INCLUDE__DEFAULT_ONLY, idx);
609         }
610         return s;
611 }
612 
613 void dso__sort_by_name(struct dso *dso)
614 {
615         mutex_lock(dso__lock(dso));
616         if (!dso__sorted_by_name(dso)) {
617                 size_t len;
618 
619                 dso__set_symbol_names(dso, symbols__sort_by_name(dso__symbols(dso), &len));
620                 if (dso__symbol_names(dso)) {
621                         dso__set_symbol_names_len(dso, len);
622                         dso__set_sorted_by_name(dso);
623                 }
624         }
625         mutex_unlock(dso__lock(dso));
626 }
627 
628 /*
629  * While we find nice hex chars, build a long_val.
630  * Return number of chars processed.
631  */
632 static int hex2u64(const char *ptr, u64 *long_val)
633 {
634         char *p;
635 
636         *long_val = strtoull(ptr, &p, 16);
637 
638         return p - ptr;
639 }
640 
641 
642 int modules__parse(const char *filename, void *arg,
643                    int (*process_module)(void *arg, const char *name,
644                                          u64 start, u64 size))
645 {
646         char *line = NULL;
647         size_t n;
648         FILE *file;
649         int err = 0;
650 
651         file = fopen(filename, "r");
652         if (file == NULL)
653                 return -1;
654 
655         while (1) {
656                 char name[PATH_MAX];
657                 u64 start, size;
658                 char *sep, *endptr;
659                 ssize_t line_len;
660 
661                 line_len = getline(&line, &n, file);
662                 if (line_len < 0) {
663                         if (feof(file))
664                                 break;
665                         err = -1;
666                         goto out;
667                 }
668 
669                 if (!line) {
670                         err = -1;
671                         goto out;
672                 }
673 
674                 line[--line_len] = '\0'; /* \n */
675 
676                 sep = strrchr(line, 'x');
677                 if (sep == NULL)
678                         continue;
679 
680                 hex2u64(sep + 1, &start);
681 
682                 sep = strchr(line, ' ');
683                 if (sep == NULL)
684                         continue;
685 
686                 *sep = '\0';
687 
688                 scnprintf(name, sizeof(name), "[%s]", line);
689 
690                 size = strtoul(sep + 1, &endptr, 0);
691                 if (*endptr != ' ' && *endptr != '\t')
692                         continue;
693 
694                 err = process_module(arg, name, start, size);
695                 if (err)
696                         break;
697         }
698 out:
699         free(line);
700         fclose(file);
701         return err;
702 }
703 
704 /*
705  * These are symbols in the kernel image, so make sure that
706  * sym is from a kernel DSO.
707  */
708 static bool symbol__is_idle(const char *name)
709 {
710         const char * const idle_symbols[] = {
711                 "acpi_idle_do_entry",
712                 "acpi_processor_ffh_cstate_enter",
713                 "arch_cpu_idle",
714                 "cpu_idle",
715                 "cpu_startup_entry",
716                 "idle_cpu",
717                 "intel_idle",
718                 "intel_idle_ibrs",
719                 "default_idle",
720                 "native_safe_halt",
721                 "enter_idle",
722                 "exit_idle",
723                 "mwait_idle",
724                 "mwait_idle_with_hints",
725                 "mwait_idle_with_hints.constprop.0",
726                 "poll_idle",
727                 "ppc64_runlatch_off",
728                 "pseries_dedicated_idle_sleep",
729                 "psw_idle",
730                 "psw_idle_exit",
731                 NULL
732         };
733         int i;
734         static struct strlist *idle_symbols_list;
735 
736         if (idle_symbols_list)
737                 return strlist__has_entry(idle_symbols_list, name);
738 
739         idle_symbols_list = strlist__new(NULL, NULL);
740 
741         for (i = 0; idle_symbols[i]; i++)
742                 strlist__add(idle_symbols_list, idle_symbols[i]);
743 
744         return strlist__has_entry(idle_symbols_list, name);
745 }
746 
747 static int map__process_kallsym_symbol(void *arg, const char *name,
748                                        char type, u64 start)
749 {
750         struct symbol *sym;
751         struct dso *dso = arg;
752         struct rb_root_cached *root = dso__symbols(dso);
753 
754         if (!symbol_type__filter(type))
755                 return 0;
756 
757         /* Ignore local symbols for ARM modules */
758         if (name[0] == '$')
759                 return 0;
760 
761         /*
762          * module symbols are not sorted so we add all
763          * symbols, setting length to 0, and rely on
764          * symbols__fixup_end() to fix it up.
765          */
766         sym = symbol__new(start, 0, kallsyms2elf_binding(type), kallsyms2elf_type(type), name);
767         if (sym == NULL)
768                 return -ENOMEM;
769         /*
770          * We will pass the symbols to the filter later, in
771          * map__split_kallsyms, when we have split the maps per module
772          */
773         __symbols__insert(root, sym, !strchr(name, '['));
774 
775         return 0;
776 }
777 
778 /*
779  * Loads the function entries in /proc/kallsyms into kernel_map->dso,
780  * so that we can in the next step set the symbol ->end address and then
781  * call kernel_maps__split_kallsyms.
782  */
783 static int dso__load_all_kallsyms(struct dso *dso, const char *filename)
784 {
785         return kallsyms__parse(filename, dso, map__process_kallsym_symbol);
786 }
787 
788 static int maps__split_kallsyms_for_kcore(struct maps *kmaps, struct dso *dso)
789 {
790         struct symbol *pos;
791         int count = 0;
792         struct rb_root_cached *root = dso__symbols(dso);
793         struct rb_root_cached old_root = *root;
794         struct rb_node *next = rb_first_cached(root);
795 
796         if (!kmaps)
797                 return -1;
798 
799         *root = RB_ROOT_CACHED;
800 
801         while (next) {
802                 struct map *curr_map;
803                 struct dso *curr_map_dso;
804                 char *module;
805 
806                 pos = rb_entry(next, struct symbol, rb_node);
807                 next = rb_next(&pos->rb_node);
808 
809                 rb_erase_cached(&pos->rb_node, &old_root);
810                 RB_CLEAR_NODE(&pos->rb_node);
811                 module = strchr(pos->name, '\t');
812                 if (module)
813                         *module = '\0';
814 
815                 curr_map = maps__find(kmaps, pos->start);
816 
817                 if (!curr_map) {
818                         symbol__delete(pos);
819                         continue;
820                 }
821                 curr_map_dso = map__dso(curr_map);
822                 pos->start -= map__start(curr_map) - map__pgoff(curr_map);
823                 if (pos->end > map__end(curr_map))
824                         pos->end = map__end(curr_map);
825                 if (pos->end)
826                         pos->end -= map__start(curr_map) - map__pgoff(curr_map);
827                 symbols__insert(dso__symbols(curr_map_dso), pos);
828                 ++count;
829                 map__put(curr_map);
830         }
831 
832         /* Symbols have been adjusted */
833         dso__set_adjust_symbols(dso, true);
834 
835         return count;
836 }
837 
838 /*
839  * Split the symbols into maps, making sure there are no overlaps, i.e. the
840  * kernel range is broken in several maps, named [kernel].N, as we don't have
841  * the original ELF section names vmlinux have.
842  */
843 static int maps__split_kallsyms(struct maps *kmaps, struct dso *dso, u64 delta,
844                                 struct map *initial_map)
845 {
846         struct machine *machine;
847         struct map *curr_map = map__get(initial_map);
848         struct symbol *pos;
849         int count = 0, moved = 0;
850         struct rb_root_cached *root = dso__symbols(dso);
851         struct rb_node *next = rb_first_cached(root);
852         int kernel_range = 0;
853         bool x86_64;
854 
855         if (!kmaps)
856                 return -1;
857 
858         machine = maps__machine(kmaps);
859 
860         x86_64 = machine__is(machine, "x86_64");
861 
862         while (next) {
863                 char *module;
864 
865                 pos = rb_entry(next, struct symbol, rb_node);
866                 next = rb_next(&pos->rb_node);
867 
868                 module = strchr(pos->name, '\t');
869                 if (module) {
870                         struct dso *curr_map_dso;
871 
872                         if (!symbol_conf.use_modules)
873                                 goto discard_symbol;
874 
875                         *module++ = '\0';
876                         curr_map_dso = map__dso(curr_map);
877                         if (strcmp(dso__short_name(curr_map_dso), module)) {
878                                 if (!RC_CHK_EQUAL(curr_map, initial_map) &&
879                                     dso__kernel(dso) == DSO_SPACE__KERNEL_GUEST &&
880                                     machine__is_default_guest(machine)) {
881                                         /*
882                                          * We assume all symbols of a module are
883                                          * continuous in * kallsyms, so curr_map
884                                          * points to a module and all its
885                                          * symbols are in its kmap. Mark it as
886                                          * loaded.
887                                          */
888                                         dso__set_loaded(curr_map_dso);
889                                 }
890 
891                                 map__zput(curr_map);
892                                 curr_map = maps__find_by_name(kmaps, module);
893                                 if (curr_map == NULL) {
894                                         pr_debug("%s/proc/{kallsyms,modules} "
895                                                  "inconsistency while looking "
896                                                  "for \"%s\" module!\n",
897                                                  machine->root_dir, module);
898                                         curr_map = map__get(initial_map);
899                                         goto discard_symbol;
900                                 }
901                                 curr_map_dso = map__dso(curr_map);
902                                 if (dso__loaded(curr_map_dso) &&
903                                     !machine__is_default_guest(machine))
904                                         goto discard_symbol;
905                         }
906                         /*
907                          * So that we look just like we get from .ko files,
908                          * i.e. not prelinked, relative to initial_map->start.
909                          */
910                         pos->start = map__map_ip(curr_map, pos->start);
911                         pos->end   = map__map_ip(curr_map, pos->end);
912                 } else if (x86_64 && is_entry_trampoline(pos->name)) {
913                         /*
914                          * These symbols are not needed anymore since the
915                          * trampoline maps refer to the text section and it's
916                          * symbols instead. Avoid having to deal with
917                          * relocations, and the assumption that the first symbol
918                          * is the start of kernel text, by simply removing the
919                          * symbols at this point.
920                          */
921                         goto discard_symbol;
922                 } else if (!RC_CHK_EQUAL(curr_map, initial_map)) {
923                         char dso_name[PATH_MAX];
924                         struct dso *ndso;
925 
926                         if (delta) {
927                                 /* Kernel was relocated at boot time */
928                                 pos->start -= delta;
929                                 pos->end -= delta;
930                         }
931 
932                         if (count == 0) {
933                                 map__zput(curr_map);
934                                 curr_map = map__get(initial_map);
935                                 goto add_symbol;
936                         }
937 
938                         if (dso__kernel(dso) == DSO_SPACE__KERNEL_GUEST)
939                                 snprintf(dso_name, sizeof(dso_name),
940                                         "[guest.kernel].%d",
941                                         kernel_range++);
942                         else
943                                 snprintf(dso_name, sizeof(dso_name),
944                                         "[kernel].%d",
945                                         kernel_range++);
946 
947                         ndso = dso__new(dso_name);
948                         map__zput(curr_map);
949                         if (ndso == NULL)
950                                 return -1;
951 
952                         dso__set_kernel(ndso, dso__kernel(dso));
953 
954                         curr_map = map__new2(pos->start, ndso);
955                         if (curr_map == NULL) {
956                                 dso__put(ndso);
957                                 return -1;
958                         }
959 
960                         map__set_mapping_type(curr_map, MAPPING_TYPE__IDENTITY);
961                         if (maps__insert(kmaps, curr_map)) {
962                                 map__zput(curr_map);
963                                 dso__put(ndso);
964                                 return -1;
965                         }
966                         ++kernel_range;
967                 } else if (delta) {
968                         /* Kernel was relocated at boot time */
969                         pos->start -= delta;
970                         pos->end -= delta;
971                 }
972 add_symbol:
973                 if (!RC_CHK_EQUAL(curr_map, initial_map)) {
974                         struct dso *curr_map_dso = map__dso(curr_map);
975 
976                         rb_erase_cached(&pos->rb_node, root);
977                         symbols__insert(dso__symbols(curr_map_dso), pos);
978                         ++moved;
979                 } else
980                         ++count;
981 
982                 continue;
983 discard_symbol:
984                 rb_erase_cached(&pos->rb_node, root);
985                 symbol__delete(pos);
986         }
987 
988         if (!RC_CHK_EQUAL(curr_map, initial_map) &&
989             dso__kernel(dso) == DSO_SPACE__KERNEL_GUEST &&
990             machine__is_default_guest(maps__machine(kmaps))) {
991                 dso__set_loaded(map__dso(curr_map));
992         }
993         map__put(curr_map);
994         return count + moved;
995 }
996 
997 bool symbol__restricted_filename(const char *filename,
998                                  const char *restricted_filename)
999 {
1000         bool restricted = false;
1001 
1002         if (symbol_conf.kptr_restrict) {
1003                 char *r = realpath(filename, NULL);
1004 
1005                 if (r != NULL) {
1006                         restricted = strcmp(r, restricted_filename) == 0;
1007                         free(r);
1008                         return restricted;
1009                 }
1010         }
1011 
1012         return restricted;
1013 }
1014 
1015 struct module_info {
1016         struct rb_node rb_node;
1017         char *name;
1018         u64 start;
1019 };
1020 
1021 static void add_module(struct module_info *mi, struct rb_root *modules)
1022 {
1023         struct rb_node **p = &modules->rb_node;
1024         struct rb_node *parent = NULL;
1025         struct module_info *m;
1026 
1027         while (*p != NULL) {
1028                 parent = *p;
1029                 m = rb_entry(parent, struct module_info, rb_node);
1030                 if (strcmp(mi->name, m->name) < 0)
1031                         p = &(*p)->rb_left;
1032                 else
1033                         p = &(*p)->rb_right;
1034         }
1035         rb_link_node(&mi->rb_node, parent, p);
1036         rb_insert_color(&mi->rb_node, modules);
1037 }
1038 
1039 static void delete_modules(struct rb_root *modules)
1040 {
1041         struct module_info *mi;
1042         struct rb_node *next = rb_first(modules);
1043 
1044         while (next) {
1045                 mi = rb_entry(next, struct module_info, rb_node);
1046                 next = rb_next(&mi->rb_node);
1047                 rb_erase(&mi->rb_node, modules);
1048                 zfree(&mi->name);
1049                 free(mi);
1050         }
1051 }
1052 
1053 static struct module_info *find_module(const char *name,
1054                                        struct rb_root *modules)
1055 {
1056         struct rb_node *n = modules->rb_node;
1057 
1058         while (n) {
1059                 struct module_info *m;
1060                 int cmp;
1061 
1062                 m = rb_entry(n, struct module_info, rb_node);
1063                 cmp = strcmp(name, m->name);
1064                 if (cmp < 0)
1065                         n = n->rb_left;
1066                 else if (cmp > 0)
1067                         n = n->rb_right;
1068                 else
1069                         return m;
1070         }
1071 
1072         return NULL;
1073 }
1074 
1075 static int __read_proc_modules(void *arg, const char *name, u64 start,
1076                                u64 size __maybe_unused)
1077 {
1078         struct rb_root *modules = arg;
1079         struct module_info *mi;
1080 
1081         mi = zalloc(sizeof(struct module_info));
1082         if (!mi)
1083                 return -ENOMEM;
1084 
1085         mi->name = strdup(name);
1086         mi->start = start;
1087 
1088         if (!mi->name) {
1089                 free(mi);
1090                 return -ENOMEM;
1091         }
1092 
1093         add_module(mi, modules);
1094 
1095         return 0;
1096 }
1097 
1098 static int read_proc_modules(const char *filename, struct rb_root *modules)
1099 {
1100         if (symbol__restricted_filename(filename, "/proc/modules"))
1101                 return -1;
1102 
1103         if (modules__parse(filename, modules, __read_proc_modules)) {
1104                 delete_modules(modules);
1105                 return -1;
1106         }
1107 
1108         return 0;
1109 }
1110 
1111 int compare_proc_modules(const char *from, const char *to)
1112 {
1113         struct rb_root from_modules = RB_ROOT;
1114         struct rb_root to_modules = RB_ROOT;
1115         struct rb_node *from_node, *to_node;
1116         struct module_info *from_m, *to_m;
1117         int ret = -1;
1118 
1119         if (read_proc_modules(from, &from_modules))
1120                 return -1;
1121 
1122         if (read_proc_modules(to, &to_modules))
1123                 goto out_delete_from;
1124 
1125         from_node = rb_first(&from_modules);
1126         to_node = rb_first(&to_modules);
1127         while (from_node) {
1128                 if (!to_node)
1129                         break;
1130 
1131                 from_m = rb_entry(from_node, struct module_info, rb_node);
1132                 to_m = rb_entry(to_node, struct module_info, rb_node);
1133 
1134                 if (from_m->start != to_m->start ||
1135                     strcmp(from_m->name, to_m->name))
1136                         break;
1137 
1138                 from_node = rb_next(from_node);
1139                 to_node = rb_next(to_node);
1140         }
1141 
1142         if (!from_node && !to_node)
1143                 ret = 0;
1144 
1145         delete_modules(&to_modules);
1146 out_delete_from:
1147         delete_modules(&from_modules);
1148 
1149         return ret;
1150 }
1151 
1152 static int do_validate_kcore_modules_cb(struct map *old_map, void *data)
1153 {
1154         struct rb_root *modules = data;
1155         struct module_info *mi;
1156         struct dso *dso;
1157 
1158         if (!__map__is_kmodule(old_map))
1159                 return 0;
1160 
1161         dso = map__dso(old_map);
1162         /* Module must be in memory at the same address */
1163         mi = find_module(dso__short_name(dso), modules);
1164         if (!mi || mi->start != map__start(old_map))
1165                 return -EINVAL;
1166 
1167         return 0;
1168 }
1169 
1170 static int do_validate_kcore_modules(const char *filename, struct maps *kmaps)
1171 {
1172         struct rb_root modules = RB_ROOT;
1173         int err;
1174 
1175         err = read_proc_modules(filename, &modules);
1176         if (err)
1177                 return err;
1178 
1179         err = maps__for_each_map(kmaps, do_validate_kcore_modules_cb, &modules);
1180 
1181         delete_modules(&modules);
1182         return err;
1183 }
1184 
1185 /*
1186  * If kallsyms is referenced by name then we look for filename in the same
1187  * directory.
1188  */
1189 static bool filename_from_kallsyms_filename(char *filename,
1190                                             const char *base_name,
1191                                             const char *kallsyms_filename)
1192 {
1193         char *name;
1194 
1195         strcpy(filename, kallsyms_filename);
1196         name = strrchr(filename, '/');
1197         if (!name)
1198                 return false;
1199 
1200         name += 1;
1201 
1202         if (!strcmp(name, "kallsyms")) {
1203                 strcpy(name, base_name);
1204                 return true;
1205         }
1206 
1207         return false;
1208 }
1209 
1210 static int validate_kcore_modules(const char *kallsyms_filename,
1211                                   struct map *map)
1212 {
1213         struct maps *kmaps = map__kmaps(map);
1214         char modules_filename[PATH_MAX];
1215 
1216         if (!kmaps)
1217                 return -EINVAL;
1218 
1219         if (!filename_from_kallsyms_filename(modules_filename, "modules",
1220                                              kallsyms_filename))
1221                 return -EINVAL;
1222 
1223         if (do_validate_kcore_modules(modules_filename, kmaps))
1224                 return -EINVAL;
1225 
1226         return 0;
1227 }
1228 
1229 static int validate_kcore_addresses(const char *kallsyms_filename,
1230                                     struct map *map)
1231 {
1232         struct kmap *kmap = map__kmap(map);
1233 
1234         if (!kmap)
1235                 return -EINVAL;
1236 
1237         if (kmap->ref_reloc_sym && kmap->ref_reloc_sym->name) {
1238                 u64 start;
1239 
1240                 if (kallsyms__get_function_start(kallsyms_filename,
1241                                                  kmap->ref_reloc_sym->name, &start))
1242                         return -ENOENT;
1243                 if (start != kmap->ref_reloc_sym->addr)
1244                         return -EINVAL;
1245         }
1246 
1247         return validate_kcore_modules(kallsyms_filename, map);
1248 }
1249 
1250 struct kcore_mapfn_data {
1251         struct dso *dso;
1252         struct list_head maps;
1253 };
1254 
1255 static int kcore_mapfn(u64 start, u64 len, u64 pgoff, void *data)
1256 {
1257         struct kcore_mapfn_data *md = data;
1258         struct map_list_node *list_node = map_list_node__new();
1259 
1260         if (!list_node)
1261                 return -ENOMEM;
1262 
1263         list_node->map = map__new2(start, md->dso);
1264         if (!list_node->map) {
1265                 free(list_node);
1266                 return -ENOMEM;
1267         }
1268 
1269         map__set_end(list_node->map, map__start(list_node->map) + len);
1270         map__set_pgoff(list_node->map, pgoff);
1271 
1272         list_add(&list_node->node, &md->maps);
1273 
1274         return 0;
1275 }
1276 
1277 static bool remove_old_maps(struct map *map, void *data)
1278 {
1279         const struct map *map_to_save = data;
1280 
1281         /*
1282          * We need to preserve eBPF maps even if they are covered by kcore,
1283          * because we need to access eBPF dso for source data.
1284          */
1285         return !RC_CHK_EQUAL(map, map_to_save) && !__map__is_bpf_prog(map);
1286 }
1287 
1288 static int dso__load_kcore(struct dso *dso, struct map *map,
1289                            const char *kallsyms_filename)
1290 {
1291         struct maps *kmaps = map__kmaps(map);
1292         struct kcore_mapfn_data md;
1293         struct map *map_ref, *replacement_map = NULL;
1294         struct machine *machine;
1295         bool is_64_bit;
1296         int err, fd;
1297         char kcore_filename[PATH_MAX];
1298         u64 stext;
1299 
1300         if (!kmaps)
1301                 return -EINVAL;
1302 
1303         machine = maps__machine(kmaps);
1304 
1305         /* This function requires that the map is the kernel map */
1306         if (!__map__is_kernel(map))
1307                 return -EINVAL;
1308 
1309         if (!filename_from_kallsyms_filename(kcore_filename, "kcore",
1310                                              kallsyms_filename))
1311                 return -EINVAL;
1312 
1313         /* Modules and kernel must be present at their original addresses */
1314         if (validate_kcore_addresses(kallsyms_filename, map))
1315                 return -EINVAL;
1316 
1317         md.dso = dso;
1318         INIT_LIST_HEAD(&md.maps);
1319 
1320         fd = open(kcore_filename, O_RDONLY);
1321         if (fd < 0) {
1322                 pr_debug("Failed to open %s. Note /proc/kcore requires CAP_SYS_RAWIO capability to access.\n",
1323                          kcore_filename);
1324                 return -EINVAL;
1325         }
1326 
1327         /* Read new maps into temporary lists */
1328         err = file__read_maps(fd, map__prot(map) & PROT_EXEC, kcore_mapfn, &md,
1329                               &is_64_bit);
1330         if (err)
1331                 goto out_err;
1332         dso__set_is_64_bit(dso, is_64_bit);
1333 
1334         if (list_empty(&md.maps)) {
1335                 err = -EINVAL;
1336                 goto out_err;
1337         }
1338 
1339         /* Remove old maps */
1340         maps__remove_maps(kmaps, remove_old_maps, map);
1341         machine->trampolines_mapped = false;
1342 
1343         /* Find the kernel map using the '_stext' symbol */
1344         if (!kallsyms__get_function_start(kallsyms_filename, "_stext", &stext)) {
1345                 u64 replacement_size = 0;
1346                 struct map_list_node *new_node;
1347 
1348                 list_for_each_entry(new_node, &md.maps, node) {
1349                         struct map *new_map = new_node->map;
1350                         u64 new_size = map__size(new_map);
1351 
1352                         if (!(stext >= map__start(new_map) && stext < map__end(new_map)))
1353                                 continue;
1354 
1355                         /*
1356                          * On some architectures, ARM64 for example, the kernel
1357                          * text can get allocated inside of the vmalloc segment.
1358                          * Select the smallest matching segment, in case stext
1359                          * falls within more than one in the list.
1360                          */
1361                         if (!replacement_map || new_size < replacement_size) {
1362                                 replacement_map = new_map;
1363                                 replacement_size = new_size;
1364                         }
1365                 }
1366         }
1367 
1368         if (!replacement_map)
1369                 replacement_map = list_entry(md.maps.next, struct map_list_node, node)->map;
1370 
1371         /*
1372          * Update addresses of vmlinux map. Re-insert it to ensure maps are
1373          * correctly ordered. Do this before using maps__merge_in() for the
1374          * remaining maps so vmlinux gets split if necessary.
1375          */
1376         map_ref = map__get(map);
1377         maps__remove(kmaps, map_ref);
1378 
1379         map__set_start(map_ref, map__start(replacement_map));
1380         map__set_end(map_ref, map__end(replacement_map));
1381         map__set_pgoff(map_ref, map__pgoff(replacement_map));
1382         map__set_mapping_type(map_ref, map__mapping_type(replacement_map));
1383 
1384         err = maps__insert(kmaps, map_ref);
1385         map__put(map_ref);
1386         if (err)
1387                 goto out_err;
1388 
1389         /* Add new maps */
1390         while (!list_empty(&md.maps)) {
1391                 struct map_list_node *new_node = list_entry(md.maps.next, struct map_list_node, node);
1392                 struct map *new_map = new_node->map;
1393 
1394                 list_del_init(&new_node->node);
1395 
1396                 /* skip if replacement_map, already inserted above */
1397                 if (!RC_CHK_EQUAL(new_map, replacement_map)) {
1398                         /*
1399                          * Merge kcore map into existing maps,
1400                          * and ensure that current maps (eBPF)
1401                          * stay intact.
1402                          */
1403                         if (maps__merge_in(kmaps, new_map)) {
1404                                 err = -EINVAL;
1405                                 goto out_err;
1406                         }
1407                 }
1408                 free(new_node);
1409         }
1410 
1411         if (machine__is(machine, "x86_64")) {
1412                 u64 addr;
1413 
1414                 /*
1415                  * If one of the corresponding symbols is there, assume the
1416                  * entry trampoline maps are too.
1417                  */
1418                 if (!kallsyms__get_function_start(kallsyms_filename,
1419                                                   ENTRY_TRAMPOLINE_NAME,
1420                                                   &addr))
1421                         machine->trampolines_mapped = true;
1422         }
1423 
1424         /*
1425          * Set the data type and long name so that kcore can be read via
1426          * dso__data_read_addr().
1427          */
1428         if (dso__kernel(dso) == DSO_SPACE__KERNEL_GUEST)
1429                 dso__set_binary_type(dso, DSO_BINARY_TYPE__GUEST_KCORE);
1430         else
1431                 dso__set_binary_type(dso, DSO_BINARY_TYPE__KCORE);
1432         dso__set_long_name(dso, strdup(kcore_filename), true);
1433 
1434         close(fd);
1435 
1436         if (map__prot(map) & PROT_EXEC)
1437                 pr_debug("Using %s for kernel object code\n", kcore_filename);
1438         else
1439                 pr_debug("Using %s for kernel data\n", kcore_filename);
1440 
1441         return 0;
1442 
1443 out_err:
1444         while (!list_empty(&md.maps)) {
1445                 struct map_list_node *list_node;
1446 
1447                 list_node = list_entry(md.maps.next, struct map_list_node, node);
1448                 list_del_init(&list_node->node);
1449                 map__zput(list_node->map);
1450                 free(list_node);
1451         }
1452         close(fd);
1453         return err;
1454 }
1455 
1456 /*
1457  * If the kernel is relocated at boot time, kallsyms won't match.  Compute the
1458  * delta based on the relocation reference symbol.
1459  */
1460 static int kallsyms__delta(struct kmap *kmap, const char *filename, u64 *delta)
1461 {
1462         u64 addr;
1463 
1464         if (!kmap->ref_reloc_sym || !kmap->ref_reloc_sym->name)
1465                 return 0;
1466 
1467         if (kallsyms__get_function_start(filename, kmap->ref_reloc_sym->name, &addr))
1468                 return -1;
1469 
1470         *delta = addr - kmap->ref_reloc_sym->addr;
1471         return 0;
1472 }
1473 
1474 int __dso__load_kallsyms(struct dso *dso, const char *filename,
1475                          struct map *map, bool no_kcore)
1476 {
1477         struct kmap *kmap = map__kmap(map);
1478         u64 delta = 0;
1479 
1480         if (symbol__restricted_filename(filename, "/proc/kallsyms"))
1481                 return -1;
1482 
1483         if (!kmap || !kmap->kmaps)
1484                 return -1;
1485 
1486         if (dso__load_all_kallsyms(dso, filename) < 0)
1487                 return -1;
1488 
1489         if (kallsyms__delta(kmap, filename, &delta))
1490                 return -1;
1491 
1492         symbols__fixup_end(dso__symbols(dso), true);
1493         symbols__fixup_duplicate(dso__symbols(dso));
1494 
1495         if (dso__kernel(dso) == DSO_SPACE__KERNEL_GUEST)
1496                 dso__set_symtab_type(dso, DSO_BINARY_TYPE__GUEST_KALLSYMS);
1497         else
1498                 dso__set_symtab_type(dso, DSO_BINARY_TYPE__KALLSYMS);
1499 
1500         if (!no_kcore && !dso__load_kcore(dso, map, filename))
1501                 return maps__split_kallsyms_for_kcore(kmap->kmaps, dso);
1502         else
1503                 return maps__split_kallsyms(kmap->kmaps, dso, delta, map);
1504 }
1505 
1506 int dso__load_kallsyms(struct dso *dso, const char *filename,
1507                        struct map *map)
1508 {
1509         return __dso__load_kallsyms(dso, filename, map, false);
1510 }
1511 
1512 static int dso__load_perf_map(const char *map_path, struct dso *dso)
1513 {
1514         char *line = NULL;
1515         size_t n;
1516         FILE *file;
1517         int nr_syms = 0;
1518 
1519         file = fopen(map_path, "r");
1520         if (file == NULL)
1521                 goto out_failure;
1522 
1523         while (!feof(file)) {
1524                 u64 start, size;
1525                 struct symbol *sym;
1526                 int line_len, len;
1527 
1528                 line_len = getline(&line, &n, file);
1529                 if (line_len < 0)
1530                         break;
1531 
1532                 if (!line)
1533                         goto out_failure;
1534 
1535                 line[--line_len] = '\0'; /* \n */
1536 
1537                 len = hex2u64(line, &start);
1538 
1539                 len++;
1540                 if (len + 2 >= line_len)
1541                         continue;
1542 
1543                 len += hex2u64(line + len, &size);
1544 
1545                 len++;
1546                 if (len + 2 >= line_len)
1547                         continue;
1548 
1549                 sym = symbol__new(start, size, STB_GLOBAL, STT_FUNC, line + len);
1550 
1551                 if (sym == NULL)
1552                         goto out_delete_line;
1553 
1554                 symbols__insert(dso__symbols(dso), sym);
1555                 nr_syms++;
1556         }
1557 
1558         free(line);
1559         fclose(file);
1560 
1561         return nr_syms;
1562 
1563 out_delete_line:
1564         free(line);
1565 out_failure:
1566         return -1;
1567 }
1568 
1569 #ifdef HAVE_LIBBFD_SUPPORT
1570 #define PACKAGE 'perf'
1571 #include <bfd.h>
1572 
1573 static int bfd_symbols__cmpvalue(const void *a, const void *b)
1574 {
1575         const asymbol *as = *(const asymbol **)a, *bs = *(const asymbol **)b;
1576 
1577         if (bfd_asymbol_value(as) != bfd_asymbol_value(bs))
1578                 return bfd_asymbol_value(as) - bfd_asymbol_value(bs);
1579 
1580         return bfd_asymbol_name(as)[0] - bfd_asymbol_name(bs)[0];
1581 }
1582 
1583 static int bfd2elf_binding(asymbol *symbol)
1584 {
1585         if (symbol->flags & BSF_WEAK)
1586                 return STB_WEAK;
1587         if (symbol->flags & BSF_GLOBAL)
1588                 return STB_GLOBAL;
1589         if (symbol->flags & BSF_LOCAL)
1590                 return STB_LOCAL;
1591         return -1;
1592 }
1593 
1594 int dso__load_bfd_symbols(struct dso *dso, const char *debugfile)
1595 {
1596         int err = -1;
1597         long symbols_size, symbols_count, i;
1598         asection *section;
1599         asymbol **symbols, *sym;
1600         struct symbol *symbol;
1601         bfd *abfd;
1602         u64 start, len;
1603 
1604         abfd = bfd_openr(debugfile, NULL);
1605         if (!abfd)
1606                 return -1;
1607 
1608         if (!bfd_check_format(abfd, bfd_object)) {
1609                 pr_debug2("%s: cannot read %s bfd file.\n", __func__,
1610                           dso__long_name(dso));
1611                 goto out_close;
1612         }
1613 
1614         if (bfd_get_flavour(abfd) == bfd_target_elf_flavour)
1615                 goto out_close;
1616 
1617         symbols_size = bfd_get_symtab_upper_bound(abfd);
1618         if (symbols_size == 0) {
1619                 bfd_close(abfd);
1620                 return 0;
1621         }
1622 
1623         if (symbols_size < 0)
1624                 goto out_close;
1625 
1626         symbols = malloc(symbols_size);
1627         if (!symbols)
1628                 goto out_close;
1629 
1630         symbols_count = bfd_canonicalize_symtab(abfd, symbols);
1631         if (symbols_count < 0)
1632                 goto out_free;
1633 
1634         section = bfd_get_section_by_name(abfd, ".text");
1635         if (section) {
1636                 for (i = 0; i < symbols_count; ++i) {
1637                         if (!strcmp(bfd_asymbol_name(symbols[i]), "__ImageBase") ||
1638                             !strcmp(bfd_asymbol_name(symbols[i]), "__image_base__"))
1639                                 break;
1640                 }
1641                 if (i < symbols_count) {
1642                         /* PE symbols can only have 4 bytes, so use .text high bits */
1643                         u64 text_offset = (section->vma - (u32)section->vma)
1644                                 + (u32)bfd_asymbol_value(symbols[i]);
1645                         dso__set_text_offset(dso, text_offset);
1646                         dso__set_text_end(dso, (section->vma - text_offset) + section->size);
1647                 } else {
1648                         dso__set_text_offset(dso, section->vma - section->filepos);
1649                         dso__set_text_end(dso, section->filepos + section->size);
1650                 }
1651         }
1652 
1653         qsort(symbols, symbols_count, sizeof(asymbol *), bfd_symbols__cmpvalue);
1654 
1655 #ifdef bfd_get_section
1656 #define bfd_asymbol_section bfd_get_section
1657 #endif
1658         for (i = 0; i < symbols_count; ++i) {
1659                 sym = symbols[i];
1660                 section = bfd_asymbol_section(sym);
1661                 if (bfd2elf_binding(sym) < 0)
1662                         continue;
1663 
1664                 while (i + 1 < symbols_count &&
1665                        bfd_asymbol_section(symbols[i + 1]) == section &&
1666                        bfd2elf_binding(symbols[i + 1]) < 0)
1667                         i++;
1668 
1669                 if (i + 1 < symbols_count &&
1670                     bfd_asymbol_section(symbols[i + 1]) == section)
1671                         len = symbols[i + 1]->value - sym->value;
1672                 else
1673                         len = section->size - sym->value;
1674 
1675                 start = bfd_asymbol_value(sym) - dso__text_offset(dso);
1676                 symbol = symbol__new(start, len, bfd2elf_binding(sym), STT_FUNC,
1677                                      bfd_asymbol_name(sym));
1678                 if (!symbol)
1679                         goto out_free;
1680 
1681                 symbols__insert(dso__symbols(dso), symbol);
1682         }
1683 #ifdef bfd_get_section
1684 #undef bfd_asymbol_section
1685 #endif
1686 
1687         symbols__fixup_end(dso__symbols(dso), false);
1688         symbols__fixup_duplicate(dso__symbols(dso));
1689         dso__set_adjust_symbols(dso, true);
1690 
1691         err = 0;
1692 out_free:
1693         free(symbols);
1694 out_close:
1695         bfd_close(abfd);
1696         return err;
1697 }
1698 #endif
1699 
1700 static bool dso__is_compatible_symtab_type(struct dso *dso, bool kmod,
1701                                            enum dso_binary_type type)
1702 {
1703         switch (type) {
1704         case DSO_BINARY_TYPE__JAVA_JIT:
1705         case DSO_BINARY_TYPE__DEBUGLINK:
1706         case DSO_BINARY_TYPE__SYSTEM_PATH_DSO:
1707         case DSO_BINARY_TYPE__FEDORA_DEBUGINFO:
1708         case DSO_BINARY_TYPE__UBUNTU_DEBUGINFO:
1709         case DSO_BINARY_TYPE__MIXEDUP_UBUNTU_DEBUGINFO:
1710         case DSO_BINARY_TYPE__BUILDID_DEBUGINFO:
1711         case DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO:
1712                 return !kmod && dso__kernel(dso) == DSO_SPACE__USER;
1713 
1714         case DSO_BINARY_TYPE__KALLSYMS:
1715         case DSO_BINARY_TYPE__VMLINUX:
1716         case DSO_BINARY_TYPE__KCORE:
1717                 return dso__kernel(dso) == DSO_SPACE__KERNEL;
1718 
1719         case DSO_BINARY_TYPE__GUEST_KALLSYMS:
1720         case DSO_BINARY_TYPE__GUEST_VMLINUX:
1721         case DSO_BINARY_TYPE__GUEST_KCORE:
1722                 return dso__kernel(dso) == DSO_SPACE__KERNEL_GUEST;
1723 
1724         case DSO_BINARY_TYPE__GUEST_KMODULE:
1725         case DSO_BINARY_TYPE__GUEST_KMODULE_COMP:
1726         case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE:
1727         case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP:
1728                 /*
1729                  * kernel modules know their symtab type - it's set when
1730                  * creating a module dso in machine__addnew_module_map().
1731                  */
1732                 return kmod && dso__symtab_type(dso) == type;
1733 
1734         case DSO_BINARY_TYPE__BUILD_ID_CACHE:
1735         case DSO_BINARY_TYPE__BUILD_ID_CACHE_DEBUGINFO:
1736                 return true;
1737 
1738         case DSO_BINARY_TYPE__BPF_PROG_INFO:
1739         case DSO_BINARY_TYPE__BPF_IMAGE:
1740         case DSO_BINARY_TYPE__OOL:
1741         case DSO_BINARY_TYPE__NOT_FOUND:
1742         default:
1743                 return false;
1744         }
1745 }
1746 
1747 /* Checks for the existence of the perf-<pid>.map file in two different
1748  * locations.  First, if the process is a separate mount namespace, check in
1749  * that namespace using the pid of the innermost pid namespace.  If's not in a
1750  * namespace, or the file can't be found there, try in the mount namespace of
1751  * the tracing process using our view of its pid.
1752  */
1753 static int dso__find_perf_map(char *filebuf, size_t bufsz,
1754                               struct nsinfo **nsip)
1755 {
1756         struct nscookie nsc;
1757         struct nsinfo *nsi;
1758         struct nsinfo *nnsi;
1759         int rc = -1;
1760 
1761         nsi = *nsip;
1762 
1763         if (nsinfo__need_setns(nsi)) {
1764                 snprintf(filebuf, bufsz, "/tmp/perf-%d.map", nsinfo__nstgid(nsi));
1765                 nsinfo__mountns_enter(nsi, &nsc);
1766                 rc = access(filebuf, R_OK);
1767                 nsinfo__mountns_exit(&nsc);
1768                 if (rc == 0)
1769                         return rc;
1770         }
1771 
1772         nnsi = nsinfo__copy(nsi);
1773         if (nnsi) {
1774                 nsinfo__put(nsi);
1775 
1776                 nsinfo__clear_need_setns(nnsi);
1777                 snprintf(filebuf, bufsz, "/tmp/perf-%d.map", nsinfo__tgid(nnsi));
1778                 *nsip = nnsi;
1779                 rc = 0;
1780         }
1781 
1782         return rc;
1783 }
1784 
1785 int dso__load(struct dso *dso, struct map *map)
1786 {
1787         char *name;
1788         int ret = -1;
1789         u_int i;
1790         struct machine *machine = NULL;
1791         char *root_dir = (char *) "";
1792         int ss_pos = 0;
1793         struct symsrc ss_[2];
1794         struct symsrc *syms_ss = NULL, *runtime_ss = NULL;
1795         bool kmod;
1796         bool perfmap;
1797         struct build_id bid;
1798         struct nscookie nsc;
1799         char newmapname[PATH_MAX];
1800         const char *map_path = dso__long_name(dso);
1801 
1802         mutex_lock(dso__lock(dso));
1803         perfmap = is_perf_pid_map_name(map_path);
1804 
1805         if (perfmap) {
1806                 if (dso__nsinfo(dso) &&
1807                     (dso__find_perf_map(newmapname, sizeof(newmapname),
1808                                         dso__nsinfo_ptr(dso)) == 0)) {
1809                         map_path = newmapname;
1810                 }
1811         }
1812 
1813         nsinfo__mountns_enter(dso__nsinfo(dso), &nsc);
1814 
1815         /* check again under the dso->lock */
1816         if (dso__loaded(dso)) {
1817                 ret = 1;
1818                 goto out;
1819         }
1820 
1821         kmod = dso__is_kmod(dso);
1822 
1823         if (dso__kernel(dso) && !kmod) {
1824                 if (dso__kernel(dso) == DSO_SPACE__KERNEL)
1825                         ret = dso__load_kernel_sym(dso, map);
1826                 else if (dso__kernel(dso) == DSO_SPACE__KERNEL_GUEST)
1827                         ret = dso__load_guest_kernel_sym(dso, map);
1828 
1829                 machine = maps__machine(map__kmaps(map));
1830                 if (machine__is(machine, "x86_64"))
1831                         machine__map_x86_64_entry_trampolines(machine, dso);
1832                 goto out;
1833         }
1834 
1835         dso__set_adjust_symbols(dso, false);
1836 
1837         if (perfmap) {
1838                 ret = dso__load_perf_map(map_path, dso);
1839                 dso__set_symtab_type(dso, ret > 0
1840                                 ? DSO_BINARY_TYPE__JAVA_JIT
1841                                 : DSO_BINARY_TYPE__NOT_FOUND);
1842                 goto out;
1843         }
1844 
1845         if (machine)
1846                 root_dir = machine->root_dir;
1847 
1848         name = malloc(PATH_MAX);
1849         if (!name)
1850                 goto out;
1851 
1852         /*
1853          * Read the build id if possible. This is required for
1854          * DSO_BINARY_TYPE__BUILDID_DEBUGINFO to work
1855          */
1856         if (!dso__has_build_id(dso) &&
1857             is_regular_file(dso__long_name(dso))) {
1858                 __symbol__join_symfs(name, PATH_MAX, dso__long_name(dso));
1859                 if (filename__read_build_id(name, &bid) > 0)
1860                         dso__set_build_id(dso, &bid);
1861         }
1862 
1863         /*
1864          * Iterate over candidate debug images.
1865          * Keep track of "interesting" ones (those which have a symtab, dynsym,
1866          * and/or opd section) for processing.
1867          */
1868         for (i = 0; i < DSO_BINARY_TYPE__SYMTAB_CNT; i++) {
1869                 struct symsrc *ss = &ss_[ss_pos];
1870                 bool next_slot = false;
1871                 bool is_reg;
1872                 bool nsexit;
1873                 int bfdrc = -1;
1874                 int sirc = -1;
1875 
1876                 enum dso_binary_type symtab_type = binary_type_symtab[i];
1877 
1878                 nsexit = (symtab_type == DSO_BINARY_TYPE__BUILD_ID_CACHE ||
1879                     symtab_type == DSO_BINARY_TYPE__BUILD_ID_CACHE_DEBUGINFO);
1880 
1881                 if (!dso__is_compatible_symtab_type(dso, kmod, symtab_type))
1882                         continue;
1883 
1884                 if (dso__read_binary_type_filename(dso, symtab_type,
1885                                                    root_dir, name, PATH_MAX))
1886                         continue;
1887 
1888                 if (nsexit)
1889                         nsinfo__mountns_exit(&nsc);
1890 
1891                 is_reg = is_regular_file(name);
1892                 if (!is_reg && errno == ENOENT && dso__nsinfo(dso)) {
1893                         char *new_name = dso__filename_with_chroot(dso, name);
1894                         if (new_name) {
1895                                 is_reg = is_regular_file(new_name);
1896                                 strlcpy(name, new_name, PATH_MAX);
1897                                 free(new_name);
1898                         }
1899                 }
1900 
1901 #ifdef HAVE_LIBBFD_SUPPORT
1902                 if (is_reg)
1903                         bfdrc = dso__load_bfd_symbols(dso, name);
1904 #endif
1905                 if (is_reg && bfdrc < 0)
1906                         sirc = symsrc__init(ss, dso, name, symtab_type);
1907 
1908                 if (nsexit)
1909                         nsinfo__mountns_enter(dso__nsinfo(dso), &nsc);
1910 
1911                 if (bfdrc == 0) {
1912                         ret = 0;
1913                         break;
1914                 }
1915 
1916                 if (!is_reg || sirc < 0)
1917                         continue;
1918 
1919                 if (!syms_ss && symsrc__has_symtab(ss)) {
1920                         syms_ss = ss;
1921                         next_slot = true;
1922                         if (!dso__symsrc_filename(dso))
1923                                 dso__set_symsrc_filename(dso, strdup(name));
1924                 }
1925 
1926                 if (!runtime_ss && symsrc__possibly_runtime(ss)) {
1927                         runtime_ss = ss;
1928                         next_slot = true;
1929                 }
1930 
1931                 if (next_slot) {
1932                         ss_pos++;
1933 
1934                         if (syms_ss && runtime_ss)
1935                                 break;
1936                 } else {
1937                         symsrc__destroy(ss);
1938                 }
1939 
1940         }
1941 
1942         if (!runtime_ss && !syms_ss)
1943                 goto out_free;
1944 
1945         if (runtime_ss && !syms_ss) {
1946                 syms_ss = runtime_ss;
1947         }
1948 
1949         /* We'll have to hope for the best */
1950         if (!runtime_ss && syms_ss)
1951                 runtime_ss = syms_ss;
1952 
1953         if (syms_ss)
1954                 ret = dso__load_sym(dso, map, syms_ss, runtime_ss, kmod);
1955         else
1956                 ret = -1;
1957 
1958         if (ret > 0) {
1959                 int nr_plt;
1960 
1961                 nr_plt = dso__synthesize_plt_symbols(dso, runtime_ss);
1962                 if (nr_plt > 0)
1963                         ret += nr_plt;
1964         }
1965 
1966         for (; ss_pos > 0; ss_pos--)
1967                 symsrc__destroy(&ss_[ss_pos - 1]);
1968 out_free:
1969         free(name);
1970         if (ret < 0 && strstr(dso__name(dso), " (deleted)") != NULL)
1971                 ret = 0;
1972 out:
1973         dso__set_loaded(dso);
1974         mutex_unlock(dso__lock(dso));
1975         nsinfo__mountns_exit(&nsc);
1976 
1977         return ret;
1978 }
1979 
1980 /*
1981  * Always takes ownership of vmlinux when vmlinux_allocated == true, even if
1982  * it returns an error.
1983  */
1984 int dso__load_vmlinux(struct dso *dso, struct map *map,
1985                       const char *vmlinux, bool vmlinux_allocated)
1986 {
1987         int err = -1;
1988         struct symsrc ss;
1989         char symfs_vmlinux[PATH_MAX];
1990         enum dso_binary_type symtab_type;
1991 
1992         if (vmlinux[0] == '/')
1993                 snprintf(symfs_vmlinux, sizeof(symfs_vmlinux), "%s", vmlinux);
1994         else
1995                 symbol__join_symfs(symfs_vmlinux, vmlinux);
1996 
1997         if (dso__kernel(dso) == DSO_SPACE__KERNEL_GUEST)
1998                 symtab_type = DSO_BINARY_TYPE__GUEST_VMLINUX;
1999         else
2000                 symtab_type = DSO_BINARY_TYPE__VMLINUX;
2001 
2002         if (symsrc__init(&ss, dso, symfs_vmlinux, symtab_type)) {
2003                 if (vmlinux_allocated)
2004                         free((char *) vmlinux);
2005                 return -1;
2006         }
2007 
2008         /*
2009          * dso__load_sym() may copy 'dso' which will result in the copies having
2010          * an incorrect long name unless we set it here first.
2011          */
2012         dso__set_long_name(dso, vmlinux, vmlinux_allocated);
2013         if (dso__kernel(dso) == DSO_SPACE__KERNEL_GUEST)
2014                 dso__set_binary_type(dso, DSO_BINARY_TYPE__GUEST_VMLINUX);
2015         else
2016                 dso__set_binary_type(dso, DSO_BINARY_TYPE__VMLINUX);
2017 
2018         err = dso__load_sym(dso, map, &ss, &ss, 0);
2019         symsrc__destroy(&ss);
2020 
2021         if (err > 0) {
2022                 dso__set_loaded(dso);
2023                 pr_debug("Using %s for symbols\n", symfs_vmlinux);
2024         }
2025 
2026         return err;
2027 }
2028 
2029 int dso__load_vmlinux_path(struct dso *dso, struct map *map)
2030 {
2031         int i, err = 0;
2032         char *filename = NULL;
2033 
2034         pr_debug("Looking at the vmlinux_path (%d entries long)\n",
2035                  vmlinux_path__nr_entries + 1);
2036 
2037         for (i = 0; i < vmlinux_path__nr_entries; ++i) {
2038                 err = dso__load_vmlinux(dso, map, vmlinux_path[i], false);
2039                 if (err > 0)
2040                         goto out;
2041         }
2042 
2043         if (!symbol_conf.ignore_vmlinux_buildid)
2044                 filename = dso__build_id_filename(dso, NULL, 0, false);
2045         if (filename != NULL) {
2046                 err = dso__load_vmlinux(dso, map, filename, true);
2047                 if (err > 0)
2048                         goto out;
2049         }
2050 out:
2051         return err;
2052 }
2053 
2054 static bool visible_dir_filter(const char *name, struct dirent *d)
2055 {
2056         if (d->d_type != DT_DIR)
2057                 return false;
2058         return lsdir_no_dot_filter(name, d);
2059 }
2060 
2061 static int find_matching_kcore(struct map *map, char *dir, size_t dir_sz)
2062 {
2063         char kallsyms_filename[PATH_MAX];
2064         int ret = -1;
2065         struct strlist *dirs;
2066         struct str_node *nd;
2067 
2068         dirs = lsdir(dir, visible_dir_filter);
2069         if (!dirs)
2070                 return -1;
2071 
2072         strlist__for_each_entry(nd, dirs) {
2073                 scnprintf(kallsyms_filename, sizeof(kallsyms_filename),
2074                           "%s/%s/kallsyms", dir, nd->s);
2075                 if (!validate_kcore_addresses(kallsyms_filename, map)) {
2076                         strlcpy(dir, kallsyms_filename, dir_sz);
2077                         ret = 0;
2078                         break;
2079                 }
2080         }
2081 
2082         strlist__delete(dirs);
2083 
2084         return ret;
2085 }
2086 
2087 /*
2088  * Use open(O_RDONLY) to check readability directly instead of access(R_OK)
2089  * since access(R_OK) only checks with real UID/GID but open() use effective
2090  * UID/GID and actual capabilities (e.g. /proc/kcore requires CAP_SYS_RAWIO).
2091  */
2092 static bool filename__readable(const char *file)
2093 {
2094         int fd = open(file, O_RDONLY);
2095         if (fd < 0)
2096                 return false;
2097         close(fd);
2098         return true;
2099 }
2100 
2101 static char *dso__find_kallsyms(struct dso *dso, struct map *map)
2102 {
2103         struct build_id bid;
2104         char sbuild_id[SBUILD_ID_SIZE];
2105         bool is_host = false;
2106         char path[PATH_MAX];
2107 
2108         if (!dso__has_build_id(dso)) {
2109                 /*
2110                  * Last resort, if we don't have a build-id and couldn't find
2111                  * any vmlinux file, try the running kernel kallsyms table.
2112                  */
2113                 goto proc_kallsyms;
2114         }
2115 
2116         if (sysfs__read_build_id("/sys/kernel/notes", &bid) == 0)
2117                 is_host = dso__build_id_equal(dso, &bid);
2118 
2119         /* Try a fast path for /proc/kallsyms if possible */
2120         if (is_host) {
2121                 /*
2122                  * Do not check the build-id cache, unless we know we cannot use
2123                  * /proc/kcore or module maps don't match to /proc/kallsyms.
2124                  * To check readability of /proc/kcore, do not use access(R_OK)
2125                  * since /proc/kcore requires CAP_SYS_RAWIO to read and access
2126                  * can't check it.
2127                  */
2128                 if (filename__readable("/proc/kcore") &&
2129                     !validate_kcore_addresses("/proc/kallsyms", map))
2130                         goto proc_kallsyms;
2131         }
2132 
2133         build_id__sprintf(dso__bid(dso), sbuild_id);
2134 
2135         /* Find kallsyms in build-id cache with kcore */
2136         scnprintf(path, sizeof(path), "%s/%s/%s",
2137                   buildid_dir, DSO__NAME_KCORE, sbuild_id);
2138 
2139         if (!find_matching_kcore(map, path, sizeof(path)))
2140                 return strdup(path);
2141 
2142         /* Use current /proc/kallsyms if possible */
2143         if (is_host) {
2144 proc_kallsyms:
2145                 return strdup("/proc/kallsyms");
2146         }
2147 
2148         /* Finally, find a cache of kallsyms */
2149         if (!build_id_cache__kallsyms_path(sbuild_id, path, sizeof(path))) {
2150                 pr_err("No kallsyms or vmlinux with build-id %s was found\n",
2151                        sbuild_id);
2152                 return NULL;
2153         }
2154 
2155         return strdup(path);
2156 }
2157 
2158 static int dso__load_kernel_sym(struct dso *dso, struct map *map)
2159 {
2160         int err;
2161         const char *kallsyms_filename = NULL;
2162         char *kallsyms_allocated_filename = NULL;
2163         char *filename = NULL;
2164 
2165         /*
2166          * Step 1: if the user specified a kallsyms or vmlinux filename, use
2167          * it and only it, reporting errors to the user if it cannot be used.
2168          *
2169          * For instance, try to analyse an ARM perf.data file _without_ a
2170          * build-id, or if the user specifies the wrong path to the right
2171          * vmlinux file, obviously we can't fallback to another vmlinux (a
2172          * x86_86 one, on the machine where analysis is being performed, say),
2173          * or worse, /proc/kallsyms.
2174          *
2175          * If the specified file _has_ a build-id and there is a build-id
2176          * section in the perf.data file, we will still do the expected
2177          * validation in dso__load_vmlinux and will bail out if they don't
2178          * match.
2179          */
2180         if (symbol_conf.kallsyms_name != NULL) {
2181                 kallsyms_filename = symbol_conf.kallsyms_name;
2182                 goto do_kallsyms;
2183         }
2184 
2185         if (!symbol_conf.ignore_vmlinux && symbol_conf.vmlinux_name != NULL) {
2186                 return dso__load_vmlinux(dso, map, symbol_conf.vmlinux_name, false);
2187         }
2188 
2189         /*
2190          * Before checking on common vmlinux locations, check if it's
2191          * stored as standard build id binary (not kallsyms) under
2192          * .debug cache.
2193          */
2194         if (!symbol_conf.ignore_vmlinux_buildid)
2195                 filename = __dso__build_id_filename(dso, NULL, 0, false, false);
2196         if (filename != NULL) {
2197                 err = dso__load_vmlinux(dso, map, filename, true);
2198                 if (err > 0)
2199                         return err;
2200         }
2201 
2202         if (!symbol_conf.ignore_vmlinux && vmlinux_path != NULL) {
2203                 err = dso__load_vmlinux_path(dso, map);
2204                 if (err > 0)
2205                         return err;
2206         }
2207 
2208         /* do not try local files if a symfs was given */
2209         if (symbol_conf.symfs[0] != 0)
2210                 return -1;
2211 
2212         kallsyms_allocated_filename = dso__find_kallsyms(dso, map);
2213         if (!kallsyms_allocated_filename)
2214                 return -1;
2215 
2216         kallsyms_filename = kallsyms_allocated_filename;
2217 
2218 do_kallsyms:
2219         err = dso__load_kallsyms(dso, kallsyms_filename, map);
2220         if (err > 0)
2221                 pr_debug("Using %s for symbols\n", kallsyms_filename);
2222         free(kallsyms_allocated_filename);
2223 
2224         if (err > 0 && !dso__is_kcore(dso)) {
2225                 dso__set_binary_type(dso, DSO_BINARY_TYPE__KALLSYMS);
2226                 dso__set_long_name(dso, DSO__NAME_KALLSYMS, false);
2227                 map__fixup_start(map);
2228                 map__fixup_end(map);
2229         }
2230 
2231         return err;
2232 }
2233 
2234 static int dso__load_guest_kernel_sym(struct dso *dso, struct map *map)
2235 {
2236         int err;
2237         const char *kallsyms_filename;
2238         struct machine *machine = maps__machine(map__kmaps(map));
2239         char path[PATH_MAX];
2240 
2241         if (machine->kallsyms_filename) {
2242                 kallsyms_filename = machine->kallsyms_filename;
2243         } else if (machine__is_default_guest(machine)) {
2244                 /*
2245                  * if the user specified a vmlinux filename, use it and only
2246                  * it, reporting errors to the user if it cannot be used.
2247                  * Or use file guest_kallsyms inputted by user on commandline
2248                  */
2249                 if (symbol_conf.default_guest_vmlinux_name != NULL) {
2250                         err = dso__load_vmlinux(dso, map,
2251                                                 symbol_conf.default_guest_vmlinux_name,
2252                                                 false);
2253                         return err;
2254                 }
2255 
2256                 kallsyms_filename = symbol_conf.default_guest_kallsyms;
2257                 if (!kallsyms_filename)
2258                         return -1;
2259         } else {
2260                 sprintf(path, "%s/proc/kallsyms", machine->root_dir);
2261                 kallsyms_filename = path;
2262         }
2263 
2264         err = dso__load_kallsyms(dso, kallsyms_filename, map);
2265         if (err > 0)
2266                 pr_debug("Using %s for symbols\n", kallsyms_filename);
2267         if (err > 0 && !dso__is_kcore(dso)) {
2268                 dso__set_binary_type(dso, DSO_BINARY_TYPE__GUEST_KALLSYMS);
2269                 dso__set_long_name(dso, machine->mmap_name, false);
2270                 map__fixup_start(map);
2271                 map__fixup_end(map);
2272         }
2273 
2274         return err;
2275 }
2276 
2277 static void vmlinux_path__exit(void)
2278 {
2279         while (--vmlinux_path__nr_entries >= 0)
2280                 zfree(&vmlinux_path[vmlinux_path__nr_entries]);
2281         vmlinux_path__nr_entries = 0;
2282 
2283         zfree(&vmlinux_path);
2284 }
2285 
2286 static const char * const vmlinux_paths[] = {
2287         "vmlinux",
2288         "/boot/vmlinux"
2289 };
2290 
2291 static const char * const vmlinux_paths_upd[] = {
2292         "/boot/vmlinux-%s",
2293         "/usr/lib/debug/boot/vmlinux-%s",
2294         "/lib/modules/%s/build/vmlinux",
2295         "/usr/lib/debug/lib/modules/%s/vmlinux",
2296         "/usr/lib/debug/boot/vmlinux-%s.debug"
2297 };
2298 
2299 static int vmlinux_path__add(const char *new_entry)
2300 {
2301         vmlinux_path[vmlinux_path__nr_entries] = strdup(new_entry);
2302         if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
2303                 return -1;
2304         ++vmlinux_path__nr_entries;
2305 
2306         return 0;
2307 }
2308 
2309 static int vmlinux_path__init(struct perf_env *env)
2310 {
2311         struct utsname uts;
2312         char bf[PATH_MAX];
2313         char *kernel_version;
2314         unsigned int i;
2315 
2316         vmlinux_path = malloc(sizeof(char *) * (ARRAY_SIZE(vmlinux_paths) +
2317                               ARRAY_SIZE(vmlinux_paths_upd)));
2318         if (vmlinux_path == NULL)
2319                 return -1;
2320 
2321         for (i = 0; i < ARRAY_SIZE(vmlinux_paths); i++)
2322                 if (vmlinux_path__add(vmlinux_paths[i]) < 0)
2323                         goto out_fail;
2324 
2325         /* only try kernel version if no symfs was given */
2326         if (symbol_conf.symfs[0] != 0)
2327                 return 0;
2328 
2329         if (env) {
2330                 kernel_version = env->os_release;
2331         } else {
2332                 if (uname(&uts) < 0)
2333                         goto out_fail;
2334 
2335                 kernel_version = uts.release;
2336         }
2337 
2338         for (i = 0; i < ARRAY_SIZE(vmlinux_paths_upd); i++) {
2339                 snprintf(bf, sizeof(bf), vmlinux_paths_upd[i], kernel_version);
2340                 if (vmlinux_path__add(bf) < 0)
2341                         goto out_fail;
2342         }
2343 
2344         return 0;
2345 
2346 out_fail:
2347         vmlinux_path__exit();
2348         return -1;
2349 }
2350 
2351 int setup_list(struct strlist **list, const char *list_str,
2352                       const char *list_name)
2353 {
2354         if (list_str == NULL)
2355                 return 0;
2356 
2357         *list = strlist__new(list_str, NULL);
2358         if (!*list) {
2359                 pr_err("problems parsing %s list\n", list_name);
2360                 return -1;
2361         }
2362 
2363         symbol_conf.has_filter = true;
2364         return 0;
2365 }
2366 
2367 int setup_intlist(struct intlist **list, const char *list_str,
2368                   const char *list_name)
2369 {
2370         if (list_str == NULL)
2371                 return 0;
2372 
2373         *list = intlist__new(list_str);
2374         if (!*list) {
2375                 pr_err("problems parsing %s list\n", list_name);
2376                 return -1;
2377         }
2378         return 0;
2379 }
2380 
2381 static int setup_addrlist(struct intlist **addr_list, struct strlist *sym_list)
2382 {
2383         struct str_node *pos, *tmp;
2384         unsigned long val;
2385         char *sep;
2386         const char *end;
2387         int i = 0, err;
2388 
2389         *addr_list = intlist__new(NULL);
2390         if (!*addr_list)
2391                 return -1;
2392 
2393         strlist__for_each_entry_safe(pos, tmp, sym_list) {
2394                 errno = 0;
2395                 val = strtoul(pos->s, &sep, 16);
2396                 if (errno || (sep == pos->s))
2397                         continue;
2398 
2399                 if (*sep != '\0') {
2400                         end = pos->s + strlen(pos->s) - 1;
2401                         while (end >= sep && isspace(*end))
2402                                 end--;
2403 
2404                         if (end >= sep)
2405                                 continue;
2406                 }
2407 
2408                 err = intlist__add(*addr_list, val);
2409                 if (err)
2410                         break;
2411 
2412                 strlist__remove(sym_list, pos);
2413                 i++;
2414         }
2415 
2416         if (i == 0) {
2417                 intlist__delete(*addr_list);
2418                 *addr_list = NULL;
2419         }
2420 
2421         return 0;
2422 }
2423 
2424 static bool symbol__read_kptr_restrict(void)
2425 {
2426         bool value = false;
2427         FILE *fp = fopen("/proc/sys/kernel/kptr_restrict", "r");
2428 
2429         if (fp != NULL) {
2430                 char line[8];
2431 
2432                 if (fgets(line, sizeof(line), fp) != NULL)
2433                         value = perf_cap__capable(CAP_SYSLOG) ?
2434                                         (atoi(line) >= 2) :
2435                                         (atoi(line) != 0);
2436 
2437                 fclose(fp);
2438         }
2439 
2440         /* Per kernel/kallsyms.c:
2441          * we also restrict when perf_event_paranoid > 1 w/o CAP_SYSLOG
2442          */
2443         if (perf_event_paranoid() > 1 && !perf_cap__capable(CAP_SYSLOG))
2444                 value = true;
2445 
2446         return value;
2447 }
2448 
2449 int symbol__annotation_init(void)
2450 {
2451         if (symbol_conf.init_annotation)
2452                 return 0;
2453 
2454         if (symbol_conf.initialized) {
2455                 pr_err("Annotation needs to be init before symbol__init()\n");
2456                 return -1;
2457         }
2458 
2459         symbol_conf.priv_size += sizeof(struct annotation);
2460         symbol_conf.init_annotation = true;
2461         return 0;
2462 }
2463 
2464 int symbol__init(struct perf_env *env)
2465 {
2466         const char *symfs;
2467 
2468         if (symbol_conf.initialized)
2469                 return 0;
2470 
2471         symbol_conf.priv_size = PERF_ALIGN(symbol_conf.priv_size, sizeof(u64));
2472 
2473         symbol__elf_init();
2474 
2475         if (symbol_conf.try_vmlinux_path && vmlinux_path__init(env) < 0)
2476                 return -1;
2477 
2478         if (symbol_conf.field_sep && *symbol_conf.field_sep == '.') {
2479                 pr_err("'.' is the only non valid --field-separator argument\n");
2480                 return -1;
2481         }
2482 
2483         if (setup_list(&symbol_conf.dso_list,
2484                        symbol_conf.dso_list_str, "dso") < 0)
2485                 return -1;
2486 
2487         if (setup_list(&symbol_conf.comm_list,
2488                        symbol_conf.comm_list_str, "comm") < 0)
2489                 goto out_free_dso_list;
2490 
2491         if (setup_intlist(&symbol_conf.pid_list,
2492                        symbol_conf.pid_list_str, "pid") < 0)
2493                 goto out_free_comm_list;
2494 
2495         if (setup_intlist(&symbol_conf.tid_list,
2496                        symbol_conf.tid_list_str, "tid") < 0)
2497                 goto out_free_pid_list;
2498 
2499         if (setup_list(&symbol_conf.sym_list,
2500                        symbol_conf.sym_list_str, "symbol") < 0)
2501                 goto out_free_tid_list;
2502 
2503         if (symbol_conf.sym_list &&
2504             setup_addrlist(&symbol_conf.addr_list, symbol_conf.sym_list) < 0)
2505                 goto out_free_sym_list;
2506 
2507         if (setup_list(&symbol_conf.bt_stop_list,
2508                        symbol_conf.bt_stop_list_str, "symbol") < 0)
2509                 goto out_free_sym_list;
2510 
2511         /*
2512          * A path to symbols of "/" is identical to ""
2513          * reset here for simplicity.
2514          */
2515         symfs = realpath(symbol_conf.symfs, NULL);
2516         if (symfs == NULL)
2517                 symfs = symbol_conf.symfs;
2518         if (strcmp(symfs, "/") == 0)
2519                 symbol_conf.symfs = "";
2520         if (symfs != symbol_conf.symfs)
2521                 free((void *)symfs);
2522 
2523         symbol_conf.kptr_restrict = symbol__read_kptr_restrict();
2524 
2525         symbol_conf.initialized = true;
2526         return 0;
2527 
2528 out_free_sym_list:
2529         strlist__delete(symbol_conf.sym_list);
2530         intlist__delete(symbol_conf.addr_list);
2531 out_free_tid_list:
2532         intlist__delete(symbol_conf.tid_list);
2533 out_free_pid_list:
2534         intlist__delete(symbol_conf.pid_list);
2535 out_free_comm_list:
2536         strlist__delete(symbol_conf.comm_list);
2537 out_free_dso_list:
2538         strlist__delete(symbol_conf.dso_list);
2539         return -1;
2540 }
2541 
2542 void symbol__exit(void)
2543 {
2544         if (!symbol_conf.initialized)
2545                 return;
2546         strlist__delete(symbol_conf.bt_stop_list);
2547         strlist__delete(symbol_conf.sym_list);
2548         strlist__delete(symbol_conf.dso_list);
2549         strlist__delete(symbol_conf.comm_list);
2550         intlist__delete(symbol_conf.tid_list);
2551         intlist__delete(symbol_conf.pid_list);
2552         intlist__delete(symbol_conf.addr_list);
2553         vmlinux_path__exit();
2554         symbol_conf.sym_list = symbol_conf.dso_list = symbol_conf.comm_list = NULL;
2555         symbol_conf.bt_stop_list = NULL;
2556         symbol_conf.initialized = false;
2557 }
2558 
2559 int symbol__config_symfs(const struct option *opt __maybe_unused,
2560                          const char *dir, int unset __maybe_unused)
2561 {
2562         char *bf = NULL;
2563         int ret;
2564 
2565         symbol_conf.symfs = strdup(dir);
2566         if (symbol_conf.symfs == NULL)
2567                 return -ENOMEM;
2568 
2569         /* skip the locally configured cache if a symfs is given, and
2570          * config buildid dir to symfs/.debug
2571          */
2572         ret = asprintf(&bf, "%s/%s", dir, ".debug");
2573         if (ret < 0)
2574                 return -ENOMEM;
2575 
2576         set_buildid_dir(bf);
2577 
2578         free(bf);
2579         return 0;
2580 }
2581 
2582 /*
2583  * Checks that user supplied symbol kernel files are accessible because
2584  * the default mechanism for accessing elf files fails silently. i.e. if
2585  * debug syms for a build ID aren't found perf carries on normally. When
2586  * they are user supplied we should assume that the user doesn't want to
2587  * silently fail.
2588  */
2589 int symbol__validate_sym_arguments(void)
2590 {
2591         if (symbol_conf.vmlinux_name &&
2592             access(symbol_conf.vmlinux_name, R_OK)) {
2593                 pr_err("Invalid file: %s\n", symbol_conf.vmlinux_name);
2594                 return -EINVAL;
2595         }
2596         if (symbol_conf.kallsyms_name &&
2597             access(symbol_conf.kallsyms_name, R_OK)) {
2598                 pr_err("Invalid file: %s\n", symbol_conf.kallsyms_name);
2599                 return -EINVAL;
2600         }
2601         return 0;
2602 }
2603 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php