LLVM  8.0.1
ARMWinEH.h
Go to the documentation of this file.
1 //===-- llvm/Support/WinARMEH.h - Windows on ARM EH Constants ---*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #ifndef LLVM_SUPPORT_ARMWINEH_H
11 #define LLVM_SUPPORT_ARMWINEH_H
12 
13 #include "llvm/ADT/ArrayRef.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 /// 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 /// +-------------------+-+-+-+-----+-+---+---------------------+---+
42 ///
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
48 /// function
49 /// - 11 = reserved
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
72 /// register (LR)
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:
80 /// - Bit 0-1
81 /// indicate the number of words' of adjustment (1-4), minus 1
82 /// - Bit 2
83 /// indicates if the prologue combined adjustment into push
84 /// - Bit 3
85 /// indicates if the epilogue combined adjustment into pop
86 ///
87 /// RESTRICTIONS:
88 /// - IF C is SET:
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
91 /// - IF Ret is 0:
92 /// + L flag must be set
93 
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.
100 
102 public:
105 
107  : BeginAddress(Data[0]), UnwindData(Data[1]) {}
108 
110  const support::ulittle32_t UnwindData)
111  : BeginAddress(BeginAddress), UnwindData(UnwindData) {}
112 
114  return RuntimeFunctionFlag(UnwindData & 0x3);
115  }
116 
119  "unpacked form required for this operation");
120  return (UnwindData & ~0x3);
121  }
122 
126  "packed form required for this operation");
127  return (UnwindData & ~0x3);
128  }
132  "packed form required for this operation");
133  return (((UnwindData & 0x00001ffc) >> 2) << 1);
134  }
135  ReturnType Ret() const {
138  "packed form required for this operation");
139  assert(((UnwindData & 0x00006000) || L()) && "L must be set to 1");
140  return ReturnType((UnwindData & 0x00006000) >> 13);
141  }
142  bool H() const {
145  "packed form required for this operation");
146  return ((UnwindData & 0x00008000) >> 15);
147  }
148  uint8_t Reg() const {
151  "packed form required for this operation");
152  return ((UnwindData & 0x00070000) >> 16);
153  }
154  bool R() const {
157  "packed form required for this operation");
158  return ((UnwindData & 0x00080000) >> 19);
159  }
160  bool L() const {
163  "packed form required for this operation");
164  return ((UnwindData & 0x00100000) >> 20);
165  }
166  bool C() const {
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);
175  }
176  uint16_t StackAdjust() const {
179  "packed form required for this operation");
180  return ((UnwindData & 0xffc00000) >> 22);
181  }
182 };
183 
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);
188 }
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);
193 }
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;
201  return Adjustment;
202 }
203 
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);
207 
208 /// ExceptionDataRecord - An entry in the table of exception data (.xdata)
209 ///
210 /// The format on ARM is:
211 ///
212 /// 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
213 /// 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
214 /// +-------+---------+-+-+-+---+-----------------------------------+
215 /// | C Wrd | Epi Cnt |F|E|X|Ver| Function Length |
216 /// +-------+--------+'-'-'-'---'---+-------------------------------+
217 /// | Reserved |Ex. Code Words| (Extended Epilogue Count) |
218 /// +-------+--------+--------------+-------------------------------+
219 ///
220 /// The format on ARM64 is:
221 ///
222 /// 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
223 /// 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
224 /// +---------+---------+-+-+---+-----------------------------------+
225 /// | C Wrd | Epi Cnt |E|X|Ver| Function Length |
226 /// +---------+------+--'-'-'---'---+-------------------------------+
227 /// | Reserved |Ex. Code Words| (Extended Epilogue Count) |
228 /// +-------+--------+--------------+-------------------------------+
229 ///
230 /// Function Length : 18-bit field indicating the total length of the function
231 /// in bytes divided by 2. If a function is larger than
232 /// 512KB, then multiple pdata and xdata records must be used.
233 /// Vers : 2-bit field describing the version of the remaining structure. Only
234 /// version 0 is currently defined (values 1-3 are not permitted).
235 /// X : 1-bit field indicating the presence of exception data
236 /// E : 1-bit field indicating that the single epilogue is packed into the
237 /// header
238 /// F : 1-bit field indicating that the record describes a function fragment
239 /// (implies that no prologue is present, and prologue processing should be
240 /// skipped) (ARM only)
241 /// Epilogue Count : 5-bit field that differs in meaning based on the E field.
242 ///
243 /// If E is set, then this field specifies the index of the
244 /// first unwind code describing the (only) epilogue.
245 ///
246 /// Otherwise, this field indicates the number of exception
247 /// scopes. If more than 31 scopes exist, then this field and
248 /// the Code Words field must both be set to 0 to indicate that
249 /// an extension word is required.
250 /// Code Words : 4-bit (5-bit on ARM64) field that specifies the number of
251 /// 32-bit words needed to contain all the unwind codes. If more
252 /// than 15 words (31 words on ARM64) are required, then this field
253 /// and the Epilogue Count field must both be set to 0 to indicate
254 /// that an extension word is required.
255 /// Extended Epilogue Count, Extended Code Words :
256 /// Valid only if Epilog Count and Code Words are both
257 /// set to 0. Provides an 8-bit extended code word
258 /// count and 16-bits for epilogue count
259 ///
260 /// The epilogue scope format on ARM is:
261 ///
262 /// 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
263 /// 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
264 /// +----------------+------+---+---+-------------------------------+
265 /// | Ep Start Idx | Cond |Res| Epilogue Start Offset |
266 /// +----------------+------+---+-----------------------------------+
267 ///
268 /// The epilogue scope format on ARM64 is:
269 ///
270 /// 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
271 /// 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
272 /// +-------------------+-------+---+-------------------------------+
273 /// | Ep Start Idx | Res | Epilogue Start Offset |
274 /// +-------------------+-------+-----------------------------------+
275 ///
276 /// If the E bit is unset in the header, the header is followed by a series of
277 /// epilogue scopes, which are sorted by their offset.
278 ///
279 /// Epilogue Start Offset: 18-bit field encoding the offset of epilogue relative
280 /// to the start of the function in bytes divided by two
281 /// Res : 2-bit field reserved for future expansion (must be set to 0)
282 /// Condition : (ARM only) 4-bit field providing the condition under which the
283 /// epilogue is executed. Unconditional epilogues should set this
284 /// field to 0xe. Epilogues must be entirely conditional or
285 /// unconditional, and in Thumb-2 mode. The epilogue begins with
286 /// the first instruction after the IT opcode.
287 /// Epilogue Start Index : 8-bit field indicating the byte index of the first
288 /// unwind code describing the epilogue
289 ///
290 /// 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
291 /// 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
292 /// +---------------+---------------+---------------+---------------+
293 /// | Unwind Code 3 | Unwind Code 2 | Unwind Code 1 | Unwind Code 0 |
294 /// +---------------+---------------+---------------+---------------+
295 ///
296 /// Following the epilogue scopes, the byte code describing the unwinding
297 /// follows. This is padded to align up to word alignment. Bytes are stored in
298 /// little endian.
299 ///
300 /// 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
301 /// 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
302 /// +---------------------------------------------------------------+
303 /// | Exception Handler RVA (requires X = 1) |
304 /// +---------------------------------------------------------------+
305 /// | (possibly followed by data required for exception handler) |
306 /// +---------------------------------------------------------------+
307 ///
308 /// If the X bit is set in the header, the unwind byte code is followed by the
309 /// exception handler information. This constants of one Exception Handler RVA
310 /// which is the address to the exception handler, followed immediately by the
311 /// variable length data associated with the exception handler.
312 ///
313 
316 
317  EpilogueScope(const support::ulittle32_t Data) : ES(Data) {}
318  // Same for both ARM and AArch64.
320  return (ES & 0x0003ffff);
321  }
322 
323  // Different implementations for ARM and AArch64.
324  uint8_t ResARM() const {
325  return ((ES & 0x000c0000) >> 18);
326  }
327 
328  uint8_t ResAArch64() const {
329  return ((ES & 0x000f0000) >> 18);
330  }
331 
332  // Condition is only applicable to ARM.
333  uint8_t Condition() const {
334  return ((ES & 0x00f00000) >> 20);
335  }
336 
337  // Different implementations for ARM and AArch64.
338  uint8_t EpilogueStartIndexARM() const {
339  return ((ES & 0xff000000) >> 24);
340  }
341 
342  uint16_t EpilogueStartIndexAArch64() const {
343  return ((ES & 0xffc00000) >> 22);
344  }
345 };
346 
347 struct ExceptionDataRecord;
348 inline size_t HeaderWords(const ExceptionDataRecord &XR);
349 
352  bool isAArch64;
353 
354  ExceptionDataRecord(const support::ulittle32_t *Data, bool isAArch64) :
355  Data(Data), isAArch64(isAArch64) {}
356 
358  return (Data[0] & 0x0003ffff);
359  }
360 
362  return FunctionLength() << 1;
363  }
364 
366  return FunctionLength() << 2;
367  }
368 
369  uint8_t Vers() const {
370  return (Data[0] & 0x000C0000) >> 18;
371  }
372 
373  bool X() const {
374  return ((Data[0] & 0x00100000) >> 20);
375  }
376 
377  bool E() const {
378  return ((Data[0] & 0x00200000) >> 21);
379  }
380 
381  bool F() const {
382  assert(!isAArch64 && "Fragments are only supported on ARMv7 WinEH");
383  return ((Data[0] & 0x00400000) >> 22);
384  }
385 
386  uint8_t EpilogueCount() const {
387  if (HeaderWords(*this) == 1) {
388  if (isAArch64)
389  return (Data[0] & 0x07C00000) >> 22;
390  return (Data[0] & 0x0f800000) >> 23;
391  }
392  return Data[1] & 0x0000ffff;
393  }
394 
395  uint8_t CodeWords() const {
396  if (HeaderWords(*this) == 1) {
397  if (isAArch64)
398  return (Data[0] & 0xf8000000) >> 27;
399  return (Data[0] & 0xf0000000) >> 28;
400  }
401  return (Data[1] & 0x00ff0000) >> 16;
402  }
403 
405  assert(E() == 0 && "epilogue scopes are only present when the E bit is 0");
406  size_t Offset = HeaderWords(*this);
407  return makeArrayRef(&Data[Offset], EpilogueCount());
408  }
409 
411  const size_t Offset = HeaderWords(*this)
412  + (E() ? 0 : EpilogueCount());
413  const uint8_t *ByteCode =
414  reinterpret_cast<const uint8_t *>(&Data[Offset]);
415  return makeArrayRef(ByteCode, CodeWords() * sizeof(uint32_t));
416  }
417 
419  assert(X() && "Exception Handler RVA is only valid if the X bit is set");
420  return Data[HeaderWords(*this) + EpilogueCount() + CodeWords()];
421  }
422 
424  assert(X() && "Exception Handler RVA is only valid if the X bit is set");
425  return Data[HeaderWords(*this) + EpilogueCount() + CodeWords() + 1];
426  }
427 };
428 
429 inline size_t HeaderWords(const ExceptionDataRecord &XR) {
430  if (XR.isAArch64)
431  return (XR.Data[0] & 0xffc00000) ? 1 : 2;
432  return (XR.Data[0] & 0xff800000) ? 1 : 2;
433 }
434 }
435 }
436 }
437 
438 #endif
uint32_t FunctionLengthInBytesAArch64() const
Definition: ARMWinEH.h:365
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang", "erlang-compatible garbage collector")
This class represents lattice values for constants.
Definition: AllocatorList.h:24
uint8_t EpilogueStartIndexARM() const
Definition: ARMWinEH.h:338
unsigned Reg
const support::ulittle32_t ES
Definition: ARMWinEH.h:315
RuntimeFunctionFlag Flag() const
Definition: ARMWinEH.h:113
uint32_t ExceptionHandlerParameter() const
Definition: ARMWinEH.h:423
uint32_t ExceptionHandlerRVA() const
Definition: ARMWinEH.h:418
bool EpilogueFolding(const RuntimeFunction &RF)
Epilogue - pseudo-flag derived from Stack Adjust indicating that the epilogue has stack adjustment co...
Definition: ARMWinEH.h:191
ArrayRef< T > makeArrayRef(const T &OneElt)
Construct an ArrayRef from a single element.
Definition: ArrayRef.h:451
ExceptionDataRecord - An entry in the table of exception data (.xdata)
Definition: ARMWinEH.h:314
uint8_t ResAArch64() const
Definition: ARMWinEH.h:328
uint16_t StackAdjustment(const RuntimeFunction &RF)
StackAdjustment - calculated stack adjustment in words.
Definition: ARMWinEH.h:197
bool PrologueFolding(const RuntimeFunction &RF)
PrologueFolding - pseudo-flag derived from Stack Adjust indicating that the prologue has stack adjust...
Definition: ARMWinEH.h:186
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: APInt.h:33
RuntimeFunction(const support::ulittle32_t *Data)
Definition: ARMWinEH.h:106
const support::ulittle32_t * Data
Definition: ARMWinEH.h:351
RuntimeFunction(const support::ulittle32_t BeginAddress, const support::ulittle32_t UnwindData)
Definition: ARMWinEH.h:109
Flag
These should be considered private to the implementation of the MCInstrDesc class.
Definition: MCInstrDesc.h:118
uint32_t ExceptionInformationRVA() const
Definition: ARMWinEH.h:117
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
uint16_t EpilogueStartIndexAArch64() const
Definition: ARMWinEH.h:342
packed entry representing a fragment
RuntimeFunction - An entry in the table of procedure data (.pdata)
Definition: ARMWinEH.h:101
uint32_t FunctionLength() const
Definition: ARMWinEH.h:129
size_t HeaderWords(const ExceptionDataRecord &XR)
Definition: ARMWinEH.h:429
const support::ulittle32_t BeginAddress
Definition: ARMWinEH.h:103
uint32_t EpilogueStartOffset() const
Definition: ARMWinEH.h:319
ArrayRef< uint8_t > UnwindByteCode() const
Definition: ARMWinEH.h:410
EpilogueScope(const support::ulittle32_t Data)
Definition: ARMWinEH.h:317
const support::ulittle32_t UnwindData
Definition: ARMWinEH.h:104
uint32_t FunctionLengthInBytesARM() const
Definition: ARMWinEH.h:361
return via pop {pc} (L flag must be set)
ArrayRef< support::ulittle32_t > EpilogueScopes() const
Definition: ARMWinEH.h:404
ExceptionDataRecord(const support::ulittle32_t *Data, bool isAArch64)
Definition: ARMWinEH.h:354
std::pair< uint16_t, uint32_t > SavedRegisterMask(const RuntimeFunction &RF)
SavedRegisterMask - Utility function to calculate the set of saved general purpose (r0-r15) and VFP (...
Definition: ARMWinEH.cpp:16
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
uint16_t StackAdjust() const
Definition: ARMWinEH.h:176
uint32_t PackedUnwindData() const
Definition: ARMWinEH.h:123