简体   繁体   English

为什么动态 ELF 文件中没有.rel.dyn/.got.plt 部分?

[英]Why there are no .rel.dyn/.got.plt section in dynamic ELF files?

I have code like this我有这样的代码

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

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

And I compile it with the following code我用下面的代码编译它

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

I think if run readelf -S test_printf.so , I will see .rel.dyn and .rel.plt .我想如果运行readelf -S test_printf.so ,我会看到.rel.dyn.rel.plt This is because both of these sections act like what .rel.data and .rel.text in static linked programs do.这是因为这两个部分的行为类似于 static 链接程序中的.rel.data.rel.text所做的。

For example, in my program, since printf is an external symbol, and is referred by my test_printf.so .例如,在我的程序中,由于printf是一个外部符号,并且由我的test_printf.so So when I look into test_printf.so 's relocation table, there should be one entry names printf .所以当我查看test_printf.so的重定位表时,应该有一个条目名称printf I check that, and the entry exists.我检查了一下,条目存在。

Then I think, since printf is an external symbol, its location should be determined at runtime.那么我想,既然printf是一个外部符号,它的位置应该在运行时确定。 However, we must allocate a .got.plt section for this printf function, and the section should be in both a dynamicly linked executive and a dynamic library(test_printf.so).但是,我们必须为这个printf function 分配一个.got.plt部分,并且该部分应该在动态链接的执行程序和动态库(test_printf.so)中。

However, when I run readelf -S , there is no .got.plt section, and I am confused about this.但是,当我运行readelf -S时,没有.got.plt部分,我对此感到困惑。 Is this section not necessary in a dynamic library(test_printf.so)?动态库(test_printf.so)中是否不需要此部分? I don't think it is possible.我不认为这是可能的。 Suppose test_printf.so is finally linked with executive program a , then how can a know where is the .got.plt section for printf ?假设test_printf.so最终与执行程序a链接,那么a怎么知道 printf 的.got.plt部分在printf Does this .got.plt finally generated in a ?这个.got.plt最终是在a中生成的吗?

Meanwhile, I have a question 2. Are .rel.dyn and .rel.plt always present, if there are .got and .got.plt ?同时,我有一个问题 2. .rel.dyn.rel.plt是否总是存在,如果有.got.got.plt吗?

.rel.dyn and .got.plt can be present in a shared library .elf it depends on the structure of the functions in the library, this is in https://www.technovelty.org/linux/plt-and-got-the-key-to-code-sharing-and-dynamic-libraries.html .rel.dyn.got.plt可以存在于共享库中.elf它取决于库中函数的结构,这在https://www.technovelty.org/linux/plt-and-got -the-key-to-code-sharing-and-dynamic-libraries.html

.rela.dyn is present if on extern function is included:如果在外部.rela.dyn包括在内,则存在 .rela.dyn:

$ cat test.c
extern int foo;

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

Also see https://eli.thegreenplace.net/2011/08/25/load-time-relocation-of-shared-libraries/ and https://eli.thegreenplace.net/2011/11/03/position-independent-code-pic-in-shared-libraries另请参阅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 (linking static libraries into a dynamic library) https://blog.ramdoot.in/how-can-i-link-a-static-library-to-a-dynamic-library-e1f25c8095ef (将 static 库链接到动态库中)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM