forked from sysprog21/rv32emu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathelf.c
356 lines (303 loc) · 8.25 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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "elf.h"
#include "io.h"
enum {
EM_RISCV = 243,
};
enum {
ELFCLASS32 = 1,
};
enum {
PT_NULL = 0,
PT_LOAD = 1,
PT_DYNAMIC = 2,
PT_INTERP = 3,
PT_NOTE = 4,
PT_SHLIB = 5,
PT_PHDR = 6,
PT_TLS = 7,
};
enum {
STT_NOTYPE = 0,
STT_OBJECT = 1,
STT_FUNC = 2,
STT_SECTION = 3,
STT_FILE = 4,
STT_COMMON = 5,
STT_TLS = 6,
};
enum {
SHT_NULL = 0,
SHT_PROGBITS = 1,
SHT_SYMTAB = 2,
SHT_STRTAB = 3,
SHT_RELA = 4,
SHT_HASH = 5,
SHT_DYNAMIC = 6,
SHT_NOTE = 7,
SHT_NOBITS = 8,
SHT_REL = 9,
SHT_SHLIB = 10,
SHT_DYNSYM = 11,
SHT_NUM = 12,
SHT_LOPROC = 0x70000000,
SHT_HIPROC = 0x7fffffff,
SHT_LOUSER = 0x80000000,
SHT_HIUSER = 0xffffffff
};
#define ELF_ST_TYPE(x) (((unsigned int) x) & 0xf)
struct Elf32_Ehdr {
uint8_t e_ident[EI_NIDENT];
Elf32_Half e_type;
Elf32_Half e_machine;
Elf32_Word e_version;
Elf32_Addr e_entry;
Elf32_Off e_phoff;
Elf32_Off e_shoff;
Elf32_Word e_flags;
Elf32_Half e_ehsize;
Elf32_Half e_phentsize;
Elf32_Half e_phnum;
Elf32_Half e_shentsize;
Elf32_Half e_shnum;
Elf32_Half e_shstrndx;
};
struct Elf32_Phdr {
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;
Elf32_Word p_align;
};
struct Elf32_Shdr {
Elf32_Word sh_name;
Elf32_Word sh_type;
Elf32_Word sh_flags;
Elf32_Addr sh_addr;
Elf32_Off sh_offset;
Elf32_Word sh_size;
Elf32_Word sh_link;
Elf32_Word sh_info;
Elf32_Word sh_addralign;
Elf32_Word sh_entsize;
};
struct elf_internal {
const struct Elf32_Ehdr *hdr;
uint32_t raw_size;
uint8_t *raw_data;
/* symbol table map: uint32_t -> (const char *) */
c_map_t symbols;
};
#ifndef max
#define max(a, b) ((a) > (b) ? (a) : (b))
#endif
#ifndef min
#define min(a, b) ((a) < (b) ? (a) : (b))
#endif
elf_t *elf_new()
{
elf_t *e = malloc(sizeof(elf_t));
e->hdr = NULL;
e->raw_size = 0;
e->symbols = c_map_init(int, char *, c_map_cmp_uint);
e->raw_data = malloc(1);
return e;
}
void elf_delete(elf_t *e)
{
if (!e)
return;
c_map_delete(e->symbols);
free(e->raw_data);
free(e);
}
/* release a loaded ELF file */
static void release(elf_t *e)
{
free(e->raw_data);
e->raw_data = NULL;
e->raw_size = 0;
e->hdr = NULL;
}
/* check if the ELF file header is valid */
static bool is_valid(elf_t *e)
{
/* check for ELF magic */
if (e->hdr->e_ident[0] != 0x7f && e->hdr->e_ident[1] != 'E' &&
e->hdr->e_ident[2] != 'L' && e->hdr->e_ident[3] != 'F') {
return false;
}
/* must be 32bit ELF */
if (e->hdr->e_ident[EI_CLASS] != ELFCLASS32)
return false;
/* check if machine type is RISC-V */
if (e->hdr->e_machine != EM_RISCV)
return false;
return true;
}
/* get section header string table */
static const char *get_sh_string(elf_t *e, int index)
{
uint32_t offset =
e->hdr->e_shoff + e->hdr->e_shstrndx * e->hdr->e_shentsize;
const struct Elf32_Shdr *shdr =
(const struct Elf32_Shdr *) (e->raw_data + offset);
return (const char *) (e->raw_data + shdr->sh_offset + index);
}
/* get a section header */
static const struct Elf32_Shdr *get_section_header(elf_t *e, const char *name)
{
for (int s = 0; s < e->hdr->e_shnum; ++s) {
uint32_t offset = e->hdr->e_shoff + s * e->hdr->e_shentsize;
const struct Elf32_Shdr *shdr =
(const struct Elf32_Shdr *) (e->raw_data + offset);
const char *sname = get_sh_string(e, shdr->sh_name);
if (!strcmp(name, sname))
return shdr;
}
return NULL;
}
/* get the ELF string table */
static const char *get_strtab(elf_t *e)
{
const struct Elf32_Shdr *shdr = get_section_header(e, ".strtab");
if (!shdr)
return NULL;
return (const char *) (e->raw_data + shdr->sh_offset);
}
/* find a symbol entry */
const struct Elf32_Sym *elf_get_symbol(elf_t *e, const char *name)
{
const char *strtab = get_strtab(e); /* get the string table */
if (!strtab)
return NULL;
/* get the symbol table */
const struct Elf32_Shdr *shdr = get_section_header(e, ".symtab");
if (!shdr)
return NULL;
/* find symbol table range */
const struct Elf32_Sym *sym =
(const struct Elf32_Sym *) (e->raw_data + shdr->sh_offset);
const struct Elf32_Sym *end =
(const struct Elf32_Sym *) (e->raw_data + shdr->sh_offset +
shdr->sh_size);
for (; sym < end; ++sym) { /* try to find the symbol */
const char *sym_name = strtab + sym->st_name;
if (!strcmp(name, sym_name))
return sym;
}
/* no symbol found */
return NULL;
}
static void fill_symbols(elf_t *e)
{
/* initialize the symbol table */
c_map_clear(e->symbols);
c_map_insert(e->symbols, &(int){0}, &(char *){NULL});
/* get the string table */
const char *strtab = get_strtab(e);
if (!strtab)
return;
/* get the symbol table */
const struct Elf32_Shdr *shdr = get_section_header(e, ".symtab");
if (!shdr)
return;
/* find symbol table range */
const struct Elf32_Sym *sym =
(const struct Elf32_Sym *) (e->raw_data + shdr->sh_offset);
const struct Elf32_Sym *end =
(const struct Elf32_Sym *) (e->raw_data + shdr->sh_offset +
shdr->sh_size);
for (; sym < end; ++sym) { /* try to find the symbol */
const char *sym_name = strtab + sym->st_name;
switch (ELF_ST_TYPE(sym->st_info)) { /* add to the symbol table */
case STT_NOTYPE:
case STT_OBJECT:
case STT_FUNC:
c_map_insert(e->symbols, (void *) &(sym->st_value), &sym_name);
}
}
}
const char *elf_find_symbol(elf_t *e, uint32_t addr)
{
if (c_map_empty(e->symbols))
fill_symbols(e);
c_map_iter_t it;
c_map_find(e->symbols, &it, &addr);
return c_map_at_end(e->symbols, &it) ? NULL : c_map_iter_value(&it, char *);
}
bool elf_get_data_section_range(elf_t *e, uint32_t *start, uint32_t *end)
{
const struct Elf32_Shdr *shdr = get_section_header(e, ".data");
if (!shdr)
return false;
if (shdr->sh_type == SHT_NOBITS)
return false;
*start = shdr->sh_addr;
*end = *start + shdr->sh_size;
return true;
}
bool elf_load(elf_t *e, struct riscv_t *rv, memory_t *mem)
{
rv_set_pc(rv, e->hdr->e_entry); /* set the entry point */
/* loop over all of the program headers */
for (int p = 0; p < e->hdr->e_phnum; ++p) {
/* find next program header */
uint32_t offset = e->hdr->e_phoff + (p * e->hdr->e_phentsize);
const struct Elf32_Phdr *phdr =
(const struct Elf32_Phdr *) (e->raw_data + offset);
/* check this section should be loaded */
if (phdr->p_type != PT_LOAD)
continue;
/* memcpy required range */
const int to_copy = min(phdr->p_memsz, phdr->p_filesz);
if (to_copy)
memory_write(mem, phdr->p_vaddr, e->raw_data + phdr->p_offset,
to_copy);
/* zero fill required range */
const int to_zero = max(phdr->p_memsz, phdr->p_filesz) - to_copy;
if (to_zero)
memory_fill(mem, phdr->p_vaddr + to_copy, to_zero, 0);
}
return true;
}
bool elf_open(elf_t *e, const char *path)
{
/* free previous memory */
if (e->raw_data)
release(e);
FILE *f = fopen(path, "rb");
if (!f)
return false;
/* get file size */
fseek(f, 0, SEEK_END);
e->raw_size = ftell(f);
fseek(f, 0, SEEK_SET);
if (e->raw_size == 0) {
fclose(f);
return false;
}
/* allocate memory */
free(e->raw_data);
e->raw_data = malloc(e->raw_size);
/* read data into memory */
const size_t r = fread(e->raw_data, 1, e->raw_size, f);
fclose(f);
if (r != e->raw_size) {
release(e);
return false;
}
/* point to the header */
e->hdr = (const struct Elf32_Ehdr *) e->raw_data;
/* check it is a valid ELF file */
if (!is_valid(e)) {
release(e);
return false;
}
return true;
}