unwind-cxx.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. // -*- C++ -*- Exception handling and frame unwind runtime interface routines.
  2. // Copyright (C) 2001-2022 Free Software Foundation, Inc.
  3. //
  4. // This file is part of GCC.
  5. //
  6. // GCC is free software; you can redistribute it and/or modify
  7. // it under the terms of the GNU General Public License as published by
  8. // the Free Software Foundation; either version 3, or (at your option)
  9. // any later version.
  10. //
  11. // GCC is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. // GNU General Public License for more details.
  15. //
  16. // Under Section 7 of GPL version 3, you are granted additional
  17. // permissions described in the GCC Runtime Library Exception, version
  18. // 3.1, as published by the Free Software Foundation.
  19. // You should have received a copy of the GNU General Public License and
  20. // a copy of the GCC Runtime Library Exception along with this program;
  21. // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  22. // <http://www.gnu.org/licenses/>.
  23. // This is derived from the C++ ABI for IA-64. Where we diverge
  24. // for cross-architecture compatibility are noted with "@@@".
  25. #ifndef _UNWIND_CXX_H
  26. #define _UNWIND_CXX_H 1
  27. // Level 2: C++ ABI
  28. #include <typeinfo>
  29. #include <exception>
  30. #include <cstddef>
  31. #include "unwind.h"
  32. #include <bits/atomic_word.h>
  33. #include <cxxabi.h>
  34. #ifdef _GLIBCXX_HAVE_SYS_SDT_H
  35. #include <sys/sdt.h>
  36. /* We only want to use stap probes starting with v3. Earlier versions
  37. added too much startup cost. */
  38. #if defined (STAP_PROBE2) && _SDT_NOTE_TYPE >= 3
  39. #define PROBE2(name, arg1, arg2) STAP_PROBE2 (libstdcxx, name, arg1, arg2)
  40. #endif
  41. #endif
  42. #ifndef PROBE2
  43. #define PROBE2(name, arg1, arg2)
  44. #endif
  45. #pragma GCC visibility push(default)
  46. namespace __cxxabiv1
  47. {
  48. // A primary C++ exception object consists of a header, which is a wrapper
  49. // around an unwind object header with additional C++ specific information,
  50. // followed by the exception object itself.
  51. struct __cxa_exception
  52. {
  53. // Manage the exception object itself.
  54. std::type_info *exceptionType;
  55. void (_GLIBCXX_CDTOR_CALLABI *exceptionDestructor)(void *);
  56. // The C++ standard has entertaining rules wrt calling set_terminate
  57. // and set_unexpected in the middle of the exception cleanup process.
  58. std::terminate_handler unexpectedHandler;
  59. std::terminate_handler terminateHandler;
  60. // The caught exception stack threads through here.
  61. __cxa_exception *nextException;
  62. // How many nested handlers have caught this exception. A negated
  63. // value is a signal that this object has been rethrown.
  64. int handlerCount;
  65. #ifdef __ARM_EABI_UNWINDER__
  66. // Stack of exceptions in cleanups.
  67. __cxa_exception* nextPropagatingException;
  68. // The number of active cleanup handlers for this exception.
  69. int propagationCount;
  70. #else
  71. // Cache parsed handler data from the personality routine Phase 1
  72. // for Phase 2 and __cxa_call_unexpected.
  73. int handlerSwitchValue;
  74. const unsigned char *actionRecord;
  75. const unsigned char *languageSpecificData;
  76. _Unwind_Ptr catchTemp;
  77. void *adjustedPtr;
  78. #endif
  79. // The generic exception header. Must be last.
  80. _Unwind_Exception unwindHeader;
  81. };
  82. struct __cxa_refcounted_exception
  83. {
  84. // Manage this header.
  85. _Atomic_word referenceCount;
  86. // __cxa_exception must be last, and no padding can be after it.
  87. __cxa_exception exc;
  88. };
  89. // A dependent C++ exception object consists of a wrapper around an unwind
  90. // object header with additional C++ specific information, containing a pointer
  91. // to a primary exception object.
  92. struct __cxa_dependent_exception
  93. {
  94. // The primary exception this thing depends on.
  95. void *primaryException;
  96. // Unused member to get similar layout to __cxa_exception, otherwise the
  97. // alignment requirements of _Unwind_Exception would require padding bytes
  98. // before the unwindHeader member.
  99. void (_GLIBCXX_CDTOR_CALLABI *__padding)(void *);
  100. // The C++ standard has entertaining rules wrt calling set_terminate
  101. // and set_unexpected in the middle of the exception cleanup process.
  102. std::terminate_handler unexpectedHandler;
  103. std::terminate_handler terminateHandler;
  104. // The caught exception stack threads through here.
  105. __cxa_exception *nextException;
  106. // How many nested handlers have caught this exception. A negated
  107. // value is a signal that this object has been rethrown.
  108. int handlerCount;
  109. #ifdef __ARM_EABI_UNWINDER__
  110. // Stack of exceptions in cleanups.
  111. __cxa_exception* nextPropagatingException;
  112. // The number of active cleanup handlers for this exception.
  113. int propagationCount;
  114. #else
  115. // Cache parsed handler data from the personality routine Phase 1
  116. // for Phase 2 and __cxa_call_unexpected.
  117. int handlerSwitchValue;
  118. const unsigned char *actionRecord;
  119. const unsigned char *languageSpecificData;
  120. _Unwind_Ptr catchTemp;
  121. void *adjustedPtr;
  122. #endif
  123. // The generic exception header. Must be last.
  124. _Unwind_Exception unwindHeader;
  125. };
  126. // Each thread in a C++ program has access to a __cxa_eh_globals object.
  127. struct __cxa_eh_globals
  128. {
  129. __cxa_exception *caughtExceptions;
  130. unsigned int uncaughtExceptions;
  131. #ifdef __ARM_EABI_UNWINDER__
  132. __cxa_exception* propagatingExceptions;
  133. #endif
  134. };
  135. // @@@ These are not directly specified by the IA-64 C++ ABI.
  136. // Handles re-checking the exception specification if unexpectedHandler
  137. // throws, and if bad_exception needs to be thrown. Called from the
  138. // compiler.
  139. extern "C" void __cxa_call_unexpected (void *) __attribute__((__noreturn__));
  140. extern "C" void __cxa_call_terminate (_Unwind_Exception*) throw ()
  141. __attribute__((__noreturn__));
  142. #ifdef __ARM_EABI_UNWINDER__
  143. // Arm EABI specified routines.
  144. typedef enum {
  145. ctm_failed = 0,
  146. ctm_succeeded = 1,
  147. ctm_succeeded_with_ptr_to_base = 2
  148. } __cxa_type_match_result;
  149. extern "C" __cxa_type_match_result __cxa_type_match(_Unwind_Exception*,
  150. const std::type_info*,
  151. bool, void**);
  152. extern "C" bool __cxa_begin_cleanup (_Unwind_Exception*);
  153. extern "C" void __cxa_end_cleanup (void);
  154. #endif
  155. // Handles cleanup from transactional memory restart.
  156. extern "C" void __cxa_tm_cleanup (void *, void *, unsigned int) throw();
  157. // Invokes given handler, dying appropriately if the user handler was
  158. // so inconsiderate as to return.
  159. extern void __terminate(std::terminate_handler) throw ()
  160. __attribute__((__noreturn__));
  161. extern void __unexpected(std::terminate_handler)
  162. __attribute__((__noreturn__));
  163. // The current installed user handlers.
  164. extern std::terminate_handler __terminate_handler;
  165. extern std::terminate_handler __unexpected_handler;
  166. // These are explicitly GNU C++ specific.
  167. // Acquire the C++ exception header from the C++ object.
  168. static inline __cxa_exception *
  169. __get_exception_header_from_obj (void *ptr)
  170. {
  171. return reinterpret_cast<__cxa_exception *>(ptr) - 1;
  172. }
  173. // Acquire the C++ exception header from the generic exception header.
  174. static inline __cxa_exception *
  175. __get_exception_header_from_ue (_Unwind_Exception *exc)
  176. {
  177. return reinterpret_cast<__cxa_exception *>(exc + 1) - 1;
  178. }
  179. // Acquire the C++ refcounted exception header from the C++ object.
  180. static inline __cxa_refcounted_exception *
  181. __get_refcounted_exception_header_from_obj (void *ptr)
  182. {
  183. return reinterpret_cast<__cxa_refcounted_exception *>(ptr) - 1;
  184. }
  185. // Acquire the C++ refcounted exception header from the generic exception
  186. // header.
  187. static inline __cxa_refcounted_exception *
  188. __get_refcounted_exception_header_from_ue (_Unwind_Exception *exc)
  189. {
  190. return reinterpret_cast<__cxa_refcounted_exception *>(exc + 1) - 1;
  191. }
  192. static inline __cxa_dependent_exception *
  193. __get_dependent_exception_from_ue (_Unwind_Exception *exc)
  194. {
  195. return reinterpret_cast<__cxa_dependent_exception *>(exc + 1) - 1;
  196. }
  197. #ifdef __ARM_EABI_UNWINDER__
  198. static inline bool
  199. __is_gxx_exception_class(_Unwind_Exception_Class c)
  200. {
  201. // TODO: Take advantage of the fact that c will always be word aligned.
  202. return c[0] == 'G'
  203. && c[1] == 'N'
  204. && c[2] == 'U'
  205. && c[3] == 'C'
  206. && c[4] == 'C'
  207. && c[5] == '+'
  208. && c[6] == '+'
  209. && (c[7] == '\0' || c[7] == '\x01');
  210. }
  211. // Only checks for primary or dependent, but not that it is a C++ exception at
  212. // all.
  213. static inline bool
  214. __is_dependent_exception(_Unwind_Exception_Class c)
  215. {
  216. return c[7] == '\x01';
  217. }
  218. static inline void
  219. __GXX_INIT_PRIMARY_EXCEPTION_CLASS(_Unwind_Exception_Class c)
  220. {
  221. c[0] = 'G';
  222. c[1] = 'N';
  223. c[2] = 'U';
  224. c[3] = 'C';
  225. c[4] = 'C';
  226. c[5] = '+';
  227. c[6] = '+';
  228. c[7] = '\0';
  229. }
  230. static inline void
  231. __GXX_INIT_DEPENDENT_EXCEPTION_CLASS(_Unwind_Exception_Class c)
  232. {
  233. c[0] = 'G';
  234. c[1] = 'N';
  235. c[2] = 'U';
  236. c[3] = 'C';
  237. c[4] = 'C';
  238. c[5] = '+';
  239. c[6] = '+';
  240. c[7] = '\x01';
  241. }
  242. static inline bool
  243. __is_gxx_forced_unwind_class(_Unwind_Exception_Class c)
  244. {
  245. return c[0] == 'G'
  246. && c[1] == 'N'
  247. && c[2] == 'U'
  248. && c[3] == 'C'
  249. && c[4] == 'F'
  250. && c[5] == 'O'
  251. && c[6] == 'R'
  252. && c[7] == '\0';
  253. }
  254. static inline void
  255. __GXX_INIT_FORCED_UNWIND_CLASS(_Unwind_Exception_Class c)
  256. {
  257. c[0] = 'G';
  258. c[1] = 'N';
  259. c[2] = 'U';
  260. c[3] = 'C';
  261. c[4] = 'F';
  262. c[5] = 'O';
  263. c[6] = 'R';
  264. c[7] = '\0';
  265. }
  266. static inline void*
  267. __gxx_caught_object(_Unwind_Exception* eo)
  268. {
  269. return (void*)eo->barrier_cache.bitpattern[0];
  270. }
  271. #else // !__ARM_EABI_UNWINDER__
  272. // This is the primary exception class we report -- "GNUCC++\0".
  273. const _Unwind_Exception_Class __gxx_primary_exception_class
  274. = ((((((((_Unwind_Exception_Class) 'G'
  275. << 8 | (_Unwind_Exception_Class) 'N')
  276. << 8 | (_Unwind_Exception_Class) 'U')
  277. << 8 | (_Unwind_Exception_Class) 'C')
  278. << 8 | (_Unwind_Exception_Class) 'C')
  279. << 8 | (_Unwind_Exception_Class) '+')
  280. << 8 | (_Unwind_Exception_Class) '+')
  281. << 8 | (_Unwind_Exception_Class) '\0');
  282. // This is the dependent (from std::rethrow_exception) exception class we report
  283. // "GNUCC++\x01"
  284. const _Unwind_Exception_Class __gxx_dependent_exception_class
  285. = ((((((((_Unwind_Exception_Class) 'G'
  286. << 8 | (_Unwind_Exception_Class) 'N')
  287. << 8 | (_Unwind_Exception_Class) 'U')
  288. << 8 | (_Unwind_Exception_Class) 'C')
  289. << 8 | (_Unwind_Exception_Class) 'C')
  290. << 8 | (_Unwind_Exception_Class) '+')
  291. << 8 | (_Unwind_Exception_Class) '+')
  292. << 8 | (_Unwind_Exception_Class) '\x01');
  293. static inline bool
  294. __is_gxx_exception_class(_Unwind_Exception_Class c)
  295. {
  296. return c == __gxx_primary_exception_class
  297. || c == __gxx_dependent_exception_class;
  298. }
  299. // Only checks for primary or dependent, but not that it is a C++ exception at
  300. // all.
  301. static inline bool
  302. __is_dependent_exception(_Unwind_Exception_Class c)
  303. {
  304. return (c & 1);
  305. }
  306. #define __GXX_INIT_PRIMARY_EXCEPTION_CLASS(c) c = __gxx_primary_exception_class
  307. #define __GXX_INIT_DEPENDENT_EXCEPTION_CLASS(c) \
  308. c = __gxx_dependent_exception_class
  309. // GNU C++ personality routine, Version 0.
  310. extern "C" _Unwind_Reason_Code __gxx_personality_v0
  311. (int, _Unwind_Action, _Unwind_Exception_Class,
  312. struct _Unwind_Exception *, struct _Unwind_Context *);
  313. // GNU C++ sjlj personality routine, Version 0.
  314. extern "C" _Unwind_Reason_Code __gxx_personality_sj0
  315. (int, _Unwind_Action, _Unwind_Exception_Class,
  316. struct _Unwind_Exception *, struct _Unwind_Context *);
  317. static inline void*
  318. __gxx_caught_object(_Unwind_Exception* eo)
  319. {
  320. // Bad as it looks, this actually works for dependent exceptions too.
  321. __cxa_exception* header = __get_exception_header_from_ue (eo);
  322. return header->adjustedPtr;
  323. }
  324. #endif // !__ARM_EABI_UNWINDER__
  325. static inline void*
  326. __get_object_from_ue(_Unwind_Exception* eo) throw()
  327. {
  328. return __is_dependent_exception (eo->exception_class) ?
  329. __get_dependent_exception_from_ue (eo)->primaryException :
  330. eo + 1;
  331. }
  332. static inline void *
  333. __get_object_from_ambiguous_exception(__cxa_exception *p_or_d) throw()
  334. {
  335. return __get_object_from_ue (&p_or_d->unwindHeader);
  336. }
  337. } /* namespace __cxxabiv1 */
  338. #pragma GCC visibility pop
  339. #endif // _UNWIND_CXX_H