关于 GNU 链接器编辑器“ld”的问题吗?
我有 2 个用 GNU as 组装的 obj 文件,它们是:
- ao :我的主要程序
- b.o :一些实用函数
a.o 没有入口点。最终链接的文件将被加载到内存中,执行将跳转到其最开始加载的地址,其中是 ao 的第一条指令
。现在我想用 GNU ld 将它们链接在一起。我想让 ao 在最终文件中出现在 bo 之前。我怎样才能控制这个?我是否必须创建一个自定义部分并在链接器脚本中编写如下内容:
SECTIONS
{
. = 0x7c00;
.text : { *(.text) }
.my_custom_section : { *(.my_custom_section) }
.data : { *(.data) }
.bss : { *(.bss) }
}
OUTPUT_FORMAT(binary)
更新
这个问题有问题吗?难道是我发错帖了?如果是这样,请告诉我,伙计们。非常感谢。
目前,我发现输入文件的命令行序列似乎是相关的。
如果我这样做:
ld a.o b.o -o final.bin
ao 的内容将出现在 bo 之前
如果我这样做:
ld b.o a.o -o final.bin
则不然。
难道意味着要这样被控制吗?
I have 2 obj files assembled with GNU as, they are:
- a.o : my major program
- b.o : some utility functions
a.o doesn't have an entry point. The final linked file will be loaded into memory and the execution will jump to its very beginning loaded address, where is the first instrucion of a.o.
Now I want to link them together with GNU ld. And I want to make a.o appear before b.o in the final file. How could I control this? Do I have to make a custom section and write in the linker script like this:
SECTIONS
{
. = 0x7c00;
.text : { *(.text) }
.my_custom_section : { *(.my_custom_section) }
.data : { *(.data) }
.bss : { *(.bss) }
}
OUTPUT_FORMAT(binary)
Update
Is there something wrong with this question? Did I post it wrong? If so, please let me know, guys. Many thanks.
Currently, I found that the command line sequence of the input files seems to be relevant.
If I do like this:
ld a.o b.o -o final.bin
Content from a.o will appear before b.o.
If I do like this:
ld b.o a.o -o final.bin
It will be otherwise.
Is it meant to be controlled like this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
根据手册:
因此二进制文件中文件的顺序就是它们的顺序出现在命令行上。
因此,正如您在更新中提到的那样,它意味着受到控制。
According to the manual:
So the order of files in the binary is the order in which they appear on the command line.
Therefore, it is meant to be controlled as you mention in your update.
ld 的操作顺序实际上是相关的。
除非以某种方式明确说明,否则入口点是列表中第一个文件的第一个代码字节。
生成的可执行文件始终包含按调用顺序排列的 .o 文件的内容。 (使用 .a 文件会变得复杂)。
The order of operations to ld is in fact relevant.
Unless explicitly stated somehow, the entry point is the first code byte of the first file on the list.
The resulting executable always has the contents of the .o files in invocation order. (with .a files it gets complicated).