从引导扇区到 C++核心
我决定编写一个简单的 asm 引导加载程序和一个 c++ 内核。我读了很多教程,但我无法编译一个汇编文件,如下所示:(
[BITS 32]
[global start]
[extern _k_main]
start:
call _k_main
cli
hlt
我想从 c 文件调用 k_main 函数)
编译/汇编/链接错误:
nasm -f bin -o kernelstart.asm -o kernelstart.bin:
error: bin file cannot contain external references
好的,然后我尝试创建一个 .o 文件:
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
某人请给我一个工作示例或说明如何编译。 :/ (我两天前正在浏览教程和帮助,但找不到正确的答案)
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)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我没有直接回答您的错误来自何处。然而,我确实看到很多事情出了问题,所以我将在这里写下这些:
nasm
这有效吗?这应该是类似
ld
因为你说你想制作一个引导加载程序和一个内核,我假设你的目标是让
ld
输出可以放置的东西在MBR中。如果是这种情况,请记住以下几点:--oformat=binary
添加到命令行选项。这可确保生成平面二进制文件。_main
。我不确定该符号是在哪里定义的,但我猜您希望入口点是start
因为那是您调用内核的地方。text
部分。如果您想将映像放入 MBR 中以供 BIOS 加载,则应将其链接到 0x7c00。我希望这些要点能够帮助您解决问题。
此外,您还可以在 OSDev 中找到许多有用的信息。 这里是编写仅使用 MBR 的实模式“内核”的教程。本教程包含工作代码。
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
Does that even work? That should be something like
ld
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 bestart
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.