无操作系统执行
如何将C程序编译为有效的ELF格式(或RAW格式),以便可以直接从RAM执行而无需任何操作系统 >? 假设存在一个引导加载程序,它能够将代码加载到 RAM 中的任何位置并在该地址开始执行。 准确地说,编译器(GCC)标志应该是什么? 是否需要地图文件?
一个示例 helloworld 应用程序将是最受欢迎的:)
只是为了阐述我的观点,
让 main() 方法是一个空的无限 while 循环,以确保不使用特定于操作系统或标准库的调用。 所需的操作是挂起。 使用通常的 GCC 选项,引导加载程序肯定无法加载可执行文件,告诉它是无效的 ELF 格式。 但是,通过将 -dN 选项传递给链接器将使其成为有效的 ELF。 需要更多编译器/链接器选项才能使其挂起而不崩溃! 这些编译器选项到底是什么?
file.c:
int main()
{
while(1);
}
编译
gcc -c -nostdinc -fno-内置文件.c
ld -dN -nostdlib file.o
引导加载程序将 a.out 加载到 RAM 并执行。
How do you compile a C program in to a valid ELF format(or RAW format) so that it can be executed directly from RAM without any OS? Assume that a bootloader exists which is capable of loading the code to any location in RAM and start execution at that address.
To be precise, what should be the compiler(GCC) flags? Is there a need for a map file?
A sample helloworld application would be most welcome :)
Just to elaborate my point,
let the main() method be an empty infinite while loop to ensure that no OS specific or standard library calls are used. The desired o/p is a hang. With the usual GCC options the bootloader would definitely fail to load the executable telling it is invalid ELF format. However by passing -dN option to the linker will make it a valid ELF. More compiler/linker options are required to make it hang and not crash!! What exactly are those compiler options?
file.c:
int main()
{
while(1);
}
Compilation
gcc -c -nostdinc -fno-builtin file.c
ld -dN -nostdlib file.o
Bootloader loads a.out to RAM and executes.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
首先,一旦引导加载程序完成其工作,您可以做的事情就受到限制。
这些限制是由于以下原因:
(假设您的引导加载程序(例如:grub)将您带至 32 位保护模式)
1. 没有启用寻呼。 您需要启用它。
2. 在那里您无法与设备交互,因为您必须设置 IRQ 处理程序。
这是我用来学习裸机编程艺术的经典链接(http:// geezer.osdevbrasil.net/osd/index.htm)。
此外,页面上还有一个下载链接(http://geezer.osdevbrasil.net /osd/code/osd.tgz)。
tar 文件包含复杂程度不断增加的演示内核。
另外,您可以查看 make 文件 (linux.mak),并获取 gcc 所需的标志。
我打开一个随机的 make 文件,发现使用了以下标志:
gcc : -nostdinc -fno-builtin
ld: -nostdlib
(对链接器进行了显式调用,因此 ld 也需要一个标志)。
标志的目的是告诉 gcc 不能与标准库进行链接。
Firstly, there are limitations to what you can do, once bootloader finishes its work.
These limitations are because of following things:
(Assuming that your boot loader (eg: grub), takes you to 32-bit protected mode)
1. There is no paging enabled. You need to enable it.
2. There you cannot interact with devices as you have to set handlers for IRQ.
Here is a classic link which I used to learn the art of programming over bare metal(http://geezer.osdevbrasil.net/osd/index.htm).
Also, there is a download link on the page (http://geezer.osdevbrasil.net/osd/code/osd.tgz).
The tar file contains demo kernels with increasing level of complexity.
Also, you can look into the make file (linux.mak), and get required flags for gcc.
I opened a random make file and found that following flags were used:
gcc : -nostdinc -fno-builtin
ld: -nostdlib
(An explicit call to linker is made, hence ld also needs a flag).
The flags purpose is to tell gcc that there must be no linking done with the standard library.
coreboot可以加载独立的ELF。
请参阅“初始化 DRAM”:
http://en.wikipedia.org/wiki/Coreboot
coreboot can load stand-alone ELF.
see "Initializing DRAM":
http://en.wikipedia.org/wiki/Coreboot
http://www.osdever.net/bkerndev/index.php
该网站回答你的问题一步步...
http://www.osdever.net/bkerndev/index.php
This site answers your question step-by-step...
您是否意识到,如果不加载操作系统,则必须编写设备驱动程序才能与任何设备配合使用。 这意味着如果没有为该特定显卡编写驱动程序,则不能写入屏幕;如果没有为该特定硬盘驱动器控制器卡编写驱动程序,则不能从磁盘读取数据;不能访问键盘等。
您可能需要研究名为 < a href="http://www.google.com/search?q=linux+kiosk+mode" rel="nofollow noreferrer">Kiosk 模式。 基本思想是操作系统运行,加载应用程序所需的所有内容,加载您的应用程序,然后您无法切换到另一个应用程序。
Do you realize that if you don't load an OS you must write a device driver to work with any device. That means no writing to the screen without writing a driver for that particular video card, no reading from the disk without writing a driver for that particular hard drive controller card, no accessing the keyboard, etc.
You may want to look into something called Kiosk Mode instead. The basic idea is that the OS runs, loads everything need for your app, loads your app, and then you cannot switch to another app.