Skip to content

Hot Fix #2 for 3.9.17

Choose a tag to compare

@zackees zackees released this 29 May 04:44
· 8704 commits to master since this release

For more information see this bug fix thread.

Hot fix #2 for

It turns out the older AVR gcc compilers will not eliminate complex static objects that are not referenced. However it seems it will eliminate them if they are in statics inside static functions, like this:

This will unconditionally initialize in avr-gcc at startup, but be removed in more modern toolchains.

typedef fl::hash_map<Key, Value> HashMap;
static HashMap gStatic;

This however, will be removed in avr-gcc and others

static HashMap& get_static() {
  static HashMap s_static;
  return s_static;
}

Here is a Chat GPT Summary:

🔍 Summary:

  • AVR-GCC does not aggressively eliminate unused static non-POD objects — even if they appear to be unused — because:

    • It lacks certain modern optimizations (especially LTO-level optimizations).
    • It typically relies on the .ctors section to ensure initialization of static objects with constructors.
    • By default, objects with static storage duration and non-trivial constructors get registered for initialization via .ctors and the linker keeps them.
  • Modern Clang and GCC (non-AVR) will remove these if they are not referenced and the constructors are not marked in a way that ensures their side effects are preserved.


🔧 Arduino Build Settings

In the Arduino build system (typically using avr-gcc):

  • Link-Time Optimization (LTO) is often disabled by default for compatibility. Without LTO, the compiler doesn't see across translation units and is less aggressive about removing "unused" symbols.
  • Static initializers are kept because they're registered in the .ctors section which the startup code walks through during initialization.
  • If you're using Arduino CLI or PlatformIO with LTO enabled (-flto), you may start seeing more Clang/GCC-like elision behavior if you're not careful.

🔒 Why static constructors aren’t elided (AVR-specific reasons)

  • AVR libc (avr-libc) startup code explicitly calls global/static constructors via __do_global_ctors.
  • The compiler places constructors into .ctors section using .section .ctors + .initN priorities.
  • These sections are preserved unless explicitly dropped or optimized out with aggressive LTO.

✅ To Preserve in Modern Toolchains

In more aggressive modern toolchains (Clang/GCC with LTO):

  • Use __attribute__((used)) or volatile globals if you want to force retention.
  • Or rely on __attribute__((constructor)) for function-level hooks that are less likely to be optimized out.

👇 Example

struct AutoRun {
  AutoRun() { Serial.println("I ran!"); }
};

static AutoRun my_auto_run; // Kept by AVR-GCC, likely dropped by Clang w/LTO

To preserve it in Clang:

__attribute__((used)) static AutoRun my_auto_run;

✅ Conclusion

This is an AVR-GCC behavior, not strictly Arduino's doing — but Arduino's default settings do reinforce this behavior by avoiding LTO and preserving .ctors invocations. Modern toolchains need extra care to keep static initializers with side effects.