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