为什么ld在输出文件中包括链接命令文件中未指定的部分? [英] Why does ld include sections in the output file that are not specified in the link command file?

查看:98
本文介绍了为什么ld在输出文件中包括链接命令文件中未指定的部分?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力了解如何使ld(在gcc arm嵌入式工具套件中)做我想要的事情:

I am struggling to understand how to make ld (in the gcc arm embedded tool kit) do what I want:

仅作为示例,我有几个要链接的.o文件.说我 only 只希望在输出中有一个名为.foo的部分.我将其放在命令文件link.ld中:

just as an example, I have a couple of .o files I want to link. say I only want a section called .foo in the output. I put this in the command file link.ld:

SECTIONS{
  .foo : {*(.text)}
}

并转到"ld -T link.ld file1.o fil2.o -o output.o"

and go "ld -T link.ld file1.o fil2.o -o output.o"

然后我在output.o上使用objdump,它只包含输入文件()中的 every 部分,而且绝对没有名为.foo的部分,这是最令人困惑的部分,为什么没有输出中名为.foo的部分?

then i use objdump on output.o and it simply contains every section that was in the input files (), and absolutely no section called .foo this is the most confusing part why is there no section in the output called .foo?

我已阅读 http://www.scoberlin .de/content/media/http/informatik/gcc_docs/ld_3.html ,这一切似乎都是直截了当的,我只是不明白为什么这样一个简单的链接描述文件并没有达到我想要的目的.

I have read this http://www.scoberlin.de/content/media/http/informatik/gcc_docs/ld_3.html and it all seems straight foward, I just cannot understand why such a simple linker script does nowhere near what I want it to do.

我一定错过了非常明显的东西;有人可以解释我如何精确控制哪些部分出现在输出文件中以及它们的名称吗?

I must be missing something extremely obvious; can someone explain how I can control exactly which sections appear in the output file and what they are called?

推荐答案

好吧,您不能如此轻松地控制它-如您所见.如果您没有明确"链接某些部分(在这种情况下,您可以控制顺序,对齐方式,位置,填充等),则链接器认为必要的其他所有内容都将放在之后".这是一种简化,但这大致上是这样的.

Well, you cannot control that so easily - as you see. If you don't link some sections "explicitly" (in which case you have control over things like order, alignment, location, fill, ...), everything else that the linker finds necessary is just placed "after that". It's a simplification, but this mostly works like this.

我认为没有办法告诉链接器仅包含 您所请求的部分.但是有一种方法可以告诉它排除某些部分.但是问题在于,您必须明确说明要排除的部分.

I don't think there's a way to tell linker to include ONLY the section you requested. But there's a way to tell it to exclude some sections. But the problem is that you have to be explicit about the sections you want to exclude.

3.6.7输出节丢弃

3.6.7 Output Section Discarding

...

特殊的输出段名称/DISCARD/可用于丢弃输入段.任何输入部分 分配给名为/DISCARD/的输出节的文件不是 包含在输出文件中.

The special output section name /DISCARD/ may be used to discard input sections. Any input sections which are assigned to an output section named /DISCARD/ are not included in the output file.

也许这样会起作用?

SECTIONS{
  .foo : {*(.text)}
  /DISCARD/ : {*(*)}
}

这篇关于为什么ld在输出文件中包括链接命令文件中未指定的部分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆