-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathelf-parser-main.c
More file actions
93 lines (75 loc) · 1.96 KB
/
elf-parser-main.c
File metadata and controls
93 lines (75 loc) · 1.96 KB
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
#include <elf-parser.h>
/* Main entry point of elf-parser */
int32_t main(int32_t argc, char *argv[])
{
int32_t fd;
Elf32_Ehdr eh; /* elf-header is fixed size */
if(argc!=2) {
printf("Usage: elf-parser <ELF-file>\n");
return 0;
}
fd = open(argv[1], O_RDONLY|O_SYNC);
if(fd<0) {
printf("Error %d Unable to open %s\n", fd, argv[1]);
return 0;
}
/* ELF header : at start of file */
read_elf_header(fd, &eh);
if(!is_ELF(eh)) {
return 0;
}
if(is64Bit(eh)){
Elf64_Ehdr eh64; /* elf-header is fixed size */
Elf64_Shdr* sh_tbl; /* section-header table is variable size */
read_elf_header64(fd, &eh64);
print_elf_header64(eh64);
/* Section header table : */
sh_tbl = malloc(eh64.e_shentsize * eh64.e_shnum);
if(!sh_tbl) {
printf("Failed to allocate %d bytes\n",
(eh64.e_shentsize * eh64.e_shnum));
}
read_section_header_table64(fd, eh64, sh_tbl);
print_section_headers64(fd, eh64, sh_tbl);
/* Symbol tables :
* sh_tbl[i].sh_type
* |`- SHT_SYMTAB
* `- SHT_DYNSYM
*/
print_symbols64(fd, eh64, sh_tbl);
/* Save .text section as text.S
*/
save_text_section64(fd, eh64, sh_tbl);
/* Disassemble .text section
* Logs asm instructions to stdout
* Currently supports ARMv7
*/
disassemble64(fd, eh64, sh_tbl);
} else{
Elf32_Shdr* sh_tbl; /* section-header table is variable size */
print_elf_header(eh);
/* Section header table : */
sh_tbl = malloc(eh.e_shentsize * eh.e_shnum);
if(!sh_tbl) {
printf("Failed to allocate %d bytes\n",
(eh.e_shentsize * eh.e_shnum));
}
read_section_header_table(fd, eh, sh_tbl);
print_section_headers(fd, eh, sh_tbl);
/* Symbol tables :
* sh_tbl[i].sh_type
* |`- SHT_SYMTAB
* `- SHT_DYNSYM
*/
print_symbols(fd, eh, sh_tbl);
/* Save .text section as text.S
*/
save_text_section(fd, eh, sh_tbl);
/* Disassemble .text section
* Logs asm instructions to stdout
* Currently supports ARMv7
*/
disassemble(fd, eh, sh_tbl);
}
return 0;
}