-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathelf.c
422 lines (335 loc) · 9.93 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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
// Simple ELF file parser.
//
// Copyright (c) 2016 DIKU.
// Copenhagen Liberal License - v0.6 - January 25, 2016.
//
// Authors (most recent contributors first):
// Oleksandr Shturmov <[email protected]>
// Written while reading the [ELFspec].
//
// [ELFspec] TIS Committee. Tool Interface Standard (TIS) Executable and
// Linking Format (ELF) Specification. May 1999. Retrieved from
// https://refspecs.linuxbase.org/elf/elf.pdf on November 19, 2015.
// Archived by WebCite® at http://www.webcitation.org/6d9VYmYxi.
#include "elf.h"
#include "mips32.h" // MIPS_RESERVE
#include <stdio.h>
#include <stdint.h>
#include <error.h> // error
#include <errno.h>
#include <stdlib.h> // exit
#include <string.h> // memset
#define EI_MAG0 (0)
#define EI_MAG1 (1)
#define EI_MAG2 (2)
#define EI_MAG3 (3)
#define EI_CLASS (4)
#define EI_DATA (5)
#define EI_NIDENT (16)
#define ELFCLASS32 (1)
#define ELFDATA2MSB (2)
#define ET_EXEC (2)
#define EM_MIPS (8)
typedef uint16_t Elf32_Half;
typedef uint32_t Elf32_Word;
typedef uint32_t Elf32_Addr;
typedef uint32_t Elf32_Off;
#define ET_EXEC (2)
struct ehdr {
unsigned char e_ident[EI_NIDENT];
Elf32_Half e_type;
Elf32_Half e_machine;
Elf32_Word e_version; // Not used.
Elf32_Addr e_entry;
Elf32_Off e_phoff;
Elf32_Off e_shoff; // Not used.
Elf32_Addr e_flags;
Elf32_Half e_ehsize; // Not used.
Elf32_Half e_phentsize;
Elf32_Half e_phnum;
Elf32_Half e_shentsize; // Not used.
Elf32_Half e_shnum; // Not used.
Elf32_Half e_shstrndx; // Not used.
};
struct elf_file {
FILE *stream;
struct ehdr ehdr;
};
#define PT_NULL (0x0)
#define PT_LOAD (0x1)
#define PT_NOTE (0x4)
#define PT_MIPS_ABIFLAGS (0x70000003)
struct p_header {
Elf32_Word p_type;
Elf32_Off p_offset;
Elf32_Addr p_vaddr;
Elf32_Addr p_paddr;
Elf32_Word p_filesz;
Elf32_Word p_memsz;
Elf32_Word p_flags; // Not used.
Elf32_Word p_align; // Not used.
};
static inline int
check_magic_number(struct ehdr *ehdr) {
if (
(ehdr->e_ident[EI_MAG0] != 0x7F) ||
(ehdr->e_ident[EI_MAG1] != 'E') ||
(ehdr->e_ident[EI_MAG2] != 'L') ||
(ehdr->e_ident[EI_MAG3] != 'F')) {
error(0, 0, "this doesn't look like an ELF-file.");
return ELF_ERROR_NOT_ELF;
}
return 0;
}
static inline int
check_elf_class_32(struct ehdr *ehdr) {
if (ehdr->e_ident[EI_CLASS] != ELFCLASS32) {
error(0, 0, "only 32-bit ELF is supported.");
return ELF_ERROR_NOT_SUPPORTED;
}
return 0;
}
static inline int
check_data_encoding(struct ehdr *ehdr) {
if (ehdr->e_ident[EI_DATA] != ELFDATA2MSB) {
error(0, 0, "only 2's complement, big endian is supported.");
return ELF_ERROR_NOT_SUPPORTED;
}
return 0;
}
static inline int
read_elf_ident(struct elf_file *file) {
struct ehdr *ehdr = &file->ehdr;
int retval = 0;
// It is straight-forward to read the ELF Identification. It is just an array
// of 16 bytes. So there is no trouble with endianness; that comes later.
if (fread(&ehdr->e_ident, sizeof(ehdr->e_ident), 1, file->stream) != 1) {
error(0, errno, "couldn't read ELF Identification");
return ELF_ERROR_IO_ERROR;
}
retval = check_magic_number(ehdr);
if (retval != 0) return retval;
retval = check_elf_class_32(ehdr);
if (retval != 0) return retval;
retval = check_data_encoding(ehdr);
if (retval != 0) return retval;
return 0;
}
// Cross-platform read of 2's complement, big endian, 32-bit half words.
static inline int
read_be_half_words(FILE *stream, uint16_t *dst, size_t n) {
unsigned char buf[2];
size_t i;
for (i = 0; i < n; ++i) {
if (fread(&buf, sizeof(buf), 1, stream) != 1) {
error(0, 0, "couldn't read ELF32 half word.");
return ELF_ERROR_IO_ERROR;
}
*(dst++) = (buf[0] << 8) | buf[1];
}
return 0;
}
// Cross-platform read of 2's complement, big endian, 32-bit full words.
static inline int
read_be_words(FILE *stream, uint32_t *dst, size_t n) {
unsigned char buf[4];
size_t i;
for (i = 0; i < n; ++i) {
if (fread(&buf, sizeof(buf), 1, stream) != 1) {
error(0, 0, "couldn't read ELF32 word.");
return ELF_ERROR_IO_ERROR;
}
*(dst++) = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
}
return 0;
}
static inline int
check_type(struct ehdr *ehdr) {
if (ehdr->e_type != ET_EXEC) {
error(0, 0, "only executables are supported.");
return ELF_ERROR_NOT_SUPPORTED;
}
return 0;
}
static inline int
check_machine(struct ehdr *ehdr) {
if (ehdr->e_machine != EM_MIPS) {
error(0, 0, "only MIPS RS3000 machine is supported.");
return ELF_ERROR_NOT_SUPPORTED;
}
return 0;
}
static inline int
read_type_and_machine(struct elf_file *file) {
struct ehdr *ehdr = &file->ehdr;
int retval = 0;
retval = read_be_half_words(file->stream, &ehdr->e_type, 2);
if (retval != 0) {
error(0, 0, "couldn't read type and machine.");
return retval;
}
retval = check_type(ehdr);
if (retval != 0) return retval;
retval = check_machine(ehdr);
if (retval != 0) return retval;
return 0;
}
static inline int
read_ehdr(struct elf_file *file) {
struct ehdr *ehdr = &file->ehdr;
int retval = 0;
retval = read_elf_ident(file);
if (retval != 0) {
error(0, 0, "couldn't read ELF ident array.");
return retval;
}
retval = read_type_and_machine(file);
if (retval != 0) {
error(0, 0, "couldn't read ELF type and machine.");
return retval;
}
retval = read_be_words(file->stream, &ehdr->e_version, 5);
if (retval != 0) {
error(0, 0, "couldn't read rest of ELF header.");
return retval;
}
retval = read_be_half_words(file-> stream, &ehdr->e_ehsize, 6);
if (retval != 0) {
error(0, 0, "couldn't read rest of ELF header.");
return retval;
}
return retval;
}
// Assumes first 6 words of p_header are set, i.e. p_type, p_offset, p_vaddr,
// p_paddr, p_filesz, p_memsz.
//
// After this function, stream will point to one past the end of the segment.
static inline int
copy_segment(FILE *stream, struct p_header *phdr,
unsigned char *mem, size_t memsz) {
uint32_t offset;
unsigned char *segmem;
offset = phdr->p_vaddr - MIPS_RESERVE;
if (memsz < offset + phdr->p_memsz) {
return ELF_ERROR_OUT_OF_MEM;
}
segmem = mem + offset;
// Zero out the memory before copying segment from file.
memset(segmem, 0, phdr->p_memsz);
if (fseek(stream, phdr->p_offset, SEEK_SET) != 0) {
error(0, 0, "couldn't seek to first byte of program segment.");
return ELF_ERROR_IO_ERROR;
}
if (fread(segmem, phdr->p_filesz, 1, stream) != 1) {
error(0, 0, "couldn't read program segment");
return ELF_ERROR_IO_ERROR;
}
printf("Hello %x %x\n", offset, segmem[0x19]);
return 0;
}
// Assumes stream points to the first byte of a program header entry.
// After this function, stream points to one past the end of the associated
// segment. Also, the first 6 words of the p_header will be set.
static inline int
copy_cur_segment_aux(FILE *stream, unsigned char *mem, size_t memsz) {
int retval = 0;
struct p_header phdr;
if (read_be_words(stream, &phdr.p_type, 6) != 0) {
error(0, 0, "couldn't read program header entry.");
return ELF_ERROR_IO_ERROR;
}
switch(phdr.p_type) {
case PT_NULL:
case PT_NOTE:
case PT_MIPS_ABIFLAGS:
// Naïvely skip the above.
break;
case PT_LOAD:
retval = copy_segment(stream, &phdr, mem, memsz);
break;
default:
error(0, 0, "unknown program header entry type 0x%x", phdr.p_type);
retval = ELF_ERROR_NOT_SUPPORTED;
break;
}
return retval;
}
// After this function, stream points to one past the end of the current program
// header entry. Also, the first 6 words of the p_header will be set.
static inline int
copy_cur_segment(struct elf_file *file, unsigned char *mem, size_t memsz) {
struct ehdr *ehdr = &file->ehdr;
long origin = 0;
int retval = 0;
origin = ftell(file->stream);
if (origin == -1) {
error(0, errno, "couldn't tell current position in file");
return ELF_ERROR_IO_ERROR;
}
retval = copy_cur_segment_aux(file->stream, mem, memsz);
// Restore stream to next program header entry (if any).
if (fseek(file->stream, origin + ehdr->e_phentsize, SEEK_SET) != 0) {
error(0, errno, "couldn't seek past program header entity");
return ELF_ERROR_IO_ERROR;
}
return retval;
}
// After this function, stream point to one past the end of the last program
// header entry.
static inline int
copy_all_segments(struct elf_file *file, unsigned char *mem, size_t memsz) {
struct ehdr *ehdr = &file->ehdr;
size_t i = 0;
int retval = 0;
if (fseek(file->stream, ehdr->e_phoff, SEEK_SET) != 0) {
error(0, errno, "couldn't seek to program header table");
return ELF_ERROR_IO_ERROR;
}
for (i = 0; i < ehdr->e_phnum; ++i) {
retval = copy_cur_segment(file, mem, memsz);
if (retval != 0) return retval;
}
return 0;
}
static inline int
elf_open(struct elf_file *file, const char *path) {
int retval = 0;
file->stream = fopen(path, "r");
if (file->stream == NULL) {
error(0, errno, "couldn't open file (%s) for reading", path);
return ELF_ERROR_IO_ERROR;
}
retval = read_ehdr(file);
if (retval != 0) {
error(0, errno, "couldn't read ELF header");
return retval;
}
return 0;
}
static inline int
elf_close(struct elf_file *file) {
if (fclose(file->stream) != 0) {
error(0, errno, "couldn't close opened file");
return ELF_ERROR_IO_ERROR;
}
file->stream = NULL;
return 0;
}
int
elf_dump(const char *path, uint32_t *entry,
unsigned char *mem, size_t memsz) {
struct elf_file file;
int retval = 0;
retval = elf_open(&file, path);
if (retval != 0) {
error(0, 0, "couldn't get started on the ELF file.");
return retval;
}
*entry = file.ehdr.e_entry;
retval = copy_all_segments(&file, mem, memsz);
if (retval != 0) {
error(0, 0, "couldn't read prog segments.");
return retval;
}
return elf_close(&file);
}