This repository has been archived by the owner on Mar 10, 2022. It is now read-only.
forked from TakefiveInteractive/TedkOS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
elfconvert.c
312 lines (282 loc) · 9.15 KB
/
elfconvert.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
299
300
301
302
303
304
305
306
307
308
309
310
311
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <stdint.h>
//-------------------------------------------------------------------------
// Data declarations
typedef struct pheader_list_t {
struct pheader_list_t *prev;
struct pheader_list_t *next;
void *data;
} pheader_list_t;
typedef struct pheader_root_t {
struct pheader_list_t *head;
struct pheader_list_t *tail;
uint32_t data;
uint32_t count;
} pheader_root_t;
pheader_root_t pheader_list = { .head = &pheader_list, .tail = &pheader_list, .data = 0, .count = 0 };
char magic_2882[5] = { 0x7f, 0x45, 0x4c, 0x46 };
//-------------------------------------------------------------------------
// Function declarations
int32_t verify_magic(const char *s2);
int32_t print_usage();
int32_t verify_ident(char* a1);
void * read_program_header(int32_t fd, void* a2, int32_t a3);
int32_t parse_program_header(void *a1);
void * list_insert_after(pheader_root_t* a1, pheader_list_t* a2, void* a3);
void * list_insert_before(pheader_root_t* a1, pheader_list_t* a2, void* a3);
void * list_insert_at_head(pheader_root_t* a1, void* a2);
void * list_insert_at_tail(pheader_root_t* a1, void* a2);
typedef uint32_t _DWORD;
typedef uint16_t _WORD;
typedef uint8_t _BYTE;
typedef int16_t __int16;
#define DWORD uint32_t
#define WORD uint16_t
#define LOWORD(l) ((WORD)(l))
#define HIWORD(l) ((WORD)(((DWORD)(l) >> 16) & 0xFFFF))
#define LOBYTE(w) ((BYTE)(w))
#define HIBYTE(w) ((BYTE)(((WORD)(w) >> 8) & 0xFF))
int32_t inputbuf[128 * 1024 * 1024] = { };
//----- (08048674) --------------------------------------------------------
int main(int argc, char *argv[])
{
ssize_t v4; // eax@37
const char *v7; // [sp+4h] [bp-74h]@24
int32_t v8; // [sp+8h] [bp-70h]@13
int32_t v9; // [sp+Ch] [bp-6Ch]@24
char v10; // [sp+1Bh] [bp-5Dh]@24
int32_t v12; // [sp+34h] [bp-44h]@13
int32_t v13; // [sp+38h] [bp-40h]@13
int32_t v14; // [sp+3Ch] [bp-3Ch]@13
int32_t v15; // [sp+40h] [bp-38h]@13
int32_t v16; // [sp+44h] [bp-34h]@13
int32_t v17; // [sp+48h] [bp-30h]@13
int32_t v18; // [sp+4Ch] [bp-2Ch]@13
int32_t fd; // [sp+54h] [bp-24h]@4
int32_t v21; // [sp+58h] [bp-20h]@24
int32_t i; // [sp+5Ch] [bp-1Ch]@13
int32_t v23; // [sp+60h] [bp-18h]@14
pheader_list_t *j; // [sp+64h] [bp-14h]@24
void *ptr; // [sp+68h] [bp-10h]@25
size_t n; // [sp+6Ch] [bp-Ch]@36
size_t v27; // [sp+70h] [bp-8h]@25
size_t v28; // [sp+78h] [bp+0h]@29
if ( argc != 2 )
{
print_usage();
exit(1);
}
fd = open(argv[1], O_RDONLY);
if ( read(fd, inputbuf, 0x34u) != 52 )
{
perror("Reading elf header");
exit(1);
}
if ( verify_magic(inputbuf) )
{
fprintf(stderr, "%s is not an ELF file.\n", argv[1]);
exit(1);
}
if ( verify_ident(inputbuf) )
{
fprintf(stderr, "%s is not a compatible ELF file.\n", argv[1]);
exit(1);
}
v12 = inputbuf[0x18 / 4];
v13 = inputbuf[0x1C / 4];
v14 = inputbuf[0x20 / 4];
v15 = inputbuf[0x24 / 4];
v16 = inputbuf[0x28 / 4];
v17 = inputbuf[0x2A / 4];
v18 = inputbuf[0x2C / 4];
fprintf(stderr, "e_entry : 0x%08x\n", v12);
fprintf(stderr, "e_phoff : 0x%08x\n", v13);
fprintf(stderr, "e_shoff : 0x%08x\n", v14);
fprintf(stderr, "e_flags : 0x%08x\n", v15);
fprintf(stderr, "e_ehsize : 0x%08x\n", (uint16_t)v16);
fprintf(stderr, "e_phentsize: 0x%08x\n", HIWORD(v16));
fprintf(stderr, "e_phnum : 0x%08x\n", (uint16_t)v17);
fprintf(stderr, "e_shentsize: 0x%08x\n", HIWORD(v17));
v8 = (uint16_t)v18;
fprintf(stderr, "e_shnum : 0x%08x\n", (uint16_t)v18);
fprintf(stderr, "\n", v8);
for ( i = 0; (uint16_t)v17 > i; ++i )
{
void *header = read_program_header(fd, inputbuf, i);
if ( !header )
{
fprintf(stderr, "Error reading header %d, skipping...\n", i);
exit(1);
}
if ( parse_program_header(header) )
list_insert_at_tail(&pheader_list, header);
}
if ( !i )
{
fprintf(stderr, "No loadable segments found...\n");
exit(1);
}
char file[80] = { };
strcpy(file, argv[1]);
strcat(file, ".converted");
int outputFD = open(file, O_CREAT | O_TRUNC | O_SYNC | O_WRONLY,
S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
if (outputFD == -1) perror("open file failed");
for (j = pheader_list.head; j != &pheader_list; j = j->prev )
{
char *v23 = j->data;
v27 = *(_DWORD *)(v23 + 20);
ptr = malloc(v27);
if (!ptr)
{
fprintf(stderr, "Failed allocating size of %d\n", v27);
exit(1);
}
if ( lseek(fd, *(_DWORD *)(v23 + 4), 0) == -1 )
{
perror("lseek for pheader read");
exit(1);
}
if ( *(_DWORD *)(v23 + 16) )
{
v28 = *(_DWORD *)(v23 + 16);
if ( v28 != read(fd, ptr, v28) )
{
perror("pheader read");
exit(1);
}
}
if ( lseek(outputFD, *(_DWORD *)(v23 + 8) - 0x8048000, 0) == -1 )
{
perror("lseek for pheader write");
exit(1);
}
if ( *(_DWORD *)(v23 + 20) > *(_DWORD *)(v23 + 16) )
{
n = *(_DWORD *)(v23 + 20) - *(_DWORD *)(v23 + 16);
memset((char *)ptr + *(_DWORD *)(v23 + 16), 0, n);
}
v28 = *(_DWORD *)(v23 + 20);
v4 = write(outputFD, ptr, v28);
if ( v4 != *(_DWORD *)(v23 + 20) )
{
fprintf(stderr, "write returned %d\n", v4);
perror("pheader write");
exit(1);
}
free(ptr);
}
return 0;
}
//----- (08048B7E) --------------------------------------------------------
int32_t verify_magic(const char *s2)
{
return strncmp(magic_2882, s2, 4u);
}
//----- (08048BA2) --------------------------------------------------------
int32_t print_usage()
{
return fprintf(stderr, "Usage: elfconvert <exename>\n");
}
//----- (08048BBF) --------------------------------------------------------
int32_t verify_ident(char *a1)
{
int32_t v2; // [sp+0h] [bp-4h]@7
if ( *(_BYTE *)(a1 + 4) != 1
|| *(_BYTE *)(a1 + 5) != 1
|| *(_BYTE *)(a1 + 6) != 1
|| *(_WORD *)(a1 + 16) != 2
|| *(_WORD *)(a1 + 18) != 3
|| *(_DWORD *)(a1 + 20) != 1 )
v2 = -1;
else
v2 = 0;
return v2;
}
//----- (08048C20) --------------------------------------------------------
void * read_program_header(int32_t fd, void *input, int32_t idx)
{
void *v4; // [sp+14h] [bp-14h]@2
void *buf; // [sp+24h] [bp-4h]@3
if ( lseek(fd, *(_DWORD *)(input + 28) + idx * *(_WORD *)(input + 42), 0) == -1 )
{
perror("lseek to program header");
v4 = 0;
}
else
{
buf = malloc(0x20u);
if ( read(fd, buf, *(_WORD *)(input + 42)) == *(_WORD *)(input + 42) )
{
v4 = buf;
}
else
{
perror("read program header");
v4 = 0;
}
}
return v4;
}
//----- (08048CCD) --------------------------------------------------------
int32_t parse_program_header(void *a1)
{
int32_t v2; // [sp+14h] [bp-4h]@2
if ( *(_DWORD *)a1 == 1 )
{
fprintf(stderr, "p_type : 0x%08x\n", *(_DWORD *)a1);
fprintf(stderr, "p_offset: 0x%08x\n", *(_DWORD *)(a1 + 4));
fprintf(stderr, "p_vaddr : 0x%08x\n", *(_DWORD *)(a1 + 8));
fprintf(stderr, "p_paddr : 0x%08x\n", *(_DWORD *)(a1 + 12));
fprintf(stderr, "p_filesz: 0x%08x\n", *(_DWORD *)(a1 + 16));
fprintf(stderr, "p_memsz : 0x%08x\n", *(_DWORD *)(a1 + 20));
fprintf(stderr, "p_flags : 0x%08x\n", *(_DWORD *)(a1 + 24));
fprintf(stderr, "p_align : 0x%08x\n", *(_DWORD *)(a1 + 28));
fprintf(stderr, "\n");
v2 = *(_DWORD *)(a1 + 8);
}
else
{
v2 = 0;
}
return v2;
}
//----- (08048E0C) --------------------------------------------------------
void * list_insert_after(pheader_root_t* a1, pheader_list_t* element, void* data)
{
pheader_list_t *result; // eax@1
a1->count++;
result = malloc(sizeof(pheader_list_t));
result->data = data;
result->prev = element->prev;
result->next = element;
element->prev->next = result;
element->prev = result;
return result;
}
//----- (08048E64) --------------------------------------------------------
void * list_insert_before(pheader_root_t* a1, pheader_list_t* element, void* data)
{
pheader_list_t *result; // eax@1
a1->count++;
result = malloc(sizeof(pheader_list_t));
result->data = data;
result->prev = element;
result->next = element->next;
element->next->prev = result;
element->next = result;
return result;
}
//----- (08048F31) --------------------------------------------------------
void * list_insert_at_head(pheader_root_t* a1, void* a2)
{
return list_insert_after(a1, (pheader_list_t *)a1, a2);
}
//----- (08048F52) --------------------------------------------------------
void * list_insert_at_tail(pheader_root_t* a1, void* a2)
{
return list_insert_before(a1, (pheader_list_t *)a1, a2);
}