summaryrefslogtreecommitdiffstats
path: root/arch/x86_64/elf_load.c
blob: d7d1b29c2e22954702bc8e2d907c6516ac61d672 (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
56
57
58
#include "arch/elf.h"
#include "string.h"
#include "memory.h"
#include "print.h"

uintptr_t
elf_load(const void *data, size_t len)
{
    if(data == NULL) return 0;
    if(len < 4) return 0;

    elf_header_t *ehdr = (elf_header_t*)data;
    if(ehdr->ei_mag[0] != EI_MAG0 ||
       ehdr->ei_mag[1] != EI_MAG1 ||
       ehdr->ei_mag[2] != EI_MAG2 ||
       ehdr->ei_mag[3] != EI_MAG3) return 0;

    if(ehdr->ei_class != EI_CLASS_64) {
        kerrf("Jove does not support ELF files of class CLASS32\n");
        return 0;
    }

    if(ehdr->e_type != ET_EXEC) {
        kerrf("Jove does not support ELF files other than ET_EXEC\n");
        return 0;
    }

    uint64_t entry = ehdr->e_entry;
    
    size_t phdrc = ehdr->e_phnum;
    elf_phdr_t *phdrs = (elf_phdr_t*)((uintptr_t)data + ehdr->e_phoff);
    
    for(size_t phdri = 0; phdri < phdrc; phdri++)
    {
        elf_phdr_t *phdr = &phdrs[phdri];
        void *pdata = (void*)phdr->p_vaddr;
            
        vm_ensure(
                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)
        {
            kdbgf("PT_LOAD %#016X [%#016X] -> %#016X\n",
                    (uintptr_t)data + phdr->p_offset,
                    phdr->p_filesz,
                    pdata);
            memcpy(pdata, (void*)((uintptr_t)data + phdr->p_offset), phdr->p_filesz);
        }
    }
    return entry;
}