cxx_exception_stubs.cpp 4.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. #include <cstdlib>
  2. #include <cstdio>
  3. #include <exception>
  4. #include <regex>
  5. #include <bits/functexcept.h>
  6. #include <sdkconfig.h>
  7. #ifndef CONFIG_CXX_EXCEPTIONS
  8. const char *FATAL_EXCEPTION = "Fatal C++ exception: ";
  9. extern "C" void __cxx_fatal_exception(void)
  10. {
  11. abort();
  12. }
  13. extern "C" bool __cxx_fatal_exception_bool(void)
  14. {
  15. __cxx_fatal_exception();
  16. return false;
  17. }
  18. extern "C" void __cxx_fatal_exception_message(const char *msg)
  19. {
  20. printf("%s%s\n", FATAL_EXCEPTION, msg);
  21. abort();
  22. }
  23. extern "C" void __cxx_fatal_exception_message_va(const char *msg, ...)
  24. {
  25. __cxx_fatal_exception_message(msg);
  26. }
  27. extern "C" void __cxx_fatal_exception_int(int i)
  28. {
  29. printf("%s (%d)\n", FATAL_EXCEPTION, i);
  30. abort();
  31. }
  32. extern "C" void __cxx_fatal_exception_regex_error_type(std::regex_constants::error_type error)
  33. {
  34. printf("%s Regex error (%u)\n", FATAL_EXCEPTION, error);
  35. abort();
  36. }
  37. #if !GCC_NOT_5_2_0
  38. void std::__throw_bad_exception(void) __attribute__((alias("__cxx_fatal_exception")));
  39. void std::__throw_bad_alloc(void) __attribute__((alias("__cxx_fatal_exception")));
  40. void std::__throw_bad_cast(void) __attribute__((alias("__cxx_fatal_exception")));
  41. void std::__throw_bad_typeid(void) __attribute__((alias("__cxx_fatal_exception")));
  42. void std::__throw_logic_error(const char*) __attribute__((alias("__cxx_fatal_exception_message")));
  43. void std::__throw_domain_error(const char*) __attribute__((alias("__cxx_fatal_exception_message")));
  44. void std::__throw_invalid_argument(const char*) __attribute__((alias("__cxx_fatal_exception_message")));
  45. void std::__throw_length_error(const char*) __attribute__((alias("__cxx_fatal_exception_message")));
  46. void std::__throw_out_of_range(const char*) __attribute__((alias("__cxx_fatal_exception_message")));
  47. void std::__throw_out_of_range_fmt(const char*, ...) __attribute__((alias("__cxx_fatal_exception_message_va")));
  48. void std::__throw_runtime_error(const char*) __attribute__((alias("__cxx_fatal_exception_message")));
  49. void std::__throw_range_error(const char*) __attribute__((alias("__cxx_fatal_exception_message")));
  50. void std::__throw_overflow_error(const char*) __attribute__((alias("__cxx_fatal_exception_message")));
  51. void std::__throw_underflow_error(const char*) __attribute__((alias("__cxx_fatal_exception_message")));
  52. void std::__throw_ios_failure(const char*) __attribute__((alias("__cxx_fatal_exception_message")));
  53. void std::__throw_system_error(int) __attribute__((alias("__cxx_fatal_exception_int")));
  54. void std::__throw_bad_function_call(void) __attribute__((alias("__cxx_fatal_exception")));
  55. void std::__throw_future_error(int) __attribute__((alias("__cxx_fatal_exception_int")));
  56. void std::__throw_regex_error(std::regex_constants::error_type) __attribute__((alias("__cxx_fatal_exception_regex_error_type")));
  57. #endif
  58. /* The following definitions are needed because libstdc++ is also compiled with
  59. __throw_exception_again defined to throw, and some other exception code in a few places.
  60. This cause exception handler code to be emitted in the library even though it's mostly
  61. unreachable (as any libstdc++ "throw" will first call one of the above stubs, which will abort).
  62. If these are left out, a bunch of unwanted exception handler code is linked.
  63. Note: these function prototypes are not correct.
  64. */
  65. extern "C" void __cxa_allocate_exception(void) __attribute__((alias("__cxx_fatal_exception")));
  66. extern "C" void __cxa_allocate_dependent_exception(void) __attribute__((alias("__cxx_fatal_exception")));
  67. extern "C" void __cxa_begin_catch(void) __attribute__((alias("__cxx_fatal_exception")));
  68. extern "C" void __cxa_end_catch(void) __attribute__((alias("__cxx_fatal_exception")));
  69. extern "C" void __cxa_get_exception_ptr(void) __attribute__((alias("__cxx_fatal_exception")));
  70. extern "C" void __cxa_free_exception(void) __attribute__((alias("__cxx_fatal_exception")));
  71. extern "C" void __cxa_free_dependent_exception(void) __attribute__((alias("__cxx_fatal_exception")));
  72. extern "C" void __cxa_rethrow(void) __attribute__((alias("__cxx_fatal_exception")));
  73. extern "C" void __cxa_throw(void) __attribute__((alias("__cxx_fatal_exception")));
  74. extern "C" void __cxa_call_terminate(void) __attribute__((alias("__cxx_fatal_exception")));
  75. bool std::uncaught_exception() __attribute__((alias("__cxx_fatal_exception_bool")));
  76. #endif // CONFIG_CXX_EXCEPTIONS