Page 1 of 1

ELF

Posted: Fri Jan 11, 2008 7:17 pm
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!

Posted: Fri Jan 11, 2008 9:45 pm
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.

Posted: Fri Jan 11, 2008 10:30 pm
by crazygray1
I don't really know what your asking... :?

Posted: Fri Jan 11, 2008 11:29 pm
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.

Posted: Sat Jan 12, 2008 7:08 am
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.

Posted: Sat Jan 12, 2008 8:08 am
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).

Posted: Sat Jan 12, 2008 10:22 am
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)

Posted: Sat Jan 12, 2008 10:46 am
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.