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