File Index
Files and directories
Sign in
FILE

symbol.c

File preview with syntax highlighting.

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