Added load_program() function

Builds program image from ELF file in memory
Fixed multiple definition error caused by constant in elf.h:
moved contant to elf.c
This commit is contained in:
2021-04-17 00:56:44 -05:00
parent 2d6fa0d163
commit 9630d0a396
2 changed files with 47 additions and 6 deletions

View File

@@ -1,10 +1,9 @@
#pragma once
#include "pageallocator.h"
#include "types/physaddr.h"
#include <stdint.h>
const uint32_t elf_magic_number = 0x464c457f;
enum elf_endianness_t
{
ELF_LITTLE_ENDIAN = 1,
@@ -107,11 +106,13 @@ struct elf_section_header_t
};
#if defined __i386__
static const elf_isa_t HOST_ISA = ELF_ISA_x86;
static const enum elf_isa_t HOST_ISA = ELF_ISA_x86;
#elif defined __x86_64__
static const elf_isa_t HOST_ISA = ELF_ISA_x86_64;
static const enum elf_isa_t HOST_ISA = ELF_ISA_x86_64;
#elif defined __arm__
static const elf_isa_t HOST_ISA = ELF_ISA_ARM;
static const enum elf_isa_t HOST_ISA = ELF_ISA_ARM;
#elif defined __aarch64__
static const elf_isa_t HOST_ISA = ELF_ISA_AARCH64;
static const enum elf_isa_t HOST_ISA = ELF_ISA_AARCH64;
#endif
int load_program(struct elf_file_header_t *elf, struct page_stack_t *page_stack);

40
src/elf.c Normal file
View File

@@ -0,0 +1,40 @@
#include "elf.h"
#include "pageallocator.h"
#include "mmgr.h"
#include "string.h"
#include "types/status.h"
const uint32_t elf_magic_number = 0x464c457f;
int load_program(struct elf_file_header_t *elf, struct page_stack_t *page_stack)
{
struct elf_program_header_t *program_header = (struct elf_program_header_t*)((void*)elf + elf->phoffset);
unsigned int count = elf->phcount;
while(count > 0)
{
if(program_header->type == ELF_LOAD)
{
void *d = program_header->vaddr, *s = (void*)elf + program_header->offset;
for(size_t n = 0; n < program_header->memsize; n += page_size)
{
physaddr_t page = reserve_page(page_stack);
if(page == S_OUT_OF_MEMORY)
{
return S_OUT_OF_MEMORY;
}
int status = map_page(page_stack, d, page, PAGE_RW | PAGE_USERMODE | PAGE_EXECUTABLE);
switch(status)
{
case S_OUT_OF_MEMORY:
return status;
case S_OUT_OF_BOUNDS:
return status;
case S_OK:
memcpy(d, s, page_size);
}
}
}
count--;
program_header = (struct elf_program_header_t*)((void*)program_header + elf->phsize);
}
}