Page 1 of 1
ELF Files? Loading/Running :)
Posted: Tue Feb 10, 2015 12:46 pm
by deleted
Hello all,
I am attempting to add a module to my OS to load and execute a simple ELF file. Just a:
Kindof thing. Just for a little background, I am trying to read it from an INITRD. I can read the file in as needed.
1. How do I load them (in memory?)
2. What do I do? (Use my program as a parser to execute the code?)
3. What is the right way to do this?
Please point me in the right direction, tutorials, specs, old dust books, etc.
Thanks!
Re: ELF Files? Loading/Running :)
Posted: Tue Feb 10, 2015 2:33 pm
by max
Heyho
I'll assume that you have your virtual memory management & multitasking set up.
If you don't have virtual memory management, you should implement it before doing this.
What you do is:
- Read the specification, especially part 2
- Read the ELF header of the file & validate it. This header contains all the information you need for loading
- Set up an empty process/task
- Copy all "load segments" into memory: each of these segments has a virtual address (vaddr) where it should to be copied to, the amount of bytes that have to be copied (filesz) and the amount of memory that has to be zero'd (memsz), both based on the vaddr (read specification for more details)
- Set the instruction pointer of the task to start at the entry point specified in the program header (e_entry)
- Add the task to your scheduler
This should be appropriate for the start. In essence you load all "load" segments into memory and kick off the executable at it's entry point.
Greets,
Max
Re: ELF Files? Loading/Running :)
Posted: Tue Feb 10, 2015 2:48 pm
by sortie
You want the
System V ABI as the true standard of ELF. The trick is to parse the header and recognize it is an ELF. You then parse the program headers (not section headers!) and load each entry by allocating memory at the desired address and copying data from the file there, and zeroing the rest. Pretty simple. You locate the program entry point in the ELF header, and transfer execution there after setting up a stack.