ELF program header doesn't load correctly although the file is loaded correctly
Posted: Sat Dec 27, 2025 6:15 am
I am trying to load an ELF executable right now but I have been unsuccessful despite my attempts at fixing the code. The program I have been trying to load is a basic statically linked C program. I have fixed a lot of bugs but it still doesn't work. It can locate the program's entry address and load the program headers but when it tries to start from the entry point, the EIP register just increments until it encounters a page that hasn't been mapped. The file is loaded to memory correctly as far as I checked. The problem occurs when I look at the memory address that the program has been loaded. It returns zero instead of 0xC6 (which is the value that is in the .text section of executable file). I have checked the memory address the file has been loaded plus the offset and it returns that value. I haven't loaded the section headers yet but as far as I know statically linked ELF executables can be loaded without loading the section headers. Thanks for any help
Here is the related code:
elf.c
Here is the related code:
elf.c
Code: Select all
#include <elf.h>
#include <kernel/mmu.h>
#include <stdlib.h>
void * memcpy(void * restrict dest, const void * restrict src, int n) {
asm volatile("cld; rep movsb"
: "=c"((int){0})
: "D"(dest), "S"(src), "c"(n)
: "flags", "memory");
return dest;
}
struct ElfHeader *parse_header(void *header_place){
return (struct ElfHeader *) header_place;
}
void load_program(void *file_start){
struct ElfHeader *header = parse_header(file_start);
if (header->magic != 0x464C457F) {
*((char *)(0xB8008)) = 'N';
return ;
}
for(int i = 0; i < header->prtaennu; ++i){
struct ProgramHeader *pheader = (struct ProgramHeader *)(file_start + header->program_header + i * header->prtaensi);
if(pheader->type == 1){
allocate_page(pheader->virtual_addr);
memcpy((void *)pheader->virtual_addr, (void *)(file_start + pheader->offset), pheader->memory_size);
}
}
int (*entry) (void) = (int (*)(void))(header->program_entry);
entry();
}