Skip to content

Commit 57e446d

Browse files
author
Asiri Rathnayake
committed
[libcxxabi] Introduce a -fno-exceptions libc++abi libary variant
Currently there is only support for a -fno-exceptions libc++ build. This is problematic for functions such as std::terminate() which are defined in libc++abi and using any of those functions throws away most of the benefits of using -fno-exceptions (code-size). This patch introduces a -fno-exceptions libc++abi build to address this issue. This new variant of libc++abi cannot be linked against any with-exceptions code as some symbols necessary for handling exceptions are missing in this library. Differential revision: http://reviews.llvm.org/D20677 Reviewers: EricWF, mclow.lists, bcraig llvm-svn: 271267
1 parent 8f961aa commit 57e446d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+450
-17
lines changed

libcxxabi/CMakeLists.txt

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ endif()
108108
#===============================================================================
109109

110110
# Define options.
111+
option(LIBCXXABI_ENABLE_EXCEPTIONS "Use exceptions." ON)
111112
option(LIBCXXABI_ENABLE_ASSERTIONS "Enable assertions independent of build mode." ON)
112113
option(LIBCXXABI_ENABLE_PEDANTIC "Compile with pedantic enabled." ON)
113114
option(LIBCXXABI_ENABLE_WERROR "Fail and stop if a warning is triggered." OFF)
@@ -242,13 +243,20 @@ if (LIBCXXABI_ENABLE_PEDANTIC)
242243
endif()
243244

244245
# Get feature flags.
245-
# Exceptions
246-
# Catches C++ exceptions only and tells the compiler to assume that extern C
247-
# functions never throw a C++ exception.
248246
append_if(LIBCXXABI_CXX_FLAGS LIBCXXABI_HAS_FSTRICT_ALIASING_FLAG -fstrict-aliasing)
249-
append_if(LIBCXXABI_CXX_FLAGS LIBCXXABI_HAS_EHSC_FLAG -EHsc)
250247

251-
append_if(LIBCXXABI_C_FLAGS LIBCXXABI_HAS_FUNWIND_TABLES -funwind-tables)
248+
# Exceptions
249+
if (LIBCXXABI_ENABLE_EXCEPTIONS)
250+
# Catches C++ exceptions only and tells the compiler to assume that extern C
251+
# functions never throw a C++ exception.
252+
append_if(LIBCXXABI_CXX_FLAGS LIBCXXABI_HAS_EHSC_FLAG -EHsc)
253+
append_if(LIBCXXABI_C_FLAGS LIBCXXABI_HAS_FUNWIND_TABLES -funwind-tables)
254+
else()
255+
add_definitions(-D_LIBCXXABI_NO_EXCEPTIONS)
256+
append_if(LIBCXXABI_CXX_FLAGS LIBCXXABI_HAS_NO_EXCEPTIONS_FLAG -fno-exceptions)
257+
append_if(LIBCXXABI_CXX_FLAGS LIBCXXABI_HAS_NO_EHS_FLAG -EHs-)
258+
append_if(LIBCXXABI_CXX_FLAGS LIBCXXABI_HAS_NO_EHA_FLAG -EHa-)
259+
endif()
252260

253261
append_if(LIBCXXABI_COMPILE_FLAGS LIBCXXABI_HAS_FVISIBILITY_HIDDEN_FLAG -fvisibility=hidden)
254262

libcxxabi/src/CMakeLists.txt

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,10 @@ set(LIBCXXABI_SOURCES
44
cxa_aux_runtime.cpp
55
cxa_default_handlers.cpp
66
cxa_demangle.cpp
7-
cxa_exception.cpp
87
cxa_exception_storage.cpp
98
cxa_guard.cpp
109
cxa_handlers.cpp
1110
cxa_new_delete.cpp
12-
cxa_personality.cpp
1311
cxa_unexpected.cpp
1412
cxa_vector.cpp
1513
cxa_virtual.cpp
@@ -19,6 +17,13 @@ set(LIBCXXABI_SOURCES
1917
typeinfo.cpp
2018
)
2119

20+
if (LIBCXXABI_ENABLE_EXCEPTIONS)
21+
list(APPEND LIBCXXABI_SOURCES cxa_exception.cpp)
22+
list(APPEND LIBCXXABI_SOURCES cxa_personality.cpp)
23+
else()
24+
list(APPEND LIBCXXABI_SOURCES cxa_noexception.cpp)
25+
endif()
26+
2227
if (UNIX AND NOT (APPLE OR CYGWIN))
2328
list(APPEND LIBCXXABI_SOURCES cxa_thread_atexit.cpp)
2429
endif()

libcxxabi/src/cxa_aux_runtime.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,28 @@
1717
namespace __cxxabiv1 {
1818
extern "C" {
1919
_LIBCXXABI_FUNC_VIS LIBCXXABI_NORETURN void __cxa_bad_cast(void) {
20+
#ifndef _LIBCXXABI_NO_EXCEPTIONS
2021
throw std::bad_cast();
22+
#else
23+
std::terminate();
24+
#endif
2125
}
2226

2327
_LIBCXXABI_FUNC_VIS LIBCXXABI_NORETURN void __cxa_bad_typeid(void) {
28+
#ifndef _LIBCXXABI_NO_EXCEPTIONS
2429
throw std::bad_typeid();
30+
#else
31+
std::terminate();
32+
#endif
2533
}
2634

2735
_LIBCXXABI_FUNC_VIS LIBCXXABI_NORETURN void
2836
__cxa_throw_bad_array_new_length(void) {
37+
#ifndef _LIBCXXABI_NO_EXCEPTIONS
2938
throw std::bad_array_new_length();
39+
#else
40+
std::terminate();
41+
#endif
3042
}
3143
} // extern "C"
3244
} // abi

libcxxabi/src/cxa_handlers.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,21 +61,21 @@ __attribute__((visibility("hidden"), noreturn))
6161
void
6262
__terminate(terminate_handler func) _NOEXCEPT
6363
{
64-
#if __has_feature(cxx_exceptions)
64+
#ifndef _LIBCXXABI_NO_EXCEPTIONS
6565
try
6666
{
67-
#endif // __has_feature(cxx_exceptions)
67+
#endif // _LIBCXXABI_NO_EXCEPTIONS
6868
func();
6969
// handler should not return
7070
abort_message("terminate_handler unexpectedly returned");
71-
#if __has_feature(cxx_exceptions)
71+
#ifndef _LIBCXXABI_NO_EXCEPTIONS
7272
}
7373
catch (...)
7474
{
7575
// handler should not throw exception
7676
abort_message("terminate_handler unexpectedly threw an exception");
7777
}
78-
#endif // #if __has_feature(cxx_exceptions)
78+
#endif // _LIBCXXABI_NO_EXCEPTIONS
7979
}
8080

8181
__attribute__((noreturn))

libcxxabi/src/cxa_new_delete.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,11 @@ operator new(std::size_t size)
4747
if (nh)
4848
nh();
4949
else
50+
#ifndef _LIBCXXABI_NO_EXCEPTIONS
5051
throw std::bad_alloc();
52+
#else
53+
break;
54+
#endif
5155
}
5256
return p;
5357
}
@@ -74,13 +78,17 @@ operator new(size_t size, const std::nothrow_t&)
7478
#endif
7579
{
7680
void* p = 0;
81+
#ifndef _LIBCXXABI_NO_EXCEPTIONS
7782
try
7883
{
84+
#endif
7985
p = ::operator new(size);
86+
#ifndef _LIBCXXABI_NO_EXCEPTIONS
8087
}
8188
catch (...)
8289
{
8390
}
91+
#endif
8492
return p;
8593
}
8694

@@ -115,13 +123,17 @@ operator new[](size_t size, const std::nothrow_t&)
115123
#endif
116124
{
117125
void* p = 0;
126+
#ifndef _LIBCXXABI_NO_EXCEPTIONS
118127
try
119128
{
129+
#endif
120130
p = ::operator new[](size);
131+
#ifndef _LIBCXXABI_NO_EXCEPTIONS
121132
}
122133
catch (...)
123134
{
124135
}
136+
#endif
125137
return p;
126138
}
127139

libcxxabi/src/cxa_noexception.cpp

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
//===------------------------- cxa_exception.cpp --------------------------===//
2+
//
3+
// The LLVM Compiler Infrastructure
4+
//
5+
// This file is dual licensed under the MIT and the University of Illinois Open
6+
// Source Licenses. See LICENSE.TXT for details.
7+
//
8+
//
9+
// This file implements the "Exception Handling APIs"
10+
// http://mentorembedded.github.io/cxx-abi/abi-eh.html
11+
//
12+
//===----------------------------------------------------------------------===//
13+
14+
// Support functions for the no-exceptions libc++ library
15+
16+
#include "config.h"
17+
#include "cxxabi.h"
18+
19+
#include <exception> // for std::terminate
20+
#include "cxa_exception.hpp"
21+
#include "cxa_handlers.hpp"
22+
23+
namespace __cxxabiv1 {
24+
25+
#pragma GCC visibility push(default)
26+
27+
extern "C" {
28+
29+
void
30+
__cxa_increment_exception_refcount(void *thrown_object) throw() {
31+
if (thrown_object != nullptr)
32+
std::terminate();
33+
}
34+
35+
void
36+
__cxa_decrement_exception_refcount(void *thrown_object) throw() {
37+
if (thrown_object != nullptr)
38+
std::terminate();
39+
}
40+
41+
42+
void *__cxa_current_primary_exception() throw() { return nullptr; }
43+
44+
void
45+
__cxa_rethrow_primary_exception(void* thrown_object) {
46+
if (thrown_object != nullptr)
47+
std::terminate();
48+
}
49+
50+
bool
51+
__cxa_uncaught_exception() throw() { return false; }
52+
53+
unsigned int
54+
__cxa_uncaught_exceptions() throw() { return 0; }
55+
56+
} // extern "C"
57+
58+
#pragma GCC visibility pop
59+
60+
} // abi

libcxxabi/test/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ pythonize_bool(LLVM_BUILD_32_BITS)
1414
pythonize_bool(LIBCXX_ENABLE_SHARED)
1515
pythonize_bool(LIBCXXABI_ENABLE_SHARED)
1616
pythonize_bool(LIBCXXABI_ENABLE_THREADS)
17+
pythonize_bool(LIBCXXABI_ENABLE_EXCEPTIONS)
1718
pythonize_bool(LIBCXXABI_USE_LLVM_UNWINDER)
1819
pythonize_bool(LIBCXXABI_HAS_CXA_THREAD_ATEXIT_IMPL)
1920
set(LIBCXXABI_TARGET_INFO "libcxx.test.target_info.LocalTI" CACHE STRING

libcxxabi/test/backtrace_test.pass.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
// Source Licenses. See LICENSE.TXT for details.
77
//
88
//===----------------------------------------------------------------------===//
9+
10+
// UNSUPPORTED: libcxxabi-no-exceptions
11+
912
#include <assert.h>
1013
#include <stddef.h>
1114
#include <unwind.h>

libcxxabi/test/catch_array_01.pass.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@
99

1010
// Can you have a catch clause of array type that catches anything?
1111

12-
1312
// GCC incorrectly allows array types to be caught by reference.
1413
// See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=69372
1514
// XFAIL: gcc
15+
// UNSUPPORTED: libcxxabi-no-exceptions
1616

1717
#include <cassert>
1818

libcxxabi/test/catch_array_02.pass.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
//===----------------------------------------------------------------------===//
99

1010
// Can you have a catch clause of array type that catches anything?
11+
// UNSUPPORTED: libcxxabi-no-exceptions
1112

1213
#include <cassert>
1314

0 commit comments

Comments
 (0)