为什么在静态链接的可执行文件中有全局偏移表和过程链接表? [英] Why are there global offset tables and procedure linkage tables in statically linked executables?
问题描述
我已经完成了大量有关动态链接器重定位和位置无关代码的阅读,其中包括过程链接表和全局偏移量表.我不明白为什么静态链接的可执行文件需要PLT和GOT.我在ubuntu x86_64机器上编译了一个hello world程序,当我用readelf -S
转储节头时,它会显示PLT和GOT节.
I've done a bunch of reading on dynamic linker relocations and position independent code including procedure linkage tables and global offset tables. I don't understand why a statically linked executable needs a PLT and GOT. I compiled a hello world program on my ubuntu x86_64 machine and when I dump the section headers with readelf -S
it shows PLT and GOT sections.
我还创建了一个共享库,它具有一个简单的增量函数,该函数使用gcc -shared
编译而没有-fpic
,并且还看到了PLT和GOT部分.我也没想到这一点.
I also created a shared library with a simple increment function that I compiled with gcc -shared
without -fpic
and I also see PLT and GOT sections. I didn't expect this either.
推荐答案
我不明白为什么静态链接的可执行文件需要PLT和GOT.
I don't understand why a statically linked executable needs a PLT and GOT.
不是.
我在ubuntu x86_64机器上编译了一个hello world程序,当我用readelf -S转储节头时,它会显示PLT和GOT节.
I compiled a hello world program on my ubuntu x86_64 machine and when I dump the section headers with readelf -S it shows PLT and GOT sections.
这是实施的偶然事件.这些部分来自crt1.o
,并且没有单独的crt1s.o
用于全静态链接,因此您最终从那里获得.plt
和.got
条目.
This is an accident of implementation. The sections come from crt1.o
, and there isn't a separate crt1s.o
for fully-static linking, so you end up with .plt
and .got
entries from there.
您可以删除这些部分,二进制文件仍将起作用:
You can strip these sections, and the binary will still work:
objcopy -R.got -R.plt a.out a.out2
注意:请勿剥离.rela.plt
,因为实现IFUNC
仍需要该部分.
Note: do not strip .rela.plt
, as that section is still needed to implement IFUNC
s.
这篇关于为什么在静态链接的可执行文件中有全局偏移表和过程链接表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!