Sizing command for bss data segment in C

I get unexpected output from the command size.

Afaik initialized global and static variables stored in the segment data, not initialized and initialized to 0 global / static variables stored in the segment bss.

printf("%d",sizeof(int));It gives the size int4. However segment bssand datadoes not increase to 4 respectively.

#include <stdio.h>
int main()
{
    return 0;
}

C:\Program Files (x86)\Dev-Cpp\MinGW64\bin>size memory-layout.exe
   text    data     bss     dec     hex filename
  10044    2292    2512   14848    3a00 memory-layout.exe

#include <stdio.h>
int g; //uninitialised global variable so, stored in bss segment
int main()
{
    return 0;
}

C:\Program Files (x86)\Dev-Cpp\MinGW64\bin>size memory-layout.exe
   text    data     bss     dec     hex filename
  10044    2292    2528   14864    3a10 memory-layout.exe

why bssincreased by 16 (2528 - 2512) instead of 4? (in the code above)

#include <stdio.h>
int g=0; //initialised to 0 so, stored in bss segment
int main()
{
    return 0;
}

C:\Program Files (x86)\Dev-Cpp\MinGW64\bin>size memory-layout.exe
   text    data     bss     dec     hex filename
  10044    2292    2512   14848    3a00 memory-layout.exe

there is bssno increment despite using a global variable. Why is this?

 #include <stdio.h>
int main()
{   static int g; //should be on bss segment
    return 0;
}

C:\Program Files (x86)\Dev-Cpp\MinGW64\bin>size memory-layout.ex
   text    data     bss     dec     hex filename
  10044    2292    2512   14848    3a00 memory-layout.exe

no segment increment bssdespite using a static variable, why?

, dec?

+4
3

, , - . , . 16 , 16- (2512/16 = 157, 2528/16 = 158). .

C, , , int g , ( ). , , .

g 0, , BSS.

:

int g ( BSS)

.comm   g,4,4

, , .

int g = 0

    .bss
    .align 4
    .type   g, @object
    .size   g, 4
g:
    .zero   4

, BSS .

. BSS , BSS. , nm.

nm -n file2 file3 | grep g$

000000000060103c B g
000000000060103c B g

. g . , :

(gdb) info symbol 0x60103c
g in section .bss of /tmp/file2

, , .

dec, .

+1

gcc on linux:

No Variable
   text    data     bss     dec     hex filename
    915     248       8    1171     493 none.out
Uninitialized Global
   text    data     bss     dec     hex filename
    915     248      12    1175     497 u_g.out
Initialized Global to 123
   text    data     bss     dec     hex filename
    915     252       8    1175     497 i_g.out
Initialized Local to 124
   text    data     bss     dec     hex filename
    915     252       8    1175     497 i_l.out
Initialized Global to 0
   text    data     bss     dec     hex filename
    915     248      12    1175     497 i_g_0.out
Initialized Local to 0
   text    data     bss     dec     hex filename
    915     248      12    1175     497 i_l_0.out

mingw64 Windows:

No Variable
   text    data     bss     dec     hex filename
   3173    1976     448    5597    15dd none.out
Uninitialized Global
   text    data     bss     dec     hex filename
   3173    1976     464    5613    15ed u_g.out
Initialized Global to 123
   text    data     bss     dec     hex filename
   3173    1976     448    5597    15dd i_g.out
Initialized Local to 124
   text    data     bss     dec     hex filename
   3173    1976     448    5597    15dd i_l.out
Initialized Global to 0
   text    data     bss     dec     hex filename
   3173    1976     480    5629    15fd i_g_0.out
Initialized Local to 0
   text    data     bss     dec     hex filename
   3173    1976     480    5629    15fd i_l_0.out

, ( ), Windows / MinGW (.. gcc).

0

BSS contains only static and global values ​​that are not explicitly initialized. Even if you explicitly initialize it to the same value that would be initialized, if it was not initialized explicitly, the fact of explicit initialization means that it does not belong to bss.

0
source

All Articles