Skip to content

Commit

Permalink
Lecture 99 - Implementing Elf Loader - Part 6
Browse files Browse the repository at this point in the history
  • Loading branch information
nibblebits committed Dec 14, 2020
1 parent 4739084 commit 1f44a72
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 1 deletion.
6 changes: 6 additions & 0 deletions src/loader/formats/elfloader.c
Expand Up @@ -73,6 +73,12 @@ struct elf32_shdr* elf_section(struct elf_header* header, int index)
return &elf_sheader(header)[index];
}


void* elf_phdr_phys_address(struct elf_file* file, struct elf32_phdr* phdr)
{
return elf_memory(file)+phdr->p_offset;
}

char* elf_str_table(struct elf_header* header)
{
return (char*) header + elf_section(header, header->e_shstrndx)->sh_offset;
Expand Down
8 changes: 8 additions & 0 deletions src/loader/formats/elfloader.h
Expand Up @@ -48,4 +48,12 @@ void* elf_virtual_end(struct elf_file* file);
void* elf_phys_base(struct elf_file* file);
void* elf_phys_end(struct elf_file* file);

struct elf_header* elf_header(struct elf_file* file);
struct elf32_shdr* elf_sheader(struct elf_header* header);
void* elf_memory(struct elf_file* file);
struct elf32_phdr* elf_pheader(struct elf_header* header);
struct elf32_phdr* elf_program_header(struct elf_header* header, int index);
struct elf32_shdr* elf_section(struct elf_header* header, int index);
void* elf_phdr_phys_address(struct elf_file* file, struct elf32_phdr* phdr);

#endif
18 changes: 17 additions & 1 deletion src/task/process.c
Expand Up @@ -119,7 +119,23 @@ static int process_map_elf(struct process* process)
int res = 0;

struct elf_file* elf_file = process->elf_file;
res = paging_map_to(process->task->page_directory, paging_align_to_lower_page(elf_virtual_base(elf_file)), elf_phys_base(elf_file), paging_align_address(elf_phys_end(elf_file)), PAGING_IS_PRESENT | PAGING_ACCESS_FROM_ALL | PAGING_IS_WRITEABLE);
struct elf_header* header = elf_header(elf_file);
struct elf32_phdr* phdrs = elf_pheader(header);
for (int i = 0; i < header->e_phnum; i++)
{
struct elf32_phdr* phdr = &phdrs[i];
void* phdr_phys_address = elf_phdr_phys_address(elf_file, phdr);
int flags = PAGING_IS_PRESENT | PAGING_ACCESS_FROM_ALL;
if (phdr->p_flags & PF_W)
{
flags |= PAGING_IS_WRITEABLE;
}
res = paging_map_to(process->task->page_directory, paging_align_to_lower_page((void*)phdr->p_vaddr), paging_align_to_lower_page(phdr_phys_address), paging_align_address(phdr_phys_address+phdr->p_filesz), flags);
if (ISERR(res))
{
break;
}
}
return res;
}
int process_map_memory(struct process* process)
Expand Down
6 changes: 6 additions & 0 deletions src/task/task.c
Expand Up @@ -6,6 +6,7 @@
#include "memory/memory.h"
#include "string/string.h"
#include "memory/paging/paging.h"
#include "loader/formats/elfloader.h"
#include "idt/idt.h"

// The current task that is running
Expand Down Expand Up @@ -208,6 +209,11 @@ int task_init(struct task *task, struct process *process)
}

task->registers.ip = PEACHOS_PROGRAM_VIRTUAL_ADDRESS;
if (process->filetype == PROCESS_FILETYPE_ELF)
{
task->registers.ip = elf_header(process->elf_file)->e_entry;
}

task->registers.ss = USER_DATA_SEGMENT;
task->registers.cs = USER_CODE_SEGMENT;
task->registers.esp = PEACHOS_PROGRAM_VIRTUAL_STACK_ADDRESS_START;
Expand Down

0 comments on commit 1f44a72

Please sign in to comment.