-
Notifications
You must be signed in to change notification settings - Fork 51
/
kallsyms.c
85 lines (70 loc) · 1.39 KB
/
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#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.\n", 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;
}
char *
kallsyms_get_symbol_by_address(void *symbol_address)
{
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.\n", strerror(errno));
return 0;
}
while(!feof(fp)) {
ret = fscanf(fp, "%p %c %s", &address, &symbol, function);
if (ret != 3) {
break;
}
if (symbol_address == address) {
fclose(fp);
return strdup(function);
}
}
fclose(fp);
return NULL;
}