fiber
Loading...
Searching...
No Matches
System Stubs: Wrappers for Embedded Binary Size Reduction

Overview

fiber_sys_stubs is an optional object library that can be linked into embedded projects to eliminate unnecessary system-level functionality such as file I/O, heap management glue, and exit-related infrastructure — all of which are irrelevant in bare-metal environments.

These wrappers disable dynamic and OS-linked behavior from libc and libstdc++ by redirecting specific system functions via the linker --wrap option. The result is a dramatic reduction in binary size (commonly 50–90%), especially in projects using exceptions, static objects, or any part of the standard library.

Usage Example

In your main CMakeLists.txt, enable the wrappers via an option:

set(FIBER_USE_SYS_STUBS ON)
add_subdirectory(fiber)
target_link_libraries(App PRIVATE fiber)
target_link_libraries(App PUBLIC fiber_sys_stubs) # apply wrapping

It Works

The fiber_sys_stubs links in sys_stubs together with wrapper functions and tells the liker to use the wrappers instead of the originals if they are hard symbols, and just defines normally if it is a weak symbol.

Specifically the following have been defined/wrapped:

  • std::terminate_handler __terminate_handler
  • __wrap_atexit

Even if your code never calls them directly, the C++ runtime does, especially when:

  • Using throw, std::terminate(), or global/static objects
  • Linking any part of the standard library that references stderr or exit()

By providing __wrap_* versions of these functions and overriding them with stubs, you:

  • Prevent libc and libstdc++ from linking their real (heavy) implementations
  • Remove implicit I/O and shutdown logic
  • Avoid bringing in string formatting, FILE*, and heap code for exit messages

Results and Measured Impact

Scenario Binary Size (Flash)
Default (newlib, stdlibc++) ~70–90kB
With fiber_sys_stubs enabled ~18–24kB

Real-world savings depend on how many standard features are used (e.g., std::string, exceptions, RTTI).

Summary

fiber_sys_stubs is the easiest and safest way to:

  • Enable full C++ (including exceptions)
  • Avoid the trap of heap and stdio linkage
  • Shrink your binary by 50–90%
  • Run predictable, deterministic bare-metal code with zero libc glue

It's opt-in, and safe to leave off in non-embedded builds.


See also
__terminate_handler, __wrap_atexit