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 <yinshengkai@xiaomi.com>
This commit is contained in:
yinshengkai 2024-07-05 15:25:54 +08:00 committed by Xiang Xiao
parent b721f2c84e
commit ad76a4f7a9
2 changed files with 21 additions and 10 deletions

View File

@ -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(

View File

@ -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