Hey all!
I was wondering,
How do you load and start a ELF program?
I'm having a hard time understanding the (little) documentation.
Load a program
Load a program
Jokes:
Q. Why did the scarecrow got promoted?
A. Because he was OUTSTANDING in his field!
=====================
Q. What's blue and isn't heavy?
A. Lightblue!
- Combuster
- Member
- Posts: 9301
- Joined: Wed Oct 18, 2006 3:45 am
- Libera.chat IRC: [com]buster
- Location: On the balcony, where I can actually keep 1½m distance
- Contact:
Re: Load a program
Keeping up appearances? You'll be earning more karma here by being honest...roelforg wrote:it's almost done, the biggest problem is that i can't get initrd working.
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
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 .
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 .
Jokes:
Q. Why did the scarecrow got promoted?
A. Because he was OUTSTANDING in his field!
=====================
Q. What's blue and isn't heavy?
A. Lightblue!
- LegendDairy
- Member
- Posts: 52
- Joined: Sat Nov 06, 2010 10:42 am
- Location: Antwerp (Belgium)
Re: Load a program
[Irony alert]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 .
If there was just an assembly instruction to execute an address in the RAM...
[/Irony]
- piranha
- Member
- Posts: 1391
- Joined: Thu Dec 21, 2006 7:42 pm
- Location: Unknown. Momentum is pretty certain, however.
- Contact:
Re: Load a program
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
-JL
SeaOS: Adding VT-x, networking, and ARM support
dbittman on IRC, @danielbittman on twitter
https://dbittman.github.io
dbittman on IRC, @danielbittman on twitter
https://dbittman.github.io
Re: Load a program
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.
Re: Load a program
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).
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).
Fudge - Simplicity, clarity and speed.
http://github.com/Jezze/fudge/
http://github.com/Jezze/fudge/
Re: Load a program
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
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
Jokes:
Q. Why did the scarecrow got promoted?
A. Because he was OUTSTANDING in his field!
=====================
Q. What's blue and isn't heavy?
A. Lightblue!
Re: Load a program
got it working,
thx
thx
Jokes:
Q. Why did the scarecrow got promoted?
A. Because he was OUTSTANDING in his field!
=====================
Q. What's blue and isn't heavy?
A. Lightblue!