From 89a6124022dedb99fbc675c5a72b9546459efd01 Mon Sep 17 00:00:00 2001
From: Sergey Fedorov <barracuda@macos-powerpc.org>
Date: Thu, 23 Jan 2025 13:46:57 +0800
Subject: [PATCH] libc++abi: backport fix for __cxa_call_terminate

See: https://lists.llvm.org/pipermail/libcxx-commits/2024-June/085127.html
---
 libcxxabi/include/cxxabi.h      | 3 +++
 libcxxabi/src/cxa_exception.cpp | 5 +++++
 2 files changed, 8 insertions(+)

diff --git a/libcxxabi/include/cxxabi.h b/libcxxabi/include/cxxabi.h
index 2596560d6e9..39682e807fc 100644
--- a/libcxxabi/include/cxxabi.h
+++ b/libcxxabi/include/cxxabi.h
@@ -62,6 +62,9 @@ extern _LIBCXXABI_FUNC_VIS void __cxa_end_cleanup();
 #endif
 extern _LIBCXXABI_FUNC_VIS std::type_info *__cxa_current_exception_type();
 
+// GNU extension
+extern _LIBCXXABI_FUNC_VIS _LIBCXXABI_NORETURN void __cxa_call_terminate(void*) throw();
+
 // 2.5.4 Rethrowing Exceptions
 extern _LIBCXXABI_FUNC_VIS _LIBCXXABI_NORETURN void __cxa_rethrow();
 
diff --git a/libcxxabi/src/cxa_exception.cpp b/libcxxabi/src/cxa_exception.cpp
index 397427a0299..c5bf17c9f17 100644
--- a/libcxxabi/src/cxa_exception.cpp
+++ b/libcxxabi/src/cxa_exception.cpp
@@ -539,6 +539,11 @@ void __cxa_end_catch() {
     }
 }
 
+void __cxa_call_terminate(void* unwind_arg) throw() {
+    __cxa_begin_catch(unwind_arg);
+    std::terminate();
+}
+
 // Note:  exception_header may be masquerading as a __cxa_dependent_exception
 //        and that's ok.  exceptionType is there too.
 //        However watch out for foreign exceptions.  Return null for them.
