移植NewLib:crt0

发布于 2024-09-12 00:10:12 字数 162 浏览 9 评论 0原文

我正在按照教程为我自己的操作系统移植 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 技术交流群。

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

发布评论

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

评论(3

留蓝 2024-09-19 00:10:12

它说一旦我完成了 crt0,我必须“将其链接为第一个对象”。

这正是所说的意思。当链接操作系统的应用程序时,crt0 必须是链接器命令行上的第一个目标文件:位于任何其他目标文件之前。

传统上,UNIX 链接器通过从第一个对象/库循环到最后一个对象/库进行查找来解析符号。将 crt0 作为第一个目标文件可确保从 crt0 文件而不是其他文件中选取系统符号(函数、变量)。

此外,正如 R.. 所指出的,传统链接器假设应用程序的入口点位于代码段的开头。这也适合链接页面上找到的源代码:代码中的第一个符号是_start,它是程序入口点的常用名称。

看到以下链接命令(为了便于阅读,我添加了新行):

/usr/lib/gcc/i486-linux-gnu/4.4.4/collect2
--build-id
--eh-frame-hdr
-m elf_i386
--hash-style=both
-dynamic-linker /lib/ld-linux.so.2
-o a 
/usr/lib/gcc/i486-linux-gnu/4.4.4/../../../../lib/crt1.o 
/usr/lib/gcc/i486-linux-gnu/4.4.4/../../../../lib/crti.o 
/usr/lib/gcc/i486-linux-gnu/4.4.4/crtbegin.o
-L/usr/lib/gcc/i486-linux-gnu/4.4.4
-L/usr/lib/gcc/i486-linux-gnu/4.4.4
-L/usr/lib/gcc/i486-linux-gnu/4.4.4/../../../../lib
-L/lib/../lib
-L/usr/lib/../lib
-L/usr/lib/gcc/i486-linux-gnu/4.4.4/../../.. 
/tmp/ccndJ4YV.o
-lgcc
--as-needed
-lgcc_s
--no-as-needed
-lc
-lgcc
--as-needed
-lgcc_s
--no-as-needed 
/usr/lib/gcc/i486-linux-gnu/4.4.4/crtend.o 
/usr/lib/gcc/i486-linux-gnu/4.4.4/../../../../lib/crtn.o

例如,在我的 Debian 上运行 gcc -v ac -o a (注意 -v),可以 crt1 是命令行上的第一个对象。查看链接器脚本 (-m elf_i386 -> find /usr -name '*elf_i386*' -> 在我的系统 /usr/lib/ ldscripts/elf_i386.x)可以验证在 Linux 上没有 crt0,但有:crt1crticrtbegincrtendcrtn。应用程序对象文件(上面示例中的 /tmp/ccndJ4YV.o)放置在 crtbegincrtend 之间。

It says that once I finished my crt0, I have to "link it as the first object".

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):

/usr/lib/gcc/i486-linux-gnu/4.4.4/collect2
--build-id
--eh-frame-hdr
-m elf_i386
--hash-style=both
-dynamic-linker /lib/ld-linux.so.2
-o a 
/usr/lib/gcc/i486-linux-gnu/4.4.4/../../../../lib/crt1.o 
/usr/lib/gcc/i486-linux-gnu/4.4.4/../../../../lib/crti.o 
/usr/lib/gcc/i486-linux-gnu/4.4.4/crtbegin.o
-L/usr/lib/gcc/i486-linux-gnu/4.4.4
-L/usr/lib/gcc/i486-linux-gnu/4.4.4
-L/usr/lib/gcc/i486-linux-gnu/4.4.4/../../../../lib
-L/lib/../lib
-L/usr/lib/../lib
-L/usr/lib/gcc/i486-linux-gnu/4.4.4/../../.. 
/tmp/ccndJ4YV.o
-lgcc
--as-needed
-lgcc_s
--no-as-needed
-lc
-lgcc
--as-needed
-lgcc_s
--no-as-needed 
/usr/lib/gcc/i486-linux-gnu/4.4.4/crtend.o 
/usr/lib/gcc/i486-linux-gnu/4.4.4/../../../../lib/crtn.o

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 no crt0, but there are: crt1, crti, crtbegin, crtend, crtn. And the application object files (/tmp/ccndJ4YV.o in the sample above) are put between the crtbegin and crtend.

巡山小妖精 2024-09-19 00:10:12

在跳转到 main() 之前,c 运行时需要进行初始化,该工作由 cert{i,n,0} 处理。

在此处输入图像描述

before jumping to main(), c runtime need to do initialization, this job is handled by cert{i,n,0}.

enter image description here

半枫 2024-09-19 00:10:12

至少对于测试来说,一种方法是将 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.

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