开发者

From bootsector to C++ kernel

开发者 https://www.devze.com 2023-02-03 21:44 出处:网络
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:

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开发者_Python百科
   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:

  • You didn't specify the output format. If you want to make an MBR image, add --oformat=binary to the command line options. This makes sure a flat binary file is generated.
  • You set the entry point to _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.
  • You link your 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.
  • As a side note: it seems your trying to link your bootloader and kernel together in one image. Just remember that the MBR is can only hold 512 bytes (well, actually 510 bytes since the last 2 should contain a magic value) so you won't be able to write much of a kernel there. What you should do is create a separate kernel image and load this from your bootloader.

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号