Load a program

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
roelforg
Posts: 22
Joined: Mon Aug 08, 2011 6:27 am

Load a program

Post 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.
Image

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!
User avatar
Combuster
Member
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

Post 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?
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
roelforg
Posts: 22
Joined: Mon Aug 08, 2011 6:27 am

Re: Load a program

Post by roelforg »

Answers:
:arrow: I just wanted to know how you get from just having the file in mem to executing its code. :?
:arrow: 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 :mrgreen: , i just give the same values as a fsread would do :roll: .
Image

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!
User avatar
LegendDairy
Member
Member
Posts: 52
Joined: Sat Nov 06, 2010 10:42 am
Location: Antwerp (Belgium)

Re: Load a program

Post by LegendDairy »

roelforg wrote:Answers:
:arrow: I just wanted to know how you get from just having the file in mem to executing its code. :?
:arrow: 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 :mrgreen: , i just give the same values as a fsread would do :roll: .
[Irony alert] :roll:
If there was just an assembly instruction to execute an address in the RAM...
[/Irony]
User avatar
piranha
Member
Member
Posts: 1391
Joined: Thu Dec 21, 2006 7:42 pm
Location: Unknown. Momentum is pretty certain, however.
Contact:

Re: Load a program

Post 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
SeaOS: Adding VT-x, networking, and ARM support
dbittman on IRC, @danielbittman on twitter
https://dbittman.github.io
User avatar
zity
Member
Member
Posts: 99
Joined: Mon Jul 13, 2009 5:52 am
Location: Denmark

Re: Load a program

Post 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.
User avatar
Jezze
Member
Member
Posts: 395
Joined: Thu Jul 26, 2007 1:53 am
Libera.chat IRC: jfu
Contact:

Re: Load a program

Post 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).
Fudge - Simplicity, clarity and speed.
http://github.com/Jezze/fudge/
roelforg
Posts: 22
Joined: Mon Aug 08, 2011 6:27 am

Re: Load a program

Post 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 :mrgreen:

@mods: topic solved
Image

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!
roelforg
Posts: 22
Joined: Mon Aug 08, 2011 6:27 am

Re: Load a program

Post by roelforg »

got it working,
thx
Image

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!
Post Reply