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