C++ 中的引导加载程序
我已经通过在软盘上制作一个小型程序集引导加载程序而搞乱了几次,并且想知道是否可以用 C++ 制作引导加载程序,如果可以的话我可以从哪里开始?据我所知,我不确定它是否会使用 int main() 。
感谢您的任何帮助。
I have messed around a few times by making a small assembly boot loader on a floppy disk and was wondering if it's possible to make a boot loader in c++ and if so where might I begin? For all I know im not sure it would even use int main()
.
Thanks for any help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您正在编写引导加载程序,那么您实际上是从零开始:一小块代码被加载到内存中并执行。您可以用 C++ 编写大部分引导加载程序,但您需要首先引导自己的 C++ 运行时环境。
汇编实际上是第一阶段的唯一选择,因为您需要设置一个合理的环境来运行更高级别的任何内容。执行足够的操作来运行 C 代码相当简单——您需要:
然后,您可以在适当的位置跳入代码(例如
main()
),并期望基本的语言功能能够正常工作。 (在此阶段,可能已实现或链接的标准库的任何功能都可能需要额外的初始化。)获得适合 C++ 的环境需要更多的努力,因为它需要更多的初始化这里,还有需要运行时支持的核心语言功能(同样,这是在考虑库功能之前)。其中包括:
new
和delete
;在 C 环境启动并运行之前,这些都不是必需的,因此处理这些问题的代码可以用 C 而不是汇编语言编写(甚至可以用不使用上述功能的 C++ 子集)编写。
(同样的原理也适用于嵌入式系统,此类系统使用 C++ 的情况并不罕见,但只是以有限的方式——例如,没有例外和/或 RTTI,因为未实现运行时支持。)
If you're writing a boot loader, you're essentially starting from nothing: a small chunk of code is loaded into memory, and executed. You can write the majority of your boot loader in C++, but you will need to bootstrap your own C++ runtime environment first.
Assembly is really the only option for the first stage, as you need to set up a sensible environment for running anything higher-level. Doing enough to run C code is fairly straightforward -- you need:
Then you can jump into the code at an appropriate point (e.g.
main()
) and expect that the basic language features will work. (It's possible that any features of the standard library that may have been implemented or linked in might require additional initialisation at this stage.)Getting a suitable environment going for C++ requires more effort, as it needs more initialisation here, and also has core language features which require runtime support (again, this is before considering library features). These include:
new
anddelete
;None of these are required until the C environment is up and running, so the code that handles these can be written in C rather than assembler (or even in a subset of C++ that does not make use of the above features).
(The same principles apply in embedded systems, and it's not uncommon for such systems to make use of C++, but only in a limited way -- e.g. no exceptions and/or RTTI because the runtime support isn't implemented.)
我已经有一段时间没有编写引导加载程序了,所以我要忘记了。
对于 x86 引导加载程序,您需要有一个可以生成 x86 程序集的 C++ 编译器,或者至少,您需要在 16 位程序集中编写自己的前导码,将 CPU 置于 32 位保护(或 64 位)状态。 -bit long) 模式,然后才能调用 C++ 函数。
不过,一旦完成此操作,您应该能够利用大部分(如果不是全部)C++ 语言功能,只要您远离需要底层 libc 的东西。但是静态链接所有内容而不需要 CRT,你就成功了。
It's been a while since I played with writing bootloaders, so I'm going off memory.
For an x86 bootloader, you need to have a C++ compiler that can emit x86 assembly, or, at the very least, you need to write your own preamble in 16-bit assembly that will put the CPU into 32-bit protected (or 64-bit long) mode, before you can call your C++ functions.
Once you've done that, though, you should be able to make use of most, if not all, of C++'s language features, so long as you stay away from things that require an underlying libc. But statically link everything without the CRT and you're golden.
引导加载程序没有“int main()”,除非您编写汇编代码来调用它。
如果您正在编写第一阶段引导加载程序,那么非常不鼓励这样做。
否则,osdev.org 有关于该主题的精彩文档。
虽然可能可以用 C++ 制作引导加载程序,但请记住不要将代码链接到任何动态库,并记住,仅仅因为它是 C++,并不意味着您可以/应该使用STL等
Bootloaders don't have "int main()"s, unless you write assembly code to call it.
If you are writing a stage 1 bootloader, then it is seriously discouraged.
Otherwise, the osdev.org has great documentation on the topic.
While it is probably possible to make a bootloader in C++, remember not to link your code to any dynamic libraries, and remember that just because it is C++, that doesn't mean you can/should use the STL, etc.
是的,这是可能的。 中包含答案元素和有用的链接
此问题 也可以看看这里,有一个C++ bootloader示例。
主要要理解的是,您需要创建一个平面二进制文件,而不是通常的花哨的可执行文件格式(Windows 上的 PE,或 Unix 上的 ELF),因为这些文件格式需要操作系统来加载它们,并且在引导加载程序中还没有操作系统。
如果静态链接,使用库不是问题(没有动态链接,因为再次出现上述可执行问题)。但显然所有与操作系统 API 相关的入口点都不可用......
Yes it is possible. You have elements of answer and usefull links in this question
You also can have a look here, there is a C++ bootloader example.
The main thing to understand is that you need to create a flat binary instead of the usual fancy executable file formats (PE on windows, or ELF on Unixes), because these file format need an OS to load them, and in a boot loader you don't have an OS yet.
Using library is not a problem if you link statically (no dynamic link because again of the above executable problem). But obviously all OS API related entry points are not available...