You could use pyelftools to load the elf and compute text/data/bss sums for each memory region you want using those rules.
I wrote two python script to compute sum: elf_test_rt105x.py and elf_test_stm32.py.
One is for STM32 and the other is for RT105x. The difference between the two is only the map of the memory regions and their addresses.
I tried it on an elf for STM32:
linker output is:
Memory region Used Size Region Size %age Used
FLASH: 90124 B 128 KB 68.76%
RAM: 31948 B 32 KB 97.50%
Python script output is:
Memory region .text .data .bss Total
FLASH: 88916 B 448 B 0 B 89364 B
RAM: 0 B 760 B 31188 B 31948 B
as you can see, the SRAM total is identical. The flash should be added to the size of .data
: 89364+760=90124 that match too.
I also have another example for RT105x:
linker output:
Memory region Used Size Region Size %age Used
BOARD_FLASH: 280964 B 2 MB 13.40%
SRAM_DTC: 122580 B 128 KB 93.52%
SRAM_ITC: 106112 B 128 KB 80.96%
SRAM_OC: 61444 B 256 KB 23.44%
python script output:
Memory region .text .data .bss Total
FLASH: 171876 B 480 B 0 B 172356 B
ITCM: 106112 B 0 B 0 B 106112 B
DTCM: 0 B 1796 B 120784 B 122580 B
OCRAM: 0 B 0 B 61444 B 61444 B
even all these numbers match
I have more questions, though:
Does the elf file embed (or could it embed) information about memory regions? This way I could recover them from the file itself without having to code them in python scripts from time to time. Is this information stored in the program headers ( arm-none-eabi-readelf -l <file.elf>
)?
Is there a reliable way to calculate the amount of flash actually occupied? I mean considering also initialized data and the portion of .text
that is in RAM.
Could you review my Python code? I’m not that skilled, so I’d love the opinion of someone who knows more about it than I do.
I try to make the adjustments due to section alignment, but the prerequisite for this to work is that the sections are sorted by address within the elf. Is that always the case?
Finally, for our needs it can often be useful to know how big is .rodata
, so I added the --rodata
parameter to the script. Did I identify well the sections containing .rodata
(both the SHF_EXECINSTR
and SHF_WRITE
flags cleared)?
best regards
Max