Know What is .bss .text .data memory segments of an executable file in embedded systems

Sharmila Shahul
iQube
Published in
3 min readJun 13, 2020

Embedded devices has become an essential part of human life. Every embedded devices like smart phones,wearable devices etc., needs to be programmed with an executable embedded firmware in assembly level code.Embedded firmware are written in Embedded C (high level language).It is then preprocessed ,compiled and linked to give the single executable object file in .hex ,.bin , .elf (executable and linkable file)formats based on the IDE(Integrated development environment)settings. This object file is finally loaded into the target embedded device processor’s code memory block for functional application execution.

Every code build has .bss, .data, .text segments contained in the final executable file. Let us see in detail,what does every section holds

1.0 Build_output sections

.bss segment stands for ‘block start by symbol’ is the memory space for uninitialized variables of your code. It is a method of optimization to reduce the code size.

The uninitialized variables in .bss area will be assigned with zero during the startup code. Start up code is the program code that executes immediately after device reset.

.bss files essentially reduce the size of the executable output of the built code,which is one of the critical factors in embedded devices with minimal flash or optimal memory block for code.

.bss has some space occupied not essentially the same size. when later that variable is assigned with some value during the execution will take the actual size in system memory .

A buffer that takes up 12,000 plus bytes (or more, sometimes much more) can be defined in .bss and add almost nothing (about 4–8 bytes for the description) to the executable file size.

So the reason for .bss is to have smaller executable, saving space and allowing faster loading of the program, as the loader(startup) can just allocate a bunch of zeros instead of having to copy the data from disk. So .bss section is for the uninitialized data in RAM which is initialized with zero in the startup code.

Eg: int i; uint32_t my_var; /* Unintialized i , my_var will be put in .bss memory section*/

.data section holds the initialized value. .data holds the data of the initialized variable(global or local)

For Example :int example_variable = 0xBEEFDEAD;

example_variable goes to the RAM area, value will be stored in the FLASH. Then the startup code will copy the data to the variable address,which process is described as “copydown”. Initialization will happen during the startup code

.data section holds the data that has been initialized and adds the actual memory size of the variables(global & static) to the executable.The linker allocates the data in FLASH which then is copied from ROM to RAM in the startup code.

‘.text’ segment is the code, vector table and constants. It is the section that holds the executable instructions.

Eg: .void HAL_MspInit(); /* this will be put in text memory segment*/

Stack and heap are also the memory section that is to be initialized in the linker file, which will be occupying a memory block in executable file. To be short,stack is the place holder for all the temporary variables getting called inside the functions etc., during the execution of the program. Heap is used for the dynamic memory locations.

There are lot more things beyond this to go deep in embedded code compilation and buildingAlso check the output.map file generated during compilation for knowing more about the above memory segments.

--

--

Sharmila Shahul
iQube
Editor for

Tech aspirant working on embedded systems,IOT and Automation. I am not a professional writer, but would like to let others know what I have learnt.