cxxabi.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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. * Now _Unwind_Exception is marked with __attribute__((aligned)), which
  77. * implies __cxa_exception is also aligned. Insert padding in the
  78. * beginning of the struct, rather than before unwindHeader.
  79. */
  80. void *reserve;
  81. /**
  82. * Reference count. Used to support the C++11 exception_ptr class. This
  83. * is prepended to the structure in 64-bit mode and squeezed in to the
  84. * padding left before the 64-bit aligned _Unwind_Exception at the end in
  85. * 32-bit mode.
  86. *
  87. * Note that it is safe to extend this structure at the beginning, rather
  88. * than the end, because the public API for creating it returns the address
  89. * of the end (where the exception object can be stored).
  90. */
  91. uintptr_t referenceCount;
  92. #endif
  93. /** Type info for the thrown object. */
  94. std::type_info *exceptionType;
  95. /** Destructor for the object, if one exists. */
  96. void (*exceptionDestructor) (void *);
  97. /** Handler called when an exception specification is violated. */
  98. unexpected_handler unexpectedHandler;
  99. /** Hander called to terminate. */
  100. terminate_handler terminateHandler;
  101. /**
  102. * Next exception in the list. If an exception is thrown inside a catch
  103. * block and caught in a nested catch, this points to the exception that
  104. * will be handled after the inner catch block completes.
  105. */
  106. __cxa_exception *nextException;
  107. /**
  108. * The number of handlers that currently have references to this
  109. * exception. The top (non-sign) bit of this is used as a flag to indicate
  110. * that the exception is being rethrown, so should not be deleted when its
  111. * handler count reaches 0 (which it doesn't with the top bit set).
  112. */
  113. int handlerCount;
  114. #if defined(__arm__) && !defined(__ARM_DWARF_EH__)
  115. /**
  116. * The ARM EH ABI requires the unwind library to keep track of exceptions
  117. * during cleanups. These support nesting, so we need to keep a list of
  118. * them.
  119. */
  120. _Unwind_Exception *nextCleanup;
  121. /**
  122. * The number of cleanups that are currently being run on this exception.
  123. */
  124. int cleanupCount;
  125. #endif
  126. /**
  127. * The selector value to be returned when installing the catch handler.
  128. * Used at the call site to determine which catch() block should execute.
  129. * This is found in phase 1 of unwinding then installed in phase 2.
  130. */
  131. int handlerSwitchValue;
  132. /**
  133. * The action record for the catch. This is cached during phase 1
  134. * unwinding.
  135. */
  136. const char *actionRecord;
  137. /**
  138. * Pointer to the language-specific data area (LSDA) for the handler
  139. * frame. This is unused in this implementation, but set for ABI
  140. * compatibility in case we want to mix code in very weird ways.
  141. */
  142. const char *languageSpecificData;
  143. /** The cached landing pad for the catch handler.*/
  144. void *catchTemp;
  145. /**
  146. * The pointer that will be returned as the pointer to the object. When
  147. * throwing a class and catching a virtual superclass (for example), we
  148. * need to adjust the thrown pointer to make it all work correctly.
  149. */
  150. void *adjustedPtr;
  151. #if !__LP64__
  152. /**
  153. * Reference count. Used to support the C++11 exception_ptr class. This
  154. * is prepended to the structure in 64-bit mode and squeezed in to the
  155. * padding left before the 64-bit aligned _Unwind_Exception at the end in
  156. * 32-bit mode.
  157. *
  158. * Note that it is safe to extend this structure at the beginning, rather
  159. * than the end, because the public API for creating it returns the address
  160. * of the end (where the exception object can be stored)
  161. */
  162. uintptr_t referenceCount;
  163. #endif
  164. /** The language-agnostic part of the exception header. */
  165. _Unwind_Exception unwindHeader;
  166. };
  167. /**
  168. * ABI-specified globals structure. Returned by the __cxa_get_globals()
  169. * function and its fast variant. This is a per-thread structure - every
  170. * thread will have one lazily allocated.
  171. *
  172. * This structure is defined by the ABI, so may be used outside of this
  173. * library.
  174. */
  175. struct __cxa_eh_globals
  176. {
  177. /**
  178. * A linked list of exceptions that are currently caught. There may be
  179. * several of these in nested catch() blocks.
  180. */
  181. __cxa_exception *caughtExceptions;
  182. /**
  183. * The number of uncaught exceptions.
  184. */
  185. unsigned int uncaughtExceptions;
  186. };
  187. #define Y_CXA_EH_GLOBALS_COMPLETE
  188. /**
  189. * ABI function returning the __cxa_eh_globals structure.
  190. */
  191. __cxa_eh_globals *__cxa_get_globals(void);
  192. /**
  193. * Version of __cxa_get_globals() assuming that __cxa_get_globals() has already
  194. * been called at least once by this thread.
  195. */
  196. __cxa_eh_globals *__cxa_get_globals_fast(void);
  197. std::type_info * __cxa_current_exception_type();
  198. void *__cxa_allocate_exception(size_t thrown_size);
  199. void __cxa_free_exception(void* thrown_exception);
  200. __cxa_exception *__cxa_init_primary_exception(
  201. void *object, std::type_info* tinfo, void (*dest)(void *));
  202. /**
  203. * Throws an exception returned by __cxa_current_primary_exception(). This
  204. * exception may have been caught in another thread.
  205. */
  206. void __cxa_rethrow_primary_exception(void* thrown_exception);
  207. /**
  208. * Returns the current exception in a form that can be stored in an
  209. * exception_ptr object and then rethrown by a call to
  210. * __cxa_rethrow_primary_exception().
  211. */
  212. void *__cxa_current_primary_exception(void);
  213. /**
  214. * Increments the reference count of an exception. Called when an
  215. * exception_ptr is copied.
  216. */
  217. void __cxa_increment_exception_refcount(void* thrown_exception);
  218. /**
  219. * Decrements the reference count of an exception. Called when an
  220. * exception_ptr is deleted.
  221. */
  222. void __cxa_decrement_exception_refcount(void* thrown_exception);
  223. /**
  224. * Demangles a C++ symbol or type name. The buffer, if non-NULL, must be
  225. * allocated with malloc() and must be *n bytes or more long. This function
  226. * may call realloc() on the value pointed to by buf, and will return the
  227. * length of the string via *n.
  228. *
  229. * The value pointed to by status is set to one of the following:
  230. *
  231. * 0: success
  232. * -1: memory allocation failure
  233. * -2: invalid mangled name
  234. * -3: invalid arguments
  235. */
  236. char* __cxa_demangle(const char* mangled_name,
  237. char* buf,
  238. size_t* n,
  239. int* status);
  240. #ifdef _YNDX_LIBUNWIND_ENABLE_EXCEPTION_BACKTRACE
  241. size_t __cxa_collect_current_exception_backtrace(void** dest, size_t size);
  242. #endif
  243. #ifdef __cplusplus
  244. } // extern "C"
  245. } // namespace
  246. namespace abi = __cxxabiv1;
  247. #endif /* __cplusplus */
  248. #endif /* __CXXABI_H_ */