unwind-c.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /* Supporting functions for C exception handling.
  2. Copyright (C) 2002-2022 Free Software Foundation, Inc.
  3. Contributed by Aldy Hernandez <aldy@quesejoda.com>.
  4. Shamelessly stolen from the Java front end.
  5. This file is part of GCC.
  6. GCC is free software; you can redistribute it and/or modify it under
  7. the terms of the GNU General Public License as published by the Free
  8. Software Foundation; either version 3, or (at your option) any later
  9. version.
  10. GCC is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  13. for more details.
  14. Under Section 7 of GPL version 3, you are granted additional
  15. permissions described in the GCC Runtime Library Exception, version
  16. 3.1, as published by the Free Software Foundation.
  17. You should have received a copy of the GNU General Public License and
  18. a copy of the GCC Runtime Library Exception along with this program;
  19. see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  20. <http://www.gnu.org/licenses/>. */
  21. #include "tconfig.h"
  22. #include "tsystem.h"
  23. #include "auto-target.h"
  24. #include "unwind.h"
  25. #define NO_SIZE_OF_ENCODED_VALUE
  26. #include "unwind-pe.h"
  27. typedef struct
  28. {
  29. _Unwind_Ptr Start;
  30. _Unwind_Ptr LPStart;
  31. _Unwind_Ptr ttype_base;
  32. const unsigned char *TType;
  33. const unsigned char *action_table;
  34. unsigned char ttype_encoding;
  35. unsigned char call_site_encoding;
  36. } lsda_header_info;
  37. static const unsigned char *
  38. parse_lsda_header (struct _Unwind_Context *context, const unsigned char *p,
  39. lsda_header_info *info)
  40. {
  41. _uleb128_t tmp;
  42. unsigned char lpstart_encoding;
  43. info->Start = (context ? _Unwind_GetRegionStart (context) : 0);
  44. /* Find @LPStart, the base to which landing pad offsets are relative. */
  45. lpstart_encoding = *p++;
  46. if (lpstart_encoding != DW_EH_PE_omit)
  47. p = read_encoded_value (context, lpstart_encoding, p, &info->LPStart);
  48. else
  49. info->LPStart = info->Start;
  50. /* Find @TType, the base of the handler and exception spec type data. */
  51. info->ttype_encoding = *p++;
  52. if (info->ttype_encoding != DW_EH_PE_omit)
  53. {
  54. p = read_uleb128 (p, &tmp);
  55. info->TType = p + tmp;
  56. }
  57. else
  58. info->TType = 0;
  59. /* The encoding and length of the call-site table; the action table
  60. immediately follows. */
  61. info->call_site_encoding = *p++;
  62. p = read_uleb128 (p, &tmp);
  63. info->action_table = p + tmp;
  64. return p;
  65. }
  66. #ifdef __ARM_EABI_UNWINDER__
  67. /* ARM EABI personality routines must also unwind the stack. */
  68. #define CONTINUE_UNWINDING \
  69. do \
  70. { \
  71. if (__gnu_unwind_frame (ue_header, context) != _URC_OK) \
  72. return _URC_FAILURE; \
  73. return _URC_CONTINUE_UNWIND; \
  74. } \
  75. while (0)
  76. #else
  77. #define CONTINUE_UNWINDING return _URC_CONTINUE_UNWIND
  78. #endif
  79. #ifdef __USING_SJLJ_EXCEPTIONS__
  80. #define PERSONALITY_FUNCTION __gcc_personality_sj0
  81. #define __builtin_eh_return_data_regno(x) x
  82. #elif defined(__SEH__)
  83. #define PERSONALITY_FUNCTION __gcc_personality_imp
  84. #else
  85. #define PERSONALITY_FUNCTION __gcc_personality_v0
  86. #endif
  87. #ifdef __ARM_EABI_UNWINDER__
  88. _Unwind_Reason_Code
  89. PERSONALITY_FUNCTION (_Unwind_State, struct _Unwind_Exception *,
  90. struct _Unwind_Context *);
  91. _Unwind_Reason_Code
  92. __attribute__((target ("general-regs-only")))
  93. PERSONALITY_FUNCTION (_Unwind_State state,
  94. struct _Unwind_Exception * ue_header,
  95. struct _Unwind_Context * context)
  96. #else
  97. #if defined (__SEH__) && !defined (__USING_SJLJ_EXCEPTIONS__)
  98. static
  99. #endif
  100. _Unwind_Reason_Code
  101. PERSONALITY_FUNCTION (int, _Unwind_Action, _Unwind_Exception_Class,
  102. struct _Unwind_Exception *, struct _Unwind_Context *);
  103. _Unwind_Reason_Code
  104. PERSONALITY_FUNCTION (int version,
  105. _Unwind_Action actions,
  106. _Unwind_Exception_Class exception_class ATTRIBUTE_UNUSED,
  107. struct _Unwind_Exception *ue_header,
  108. struct _Unwind_Context *context)
  109. #endif
  110. {
  111. lsda_header_info info;
  112. const unsigned char *language_specific_data, *p;
  113. _Unwind_Ptr landing_pad, ip;
  114. int ip_before_insn = 0;
  115. #ifdef __ARM_EABI_UNWINDER__
  116. if ((state & _US_ACTION_MASK) != _US_UNWIND_FRAME_STARTING)
  117. CONTINUE_UNWINDING;
  118. /* The dwarf unwinder assumes the context structure holds things like the
  119. function and LSDA pointers. The ARM implementation caches these in
  120. the exception header (UCB). To avoid rewriting everything we make a
  121. virtual scratch register point at the UCB. */
  122. ip = (_Unwind_Ptr) ue_header;
  123. _Unwind_SetGR (context, UNWIND_POINTER_REG, ip);
  124. #else
  125. if (version != 1)
  126. return _URC_FATAL_PHASE1_ERROR;
  127. /* Currently we only support cleanups for C. */
  128. if ((actions & _UA_CLEANUP_PHASE) == 0)
  129. CONTINUE_UNWINDING;
  130. #endif
  131. language_specific_data = (const unsigned char *)
  132. _Unwind_GetLanguageSpecificData (context);
  133. /* If no LSDA, then there are no handlers or cleanups. */
  134. if (! language_specific_data)
  135. CONTINUE_UNWINDING;
  136. /* Parse the LSDA header. */
  137. p = parse_lsda_header (context, language_specific_data, &info);
  138. #ifdef HAVE_GETIPINFO
  139. ip = _Unwind_GetIPInfo (context, &ip_before_insn);
  140. #else
  141. ip = _Unwind_GetIP (context);
  142. #endif
  143. if (! ip_before_insn)
  144. --ip;
  145. landing_pad = 0;
  146. #ifdef __USING_SJLJ_EXCEPTIONS__
  147. /* The given "IP" is an index into the call-site table, with two
  148. exceptions -- -1 means no-action, and 0 means terminate. But
  149. since we're using uleb128 values, we've not got random access
  150. to the array. */
  151. if ((int) ip <= 0)
  152. return _URC_CONTINUE_UNWIND;
  153. else
  154. {
  155. _uleb128_t cs_lp, cs_action;
  156. do
  157. {
  158. p = read_uleb128 (p, &cs_lp);
  159. p = read_uleb128 (p, &cs_action);
  160. }
  161. while (--ip);
  162. /* Can never have null landing pad for sjlj -- that would have
  163. been indicated by a -1 call site index. */
  164. landing_pad = (_Unwind_Ptr)cs_lp + 1;
  165. goto found_something;
  166. }
  167. #else
  168. /* Search the call-site table for the action associated with this IP. */
  169. while (p < info.action_table)
  170. {
  171. _Unwind_Ptr cs_start, cs_len, cs_lp;
  172. _uleb128_t cs_action;
  173. /* Note that all call-site encodings are "absolute" displacements. */
  174. p = read_encoded_value (0, info.call_site_encoding, p, &cs_start);
  175. p = read_encoded_value (0, info.call_site_encoding, p, &cs_len);
  176. p = read_encoded_value (0, info.call_site_encoding, p, &cs_lp);
  177. p = read_uleb128 (p, &cs_action);
  178. /* The table is sorted, so if we've passed the ip, stop. */
  179. if (ip < info.Start + cs_start)
  180. p = info.action_table;
  181. else if (ip < info.Start + cs_start + cs_len)
  182. {
  183. if (cs_lp)
  184. landing_pad = info.LPStart + cs_lp;
  185. goto found_something;
  186. }
  187. }
  188. #endif
  189. /* IP is not in table. No associated cleanups. */
  190. /* ??? This is where C++ calls std::terminate to catch throw
  191. from a destructor. */
  192. CONTINUE_UNWINDING;
  193. found_something:
  194. if (landing_pad == 0)
  195. {
  196. /* IP is present, but has a null landing pad.
  197. No handler to be run. */
  198. CONTINUE_UNWINDING;
  199. }
  200. _Unwind_SetGR (context, __builtin_eh_return_data_regno (0),
  201. (_Unwind_Ptr) ue_header);
  202. _Unwind_SetGR (context, __builtin_eh_return_data_regno (1), 0);
  203. _Unwind_SetIP (context, landing_pad);
  204. return _URC_INSTALL_CONTEXT;
  205. }
  206. #if defined (__SEH__) && !defined (__USING_SJLJ_EXCEPTIONS__)
  207. EXCEPTION_DISPOSITION
  208. __gcc_personality_seh0 (PEXCEPTION_RECORD ms_exc, void *this_frame,
  209. PCONTEXT ms_orig_context, PDISPATCHER_CONTEXT ms_disp)
  210. {
  211. return _GCC_specific_handler (ms_exc, this_frame, ms_orig_context,
  212. ms_disp, __gcc_personality_imp);
  213. }
  214. #endif /* SEH */