Tools for Firmware Code Size Optimization

It is mentioned that the reason for the overflow in flash is the fact that initialization values for our initialized static variables are stored in flash as well. Does this mean that if we create a static variable (int) with value 3, this value will be stored in the flash memory, while the variable will be mapped to RAM like normal a variable? Is this commonly used in the embedded systems world or is it a platform-specific implementation?

Afterwards, in ‘Digging into functions’ we can see the size of each symbol. But what does it really mean? Does it refer to the entry in the symbol table?

Thank you in advance

Thanks for the good questions @konark!

Does this mean that if we create a static variable (int) with value 3, this value will be stored in the flash memory, while the variable will be mapped to RAM like normal a variable?

You’ve got this mostly right. When you write the following code:

static int my_var = 3;

The linker will do two things:

  1. Allocate some space in program space (i.e. flash) for the initialization value of your variable. Concretely this will be a 32-bit word with the value 0x00000003 in it.
  2. Create an address in data space (i.e. RAM) for that variable. Concretely one 32-bit word of memory will be earmarked to hold that data in RAM.

When your program (or firmware) starts, the initial value will be copied from flash to RAM. This is typically done in the Reset_Handler for firmware projects. You can read more about it in our “Zero to Main” blog post: From Zero to main(): Bare metal C | Interrupt.

Is this commonly used in the embedded systems world or is it a platform-specific implementation?

This is not only common to embedded, but to most C programs on other platforms as well. Initial values of static variables must be stored somewhere before RAM is allocated for the program!

Afterwards, in ‘Digging into functions’ we can see the size of each symbol. But what does it really mean? Does it refer to the entry in the symbol table?

This is the size of the code in flash. i.e. the amount of flash space used to store the instructions contained within that function. Does that make sense?

Best,

François