xref: /openbmc/linux/tools/perf/util/symbol.c (revision 1c2dd16a)
1 #include <dirent.h>
2 #include <errno.h>
3 #include <stdlib.h>
4 #include <stdio.h>
5 #include <string.h>
6 #include <linux/kernel.h>
7 #include <sys/types.h>
8 #include <sys/stat.h>
9 #include <sys/param.h>
10 #include <fcntl.h>
11 #include <unistd.h>
12 #include <inttypes.h>
13 #include "annotate.h"
14 #include "build-id.h"
15 #include "util.h"
16 #include "debug.h"
17 #include "machine.h"
18 #include "symbol.h"
19 #include "strlist.h"
20 #include "intlist.h"
21 #include "header.h"
22 #include "path.h"
23 #include "sane_ctype.h"
24 
25 #include <elf.h>
26 #include <limits.h>
27 #include <symbol/kallsyms.h>
28 #include <sys/utsname.h>
29 
30 static int dso__load_kernel_sym(struct dso *dso, struct map *map);
31 static int dso__load_guest_kernel_sym(struct dso *dso, struct map *map);
32 static bool symbol__is_idle(const char *name);
33 
34 int vmlinux_path__nr_entries;
35 char **vmlinux_path;
36 
37 struct symbol_conf symbol_conf = {
38 	.use_modules		= true,
39 	.try_vmlinux_path	= true,
40 	.annotate_src		= true,
41 	.demangle		= true,
42 	.demangle_kernel	= false,
43 	.cumulate_callchain	= true,
44 	.show_hist_headers	= true,
45 	.symfs			= "",
46 	.event_group		= true,
47 };
48 
49 static enum dso_binary_type binary_type_symtab[] = {
50 	DSO_BINARY_TYPE__KALLSYMS,
51 	DSO_BINARY_TYPE__GUEST_KALLSYMS,
52 	DSO_BINARY_TYPE__JAVA_JIT,
53 	DSO_BINARY_TYPE__DEBUGLINK,
54 	DSO_BINARY_TYPE__BUILD_ID_CACHE,
55 	DSO_BINARY_TYPE__FEDORA_DEBUGINFO,
56 	DSO_BINARY_TYPE__UBUNTU_DEBUGINFO,
57 	DSO_BINARY_TYPE__BUILDID_DEBUGINFO,
58 	DSO_BINARY_TYPE__SYSTEM_PATH_DSO,
59 	DSO_BINARY_TYPE__GUEST_KMODULE,
60 	DSO_BINARY_TYPE__GUEST_KMODULE_COMP,
61 	DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE,
62 	DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP,
63 	DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO,
64 	DSO_BINARY_TYPE__NOT_FOUND,
65 };
66 
67 #define DSO_BINARY_TYPE__SYMTAB_CNT ARRAY_SIZE(binary_type_symtab)
68 
69 bool symbol_type__is_a(char symbol_type, enum map_type map_type)
70 {
71 	symbol_type = toupper(symbol_type);
72 
73 	switch (map_type) {
74 	case MAP__FUNCTION:
75 		return symbol_type == 'T' || symbol_type == 'W';
76 	case MAP__VARIABLE:
77 		return symbol_type == 'D';
78 	default:
79 		return false;
80 	}
81 }
82 
83 static int prefix_underscores_count(const char *str)
84 {
85 	const char *tail = str;
86 
87 	while (*tail == '_')
88 		tail++;
89 
90 	return tail - str;
91 }
92 
93 int __weak arch__choose_best_symbol(struct symbol *syma,
94 				    struct symbol *symb __maybe_unused)
95 {
96 	/* Avoid "SyS" kernel syscall aliases */
97 	if (strlen(syma->name) >= 3 && !strncmp(syma->name, "SyS", 3))
98 		return SYMBOL_B;
99 	if (strlen(syma->name) >= 10 && !strncmp(syma->name, "compat_SyS", 10))
100 		return SYMBOL_B;
101 
102 	return SYMBOL_A;
103 }
104 
105 static int choose_best_symbol(struct symbol *syma, struct symbol *symb)
106 {
107 	s64 a;
108 	s64 b;
109 	size_t na, nb;
110 
111 	/* Prefer a symbol with non zero length */
112 	a = syma->end - syma->start;
113 	b = symb->end - symb->start;
114 	if ((b == 0) && (a > 0))
115 		return SYMBOL_A;
116 	else if ((a == 0) && (b > 0))
117 		return SYMBOL_B;
118 
119 	/* Prefer a non weak symbol over a weak one */
120 	a = syma->binding == STB_WEAK;
121 	b = symb->binding == STB_WEAK;
122 	if (b && !a)
123 		return SYMBOL_A;
124 	if (a && !b)
125 		return SYMBOL_B;
126 
127 	/* Prefer a global symbol over a non global one */
128 	a = syma->binding == STB_GLOBAL;
129 	b = symb->binding == STB_GLOBAL;
130 	if (a && !b)
131 		return SYMBOL_A;
132 	if (b && !a)
133 		return SYMBOL_B;
134 
135 	/* Prefer a symbol with less underscores */
136 	a = prefix_underscores_count(syma->name);
137 	b = prefix_underscores_count(symb->name);
138 	if (b > a)
139 		return SYMBOL_A;
140 	else if (a > b)
141 		return SYMBOL_B;
142 
143 	/* Choose the symbol with the longest name */
144 	na = strlen(syma->name);
145 	nb = strlen(symb->name);
146 	if (na > nb)
147 		return SYMBOL_A;
148 	else if (na < nb)
149 		return SYMBOL_B;
150 
151 	return arch__choose_best_symbol(syma, symb);
152 }
153 
154 void symbols__fixup_duplicate(struct rb_root *symbols)
155 {
156 	struct rb_node *nd;
157 	struct symbol *curr, *next;
158 
159 	if (symbol_conf.allow_aliases)
160 		return;
161 
162 	nd = rb_first(symbols);
163 
164 	while (nd) {
165 		curr = rb_entry(nd, struct symbol, rb_node);
166 again:
167 		nd = rb_next(&curr->rb_node);
168 		next = rb_entry(nd, struct symbol, rb_node);
169 
170 		if (!nd)
171 			break;
172 
173 		if (curr->start != next->start)
174 			continue;
175 
176 		if (choose_best_symbol(curr, next) == SYMBOL_A) {
177 			rb_erase(&next->rb_node, symbols);
178 			symbol__delete(next);
179 			goto again;
180 		} else {
181 			nd = rb_next(&curr->rb_node);
182 			rb_erase(&curr->rb_node, symbols);
183 			symbol__delete(curr);
184 		}
185 	}
186 }
187 
188 void symbols__fixup_end(struct rb_root *symbols)
189 {
190 	struct rb_node *nd, *prevnd = rb_first(symbols);
191 	struct symbol *curr, *prev;
192 
193 	if (prevnd == NULL)
194 		return;
195 
196 	curr = rb_entry(prevnd, struct symbol, rb_node);
197 
198 	for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
199 		prev = curr;
200 		curr = rb_entry(nd, struct symbol, rb_node);
201 
202 		if (prev->end == prev->start && prev->end != curr->start)
203 			prev->end = curr->start;
204 	}
205 
206 	/* Last entry */
207 	if (curr->end == curr->start)
208 		curr->end = roundup(curr->start, 4096) + 4096;
209 }
210 
211 void __map_groups__fixup_end(struct map_groups *mg, enum map_type type)
212 {
213 	struct maps *maps = &mg->maps[type];
214 	struct map *next, *curr;
215 
216 	pthread_rwlock_wrlock(&maps->lock);
217 
218 	curr = maps__first(maps);
219 	if (curr == NULL)
220 		goto out_unlock;
221 
222 	for (next = map__next(curr); next; next = map__next(curr)) {
223 		curr->end = next->start;
224 		curr = next;
225 	}
226 
227 	/*
228 	 * We still haven't the actual symbols, so guess the
229 	 * last map final address.
230 	 */
231 	curr->end = ~0ULL;
232 
233 out_unlock:
234 	pthread_rwlock_unlock(&maps->lock);
235 }
236 
237 struct symbol *symbol__new(u64 start, u64 len, u8 binding, const char *name)
238 {
239 	size_t namelen = strlen(name) + 1;
240 	struct symbol *sym = calloc(1, (symbol_conf.priv_size +
241 					sizeof(*sym) + namelen));
242 	if (sym == NULL)
243 		return NULL;
244 
245 	if (symbol_conf.priv_size) {
246 		if (symbol_conf.init_annotation) {
247 			struct annotation *notes = (void *)sym;
248 			pthread_mutex_init(&notes->lock, NULL);
249 		}
250 		sym = ((void *)sym) + symbol_conf.priv_size;
251 	}
252 
253 	sym->start   = start;
254 	sym->end     = len ? start + len : start;
255 	sym->binding = binding;
256 	sym->namelen = namelen - 1;
257 
258 	pr_debug4("%s: %s %#" PRIx64 "-%#" PRIx64 "\n",
259 		  __func__, name, start, sym->end);
260 	memcpy(sym->name, name, namelen);
261 
262 	return sym;
263 }
264 
265 void symbol__delete(struct symbol *sym)
266 {
267 	free(((void *)sym) - symbol_conf.priv_size);
268 }
269 
270 void symbols__delete(struct rb_root *symbols)
271 {
272 	struct symbol *pos;
273 	struct rb_node *next = rb_first(symbols);
274 
275 	while (next) {
276 		pos = rb_entry(next, struct symbol, rb_node);
277 		next = rb_next(&pos->rb_node);
278 		rb_erase(&pos->rb_node, symbols);
279 		symbol__delete(pos);
280 	}
281 }
282 
283 void __symbols__insert(struct rb_root *symbols, struct symbol *sym, bool kernel)
284 {
285 	struct rb_node **p = &symbols->rb_node;
286 	struct rb_node *parent = NULL;
287 	const u64 ip = sym->start;
288 	struct symbol *s;
289 
290 	if (kernel) {
291 		const char *name = sym->name;
292 		/*
293 		 * ppc64 uses function descriptors and appends a '.' to the
294 		 * start of every instruction address. Remove it.
295 		 */
296 		if (name[0] == '.')
297 			name++;
298 		sym->idle = symbol__is_idle(name);
299 	}
300 
301 	while (*p != NULL) {
302 		parent = *p;
303 		s = rb_entry(parent, struct symbol, rb_node);
304 		if (ip < s->start)
305 			p = &(*p)->rb_left;
306 		else
307 			p = &(*p)->rb_right;
308 	}
309 	rb_link_node(&sym->rb_node, parent, p);
310 	rb_insert_color(&sym->rb_node, symbols);
311 }
312 
313 void symbols__insert(struct rb_root *symbols, struct symbol *sym)
314 {
315 	__symbols__insert(symbols, sym, false);
316 }
317 
318 static struct symbol *symbols__find(struct rb_root *symbols, u64 ip)
319 {
320 	struct rb_node *n;
321 
322 	if (symbols == NULL)
323 		return NULL;
324 
325 	n = symbols->rb_node;
326 
327 	while (n) {
328 		struct symbol *s = rb_entry(n, struct symbol, rb_node);
329 
330 		if (ip < s->start)
331 			n = n->rb_left;
332 		else if (ip > s->end || (ip == s->end && ip != s->start))
333 			n = n->rb_right;
334 		else
335 			return s;
336 	}
337 
338 	return NULL;
339 }
340 
341 static struct symbol *symbols__first(struct rb_root *symbols)
342 {
343 	struct rb_node *n = rb_first(symbols);
344 
345 	if (n)
346 		return rb_entry(n, struct symbol, rb_node);
347 
348 	return NULL;
349 }
350 
351 static struct symbol *symbols__last(struct rb_root *symbols)
352 {
353 	struct rb_node *n = rb_last(symbols);
354 
355 	if (n)
356 		return rb_entry(n, struct symbol, rb_node);
357 
358 	return NULL;
359 }
360 
361 static struct symbol *symbols__next(struct symbol *sym)
362 {
363 	struct rb_node *n = rb_next(&sym->rb_node);
364 
365 	if (n)
366 		return rb_entry(n, struct symbol, rb_node);
367 
368 	return NULL;
369 }
370 
371 static void symbols__insert_by_name(struct rb_root *symbols, struct symbol *sym)
372 {
373 	struct rb_node **p = &symbols->rb_node;
374 	struct rb_node *parent = NULL;
375 	struct symbol_name_rb_node *symn, *s;
376 
377 	symn = container_of(sym, struct symbol_name_rb_node, sym);
378 
379 	while (*p != NULL) {
380 		parent = *p;
381 		s = rb_entry(parent, struct symbol_name_rb_node, rb_node);
382 		if (strcmp(sym->name, s->sym.name) < 0)
383 			p = &(*p)->rb_left;
384 		else
385 			p = &(*p)->rb_right;
386 	}
387 	rb_link_node(&symn->rb_node, parent, p);
388 	rb_insert_color(&symn->rb_node, symbols);
389 }
390 
391 static void symbols__sort_by_name(struct rb_root *symbols,
392 				  struct rb_root *source)
393 {
394 	struct rb_node *nd;
395 
396 	for (nd = rb_first(source); nd; nd = rb_next(nd)) {
397 		struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
398 		symbols__insert_by_name(symbols, pos);
399 	}
400 }
401 
402 static struct symbol *symbols__find_by_name(struct rb_root *symbols,
403 					    const char *name)
404 {
405 	struct rb_node *n;
406 	struct symbol_name_rb_node *s = NULL;
407 
408 	if (symbols == NULL)
409 		return NULL;
410 
411 	n = symbols->rb_node;
412 
413 	while (n) {
414 		int cmp;
415 
416 		s = rb_entry(n, struct symbol_name_rb_node, rb_node);
417 		cmp = arch__compare_symbol_names(name, s->sym.name);
418 
419 		if (cmp < 0)
420 			n = n->rb_left;
421 		else if (cmp > 0)
422 			n = n->rb_right;
423 		else
424 			break;
425 	}
426 
427 	if (n == NULL)
428 		return NULL;
429 
430 	/* return first symbol that has same name (if any) */
431 	for (n = rb_prev(n); n; n = rb_prev(n)) {
432 		struct symbol_name_rb_node *tmp;
433 
434 		tmp = rb_entry(n, struct symbol_name_rb_node, rb_node);
435 		if (arch__compare_symbol_names(tmp->sym.name, s->sym.name))
436 			break;
437 
438 		s = tmp;
439 	}
440 
441 	return &s->sym;
442 }
443 
444 void dso__reset_find_symbol_cache(struct dso *dso)
445 {
446 	enum map_type type;
447 
448 	for (type = MAP__FUNCTION; type <= MAP__VARIABLE; ++type) {
449 		dso->last_find_result[type].addr   = 0;
450 		dso->last_find_result[type].symbol = NULL;
451 	}
452 }
453 
454 void dso__insert_symbol(struct dso *dso, enum map_type type, struct symbol *sym)
455 {
456 	__symbols__insert(&dso->symbols[type], sym, dso->kernel);
457 
458 	/* update the symbol cache if necessary */
459 	if (dso->last_find_result[type].addr >= sym->start &&
460 	    (dso->last_find_result[type].addr < sym->end ||
461 	    sym->start == sym->end)) {
462 		dso->last_find_result[type].symbol = sym;
463 	}
464 }
465 
466 struct symbol *dso__find_symbol(struct dso *dso,
467 				enum map_type type, u64 addr)
468 {
469 	if (dso->last_find_result[type].addr != addr) {
470 		dso->last_find_result[type].addr   = addr;
471 		dso->last_find_result[type].symbol = symbols__find(&dso->symbols[type], addr);
472 	}
473 
474 	return dso->last_find_result[type].symbol;
475 }
476 
477 struct symbol *dso__first_symbol(struct dso *dso, enum map_type type)
478 {
479 	return symbols__first(&dso->symbols[type]);
480 }
481 
482 struct symbol *dso__last_symbol(struct dso *dso, enum map_type type)
483 {
484 	return symbols__last(&dso->symbols[type]);
485 }
486 
487 struct symbol *dso__next_symbol(struct symbol *sym)
488 {
489 	return symbols__next(sym);
490 }
491 
492 struct symbol *symbol__next_by_name(struct symbol *sym)
493 {
494 	struct symbol_name_rb_node *s = container_of(sym, struct symbol_name_rb_node, sym);
495 	struct rb_node *n = rb_next(&s->rb_node);
496 
497 	return n ? &rb_entry(n, struct symbol_name_rb_node, rb_node)->sym : NULL;
498 }
499 
500  /*
501   * Teturns first symbol that matched with @name.
502   */
503 struct symbol *dso__find_symbol_by_name(struct dso *dso, enum map_type type,
504 					const char *name)
505 {
506 	return symbols__find_by_name(&dso->symbol_names[type], name);
507 }
508 
509 void dso__sort_by_name(struct dso *dso, enum map_type type)
510 {
511 	dso__set_sorted_by_name(dso, type);
512 	return symbols__sort_by_name(&dso->symbol_names[type],
513 				     &dso->symbols[type]);
514 }
515 
516 int modules__parse(const char *filename, void *arg,
517 		   int (*process_module)(void *arg, const char *name,
518 					 u64 start))
519 {
520 	char *line = NULL;
521 	size_t n;
522 	FILE *file;
523 	int err = 0;
524 
525 	file = fopen(filename, "r");
526 	if (file == NULL)
527 		return -1;
528 
529 	while (1) {
530 		char name[PATH_MAX];
531 		u64 start;
532 		char *sep;
533 		ssize_t line_len;
534 
535 		line_len = getline(&line, &n, file);
536 		if (line_len < 0) {
537 			if (feof(file))
538 				break;
539 			err = -1;
540 			goto out;
541 		}
542 
543 		if (!line) {
544 			err = -1;
545 			goto out;
546 		}
547 
548 		line[--line_len] = '\0'; /* \n */
549 
550 		sep = strrchr(line, 'x');
551 		if (sep == NULL)
552 			continue;
553 
554 		hex2u64(sep + 1, &start);
555 
556 		sep = strchr(line, ' ');
557 		if (sep == NULL)
558 			continue;
559 
560 		*sep = '\0';
561 
562 		scnprintf(name, sizeof(name), "[%s]", line);
563 
564 		err = process_module(arg, name, start);
565 		if (err)
566 			break;
567 	}
568 out:
569 	free(line);
570 	fclose(file);
571 	return err;
572 }
573 
574 struct process_kallsyms_args {
575 	struct map *map;
576 	struct dso *dso;
577 };
578 
579 /*
580  * These are symbols in the kernel image, so make sure that
581  * sym is from a kernel DSO.
582  */
583 static bool symbol__is_idle(const char *name)
584 {
585 	const char * const idle_symbols[] = {
586 		"cpu_idle",
587 		"cpu_startup_entry",
588 		"intel_idle",
589 		"default_idle",
590 		"native_safe_halt",
591 		"enter_idle",
592 		"exit_idle",
593 		"mwait_idle",
594 		"mwait_idle_with_hints",
595 		"poll_idle",
596 		"ppc64_runlatch_off",
597 		"pseries_dedicated_idle_sleep",
598 		NULL
599 	};
600 	int i;
601 
602 	for (i = 0; idle_symbols[i]; i++) {
603 		if (!strcmp(idle_symbols[i], name))
604 			return true;
605 	}
606 
607 	return false;
608 }
609 
610 static int map__process_kallsym_symbol(void *arg, const char *name,
611 				       char type, u64 start)
612 {
613 	struct symbol *sym;
614 	struct process_kallsyms_args *a = arg;
615 	struct rb_root *root = &a->dso->symbols[a->map->type];
616 
617 	if (!symbol_type__is_a(type, a->map->type))
618 		return 0;
619 
620 	/*
621 	 * module symbols are not sorted so we add all
622 	 * symbols, setting length to 0, and rely on
623 	 * symbols__fixup_end() to fix it up.
624 	 */
625 	sym = symbol__new(start, 0, kallsyms2elf_binding(type), name);
626 	if (sym == NULL)
627 		return -ENOMEM;
628 	/*
629 	 * We will pass the symbols to the filter later, in
630 	 * map__split_kallsyms, when we have split the maps per module
631 	 */
632 	__symbols__insert(root, sym, !strchr(name, '['));
633 
634 	return 0;
635 }
636 
637 /*
638  * Loads the function entries in /proc/kallsyms into kernel_map->dso,
639  * so that we can in the next step set the symbol ->end address and then
640  * call kernel_maps__split_kallsyms.
641  */
642 static int dso__load_all_kallsyms(struct dso *dso, const char *filename,
643 				  struct map *map)
644 {
645 	struct process_kallsyms_args args = { .map = map, .dso = dso, };
646 	return kallsyms__parse(filename, &args, map__process_kallsym_symbol);
647 }
648 
649 static int dso__split_kallsyms_for_kcore(struct dso *dso, struct map *map)
650 {
651 	struct map_groups *kmaps = map__kmaps(map);
652 	struct map *curr_map;
653 	struct symbol *pos;
654 	int count = 0;
655 	struct rb_root old_root = dso->symbols[map->type];
656 	struct rb_root *root = &dso->symbols[map->type];
657 	struct rb_node *next = rb_first(root);
658 
659 	if (!kmaps)
660 		return -1;
661 
662 	*root = RB_ROOT;
663 
664 	while (next) {
665 		char *module;
666 
667 		pos = rb_entry(next, struct symbol, rb_node);
668 		next = rb_next(&pos->rb_node);
669 
670 		rb_erase_init(&pos->rb_node, &old_root);
671 
672 		module = strchr(pos->name, '\t');
673 		if (module)
674 			*module = '\0';
675 
676 		curr_map = map_groups__find(kmaps, map->type, pos->start);
677 
678 		if (!curr_map) {
679 			symbol__delete(pos);
680 			continue;
681 		}
682 
683 		pos->start -= curr_map->start - curr_map->pgoff;
684 		if (pos->end)
685 			pos->end -= curr_map->start - curr_map->pgoff;
686 		symbols__insert(&curr_map->dso->symbols[curr_map->type], pos);
687 		++count;
688 	}
689 
690 	/* Symbols have been adjusted */
691 	dso->adjust_symbols = 1;
692 
693 	return count;
694 }
695 
696 /*
697  * Split the symbols into maps, making sure there are no overlaps, i.e. the
698  * kernel range is broken in several maps, named [kernel].N, as we don't have
699  * the original ELF section names vmlinux have.
700  */
701 static int dso__split_kallsyms(struct dso *dso, struct map *map, u64 delta)
702 {
703 	struct map_groups *kmaps = map__kmaps(map);
704 	struct machine *machine;
705 	struct map *curr_map = map;
706 	struct symbol *pos;
707 	int count = 0, moved = 0;
708 	struct rb_root *root = &dso->symbols[map->type];
709 	struct rb_node *next = rb_first(root);
710 	int kernel_range = 0;
711 
712 	if (!kmaps)
713 		return -1;
714 
715 	machine = kmaps->machine;
716 
717 	while (next) {
718 		char *module;
719 
720 		pos = rb_entry(next, struct symbol, rb_node);
721 		next = rb_next(&pos->rb_node);
722 
723 		module = strchr(pos->name, '\t');
724 		if (module) {
725 			if (!symbol_conf.use_modules)
726 				goto discard_symbol;
727 
728 			*module++ = '\0';
729 
730 			if (strcmp(curr_map->dso->short_name, module)) {
731 				if (curr_map != map &&
732 				    dso->kernel == DSO_TYPE_GUEST_KERNEL &&
733 				    machine__is_default_guest(machine)) {
734 					/*
735 					 * We assume all symbols of a module are
736 					 * continuous in * kallsyms, so curr_map
737 					 * points to a module and all its
738 					 * symbols are in its kmap. Mark it as
739 					 * loaded.
740 					 */
741 					dso__set_loaded(curr_map->dso,
742 							curr_map->type);
743 				}
744 
745 				curr_map = map_groups__find_by_name(kmaps,
746 							map->type, module);
747 				if (curr_map == NULL) {
748 					pr_debug("%s/proc/{kallsyms,modules} "
749 					         "inconsistency while looking "
750 						 "for \"%s\" module!\n",
751 						 machine->root_dir, module);
752 					curr_map = map;
753 					goto discard_symbol;
754 				}
755 
756 				if (curr_map->dso->loaded &&
757 				    !machine__is_default_guest(machine))
758 					goto discard_symbol;
759 			}
760 			/*
761 			 * So that we look just like we get from .ko files,
762 			 * i.e. not prelinked, relative to map->start.
763 			 */
764 			pos->start = curr_map->map_ip(curr_map, pos->start);
765 			pos->end   = curr_map->map_ip(curr_map, pos->end);
766 		} else if (curr_map != map) {
767 			char dso_name[PATH_MAX];
768 			struct dso *ndso;
769 
770 			if (delta) {
771 				/* Kernel was relocated at boot time */
772 				pos->start -= delta;
773 				pos->end -= delta;
774 			}
775 
776 			if (count == 0) {
777 				curr_map = map;
778 				goto add_symbol;
779 			}
780 
781 			if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
782 				snprintf(dso_name, sizeof(dso_name),
783 					"[guest.kernel].%d",
784 					kernel_range++);
785 			else
786 				snprintf(dso_name, sizeof(dso_name),
787 					"[kernel].%d",
788 					kernel_range++);
789 
790 			ndso = dso__new(dso_name);
791 			if (ndso == NULL)
792 				return -1;
793 
794 			ndso->kernel = dso->kernel;
795 
796 			curr_map = map__new2(pos->start, ndso, map->type);
797 			if (curr_map == NULL) {
798 				dso__put(ndso);
799 				return -1;
800 			}
801 
802 			curr_map->map_ip = curr_map->unmap_ip = identity__map_ip;
803 			map_groups__insert(kmaps, curr_map);
804 			++kernel_range;
805 		} else if (delta) {
806 			/* Kernel was relocated at boot time */
807 			pos->start -= delta;
808 			pos->end -= delta;
809 		}
810 add_symbol:
811 		if (curr_map != map) {
812 			rb_erase(&pos->rb_node, root);
813 			symbols__insert(&curr_map->dso->symbols[curr_map->type], pos);
814 			++moved;
815 		} else
816 			++count;
817 
818 		continue;
819 discard_symbol:
820 		rb_erase(&pos->rb_node, root);
821 		symbol__delete(pos);
822 	}
823 
824 	if (curr_map != map &&
825 	    dso->kernel == DSO_TYPE_GUEST_KERNEL &&
826 	    machine__is_default_guest(kmaps->machine)) {
827 		dso__set_loaded(curr_map->dso, curr_map->type);
828 	}
829 
830 	return count + moved;
831 }
832 
833 bool symbol__restricted_filename(const char *filename,
834 				 const char *restricted_filename)
835 {
836 	bool restricted = false;
837 
838 	if (symbol_conf.kptr_restrict) {
839 		char *r = realpath(filename, NULL);
840 
841 		if (r != NULL) {
842 			restricted = strcmp(r, restricted_filename) == 0;
843 			free(r);
844 			return restricted;
845 		}
846 	}
847 
848 	return restricted;
849 }
850 
851 struct module_info {
852 	struct rb_node rb_node;
853 	char *name;
854 	u64 start;
855 };
856 
857 static void add_module(struct module_info *mi, struct rb_root *modules)
858 {
859 	struct rb_node **p = &modules->rb_node;
860 	struct rb_node *parent = NULL;
861 	struct module_info *m;
862 
863 	while (*p != NULL) {
864 		parent = *p;
865 		m = rb_entry(parent, struct module_info, rb_node);
866 		if (strcmp(mi->name, m->name) < 0)
867 			p = &(*p)->rb_left;
868 		else
869 			p = &(*p)->rb_right;
870 	}
871 	rb_link_node(&mi->rb_node, parent, p);
872 	rb_insert_color(&mi->rb_node, modules);
873 }
874 
875 static void delete_modules(struct rb_root *modules)
876 {
877 	struct module_info *mi;
878 	struct rb_node *next = rb_first(modules);
879 
880 	while (next) {
881 		mi = rb_entry(next, struct module_info, rb_node);
882 		next = rb_next(&mi->rb_node);
883 		rb_erase(&mi->rb_node, modules);
884 		zfree(&mi->name);
885 		free(mi);
886 	}
887 }
888 
889 static struct module_info *find_module(const char *name,
890 				       struct rb_root *modules)
891 {
892 	struct rb_node *n = modules->rb_node;
893 
894 	while (n) {
895 		struct module_info *m;
896 		int cmp;
897 
898 		m = rb_entry(n, struct module_info, rb_node);
899 		cmp = strcmp(name, m->name);
900 		if (cmp < 0)
901 			n = n->rb_left;
902 		else if (cmp > 0)
903 			n = n->rb_right;
904 		else
905 			return m;
906 	}
907 
908 	return NULL;
909 }
910 
911 static int __read_proc_modules(void *arg, const char *name, u64 start)
912 {
913 	struct rb_root *modules = arg;
914 	struct module_info *mi;
915 
916 	mi = zalloc(sizeof(struct module_info));
917 	if (!mi)
918 		return -ENOMEM;
919 
920 	mi->name = strdup(name);
921 	mi->start = start;
922 
923 	if (!mi->name) {
924 		free(mi);
925 		return -ENOMEM;
926 	}
927 
928 	add_module(mi, modules);
929 
930 	return 0;
931 }
932 
933 static int read_proc_modules(const char *filename, struct rb_root *modules)
934 {
935 	if (symbol__restricted_filename(filename, "/proc/modules"))
936 		return -1;
937 
938 	if (modules__parse(filename, modules, __read_proc_modules)) {
939 		delete_modules(modules);
940 		return -1;
941 	}
942 
943 	return 0;
944 }
945 
946 int compare_proc_modules(const char *from, const char *to)
947 {
948 	struct rb_root from_modules = RB_ROOT;
949 	struct rb_root to_modules = RB_ROOT;
950 	struct rb_node *from_node, *to_node;
951 	struct module_info *from_m, *to_m;
952 	int ret = -1;
953 
954 	if (read_proc_modules(from, &from_modules))
955 		return -1;
956 
957 	if (read_proc_modules(to, &to_modules))
958 		goto out_delete_from;
959 
960 	from_node = rb_first(&from_modules);
961 	to_node = rb_first(&to_modules);
962 	while (from_node) {
963 		if (!to_node)
964 			break;
965 
966 		from_m = rb_entry(from_node, struct module_info, rb_node);
967 		to_m = rb_entry(to_node, struct module_info, rb_node);
968 
969 		if (from_m->start != to_m->start ||
970 		    strcmp(from_m->name, to_m->name))
971 			break;
972 
973 		from_node = rb_next(from_node);
974 		to_node = rb_next(to_node);
975 	}
976 
977 	if (!from_node && !to_node)
978 		ret = 0;
979 
980 	delete_modules(&to_modules);
981 out_delete_from:
982 	delete_modules(&from_modules);
983 
984 	return ret;
985 }
986 
987 static int do_validate_kcore_modules(const char *filename, struct map *map,
988 				  struct map_groups *kmaps)
989 {
990 	struct rb_root modules = RB_ROOT;
991 	struct map *old_map;
992 	int err;
993 
994 	err = read_proc_modules(filename, &modules);
995 	if (err)
996 		return err;
997 
998 	old_map = map_groups__first(kmaps, map->type);
999 	while (old_map) {
1000 		struct map *next = map_groups__next(old_map);
1001 		struct module_info *mi;
1002 
1003 		if (old_map == map || old_map->start == map->start) {
1004 			/* The kernel map */
1005 			old_map = next;
1006 			continue;
1007 		}
1008 
1009 		/* Module must be in memory at the same address */
1010 		mi = find_module(old_map->dso->short_name, &modules);
1011 		if (!mi || mi->start != old_map->start) {
1012 			err = -EINVAL;
1013 			goto out;
1014 		}
1015 
1016 		old_map = next;
1017 	}
1018 out:
1019 	delete_modules(&modules);
1020 	return err;
1021 }
1022 
1023 /*
1024  * If kallsyms is referenced by name then we look for filename in the same
1025  * directory.
1026  */
1027 static bool filename_from_kallsyms_filename(char *filename,
1028 					    const char *base_name,
1029 					    const char *kallsyms_filename)
1030 {
1031 	char *name;
1032 
1033 	strcpy(filename, kallsyms_filename);
1034 	name = strrchr(filename, '/');
1035 	if (!name)
1036 		return false;
1037 
1038 	name += 1;
1039 
1040 	if (!strcmp(name, "kallsyms")) {
1041 		strcpy(name, base_name);
1042 		return true;
1043 	}
1044 
1045 	return false;
1046 }
1047 
1048 static int validate_kcore_modules(const char *kallsyms_filename,
1049 				  struct map *map)
1050 {
1051 	struct map_groups *kmaps = map__kmaps(map);
1052 	char modules_filename[PATH_MAX];
1053 
1054 	if (!kmaps)
1055 		return -EINVAL;
1056 
1057 	if (!filename_from_kallsyms_filename(modules_filename, "modules",
1058 					     kallsyms_filename))
1059 		return -EINVAL;
1060 
1061 	if (do_validate_kcore_modules(modules_filename, map, kmaps))
1062 		return -EINVAL;
1063 
1064 	return 0;
1065 }
1066 
1067 static int validate_kcore_addresses(const char *kallsyms_filename,
1068 				    struct map *map)
1069 {
1070 	struct kmap *kmap = map__kmap(map);
1071 
1072 	if (!kmap)
1073 		return -EINVAL;
1074 
1075 	if (kmap->ref_reloc_sym && kmap->ref_reloc_sym->name) {
1076 		u64 start;
1077 
1078 		start = kallsyms__get_function_start(kallsyms_filename,
1079 						     kmap->ref_reloc_sym->name);
1080 		if (start != kmap->ref_reloc_sym->addr)
1081 			return -EINVAL;
1082 	}
1083 
1084 	return validate_kcore_modules(kallsyms_filename, map);
1085 }
1086 
1087 struct kcore_mapfn_data {
1088 	struct dso *dso;
1089 	enum map_type type;
1090 	struct list_head maps;
1091 };
1092 
1093 static int kcore_mapfn(u64 start, u64 len, u64 pgoff, void *data)
1094 {
1095 	struct kcore_mapfn_data *md = data;
1096 	struct map *map;
1097 
1098 	map = map__new2(start, md->dso, md->type);
1099 	if (map == NULL)
1100 		return -ENOMEM;
1101 
1102 	map->end = map->start + len;
1103 	map->pgoff = pgoff;
1104 
1105 	list_add(&map->node, &md->maps);
1106 
1107 	return 0;
1108 }
1109 
1110 static int dso__load_kcore(struct dso *dso, struct map *map,
1111 			   const char *kallsyms_filename)
1112 {
1113 	struct map_groups *kmaps = map__kmaps(map);
1114 	struct machine *machine;
1115 	struct kcore_mapfn_data md;
1116 	struct map *old_map, *new_map, *replacement_map = NULL;
1117 	bool is_64_bit;
1118 	int err, fd;
1119 	char kcore_filename[PATH_MAX];
1120 	struct symbol *sym;
1121 
1122 	if (!kmaps)
1123 		return -EINVAL;
1124 
1125 	machine = kmaps->machine;
1126 
1127 	/* This function requires that the map is the kernel map */
1128 	if (map != machine->vmlinux_maps[map->type])
1129 		return -EINVAL;
1130 
1131 	if (!filename_from_kallsyms_filename(kcore_filename, "kcore",
1132 					     kallsyms_filename))
1133 		return -EINVAL;
1134 
1135 	/* Modules and kernel must be present at their original addresses */
1136 	if (validate_kcore_addresses(kallsyms_filename, map))
1137 		return -EINVAL;
1138 
1139 	md.dso = dso;
1140 	md.type = map->type;
1141 	INIT_LIST_HEAD(&md.maps);
1142 
1143 	fd = open(kcore_filename, O_RDONLY);
1144 	if (fd < 0) {
1145 		pr_debug("Failed to open %s. Note /proc/kcore requires CAP_SYS_RAWIO capability to access.\n",
1146 			 kcore_filename);
1147 		return -EINVAL;
1148 	}
1149 
1150 	/* Read new maps into temporary lists */
1151 	err = file__read_maps(fd, md.type == MAP__FUNCTION, kcore_mapfn, &md,
1152 			      &is_64_bit);
1153 	if (err)
1154 		goto out_err;
1155 	dso->is_64_bit = is_64_bit;
1156 
1157 	if (list_empty(&md.maps)) {
1158 		err = -EINVAL;
1159 		goto out_err;
1160 	}
1161 
1162 	/* Remove old maps */
1163 	old_map = map_groups__first(kmaps, map->type);
1164 	while (old_map) {
1165 		struct map *next = map_groups__next(old_map);
1166 
1167 		if (old_map != map)
1168 			map_groups__remove(kmaps, old_map);
1169 		old_map = next;
1170 	}
1171 
1172 	/* Find the kernel map using the first symbol */
1173 	sym = dso__first_symbol(dso, map->type);
1174 	list_for_each_entry(new_map, &md.maps, node) {
1175 		if (sym && sym->start >= new_map->start &&
1176 		    sym->start < new_map->end) {
1177 			replacement_map = new_map;
1178 			break;
1179 		}
1180 	}
1181 
1182 	if (!replacement_map)
1183 		replacement_map = list_entry(md.maps.next, struct map, node);
1184 
1185 	/* Add new maps */
1186 	while (!list_empty(&md.maps)) {
1187 		new_map = list_entry(md.maps.next, struct map, node);
1188 		list_del_init(&new_map->node);
1189 		if (new_map == replacement_map) {
1190 			map->start	= new_map->start;
1191 			map->end	= new_map->end;
1192 			map->pgoff	= new_map->pgoff;
1193 			map->map_ip	= new_map->map_ip;
1194 			map->unmap_ip	= new_map->unmap_ip;
1195 			/* Ensure maps are correctly ordered */
1196 			map__get(map);
1197 			map_groups__remove(kmaps, map);
1198 			map_groups__insert(kmaps, map);
1199 			map__put(map);
1200 		} else {
1201 			map_groups__insert(kmaps, new_map);
1202 		}
1203 
1204 		map__put(new_map);
1205 	}
1206 
1207 	/*
1208 	 * Set the data type and long name so that kcore can be read via
1209 	 * dso__data_read_addr().
1210 	 */
1211 	if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
1212 		dso->binary_type = DSO_BINARY_TYPE__GUEST_KCORE;
1213 	else
1214 		dso->binary_type = DSO_BINARY_TYPE__KCORE;
1215 	dso__set_long_name(dso, strdup(kcore_filename), true);
1216 
1217 	close(fd);
1218 
1219 	if (map->type == MAP__FUNCTION)
1220 		pr_debug("Using %s for kernel object code\n", kcore_filename);
1221 	else
1222 		pr_debug("Using %s for kernel data\n", kcore_filename);
1223 
1224 	return 0;
1225 
1226 out_err:
1227 	while (!list_empty(&md.maps)) {
1228 		map = list_entry(md.maps.next, struct map, node);
1229 		list_del_init(&map->node);
1230 		map__put(map);
1231 	}
1232 	close(fd);
1233 	return -EINVAL;
1234 }
1235 
1236 /*
1237  * If the kernel is relocated at boot time, kallsyms won't match.  Compute the
1238  * delta based on the relocation reference symbol.
1239  */
1240 static int kallsyms__delta(struct map *map, const char *filename, u64 *delta)
1241 {
1242 	struct kmap *kmap = map__kmap(map);
1243 	u64 addr;
1244 
1245 	if (!kmap)
1246 		return -1;
1247 
1248 	if (!kmap->ref_reloc_sym || !kmap->ref_reloc_sym->name)
1249 		return 0;
1250 
1251 	addr = kallsyms__get_function_start(filename,
1252 					    kmap->ref_reloc_sym->name);
1253 	if (!addr)
1254 		return -1;
1255 
1256 	*delta = addr - kmap->ref_reloc_sym->addr;
1257 	return 0;
1258 }
1259 
1260 int __dso__load_kallsyms(struct dso *dso, const char *filename,
1261 			 struct map *map, bool no_kcore)
1262 {
1263 	u64 delta = 0;
1264 
1265 	if (symbol__restricted_filename(filename, "/proc/kallsyms"))
1266 		return -1;
1267 
1268 	if (dso__load_all_kallsyms(dso, filename, map) < 0)
1269 		return -1;
1270 
1271 	if (kallsyms__delta(map, filename, &delta))
1272 		return -1;
1273 
1274 	symbols__fixup_end(&dso->symbols[map->type]);
1275 	symbols__fixup_duplicate(&dso->symbols[map->type]);
1276 
1277 	if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
1278 		dso->symtab_type = DSO_BINARY_TYPE__GUEST_KALLSYMS;
1279 	else
1280 		dso->symtab_type = DSO_BINARY_TYPE__KALLSYMS;
1281 
1282 	if (!no_kcore && !dso__load_kcore(dso, map, filename))
1283 		return dso__split_kallsyms_for_kcore(dso, map);
1284 	else
1285 		return dso__split_kallsyms(dso, map, delta);
1286 }
1287 
1288 int dso__load_kallsyms(struct dso *dso, const char *filename,
1289 		       struct map *map)
1290 {
1291 	return __dso__load_kallsyms(dso, filename, map, false);
1292 }
1293 
1294 static int dso__load_perf_map(struct dso *dso, struct map *map)
1295 {
1296 	char *line = NULL;
1297 	size_t n;
1298 	FILE *file;
1299 	int nr_syms = 0;
1300 
1301 	file = fopen(dso->long_name, "r");
1302 	if (file == NULL)
1303 		goto out_failure;
1304 
1305 	while (!feof(file)) {
1306 		u64 start, size;
1307 		struct symbol *sym;
1308 		int line_len, len;
1309 
1310 		line_len = getline(&line, &n, file);
1311 		if (line_len < 0)
1312 			break;
1313 
1314 		if (!line)
1315 			goto out_failure;
1316 
1317 		line[--line_len] = '\0'; /* \n */
1318 
1319 		len = hex2u64(line, &start);
1320 
1321 		len++;
1322 		if (len + 2 >= line_len)
1323 			continue;
1324 
1325 		len += hex2u64(line + len, &size);
1326 
1327 		len++;
1328 		if (len + 2 >= line_len)
1329 			continue;
1330 
1331 		sym = symbol__new(start, size, STB_GLOBAL, line + len);
1332 
1333 		if (sym == NULL)
1334 			goto out_delete_line;
1335 
1336 		symbols__insert(&dso->symbols[map->type], sym);
1337 		nr_syms++;
1338 	}
1339 
1340 	free(line);
1341 	fclose(file);
1342 
1343 	return nr_syms;
1344 
1345 out_delete_line:
1346 	free(line);
1347 out_failure:
1348 	return -1;
1349 }
1350 
1351 static bool dso__is_compatible_symtab_type(struct dso *dso, bool kmod,
1352 					   enum dso_binary_type type)
1353 {
1354 	switch (type) {
1355 	case DSO_BINARY_TYPE__JAVA_JIT:
1356 	case DSO_BINARY_TYPE__DEBUGLINK:
1357 	case DSO_BINARY_TYPE__SYSTEM_PATH_DSO:
1358 	case DSO_BINARY_TYPE__FEDORA_DEBUGINFO:
1359 	case DSO_BINARY_TYPE__UBUNTU_DEBUGINFO:
1360 	case DSO_BINARY_TYPE__BUILDID_DEBUGINFO:
1361 	case DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO:
1362 		return !kmod && dso->kernel == DSO_TYPE_USER;
1363 
1364 	case DSO_BINARY_TYPE__KALLSYMS:
1365 	case DSO_BINARY_TYPE__VMLINUX:
1366 	case DSO_BINARY_TYPE__KCORE:
1367 		return dso->kernel == DSO_TYPE_KERNEL;
1368 
1369 	case DSO_BINARY_TYPE__GUEST_KALLSYMS:
1370 	case DSO_BINARY_TYPE__GUEST_VMLINUX:
1371 	case DSO_BINARY_TYPE__GUEST_KCORE:
1372 		return dso->kernel == DSO_TYPE_GUEST_KERNEL;
1373 
1374 	case DSO_BINARY_TYPE__GUEST_KMODULE:
1375 	case DSO_BINARY_TYPE__GUEST_KMODULE_COMP:
1376 	case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE:
1377 	case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP:
1378 		/*
1379 		 * kernel modules know their symtab type - it's set when
1380 		 * creating a module dso in machine__findnew_module_map().
1381 		 */
1382 		return kmod && dso->symtab_type == type;
1383 
1384 	case DSO_BINARY_TYPE__BUILD_ID_CACHE:
1385 		return true;
1386 
1387 	case DSO_BINARY_TYPE__NOT_FOUND:
1388 	default:
1389 		return false;
1390 	}
1391 }
1392 
1393 int dso__load(struct dso *dso, struct map *map)
1394 {
1395 	char *name;
1396 	int ret = -1;
1397 	u_int i;
1398 	struct machine *machine;
1399 	char *root_dir = (char *) "";
1400 	int ss_pos = 0;
1401 	struct symsrc ss_[2];
1402 	struct symsrc *syms_ss = NULL, *runtime_ss = NULL;
1403 	bool kmod;
1404 	unsigned char build_id[BUILD_ID_SIZE];
1405 
1406 	pthread_mutex_lock(&dso->lock);
1407 
1408 	/* check again under the dso->lock */
1409 	if (dso__loaded(dso, map->type)) {
1410 		ret = 1;
1411 		goto out;
1412 	}
1413 
1414 	if (dso->kernel) {
1415 		if (dso->kernel == DSO_TYPE_KERNEL)
1416 			ret = dso__load_kernel_sym(dso, map);
1417 		else if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
1418 			ret = dso__load_guest_kernel_sym(dso, map);
1419 
1420 		goto out;
1421 	}
1422 
1423 	if (map->groups && map->groups->machine)
1424 		machine = map->groups->machine;
1425 	else
1426 		machine = NULL;
1427 
1428 	dso->adjust_symbols = 0;
1429 
1430 	if (strncmp(dso->name, "/tmp/perf-", 10) == 0) {
1431 		struct stat st;
1432 
1433 		if (lstat(dso->name, &st) < 0)
1434 			goto out;
1435 
1436 		if (!symbol_conf.force && st.st_uid && (st.st_uid != geteuid())) {
1437 			pr_warning("File %s not owned by current user or root, "
1438 				   "ignoring it (use -f to override).\n", dso->name);
1439 			goto out;
1440 		}
1441 
1442 		ret = dso__load_perf_map(dso, map);
1443 		dso->symtab_type = ret > 0 ? DSO_BINARY_TYPE__JAVA_JIT :
1444 					     DSO_BINARY_TYPE__NOT_FOUND;
1445 		goto out;
1446 	}
1447 
1448 	if (machine)
1449 		root_dir = machine->root_dir;
1450 
1451 	name = malloc(PATH_MAX);
1452 	if (!name)
1453 		goto out;
1454 
1455 	kmod = dso->symtab_type == DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE ||
1456 		dso->symtab_type == DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP ||
1457 		dso->symtab_type == DSO_BINARY_TYPE__GUEST_KMODULE ||
1458 		dso->symtab_type == DSO_BINARY_TYPE__GUEST_KMODULE_COMP;
1459 
1460 
1461 	/*
1462 	 * Read the build id if possible. This is required for
1463 	 * DSO_BINARY_TYPE__BUILDID_DEBUGINFO to work
1464 	 */
1465 	if (!dso->has_build_id &&
1466 	    is_regular_file(dso->long_name)) {
1467 	    __symbol__join_symfs(name, PATH_MAX, dso->long_name);
1468 	    if (filename__read_build_id(name, build_id, BUILD_ID_SIZE) > 0)
1469 		dso__set_build_id(dso, build_id);
1470 	}
1471 
1472 	/*
1473 	 * Iterate over candidate debug images.
1474 	 * Keep track of "interesting" ones (those which have a symtab, dynsym,
1475 	 * and/or opd section) for processing.
1476 	 */
1477 	for (i = 0; i < DSO_BINARY_TYPE__SYMTAB_CNT; i++) {
1478 		struct symsrc *ss = &ss_[ss_pos];
1479 		bool next_slot = false;
1480 
1481 		enum dso_binary_type symtab_type = binary_type_symtab[i];
1482 
1483 		if (!dso__is_compatible_symtab_type(dso, kmod, symtab_type))
1484 			continue;
1485 
1486 		if (dso__read_binary_type_filename(dso, symtab_type,
1487 						   root_dir, name, PATH_MAX))
1488 			continue;
1489 
1490 		if (!is_regular_file(name))
1491 			continue;
1492 
1493 		/* Name is now the name of the next image to try */
1494 		if (symsrc__init(ss, dso, name, symtab_type) < 0)
1495 			continue;
1496 
1497 		if (!syms_ss && symsrc__has_symtab(ss)) {
1498 			syms_ss = ss;
1499 			next_slot = true;
1500 			if (!dso->symsrc_filename)
1501 				dso->symsrc_filename = strdup(name);
1502 		}
1503 
1504 		if (!runtime_ss && symsrc__possibly_runtime(ss)) {
1505 			runtime_ss = ss;
1506 			next_slot = true;
1507 		}
1508 
1509 		if (next_slot) {
1510 			ss_pos++;
1511 
1512 			if (syms_ss && runtime_ss)
1513 				break;
1514 		} else {
1515 			symsrc__destroy(ss);
1516 		}
1517 
1518 	}
1519 
1520 	if (!runtime_ss && !syms_ss)
1521 		goto out_free;
1522 
1523 	if (runtime_ss && !syms_ss) {
1524 		syms_ss = runtime_ss;
1525 	}
1526 
1527 	/* We'll have to hope for the best */
1528 	if (!runtime_ss && syms_ss)
1529 		runtime_ss = syms_ss;
1530 
1531 	if (syms_ss && syms_ss->type == DSO_BINARY_TYPE__BUILD_ID_CACHE)
1532 		if (dso__build_id_is_kmod(dso, name, PATH_MAX))
1533 			kmod = true;
1534 
1535 	if (syms_ss)
1536 		ret = dso__load_sym(dso, map, syms_ss, runtime_ss, kmod);
1537 	else
1538 		ret = -1;
1539 
1540 	if (ret > 0) {
1541 		int nr_plt;
1542 
1543 		nr_plt = dso__synthesize_plt_symbols(dso, runtime_ss, map);
1544 		if (nr_plt > 0)
1545 			ret += nr_plt;
1546 	}
1547 
1548 	for (; ss_pos > 0; ss_pos--)
1549 		symsrc__destroy(&ss_[ss_pos - 1]);
1550 out_free:
1551 	free(name);
1552 	if (ret < 0 && strstr(dso->name, " (deleted)") != NULL)
1553 		ret = 0;
1554 out:
1555 	dso__set_loaded(dso, map->type);
1556 	pthread_mutex_unlock(&dso->lock);
1557 
1558 	return ret;
1559 }
1560 
1561 struct map *map_groups__find_by_name(struct map_groups *mg,
1562 				     enum map_type type, const char *name)
1563 {
1564 	struct maps *maps = &mg->maps[type];
1565 	struct map *map;
1566 
1567 	pthread_rwlock_rdlock(&maps->lock);
1568 
1569 	for (map = maps__first(maps); map; map = map__next(map)) {
1570 		if (map->dso && strcmp(map->dso->short_name, name) == 0)
1571 			goto out_unlock;
1572 	}
1573 
1574 	map = NULL;
1575 
1576 out_unlock:
1577 	pthread_rwlock_unlock(&maps->lock);
1578 	return map;
1579 }
1580 
1581 int dso__load_vmlinux(struct dso *dso, struct map *map,
1582 		      const char *vmlinux, bool vmlinux_allocated)
1583 {
1584 	int err = -1;
1585 	struct symsrc ss;
1586 	char symfs_vmlinux[PATH_MAX];
1587 	enum dso_binary_type symtab_type;
1588 
1589 	if (vmlinux[0] == '/')
1590 		snprintf(symfs_vmlinux, sizeof(symfs_vmlinux), "%s", vmlinux);
1591 	else
1592 		symbol__join_symfs(symfs_vmlinux, vmlinux);
1593 
1594 	if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
1595 		symtab_type = DSO_BINARY_TYPE__GUEST_VMLINUX;
1596 	else
1597 		symtab_type = DSO_BINARY_TYPE__VMLINUX;
1598 
1599 	if (symsrc__init(&ss, dso, symfs_vmlinux, symtab_type))
1600 		return -1;
1601 
1602 	err = dso__load_sym(dso, map, &ss, &ss, 0);
1603 	symsrc__destroy(&ss);
1604 
1605 	if (err > 0) {
1606 		if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
1607 			dso->binary_type = DSO_BINARY_TYPE__GUEST_VMLINUX;
1608 		else
1609 			dso->binary_type = DSO_BINARY_TYPE__VMLINUX;
1610 		dso__set_long_name(dso, vmlinux, vmlinux_allocated);
1611 		dso__set_loaded(dso, map->type);
1612 		pr_debug("Using %s for symbols\n", symfs_vmlinux);
1613 	}
1614 
1615 	return err;
1616 }
1617 
1618 int dso__load_vmlinux_path(struct dso *dso, struct map *map)
1619 {
1620 	int i, err = 0;
1621 	char *filename = NULL;
1622 
1623 	pr_debug("Looking at the vmlinux_path (%d entries long)\n",
1624 		 vmlinux_path__nr_entries + 1);
1625 
1626 	for (i = 0; i < vmlinux_path__nr_entries; ++i) {
1627 		err = dso__load_vmlinux(dso, map, vmlinux_path[i], false);
1628 		if (err > 0)
1629 			goto out;
1630 	}
1631 
1632 	if (!symbol_conf.ignore_vmlinux_buildid)
1633 		filename = dso__build_id_filename(dso, NULL, 0);
1634 	if (filename != NULL) {
1635 		err = dso__load_vmlinux(dso, map, filename, true);
1636 		if (err > 0)
1637 			goto out;
1638 		free(filename);
1639 	}
1640 out:
1641 	return err;
1642 }
1643 
1644 static bool visible_dir_filter(const char *name, struct dirent *d)
1645 {
1646 	if (d->d_type != DT_DIR)
1647 		return false;
1648 	return lsdir_no_dot_filter(name, d);
1649 }
1650 
1651 static int find_matching_kcore(struct map *map, char *dir, size_t dir_sz)
1652 {
1653 	char kallsyms_filename[PATH_MAX];
1654 	int ret = -1;
1655 	struct strlist *dirs;
1656 	struct str_node *nd;
1657 
1658 	dirs = lsdir(dir, visible_dir_filter);
1659 	if (!dirs)
1660 		return -1;
1661 
1662 	strlist__for_each_entry(nd, dirs) {
1663 		scnprintf(kallsyms_filename, sizeof(kallsyms_filename),
1664 			  "%s/%s/kallsyms", dir, nd->s);
1665 		if (!validate_kcore_addresses(kallsyms_filename, map)) {
1666 			strlcpy(dir, kallsyms_filename, dir_sz);
1667 			ret = 0;
1668 			break;
1669 		}
1670 	}
1671 
1672 	strlist__delete(dirs);
1673 
1674 	return ret;
1675 }
1676 
1677 /*
1678  * Use open(O_RDONLY) to check readability directly instead of access(R_OK)
1679  * since access(R_OK) only checks with real UID/GID but open() use effective
1680  * UID/GID and actual capabilities (e.g. /proc/kcore requires CAP_SYS_RAWIO).
1681  */
1682 static bool filename__readable(const char *file)
1683 {
1684 	int fd = open(file, O_RDONLY);
1685 	if (fd < 0)
1686 		return false;
1687 	close(fd);
1688 	return true;
1689 }
1690 
1691 static char *dso__find_kallsyms(struct dso *dso, struct map *map)
1692 {
1693 	u8 host_build_id[BUILD_ID_SIZE];
1694 	char sbuild_id[SBUILD_ID_SIZE];
1695 	bool is_host = false;
1696 	char path[PATH_MAX];
1697 
1698 	if (!dso->has_build_id) {
1699 		/*
1700 		 * Last resort, if we don't have a build-id and couldn't find
1701 		 * any vmlinux file, try the running kernel kallsyms table.
1702 		 */
1703 		goto proc_kallsyms;
1704 	}
1705 
1706 	if (sysfs__read_build_id("/sys/kernel/notes", host_build_id,
1707 				 sizeof(host_build_id)) == 0)
1708 		is_host = dso__build_id_equal(dso, host_build_id);
1709 
1710 	/* Try a fast path for /proc/kallsyms if possible */
1711 	if (is_host) {
1712 		/*
1713 		 * Do not check the build-id cache, unless we know we cannot use
1714 		 * /proc/kcore or module maps don't match to /proc/kallsyms.
1715 		 * To check readability of /proc/kcore, do not use access(R_OK)
1716 		 * since /proc/kcore requires CAP_SYS_RAWIO to read and access
1717 		 * can't check it.
1718 		 */
1719 		if (filename__readable("/proc/kcore") &&
1720 		    !validate_kcore_addresses("/proc/kallsyms", map))
1721 			goto proc_kallsyms;
1722 	}
1723 
1724 	build_id__sprintf(dso->build_id, sizeof(dso->build_id), sbuild_id);
1725 
1726 	/* Find kallsyms in build-id cache with kcore */
1727 	scnprintf(path, sizeof(path), "%s/%s/%s",
1728 		  buildid_dir, DSO__NAME_KCORE, sbuild_id);
1729 
1730 	if (!find_matching_kcore(map, path, sizeof(path)))
1731 		return strdup(path);
1732 
1733 	/* Use current /proc/kallsyms if possible */
1734 	if (is_host) {
1735 proc_kallsyms:
1736 		return strdup("/proc/kallsyms");
1737 	}
1738 
1739 	/* Finally, find a cache of kallsyms */
1740 	if (!build_id_cache__kallsyms_path(sbuild_id, path, sizeof(path))) {
1741 		pr_err("No kallsyms or vmlinux with build-id %s was found\n",
1742 		       sbuild_id);
1743 		return NULL;
1744 	}
1745 
1746 	return strdup(path);
1747 }
1748 
1749 static int dso__load_kernel_sym(struct dso *dso, struct map *map)
1750 {
1751 	int err;
1752 	const char *kallsyms_filename = NULL;
1753 	char *kallsyms_allocated_filename = NULL;
1754 	/*
1755 	 * Step 1: if the user specified a kallsyms or vmlinux filename, use
1756 	 * it and only it, reporting errors to the user if it cannot be used.
1757 	 *
1758 	 * For instance, try to analyse an ARM perf.data file _without_ a
1759 	 * build-id, or if the user specifies the wrong path to the right
1760 	 * vmlinux file, obviously we can't fallback to another vmlinux (a
1761 	 * x86_86 one, on the machine where analysis is being performed, say),
1762 	 * or worse, /proc/kallsyms.
1763 	 *
1764 	 * If the specified file _has_ a build-id and there is a build-id
1765 	 * section in the perf.data file, we will still do the expected
1766 	 * validation in dso__load_vmlinux and will bail out if they don't
1767 	 * match.
1768 	 */
1769 	if (symbol_conf.kallsyms_name != NULL) {
1770 		kallsyms_filename = symbol_conf.kallsyms_name;
1771 		goto do_kallsyms;
1772 	}
1773 
1774 	if (!symbol_conf.ignore_vmlinux && symbol_conf.vmlinux_name != NULL) {
1775 		return dso__load_vmlinux(dso, map, symbol_conf.vmlinux_name, false);
1776 	}
1777 
1778 	if (!symbol_conf.ignore_vmlinux && vmlinux_path != NULL) {
1779 		err = dso__load_vmlinux_path(dso, map);
1780 		if (err > 0)
1781 			return err;
1782 	}
1783 
1784 	/* do not try local files if a symfs was given */
1785 	if (symbol_conf.symfs[0] != 0)
1786 		return -1;
1787 
1788 	kallsyms_allocated_filename = dso__find_kallsyms(dso, map);
1789 	if (!kallsyms_allocated_filename)
1790 		return -1;
1791 
1792 	kallsyms_filename = kallsyms_allocated_filename;
1793 
1794 do_kallsyms:
1795 	err = dso__load_kallsyms(dso, kallsyms_filename, map);
1796 	if (err > 0)
1797 		pr_debug("Using %s for symbols\n", kallsyms_filename);
1798 	free(kallsyms_allocated_filename);
1799 
1800 	if (err > 0 && !dso__is_kcore(dso)) {
1801 		dso->binary_type = DSO_BINARY_TYPE__KALLSYMS;
1802 		dso__set_long_name(dso, DSO__NAME_KALLSYMS, false);
1803 		map__fixup_start(map);
1804 		map__fixup_end(map);
1805 	}
1806 
1807 	return err;
1808 }
1809 
1810 static int dso__load_guest_kernel_sym(struct dso *dso, struct map *map)
1811 {
1812 	int err;
1813 	const char *kallsyms_filename = NULL;
1814 	struct machine *machine;
1815 	char path[PATH_MAX];
1816 
1817 	if (!map->groups) {
1818 		pr_debug("Guest kernel map hasn't the point to groups\n");
1819 		return -1;
1820 	}
1821 	machine = map->groups->machine;
1822 
1823 	if (machine__is_default_guest(machine)) {
1824 		/*
1825 		 * if the user specified a vmlinux filename, use it and only
1826 		 * it, reporting errors to the user if it cannot be used.
1827 		 * Or use file guest_kallsyms inputted by user on commandline
1828 		 */
1829 		if (symbol_conf.default_guest_vmlinux_name != NULL) {
1830 			err = dso__load_vmlinux(dso, map,
1831 						symbol_conf.default_guest_vmlinux_name,
1832 						false);
1833 			return err;
1834 		}
1835 
1836 		kallsyms_filename = symbol_conf.default_guest_kallsyms;
1837 		if (!kallsyms_filename)
1838 			return -1;
1839 	} else {
1840 		sprintf(path, "%s/proc/kallsyms", machine->root_dir);
1841 		kallsyms_filename = path;
1842 	}
1843 
1844 	err = dso__load_kallsyms(dso, kallsyms_filename, map);
1845 	if (err > 0)
1846 		pr_debug("Using %s for symbols\n", kallsyms_filename);
1847 	if (err > 0 && !dso__is_kcore(dso)) {
1848 		dso->binary_type = DSO_BINARY_TYPE__GUEST_KALLSYMS;
1849 		machine__mmap_name(machine, path, sizeof(path));
1850 		dso__set_long_name(dso, strdup(path), true);
1851 		map__fixup_start(map);
1852 		map__fixup_end(map);
1853 	}
1854 
1855 	return err;
1856 }
1857 
1858 static void vmlinux_path__exit(void)
1859 {
1860 	while (--vmlinux_path__nr_entries >= 0)
1861 		zfree(&vmlinux_path[vmlinux_path__nr_entries]);
1862 	vmlinux_path__nr_entries = 0;
1863 
1864 	zfree(&vmlinux_path);
1865 }
1866 
1867 static const char * const vmlinux_paths[] = {
1868 	"vmlinux",
1869 	"/boot/vmlinux"
1870 };
1871 
1872 static const char * const vmlinux_paths_upd[] = {
1873 	"/boot/vmlinux-%s",
1874 	"/usr/lib/debug/boot/vmlinux-%s",
1875 	"/lib/modules/%s/build/vmlinux",
1876 	"/usr/lib/debug/lib/modules/%s/vmlinux",
1877 	"/usr/lib/debug/boot/vmlinux-%s.debug"
1878 };
1879 
1880 static int vmlinux_path__add(const char *new_entry)
1881 {
1882 	vmlinux_path[vmlinux_path__nr_entries] = strdup(new_entry);
1883 	if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1884 		return -1;
1885 	++vmlinux_path__nr_entries;
1886 
1887 	return 0;
1888 }
1889 
1890 static int vmlinux_path__init(struct perf_env *env)
1891 {
1892 	struct utsname uts;
1893 	char bf[PATH_MAX];
1894 	char *kernel_version;
1895 	unsigned int i;
1896 
1897 	vmlinux_path = malloc(sizeof(char *) * (ARRAY_SIZE(vmlinux_paths) +
1898 			      ARRAY_SIZE(vmlinux_paths_upd)));
1899 	if (vmlinux_path == NULL)
1900 		return -1;
1901 
1902 	for (i = 0; i < ARRAY_SIZE(vmlinux_paths); i++)
1903 		if (vmlinux_path__add(vmlinux_paths[i]) < 0)
1904 			goto out_fail;
1905 
1906 	/* only try kernel version if no symfs was given */
1907 	if (symbol_conf.symfs[0] != 0)
1908 		return 0;
1909 
1910 	if (env) {
1911 		kernel_version = env->os_release;
1912 	} else {
1913 		if (uname(&uts) < 0)
1914 			goto out_fail;
1915 
1916 		kernel_version = uts.release;
1917 	}
1918 
1919 	for (i = 0; i < ARRAY_SIZE(vmlinux_paths_upd); i++) {
1920 		snprintf(bf, sizeof(bf), vmlinux_paths_upd[i], kernel_version);
1921 		if (vmlinux_path__add(bf) < 0)
1922 			goto out_fail;
1923 	}
1924 
1925 	return 0;
1926 
1927 out_fail:
1928 	vmlinux_path__exit();
1929 	return -1;
1930 }
1931 
1932 int setup_list(struct strlist **list, const char *list_str,
1933 		      const char *list_name)
1934 {
1935 	if (list_str == NULL)
1936 		return 0;
1937 
1938 	*list = strlist__new(list_str, NULL);
1939 	if (!*list) {
1940 		pr_err("problems parsing %s list\n", list_name);
1941 		return -1;
1942 	}
1943 
1944 	symbol_conf.has_filter = true;
1945 	return 0;
1946 }
1947 
1948 int setup_intlist(struct intlist **list, const char *list_str,
1949 		  const char *list_name)
1950 {
1951 	if (list_str == NULL)
1952 		return 0;
1953 
1954 	*list = intlist__new(list_str);
1955 	if (!*list) {
1956 		pr_err("problems parsing %s list\n", list_name);
1957 		return -1;
1958 	}
1959 	return 0;
1960 }
1961 
1962 static bool symbol__read_kptr_restrict(void)
1963 {
1964 	bool value = false;
1965 	FILE *fp = fopen("/proc/sys/kernel/kptr_restrict", "r");
1966 
1967 	if (fp != NULL) {
1968 		char line[8];
1969 
1970 		if (fgets(line, sizeof(line), fp) != NULL)
1971 			value = ((geteuid() != 0) || (getuid() != 0)) ?
1972 					(atoi(line) != 0) :
1973 					(atoi(line) == 2);
1974 
1975 		fclose(fp);
1976 	}
1977 
1978 	return value;
1979 }
1980 
1981 int symbol__annotation_init(void)
1982 {
1983 	if (symbol_conf.initialized) {
1984 		pr_err("Annotation needs to be init before symbol__init()\n");
1985 		return -1;
1986 	}
1987 
1988 	if (symbol_conf.init_annotation) {
1989 		pr_warning("Annotation being initialized multiple times\n");
1990 		return 0;
1991 	}
1992 
1993 	symbol_conf.priv_size += sizeof(struct annotation);
1994 	symbol_conf.init_annotation = true;
1995 	return 0;
1996 }
1997 
1998 int symbol__init(struct perf_env *env)
1999 {
2000 	const char *symfs;
2001 
2002 	if (symbol_conf.initialized)
2003 		return 0;
2004 
2005 	symbol_conf.priv_size = PERF_ALIGN(symbol_conf.priv_size, sizeof(u64));
2006 
2007 	symbol__elf_init();
2008 
2009 	if (symbol_conf.sort_by_name)
2010 		symbol_conf.priv_size += (sizeof(struct symbol_name_rb_node) -
2011 					  sizeof(struct symbol));
2012 
2013 	if (symbol_conf.try_vmlinux_path && vmlinux_path__init(env) < 0)
2014 		return -1;
2015 
2016 	if (symbol_conf.field_sep && *symbol_conf.field_sep == '.') {
2017 		pr_err("'.' is the only non valid --field-separator argument\n");
2018 		return -1;
2019 	}
2020 
2021 	if (setup_list(&symbol_conf.dso_list,
2022 		       symbol_conf.dso_list_str, "dso") < 0)
2023 		return -1;
2024 
2025 	if (setup_list(&symbol_conf.comm_list,
2026 		       symbol_conf.comm_list_str, "comm") < 0)
2027 		goto out_free_dso_list;
2028 
2029 	if (setup_intlist(&symbol_conf.pid_list,
2030 		       symbol_conf.pid_list_str, "pid") < 0)
2031 		goto out_free_comm_list;
2032 
2033 	if (setup_intlist(&symbol_conf.tid_list,
2034 		       symbol_conf.tid_list_str, "tid") < 0)
2035 		goto out_free_pid_list;
2036 
2037 	if (setup_list(&symbol_conf.sym_list,
2038 		       symbol_conf.sym_list_str, "symbol") < 0)
2039 		goto out_free_tid_list;
2040 
2041 	if (setup_list(&symbol_conf.bt_stop_list,
2042 		       symbol_conf.bt_stop_list_str, "symbol") < 0)
2043 		goto out_free_sym_list;
2044 
2045 	/*
2046 	 * A path to symbols of "/" is identical to ""
2047 	 * reset here for simplicity.
2048 	 */
2049 	symfs = realpath(symbol_conf.symfs, NULL);
2050 	if (symfs == NULL)
2051 		symfs = symbol_conf.symfs;
2052 	if (strcmp(symfs, "/") == 0)
2053 		symbol_conf.symfs = "";
2054 	if (symfs != symbol_conf.symfs)
2055 		free((void *)symfs);
2056 
2057 	symbol_conf.kptr_restrict = symbol__read_kptr_restrict();
2058 
2059 	symbol_conf.initialized = true;
2060 	return 0;
2061 
2062 out_free_sym_list:
2063 	strlist__delete(symbol_conf.sym_list);
2064 out_free_tid_list:
2065 	intlist__delete(symbol_conf.tid_list);
2066 out_free_pid_list:
2067 	intlist__delete(symbol_conf.pid_list);
2068 out_free_comm_list:
2069 	strlist__delete(symbol_conf.comm_list);
2070 out_free_dso_list:
2071 	strlist__delete(symbol_conf.dso_list);
2072 	return -1;
2073 }
2074 
2075 void symbol__exit(void)
2076 {
2077 	if (!symbol_conf.initialized)
2078 		return;
2079 	strlist__delete(symbol_conf.bt_stop_list);
2080 	strlist__delete(symbol_conf.sym_list);
2081 	strlist__delete(symbol_conf.dso_list);
2082 	strlist__delete(symbol_conf.comm_list);
2083 	intlist__delete(symbol_conf.tid_list);
2084 	intlist__delete(symbol_conf.pid_list);
2085 	vmlinux_path__exit();
2086 	symbol_conf.sym_list = symbol_conf.dso_list = symbol_conf.comm_list = NULL;
2087 	symbol_conf.bt_stop_list = NULL;
2088 	symbol_conf.initialized = false;
2089 }
2090 
2091 int symbol__config_symfs(const struct option *opt __maybe_unused,
2092 			 const char *dir, int unset __maybe_unused)
2093 {
2094 	char *bf = NULL;
2095 	int ret;
2096 
2097 	symbol_conf.symfs = strdup(dir);
2098 	if (symbol_conf.symfs == NULL)
2099 		return -ENOMEM;
2100 
2101 	/* skip the locally configured cache if a symfs is given, and
2102 	 * config buildid dir to symfs/.debug
2103 	 */
2104 	ret = asprintf(&bf, "%s/%s", dir, ".debug");
2105 	if (ret < 0)
2106 		return -ENOMEM;
2107 
2108 	set_buildid_dir(bf);
2109 
2110 	free(bf);
2111 	return 0;
2112 }
2113