Here is how you should compile it:
First, run this command
The -c option tells gcc to compile and assemble it only, not to link. Note that you could in theory directly call i686-elf-as
Then compile your kernel file:
Code: Select all
i686-elf-gcc -c kernel.c -o kernel.o -ffreestanding -fno-stack-protector -O0 -g -Wall -Wextra
-ffreestanding tells gcc that the compiled file does not have any standard includes or standard libraries. -fno-stack-protector tells gcc not to imply calls to the stack smashing protector.
Then link the whole thing with
Code: Select all
i686-elf-gcc -Tlink.ld -nostdlib boot.o kernel.o -lgcc
-nostdlib tells gcc that there is no standard library here. -lgcc tell gcc to link with libgcc, as gcc emits calls to libgcc even with -ffreestanding. link.ld is a linker script. It tells ld how to layout the sections. It should look like this
Code: Select all
ENTRY(_start)
SECTIONS
{
. = 0x100000
.text ALIGN(4096) : {
*(.text)
}
.data ALIGN(4096) : {
*(.data)
}
.rodata ALIGN(4096) : {
*(.rodata*)
}
.bss ALIGN(4096) : {
*(.bss)
}
end = .;
}
I can answer question you have about this linker script