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

查看:22
本文介绍了为什么 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 文件.说我想要一个名为 .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,它只包含输入文件 () 中的 每个 部分,并且绝对没有名为 .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.

也许这样的事情会奏效?

Maybe something like this would work?

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

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

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