Skip to content

Instantly share code, notes, and snippets.

@kammce
Created December 18, 2025 23:23
Show Gist options
  • Select an option

  • Save kammce/e5fcdf9ded398df80a93610f56a7226a to your computer and use it in GitHub Desktop.

Select an option

Save kammce/e5fcdf9ded398df80a93610f56a7226a to your computer and use it in GitHub Desktop.
Minimum code to override the terminate handler for both LLVM and GCC. Don't forget to add the --wrap linker arguments, otherwise, this will not work.
#include <exception>
std::terminate_handler halt = +[]() { while (true) { continue; } };
extern "C" {
#if defined(__clang__)
// Terminate symbol within LLVM
// NOTE: Add linker argument: -Wl,--wrap=__cxa_terminate_handler
std::terminate_handler __wrap___cxa_terminate_handler = halt;
#elif defined(__GNUC__)
// Terminate symbol within GCC (within the __cxxabiv1 namespace)
// NOTE: Add linker argument: -Wl,--wrap=_ZN10__cxxabiv119__terminate_handlerE
std::terminate_handler __wrap__ZN10__cxxabiv119__terminate_handlerE = halt;
#endif
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment