-
Notifications
You must be signed in to change notification settings - Fork 356
/
kallsyms.c
55 lines (44 loc) · 882 Bytes
/
kallsyms.c
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "kallsyms.h"
bool
kallsyms_exist(void)
{
struct stat st;
if (stat("/proc/kallsyms", &st) < 0) {
return false;
}
if (st.st_mode & S_IROTH) {
return kallsyms_get_symbol_address("_stext") != 0;
}
return false;
}
void *
kallsyms_get_symbol_address(const char *symbol_name)
{
FILE *fp;
char function[BUFSIZ];
char symbol;
void *address;
int ret;
fp = fopen("/proc/kallsyms", "r");
if (!fp) {
printf("Failed to open /proc/kallsyms due to %s.", strerror(errno));
return 0;
}
while(!feof(fp)) {
ret = fscanf(fp, "%p %c %s", &address, &symbol, function);
if (ret != 3) {
break;
}
if (!strcmp(function, symbol_name)) {
fclose(fp);
return address;
}
}
fclose(fp);
return NULL;
}