From 8dc2a7d9d7a42f048c70b948c150cd1c7508710a Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Mon, 26 Jan 2026 21:33:03 -1000 Subject: [PATCH 1/3] [esp32] Eliminate dead exception class code via linker wraps --- esphome/components/esp32/__init__.py | 11 +++++ esphome/components/esp32/throw_stubs.cpp | 53 ++++++++++++++++++++++++ 2 files changed, 64 insertions(+) create mode 100644 esphome/components/esp32/throw_stubs.cpp diff --git a/esphome/components/esp32/__init__.py b/esphome/components/esp32/__init__.py index da49fdc070..37bbe66bab 100644 --- a/esphome/components/esp32/__init__.py +++ b/esphome/components/esp32/__init__.py @@ -1048,6 +1048,17 @@ async def to_code(config): cg.add_build_flag("-DUSE_ESP32_FRAMEWORK_ESP_IDF") if use_platformio: cg.add_platformio_option("framework", "espidf") + + # Wrap std::__throw_* functions to abort immediately, eliminating ~1KB of + # exception class overhead. See throw_stubs.cpp for implementation. + # ESP-IDF wraps __cxa_throw but not these higher-level functions. + # Mangled names: _ZSt = std::, number = identifier length, PKc = char const* + cg.add_build_flag("-Wl,--wrap=_ZSt20__throw_length_errorPKc") + cg.add_build_flag("-Wl,--wrap=_ZSt19__throw_logic_errorPKc") + cg.add_build_flag("-Wl,--wrap=_ZSt20__throw_out_of_rangePKc") + cg.add_build_flag("-Wl,--wrap=_ZSt24__throw_out_of_range_fmtPKcz") + cg.add_build_flag("-Wl,--wrap=_ZSt17__throw_bad_allocv") + cg.add_build_flag("-Wl,--wrap=_ZSt25__throw_bad_function_callv") else: cg.add_build_flag("-DUSE_ARDUINO") cg.add_build_flag("-DUSE_ESP32_FRAMEWORK_ARDUINO") diff --git a/esphome/components/esp32/throw_stubs.cpp b/esphome/components/esp32/throw_stubs.cpp new file mode 100644 index 0000000000..3f54007cf4 --- /dev/null +++ b/esphome/components/esp32/throw_stubs.cpp @@ -0,0 +1,53 @@ +/* + * Linker wrap stubs for std::__throw_* functions. + * + * ESP-IDF disables C++ exceptions but doesn't wrap the std::__throw_* + * functions that construct exception objects before calling __cxa_throw. + * This wastes ~1KB on exception class code that can never execute. + * + * These stubs abort immediately with a descriptive message, allowing + * the linker to dead-code eliminate the exception class infrastructure. + * + * The ESP8266 Arduino toolchain solves this by rebuilding libstdc++ with + * similar stubs. We achieve the same result using linker --wrap. + * + * Wrapped functions and their callers: + * - std::__throw_length_error: std::string::reserve, std::vector::reserve + * - std::__throw_logic_error: std::promise, std::packaged_task + * - std::__throw_out_of_range: std::string::at, std::vector::at + * - std::__throw_out_of_range_fmt: std::bitset::to_ulong + * - std::__throw_bad_alloc: operator new + * - std::__throw_bad_function_call: std::function::operator() + */ + +#ifdef USE_ESP_IDF +#include "esp_system.h" + +extern "C" { + +// std::__throw_length_error(char const*) +// Called when container size exceeds max_size() +void __wrap__ZSt20__throw_length_errorPKc(const char *) { esp_system_abort("std::length_error"); } + +// std::__throw_logic_error(char const*) +// Called for logic errors (e.g., promise already satisfied) +void __wrap__ZSt19__throw_logic_errorPKc(const char *) { esp_system_abort("std::logic_error"); } + +// std::__throw_out_of_range(char const*) +// Called by at() when index is out of bounds +void __wrap__ZSt20__throw_out_of_rangePKc(const char *) { esp_system_abort("std::out_of_range"); } + +// std::__throw_out_of_range_fmt(char const*, ...) +// Called by bitset::to_ulong when value doesn't fit +void __wrap__ZSt24__throw_out_of_range_fmtPKcz(const char *, ...) { esp_system_abort("std::out_of_range"); } + +// std::__throw_bad_alloc() +// Called when operator new fails +void __wrap__ZSt17__throw_bad_allocv() { esp_system_abort("std::bad_alloc"); } + +// std::__throw_bad_function_call() +// Called when invoking empty std::function +void __wrap__ZSt25__throw_bad_function_callv() { esp_system_abort("std::bad_function_call"); } + +} // extern "C" +#endif // USE_ESP_IDF From e2cd8a60041403c4708cfc84c487c912a6e82d8f Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Mon, 26 Jan 2026 21:40:47 -1000 Subject: [PATCH 2/3] [esp32] Eliminate dead exception class code via linker wraps --- esphome/components/esp32/__init__.py | 20 ++++++++++-------- esphome/components/esp32/throw_stubs.cpp | 26 +++++++++++++----------- 2 files changed, 25 insertions(+), 21 deletions(-) diff --git a/esphome/components/esp32/__init__.py b/esphome/components/esp32/__init__.py index 37bbe66bab..b7faccaed6 100644 --- a/esphome/components/esp32/__init__.py +++ b/esphome/components/esp32/__init__.py @@ -1049,16 +1049,18 @@ async def to_code(config): if use_platformio: cg.add_platformio_option("framework", "espidf") - # Wrap std::__throw_* functions to abort immediately, eliminating ~1KB of + # Wrap std::__throw_* functions to abort immediately, eliminating ~3KB of # exception class overhead. See throw_stubs.cpp for implementation. - # ESP-IDF wraps __cxa_throw but not these higher-level functions. - # Mangled names: _ZSt = std::, number = identifier length, PKc = char const* - cg.add_build_flag("-Wl,--wrap=_ZSt20__throw_length_errorPKc") - cg.add_build_flag("-Wl,--wrap=_ZSt19__throw_logic_errorPKc") - cg.add_build_flag("-Wl,--wrap=_ZSt20__throw_out_of_rangePKc") - cg.add_build_flag("-Wl,--wrap=_ZSt24__throw_out_of_range_fmtPKcz") - cg.add_build_flag("-Wl,--wrap=_ZSt17__throw_bad_allocv") - cg.add_build_flag("-Wl,--wrap=_ZSt25__throw_bad_function_callv") + # ESP-IDF already compiles with -fno-exceptions, so this code was dead anyway. + for mangled in [ + "_ZSt20__throw_length_errorPKc", + "_ZSt19__throw_logic_errorPKc", + "_ZSt20__throw_out_of_rangePKc", + "_ZSt24__throw_out_of_range_fmtPKcz", + "_ZSt17__throw_bad_allocv", + "_ZSt25__throw_bad_function_callv", + ]: + cg.add_build_flag(f"-Wl,--wrap={mangled}") else: cg.add_build_flag("-DUSE_ARDUINO") cg.add_build_flag("-DUSE_ESP32_FRAMEWORK_ARDUINO") diff --git a/esphome/components/esp32/throw_stubs.cpp b/esphome/components/esp32/throw_stubs.cpp index 3f54007cf4..108ec5a416 100644 --- a/esphome/components/esp32/throw_stubs.cpp +++ b/esphome/components/esp32/throw_stubs.cpp @@ -23,31 +23,33 @@ #ifdef USE_ESP_IDF #include "esp_system.h" +namespace esphome::esp32 {} + +// Linker wraps for std::__throw_* - must be extern "C" at global scope. +// Names must be __wrap_ + mangled name for the linker's --wrap option. + +// NOLINTBEGIN(bugprone-reserved-identifier,cert-dcl37-c,cert-dcl51-cpp,readability-identifier-naming) extern "C" { -// std::__throw_length_error(char const*) -// Called when container size exceeds max_size() +// std::__throw_length_error(char const*) - called when container size exceeds max_size() void __wrap__ZSt20__throw_length_errorPKc(const char *) { esp_system_abort("std::length_error"); } -// std::__throw_logic_error(char const*) -// Called for logic errors (e.g., promise already satisfied) +// std::__throw_logic_error(char const*) - called for logic errors (e.g., promise already satisfied) void __wrap__ZSt19__throw_logic_errorPKc(const char *) { esp_system_abort("std::logic_error"); } -// std::__throw_out_of_range(char const*) -// Called by at() when index is out of bounds +// std::__throw_out_of_range(char const*) - called by at() when index is out of bounds void __wrap__ZSt20__throw_out_of_rangePKc(const char *) { esp_system_abort("std::out_of_range"); } -// std::__throw_out_of_range_fmt(char const*, ...) -// Called by bitset::to_ulong when value doesn't fit +// std::__throw_out_of_range_fmt(char const*, ...) - called by bitset::to_ulong when value doesn't fit void __wrap__ZSt24__throw_out_of_range_fmtPKcz(const char *, ...) { esp_system_abort("std::out_of_range"); } -// std::__throw_bad_alloc() -// Called when operator new fails +// std::__throw_bad_alloc() - called when operator new fails void __wrap__ZSt17__throw_bad_allocv() { esp_system_abort("std::bad_alloc"); } -// std::__throw_bad_function_call() -// Called when invoking empty std::function +// std::__throw_bad_function_call() - called when invoking empty std::function void __wrap__ZSt25__throw_bad_function_callv() { esp_system_abort("std::bad_function_call"); } } // extern "C" +// NOLINTEND(bugprone-reserved-identifier,cert-dcl37-c,cert-dcl51-cpp,readability-identifier-naming) + #endif // USE_ESP_IDF From e5f70d1677705a322dadbe3568e2a7bf16ceba02 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Mon, 26 Jan 2026 21:48:13 -1000 Subject: [PATCH 3/3] [esp32] Eliminate dead exception class code via linker wraps --- esphome/components/esp32/throw_stubs.cpp | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/esphome/components/esp32/throw_stubs.cpp b/esphome/components/esp32/throw_stubs.cpp index 108ec5a416..e82e5645de 100644 --- a/esphome/components/esp32/throw_stubs.cpp +++ b/esphome/components/esp32/throw_stubs.cpp @@ -1,16 +1,18 @@ /* * Linker wrap stubs for std::__throw_* functions. * - * ESP-IDF disables C++ exceptions but doesn't wrap the std::__throw_* - * functions that construct exception objects before calling __cxa_throw. - * This wastes ~1KB on exception class code that can never execute. + * ESP-IDF compiles with -fno-exceptions, so C++ exceptions always abort. + * However, ESP-IDF only wraps low-level functions (__cxa_throw, etc.), + * not the std::__throw_* functions that construct exception objects first. + * This pulls in ~3KB of dead exception class code that can never run. + * + * ESP8266 Arduino already solved this: their toolchain rebuilds libstdc++ + * with throw functions that just call abort(). We achieve the same result + * using linker --wrap without requiring toolchain changes. * * These stubs abort immediately with a descriptive message, allowing * the linker to dead-code eliminate the exception class infrastructure. * - * The ESP8266 Arduino toolchain solves this by rebuilding libstdc++ with - * similar stubs. We achieve the same result using linker --wrap. - * * Wrapped functions and their callers: * - std::__throw_length_error: std::string::reserve, std::vector::reserve * - std::__throw_logic_error: std::promise, std::packaged_task