unwind-arm.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /*
  2. * Copyright 2012 David Chisnall. All rights reserved.
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to
  6. * deal in the Software without restriction, including without limitation the
  7. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. * sell copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be
  12. * included in all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  16. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  18. * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  19. * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  20. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  21. */
  22. /**
  23. * ARM-specific unwind definitions. These are taken from the ARM EHABI
  24. * specification.
  25. */
  26. typedef enum
  27. {
  28. _URC_OK = 0, /* operation completed successfully */
  29. _URC_FOREIGN_EXCEPTION_CAUGHT = 1,
  30. _URC_END_OF_STACK = 5,
  31. _URC_HANDLER_FOUND = 6,
  32. _URC_INSTALL_CONTEXT = 7,
  33. _URC_CONTINUE_UNWIND = 8,
  34. _URC_FAILURE = 9, /* unspecified failure of some kind */
  35. _URC_FATAL_PHASE1_ERROR = _URC_FAILURE
  36. } _Unwind_Reason_Code;
  37. typedef uint32_t _Unwind_State;
  38. #ifdef __clang__
  39. static const _Unwind_State _US_VIRTUAL_UNWIND_FRAME = 0;
  40. static const _Unwind_State _US_UNWIND_FRAME_STARTING = 1;
  41. static const _Unwind_State _US_UNWIND_FRAME_RESUME = 2;
  42. #else // GCC fails at knowing what a constant expression is
  43. # define _US_VIRTUAL_UNWIND_FRAME 0
  44. # define _US_UNWIND_FRAME_STARTING 1
  45. # define _US_UNWIND_FRAME_RESUME 2
  46. #endif
  47. typedef struct _Unwind_Context _Unwind_Context;
  48. typedef uint32_t _Unwind_EHT_Header;
  49. struct _Unwind_Exception
  50. {
  51. uint64_t exception_class;
  52. void (*exception_cleanup)(_Unwind_Reason_Code, struct _Unwind_Exception *);
  53. /* Unwinder cache, private fields for the unwinder's use */
  54. struct
  55. {
  56. uint32_t reserved1;
  57. uint32_t reserved2;
  58. uint32_t reserved3;
  59. uint32_t reserved4;
  60. uint32_t reserved5;
  61. /* init reserved1 to 0, then don't touch */
  62. } unwinder_cache;
  63. /* Propagation barrier cache (valid after phase 1): */
  64. struct
  65. {
  66. uint32_t sp;
  67. uint32_t bitpattern[5];
  68. } barrier_cache;
  69. /* Cleanup cache (preserved over cleanup): */
  70. struct
  71. {
  72. uint32_t bitpattern[4];
  73. } cleanup_cache;
  74. /* Pr cache (for pr's benefit): */
  75. struct
  76. {
  77. /** function start address */
  78. uint32_t fnstart;
  79. /** pointer to EHT entry header word */
  80. _Unwind_EHT_Header *ehtp;
  81. /** additional data */
  82. uint32_t additional;
  83. uint32_t reserved1;
  84. } pr_cache;
  85. /** Force alignment of next item to 8-byte boundary */
  86. long long int :0;
  87. };
  88. /* Unwinding functions */
  89. _Unwind_Reason_Code _Unwind_RaiseException(struct _Unwind_Exception *ucbp);
  90. void _Unwind_Resume(struct _Unwind_Exception *ucbp);
  91. void _Unwind_Complete(struct _Unwind_Exception *ucbp);
  92. void _Unwind_DeleteException(struct _Unwind_Exception *ucbp);
  93. void *_Unwind_GetLanguageSpecificData(struct _Unwind_Context*);
  94. typedef enum
  95. {
  96. _UVRSR_OK = 0,
  97. _UVRSR_NOT_IMPLEMENTED = 1,
  98. _UVRSR_FAILED = 2
  99. } _Unwind_VRS_Result;
  100. typedef enum
  101. {
  102. _UVRSC_CORE = 0,
  103. _UVRSC_VFP = 1,
  104. _UVRSC_WMMXD = 3,
  105. _UVRSC_WMMXC = 4
  106. } _Unwind_VRS_RegClass;
  107. typedef enum
  108. {
  109. _UVRSD_UINT32 = 0,
  110. _UVRSD_VFPX = 1,
  111. _UVRSD_UINT64 = 3,
  112. _UVRSD_FLOAT = 4,
  113. _UVRSD_DOUBLE = 5
  114. } _Unwind_VRS_DataRepresentation;
  115. _Unwind_VRS_Result _Unwind_VRS_Get(_Unwind_Context *context,
  116. _Unwind_VRS_RegClass regclass,
  117. uint32_t regno,
  118. _Unwind_VRS_DataRepresentation representation,
  119. void *valuep);
  120. _Unwind_VRS_Result _Unwind_VRS_Set(_Unwind_Context *context,
  121. _Unwind_VRS_RegClass regclass,
  122. uint32_t regno,
  123. _Unwind_VRS_DataRepresentation representation,
  124. void *valuep);
  125. /* Return the base-address for data references. */
  126. extern unsigned long _Unwind_GetDataRelBase(struct _Unwind_Context *);
  127. /* Return the base-address for text references. */
  128. extern unsigned long _Unwind_GetTextRelBase(struct _Unwind_Context *);
  129. extern unsigned long _Unwind_GetRegionStart(struct _Unwind_Context *);
  130. typedef _Unwind_Reason_Code (*_Unwind_Trace_Fn) (struct _Unwind_Context *,
  131. void *);
  132. extern _Unwind_Reason_Code _Unwind_Backtrace (_Unwind_Trace_Fn, void *);
  133. extern _Unwind_Reason_Code
  134. _Unwind_Resume_or_Rethrow (struct _Unwind_Exception *);
  135. /**
  136. * The next set of functions are compatibility extensions, implementing Itanium
  137. * ABI functions on top of ARM ones.
  138. */
  139. #define _UA_SEARCH_PHASE 1
  140. #define _UA_CLEANUP_PHASE 2
  141. #define _UA_HANDLER_FRAME 4
  142. #define _UA_FORCE_UNWIND 8
  143. static inline unsigned long _Unwind_GetGR(struct _Unwind_Context *context, int reg)
  144. {
  145. unsigned long val;
  146. _Unwind_VRS_Get(context, _UVRSC_CORE, reg, _UVRSD_UINT32, &val);
  147. return val;
  148. }
  149. static inline void _Unwind_SetGR(struct _Unwind_Context *context, int reg, unsigned long val)
  150. {
  151. _Unwind_VRS_Set(context, _UVRSC_CORE, reg, _UVRSD_UINT32, &val);
  152. }
  153. static inline unsigned long _Unwind_GetIP(_Unwind_Context *context)
  154. {
  155. // Low bit store the thumb state - discard it
  156. return _Unwind_GetGR(context, 15) & ~1;
  157. }
  158. static inline void _Unwind_SetIP(_Unwind_Context *context, unsigned long val)
  159. {
  160. // The lowest bit of the instruction pointer indicates whether we're in
  161. // thumb or ARM mode. This is assumed to be fixed throughout a function,
  162. // so must be propagated when setting the program counter.
  163. unsigned long thumbState = _Unwind_GetGR(context, 15) & 1;
  164. _Unwind_SetGR(context, 15, (val | thumbState));
  165. }
  166. /** GNU API function that unwinds the frame */
  167. _Unwind_Reason_Code __gnu_unwind_frame(struct _Unwind_Exception*, struct _Unwind_Context*);
  168. #define DECLARE_PERSONALITY_FUNCTION(name) \
  169. _Unwind_Reason_Code name(_Unwind_State state,\
  170. struct _Unwind_Exception *exceptionObject,\
  171. struct _Unwind_Context *context);
  172. #define BEGIN_PERSONALITY_FUNCTION(name) \
  173. _Unwind_Reason_Code name(_Unwind_State state,\
  174. struct _Unwind_Exception *exceptionObject,\
  175. struct _Unwind_Context *context)\
  176. {\
  177. int version = 1;\
  178. uint64_t exceptionClass = exceptionObject->exception_class;\
  179. int actions;\
  180. switch (state)\
  181. {\
  182. default: return _URC_FAILURE;\
  183. case _US_VIRTUAL_UNWIND_FRAME:\
  184. {\
  185. actions = _UA_SEARCH_PHASE;\
  186. break;\
  187. }\
  188. case _US_UNWIND_FRAME_STARTING:\
  189. {\
  190. actions = _UA_CLEANUP_PHASE;\
  191. if (exceptionObject->barrier_cache.sp == _Unwind_GetGR(context, 13))\
  192. {\
  193. actions |= _UA_HANDLER_FRAME;\
  194. }\
  195. break;\
  196. }\
  197. case _US_UNWIND_FRAME_RESUME:\
  198. {\
  199. return continueUnwinding(exceptionObject, context);\
  200. break;\
  201. }\
  202. }\
  203. _Unwind_SetGR (context, 12, (unsigned long)exceptionObject);\
  204. #define CALL_PERSONALITY_FUNCTION(name) name(state,exceptionObject,context)