Integer variable size in bss and data segment

I am using a test program to understand the C memory model on Linux 6.3 with kernel version 2.6.32-279.el6.x86_64.

First I compile the code below,

#include <stdio.h>
int main(void)
{
    static int i = 100; /* Initialized static variable stored in DS*/
    return 0;
}

when running the size command, I got below

[root@rachitjain jan14]# size a.out
   text    data     bss     dec     hex filename
   1040     488      16    1544     608 a.out

then after removing intialization for static variable i, my code will be,

include <stdio.h>
int main(void)
{
    static int i ;
    return 0;
}

When you run the size after compiling above,

[root@rachitjain jan14]# size a.out
   text    data     bss     dec     hex filename
   1040     484      24    1548     60c a.out

There is an 8-byte step in the bss section, but only 4 bytes are reduced in the data section. Why is the size an integer when doubling when moving to the bss segment?

I tested this symbol and swam, observed the same behavioral.

+4
source share
1 answer

, , .bss 64 , .data .

? , ld script . , -Wl, -verbose:

g++ main.cpp -Wl,-verbose

64- , .bss:

  .bss            :
  {
   *(.dynbss)
   *(.bss .bss.* .gnu.linkonce.b.*)
   *(COMMON)
   /* Align here to ensure that the .bss section occupies space up to
      _end.  Align after .bss to ensure correct alignment even if the
      .bss section disappears because there are no input sections.
      FIXME: Why do we need it? When there is no .bss section, we don't
      pad the .data section.  */
   . = ALIGN(. != 0 ? 64 / 8 : 1);
  }

, ALIGN(. != 0 ? 64 / 8 : 1); , 8

32- (g++ -m32 main.cpp -Wl, -verbose), .bss:

  .bss            :
  {
   *(.dynbss)
   *(.bss .bss.* .gnu.linkonce.b.*)
   *(COMMON)
   /* Align here to ensure that the .bss section occupies space up to
      _end.  Align after .bss to ensure correct alignment even if the
      .bss section disappears because there are no input sections.
      FIXME: Why do we need it? When there is no .bss section, we don't
      pad the .data section.  */
   . = ALIGN(. != 0 ? 32 / 8 : 1);
  }

.data ALIGN script:

  .data           :
  {
    *(.data .data.* .gnu.linkonce.d.*)
    SORT(CONSTRUCTORS)
  }

:

+4

All Articles