Wrong RAM and FLASH used values when building

I am using a custom board based on STM322WB nucleo board, the project builds correctly but the size is clearly wrong, as it retrieves 0 bytes for flash and ram. When running inspect from PIO Home it fails with error:
PIO Core Call Error: "Processing debug (platform: git+ssh://git-codecommit.us-east-1.amazonaws.com/v1/repos/EyC_FW_PIO_Platform_ststm32; board: eTraffic_poc; framework: libopencm3-stm32wb)\r\n--------------------------------------------------------------------------------\r\nVerbose mode can be enabled via -v, --verbose option\r\nCONFIGURATION: https://docs.platformio.org/page/boards/ststm32/eTraffic_poc.html\r\nPLATFORM: ST STM32 (17.3.0+sha.bde6c3d) > eTraffic Proof Of Concept\r\nHARDWARE: STM32WB55RG 64MHz, 192KB RAM, 512KB Flash\r\nDEBUG: Current (stlink) On-board (stlink) External (blackmagic, cmsis-dap, jlink)\r\nPACKAGES: \r\n - framework-libopencm3-stm32wb @ 1.10000.200730+sha.01dfe00 \r\n - toolchain-gccarmnoneeabi @ 1.70201.0 (7.2.1)\r\nLDF: Library Dependency Finder -> https://bit.ly/configure-pio-ldf\r\nLDF Modes: Finder ~ deep+, Compatibility ~ soft\r\nFound 4 compatible libraries\r\nScanning dependencies...\r\nDependency Graph\r\n|-- FreeRTOS @ 1.0.0+sha.93e1bc2\r\n|-- GPIO @ 1.0.0+sha.58a1153\r\n|-- EYCON @ 1.0.0+sha.51da333\r\n|-- UART @ 2.0.0+sha.8d5a4db\r\nBuilding in debug mode\r\nChecking size .pio\\build\\debug\\firmware.elf\r\nCalculating size .pio\\build\\debug\\firmware.elf\r\n text\t data\t bss\t dec\t hex\tfilename\r\n 0\t 0\t 1540\t 1540\t 604\t.pio\\build\\debug\\firmware.elf\r\nAdvanced Memory Usage is available via \"PlatformIO Home > Project Inspect\"\r\nRAM: [ ] 0.0% (used 0 bytes from 196608 bytes)\r\nFlash: [ ] 0.0% (used 0 bytes from 524288 bytes)\r\nGenerating memory usage report...\r\n\n\nElf file doesn't contain DWARF informationscons: *** [sizedata] Explicit exit, status 1\r\n========================== [FAILED] Took 3.62 seconds =========================="

This package isn’t public, so I have zero clue what the used linker script here is.

The main point here that, after the .elf file has been built, PlatformIO will run the “size tool” (here arm-none-eabi-size) on the ELF file. This will print out each section of the ELF file.

If the section name then matches the regex stored in env["SIZEPROGREGEXP"], it counts towards Flash usage, if it matches the regex stored in env["SIZEDATAREGEXP"], it counts towards RAM usage. These regular expressions are by default

Naturally, the correctness of this depends on the used linker script. However, most of the linker scripts have converged to use the same names (.text for where functions are stored, .rodata for read-only variables, .data for initial values of variables, .bss for uninitializedvariables, etc.). So, if your linker script is weird, non-normal, then those regexes won’t match and you’ll have no matches, resulting in 0 byte displayed usage.

Thus:

  • double check if the right linker script is used. This project seems like it has lots of custom components in it, so it may just be using the wrong linker script. Use CLIpio run -t clean and pio run -v -j1 to check the final -Wl, -T <linker script> invocation.
  • modify the regular expression to match the naming used in the linker script. This can be done with a pre extra_scripts directive in the platformio.ini. The script can just say
Import("env")
env.Replace(
  SIZEPROGREGEXP=r"^(?:\.text|\.data|\.rodata|\.text.align|\.ARM.exidx)\s+(\d+).*",
  SIZEDATAREGEXP=r"^(?:\.data|\.bss|\.noinit)\s+(\d+).*",
)

with whatever new regex you need. You can check if the regex matches by executing arm-none-eabi-size -A -d .pio\build\debug\firmware.elf to produce the output and https://regexr.com/ to test the regex.

Thanks @maxgerhardt ! The linker script file was the problem. We were using the one generated by cubeIDE modified to haver the vector_table and reset_handler correctly spelled, but we didn’t change the names of the sections. It now works as expected!