简体   繁体   English

如果全局变量初始化为0,它会转到BSS吗?

[英]If a global variable is initialized to 0, will it go to BSS?

All the initialized global/static variables will go to initialized data section . 所有初始化的全局/静态变量都将转到初始化数据部分 All the uninitialized global/static variables will go to uninitialed data section (BSS). 所有未初始化的全局/静态变量将转到uninitialed数据部分 (BSS)。 The variables in BSS will get a value 0 during program load time. 在程序加载时间内,BSS中的变量将获得值0。

If a global variable is explicitly initialized to zero ( int myglobal = 0 ), where that variable will be stored? 如果全局变量显式初始化为零( int myglobal = 0 ),那么该变量将被存储在哪里?

Compiler is free to put such variable into bss as well as into data . 编译器可以自由地将这样的变量放入bss以及data For example, GCC has a special option controlling such behavior: 例如,GCC有一个控制此类行为的特殊选项

-fno-zero-initialized-in-bss

If the target supports a BSS section, GCC by default puts variables that are initialized to zero into BSS. 如果目标支持BSS部分,则GCC默认将初始化为零的变量放入BSS。 This can save space in the resulting code. 这可以节省生成的代码中的空间。 This option turns off this behavior because some programs explicitly rely on variables going to the data section. 此选项会关闭此行为,因为某些程序明确依赖于转到数据部分的变量。 Eg, so that the resulting executable can find the beginning of that section and/or make assumptions based on that. 例如,以便生成的可执行文件可以找到该部分的开头和/或基于此做出假设。

The default is -fzero-initialized-in-bss . 默认值为-fzero-initialized-in-bss

Tried with the following example ( test.c file): 尝试使用以下示例( test.c文件):

int put_me_somewhere = 0;

int main(int argc, char* argv[]) { return 0; }

Compiling with no options (implicitly -fzero-initialized-in-bss ): 没有选项编译(隐式-fzero-initialized-in-bss ):

$ touch test.c && make test && objdump -x test | grep put_me_somewhere
cc     test.c   -o test
0000000000601028 g     O .bss   0000000000000004              put_me_somewhere

Compiling with -fno-zero-initialized-in-bss option: 使用-fno-zero-initialized-in-bss选项进行编译:

$ touch test.c && make test CFLAGS=-fno-zero-initialized-in-bss && objdump -x test | grep put_me_somewhere
cc -fno-zero-initialized-in-bss    test.c   -o test
0000000000601018 g     O .data  0000000000000004              put_me_somewhere

It's easy enough to test for a specific compiler: 测试特定的编译器很容易:

$ cat bss.c
int global_no_value;
int global_initialized = 0;

int main(int argc, char* argv[]) {
    return 0;
}
$ make bss
cc     bss.c   -o bss
$ readelf -s bss | grep global_
    32: 0000000000400420     0 FUNC    LOCAL  DEFAULT   13 __do_global_dtors_aux
    40: 0000000000400570     0 FUNC    LOCAL  DEFAULT   13 __do_global_ctors_aux
    55: 0000000000601028     4 OBJECT  GLOBAL DEFAULT   25 global_initialized
    60: 000000000060102c     4 OBJECT  GLOBAL DEFAULT   25 global_no_value

We're looking for the location of 0000000000601028 and 000000000060102c : 我们正在寻找0000000000601028000000000060102c的位置:

$ readelf -S bss
There are 30 section headers, starting at offset 0x1170:

Section Headers:
  [Nr] Name              Type             Address           Offset
       Size              EntSize          Flags  Link  Info  Align
...
  [24] .data             PROGBITS         0000000000601008  00001008
       0000000000000010  0000000000000000  WA       0     0     8
  [25] .bss              NOBITS           0000000000601018  00001018
       0000000000000018  0000000000000000  WA       0     0     8

It looks like both values are stored in the .bss section on my system: gcc version 4.5.2 (Ubuntu/Linaro 4.5.2-8ubuntu4) . 看起来这两个值都存储在我的系统的.bss部分: gcc version 4.5.2 (Ubuntu/Linaro 4.5.2-8ubuntu4)

The behavior is dependent upon the C implementation. 行为取决于C实现。 It may end up in either .data or .bss, and to increase changes that it does not end up in .data taking redundant space up, it's better not to explicitly initialize it to 0, since it will be set to 0 anyway if the object is of static duration. 它可能最终以.data或.bss结尾,并且为了增加它不会最终的变化.data占用冗余空间,最好不要将它显式初始化为0,因为无论如何它都将被设置为0对象具有静态持续时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在C中,是数据段中malloc()初始化的全局指针还是BSS? - In C, is a global pointer initialized by malloc() in the data segment or the BSS? 为什么.bss将全局变量显式初始化为零? - why .bss explicitly initialize global variable to zero? 添加初始化的静态变量时,为什么 .bss 大小会减小? - Why .bss size decrease when adding an initialized static variable? 全局变量始终初始化为零 - global variable always initialized zero c中的初始化vs未初始化的全局const变量 - initialized vs uninitialized global const variable in c 在main.o的'.text'节中引用的全局变量在废弃的'.bss'节中定义 - Global variable referenced in section '.text' of main.o defined in discarded section '.bss' 为什么在代码中没有未初始化的全局或静态变量的情况下,bss段为何包含前4个字节 - why does bss segment contain initial 4 bytes when no uninitialised global or static variable is there in code 为什么即使添加了未初始化的全局变量,size命令显示的bss值也不会增加? - why bss value shown by size command doesn't increase even after adding uninitialized global variable? 如何.bss部分初始化为零 - how about .bss section not zero initialized 在基于Eclipse的IDE中将全局变量初始化为错误的值-故障排除 - In Eclipse-based IDE global variable initialized to a wrong value - troubleshooting
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM