4

1st case:

#include <stdio.h>

int main(void)
{
    return 0;
}

Size output:

text       data     bss     dec     hex filename

1115        552       8    1675     68b ./a.out

2nd case:

#include <stdio.h>

int global;  // new line compared to previous case

int main(void)
{
    return 0;
}

size output:

text       data     bss     dec     hex filename
1115        552       8    1675     68b ./a.out

Ideally it should be:

bss=12 and all other (text and data) same

3rd case:

#include <stdio.h>

int global;

int main(void)
{
    static int i;  // new line compared to previous case
    return 0;
}

size output:

text       data     bss     dec     hex filename
1115        552      16    1683     693 ./a.out

this is correct

Why the output in 2nd case is not correct?

Patrick
  • 2,464
  • 3
  • 30
  • 47
  • 3
    What are your optimization flags? – zoul Mar 05 '16 at 09:28
  • 5
    It is like ignored by the compiler because it is not used at all. – Ian Mar 05 '16 at 09:32
  • 1
    In the third case, did you keep the `global` variable? – Joël Hecht Mar 05 '16 at 09:44
  • Check this: http://stackoverflow.com/questions/29545191/prevent-gcc-from-removing-an-unused-variable – hek2mgl Mar 05 '16 at 09:52
  • 2
    To explain for certain you need to look into the ELF info contained in the file. Personally I don't think it is to do with the variable being optimised out. The bss section usually comes before a dynamic section. The dynamic section needs to be 8 byte aligned. Thus the bss section needs to be padded to ensure the dynamic section starts at the right alignment. In first case there are no user bss vars. But there is always at least one bss symbol `__bss_start`. So the bss size is 8. When a 4 byte bss variable is added, it fits into the padding space and hence the overall bss size does not change. – kaylum Mar 05 '16 at 10:13
  • @zoul How to know optimization flags? I have compile without any option like gcc test.c – Patrick Mar 05 '16 at 10:16
  • @pythonlearner try `gcc test.c -O0` If that gives you the same output we can say relatively definitively that @kaylum's comment is correct. – Magisch Mar 05 '16 at 10:24
  • @Magisch with -O0 also same result – Patrick Mar 05 '16 at 10:29
  • Try the third case without `global` variable. If bss is then 8, I'll have a explanation. On the other case, it'll be a mystery... (the excplanation would be like @kaylum said). – Joël Hecht Mar 05 '16 at 10:36
  • @JoëlHecht Yes bss is 8 for third case without *global variable* – Patrick Mar 05 '16 at 10:45
  • 1
    Most likely it allocates bss only in 8-byte units, and stdio.h or something did a 4-byte allocation. You could confirm this by generating a map file which will list the names of everything in those segments – M.M Mar 05 '16 at 11:43

1 Answers1

2

You are probably compiling for a 64-bit architecture, where you get memory aligned to 8 bytes (64 bits).

A program as simple as the one in your first case has a 4-byte starting bss, but 8 bytes is allocated for alignment purposes, so as you declared the global variable, you filled up the left 4 bytes.

Declaring another 4-bytes variable will add 8 bytes to the bss, until it also gets filled, and so on.

Jefferson
  • 529
  • 4
  • 9