I don't even know what to write at this point
This commit is contained in:
parent
df0ebbbbcd
commit
72a46bc7ce
12 changed files with 235 additions and 96 deletions
|
@ -28,7 +28,61 @@
|
|||
/* https://github.com/KevinAlavik/nekonix/blob/main/kernel/src/proc/elf.c */
|
||||
/* Thanks, Kevin <3 */
|
||||
|
||||
uint64_t elf_load(char *data, uintptr_t *pagemap)
|
||||
uintptr_t elf32_load(char *data, pagetable *pagemap)
|
||||
{
|
||||
(void)data;
|
||||
(void)pagemap;
|
||||
return 0;
|
||||
}
|
||||
|
||||
uintptr_t elf64_load(char *data, pagetable *pagemap)
|
||||
{
|
||||
struct elf_header *header = (struct elf_header *)data;
|
||||
struct elf_program_header *ph = (struct elf_program_header *)((uint8_t *)data + header->e_phoff);
|
||||
|
||||
uint64_t lowest = UINT64_MAX;
|
||||
uint64_t max_align = 0;
|
||||
|
||||
for (uint16_t i = 0; i < header->e_phnum; i++) {
|
||||
if (ph[i].p_type != PT_LOAD)
|
||||
continue;
|
||||
|
||||
if (ph[i].p_align > max_align) {
|
||||
max_align = ph[i].p_align;
|
||||
}
|
||||
}
|
||||
|
||||
for (uint16_t i = 0; i < header->e_phnum; i++) {
|
||||
if (ph[i].p_type != PT_LOAD)
|
||||
continue;
|
||||
|
||||
if ((ph[i].p_vaddr & (~(max_align - 1))) < lowest) {
|
||||
lowest = ph[i].p_vaddr & ~(max_align - 1);
|
||||
}
|
||||
|
||||
uint64_t flags = VMM_PRESENT;
|
||||
if (ph[i].p_flags & PF_W)
|
||||
flags |= VMM_WRITABLE;
|
||||
if (!(ph[i].p_flags & PF_X))
|
||||
flags |= VMM_NX;
|
||||
|
||||
debug("elf64_load(): phys=0x%llx, virt=0x%llx, size=%lu\n", ph[i].p_paddr, ph[i].p_vaddr, ph[i].p_filesz);
|
||||
|
||||
uint64_t phys = (uint64_t)mem_alloc(ph[i].p_memsz);
|
||||
if (!phys) {
|
||||
debug("elf64_load(): Out of memory\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
map_page(pagemap, ph[i].p_vaddr, phys, flags);
|
||||
memcpy((void*)ph[i].p_vaddr - lowest, data + ph[i].p_offset, ph[i].p_filesz);
|
||||
}
|
||||
|
||||
debug("elf64_load(): ELF loaded successfully, entry: 0x%llx\n", header->e_entry);
|
||||
return (uintptr_t)((uint8_t *)data + (header->e_entry - lowest));
|
||||
}
|
||||
|
||||
uintptr_t elf_load(char *data, pagetable *pagemap)
|
||||
{
|
||||
struct elf_header *header = (struct elf_header *)data;
|
||||
|
||||
|
@ -42,44 +96,14 @@ uint64_t elf_load(char *data, uintptr_t *pagemap)
|
|||
return 0;
|
||||
}
|
||||
|
||||
struct elf_program_header *ph = (struct elf_program_header *)(data + header->e_phoff);
|
||||
|
||||
for (uint16_t i = 0; i < header->e_phnum; i++) {
|
||||
if (ph[i].p_type != PT_LOAD)
|
||||
continue;
|
||||
|
||||
uint64_t vaddr_start = ALIGN_DOWN(ph[i].p_vaddr, PAGE_SIZE);
|
||||
uint64_t vaddr_end = ALIGN_UP(ph[i].p_vaddr + ph[i].p_memsz, PAGE_SIZE);
|
||||
uint64_t offset = ph[i].p_offset;
|
||||
|
||||
uint64_t flags = VMM_PRESENT;
|
||||
if (ph[i].p_flags & PF_W)
|
||||
flags |= VMM_WRITABLE;
|
||||
if (!(ph[i].p_flags & PF_X))
|
||||
flags |= VMM_NX;
|
||||
|
||||
for (uint64_t addr = vaddr_start; addr < vaddr_end; addr += PAGE_SIZE) {
|
||||
uint64_t phys = (uint64_t)mem_alloc(PAGE_SIZE);
|
||||
if (!phys) {
|
||||
debug("Out of physical memory");
|
||||
return 0;
|
||||
}
|
||||
|
||||
map_page(pagemap, addr, phys, flags);
|
||||
|
||||
uint64_t file_offset = offset + (addr - vaddr_start);
|
||||
if (file_offset < offset + ph[i].p_filesz) {
|
||||
uint64_t to_copy = PAGE_SIZE;
|
||||
if (file_offset + PAGE_SIZE > offset + ph[i].p_filesz)
|
||||
to_copy = offset + ph[i].p_filesz - file_offset;
|
||||
|
||||
memcpy((void *)phys, data + file_offset, to_copy);
|
||||
} else {
|
||||
memset((void *)phys, 0, PAGE_SIZE);
|
||||
}
|
||||
}
|
||||
if (header->e_machine == 20 ||
|
||||
header->e_machine == 3 ||
|
||||
header->e_machine == 40) {
|
||||
return elf32_load(data, pagemap);
|
||||
} else if (header->e_machine == 62) {
|
||||
return elf64_load(data, pagemap);
|
||||
}
|
||||
|
||||
debug("ELF loaded successfully, entry: 0x%lx", header->e_entry);
|
||||
return header->e_entry;
|
||||
debug("Unsupported ELF machine: %u", header->e_machine);
|
||||
return 0;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue