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