1 //===------------------------- cxa_exception.h ----------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //
8 //  This file implements the "Exception Handling APIs"
9 //  https://itanium-cxx-abi.github.io/cxx-abi/abi-eh.html
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef _CXA_EXCEPTION_H
14 #define _CXA_EXCEPTION_H
15 
16 #include <exception> // for std::unexpected_handler and std::terminate_handler
17 #include "cxxabi.h"
18 #include "unwind.h"
19 
20 namespace __cxxabiv1 {
21 
22 static const uint64_t kOurExceptionClass          = 0x434C4E47432B2B00; // CLNGC++\0
23 static const uint64_t kOurDependentExceptionClass = 0x434C4E47432B2B01; // CLNGC++\1
24 static const uint64_t get_vendor_and_language     = 0xFFFFFFFFFFFFFF00; // mask for CLNGC++
25 
26 _LIBCXXABI_HIDDEN uint64_t __getExceptionClass  (const _Unwind_Exception*);
27 _LIBCXXABI_HIDDEN void     __setExceptionClass  (      _Unwind_Exception*, uint64_t);
28 _LIBCXXABI_HIDDEN bool     __isOurExceptionClass(const _Unwind_Exception*);
29 
30 struct _LIBCXXABI_HIDDEN __cxa_exception {
31 #if defined(__LP64__) || defined(_WIN64) || defined(_LIBCXXABI_ARM_EHABI)
32     // Now _Unwind_Exception is marked with __attribute__((aligned)),
33     // which implies __cxa_exception is also aligned. Insert padding
34     // in the beginning of the struct, rather than before unwindHeader.
35     void *reserve;
36 
37     // This is a new field to support C++ 0x exception_ptr.
38     // For binary compatibility it is at the start of this
39     // struct which is prepended to the object thrown in
40     // __cxa_allocate_exception.
41     size_t referenceCount;
42 #endif
43 
44     //  Manage the exception object itself.
45     std::type_info *exceptionType;
46     void (*exceptionDestructor)(void *);
47     std::unexpected_handler unexpectedHandler;
48     std::terminate_handler  terminateHandler;
49 
50     __cxa_exception *nextException;
51 
52     int handlerCount;
53 
54 #if defined(_LIBCXXABI_ARM_EHABI)
55     __cxa_exception* nextPropagatingException;
56     int propagationCount;
57 #else
58     int handlerSwitchValue;
59     const unsigned char *actionRecord;
60     const unsigned char *languageSpecificData;
61     void *catchTemp;
62     void *adjustedPtr;
63 #endif
64 
65 #if !defined(__LP64__) && !defined(_WIN64) && !defined(_LIBCXXABI_ARM_EHABI)
66     // This is a new field to support C++ 0x exception_ptr.
67     // For binary compatibility it is placed where the compiler
68     // previously adding padded to 64-bit align unwindHeader.
69     size_t referenceCount;
70 #endif
71     _Unwind_Exception unwindHeader;
72 };
73 
74 // http://sourcery.mentor.com/archives/cxx-abi-dev/msg01924.html
75 // The layout of this structure MUST match the layout of __cxa_exception, with
76 // primaryException instead of referenceCount.
77 struct _LIBCXXABI_HIDDEN __cxa_dependent_exception {
78 #if defined(__LP64__) || defined(_WIN64) || defined(_LIBCXXABI_ARM_EHABI)
79     void* reserve; // padding.
80     void* primaryException;
81 #endif
82 
83     std::type_info *exceptionType;
84     void (*exceptionDestructor)(void *);
85     std::unexpected_handler unexpectedHandler;
86     std::terminate_handler terminateHandler;
87 
88     __cxa_exception *nextException;
89 
90     int handlerCount;
91 
92 #if defined(_LIBCXXABI_ARM_EHABI)
93     __cxa_exception* nextPropagatingException;
94     int propagationCount;
95 #else
96     int handlerSwitchValue;
97     const unsigned char *actionRecord;
98     const unsigned char *languageSpecificData;
99     void * catchTemp;
100     void *adjustedPtr;
101 #endif
102 
103 #if !defined(__LP64__) && !defined(_WIN64) && !defined(_LIBCXXABI_ARM_EHABI)
104     void* primaryException;
105 #endif
106     _Unwind_Exception unwindHeader;
107 };
108 
109 // Verify the negative offsets of different fields.
110 static_assert(sizeof(_Unwind_Exception) +
111                       offsetof(__cxa_exception, unwindHeader) ==
112                   sizeof(__cxa_exception),
113               "unwindHeader has wrong negative offsets");
114 static_assert(sizeof(_Unwind_Exception) +
115                       offsetof(__cxa_dependent_exception, unwindHeader) ==
116                   sizeof(__cxa_dependent_exception),
117               "unwindHeader has wrong negative offsets");
118 
119 #if defined(_LIBCXXABI_ARM_EHABI)
120 static_assert(offsetof(__cxa_exception, propagationCount) +
121                       sizeof(_Unwind_Exception) + sizeof(void*) ==
122                   sizeof(__cxa_exception),
123               "propagationCount has wrong negative offset");
124 static_assert(offsetof(__cxa_dependent_exception, propagationCount) +
125                       sizeof(_Unwind_Exception) + sizeof(void*) ==
126                   sizeof(__cxa_dependent_exception),
127               "propagationCount has wrong negative offset");
128 #elif defined(__LP64__) || defined(_WIN64)
129 static_assert(offsetof(__cxa_exception, adjustedPtr) +
130                       sizeof(_Unwind_Exception) + sizeof(void*) ==
131                   sizeof(__cxa_exception),
132               "adjustedPtr has wrong negative offset");
133 static_assert(offsetof(__cxa_dependent_exception, adjustedPtr) +
134                       sizeof(_Unwind_Exception) + sizeof(void*) ==
135                   sizeof(__cxa_dependent_exception),
136               "adjustedPtr has wrong negative offset");
137 #else
138 static_assert(offsetof(__cxa_exception, referenceCount) +
139                       sizeof(_Unwind_Exception) + sizeof(void*) ==
140                   sizeof(__cxa_exception),
141               "referenceCount has wrong negative offset");
142 static_assert(offsetof(__cxa_dependent_exception, primaryException) +
143                       sizeof(_Unwind_Exception) + sizeof(void*) ==
144                   sizeof(__cxa_dependent_exception),
145               "primaryException has wrong negative offset");
146 #endif
147 
148 struct _LIBCXXABI_HIDDEN __cxa_eh_globals {
149     __cxa_exception *   caughtExceptions;
150     unsigned int        uncaughtExceptions;
151 #if defined(_LIBCXXABI_ARM_EHABI)
152     __cxa_exception* propagatingExceptions;
153 #endif
154 };
155 
156 extern "C" _LIBCXXABI_FUNC_VIS __cxa_eh_globals * __cxa_get_globals      ();
157 extern "C" _LIBCXXABI_FUNC_VIS __cxa_eh_globals * __cxa_get_globals_fast ();
158 
159 extern "C" _LIBCXXABI_FUNC_VIS void * __cxa_allocate_dependent_exception ();
160 extern "C" _LIBCXXABI_FUNC_VIS void __cxa_free_dependent_exception (void * dependent_exception);
161 
162 }  // namespace __cxxabiv1
163 
164 #endif  // _CXA_EXCEPTION_H
165