Linux size command, why are bss and data sections not zero?

  • A+
Category:Languages

I came across the size command which gives the section size of the ELF file. While playing around with it, I created an output file for the simplest C++ program :

int main(){return 0;} 

Clearly, I have not defined any initialized or uninitialized, data then why are my BSS and DATA sections of the size 512 and 8 bytes?

I thought it might be because of int main(), I tried creating object file for the following C program :

void main(){} 

I still don't get 0 for BSS and DATA sections.

Is it because a certain minimum sized memory is allocated to those section?

EDIT- I thought it might be because of linked libraries but my object is dynamically linked so probably it shouldn't be the issue

 


In fact, if you are compiling with the libc attached to the binary, there are functions that are added before (and after) the main() function. They are here mostly to load dynamic libraries (even if you do not need it in your case) and unload it properly once main() end.

These functions use a little bit of memory that is taken from the BSS and the DATA segment.

This is why, you will always see BSS and DATA in all the binaries compiled with the libc. If you want to get rid of this, then you should write your own assembly program, like this (asm.s):

.globl _start  _start:     mov %eax, %ebx 

And, then compile it without the libc:

$> gcc -nostdlib -o asm asm.s 

You should reduce your footprint to the BSS and DATA segment on this ELF binary.

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: