Skip to content

fix(size): count only sections with contents as flash (#2012) - #2021

Merged
zackees merged 2 commits into
mainfrom
fix/size-heap-nobits-2012
Nov 16, 2026
Merged

zackees merged 2 commits into
mainfrom
fix/size-heap-nobits-2012

Conversation

@zackees

@zackees zackees commented Nov 16, 2026

Copy link
Copy Markdown
Member

Root cause

LinkerBase::report_size ran the toolchain size tool (Berkeley format) and reported text + data as flash. GNU size puts every read-only SHF_ALLOC section under text, including SHT_NOBITS ones. Arduino-Pico's linker script leaves the NOLOAD .heap reservation read-only, so its 252,216 bytes (513,528 on RP2350) were counted as flash. An empty Pico sketch reported flash: 334608 bytes against an 82,392-byte firmware.bin.

Fix

  • fbuild-core: new ElfSectionSize and SizeInfo::from_elf_sections. It uses the Berkeley rules with one correction: a SHT_NOBITS section is never text. Writable NOBITS is still bss. A read-only NOBITS reservation counts as neither flash nor static RAM, and Berkeley never counted it as RAM either.
  • fbuild-build-engine: LinkerBase::report_size now reads the ELF section headers directly (read_elf_section_sizes, via object) instead of spawning size. It is now synchronous and no longer takes the size-tool path or label. The 9 callers (AVR, ESP8266, CH32V, generic ARM, nRF52, SAM, Silabs, Teensy, Renesas) were updated to match.

Flash is now exactly the bytes of every allocated section with contents, which is the firmware.bin payload. Every other section is classified exactly as Berkeley did, so non-RP2040 figures and all RAM figures stay the same.

Out of scope: ESP32 has its own SysV size -A parser (fbuild-build-esp/src/esp32/size_report.rs), so this does not fix #2013. RP2040 RAM still uses Berkeley semantics, so .data is AX here and is not counted, while the flash-resident writable .ota/.partition are. That is the existing behaviour, left unchanged on purpose and worth a follow-up.

Tests

  • fbuild-core: size_info_from_elf_sections_excludes_nobits_from_flash uses the issue's exact section table. It was RED against Berkeley rules (334,608) and is GREEN now (82,392, RAM 19,360). size_info_from_elf_sections_matches_berkeley_for_classic_layout checks that the AVR-style layout is unchanged.
  • fbuild-build-engine: report_size_does_not_count_read_only_nobits_heap_as_flash writes a synthetic ELF with a read-only NOBITS .heap (tempfile + object::write). report_size_rejects_non_elf checks that a non-ELF input returns an error.

Validation

  • soldr cargo fmt --all --check: passed
  • soldr cargo clippy --workspace --all-targets -- -D warnings: passed
  • bash test -p fbuild-core -p fbuild-build-engine -p fbuild-build-arm -p fbuild-build-mcu -p fbuild-build-esp: all passed
  • End to end, with the dev daemon on the issue's empty sketch (plus board_build.core = earlephilhower, which RP2040: default Arduino Mbed configuration enters Arduino-Pico backend and fails with ENOENT #1977 now requires):
    • before: Flash: 326.77KB / 2.00MB (16.0%) … flash: 334608 bytes, ram: 19360 bytes
    • after: Flash: 80.46KB / 2.00MB (3.9%) … flash: 82392 bytes, ram: 19360 bytes; firmware.bin is 82,392 bytes

Fixes #2012

The shared size report ran the toolchain's `size` tool in Berkeley
format and parsed `text + data` as flash. GNU `size` files every
read-only ALLOC section under `text`, including SHT_NOBITS ones, so
Arduino-Pico's read-only NOLOAD `.heap` (252,216 bytes on RP2040,
513,528 on RP2350) was reported as flash: an empty Pico sketch showed
334,608 bytes against an 82,392-byte firmware.bin.

LinkerBase::report_size now reads the ELF section headers directly and
classifies them with the new SizeInfo::from_elf_sections: Berkeley
rules, except that a SHT_NOBITS section is never text. A writable
NOBITS section is still bss; a read-only NOBITS reservation is neither
flash nor static RAM (Berkeley never counted it as RAM either). Flash
is therefore exactly the bytes of every allocated section with
contents, and the RAM figure is unchanged on every platform. All other
sections classify exactly as before, so non-RP2040 numbers do not move.

Fixes #2012
@coderabbitai

coderabbitai Bot commented Nov 16, 2026 •

Copy link
Copy Markdown

Important

Review skipped

Auto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

Run configuration

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • Trigger review
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

#2012)

Move-only: ElfSectionSize and SizeInfo::from_elf_sections (fbuild-core)
and read_elf_section_sizes (fbuild-build-engine), together with their
tests, are moved into new elf_size modules, so neither lib.rs nor linker.rs
goes over the 1000-line file limit. No behaviour change.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Triage

1 participant