From ad76a4f7a908759ada5b21c11f1e0ca4ed45e05c Mon Sep 17 00:00:00 2001 From: yinshengkai Date: Fri, 5 Jul 2024 15:25:54 +0800 Subject: [PATCH] libcxxabi: libcxxabi enables exceptions by default In the following code, even though the application does not use exceptions, an exception is still thrown in libcxx If libcxxabi is not enabled, the toolchain default implementation will be used. However, arm-gcc does not enable thread support by default, which will cause errors in a multi-threaded environment. Therefore, we need to use libcxxabi to ensure normal functions in a multi-threaded environment. using namespace std; void foo(bool recur); int bar(bool recur) { if (recur) { foo(false); } return 0xFAFAFA; } void foo(bool recur) { static int i = bar(recur); cout << "Static is:" << i << "\n"; } int main(int argc, char *argv[]) { foo(true); return 0; } Signed-off-by: yinshengkai --- libs/libxx/libcxxabi.cmake | 19 +++++++++++++------ libs/libxx/libcxxabi.defs | 12 ++++++++---- 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/libs/libxx/libcxxabi.cmake b/libs/libxx/libcxxabi.cmake index 9f552e664d..f55d842a17 100644 --- a/libs/libxx/libcxxabi.cmake +++ b/libs/libxx/libcxxabi.cmake @@ -75,12 +75,9 @@ list(APPEND SRCS stdlib_exception.cpp stdlib_new_delete.cpp # Internal files list(APPEND SRCS abort_message.cpp fallback_malloc.cpp private_typeinfo.cpp) -if(CONFIG_CXX_EXCEPTION) - add_compile_definitions(LIBCXXABI_ENABLE_EXCEPTIONS) - list(APPEND SRCS cxa_exception.cpp cxa_personality.cpp) -else() - list(APPEND SRCS cxa_noexception.cpp) -endif() +# Always compile libcxxabi with exception +list(APPEND SRCS cxa_exception.cpp cxa_personality.cpp) +target_compile_options(libcxxabi PRIVATE -fexceptions) if(CONFIG_LIBCXXABI) add_compile_definitions(LIBCXX_BUILDING_LIBCXXABI) @@ -100,6 +97,16 @@ if(CONFIG_SIM_UBSAN OR CONFIG_MM_UBSAN) target_compile_options(libcxxabi PRIVATE -fno-sanitize=vptr) endif() +# Fix compilation error on ARM32:libcxxabi/src/cxa_personality.cpp:594:22: +# error: '_URC_FATAL_PHASE1_ERROR' was not declared in this scope 594 | +# results.reason = _URC_FATAL_PHASE1_ERROR; +if(CONFIG_ARCH_ARM) + target_compile_definitions(libcxxabi + PRIVATE _URC_FATAL_PHASE2_ERROR=_URC_FAILURE) + target_compile_definitions(libcxxabi + PRIVATE _URC_FATAL_PHASE1_ERROR=_URC_FAILURE) +endif() + target_sources(libcxxabi PRIVATE ${TARGET_SRCS}) target_compile_options(libcxxabi PRIVATE -frtti) target_include_directories( diff --git a/libs/libxx/libcxxabi.defs b/libs/libxx/libcxxabi.defs index a25c24bb23..40f95afc72 100644 --- a/libs/libxx/libcxxabi.defs +++ b/libs/libxx/libcxxabi.defs @@ -65,11 +65,15 @@ CPPSRCS += stdlib_exception.cpp stdlib_new_delete.cpp stdlib_stdexcept.cpp stdli # Internal files CPPSRCS += abort_message.cpp fallback_malloc.cpp private_typeinfo.cpp -ifeq ($(CONFIG_CXX_EXCEPTION), y) -CXXFLAGS += ${DEFINE_PREFIX}LIBCXXABI_ENABLE_EXCEPTIONS +# Always compile libcxxabi with exception CPPSRCS += cxa_exception.cpp cxa_personality.cpp -else -CPPSRCS += cxa_noexception.cpp +CXXFLAGS += -fexceptions + +# Fix compilation error on ARM32: +# libcxxabi/src/cxa_personality.cpp:594:22: error: '_URC_FATAL_PHASE1_ERROR' was not declared in this scope +# 594 | results.reason = _URC_FATAL_PHASE1_ERROR; +ifeq ($(CONFIG_ARCH_ARM),y) +CXXFLAGS += -D_URC_FATAL_PHASE2_ERROR=_URC_FAILURE -D_URC_FATAL_PHASE1_ERROR=_URC_FAILURE endif # RTTI is required for building the libcxxabi library