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/Compiler.h"
14 #include "llvm/Support/Endian.h"
15
16 namespace llvm {
17 namespace ARM {
18 namespace WinEH {
19 enum class RuntimeFunctionFlag {
20 RFF_Unpacked, /// unpacked entry
21 RFF_Packed, /// packed entry
22 RFF_PackedFragment, /// packed entry representing a fragment
23 RFF_Reserved, /// reserved
24 };
25
26 enum 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
104 class RuntimeFunction {
105 public:
106 const support::ulittle32_t BeginAddress;
107 const support::ulittle32_t UnwindData;
108
RuntimeFunction(const support::ulittle32_t * Data)109 RuntimeFunction(const support::ulittle32_t *Data)
110 : BeginAddress(Data[0]), UnwindData(Data[1]) {}
111
RuntimeFunction(const support::ulittle32_t BeginAddress,const support::ulittle32_t UnwindData)112 RuntimeFunction(const support::ulittle32_t BeginAddress,
113 const support::ulittle32_t UnwindData)
114 : BeginAddress(BeginAddress), UnwindData(UnwindData) {}
115
Flag()116 RuntimeFunctionFlag Flag() const {
117 return RuntimeFunctionFlag(UnwindData & 0x3);
118 }
119
ExceptionInformationRVA()120 uint32_t ExceptionInformationRVA() const {
121 assert(Flag() == RuntimeFunctionFlag::RFF_Unpacked &&
122 "unpacked form required for this operation");
123 return (UnwindData & ~0x3);
124 }
125
PackedUnwindData()126 uint32_t PackedUnwindData() const {
127 assert((Flag() == RuntimeFunctionFlag::RFF_Packed ||
128 Flag() == RuntimeFunctionFlag::RFF_PackedFragment) &&
129 "packed form required for this operation");
130 return (UnwindData & ~0x3);
131 }
FunctionLength()132 uint32_t FunctionLength() const {
133 assert((Flag() == RuntimeFunctionFlag::RFF_Packed ||
134 Flag() == RuntimeFunctionFlag::RFF_PackedFragment) &&
135 "packed form required for this operation");
136 return (((UnwindData & 0x00001ffc) >> 2) << 1);
137 }
Ret()138 ReturnType Ret() const {
139 assert((Flag() == RuntimeFunctionFlag::RFF_Packed ||
140 Flag() == RuntimeFunctionFlag::RFF_PackedFragment) &&
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 }
H()145 bool H() const {
146 assert((Flag() == RuntimeFunctionFlag::RFF_Packed ||
147 Flag() == RuntimeFunctionFlag::RFF_PackedFragment) &&
148 "packed form required for this operation");
149 return ((UnwindData & 0x00008000) >> 15);
150 }
Reg()151 uint8_t Reg() const {
152 assert((Flag() == RuntimeFunctionFlag::RFF_Packed ||
153 Flag() == RuntimeFunctionFlag::RFF_PackedFragment) &&
154 "packed form required for this operation");
155 return ((UnwindData & 0x00070000) >> 16);
156 }
R()157 bool R() const {
158 assert((Flag() == RuntimeFunctionFlag::RFF_Packed ||
159 Flag() == RuntimeFunctionFlag::RFF_PackedFragment) &&
160 "packed form required for this operation");
161 return ((UnwindData & 0x00080000) >> 19);
162 }
L()163 bool L() const {
164 assert((Flag() == RuntimeFunctionFlag::RFF_Packed ||
165 Flag() == RuntimeFunctionFlag::RFF_PackedFragment) &&
166 "packed form required for this operation");
167 return ((UnwindData & 0x00100000) >> 20);
168 }
C()169 bool C() const {
170 assert((Flag() == RuntimeFunctionFlag::RFF_Packed ||
171 Flag() == RuntimeFunctionFlag::RFF_PackedFragment) &&
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 }
StackAdjust()179 uint16_t StackAdjust() const {
180 assert((Flag() == RuntimeFunctionFlag::RFF_Packed ||
181 Flag() == RuntimeFunctionFlag::RFF_PackedFragment) &&
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
PrologueFolding(const RuntimeFunction & RF)189 inline 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
EpilogueFolding(const RuntimeFunction & RF)194 inline 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.
StackAdjustment(const RuntimeFunction & RF)200 inline uint16_t StackAdjustment(const RuntimeFunction &RF) {
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.
209 LLVM_ABI std::pair<uint16_t, uint32_t>
210 SavedRegisterMask(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
225 class RuntimeFunctionARM64 {
226 public:
227 const support::ulittle32_t BeginAddress;
228 const support::ulittle32_t UnwindData;
229
RuntimeFunctionARM64(const support::ulittle32_t * Data)230 RuntimeFunctionARM64(const support::ulittle32_t *Data)
231 : BeginAddress(Data[0]), UnwindData(Data[1]) {}
232
RuntimeFunctionARM64(const support::ulittle32_t BeginAddress,const support::ulittle32_t UnwindData)233 RuntimeFunctionARM64(const support::ulittle32_t BeginAddress,
234 const support::ulittle32_t UnwindData)
235 : BeginAddress(BeginAddress), UnwindData(UnwindData) {}
236
Flag()237 RuntimeFunctionFlag Flag() const {
238 return RuntimeFunctionFlag(UnwindData & 0x3);
239 }
240
ExceptionInformationRVA()241 uint32_t ExceptionInformationRVA() const {
242 assert(Flag() == RuntimeFunctionFlag::RFF_Unpacked &&
243 "unpacked form required for this operation");
244 return (UnwindData & ~0x3);
245 }
246
PackedUnwindData()247 uint32_t PackedUnwindData() const {
248 assert((Flag() == RuntimeFunctionFlag::RFF_Packed ||
249 Flag() == RuntimeFunctionFlag::RFF_PackedFragment) &&
250 "packed form required for this operation");
251 return (UnwindData & ~0x3);
252 }
FunctionLength()253 uint32_t FunctionLength() const {
254 assert((Flag() == RuntimeFunctionFlag::RFF_Packed ||
255 Flag() == RuntimeFunctionFlag::RFF_PackedFragment) &&
256 "packed form required for this operation");
257 return (((UnwindData & 0x00001ffc) >> 2) << 2);
258 }
RegF()259 uint8_t RegF() const {
260 assert((Flag() == RuntimeFunctionFlag::RFF_Packed ||
261 Flag() == RuntimeFunctionFlag::RFF_PackedFragment) &&
262 "packed form required for this operation");
263 return ((UnwindData & 0x0000e000) >> 13);
264 }
RegI()265 uint8_t RegI() const {
266 assert((Flag() == RuntimeFunctionFlag::RFF_Packed ||
267 Flag() == RuntimeFunctionFlag::RFF_PackedFragment) &&
268 "packed form required for this operation");
269 return ((UnwindData & 0x000f0000) >> 16);
270 }
H()271 bool H() const {
272 assert((Flag() == RuntimeFunctionFlag::RFF_Packed ||
273 Flag() == RuntimeFunctionFlag::RFF_PackedFragment) &&
274 "packed form required for this operation");
275 return ((UnwindData & 0x00100000) >> 20);
276 }
CR()277 uint8_t CR() const {
278 assert((Flag() == RuntimeFunctionFlag::RFF_Packed ||
279 Flag() == RuntimeFunctionFlag::RFF_PackedFragment) &&
280 "packed form required for this operation");
281 return ((UnwindData & 0x600000) >> 21);
282 }
FrameSize()283 uint16_t FrameSize() const {
284 assert((Flag() == RuntimeFunctionFlag::RFF_Packed ||
285 Flag() == RuntimeFunctionFlag::RFF_PackedFragment) &&
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
397 struct EpilogueScope {
398 const support::ulittle32_t ES;
399
EpilogueScopeEpilogueScope400 EpilogueScope(const support::ulittle32_t Data) : ES(Data) {}
401 // Same for both ARM and AArch64.
EpilogueStartOffsetEpilogueScope402 uint32_t EpilogueStartOffset() const {
403 return (ES & 0x0003ffff);
404 }
405
406 // Different implementations for ARM and AArch64.
ResARMEpilogueScope407 uint8_t ResARM() const {
408 return ((ES & 0x000c0000) >> 18);
409 }
410
ResAArch64EpilogueScope411 uint8_t ResAArch64() const {
412 return ((ES & 0x000f0000) >> 18);
413 }
414
415 // Condition is only applicable to ARM.
ConditionEpilogueScope416 uint8_t Condition() const {
417 return ((ES & 0x00f00000) >> 20);
418 }
419
420 // Different implementations for ARM and AArch64.
EpilogueStartIndexARMEpilogueScope421 uint8_t EpilogueStartIndexARM() const {
422 return ((ES & 0xff000000) >> 24);
423 }
424
EpilogueStartIndexAArch64EpilogueScope425 uint16_t EpilogueStartIndexAArch64() const {
426 return ((ES & 0xffc00000) >> 22);
427 }
428 };
429
430 struct ExceptionDataRecord;
431 inline size_t HeaderWords(const ExceptionDataRecord &XR);
432
433 struct ExceptionDataRecord {
434 const support::ulittle32_t *Data;
435 bool isAArch64;
436
ExceptionDataRecordExceptionDataRecord437 ExceptionDataRecord(const support::ulittle32_t *Data, bool isAArch64) :
438 Data(Data), isAArch64(isAArch64) {}
439
FunctionLengthExceptionDataRecord440 uint32_t FunctionLength() const {
441 return (Data[0] & 0x0003ffff);
442 }
443
FunctionLengthInBytesARMExceptionDataRecord444 uint32_t FunctionLengthInBytesARM() const {
445 return FunctionLength() << 1;
446 }
447
FunctionLengthInBytesAArch64ExceptionDataRecord448 uint32_t FunctionLengthInBytesAArch64() const {
449 return FunctionLength() << 2;
450 }
451
VersExceptionDataRecord452 uint8_t Vers() const {
453 return (Data[0] & 0x000C0000) >> 18;
454 }
455
XExceptionDataRecord456 bool X() const {
457 return ((Data[0] & 0x00100000) >> 20);
458 }
459
EExceptionDataRecord460 bool E() const {
461 return ((Data[0] & 0x00200000) >> 21);
462 }
463
FExceptionDataRecord464 bool F() const {
465 assert(!isAArch64 && "Fragments are only supported on ARMv7 WinEH");
466 return ((Data[0] & 0x00400000) >> 22);
467 }
468
EpilogueCountExceptionDataRecord469 uint16_t EpilogueCount() const {
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
CodeWordsExceptionDataRecord478 uint8_t CodeWords() const {
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
EpilogueScopesExceptionDataRecord487 ArrayRef<support::ulittle32_t> EpilogueScopes() const {
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
UnwindByteCodeExceptionDataRecord493 ArrayRef<uint8_t> UnwindByteCode() const {
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
ExceptionHandlerRVAExceptionDataRecord501 uint32_t ExceptionHandlerRVA() const {
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
ExceptionHandlerParameterExceptionDataRecord506 uint32_t ExceptionHandlerParameter() const {
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
HeaderWords(const ExceptionDataRecord & XR)513 inline 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
523