1 //===-- ARMWinEHPrinter.cpp - Windows on ARM EH Data Printer ----*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 // Windows on ARM uses a series of serialised data structures (RuntimeFunction)
10 // to create a table of information for unwinding. In order to conserve space,
11 // there are two different ways that this data is represented.
13 // For functions with canonical forms for the prologue and epilogue, the data
14 // can be stored in a "packed" form. In this case, the data is packed into the
15 // RuntimeFunction's remaining 30-bits and can fully describe the entire frame.
17 // +---------------------------------------+
18 // | Function Entry Address |
19 // +---------------------------------------+
20 // | Packed Form Data |
21 // +---------------------------------------+
23 // This layout is parsed by Decoder::dumpPackedEntry. No unwind bytecode is
24 // associated with such a frame as they can be derived from the provided data.
25 // The decoder does not synthesize this data as it is unnecessary for the
26 // purposes of validation, with the synthesis being required only by a proper
29 // For functions that are large or do not match canonical forms, the data is
30 // split up into two portions, with the actual data residing in the "exception
31 // data" table (.xdata) with a reference to the entry from the "procedure data"
34 // The exception data contains information about the frame setup, all of the
35 // epilogue scopes (for functions for which there are multiple exit points) and
36 // the associated exception handler. Additionally, the entry contains byte-code
37 // describing how to unwind the function (c.f. Decoder::decodeOpcodes).
39 // +---------------------------------------+
40 // | Function Entry Address |
41 // +---------------------------------------+
42 // | Exception Data Entry Address |
43 // +---------------------------------------+
45 // This layout is parsed by Decoder::dumpUnpackedEntry. Such an entry must
46 // first resolve the exception data entry address. This structure
47 // (ExceptionDataRecord) has a variable sized header
48 // (c.f. ARM::WinEH::HeaderWords) and encodes most of the same information as
49 // the packed form. However, because this information is insufficient to
50 // synthesize the unwinding, there are associated unwinding bytecode which make
51 // up the bulk of the Decoder.
53 // The decoder itself is table-driven, using the first byte to determine the
54 // opcode and dispatching to the associated printing routine. The bytecode
55 // itself is a variable length instruction encoding that can fully describe the
56 // state of the stack and the necessary operations for unwinding to the
57 // beginning of the frame.
59 // The byte-code maintains a 1-1 instruction mapping, indicating both the width
60 // of the instruction (Thumb2 instructions are variable length, 16 or 32 bits
61 // wide) allowing the program to unwind from any point in the prologue, body, or
62 // epilogue of the function.
64 #include "ARMWinEHPrinter.h"
65 #include "llvm/ADT/STLExtras.h"
66 #include "llvm/ADT/StringExtras.h"
67 #include "llvm/Support/ARMWinEH.h"
68 #include "llvm/Support/Format.h"
71 using namespace llvm::object
;
72 using namespace llvm::support
;
75 raw_ostream
&operator<<(raw_ostream
&OS
, const ARM::WinEH::ReturnType
&RT
) {
77 case ARM::WinEH::ReturnType::RT_POP
:
80 case ARM::WinEH::ReturnType::RT_B
:
83 case ARM::WinEH::ReturnType::RT_BW
:
86 case ARM::WinEH::ReturnType::RT_NoEpilogue
:
87 OS
<< "(no epilogue)";
94 static std::string
formatSymbol(StringRef Name
, uint64_t Address
,
95 uint64_t Offset
= 0) {
97 raw_string_ostream
OS(Buffer
);
103 OS
<< format("+0x%" PRIX64
" (0x%" PRIX64
")", Offset
, Address
);
104 else if (!Name
.empty())
105 OS
<< format("(0x%" PRIX64
")", Address
);
107 OS
<< format("0x%" PRIX64
, Address
);
115 const size_t Decoder::PDataEntrySize
= sizeof(RuntimeFunction
);
117 // TODO name the uops more appropriately
118 const Decoder::RingEntry
Decoder::Ring
[] = {
119 { 0x80, 0x00, 1, &Decoder::opcode_0xxxxxxx
}, // UOP_STACK_FREE (16-bit)
120 { 0xc0, 0x80, 2, &Decoder::opcode_10Lxxxxx
}, // UOP_POP (32-bit)
121 { 0xf0, 0xc0, 1, &Decoder::opcode_1100xxxx
}, // UOP_STACK_SAVE (16-bit)
122 { 0xf8, 0xd0, 1, &Decoder::opcode_11010Lxx
}, // UOP_POP (16-bit)
123 { 0xf8, 0xd8, 1, &Decoder::opcode_11011Lxx
}, // UOP_POP (32-bit)
124 { 0xf8, 0xe0, 1, &Decoder::opcode_11100xxx
}, // UOP_VPOP (32-bit)
125 { 0xfc, 0xe8, 2, &Decoder::opcode_111010xx
}, // UOP_STACK_FREE (32-bit)
126 { 0xfe, 0xec, 2, &Decoder::opcode_1110110L
}, // UOP_POP (16-bit)
127 { 0xff, 0xee, 2, &Decoder::opcode_11101110
}, // UOP_MICROSOFT_SPECIFIC (16-bit)
128 // UOP_PUSH_MACHINE_FRAME
130 // UOP_PUSH_TRAP_FRAME
131 // UOP_REDZONE_RESTORE_LR
132 { 0xff, 0xef, 2, &Decoder::opcode_11101111
}, // UOP_LDRPC_POSTINC (32-bit)
133 { 0xff, 0xf5, 2, &Decoder::opcode_11110101
}, // UOP_VPOP (32-bit)
134 { 0xff, 0xf6, 2, &Decoder::opcode_11110110
}, // UOP_VPOP (32-bit)
135 { 0xff, 0xf7, 3, &Decoder::opcode_11110111
}, // UOP_STACK_RESTORE (16-bit)
136 { 0xff, 0xf8, 4, &Decoder::opcode_11111000
}, // UOP_STACK_RESTORE (16-bit)
137 { 0xff, 0xf9, 3, &Decoder::opcode_11111001
}, // UOP_STACK_RESTORE (32-bit)
138 { 0xff, 0xfa, 4, &Decoder::opcode_11111010
}, // UOP_STACK_RESTORE (32-bit)
139 { 0xff, 0xfb, 1, &Decoder::opcode_11111011
}, // UOP_NOP (16-bit)
140 { 0xff, 0xfc, 1, &Decoder::opcode_11111100
}, // UOP_NOP (32-bit)
141 { 0xff, 0xfd, 1, &Decoder::opcode_11111101
}, // UOP_NOP (16-bit) / END
142 { 0xff, 0xfe, 1, &Decoder::opcode_11111110
}, // UOP_NOP (32-bit) / END
143 { 0xff, 0xff, 1, &Decoder::opcode_11111111
}, // UOP_END
146 // Unwind opcodes for ARM64.
147 // https://docs.microsoft.com/en-us/cpp/build/arm64-exception-handling
148 const Decoder::RingEntry
Decoder::Ring64
[] = {
149 {0xe0, 0x00, 1, &Decoder::opcode_alloc_s
},
150 {0xe0, 0x20, 1, &Decoder::opcode_save_r19r20_x
},
151 {0xc0, 0x40, 1, &Decoder::opcode_save_fplr
},
152 {0xc0, 0x80, 1, &Decoder::opcode_save_fplr_x
},
153 {0xf8, 0xc0, 2, &Decoder::opcode_alloc_m
},
154 {0xfc, 0xc8, 2, &Decoder::opcode_save_regp
},
155 {0xfc, 0xcc, 2, &Decoder::opcode_save_regp_x
},
156 {0xfc, 0xd0, 2, &Decoder::opcode_save_reg
},
157 {0xfe, 0xd4, 2, &Decoder::opcode_save_reg_x
},
158 {0xfe, 0xd6, 2, &Decoder::opcode_save_lrpair
},
159 {0xfe, 0xd8, 2, &Decoder::opcode_save_fregp
},
160 {0xfe, 0xda, 2, &Decoder::opcode_save_fregp_x
},
161 {0xfe, 0xdc, 2, &Decoder::opcode_save_freg
},
162 {0xff, 0xde, 2, &Decoder::opcode_save_freg_x
},
163 {0xff, 0xe0, 4, &Decoder::opcode_alloc_l
},
164 {0xff, 0xe1, 1, &Decoder::opcode_setfp
},
165 {0xff, 0xe2, 2, &Decoder::opcode_addfp
},
166 {0xff, 0xe3, 1, &Decoder::opcode_nop
},
167 {0xff, 0xe4, 1, &Decoder::opcode_end
},
168 {0xff, 0xe5, 1, &Decoder::opcode_end_c
},
169 {0xff, 0xe6, 1, &Decoder::opcode_save_next
},
170 {0xff, 0xe7, 3, &Decoder::opcode_save_any_reg
},
171 {0xff, 0xe8, 1, &Decoder::opcode_trap_frame
},
172 {0xff, 0xe9, 1, &Decoder::opcode_machine_frame
},
173 {0xff, 0xea, 1, &Decoder::opcode_context
},
174 {0xff, 0xec, 1, &Decoder::opcode_clear_unwound_to_call
},
175 {0xff, 0xfc, 1, &Decoder::opcode_pac_sign_lr
},
178 static void printRange(raw_ostream
&OS
, ListSeparator
&LS
, unsigned First
,
179 unsigned Last
, char Letter
) {
181 OS
<< LS
<< Letter
<< First
;
183 OS
<< LS
<< Letter
<< First
<< "-" << Letter
<< Last
;
186 static void printRange(raw_ostream
&OS
, uint32_t Mask
, ListSeparator
&LS
,
187 unsigned Start
, unsigned End
, char Letter
) {
189 for (unsigned RI
= Start
; RI
<= End
; ++RI
) {
190 if (Mask
& (1 << RI
)) {
195 printRange(OS
, LS
, First
, RI
- 1, Letter
);
201 printRange(OS
, LS
, First
, End
, Letter
);
204 void Decoder::printGPRMask(uint16_t GPRMask
) {
207 printRange(OS
, GPRMask
, LS
, 0, 12, 'r');
208 if (GPRMask
& (1 << 14))
210 if (GPRMask
& (1 << 15))
215 void Decoder::printVFPMask(uint32_t VFPMask
) {
218 printRange(OS
, VFPMask
, LS
, 0, 31, 'd');
222 ErrorOr
<object::SectionRef
>
223 Decoder::getSectionContaining(const COFFObjectFile
&COFF
, uint64_t VA
) {
224 for (const auto &Section
: COFF
.sections()) {
225 uint64_t Address
= Section
.getAddress();
226 uint64_t Size
= Section
.getSize();
228 if (VA
>= Address
&& (VA
- Address
) <= Size
)
231 return inconvertibleErrorCode();
234 ErrorOr
<object::SymbolRef
> Decoder::getSymbol(const COFFObjectFile
&COFF
,
235 uint64_t VA
, bool FunctionOnly
) {
236 for (const auto &Symbol
: COFF
.symbols()) {
237 Expected
<SymbolRef::Type
> Type
= Symbol
.getType();
239 return errorToErrorCode(Type
.takeError());
240 if (FunctionOnly
&& *Type
!= SymbolRef::ST_Function
)
243 Expected
<uint64_t> Address
= Symbol
.getAddress();
245 return errorToErrorCode(Address
.takeError());
249 return inconvertibleErrorCode();
252 ErrorOr
<SymbolRef
> Decoder::getRelocatedSymbol(const COFFObjectFile
&,
253 const SectionRef
&Section
,
255 for (const auto &Relocation
: Section
.relocations()) {
256 uint64_t RelocationOffset
= Relocation
.getOffset();
257 if (RelocationOffset
== Offset
)
258 return *Relocation
.getSymbol();
260 return inconvertibleErrorCode();
263 SymbolRef
Decoder::getPreferredSymbol(const COFFObjectFile
&COFF
, SymbolRef Sym
,
264 uint64_t &SymbolOffset
) {
265 // The symbol resolved by getRelocatedSymbol can be any internal
266 // nondescriptive symbol; try to resolve a more descriptive one.
267 COFFSymbolRef CoffSym
= COFF
.getCOFFSymbol(Sym
);
268 if (CoffSym
.getStorageClass() != COFF::IMAGE_SYM_CLASS_LABEL
&&
269 CoffSym
.getSectionDefinition() == nullptr)
271 for (const auto &S
: COFF
.symbols()) {
272 COFFSymbolRef CS
= COFF
.getCOFFSymbol(S
);
273 if (CS
.getSectionNumber() == CoffSym
.getSectionNumber() &&
274 CS
.getValue() <= CoffSym
.getValue() + SymbolOffset
&&
275 CS
.getStorageClass() != COFF::IMAGE_SYM_CLASS_LABEL
&&
276 CS
.getSectionDefinition() == nullptr) {
277 uint32_t Offset
= CoffSym
.getValue() + SymbolOffset
- CS
.getValue();
278 if (Offset
<= SymbolOffset
) {
279 SymbolOffset
= Offset
;
282 if (CS
.isExternal() && SymbolOffset
== 0)
290 ErrorOr
<SymbolRef
> Decoder::getSymbolForLocation(
291 const COFFObjectFile
&COFF
, const SectionRef
&Section
,
292 uint64_t OffsetInSection
, uint64_t ImmediateOffset
, uint64_t &SymbolAddress
,
293 uint64_t &SymbolOffset
, bool FunctionOnly
) {
294 // Try to locate a relocation that points at the offset in the section
295 ErrorOr
<SymbolRef
> SymOrErr
=
296 getRelocatedSymbol(COFF
, Section
, OffsetInSection
);
298 // We found a relocation symbol; the immediate offset needs to be added
299 // to the symbol address.
300 SymbolOffset
= ImmediateOffset
;
302 Expected
<uint64_t> AddressOrErr
= SymOrErr
->getAddress();
305 llvm::raw_string_ostream
OS(Buf
);
306 logAllUnhandledErrors(AddressOrErr
.takeError(), OS
);
307 report_fatal_error(Twine(OS
.str()));
309 // We apply SymbolOffset here directly. We return it separately to allow
310 // the caller to print it as an offset on the symbol name.
311 SymbolAddress
= *AddressOrErr
+ SymbolOffset
;
313 if (FunctionOnly
) // Resolve label/section symbols into function names.
314 SymOrErr
= getPreferredSymbol(COFF
, *SymOrErr
, SymbolOffset
);
316 // No matching relocation found; operating on a linked image. Try to
317 // find a descriptive symbol if possible. The immediate offset contains
318 // the image relative address, and we shouldn't add any offset to the
320 SymbolAddress
= COFF
.getImageBase() + ImmediateOffset
;
322 SymOrErr
= getSymbol(COFF
, SymbolAddress
, FunctionOnly
);
327 bool Decoder::opcode_0xxxxxxx(const uint8_t *OC
, unsigned &Offset
,
328 unsigned Length
, bool Prologue
) {
329 uint8_t Imm
= OC
[Offset
] & 0x7f;
330 SW
.startLine() << format("0x%02x ; %s sp, #(%u * 4)\n",
332 static_cast<const char *>(Prologue
? "sub" : "add"),
338 bool Decoder::opcode_10Lxxxxx(const uint8_t *OC
, unsigned &Offset
,
339 unsigned Length
, bool Prologue
) {
340 unsigned Link
= (OC
[Offset
] & 0x20) >> 5;
341 uint16_t RegisterMask
= (Link
<< (Prologue
? 14 : 15))
342 | ((OC
[Offset
+ 0] & 0x1f) << 8)
343 | ((OC
[Offset
+ 1] & 0xff) << 0);
344 assert((~RegisterMask
& (1 << 13)) && "sp must not be set");
345 assert((~RegisterMask
& (1 << (Prologue
? 15 : 14))) && "pc must not be set");
347 SW
.startLine() << format("0x%02x 0x%02x ; %s.w ",
348 OC
[Offset
+ 0], OC
[Offset
+ 1],
349 Prologue
? "push" : "pop");
350 printGPRMask(RegisterMask
);
357 bool Decoder::opcode_1100xxxx(const uint8_t *OC
, unsigned &Offset
,
358 unsigned Length
, bool Prologue
) {
360 SW
.startLine() << format("0x%02x ; mov r%u, sp\n",
361 OC
[Offset
], OC
[Offset
] & 0xf);
363 SW
.startLine() << format("0x%02x ; mov sp, r%u\n",
364 OC
[Offset
], OC
[Offset
] & 0xf);
369 bool Decoder::opcode_11010Lxx(const uint8_t *OC
, unsigned &Offset
,
370 unsigned Length
, bool Prologue
) {
371 unsigned Link
= (OC
[Offset
] & 0x4) >> 2;
372 unsigned Count
= (OC
[Offset
] & 0x3);
374 uint16_t GPRMask
= (Link
<< (Prologue
? 14 : 15))
375 | (((1 << (Count
+ 1)) - 1) << 4);
377 SW
.startLine() << format("0x%02x ; %s ", OC
[Offset
],
378 Prologue
? "push" : "pop");
379 printGPRMask(GPRMask
);
386 bool Decoder::opcode_11011Lxx(const uint8_t *OC
, unsigned &Offset
,
387 unsigned Length
, bool Prologue
) {
388 unsigned Link
= (OC
[Offset
] & 0x4) >> 2;
389 unsigned Count
= (OC
[Offset
] & 0x3) + 4;
391 uint16_t GPRMask
= (Link
<< (Prologue
? 14 : 15))
392 | (((1 << (Count
+ 1)) - 1) << 4);
394 SW
.startLine() << format("0x%02x ; %s.w ", OC
[Offset
],
395 Prologue
? "push" : "pop");
396 printGPRMask(GPRMask
);
403 bool Decoder::opcode_11100xxx(const uint8_t *OC
, unsigned &Offset
,
404 unsigned Length
, bool Prologue
) {
405 unsigned High
= (OC
[Offset
] & 0x7);
406 uint32_t VFPMask
= (((1 << (High
+ 1)) - 1) << 8);
408 SW
.startLine() << format("0x%02x ; %s ", OC
[Offset
],
409 Prologue
? "vpush" : "vpop");
410 printVFPMask(VFPMask
);
417 bool Decoder::opcode_111010xx(const uint8_t *OC
, unsigned &Offset
,
418 unsigned Length
, bool Prologue
) {
419 uint16_t Imm
= ((OC
[Offset
+ 0] & 0x03) << 8) | ((OC
[Offset
+ 1] & 0xff) << 0);
421 SW
.startLine() << format("0x%02x 0x%02x ; %s.w sp, #(%u * 4)\n",
422 OC
[Offset
+ 0], OC
[Offset
+ 1],
423 static_cast<const char *>(Prologue
? "sub" : "add"),
430 bool Decoder::opcode_1110110L(const uint8_t *OC
, unsigned &Offset
,
431 unsigned Length
, bool Prologue
) {
432 uint16_t GPRMask
= ((OC
[Offset
+ 0] & 0x01) << (Prologue
? 14 : 15))
433 | ((OC
[Offset
+ 1] & 0xff) << 0);
435 SW
.startLine() << format("0x%02x 0x%02x ; %s ", OC
[Offset
+ 0],
436 OC
[Offset
+ 1], Prologue
? "push" : "pop");
437 printGPRMask(GPRMask
);
444 bool Decoder::opcode_11101110(const uint8_t *OC
, unsigned &Offset
,
445 unsigned Length
, bool Prologue
) {
446 assert(!Prologue
&& "may not be used in prologue");
448 if (OC
[Offset
+ 1] & 0xf0)
449 SW
.startLine() << format("0x%02x 0x%02x ; reserved\n",
450 OC
[Offset
+ 0], OC
[Offset
+ 1]);
453 << format("0x%02x 0x%02x ; microsoft-specific (type: %u)\n",
454 OC
[Offset
+ 0], OC
[Offset
+ 1], OC
[Offset
+ 1] & 0x0f);
460 bool Decoder::opcode_11101111(const uint8_t *OC
, unsigned &Offset
,
461 unsigned Length
, bool Prologue
) {
462 if (OC
[Offset
+ 1] & 0xf0)
463 SW
.startLine() << format("0x%02x 0x%02x ; reserved\n",
464 OC
[Offset
+ 0], OC
[Offset
+ 1]);
467 << format("0x%02x 0x%02x ; str.w lr, [sp, #-%u]!\n",
468 OC
[Offset
+ 0], OC
[Offset
+ 1], OC
[Offset
+ 1] << 2);
471 << format("0x%02x 0x%02x ; ldr.w lr, [sp], #%u\n",
472 OC
[Offset
+ 0], OC
[Offset
+ 1], OC
[Offset
+ 1] << 2);
478 bool Decoder::opcode_11110101(const uint8_t *OC
, unsigned &Offset
,
479 unsigned Length
, bool Prologue
) {
480 unsigned Start
= (OC
[Offset
+ 1] & 0xf0) >> 4;
481 unsigned End
= (OC
[Offset
+ 1] & 0x0f) >> 0;
482 uint32_t VFPMask
= ((1 << (End
+ 1 - Start
)) - 1) << Start
;
484 SW
.startLine() << format("0x%02x 0x%02x ; %s ", OC
[Offset
+ 0],
485 OC
[Offset
+ 1], Prologue
? "vpush" : "vpop");
486 printVFPMask(VFPMask
);
493 bool Decoder::opcode_11110110(const uint8_t *OC
, unsigned &Offset
,
494 unsigned Length
, bool Prologue
) {
495 unsigned Start
= (OC
[Offset
+ 1] & 0xf0) >> 4;
496 unsigned End
= (OC
[Offset
+ 1] & 0x0f) >> 0;
497 uint32_t VFPMask
= ((1 << (End
+ 1 - Start
)) - 1) << (16 + Start
);
499 SW
.startLine() << format("0x%02x 0x%02x ; %s ", OC
[Offset
+ 0],
500 OC
[Offset
+ 1], Prologue
? "vpush" : "vpop");
501 printVFPMask(VFPMask
);
508 bool Decoder::opcode_11110111(const uint8_t *OC
, unsigned &Offset
,
509 unsigned Length
, bool Prologue
) {
510 uint32_t Imm
= (OC
[Offset
+ 1] << 8) | (OC
[Offset
+ 2] << 0);
512 SW
.startLine() << format("0x%02x 0x%02x 0x%02x ; %s sp, sp, #(%u * 4)\n",
513 OC
[Offset
+ 0], OC
[Offset
+ 1], OC
[Offset
+ 2],
514 static_cast<const char *>(Prologue
? "sub" : "add"),
521 bool Decoder::opcode_11111000(const uint8_t *OC
, unsigned &Offset
,
522 unsigned Length
, bool Prologue
) {
523 uint32_t Imm
= (OC
[Offset
+ 1] << 16)
524 | (OC
[Offset
+ 2] << 8)
525 | (OC
[Offset
+ 3] << 0);
528 << format("0x%02x 0x%02x 0x%02x 0x%02x ; %s sp, sp, #(%u * 4)\n",
529 OC
[Offset
+ 0], OC
[Offset
+ 1], OC
[Offset
+ 2], OC
[Offset
+ 3],
530 static_cast<const char *>(Prologue
? "sub" : "add"), Imm
);
536 bool Decoder::opcode_11111001(const uint8_t *OC
, unsigned &Offset
,
537 unsigned Length
, bool Prologue
) {
538 uint32_t Imm
= (OC
[Offset
+ 1] << 8) | (OC
[Offset
+ 2] << 0);
541 << format("0x%02x 0x%02x 0x%02x ; %s.w sp, sp, #(%u * 4)\n",
542 OC
[Offset
+ 0], OC
[Offset
+ 1], OC
[Offset
+ 2],
543 static_cast<const char *>(Prologue
? "sub" : "add"), Imm
);
549 bool Decoder::opcode_11111010(const uint8_t *OC
, unsigned &Offset
,
550 unsigned Length
, bool Prologue
) {
551 uint32_t Imm
= (OC
[Offset
+ 1] << 16)
552 | (OC
[Offset
+ 2] << 8)
553 | (OC
[Offset
+ 3] << 0);
556 << format("0x%02x 0x%02x 0x%02x 0x%02x ; %s.w sp, sp, #(%u * 4)\n",
557 OC
[Offset
+ 0], OC
[Offset
+ 1], OC
[Offset
+ 2], OC
[Offset
+ 3],
558 static_cast<const char *>(Prologue
? "sub" : "add"), Imm
);
564 bool Decoder::opcode_11111011(const uint8_t *OC
, unsigned &Offset
,
565 unsigned Length
, bool Prologue
) {
566 SW
.startLine() << format("0x%02x ; nop\n", OC
[Offset
]);
571 bool Decoder::opcode_11111100(const uint8_t *OC
, unsigned &Offset
,
572 unsigned Length
, bool Prologue
) {
573 SW
.startLine() << format("0x%02x ; nop.w\n", OC
[Offset
]);
578 bool Decoder::opcode_11111101(const uint8_t *OC
, unsigned &Offset
,
579 unsigned Length
, bool Prologue
) {
580 SW
.startLine() << format("0x%02x ; bx <reg>\n", OC
[Offset
]);
585 bool Decoder::opcode_11111110(const uint8_t *OC
, unsigned &Offset
,
586 unsigned Length
, bool Prologue
) {
587 SW
.startLine() << format("0x%02x ; b.w <target>\n", OC
[Offset
]);
592 bool Decoder::opcode_11111111(const uint8_t *OC
, unsigned &Offset
,
593 unsigned Length
, bool Prologue
) {
598 // ARM64 unwind codes start here.
599 bool Decoder::opcode_alloc_s(const uint8_t *OC
, unsigned &Offset
,
600 unsigned Length
, bool Prologue
) {
601 uint32_t NumBytes
= (OC
[Offset
] & 0x1F) << 4;
602 SW
.startLine() << format("0x%02x ; %s sp, #%u\n", OC
[Offset
],
603 static_cast<const char *>(Prologue
? "sub" : "add"),
609 bool Decoder::opcode_save_r19r20_x(const uint8_t *OC
, unsigned &Offset
,
610 unsigned Length
, bool Prologue
) {
611 uint32_t Off
= (OC
[Offset
] & 0x1F) << 3;
613 SW
.startLine() << format(
614 "0x%02x ; stp x19, x20, [sp, #-%u]!\n", OC
[Offset
], Off
);
616 SW
.startLine() << format(
617 "0x%02x ; ldp x19, x20, [sp], #%u\n", OC
[Offset
], Off
);
622 bool Decoder::opcode_save_fplr(const uint8_t *OC
, unsigned &Offset
,
623 unsigned Length
, bool Prologue
) {
624 uint32_t Off
= (OC
[Offset
] & 0x3F) << 3;
625 SW
.startLine() << format(
626 "0x%02x ; %s x29, x30, [sp, #%u]\n", OC
[Offset
],
627 static_cast<const char *>(Prologue
? "stp" : "ldp"), Off
);
632 bool Decoder::opcode_save_fplr_x(const uint8_t *OC
, unsigned &Offset
,
633 unsigned Length
, bool Prologue
) {
634 uint32_t Off
= ((OC
[Offset
] & 0x3F) + 1) << 3;
636 SW
.startLine() << format(
637 "0x%02x ; stp x29, x30, [sp, #-%u]!\n", OC
[Offset
], Off
);
639 SW
.startLine() << format(
640 "0x%02x ; ldp x29, x30, [sp], #%u\n", OC
[Offset
], Off
);
645 bool Decoder::opcode_alloc_m(const uint8_t *OC
, unsigned &Offset
,
646 unsigned Length
, bool Prologue
) {
647 uint32_t NumBytes
= ((OC
[Offset
] & 0x07) << 8);
648 NumBytes
|= (OC
[Offset
+ 1] & 0xFF);
650 SW
.startLine() << format("0x%02x%02x ; %s sp, #%u\n",
651 OC
[Offset
], OC
[Offset
+ 1],
652 static_cast<const char *>(Prologue
? "sub" : "add"),
658 bool Decoder::opcode_save_regp(const uint8_t *OC
, unsigned &Offset
,
659 unsigned Length
, bool Prologue
) {
660 uint32_t Reg
= ((OC
[Offset
] & 0x03) << 8);
661 Reg
|= (OC
[Offset
+ 1] & 0xC0);
664 uint32_t Off
= (OC
[Offset
+ 1] & 0x3F) << 3;
665 SW
.startLine() << format(
666 "0x%02x%02x ; %s x%u, x%u, [sp, #%u]\n",
667 OC
[Offset
], OC
[Offset
+ 1],
668 static_cast<const char *>(Prologue
? "stp" : "ldp"), Reg
, Reg
+ 1, Off
);
673 bool Decoder::opcode_save_regp_x(const uint8_t *OC
, unsigned &Offset
,
674 unsigned Length
, bool Prologue
) {
675 uint32_t Reg
= ((OC
[Offset
] & 0x03) << 8);
676 Reg
|= (OC
[Offset
+ 1] & 0xC0);
679 uint32_t Off
= ((OC
[Offset
+ 1] & 0x3F) + 1) << 3;
681 SW
.startLine() << format(
682 "0x%02x%02x ; stp x%u, x%u, [sp, #-%u]!\n",
683 OC
[Offset
], OC
[Offset
+ 1], Reg
,
686 SW
.startLine() << format(
687 "0x%02x%02x ; ldp x%u, x%u, [sp], #%u\n",
688 OC
[Offset
], OC
[Offset
+ 1], Reg
,
694 bool Decoder::opcode_save_reg(const uint8_t *OC
, unsigned &Offset
,
695 unsigned Length
, bool Prologue
) {
696 uint32_t Reg
= (OC
[Offset
] & 0x03) << 8;
697 Reg
|= (OC
[Offset
+ 1] & 0xC0);
700 uint32_t Off
= (OC
[Offset
+ 1] & 0x3F) << 3;
701 SW
.startLine() << format("0x%02x%02x ; %s x%u, [sp, #%u]\n",
702 OC
[Offset
], OC
[Offset
+ 1],
703 static_cast<const char *>(Prologue
? "str" : "ldr"),
709 bool Decoder::opcode_save_reg_x(const uint8_t *OC
, unsigned &Offset
,
710 unsigned Length
, bool Prologue
) {
711 uint32_t Reg
= (OC
[Offset
] & 0x01) << 8;
712 Reg
|= (OC
[Offset
+ 1] & 0xE0);
715 uint32_t Off
= ((OC
[Offset
+ 1] & 0x1F) + 1) << 3;
717 SW
.startLine() << format("0x%02x%02x ; str x%u, [sp, #-%u]!\n",
718 OC
[Offset
], OC
[Offset
+ 1], Reg
, Off
);
720 SW
.startLine() << format("0x%02x%02x ; ldr x%u, [sp], #%u\n",
721 OC
[Offset
], OC
[Offset
+ 1], Reg
, Off
);
726 bool Decoder::opcode_save_lrpair(const uint8_t *OC
, unsigned &Offset
,
727 unsigned Length
, bool Prologue
) {
728 uint32_t Reg
= (OC
[Offset
] & 0x01) << 8;
729 Reg
|= (OC
[Offset
+ 1] & 0xC0);
733 uint32_t Off
= (OC
[Offset
+ 1] & 0x3F) << 3;
734 SW
.startLine() << format("0x%02x%02x ; %s x%u, lr, [sp, #%u]\n",
735 OC
[Offset
], OC
[Offset
+ 1],
736 static_cast<const char *>(Prologue
? "stp" : "ldp"),
742 bool Decoder::opcode_save_fregp(const uint8_t *OC
, unsigned &Offset
,
743 unsigned Length
, bool Prologue
) {
744 uint32_t Reg
= (OC
[Offset
] & 0x01) << 8;
745 Reg
|= (OC
[Offset
+ 1] & 0xC0);
748 uint32_t Off
= (OC
[Offset
+ 1] & 0x3F) << 3;
749 SW
.startLine() << format("0x%02x%02x ; %s d%u, d%u, [sp, #%u]\n",
750 OC
[Offset
], OC
[Offset
+ 1],
751 static_cast<const char *>(Prologue
? "stp" : "ldp"),
757 bool Decoder::opcode_save_fregp_x(const uint8_t *OC
, unsigned &Offset
,
758 unsigned Length
, bool Prologue
) {
759 uint32_t Reg
= (OC
[Offset
] & 0x01) << 8;
760 Reg
|= (OC
[Offset
+ 1] & 0xC0);
763 uint32_t Off
= ((OC
[Offset
+ 1] & 0x3F) + 1) << 3;
765 SW
.startLine() << format(
766 "0x%02x%02x ; stp d%u, d%u, [sp, #-%u]!\n", OC
[Offset
],
767 OC
[Offset
+ 1], Reg
, Reg
+ 1, Off
);
769 SW
.startLine() << format(
770 "0x%02x%02x ; ldp d%u, d%u, [sp], #%u\n", OC
[Offset
],
771 OC
[Offset
+ 1], Reg
, Reg
+ 1, Off
);
776 bool Decoder::opcode_save_freg(const uint8_t *OC
, unsigned &Offset
,
777 unsigned Length
, bool Prologue
) {
778 uint32_t Reg
= (OC
[Offset
] & 0x01) << 8;
779 Reg
|= (OC
[Offset
+ 1] & 0xC0);
782 uint32_t Off
= (OC
[Offset
+ 1] & 0x3F) << 3;
783 SW
.startLine() << format("0x%02x%02x ; %s d%u, [sp, #%u]\n",
784 OC
[Offset
], OC
[Offset
+ 1],
785 static_cast<const char *>(Prologue
? "str" : "ldr"),
791 bool Decoder::opcode_save_freg_x(const uint8_t *OC
, unsigned &Offset
,
792 unsigned Length
, bool Prologue
) {
793 uint32_t Reg
= ((OC
[Offset
+ 1] & 0xE0) >> 5) + 8;
794 uint32_t Off
= ((OC
[Offset
+ 1] & 0x1F) + 1) << 3;
796 SW
.startLine() << format(
797 "0x%02x%02x ; str d%u, [sp, #-%u]!\n", OC
[Offset
],
798 OC
[Offset
+ 1], Reg
, Off
);
800 SW
.startLine() << format(
801 "0x%02x%02x ; ldr d%u, [sp], #%u\n", OC
[Offset
],
802 OC
[Offset
+ 1], Reg
, Off
);
807 bool Decoder::opcode_alloc_l(const uint8_t *OC
, unsigned &Offset
,
808 unsigned Length
, bool Prologue
) {
810 (OC
[Offset
+ 1] << 16) | (OC
[Offset
+ 2] << 8) | (OC
[Offset
+ 3] << 0);
812 SW
.startLine() << format(
813 "0x%02x%02x%02x%02x ; %s sp, #%u\n", OC
[Offset
], OC
[Offset
+ 1],
814 OC
[Offset
+ 2], OC
[Offset
+ 3],
815 static_cast<const char *>(Prologue
? "sub" : "add"), Off
);
820 bool Decoder::opcode_setfp(const uint8_t *OC
, unsigned &Offset
, unsigned Length
,
822 SW
.startLine() << format("0x%02x ; mov %s, %s\n", OC
[Offset
],
823 static_cast<const char *>(Prologue
? "fp" : "sp"),
824 static_cast<const char *>(Prologue
? "sp" : "fp"));
829 bool Decoder::opcode_addfp(const uint8_t *OC
, unsigned &Offset
, unsigned Length
,
831 unsigned NumBytes
= OC
[Offset
+ 1] << 3;
832 SW
.startLine() << format(
833 "0x%02x%02x ; %s %s, %s, #%u\n", OC
[Offset
], OC
[Offset
+ 1],
834 static_cast<const char *>(Prologue
? "add" : "sub"),
835 static_cast<const char *>(Prologue
? "fp" : "sp"),
836 static_cast<const char *>(Prologue
? "sp" : "fp"), NumBytes
);
841 bool Decoder::opcode_nop(const uint8_t *OC
, unsigned &Offset
, unsigned Length
,
843 SW
.startLine() << format("0x%02x ; nop\n", OC
[Offset
]);
848 bool Decoder::opcode_end(const uint8_t *OC
, unsigned &Offset
, unsigned Length
,
850 SW
.startLine() << format("0x%02x ; end\n", OC
[Offset
]);
855 bool Decoder::opcode_end_c(const uint8_t *OC
, unsigned &Offset
, unsigned Length
,
857 SW
.startLine() << format("0x%02x ; end_c\n", OC
[Offset
]);
862 bool Decoder::opcode_save_next(const uint8_t *OC
, unsigned &Offset
,
863 unsigned Length
, bool Prologue
) {
865 SW
.startLine() << format("0x%02x ; save next\n", OC
[Offset
]);
867 SW
.startLine() << format("0x%02x ; restore next\n",
873 bool Decoder::opcode_save_any_reg(const uint8_t *OC
, unsigned &Offset
,
874 unsigned Length
, bool Prologue
) {
875 // Whether the instruction has writeback
876 bool Writeback
= (OC
[Offset
+ 1] & 0x20) == 0x20;
877 // Whether the instruction is paired. (Paired instructions are required
878 // to save/restore adjacent registers.)
879 bool Paired
= (OC
[Offset
+ 1] & 0x40) == 0x40;
880 // The kind of register saved:
881 // - 0 is an x register
882 // - 1 is the low half of a q register
883 // - 2 is a whole q register
884 int RegKind
= (OC
[Offset
+ 2] & 0xC0) >> 6;
885 // Encoded register name (0 -> x0/q0, 1 -> x1/q1, etc.)
886 int Reg
= OC
[Offset
+ 1] & 0x1F;
887 // Encoded stack offset of load/store instruction; decoding varies by mode.
888 int StackOffset
= OC
[Offset
+ 2] & 0x3F;
891 if (!Writeback
&& !Paired
&& RegKind
!= 2)
896 SW
.startLine() << format("0x%02x%02x%02x ; ", OC
[Offset
],
897 OC
[Offset
+ 1], OC
[Offset
+ 2]);
899 // Verify the encoding is in a form we understand. The high bit of the first
900 // byte, and mode 3 for the register kind are apparently reserved. The
901 // encoded register must refer to a valid register.
907 if ((OC
[Offset
+ 1] & 0x80) == 0x80 || RegKind
== 3 || Reg
> MaxReg
) {
908 SW
.getOStream() << "invalid save_any_reg encoding\n";
915 SW
.getOStream() << "stp ";
917 SW
.getOStream() << "ldp ";
920 SW
.getOStream() << "str ";
922 SW
.getOStream() << "ldr ";
928 } else if (RegKind
== 2) {
933 SW
.getOStream() << format("%c%d, %c%d, ", RegChar
, Reg
, RegChar
, Reg
+ 1);
935 SW
.getOStream() << format("%c%d, ", RegChar
, Reg
);
939 SW
.getOStream() << format("[sp, #-%d]!\n", StackOffset
);
941 SW
.getOStream() << format("[sp], #%d\n", StackOffset
);
943 SW
.getOStream() << format("[sp, #%d]\n", StackOffset
);
950 bool Decoder::opcode_trap_frame(const uint8_t *OC
, unsigned &Offset
,
951 unsigned Length
, bool Prologue
) {
952 SW
.startLine() << format("0x%02x ; trap frame\n", OC
[Offset
]);
957 bool Decoder::opcode_machine_frame(const uint8_t *OC
, unsigned &Offset
,
958 unsigned Length
, bool Prologue
) {
959 SW
.startLine() << format("0x%02x ; machine frame\n",
965 bool Decoder::opcode_context(const uint8_t *OC
, unsigned &Offset
,
966 unsigned Length
, bool Prologue
) {
967 SW
.startLine() << format("0x%02x ; context\n", OC
[Offset
]);
972 bool Decoder::opcode_clear_unwound_to_call(const uint8_t *OC
, unsigned &Offset
,
973 unsigned Length
, bool Prologue
) {
974 SW
.startLine() << format("0x%02x ; clear unwound to call\n",
980 bool Decoder::opcode_pac_sign_lr(const uint8_t *OC
, unsigned &Offset
,
981 unsigned Length
, bool Prologue
) {
983 SW
.startLine() << format("0x%02x ; pacibsp\n", OC
[Offset
]);
985 SW
.startLine() << format("0x%02x ; autibsp\n", OC
[Offset
]);
990 void Decoder::decodeOpcodes(ArrayRef
<uint8_t> Opcodes
, unsigned Offset
,
992 assert((!Prologue
|| Offset
== 0) && "prologue should always use offset 0");
993 const RingEntry
* DecodeRing
= isAArch64
? Ring64
: Ring
;
994 bool Terminated
= false;
995 for (unsigned OI
= Offset
, OE
= Opcodes
.size(); !Terminated
&& OI
< OE
; ) {
996 for (unsigned DI
= 0;; ++DI
) {
997 if ((isAArch64
&& (DI
>= std::size(Ring64
))) ||
998 (!isAArch64
&& (DI
>= std::size(Ring
)))) {
999 SW
.startLine() << format("0x%02x ; Bad opcode!\n",
1000 Opcodes
.data()[OI
]);
1005 if ((Opcodes
[OI
] & DecodeRing
[DI
].Mask
) == DecodeRing
[DI
].Value
) {
1006 if (OI
+ DecodeRing
[DI
].Length
> OE
) {
1007 SW
.startLine() << format("Opcode 0x%02x goes past the unwind data\n",
1009 OI
+= DecodeRing
[DI
].Length
;
1013 (this->*DecodeRing
[DI
].Routine
)(Opcodes
.data(), OI
, 0, Prologue
);
1020 bool Decoder::dumpXDataRecord(const COFFObjectFile
&COFF
,
1021 const SectionRef
&Section
,
1022 uint64_t FunctionAddress
, uint64_t VA
) {
1023 ArrayRef
<uint8_t> Contents
;
1024 if (COFF
.getSectionContents(COFF
.getCOFFSection(Section
), Contents
))
1027 uint64_t SectionVA
= Section
.getAddress();
1028 uint64_t Offset
= VA
- SectionVA
;
1029 const ulittle32_t
*Data
=
1030 reinterpret_cast<const ulittle32_t
*>(Contents
.data() + Offset
);
1032 // Sanity check to ensure that the .xdata header is present.
1033 // A header is one or two words, followed by at least one word to describe
1034 // the unwind codes. Applicable to both ARM and AArch64.
1035 if (Contents
.size() - Offset
< 8)
1036 report_fatal_error(".xdata must be at least 8 bytes in size");
1038 const ExceptionDataRecord
XData(Data
, isAArch64
);
1039 DictScope
XRS(SW
, "ExceptionData");
1040 SW
.printNumber("FunctionLength",
1041 isAArch64
? XData
.FunctionLengthInBytesAArch64() :
1042 XData
.FunctionLengthInBytesARM());
1043 SW
.printNumber("Version", XData
.Vers());
1044 SW
.printBoolean("ExceptionData", XData
.X());
1045 SW
.printBoolean("EpiloguePacked", XData
.E());
1047 SW
.printBoolean("Fragment", XData
.F());
1048 SW
.printNumber(XData
.E() ? "EpilogueOffset" : "EpilogueScopes",
1049 XData
.EpilogueCount());
1050 uint64_t ByteCodeLength
= XData
.CodeWords() * sizeof(uint32_t);
1051 SW
.printNumber("ByteCodeLength", ByteCodeLength
);
1053 if ((int64_t)(Contents
.size() - Offset
- 4 * HeaderWords(XData
) -
1054 (XData
.E() ? 0 : XData
.EpilogueCount() * 4) -
1055 (XData
.X() ? 8 : 0)) < (int64_t)ByteCodeLength
) {
1057 report_fatal_error("Malformed unwind data");
1061 ArrayRef
<uint8_t> UC
= XData
.UnwindByteCode();
1063 ListScope
PS(SW
, "Prologue");
1064 decodeOpcodes(UC
, 0, /*Prologue=*/true);
1066 if (XData
.EpilogueCount()) {
1067 ListScope
ES(SW
, "Epilogue");
1068 decodeOpcodes(UC
, XData
.EpilogueCount(), /*Prologue=*/false);
1072 ListScope
PS(SW
, "Prologue");
1073 decodeOpcodes(XData
.UnwindByteCode(), 0, /*Prologue=*/true);
1075 ArrayRef
<ulittle32_t
> EpilogueScopes
= XData
.EpilogueScopes();
1076 ListScope
ESS(SW
, "EpilogueScopes");
1077 for (const EpilogueScope ES
: EpilogueScopes
) {
1078 DictScope
ESES(SW
, "EpilogueScope");
1079 SW
.printNumber("StartOffset", ES
.EpilogueStartOffset());
1081 SW
.printNumber("Condition", ES
.Condition());
1082 SW
.printNumber("EpilogueStartIndex",
1083 isAArch64
? ES
.EpilogueStartIndexAArch64()
1084 : ES
.EpilogueStartIndexARM());
1085 unsigned ReservedMask
= isAArch64
? 0xF : 0x3;
1086 if ((ES
.ES
>> 18) & ReservedMask
)
1087 SW
.printNumber("ReservedBits", (ES
.ES
>> 18) & ReservedMask
);
1089 ListScope
Opcodes(SW
, "Opcodes");
1090 decodeOpcodes(XData
.UnwindByteCode(),
1091 isAArch64
? ES
.EpilogueStartIndexAArch64()
1092 : ES
.EpilogueStartIndexARM(),
1093 /*Prologue=*/false);
1098 const uint32_t Parameter
= XData
.ExceptionHandlerParameter();
1099 const size_t HandlerOffset
= HeaderWords(XData
) +
1100 (XData
.E() ? 0 : XData
.EpilogueCount()) +
1103 uint64_t Address
, SymbolOffset
;
1104 ErrorOr
<SymbolRef
> Symbol
= getSymbolForLocation(
1105 COFF
, Section
, Offset
+ HandlerOffset
* sizeof(uint32_t),
1106 XData
.ExceptionHandlerRVA(), Address
, SymbolOffset
,
1107 /*FunctionOnly=*/true);
1109 ListScope
EHS(SW
, "ExceptionHandler");
1110 SW
.printHex("Routine", Address
);
1111 SW
.printHex("Parameter", Parameter
);
1115 Expected
<StringRef
> Name
= Symbol
->getName();
1118 llvm::raw_string_ostream
OS(Buf
);
1119 logAllUnhandledErrors(Name
.takeError(), OS
);
1120 report_fatal_error(Twine(OS
.str()));
1123 ListScope
EHS(SW
, "ExceptionHandler");
1124 SW
.printString("Routine", formatSymbol(*Name
, Address
, SymbolOffset
));
1125 SW
.printHex("Parameter", Parameter
);
1131 bool Decoder::dumpUnpackedEntry(const COFFObjectFile
&COFF
,
1132 const SectionRef Section
, uint64_t Offset
,
1133 unsigned Index
, const RuntimeFunction
&RF
) {
1134 assert(RF
.Flag() == RuntimeFunctionFlag::RFF_Unpacked
&&
1135 "packed entry cannot be treated as an unpacked entry");
1137 uint64_t FunctionAddress
, FunctionOffset
;
1138 ErrorOr
<SymbolRef
> Function
= getSymbolForLocation(
1139 COFF
, Section
, Offset
, RF
.BeginAddress
, FunctionAddress
, FunctionOffset
,
1140 /*FunctionOnly=*/true);
1142 uint64_t XDataAddress
, XDataOffset
;
1143 ErrorOr
<SymbolRef
> XDataRecord
= getSymbolForLocation(
1144 COFF
, Section
, Offset
+ 4, RF
.ExceptionInformationRVA(), XDataAddress
,
1147 if (!RF
.BeginAddress
&& !Function
)
1149 if (!RF
.UnwindData
&& !XDataRecord
)
1152 StringRef FunctionName
;
1154 Expected
<StringRef
> FunctionNameOrErr
= Function
->getName();
1155 if (!FunctionNameOrErr
) {
1157 llvm::raw_string_ostream
OS(Buf
);
1158 logAllUnhandledErrors(FunctionNameOrErr
.takeError(), OS
);
1159 report_fatal_error(Twine(OS
.str()));
1161 FunctionName
= *FunctionNameOrErr
;
1164 SW
.printString("Function",
1165 formatSymbol(FunctionName
, FunctionAddress
, FunctionOffset
));
1168 Expected
<StringRef
> Name
= XDataRecord
->getName();
1171 llvm::raw_string_ostream
OS(Buf
);
1172 logAllUnhandledErrors(Name
.takeError(), OS
);
1173 report_fatal_error(Twine(OS
.str()));
1176 SW
.printString("ExceptionRecord",
1177 formatSymbol(*Name
, XDataAddress
, XDataOffset
));
1179 Expected
<section_iterator
> SIOrErr
= XDataRecord
->getSection();
1181 // TODO: Actually report errors helpfully.
1182 consumeError(SIOrErr
.takeError());
1185 section_iterator SI
= *SIOrErr
;
1187 return dumpXDataRecord(COFF
, *SI
, FunctionAddress
, XDataAddress
);
1189 SW
.printString("ExceptionRecord", formatSymbol("", XDataAddress
));
1191 ErrorOr
<SectionRef
> Section
= getSectionContaining(COFF
, XDataAddress
);
1195 return dumpXDataRecord(COFF
, *Section
, FunctionAddress
, XDataAddress
);
1199 bool Decoder::dumpPackedEntry(const object::COFFObjectFile
&COFF
,
1200 const SectionRef Section
, uint64_t Offset
,
1201 unsigned Index
, const RuntimeFunction
&RF
) {
1202 assert((RF
.Flag() == RuntimeFunctionFlag::RFF_Packed
||
1203 RF
.Flag() == RuntimeFunctionFlag::RFF_PackedFragment
) &&
1204 "unpacked entry cannot be treated as a packed entry");
1206 uint64_t FunctionAddress
, FunctionOffset
;
1207 ErrorOr
<SymbolRef
> Function
= getSymbolForLocation(
1208 COFF
, Section
, Offset
, RF
.BeginAddress
, FunctionAddress
, FunctionOffset
,
1209 /*FunctionOnly=*/true);
1211 StringRef FunctionName
;
1213 Expected
<StringRef
> FunctionNameOrErr
= Function
->getName();
1214 if (!FunctionNameOrErr
) {
1216 llvm::raw_string_ostream
OS(Buf
);
1217 logAllUnhandledErrors(FunctionNameOrErr
.takeError(), OS
);
1218 report_fatal_error(Twine(OS
.str()));
1220 FunctionName
= *FunctionNameOrErr
;
1223 SW
.printString("Function",
1224 formatSymbol(FunctionName
, FunctionAddress
, FunctionOffset
));
1225 SW
.printBoolean("Fragment",
1226 RF
.Flag() == RuntimeFunctionFlag::RFF_PackedFragment
);
1227 SW
.printNumber("FunctionLength", RF
.FunctionLength());
1228 SW
.startLine() << "ReturnType: " << RF
.Ret() << '\n';
1229 SW
.printBoolean("HomedParameters", RF
.H());
1230 SW
.printNumber("Reg", RF
.Reg());
1231 SW
.printNumber("R", RF
.R());
1232 SW
.printBoolean("LinkRegister", RF
.L());
1233 SW
.printBoolean("Chaining", RF
.C());
1234 SW
.printNumber("StackAdjustment", StackAdjustment(RF
) << 2);
1237 ListScope
PS(SW
, "Prologue");
1239 uint16_t GPRMask
, VFPMask
;
1240 std::tie(GPRMask
, VFPMask
) = SavedRegisterMask(RF
, /*Prologue=*/true);
1242 if (StackAdjustment(RF
) && !PrologueFolding(RF
))
1243 SW
.startLine() << "sub sp, sp, #" << StackAdjustment(RF
) * 4 << "\n";
1245 SW
.startLine() << "vpush ";
1246 printVFPMask(VFPMask
);
1250 // Count the number of registers pushed below R11
1251 int FpOffset
= 4 * llvm::popcount(GPRMask
& ((1U << 11) - 1));
1253 SW
.startLine() << "add.w r11, sp, #" << FpOffset
<< "\n";
1255 SW
.startLine() << "mov r11, sp\n";
1258 SW
.startLine() << "push ";
1259 printGPRMask(GPRMask
);
1263 SW
.startLine() << "push {r0-r3}\n";
1266 if (RF
.Ret() != ReturnType::RT_NoEpilogue
) {
1267 ListScope
PS(SW
, "Epilogue");
1269 uint16_t GPRMask
, VFPMask
;
1270 std::tie(GPRMask
, VFPMask
) = SavedRegisterMask(RF
, /*Prologue=*/false);
1272 if (StackAdjustment(RF
) && !EpilogueFolding(RF
))
1273 SW
.startLine() << "add sp, sp, #" << StackAdjustment(RF
) * 4 << "\n";
1275 SW
.startLine() << "vpop ";
1276 printVFPMask(VFPMask
);
1280 SW
.startLine() << "pop ";
1281 printGPRMask(GPRMask
);
1285 if (RF
.L() == 0 || RF
.Ret() != ReturnType::RT_POP
)
1286 SW
.startLine() << "add sp, sp, #16\n";
1288 SW
.startLine() << "ldr pc, [sp], #20\n";
1290 if (RF
.Ret() != ReturnType::RT_POP
)
1291 SW
.startLine() << RF
.Ret() << '\n';
1297 bool Decoder::dumpPackedARM64Entry(const object::COFFObjectFile
&COFF
,
1298 const SectionRef Section
, uint64_t Offset
,
1300 const RuntimeFunctionARM64
&RF
) {
1301 assert((RF
.Flag() == RuntimeFunctionFlag::RFF_Packed
||
1302 RF
.Flag() == RuntimeFunctionFlag::RFF_PackedFragment
) &&
1303 "unpacked entry cannot be treated as a packed entry");
1305 uint64_t FunctionAddress
, FunctionOffset
;
1306 ErrorOr
<SymbolRef
> Function
= getSymbolForLocation(
1307 COFF
, Section
, Offset
, RF
.BeginAddress
, FunctionAddress
, FunctionOffset
,
1308 /*FunctionOnly=*/true);
1310 StringRef FunctionName
;
1312 Expected
<StringRef
> FunctionNameOrErr
= Function
->getName();
1313 if (!FunctionNameOrErr
) {
1315 llvm::raw_string_ostream
OS(Buf
);
1316 logAllUnhandledErrors(FunctionNameOrErr
.takeError(), OS
);
1317 report_fatal_error(Twine(OS
.str()));
1319 FunctionName
= *FunctionNameOrErr
;
1322 SW
.printString("Function",
1323 formatSymbol(FunctionName
, FunctionAddress
, FunctionOffset
));
1324 SW
.printBoolean("Fragment",
1325 RF
.Flag() == RuntimeFunctionFlag::RFF_PackedFragment
);
1326 SW
.printNumber("FunctionLength", RF
.FunctionLength());
1327 SW
.printNumber("RegF", RF
.RegF());
1328 SW
.printNumber("RegI", RF
.RegI());
1329 SW
.printBoolean("HomedParameters", RF
.H());
1330 SW
.printNumber("CR", RF
.CR());
1331 SW
.printNumber("FrameSize", RF
.FrameSize() << 4);
1332 ListScope
PS(SW
, "Prologue");
1334 // Synthesize the equivalent prologue according to the documentation
1335 // at https://docs.microsoft.com/en-us/cpp/build/arm64-exception-handling,
1336 // printed in reverse order compared to the docs, to match how prologues
1337 // are printed for the non-packed case.
1338 int IntSZ
= 8 * RF
.RegI();
1341 int FpSZ
= 8 * RF
.RegF();
1344 int SavSZ
= (IntSZ
+ FpSZ
+ 8 * 8 * RF
.H() + 0xf) & ~0xf;
1345 int LocSZ
= (RF
.FrameSize() << 4) - SavSZ
;
1347 if (RF
.CR() == 2 || RF
.CR() == 3) {
1348 SW
.startLine() << "mov x29, sp\n";
1350 SW
.startLine() << format("stp x29, lr, [sp, #-%d]!\n", LocSZ
);
1352 SW
.startLine() << "stp x29, lr, [sp, #0]\n";
1356 SW
.startLine() << format("sub sp, sp, #%d\n", LocSZ
- 4080);
1357 SW
.startLine() << "sub sp, sp, #4080\n";
1358 } else if ((RF
.CR() != 3 && RF
.CR() != 2 && LocSZ
> 0) || LocSZ
> 512) {
1359 SW
.startLine() << format("sub sp, sp, #%d\n", LocSZ
);
1362 SW
.startLine() << format("stp x6, x7, [sp, #%d]\n", SavSZ
- 16);
1363 SW
.startLine() << format("stp x4, x5, [sp, #%d]\n", SavSZ
- 32);
1364 SW
.startLine() << format("stp x2, x3, [sp, #%d]\n", SavSZ
- 48);
1365 if (RF
.RegI() > 0 || RF
.RegF() > 0 || RF
.CR() == 1) {
1366 SW
.startLine() << format("stp x0, x1, [sp, #%d]\n", SavSZ
- 64);
1368 // This case isn't documented; if neither RegI nor RegF nor CR=1
1369 // have decremented the stack pointer by SavSZ, we need to do it here
1370 // (as the final stack adjustment of LocSZ excludes SavSZ).
1371 SW
.startLine() << format("stp x0, x1, [sp, #-%d]!\n", SavSZ
);
1374 int FloatRegs
= RF
.RegF() > 0 ? RF
.RegF() + 1 : 0;
1375 for (int I
= (FloatRegs
+ 1) / 2 - 1; I
>= 0; I
--) {
1376 if (I
== (FloatRegs
+ 1) / 2 - 1 && FloatRegs
% 2 == 1) {
1377 // The last register, an odd register without a pair
1378 SW
.startLine() << format("str d%d, [sp, #%d]\n", 8 + 2 * I
,
1380 } else if (I
== 0 && RF
.RegI() == 0 && RF
.CR() != 1) {
1381 SW
.startLine() << format("stp d%d, d%d, [sp, #-%d]!\n", 8 + 2 * I
,
1382 8 + 2 * I
+ 1, SavSZ
);
1384 SW
.startLine() << format("stp d%d, d%d, [sp, #%d]\n", 8 + 2 * I
,
1385 8 + 2 * I
+ 1, IntSZ
+ 16 * I
);
1388 if (RF
.CR() == 1 && (RF
.RegI() % 2) == 0) {
1390 SW
.startLine() << format("str lr, [sp, #-%d]!\n", SavSZ
);
1392 SW
.startLine() << format("str lr, [sp, #%d]\n", IntSZ
- 8);
1394 for (int I
= (RF
.RegI() + 1) / 2 - 1; I
>= 0; I
--) {
1395 if (I
== (RF
.RegI() + 1) / 2 - 1 && RF
.RegI() % 2 == 1) {
1396 // The last register, an odd register without a pair
1398 if (I
== 0) { // If this is the only register pair
1399 // CR=1 combined with RegI=1 doesn't map to a documented case;
1400 // it doesn't map to any regular unwind info opcode, and the
1401 // actual unwinder doesn't support it.
1402 SW
.startLine() << "INVALID!\n";
1404 SW
.startLine() << format("stp x%d, lr, [sp, #%d]\n", 19 + 2 * I
,
1408 SW
.startLine() << format("str x%d, [sp, #-%d]!\n", 19 + 2 * I
, SavSZ
);
1410 SW
.startLine() << format("str x%d, [sp, #%d]\n", 19 + 2 * I
, 16 * I
);
1412 } else if (I
== 0) {
1413 // The first register pair
1414 SW
.startLine() << format("stp x19, x20, [sp, #-%d]!\n", SavSZ
);
1416 SW
.startLine() << format("stp x%d, x%d, [sp, #%d]\n", 19 + 2 * I
,
1417 19 + 2 * I
+ 1, 16 * I
);
1420 // CR=2 is yet undocumented, see
1421 // https://github.com/MicrosoftDocs/cpp-docs/pull/4202 for upstream
1422 // progress on getting it documented.
1424 SW
.startLine() << "pacibsp\n";
1425 SW
.startLine() << "end\n";
1430 bool Decoder::dumpProcedureDataEntry(const COFFObjectFile
&COFF
,
1431 const SectionRef Section
, unsigned Index
,
1432 ArrayRef
<uint8_t> Contents
) {
1433 uint64_t Offset
= PDataEntrySize
* Index
;
1434 const ulittle32_t
*Data
=
1435 reinterpret_cast<const ulittle32_t
*>(Contents
.data() + Offset
);
1437 const RuntimeFunction
Entry(Data
);
1438 DictScope
RFS(SW
, "RuntimeFunction");
1439 if (Entry
.Flag() == RuntimeFunctionFlag::RFF_Unpacked
)
1440 return dumpUnpackedEntry(COFF
, Section
, Offset
, Index
, Entry
);
1442 const RuntimeFunctionARM64
EntryARM64(Data
);
1443 return dumpPackedARM64Entry(COFF
, Section
, Offset
, Index
, EntryARM64
);
1445 return dumpPackedEntry(COFF
, Section
, Offset
, Index
, Entry
);
1448 void Decoder::dumpProcedureData(const COFFObjectFile
&COFF
,
1449 const SectionRef Section
) {
1450 ArrayRef
<uint8_t> Contents
;
1451 if (COFF
.getSectionContents(COFF
.getCOFFSection(Section
), Contents
))
1454 if (Contents
.size() % PDataEntrySize
) {
1455 errs() << ".pdata content is not " << PDataEntrySize
<< "-byte aligned\n";
1459 for (unsigned EI
= 0, EE
= Contents
.size() / PDataEntrySize
; EI
< EE
; ++EI
)
1460 if (!dumpProcedureDataEntry(COFF
, Section
, EI
, Contents
))
1464 Error
Decoder::dumpProcedureData(const COFFObjectFile
&COFF
) {
1465 for (const auto &Section
: COFF
.sections()) {
1466 Expected
<StringRef
> NameOrErr
=
1467 COFF
.getSectionName(COFF
.getCOFFSection(Section
));
1469 return NameOrErr
.takeError();
1471 if (NameOrErr
->startswith(".pdata"))
1472 dumpProcedureData(COFF
, Section
);
1474 return Error::success();