Page 1 of 1

Elf Parser

Posted: Sun Jul 26, 2009 1:04 pm
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

Re: Elf Parser

Posted: Sun Jul 26, 2009 1:12 pm
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.

Re: Elf Parser

Posted: Sun Jul 26, 2009 8:12 pm
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

Re: Elf Parser

Posted: Sun Jul 26, 2009 8:18 pm
by manonthemoon
It looks like ident[] is of type unsigned long, but you're treating it like unsigned chars.

Re: Elf Parser

Posted: Sun Jul 26, 2009 8:23 pm
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)

Re: Elf Parser

Posted: Sun Jul 26, 2009 8:24 pm
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.

Re: Elf Parser

Posted: Sun Jul 26, 2009 8:28 pm
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.

Re: Elf Parser

Posted: Sun Jul 26, 2009 8:56 pm
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

Re: Elf Parser

Posted: Sun Jul 26, 2009 9:28 pm
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.

Re: Elf Parser

Posted: Sat Aug 01, 2009 5:38 am
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.