Skip to content

fix(nrf52): don't enable LTO for the Adafruit core (#2016) - #2028

Merged
zackees merged 1 commit into
mainfrom
fix/nrf52-no-default-lto-2016
Nov 17, 2026
Merged

zackees merged 1 commit into
mainfrom
fix/nrf52-no-default-lto-2016

Conversation

@zackees

@zackees zackees commented Nov 16, 2026

Copy link
Copy Markdown
Member

Root cause

crates/fbuild-build-arm/src/nrf52/configs/nrf52840.json compiled and linked the release profile with -flto -fno-fat-lto-objects / -flto -fuse-linker-plugin. The Adafruit nRF52 core's FreeRTOS port (cores/nRF5/freertos/portable/GCC/nrf52/port.c) has naked functions that load addresses with ldr rN, =symbol. When LTO emits the whole program as one unit, the literal pool lands out of range, and the assembler rejects it with offset out of range. A blink is small enough to link; a FastLED sketch is not. The core's own platform.txt never uses -flto.

Why -flto was there: not for size. It arrived in the bulk "add board support" commit (768e446), which reused fbuild's ARM release template. -Wl,--undefined=vTaskSwitchContext was added later (37a29b2) only to stop LTO discarding a symbol that the port's inline asm calls. In #1515 the GCC 7 registry toolchain hit the same offset out of range error, and the fix stripped LTO for that one toolchain (without_release_lto). GCC 15 has the same problem once the program is large enough.

Fix

  • Drop all LTO flags from the nrf52840 release profile, along with the LTO-only --undefined=vTaskSwitchContext. Without LTO, the inline asm's bl vTaskSwitchContext is a real relocation, so --gc-sections keeps the symbol.
  • Delete the now-dead GCC 7 special case (for_registry_toolchain, is_gcc7_registry_toolchain, without_release_lto, without_flags) and its test.
  • nrf52840.json is the only nrf52 config. nrf52832 boards are rejected by get_nrf52_config_for_mcu, and the nordicnrf52 orchestrator only builds the Adafruit core, so no other framework shares these flags.

Trade-off: flash grows without LTO. On a clean blink it goes from 27,856 to 47,308 bytes, which matches what the core's own (non-LTO) build produces. A firmware that links is worth more than a smaller one that fails at random sizes.

Tests

  • New nrf52::mcu_config::tests::no_profile_enables_lto_by_default: no profile of any supported nrf52 MCU carries *lto* or -fuse-linker-plugin.
  • nrf52_compiler::tests::test_common_flags_contain_optimization now asserts the resolved compile flags contain no -flto.
  • RED: against the original JSON, both tests fail (21 passed, 2 failed). GREEN with the fix.

Validation

  • soldr cargo fmt --all -- --check: clean
  • soldr cargo clippy --workspace --all-targets -- -D warnings: clean
  • bash test -p fbuild-build-arm: 284 passed, 0 failed, 2 ignored

End-to-end (Linux, FBUILD_DEV_MODE=1, nrf52840_dk, toolchain arm-none-eabi-gcc 15.2.Rel1, FastLED from local checkout in lib/FastLED)

before (LTO) after
blink links, flash 27,856 links, flash 47,308 (clean build)
issue's FastLED sketch fails: .lto-tmp/*.s:8860/8889/8920: Error: offset out of range links, flash 65,544, no lto-wrapper

RAM figure ("Also noticed")

ram: 235520 is .data + .bss + .heap. The Adafruit linker script's .heap is a writable NOBITS section that grows to __StackLimit. In the FastLED build: .data 820, .bss 17,844, .heap 216,856. The generic SizeInfo::from_elf_sections counts all writable NOBITS sections as RAM. Changing that is cross-platform, so it is tracked separately in #2027.

Fixes #2016

The nrf52840 release profile compiled and linked with -flto. The
Adafruit nRF52 core's FreeRTOS port (cores/nRF5/freertos/portable/GCC/
nrf52/port.c) loads symbol addresses with `ldr rN, =symbol` in naked
functions. When LTO emits the program as one unit, the literal pool lands
out of range and the assembler fails with `offset out of range`. A blink
fits; a FastLED sketch does not. The core's own platform.txt never uses
-flto.

LTO was not added for a size reason. It came from the bulk "add board
support" commit (768e446), which reused the ARM release template, and
-Wl,--undefined=vTaskSwitchContext was added later (37a29b2) only to
stop LTO discarding a symbol that the port's inline asm references.

- Drop -flto, -fno-fat-lto-objects, -fuse-linker-plugin and the
  LTO-only --undefined=vTaskSwitchContext from the release profile.
- Delete the GCC 7-only LTO strip (for_registry_toolchain,
  is_gcc7_registry_toolchain, without_release_lto, without_flags).
  It is dead now that no toolchain gets LTO.
- Test: no nrf52 profile enables LTO, and the resolved compile flags
  carry no -flto.

Fixes #2016

Co-authored-by: Claude <[email protected]>
@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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Triage

Development

Successfully merging this pull request may close these issues.

nRF52840: -flto breaks the Adafruit core's FreeRTOS port, offset out of range (2.5.51)

1 participant