cxxabi.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. #ifndef __CXXABI_H_
  23. #define __CXXABI_H_
  24. #include <stdint.h>
  25. #include "unwind.h"
  26. namespace std
  27. {
  28. class type_info;
  29. }
  30. /*
  31. * The cxxabi.h header provides a set of public definitions for types and
  32. * functions defined by the Itanium C++ ABI specification. For reference, see
  33. * the ABI specification here:
  34. *
  35. * http://sourcery.mentor.com/public/cxx-abi/abi.html
  36. *
  37. * All deviations from this specification, unless otherwise noted, are
  38. * accidental.
  39. */
  40. #ifdef __cplusplus
  41. namespace __cxxabiv1 {
  42. extern "C" {
  43. #endif
  44. /**
  45. * Function type to call when an unexpected exception is encountered.
  46. */
  47. typedef void (*unexpected_handler)();
  48. /**
  49. * Function type to call when an unrecoverable condition is encountered.
  50. */
  51. typedef void (*terminate_handler)();
  52. /**
  53. * Structure used as a header on thrown exceptions. This is the same layout as
  54. * defined by the Itanium ABI spec, so should be interoperable with any other
  55. * implementation of this spec, such as GNU libsupc++.
  56. *
  57. * This structure is allocated when an exception is thrown. Unwinding happens
  58. * in two phases, the first looks for a handler and the second installs the
  59. * context. This structure stores a cache of the handler location between
  60. * phase 1 and phase 2. Unfortunately, cleanup information is not cached, so
  61. * must be looked up in both phases. This happens for two reasons. The first
  62. * is that we don't know how many frames containing cleanups there will be, and
  63. * we should avoid dynamic allocation during unwinding (the exception may be
  64. * reporting that we've run out of memory). The second is that finding
  65. * cleanups is much cheaper than finding handlers, because we don't have to
  66. * look at the type table at all.
  67. *
  68. * Note: Several fields of this structure have not-very-informative names.
  69. * These are taken from the ABI spec and have not been changed to make it
  70. * easier for people referring to to the spec while reading this code.
  71. */
  72. struct __cxa_exception
  73. {
  74. #if __LP64__
  75. /**
  76. * Reference count. Used to support the C++11 exception_ptr class. This
  77. * is prepended to the structure in 64-bit mode and squeezed in to the
  78. * padding left before the 64-bit aligned _Unwind_Exception at the end in
  79. * 32-bit mode.
  80. *
  81. * Note that it is safe to extend this structure at the beginning, rather
  82. * than the end, because the public API for creating it returns the address
  83. * of the end (where the exception object can be stored).
  84. */
  85. uintptr_t referenceCount;
  86. #endif
  87. /** Type info for the thrown object. */
  88. std::type_info *exceptionType;
  89. /** Destructor for the object, if one exists. */
  90. void (*exceptionDestructor) (void *);
  91. /** Handler called when an exception specification is violated. */
  92. unexpected_handler unexpectedHandler;
  93. /** Hander called to terminate. */
  94. terminate_handler terminateHandler;
  95. /**
  96. * Next exception in the list. If an exception is thrown inside a catch
  97. * block and caught in a nested catch, this points to the exception that
  98. * will be handled after the inner catch block completes.
  99. */
  100. __cxa_exception *nextException;
  101. /**
  102. * The number of handlers that currently have references to this
  103. * exception. The top (non-sign) bit of this is used as a flag to indicate
  104. * that the exception is being rethrown, so should not be deleted when its
  105. * handler count reaches 0 (which it doesn't with the top bit set).
  106. */
  107. int handlerCount;
  108. #ifdef __arm__
  109. /**
  110. * The ARM EH ABI requires the unwind library to keep track of exceptions
  111. * during cleanups. These support nesting, so we need to keep a list of
  112. * them.
  113. */
  114. _Unwind_Exception *nextCleanup;
  115. /**
  116. * The number of cleanups that are currently being run on this exception.
  117. */
  118. int cleanupCount;
  119. #endif
  120. /**
  121. * The selector value to be returned when installing the catch handler.
  122. * Used at the call site to determine which catch() block should execute.
  123. * This is found in phase 1 of unwinding then installed in phase 2.
  124. */
  125. int handlerSwitchValue;
  126. /**
  127. * The action record for the catch. This is cached during phase 1
  128. * unwinding.
  129. */
  130. const char *actionRecord;
  131. /**
  132. * Pointer to the language-specific data area (LSDA) for the handler
  133. * frame. This is unused in this implementation, but set for ABI
  134. * compatibility in case we want to mix code in very weird ways.
  135. */
  136. const char *languageSpecificData;
  137. /** The cached landing pad for the catch handler.*/
  138. void *catchTemp;
  139. /**
  140. * The pointer that will be returned as the pointer to the object. When
  141. * throwing a class and catching a virtual superclass (for example), we
  142. * need to adjust the thrown pointer to make it all work correctly.
  143. */
  144. void *adjustedPtr;
  145. #if !__LP64__
  146. /**
  147. * Reference count. Used to support the C++11 exception_ptr class. This
  148. * is prepended to the structure in 64-bit mode and squeezed in to the
  149. * padding left before the 64-bit aligned _Unwind_Exception at the end in
  150. * 32-bit mode.
  151. *
  152. * Note that it is safe to extend this structure at the beginning, rather
  153. * than the end, because the public API for creating it returns the address
  154. * of the end (where the exception object can be stored)
  155. */
  156. uintptr_t referenceCount;
  157. #endif
  158. /** The language-agnostic part of the exception header. */
  159. _Unwind_Exception unwindHeader;
  160. };
  161. /**
  162. * ABI-specified globals structure. Returned by the __cxa_get_globals()
  163. * function and its fast variant. This is a per-thread structure - every
  164. * thread will have one lazily allocated.
  165. *
  166. * This structure is defined by the ABI, so may be used outside of this
  167. * library.
  168. */
  169. struct __cxa_eh_globals
  170. {
  171. /**
  172. * A linked list of exceptions that are currently caught. There may be
  173. * several of these in nested catch() blocks.
  174. */
  175. __cxa_exception *caughtExceptions;
  176. /**
  177. * The number of uncaught exceptions.
  178. */
  179. unsigned int uncaughtExceptions;
  180. };
  181. /**
  182. * ABI function returning the __cxa_eh_globals structure.
  183. */
  184. __cxa_eh_globals *__cxa_get_globals(void);
  185. /**
  186. * Version of __cxa_get_globals() assuming that __cxa_get_globals() has already
  187. * been called at least once by this thread.
  188. */
  189. __cxa_eh_globals *__cxa_get_globals_fast(void);
  190. /**
  191. * Throws an exception returned by __cxa_current_primary_exception(). This
  192. * exception may have been caught in another thread.
  193. */
  194. void __cxa_rethrow_primary_exception(void* thrown_exception);
  195. /**
  196. * Returns the current exception in a form that can be stored in an
  197. * exception_ptr object and then rethrown by a call to
  198. * __cxa_rethrow_primary_exception().
  199. */
  200. void *__cxa_current_primary_exception(void);
  201. /**
  202. * Increments the reference count of an exception. Called when an
  203. * exception_ptr is copied.
  204. */
  205. void __cxa_increment_exception_refcount(void* thrown_exception);
  206. /**
  207. * Decrements the reference count of an exception. Called when an
  208. * exception_ptr is deleted.
  209. */
  210. void __cxa_decrement_exception_refcount(void* thrown_exception);
  211. /**
  212. * Demangles a C++ symbol or type name. The buffer, if non-NULL, must be
  213. * allocated with malloc() and must be *n bytes or more long. This function
  214. * may call realloc() on the value pointed to by buf, and will return the
  215. * length of the string via *n.
  216. *
  217. * The value pointed to by status is set to one of the following:
  218. *
  219. * 0: success
  220. * -1: memory allocation failure
  221. * -2: invalid mangled name
  222. * -3: invalid arguments
  223. */
  224. char* __cxa_demangle(const char* mangled_name,
  225. char* buf,
  226. size_t* n,
  227. int* status);
  228. #ifdef __cplusplus
  229. } // extern "C"
  230. } // namespace
  231. namespace abi = __cxxabiv1;
  232. #endif /* __cplusplus */
  233. #endif /* __CXXABI_H_ */