ELF

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
Fusort
Posts: 2
Joined: Thu Jan 10, 2008 11:28 am

ELF

Post by Fusort »

Hello

I'm new here, and I'm seeking some tips. I want to try out
my bootloader. In order to make that, I'm trying to extract
only the instructions from my compiled executable "bootblock"
file. The instructions are suposed to be extracted to an
imagefile that will be simmulated by bochs. I have been reading
about the structure of ELF, but I'm kind of stuck right now.

Is it possible to do the same process with the object files, .o files?

Isn't it possible to do the same process during compiletime?

I'm using ubuntu and gcc/gas.

Thank you!
User avatar
Telgin
Member
Member
Posts: 72
Joined: Thu Dec 20, 2007 1:45 pm

Post by Telgin »

So, you mean you're trying to write a bootloader but can't figure out how to make it work when compiled as an ELF executable?

For the frist stage bootloader (the first sector of a hard disk that gets run automatically when the computer is turned on), you pretty much have to use raw executables. I.e. there is nothing but the program to run in the file (mostly anyway).

There should be a compiler / assembler switch to output raw executables, but you'll have to specify which assembler / compiler you're using before we can help.

If that's not what you meant, I'm not really sure what to tell you. It sounds sort of like you have compiled executable already (what of I can't tell though), and you're trying to extract the program from it separate from the other parts of the ELF file.

If that's the case, you're going to have to write a program / code to locate the program from the file and fill out any information needed. I don't know the ELF format myself, but I have seen its spec on the internet before. It shouldn't be difficult to anaylze the header of the executable to find the program information, relocate it into memory, and dispose of the rest.
User avatar
crazygray1
Member
Member
Posts: 168
Joined: Thu Nov 22, 2007 7:18 pm
Location: USA,Hawaii,Honolulu(Seriously)

Post by crazygray1 »

I don't really know what your asking... :?
User avatar
Brynet-Inc
Member
Member
Posts: 2426
Joined: Tue Oct 17, 2006 9:29 pm
Libera.chat IRC: brynet
Location: Canada
Contact:

Post by Brynet-Inc »

crazygray1 wrote:I don't really know what your asking... :?
Then don't reply, it's not at all helpful to the original poster.. and all it serves to do is increase your post count.
Image
Twitter: @canadianbryan. Award by smcerm, I stole it. Original was larger.
Fusort
Posts: 2
Joined: Thu Jan 10, 2008 11:28 am

Post by Fusort »

Yes, after compiling my bootblock.s, which is written in assembly,
I want to extract only the raw instructions from the output executable
file. This output file has the ELF structure. What I mean with raw
instructions, is the instructions relevant for my bootloader.

I will then put these extracted instructions in a clean new file, which
is supposed to simulate a 1,44 MB floppy in bochs.

My problem is that I don't know where to start in the ELF structure.
I have read the ELF header from the file I'm trying to extract
the raw instructions from. This struct looks like this:

typedef struct {
unsigned char e_ident[EI_NIDENT];
Elf32_Half e_type;
Elf32_Half e_machine;
Elf32_Word e_version;
Elf32_Addr e_entry;
Elf32_Off e_phoff;
Elf32_Off e_shoff;
Elf32_Word e_flags;
Elf32_Half e_ehsize;
Elf32_Half e_phentsize;
Elf32_Half e_phnum;
Elf32_Half e_shentsize;
Elf32_Half e_shnum;
Elf32_Half e_shstrndx;
} Elf32_Ehdr;

But from here, I'm just confused..

I'm using the GCC compiler and the Gas assembler.

Sorry for not explaining sufficiently, I hope this is better.
Ready4Dis
Member
Member
Posts: 571
Joined: Sat Nov 18, 2006 9:11 am

Post by Ready4Dis »

Brynet-Inc wrote:
crazygray1 wrote:I don't really know what your asking... :?
Then don't reply, it's not at all helpful to the original poster.. and all it serves to do is increase your post count.
And your response was more helpful? I would say less actually, at least his tells the OP that he needs to rephrase his question for clarity.

When you link the files using ld, tell it that you want a flat binary. I use gcc under windows so it may be slightly different. Are you using a linker script or just compiling? Under ld in windows, I would use the --format TARGET or -b TARGET to change the output file format. So...

ld -b binary -o BootBlock.bin BootBlock.elf

This would take the bootblock.elf file and create a raw binary file bootblock.bin... depending on how you have it, you may need to set the .text section offset so it relocates properly (unless you use the org keyword in your asm file to accomplish this).
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:

Post by Combuster »

Assuming that you want to load and start and ELF executable, you should check all sections, have a look if they have to be loaded into memory, or have to be zeroed. Once you've parsed all section headers then you're done and you can start executing.

There's an ELF Loader in my source repository which you can look at to get some ideas. (it can also do relocation)
"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 ]
Ready4Dis
Member
Member
Posts: 571
Joined: Sat Nov 18, 2006 9:11 am

Post by Ready4Dis »

Combuster wrote:Assuming that you want to load and start and ELF executable, you should check all sections, have a look if they have to be loaded into memory, or have to be zeroed. Once you've parsed all section headers then you're done and you can start executing.

There's an ELF Loader in my source repository which you can look at to get some ideas. (it can also do relocation)
Great stuff, but he just wanted to extract the raw data, and linking to a flat binary is probably much simpler :). This is for his boot sector, so storing the elf header and section headers would waste way to much memory to be useful in a 510byte boot sector. I haven't used gas at all, but i know ld will link the elf file to a flat binary. I use nasm for my boot sector and second stage loader, and it can output flat binary directly, gas may have that option as well.
Post Reply