Parsing ELF Executable

If you are new to OS Development, plan on spending some time here first before going into the other forums.

Moderator:Moderators

Post Reply
Believer424
Posts:7
Joined:Sat Apr 24, 2010 4:19 am
Parsing ELF Executable

Post by Believer424 » Fri Oct 08, 2010 9:56 pm

So I'm using GCC and am having trouble parsing an ELF file (kernel) from stage2. Here is what I have so far for the parsing part:

Code: Select all

ParseELFImage:
                ; Check for signature -- SUCCEEDS
		mov ebx, dword [IMAGE_PMODE_BASE]
		mov eax, dword [ELFSignature]
		cmp eax, ebx
		jne FailureMagic
		
                ; Check for proper data encoding -- SUCCEEDS
		add ebx, 5
		cmp ebx, 0
		je FailureData
		
                ; Check for proper file size -- FAILS
		xor ebx, ebx
		mov bx, word [IMAGE_PMODE_BASE + 40]
		mov word [ImageSizeTest], bx
		mov bx, word [IMAGE_PMODE_BASE + 42]
		mov ax, word [IMAGE_PMODE_BASE + 44]
		mul bx
		mov bx, dx
		shl ebx, 16
		or bx, ax
		add dword [ImageSizeTest], ebx
		xor ebx, ebx
		mov bx, word [IMAGE_PMODE_BASE + 46]
		mov ax, word [IMAGE_PMODE_BASE + 48]
		mul bx
		mov bx, dx
		shl ebx, 16
		or bx, ax
		add dword [ImageSizeTest], ebx
		mov ebx, dword [ImageSizeTest]
		mov eax, dword [ImageSize]
		cmp ebx, eax
		jge FailureSizes
It's the third test I do that fails. Using the ELF specification: http://www.skyfree.org/linux/references/ELF_Format.pdf (Search for 1-3), I add up the ELF Header size + (Program Header Size * Number of Program Headers) + (Section Header Size * Number of Section Headers). Now I am aware that this isn't the full size of the file. But if all this combined is greater than ImageSize, then there is a major problem. Which is why I do jge, ebx being the combined size of all the headers. FailureSizes is just to print an error, so I know what failed.

I've relooked over my calculations plenty of times, and at first my offsets were way off, but I don't see it anymore.

pathos
Moderator
Posts:97
Joined:Thu Jan 10, 2008 6:43 pm
Location:USA

Re: Parsing ELF Executable

Post by pathos » Sat Oct 09, 2010 3:15 am

I might be able to help, but you'll have to wait until Monday, sorry. I won't be able to access the computer my code is on until then, but I loaded an ELF kernel recently, and maybe my code can help.

[edit]
Well, I just looked over what I had, and I don't think it will help. Let me ask you this, though: if you skip over the file size test, does the kernel still load?

Believer424
Posts:7
Joined:Sat Apr 24, 2010 4:19 am

Re: Parsing ELF Executable

Post by Believer424 » Mon Oct 11, 2010 8:14 pm

No, I move the virtual address of the entry value into ebp, then call ebp. But it reboots after that.

piyush.neo
Posts:6
Joined:Fri Jan 14, 2011 11:00 am

Re: Parsing ELF Executable

Post by piyush.neo » Sat Jan 29, 2011 9:48 am

@Believer424: Have you come up with solution yet? I have also completed tutorials upto 13 now in same position as yours...if you have figured it out then please post the procedure. Though i have not yet started with ELF yet just a word for your solution that keep in mind to compile with your program with static option

Code: Select all

gcc -static filename.c
to avoid using shared lib at runtime..

Post Reply