UnwindInfoManager.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. //-------------------------------------------------------------------------------------------------------
  2. // Copyright (C) Microsoft. All rights reserved.
  3. // Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
  4. //-------------------------------------------------------------------------------------------------------
  5. #pragma once
  6. enum UnwindCode;
  7. class UnwindInfoManager
  8. {
  9. public:
  10. UnwindInfoManager() :
  11. func(NULL),
  12. fragmentStart(NULL),
  13. fragmentLength(0),
  14. epilogEndOffset(0),
  15. prologOffset(0),
  16. savedRegMask(0),
  17. savedDoubleRegMask(0),
  18. homedParamCount(0),
  19. stackDepth(0),
  20. hasCalls(false),
  21. prologLabelId(0),
  22. epilogEndLabelId(0),
  23. pdataArray(NULL),
  24. xdataArray(NULL)
  25. {
  26. }
  27. void Init(Func * func) { this->func = func; }
  28. void EmitUnwindInfo(PBYTE funcStart, DWORD size, CustomHeap::Allocation* allocation) { __debugbreak(); }
  29. DWORD EmitLongUnwindInfoChunk(DWORD remainingLength) { __debugbreak(); }
  30. void SetFunc(Func *func)
  31. {
  32. Assert(this->func == NULL);
  33. this->func = func;
  34. }
  35. Func * GetFunc() const
  36. {
  37. return this->func;
  38. }
  39. void SetFragmentStart(PBYTE pStart)
  40. {
  41. this->fragmentStart = pStart;
  42. }
  43. PBYTE GetFragmentStart() const
  44. {
  45. return this->fragmentStart;
  46. }
  47. void SetEpilogEndOffset(DWORD offset)
  48. {
  49. Assert(this->epilogEndOffset == 0);
  50. this->epilogEndOffset = offset;
  51. }
  52. DWORD GetEpilogEndOffset() const
  53. {
  54. return this->epilogEndOffset;
  55. }
  56. void SetPrologOffset(DWORD offset)
  57. {
  58. Assert(this->prologOffset == 0);
  59. this->prologOffset = offset;
  60. }
  61. DWORD GetPrologOffset() const
  62. {
  63. return this->prologOffset;
  64. }
  65. void SetFragmentLength(DWORD length)
  66. {
  67. this->fragmentLength = length;
  68. }
  69. DWORD GetFragmentLength() const
  70. {
  71. return this->fragmentLength;
  72. }
  73. void SetHomedParamCount(BYTE count)
  74. {
  75. Assert(this->homedParamCount == 0);
  76. this->homedParamCount = count;
  77. }
  78. DWORD GetHomedParamCount() const
  79. {
  80. return this->homedParamCount;
  81. }
  82. void SetStackDepth(DWORD depth)
  83. {
  84. Assert(this->stackDepth == 0);
  85. this->stackDepth = depth;
  86. }
  87. DWORD GetStackDepth() const
  88. {
  89. return this->stackDepth;
  90. }
  91. void SetHasCalls(bool has)
  92. {
  93. this->hasCalls = has;
  94. }
  95. bool GetHasCalls() const
  96. {
  97. return this->hasCalls;
  98. }
  99. void SetPrologStartLabel(DWORD id)
  100. {
  101. Assert(this->prologLabelId == 0);
  102. this->prologLabelId = id;
  103. }
  104. DWORD GetPrologStartLabel() const
  105. {
  106. return this->prologLabelId;
  107. }
  108. void SetEpilogEndLabel(DWORD id)
  109. {
  110. Assert(this->epilogEndLabelId == 0);
  111. this->epilogEndLabelId = id;
  112. }
  113. DWORD GetEpilogEndLabel() const
  114. {
  115. return this->epilogEndLabelId;
  116. }
  117. bool GetHasChkStk() const { __debugbreak(); return 0; }
  118. DWORD GetPDataCount(DWORD length) { __debugbreak(); return 0; }
  119. void SetSavedReg(BYTE reg) { __debugbreak(); }
  120. DWORD ClearSavedReg(DWORD mask, BYTE reg) const { __debugbreak(); return 0; }
  121. void SetDoubleSavedRegList(DWORD doubleRegMask) { __debugbreak(); }
  122. DWORD GetDoubleSavedRegList() const { __debugbreak(); return 0; }
  123. static BYTE GetLastSavedReg(DWORD mask) { __debugbreak(); return 0; }
  124. static BYTE GetFirstSavedReg(DWORD mask) { __debugbreak(); return 0; }
  125. private:
  126. Func * func;
  127. PBYTE fragmentStart;
  128. RUNTIME_FUNCTION* pdataArray;
  129. BYTE* xdataArray;
  130. DWORD fragmentLength;
  131. DWORD prologOffset;
  132. DWORD prologLabelId;
  133. DWORD epilogEndLabelId;
  134. DWORD epilogEndOffset;
  135. DWORD savedRegMask;
  136. DWORD savedDoubleRegMask;
  137. DWORD stackDepth;
  138. BYTE homedParamCount;
  139. bool hasCalls;
  140. bool fragmentHasProlog;
  141. bool fragmentHasEpilog;
  142. void EmitPdata();
  143. bool CanEmitPackedPdata() const;
  144. void EncodePackedUnwindData();
  145. void EncodeExpandedUnwindData();
  146. BYTE * GetBaseAddress();
  147. bool IsPdataPacked(const DWORD *pdata) const;
  148. bool IsR4SavedRegRange(bool saveR11) const;
  149. static bool IsR4SavedRegRange(DWORD saveRegMask);
  150. DWORD XdataTemplate(UnwindCode op) const;
  151. DWORD XdataLength(UnwindCode op) const;
  152. DWORD EmitXdataStackAlloc(BYTE xData[], DWORD byte, DWORD stack);
  153. DWORD EmitXdataHomeParams(BYTE xData[], DWORD byte);
  154. DWORD EmitXdataRestoreRegs(BYTE xData[], DWORD byte, DWORD savedRegMask, bool restoreLR);
  155. DWORD EmitXdataRestoreDoubleRegs(BYTE xData[], DWORD byte, DWORD savedDoubleRegMask);
  156. DWORD EmitXdataIndirReturn(BYTE xData[], DWORD byte);
  157. DWORD EmitXdataNop32(BYTE xData[], DWORD byte);
  158. DWORD EmitXdataNop16(BYTE xData[], DWORD byte);
  159. DWORD EmitXdataEnd(BYTE xData[], DWORD byte);
  160. DWORD EmitXdataEndPlus16(BYTE xData[], DWORD byte);
  161. DWORD EmitXdataLocalsPointer(BYTE xData[], DWORD byte, BYTE regEncode);
  162. DWORD RelativeRegEncoding(RegNum reg, RegNum baseReg) const;
  163. DWORD WriteXdataBytes(BYTE xdata[], DWORD byte, DWORD encoding, DWORD length);
  164. // Constants defined in the ABI.
  165. static const DWORD MaxPackedPdataFuncLength = 0xFFE;
  166. static const DWORD MaxPackedPdataStackDepth = 0xFCC;
  167. static const DWORD PackedPdataFlagMask = 3;
  168. static const DWORD ExpandedPdataFlag = 0;
  169. // Function length is required to have only these bits set.
  170. static const DWORD PackedFuncLengthMask = 0xFFE;
  171. // Bit offset of length within pdata dword, combined with right-shift of encoded length.
  172. static const DWORD PackedFuncLengthShift = 1;
  173. static const DWORD PackedNoPrologBits = 2;
  174. static const DWORD PackedNormalFuncBits = 1;
  175. static const DWORD PackedNonLeafRetBits = 0;
  176. static const DWORD PackedLeafRetBits = (1 << 13);
  177. static const DWORD PackedNoEpilogBits = (3 << 13);
  178. // C (frame chaining) and L (save LR) bits.
  179. static const DWORD PackedNonLeafFunctionBits = (1 << 20) | (1 << 21);
  180. static const DWORD PackedHomedParamsBit = (1 << 15);
  181. static const DWORD PackedRegShift = 16;
  182. static const DWORD PackedRegMask = 7;
  183. // Indicate no saved regs with a Reg field of 0x111 and the R bit set.
  184. static const DWORD PackedNoSavedRegsBits = (7 << PackedRegShift) | (1 << 19);
  185. // Stack depth is required to have only these bits set.
  186. static const DWORD PackedStackDepthMask = 0xFFC;
  187. // Bit offset of stack depth within pdata dword, combined with right-shift of encoded value.
  188. static const DWORD PackedStackDepthShift = 20;
  189. static const DWORD MaxXdataFuncLength = 0x7FFFE;
  190. static const DWORD XdataFuncLengthMask = 0x7FFFE;
  191. static const DWORD XdataFuncLengthAdjust = 1;
  192. static const DWORD XdataSingleEpilogShift = 21;
  193. static const DWORD XdataFuncFragmentShift = 22;
  194. static const DWORD XdataEpilogCountShift = 23;
  195. static const DWORD MaxXdataEpilogCount = 0x1F;
  196. static const DWORD MaxXdataDwordCount = 0xF;
  197. static const DWORD XdataDwordCountShift = 28;
  198. public:
  199. // Xdata constants.
  200. static const DWORD MaxXdataBytes = 40; //buffer of 4 for any future additions
  201. //
  202. // 28 == 4 (header DWORD) +
  203. // (4 (max stack alloc code) +
  204. // 1 (locals pointer setup) +
  205. // 5 (NOP for _chkstk case) +
  206. // 1 (double reg saves) +
  207. // 2 (reg saves) +
  208. // 1 (r11 setup) +
  209. // 2 (r11,lr saves) +
  210. // 1 (home params) +
  211. // 1 (NOP) +
  212. // 1 (end prolog) +
  213. // 4 (max stack alloc code, in case of locals pointer setup) +
  214. // 1 (double reg saves) +
  215. // 2 (reg saves) +
  216. // 2 (r11 save) +
  217. // 2 (indir return) +
  218. // 1 (end epilog)) rounded up to a DWORD boundary
  219. };