views:

430

answers:

4

How do you compile a C program in to a valid ELF format(or RAW format) so that it can be executed directly from RAM without any OS? Assume that a bootloader exists which is capable of loading the code to any location in RAM and start execution at that address? To be precise , what should be the compiler(GCC) flags? Is there a need for a map file?

A sample helloworld application would be most welcome :)

Just to elaborate my point,
let the main() method be an empty infinite while loop to ensure that no OS specific or standard library calls are used. The desired o/p is a hang. With the usual GCC options the bootloader would definitely fail to load the executable telling it is invalid ELF format. However by passing -dN option to the linker will make it a valid ELF. More compiler/linker options are required to make it hang and not crash!! What exactly are those compiler options?

file.c:
int main()
{
    while(1);
}

Compilation
gcc -c -nostdinc -fno-builtin file.c
ld -dN -nostdlib file.o

Bootloader loads a.out to RAM and executes.

+6  A: 

Hi,

Firstly, there are limitations to what you can do, once bootloader finishes its work. These limitations are because of following things: (Assuming that your boot loader (eg: grub), takes you to 32-bit protected mode) 1. There is no paging enabled. You need to enable it. 2. There you cannot interact with devices as you have to set handlers for IRQ.

Here is a classic link which I used to learn the art of programming over bare metal(http://geezer.osdevbrasil.net/osd/index.htm). Also, there is a download link on the page (http://geezer.osdevbrasil.net/osd/code/osd.tgz). The tar file contains demo kernels with increasing level of complexity. Also, you can look into the make file (linux.mak), and get required flags for gcc.

I opened a random make file and found that following flags were used: gcc : -nostdinc -fno-builtin ld: -nostdlib
(An explicit call to linker is made, hence ld also needs a flag).

The flags purpose is to tell gcc that there must be no linking done with the standard library.

A: 

Do you realize that if you don't load an OS you must write a device driver to work with any device. That means no writing to the screen without writing a driver for that particular video card, no reading from the disk without writing a driver for that particular hard drive controller card, no accessing the keyboard, etc.

You may want to look into something called Kiosk Mode instead. The basic idea is that the OS runs, loads everything need for your app, loads your app, and then you cannot switch to another app.

Chas. Owens
Instead of writing device drivers, you could just stay in real-mode and use BIOS interrupts to access all these things. Otherwise, video (VESA), keyboard (PS/2 emulation), etc. is pretty standardized.
Andy
+1  A: 

coreboot can load stand-alone ELF. see "Initializing DRAM":

http://en.wikipedia.org/wiki/Coreboot

plan9assembler
A: 

http://www.osdever.net/bkerndev/index.php

This site answers ur question in step by step...

suresh
thanks for the link...
AIB