ESPHome 2026.3.0-dev
Loading...
Searching...
No Matches
throw_stubs.cpp
Go to the documentation of this file.
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 ~3KB of dead exception class code that can never run.
8 *
9 * ESP8266 Arduino already solved this: their toolchain rebuilds libstdc++
10 * with throw functions that just call abort(). We achieve the same result
11 * using linker --wrap without requiring toolchain changes.
12 *
13 * These stubs abort immediately with a descriptive message, allowing
14 * the linker to dead-code eliminate the exception class infrastructure.
15 *
16 * Wrapped functions and their callers:
17 * - std::__throw_length_error: std::string::reserve, std::vector::reserve
18 * - std::__throw_logic_error: std::promise, std::packaged_task
19 * - std::__throw_out_of_range: std::string::at, std::vector::at
20 * - std::__throw_out_of_range_fmt: std::bitset::to_ulong
21 * - std::__throw_bad_alloc: operator new
22 * - std::__throw_bad_function_call: std::function::operator()
23 */
24
25#ifdef USE_ESP_IDF
26#include "esp_system.h"
27
28namespace esphome::esp32 {}
29
30// Linker wraps for std::__throw_* - must be extern "C" at global scope.
31// Names must be __wrap_ + mangled name for the linker's --wrap option.
32
33// NOLINTBEGIN(bugprone-reserved-identifier,cert-dcl37-c,cert-dcl51-cpp,readability-identifier-naming)
34extern "C" {
35
36// std::__throw_length_error(char const*) - called when container size exceeds max_size()
37void __wrap__ZSt20__throw_length_errorPKc(const char *) { esp_system_abort("std::length_error"); }
38
39// std::__throw_logic_error(char const*) - called for logic errors (e.g., promise already satisfied)
40void __wrap__ZSt19__throw_logic_errorPKc(const char *) { esp_system_abort("std::logic_error"); }
41
42// std::__throw_out_of_range(char const*) - called by at() when index is out of bounds
43void __wrap__ZSt20__throw_out_of_rangePKc(const char *) { esp_system_abort("std::out_of_range"); }
44
45// std::__throw_out_of_range_fmt(char const*, ...) - called by bitset::to_ulong when value doesn't fit
46void __wrap__ZSt24__throw_out_of_range_fmtPKcz(const char *, ...) { esp_system_abort("std::out_of_range"); }
47
48// std::__throw_bad_alloc() - called when operator new fails
49void __wrap__ZSt17__throw_bad_allocv() { esp_system_abort("std::bad_alloc"); }
50
51// std::__throw_bad_function_call() - called when invoking empty std::function
52void __wrap__ZSt25__throw_bad_function_callv() { esp_system_abort("std::bad_function_call"); }
53
54} // extern "C"
55// NOLINTEND(bugprone-reserved-identifier,cert-dcl37-c,cert-dcl51-cpp,readability-identifier-naming)
56
57#endif // USE_ESP_IDF
void __wrap__ZSt25__throw_bad_function_callv()
void __wrap__ZSt17__throw_bad_allocv()
void __wrap__ZSt24__throw_out_of_range_fmtPKcz(const char *,...)
void __wrap__ZSt20__throw_out_of_rangePKc(const char *)
void __wrap__ZSt20__throw_length_errorPKc(const char *)
void __wrap__ZSt19__throw_logic_errorPKc(const char *)