【问题标题】:Why there are no .rel.dyn/.got.plt section in dynamic ELF files?为什么动态 ELF 文件中没有 .rel.dyn/.got.plt 部分?
【发布时间】:2020-12-09 07:15:04
【问题描述】:

我有这样的代码

// test_printf.c
#include <stdio.h>

int f(){
    printf("aaa %d\n", 1);
}

我用下面的代码编译它

gcc -shared -fPIC -m32 -g -c -o test_printf.so test_printf.c

我想如果运行readelf -S test_printf.so,我会看到.rel.dyn.rel.plt。这是因为这两个部分的行为类似于静态链接程序中的.rel.data.rel.text

例如,在我的程序中,因为printf 是一个外部符号,并且由我的test_printf.so 引用。所以当我查看test_printf.so 的重定位表时,应该有一个条目名称printf。我检查了一下,条目存在。

那我想,既然printf是一个外部符号,它的位置应该在运行时确定。但是,我们必须为这个printf 函数分配一个.got.plt 部分,并且该部分应该在动态链接的执行程序和动态库(test_printf.so)中。

但是,当我运行readelf -S 时,没有.got.plt 部分,我对此感到困惑。动态库(test_printf.so)中是否不需要此部分?我不认为这是可能的。假设test_printf.so最终链接到执行程序a,那么a怎么知道printf.got.plt部分在哪里呢?这个.got.plt最后是在a生成的吗?

同时,我有一个问题 2。.rel.dyn.rel.plt 是否始终存在,如果有 .got.got.plt

【问题讨论】:

    标签: c linker elf dynamic-linking got


    【解决方案1】:

    .rel.dyn.got.plt 可以存在于共享库中 .elf 这取决于库中函数的结构,这在 https://www.technovelty.org/linux/plt-and-got-the-key-to-code-sharing-and-dynamic-libraries.html

    .rela.dyn 如果包含外部函数,则存在:

    $ cat test.c
    extern int foo;
    
    int function(void) {
        return foo;
    }
    $ gcc -shared -fPIC -o libtest.so test.c
    

    另见https://eli.thegreenplace.net/2011/08/25/load-time-relocation-of-shared-libraries/https://eli.thegreenplace.net/2011/11/03/position-independent-code-pic-in-shared-libraries

    https://blog.ramdoot.in/how-can-i-link-a-static-library-to-a-dynamic-library-e1f25c8095ef(将静态库链接到动态库中)

    【讨论】:

      猜你喜欢
      • 2013-02-09
      • 2012-07-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-29
      • 2020-06-07
      • 2012-05-15
      • 1970-01-01
      相关资源
      最近更新 更多