ELF program header doesn't load correctly although the file is loaded correctly

Programming, for all ages and all languages.
Post Reply
yucarp
Member
Member
Posts: 39
Joined: Fri Dec 05, 2025 5:19 pm
Libera.chat IRC: yucarp

ELF program header doesn't load correctly although the file is loaded correctly

Post by yucarp »

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

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();
}
nullplan
Member
Member
Posts: 2034
Joined: Wed Aug 30, 2017 8:24 am

Re: ELF program header doesn't load correctly although the file is loaded correctly

Post by nullplan »

Right off the bat I see issues: Your memcpy function changes the values of ESI, EDI, and ECX, but doesn't tell the compiler that. Easiest to fix that would be

Code: Select all

void *memcpy(void *restrict dest, const void *restrict src, int n) {
  void *rv = dest;
  asm volatile("rep movsb" : "+c"(n), "+D"(dest), "+S"(src) :: "cc", "memory");
  return rv;
}
Second issue: That count variable should be of type size_t.

This might be it. Next issue is you only allocate a single page per segment. Is that enough? And is the address space free at those locations? You are writing directly to the VGA area, so I'm guessing you run without paging. So the question is: Are the locations mentioned in the ELF file normal RAM or something else?
Carpe diem!
yucarp
Member
Member
Posts: 39
Joined: Fri Dec 05, 2025 5:19 pm
Libera.chat IRC: yucarp

Re: ELF program header doesn't load correctly although the file is loaded correctly

Post by yucarp »

I found the problem. I have been identity paging the section. Since that memory address doesn't exist physically, it can't write to or read from that address. After changing the code to physically map the address, it worked. I guess I have to improve my paging code...
Post Reply