It is known that .bss section was not stored in the disk, but the .bss section in memory should be initialized to zero. but where should it take in the memory? Is there any information displayed in the ELF header or the Is the .bss section likely to appear next to the data section, or something else??
The . bss section is used by the compiler for global and static variables. It is one of the default COFF sections that is used to reserve a specified amount of space in the memory map that can later be used for storing data. It is normally uninitialized.
'bss' is for the uninitialized data in RAM which is initialized with zero in the startup code.
In computer programming, the block starting symbol (abbreviated to . bss or bss) is the portion of an object file, executable, or assembly language code that contains statically allocated variables that are declared but have not been assigned a value yet. It is often referred to as the "bss section" or "bss segment".
The data segment contains initialized static variables, i.e. global variables and local static variables which have a defined value and can be modified.
The BSS is between the data and the heap, as detailed in this marvelous article.
You can find out the size of each section using size
:
cnicutar@lemon:~$ size try
text data bss dec hex filename
1108 496 16 1620 654 try
To know where the bss segment will be in memory, it is sufficient to run readelf -S program
, and check the Addr column on the .bss
row.
In most cases, you will also see that the initialized data section (.data
) comes immediately before. That is, you will see that Addr+Size of the .data
section matches the starting address of the .bss
section.
However, that is not always necessarily the case. These are historical conventions, and the ELF specification (to be read alongside the platform specific supplement, for instance Chapter 5 in the one covering 32-bit x86 machines) allows for much more sophisticated configurations, and not all of them are supported by Linux.
For instance, the section may not be called .bss
at all. The only 2 properties that make a BSS section such are:
SHT_NOBITS
(that is, it takes space in memory but none on the storage) which shows up as NOBITS
in readelf
's output.PT_LOAD
), readable (PF_R
), and writeable (PF_W
) segment. Such a segment is also shorter on storage than it is in memory (p_filesz
< p_memsz
).You can have multiple BSS sections: PowerPC executables may have .sbss
and .sbss2
for uninitialized data variables.
Finally, the BSS section is not necessarily adjacent to the data section or the heap. If you check the Linux kernel (more in particular the load_elf_binary
function) you can see that the BSS sections (or more precisely, the segment it maps to) may even be interleaved with code and initialized data. The Linux kernel manages to sort that out.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With