Program size mismatch between bin and print out

Hello,

while trying to implement a bootloader on a custom F303CC I wanted to understand elf files and use this for flashing my uC to have more information about the addresses used etc.

Project config:
platform = ststm32
board = robotdyn_blackpill_f303cc
framework = cmsis

Linker script is the default one provided by Cube.

To do so, I created a simple program in C# to generate a bin file out of elf and compare it to the bin generated during pio build.
For STM32, ROM sits at address 0x800 0000, and RAM at 0x200 0000.
But the bin file also contains the data section at location 0x200 0000.

The linker file also states for the data section:
/* Initialized data sections goes into RAM, load LMA copy after code */

Therefore init values need to be stored in flash.

So I added the section and my total flash size equals 5748 bytes including padding (see Total size in screenshot).

All headers from the elf file are also listed.

But in pio flash size is reported as:
RAM: [== ] 17.1% (used 7004 bytes from 40960 bytes)
Flash: [========= ] 87.0% (used 5344 bytes from 6144 bytes)

This behavior is similar with an arduino project on F303CC:


Checking size .pio\build\robotdyn_blackpill_f303cc\firmware.elf
Advanced Memory Usage is available via “PlatformIO Home > Project Inspect”
RAM: [== ] 15.1% (used 6192 bytes from 40960 bytes)
Flash: [= ] 9.8% (used 25204 bytes from 256000 bytes)

Is this intentional? Or is it just an oversight and just a check for 0x8 memory addresses is done?
Which also does not fit the size used.

Best Regards
Kevin

PlatformIO extracts the “RAM” and “Flash” numbers in this case by using only these sections

These are the regular-expressions used for parsing the text-output of the arm-none-eabi-size tool.

So e.g., when I build the platform-ststm32/examples/cmsis-blink at develop · platformio/platform-ststm32 · GitHub example with your platformio.ini, I get

RAM:   [          ]   0.1% (used 28 bytes from 40960 bytes)
Flash: [          ]   0.1% (used 344 bytes from 262144 bytes)

which comes from

> C:\Users\Max\.platformio\packages\toolchain-gccarmnoneeabi\bin\arm-none-eabi-size.exe -A -d .\.pio\build\robotdyn_blackpill_f303cc\firmware.elf 
.\.pio\build\robotdyn_blackpill_f303cc\firmware.elf  :
section             size        addr
.isr_vector          392   134217728
.text                344   134218120
.rodata                0   134218464
.ARM.extab             0   134218464
.ARM                   0   134218464
.preinit_array         0   134218464
.init_array            4   134218464
.fini_array            4   134218468
.data                  0   536870912
.bss                  28   536870912
._user_heap_stack   1540   536870940
.ARM.attributes       42           0
.comment             126           0
.debug_frame          44           0
Total               2524

Thus FLASH = .text + .data + .rodata (here; all other empty) and thus 344 + 0 + 0 = 344 which is the same as PlatformIO says.

For RAM, only the sections .data + .bss + .noinit are considered and that is in this case just .bss and 28 bytes.

For your example, FLASH = 4592 + 560 + 192 = 5344 which matches

If the linker file has section names which are not listed in this regular expression but still contribute to flash or RAM, they are not counted. The size-tool and the PIO core logic is not intelligent enough to figure out that certain sections do end up in RAM or Flash after all based on its address.

Notice that it also ignores the core-coupled-memory (CCM) RAM.

So your calculations might be more correct than PlatformIO ones’.