-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathelf.c
298 lines (238 loc) · 5.56 KB
/
elf.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
#include "retmas.h"
int getElf32(char *elfimage, machine32info *machineinfo, char **nametbl)
{
int i;
char buf[1024];
char *name_table = *nametbl;
#ifdef DEBUG
printf("%d\n", sizeof(Elf32_Ehdr));
#endif
Elf32_Ehdr *ia32 = (Elf32_Ehdr *)elfimage;
#ifdef DEBUG
printf("Entering elf header parsing mode.........\n");
#endif
// object type checking. only executable image is allowed
if(ia32->e_type != ET_EXEC)
{
#ifdef DEBUG
printf("Not a executable image.\n");
#endif
// return 0;
}
// magic number check
if
(!( ia32->e_ident[EI_MAG0]==ELFMAG0 &&
ia32->e_ident[EI_MAG1]==ELFMAG1 &&
ia32->e_ident[EI_MAG2]==ELFMAG2 &&
ia32->e_ident[EI_MAG3]==ELFMAG3
))
{
#ifdef DEBUG
printf("Wrong magic number.\n");
#endif
return 0;
}
// machine check - targetting only ia
if(ia32->e_machine != EM_386)
{
#ifdef DEBUG
printf("Wrong executable image : do not targeting Intel Architecture\n");
#endif
return 0;
}
// version check - only EV_CURRENT is allowed
if(ia32->e_version != EV_CURRENT)
{
#ifdef DEBUG
printf("Wrong ELF version.\n");
#endif
return 0;
}
// CPU type check. only 32bit object is allowed
if(ia32->e_ident[EI_CLASS] != ELFCLASS32)
{
#ifdef DEBUG
printf("Wrong executable. only 32bit object is allowed.\n");
#endif
return 0;
}
machineinfo->elfHeader = (Elf32_Ehdr *)elfimage;
// initialize structures
// section header and program header
machineinfo->sectionHeader = (Elf32_Shdr *)(elfimage + ia32->e_shoff);
machineinfo->programHeader = (Elf32_Phdr *)(elfimage + ia32->e_phoff);
// find string table
*nametbl = (char *)(elfimage + machineinfo->sectionHeader[ia32->e_shstrndx].sh_offset);
name_table = *nametbl;
#ifdef DEBUG
printf("phnum : %d\n", machineinfo->elfHeader->e_phnum);
// find symbol table
//printf("%s\n", symboltable);
#endif
// showing section header's informations
#ifdef DEBUG
printf("Section header's informations\n");
printf("type\t\t\toffset\tsize\tname");
for(i=0; i<ia32->e_shnum; i++)
{
switch(machineinfo->sectionHeader[i].sh_type)
{
case SHT_NULL:
printf("SHT_NULL\t\t\t");
break;
case SHT_PROGBITS:
printf("SHT_PROGBITS\t\t");
break;
case SHT_SYMTAB:
printf("SHT_SYMTAB\t\t");
break;
case SHT_STRTAB:
printf("SHT_STRTAB\t\t");
break;
case SHT_RELA:
printf("SHT_RELA\t\t\t");
break;
case SHT_HASH:
printf("SHT_HASH\t\t\t");
break;
case SHT_DYNAMIC:
printf("SHT_DYNAMIC\t\t");
break;
case SHT_NOTE:
printf("SHT_NOTE\t\t");
break;
case SHT_NOBITS:
printf("SHT_NOBITS\t\t");
break;
case SHT_REL:
printf("SHT_REL\t\t\t");
break;
case SHT_SHLIB:
printf("SHT_SHLIB\t\t");
break;
case SHT_DYNSYM:
printf("SHT_DYNSYM\t\t");
break;
case SHT_LOPROC:
printf("SHT_LOPROC\t\t");
break;
case SHT_HIPROC:
printf("SHT_HIPROC\t\t");
break;
case SHT_LOUSER:
printf("SHT_LOUSER\t\t");
break;
case SHT_HIUSER:
printf("SHT_HIUSER\t\t");
break;
default:
printf("Unrecognized type\t");
break;
}
printf("%d\t%d\t%s\n",
machineinfo->sectionHeader[i].sh_offset,
machineinfo->sectionHeader[i].sh_size,
name_table + machineinfo->sectionHeader[i].sh_name
);
}
#endif
// showing program header's informations
#ifdef DEBUG
printf("\nProgram header's informations\n");
printf("header name\toffset\t vaddr\t\t paddr\t\tfilesz\tmemsize\talign\tflags\n");
for(i=0; i<machineinfo->elfHeader->e_phnum; i++)
{
switch(machineinfo->programHeader[i].p_type)
{
case PT_NULL:
printf("PT_NULL\t\t");
break;
case PT_LOAD:
printf("PT_LOAD\t\t");
break;
case PT_DYNAMIC:
printf("PT_DYNAMIC\t");
break;
case PT_INTERP:
printf("PT_INTERP\t");
break;
case PT_NOTE:
printf("PT_NOTE\t\t");
break;
case PT_SHLIB:
printf("PT_SHLIB\t");
break;
case PT_PHDR:
printf("PT_PHDR\t\t");
break;
case PT_LOPROC:
printf("PT_LOPROC\t");
break;
case PT_TLS:
printf("PT_TLS\t\t");
break;
case PT_NUM:
printf("PT_NUM\t\t");
break;
case PT_LOOS:
printf("PT_LOOS\t\t");
break;
case PT_GNU_EH_FRAME:
printf("PT_GNU_EH_FRAME\t");
break;
case PT_GNU_STACK:
printf("PT_GNU_STACK\t");
break;
case PT_GNU_RELRO:
printf("PT_GNU_RELRO\t");
break;
/* sun specified values
case PT_LOSUNW:
printf("PT_LOSUNW\t");
break;
case PT_SUNWBSS:
printf("PT_SUNWBSS\t");
break;
case PT_SUNWSTACK:
printf("PT_SUNWSTACK\t");
break;
case PT_HISUNW:
printf("PT_HISUNW\t");
break;
case PT_HIOS:
printf("PT_HIOS\t");
break;
end of os-specific*/
case PT_HIPROC:
printf("PT_HIPROC\t");
break;
default:
printf("Unrecognizable type of program header\n");
}
printf("%d\t%10p\t%10p\t%d\t%d\t%d\t",
machineinfo->programHeader[i].p_offset,
machineinfo->programHeader[i].p_vaddr,
machineinfo->programHeader[i].p_paddr,
machineinfo->programHeader[i].p_filesz,
machineinfo->programHeader[i].p_memsz,
machineinfo->programHeader[i].p_align
);
if(machineinfo->programHeader[i].p_flags & PF_X)
printf("PF_X ");
if(machineinfo->programHeader[i].p_flags & PF_W)
printf("PF_W ");
if(machineinfo->programHeader[i].p_flags & PF_R)
printf("PF_R ");
if(machineinfo->programHeader[i].p_flags & PF_MASKOS)
printf("PF_MASKOS ");
if(machineinfo->programHeader[i].p_flags & PF_MASKPROC)
printf("PF_MASKPROC ");
printf("\n");
}
#endif
// all job finished successfully
#ifdef DEBUG
printf("Elf header parsing success.\n");
#endif
return 1;
}