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
. This is because both of these sections act like what .rel.data
and .rel.text
in static linked programs do.
For example, in my program, since printf
is an external symbol, and is referred by my test_printf.so
. So when I look into test_printf.so
's relocation table, there should be one entry names printf
. I check that, and the entry exists.
Then I think, since printf
is an external symbol, its location should be determined at runtime. 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).
However, when I run readelf -S
, there is no .got.plt
section, and I am confused about this. Is this section not necessary in a dynamic library(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
? Does this .got.plt
finally generated in a
?
Meanwhile, I have a question 2. Are .rel.dyn
and .rel.plt
always present, if there are .got
and .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.rela.dyn
is present if on extern function is included :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://blog.ramdoot.in/how-can-i-link-a-static-library-to-a-dynamic-library-e1f25c8095ef (linking static libraries into a dynamic library)