-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.c
185 lines (161 loc) · 3.63 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <string.h>
#include <elf.h>
void dump_buffer(void* buffer){
}
void dump_elf_hdr(Elf64_Ehdr* elf_hdr){
if(elf_hdr == NULL){
return;
}
unsigned char *e_ident = elf_hdr->e_ident;
if(strncmp(e_ident,ELFMAG,SELFMAG) != 0){
printf("that isn't a elf file \n");
return;
}
switch(e_ident[EI_CLASS]){
case ELFCLASSNONE:{
break;
}
case ELFCLASS32:{
printf("32-bit objects \n");
break;
}
case ELFCLASS64:{
printf("64-bit objects \n");
break;
}
}
switch(e_ident[EI_DATA]){
case ELFDATANONE:{
printf("invalid data encoding \n");
break;
}
case ELFDATA2LSB:{
printf("2's complement,little endian \n");
break;
}
case ELFDATA2MSB:{
printf("2's complement,big endian \n");
break;
}
}
printf("e_ident[EI_VERSION] = %d \n",e_ident[EI_VERSION]);
printf("e_ident[EI_PAD] = %d \n",e_ident[EI_PAD]);
char* str_e_type = NULL;
switch(elf_hdr->e_type){
case ET_NONE:{
str_e_type = "no file type";
break;
}
case ET_REL:{
str_e_type = "relocatable file";
break;
}
case ET_EXEC:{
str_e_type = "executable file";
break;
}
case ET_DYN:{
str_e_type = "shared object file";
break;
}
case ET_CORE:{
str_e_type = "core file";
break;
}
case ET_LOPROC:
case ET_HIPROC:{
str_e_type = "processor-specific";
break;
}
}
char* str_e_machine = NULL;
switch(elf_hdr->e_machine){
case ET_NONE:{
str_e_machine = "no machine";
break;
}
case EM_M32:{
str_e_machine = "AT&T WE32100A";
break;
}
case EM_SPARC:{
str_e_machine = "SPARC";
break;
}
case EM_386:{
str_e_machine = "intel architecture";
break;
}
case EM_68K:{
str_e_machine = "motorola 68000";
break;
}
case EM_88K:{
str_e_machine = "motorola 88000";
break;
}
case EM_860:{
str_e_machine = "intel 80860";
break;
}
case EM_MIPS:{
str_e_machine = "MIPS RS3000 Big-Endian";
break;
}
/* case EM_MIPS_RS4_BE:{
str_e_machine = "MIPS RS4000 Big-Endian";
break;
}
*/
}
printf("elf_hdr->e_type = %s \n",str_e_type);
printf("elf_hdr->e_machine = %s \n",str_e_machine);
printf("elf_hdr->e_machine = %d \n",elf_hdr->e_machine);
printf("elf_hdr->e_version = %d \n",elf_hdr->e_version);
printf("elf_hdr->e_entry = %#x \n",elf_hdr->e_entry);
printf("elf_hdr->e_phoff = %d \n",elf_hdr->e_phoff);
printf("elf_hdr->e_shoff = %d \n",elf_hdr->e_shoff);
printf("elf_hdr->e_ehsize = %d \n",elf_hdr->e_ehsize);
printf("elf_hdr->e_phentsize = %d bytes \n",elf_hdr->e_phentsize);
printf("elf_hdr->e_phnum = %d \n",elf_hdr->e_phnum);
printf("elf_hdr->e_shentsize = %d bytes \n",elf_hdr->e_shentsize);
printf("elf_hdr->e_shnum = %d \n",elf_hdr->e_shnum);
//printf("elf_hdr->shstrndx = ");
}
void dump_elf_section(Elf64_Ehdr* elf_hdr){
Elf64_Shdr* shdr = (Elf64_Shdr*)((char*)elf_hdr + elf_hdr->e_shoff);
for(int i = 0;i < elf_hdr->e_shnum;i++){
printf("%d 's section name = %s \n",i,shdr->sh_name);
shdr = (Elf64_Shdr*)((char*)shdr + elf_hdr->e_shentsize);
}
}
int main(int argc,char* argv[]){
if(argc < 2){
printf("invalid elf file name \n");
exit(1);
}
char* elf_file_name = argv[1];
FILE* elf_file = NULL;
int file_len = 0;
if((elf_file = fopen(elf_file_name,"r")) == NULL){
perror(NULL);
exit(1);
}
fseek(elf_file,0,SEEK_END);
file_len = ftell(elf_file);
fseek(elf_file,0,SEEK_SET);
printf("file_len = %d \n",file_len);
char* buffer = (char*)malloc(file_len);
if(fread(buffer,file_len,1,elf_file) != 1){
perror(NULL);
exit(1);
}
Elf64_Ehdr* elf_hdr = (Elf64_Ehdr*)buffer;
dump_elf_hdr(elf_hdr);
dump_elf_section(elf_hdr);
free(buffer);
return 0;
}