GNU 链接器 - 孤立部分和符号分配

GNU Linker - orphan sections and symbol assignment

在阅读了足够多的关于 GNU 链接器的文档之后,我对结合两个关于实现自定义链接器文件的不同概念感到困惑。

第一个概念是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.

第二个概念是关于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.

因此,粗体 对孤立部分的解释表明,在上面的示例中,链接器可能会在 .text 输出部分之后放置另一个输出部分,这表示符号赋值解释中的粗体文字是错误的。

那么,如果其中存在孤立部分,此示例是否会在 _bdata 符号中产生不需要的值?

答案在 源软件 LD 文档 The Location Counter 章内找到-

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.

因此,他们带有符号分配示例和解释的文档似乎应该编辑以提及孤立部分,或者删除。