Report flash and RAM use
Add up section sizes the way the size tool does, and work out flash and RAM use. An easy embedded C problem (ELF, map files and binary inspection), run and…
easy · ELF, map files and binary inspection · elf · memory · sections
The size tool summarises an ELF file as three numbers, from the sections' flags:
- text: allocated, not writable (code, read-only data, the vector table),
- data: allocated, writable, with contents (initialised variables),
- bss: allocated, writable,
NOBITS(zeroed variables: no bytes in the file).
Sections without ALLOC (debug information, symbol tables) are not counted. On a microcontroller, flash holds text + data (the initial values of data are stored in flash), and RAM holds data + bss.
Write size_report(sh, n, text, data, bss).
Starter code
void size_report(const shdr_t *sh, int n, uint32_t *text, uint32_t *data, uint32_t *bss) {
*text = *data = *bss = 0;
for (int i = 0; i < n; i++) {
if (sh[i].flags & EXEC) *text += sh[i].size;
else if (sh[i].flags & NOBITS) *bss += sh[i].size;
else *data += sh[i].size;
}
}Example tests
A small firmware
shdr_t sh[] = { {".isr_vector", 0x10C, ALLOC}, {".text", 0x2F38, ALLOC | EXEC}, {".rodata", 0x220, ALLOC}, {".data", 0x84, ALLOC | WRITE}, {".bss", 0x3F8, ALLOC | WRITE | NOBITS} };
show(sh, 5);Expected output:
text 12900 data 132 bss 1016: flash 13032, RAM 1148Learn it first
- ELF, map files and binary inspection · Reading what the linker built with size, nm, objdump and the map file.