Skip to content

AVR: framework objects linked directly instead of archived → +10.1% size on attiny85 Blink (Tone.cpp ISR pull-in) #304

Closed

Description

@zackees

Symptom

On AVR (and likely any framework-rich Arduino target), an empty FastLED Blink sketch built with fbuild is ~11% larger than the same sketch built with PlatformIO. On attiny85:

Backend Flash text + data (bytes) Hex file (bytes)
platformio 3386 9554
fbuild 3728 10511
delta +342 (+10.1%) +957

The delta is large enough that FastLED's attiny85_binary_size workflow's max_size: 9500 Apa102 budget is starting to come under pressure on otherwise-innocent commits.

Root cause (confirmed via symbol diff)

The entire 342-byte delta is three symbols from Tone.cpp in the ATtinyCore framework that are present only in the fbuild ELF:

# Symbol fbuild platformio Source
1 __vector_3 206 B weak/default (absent) framework-arduino-avr-attiny's Tone.cpp
2 __vector_1 90 B weak/default (absent) Tone.cpp
3 noTone (+.part.0) 50 B absent Tone.cpp
total extra 346 0 matches text delta to within link-time padding

All other ~1,500 symbols are byte-identical between the two builds.

Why are they in fbuild and not in PlatformIO?

PlatformIO archives the framework core into libFrameworkArduino.a and passes that archive to the linker. The linker only pulls archive members whose strong symbols are referenced. FastLED's Blink doesn't call tone(), so Tone.cpp.o is not referenced and is silently dropped. Confirmed in PIO's firmware.map, line 1:

Archive member included to satisfy reference by file (symbol)

fbuild instead links all framework .o files directly as inputs (visible under .build/pio/<board>/.fbuild/build/<board>/release/core/Tone_<hash>.cpp.o, no .a produced). Direct .o inputs are always kept by the linker — section-GC (-ffunction-sections -fdata-sections -Wl,--gc-sections, which fbuild does pass) doesn't help because the ISRs __vector_1 / __vector_3 are reachable from the AVR interrupt vector table. They override AVR libc's weak __vector_default, get linked in, and drag noTone and its static state along for the ride.

The mechanism is hard to fix at the user's source level — the symbols are reachable. The fix has to be at the link-input shape: archive the framework objects so unreferenced ones are dropped by archive-member-selection rules before the vector table sees them.

Reproduction (FastLED master @ 064354e6ea)

# Get worktree, fall through to fbuild defaults
cd FastLED
bash compile attiny85 --examples Blink                     # backend=fbuild (default)
ls -l .build/pio/attiny85/.fbuild/build/attiny85/release/firmware.elf
avr-size .build/pio/attiny85/.fbuild/build/attiny85/release/firmware.elf

bash compile attiny85 --examples Blink --backend platformio
ls -l .build/pio/attiny85/.pio/build/attiny85/firmware.elf
avr-size .build/pio/attiny85/.pio/build/attiny85/firmware.elf

# Diff symbols
avr-nm --size-sort --print-size \
  .build/pio/attiny85/.fbuild/build/attiny85/release/firmware.elf > /tmp/fb.syms
avr-nm --size-sort --print-size \
  .build/pio/attiny85/.pio/build/attiny85/firmware.elf > /tmp/pio.syms
diff /tmp/pio.syms /tmp/fb.syms      # ← shows __vector_1, __vector_3, noTone

Proposed fix

Archive framework core objects into libFrameworkArduino.a before passing them to the linker, matching PlatformIO's behavior. Equivalent shape:

avr-ar rcs libFrameworkArduino.a core/*.o variants/*.o ...
avr-gcc <other objects> -L<dir> -lFrameworkArduino -Wl,--gc-sections -o firmware.elf

Acceptable alternatives that achieve the same effect:

  1. Pass framework objects via -Wl,--exclude-libs semantics so unreferenced ones don't impose dependencies.
  2. Two-pass link: first pass with -Wl,--no-whole-archive for framework, second pass adds anything that pulled in symbols from the framework. (Not worth the complexity vs. just making an archive.)
  3. Static analysis of framework sources to identify objects whose only strong symbols are weak-overridable ISRs, and exclude those when not referenced. (Brittle.)

Option 1 (the canonical Arduino-build pattern) is the recommendation. Should localize to whatever crate emits link inputs for the Arduino/AVR target.

Why this matters broadly

The same shape will hit any Arduino-framework target where the framework ships .cpp files that define weak-overridable ISR symbols. Probable also-affected (untested in this report but worth checking):

  • AVR: uno, nano, attiny* — Tone.cpp, HardwareSerial1.cpp/HardwareSerial2.cpp/etc. (vector pull-ins for unused UARTs)
  • Teensy 3.x / LC: serialEvent*, audio library entry points
  • STM32 (bluepill): HAL IRQ handlers
  • ESP32: probably masked by larger overall binary size and IDF framework already shipping as .a, but worth diff-checking

The attiny85 reproduction is the cleanest because the framework is small enough that 340 bytes shows up as 10% — but the absolute byte savings should be similar or larger on Teensy 3.x and STM32.

Test plan

  • Cargo test of the new link-input emitter: assert that for a "core_with_isr.cpp" fixture (defines __vector_5 and noTone), an Arduino-style link without any reference to tone() produces an ELF with zero bytes from that file.
  • Integration test in fbuild's own CI: compile FastLED examples/Blink for attiny85 and assert text + data <= 3450 bytes (PIO baseline + 64 byte slack).
  • FastLED-side: revert the --backend platformio workaround that the size-check workflow will get (coming in a parallel FastLED PR) and confirm attiny85_binary_size stays green.

Related


Filed during a coordinated CI-green sweep on FastLED master. Detailed symbol-level diff available on request — every byte of the 342-byte delta is accounted for above.

Activity

  1. zackees commented on May 30, 2026

    @zackees
    MemberAuthor

    Implementation plan (after reading the relevant fbuild source)

    Tracing through the code to confirm exactly where to fix:

    1. `crates/fbuild-build/src/pipeline/sequential.rs:229` — `core_objects.extend(library_objects);` concatenates framework core, variant, and local-library `.o` files into a single `Vec`.

    2. `crates/fbuild-build/src/pipeline/sequential.rs:235-248` — passes that vec as the `archives` parameter to `Linker::link_all` / `Linker::link`.

    3. `crates/fbuild-build/src/avr/avr_linker.rs:93-97` — the AVR `link` impl, despite the parameter being named `archives`, iterates and passes each as a raw positional arg:

      ```rust
      // Core objects passed directly (not archived) for LTO compatibility
      // Python fbuild does the same: archive breaks LTO symbol visibility
      for archive in archives {
      args.push(archive.to_string_lossy().to_string());
      }
      ```

    So the `.o` files end up as direct linker inputs, not archive members. The comment cites LTO compatibility, but:

    • Plain `ar` does break LTO indexes. However, fbuild already knows to swap in `gcc-ar` for LTO archives — see `crates/fbuild-build/src/avr/orchestrator.rs:261-262`:
      ```rust
      // Use gcc-ar for LTO archives so the linker-plugin index is written.
      let lib_ar_path = pipeline::pick_archiver(&ar_path, &gcc_ar_path, &c_flags, &cpp_flags);
      ```
    • That `pick_archiver` machinery only gets used inside `LibraryBuildEnv` for the per-library project-as-library archive. The framework `.o` files never go through it.

    Recommended patch shape

    Two reasonable options, both small (~30 lines):

    Option A — fix at the AVR linker boundary (minimal blast radius)

    In `crates/fbuild-build/src/avr/avr_linker.rs::link()`:

    1. Partition the `archives` slice into `.a` (pass-through) and `.o` (to-be-archived).
    2. If any `.o` are present, invoke `self.ar_path` (and use `gcc-ar` when an LTO flag is in `c_flags`/`cpp_flags` — reuse `pipeline::pick_archiver`) to create `output_dir/libframework.a`.
    3. Pass the new archive + any pre-existing `.a` files in the same positional slot.

    The current LTO concern is satisfied because gcc-ar writes the plugin index. The existing comment on lines 93-94 should be updated to:

    ```
    // Archive framework + library objects into libframework.a so the linker
    // only pulls in members whose strong symbols are referenced. This prevents
    // unused ISRs (Tone.cpp __vector_1/__vector_3 on AVR) from being dragged
    // in via weak-symbol override of __vector_default. Use gcc-ar when LTO is
    // enabled to preserve the linker-plugin index.
    ```

    Pro: contained to AVR. No risk to Teensy/RP2040/STM32/etc. Aligns AVR behavior with PlatformIO exactly.
    Con: each `avr_linker.rs::link` call now incurs ~30 ms of `ar` runtime. Negligible vs. compile+link total.

    Option B — fix at the pipeline boundary (broader applicability)

    In `crates/fbuild-build/src/pipeline/sequential.rs` between lines 229 and 235, take `core_objects` and run `linker.archive(&core_objects, &ctx.build_dir.join("libframework.a"))?` before passing it down. Then `core_objects = vec![framework_archive];`.

    Pro: every platform that goes through `run_sequential_build_with_libs` benefits (Teensy, RP2040, STM32, NRF52, SAM, Renesas, Apollo3, CH32V, ESP8266). Likely smaller binaries everywhere.
    Con: needs validation on each of those platforms (and they all have their own `Linker` impls — `archive()` must be functional on all). For platforms whose frameworks already ship as `.a` (none in this list AFAICT), this changes nothing.

    I'd recommend Option A first as a 2-hour fix that nails AVR, then a follow-up sweep for Option B once Teensy / STM32 fixtures exist in fbuild's test matrix to catch any regressions.

    Acceptance test (to add to fbuild's test suite)

    ```rust
    #[test]
    fn attiny85_blink_size_under_pio_baseline_plus_64b() {
    // Reference: PlatformIO produces 3386 bytes flash for the same source.
    // 64B slack covers padding/alignment differences from any future
    // toolchain bump.
    let result = build_fixture("attiny85", "blink").unwrap();
    assert!(
    result.size_info.unwrap().total_flash <= 3450,
    "AVR Blink size regression — Tone.cpp ISR pull-in may be back"
    );
    }
    ```

    My environment limitation

    I have the repo cloned and reviewed but don't have the full soldr + Rust 1.94.1 toolchain set up locally on this Windows box (and the hook discipline in this repo correctly blocks bare `cargo` invocations). Happy to file a PR if a maintainer can confirm Option A is the preferred direction, or I can spin up the dev env if the answer is "just send the PR."

    In the meantime, FastLED master is unblocked via FastLED PR #2602 (pending) which routes the size-check workflows back through PlatformIO. Easy revert once this lands.

  2. added 2 commits that reference this issue on May 30, 2026
    b7560aa
    3884f55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions