从引导扇区到 C++核心

发布于 2024-10-12 01:20:02 字数 618 浏览 6 评论 0原文

我决定编写一个简单的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

爱她像谁 2024-10-19 01:20:02

我没有直接回答您的错误来自何处。然而,我确实看到很多事情出了问题,所以我将在这里写下这些:

nasm

nasm -f aout -o kernelstart.asm -o kernelstart

这有效吗?这应该是类似

nasm -f aout -o kernelstart kernelstart.asm

ld

ld -i -e _main -Ttext 0x1000 kernel.o kernelstart.o main.o

因为你说你想制作一个引导加载程序和一个内核,我假设你的目标是让 ld 输出可以放置的东西在MBR中。如果是这种情况,请记住以下几点:

  • 您没有指定输出格式。如果要制作 MBR 映像,请将 --oformat=binary 添加到命令行选项。这可确保生成平面二进制文件。
  • 您将入口点设置为_main。我不确定该符号是在哪里定义的,但我猜您希望入口点是 start 因为那是您调用内核的地方。
  • 您链接从 0x1000 开始的 text 部分。如果您想将映像放入 MBR 中以供 BIOS 加载,则应将其链接到 0x7c00。
  • 作为旁注:您似乎试图将引导加载程序和内核链接到一个映像中。请记住,MBR 只能容纳 512 个字节(实际上是 510 个字节,因为最后 2 个字节应该包含一个神奇的值),因此您无法在那里编写太多内核内容。您应该做的是创建一个单独的内核映像并从引导加载程序加载它。

我希望这些要点能够帮助您解决问题。

此外,您还可以在 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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文