elf 加载器如何初始化全局变量
对于 C 中的全局变量,如
int aglobal = 5;
加载器什么时候将 5 转移到 aglobal 中?它如何知道将 5 放入 aglobal 中。
函数中的静态声明的情况相同。就像
int afunc() { 静态 int astatic = 8; 返回不稳定; }
For global variables in C like
int aglobal = 5;
When does the 5 get transferred into aglobal by the loader and how does it know to put 5 in aglobal.
Same situation with a static declaration in a function. Like
int afunc()
{
static int astatic = 8;
return astatic;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在数据段中创建一个 int 大小的空间,其中编码值 5,并将名为“aglobal”的全局非函数符号添加到指向它的符号表中。对 aglobal 的引用将转换为在链接时解析为指向该数据块的重定位,因此在完全链接的映像中,指令将直接从内存中保存 5 值的位置加载,
例如,(x86) 程序集可能看起来像这样:
在目标文件中,mov指令将变成带有重定位
R_386_32 aglobal+0
的mov eax, 0
,因为目标文件不知道确定数据部分在内存中的位置。在完全链接的图像中,可能是这样的:
现在数据部分中 4 个字节的实际地址已知,因此直接指定
An int-sized space is made in a data section, with the value 5 encoded in it and a global non-function symbol named 'aglobal' is added to the symbol table pointing at it. References to aglobal are turned into relocations that are resolved at link-time to point to that data block, so in a fully-linked image instructions will load directly from that spot in memory that holds the 5 value
For example, the (x86) assembly might look something like:
In an object file, the mov instruction will turn into
mov eax, 0
with a relocationR_386_32 aglobal+0
, because the object file doesn't know for sure where the data section will be in memory.In a fully-linked image, it might be something like:
Now the actual address of the 4 bytes in the data section is known, so it's specified directly