Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

c

memory

elf

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

like image 367
Time Traveller Avatar asked Aug 22 '18 06:08

Time Traveller


People also ask

Is BSS zeroed?

On some platforms, some or all of the bss section is initialized to zeroes. Unix-like systems and Windows initialize the bss section to zero, allowing C and C++ statically allocated variables initialized to values represented with all bits zero to be put in the bss segment.

Is BSS a RAM?

'bss' is for the uninitialized data in RAM which is initialized with zero in the startup code.

What is size command in Linux?

The size command in Linux is a very important command that will allow to list the section size and the total size of the object files or the archived files in its argument list. When we do not specify the object file in the parameter list, then by default, 'a. out' file is used.

Where is BSS stored?

bss are stored in RAM, but init_value is stored in ROM.


1 Answers

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 have global variables that require storage; uninitialized (zero initialized) global variables in the BSS segment and initialized global variables in 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.

like image 145
perror Avatar answered Sep 19 '22 20:09

perror