过程链接表和全局偏移表 [英] Process Linkage Table and Global Offset Table
问题描述
我读本文就PLT(过程链接表)和GOT(全局偏移表)。虽然PLT的目的,我很清楚,我仍然感到困惑GOT。我已经从文章理解的是,GOT仅供声明为共享库的extern
变量必要的。对于声明为共享库code静态
全局变量,它不是必需的。
I am reading this article on PLT (Process Linkage Table) and GOT (Global Offset Table). While the purpose of PLT is clear to me, I'm still confused about GOT. What I've understood from the article is that GOT is only necessary for variables declared as extern
in a shared library. For global variables declared as static
in a shared library code, it is not required.
我的理解是正确的,还是我完全错过了点。
Is my understanding right, or am I completely missing the point.
推荐答案
也许你的困惑与的含义的extern
。由于默认连接是的extern
,对外宣称的功能范围,而不静态
关键字的任何变量是 EXTERN
。
Perhaps your confusion is with the meaning of extern
. Since the default linkage is extern
, any variable declared outside function scope without the static
keyword is extern
.
究其原因,GOT是必要的,因为是通过共享库code访问的变量的地址不是在生成共享库时已知的。这取决于无论是在加载地址库被加载时(如果定义在库本身)或C的变量定义(如果定义是其他地方)的第三方$ C $。因此,而不是把地址内嵌在code,编译器生成code读取共享库的GOT,然后在运行时加载从GOT的地址。
The reason the GOT is necessary is because the address of variables accessed by the shared library code is not known at the time the shared library is generated. It depends either on the load address the library gets loaded at (if the definition is in the library itself) or the third-party code the variable is defined in (if the definition is elsewhere). So rather than putting the address inline in the code, the compiler generates code to read the shared library's GOT and then loads the address from the GOT at runtime.
如果该变量的知的相同的共享库中定义(可能是因为它的静态
或隐藏
或保护
知名度属性它使用),那么该地址的相对于库中的code 的可固定在当时产生的共享库文件。在这种情况下,而不是通过在GOT执行查找,编译器只产生code。与程序计数器相对寻址到访问变量。这两者在运行时加载时成本较低(因为整个符号查找和搬迁过程可以在加载时被忽略)。
If the variable is known to be defined within the same shared library (either because it's static
or the hidden
or protected
visibility attribute it used) then the address relative to the code in the library can be fixed at the time the shared library file is generated. In this case, rather than performing a lookup through the GOT, the compiler just generates code to access the variable with program-counter-relative addressing. This is less expensive both at runtime and at load time (because the whole symbol lookup and relocation process can be skipped at load time).
这篇关于过程链接表和全局偏移表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!