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