eh_arm.cc 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. // -*- C++ -*- ARM specific Exception handling support routines.
  2. // Copyright (C) 2004-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. //
  20. // You should have received a copy of the GNU General Public License and
  21. // a copy of the GCC Runtime Library Exception along with this program;
  22. // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  23. // <http://www.gnu.org/licenses/>.
  24. #include <cxxabi.h>
  25. #include "unwind-cxx.h"
  26. #ifdef __ARM_EABI_UNWINDER__
  27. using namespace __cxxabiv1;
  28. // Given the thrown type THROW_TYPE, exception object UE_HEADER and a
  29. // type CATCH_TYPE to compare against, return whether or not there is
  30. // a match and if so, update *THROWN_PTR_P to point to either the
  31. // type-matched object, or in the case of a pointer type, the object
  32. // pointed to by the pointer.
  33. extern "C" __cxa_type_match_result
  34. __cxa_type_match(_Unwind_Exception* ue_header,
  35. const std::type_info* catch_type,
  36. bool is_reference __attribute__((__unused__)),
  37. void** thrown_ptr_p)
  38. {
  39. bool forced_unwind
  40. = __is_gxx_forced_unwind_class(ue_header->exception_class);
  41. bool foreign_exception
  42. = !forced_unwind && !__is_gxx_exception_class(ue_header->exception_class);
  43. bool dependent_exception
  44. = __is_dependent_exception(ue_header->exception_class);
  45. __cxa_exception* xh = __get_exception_header_from_ue(ue_header);
  46. __cxa_dependent_exception *dx = __get_dependent_exception_from_ue(ue_header);
  47. const std::type_info* throw_type;
  48. void *thrown_ptr = 0;
  49. if (forced_unwind)
  50. throw_type = &typeid(abi::__forced_unwind);
  51. else if (foreign_exception)
  52. throw_type = &typeid(abi::__foreign_exception);
  53. else
  54. {
  55. if (dependent_exception)
  56. xh = __get_exception_header_from_obj (dx->primaryException);
  57. throw_type = xh->exceptionType;
  58. // We used to require the caller set the target of thrown_ptr_p,
  59. // but that's incorrect -- the EHABI makes no such requirement
  60. // -- and not all callers will set it. Fortunately callers that
  61. // do initialize will always pass us the value we calculate
  62. // here, so there's no backwards compatibility problem.
  63. thrown_ptr = __get_object_from_ue (ue_header);
  64. }
  65. __cxa_type_match_result result = ctm_succeeded;
  66. // Pointer types need to adjust the actual pointer, not
  67. // the pointer to pointer that is the exception object.
  68. // This also has the effect of passing pointer types
  69. // "by value" through the __cxa_begin_catch return value.
  70. if (throw_type->__is_pointer_p())
  71. {
  72. thrown_ptr = *(void**) thrown_ptr;
  73. // We need to indicate the indirection to our caller.
  74. result = ctm_succeeded_with_ptr_to_base;
  75. }
  76. if (catch_type->__do_catch(throw_type, &thrown_ptr, 1))
  77. {
  78. *thrown_ptr_p = thrown_ptr;
  79. return result;
  80. }
  81. return ctm_failed;
  82. }
  83. // ABI defined routine called at the start of a cleanup handler.
  84. extern "C" bool
  85. __cxa_begin_cleanup(_Unwind_Exception* ue_header)
  86. {
  87. __cxa_eh_globals *globals = __cxa_get_globals();
  88. __cxa_exception *header = __get_exception_header_from_ue(ue_header);
  89. bool native = __is_gxx_exception_class(header->unwindHeader.exception_class);
  90. if (native)
  91. {
  92. header->propagationCount++;
  93. // Add it to the chain if this is the first time we've seen this
  94. // exception.
  95. if (header->propagationCount == 1)
  96. {
  97. header->nextPropagatingException = globals->propagatingExceptions;
  98. globals->propagatingExceptions = header;
  99. }
  100. }
  101. else
  102. {
  103. // Remember the exception object, so end_cleanup can return it.
  104. // These cannot be stacked, so we must abort if we already have
  105. // a propagating exception.
  106. if (globals->propagatingExceptions)
  107. std::terminate ();
  108. globals->propagatingExceptions = header;
  109. }
  110. return true;
  111. }
  112. // Do the work for __cxa_end_cleanup. Returns the currently propagating
  113. // exception object.
  114. extern "C" _Unwind_Exception *
  115. __gnu_end_cleanup(void)
  116. {
  117. __cxa_exception *header;
  118. __cxa_eh_globals *globals = __cxa_get_globals();
  119. header = globals->propagatingExceptions;
  120. // Check something hasn't gone horribly wrong.
  121. if (!header)
  122. std::terminate();
  123. if (__is_gxx_exception_class(header->unwindHeader.exception_class))
  124. {
  125. header->propagationCount--;
  126. if (header->propagationCount == 0)
  127. {
  128. // Remove exception from chain.
  129. globals->propagatingExceptions = header->nextPropagatingException;
  130. header->nextPropagatingException = NULL;
  131. }
  132. }
  133. else
  134. globals->propagatingExceptions = NULL;
  135. return &header->unwindHeader;
  136. }
  137. #ifdef __TMS320C6X__
  138. // Assembly wrapper to call __gnu_end_cleanup without clobbering
  139. // function arguments to _Unwind_Resume.
  140. asm (".global __cxa_end_cleanup\n"
  141. " .type __cxa_end_cleanup, \"function\"\n"
  142. "__cxa_end_cleanup:\n"
  143. " stw .d2t2 B9, *B15--[10]\n"
  144. " stw .d2t2 B8, *+B15[9]\n"
  145. " stw .d2t2 B7, *+B15[8]\n"
  146. " stw .d2t2 B6, *+B15[7]\n"
  147. " stw .d2t2 B5, *+B15[6]\n"
  148. " stw .d2t2 B4, *+B15[5]\n"
  149. " stw .d2t1 A9, *+B15[4]\n"
  150. " stw .d2t1 A8, *+B15[3]\n"
  151. " stw .d2t1 A7, *+B15[2]\n"
  152. " stw .d2t1 A6, *+B15[1]\n"
  153. #ifdef _TMS320C6400_PLUS
  154. " callp .s2 (__gnu_end_cleanup), B3\n"
  155. #elif defined(_TMS320C6400)
  156. " call .s2 (__gnu_end_cleanup)\n"
  157. " addkpc .s2 1f, B3, 0\n"
  158. " nop 4\n"
  159. "1:\n"
  160. #else
  161. " call .s2 (__gnu_end_cleanup)\n"
  162. " mvkl .s2 1f, B3\n"
  163. " mvkh .s2 1f, B3\n"
  164. " nop 3\n"
  165. "1:\n"
  166. #endif
  167. " ldw .d2t1 *+B15[1], A6\n"
  168. " ldw .d2t1 *+B15[2], A7\n"
  169. " ldw .d2t1 *+B15[3], A8\n"
  170. " ldw .d2t1 *+B15[4], A9\n"
  171. " ldw .d2t2 *+B15[5], B4\n"
  172. " ldw .d2t2 *+B15[6], B5\n"
  173. " ldw .d2t2 *+B15[7], B6\n"
  174. " ldw .d2t2 *+B15[8], B7\n"
  175. " ldw .d2t2 *+B15[9], B8\n"
  176. " ldw .d2t2 *++B15[10], B9\n"
  177. " b .s2 _Unwind_Resume\n"
  178. " nop 5\n");
  179. #else
  180. // Assembly wrapper to call __gnu_end_cleanup without clobbering r1-r3.
  181. // Also push r4 to preserve stack alignment.
  182. #ifdef __thumb__
  183. asm (" .pushsection .text.__cxa_end_cleanup\n"
  184. " .global __cxa_end_cleanup\n"
  185. " .type __cxa_end_cleanup, \"function\"\n"
  186. " .thumb_func\n"
  187. "__cxa_end_cleanup:\n"
  188. " push\t{r1, r2, r3, r4}\n"
  189. " bl\t__gnu_end_cleanup\n"
  190. " pop\t{r1, r2, r3, r4}\n"
  191. " bl\t_Unwind_Resume @ Never returns\n"
  192. " .popsection\n");
  193. #else
  194. asm (" .pushsection .text.__cxa_end_cleanup\n"
  195. " .global __cxa_end_cleanup\n"
  196. " .type __cxa_end_cleanup, \"function\"\n"
  197. "__cxa_end_cleanup:\n"
  198. " stmfd\tsp!, {r1, r2, r3, r4}\n"
  199. " bl\t__gnu_end_cleanup\n"
  200. " ldmfd\tsp!, {r1, r2, r3, r4}\n"
  201. " bl\t_Unwind_Resume @ Never returns\n"
  202. " .popsection\n");
  203. #endif
  204. #endif
  205. #endif