Why doesn't the .bss segment grow when variables are added?

I recently found out that a segment .bssstores uninitialized data. However, when I try a small program, as shown below, and use the command size(1)in the terminal, the .bss segment has not changed, even if I add some global variables. Do I not understand something?

jameschu@aspire-e5-573g:~$ cat test.c
#include <stdio.h>

int main(void)
    {
  printf("hello world\n");
  return 0;
}
jameschu@aspire-e5-573g:~$ gcc -c test.c 
jameschu@aspire-e5-573g:~$ size test.o
   text    data     bss     dec     hex filename
     89       0       0      89      59 test.o
jameschu@aspire-e5-573g:~$ cat test.c
#include <stdio.h>
int a1;
int a2;
int a3;

int main(void)
{
  printf("hello world\n");
  return 0;
}
jameschu@aspire-e5-573g:~$ gcc -c test.c 
jameschu@aspire-e5-573g:~$ size test.o
   text    data     bss     dec     hex filename
     89       0       0      89      59 test.o
+4
source share
1 answer

This is due to the way global variables work.

, , , .c . , external, .

? :

  • bss ​​ COMMON.
  • , , COMMON , . bss .

bss , .

, size, objdump -x. , *COM*.

, static, , , COMMON , :

int a;
int b;
static int c;

$ size test.o
text       data     bss     dec     hex filename
 91       0       4      95      5f test.o

0 .

int a;
int b;
int c = 0;

$ size test.o
text      data    bss    dec     hex    filename
 91       0       4      95      5f test.o

-, 0, data:

int a;
int b = 1;
int c = 0;

$ size test.o
text      data    bss    dec     hex    filename
 91       4       4      99      5f test.o
+3

Source: https://habr.com/ru/post/1690476/


All Articles