xref: /openbmc/linux/tools/perf/util/symbol.c (revision 046b212a)
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 		next = rb_entry(nd, struct symbol, rb_node);
206 
207 		if (!nd)
208 			break;
209 
210 		if (curr->start != next->start)
211 			continue;
212 
213 		if (choose_best_symbol(curr, next) == SYMBOL_A) {
214 			if (next->type == STT_GNU_IFUNC)
215 				curr->ifunc_alias = true;
216 			rb_erase_cached(&next->rb_node, symbols);
217 			symbol__delete(next);
218 			goto again;
219 		} else {
220 			if (curr->type == STT_GNU_IFUNC)
221 				next->ifunc_alias = true;
222 			nd = rb_next(&curr->rb_node);
223 			rb_erase_cached(&curr->rb_node, symbols);
224 			symbol__delete(curr);
225 		}
226 	}
227 }
228 
229 /* Update zero-sized symbols using the address of the next symbol */
230 void symbols__fixup_end(struct rb_root_cached *symbols, bool is_kallsyms)
231 {
232 	struct rb_node *nd, *prevnd = rb_first_cached(symbols);
233 	struct symbol *curr, *prev;
234 
235 	if (prevnd == NULL)
236 		return;
237 
238 	curr = rb_entry(prevnd, struct symbol, rb_node);
239 
240 	for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
241 		prev = curr;
242 		curr = rb_entry(nd, struct symbol, rb_node);
243 
244 		/*
245 		 * On some architecture kernel text segment start is located at
246 		 * some low memory address, while modules are located at high
247 		 * memory addresses (or vice versa).  The gap between end of
248 		 * kernel text segment and beginning of first module's text
249 		 * segment is very big.  Therefore do not fill this gap and do
250 		 * not assign it to the kernel dso map (kallsyms).
251 		 *
252 		 * In kallsyms, it determines module symbols using '[' character
253 		 * like in:
254 		 *   ffffffffc1937000 T hdmi_driver_init  [snd_hda_codec_hdmi]
255 		 */
256 		if (prev->end == prev->start) {
257 			/* Last kernel/module symbol mapped to end of page */
258 			if (is_kallsyms && (!strchr(prev->name, '[') !=
259 					    !strchr(curr->name, '[')))
260 				prev->end = roundup(prev->end + 4096, 4096);
261 			else
262 				prev->end = curr->start;
263 
264 			pr_debug4("%s sym:%s end:%#" PRIx64 "\n",
265 				  __func__, prev->name, prev->end);
266 		}
267 	}
268 
269 	/* Last entry */
270 	if (curr->end == curr->start)
271 		curr->end = roundup(curr->start, 4096) + 4096;
272 }
273 
274 void maps__fixup_end(struct maps *maps)
275 {
276 	struct map_rb_node *prev = NULL, *curr;
277 
278 	down_write(maps__lock(maps));
279 
280 	maps__for_each_entry(maps, curr) {
281 		if (prev != NULL && !map__end(prev->map))
282 			map__set_end(prev->map, map__start(curr->map));
283 
284 		prev = curr;
285 	}
286 
287 	/*
288 	 * We still haven't the actual symbols, so guess the
289 	 * last map final address.
290 	 */
291 	if (curr && !map__end(curr->map))
292 		map__set_end(curr->map, ~0ULL);
293 
294 	up_write(maps__lock(maps));
295 }
296 
297 struct symbol *symbol__new(u64 start, u64 len, u8 binding, u8 type, const char *name)
298 {
299 	size_t namelen = strlen(name) + 1;
300 	struct symbol *sym = calloc(1, (symbol_conf.priv_size +
301 					sizeof(*sym) + namelen));
302 	if (sym == NULL)
303 		return NULL;
304 
305 	if (symbol_conf.priv_size) {
306 		if (symbol_conf.init_annotation) {
307 			struct annotation *notes = (void *)sym;
308 			annotation__init(notes);
309 		}
310 		sym = ((void *)sym) + symbol_conf.priv_size;
311 	}
312 
313 	sym->start   = start;
314 	sym->end     = len ? start + len : start;
315 	sym->type    = type;
316 	sym->binding = binding;
317 	sym->namelen = namelen - 1;
318 
319 	pr_debug4("%s: %s %#" PRIx64 "-%#" PRIx64 "\n",
320 		  __func__, name, start, sym->end);
321 	memcpy(sym->name, name, namelen);
322 
323 	return sym;
324 }
325 
326 void symbol__delete(struct symbol *sym)
327 {
328 	if (symbol_conf.priv_size) {
329 		if (symbol_conf.init_annotation) {
330 			struct annotation *notes = symbol__annotation(sym);
331 
332 			annotation__exit(notes);
333 		}
334 	}
335 	free(((void *)sym) - symbol_conf.priv_size);
336 }
337 
338 void symbols__delete(struct rb_root_cached *symbols)
339 {
340 	struct symbol *pos;
341 	struct rb_node *next = rb_first_cached(symbols);
342 
343 	while (next) {
344 		pos = rb_entry(next, struct symbol, rb_node);
345 		next = rb_next(&pos->rb_node);
346 		rb_erase_cached(&pos->rb_node, symbols);
347 		symbol__delete(pos);
348 	}
349 }
350 
351 void __symbols__insert(struct rb_root_cached *symbols,
352 		       struct symbol *sym, bool kernel)
353 {
354 	struct rb_node **p = &symbols->rb_root.rb_node;
355 	struct rb_node *parent = NULL;
356 	const u64 ip = sym->start;
357 	struct symbol *s;
358 	bool leftmost = true;
359 
360 	if (kernel) {
361 		const char *name = sym->name;
362 		/*
363 		 * ppc64 uses function descriptors and appends a '.' to the
364 		 * start of every instruction address. Remove it.
365 		 */
366 		if (name[0] == '.')
367 			name++;
368 		sym->idle = symbol__is_idle(name);
369 	}
370 
371 	while (*p != NULL) {
372 		parent = *p;
373 		s = rb_entry(parent, struct symbol, rb_node);
374 		if (ip < s->start)
375 			p = &(*p)->rb_left;
376 		else {
377 			p = &(*p)->rb_right;
378 			leftmost = false;
379 		}
380 	}
381 	rb_link_node(&sym->rb_node, parent, p);
382 	rb_insert_color_cached(&sym->rb_node, symbols, leftmost);
383 }
384 
385 void symbols__insert(struct rb_root_cached *symbols, struct symbol *sym)
386 {
387 	__symbols__insert(symbols, sym, false);
388 }
389 
390 static struct symbol *symbols__find(struct rb_root_cached *symbols, u64 ip)
391 {
392 	struct rb_node *n;
393 
394 	if (symbols == NULL)
395 		return NULL;
396 
397 	n = symbols->rb_root.rb_node;
398 
399 	while (n) {
400 		struct symbol *s = rb_entry(n, struct symbol, rb_node);
401 
402 		if (ip < s->start)
403 			n = n->rb_left;
404 		else if (ip > s->end || (ip == s->end && ip != s->start))
405 			n = n->rb_right;
406 		else
407 			return s;
408 	}
409 
410 	return NULL;
411 }
412 
413 static struct symbol *symbols__first(struct rb_root_cached *symbols)
414 {
415 	struct rb_node *n = rb_first_cached(symbols);
416 
417 	if (n)
418 		return rb_entry(n, struct symbol, rb_node);
419 
420 	return NULL;
421 }
422 
423 static struct symbol *symbols__last(struct rb_root_cached *symbols)
424 {
425 	struct rb_node *n = rb_last(&symbols->rb_root);
426 
427 	if (n)
428 		return rb_entry(n, struct symbol, rb_node);
429 
430 	return NULL;
431 }
432 
433 static struct symbol *symbols__next(struct symbol *sym)
434 {
435 	struct rb_node *n = rb_next(&sym->rb_node);
436 
437 	if (n)
438 		return rb_entry(n, struct symbol, rb_node);
439 
440 	return NULL;
441 }
442 
443 static int symbols__sort_name_cmp(const void *vlhs, const void *vrhs)
444 {
445 	const struct symbol *lhs = *((const struct symbol **)vlhs);
446 	const struct symbol *rhs = *((const struct symbol **)vrhs);
447 
448 	return strcmp(lhs->name, rhs->name);
449 }
450 
451 static struct symbol **symbols__sort_by_name(struct rb_root_cached *source, size_t *len)
452 {
453 	struct rb_node *nd;
454 	struct symbol **result;
455 	size_t i = 0, size = 0;
456 
457 	for (nd = rb_first_cached(source); nd; nd = rb_next(nd))
458 		size++;
459 
460 	result = malloc(sizeof(*result) * size);
461 	if (!result)
462 		return NULL;
463 
464 	for (nd = rb_first_cached(source); nd; nd = rb_next(nd)) {
465 		struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
466 
467 		result[i++] = pos;
468 	}
469 	qsort(result, size, sizeof(*result), symbols__sort_name_cmp);
470 	*len = size;
471 	return result;
472 }
473 
474 int symbol__match_symbol_name(const char *name, const char *str,
475 			      enum symbol_tag_include includes)
476 {
477 	const char *versioning;
478 
479 	if (includes == SYMBOL_TAG_INCLUDE__DEFAULT_ONLY &&
480 	    (versioning = strstr(name, "@@"))) {
481 		int len = strlen(str);
482 
483 		if (len < versioning - name)
484 			len = versioning - name;
485 
486 		return arch__compare_symbol_names_n(name, str, len);
487 	} else
488 		return arch__compare_symbol_names(name, str);
489 }
490 
491 static struct symbol *symbols__find_by_name(struct symbol *symbols[],
492 					    size_t symbols_len,
493 					    const char *name,
494 					    enum symbol_tag_include includes,
495 					    size_t *found_idx)
496 {
497 	size_t i, lower = 0, upper = symbols_len;
498 	struct symbol *s = NULL;
499 
500 	if (found_idx)
501 		*found_idx = SIZE_MAX;
502 
503 	if (!symbols_len)
504 		return NULL;
505 
506 	while (lower < upper) {
507 		int cmp;
508 
509 		i = (lower + upper) / 2;
510 		cmp = symbol__match_symbol_name(symbols[i]->name, name, includes);
511 
512 		if (cmp > 0)
513 			upper = i;
514 		else if (cmp < 0)
515 			lower = i + 1;
516 		else {
517 			if (found_idx)
518 				*found_idx = i;
519 			s = symbols[i];
520 			break;
521 		}
522 	}
523 	if (s && includes != SYMBOL_TAG_INCLUDE__DEFAULT_ONLY) {
524 		/* return first symbol that has same name (if any) */
525 		for (; i > 0; i--) {
526 			struct symbol *tmp = symbols[i - 1];
527 
528 			if (!arch__compare_symbol_names(tmp->name, s->name)) {
529 				if (found_idx)
530 					*found_idx = i - 1;
531 				s = tmp;
532 			} else
533 				break;
534 		}
535 	}
536 	assert(!found_idx || !s || s == symbols[*found_idx]);
537 	return s;
538 }
539 
540 void dso__reset_find_symbol_cache(struct dso *dso)
541 {
542 	dso->last_find_result.addr   = 0;
543 	dso->last_find_result.symbol = NULL;
544 }
545 
546 void dso__insert_symbol(struct dso *dso, struct symbol *sym)
547 {
548 	__symbols__insert(&dso->symbols, sym, dso->kernel);
549 
550 	/* update the symbol cache if necessary */
551 	if (dso->last_find_result.addr >= sym->start &&
552 	    (dso->last_find_result.addr < sym->end ||
553 	    sym->start == sym->end)) {
554 		dso->last_find_result.symbol = sym;
555 	}
556 }
557 
558 void dso__delete_symbol(struct dso *dso, struct symbol *sym)
559 {
560 	rb_erase_cached(&sym->rb_node, &dso->symbols);
561 	symbol__delete(sym);
562 	dso__reset_find_symbol_cache(dso);
563 }
564 
565 struct symbol *dso__find_symbol(struct dso *dso, u64 addr)
566 {
567 	if (dso->last_find_result.addr != addr || dso->last_find_result.symbol == NULL) {
568 		dso->last_find_result.addr   = addr;
569 		dso->last_find_result.symbol = symbols__find(&dso->symbols, addr);
570 	}
571 
572 	return dso->last_find_result.symbol;
573 }
574 
575 struct symbol *dso__find_symbol_nocache(struct dso *dso, u64 addr)
576 {
577 	return symbols__find(&dso->symbols, addr);
578 }
579 
580 struct symbol *dso__first_symbol(struct dso *dso)
581 {
582 	return symbols__first(&dso->symbols);
583 }
584 
585 struct symbol *dso__last_symbol(struct dso *dso)
586 {
587 	return symbols__last(&dso->symbols);
588 }
589 
590 struct symbol *dso__next_symbol(struct symbol *sym)
591 {
592 	return symbols__next(sym);
593 }
594 
595 struct symbol *dso__next_symbol_by_name(struct dso *dso, size_t *idx)
596 {
597 	if (*idx + 1 >= dso->symbol_names_len)
598 		return NULL;
599 
600 	++*idx;
601 	return dso->symbol_names[*idx];
602 }
603 
604  /*
605   * Returns first symbol that matched with @name.
606   */
607 struct symbol *dso__find_symbol_by_name(struct dso *dso, const char *name, size_t *idx)
608 {
609 	struct symbol *s = symbols__find_by_name(dso->symbol_names, dso->symbol_names_len,
610 						name, SYMBOL_TAG_INCLUDE__NONE, idx);
611 	if (!s)
612 		s = symbols__find_by_name(dso->symbol_names, dso->symbol_names_len,
613 					name, SYMBOL_TAG_INCLUDE__DEFAULT_ONLY, idx);
614 	return s;
615 }
616 
617 void dso__sort_by_name(struct dso *dso)
618 {
619 	mutex_lock(&dso->lock);
620 	if (!dso__sorted_by_name(dso)) {
621 		size_t len;
622 
623 		dso->symbol_names = symbols__sort_by_name(&dso->symbols, &len);
624 		if (dso->symbol_names) {
625 			dso->symbol_names_len = len;
626 			dso__set_sorted_by_name(dso);
627 		}
628 	}
629 	mutex_unlock(&dso->lock);
630 }
631 
632 /*
633  * While we find nice hex chars, build a long_val.
634  * Return number of chars processed.
635  */
636 static int hex2u64(const char *ptr, u64 *long_val)
637 {
638 	char *p;
639 
640 	*long_val = strtoull(ptr, &p, 16);
641 
642 	return p - ptr;
643 }
644 
645 
646 int modules__parse(const char *filename, void *arg,
647 		   int (*process_module)(void *arg, const char *name,
648 					 u64 start, u64 size))
649 {
650 	char *line = NULL;
651 	size_t n;
652 	FILE *file;
653 	int err = 0;
654 
655 	file = fopen(filename, "r");
656 	if (file == NULL)
657 		return -1;
658 
659 	while (1) {
660 		char name[PATH_MAX];
661 		u64 start, size;
662 		char *sep, *endptr;
663 		ssize_t line_len;
664 
665 		line_len = getline(&line, &n, file);
666 		if (line_len < 0) {
667 			if (feof(file))
668 				break;
669 			err = -1;
670 			goto out;
671 		}
672 
673 		if (!line) {
674 			err = -1;
675 			goto out;
676 		}
677 
678 		line[--line_len] = '\0'; /* \n */
679 
680 		sep = strrchr(line, 'x');
681 		if (sep == NULL)
682 			continue;
683 
684 		hex2u64(sep + 1, &start);
685 
686 		sep = strchr(line, ' ');
687 		if (sep == NULL)
688 			continue;
689 
690 		*sep = '\0';
691 
692 		scnprintf(name, sizeof(name), "[%s]", line);
693 
694 		size = strtoul(sep + 1, &endptr, 0);
695 		if (*endptr != ' ' && *endptr != '\t')
696 			continue;
697 
698 		err = process_module(arg, name, start, size);
699 		if (err)
700 			break;
701 	}
702 out:
703 	free(line);
704 	fclose(file);
705 	return err;
706 }
707 
708 /*
709  * These are symbols in the kernel image, so make sure that
710  * sym is from a kernel DSO.
711  */
712 static bool symbol__is_idle(const char *name)
713 {
714 	const char * const idle_symbols[] = {
715 		"acpi_idle_do_entry",
716 		"acpi_processor_ffh_cstate_enter",
717 		"arch_cpu_idle",
718 		"cpu_idle",
719 		"cpu_startup_entry",
720 		"idle_cpu",
721 		"intel_idle",
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_ACCESS(curr_map) != RC_CHK_ACCESS(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_ACCESS(new_map) == RC_CHK_ACCESS(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 		} else {
1737 			dso->text_offset = section->vma - section->filepos;
1738 		}
1739 	}
1740 
1741 	qsort(symbols, symbols_count, sizeof(asymbol *), bfd_symbols__cmpvalue);
1742 
1743 #ifdef bfd_get_section
1744 #define bfd_asymbol_section bfd_get_section
1745 #endif
1746 	for (i = 0; i < symbols_count; ++i) {
1747 		sym = symbols[i];
1748 		section = bfd_asymbol_section(sym);
1749 		if (bfd2elf_binding(sym) < 0)
1750 			continue;
1751 
1752 		while (i + 1 < symbols_count &&
1753 		       bfd_asymbol_section(symbols[i + 1]) == section &&
1754 		       bfd2elf_binding(symbols[i + 1]) < 0)
1755 			i++;
1756 
1757 		if (i + 1 < symbols_count &&
1758 		    bfd_asymbol_section(symbols[i + 1]) == section)
1759 			len = symbols[i + 1]->value - sym->value;
1760 		else
1761 			len = section->size - sym->value;
1762 
1763 		start = bfd_asymbol_value(sym) - dso->text_offset;
1764 		symbol = symbol__new(start, len, bfd2elf_binding(sym), STT_FUNC,
1765 				     bfd_asymbol_name(sym));
1766 		if (!symbol)
1767 			goto out_free;
1768 
1769 		symbols__insert(&dso->symbols, symbol);
1770 	}
1771 #ifdef bfd_get_section
1772 #undef bfd_asymbol_section
1773 #endif
1774 
1775 	symbols__fixup_end(&dso->symbols, false);
1776 	symbols__fixup_duplicate(&dso->symbols);
1777 	dso->adjust_symbols = 1;
1778 
1779 	err = 0;
1780 out_free:
1781 	free(symbols);
1782 out_close:
1783 	bfd_close(abfd);
1784 	return err;
1785 }
1786 #endif
1787 
1788 static bool dso__is_compatible_symtab_type(struct dso *dso, bool kmod,
1789 					   enum dso_binary_type type)
1790 {
1791 	switch (type) {
1792 	case DSO_BINARY_TYPE__JAVA_JIT:
1793 	case DSO_BINARY_TYPE__DEBUGLINK:
1794 	case DSO_BINARY_TYPE__SYSTEM_PATH_DSO:
1795 	case DSO_BINARY_TYPE__FEDORA_DEBUGINFO:
1796 	case DSO_BINARY_TYPE__UBUNTU_DEBUGINFO:
1797 	case DSO_BINARY_TYPE__MIXEDUP_UBUNTU_DEBUGINFO:
1798 	case DSO_BINARY_TYPE__BUILDID_DEBUGINFO:
1799 	case DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO:
1800 		return !kmod && dso->kernel == DSO_SPACE__USER;
1801 
1802 	case DSO_BINARY_TYPE__KALLSYMS:
1803 	case DSO_BINARY_TYPE__VMLINUX:
1804 	case DSO_BINARY_TYPE__KCORE:
1805 		return dso->kernel == DSO_SPACE__KERNEL;
1806 
1807 	case DSO_BINARY_TYPE__GUEST_KALLSYMS:
1808 	case DSO_BINARY_TYPE__GUEST_VMLINUX:
1809 	case DSO_BINARY_TYPE__GUEST_KCORE:
1810 		return dso->kernel == DSO_SPACE__KERNEL_GUEST;
1811 
1812 	case DSO_BINARY_TYPE__GUEST_KMODULE:
1813 	case DSO_BINARY_TYPE__GUEST_KMODULE_COMP:
1814 	case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE:
1815 	case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP:
1816 		/*
1817 		 * kernel modules know their symtab type - it's set when
1818 		 * creating a module dso in machine__addnew_module_map().
1819 		 */
1820 		return kmod && dso->symtab_type == type;
1821 
1822 	case DSO_BINARY_TYPE__BUILD_ID_CACHE:
1823 	case DSO_BINARY_TYPE__BUILD_ID_CACHE_DEBUGINFO:
1824 		return true;
1825 
1826 	case DSO_BINARY_TYPE__BPF_PROG_INFO:
1827 	case DSO_BINARY_TYPE__BPF_IMAGE:
1828 	case DSO_BINARY_TYPE__OOL:
1829 	case DSO_BINARY_TYPE__NOT_FOUND:
1830 	default:
1831 		return false;
1832 	}
1833 }
1834 
1835 /* Checks for the existence of the perf-<pid>.map file in two different
1836  * locations.  First, if the process is a separate mount namespace, check in
1837  * that namespace using the pid of the innermost pid namespace.  If's not in a
1838  * namespace, or the file can't be found there, try in the mount namespace of
1839  * the tracing process using our view of its pid.
1840  */
1841 static int dso__find_perf_map(char *filebuf, size_t bufsz,
1842 			      struct nsinfo **nsip)
1843 {
1844 	struct nscookie nsc;
1845 	struct nsinfo *nsi;
1846 	struct nsinfo *nnsi;
1847 	int rc = -1;
1848 
1849 	nsi = *nsip;
1850 
1851 	if (nsinfo__need_setns(nsi)) {
1852 		snprintf(filebuf, bufsz, "/tmp/perf-%d.map", nsinfo__nstgid(nsi));
1853 		nsinfo__mountns_enter(nsi, &nsc);
1854 		rc = access(filebuf, R_OK);
1855 		nsinfo__mountns_exit(&nsc);
1856 		if (rc == 0)
1857 			return rc;
1858 	}
1859 
1860 	nnsi = nsinfo__copy(nsi);
1861 	if (nnsi) {
1862 		nsinfo__put(nsi);
1863 
1864 		nsinfo__clear_need_setns(nnsi);
1865 		snprintf(filebuf, bufsz, "/tmp/perf-%d.map", nsinfo__tgid(nnsi));
1866 		*nsip = nnsi;
1867 		rc = 0;
1868 	}
1869 
1870 	return rc;
1871 }
1872 
1873 int dso__load(struct dso *dso, struct map *map)
1874 {
1875 	char *name;
1876 	int ret = -1;
1877 	u_int i;
1878 	struct machine *machine = NULL;
1879 	char *root_dir = (char *) "";
1880 	int ss_pos = 0;
1881 	struct symsrc ss_[2];
1882 	struct symsrc *syms_ss = NULL, *runtime_ss = NULL;
1883 	bool kmod;
1884 	bool perfmap;
1885 	struct build_id bid;
1886 	struct nscookie nsc;
1887 	char newmapname[PATH_MAX];
1888 	const char *map_path = dso->long_name;
1889 
1890 	mutex_lock(&dso->lock);
1891 	perfmap = strncmp(dso->name, "/tmp/perf-", 10) == 0;
1892 	if (perfmap) {
1893 		if (dso->nsinfo && (dso__find_perf_map(newmapname,
1894 		    sizeof(newmapname), &dso->nsinfo) == 0)) {
1895 			map_path = newmapname;
1896 		}
1897 	}
1898 
1899 	nsinfo__mountns_enter(dso->nsinfo, &nsc);
1900 
1901 	/* check again under the dso->lock */
1902 	if (dso__loaded(dso)) {
1903 		ret = 1;
1904 		goto out;
1905 	}
1906 
1907 	kmod = dso->symtab_type == DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE ||
1908 		dso->symtab_type == DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP ||
1909 		dso->symtab_type == DSO_BINARY_TYPE__GUEST_KMODULE ||
1910 		dso->symtab_type == DSO_BINARY_TYPE__GUEST_KMODULE_COMP;
1911 
1912 	if (dso->kernel && !kmod) {
1913 		if (dso->kernel == DSO_SPACE__KERNEL)
1914 			ret = dso__load_kernel_sym(dso, map);
1915 		else if (dso->kernel == DSO_SPACE__KERNEL_GUEST)
1916 			ret = dso__load_guest_kernel_sym(dso, map);
1917 
1918 		machine = maps__machine(map__kmaps(map));
1919 		if (machine__is(machine, "x86_64"))
1920 			machine__map_x86_64_entry_trampolines(machine, dso);
1921 		goto out;
1922 	}
1923 
1924 	dso->adjust_symbols = 0;
1925 
1926 	if (perfmap) {
1927 		ret = dso__load_perf_map(map_path, dso);
1928 		dso->symtab_type = ret > 0 ? DSO_BINARY_TYPE__JAVA_JIT :
1929 					     DSO_BINARY_TYPE__NOT_FOUND;
1930 		goto out;
1931 	}
1932 
1933 	if (machine)
1934 		root_dir = machine->root_dir;
1935 
1936 	name = malloc(PATH_MAX);
1937 	if (!name)
1938 		goto out;
1939 
1940 	/*
1941 	 * Read the build id if possible. This is required for
1942 	 * DSO_BINARY_TYPE__BUILDID_DEBUGINFO to work
1943 	 */
1944 	if (!dso->has_build_id &&
1945 	    is_regular_file(dso->long_name)) {
1946 	    __symbol__join_symfs(name, PATH_MAX, dso->long_name);
1947 		if (filename__read_build_id(name, &bid) > 0)
1948 			dso__set_build_id(dso, &bid);
1949 	}
1950 
1951 	/*
1952 	 * Iterate over candidate debug images.
1953 	 * Keep track of "interesting" ones (those which have a symtab, dynsym,
1954 	 * and/or opd section) for processing.
1955 	 */
1956 	for (i = 0; i < DSO_BINARY_TYPE__SYMTAB_CNT; i++) {
1957 		struct symsrc *ss = &ss_[ss_pos];
1958 		bool next_slot = false;
1959 		bool is_reg;
1960 		bool nsexit;
1961 		int bfdrc = -1;
1962 		int sirc = -1;
1963 
1964 		enum dso_binary_type symtab_type = binary_type_symtab[i];
1965 
1966 		nsexit = (symtab_type == DSO_BINARY_TYPE__BUILD_ID_CACHE ||
1967 		    symtab_type == DSO_BINARY_TYPE__BUILD_ID_CACHE_DEBUGINFO);
1968 
1969 		if (!dso__is_compatible_symtab_type(dso, kmod, symtab_type))
1970 			continue;
1971 
1972 		if (dso__read_binary_type_filename(dso, symtab_type,
1973 						   root_dir, name, PATH_MAX))
1974 			continue;
1975 
1976 		if (nsexit)
1977 			nsinfo__mountns_exit(&nsc);
1978 
1979 		is_reg = is_regular_file(name);
1980 		if (!is_reg && errno == ENOENT && dso->nsinfo) {
1981 			char *new_name = dso__filename_with_chroot(dso, name);
1982 			if (new_name) {
1983 				is_reg = is_regular_file(new_name);
1984 				strlcpy(name, new_name, PATH_MAX);
1985 				free(new_name);
1986 			}
1987 		}
1988 
1989 #ifdef HAVE_LIBBFD_SUPPORT
1990 		if (is_reg)
1991 			bfdrc = dso__load_bfd_symbols(dso, name);
1992 #endif
1993 		if (is_reg && bfdrc < 0)
1994 			sirc = symsrc__init(ss, dso, name, symtab_type);
1995 
1996 		if (nsexit)
1997 			nsinfo__mountns_enter(dso->nsinfo, &nsc);
1998 
1999 		if (bfdrc == 0) {
2000 			ret = 0;
2001 			break;
2002 		}
2003 
2004 		if (!is_reg || sirc < 0)
2005 			continue;
2006 
2007 		if (!syms_ss && symsrc__has_symtab(ss)) {
2008 			syms_ss = ss;
2009 			next_slot = true;
2010 			if (!dso->symsrc_filename)
2011 				dso->symsrc_filename = strdup(name);
2012 		}
2013 
2014 		if (!runtime_ss && symsrc__possibly_runtime(ss)) {
2015 			runtime_ss = ss;
2016 			next_slot = true;
2017 		}
2018 
2019 		if (next_slot) {
2020 			ss_pos++;
2021 
2022 			if (syms_ss && runtime_ss)
2023 				break;
2024 		} else {
2025 			symsrc__destroy(ss);
2026 		}
2027 
2028 	}
2029 
2030 	if (!runtime_ss && !syms_ss)
2031 		goto out_free;
2032 
2033 	if (runtime_ss && !syms_ss) {
2034 		syms_ss = runtime_ss;
2035 	}
2036 
2037 	/* We'll have to hope for the best */
2038 	if (!runtime_ss && syms_ss)
2039 		runtime_ss = syms_ss;
2040 
2041 	if (syms_ss)
2042 		ret = dso__load_sym(dso, map, syms_ss, runtime_ss, kmod);
2043 	else
2044 		ret = -1;
2045 
2046 	if (ret > 0) {
2047 		int nr_plt;
2048 
2049 		nr_plt = dso__synthesize_plt_symbols(dso, runtime_ss);
2050 		if (nr_plt > 0)
2051 			ret += nr_plt;
2052 	}
2053 
2054 	for (; ss_pos > 0; ss_pos--)
2055 		symsrc__destroy(&ss_[ss_pos - 1]);
2056 out_free:
2057 	free(name);
2058 	if (ret < 0 && strstr(dso->name, " (deleted)") != NULL)
2059 		ret = 0;
2060 out:
2061 	dso__set_loaded(dso);
2062 	mutex_unlock(&dso->lock);
2063 	nsinfo__mountns_exit(&nsc);
2064 
2065 	return ret;
2066 }
2067 
2068 static int map__strcmp(const void *a, const void *b)
2069 {
2070 	const struct map *map_a = *(const struct map **)a;
2071 	const struct map *map_b = *(const struct map **)b;
2072 	const struct dso *dso_a = map__dso(map_a);
2073 	const struct dso *dso_b = map__dso(map_b);
2074 	int ret = strcmp(dso_a->short_name, dso_b->short_name);
2075 
2076 	if (ret == 0 && map_a != map_b) {
2077 		/*
2078 		 * Ensure distinct but name equal maps have an order in part to
2079 		 * aid reference counting.
2080 		 */
2081 		ret = (int)map__start(map_a) - (int)map__start(map_b);
2082 		if (ret == 0)
2083 			ret = (int)((intptr_t)map_a - (intptr_t)map_b);
2084 	}
2085 
2086 	return ret;
2087 }
2088 
2089 static int map__strcmp_name(const void *name, const void *b)
2090 {
2091 	const struct dso *dso = map__dso(*(const struct map **)b);
2092 
2093 	return strcmp(name, dso->short_name);
2094 }
2095 
2096 void __maps__sort_by_name(struct maps *maps)
2097 {
2098 	qsort(maps__maps_by_name(maps), maps__nr_maps(maps), sizeof(struct map *), map__strcmp);
2099 }
2100 
2101 static int map__groups__sort_by_name_from_rbtree(struct maps *maps)
2102 {
2103 	struct map_rb_node *rb_node;
2104 	struct map **maps_by_name = realloc(maps__maps_by_name(maps),
2105 					    maps__nr_maps(maps) * sizeof(struct map *));
2106 	int i = 0;
2107 
2108 	if (maps_by_name == NULL)
2109 		return -1;
2110 
2111 	up_read(maps__lock(maps));
2112 	down_write(maps__lock(maps));
2113 
2114 	RC_CHK_ACCESS(maps)->maps_by_name = maps_by_name;
2115 	RC_CHK_ACCESS(maps)->nr_maps_allocated = maps__nr_maps(maps);
2116 
2117 	maps__for_each_entry(maps, rb_node)
2118 		maps_by_name[i++] = map__get(rb_node->map);
2119 
2120 	__maps__sort_by_name(maps);
2121 
2122 	up_write(maps__lock(maps));
2123 	down_read(maps__lock(maps));
2124 
2125 	return 0;
2126 }
2127 
2128 static struct map *__maps__find_by_name(struct maps *maps, const char *name)
2129 {
2130 	struct map **mapp;
2131 
2132 	if (maps__maps_by_name(maps) == NULL &&
2133 	    map__groups__sort_by_name_from_rbtree(maps))
2134 		return NULL;
2135 
2136 	mapp = bsearch(name, maps__maps_by_name(maps), maps__nr_maps(maps),
2137 		       sizeof(*mapp), map__strcmp_name);
2138 	if (mapp)
2139 		return *mapp;
2140 	return NULL;
2141 }
2142 
2143 struct map *maps__find_by_name(struct maps *maps, const char *name)
2144 {
2145 	struct map_rb_node *rb_node;
2146 	struct map *map;
2147 
2148 	down_read(maps__lock(maps));
2149 
2150 
2151 	if (RC_CHK_ACCESS(maps)->last_search_by_name) {
2152 		const struct dso *dso = map__dso(RC_CHK_ACCESS(maps)->last_search_by_name);
2153 
2154 		if (strcmp(dso->short_name, name) == 0) {
2155 			map = RC_CHK_ACCESS(maps)->last_search_by_name;
2156 			goto out_unlock;
2157 		}
2158 	}
2159 	/*
2160 	 * If we have maps->maps_by_name, then the name isn't in the rbtree,
2161 	 * as maps->maps_by_name mirrors the rbtree when lookups by name are
2162 	 * made.
2163 	 */
2164 	map = __maps__find_by_name(maps, name);
2165 	if (map || maps__maps_by_name(maps) != NULL)
2166 		goto out_unlock;
2167 
2168 	/* Fallback to traversing the rbtree... */
2169 	maps__for_each_entry(maps, rb_node) {
2170 		struct dso *dso;
2171 
2172 		map = rb_node->map;
2173 		dso = map__dso(map);
2174 		if (strcmp(dso->short_name, name) == 0) {
2175 			RC_CHK_ACCESS(maps)->last_search_by_name = map;
2176 			goto out_unlock;
2177 		}
2178 	}
2179 	map = NULL;
2180 
2181 out_unlock:
2182 	up_read(maps__lock(maps));
2183 	return map;
2184 }
2185 
2186 int dso__load_vmlinux(struct dso *dso, struct map *map,
2187 		      const char *vmlinux, bool vmlinux_allocated)
2188 {
2189 	int err = -1;
2190 	struct symsrc ss;
2191 	char symfs_vmlinux[PATH_MAX];
2192 	enum dso_binary_type symtab_type;
2193 
2194 	if (vmlinux[0] == '/')
2195 		snprintf(symfs_vmlinux, sizeof(symfs_vmlinux), "%s", vmlinux);
2196 	else
2197 		symbol__join_symfs(symfs_vmlinux, vmlinux);
2198 
2199 	if (dso->kernel == DSO_SPACE__KERNEL_GUEST)
2200 		symtab_type = DSO_BINARY_TYPE__GUEST_VMLINUX;
2201 	else
2202 		symtab_type = DSO_BINARY_TYPE__VMLINUX;
2203 
2204 	if (symsrc__init(&ss, dso, symfs_vmlinux, symtab_type))
2205 		return -1;
2206 
2207 	/*
2208 	 * dso__load_sym() may copy 'dso' which will result in the copies having
2209 	 * an incorrect long name unless we set it here first.
2210 	 */
2211 	dso__set_long_name(dso, vmlinux, vmlinux_allocated);
2212 	if (dso->kernel == DSO_SPACE__KERNEL_GUEST)
2213 		dso->binary_type = DSO_BINARY_TYPE__GUEST_VMLINUX;
2214 	else
2215 		dso->binary_type = DSO_BINARY_TYPE__VMLINUX;
2216 
2217 	err = dso__load_sym(dso, map, &ss, &ss, 0);
2218 	symsrc__destroy(&ss);
2219 
2220 	if (err > 0) {
2221 		dso__set_loaded(dso);
2222 		pr_debug("Using %s for symbols\n", symfs_vmlinux);
2223 	}
2224 
2225 	return err;
2226 }
2227 
2228 int dso__load_vmlinux_path(struct dso *dso, struct map *map)
2229 {
2230 	int i, err = 0;
2231 	char *filename = NULL;
2232 
2233 	pr_debug("Looking at the vmlinux_path (%d entries long)\n",
2234 		 vmlinux_path__nr_entries + 1);
2235 
2236 	for (i = 0; i < vmlinux_path__nr_entries; ++i) {
2237 		err = dso__load_vmlinux(dso, map, vmlinux_path[i], false);
2238 		if (err > 0)
2239 			goto out;
2240 	}
2241 
2242 	if (!symbol_conf.ignore_vmlinux_buildid)
2243 		filename = dso__build_id_filename(dso, NULL, 0, false);
2244 	if (filename != NULL) {
2245 		err = dso__load_vmlinux(dso, map, filename, true);
2246 		if (err > 0)
2247 			goto out;
2248 		free(filename);
2249 	}
2250 out:
2251 	return err;
2252 }
2253 
2254 static bool visible_dir_filter(const char *name, struct dirent *d)
2255 {
2256 	if (d->d_type != DT_DIR)
2257 		return false;
2258 	return lsdir_no_dot_filter(name, d);
2259 }
2260 
2261 static int find_matching_kcore(struct map *map, char *dir, size_t dir_sz)
2262 {
2263 	char kallsyms_filename[PATH_MAX];
2264 	int ret = -1;
2265 	struct strlist *dirs;
2266 	struct str_node *nd;
2267 
2268 	dirs = lsdir(dir, visible_dir_filter);
2269 	if (!dirs)
2270 		return -1;
2271 
2272 	strlist__for_each_entry(nd, dirs) {
2273 		scnprintf(kallsyms_filename, sizeof(kallsyms_filename),
2274 			  "%s/%s/kallsyms", dir, nd->s);
2275 		if (!validate_kcore_addresses(kallsyms_filename, map)) {
2276 			strlcpy(dir, kallsyms_filename, dir_sz);
2277 			ret = 0;
2278 			break;
2279 		}
2280 	}
2281 
2282 	strlist__delete(dirs);
2283 
2284 	return ret;
2285 }
2286 
2287 /*
2288  * Use open(O_RDONLY) to check readability directly instead of access(R_OK)
2289  * since access(R_OK) only checks with real UID/GID but open() use effective
2290  * UID/GID and actual capabilities (e.g. /proc/kcore requires CAP_SYS_RAWIO).
2291  */
2292 static bool filename__readable(const char *file)
2293 {
2294 	int fd = open(file, O_RDONLY);
2295 	if (fd < 0)
2296 		return false;
2297 	close(fd);
2298 	return true;
2299 }
2300 
2301 static char *dso__find_kallsyms(struct dso *dso, struct map *map)
2302 {
2303 	struct build_id bid;
2304 	char sbuild_id[SBUILD_ID_SIZE];
2305 	bool is_host = false;
2306 	char path[PATH_MAX];
2307 
2308 	if (!dso->has_build_id) {
2309 		/*
2310 		 * Last resort, if we don't have a build-id and couldn't find
2311 		 * any vmlinux file, try the running kernel kallsyms table.
2312 		 */
2313 		goto proc_kallsyms;
2314 	}
2315 
2316 	if (sysfs__read_build_id("/sys/kernel/notes", &bid) == 0)
2317 		is_host = dso__build_id_equal(dso, &bid);
2318 
2319 	/* Try a fast path for /proc/kallsyms if possible */
2320 	if (is_host) {
2321 		/*
2322 		 * Do not check the build-id cache, unless we know we cannot use
2323 		 * /proc/kcore or module maps don't match to /proc/kallsyms.
2324 		 * To check readability of /proc/kcore, do not use access(R_OK)
2325 		 * since /proc/kcore requires CAP_SYS_RAWIO to read and access
2326 		 * can't check it.
2327 		 */
2328 		if (filename__readable("/proc/kcore") &&
2329 		    !validate_kcore_addresses("/proc/kallsyms", map))
2330 			goto proc_kallsyms;
2331 	}
2332 
2333 	build_id__sprintf(&dso->bid, sbuild_id);
2334 
2335 	/* Find kallsyms in build-id cache with kcore */
2336 	scnprintf(path, sizeof(path), "%s/%s/%s",
2337 		  buildid_dir, DSO__NAME_KCORE, sbuild_id);
2338 
2339 	if (!find_matching_kcore(map, path, sizeof(path)))
2340 		return strdup(path);
2341 
2342 	/* Use current /proc/kallsyms if possible */
2343 	if (is_host) {
2344 proc_kallsyms:
2345 		return strdup("/proc/kallsyms");
2346 	}
2347 
2348 	/* Finally, find a cache of kallsyms */
2349 	if (!build_id_cache__kallsyms_path(sbuild_id, path, sizeof(path))) {
2350 		pr_err("No kallsyms or vmlinux with build-id %s was found\n",
2351 		       sbuild_id);
2352 		return NULL;
2353 	}
2354 
2355 	return strdup(path);
2356 }
2357 
2358 static int dso__load_kernel_sym(struct dso *dso, struct map *map)
2359 {
2360 	int err;
2361 	const char *kallsyms_filename = NULL;
2362 	char *kallsyms_allocated_filename = NULL;
2363 	char *filename = NULL;
2364 
2365 	/*
2366 	 * Step 1: if the user specified a kallsyms or vmlinux filename, use
2367 	 * it and only it, reporting errors to the user if it cannot be used.
2368 	 *
2369 	 * For instance, try to analyse an ARM perf.data file _without_ a
2370 	 * build-id, or if the user specifies the wrong path to the right
2371 	 * vmlinux file, obviously we can't fallback to another vmlinux (a
2372 	 * x86_86 one, on the machine where analysis is being performed, say),
2373 	 * or worse, /proc/kallsyms.
2374 	 *
2375 	 * If the specified file _has_ a build-id and there is a build-id
2376 	 * section in the perf.data file, we will still do the expected
2377 	 * validation in dso__load_vmlinux and will bail out if they don't
2378 	 * match.
2379 	 */
2380 	if (symbol_conf.kallsyms_name != NULL) {
2381 		kallsyms_filename = symbol_conf.kallsyms_name;
2382 		goto do_kallsyms;
2383 	}
2384 
2385 	if (!symbol_conf.ignore_vmlinux && symbol_conf.vmlinux_name != NULL) {
2386 		return dso__load_vmlinux(dso, map, symbol_conf.vmlinux_name, false);
2387 	}
2388 
2389 	/*
2390 	 * Before checking on common vmlinux locations, check if it's
2391 	 * stored as standard build id binary (not kallsyms) under
2392 	 * .debug cache.
2393 	 */
2394 	if (!symbol_conf.ignore_vmlinux_buildid)
2395 		filename = __dso__build_id_filename(dso, NULL, 0, false, false);
2396 	if (filename != NULL) {
2397 		err = dso__load_vmlinux(dso, map, filename, true);
2398 		if (err > 0)
2399 			return err;
2400 		free(filename);
2401 	}
2402 
2403 	if (!symbol_conf.ignore_vmlinux && vmlinux_path != NULL) {
2404 		err = dso__load_vmlinux_path(dso, map);
2405 		if (err > 0)
2406 			return err;
2407 	}
2408 
2409 	/* do not try local files if a symfs was given */
2410 	if (symbol_conf.symfs[0] != 0)
2411 		return -1;
2412 
2413 	kallsyms_allocated_filename = dso__find_kallsyms(dso, map);
2414 	if (!kallsyms_allocated_filename)
2415 		return -1;
2416 
2417 	kallsyms_filename = kallsyms_allocated_filename;
2418 
2419 do_kallsyms:
2420 	err = dso__load_kallsyms(dso, kallsyms_filename, map);
2421 	if (err > 0)
2422 		pr_debug("Using %s for symbols\n", kallsyms_filename);
2423 	free(kallsyms_allocated_filename);
2424 
2425 	if (err > 0 && !dso__is_kcore(dso)) {
2426 		dso->binary_type = DSO_BINARY_TYPE__KALLSYMS;
2427 		dso__set_long_name(dso, DSO__NAME_KALLSYMS, false);
2428 		map__fixup_start(map);
2429 		map__fixup_end(map);
2430 	}
2431 
2432 	return err;
2433 }
2434 
2435 static int dso__load_guest_kernel_sym(struct dso *dso, struct map *map)
2436 {
2437 	int err;
2438 	const char *kallsyms_filename;
2439 	struct machine *machine = maps__machine(map__kmaps(map));
2440 	char path[PATH_MAX];
2441 
2442 	if (machine->kallsyms_filename) {
2443 		kallsyms_filename = machine->kallsyms_filename;
2444 	} else if (machine__is_default_guest(machine)) {
2445 		/*
2446 		 * if the user specified a vmlinux filename, use it and only
2447 		 * it, reporting errors to the user if it cannot be used.
2448 		 * Or use file guest_kallsyms inputted by user on commandline
2449 		 */
2450 		if (symbol_conf.default_guest_vmlinux_name != NULL) {
2451 			err = dso__load_vmlinux(dso, map,
2452 						symbol_conf.default_guest_vmlinux_name,
2453 						false);
2454 			return err;
2455 		}
2456 
2457 		kallsyms_filename = symbol_conf.default_guest_kallsyms;
2458 		if (!kallsyms_filename)
2459 			return -1;
2460 	} else {
2461 		sprintf(path, "%s/proc/kallsyms", machine->root_dir);
2462 		kallsyms_filename = path;
2463 	}
2464 
2465 	err = dso__load_kallsyms(dso, kallsyms_filename, map);
2466 	if (err > 0)
2467 		pr_debug("Using %s for symbols\n", kallsyms_filename);
2468 	if (err > 0 && !dso__is_kcore(dso)) {
2469 		dso->binary_type = DSO_BINARY_TYPE__GUEST_KALLSYMS;
2470 		dso__set_long_name(dso, machine->mmap_name, false);
2471 		map__fixup_start(map);
2472 		map__fixup_end(map);
2473 	}
2474 
2475 	return err;
2476 }
2477 
2478 static void vmlinux_path__exit(void)
2479 {
2480 	while (--vmlinux_path__nr_entries >= 0)
2481 		zfree(&vmlinux_path[vmlinux_path__nr_entries]);
2482 	vmlinux_path__nr_entries = 0;
2483 
2484 	zfree(&vmlinux_path);
2485 }
2486 
2487 static const char * const vmlinux_paths[] = {
2488 	"vmlinux",
2489 	"/boot/vmlinux"
2490 };
2491 
2492 static const char * const vmlinux_paths_upd[] = {
2493 	"/boot/vmlinux-%s",
2494 	"/usr/lib/debug/boot/vmlinux-%s",
2495 	"/lib/modules/%s/build/vmlinux",
2496 	"/usr/lib/debug/lib/modules/%s/vmlinux",
2497 	"/usr/lib/debug/boot/vmlinux-%s.debug"
2498 };
2499 
2500 static int vmlinux_path__add(const char *new_entry)
2501 {
2502 	vmlinux_path[vmlinux_path__nr_entries] = strdup(new_entry);
2503 	if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
2504 		return -1;
2505 	++vmlinux_path__nr_entries;
2506 
2507 	return 0;
2508 }
2509 
2510 static int vmlinux_path__init(struct perf_env *env)
2511 {
2512 	struct utsname uts;
2513 	char bf[PATH_MAX];
2514 	char *kernel_version;
2515 	unsigned int i;
2516 
2517 	vmlinux_path = malloc(sizeof(char *) * (ARRAY_SIZE(vmlinux_paths) +
2518 			      ARRAY_SIZE(vmlinux_paths_upd)));
2519 	if (vmlinux_path == NULL)
2520 		return -1;
2521 
2522 	for (i = 0; i < ARRAY_SIZE(vmlinux_paths); i++)
2523 		if (vmlinux_path__add(vmlinux_paths[i]) < 0)
2524 			goto out_fail;
2525 
2526 	/* only try kernel version if no symfs was given */
2527 	if (symbol_conf.symfs[0] != 0)
2528 		return 0;
2529 
2530 	if (env) {
2531 		kernel_version = env->os_release;
2532 	} else {
2533 		if (uname(&uts) < 0)
2534 			goto out_fail;
2535 
2536 		kernel_version = uts.release;
2537 	}
2538 
2539 	for (i = 0; i < ARRAY_SIZE(vmlinux_paths_upd); i++) {
2540 		snprintf(bf, sizeof(bf), vmlinux_paths_upd[i], kernel_version);
2541 		if (vmlinux_path__add(bf) < 0)
2542 			goto out_fail;
2543 	}
2544 
2545 	return 0;
2546 
2547 out_fail:
2548 	vmlinux_path__exit();
2549 	return -1;
2550 }
2551 
2552 int setup_list(struct strlist **list, const char *list_str,
2553 		      const char *list_name)
2554 {
2555 	if (list_str == NULL)
2556 		return 0;
2557 
2558 	*list = strlist__new(list_str, NULL);
2559 	if (!*list) {
2560 		pr_err("problems parsing %s list\n", list_name);
2561 		return -1;
2562 	}
2563 
2564 	symbol_conf.has_filter = true;
2565 	return 0;
2566 }
2567 
2568 int setup_intlist(struct intlist **list, const char *list_str,
2569 		  const char *list_name)
2570 {
2571 	if (list_str == NULL)
2572 		return 0;
2573 
2574 	*list = intlist__new(list_str);
2575 	if (!*list) {
2576 		pr_err("problems parsing %s list\n", list_name);
2577 		return -1;
2578 	}
2579 	return 0;
2580 }
2581 
2582 static int setup_addrlist(struct intlist **addr_list, struct strlist *sym_list)
2583 {
2584 	struct str_node *pos, *tmp;
2585 	unsigned long val;
2586 	char *sep;
2587 	const char *end;
2588 	int i = 0, err;
2589 
2590 	*addr_list = intlist__new(NULL);
2591 	if (!*addr_list)
2592 		return -1;
2593 
2594 	strlist__for_each_entry_safe(pos, tmp, sym_list) {
2595 		errno = 0;
2596 		val = strtoul(pos->s, &sep, 16);
2597 		if (errno || (sep == pos->s))
2598 			continue;
2599 
2600 		if (*sep != '\0') {
2601 			end = pos->s + strlen(pos->s) - 1;
2602 			while (end >= sep && isspace(*end))
2603 				end--;
2604 
2605 			if (end >= sep)
2606 				continue;
2607 		}
2608 
2609 		err = intlist__add(*addr_list, val);
2610 		if (err)
2611 			break;
2612 
2613 		strlist__remove(sym_list, pos);
2614 		i++;
2615 	}
2616 
2617 	if (i == 0) {
2618 		intlist__delete(*addr_list);
2619 		*addr_list = NULL;
2620 	}
2621 
2622 	return 0;
2623 }
2624 
2625 static bool symbol__read_kptr_restrict(void)
2626 {
2627 	bool value = false;
2628 	FILE *fp = fopen("/proc/sys/kernel/kptr_restrict", "r");
2629 
2630 	if (fp != NULL) {
2631 		char line[8];
2632 
2633 		if (fgets(line, sizeof(line), fp) != NULL)
2634 			value = perf_cap__capable(CAP_SYSLOG) ?
2635 					(atoi(line) >= 2) :
2636 					(atoi(line) != 0);
2637 
2638 		fclose(fp);
2639 	}
2640 
2641 	/* Per kernel/kallsyms.c:
2642 	 * we also restrict when perf_event_paranoid > 1 w/o CAP_SYSLOG
2643 	 */
2644 	if (perf_event_paranoid() > 1 && !perf_cap__capable(CAP_SYSLOG))
2645 		value = true;
2646 
2647 	return value;
2648 }
2649 
2650 int symbol__annotation_init(void)
2651 {
2652 	if (symbol_conf.init_annotation)
2653 		return 0;
2654 
2655 	if (symbol_conf.initialized) {
2656 		pr_err("Annotation needs to be init before symbol__init()\n");
2657 		return -1;
2658 	}
2659 
2660 	symbol_conf.priv_size += sizeof(struct annotation);
2661 	symbol_conf.init_annotation = true;
2662 	return 0;
2663 }
2664 
2665 int symbol__init(struct perf_env *env)
2666 {
2667 	const char *symfs;
2668 
2669 	if (symbol_conf.initialized)
2670 		return 0;
2671 
2672 	symbol_conf.priv_size = PERF_ALIGN(symbol_conf.priv_size, sizeof(u64));
2673 
2674 	symbol__elf_init();
2675 
2676 	if (symbol_conf.try_vmlinux_path && vmlinux_path__init(env) < 0)
2677 		return -1;
2678 
2679 	if (symbol_conf.field_sep && *symbol_conf.field_sep == '.') {
2680 		pr_err("'.' is the only non valid --field-separator argument\n");
2681 		return -1;
2682 	}
2683 
2684 	if (setup_list(&symbol_conf.dso_list,
2685 		       symbol_conf.dso_list_str, "dso") < 0)
2686 		return -1;
2687 
2688 	if (setup_list(&symbol_conf.comm_list,
2689 		       symbol_conf.comm_list_str, "comm") < 0)
2690 		goto out_free_dso_list;
2691 
2692 	if (setup_intlist(&symbol_conf.pid_list,
2693 		       symbol_conf.pid_list_str, "pid") < 0)
2694 		goto out_free_comm_list;
2695 
2696 	if (setup_intlist(&symbol_conf.tid_list,
2697 		       symbol_conf.tid_list_str, "tid") < 0)
2698 		goto out_free_pid_list;
2699 
2700 	if (setup_list(&symbol_conf.sym_list,
2701 		       symbol_conf.sym_list_str, "symbol") < 0)
2702 		goto out_free_tid_list;
2703 
2704 	if (symbol_conf.sym_list &&
2705 	    setup_addrlist(&symbol_conf.addr_list, symbol_conf.sym_list) < 0)
2706 		goto out_free_sym_list;
2707 
2708 	if (setup_list(&symbol_conf.bt_stop_list,
2709 		       symbol_conf.bt_stop_list_str, "symbol") < 0)
2710 		goto out_free_sym_list;
2711 
2712 	/*
2713 	 * A path to symbols of "/" is identical to ""
2714 	 * reset here for simplicity.
2715 	 */
2716 	symfs = realpath(symbol_conf.symfs, NULL);
2717 	if (symfs == NULL)
2718 		symfs = symbol_conf.symfs;
2719 	if (strcmp(symfs, "/") == 0)
2720 		symbol_conf.symfs = "";
2721 	if (symfs != symbol_conf.symfs)
2722 		free((void *)symfs);
2723 
2724 	symbol_conf.kptr_restrict = symbol__read_kptr_restrict();
2725 
2726 	symbol_conf.initialized = true;
2727 	return 0;
2728 
2729 out_free_sym_list:
2730 	strlist__delete(symbol_conf.sym_list);
2731 	intlist__delete(symbol_conf.addr_list);
2732 out_free_tid_list:
2733 	intlist__delete(symbol_conf.tid_list);
2734 out_free_pid_list:
2735 	intlist__delete(symbol_conf.pid_list);
2736 out_free_comm_list:
2737 	strlist__delete(symbol_conf.comm_list);
2738 out_free_dso_list:
2739 	strlist__delete(symbol_conf.dso_list);
2740 	return -1;
2741 }
2742 
2743 void symbol__exit(void)
2744 {
2745 	if (!symbol_conf.initialized)
2746 		return;
2747 	strlist__delete(symbol_conf.bt_stop_list);
2748 	strlist__delete(symbol_conf.sym_list);
2749 	strlist__delete(symbol_conf.dso_list);
2750 	strlist__delete(symbol_conf.comm_list);
2751 	intlist__delete(symbol_conf.tid_list);
2752 	intlist__delete(symbol_conf.pid_list);
2753 	intlist__delete(symbol_conf.addr_list);
2754 	vmlinux_path__exit();
2755 	symbol_conf.sym_list = symbol_conf.dso_list = symbol_conf.comm_list = NULL;
2756 	symbol_conf.bt_stop_list = NULL;
2757 	symbol_conf.initialized = false;
2758 }
2759 
2760 int symbol__config_symfs(const struct option *opt __maybe_unused,
2761 			 const char *dir, int unset __maybe_unused)
2762 {
2763 	char *bf = NULL;
2764 	int ret;
2765 
2766 	symbol_conf.symfs = strdup(dir);
2767 	if (symbol_conf.symfs == NULL)
2768 		return -ENOMEM;
2769 
2770 	/* skip the locally configured cache if a symfs is given, and
2771 	 * config buildid dir to symfs/.debug
2772 	 */
2773 	ret = asprintf(&bf, "%s/%s", dir, ".debug");
2774 	if (ret < 0)
2775 		return -ENOMEM;
2776 
2777 	set_buildid_dir(bf);
2778 
2779 	free(bf);
2780 	return 0;
2781 }
2782 
2783 struct mem_info *mem_info__get(struct mem_info *mi)
2784 {
2785 	if (mi)
2786 		refcount_inc(&mi->refcnt);
2787 	return mi;
2788 }
2789 
2790 void mem_info__put(struct mem_info *mi)
2791 {
2792 	if (mi && refcount_dec_and_test(&mi->refcnt))
2793 		free(mi);
2794 }
2795 
2796 struct mem_info *mem_info__new(void)
2797 {
2798 	struct mem_info *mi = zalloc(sizeof(*mi));
2799 
2800 	if (mi)
2801 		refcount_set(&mi->refcnt, 1);
2802 	return mi;
2803 }
2804 
2805 /*
2806  * Checks that user supplied symbol kernel files are accessible because
2807  * the default mechanism for accessing elf files fails silently. i.e. if
2808  * debug syms for a build ID aren't found perf carries on normally. When
2809  * they are user supplied we should assume that the user doesn't want to
2810  * silently fail.
2811  */
2812 int symbol__validate_sym_arguments(void)
2813 {
2814 	if (symbol_conf.vmlinux_name &&
2815 	    access(symbol_conf.vmlinux_name, R_OK)) {
2816 		pr_err("Invalid file: %s\n", symbol_conf.vmlinux_name);
2817 		return -EINVAL;
2818 	}
2819 	if (symbol_conf.kallsyms_name &&
2820 	    access(symbol_conf.kallsyms_name, R_OK)) {
2821 		pr_err("Invalid file: %s\n", symbol_conf.kallsyms_name);
2822 		return -EINVAL;
2823 	}
2824 	return 0;
2825 }
2826