Skip to content

Commit 8f7b62d

Browse files
authored
Wrap dead exceptions code to save some flash (#149)
1 parent e24725a commit 8f7b62d

3 files changed

Lines changed: 72 additions & 0 deletions

File tree

CMakeLists.txt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,22 @@ add_custom_command(
3535
add_custom_target(mem-variant DEPENDS "mem_variant")
3636

3737
idf_build_set_property(COMPILE_DEFINITIONS "-DESP32_ARDUINO_LIB_BUILDER" APPEND)
38+
39+
# Wrap std::__throw_* functions to abort immediately, eliminating ~2KB of
40+
# exception class overhead. Implementation lives in components/throw_stubs.
41+
# ESP-IDF already compiles with -fno-exceptions, so this code was dead anyway.
42+
# Apply directly on the project elf so the flags end up in
43+
# build/CMakeFiles/${CMAKE_PROJECT_NAME}.elf.dir/link.txt and get picked up by
44+
# tools/copy-libs.sh into pioarduino-build.py LINKFLAGS / flags/ld_flags.
45+
46+
target_link_options(${CMAKE_PROJECT_NAME}.elf PRIVATE
47+
"-Wl,--wrap=_ZSt20__throw_length_errorPKc"
48+
"-Wl,--wrap=_ZSt19__throw_logic_errorPKc"
49+
"-Wl,--wrap=_ZSt20__throw_out_of_rangePKc"
50+
"-Wl,--wrap=_ZSt24__throw_out_of_range_fmtPKcz"
51+
"-Wl,--wrap=_ZSt17__throw_bad_allocv"
52+
"-Wl,--wrap=_ZSt25__throw_bad_function_callv"
53+
# Keep at least one __wrap_* symbol referenced so the object is pulled
54+
# from libthrow_stubs.a during the user sketch link.
55+
"-u__wrap__ZSt17__throw_bad_allocv"
56+
)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
idf_component_register(SRCS "throw_stubs.cpp"
2+
INCLUDE_DIRS ""
3+
REQUIRES esp_system)
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Linker wrap stubs for std::__throw_* functions.
3+
*
4+
* ESP-IDF compiles with -fno-exceptions, so C++ exceptions always abort.
5+
* However, ESP-IDF only wraps low-level functions (__cxa_throw, etc.),
6+
* not the std::__throw_* functions that construct exception objects first.
7+
* This pulls in ~2KB of dead exception class code that can never run.
8+
*
9+
* These stubs abort immediately with a descriptive message, allowing
10+
* the linker to dead-code eliminate the exception class infrastructure.
11+
*
12+
* Wrapped functions and their callers:
13+
* - std::__throw_length_error: std::string::reserve, std::vector::reserve
14+
* - std::__throw_logic_error: std::promise, std::packaged_task
15+
* - std::__throw_out_of_range: std::string::at, std::vector::at
16+
* - std::__throw_out_of_range_fmt: std::bitset::to_ulong
17+
* - std::__throw_bad_alloc: operator new
18+
* - std::__throw_bad_function_call: std::function::operator()
19+
*
20+
* Source: https://github.com/esphome/esphome/blob/dev/esphome/components/esp32/throw_stubs.cpp
21+
*/
22+
23+
#include "esp_system.h"
24+
25+
// Linker wraps for std::__throw_* - must be extern "C" at global scope.
26+
// Names must be __wrap_ + mangled name for the linker's --wrap option.
27+
28+
// NOLINTBEGIN(bugprone-reserved-identifier,cert-dcl37-c,cert-dcl51-cpp,readability-identifier-naming)
29+
extern "C" {
30+
31+
// std::__throw_length_error(char const*) - called when container size exceeds max_size()
32+
void __wrap__ZSt20__throw_length_errorPKc(const char *) { esp_system_abort("std::length_error"); }
33+
34+
// std::__throw_logic_error(char const*) - called for logic errors (e.g., promise already satisfied)
35+
void __wrap__ZSt19__throw_logic_errorPKc(const char *) { esp_system_abort("std::logic_error"); }
36+
37+
// std::__throw_out_of_range(char const*) - called by at() when index is out of bounds
38+
void __wrap__ZSt20__throw_out_of_rangePKc(const char *) { esp_system_abort("std::out_of_range"); }
39+
40+
// std::__throw_out_of_range_fmt(char const*, ...) - variadic form called by container at() with formatted messages
41+
void __wrap__ZSt24__throw_out_of_range_fmtPKcz(const char *, ...) { esp_system_abort("std::out_of_range"); }
42+
43+
// std::__throw_bad_alloc() - called when operator new fails
44+
void __wrap__ZSt17__throw_bad_allocv() { esp_system_abort("std::bad_alloc"); }
45+
46+
// std::__throw_bad_function_call() - called when invoking empty std::function
47+
void __wrap__ZSt25__throw_bad_function_callv() { esp_system_abort("std::bad_function_call"); }
48+
49+
} // extern "C"
50+
// NOLINTEND(bugprone-reserved-identifier,cert-dcl37-c,cert-dcl51-cpp,readability-identifier-naming)

0 commit comments

Comments
 (0)