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