GNU链接器 - 孤儿节和符号分配 [英] GNU Linker - orphan sections and symbol assignment

查看:261
本文介绍了GNU链接器 - 孤儿节和符号分配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在阅读了关于GNU链接器的足够的文档后,我对关于实现定制链接器文件的两个不同概念进行了混淆。 第一个概念是 orphan sections -


如果没有名称匹配的输出部分,则会创建新的输出部分。每个新的输出部分将与放置在其中的孤立部分具有相同的名称。 如果有多个具有相同名称的孤儿部分,则这些部分将全部组合为一个新的输出部分
如果创建新的输出节来保存孤立的输入节
,则链接器必须决定将这些新的输出节
与现有输出节相关的位置。 在大多数现代目标中,
链接程序试图将孤行段放在相同
属性的部分之后,例如代码与数据,可加载与不可加载等
。如果没有找到具有匹配属性的
部分,或者您的目标缺少
支持,则孤立部分将放置在文件末尾。


第二个概念是关于 符号分配 -


以下示例显示了符号
赋值可能使用的三个不同位置:




  floating_point = 0; 
SECTIONS
{
.text:
{
*(。text)
_etext =。;
}
_bdata =(。+ 3)& 〜3;
.data:{*(。data)}
}




在这个例子中,符号'floating_point'将被定义为零。
符号'_etext'将被定义为最后
'.text'输入部分之后的地址。 符号'_bdata'将被定义为'.text'输出部分之后的
地址,并向上对齐至4
字节的边界。


因此,有关孤儿节的粗体解释说明,在上例中,链接器可能会在 .text 输出部分,这意味着符号分配说明中的粗体文本是错误的。



那么,这个例子是否可以在 _bdata 符号中产生不希望的值,如果orphan部分存在的话?

解决方案

Answer Sourceware LD文档在位置计数器一章 -


将符号设置为值的位置计数器如果链接器
需要放置孤立节

的输出节语句之外可能会导致意外的值。




  SECTIONS {
start_of_text =。 ;
.text:{*(.text)}
end_of_text =。 ;

start_of_data =。 ;
.rodata:{*(。rodata)}
.data:{*(。data)}
end_of_data =。 ; }




这可能是也可能不是脚本作者的意图对于
start_of_data的值

所以看起来他们的文档中用符号赋值的例子和解释应该被编辑为提及孤儿部分,或者被删除。


After reading enough documentation about the GNU linker, I fill confused about combining two different concepts regarding implementing custom linker file.

First concept is orphan sections-

If there is no output section with a matching name then new output sections will be created. Each new output section will have the same name as the orphan section placed within it. If there are multiple orphan sections with the same name, these will all be combined into one new output section. If new output sections are created to hold orphaned input sections, then the linker must decide where to place these new output sections in relation to existing output sections. On most modern targets, the linker attempts to place orphan sections after sections of the same attribute, such as code vs data, loadable vs non-loadable, etc. If no sections with matching attributes are found, or your target lacks this support, the orphan section is placed at the end of the file.

Second concept is about symbol assignment-

Here is an example showing the three different places that symbol assignments may be used:

floating_point = 0;
SECTIONS
{
  .text :
    {
      *(.text)
      _etext = .;
    }
  _bdata = (. + 3) & ~ 3;
  .data : { *(.data) }
}

In this example, the symbol ‘floating_point’ will be defined as zero. The symbol ‘_etext’ will be defined as the address following the last ‘.text’ input section. The symbol ‘_bdata’ will be defined as the address following the ‘.text’ output section aligned upward to a 4 byte boundary.

So, the bold explanation regarding orphan sections dictates that is possible that in the above example, the linker will place another output sections after .text output section, which means that the bold text in the symbol assignment explanation is wrong.

So, is this example can produce undesired value into the _bdata symbol if orphan section exist in it?

解决方案

Answer found on Sourceware LD documentation inside the The Location Counter chapter-

Setting symbols to the value of the location counter outside of an output section statement can result in unexpected values if the linker needs to place orphan sections.

 SECTIONS {
     start_of_text = . ;
     .text: { *(.text) }
     end_of_text = . ;

     start_of_data = . ;
     .rodata: { *(.rodata) }
     .data: { *(.data) }
     end_of_data = . ; }

This may or may not be the script author’s intention for the value of start_of_data.

So it seems that their documentation with the symbol assignment example and explanation should be edited to mention orphan sections, or removed.

这篇关于GNU链接器 - 孤儿节和符号分配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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