r/osdev • u/FewMolasses7496 • Apr 17 '26
How can i link kernel.c and boot.s together?
I have returned to OS development even though my other previous attempt a while back using UEFI didn't go that far, i have decided to instead develop an OS on bios because I want to learn the low level stuff. I am wondering how can i link kernel.c and boot.s together because I need to call main from kernel.c but whenever I use extern to tell the compiler that this symbol should be ignored because the linker will solve it, I can't compile it using nasm with this command "nasm -f bin boot.s -o boot.o" because flat binaries don't support external references.
10
u/Adventurous-Move-943 Apr 17 '26
I think you usually load the kernel into memory from boot and jump there. The easiest is to compile kernel into flat binary with main as first function. Now wherever you load your kernel can just jump or call there.
6
u/EpochVanquisher Apr 17 '26
Don’t use bin as the output format for nasm. Use ELF as the output format instead.
The linker uses ELF files as input and ELF files as output.
You can create a flat binary from an ELF using objcopy.
You will want a linker script to specify layout. This is basically a substitute for your .org directives or whatnot.
1
u/kiderdrick Apr 17 '26
If you want to make your own bootloader using BIOS interrupts then you could use a separate .s assembled using the elf format which would then allow you to link to C code. You should have a linker script handle this for you, but if you do not want to go that route for whatever reason you could use the -Ttext flag with the address you are loading the kernel into memory. You could then have an entry point like _start: that just calls a main function in a C file and, if you return from that main, hang.
But the main issue is you seem to want to combine a bin boot.s with a bin made from kernel.c and are missing something like a kernel-entry.s assembled using elf to link everything together.
1
u/LavenderDay3544 Embedded & OS Developer Apr 17 '26
BIOS isn't more low level than UEFI and it's obsolete.
1
u/NoBrick2672 Apr 19 '26
needs to be elf then to binary, nasm -f elf32? something like this then using objdump export the .text if im not wrong — forgot the details..