未初始化的全局数据部分是什么? [英] What is the section for uninitialized global data?
问题描述
我对ELF文件中的未初始化全局变量的位置感到有点困惑。我有这个简单的程序来测试变量将在哪些部分:
I am a little confused as to where uninitialized global variables go in the ELF file. I have this simple program to test in which sections the variables will be located:
const int a = 11;
int b = 10;
int c;
int main()
{
return 0;
}
我知道未初始化的全局变量应该放入ELF文件的.bss段,但objdump -h给了我以下输出:
I know that uninitialized global variable should be put into .bss section of ELF file, but objdump -h gives me the following output:
Sections:
Idx Name Size VMA LMA File off Algn
0 .text 0000000a 00000000 00000000 00000034 2**2
CONTENTS, ALLOC, LOAD, READONLY, CODE
1 .data 00000004 00000000 00000000 00000040 2**2
CONTENTS, ALLOC, LOAD, DATA
2 .bss 00000000 00000000 00000000 00000044 2**2
ALLOC
3 .rodata 00000004 00000000 00000000 00000044 2**2
CONTENTS, ALLOC, LOAD, READONLY, DATA
4 .comment 00000024 00000000 00000000 00000048 2**0
CONTENTS, READONLY
5 .note.GNU-stack 00000000 00000000 00000000 0000006c 2**0
CONTENTS, READONLY
所以变量a转到.rodata,b转到.data,c不通?当我将代码更改为:
So the variable a goes to .rodata, b goes to .data, and c goes nowhere? When i change the code to:
int c = 0;
一切都如预期的那样 - .bss节的长度为4,但变量c发生了什么它没有被初始化?
everything is as expected - .bss section has the length 4, but what happens with the variable c when it is not initialized?
推荐答案
它进入公共区域。您可以使用 objdump -t
或使用 nm
来查看它。
It goes into a "common section". You can see it with objdump -t
or by using nm
.
我不太确定我明白这是关于什么的,但引用 ld
-warn-common
flag表示:
I'm not quite sure I understand what this is about, but the reference to the ld
-warn-common
flag says this:
int i;
常见符号。如果
变量只有
(一个或多个)通用符号,它将进入输出文件未初始化的
数据区域。
链接器将多个常见符号
合并为一个单一的
符号。如果它们具有不同的
尺寸,则它选择最大的尺寸。
链接器将一个通用符号变成
声明,如果有相同变量的定义
。
A common symbol. If there are only (one or more) common symbols for a variable, it goes in the uninitialized data area of the output file. The linker merges multiple common symbols for the same variable into a single symbol. If they are of different sizes, it picks the largest size. The linker turns a common symbol into a declaration, if there is a definition of the same variable.
(通过 nm
手册页找到)。手册页中有更多信息。
(Found via the nm
man page.) There is more information after that in the man page itself.
这篇关于未初始化的全局数据部分是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!