ARMWinEH.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. //===-- llvm/Support/WinARMEH.h - Windows on ARM EH Constants ---*- C++ -*-===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #ifndef LLVM_SUPPORT_ARMWINEH_H
  10. #define LLVM_SUPPORT_ARMWINEH_H
  11. #include "llvm/ADT/ArrayRef.h"
  12. #include "llvm/Support/Endian.h"
  13. namespace llvm {
  14. namespace ARM {
  15. namespace WinEH {
  16. enum class RuntimeFunctionFlag {
  17. RFF_Unpacked, /// unpacked entry
  18. RFF_Packed, /// packed entry
  19. RFF_PackedFragment, /// packed entry representing a fragment
  20. RFF_Reserved, /// reserved
  21. };
  22. enum class ReturnType {
  23. RT_POP, /// return via pop {pc} (L flag must be set)
  24. RT_B, /// 16-bit branch
  25. RT_BW, /// 32-bit branch
  26. RT_NoEpilogue, /// no epilogue (fragment)
  27. };
  28. /// RuntimeFunction - An entry in the table of procedure data (.pdata)
  29. ///
  30. /// 3 3 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0
  31. /// 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0
  32. /// +---------------------------------------------------------------+
  33. /// | Function Start RVA |
  34. /// +-------------------+-+-+-+-----+-+---+---------------------+---+
  35. /// | Stack Adjust |C|L|R| Reg |H|Ret| Function Length |Flg|
  36. /// +-------------------+-+-+-+-----+-+---+---------------------+---+
  37. ///
  38. /// Flag : 2-bit field with the following meanings:
  39. /// - 00 = packed unwind data not used; reamining bits point to .xdata record
  40. /// - 01 = packed unwind data
  41. /// - 10 = packed unwind data, function assumed to have no prologue; useful
  42. /// for function fragments that are discontiguous with the start of the
  43. /// function
  44. /// - 11 = reserved
  45. /// Function Length : 11-bit field providing the length of the entire function
  46. /// in bytes, divided by 2; if the function is greater than
  47. /// 4KB, a full .xdata record must be used instead
  48. /// Ret : 2-bit field indicating how the function returns
  49. /// - 00 = return via pop {pc} (the L bit must be set)
  50. /// - 01 = return via 16-bit branch
  51. /// - 10 = return via 32-bit branch
  52. /// - 11 = no epilogue; useful for function fragments that may only contain a
  53. /// prologue but the epilogue is elsewhere
  54. /// H : 1-bit flag indicating whether the function "homes" the integer parameter
  55. /// registers (r0-r3), allocating 16-bytes on the stack
  56. /// Reg : 3-bit field indicating the index of the last saved non-volatile
  57. /// register. If the R bit is set to 0, then only integer registers are
  58. /// saved (r4-rN, where N is 4 + Reg). If the R bit is set to 1, then
  59. /// only floating-point registers are being saved (d8-dN, where N is
  60. /// 8 + Reg). The special case of the R bit being set to 1 and Reg equal
  61. /// to 7 indicates that no registers are saved.
  62. /// R : 1-bit flag indicating whether the non-volatile registers are integer or
  63. /// floating-point. 0 indicates integer, 1 indicates floating-point. The
  64. /// special case of the R-flag being set and Reg being set to 7 indicates
  65. /// that no non-volatile registers are saved.
  66. /// L : 1-bit flag indicating whether the function saves/restores the link
  67. /// register (LR)
  68. /// C : 1-bit flag indicating whether the function includes extra instructions
  69. /// to setup a frame chain for fast walking. If this flag is set, r11 is
  70. /// implicitly added to the list of saved non-volatile integer registers.
  71. /// Stack Adjust : 10-bit field indicating the number of bytes of stack that are
  72. /// allocated for this function. Only values between 0x000 and
  73. /// 0x3f3 can be directly encoded. If the value is 0x3f4 or
  74. /// greater, then the low 4 bits have special meaning as follows:
  75. /// - Bit 0-1
  76. /// indicate the number of words' of adjustment (1-4), minus 1
  77. /// - Bit 2
  78. /// indicates if the prologue combined adjustment into push
  79. /// - Bit 3
  80. /// indicates if the epilogue combined adjustment into pop
  81. ///
  82. /// RESTRICTIONS:
  83. /// - IF C is SET:
  84. /// + L flag must be set since frame chaining requires r11 and lr
  85. /// + r11 must NOT be included in the set of registers described by Reg
  86. /// - IF Ret is 0:
  87. /// + L flag must be set
  88. // NOTE: RuntimeFunction is meant to be a simple class that provides raw access
  89. // to all fields in the structure. The accessor methods reflect the names of
  90. // the bitfields that they correspond to. Although some obvious simplifications
  91. // are possible via merging of methods, it would prevent the use of this class
  92. // to fully inspect the contents of the data structure which is particularly
  93. // useful for scenarios such as llvm-readobj to aid in testing.
  94. class RuntimeFunction {
  95. public:
  96. const support::ulittle32_t BeginAddress;
  97. const support::ulittle32_t UnwindData;
  98. RuntimeFunction(const support::ulittle32_t *Data)
  99. : BeginAddress(Data[0]), UnwindData(Data[1]) {}
  100. RuntimeFunction(const support::ulittle32_t BeginAddress,
  101. const support::ulittle32_t UnwindData)
  102. : BeginAddress(BeginAddress), UnwindData(UnwindData) {}
  103. RuntimeFunctionFlag Flag() const {
  104. return RuntimeFunctionFlag(UnwindData & 0x3);
  105. }
  106. uint32_t ExceptionInformationRVA() const {
  107. assert(Flag() == RuntimeFunctionFlag::RFF_Unpacked &&
  108. "unpacked form required for this operation");
  109. return (UnwindData & ~0x3);
  110. }
  111. uint32_t PackedUnwindData() const {
  112. assert((Flag() == RuntimeFunctionFlag::RFF_Packed ||
  113. Flag() == RuntimeFunctionFlag::RFF_PackedFragment) &&
  114. "packed form required for this operation");
  115. return (UnwindData & ~0x3);
  116. }
  117. uint32_t FunctionLength() const {
  118. assert((Flag() == RuntimeFunctionFlag::RFF_Packed ||
  119. Flag() == RuntimeFunctionFlag::RFF_PackedFragment) &&
  120. "packed form required for this operation");
  121. return (((UnwindData & 0x00001ffc) >> 2) << 1);
  122. }
  123. ReturnType Ret() const {
  124. assert((Flag() == RuntimeFunctionFlag::RFF_Packed ||
  125. Flag() == RuntimeFunctionFlag::RFF_PackedFragment) &&
  126. "packed form required for this operation");
  127. assert(((UnwindData & 0x00006000) || L()) && "L must be set to 1");
  128. return ReturnType((UnwindData & 0x00006000) >> 13);
  129. }
  130. bool H() const {
  131. assert((Flag() == RuntimeFunctionFlag::RFF_Packed ||
  132. Flag() == RuntimeFunctionFlag::RFF_PackedFragment) &&
  133. "packed form required for this operation");
  134. return ((UnwindData & 0x00008000) >> 15);
  135. }
  136. uint8_t Reg() const {
  137. assert((Flag() == RuntimeFunctionFlag::RFF_Packed ||
  138. Flag() == RuntimeFunctionFlag::RFF_PackedFragment) &&
  139. "packed form required for this operation");
  140. return ((UnwindData & 0x00070000) >> 16);
  141. }
  142. bool R() const {
  143. assert((Flag() == RuntimeFunctionFlag::RFF_Packed ||
  144. Flag() == RuntimeFunctionFlag::RFF_PackedFragment) &&
  145. "packed form required for this operation");
  146. return ((UnwindData & 0x00080000) >> 19);
  147. }
  148. bool L() const {
  149. assert((Flag() == RuntimeFunctionFlag::RFF_Packed ||
  150. Flag() == RuntimeFunctionFlag::RFF_PackedFragment) &&
  151. "packed form required for this operation");
  152. return ((UnwindData & 0x00100000) >> 20);
  153. }
  154. bool C() const {
  155. assert((Flag() == RuntimeFunctionFlag::RFF_Packed ||
  156. Flag() == RuntimeFunctionFlag::RFF_PackedFragment) &&
  157. "packed form required for this operation");
  158. assert(((~UnwindData & 0x00200000) || L()) &&
  159. "L flag must be set, chaining requires r11 and LR");
  160. assert(((~UnwindData & 0x00200000) || (Reg() < 7) || R()) &&
  161. "r11 must not be included in Reg; C implies r11");
  162. return ((UnwindData & 0x00200000) >> 21);
  163. }
  164. uint16_t StackAdjust() const {
  165. assert((Flag() == RuntimeFunctionFlag::RFF_Packed ||
  166. Flag() == RuntimeFunctionFlag::RFF_PackedFragment) &&
  167. "packed form required for this operation");
  168. return ((UnwindData & 0xffc00000) >> 22);
  169. }
  170. };
  171. /// PrologueFolding - pseudo-flag derived from Stack Adjust indicating that the
  172. /// prologue has stack adjustment combined into the push
  173. inline bool PrologueFolding(const RuntimeFunction &RF) {
  174. return RF.StackAdjust() >= 0x3f4 && (RF.StackAdjust() & 0x4);
  175. }
  176. /// Epilogue - pseudo-flag derived from Stack Adjust indicating that the
  177. /// epilogue has stack adjustment combined into the pop
  178. inline bool EpilogueFolding(const RuntimeFunction &RF) {
  179. return RF.StackAdjust() >= 0x3f4 && (RF.StackAdjust() & 0x8);
  180. }
  181. /// StackAdjustment - calculated stack adjustment in words. The stack
  182. /// adjustment should be determined via this function to account for the special
  183. /// handling the special encoding when the value is >= 0x3f4.
  184. inline uint16_t StackAdjustment(const RuntimeFunction &RF) {
  185. uint16_t Adjustment = RF.StackAdjust();
  186. if (Adjustment >= 0x3f4)
  187. return (Adjustment & 0x3) ? ((Adjustment & 0x3) << 2) - 1 : 0;
  188. return Adjustment;
  189. }
  190. /// SavedRegisterMask - Utility function to calculate the set of saved general
  191. /// purpose (r0-r15) and VFP (d0-d31) registers.
  192. std::pair<uint16_t, uint32_t> SavedRegisterMask(const RuntimeFunction &RF);
  193. /// ExceptionDataRecord - An entry in the table of exception data (.xdata)
  194. ///
  195. /// 3 3 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0
  196. /// 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0
  197. /// +-------+---------+-+-+-+---+-----------------------------------+
  198. /// | C Wrd | Epi Cnt |F|E|X|Ver| Function Length |
  199. /// +-------+--------+'-'-'-'---'---+-------------------------------+
  200. /// | Reserved |Ex. Code Words| (Extended Epilogue Count) |
  201. /// +-------+--------+--------------+-------------------------------+
  202. ///
  203. /// Function Length : 18-bit field indicating the total length of the function
  204. /// in bytes divided by 2. If a function is larger than
  205. /// 512KB, then multiple pdata and xdata records must be used.
  206. /// Vers : 2-bit field describing the version of the remaining structure. Only
  207. /// version 0 is currently defined (values 1-3 are not permitted).
  208. /// X : 1-bit field indicating the presence of exception data
  209. /// E : 1-bit field indicating that the single epilogue is packed into the
  210. /// header
  211. /// F : 1-bit field indicating that the record describes a function fragment
  212. /// (implies that no prologue is present, and prologue processing should be
  213. /// skipped)
  214. /// Epilogue Count : 5-bit field that differs in meaning based on the E field.
  215. ///
  216. /// If E is set, then this field specifies the index of the
  217. /// first unwind code describing the (only) epilogue.
  218. ///
  219. /// Otherwise, this field indicates the number of exception
  220. /// scopes. If more than 31 scopes exist, then this field and
  221. /// the Code Words field must both be set to 0 to indicate that
  222. /// an extension word is required.
  223. /// Code Words : 4-bit field that species the number of 32-bit words needed to
  224. /// contain all the unwind codes. If more than 15 words (63 code
  225. /// bytes) are required, then this field and the Epilogue Count
  226. /// field must both be set to 0 to indicate that an extension word
  227. /// is required.
  228. /// Extended Epilogue Count, Extended Code Words :
  229. /// Valid only if Epilog Count and Code Words are both
  230. /// set to 0. Provides an 8-bit extended code word
  231. /// count and 16-bits for epilogue count
  232. ///
  233. /// 3 3 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0
  234. /// 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0
  235. /// +----------------+------+---+---+-------------------------------+
  236. /// | Ep Start Idx | Cond |Res| Epilogue Start Offset |
  237. /// +----------------+------+---+-----------------------------------+
  238. ///
  239. /// If the E bit is unset in the header, the header is followed by a series of
  240. /// epilogue scopes, which are sorted by their offset.
  241. ///
  242. /// Epilogue Start Offset: 18-bit field encoding the offset of epilogue relative
  243. /// to the start of the function in bytes divided by two
  244. /// Res : 2-bit field reserved for future expansion (must be set to 0)
  245. /// Condition : 4-bit field providing the condition under which the epilogue is
  246. /// executed. Unconditional epilogues should set this field to 0xe.
  247. /// Epilogues must be entirely conditional or unconditional, and in
  248. /// Thumb-2 mode. The epilogue beings with the first instruction
  249. /// after the IT opcode.
  250. /// Epilogue Start Index : 8-bit field indicating the byte index of the first
  251. /// unwind code describing the epilogue
  252. ///
  253. /// 3 3 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0
  254. /// 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0
  255. /// +---------------+---------------+---------------+---------------+
  256. /// | Unwind Code 3 | Unwind Code 2 | Unwind Code 1 | Unwind Code 0 |
  257. /// +---------------+---------------+---------------+---------------+
  258. ///
  259. /// Following the epilogue scopes, the byte code describing the unwinding
  260. /// follows. This is padded to align up to word alignment. Bytes are stored in
  261. /// little endian.
  262. ///
  263. /// 3 3 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0
  264. /// 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0
  265. /// +---------------------------------------------------------------+
  266. /// | Exception Handler RVA (requires X = 1) |
  267. /// +---------------------------------------------------------------+
  268. /// | (possibly followed by data required for exception handler) |
  269. /// +---------------------------------------------------------------+
  270. ///
  271. /// If the X bit is set in the header, the unwind byte code is followed by the
  272. /// exception handler information. This constants of one Exception Handler RVA
  273. /// which is the address to the exception handler, followed immediately by the
  274. /// variable length data associated with the exception handler.
  275. ///
  276. struct EpilogueScope {
  277. const support::ulittle32_t ES;
  278. EpilogueScope(const support::ulittle32_t Data) : ES(Data) {}
  279. uint32_t EpilogueStartOffset() const {
  280. return (ES & 0x0003ffff);
  281. }
  282. uint8_t Res() const {
  283. return ((ES & 0x000c0000) >> 18);
  284. }
  285. uint8_t Condition() const {
  286. return ((ES & 0x00f00000) >> 20);
  287. }
  288. uint8_t EpilogueStartIndex() const {
  289. return ((ES & 0xff000000) >> 24);
  290. }
  291. };
  292. struct ExceptionDataRecord;
  293. inline size_t HeaderWords(const ExceptionDataRecord &XR);
  294. struct ExceptionDataRecord {
  295. const support::ulittle32_t *Data;
  296. ExceptionDataRecord(const support::ulittle32_t *Data) : Data(Data) {}
  297. uint32_t FunctionLength() const {
  298. return (Data[0] & 0x0003ffff);
  299. }
  300. uint8_t Vers() const {
  301. return (Data[0] & 0x000C0000) >> 18;
  302. }
  303. bool X() const {
  304. return ((Data[0] & 0x00100000) >> 20);
  305. }
  306. bool E() const {
  307. return ((Data[0] & 0x00200000) >> 21);
  308. }
  309. bool F() const {
  310. return ((Data[0] & 0x00400000) >> 22);
  311. }
  312. uint8_t EpilogueCount() const {
  313. if (HeaderWords(*this) == 1)
  314. return (Data[0] & 0x0f800000) >> 23;
  315. return Data[1] & 0x0000ffff;
  316. }
  317. uint8_t CodeWords() const {
  318. if (HeaderWords(*this) == 1)
  319. return (Data[0] & 0xf0000000) >> 28;
  320. return (Data[1] & 0x00ff0000) >> 16;
  321. }
  322. ArrayRef<support::ulittle32_t> EpilogueScopes() const {
  323. assert(E() == 0 && "epilogue scopes are only present when the E bit is 0");
  324. size_t Offset = HeaderWords(*this);
  325. return makeArrayRef(&Data[Offset], EpilogueCount());
  326. }
  327. ArrayRef<uint8_t> UnwindByteCode() const {
  328. const size_t Offset = HeaderWords(*this)
  329. + (E() ? 0 : EpilogueCount());
  330. const uint8_t *ByteCode =
  331. reinterpret_cast<const uint8_t *>(&Data[Offset]);
  332. return makeArrayRef(ByteCode, CodeWords() * sizeof(uint32_t));
  333. }
  334. uint32_t ExceptionHandlerRVA() const {
  335. assert(X() && "Exception Handler RVA is only valid if the X bit is set");
  336. return Data[HeaderWords(*this) + EpilogueCount() + CodeWords()];
  337. }
  338. uint32_t ExceptionHandlerParameter() const {
  339. assert(X() && "Exception Handler RVA is only valid if the X bit is set");
  340. return Data[HeaderWords(*this) + EpilogueCount() + CodeWords() + 1];
  341. }
  342. };
  343. inline size_t HeaderWords(const ExceptionDataRecord &XR) {
  344. return (XR.Data[0] & 0xff800000) ? 1 : 2;
  345. }
  346. }
  347. }
  348. }
  349. #endif