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