-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.c
103 lines (85 loc) · 1.73 KB
/
main.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include <fcntl.h>
#include <gelf.h>
#include <libelf.h>
#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
int main(int argc, char **argv) {
Elf *elf;
Elf_Scn *scn = NULL;
GElf_Shdr shdr;
Elf_Data *data;
GElf_Sym sym;
int fd, ii, count;
long unsigned int iterator_print = 0;
if (argc == 1) {
printf("Please input the elf file path in args!\n");
return 1;
}
elf_version(EV_CURRENT);
fd = open(argv[1], O_RDONLY);
elf = elf_begin(fd, ELF_C_READ, NULL);
while ((scn = elf_nextscn(elf, scn)) != NULL) {
gelf_getshdr(scn, &shdr);
if (shdr.sh_type == SHT_SYMTAB) {
/* found a symbol table, go print it. */
break;
}
}
data = elf_getdata(scn, NULL);
count = shdr.sh_size / shdr.sh_entsize;
/* print the symbol names */
for (ii = 0; ii < count; ++ii) {
gelf_getsym(data, ii, &sym);
printf("%lu: ", iterator_print);
iterator_print++;
// value
printf("0x%.8lu", sym.st_value);
// size
printf(" 0x%.8lu", sym.st_size);
// type
switch (sym.st_info) {
case 0:
printf(" NOTY");
break;
case 1:
printf(" OBJT");
break;
case 2:
printf(" FUNC");
break;
case 3:
printf(" SECT");
break;
case 4:
printf(" FILE");
break;
default:
break;
}
// bind
switch (sym.st_info) {
case 0:
printf(" LOCL");
break;
case 1:
printf(" GLOB");
break;
case 2:
printf(" WEAK");
break;
case 3:
printf(" NUM");
break;
default:
break;
}
// name
printf(" %s", elf_strptr(elf, shdr.sh_link, sym.st_name));
printf("\n");
}
elf_end(elf);
close(fd);
return 0;
}