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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With