blob: 969cbf0df567c81a743f4d2ec306b1306445e90a (
plain) (
blame)
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
|
#include "elf.h"
#include "lib/string.h"
#include "usr/elf.h"
#include "io/log.h"
#include "mem/memory.h"
void*
elf_load(const void *data, size_t len)
{
if(data == NULL) return NULL;
if(len < 4) return NULL;
struct ELF_header *ehdr = (struct ELF_header*)data;
if(ehdr->e_ident[EI_MAG0] != E_IDENT_MAG0 &&
ehdr->e_ident[EI_MAG1] != E_IDENT_MAG1 &&
ehdr->e_ident[EI_MAG2] != E_IDENT_MAG2 &&
ehdr->e_ident[EI_MAG3] != E_IDENT_MAG3)
return NULL;
if(ehdr->e_ident[EI_CLASS] != E_IDENT_CLASS64) {
klogf("Jove does not support ELF files of class CLASS32\n");
return NULL;
}
if(ehdr->e_type != ET_EXEC) {
klogf("Jove does not support ELF files other than ET_EXEC\n");
return NULL;
}
uint64_t entry = ehdr->e_entry;
size_t phdrc = ehdr->e_phnum;
struct ELF_phdr *phdrs = (struct ELF_phdr*)((uintptr_t)data + ehdr->e_phoff);
for(size_t phdri = 0; phdri < phdrc; phdri++)
{
struct ELF_phdr *phdr = &phdrs[phdri];
void *pdata = (void*)phdr->p_vaddr;
mem_ensure_range(
phdr->p_vaddr,
phdr->p_vaddr + phdr->p_memsz,
(page_flags_t) {
.present = true,
.writeable = true,
.useraccess = true,
.executable = true
});
if(phdr->p_type == PT_LOAD)
{
memcpy(pdata, (void*)((uintptr_t)data + phdr->p_offset), phdr->p_filesz);
}
}
return (void*)entry;
}
|