parse program header of elf file and use load sections

This commit is contained in:
rami 2024-10-03 22:13:13 -04:00
parent 6243b46072
commit 114468a6ad
9 changed files with 54 additions and 10 deletions

Binary file not shown.

BIN
hdd.img Normal file

Binary file not shown.

View File

@ -41,6 +41,30 @@ typedef struct {
Elf32_Word sh_entsize;
} section_t;
typedef struct {
Elf32_Word p_type;
Elf32_Off p_offset;
Elf32_Addr p_vaddr;
Elf32_Addr p_paddr;
Elf32_Word p_filesz;
Elf32_Word p_memsz;
Elf32_Word p_flags;
Elf32_Word p_align;
} phdr_t;
#define PT_NULL 0
#define PT_LOAD 1
#define PT_DYNAMIC 2
#define PT_INTERP 3
#define PT_NOTE 4
#define PT_SHLIB 5
#define PT_PHDR 6
#define PT_TLS 7
#define PT_LOOS 0x60000000
#define PT_HIOS 0x6fffffff
#define PT_LOPROC 0x70000000
#define PT_HIPROC 0x7fffffff
section_t *elf_find_section(elf_t *elf, const char *name);
#endif

6
include/kernel/ide.h Normal file
View File

@ -0,0 +1,6 @@
#ifndef HAZEL_IDE_H_
#define HAZEL_IDE_H_
void ide_init();
#endif

View File

@ -22,8 +22,8 @@ typedef struct {
uint32_t *mmap;
uint32_t mmap_size;
log_method_t log_method;
uint64_t ticks;
task_t *current_task;
volatile uint64_t ticks;
volatile task_t *current_task;
rsdt_t *rsdt;
} kernel_ctx_t;

5
kernel/ide.c Normal file
View File

@ -0,0 +1,5 @@
#include <kernel/ide.h>
void ide_init() {
}

View File

@ -7,10 +7,8 @@ extern kernel_ctx_t ctx;
//int task_init(task_t *task, uint32_t *img) {
int task_init(task_t *task, elf_t *elf) {
if (!elf) return 0;
uint8_t *s = (uint8_t *)mmap_find_first_free_block();
memcpy(s, elf, 0x1000);
if (!elf->e_phoff) return 0;
task->eip = elf->e_entry;
// Allocate a page for a stack
@ -31,9 +29,17 @@ int task_init(task_t *task, elf_t *elf) {
// Map remaining entries as non-present u/s
else page_dir[i] = 6;
}
phdr_t *phdr = (phdr_t *)((uint32_t)elf + elf->e_phoff);
for (int i = 0; i < elf->e_phnum; i++) {
if (phdr[i].p_type == PT_LOAD) {
uint8_t *s = (uint8_t *)mmap_find_first_free_block();
memcpy(s, (uint8_t *)elf + phdr[i].p_offset, phdr[i].p_filesz);
// Map the ELF image to its specified entry
page_dir[PAGE_DIR_INDEX(elf->e_entry)] = ((uint32_t)page_tab - KERNEL_VMA) | 7;
page_tab[PAGE_TAB_INDEX(elf->e_entry)] = ((uint32_t)s - KERNEL_VMA) | 7;
page_dir[PAGE_DIR_INDEX(phdr[i].p_vaddr)] = ((uint32_t)page_tab - KERNEL_VMA) | 7;
page_tab[PAGE_TAB_INDEX(phdr[i].p_vaddr)] = ((uint32_t)s - KERNEL_VMA) | 7;
}
}
return 1;
}

View File

@ -1,5 +1,7 @@
#include "../lib/strcmp.h"
static const char test[] = "Hello\n";
int strlen(const char *str) {
const char *s;
for (s = str; *s; s++)
@ -86,7 +88,8 @@ void print_int(int num){
void _start() {
char buf1[256] = {0};
puts("Welcome to the Hazel user shell!\n");
//puts("Welcome to the Hazel user shell!\n");
puts(test);
while (1) {
puts("$ ");
read(0, buf1, 256);