programing

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

newsource 2023. 9. 27. 17:57

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). The variables in BSS will get a value 0 during program load time.

글로벌 변수가 명시적으로 0으로 초기화된 경우 (int myglobal = 0), 해당 변수는 어디에 저장됩니까?

컴파일러는 그러한 변수를 자유롭게 넣을 수 있습니다.bss로 뿐만 아니라data. 예를 들어, 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. 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. E.g., so that the resulting executable can find the beginning of that section and/or make assumptions based on that.

기본값은-fzero-initialized-in-bss.

다음 예를 사용하여 시도합니다(test.c파일):

int put_me_somewhere = 0;

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

옵션이 없는 컴파일링(묵시적으로)-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

컴파일링-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

우리는 그 장소를 찾고 있습니다0000000000601028그리고.000000000060102c:

$ 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

두 값이 모두 저장되어 있는 것 같습니다..bss시스템 섹션:gcc version 4.5.2 (Ubuntu/Linaro 4.5.2-8ubuntu4).

The behavior is dependent upon the C implementation. 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.

ReferenceURL : https://stackoverflow.com/questions/8721475/if-a-global-variable-is-initialized-to-0-will-it-go-to-bss