LLVM 24.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
86
87 // The following set of unwind opcodes is for ARM. They are documented at
88 // https://docs.microsoft.com/en-us/cpp/build/arm-exception-handling
89
90 // Stack allocations use UOP_AllocSmall, UOP_AllocLarge from above, plus
91 // the following. AllocSmall, AllocLarge and AllocHuge represent a 16 bit
92 // instruction, while the WideAlloc* opcodes represent a 32 bit instruction.
93 // Small can represent a stack offset of 0x7f*4 (252) bytes, Medium can
94 // represent up to 0x3ff*4 (4092) bytes, Large up to 0xffff*4 (262140) bytes,
95 // and Huge up to 0xffffff*4 (67108860) bytes.
100
110 // Using UOP_Nop from above
112 // Using UOP_End from above
115 // A custom unspecified opcode, consisting of one or more bytes. This
116 // allows producing opcodes in the implementation defined/reserved range.
118
119 // V3-only x86_64 opcodes. They are documented at
120 // https://learn.microsoft.com/en-us/cpp/build/x64-unwind-information-v3
121 UOP_Push2, // PUSH2 — two registers in one instruction
122};
123
124/// UnwindCode - This union describes a single operation in a function prolog,
125/// or part thereof.
127 struct {
130 } u;
132
134 return u.UnwindOpAndOpInfo & 0x0F;
135 }
137 return (u.UnwindOpAndOpInfo >> 4) & 0x0F;
138 }
139 /// Gets the offset for an UOP_Epilog unwind code.
142 return (getOpInfo() << 8) | static_cast<uint32_t>(u.CodeOffset);
143 }
144};
145
146enum {
147 /// UNW_ExceptionHandler - Specifies that this function has an exception
148 /// handler.
150 /// UNW_TerminateHandler - Specifies that this function has a termination
151 /// handler.
153 /// UNW_ChainInfo - Specifies that this UnwindInfo structure is chained to
154 /// another one.
156 /// UNW_FlagLarge - V3 only. When set, the header is 5 bytes (an extra
157 /// UNWIND_INFO_LARGE_V3 byte follows), SizeOfProlog extends to 16 bits,
158 /// and prolog IP offset entries are 16-bit.
160};
161
162/// RuntimeFunction - An entry in the table of functions with unwind info.
168
169/// UnwindInfo - An entry in the exception table.
176
178 return VersionAndFlags & 0x07;
179 }
181 return (VersionAndFlags >> 3) & 0x1f;
182 }
184 return FrameRegisterAndOffset & 0x0f;
185 }
187 return (FrameRegisterAndOffset >> 4) & 0x0f;
188 }
189
190 // The data after unwindCodes depends on flags.
191 // If UNW_ExceptionHandler or UNW_TerminateHandler is set then follows
192 // the address of the language-specific exception handler.
193 // If UNW_ChainInfo is set then follows a RuntimeFunction which defines
194 // the chained unwind info.
195 // For more information please see MSDN at:
196 // http://msdn.microsoft.com/en-us/library/ddssxxy8.aspx
197
198 /// Return pointer to language specific data part of UnwindInfo.
200 return reinterpret_cast<void *>(&UnwindCodes[(NumCodes+1) & ~1]);
201 }
202
203 /// Return pointer to language specific data part of UnwindInfo.
204 const void *getLanguageSpecificData() const {
205 return reinterpret_cast<const void *>(&UnwindCodes[(NumCodes + 1) & ~1]);
206 }
207
208 /// Return image-relative offset of language-specific exception handler.
210 return *reinterpret_cast<const support::ulittle32_t *>(
212 }
213
214 /// Set image-relative offset of language-specific exception handler.
216 *reinterpret_cast<support::ulittle32_t *>(getLanguageSpecificData()) =
217 offset;
218 }
219
220 /// Return pointer to exception-specific data.
222 return reinterpret_cast<void *>(reinterpret_cast<uint32_t *>(
224 }
225
226 /// Return pointer to chained unwind info.
230
231 /// Return pointer to chained unwind info.
233 return reinterpret_cast<const RuntimeFunction *>(getLanguageSpecificData());
234 }
235};
236
237//===----------------------------------------------------------------------===//
238// V3 Unwind Information
239//===----------------------------------------------------------------------===//
240
241/// V3 Winding Operation Descriptor opcodes.
243 WOD_SET_FPREG = 0, // 8-bit opcode, 2 bytes
244 WOD_ALLOC_HUGE = 1, // 8-bit opcode, 5 bytes
245 WOD_ALLOC_LARGE = 2, // 8-bit opcode, 3 bytes
246 WOD_PUSH_CANONICAL_FRAME = 3, // 8-bit opcode, 2 bytes
247 WOD_PUSH = 4, // 3-bit opcode, 1 byte
248 WOD_SAVE_NONVOL_FAR = 5, // 3-bit opcode, 5 bytes
249 WOD_SAVE_NONVOL = 6, // 3-bit opcode, 3 bytes
250 WOD_PUSH_CONSECUTIVE_2 = 7, // 3-bit opcode, 1 byte
251 WOD_ALLOC_SMALL = 8, // 4-bit opcode, 1 byte
252 WOD_SAVE_XMM128_FAR = 9, // 4-bit opcode, 5 bytes
253 WOD_SAVE_XMM128 = 10, // 4-bit opcode, 3 bytes
254 WOD_PUSH2 = 32, // 6-bit opcode, 2 bytes
255};
256
257/// V3 EPILOG_INFO flags.
260 /// When set, the extended descriptor uses EPILOG_INFO_LARGE_EX_V3 (16-bit
261 /// IpOffsetOfLastInstruction) and the IP offset array uses 16-bit entries.
263};
264
265/// Decoded V3 Winding Operation Descriptor.
268 uint8_t Register; // For applicable ops (5-bit for int, 4-bit for XMM)
269 uint8_t Register2; // For WOD_PUSH2
270 // TODO: Define a named enum for WOD_PUSH_CANONICAL_FRAME Type values once
271 // the Windows x64 Unwind V3 spec is finalized. The set of valid values is
272 // defined by the OS (see the Windows SDK headers) but is not yet stable.
273 uint8_t Type; // For WOD_PUSH_CANONICAL_FRAME
274 uint8_t ByteSize; // How many bytes this WOD consumed (max 5)
275 uint32_t Size; // For alloc ops: final computed size
276 uint32_t Displacement; // For save ops: final computed displacement
277};
278
279/// Decoded V3 epilog descriptor.
285 int32_t EpilogOffset; // Resolved absolute offset (accumulated from deltas).
287
288 /// Whether the EPILOG_INFO_LARGE flag is set.
289 bool isLarge() const { return Flags & EPILOG_INFO_LARGE; }
290};
291
292/// Decoded V3 UNWIND_INFO.
309
310/// Return the register name for a 5-bit AMD64 integer register number.
311/// Covers 0-15 (RAX-R15) and 16-31 (R16-R31 for APX).
313
314/// Decode one WOD from the pool at the given byte offset.
315/// Returns an error on malformed data.
317 unsigned Offset);
318
319/// Parse a V3 UNWIND_INFO from raw bytes.
320/// Returns an error on malformed data.
323
324} // End of namespace Win64EH
325} // End of namespace llvm
326
327#endif
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
#define LLVM_ABI
Definition Compiler.h:215
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
LLVM_ABI 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:258
@ EPILOG_PARENT_FRAGMENT_TRANSFER
Definition Win64EH.h:259
@ EPILOG_INFO_LARGE
When set, the extended descriptor uses EPILOG_INFO_LARGE_EX_V3 (16-bit IpOffsetOfLastInstruction) and...
Definition Win64EH.h:262
@ UNW_TerminateHandler
UNW_TerminateHandler - Specifies that this function has a termination handler.
Definition Win64EH.h:152
@ UNW_FlagLarge
UNW_FlagLarge - V3 only.
Definition Win64EH.h:159
@ UNW_ExceptionHandler
UNW_ExceptionHandler - Specifies that this function has an exception handler.
Definition Win64EH.h:149
@ UNW_ChainInfo
UNW_ChainInfo - Specifies that this UnwindInfo structure is chained to another one.
Definition Win64EH.h:155
UnwindOpcodes
UnwindOpcodes - Enumeration whose values specify a single operation in the prolog of a function.
Definition Win64EH.h:30
@ UOP_SaveAnyRegDPX
Definition Win64EH.h:80
@ UOP_SaveRegsR4R7LR
Definition Win64EH.h:103
@ UOP_ClearUnwoundToCall
Definition Win64EH.h:69
@ UOP_SaveNonVolBig
Definition Win64EH.h:39
@ UOP_WideAllocMedium
Definition Win64EH.h:97
@ UOP_SaveAnyRegDX
Definition Win64EH.h:79
@ UOP_SaveFRegD0D15
Definition Win64EH.h:108
@ UOP_SaveAnyRegQP
Definition Win64EH.h:76
@ UOP_SaveAnyRegD
Definition Win64EH.h:73
@ UOP_WideSaveRegsR4R11LR
Definition Win64EH.h:104
@ UOP_SaveAnyRegIPX
Definition Win64EH.h:78
@ UOP_WideAllocHuge
Definition Win64EH.h:99
@ UOP_SaveAnyRegQX
Definition Win64EH.h:81
@ UOP_SaveAnyRegIX
Definition Win64EH.h:77
@ UOP_SaveXMM128Big
Definition Win64EH.h:43
@ UOP_SaveAnyRegQ
Definition Win64EH.h:75
@ UOP_SaveAnyRegDP
Definition Win64EH.h:74
@ UOP_SaveFRegD8D15
Definition Win64EH.h:105
@ UOP_PushMachFrame
Definition Win64EH.h:44
@ UOP_SaveR19R20X
Definition Win64EH.h:48
@ UOP_SaveAnyRegQPX
Definition Win64EH.h:82
@ UOP_WideAllocLarge
Definition Win64EH.h:98
@ UOP_WideSaveRegMask
Definition Win64EH.h:101
@ UOP_AllocMedium
Definition Win64EH.h:47
@ UOP_SaveAnyRegIP
Definition Win64EH.h:72
@ UOP_SaveFRegD16D31
Definition Win64EH.h:109
@ UOP_SaveAnyRegI
Definition Win64EH.h:71
LLVM_ABI 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:242
@ WOD_SAVE_XMM128_FAR
Definition Win64EH.h:252
@ WOD_PUSH_CANONICAL_FRAME
Definition Win64EH.h:246
@ WOD_PUSH_CONSECUTIVE_2
Definition Win64EH.h:250
@ WOD_SAVE_NONVOL_FAR
Definition Win64EH.h:248
LLVM_ABI 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:270
detail::packed_endian_specific_integral< uint16_t, llvm::endianness::little, unaligned > ulittle16_t
Definition Endian.h:267
This is an optimization pass for GlobalISel generic memory operations.
@ Offset
Definition DWP.cpp:577
Decoded V3 epilog descriptor.
Definition Win64EH.h:280
bool isLarge() const
Whether the EPILOG_INFO_LARGE flag is set.
Definition Win64EH.h:289
SmallVector< uint16_t, 8 > IpOffsets
Definition Win64EH.h:286
Decoded V3 UNWIND_INFO.
Definition Win64EH.h:293
uint16_t PayloadSize
Total bytes consumed by header + payload (used to locate handler/chain).
Definition Win64EH.h:301
SmallVector< DecodedEpilogV3, 4 > Epilogs
Definition Win64EH.h:303
ArrayRef< uint8_t > WODPool
Definition Win64EH.h:304
bool isLarge() const
Whether the UNW_FlagLarge flag is set.
Definition Win64EH.h:307
SmallVector< uint16_t, 8 > PrologIpOffsets
Definition Win64EH.h:302
Decoded V3 Winding Operation Descriptor.
Definition Win64EH.h:266
RuntimeFunction - An entry in the table of functions with unwind info.
Definition Win64EH.h:163
support::ulittle32_t StartAddress
Definition Win64EH.h:164
support::ulittle32_t EndAddress
Definition Win64EH.h:165
support::ulittle32_t UnwindInfoOffset
Definition Win64EH.h:166
UnwindInfo - An entry in the exception table.
Definition Win64EH.h:170
void * getExceptionData()
Return pointer to exception-specific data.
Definition Win64EH.h:221
uint8_t getFrameOffset() const
Definition Win64EH.h:186
uint8_t getFrameRegister() const
Definition Win64EH.h:183
UnwindCode UnwindCodes[1]
Definition Win64EH.h:175
uint8_t getFlags() const
Definition Win64EH.h:180
const void * getLanguageSpecificData() const
Return pointer to language specific data part of UnwindInfo.
Definition Win64EH.h:204
void setLanguageSpecificHandlerOffset(uint32_t offset)
Set image-relative offset of language-specific exception handler.
Definition Win64EH.h:215
const RuntimeFunction * getChainedFunctionEntry() const
Return pointer to chained unwind info.
Definition Win64EH.h:232
uint32_t getLanguageSpecificHandlerOffset() const
Return image-relative offset of language-specific exception handler.
Definition Win64EH.h:209
void * getLanguageSpecificData()
Return pointer to language specific data part of UnwindInfo.
Definition Win64EH.h:199
uint8_t getVersion() const
Definition Win64EH.h:177
RuntimeFunction * getChainedFunctionEntry()
Return pointer to chained unwind info.
Definition Win64EH.h:227
UnwindCode - This union describes a single operation in a function prolog, or part thereof.
Definition Win64EH.h:126
struct llvm::Win64EH::UnwindCode::@155363032112205007051067226341223137067342247116 u
uint8_t getOpInfo() const
Definition Win64EH.h:136
support::ulittle16_t FrameOffset
Definition Win64EH.h:131
uint8_t getUnwindOp() const
Definition Win64EH.h:133
uint32_t getEpilogOffset() const
Gets the offset for an UOP_Epilog unwind code.
Definition Win64EH.h:140