Page 1 of 1
Load a program
Posted: Mon Aug 08, 2011 10:46 am
by roelforg
Hey all!
I was wondering,
How do you load and start a ELF program?
I'm having a hard time understanding the (little) documentation.
Re: Load a program
Posted: Mon Aug 08, 2011 11:12 am
by Combuster
roelforg wrote:it's almost done, the biggest problem is that i can't get initrd working.
Keeping up appearances? You'll be earning more karma here by being honest...
On topic: I use 400 lines of assembly, which is more than what is strictly necessary to get the job done. Little documentation does not mean at all that it is insufficient:
The official specification is just one document and tells you everything you will need for now. If you want examples, get an ELF file and put a hexdump on one half of the screen, put the specification on the other half and then pretend
you are the program. You learn a lot by just doing things and seeing where you get stuck instead of seeing some mental block and giving up because of it.
On another note, the program loader depends on a working storage implementation. Since the latter is known to be broken, why are you trying to build on top of quicksand?
Re: Load a program
Posted: Mon Aug 08, 2011 11:51 am
by roelforg
Answers:
I just wanted to know how you get from just having the file in mem to executing its code.
I am after theory here, the docs/wiki say about putting in mem, but nothing on actually executing the code
, the putting in mem is done by taking a program which is hardcoded in a variable and loading it in mem
, i just give the same values as a fsread would do
.
Re: Load a program
Posted: Mon Aug 08, 2011 12:30 pm
by LegendDairy
roelforg wrote:Answers:
I just wanted to know how you get from just having the file in mem to executing its code.
I am after theory here, the docs/wiki say about putting in mem, but nothing on actually executing the code
, the putting in mem is done by taking a program which is hardcoded in a variable and loading it in mem
, i just give the same values as a fsread would do
.
[Irony alert]
If there was just an assembly instruction to execute an address in the RAM...
[/Irony]
Re: Load a program
Posted: Mon Aug 08, 2011 12:50 pm
by piranha
Actually, the loading of the program is when the OS parses the information inside the ELF headers that then tell you what parts of the file go where in memory, what else to load, what portions of memory should be zero'd, etc. And then you are given an entry point (which is a memory address).
-JL
Re: Load a program
Posted: Mon Aug 08, 2011 1:52 pm
by zity
Loading a static elf program is actually _very_ simple. About 10-15 lines of C code (and some structures as described in the specification) should be sufficient as long as we don't consider dynamic linking.
- Load program into memory
- The elf header points to a number of different headers. To write a simple loader you're only interested in the the program headers.
- There are different types of program headers. You're are only interested in the headers of type PT_LOAD. These headers (there might be one or more of type PT_LOAD) point to the actual code in the elf file.
- Copy the code from the program headers with type PT_LOAD into the correct memory locations (as specified by the program header).
- Jump to the entry point specified in the elf header.
If you read the specification, especially the part about sections, it shouldn't be that hard.
Re: Load a program
Posted: Mon Aug 08, 2011 4:40 pm
by Jezze
Like the previous poster said but I wan't to add that if you are using paging the physical and the virtual address is usually not the same so you need to set a correctly mapped page directory and page tables for the program you wan't to execute.
Because this might be tricky the first time you could just identity map the first four megabytes and then tell the compiler that the program should expect to exist at an exact address like 0x00300000 in memory (using -Ttext i think) and you can just copy the program to that address and execute it (but you need to offset it by the header size which for ELF usually is 4096 or else you'll be executing the header).
Re: Load a program
Posted: Thu Aug 11, 2011 5:17 pm
by roelforg
Thx everyone!!!
After a lot of reading and searching i programmed an api for elf exec.
Very ease:
Kernel:
-> fileaddr = fsread("test");
-> exec_elf(fileaddr, &entry);
-> (*entry)();
User land:
-> write code...
-> gcc-linux -c test.c -o test #linking is done by kernel
-> cp to initrd
Once i'm back behind my pc (i'm using my phone) i'll post some code
@mods: topic solved
Re: Load a program
Posted: Sat Aug 13, 2011 6:52 am
by roelforg
got it working,
thx