unwind-c.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /* Supporting functions for C exception handling.
  2. Copyright (C) 2002-2015 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 "unwind.h"
  24. #define NO_SIZE_OF_ENCODED_VALUE
  25. #include "unwind-pe.h"
  26. typedef struct
  27. {
  28. _Unwind_Ptr Start;
  29. _Unwind_Ptr LPStart;
  30. _Unwind_Ptr ttype_base;
  31. const unsigned char *TType;
  32. const unsigned char *action_table;
  33. unsigned char ttype_encoding;
  34. unsigned char call_site_encoding;
  35. } lsda_header_info;
  36. static const unsigned char *
  37. parse_lsda_header (struct _Unwind_Context *context, const unsigned char *p,
  38. lsda_header_info *info)
  39. {
  40. _uleb128_t tmp;
  41. unsigned char lpstart_encoding;
  42. info->Start = (context ? _Unwind_GetRegionStart (context) : 0);
  43. /* Find @LPStart, the base to which landing pad offsets are relative. */
  44. lpstart_encoding = *p++;
  45. if (lpstart_encoding != DW_EH_PE_omit)
  46. p = read_encoded_value (context, lpstart_encoding, p, &info->LPStart);
  47. else
  48. info->LPStart = info->Start;
  49. /* Find @TType, the base of the handler and exception spec type data. */
  50. info->ttype_encoding = *p++;
  51. if (info->ttype_encoding != DW_EH_PE_omit)
  52. {
  53. p = read_uleb128 (p, &tmp);
  54. info->TType = p + tmp;
  55. }
  56. else
  57. info->TType = 0;
  58. /* The encoding and length of the call-site table; the action table
  59. immediately follows. */
  60. info->call_site_encoding = *p++;
  61. p = read_uleb128 (p, &tmp);
  62. info->action_table = p + tmp;
  63. return p;
  64. }
  65. #ifdef __ARM_EABI_UNWINDER__
  66. /* ARM EABI personality routines must also unwind the stack. */
  67. #define CONTINUE_UNWINDING \
  68. do \
  69. { \
  70. if (__gnu_unwind_frame (ue_header, context) != _URC_OK) \
  71. return _URC_FAILURE; \
  72. return _URC_CONTINUE_UNWIND; \
  73. } \
  74. while (0)
  75. #else
  76. #define CONTINUE_UNWINDING return _URC_CONTINUE_UNWIND
  77. #endif
  78. #ifdef __USING_SJLJ_EXCEPTIONS__
  79. #define PERSONALITY_FUNCTION __gcc_personality_sj0
  80. #define __builtin_eh_return_data_regno(x) x
  81. #elif defined(__SEH__)
  82. #define PERSONALITY_FUNCTION __gcc_personality_imp
  83. #else
  84. #define PERSONALITY_FUNCTION __gcc_personality_v0
  85. #endif
  86. #ifdef __ARM_EABI_UNWINDER__
  87. _Unwind_Reason_Code
  88. PERSONALITY_FUNCTION (_Unwind_State, struct _Unwind_Exception *,
  89. struct _Unwind_Context *);
  90. _Unwind_Reason_Code
  91. PERSONALITY_FUNCTION (_Unwind_State state,
  92. struct _Unwind_Exception * ue_header,
  93. struct _Unwind_Context * context)
  94. #else
  95. #if defined (__SEH__) && !defined (__USING_SJLJ_EXCEPTIONS__)
  96. static
  97. #endif
  98. _Unwind_Reason_Code
  99. PERSONALITY_FUNCTION (int, _Unwind_Action, _Unwind_Exception_Class,
  100. struct _Unwind_Exception *, struct _Unwind_Context *);
  101. _Unwind_Reason_Code
  102. PERSONALITY_FUNCTION (int version,
  103. _Unwind_Action actions,
  104. _Unwind_Exception_Class exception_class ATTRIBUTE_UNUSED,
  105. struct _Unwind_Exception *ue_header,
  106. struct _Unwind_Context *context)
  107. #endif
  108. {
  109. lsda_header_info info;
  110. const unsigned char *language_specific_data, *p;
  111. _Unwind_Ptr landing_pad, ip;
  112. int ip_before_insn = 0;
  113. #ifdef __ARM_EABI_UNWINDER__
  114. if ((state & _US_ACTION_MASK) != _US_UNWIND_FRAME_STARTING)
  115. CONTINUE_UNWINDING;
  116. /* The dwarf unwinder assumes the context structure holds things like the
  117. function and LSDA pointers. The ARM implementation caches these in
  118. the exception header (UCB). To avoid rewriting everything we make a
  119. virtual scratch register point at the UCB. */
  120. ip = (_Unwind_Ptr) ue_header;
  121. _Unwind_SetGR (context, UNWIND_POINTER_REG, ip);
  122. #else
  123. if (version != 1)
  124. return _URC_FATAL_PHASE1_ERROR;
  125. /* Currently we only support cleanups for C. */
  126. if ((actions & _UA_CLEANUP_PHASE) == 0)
  127. CONTINUE_UNWINDING;
  128. #endif
  129. language_specific_data = (const unsigned char *)
  130. _Unwind_GetLanguageSpecificData (context);
  131. /* If no LSDA, then there are no handlers or cleanups. */
  132. if (! language_specific_data)
  133. CONTINUE_UNWINDING;
  134. /* Parse the LSDA header. */
  135. p = parse_lsda_header (context, language_specific_data, &info);
  136. #ifdef HAVE_GETIPINFO
  137. ip = _Unwind_GetIPInfo (context, &ip_before_insn);
  138. #else
  139. ip = _Unwind_GetIP (context);
  140. #endif
  141. if (! ip_before_insn)
  142. --ip;
  143. landing_pad = 0;
  144. #ifdef __USING_SJLJ_EXCEPTIONS__
  145. /* The given "IP" is an index into the call-site table, with two
  146. exceptions -- -1 means no-action, and 0 means terminate. But
  147. since we're using uleb128 values, we've not got random access
  148. to the array. */
  149. if ((int) ip <= 0)
  150. return _URC_CONTINUE_UNWIND;
  151. else
  152. {
  153. _uleb128_t cs_lp, cs_action;
  154. do
  155. {
  156. p = read_uleb128 (p, &cs_lp);
  157. p = read_uleb128 (p, &cs_action);
  158. }
  159. while (--ip);
  160. /* Can never have null landing pad for sjlj -- that would have
  161. been indicated by a -1 call site index. */
  162. landing_pad = (_Unwind_Ptr)cs_lp + 1;
  163. goto found_something;
  164. }
  165. #else
  166. /* Search the call-site table for the action associated with this IP. */
  167. while (p < info.action_table)
  168. {
  169. _Unwind_Ptr cs_start, cs_len, cs_lp;
  170. _uleb128_t cs_action;
  171. /* Note that all call-site encodings are "absolute" displacements. */
  172. p = read_encoded_value (0, info.call_site_encoding, p, &cs_start);
  173. p = read_encoded_value (0, info.call_site_encoding, p, &cs_len);
  174. p = read_encoded_value (0, info.call_site_encoding, p, &cs_lp);
  175. p = read_uleb128 (p, &cs_action);
  176. /* The table is sorted, so if we've passed the ip, stop. */
  177. if (ip < info.Start + cs_start)
  178. p = info.action_table;
  179. else if (ip < info.Start + cs_start + cs_len)
  180. {
  181. if (cs_lp)
  182. landing_pad = info.LPStart + cs_lp;
  183. goto found_something;
  184. }
  185. }
  186. #endif
  187. /* IP is not in table. No associated cleanups. */
  188. /* ??? This is where C++ calls std::terminate to catch throw
  189. from a destructor. */
  190. CONTINUE_UNWINDING;
  191. found_something:
  192. if (landing_pad == 0)
  193. {
  194. /* IP is present, but has a null landing pad.
  195. No handler to be run. */
  196. CONTINUE_UNWINDING;
  197. }
  198. _Unwind_SetGR (context, __builtin_eh_return_data_regno (0),
  199. (_Unwind_Ptr) ue_header);
  200. _Unwind_SetGR (context, __builtin_eh_return_data_regno (1), 0);
  201. _Unwind_SetIP (context, landing_pad);
  202. return _URC_INSTALL_CONTEXT;
  203. }
  204. #if defined (__SEH__) && !defined (__USING_SJLJ_EXCEPTIONS__)
  205. EXCEPTION_DISPOSITION
  206. __gcc_personality_seh0 (PEXCEPTION_RECORD ms_exc, void *this_frame,
  207. PCONTEXT ms_orig_context, PDISPATCHER_CONTEXT ms_disp)
  208. {
  209. return _GCC_specific_handler (ms_exc, this_frame, ms_orig_context,
  210. ms_disp, __gcc_personality_imp);
  211. }
  212. #endif /* SEH */