Elf Parser

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
User avatar
balthasar
Member
Member
Posts: 30
Joined: Mon Mar 31, 2008 8:39 pm
Contact:

Elf Parser

Post by balthasar »

I need help with an ELF Parser. now i have a Grub module that is at a random address (due to the initrd i made is variable) and i dont know how to run the console i added as a grub module and call its entry function. i have in a stored variable the start and end addresses of the elf program and just need to figure out how to call the entry level.

all sources are here if you need any code http://code.google.com/p/ensemble/sourc ... #svn/trunk

Thanks :D
My OS: NasuTek Ensemble http://code.google.com/p/ensemble/
User avatar
NickJohnson
Member
Member
Posts: 1249
Joined: Tue Mar 24, 2009 8:11 pm
Location: Sunnyvale, California

Re: Elf Parser

Post by NickJohnson »

The structure of an ELF binary is actually quite simple, and a loader is usually only about 50 lines of C or so. Here are the ELF specs: http://www.skyfree.org/linux/references/ELF_Format.pdf. You only need to care about the loading section - all linking information can be ignored. For you, the beginning of the file will really be the offset from that start pointer you have.
User avatar
balthasar
Member
Member
Posts: 30
Joined: Mon Mar 31, 2008 8:39 pm
Contact:

Re: Elf Parser

Post by balthasar »

ok i implemented some code but it pagefaults and i dont know why. and my elf verifier is pulling invalid data as well saying its not an ELF executable but i did a printf of the memory area and got this (the (triangle)ELF is me printing that address of memory as string)
Image
so obviously the ELF Header is there, just my parser isnt correctly reading it

Sources in question Includes/Kernel/ELF.h and Kernel/ELF.cpp
manonthemoon
Member
Member
Posts: 65
Joined: Sat Jul 04, 2009 9:39 pm

Re: Elf Parser

Post by manonthemoon »

It looks like ident[] is of type unsigned long, but you're treating it like unsigned chars.
User avatar
balthasar
Member
Member
Posts: 30
Joined: Mon Mar 31, 2008 8:39 pm
Contact:

Re: Elf Parser

Post by balthasar »

yeah that fixed that issue so obviously i need to correct them as other then unsigned long (which is for some reason what i set things up to in structs)
My OS: NasuTek Ensemble http://code.google.com/p/ensemble/
pcmattman
Member
Member
Posts: 2566
Joined: Sun Jan 14, 2007 9:15 pm
Libera.chat IRC: miselin
Location: Sydney, Australia (I come from a land down under!)
Contact:

Re: Elf Parser

Post by pcmattman »

(which is for some reason what i set things up to in structs)
Read the spec - it tells you exactly how wide each element of the struct should be. Assuming you have at least a basic knowledge of your types, you should be able to make the structs with ease.
manonthemoon
Member
Member
Posts: 65
Joined: Sat Jul 04, 2009 9:39 pm

Re: Elf Parser

Post by manonthemoon »

Double check the link that NickJohnson posted above. All of your types are unsigned longs, but the header uses various types of different sizes.

You may need to use __attribute__((packed)) on the struct or else the compiler may align things and add padding, which will definitely mess things up.
User avatar
balthasar
Member
Member
Posts: 30
Joined: Mon Mar 31, 2008 8:39 pm
Contact:

Re: Elf Parser

Post by balthasar »

Ok all is fixed it was the headers and i figured out by reading the spec more thuroughly what the types were. jeeze i sometimes get lazy and thats not good in programming oses

now it executes the ELF Module thanks for all your help
My OS: NasuTek Ensemble http://code.google.com/p/ensemble/
User avatar
balthasar
Member
Member
Posts: 30
Joined: Mon Mar 31, 2008 8:39 pm
Contact:

Re: Elf Parser

Post by balthasar »

actually its not fixed :evil:

i forgot i commented out paging so when i reenabled paging this happened
Image

and also it seems when the ELF got copied it rewritten over data because those are not the strings how can i run the elf from where its already preexisting into memory like a in place execution without copying or am i thinking this wrong.
My OS: NasuTek Ensemble http://code.google.com/p/ensemble/
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: Elf Parser

Post by Combuster »

ELF describes a layout as it is intended to be, then packs all the data bits together. That means that in the normal case you will have to copy ELF sections to their intended locations, since the sections are not page aligned in the input file.

If you want to execute an ELF in place, you should know link it to exact that location. Since I don't know of ld being able to pull such a thing off, your best bet is to do relocation during load, but doing so is more complicated than a simple map_memory and copy.
"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 ]
Post Reply