1 //===-- llvm/Support/WinARMEH.h - Windows on ARM EH Constants ---*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef LLVM_SUPPORT_ARMWINEH_H
11 #define LLVM_SUPPORT_ARMWINEH_H
13 #include "llvm/ADT/ArrayRef.h"
14 #include "llvm/Support/Endian.h"
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
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)
33 /// RuntimeFunction - An entry in the table of procedure data (.pdata)
35 /// 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
36 /// 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
37 /// +---------------------------------------------------------------+
38 /// | Function Start RVA |
39 /// +-------------------+-+-+-+-----+-+---+---------------------+---+
40 /// | Stack Adjust |C|L|R| Reg |H|Ret| Function Length |Flg|
41 /// +-------------------+-+-+-+-----+-+---+---------------------+---+
43 /// Flag : 2-bit field with the following meanings:
44 /// - 00 = packed unwind data not used; reamining bits point to .xdata record
45 /// - 01 = packed unwind data
46 /// - 10 = packed unwind data, function assumed to have no prologue; useful
47 /// for function fragments that are discontiguous with the start of the
50 /// Function Length : 11-bit field providing the length of the entire function
51 /// in bytes, divided by 2; if the function is greater than
52 /// 4KB, a full .xdata record must be used instead
53 /// Ret : 2-bit field indicating how the function returns
54 /// - 00 = return via pop {pc} (the L bit must be set)
55 /// - 01 = return via 16-bit branch
56 /// - 10 = return via 32-bit branch
57 /// - 11 = no epilogue; useful for function fragments that may only contain a
58 /// prologue but the epilogue is elsewhere
59 /// H : 1-bit flag indicating whether the function "homes" the integer parameter
60 /// registers (r0-r3), allocating 16-bytes on the stack
61 /// Reg : 3-bit field indicating the index of the last saved non-volatile
62 /// register. If the R bit is set to 0, then only integer registers are
63 /// saved (r4-rN, where N is 4 + Reg). If the R bit is set to 1, then
64 /// only floating-point registers are being saved (d8-dN, where N is
65 /// 8 + Reg). The special case of the R bit being set to 1 and Reg equal
66 /// to 7 indicates that no registers are saved.
67 /// R : 1-bit flag indicating whether the non-volatile registers are integer or
68 /// floating-point. 0 indicates integer, 1 indicates floating-point. The
69 /// special case of the R-flag being set and Reg being set to 7 indicates
70 /// that no non-volatile registers are saved.
71 /// L : 1-bit flag indicating whether the function saves/restores the link
73 /// C : 1-bit flag indicating whether the function includes extra instructions
74 /// to setup a frame chain for fast walking. If this flag is set, r11 is
75 /// implicitly added to the list of saved non-volatile integer registers.
76 /// Stack Adjust : 10-bit field indicating the number of bytes of stack that are
77 /// allocated for this function. Only values between 0x000 and
78 /// 0x3f3 can be directly encoded. If the value is 0x3f4 or
79 /// greater, then the low 4 bits have special meaning as follows:
81 /// indicate the number of words' of adjustment (1-4), minus 1
83 /// indicates if the prologue combined adjustment into push
85 /// indicates if the epilogue combined adjustment into pop
89 /// + L flag must be set since frame chaining requires r11 and lr
90 /// + r11 must NOT be included in the set of registers described by Reg
92 /// + L flag must be set
94 // NOTE: RuntimeFunction is meant to be a simple class that provides raw access
95 // to all fields in the structure. The accessor methods reflect the names of
96 // the bitfields that they correspond to. Although some obvious simplifications
97 // are possible via merging of methods, it would prevent the use of this class
98 // to fully inspect the contents of the data structure which is particularly
99 // useful for scenarios such as llvm-readobj to aid in testing.
101 class RuntimeFunction {
103 const support::ulittle32_t BeginAddress;
104 const support::ulittle32_t UnwindData;
106 RuntimeFunction(const support::ulittle32_t *Data)
107 : BeginAddress(Data[0]), UnwindData(Data[1]) {}
109 RuntimeFunction(const support::ulittle32_t BeginAddress,
110 const support::ulittle32_t UnwindData)
111 : BeginAddress(BeginAddress), UnwindData(UnwindData) {}
113 RuntimeFunctionFlag Flag() const {
114 return RuntimeFunctionFlag(UnwindData & 0x3);
117 uint32_t ExceptionInformationRVA() const {
118 assert(Flag() == RuntimeFunctionFlag::RFF_Unpacked &&
119 "unpacked form required for this operation");
120 return (UnwindData & ~0x3);
123 uint32_t PackedUnwindData() const {
124 assert((Flag() == RuntimeFunctionFlag::RFF_Packed ||
125 Flag() == RuntimeFunctionFlag::RFF_PackedFragment) &&
126 "packed form required for this operation");
127 return (UnwindData & ~0x3);
129 uint32_t FunctionLength() const {
130 assert((Flag() == RuntimeFunctionFlag::RFF_Packed ||
131 Flag() == RuntimeFunctionFlag::RFF_PackedFragment) &&
132 "packed form required for this operation");
133 return (((UnwindData & 0x00001ffc) >> 2) << 1);
135 ReturnType Ret() const {
136 assert((Flag() == RuntimeFunctionFlag::RFF_Packed ||
137 Flag() == RuntimeFunctionFlag::RFF_PackedFragment) &&
138 "packed form required for this operation");
139 assert(((UnwindData & 0x00006000) || L()) && "L must be set to 1");
140 return ReturnType((UnwindData & 0x00006000) >> 13);
143 assert((Flag() == RuntimeFunctionFlag::RFF_Packed ||
144 Flag() == RuntimeFunctionFlag::RFF_PackedFragment) &&
145 "packed form required for this operation");
146 return ((UnwindData & 0x00008000) >> 15);
148 uint8_t Reg() const {
149 assert((Flag() == RuntimeFunctionFlag::RFF_Packed ||
150 Flag() == RuntimeFunctionFlag::RFF_PackedFragment) &&
151 "packed form required for this operation");
152 return ((UnwindData & 0x00070000) >> 16);
155 assert((Flag() == RuntimeFunctionFlag::RFF_Packed ||
156 Flag() == RuntimeFunctionFlag::RFF_PackedFragment) &&
157 "packed form required for this operation");
158 return ((UnwindData & 0x00080000) >> 19);
161 assert((Flag() == RuntimeFunctionFlag::RFF_Packed ||
162 Flag() == RuntimeFunctionFlag::RFF_PackedFragment) &&
163 "packed form required for this operation");
164 return ((UnwindData & 0x00100000) >> 20);
167 assert((Flag() == RuntimeFunctionFlag::RFF_Packed ||
168 Flag() == RuntimeFunctionFlag::RFF_PackedFragment) &&
169 "packed form required for this operation");
170 assert(((~UnwindData & 0x00200000) || L()) &&
171 "L flag must be set, chaining requires r11 and LR");
172 assert(((~UnwindData & 0x00200000) || (Reg() < 7) || R()) &&
173 "r11 must not be included in Reg; C implies r11");
174 return ((UnwindData & 0x00200000) >> 21);
176 uint16_t StackAdjust() const {
177 assert((Flag() == RuntimeFunctionFlag::RFF_Packed ||
178 Flag() == RuntimeFunctionFlag::RFF_PackedFragment) &&
179 "packed form required for this operation");
180 return ((UnwindData & 0xffc00000) >> 22);
184 /// PrologueFolding - pseudo-flag derived from Stack Adjust indicating that the
185 /// prologue has stack adjustment combined into the push
186 inline bool PrologueFolding(const RuntimeFunction &RF) {
187 return RF.StackAdjust() >= 0x3f4 && (RF.StackAdjust() & 0x4);
189 /// Epilogue - pseudo-flag derived from Stack Adjust indicating that the
190 /// epilogue has stack adjustment combined into the pop
191 inline bool EpilogueFolding(const RuntimeFunction &RF) {
192 return RF.StackAdjust() >= 0x3f4 && (RF.StackAdjust() & 0x8);
194 /// StackAdjustment - calculated stack adjustment in words. The stack
195 /// adjustment should be determined via this function to account for the special
196 /// handling the special encoding when the value is >= 0x3f4.
197 inline uint16_t StackAdjustment(const RuntimeFunction &RF) {
198 uint16_t Adjustment = RF.StackAdjust();
199 if (Adjustment >= 0x3f4)
200 return (Adjustment & 0x3) ? ((Adjustment & 0x3) << 2) - 1 : 0;
204 /// SavedRegisterMask - Utility function to calculate the set of saved general
205 /// purpose (r0-r15) and VFP (d0-d31) registers.
206 std::pair<uint16_t, uint32_t> SavedRegisterMask(const RuntimeFunction &RF);
208 /// ExceptionDataRecord - An entry in the table of exception data (.xdata)
210 /// 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
211 /// 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
212 /// +-------+---------+-+-+-+---+-----------------------------------+
213 /// | C Wrd | Epi Cnt |F|E|X|Ver| Function Length |
214 /// +-------+--------+'-'-'-'---'---+-------------------------------+
215 /// | Reserved |Ex. Code Words| (Extended Epilogue Count) |
216 /// +-------+--------+--------------+-------------------------------+
218 /// Function Length : 18-bit field indicating the total length of the function
219 /// in bytes divided by 2. If a function is larger than
220 /// 512KB, then multiple pdata and xdata records must be used.
221 /// Vers : 2-bit field describing the version of the remaining structure. Only
222 /// version 0 is currently defined (values 1-3 are not permitted).
223 /// X : 1-bit field indicating the presence of exception data
224 /// E : 1-bit field indicating that the single epilogue is packed into the
226 /// F : 1-bit field indicating that the record describes a function fragment
227 /// (implies that no prologue is present, and prologue processing should be
229 /// Epilogue Count : 5-bit field that differs in meaning based on the E field.
231 /// If E is set, then this field specifies the index of the
232 /// first unwind code describing the (only) epilogue.
234 /// Otherwise, this field indicates the number of exception
235 /// scopes. If more than 31 scopes exist, then this field and
236 /// the Code Words field must both be set to 0 to indicate that
237 /// an extension word is required.
238 /// Code Words : 4-bit field that species the number of 32-bit words needed to
239 /// contain all the unwind codes. If more than 15 words (63 code
240 /// bytes) are required, then this field and the Epilogue Count
241 /// field must both be set to 0 to indicate that an extension word
243 /// Extended Epilogue Count, Extended Code Words :
244 /// Valid only if Epilog Count and Code Words are both
245 /// set to 0. Provides an 8-bit extended code word
246 /// count and 16-bits for epilogue count
248 /// 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
249 /// 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
250 /// +----------------+------+---+---+-------------------------------+
251 /// | Ep Start Idx | Cond |Res| Epilogue Start Offset |
252 /// +----------------+------+---+-----------------------------------+
254 /// If the E bit is unset in the header, the header is followed by a series of
255 /// epilogue scopes, which are sorted by their offset.
257 /// Epilogue Start Offset: 18-bit field encoding the offset of epilogue relative
258 /// to the start of the function in bytes divided by two
259 /// Res : 2-bit field reserved for future expansion (must be set to 0)
260 /// Condition : 4-bit field providing the condition under which the epilogue is
261 /// executed. Unconditional epilogues should set this field to 0xe.
262 /// Epilogues must be entirely conditional or unconditional, and in
263 /// Thumb-2 mode. The epilogue beings with the first instruction
264 /// after the IT opcode.
265 /// Epilogue Start Index : 8-bit field indicating the byte index of the first
266 /// unwind code describing the epilogue
268 /// 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
269 /// 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
270 /// +---------------+---------------+---------------+---------------+
271 /// | Unwind Code 3 | Unwind Code 2 | Unwind Code 1 | Unwind Code 0 |
272 /// +---------------+---------------+---------------+---------------+
274 /// Following the epilogue scopes, the byte code describing the unwinding
275 /// follows. This is padded to align up to word alignment. Bytes are stored in
278 /// 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
279 /// 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
280 /// +---------------------------------------------------------------+
281 /// | Exception Handler RVA (requires X = 1) |
282 /// +---------------------------------------------------------------+
283 /// | (possibly followed by data required for exception handler) |
284 /// +---------------------------------------------------------------+
286 /// If the X bit is set in the header, the unwind byte code is followed by the
287 /// exception handler information. This constants of one Exception Handler RVA
288 /// which is the address to the exception handler, followed immediately by the
289 /// variable length data associated with the exception handler.
292 struct EpilogueScope {
293 const support::ulittle32_t ES;
295 EpilogueScope(const support::ulittle32_t Data) : ES(Data) {}
296 uint32_t EpilogueStartOffset() const {
297 return (ES & 0x0003ffff);
299 uint8_t Res() const {
300 return ((ES & 0x000c0000) >> 18);
302 uint8_t Condition() const {
303 return ((ES & 0x00f00000) >> 20);
305 uint8_t EpilogueStartIndex() const {
306 return ((ES & 0xff000000) >> 24);
310 struct ExceptionDataRecord;
311 inline size_t HeaderWords(const ExceptionDataRecord &XR);
313 struct ExceptionDataRecord {
314 const support::ulittle32_t *Data;
316 ExceptionDataRecord(const support::ulittle32_t *Data) : Data(Data) {}
318 uint32_t FunctionLength() const {
319 return (Data[0] & 0x0003ffff);
322 uint8_t Vers() const {
323 return (Data[0] & 0x000C0000) >> 18;
327 return ((Data[0] & 0x00100000) >> 20);
331 return ((Data[0] & 0x00200000) >> 21);
335 return ((Data[0] & 0x00400000) >> 22);
338 uint8_t EpilogueCount() const {
339 if (HeaderWords(*this) == 1)
340 return (Data[0] & 0x0f800000) >> 23;
341 return Data[1] & 0x0000ffff;
344 uint8_t CodeWords() const {
345 if (HeaderWords(*this) == 1)
346 return (Data[0] & 0xf0000000) >> 28;
347 return (Data[1] & 0x00ff0000) >> 16;
350 ArrayRef<support::ulittle32_t> EpilogueScopes() const {
351 assert(E() == 0 && "epilogue scopes are only present when the E bit is 0");
352 size_t Offset = HeaderWords(*this);
353 return makeArrayRef(&Data[Offset], EpilogueCount());
356 ArrayRef<uint8_t> UnwindByteCode() const {
357 const size_t Offset = HeaderWords(*this)
358 + (E() ? 0 : EpilogueCount());
359 const uint8_t *ByteCode =
360 reinterpret_cast<const uint8_t *>(&Data[Offset]);
361 return makeArrayRef(ByteCode, CodeWords() * sizeof(uint32_t));
364 uint32_t ExceptionHandlerRVA() const {
365 assert(X() && "Exception Handler RVA is only valid if the X bit is set");
366 return Data[HeaderWords(*this) + EpilogueCount() + CodeWords()];
369 uint32_t ExceptionHandlerParameter() const {
370 assert(X() && "Exception Handler RVA is only valid if the X bit is set");
371 return Data[HeaderWords(*this) + EpilogueCount() + CodeWords() + 1];
375 inline size_t HeaderWords(const ExceptionDataRecord &XR) {
376 return (XR.Data[0] & 0xff800000) ? 1 : 2;