unwind.inc 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. /* Exception handling and frame unwind runtime interface routines. -*- C -*-
  2. Copyright (C) 2001-2022 Free Software Foundation, Inc.
  3. This file is part of GCC.
  4. GCC is free software; you can redistribute it and/or modify it
  5. under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3, or (at your option)
  7. any later version.
  8. GCC is distributed in the hope that it will be useful, but WITHOUT
  9. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  10. or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
  11. License for more details.
  12. Under Section 7 of GPL version 3, you are granted additional
  13. permissions described in the GCC Runtime Library Exception, version
  14. 3.1, as published by the Free Software Foundation.
  15. You should have received a copy of the GNU General Public License and
  16. a copy of the GCC Runtime Library Exception along with this program;
  17. see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  18. <http://www.gnu.org/licenses/>. */
  19. /* This is derived from the C++ ABI for IA-64. Where we diverge
  20. for cross-architecture compatibility are noted with "@@@".
  21. This file is included from unwind-dw2.c, unwind-sjlj.c or
  22. unwind-ia64.c. */
  23. /* Subroutine of _Unwind_RaiseException also invoked from _Unwind_Resume.
  24. Unwind the stack calling the personality routine to find both the
  25. exception handler and intermediary cleanup code. We'll only locate
  26. the first such frame here. Cleanup code will call back into
  27. _Unwind_Resume and we'll continue Phase 2 there. */
  28. static _Unwind_Reason_Code
  29. _Unwind_RaiseException_Phase2(struct _Unwind_Exception *exc,
  30. struct _Unwind_Context *context,
  31. unsigned long *frames_p)
  32. {
  33. _Unwind_Reason_Code code;
  34. unsigned long frames = 1;
  35. while (1)
  36. {
  37. _Unwind_FrameState fs;
  38. int match_handler;
  39. code = uw_frame_state_for (context, &fs);
  40. /* Identify when we've reached the designated handler context. */
  41. match_handler = (uw_identify_context (context) == exc->private_2
  42. ? _UA_HANDLER_FRAME : 0);
  43. if (code != _URC_NO_REASON)
  44. /* Some error encountered. Usually the unwinder doesn't
  45. diagnose these and merely crashes. */
  46. return _URC_FATAL_PHASE2_ERROR;
  47. /* Unwind successful. Run the personality routine, if any. */
  48. if (fs.personality)
  49. {
  50. code = (*fs.personality) (1, _UA_CLEANUP_PHASE | match_handler,
  51. exc->exception_class, exc, context);
  52. if (code == _URC_INSTALL_CONTEXT)
  53. break;
  54. if (code != _URC_CONTINUE_UNWIND)
  55. return _URC_FATAL_PHASE2_ERROR;
  56. }
  57. /* Don't let us unwind past the handler context. */
  58. gcc_assert (!match_handler);
  59. uw_update_context (context, &fs);
  60. _Unwind_Frames_Increment (context, frames);
  61. }
  62. *frames_p = frames;
  63. return code;
  64. }
  65. /* Raise an exception, passing along the given exception object. */
  66. _Unwind_Reason_Code LIBGCC2_UNWIND_ATTRIBUTE
  67. _Unwind_RaiseException(struct _Unwind_Exception *exc)
  68. {
  69. struct _Unwind_Context this_context, cur_context;
  70. _Unwind_Reason_Code code;
  71. unsigned long frames;
  72. /* Set up this_context to describe the current stack frame. */
  73. uw_init_context (&this_context);
  74. cur_context = this_context;
  75. /* Phase 1: Search. Unwind the stack, calling the personality routine
  76. with the _UA_SEARCH_PHASE flag set. Do not modify the stack yet. */
  77. while (1)
  78. {
  79. _Unwind_FrameState fs;
  80. /* Set up fs to describe the FDE for the caller of cur_context. The
  81. first time through the loop, that means __cxa_throw. */
  82. code = uw_frame_state_for (&cur_context, &fs);
  83. if (code == _URC_END_OF_STACK)
  84. /* Hit end of stack with no handler found. */
  85. return _URC_END_OF_STACK;
  86. if (code != _URC_NO_REASON)
  87. /* Some error encountered. Usually the unwinder doesn't
  88. diagnose these and merely crashes. */
  89. return _URC_FATAL_PHASE1_ERROR;
  90. /* Unwind successful. Run the personality routine, if any. */
  91. if (fs.personality)
  92. {
  93. code = (*fs.personality) (1, _UA_SEARCH_PHASE, exc->exception_class,
  94. exc, &cur_context);
  95. if (code == _URC_HANDLER_FOUND)
  96. break;
  97. else if (code != _URC_CONTINUE_UNWIND)
  98. return _URC_FATAL_PHASE1_ERROR;
  99. }
  100. /* Update cur_context to describe the same frame as fs. */
  101. uw_update_context (&cur_context, &fs);
  102. }
  103. /* Indicate to _Unwind_Resume and associated subroutines that this
  104. is not a forced unwind. Further, note where we found a handler. */
  105. exc->private_1 = 0;
  106. exc->private_2 = uw_identify_context (&cur_context);
  107. cur_context = this_context;
  108. code = _Unwind_RaiseException_Phase2 (exc, &cur_context, &frames);
  109. if (code != _URC_INSTALL_CONTEXT)
  110. return code;
  111. uw_install_context (&this_context, &cur_context, frames);
  112. }
  113. /* Subroutine of _Unwind_ForcedUnwind also invoked from _Unwind_Resume. */
  114. static _Unwind_Reason_Code
  115. _Unwind_ForcedUnwind_Phase2 (struct _Unwind_Exception *exc,
  116. struct _Unwind_Context *context,
  117. unsigned long *frames_p)
  118. {
  119. _Unwind_Stop_Fn stop = (_Unwind_Stop_Fn) (_Unwind_Ptr) exc->private_1;
  120. void *stop_argument = (void *) (_Unwind_Ptr) exc->private_2;
  121. _Unwind_Reason_Code code, stop_code;
  122. unsigned long frames = 1;
  123. while (1)
  124. {
  125. _Unwind_FrameState fs;
  126. int action;
  127. /* Set up fs to describe the FDE for the caller of cur_context. */
  128. code = uw_frame_state_for (context, &fs);
  129. if (code != _URC_NO_REASON && code != _URC_END_OF_STACK
  130. && code != _URC_NORMAL_STOP)
  131. return _URC_FATAL_PHASE2_ERROR;
  132. /* Unwind successful. */
  133. action = _UA_FORCE_UNWIND | _UA_CLEANUP_PHASE;
  134. if (code == _URC_END_OF_STACK || code == _URC_NORMAL_STOP)
  135. action |= _UA_END_OF_STACK;
  136. stop_code = (*stop) (1, action, exc->exception_class, exc,
  137. context, stop_argument);
  138. if (stop_code != _URC_NO_REASON)
  139. return _URC_FATAL_PHASE2_ERROR;
  140. /* Stop didn't want to do anything. Invoke the personality
  141. handler, if applicable, to run cleanups. */
  142. if (code == _URC_END_OF_STACK)
  143. break;
  144. if (fs.personality)
  145. {
  146. code = (*fs.personality) (1, _UA_FORCE_UNWIND | _UA_CLEANUP_PHASE,
  147. exc->exception_class, exc, context);
  148. if (code == _URC_INSTALL_CONTEXT)
  149. break;
  150. if (code != _URC_CONTINUE_UNWIND)
  151. return _URC_FATAL_PHASE2_ERROR;
  152. }
  153. /* Update cur_context to describe the same frame as fs, and discard
  154. the previous context if necessary. */
  155. uw_advance_context (context, &fs);
  156. _Unwind_Frames_Increment (context, frames);
  157. }
  158. *frames_p = frames;
  159. return code;
  160. }
  161. /* Raise an exception for forced unwinding. */
  162. _Unwind_Reason_Code LIBGCC2_UNWIND_ATTRIBUTE
  163. _Unwind_ForcedUnwind (struct _Unwind_Exception *exc,
  164. _Unwind_Stop_Fn stop, void * stop_argument)
  165. {
  166. struct _Unwind_Context this_context, cur_context;
  167. _Unwind_Reason_Code code;
  168. unsigned long frames;
  169. uw_init_context (&this_context);
  170. cur_context = this_context;
  171. exc->private_1 = (_Unwind_Ptr) stop;
  172. exc->private_2 = (_Unwind_Ptr) stop_argument;
  173. code = _Unwind_ForcedUnwind_Phase2 (exc, &cur_context, &frames);
  174. if (code != _URC_INSTALL_CONTEXT)
  175. return code;
  176. uw_install_context (&this_context, &cur_context, frames);
  177. }
  178. /* Resume propagation of an existing exception. This is used after
  179. e.g. executing cleanup code, and not to implement rethrowing. */
  180. void LIBGCC2_UNWIND_ATTRIBUTE
  181. _Unwind_Resume (struct _Unwind_Exception *exc)
  182. {
  183. struct _Unwind_Context this_context, cur_context;
  184. _Unwind_Reason_Code code;
  185. unsigned long frames;
  186. uw_init_context (&this_context);
  187. cur_context = this_context;
  188. /* Choose between continuing to process _Unwind_RaiseException
  189. or _Unwind_ForcedUnwind. */
  190. if (exc->private_1 == 0)
  191. code = _Unwind_RaiseException_Phase2 (exc, &cur_context, &frames);
  192. else
  193. code = _Unwind_ForcedUnwind_Phase2 (exc, &cur_context, &frames);
  194. gcc_assert (code == _URC_INSTALL_CONTEXT);
  195. uw_install_context (&this_context, &cur_context, frames);
  196. }
  197. /* Resume propagation of an FORCE_UNWIND exception, or to rethrow
  198. a normal exception that was handled. */
  199. _Unwind_Reason_Code LIBGCC2_UNWIND_ATTRIBUTE
  200. _Unwind_Resume_or_Rethrow (struct _Unwind_Exception *exc)
  201. {
  202. struct _Unwind_Context this_context, cur_context;
  203. _Unwind_Reason_Code code;
  204. unsigned long frames;
  205. /* Choose between continuing to process _Unwind_RaiseException
  206. or _Unwind_ForcedUnwind. */
  207. if (exc->private_1 == 0)
  208. return _Unwind_RaiseException (exc);
  209. uw_init_context (&this_context);
  210. cur_context = this_context;
  211. code = _Unwind_ForcedUnwind_Phase2 (exc, &cur_context, &frames);
  212. gcc_assert (code == _URC_INSTALL_CONTEXT);
  213. uw_install_context (&this_context, &cur_context, frames);
  214. }
  215. /* A convenience function that calls the exception_cleanup field. */
  216. void
  217. _Unwind_DeleteException (struct _Unwind_Exception *exc)
  218. {
  219. if (exc->exception_cleanup)
  220. (*exc->exception_cleanup) (_URC_FOREIGN_EXCEPTION_CAUGHT, exc);
  221. }
  222. /* Perform stack backtrace through unwind data. */
  223. _Unwind_Reason_Code LIBGCC2_UNWIND_ATTRIBUTE
  224. _Unwind_Backtrace(_Unwind_Trace_Fn trace, void * trace_argument)
  225. {
  226. struct _Unwind_Context context;
  227. _Unwind_Reason_Code code;
  228. uw_init_context (&context);
  229. while (1)
  230. {
  231. _Unwind_FrameState fs;
  232. /* Set up fs to describe the FDE for the caller of context. */
  233. code = uw_frame_state_for (&context, &fs);
  234. if (code != _URC_NO_REASON && code != _URC_END_OF_STACK
  235. && code != _URC_NORMAL_STOP)
  236. return _URC_FATAL_PHASE1_ERROR;
  237. /* Call trace function. */
  238. if ((*trace) (&context, trace_argument) != _URC_NO_REASON)
  239. return _URC_FATAL_PHASE1_ERROR;
  240. #ifdef MD_BACKCHAIN_FALLBACK
  241. /* Do a backchain if there is no DWARF data. */
  242. if (code == _URC_NORMAL_STOP)
  243. {
  244. MD_BACKCHAIN_FALLBACK(&context, trace_argument);
  245. break;
  246. }
  247. #endif
  248. /* We're done at end of stack. */
  249. if (code == _URC_END_OF_STACK)
  250. break;
  251. /* Update context to describe the same frame as fs. */
  252. uw_update_context (&context, &fs);
  253. }
  254. return code;
  255. }