Hello, I was able to get the JamesM kernel tutorial completely, and I've a full Initrd which is loaded to memory.
now I've the executable file at 0x116834, and I parsed the ELF header to find the entry address which is at 0x100000.
I know I should jump to that address and I did use Fork and so on... but I don't know how can I get the real code address in this file as it's different to the memory.
thanks, and sorry if it's a noob question, I'm trying to learn about all these stuff using tutorials and so on.. )))
[Q] Executing ELF File From RAM.
Re: [Q] Executing ELF File From RAM.
You should parse the program headers as well. Those headers give information about parts of an ELF which should be loaded into the address space (e.g, a segment with type LOAD should be loaded at the virtual address in its corresponding program header).
a simple loader may look like:
provided that we're in the right address space.
a simple loader may look like:
Code: Select all
static
void write_elf_segment(elf32_program_hdr_t *phdr, uint_t image_addr) {
memcpy((void *) (phdr->p_vaddr),
(void *) (image_addr + phdr->p_offset),
phdr->p_filesz);
}
int elf32_load(uint_t image_addr) {
elf32_hdr_t *hdr = (elf32_hdr_t *) image_addr;
if (hdr->e_ident[0] != 0x7F &&
hdr->e_ident[1] != 'E' &&
hdr->e_ident[2] != 'L' &&
hdr->e_ident[3] != 'F')
return -1;
elf32_program_hdr_t *phaddr =
(elf32_program_hdr_t *) (image_addr + hdr->e_phoff);
uint_t idx;
for (idx = 0; idx < hdr->e_phnum; idx++) {
if (phaddr[idx].p_type == PT_LOAD) {
write_elf_segment(&phaddr[idx], image_addr);
if (phaddr[idx].p_filesz < phaddr[idx].p_memsz) {
memset((void *) (phaddr[idx].p_vaddr + phaddr[idx].p_filesz),
0,
phaddr[idx].p_memsz - phaddr[idx].p_filesz);
}
}
}
return 0;
}