1 /* 2 * Copyright 2012 David Chisnall. All rights reserved. 3 * 4 * Permission is hereby granted, free of charge, to any person obtaining a copy 5 * of this software and associated documentation files (the "Software"), to 6 * deal in the Software without restriction, including without limitation the 7 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 8 * sell copies of the Software, and to permit persons to whom the Software is 9 * furnished to do so, subject to the following conditions: 10 * 11 * The above copyright notice and this permission notice shall be 12 * included in all copies or substantial portions of the Software. 13 * 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 15 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 17 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 18 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 19 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 20 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 */ 22 23 #ifndef __CXXABI_H_ 24 #define __CXXABI_H_ 25 #include <stddef.h> 26 #include <stdint.h> 27 #include "unwind.h" 28 namespace std 29 { 30 class type_info; 31 } 32 /* 33 * The cxxabi.h header provides a set of public definitions for types and 34 * functions defined by the Itanium C++ ABI specification. For reference, see 35 * the ABI specification here: 36 * 37 * http://sourcery.mentor.com/public/cxx-abi/abi.html 38 * 39 * All deviations from this specification, unless otherwise noted, are 40 * accidental. 41 */ 42 43 #ifdef __cplusplus 44 #if __cplusplus < 201103L 45 #define _LIBCXXRT_NOEXCEPT throw() 46 #else 47 #define _LIBCXXRT_NOEXCEPT noexcept 48 #endif 49 namespace __cxxabiv1 { 50 extern "C" { 51 #else 52 #define _LIBCXXRT_NOEXCEPT 53 #endif 54 /** 55 * Function type to call when an unexpected exception is encountered. 56 */ 57 typedef void (*unexpected_handler)(); 58 /** 59 * Function type to call when an unrecoverable condition is encountered. 60 */ 61 typedef void (*terminate_handler)(); 62 63 64 /** 65 * Structure used as a header on thrown exceptions. This is the same layout as 66 * defined by the Itanium ABI spec, so should be interoperable with any other 67 * implementation of this spec, such as GNU libsupc++. 68 * 69 * This structure is allocated when an exception is thrown. Unwinding happens 70 * in two phases, the first looks for a handler and the second installs the 71 * context. This structure stores a cache of the handler location between 72 * phase 1 and phase 2. Unfortunately, cleanup information is not cached, so 73 * must be looked up in both phases. This happens for two reasons. The first 74 * is that we don't know how many frames containing cleanups there will be, and 75 * we should avoid dynamic allocation during unwinding (the exception may be 76 * reporting that we've run out of memory). The second is that finding 77 * cleanups is much cheaper than finding handlers, because we don't have to 78 * look at the type table at all. 79 * 80 * Note: Several fields of this structure have not-very-informative names. 81 * These are taken from the ABI spec and have not been changed to make it 82 * easier for people referring to to the spec while reading this code. 83 */ 84 struct __cxa_exception 85 { 86 #ifdef __LP64__ 87 /** 88 * Now _Unwind_Exception is marked with __attribute__((aligned)), which 89 * implies __cxa_exception is also aligned. Insert padding in the 90 * beginning of the struct, rather than before unwindHeader. 91 */ 92 void *reserve; 93 94 /** 95 * Reference count. Used to support the C++11 exception_ptr class. This 96 * is prepended to the structure in 64-bit mode and squeezed in to the 97 * padding left before the 64-bit aligned _Unwind_Exception at the end in 98 * 32-bit mode. 99 * 100 * Note that it is safe to extend this structure at the beginning, rather 101 * than the end, because the public API for creating it returns the address 102 * of the end (where the exception object can be stored). 103 */ 104 uintptr_t referenceCount; 105 #endif 106 /** Type info for the thrown object. */ 107 std::type_info *exceptionType; 108 /** Destructor for the object, if one exists. */ 109 void (*exceptionDestructor) (void *); 110 /** Handler called when an exception specification is violated. */ 111 unexpected_handler unexpectedHandler; 112 /** Hander called to terminate. */ 113 terminate_handler terminateHandler; 114 /** 115 * Next exception in the list. If an exception is thrown inside a catch 116 * block and caught in a nested catch, this points to the exception that 117 * will be handled after the inner catch block completes. 118 */ 119 __cxa_exception *nextException; 120 /** 121 * The number of handlers that currently have references to this 122 * exception. The top (non-sign) bit of this is used as a flag to indicate 123 * that the exception is being rethrown, so should not be deleted when its 124 * handler count reaches 0 (which it doesn't with the top bit set). 125 */ 126 int handlerCount; 127 #if defined(__arm__) && !defined(__ARM_DWARF_EH__) 128 /** 129 * The ARM EH ABI requires the unwind library to keep track of exceptions 130 * during cleanups. These support nesting, so we need to keep a list of 131 * them. 132 */ 133 _Unwind_Exception *nextCleanup; 134 /** 135 * The number of cleanups that are currently being run on this exception. 136 */ 137 int cleanupCount; 138 #endif 139 /** 140 * The selector value to be returned when installing the catch handler. 141 * Used at the call site to determine which catch() block should execute. 142 * This is found in phase 1 of unwinding then installed in phase 2. 143 */ 144 int handlerSwitchValue; 145 /** 146 * The action record for the catch. This is cached during phase 1 147 * unwinding. 148 */ 149 const char *actionRecord; 150 /** 151 * Pointer to the language-specific data area (LSDA) for the handler 152 * frame. This is unused in this implementation, but set for ABI 153 * compatibility in case we want to mix code in very weird ways. 154 */ 155 const char *languageSpecificData; 156 /** The cached landing pad for the catch handler.*/ 157 void *catchTemp; 158 /** 159 * The pointer that will be returned as the pointer to the object. When 160 * throwing a class and catching a virtual superclass (for example), we 161 * need to adjust the thrown pointer to make it all work correctly. 162 */ 163 void *adjustedPtr; 164 #ifndef __LP64__ 165 /** 166 * Reference count. Used to support the C++11 exception_ptr class. This 167 * is prepended to the structure in 64-bit mode and squeezed in to the 168 * padding left before the 64-bit aligned _Unwind_Exception at the end in 169 * 32-bit mode. 170 * 171 * Note that it is safe to extend this structure at the beginning, rather 172 * than the end, because the public API for creating it returns the address 173 * of the end (where the exception object can be stored) 174 */ 175 uintptr_t referenceCount; 176 #endif 177 /** The language-agnostic part of the exception header. */ 178 _Unwind_Exception unwindHeader; 179 }; 180 181 /** 182 * ABI-specified globals structure. Returned by the __cxa_get_globals() 183 * function and its fast variant. This is a per-thread structure - every 184 * thread will have one lazily allocated. 185 * 186 * This structure is defined by the ABI, so may be used outside of this 187 * library. 188 */ 189 struct __cxa_eh_globals 190 { 191 /** 192 * A linked list of exceptions that are currently caught. There may be 193 * several of these in nested catch() blocks. 194 */ 195 __cxa_exception *caughtExceptions; 196 /** 197 * The number of uncaught exceptions. 198 */ 199 unsigned int uncaughtExceptions; 200 }; 201 /** 202 * ABI function returning the __cxa_eh_globals structure. 203 */ 204 __cxa_eh_globals *__cxa_get_globals(void); 205 /** 206 * Version of __cxa_get_globals() assuming that __cxa_get_globals() has already 207 * been called at least once by this thread. 208 */ 209 __cxa_eh_globals *__cxa_get_globals_fast(void); 210 211 std::type_info * __cxa_current_exception_type(); 212 213 214 void *__cxa_allocate_exception(size_t thrown_size) _LIBCXXRT_NOEXCEPT; 215 216 void __cxa_free_exception(void* thrown_exception) _LIBCXXRT_NOEXCEPT; 217 218 __cxa_exception *__cxa_init_primary_exception( 219 void *object, std::type_info* tinfo, void (*dest)(void *)) _LIBCXXRT_NOEXCEPT; 220 221 /** 222 * Throws an exception returned by __cxa_current_primary_exception(). This 223 * exception may have been caught in another thread. 224 */ 225 void __cxa_rethrow_primary_exception(void* thrown_exception); 226 /** 227 * Returns the current exception in a form that can be stored in an 228 * exception_ptr object and then rethrown by a call to 229 * __cxa_rethrow_primary_exception(). 230 */ 231 void *__cxa_current_primary_exception(void); 232 /** 233 * Increments the reference count of an exception. Called when an 234 * exception_ptr is copied. 235 */ 236 void __cxa_increment_exception_refcount(void* thrown_exception); 237 /** 238 * Decrements the reference count of an exception. Called when an 239 * exception_ptr is deleted. 240 */ 241 void __cxa_decrement_exception_refcount(void* thrown_exception); 242 /** 243 * Demangles a C++ symbol or type name. The buffer, if non-NULL, must be 244 * allocated with malloc() and must be *n bytes or more long. This function 245 * may call realloc() on the value pointed to by buf, and will return the 246 * length of the string via *n. 247 * 248 * The value pointed to by status is set to one of the following: 249 * 250 * 0: success 251 * -1: memory allocation failure 252 * -2: invalid mangled name 253 * -3: invalid arguments 254 */ 255 char* __cxa_demangle(const char* mangled_name, 256 char* buf, 257 size_t* n, 258 int* status); 259 #ifdef __cplusplus 260 } // extern "C" 261 } // namespace 262 263 namespace abi = __cxxabiv1; 264 265 #endif /* __cplusplus */ 266 #endif /* __CXXABI_H_ */ 267