LLVM 22.0.0git
ARMWinEH.h
Go to the documentation of this file.
1//===-- llvm/Support/ARMWinEH.h - Windows on ARM 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#ifndef LLVM_SUPPORT_ARMWINEH_H
10#define LLVM_SUPPORT_ARMWINEH_H
11
12#include "llvm/ADT/ArrayRef.h"
14#include "llvm/Support/Endian.h"
15
16namespace llvm {
17namespace ARM {
18namespace WinEH {
20 RFF_Unpacked, /// unpacked entry
21 RFF_Packed, /// packed entry
22 RFF_PackedFragment, /// packed entry representing a fragment
23 RFF_Reserved, /// reserved
24};
25
26enum class ReturnType {
27 RT_POP, /// return via pop {pc} (L flag must be set)
28 RT_B, /// 16-bit branch
29 RT_BW, /// 32-bit branch
30 RT_NoEpilogue, /// no epilogue (fragment)
31};
32
33/// RuntimeFunction - An entry in the table of procedure data (.pdata)
34///
35/// This is ARM specific, but the Function Start RVA, Flag and
36/// ExceptionInformationRVA fields work identically for ARM64.
37///
38/// 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
39/// 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
40/// +---------------------------------------------------------------+
41/// | Function Start RVA |
42/// +-------------------+-+-+-+-----+-+---+---------------------+---+
43/// | Stack Adjust |C|L|R| Reg |H|Ret| Function Length |Flg|
44/// +-------------------+-+-+-+-----+-+---+---------------------+---+
45///
46/// Flag : 2-bit field with the following meanings:
47/// - 00 = packed unwind data not used; reamining bits point to .xdata record
48/// - 01 = packed unwind data
49/// - 10 = packed unwind data, function assumed to have no prologue; useful
50/// for function fragments that are discontiguous with the start of the
51/// function
52/// - 11 = reserved
53/// Function Length : 11-bit field providing the length of the entire function
54/// in bytes, divided by 2; if the function is greater than
55/// 4KB, a full .xdata record must be used instead
56/// Ret : 2-bit field indicating how the function returns
57/// - 00 = return via pop {pc} (the L bit must be set)
58/// - 01 = return via 16-bit branch
59/// - 10 = return via 32-bit branch
60/// - 11 = no epilogue; useful for function fragments that may only contain a
61/// prologue but the epilogue is elsewhere
62/// H : 1-bit flag indicating whether the function "homes" the integer parameter
63/// registers (r0-r3), allocating 16-bytes on the stack
64/// Reg : 3-bit field indicating the index of the last saved non-volatile
65/// register. If the R bit is set to 0, then only integer registers are
66/// saved (r4-rN, where N is 4 + Reg). If the R bit is set to 1, then
67/// only floating-point registers are being saved (d8-dN, where N is
68/// 8 + Reg). The special case of the R bit being set to 1 and Reg equal
69/// to 7 indicates that no registers are saved.
70/// R : 1-bit flag indicating whether the non-volatile registers are integer or
71/// floating-point. 0 indicates integer, 1 indicates floating-point. The
72/// special case of the R-flag being set and Reg being set to 7 indicates
73/// that no non-volatile registers are saved.
74/// L : 1-bit flag indicating whether the function saves/restores the link
75/// register (LR)
76/// C : 1-bit flag indicating whether the function includes extra instructions
77/// to setup a frame chain for fast walking. If this flag is set, r11 is
78/// implicitly added to the list of saved non-volatile integer registers.
79/// Stack Adjust : 10-bit field indicating the number of bytes of stack that are
80/// allocated for this function. Only values between 0x000 and
81/// 0x3f3 can be directly encoded. If the value is 0x3f4 or
82/// greater, then the low 4 bits have special meaning as follows:
83/// - Bit 0-1
84/// indicate the number of words' of adjustment (1-4), minus 1
85/// - Bit 2
86/// indicates if the prologue combined adjustment into push
87/// - Bit 3
88/// indicates if the epilogue combined adjustment into pop
89///
90/// RESTRICTIONS:
91/// - IF C is SET:
92/// + L flag must be set since frame chaining requires r11 and lr
93/// + r11 must NOT be included in the set of registers described by Reg
94/// - IF Ret is 0:
95/// + L flag must be set
96
97// NOTE: RuntimeFunction is meant to be a simple class that provides raw access
98// to all fields in the structure. The accessor methods reflect the names of
99// the bitfields that they correspond to. Although some obvious simplifications
100// are possible via merging of methods, it would prevent the use of this class
101// to fully inspect the contents of the data structure which is particularly
102// useful for scenarios such as llvm-readobj to aid in testing.
103
105public:
108
110 : BeginAddress(Data[0]), UnwindData(Data[1]) {}
111
115
117 return RuntimeFunctionFlag(UnwindData & 0x3);
118 }
119
122 "unpacked form required for this operation");
123 return (UnwindData & ~0x3);
124 }
125
129 "packed form required for this operation");
130 return (UnwindData & ~0x3);
131 }
135 "packed form required for this operation");
136 return (((UnwindData & 0x00001ffc) >> 2) << 1);
137 }
138 ReturnType Ret() const {
141 "packed form required for this operation");
142 assert(((UnwindData & 0x00006000) || L()) && "L must be set to 1");
143 return ReturnType((UnwindData & 0x00006000) >> 13);
144 }
145 bool H() const {
148 "packed form required for this operation");
149 return ((UnwindData & 0x00008000) >> 15);
150 }
151 uint8_t Reg() const {
154 "packed form required for this operation");
155 return ((UnwindData & 0x00070000) >> 16);
156 }
157 bool R() const {
160 "packed form required for this operation");
161 return ((UnwindData & 0x00080000) >> 19);
162 }
163 bool L() const {
166 "packed form required for this operation");
167 return ((UnwindData & 0x00100000) >> 20);
168 }
169 bool C() const {
172 "packed form required for this operation");
173 assert(((~UnwindData & 0x00200000) || L()) &&
174 "L flag must be set, chaining requires r11 and LR");
175 assert(((~UnwindData & 0x00200000) || (Reg() < 7) || R()) &&
176 "r11 must not be included in Reg; C implies r11");
177 return ((UnwindData & 0x00200000) >> 21);
178 }
182 "packed form required for this operation");
183 return ((UnwindData & 0xffc00000) >> 22);
184 }
185};
186
187/// PrologueFolding - pseudo-flag derived from Stack Adjust indicating that the
188/// prologue has stack adjustment combined into the push
189inline bool PrologueFolding(const RuntimeFunction &RF) {
190 return RF.StackAdjust() >= 0x3f4 && (RF.StackAdjust() & 0x4);
191}
192/// Epilogue - pseudo-flag derived from Stack Adjust indicating that the
193/// epilogue has stack adjustment combined into the pop
194inline bool EpilogueFolding(const RuntimeFunction &RF) {
195 return RF.StackAdjust() >= 0x3f4 && (RF.StackAdjust() & 0x8);
196}
197/// StackAdjustment - calculated stack adjustment in words. The stack
198/// adjustment should be determined via this function to account for the special
199/// handling the special encoding when the value is >= 0x3f4.
201 uint16_t Adjustment = RF.StackAdjust();
202 if (Adjustment >= 0x3f4)
203 return (Adjustment & 0x3) + 1;
204 return Adjustment;
205}
206
207/// SavedRegisterMask - Utility function to calculate the set of saved general
208/// purpose (r0-r15) and VFP (d0-d31) registers.
209LLVM_ABI std::pair<uint16_t, uint32_t>
210SavedRegisterMask(const RuntimeFunction &RF, bool Prologue = true);
211
212/// RuntimeFunctionARM64 - An entry in the table of procedure data (.pdata)
213///
214/// 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
215/// 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
216/// +---------------------------------------------------------------+
217/// | Function Start RVA |
218/// +-----------------+---+-+-------+-----+---------------------+---+
219/// | Frame Size |CR |H| RegI |RegF | Function Length |Flg|
220/// +-----------------+---+-+-------+-----+---------------------+---+
221///
222/// See https://docs.microsoft.com/en-us/cpp/build/arm64-exception-handling
223/// for the full reference for this struct.
224
226public:
229
231 : BeginAddress(Data[0]), UnwindData(Data[1]) {}
232
236
238 return RuntimeFunctionFlag(UnwindData & 0x3);
239 }
240
243 "unpacked form required for this operation");
244 return (UnwindData & ~0x3);
245 }
246
250 "packed form required for this operation");
251 return (UnwindData & ~0x3);
252 }
256 "packed form required for this operation");
257 return (((UnwindData & 0x00001ffc) >> 2) << 2);
258 }
259 uint8_t RegF() const {
262 "packed form required for this operation");
263 return ((UnwindData & 0x0000e000) >> 13);
264 }
265 uint8_t RegI() const {
268 "packed form required for this operation");
269 return ((UnwindData & 0x000f0000) >> 16);
270 }
271 bool H() const {
274 "packed form required for this operation");
275 return ((UnwindData & 0x00100000) >> 20);
276 }
277 uint8_t CR() const {
280 "packed form required for this operation");
281 return ((UnwindData & 0x600000) >> 21);
282 }
286 "packed form required for this operation");
287 return ((UnwindData & 0xff800000) >> 23);
288 }
289};
290
291/// ExceptionDataRecord - An entry in the table of exception data (.xdata)
292///
293/// The format on ARM is:
294///
295/// 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
296/// 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
297/// +-------+---------+-+-+-+---+-----------------------------------+
298/// | C Wrd | Epi Cnt |F|E|X|Ver| Function Length |
299/// +-------+--------+'-'-'-'---'---+-------------------------------+
300/// | Reserved |Ex. Code Words| (Extended Epilogue Count) |
301/// +-------+--------+--------------+-------------------------------+
302///
303/// The format on ARM64 is:
304///
305/// 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
306/// 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
307/// +---------+---------+-+-+---+-----------------------------------+
308/// | C Wrd | Epi Cnt |E|X|Ver| Function Length |
309/// +---------+------+--'-'-'---'---+-------------------------------+
310/// | Reserved |Ex. Code Words| (Extended Epilogue Count) |
311/// +-------+--------+--------------+-------------------------------+
312///
313/// Function Length : 18-bit field indicating the total length of the function
314/// in bytes divided by 2. If a function is larger than
315/// 512KB, then multiple pdata and xdata records must be used.
316/// Vers : 2-bit field describing the version of the remaining structure. Only
317/// version 0 is currently defined (values 1-3 are not permitted).
318/// X : 1-bit field indicating the presence of exception data
319/// E : 1-bit field indicating that the single epilogue is packed into the
320/// header
321/// F : 1-bit field indicating that the record describes a function fragment
322/// (implies that no prologue is present, and prologue processing should be
323/// skipped) (ARM only)
324/// Epilogue Count : 5-bit field that differs in meaning based on the E field.
325///
326/// If E is set, then this field specifies the index of the
327/// first unwind code describing the (only) epilogue.
328///
329/// Otherwise, this field indicates the number of exception
330/// scopes. If more than 31 scopes exist, then this field and
331/// the Code Words field must both be set to 0 to indicate that
332/// an extension word is required.
333/// Code Words : 4-bit (5-bit on ARM64) field that specifies the number of
334/// 32-bit words needed to contain all the unwind codes. If more
335/// than 15 words (31 words on ARM64) are required, then this field
336/// and the Epilogue Count field must both be set to 0 to indicate
337/// that an extension word is required.
338/// Extended Epilogue Count, Extended Code Words :
339/// Valid only if Epilog Count and Code Words are both
340/// set to 0. Provides an 8-bit extended code word
341/// count and 16-bits for epilogue count
342///
343/// The epilogue scope format on ARM is:
344///
345/// 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
346/// 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
347/// +----------------+------+---+---+-------------------------------+
348/// | Ep Start Idx | Cond |Res| Epilogue Start Offset |
349/// +----------------+------+---+-----------------------------------+
350///
351/// The epilogue scope format on ARM64 is:
352///
353/// 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
354/// 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
355/// +-------------------+-------+---+-------------------------------+
356/// | Ep Start Idx | Res | Epilogue Start Offset |
357/// +-------------------+-------+-----------------------------------+
358///
359/// If the E bit is unset in the header, the header is followed by a series of
360/// epilogue scopes, which are sorted by their offset.
361///
362/// Epilogue Start Offset: 18-bit field encoding the offset of epilogue relative
363/// to the start of the function in bytes divided by two
364/// Res : 2-bit field reserved for future expansion (must be set to 0)
365/// Condition : (ARM only) 4-bit field providing the condition under which the
366/// epilogue is executed. Unconditional epilogues should set this
367/// field to 0xe. Epilogues must be entirely conditional or
368/// unconditional, and in Thumb-2 mode. The epilogue begins with
369/// the first instruction after the IT opcode.
370/// Epilogue Start Index : 8-bit field indicating the byte index of the first
371/// unwind code describing the epilogue
372///
373/// 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
374/// 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
375/// +---------------+---------------+---------------+---------------+
376/// | Unwind Code 3 | Unwind Code 2 | Unwind Code 1 | Unwind Code 0 |
377/// +---------------+---------------+---------------+---------------+
378///
379/// Following the epilogue scopes, the byte code describing the unwinding
380/// follows. This is padded to align up to word alignment. Bytes are stored in
381/// little endian.
382///
383/// 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
384/// 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
385/// +---------------------------------------------------------------+
386/// | Exception Handler RVA (requires X = 1) |
387/// +---------------------------------------------------------------+
388/// | (possibly followed by data required for exception handler) |
389/// +---------------------------------------------------------------+
390///
391/// If the X bit is set in the header, the unwind byte code is followed by the
392/// exception handler information. This constants of one Exception Handler RVA
393/// which is the address to the exception handler, followed immediately by the
394/// variable length data associated with the exception handler.
395///
396
399
401 // Same for both ARM and AArch64.
403 return (ES & 0x0003ffff);
404 }
405
406 // Different implementations for ARM and AArch64.
407 uint8_t ResARM() const {
408 return ((ES & 0x000c0000) >> 18);
409 }
410
412 return ((ES & 0x000f0000) >> 18);
413 }
414
415 // Condition is only applicable to ARM.
417 return ((ES & 0x00f00000) >> 20);
418 }
419
420 // Different implementations for ARM and AArch64.
422 return ((ES & 0xff000000) >> 24);
423 }
424
426 return ((ES & 0xffc00000) >> 22);
427 }
428};
429
430struct ExceptionDataRecord;
431inline size_t HeaderWords(const ExceptionDataRecord &XR);
432
436
439
441 return (Data[0] & 0x0003ffff);
442 }
443
445 return FunctionLength() << 1;
446 }
447
449 return FunctionLength() << 2;
450 }
451
452 uint8_t Vers() const {
453 return (Data[0] & 0x000C0000) >> 18;
454 }
455
456 bool X() const {
457 return ((Data[0] & 0x00100000) >> 20);
458 }
459
460 bool E() const {
461 return ((Data[0] & 0x00200000) >> 21);
462 }
463
464 bool F() const {
465 assert(!isAArch64 && "Fragments are only supported on ARMv7 WinEH");
466 return ((Data[0] & 0x00400000) >> 22);
467 }
468
470 if (HeaderWords(*this) == 1) {
471 if (isAArch64)
472 return (Data[0] & 0x07C00000) >> 22;
473 return (Data[0] & 0x0f800000) >> 23;
474 }
475 return Data[1] & 0x0000ffff;
476 }
477
479 if (HeaderWords(*this) == 1) {
480 if (isAArch64)
481 return (Data[0] & 0xf8000000) >> 27;
482 return (Data[0] & 0xf0000000) >> 28;
483 }
484 return (Data[1] & 0x00ff0000) >> 16;
485 }
486
488 assert(E() == 0 && "epilogue scopes are only present when the E bit is 0");
489 size_t Offset = HeaderWords(*this);
490 return ArrayRef(&Data[Offset], EpilogueCount());
491 }
492
494 const size_t Offset = HeaderWords(*this)
495 + (E() ? 0 : EpilogueCount());
496 const uint8_t *ByteCode =
497 reinterpret_cast<const uint8_t *>(&Data[Offset]);
498 return ArrayRef(ByteCode, CodeWords() * sizeof(uint32_t));
499 }
500
502 assert(X() && "Exception Handler RVA is only valid if the X bit is set");
503 return Data[HeaderWords(*this) + (E() ? 0 : EpilogueCount()) + CodeWords()];
504 }
505
507 assert(X() && "Exception Handler RVA is only valid if the X bit is set");
508 return Data[HeaderWords(*this) + (E() ? 0 : EpilogueCount()) + CodeWords() +
509 1];
510 }
511};
512
513inline size_t HeaderWords(const ExceptionDataRecord &XR) {
514 if (XR.isAArch64)
515 return (XR.Data[0] & 0xffc00000) ? 1 : 2;
516 return (XR.Data[0] & 0xff800000) ? 1 : 2;
517}
518}
519}
520}
521
522#endif
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
#define LLVM_ABI
Definition: Compiler.h:213
RuntimeFunctionARM64 - An entry in the table of procedure data (.pdata)
Definition: ARMWinEH.h:225
RuntimeFunctionARM64(const support::ulittle32_t *Data)
Definition: ARMWinEH.h:230
RuntimeFunctionARM64(const support::ulittle32_t BeginAddress, const support::ulittle32_t UnwindData)
Definition: ARMWinEH.h:233
const support::ulittle32_t BeginAddress
Definition: ARMWinEH.h:227
uint32_t ExceptionInformationRVA() const
Definition: ARMWinEH.h:241
const support::ulittle32_t UnwindData
Definition: ARMWinEH.h:228
RuntimeFunctionFlag Flag() const
Definition: ARMWinEH.h:237
RuntimeFunction - An entry in the table of procedure data (.pdata)
Definition: ARMWinEH.h:104
const support::ulittle32_t UnwindData
Definition: ARMWinEH.h:107
RuntimeFunctionFlag Flag() const
Definition: ARMWinEH.h:116
uint32_t PackedUnwindData() const
Definition: ARMWinEH.h:126
RuntimeFunction(const support::ulittle32_t BeginAddress, const support::ulittle32_t UnwindData)
Definition: ARMWinEH.h:112
const support::ulittle32_t BeginAddress
Definition: ARMWinEH.h:106
uint32_t FunctionLength() const
Definition: ARMWinEH.h:132
RuntimeFunction(const support::ulittle32_t *Data)
Definition: ARMWinEH.h:109
uint16_t StackAdjust() const
Definition: ARMWinEH.h:179
uint32_t ExceptionInformationRVA() const
Definition: ARMWinEH.h:120
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
@ RFF_Reserved
packed entry representing a fragment
bool EpilogueFolding(const RuntimeFunction &RF)
Epilogue - pseudo-flag derived from Stack Adjust indicating that the epilogue has stack adjustment co...
Definition: ARMWinEH.h:194
LLVM_ABI std::pair< uint16_t, uint32_t > SavedRegisterMask(const RuntimeFunction &RF, bool Prologue=true)
SavedRegisterMask - Utility function to calculate the set of saved general purpose (r0-r15) and VFP (...
Definition: ARMWinEH.cpp:14
bool PrologueFolding(const RuntimeFunction &RF)
PrologueFolding - pseudo-flag derived from Stack Adjust indicating that the prologue has stack adjust...
Definition: ARMWinEH.h:189
uint16_t StackAdjustment(const RuntimeFunction &RF)
StackAdjustment - calculated stack adjustment in words.
Definition: ARMWinEH.h:200
size_t HeaderWords(const ExceptionDataRecord &XR)
Definition: ARMWinEH.h:513
@ RT_B
return via pop {pc} (L flag must be set)
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
@ Offset
Definition: DWP.cpp:477
@ WinEH
Windows Exception Handling.
ExceptionDataRecord - An entry in the table of exception data (.xdata)
Definition: ARMWinEH.h:397
const support::ulittle32_t ES
Definition: ARMWinEH.h:398
uint8_t ResAArch64() const
Definition: ARMWinEH.h:411
uint32_t EpilogueStartOffset() const
Definition: ARMWinEH.h:402
uint8_t EpilogueStartIndexARM() const
Definition: ARMWinEH.h:421
EpilogueScope(const support::ulittle32_t Data)
Definition: ARMWinEH.h:400
uint16_t EpilogueStartIndexAArch64() const
Definition: ARMWinEH.h:425
ArrayRef< uint8_t > UnwindByteCode() const
Definition: ARMWinEH.h:493
uint32_t FunctionLengthInBytesAArch64() const
Definition: ARMWinEH.h:448
uint32_t ExceptionHandlerRVA() const
Definition: ARMWinEH.h:501
const support::ulittle32_t * Data
Definition: ARMWinEH.h:434
ArrayRef< support::ulittle32_t > EpilogueScopes() const
Definition: ARMWinEH.h:487
uint32_t ExceptionHandlerParameter() const
Definition: ARMWinEH.h:506
uint32_t FunctionLengthInBytesARM() const
Definition: ARMWinEH.h:444
ExceptionDataRecord(const support::ulittle32_t *Data, bool isAArch64)
Definition: ARMWinEH.h:437