14684ddb6SLionel Sambuc /* 24684ddb6SLionel Sambuc * Copyright 2012 David Chisnall. All rights reserved. 34684ddb6SLionel Sambuc * 44684ddb6SLionel Sambuc * Permission is hereby granted, free of charge, to any person obtaining a copy 54684ddb6SLionel Sambuc * of this software and associated documentation files (the "Software"), to 64684ddb6SLionel Sambuc * deal in the Software without restriction, including without limitation the 74684ddb6SLionel Sambuc * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 84684ddb6SLionel Sambuc * sell copies of the Software, and to permit persons to whom the Software is 94684ddb6SLionel Sambuc * furnished to do so, subject to the following conditions: 104684ddb6SLionel Sambuc * 114684ddb6SLionel Sambuc * The above copyright notice and this permission notice shall be 124684ddb6SLionel Sambuc * included in all copies or substantial portions of the Software. 134684ddb6SLionel Sambuc * 144684ddb6SLionel Sambuc * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 154684ddb6SLionel Sambuc * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 164684ddb6SLionel Sambuc * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 174684ddb6SLionel Sambuc * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 184684ddb6SLionel Sambuc * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 194684ddb6SLionel Sambuc * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 204684ddb6SLionel Sambuc * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 214684ddb6SLionel Sambuc */ 224684ddb6SLionel Sambuc 234684ddb6SLionel Sambuc #ifndef __CXXABI_H_ 244684ddb6SLionel Sambuc #define __CXXABI_H_ 25*0a6a1f1dSLionel Sambuc #include <stddef.h> 264684ddb6SLionel Sambuc #include <stdint.h> 274684ddb6SLionel Sambuc #include "unwind.h" 284684ddb6SLionel Sambuc namespace std 294684ddb6SLionel Sambuc { 304684ddb6SLionel Sambuc class type_info; 314684ddb6SLionel Sambuc } 324684ddb6SLionel Sambuc /* 334684ddb6SLionel Sambuc * The cxxabi.h header provides a set of public definitions for types and 344684ddb6SLionel Sambuc * functions defined by the Itanium C++ ABI specification. For reference, see 354684ddb6SLionel Sambuc * the ABI specification here: 364684ddb6SLionel Sambuc * 374684ddb6SLionel Sambuc * http://sourcery.mentor.com/public/cxx-abi/abi.html 384684ddb6SLionel Sambuc * 394684ddb6SLionel Sambuc * All deviations from this specification, unless otherwise noted, are 404684ddb6SLionel Sambuc * accidental. 414684ddb6SLionel Sambuc */ 424684ddb6SLionel Sambuc 434684ddb6SLionel Sambuc #ifdef __cplusplus 444684ddb6SLionel Sambuc namespace __cxxabiv1 { 454684ddb6SLionel Sambuc extern "C" { 464684ddb6SLionel Sambuc #endif 474684ddb6SLionel Sambuc /** 484684ddb6SLionel Sambuc * Function type to call when an unexpected exception is encountered. 494684ddb6SLionel Sambuc */ 504684ddb6SLionel Sambuc typedef void (*unexpected_handler)(); 514684ddb6SLionel Sambuc /** 524684ddb6SLionel Sambuc * Function type to call when an unrecoverable condition is encountered. 534684ddb6SLionel Sambuc */ 544684ddb6SLionel Sambuc typedef void (*terminate_handler)(); 554684ddb6SLionel Sambuc 564684ddb6SLionel Sambuc 574684ddb6SLionel Sambuc /** 584684ddb6SLionel Sambuc * Structure used as a header on thrown exceptions. This is the same layout as 594684ddb6SLionel Sambuc * defined by the Itanium ABI spec, so should be interoperable with any other 604684ddb6SLionel Sambuc * implementation of this spec, such as GNU libsupc++. 614684ddb6SLionel Sambuc * 624684ddb6SLionel Sambuc * This structure is allocated when an exception is thrown. Unwinding happens 634684ddb6SLionel Sambuc * in two phases, the first looks for a handler and the second installs the 644684ddb6SLionel Sambuc * context. This structure stores a cache of the handler location between 654684ddb6SLionel Sambuc * phase 1 and phase 2. Unfortunately, cleanup information is not cached, so 664684ddb6SLionel Sambuc * must be looked up in both phases. This happens for two reasons. The first 674684ddb6SLionel Sambuc * is that we don't know how many frames containing cleanups there will be, and 684684ddb6SLionel Sambuc * we should avoid dynamic allocation during unwinding (the exception may be 694684ddb6SLionel Sambuc * reporting that we've run out of memory). The second is that finding 704684ddb6SLionel Sambuc * cleanups is much cheaper than finding handlers, because we don't have to 714684ddb6SLionel Sambuc * look at the type table at all. 724684ddb6SLionel Sambuc * 734684ddb6SLionel Sambuc * Note: Several fields of this structure have not-very-informative names. 744684ddb6SLionel Sambuc * These are taken from the ABI spec and have not been changed to make it 754684ddb6SLionel Sambuc * easier for people referring to to the spec while reading this code. 764684ddb6SLionel Sambuc */ 774684ddb6SLionel Sambuc struct __cxa_exception 784684ddb6SLionel Sambuc { 794684ddb6SLionel Sambuc #if __LP64__ 804684ddb6SLionel Sambuc /** 814684ddb6SLionel Sambuc * Reference count. Used to support the C++11 exception_ptr class. This 824684ddb6SLionel Sambuc * is prepended to the structure in 64-bit mode and squeezed in to the 834684ddb6SLionel Sambuc * padding left before the 64-bit aligned _Unwind_Exception at the end in 844684ddb6SLionel Sambuc * 32-bit mode. 854684ddb6SLionel Sambuc * 864684ddb6SLionel Sambuc * Note that it is safe to extend this structure at the beginning, rather 874684ddb6SLionel Sambuc * than the end, because the public API for creating it returns the address 884684ddb6SLionel Sambuc * of the end (where the exception object can be stored). 894684ddb6SLionel Sambuc */ 904684ddb6SLionel Sambuc uintptr_t referenceCount; 914684ddb6SLionel Sambuc #endif 924684ddb6SLionel Sambuc /** Type info for the thrown object. */ 934684ddb6SLionel Sambuc std::type_info *exceptionType; 944684ddb6SLionel Sambuc /** Destructor for the object, if one exists. */ 954684ddb6SLionel Sambuc void (*exceptionDestructor) (void *); 964684ddb6SLionel Sambuc /** Handler called when an exception specification is violated. */ 974684ddb6SLionel Sambuc unexpected_handler unexpectedHandler; 984684ddb6SLionel Sambuc /** Hander called to terminate. */ 994684ddb6SLionel Sambuc terminate_handler terminateHandler; 1004684ddb6SLionel Sambuc /** 1014684ddb6SLionel Sambuc * Next exception in the list. If an exception is thrown inside a catch 1024684ddb6SLionel Sambuc * block and caught in a nested catch, this points to the exception that 1034684ddb6SLionel Sambuc * will be handled after the inner catch block completes. 1044684ddb6SLionel Sambuc */ 1054684ddb6SLionel Sambuc __cxa_exception *nextException; 1064684ddb6SLionel Sambuc /** 1074684ddb6SLionel Sambuc * The number of handlers that currently have references to this 1084684ddb6SLionel Sambuc * exception. The top (non-sign) bit of this is used as a flag to indicate 1094684ddb6SLionel Sambuc * that the exception is being rethrown, so should not be deleted when its 1104684ddb6SLionel Sambuc * handler count reaches 0 (which it doesn't with the top bit set). 1114684ddb6SLionel Sambuc */ 1124684ddb6SLionel Sambuc int handlerCount; 113*0a6a1f1dSLionel Sambuc #if defined(__arm__) && !defined(__ARM_DWARF_EH__) 1144684ddb6SLionel Sambuc /** 1154684ddb6SLionel Sambuc * The ARM EH ABI requires the unwind library to keep track of exceptions 1164684ddb6SLionel Sambuc * during cleanups. These support nesting, so we need to keep a list of 1174684ddb6SLionel Sambuc * them. 1184684ddb6SLionel Sambuc */ 1194684ddb6SLionel Sambuc _Unwind_Exception *nextCleanup; 1204684ddb6SLionel Sambuc /** 1214684ddb6SLionel Sambuc * The number of cleanups that are currently being run on this exception. 1224684ddb6SLionel Sambuc */ 1234684ddb6SLionel Sambuc int cleanupCount; 1244684ddb6SLionel Sambuc #endif 1254684ddb6SLionel Sambuc /** 1264684ddb6SLionel Sambuc * The selector value to be returned when installing the catch handler. 1274684ddb6SLionel Sambuc * Used at the call site to determine which catch() block should execute. 1284684ddb6SLionel Sambuc * This is found in phase 1 of unwinding then installed in phase 2. 1294684ddb6SLionel Sambuc */ 1304684ddb6SLionel Sambuc int handlerSwitchValue; 1314684ddb6SLionel Sambuc /** 1324684ddb6SLionel Sambuc * The action record for the catch. This is cached during phase 1 1334684ddb6SLionel Sambuc * unwinding. 1344684ddb6SLionel Sambuc */ 1354684ddb6SLionel Sambuc const char *actionRecord; 1364684ddb6SLionel Sambuc /** 1374684ddb6SLionel Sambuc * Pointer to the language-specific data area (LSDA) for the handler 1384684ddb6SLionel Sambuc * frame. This is unused in this implementation, but set for ABI 1394684ddb6SLionel Sambuc * compatibility in case we want to mix code in very weird ways. 1404684ddb6SLionel Sambuc */ 1414684ddb6SLionel Sambuc const char *languageSpecificData; 1424684ddb6SLionel Sambuc /** The cached landing pad for the catch handler.*/ 1434684ddb6SLionel Sambuc void *catchTemp; 1444684ddb6SLionel Sambuc /** 1454684ddb6SLionel Sambuc * The pointer that will be returned as the pointer to the object. When 1464684ddb6SLionel Sambuc * throwing a class and catching a virtual superclass (for example), we 1474684ddb6SLionel Sambuc * need to adjust the thrown pointer to make it all work correctly. 1484684ddb6SLionel Sambuc */ 1494684ddb6SLionel Sambuc void *adjustedPtr; 1504684ddb6SLionel Sambuc #if !__LP64__ 1514684ddb6SLionel Sambuc /** 1524684ddb6SLionel Sambuc * Reference count. Used to support the C++11 exception_ptr class. This 1534684ddb6SLionel Sambuc * is prepended to the structure in 64-bit mode and squeezed in to the 1544684ddb6SLionel Sambuc * padding left before the 64-bit aligned _Unwind_Exception at the end in 1554684ddb6SLionel Sambuc * 32-bit mode. 1564684ddb6SLionel Sambuc * 1574684ddb6SLionel Sambuc * Note that it is safe to extend this structure at the beginning, rather 1584684ddb6SLionel Sambuc * than the end, because the public API for creating it returns the address 1594684ddb6SLionel Sambuc * of the end (where the exception object can be stored) 1604684ddb6SLionel Sambuc */ 1614684ddb6SLionel Sambuc uintptr_t referenceCount; 1624684ddb6SLionel Sambuc #endif 1634684ddb6SLionel Sambuc /** The language-agnostic part of the exception header. */ 1644684ddb6SLionel Sambuc _Unwind_Exception unwindHeader; 1654684ddb6SLionel Sambuc }; 1664684ddb6SLionel Sambuc 1674684ddb6SLionel Sambuc /** 1684684ddb6SLionel Sambuc * ABI-specified globals structure. Returned by the __cxa_get_globals() 1694684ddb6SLionel Sambuc * function and its fast variant. This is a per-thread structure - every 1704684ddb6SLionel Sambuc * thread will have one lazily allocated. 1714684ddb6SLionel Sambuc * 1724684ddb6SLionel Sambuc * This structure is defined by the ABI, so may be used outside of this 1734684ddb6SLionel Sambuc * library. 1744684ddb6SLionel Sambuc */ 1754684ddb6SLionel Sambuc struct __cxa_eh_globals 1764684ddb6SLionel Sambuc { 1774684ddb6SLionel Sambuc /** 1784684ddb6SLionel Sambuc * A linked list of exceptions that are currently caught. There may be 1794684ddb6SLionel Sambuc * several of these in nested catch() blocks. 1804684ddb6SLionel Sambuc */ 1814684ddb6SLionel Sambuc __cxa_exception *caughtExceptions; 1824684ddb6SLionel Sambuc /** 1834684ddb6SLionel Sambuc * The number of uncaught exceptions. 1844684ddb6SLionel Sambuc */ 1854684ddb6SLionel Sambuc unsigned int uncaughtExceptions; 1864684ddb6SLionel Sambuc }; 1874684ddb6SLionel Sambuc /** 1884684ddb6SLionel Sambuc * ABI function returning the __cxa_eh_globals structure. 1894684ddb6SLionel Sambuc */ 1904684ddb6SLionel Sambuc __cxa_eh_globals *__cxa_get_globals(void); 1914684ddb6SLionel Sambuc /** 1924684ddb6SLionel Sambuc * Version of __cxa_get_globals() assuming that __cxa_get_globals() has already 1934684ddb6SLionel Sambuc * been called at least once by this thread. 1944684ddb6SLionel Sambuc */ 1954684ddb6SLionel Sambuc __cxa_eh_globals *__cxa_get_globals_fast(void); 1964684ddb6SLionel Sambuc 1974684ddb6SLionel Sambuc std::type_info * __cxa_current_exception_type(); 1984684ddb6SLionel Sambuc 1994684ddb6SLionel Sambuc /** 2004684ddb6SLionel Sambuc * Throws an exception returned by __cxa_current_primary_exception(). This 2014684ddb6SLionel Sambuc * exception may have been caught in another thread. 2024684ddb6SLionel Sambuc */ 2034684ddb6SLionel Sambuc void __cxa_rethrow_primary_exception(void* thrown_exception); 2044684ddb6SLionel Sambuc /** 2054684ddb6SLionel Sambuc * Returns the current exception in a form that can be stored in an 2064684ddb6SLionel Sambuc * exception_ptr object and then rethrown by a call to 2074684ddb6SLionel Sambuc * __cxa_rethrow_primary_exception(). 2084684ddb6SLionel Sambuc */ 2094684ddb6SLionel Sambuc void *__cxa_current_primary_exception(void); 2104684ddb6SLionel Sambuc /** 2114684ddb6SLionel Sambuc * Increments the reference count of an exception. Called when an 2124684ddb6SLionel Sambuc * exception_ptr is copied. 2134684ddb6SLionel Sambuc */ 2144684ddb6SLionel Sambuc void __cxa_increment_exception_refcount(void* thrown_exception); 2154684ddb6SLionel Sambuc /** 2164684ddb6SLionel Sambuc * Decrements the reference count of an exception. Called when an 2174684ddb6SLionel Sambuc * exception_ptr is deleted. 2184684ddb6SLionel Sambuc */ 2194684ddb6SLionel Sambuc void __cxa_decrement_exception_refcount(void* thrown_exception); 2204684ddb6SLionel Sambuc /** 2214684ddb6SLionel Sambuc * Demangles a C++ symbol or type name. The buffer, if non-NULL, must be 2224684ddb6SLionel Sambuc * allocated with malloc() and must be *n bytes or more long. This function 2234684ddb6SLionel Sambuc * may call realloc() on the value pointed to by buf, and will return the 2244684ddb6SLionel Sambuc * length of the string via *n. 2254684ddb6SLionel Sambuc * 2264684ddb6SLionel Sambuc * The value pointed to by status is set to one of the following: 2274684ddb6SLionel Sambuc * 2284684ddb6SLionel Sambuc * 0: success 2294684ddb6SLionel Sambuc * -1: memory allocation failure 2304684ddb6SLionel Sambuc * -2: invalid mangled name 2314684ddb6SLionel Sambuc * -3: invalid arguments 2324684ddb6SLionel Sambuc */ 2334684ddb6SLionel Sambuc char* __cxa_demangle(const char* mangled_name, 2344684ddb6SLionel Sambuc char* buf, 2354684ddb6SLionel Sambuc size_t* n, 2364684ddb6SLionel Sambuc int* status); 2374684ddb6SLionel Sambuc #ifdef __cplusplus 2384684ddb6SLionel Sambuc } // extern "C" 2394684ddb6SLionel Sambuc } // namespace 2404684ddb6SLionel Sambuc 2414684ddb6SLionel Sambuc namespace abi = __cxxabiv1; 2424684ddb6SLionel Sambuc 2434684ddb6SLionel Sambuc #endif /* __cplusplus */ 2444684ddb6SLionel Sambuc #endif /* __CXXABI_H_ */ 245