Loading an ELF-executable

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
kutkloon7
Member
Member
Posts: 98
Joined: Fri Jan 04, 2013 6:56 pm

Loading an ELF-executable

Post by kutkloon7 »

Hello,

I have implemented a simple 2-stage bootloader. My second stage bootloader is loaded at 0x500 and loads an executable ELF-file from there. This worked fine at the start, but now my kernel got bigger and I've reached the point where it overwrites my stage 2 bootloader (the code that is loading the ELF-file). When I inspect the elf binary with readelf -S, I can see the sections:

Image
For clarity's sake: the problem is the .symtab, as it wants to be loaded at 0x0 to 0x5c0 (while my bootloader is at 0x500), but the sections .strtab, .symtab and .shstrtab are all potential problems, as they might grow into the memory > 0x500 that I need for my second stage bootloader.

The behaviour of my second stage bootloader is correct. However, I was convinced the linker should only allocate sections in the memory > 1MB. I've grabbed the linker script from here(without really understanding it in the first place, so maybe this will finally teach me that those things always backfire :P ).

How can I make sure the .strtab, .symtab and .shstrtab sections aren't loaded to the memory where my bootloader is?
jnc100
Member
Member
Posts: 775
Joined: Mon Apr 09, 2007 12:10 pm
Location: London, UK
Contact:

Re: Loading an ELF-executable

Post by jnc100 »

Only those sections with the SHF_ALLOC flag ('A' in the readelf output) should actually be loaded. As you can see, these all have sensible load addresses (> 1MiB).

However, the best way to load executables is to use the program headers instead (readelf -l will list these).

Regards,
John.
kutkloon7
Member
Member
Posts: 98
Joined: Fri Jan 04, 2013 6:56 pm

Re: Loading an ELF-executable

Post by kutkloon7 »

Makes a lot of sense! Thank you sir.

Edit: I'm experiencing some odd troubles (my interrupt handler is executed but, for example, my clearscreen routine doesn't work and clears only the first character of the screen) when I load only the memory specified in the program headers. Loading only the sections where the allocation flag is set works fine.
User avatar
sortie
Member
Member
Posts: 931
Joined: Wed Mar 21, 2012 3:01 pm
Libera.chat IRC: sortie

Re: Loading an ELF-executable

Post by sortie »

Sections become segments after the linking process has been performed. *Never* look at the sections for program loading purposes. Use the program headers and look at the LOAD segment entries instead.
kutkloon7
Member
Member
Posts: 98
Joined: Fri Jan 04, 2013 6:56 pm

Re: Loading an ELF-executable

Post by kutkloon7 »

Thank you again, sortie! Everything finally works again. My code that reads the program headers was bugged (I used the header size for section headers instead of the size for program headers) so that the .data segment wasn't loaded correctly.
Post Reply