I decided to write a simple asm bootloader and a c++ kernel. I read a lot of tutorials, but I cant compile an assembly file seems like this:
[BITS 32]
[global start]
[extern _k_main]
start:
call _k_main
cli
hlt
(I would like to call th k_main function from c file)
Compile/assemble/linking errors:
nasm -f bin -o kernelstart.asm -o kernelstart.bin:
error: bin file cannot contain external references
okay, then i tried create a .o file:
nasm -f aout -o kernelstart.asm -o kernelstart.o (That's right)
ld -i -e _main -Ttext 0x1000 kernel.o kernelstart.o main.o
error: File format not recognized
Someone give me plz a working example or say how to compile. :/ (I'm browsing the tutorials and helps 2 days ago but cannot find a right answer)
I don't have a direct answer on where your error comes from. However, I do see a lot of things going wrong so I'll write these here:
nasm
nasm -f aout -o kernelstart.asm -o kernelstart
Does that even work? That should be something like
nasm -f aout -o kernelstart kernelstart.asm
ld
ld -i -e _main -Ttext 0x1000 kernel.o kernelstart.o main.o
Since you said you wanted to make a bootloader and a kernel, I'm assuming your goal here is to make ld output something that can be put in the MBR. If that's the case, here are some things to keep in mind:
--oformat=binary to the command line options. This makes sure a flat binary file is generated._main. I'm not sure where that symbol is defined, but I guess you want your entry point to be start because that's where you call your kernel.text section starting at 0x1000. If you want to put your image in the MBR to be loaded by the BIOS, it should be linked at 0x7c00.I hope these points will help you in solving your problem.
Also, you'll find a lot of useful information as OSDev. Here is a tutorial on writing a real mode "kernel" that only uses the MBR. The tutorial contains working code.
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