Skip to content

Commit

Permalink
Added somewhat working dynamic lib/.so file loading
Browse files Browse the repository at this point in the history
  • Loading branch information
GAMINGNOOBdev committed Jan 25, 2025
1 parent 175b611 commit d44e6a7
Show file tree
Hide file tree
Showing 6 changed files with 335 additions and 48 deletions.
42 changes: 1 addition & 41 deletions source/includes/executables/elf.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,44 +10,4 @@
*/
#include <basics.h>

#define elf_magic 0x464C457F // 0x7F, then 'ELF' in ASCII

typedef struct {
int32 magic;
int8 _class;
int8 data;
int8 version;
int8 osabi;
int8 abiversion;
int8 pad[7];
int16 type;
int16 machine;
int32 version_elf;
int64 entry;
int64 ph_offset;
int64 sh_offset;
int32 flags;
int16 header_size;
int16 ph_entry_size;
int16 ph_num;
int16 sh_entry_size;
int16 sh_num;
int16 sh_str_index;
} elf_x64_header;

/**
* @brief Unused yet!
*
*/
typedef struct {
int32 type;
int32 flags;
int64 offset;
int64 vaddr;
int64 paddr;
int64 filesz;
int64 memsz;
int64 align;
} elf_x64_program_header;

void execute_elf(int64* address);
void* elf_load_from_memory(void* file_base_address);
31 changes: 31 additions & 0 deletions source/includes/fdlfcn.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#ifndef __FDLFCN_H_
#define __FDLFCN_H_ 1

#include <elf.h>

typedef struct
{
void* address;
Elf64_Ehdr ehdr;
Elf64_Shdr* shdrs;
Elf64_Sym* symbols;
Elf64_Rela* relocations;

int symtab_index;
void* text_section_data;
void* data_section_data;
void* rodata_section_data;

Elf64_Shdr* text_section_header;
Elf64_Shdr* data_section_header;
Elf64_Shdr* rodata_section_header;
} fdlfcn_handle;

// immediately load sections into memory
#define FDL_IMMEDIATE 0

fdlfcn_handle* fdlopen(void* filedata, int flags);
void* fdlsym(fdlfcn_handle* handle, const char* symbol_name);
int fdlclose(fdlfcn_handle* handle);

#endif
1 change: 1 addition & 0 deletions source/includes/kernel.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
#include <ps2-mouse.h>
#include <image/targa.h>
#include <executables/fwde.h>
#include <fdlfcn.h>

/**
* @brief The memory address pointer where the kernel ends.
Expand Down
44 changes: 37 additions & 7 deletions source/kernel/C/executables/elf.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,46 @@
*
*/
#include <executables/elf.h>
#include <memory2.h>
#include <stdint.h>
#include <heap.h>
#include <elf.h>

void execute_elf(int64* address) {
elf_x64_header *header = (elf_x64_header*)address;

if (header->magic != elf_magic) {
error("Not a valid ELF file to load!", __FILE__);
void elf_load_program_header(Elf64_Phdr* program_header, void* file_base_addr)
{
if (program_header == NULL)
return;

Elf64_Word type = program_header->p_type;
printf("Program Header type: %x", type);
}

void* elf_load_from_memory(void* file_base_address)
{
if (file_base_address == NULL)
return NULL;

uint8_t* file_ptr = file_base_address;
Elf64_Ehdr header = {};
memcpy(&header, file_ptr, sizeof(Elf64_Ehdr));

if (memcmp(&header.e_ident[EI_MAG0], ELFMAG, SELFMAG) != 0 || header.e_ident[EI_CLASS] != ELFCLASS64 ||
header.e_ident[EI_DATA] != ELFDATA2LSB || header.e_type != ET_EXEC ||
header.e_machine != EM_X86_64 || header.e_version != EV_CURRENT)
{
error("Not a valid ELF file to load!", __FILE__);
return NULL;
}

void (*entry_point)() = (void (*)(void)) (int64*)&header->entry;
printf("Parsing ELF64 file with %d PHDRs\n", header.e_phnum);
file_ptr = file_ptr + header.e_phoff;
size_t program_headers_size = header.e_phnum * header.e_phentsize;
Elf64_Phdr* program_headers_start = (Elf64_Phdr*)file_ptr;
Elf64_Phdr* program_headers_end = (Elf64_Phdr*)(file_ptr + program_headers_size);
for (Elf64_Phdr* prog_header = program_headers_start;
(uint8_t*)prog_header < program_headers_end;
prog_header = (Elf64_Phdr*)(((uint8_t*)prog_header + header.e_phentsize)))
elf_load_program_header(prog_header, file_base_address);

entry_point();
return (void*)header.e_entry;
}
Loading

0 comments on commit d44e6a7

Please sign in to comment.