LLVM 23.0.0git
Win64EH.h
Go to the documentation of this file.
1//===-- llvm/Support/Win64EH.h ---Win64 EH Constants-------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file contains constants and structures used for implementing
10// exception handling on Win64 platforms. For more information, see
11// http://msdn.microsoft.com/en-us/library/1eyas8tf.aspx
12//
13//===----------------------------------------------------------------------===//
14
15#ifndef LLVM_SUPPORT_WIN64EH_H
16#define LLVM_SUPPORT_WIN64EH_H
17
18#include "llvm/ADT/ArrayRef.h"
20#include "llvm/ADT/StringRef.h"
22#include "llvm/Support/Endian.h"
23#include "llvm/Support/Error.h"
24
25namespace llvm {
26namespace Win64EH {
27
28/// UnwindOpcodes - Enumeration whose values specify a single operation in
29/// the prolog of a function.
31 // The following set of unwind opcodes is for x86_64. They are documented at
32 // https://docs.microsoft.com/en-us/cpp/build/exception-handling-x64.
33 // Some generic values from this set are used for other architectures too.
45 // The following set of unwind opcodes is for ARM64. They are documented at
46 // https://docs.microsoft.com/en-us/cpp/build/arm64-exception-handling
85
86 // The following set of unwind opcodes is for ARM. They are documented at
87 // https://docs.microsoft.com/en-us/cpp/build/arm-exception-handling
88
89 // Stack allocations use UOP_AllocSmall, UOP_AllocLarge from above, plus
90 // the following. AllocSmall, AllocLarge and AllocHuge represent a 16 bit
91 // instruction, while the WideAlloc* opcodes represent a 32 bit instruction.
92 // Small can represent a stack offset of 0x7f*4 (252) bytes, Medium can
93 // represent up to 0x3ff*4 (4092) bytes, Large up to 0xffff*4 (262140) bytes,
94 // and Huge up to 0xffffff*4 (67108860) bytes.
99
109 // Using UOP_Nop from above
111 // Using UOP_End from above
114 // A custom unspecified opcode, consisting of one or more bytes. This
115 // allows producing opcodes in the implementation defined/reserved range.
117
118 // V3-only x86_64 opcodes. They are documented at
119 // https://learn.microsoft.com/en-us/cpp/build/x64-unwind-information-v3
120 UOP_Push2, // PUSH2 — two registers in one instruction
121};
122
123/// UnwindCode - This union describes a single operation in a function prolog,
124/// or part thereof.
126 struct {
129 } u;
131
133 return u.UnwindOpAndOpInfo & 0x0F;
134 }
136 return (u.UnwindOpAndOpInfo >> 4) & 0x0F;
137 }
138 /// Gets the offset for an UOP_Epilog unwind code.
141 return (getOpInfo() << 8) | static_cast<uint32_t>(u.CodeOffset);
142 }
143};
144
145enum {
146 /// UNW_ExceptionHandler - Specifies that this function has an exception
147 /// handler.
149 /// UNW_TerminateHandler - Specifies that this function has a termination
150 /// handler.
152 /// UNW_ChainInfo - Specifies that this UnwindInfo structure is chained to
153 /// another one.
155 /// UNW_FlagLarge - V3 only. When set, the header is 5 bytes (an extra
156 /// UNWIND_INFO_LARGE_V3 byte follows), SizeOfProlog extends to 16 bits,
157 /// and prolog IP offset entries are 16-bit.
159};
160
161/// RuntimeFunction - An entry in the table of functions with unwind info.
167
168/// UnwindInfo - An entry in the exception table.
175
177 return VersionAndFlags & 0x07;
178 }
180 return (VersionAndFlags >> 3) & 0x1f;
181 }
183 return FrameRegisterAndOffset & 0x0f;
184 }
186 return (FrameRegisterAndOffset >> 4) & 0x0f;
187 }
188
189 // The data after unwindCodes depends on flags.
190 // If UNW_ExceptionHandler or UNW_TerminateHandler is set then follows
191 // the address of the language-specific exception handler.
192 // If UNW_ChainInfo is set then follows a RuntimeFunction which defines
193 // the chained unwind info.
194 // For more information please see MSDN at:
195 // http://msdn.microsoft.com/en-us/library/ddssxxy8.aspx
196
197 /// Return pointer to language specific data part of UnwindInfo.
199 return reinterpret_cast<void *>(&UnwindCodes[(NumCodes+1) & ~1]);
200 }
201
202 /// Return pointer to language specific data part of UnwindInfo.
203 const void *getLanguageSpecificData() const {
204 return reinterpret_cast<const void *>(&UnwindCodes[(NumCodes + 1) & ~1]);
205 }
206
207 /// Return image-relative offset of language-specific exception handler.
209 return *reinterpret_cast<const support::ulittle32_t *>(
211 }
212
213 /// Set image-relative offset of language-specific exception handler.
215 *reinterpret_cast<support::ulittle32_t *>(getLanguageSpecificData()) =
216 offset;
217 }
218
219 /// Return pointer to exception-specific data.
221 return reinterpret_cast<void *>(reinterpret_cast<uint32_t *>(
223 }
224
225 /// Return pointer to chained unwind info.
229
230 /// Return pointer to chained unwind info.
232 return reinterpret_cast<const RuntimeFunction *>(getLanguageSpecificData());
233 }
234};
235
236//===----------------------------------------------------------------------===//
237// V3 Unwind Information
238//===----------------------------------------------------------------------===//
239
240/// V3 Winding Operation Descriptor opcodes.
242 WOD_SET_FPREG = 0, // 8-bit opcode, 2 bytes
243 WOD_ALLOC_HUGE = 1, // 8-bit opcode, 5 bytes
244 WOD_ALLOC_LARGE = 2, // 8-bit opcode, 3 bytes
245 WOD_PUSH_CANONICAL_FRAME = 3, // 8-bit opcode, 2 bytes
246 WOD_PUSH = 4, // 3-bit opcode, 1 byte
247 WOD_SAVE_NONVOL_FAR = 5, // 3-bit opcode, 5 bytes
248 WOD_SAVE_NONVOL = 6, // 3-bit opcode, 3 bytes
249 WOD_PUSH_CONSECUTIVE_2 = 7, // 3-bit opcode, 1 byte
250 WOD_ALLOC_SMALL = 8, // 4-bit opcode, 1 byte
251 WOD_SAVE_XMM128_FAR = 9, // 4-bit opcode, 5 bytes
252 WOD_SAVE_XMM128 = 10, // 4-bit opcode, 3 bytes
253 WOD_PUSH2 = 32, // 6-bit opcode, 2 bytes
254};
255
256/// V3 EPILOG_INFO flags.
259 /// When set, the extended descriptor uses EPILOG_INFO_LARGE_EX_V3 (16-bit
260 /// IpOffsetOfLastInstruction) and the IP offset array uses 16-bit entries.
262};
263
264/// Decoded V3 Winding Operation Descriptor.
267 uint8_t Register; // For applicable ops (5-bit for int, 4-bit for XMM)
268 uint8_t Register2; // For WOD_PUSH2
269 // TODO: Define a named enum for WOD_PUSH_CANONICAL_FRAME Type values once
270 // the Windows x64 Unwind V3 spec is finalized. The set of valid values is
271 // defined by the OS (see the Windows SDK headers) but is not yet stable.
272 uint8_t Type; // For WOD_PUSH_CANONICAL_FRAME
273 uint8_t ByteSize; // How many bytes this WOD consumed (max 5)
274 uint32_t Size; // For alloc ops: final computed size
275 uint32_t Displacement; // For save ops: final computed displacement
276};
277
278/// Decoded V3 epilog descriptor.
284 int32_t EpilogOffset; // Resolved absolute offset (accumulated from deltas).
286
287 /// Whether the EPILOG_INFO_LARGE flag is set.
288 bool isLarge() const { return Flags & EPILOG_INFO_LARGE; }
289};
290
291/// Decoded V3 UNWIND_INFO.
308
309/// Return the register name for a 5-bit AMD64 integer register number.
310/// Covers 0-15 (RAX-R15) and 16-31 (R16-R31 for APX).
312
313/// Decode one WOD from the pool at the given byte offset.
314/// Returns an error on malformed data.
316
317/// Parse a V3 UNWIND_INFO from raw bytes.
318/// Returns an error on malformed data.
320
321} // End of namespace Win64EH
322} // End of namespace llvm
323
324#endif
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
Register Reg
This file defines the SmallVector class.
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
Tagged union holding either a T or a Error.
Definition Error.h:485
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
StringRef getRegisterNameV3(unsigned Reg)
Return the register name for a 5-bit AMD64 integer register number.
Definition Win64EH.cpp:21
EpilogInfoFlagsV3
V3 EPILOG_INFO flags.
Definition Win64EH.h:257
@ EPILOG_PARENT_FRAGMENT_TRANSFER
Definition Win64EH.h:258
@ EPILOG_INFO_LARGE
When set, the extended descriptor uses EPILOG_INFO_LARGE_EX_V3 (16-bit IpOffsetOfLastInstruction) and...
Definition Win64EH.h:261
@ UNW_TerminateHandler
UNW_TerminateHandler - Specifies that this function has a termination handler.
Definition Win64EH.h:151
@ UNW_FlagLarge
UNW_FlagLarge - V3 only.
Definition Win64EH.h:158
@ UNW_ExceptionHandler
UNW_ExceptionHandler - Specifies that this function has an exception handler.
Definition Win64EH.h:148
@ UNW_ChainInfo
UNW_ChainInfo - Specifies that this UnwindInfo structure is chained to another one.
Definition Win64EH.h:154
UnwindOpcodes
UnwindOpcodes - Enumeration whose values specify a single operation in the prolog of a function.
Definition Win64EH.h:30
@ UOP_SaveAnyRegDPX
Definition Win64EH.h:79
@ UOP_SaveRegsR4R7LR
Definition Win64EH.h:102
@ UOP_ClearUnwoundToCall
Definition Win64EH.h:68
@ UOP_SaveNonVolBig
Definition Win64EH.h:39
@ UOP_WideAllocMedium
Definition Win64EH.h:96
@ UOP_SaveAnyRegDX
Definition Win64EH.h:78
@ UOP_SaveFRegD0D15
Definition Win64EH.h:107
@ UOP_SaveAnyRegQP
Definition Win64EH.h:75
@ UOP_SaveAnyRegD
Definition Win64EH.h:72
@ UOP_WideSaveRegsR4R11LR
Definition Win64EH.h:103
@ UOP_SaveAnyRegIPX
Definition Win64EH.h:77
@ UOP_WideAllocHuge
Definition Win64EH.h:98
@ UOP_SaveAnyRegQX
Definition Win64EH.h:80
@ UOP_SaveAnyRegIX
Definition Win64EH.h:76
@ UOP_SaveXMM128Big
Definition Win64EH.h:43
@ UOP_SaveAnyRegQ
Definition Win64EH.h:74
@ UOP_SaveAnyRegDP
Definition Win64EH.h:73
@ UOP_SaveFRegD8D15
Definition Win64EH.h:104
@ UOP_PushMachFrame
Definition Win64EH.h:44
@ UOP_SaveR19R20X
Definition Win64EH.h:48
@ UOP_SaveAnyRegQPX
Definition Win64EH.h:81
@ UOP_WideAllocLarge
Definition Win64EH.h:97
@ UOP_WideSaveRegMask
Definition Win64EH.h:100
@ UOP_AllocMedium
Definition Win64EH.h:47
@ UOP_SaveAnyRegIP
Definition Win64EH.h:71
@ UOP_SaveFRegD16D31
Definition Win64EH.h:108
@ UOP_SaveAnyRegI
Definition Win64EH.h:70
Expected< DecodedWOD > decodeWOD(ArrayRef< uint8_t > Pool, unsigned Offset)
Decode one WOD from the pool at the given byte offset.
Definition Win64EH.cpp:33
WODOpcode
V3 Winding Operation Descriptor opcodes.
Definition Win64EH.h:241
@ WOD_SAVE_XMM128_FAR
Definition Win64EH.h:251
@ WOD_PUSH_CANONICAL_FRAME
Definition Win64EH.h:245
@ WOD_PUSH_CONSECUTIVE_2
Definition Win64EH.h:249
@ WOD_SAVE_NONVOL_FAR
Definition Win64EH.h:247
Expected< DecodedUnwindInfoV3 > decodeUnwindInfoV3(ArrayRef< uint8_t > Data)
Parse a V3 UNWIND_INFO from raw bytes.
Definition Win64EH.cpp:194
detail::packed_endian_specific_integral< uint32_t, llvm::endianness::little, unaligned > ulittle32_t
Definition Endian.h:290
detail::packed_endian_specific_integral< uint16_t, llvm::endianness::little, unaligned > ulittle16_t
Definition Endian.h:287
This is an optimization pass for GlobalISel generic memory operations.
@ Offset
Definition DWP.cpp:558
FunctionAddr VTableAddr uintptr_t uintptr_t Data
Definition InstrProf.h:221
Decoded V3 epilog descriptor.
Definition Win64EH.h:279
bool isLarge() const
Whether the EPILOG_INFO_LARGE flag is set.
Definition Win64EH.h:288
SmallVector< uint16_t, 8 > IpOffsets
Definition Win64EH.h:285
Decoded V3 UNWIND_INFO.
Definition Win64EH.h:292
uint16_t PayloadSize
Total bytes consumed by header + payload (used to locate handler/chain).
Definition Win64EH.h:300
SmallVector< DecodedEpilogV3, 4 > Epilogs
Definition Win64EH.h:302
ArrayRef< uint8_t > WODPool
Definition Win64EH.h:303
bool isLarge() const
Whether the UNW_FlagLarge flag is set.
Definition Win64EH.h:306
SmallVector< uint16_t, 8 > PrologIpOffsets
Definition Win64EH.h:301
Decoded V3 Winding Operation Descriptor.
Definition Win64EH.h:265
RuntimeFunction - An entry in the table of functions with unwind info.
Definition Win64EH.h:162
support::ulittle32_t StartAddress
Definition Win64EH.h:163
support::ulittle32_t EndAddress
Definition Win64EH.h:164
support::ulittle32_t UnwindInfoOffset
Definition Win64EH.h:165
UnwindInfo - An entry in the exception table.
Definition Win64EH.h:169
void * getExceptionData()
Return pointer to exception-specific data.
Definition Win64EH.h:220
uint8_t getFrameOffset() const
Definition Win64EH.h:185
uint8_t getFrameRegister() const
Definition Win64EH.h:182
UnwindCode UnwindCodes[1]
Definition Win64EH.h:174
uint8_t getFlags() const
Definition Win64EH.h:179
const void * getLanguageSpecificData() const
Return pointer to language specific data part of UnwindInfo.
Definition Win64EH.h:203
void setLanguageSpecificHandlerOffset(uint32_t offset)
Set image-relative offset of language-specific exception handler.
Definition Win64EH.h:214
const RuntimeFunction * getChainedFunctionEntry() const
Return pointer to chained unwind info.
Definition Win64EH.h:231
uint32_t getLanguageSpecificHandlerOffset() const
Return image-relative offset of language-specific exception handler.
Definition Win64EH.h:208
void * getLanguageSpecificData()
Return pointer to language specific data part of UnwindInfo.
Definition Win64EH.h:198
uint8_t getVersion() const
Definition Win64EH.h:176
RuntimeFunction * getChainedFunctionEntry()
Return pointer to chained unwind info.
Definition Win64EH.h:226
UnwindCode - This union describes a single operation in a function prolog, or part thereof.
Definition Win64EH.h:125
struct llvm::Win64EH::UnwindCode::@155363032112205007051067226341223137067342247116 u
uint8_t getOpInfo() const
Definition Win64EH.h:135
support::ulittle16_t FrameOffset
Definition Win64EH.h:130
uint8_t getUnwindOp() const
Definition Win64EH.h:132
uint32_t getEpilogOffset() const
Gets the offset for an UOP_Epilog unwind code.
Definition Win64EH.h:139