Professional Documents
Culture Documents
MemoryRegions 01
MemoryRegions 01
MemoryRegions 01
In “Code Size Information with gcc for ARM/Kinetis” I use an option in the ARM gcc tool chain for Eclipse to show me the code
size:
I have been asked by a reader of this blog what these item numbers really mean. Especially: what the heck is ‘bss’????
Note: I’m using the ARM GNU ‘printsize’ utility for gcc, with an example for Kinetis-L (KL25Z).
text
‘text’ is what ends up in FLASH memory. I can show this with adding
1 void foo(void) {
2 /* dummy function to show how this adds to 'text' */
3 }
Likewise, my new function ‘foo’ gets added to the .text segment, as I can see in the map file generated by the linker:
*(.text*)
.text.foo 0x000008c8 0x8 ./Sources/main_c.o
0x000008c8 foo
But it does not only contain functions, it has constant data as too. If I have a constant table like
then this adds to ‘text’ too. That variable ‘table’ will be in FLASH, initialized with the values specified in the source.
Another thing which is included in ‘text’ is the interrupt vector table (more on this later).
In summary: ‘text’ is what ends up typically in FLASH and has code and constant data.
data
‘data’ is used for initialized data. This is best explained with the following (global/extern) variable:
Adding above variable to my application will increase the ‘data’ portion by 4 bytes:
This variable ‘myVar’ is not constant, so it will end up in RAM. But the initialization (0x12345678) *is* constant, and can live in
FLASH memory. The initialization of the variable is done during the normal ANSI startup code. The code will assign/copy the
initialization value. This is sometimes named ‘copy-down’. For the startup code used by CodeWarrior for MCU10.3 for Kinetis-L
(ARM Cortex-M0+), this is performed in __copy_rom_sections_to_ram():
https://www.printwhatyoulike.com/print?url=https%3A%2F%2Fmcuoneclipse.com%2F2013%2F04%2F14%2Ftext-data-and-bss-code-and-data-si… 1/4
28/01/2021 PrintWhatYouLike on text, data and bss: Code and Data Size Explained | MCU on Eclipse
Just one thing to consider: my variable ‘myVar’ will use space in RAM (4 bytes in my case), *plus* space in FLASH/ROM for the
initialization value (0x12345678). So I need to count the ‘data’ size twice: that size will end up in RAM, plus will occupy
FLASH/ROM. That amount of data in FLASH is *not* counted in the text portion.
The ‘data’ only has the initialization data (in my example 0x12345678. And not the variable (myVar).
bss
bss (or .bss, or BSS) is the abbreviation for ‘Block Started by Symbol’ by an old assembler (see this link).
1 int32_t myGlobal;
I like to remember ‘bss’ as ‘Better Save Space’ :-). As bss ends up in RAM, and RAM is very valuable for a microcontroller, I
want to keep the amount of variables which end up in the .bss at the absolute minimum.
The bss segment is initialized in the startup code by the zero_fill_bss() function:
dec
The ‘dec’ (as a decimal number) is the sum of text, data and bss:
size [-A|-B|--format=compatibility]
[--help]
[-d|-o|-x|--radix=number]
[--common]
[-t|--totals]
[--target=bfdname] [-V|--version]
[objfile...]
https://www.printwhatyoulike.com/print?url=https%3A%2F%2Fmcuoneclipse.com%2F2013%2F04%2F14%2Ftext-data-and-bss-code-and-data-si… 2/4
28/01/2021 PrintWhatYouLike on text, data and bss: Code and Data Size Explained | MCU on Eclipse
I’m using an ARM Cortex-M0+ in my example, so addresses greater 0x1ffff000 are in RAM.
The lines from .ARM.attributes up to .debug_frame are not ending up in the target, they are debug and other information.
.interrupts is my interrupt vector table, and .text is my code plus constants, and is in FLASH memory. That makes the
0xc0+0x134c=0x140c for text in ‘Berkeley’.
https://www.printwhatyoulike.com/print?url=https%3A%2F%2Fmcuoneclipse.com%2F2013%2F04%2F14%2Ftext-data-and-bss-code-and-data-si… 3/4
28/01/2021 PrintWhatYouLike on text, data and bss: Code and Data Size Explained | MCU on Eclipse
.bss is my uninitialized (zero-outed) variable area. Additionally there is .user_heap_stack: this is the heap defined in the ANSI
library for malloc() calls. That makes the total of 0x1c+0x800=0x81c shown in ‘Berkeley’ format.
The .romp is used by the linker for the ‘copy-down’ and initialization of .data. But it looks confusing: it is shown with addresses
in RAM? Checking the linker map file shows:
Ah! That actually is not in RAM, but in FLASH: the linker maps this to the FLASH address 0x1b60! So this size 0x18 really
needs to be added to the FLASH size too!
Summary
I hope I have sorted out things in a correct way. The way how the initialized data is reported might be confusing. But with the
right knowledge (and .map file in mind), things get much clearer:
‘data’ is for initialized variables, and it counts for RAM and FLASH. The linker allocates the data in FLASH which then is copied
from ROM to RAM in the startup code.
‘bss’ is for the uninitialized data in RAM which is initialized with zero in the startup code.
Happy Sizing
https://www.printwhatyoulike.com/print?url=https%3A%2F%2Fmcuoneclipse.com%2F2013%2F04%2F14%2Ftext-data-and-bss-code-and-data-si… 4/4