summaryrefslogtreecommitdiffstats
path: root/kexec/symbols.c
blob: 04377ca659ba26bca0a91051e1ae5bab0b21999e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <stdio.h>
#include <string.h>
#include "kexec.h"

/* Retrieve kernel symbol virtual address from /proc/kallsyms */
unsigned long long get_kernel_sym(const char *symbol)
{
	const char *kallsyms = "/proc/kallsyms";
	char sym[128];
	char line[128];
	FILE *fp;
	unsigned long long vaddr;
	char type;

	fp = fopen(kallsyms, "r");
	if (!fp) {
		fprintf(stderr, "Cannot open %s\n", kallsyms);
		return 0;
	}

	while (fgets(line, sizeof(line), fp) != NULL) {
		if (sscanf(line, "%llx %c %s", &vaddr, &type, sym) != 3)
			continue;
		if (strcmp(sym, symbol) == 0) {
			dbgprintf("kernel symbol %s vaddr = %16llx\n",
					symbol, vaddr);
			fclose(fp);
			return vaddr;
		}
	}

	dbgprintf("Cannot get kernel %s symbol address\n", symbol);

	fclose(fp);
	return 0;
}