Why is a different memory size allocated to an integer in the BSS and in the data segment?

Read the following program -

#include <stdio.h>  
void main()
{
}

The memory allocated for each segment is as follows (using the size command on Unix) -

   text    data     bss     dec     hex filename
   1040     484      16    1540     604 try

After declaring a global variable -

#include <stdio.h>
int i;

void main()
{
}

The memory allocated for each segment is as follows (using the Unix size command) Here, the variable "i" received memory in BSS (earlier it was 16, but now it is 24) -

   text    data     bss     dec     hex filename
   1040     484      24    1548     60c try

After declaring a global variable and initializing it with 10 -

#include <stdio.h>
int i=10;

void main()
{
}

The memory allocated for each segment is as follows (using the Unix size command) Here the variable "i" received memory in the data segment (previously it was 484, but now it is 488) -

   text    data     bss     dec     hex filename
   1040     488      16    1544     608 try

, 'i' 8 , BSS, 4 , ? BSS ?

+4
1

"i" 8 , BSS, 4 , ?

First, why 4 bytes in data segment?

- .data , . 4 , int i=10;.

Now, why 8 bytes in .bss segment?

- script GNU linker GNU ld. script .

GNU linker (GNU ld) script.

script .bss.

script, , -

gcc -Wl,-verbose main.c

gcc :

using internal linker script:
==================================================
// The content between these two lines is the default linker script
==================================================

script .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 .

:

#include <stdio.h>
int i;

void main()
{
}

script, 'i' 8 BSS - 8 :

# size a.out
   text    data     bss     dec     hex filename
   1040     484      24    1548     60c a.out

[bss = 24 (16 + 8)]

GNU linker script, script script.

, script script GNU ld:

gcc -Xlinker -T my_linker_script main.c

script, .

.bss . = ALIGN(. != 0 ? 64 / 8 : 1); . = ALIGN(. != 0 ? 32 / 8 : 1);. 8 4 . script .

:

# size a.out
   text    data     bss     dec     hex filename
   1040     484      20    1544     608 a.out

bss size is 20 bytes (16 + 4) - 4 .

, .

+1

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


All Articles