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

标签 gcc linker ld

在阅读了足够多有关 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 符号中产生不需要的值?

最佳答案

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

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.

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

关于gcc - GNU 链接器 - 孤立部分和符号分配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49095127/

相关文章:

ios - 几乎赤裸裸的iOS8.4 --> 如何获取链接器?

c++ - 在 mac os x 上将/usr/local/lib 设为ld 的默认库搜索路径?

node.js - Gulp/Node : error while loading shared libraries: cannot allocate memory in static TLS block

gcc - x86 CPU 的 128x128 位乘法

linux - 即使 nm 指示此符号存在于共享库中,也未定义对符号的引用

linux - 为什么 GNU 链接器找不到带有 -l<library> 的共享对象?

linux - OpenCV多个版本的安装和维护(也适用于任何其他3rd方库)

c - 如何帮助 gcc 向量化 C 代码

linux - 强制 GCC 静态链接,例如pthreads(而不是动态链接)

c - 全局空间中的指针初始化 C 语句如何在编译/链接时获取其分配的值?