Win64EH.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===-- llvm/Support/Win64EH.h ---Win64 EH Constants-------------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. // This file contains constants and structures used for implementing
  15. // exception handling on Win64 platforms. For more information, see
  16. // http://msdn.microsoft.com/en-us/library/1eyas8tf.aspx
  17. //
  18. //===----------------------------------------------------------------------===//
  19. #ifndef LLVM_SUPPORT_WIN64EH_H
  20. #define LLVM_SUPPORT_WIN64EH_H
  21. #include "llvm/Support/DataTypes.h"
  22. #include "llvm/Support/Endian.h"
  23. namespace llvm {
  24. namespace Win64EH {
  25. /// UnwindOpcodes - Enumeration whose values specify a single operation in
  26. /// the prolog of a function.
  27. enum UnwindOpcodes {
  28. UOP_PushNonVol = 0,
  29. UOP_AllocLarge,
  30. UOP_AllocSmall,
  31. UOP_SetFPReg,
  32. UOP_SaveNonVol,
  33. UOP_SaveNonVolBig,
  34. UOP_Epilog,
  35. UOP_SpareCode,
  36. UOP_SaveXMM128,
  37. UOP_SaveXMM128Big,
  38. UOP_PushMachFrame,
  39. // The following set of unwind opcodes is for ARM64. They are documented at
  40. // https://docs.microsoft.com/en-us/cpp/build/arm64-exception-handling
  41. UOP_AllocMedium,
  42. UOP_SaveR19R20X,
  43. UOP_SaveFPLRX,
  44. UOP_SaveFPLR,
  45. UOP_SaveReg,
  46. UOP_SaveRegX,
  47. UOP_SaveRegP,
  48. UOP_SaveRegPX,
  49. UOP_SaveLRPair,
  50. UOP_SaveFReg,
  51. UOP_SaveFRegX,
  52. UOP_SaveFRegP,
  53. UOP_SaveFRegPX,
  54. UOP_SetFP,
  55. UOP_AddFP,
  56. UOP_Nop,
  57. UOP_End,
  58. UOP_SaveNext,
  59. UOP_TrapFrame,
  60. UOP_Context,
  61. UOP_ClearUnwoundToCall
  62. };
  63. /// UnwindCode - This union describes a single operation in a function prolog,
  64. /// or part thereof.
  65. union UnwindCode {
  66. struct {
  67. uint8_t CodeOffset;
  68. uint8_t UnwindOpAndOpInfo;
  69. } u;
  70. support::ulittle16_t FrameOffset;
  71. uint8_t getUnwindOp() const {
  72. return u.UnwindOpAndOpInfo & 0x0F;
  73. }
  74. uint8_t getOpInfo() const {
  75. return (u.UnwindOpAndOpInfo >> 4) & 0x0F;
  76. }
  77. };
  78. enum {
  79. /// UNW_ExceptionHandler - Specifies that this function has an exception
  80. /// handler.
  81. UNW_ExceptionHandler = 0x01,
  82. /// UNW_TerminateHandler - Specifies that this function has a termination
  83. /// handler.
  84. UNW_TerminateHandler = 0x02,
  85. /// UNW_ChainInfo - Specifies that this UnwindInfo structure is chained to
  86. /// another one.
  87. UNW_ChainInfo = 0x04
  88. };
  89. /// RuntimeFunction - An entry in the table of functions with unwind info.
  90. struct RuntimeFunction {
  91. support::ulittle32_t StartAddress;
  92. support::ulittle32_t EndAddress;
  93. support::ulittle32_t UnwindInfoOffset;
  94. };
  95. /// UnwindInfo - An entry in the exception table.
  96. struct UnwindInfo {
  97. uint8_t VersionAndFlags;
  98. uint8_t PrologSize;
  99. uint8_t NumCodes;
  100. uint8_t FrameRegisterAndOffset;
  101. UnwindCode UnwindCodes[1];
  102. uint8_t getVersion() const {
  103. return VersionAndFlags & 0x07;
  104. }
  105. uint8_t getFlags() const {
  106. return (VersionAndFlags >> 3) & 0x1f;
  107. }
  108. uint8_t getFrameRegister() const {
  109. return FrameRegisterAndOffset & 0x0f;
  110. }
  111. uint8_t getFrameOffset() const {
  112. return (FrameRegisterAndOffset >> 4) & 0x0f;
  113. }
  114. // The data after unwindCodes depends on flags.
  115. // If UNW_ExceptionHandler or UNW_TerminateHandler is set then follows
  116. // the address of the language-specific exception handler.
  117. // If UNW_ChainInfo is set then follows a RuntimeFunction which defines
  118. // the chained unwind info.
  119. // For more information please see MSDN at:
  120. // http://msdn.microsoft.com/en-us/library/ddssxxy8.aspx
  121. /// Return pointer to language specific data part of UnwindInfo.
  122. void *getLanguageSpecificData() {
  123. return reinterpret_cast<void *>(&UnwindCodes[(NumCodes+1) & ~1]);
  124. }
  125. /// Return pointer to language specific data part of UnwindInfo.
  126. const void *getLanguageSpecificData() const {
  127. return reinterpret_cast<const void *>(&UnwindCodes[(NumCodes + 1) & ~1]);
  128. }
  129. /// Return image-relative offset of language-specific exception handler.
  130. uint32_t getLanguageSpecificHandlerOffset() const {
  131. return *reinterpret_cast<const support::ulittle32_t *>(
  132. getLanguageSpecificData());
  133. }
  134. /// Set image-relative offset of language-specific exception handler.
  135. void setLanguageSpecificHandlerOffset(uint32_t offset) {
  136. *reinterpret_cast<support::ulittle32_t *>(getLanguageSpecificData()) =
  137. offset;
  138. }
  139. /// Return pointer to exception-specific data.
  140. void *getExceptionData() {
  141. return reinterpret_cast<void *>(reinterpret_cast<uint32_t *>(
  142. getLanguageSpecificData())+1);
  143. }
  144. /// Return pointer to chained unwind info.
  145. RuntimeFunction *getChainedFunctionEntry() {
  146. return reinterpret_cast<RuntimeFunction *>(getLanguageSpecificData());
  147. }
  148. /// Return pointer to chained unwind info.
  149. const RuntimeFunction *getChainedFunctionEntry() const {
  150. return reinterpret_cast<const RuntimeFunction *>(getLanguageSpecificData());
  151. }
  152. };
  153. } // End of namespace Win64EH
  154. } // End of namespace llvm
  155. #endif
  156. #ifdef __GNUC__
  157. #pragma GCC diagnostic pop
  158. #endif