移植NewLib:crt0
我正在按照教程为我自己的操作系统移植 NewLib。
它说一旦我完成了我的 crt0,我必须“将其链接为第一个对象”。我怎样才能做到这一点?
I am porting NewLib for my own OS by following a tutorial.
It says that once I finished my crt0, I have to "link it as the first object". How can I do that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这正是所说的意思。当链接操作系统的应用程序时,crt0 必须是链接器命令行上的第一个目标文件:位于任何其他目标文件之前。
传统上,UNIX 链接器通过从第一个对象/库循环到最后一个对象/库进行查找来解析符号。将
crt0
作为第一个目标文件可确保从 crt0 文件而不是其他文件中选取系统符号(函数、变量)。此外,正如 R.. 所指出的,传统链接器假设应用程序的入口点位于代码段的开头。这也适合链接页面上找到的源代码:代码中的第一个符号是
_start
,它是程序入口点的常用名称。看到以下链接命令(为了便于阅读,我添加了新行):
例如,在我的 Debian 上运行
gcc -v ac -o a
(注意-v
),可以 crt1 是命令行上的第一个对象。查看链接器脚本 (-m elf_i386
->find /usr -name '*elf_i386*'
-> 在我的系统/usr/lib/ ldscripts/elf_i386.x
)可以验证在 Linux 上没有crt0
,但有:crt1
、crti
、crtbegin
、crtend
、crtn
。应用程序对象文件(上面示例中的/tmp/ccndJ4YV.o
)放置在crtbegin
和crtend
之间。That means precisely what is says. When an application for your OS is being linked, the crt0 must be the very first object file on linker's command line: before any other object file.
Traditionally, UNIX linkers resolve a symbol by making a look up starting from first object/library looping over to the last object/library. Putting
crt0
as the first object file makes sure that system symbols (functions, variables) are picked from the crt0 file, not some other file.Additionally, as pointed by R.., traditional linkers assume that application's entry point is at the beginning of the code segment. That also fits to the source code found on the linked page: first symbol in the code is
_start
, the usual name of the program's entry point.E.g. running
gcc -v a.c -o a
on my Debian (notice the-v
), one sees following linking command (I have added new lines for readability):One can see that crt1 is first object on the command line. Looking at the linker script (
-m elf_i386
->find /usr -name '*elf_i386*'
-> on my system/usr/lib/ldscripts/elf_i386.x
) one can verify that on Linux there is nocrt0
, but there are:crt1
,crti
,crtbegin
,crtend
,crtn
. And the application object files (/tmp/ccndJ4YV.o
in the sample above) are put between thecrtbegin
andcrtend
.在跳转到
main()
之前,c 运行时需要进行初始化,该工作由cert{i,n,0}
处理。before jumping to
main()
, c runtime need to do initialization, this job is handled bycert{i,n,0}
.至少对于测试来说,一种方法是将 crt0.o 作为编译器或链接器命令行上的第一个文件。
对于生产,您可以将其放入链接器命令文件中。
One way, for testing at least, is to place crt0.o as the first file on the compiler or linker command line.
For production, you'd put it in the linker command file.