-
Notifications
You must be signed in to change notification settings - Fork 50
/
elf.c
326 lines (263 loc) · 8.8 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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
#include <assert.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "elf.h"
#if __SIZEOF_POINTER__ == __SIZEOF_INT__
struct __attribute__((__packed__)) _elf32_hdr_entry {
uint8_t e_ident[16];
uint16_t e_type;
uint16_t e_machine;
uint32_t e_version;
uint32_t e_entry;
uint32_t e_phoff;
uint32_t e_shoff;
uint32_t e_flags;
uint16_t e_ehsize;
uint16_t e_phentsize;
uint16_t e_phnum;
uint16_t e_shentsize;
uint16_t e_shnum;
uint16_t e_shstrndx;
};
typedef struct _elf32_hdr_entry elf_hdr_entry;
static const char elf_hdr_default[] = {
/* 00 */
0x7f, 0x45, 0x4c, 0x46, /* magic */
0x01, /* 32-bit */
0x01, /* little-endian */
0x01, /* original version of ELF */
0x00, /* System V */
/* 08 */
0x00, /* ABI Version */
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* Unused */
/* 10 */
0x02, 0x00, /* Executable */
0x00, 0x00, /* No specific instruction set */
0x01, 0x00, 0x00, 0x00, /* original version of ELF */
/* 18 */
0x00, 0x00, 0x00, 0x00, /* entry point */
0x34, 0x00, 0x00, 0x00, /* start of the program header table */
/* 20 */
0x00, 0x00, 0x00, 0x00, /* start of the section header table */
0x00, 0x00, 0x00, 0x00, /* not used by our VM currently */
/* 28 */
0x34, 0x00, /* size of this header */
0x00, 0x00, /* size of a program header table entry */
0x00, 0x00, /* number of entries in the program header table */
0x00, 0x00, /* size of a section header table entry */
/* 30 */
0x00, 0x00, /* number of entries in the section header table */
0x00, 0x00 /* index of the section header table entry that contains the
section names */
};
#else
struct __attribute__((__packed__)) _elf64_hdr_entry {
uint8_t e_ident[16];
uint16_t e_type;
uint16_t e_machine;
uint32_t e_version;
uint64_t e_entry;
uint64_t e_phoff;
uint64_t e_shoff;
uint32_t e_flags;
uint16_t e_ehsize;
uint16_t e_phentsize;
uint16_t e_phnum;
uint16_t e_shentsize;
uint16_t e_shnum;
uint16_t e_shstrndx;
};
typedef struct _elf64_hdr_entry elf_hdr_entry;
static const char elf_hdr_default[] = {
/* 00 */
0x7f, 0x45, 0x4c, 0x46, /* magic */
0x02, /* 64-bit */
0x01, /* little-endian */
0x01, /* original version of ELF */
0x00, /* System V */
/* 08 */
0x00, /* ABI Version */
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* Unused */
/* 10 */
0x02, 0x00, /* Executable */
0x00, 0x00, /* No specific instruction set */
0x01, 0x00, 0x00, 0x00, /* original version of ELF */
/* 18 */
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
/* 20 */
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, /* start of the section header table */
/* 28 */
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, /* not used by our VM currently */
/* 30 */
0x00, 0x00, 0x00, 0x00, /* not used by our VM currently */
0x34, 0x00, /* size of this header */
0x00, 0x00, /* size of a program header table entry */
/* 38 */
0x00, 0x00, /* number of entries in the program header table */
0x00, 0x00, /* size of a section header table entry */
0x00, 0x00, /* number of entries in the section header table */
0x00, 0x00 /* index of the section header table entry that contains the
section names */
};
#endif
#if __SIZEOF_POINTER__ == __SIZEOF_INT__
struct __attribute__((__packed__)) _elf32_prog_hdr_entry {
uint32_t p_type;
uint32_t p_offset;
uint32_t p_vaddr;
uint32_t p_paddr;
uint32_t p_filesz;
uint32_t p_memsz;
uint32_t p_flags;
uint32_t p_align;
};
typedef struct _elf32_prog_hdr_entry elf_prog_hdr_entry;
static const elf_prog_hdr_entry elf_prog_hdr_default = {.p_type = 1,
.p_offset = 0,
.p_vaddr = 0,
.p_paddr = 0,
.p_filesz = 0,
.p_memsz = 0,
.p_flags = 0,
.p_align = 32};
#else
struct __attribute__((__packed__)) _elf64_prog_hdr_entry {
uint32_t p_type;
uint32_t p_flags;
uint64_t p_offset;
uint64_t p_vaddr;
uint64_t p_paddr;
uint64_t p_filesz;
uint64_t p_memsz;
uint64_t p_align;
};
typedef struct _elf64_prog_hdr_entry elf_prog_hdr_entry;
static const elf_prog_hdr_entry elf_prog_hdr_default = {.p_type = 1,
.p_flags = 0,
.p_offset = 0,
.p_vaddr = 0,
.p_paddr = 0,
.p_filesz = 0,
.p_memsz = 0,
.p_align = 32};
#endif
elf *elf_init(void)
{
elf *p;
assert(sizeof(elf_hdr_default) == ELF_HDR_SIZE);
p = malloc(sizeof(elf));
memcpy(p, elf_hdr_default, sizeof(elf_hdr_default));
return p;
}
void elf_new_prog_hdrs(elf *elf_info, unsigned short count)
{
elf_hdr_entry *file_hdr;
file_hdr = (elf_hdr_entry *) &elf_info->elf_hdr[0];
file_hdr->e_phnum = count;
file_hdr->e_phentsize = sizeof(elf_prog_hdr_entry);
elf_info->prog_hdrs = malloc(count * sizeof(elf_prog_hdr_entry));
assert(elf_info->prog_hdrs != NULL);
}
void elf_set_prog_hdr(elf *elf_info,
unsigned short idx,
unsigned int mem_size,
unsigned int file_size)
{
elf_hdr_entry *file_hdr;
elf_prog_hdr_entry *p;
file_hdr = (elf_hdr_entry *) &elf_info->elf_hdr[0];
assert(idx < file_hdr->e_phnum);
p = &(((elf_prog_hdr_entry *) (elf_info->prog_hdrs))[idx]);
memcpy(p, &elf_prog_hdr_default, sizeof(*p));
if (idx == 0) {
p->p_offset =
sizeof(elf_hdr_entry) + file_hdr->e_phnum * file_hdr->e_phentsize;
} else {
elf_prog_hdr_entry *last = p - 1;
p->p_offset = last->p_filesz + last->p_offset;
}
p->p_memsz = mem_size;
p->p_filesz = file_size;
}
int elf_write_file_hdr(int fd, elf *elf_info)
{
elf_hdr_entry *file_hdr;
int len;
int sz;
len = sz = write(fd, &elf_info->elf_hdr[0], ELF_HDR_SIZE);
if (sz < 0)
return sz;
file_hdr = (elf_hdr_entry *) &elf_info->elf_hdr[0];
if (file_hdr->e_phnum) {
sz = write(fd, elf_info->prog_hdrs,
file_hdr->e_phnum * file_hdr->e_phentsize);
if (sz < 0)
return sz;
len += sz;
}
return len;
}
int elf_write_prog_seg(elf *elf_info, int fd, unsigned short idx, void *mem)
{
elf_prog_hdr_entry *p;
int len = 0;
p = (elf_prog_hdr_entry *) elf_info->prog_hdrs;
if (p[idx].p_filesz)
len = write(fd, mem, p[idx].p_filesz);
return len;
}
elf *elf_read_file_hdr(int fd)
{
elf *e;
e = malloc(sizeof(*e));
if (!e)
return NULL;
if (read(fd, &e->elf_hdr[0], sizeof(e->elf_hdr)) < sizeof(e->elf_hdr)) {
free(e);
return NULL;
}
if (memcmp(&e->elf_hdr[0], &elf_hdr_default, 4)) {
free(e);
return NULL;
}
return e;
}
int elf_read_prog_hdrs(elf *e, int fd)
{
elf_hdr_entry *file_hdr;
int prog_hdrs_sz;
file_hdr = (elf_hdr_entry *) &e->elf_hdr[0];
if (file_hdr->e_phentsize != sizeof(elf_prog_hdr_entry))
return -1;
if (!file_hdr->e_phnum)
return 0;
prog_hdrs_sz = file_hdr->e_phnum * file_hdr->e_phentsize;
e->prog_hdrs = malloc(prog_hdrs_sz);
if (read(fd, e->prog_hdrs, prog_hdrs_sz) != prog_hdrs_sz) {
free(e->prog_hdrs);
e->prog_hdrs = NULL;
}
return file_hdr->e_phnum;
}
int elf_read_prog_seg(elf *e, int fd, int idx, char **mem, size_t *size)
{
elf_hdr_entry *file_hdr;
elf_prog_hdr_entry *p;
file_hdr = (elf_hdr_entry *) &e->elf_hdr[0];
if (idx >= file_hdr->e_phnum || !e->prog_hdrs)
return -1;
p = &((elf_prog_hdr_entry *) e->prog_hdrs)[idx];
if (!p->p_filesz)
return 0;
if (!(*mem = realloc(*mem, p->p_filesz)))
return -1;
if (read(fd, *mem, p->p_filesz) != p->p_filesz)
return -1;
*size = p->p_filesz;
return (int) p->p_filesz;
}