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, 0xeb, 1, &Decoder::opcode_ec_context
},
175 {0xff, 0xec, 1, &Decoder::opcode_clear_unwound_to_call
},
176 {0xff, 0xfc, 1, &Decoder::opcode_pac_sign_lr
},
179 static void printRange(raw_ostream
&OS
, ListSeparator
&LS
, unsigned First
,
180 unsigned Last
, char Letter
) {
182 OS
<< LS
<< Letter
<< First
;
184 OS
<< LS
<< Letter
<< First
<< "-" << Letter
<< Last
;
187 static void printRange(raw_ostream
&OS
, uint32_t Mask
, ListSeparator
&LS
,
188 unsigned Start
, unsigned End
, char Letter
) {
190 for (unsigned RI
= Start
; RI
<= End
; ++RI
) {
191 if (Mask
& (1 << RI
)) {
196 printRange(OS
, LS
, First
, RI
- 1, Letter
);
202 printRange(OS
, LS
, First
, End
, Letter
);
205 void Decoder::printGPRMask(uint16_t GPRMask
) {
208 printRange(OS
, GPRMask
, LS
, 0, 12, 'r');
209 if (GPRMask
& (1 << 14))
211 if (GPRMask
& (1 << 15))
216 void Decoder::printVFPMask(uint32_t VFPMask
) {
219 printRange(OS
, VFPMask
, LS
, 0, 31, 'd');
223 ErrorOr
<object::SectionRef
>
224 Decoder::getSectionContaining(const COFFObjectFile
&COFF
, uint64_t VA
) {
225 for (const auto &Section
: COFF
.sections()) {
226 uint64_t Address
= Section
.getAddress();
227 uint64_t Size
= Section
.getSize();
229 if (VA
>= Address
&& (VA
- Address
) <= Size
)
232 return inconvertibleErrorCode();
235 ErrorOr
<object::SymbolRef
> Decoder::getSymbol(const COFFObjectFile
&COFF
,
236 uint64_t VA
, bool FunctionOnly
) {
237 for (const auto &Symbol
: COFF
.symbols()) {
238 Expected
<SymbolRef::Type
> Type
= Symbol
.getType();
240 return errorToErrorCode(Type
.takeError());
241 if (FunctionOnly
&& *Type
!= SymbolRef::ST_Function
)
244 Expected
<uint64_t> Address
= Symbol
.getAddress();
246 return errorToErrorCode(Address
.takeError());
250 return inconvertibleErrorCode();
253 ErrorOr
<SymbolRef
> Decoder::getRelocatedSymbol(const COFFObjectFile
&,
254 const SectionRef
&Section
,
256 for (const auto &Relocation
: Section
.relocations()) {
257 uint64_t RelocationOffset
= Relocation
.getOffset();
258 if (RelocationOffset
== Offset
)
259 return *Relocation
.getSymbol();
261 return inconvertibleErrorCode();
264 SymbolRef
Decoder::getPreferredSymbol(const COFFObjectFile
&COFF
, SymbolRef Sym
,
265 uint64_t &SymbolOffset
) {
266 // The symbol resolved by getRelocatedSymbol can be any internal
267 // nondescriptive symbol; try to resolve a more descriptive one.
268 COFFSymbolRef CoffSym
= COFF
.getCOFFSymbol(Sym
);
269 if (CoffSym
.getStorageClass() != COFF::IMAGE_SYM_CLASS_LABEL
&&
270 CoffSym
.getSectionDefinition() == nullptr)
272 for (const auto &S
: COFF
.symbols()) {
273 COFFSymbolRef CS
= COFF
.getCOFFSymbol(S
);
274 if (CS
.getSectionNumber() == CoffSym
.getSectionNumber() &&
275 CS
.getValue() <= CoffSym
.getValue() + SymbolOffset
&&
276 CS
.getStorageClass() != COFF::IMAGE_SYM_CLASS_LABEL
&&
277 CS
.getSectionDefinition() == nullptr) {
278 uint32_t Offset
= CoffSym
.getValue() + SymbolOffset
- CS
.getValue();
279 if (Offset
<= SymbolOffset
) {
280 SymbolOffset
= Offset
;
283 if (CS
.isExternal() && SymbolOffset
== 0)
291 ErrorOr
<SymbolRef
> Decoder::getSymbolForLocation(
292 const COFFObjectFile
&COFF
, const SectionRef
&Section
,
293 uint64_t OffsetInSection
, uint64_t ImmediateOffset
, uint64_t &SymbolAddress
,
294 uint64_t &SymbolOffset
, bool FunctionOnly
) {
295 // Try to locate a relocation that points at the offset in the section
296 ErrorOr
<SymbolRef
> SymOrErr
=
297 getRelocatedSymbol(COFF
, Section
, OffsetInSection
);
299 // We found a relocation symbol; the immediate offset needs to be added
300 // to the symbol address.
301 SymbolOffset
= ImmediateOffset
;
303 Expected
<uint64_t> AddressOrErr
= SymOrErr
->getAddress();
306 llvm::raw_string_ostream
OS(Buf
);
307 logAllUnhandledErrors(AddressOrErr
.takeError(), OS
);
308 report_fatal_error(Twine(Buf
));
310 // We apply SymbolOffset here directly. We return it separately to allow
311 // the caller to print it as an offset on the symbol name.
312 SymbolAddress
= *AddressOrErr
+ SymbolOffset
;
314 if (FunctionOnly
) // Resolve label/section symbols into function names.
315 SymOrErr
= getPreferredSymbol(COFF
, *SymOrErr
, SymbolOffset
);
317 // No matching relocation found; operating on a linked image. Try to
318 // find a descriptive symbol if possible. The immediate offset contains
319 // the image relative address, and we shouldn't add any offset to the
321 SymbolAddress
= COFF
.getImageBase() + ImmediateOffset
;
323 SymOrErr
= getSymbol(COFF
, SymbolAddress
, FunctionOnly
);
328 bool Decoder::opcode_0xxxxxxx(const uint8_t *OC
, unsigned &Offset
,
329 unsigned Length
, bool Prologue
) {
330 uint8_t Imm
= OC
[Offset
] & 0x7f;
331 SW
.startLine() << format("0x%02x ; %s sp, #(%u * 4)\n",
333 static_cast<const char *>(Prologue
? "sub" : "add"),
339 bool Decoder::opcode_10Lxxxxx(const uint8_t *OC
, unsigned &Offset
,
340 unsigned Length
, bool Prologue
) {
341 unsigned Link
= (OC
[Offset
] & 0x20) >> 5;
342 uint16_t RegisterMask
= (Link
<< (Prologue
? 14 : 15))
343 | ((OC
[Offset
+ 0] & 0x1f) << 8)
344 | ((OC
[Offset
+ 1] & 0xff) << 0);
345 assert((~RegisterMask
& (1 << 13)) && "sp must not be set");
346 assert((~RegisterMask
& (1 << (Prologue
? 15 : 14))) && "pc must not be set");
348 SW
.startLine() << format("0x%02x 0x%02x ; %s.w ",
349 OC
[Offset
+ 0], OC
[Offset
+ 1],
350 Prologue
? "push" : "pop");
351 printGPRMask(RegisterMask
);
358 bool Decoder::opcode_1100xxxx(const uint8_t *OC
, unsigned &Offset
,
359 unsigned Length
, bool Prologue
) {
361 SW
.startLine() << format("0x%02x ; mov r%u, sp\n",
362 OC
[Offset
], OC
[Offset
] & 0xf);
364 SW
.startLine() << format("0x%02x ; mov sp, r%u\n",
365 OC
[Offset
], OC
[Offset
] & 0xf);
370 bool Decoder::opcode_11010Lxx(const uint8_t *OC
, unsigned &Offset
,
371 unsigned Length
, bool Prologue
) {
372 unsigned Link
= (OC
[Offset
] & 0x4) >> 2;
373 unsigned Count
= (OC
[Offset
] & 0x3);
375 uint16_t GPRMask
= (Link
<< (Prologue
? 14 : 15))
376 | (((1 << (Count
+ 1)) - 1) << 4);
378 SW
.startLine() << format("0x%02x ; %s ", OC
[Offset
],
379 Prologue
? "push" : "pop");
380 printGPRMask(GPRMask
);
387 bool Decoder::opcode_11011Lxx(const uint8_t *OC
, unsigned &Offset
,
388 unsigned Length
, bool Prologue
) {
389 unsigned Link
= (OC
[Offset
] & 0x4) >> 2;
390 unsigned Count
= (OC
[Offset
] & 0x3) + 4;
392 uint16_t GPRMask
= (Link
<< (Prologue
? 14 : 15))
393 | (((1 << (Count
+ 1)) - 1) << 4);
395 SW
.startLine() << format("0x%02x ; %s.w ", OC
[Offset
],
396 Prologue
? "push" : "pop");
397 printGPRMask(GPRMask
);
404 bool Decoder::opcode_11100xxx(const uint8_t *OC
, unsigned &Offset
,
405 unsigned Length
, bool Prologue
) {
406 unsigned High
= (OC
[Offset
] & 0x7);
407 uint32_t VFPMask
= (((1 << (High
+ 1)) - 1) << 8);
409 SW
.startLine() << format("0x%02x ; %s ", OC
[Offset
],
410 Prologue
? "vpush" : "vpop");
411 printVFPMask(VFPMask
);
418 bool Decoder::opcode_111010xx(const uint8_t *OC
, unsigned &Offset
,
419 unsigned Length
, bool Prologue
) {
420 uint16_t Imm
= ((OC
[Offset
+ 0] & 0x03) << 8) | ((OC
[Offset
+ 1] & 0xff) << 0);
422 SW
.startLine() << format("0x%02x 0x%02x ; %s.w sp, #(%u * 4)\n",
423 OC
[Offset
+ 0], OC
[Offset
+ 1],
424 static_cast<const char *>(Prologue
? "sub" : "add"),
431 bool Decoder::opcode_1110110L(const uint8_t *OC
, unsigned &Offset
,
432 unsigned Length
, bool Prologue
) {
433 uint16_t GPRMask
= ((OC
[Offset
+ 0] & 0x01) << (Prologue
? 14 : 15))
434 | ((OC
[Offset
+ 1] & 0xff) << 0);
436 SW
.startLine() << format("0x%02x 0x%02x ; %s ", OC
[Offset
+ 0],
437 OC
[Offset
+ 1], Prologue
? "push" : "pop");
438 printGPRMask(GPRMask
);
445 bool Decoder::opcode_11101110(const uint8_t *OC
, unsigned &Offset
,
446 unsigned Length
, bool Prologue
) {
447 assert(!Prologue
&& "may not be used in prologue");
449 if (OC
[Offset
+ 1] & 0xf0)
450 SW
.startLine() << format("0x%02x 0x%02x ; reserved\n",
451 OC
[Offset
+ 0], OC
[Offset
+ 1]);
454 << format("0x%02x 0x%02x ; microsoft-specific (type: %u)\n",
455 OC
[Offset
+ 0], OC
[Offset
+ 1], OC
[Offset
+ 1] & 0x0f);
461 bool Decoder::opcode_11101111(const uint8_t *OC
, unsigned &Offset
,
462 unsigned Length
, bool Prologue
) {
463 if (OC
[Offset
+ 1] & 0xf0)
464 SW
.startLine() << format("0x%02x 0x%02x ; reserved\n",
465 OC
[Offset
+ 0], OC
[Offset
+ 1]);
468 << format("0x%02x 0x%02x ; str.w lr, [sp, #-%u]!\n",
469 OC
[Offset
+ 0], OC
[Offset
+ 1], OC
[Offset
+ 1] << 2);
472 << format("0x%02x 0x%02x ; ldr.w lr, [sp], #%u\n",
473 OC
[Offset
+ 0], OC
[Offset
+ 1], OC
[Offset
+ 1] << 2);
479 bool Decoder::opcode_11110101(const uint8_t *OC
, unsigned &Offset
,
480 unsigned Length
, bool Prologue
) {
481 unsigned Start
= (OC
[Offset
+ 1] & 0xf0) >> 4;
482 unsigned End
= (OC
[Offset
+ 1] & 0x0f) >> 0;
483 uint32_t VFPMask
= ((1 << (End
+ 1 - Start
)) - 1) << Start
;
485 SW
.startLine() << format("0x%02x 0x%02x ; %s ", OC
[Offset
+ 0],
486 OC
[Offset
+ 1], Prologue
? "vpush" : "vpop");
487 printVFPMask(VFPMask
);
494 bool Decoder::opcode_11110110(const uint8_t *OC
, unsigned &Offset
,
495 unsigned Length
, bool Prologue
) {
496 unsigned Start
= (OC
[Offset
+ 1] & 0xf0) >> 4;
497 unsigned End
= (OC
[Offset
+ 1] & 0x0f) >> 0;
498 uint32_t VFPMask
= ((1 << (End
+ 1 - Start
)) - 1) << (16 + Start
);
500 SW
.startLine() << format("0x%02x 0x%02x ; %s ", OC
[Offset
+ 0],
501 OC
[Offset
+ 1], Prologue
? "vpush" : "vpop");
502 printVFPMask(VFPMask
);
509 bool Decoder::opcode_11110111(const uint8_t *OC
, unsigned &Offset
,
510 unsigned Length
, bool Prologue
) {
511 uint32_t Imm
= (OC
[Offset
+ 1] << 8) | (OC
[Offset
+ 2] << 0);
513 SW
.startLine() << format("0x%02x 0x%02x 0x%02x ; %s sp, sp, #(%u * 4)\n",
514 OC
[Offset
+ 0], OC
[Offset
+ 1], OC
[Offset
+ 2],
515 static_cast<const char *>(Prologue
? "sub" : "add"),
522 bool Decoder::opcode_11111000(const uint8_t *OC
, unsigned &Offset
,
523 unsigned Length
, bool Prologue
) {
524 uint32_t Imm
= (OC
[Offset
+ 1] << 16)
525 | (OC
[Offset
+ 2] << 8)
526 | (OC
[Offset
+ 3] << 0);
529 << format("0x%02x 0x%02x 0x%02x 0x%02x ; %s sp, sp, #(%u * 4)\n",
530 OC
[Offset
+ 0], OC
[Offset
+ 1], OC
[Offset
+ 2], OC
[Offset
+ 3],
531 static_cast<const char *>(Prologue
? "sub" : "add"), Imm
);
537 bool Decoder::opcode_11111001(const uint8_t *OC
, unsigned &Offset
,
538 unsigned Length
, bool Prologue
) {
539 uint32_t Imm
= (OC
[Offset
+ 1] << 8) | (OC
[Offset
+ 2] << 0);
542 << format("0x%02x 0x%02x 0x%02x ; %s.w sp, sp, #(%u * 4)\n",
543 OC
[Offset
+ 0], OC
[Offset
+ 1], OC
[Offset
+ 2],
544 static_cast<const char *>(Prologue
? "sub" : "add"), Imm
);
550 bool Decoder::opcode_11111010(const uint8_t *OC
, unsigned &Offset
,
551 unsigned Length
, bool Prologue
) {
552 uint32_t Imm
= (OC
[Offset
+ 1] << 16)
553 | (OC
[Offset
+ 2] << 8)
554 | (OC
[Offset
+ 3] << 0);
557 << format("0x%02x 0x%02x 0x%02x 0x%02x ; %s.w sp, sp, #(%u * 4)\n",
558 OC
[Offset
+ 0], OC
[Offset
+ 1], OC
[Offset
+ 2], OC
[Offset
+ 3],
559 static_cast<const char *>(Prologue
? "sub" : "add"), Imm
);
565 bool Decoder::opcode_11111011(const uint8_t *OC
, unsigned &Offset
,
566 unsigned Length
, bool Prologue
) {
567 SW
.startLine() << format("0x%02x ; nop\n", OC
[Offset
]);
572 bool Decoder::opcode_11111100(const uint8_t *OC
, unsigned &Offset
,
573 unsigned Length
, bool Prologue
) {
574 SW
.startLine() << format("0x%02x ; nop.w\n", OC
[Offset
]);
579 bool Decoder::opcode_11111101(const uint8_t *OC
, unsigned &Offset
,
580 unsigned Length
, bool Prologue
) {
581 SW
.startLine() << format("0x%02x ; bx <reg>\n", OC
[Offset
]);
586 bool Decoder::opcode_11111110(const uint8_t *OC
, unsigned &Offset
,
587 unsigned Length
, bool Prologue
) {
588 SW
.startLine() << format("0x%02x ; b.w <target>\n", OC
[Offset
]);
593 bool Decoder::opcode_11111111(const uint8_t *OC
, unsigned &Offset
,
594 unsigned Length
, bool Prologue
) {
599 // ARM64 unwind codes start here.
600 bool Decoder::opcode_alloc_s(const uint8_t *OC
, unsigned &Offset
,
601 unsigned Length
, bool Prologue
) {
602 uint32_t NumBytes
= (OC
[Offset
] & 0x1F) << 4;
603 SW
.startLine() << format("0x%02x ; %s sp, #%u\n", OC
[Offset
],
604 static_cast<const char *>(Prologue
? "sub" : "add"),
610 bool Decoder::opcode_save_r19r20_x(const uint8_t *OC
, unsigned &Offset
,
611 unsigned Length
, bool Prologue
) {
612 uint32_t Off
= (OC
[Offset
] & 0x1F) << 3;
614 SW
.startLine() << format(
615 "0x%02x ; stp x19, x20, [sp, #-%u]!\n", OC
[Offset
], Off
);
617 SW
.startLine() << format(
618 "0x%02x ; ldp x19, x20, [sp], #%u\n", OC
[Offset
], Off
);
623 bool Decoder::opcode_save_fplr(const uint8_t *OC
, unsigned &Offset
,
624 unsigned Length
, bool Prologue
) {
625 uint32_t Off
= (OC
[Offset
] & 0x3F) << 3;
626 SW
.startLine() << format(
627 "0x%02x ; %s x29, x30, [sp, #%u]\n", OC
[Offset
],
628 static_cast<const char *>(Prologue
? "stp" : "ldp"), Off
);
633 bool Decoder::opcode_save_fplr_x(const uint8_t *OC
, unsigned &Offset
,
634 unsigned Length
, bool Prologue
) {
635 uint32_t Off
= ((OC
[Offset
] & 0x3F) + 1) << 3;
637 SW
.startLine() << format(
638 "0x%02x ; stp x29, x30, [sp, #-%u]!\n", OC
[Offset
], Off
);
640 SW
.startLine() << format(
641 "0x%02x ; ldp x29, x30, [sp], #%u\n", OC
[Offset
], Off
);
646 bool Decoder::opcode_alloc_m(const uint8_t *OC
, unsigned &Offset
,
647 unsigned Length
, bool Prologue
) {
648 uint32_t NumBytes
= ((OC
[Offset
] & 0x07) << 8);
649 NumBytes
|= (OC
[Offset
+ 1] & 0xFF);
651 SW
.startLine() << format("0x%02x%02x ; %s sp, #%u\n",
652 OC
[Offset
], OC
[Offset
+ 1],
653 static_cast<const char *>(Prologue
? "sub" : "add"),
659 bool Decoder::opcode_save_regp(const uint8_t *OC
, unsigned &Offset
,
660 unsigned Length
, bool Prologue
) {
661 uint32_t Reg
= ((OC
[Offset
] & 0x03) << 8);
662 Reg
|= (OC
[Offset
+ 1] & 0xC0);
665 uint32_t Off
= (OC
[Offset
+ 1] & 0x3F) << 3;
666 SW
.startLine() << format(
667 "0x%02x%02x ; %s x%u, x%u, [sp, #%u]\n",
668 OC
[Offset
], OC
[Offset
+ 1],
669 static_cast<const char *>(Prologue
? "stp" : "ldp"), Reg
, Reg
+ 1, Off
);
674 bool Decoder::opcode_save_regp_x(const uint8_t *OC
, unsigned &Offset
,
675 unsigned Length
, bool Prologue
) {
676 uint32_t Reg
= ((OC
[Offset
] & 0x03) << 8);
677 Reg
|= (OC
[Offset
+ 1] & 0xC0);
680 uint32_t Off
= ((OC
[Offset
+ 1] & 0x3F) + 1) << 3;
682 SW
.startLine() << format(
683 "0x%02x%02x ; stp x%u, x%u, [sp, #-%u]!\n",
684 OC
[Offset
], OC
[Offset
+ 1], Reg
,
687 SW
.startLine() << format(
688 "0x%02x%02x ; ldp x%u, x%u, [sp], #%u\n",
689 OC
[Offset
], OC
[Offset
+ 1], Reg
,
695 bool Decoder::opcode_save_reg(const uint8_t *OC
, unsigned &Offset
,
696 unsigned Length
, bool Prologue
) {
697 uint32_t Reg
= (OC
[Offset
] & 0x03) << 8;
698 Reg
|= (OC
[Offset
+ 1] & 0xC0);
701 uint32_t Off
= (OC
[Offset
+ 1] & 0x3F) << 3;
702 SW
.startLine() << format("0x%02x%02x ; %s x%u, [sp, #%u]\n",
703 OC
[Offset
], OC
[Offset
+ 1],
704 static_cast<const char *>(Prologue
? "str" : "ldr"),
710 bool Decoder::opcode_save_reg_x(const uint8_t *OC
, unsigned &Offset
,
711 unsigned Length
, bool Prologue
) {
712 uint32_t Reg
= (OC
[Offset
] & 0x01) << 8;
713 Reg
|= (OC
[Offset
+ 1] & 0xE0);
716 uint32_t Off
= ((OC
[Offset
+ 1] & 0x1F) + 1) << 3;
718 SW
.startLine() << format("0x%02x%02x ; str x%u, [sp, #-%u]!\n",
719 OC
[Offset
], OC
[Offset
+ 1], Reg
, Off
);
721 SW
.startLine() << format("0x%02x%02x ; ldr x%u, [sp], #%u\n",
722 OC
[Offset
], OC
[Offset
+ 1], Reg
, Off
);
727 bool Decoder::opcode_save_lrpair(const uint8_t *OC
, unsigned &Offset
,
728 unsigned Length
, bool Prologue
) {
729 uint32_t Reg
= (OC
[Offset
] & 0x01) << 8;
730 Reg
|= (OC
[Offset
+ 1] & 0xC0);
734 uint32_t Off
= (OC
[Offset
+ 1] & 0x3F) << 3;
735 SW
.startLine() << format("0x%02x%02x ; %s x%u, lr, [sp, #%u]\n",
736 OC
[Offset
], OC
[Offset
+ 1],
737 static_cast<const char *>(Prologue
? "stp" : "ldp"),
743 bool Decoder::opcode_save_fregp(const uint8_t *OC
, unsigned &Offset
,
744 unsigned Length
, bool Prologue
) {
745 uint32_t Reg
= (OC
[Offset
] & 0x01) << 8;
746 Reg
|= (OC
[Offset
+ 1] & 0xC0);
749 uint32_t Off
= (OC
[Offset
+ 1] & 0x3F) << 3;
750 SW
.startLine() << format("0x%02x%02x ; %s d%u, d%u, [sp, #%u]\n",
751 OC
[Offset
], OC
[Offset
+ 1],
752 static_cast<const char *>(Prologue
? "stp" : "ldp"),
758 bool Decoder::opcode_save_fregp_x(const uint8_t *OC
, unsigned &Offset
,
759 unsigned Length
, bool Prologue
) {
760 uint32_t Reg
= (OC
[Offset
] & 0x01) << 8;
761 Reg
|= (OC
[Offset
+ 1] & 0xC0);
764 uint32_t Off
= ((OC
[Offset
+ 1] & 0x3F) + 1) << 3;
766 SW
.startLine() << format(
767 "0x%02x%02x ; stp d%u, d%u, [sp, #-%u]!\n", OC
[Offset
],
768 OC
[Offset
+ 1], Reg
, Reg
+ 1, Off
);
770 SW
.startLine() << format(
771 "0x%02x%02x ; ldp d%u, d%u, [sp], #%u\n", OC
[Offset
],
772 OC
[Offset
+ 1], Reg
, Reg
+ 1, Off
);
777 bool Decoder::opcode_save_freg(const uint8_t *OC
, unsigned &Offset
,
778 unsigned Length
, bool Prologue
) {
779 uint32_t Reg
= (OC
[Offset
] & 0x01) << 8;
780 Reg
|= (OC
[Offset
+ 1] & 0xC0);
783 uint32_t Off
= (OC
[Offset
+ 1] & 0x3F) << 3;
784 SW
.startLine() << format("0x%02x%02x ; %s d%u, [sp, #%u]\n",
785 OC
[Offset
], OC
[Offset
+ 1],
786 static_cast<const char *>(Prologue
? "str" : "ldr"),
792 bool Decoder::opcode_save_freg_x(const uint8_t *OC
, unsigned &Offset
,
793 unsigned Length
, bool Prologue
) {
794 uint32_t Reg
= ((OC
[Offset
+ 1] & 0xE0) >> 5) + 8;
795 uint32_t Off
= ((OC
[Offset
+ 1] & 0x1F) + 1) << 3;
797 SW
.startLine() << format(
798 "0x%02x%02x ; str d%u, [sp, #-%u]!\n", OC
[Offset
],
799 OC
[Offset
+ 1], Reg
, Off
);
801 SW
.startLine() << format(
802 "0x%02x%02x ; ldr d%u, [sp], #%u\n", OC
[Offset
],
803 OC
[Offset
+ 1], Reg
, Off
);
808 bool Decoder::opcode_alloc_l(const uint8_t *OC
, unsigned &Offset
,
809 unsigned Length
, bool Prologue
) {
811 (OC
[Offset
+ 1] << 16) | (OC
[Offset
+ 2] << 8) | (OC
[Offset
+ 3] << 0);
813 SW
.startLine() << format(
814 "0x%02x%02x%02x%02x ; %s sp, #%u\n", OC
[Offset
], OC
[Offset
+ 1],
815 OC
[Offset
+ 2], OC
[Offset
+ 3],
816 static_cast<const char *>(Prologue
? "sub" : "add"), Off
);
821 bool Decoder::opcode_setfp(const uint8_t *OC
, unsigned &Offset
, unsigned Length
,
823 SW
.startLine() << format("0x%02x ; mov %s, %s\n", OC
[Offset
],
824 static_cast<const char *>(Prologue
? "fp" : "sp"),
825 static_cast<const char *>(Prologue
? "sp" : "fp"));
830 bool Decoder::opcode_addfp(const uint8_t *OC
, unsigned &Offset
, unsigned Length
,
832 unsigned NumBytes
= OC
[Offset
+ 1] << 3;
833 SW
.startLine() << format(
834 "0x%02x%02x ; %s %s, %s, #%u\n", OC
[Offset
], OC
[Offset
+ 1],
835 static_cast<const char *>(Prologue
? "add" : "sub"),
836 static_cast<const char *>(Prologue
? "fp" : "sp"),
837 static_cast<const char *>(Prologue
? "sp" : "fp"), NumBytes
);
842 bool Decoder::opcode_nop(const uint8_t *OC
, unsigned &Offset
, unsigned Length
,
844 SW
.startLine() << format("0x%02x ; nop\n", OC
[Offset
]);
849 bool Decoder::opcode_end(const uint8_t *OC
, unsigned &Offset
, unsigned Length
,
851 SW
.startLine() << format("0x%02x ; end\n", OC
[Offset
]);
856 bool Decoder::opcode_end_c(const uint8_t *OC
, unsigned &Offset
, unsigned Length
,
858 SW
.startLine() << format("0x%02x ; end_c\n", OC
[Offset
]);
863 bool Decoder::opcode_save_next(const uint8_t *OC
, unsigned &Offset
,
864 unsigned Length
, bool Prologue
) {
866 SW
.startLine() << format("0x%02x ; save next\n", OC
[Offset
]);
868 SW
.startLine() << format("0x%02x ; restore next\n",
874 bool Decoder::opcode_save_any_reg(const uint8_t *OC
, unsigned &Offset
,
875 unsigned Length
, bool Prologue
) {
876 // Whether the instruction has writeback
877 bool Writeback
= (OC
[Offset
+ 1] & 0x20) == 0x20;
878 // Whether the instruction is paired. (Paired instructions are required
879 // to save/restore adjacent registers.)
880 bool Paired
= (OC
[Offset
+ 1] & 0x40) == 0x40;
881 // The kind of register saved:
882 // - 0 is an x register
883 // - 1 is the low half of a q register
884 // - 2 is a whole q register
885 int RegKind
= (OC
[Offset
+ 2] & 0xC0) >> 6;
886 // Encoded register name (0 -> x0/q0, 1 -> x1/q1, etc.)
887 int Reg
= OC
[Offset
+ 1] & 0x1F;
888 // Encoded stack offset of load/store instruction; decoding varies by mode.
889 int StackOffset
= OC
[Offset
+ 2] & 0x3F;
892 if (!Writeback
&& !Paired
&& RegKind
!= 2)
897 SW
.startLine() << format("0x%02x%02x%02x ; ", OC
[Offset
],
898 OC
[Offset
+ 1], OC
[Offset
+ 2]);
900 // Verify the encoding is in a form we understand. The high bit of the first
901 // byte, and mode 3 for the register kind are apparently reserved. The
902 // encoded register must refer to a valid register.
908 if ((OC
[Offset
+ 1] & 0x80) == 0x80 || RegKind
== 3 || Reg
> MaxReg
) {
909 SW
.getOStream() << "invalid save_any_reg encoding\n";
916 SW
.getOStream() << "stp ";
918 SW
.getOStream() << "ldp ";
921 SW
.getOStream() << "str ";
923 SW
.getOStream() << "ldr ";
929 } else if (RegKind
== 2) {
934 SW
.getOStream() << format("%c%d, %c%d, ", RegChar
, Reg
, RegChar
, Reg
+ 1);
936 SW
.getOStream() << format("%c%d, ", RegChar
, Reg
);
940 SW
.getOStream() << format("[sp, #-%d]!\n", StackOffset
);
942 SW
.getOStream() << format("[sp], #%d\n", StackOffset
);
944 SW
.getOStream() << format("[sp, #%d]\n", StackOffset
);
951 bool Decoder::opcode_trap_frame(const uint8_t *OC
, unsigned &Offset
,
952 unsigned Length
, bool Prologue
) {
953 SW
.startLine() << format("0x%02x ; trap frame\n", OC
[Offset
]);
958 bool Decoder::opcode_machine_frame(const uint8_t *OC
, unsigned &Offset
,
959 unsigned Length
, bool Prologue
) {
960 SW
.startLine() << format("0x%02x ; machine frame\n",
966 bool Decoder::opcode_context(const uint8_t *OC
, unsigned &Offset
,
967 unsigned Length
, bool Prologue
) {
968 SW
.startLine() << format("0x%02x ; context\n", OC
[Offset
]);
973 bool Decoder::opcode_ec_context(const uint8_t *OC
, unsigned &Offset
,
974 unsigned Length
, bool Prologue
) {
975 SW
.startLine() << format("0x%02x ; EC context\n", OC
[Offset
]);
980 bool Decoder::opcode_clear_unwound_to_call(const uint8_t *OC
, unsigned &Offset
,
981 unsigned Length
, bool Prologue
) {
982 SW
.startLine() << format("0x%02x ; clear unwound to call\n",
988 bool Decoder::opcode_pac_sign_lr(const uint8_t *OC
, unsigned &Offset
,
989 unsigned Length
, bool Prologue
) {
991 SW
.startLine() << format("0x%02x ; pacibsp\n", OC
[Offset
]);
993 SW
.startLine() << format("0x%02x ; autibsp\n", OC
[Offset
]);
998 void Decoder::decodeOpcodes(ArrayRef
<uint8_t> Opcodes
, unsigned Offset
,
1000 assert((!Prologue
|| Offset
== 0) && "prologue should always use offset 0");
1001 const RingEntry
* DecodeRing
= isAArch64
? Ring64
: Ring
;
1002 bool Terminated
= false;
1003 for (unsigned OI
= Offset
, OE
= Opcodes
.size(); !Terminated
&& OI
< OE
; ) {
1004 for (unsigned DI
= 0;; ++DI
) {
1005 if ((isAArch64
&& (DI
>= std::size(Ring64
))) ||
1006 (!isAArch64
&& (DI
>= std::size(Ring
)))) {
1007 SW
.startLine() << format("0x%02x ; Bad opcode!\n",
1008 Opcodes
.data()[OI
]);
1013 if ((Opcodes
[OI
] & DecodeRing
[DI
].Mask
) == DecodeRing
[DI
].Value
) {
1014 if (OI
+ DecodeRing
[DI
].Length
> OE
) {
1015 SW
.startLine() << format("Opcode 0x%02x goes past the unwind data\n",
1017 OI
+= DecodeRing
[DI
].Length
;
1021 (this->*DecodeRing
[DI
].Routine
)(Opcodes
.data(), OI
, 0, Prologue
);
1028 bool Decoder::dumpXDataRecord(const COFFObjectFile
&COFF
,
1029 const SectionRef
&Section
,
1030 uint64_t FunctionAddress
, uint64_t VA
) {
1031 ArrayRef
<uint8_t> Contents
;
1032 if (COFF
.getSectionContents(COFF
.getCOFFSection(Section
), Contents
))
1035 uint64_t SectionVA
= Section
.getAddress();
1036 uint64_t Offset
= VA
- SectionVA
;
1037 const ulittle32_t
*Data
=
1038 reinterpret_cast<const ulittle32_t
*>(Contents
.data() + Offset
);
1040 // Sanity check to ensure that the .xdata header is present.
1041 // A header is one or two words, followed by at least one word to describe
1042 // the unwind codes. Applicable to both ARM and AArch64.
1043 if (Contents
.size() - Offset
< 8)
1044 report_fatal_error(".xdata must be at least 8 bytes in size");
1046 const ExceptionDataRecord
XData(Data
, isAArch64
);
1047 DictScope
XRS(SW
, "ExceptionData");
1048 SW
.printNumber("FunctionLength",
1049 isAArch64
? XData
.FunctionLengthInBytesAArch64() :
1050 XData
.FunctionLengthInBytesARM());
1051 SW
.printNumber("Version", XData
.Vers());
1052 SW
.printBoolean("ExceptionData", XData
.X());
1053 SW
.printBoolean("EpiloguePacked", XData
.E());
1055 SW
.printBoolean("Fragment", XData
.F());
1056 SW
.printNumber(XData
.E() ? "EpilogueOffset" : "EpilogueScopes",
1057 XData
.EpilogueCount());
1058 uint64_t ByteCodeLength
= XData
.CodeWords() * sizeof(uint32_t);
1059 SW
.printNumber("ByteCodeLength", ByteCodeLength
);
1061 if ((int64_t)(Contents
.size() - Offset
- 4 * HeaderWords(XData
) -
1062 (XData
.E() ? 0 : XData
.EpilogueCount() * 4) -
1063 (XData
.X() ? 8 : 0)) < (int64_t)ByteCodeLength
) {
1065 report_fatal_error("Malformed unwind data");
1069 ArrayRef
<uint8_t> UC
= XData
.UnwindByteCode();
1071 ListScope
PS(SW
, "Prologue");
1072 decodeOpcodes(UC
, 0, /*Prologue=*/true);
1074 if (XData
.EpilogueCount()) {
1075 ListScope
ES(SW
, "Epilogue");
1076 decodeOpcodes(UC
, XData
.EpilogueCount(), /*Prologue=*/false);
1080 ListScope
PS(SW
, "Prologue");
1081 decodeOpcodes(XData
.UnwindByteCode(), 0, /*Prologue=*/true);
1083 ArrayRef
<ulittle32_t
> EpilogueScopes
= XData
.EpilogueScopes();
1084 ListScope
ESS(SW
, "EpilogueScopes");
1085 for (const EpilogueScope ES
: EpilogueScopes
) {
1086 DictScope
ESES(SW
, "EpilogueScope");
1087 SW
.printNumber("StartOffset", ES
.EpilogueStartOffset());
1089 SW
.printNumber("Condition", ES
.Condition());
1090 SW
.printNumber("EpilogueStartIndex",
1091 isAArch64
? ES
.EpilogueStartIndexAArch64()
1092 : ES
.EpilogueStartIndexARM());
1093 unsigned ReservedMask
= isAArch64
? 0xF : 0x3;
1094 if ((ES
.ES
>> 18) & ReservedMask
)
1095 SW
.printNumber("ReservedBits", (ES
.ES
>> 18) & ReservedMask
);
1097 ListScope
Opcodes(SW
, "Opcodes");
1098 decodeOpcodes(XData
.UnwindByteCode(),
1099 isAArch64
? ES
.EpilogueStartIndexAArch64()
1100 : ES
.EpilogueStartIndexARM(),
1101 /*Prologue=*/false);
1106 const uint32_t Parameter
= XData
.ExceptionHandlerParameter();
1107 const size_t HandlerOffset
= HeaderWords(XData
) +
1108 (XData
.E() ? 0 : XData
.EpilogueCount()) +
1111 uint64_t Address
, SymbolOffset
;
1112 ErrorOr
<SymbolRef
> Symbol
= getSymbolForLocation(
1113 COFF
, Section
, Offset
+ HandlerOffset
* sizeof(uint32_t),
1114 XData
.ExceptionHandlerRVA(), Address
, SymbolOffset
,
1115 /*FunctionOnly=*/true);
1117 ListScope
EHS(SW
, "ExceptionHandler");
1118 SW
.printHex("Routine", Address
);
1119 SW
.printHex("Parameter", Parameter
);
1123 Expected
<StringRef
> Name
= Symbol
->getName();
1126 llvm::raw_string_ostream
OS(Buf
);
1127 logAllUnhandledErrors(Name
.takeError(), OS
);
1128 report_fatal_error(Twine(Buf
));
1131 ListScope
EHS(SW
, "ExceptionHandler");
1132 SW
.printString("Routine", formatSymbol(*Name
, Address
, SymbolOffset
));
1133 SW
.printHex("Parameter", Parameter
);
1139 bool Decoder::dumpUnpackedEntry(const COFFObjectFile
&COFF
,
1140 const SectionRef Section
, uint64_t Offset
,
1141 unsigned Index
, const RuntimeFunction
&RF
) {
1142 assert(RF
.Flag() == RuntimeFunctionFlag::RFF_Unpacked
&&
1143 "packed entry cannot be treated as an unpacked entry");
1145 uint64_t FunctionAddress
, FunctionOffset
;
1146 ErrorOr
<SymbolRef
> Function
= getSymbolForLocation(
1147 COFF
, Section
, Offset
, RF
.BeginAddress
, FunctionAddress
, FunctionOffset
,
1148 /*FunctionOnly=*/true);
1150 uint64_t XDataAddress
, XDataOffset
;
1151 ErrorOr
<SymbolRef
> XDataRecord
= getSymbolForLocation(
1152 COFF
, Section
, Offset
+ 4, RF
.ExceptionInformationRVA(), XDataAddress
,
1155 if (!RF
.BeginAddress
&& !Function
)
1157 if (!RF
.UnwindData
&& !XDataRecord
)
1160 StringRef FunctionName
;
1162 Expected
<StringRef
> FunctionNameOrErr
= Function
->getName();
1163 if (!FunctionNameOrErr
) {
1165 llvm::raw_string_ostream
OS(Buf
);
1166 logAllUnhandledErrors(FunctionNameOrErr
.takeError(), OS
);
1167 report_fatal_error(Twine(Buf
));
1169 FunctionName
= *FunctionNameOrErr
;
1172 SW
.printString("Function",
1173 formatSymbol(FunctionName
, FunctionAddress
, FunctionOffset
));
1176 Expected
<StringRef
> Name
= XDataRecord
->getName();
1179 llvm::raw_string_ostream
OS(Buf
);
1180 logAllUnhandledErrors(Name
.takeError(), OS
);
1181 report_fatal_error(Twine(Buf
));
1184 SW
.printString("ExceptionRecord",
1185 formatSymbol(*Name
, XDataAddress
, XDataOffset
));
1187 Expected
<section_iterator
> SIOrErr
= XDataRecord
->getSection();
1189 // TODO: Actually report errors helpfully.
1190 consumeError(SIOrErr
.takeError());
1193 section_iterator SI
= *SIOrErr
;
1195 return dumpXDataRecord(COFF
, *SI
, FunctionAddress
, XDataAddress
);
1197 SW
.printString("ExceptionRecord", formatSymbol("", XDataAddress
));
1199 ErrorOr
<SectionRef
> Section
= getSectionContaining(COFF
, XDataAddress
);
1203 return dumpXDataRecord(COFF
, *Section
, FunctionAddress
, XDataAddress
);
1207 bool Decoder::dumpPackedEntry(const object::COFFObjectFile
&COFF
,
1208 const SectionRef Section
, uint64_t Offset
,
1209 unsigned Index
, const RuntimeFunction
&RF
) {
1210 assert((RF
.Flag() == RuntimeFunctionFlag::RFF_Packed
||
1211 RF
.Flag() == RuntimeFunctionFlag::RFF_PackedFragment
) &&
1212 "unpacked entry cannot be treated as a packed entry");
1214 uint64_t FunctionAddress
, FunctionOffset
;
1215 ErrorOr
<SymbolRef
> Function
= getSymbolForLocation(
1216 COFF
, Section
, Offset
, RF
.BeginAddress
, FunctionAddress
, FunctionOffset
,
1217 /*FunctionOnly=*/true);
1219 StringRef FunctionName
;
1221 Expected
<StringRef
> FunctionNameOrErr
= Function
->getName();
1222 if (!FunctionNameOrErr
) {
1224 llvm::raw_string_ostream
OS(Buf
);
1225 logAllUnhandledErrors(FunctionNameOrErr
.takeError(), OS
);
1226 report_fatal_error(Twine(Buf
));
1228 FunctionName
= *FunctionNameOrErr
;
1231 SW
.printString("Function",
1232 formatSymbol(FunctionName
, FunctionAddress
, FunctionOffset
));
1233 SW
.printBoolean("Fragment",
1234 RF
.Flag() == RuntimeFunctionFlag::RFF_PackedFragment
);
1235 SW
.printNumber("FunctionLength", RF
.FunctionLength());
1236 SW
.startLine() << "ReturnType: " << RF
.Ret() << '\n';
1237 SW
.printBoolean("HomedParameters", RF
.H());
1238 SW
.printNumber("Reg", RF
.Reg());
1239 SW
.printNumber("R", RF
.R());
1240 SW
.printBoolean("LinkRegister", RF
.L());
1241 SW
.printBoolean("Chaining", RF
.C());
1242 SW
.printNumber("StackAdjustment", StackAdjustment(RF
) << 2);
1245 ListScope
PS(SW
, "Prologue");
1247 uint16_t GPRMask
, VFPMask
;
1248 std::tie(GPRMask
, VFPMask
) = SavedRegisterMask(RF
, /*Prologue=*/true);
1250 if (StackAdjustment(RF
) && !PrologueFolding(RF
))
1251 SW
.startLine() << "sub sp, sp, #" << StackAdjustment(RF
) * 4 << "\n";
1253 SW
.startLine() << "vpush ";
1254 printVFPMask(VFPMask
);
1258 // Count the number of registers pushed below R11
1259 int FpOffset
= 4 * llvm::popcount(GPRMask
& ((1U << 11) - 1));
1261 SW
.startLine() << "add.w r11, sp, #" << FpOffset
<< "\n";
1263 SW
.startLine() << "mov r11, sp\n";
1266 SW
.startLine() << "push ";
1267 printGPRMask(GPRMask
);
1271 SW
.startLine() << "push {r0-r3}\n";
1274 if (RF
.Ret() != ReturnType::RT_NoEpilogue
) {
1275 ListScope
PS(SW
, "Epilogue");
1277 uint16_t GPRMask
, VFPMask
;
1278 std::tie(GPRMask
, VFPMask
) = SavedRegisterMask(RF
, /*Prologue=*/false);
1280 if (StackAdjustment(RF
) && !EpilogueFolding(RF
))
1281 SW
.startLine() << "add sp, sp, #" << StackAdjustment(RF
) * 4 << "\n";
1283 SW
.startLine() << "vpop ";
1284 printVFPMask(VFPMask
);
1288 SW
.startLine() << "pop ";
1289 printGPRMask(GPRMask
);
1293 if (RF
.L() == 0 || RF
.Ret() != ReturnType::RT_POP
)
1294 SW
.startLine() << "add sp, sp, #16\n";
1296 SW
.startLine() << "ldr pc, [sp], #20\n";
1298 if (RF
.Ret() != ReturnType::RT_POP
)
1299 SW
.startLine() << RF
.Ret() << '\n';
1305 bool Decoder::dumpPackedARM64Entry(const object::COFFObjectFile
&COFF
,
1306 const SectionRef Section
, uint64_t Offset
,
1308 const RuntimeFunctionARM64
&RF
) {
1309 assert((RF
.Flag() == RuntimeFunctionFlag::RFF_Packed
||
1310 RF
.Flag() == RuntimeFunctionFlag::RFF_PackedFragment
) &&
1311 "unpacked entry cannot be treated as a packed entry");
1313 uint64_t FunctionAddress
, FunctionOffset
;
1314 ErrorOr
<SymbolRef
> Function
= getSymbolForLocation(
1315 COFF
, Section
, Offset
, RF
.BeginAddress
, FunctionAddress
, FunctionOffset
,
1316 /*FunctionOnly=*/true);
1318 StringRef FunctionName
;
1320 Expected
<StringRef
> FunctionNameOrErr
= Function
->getName();
1321 if (!FunctionNameOrErr
) {
1323 llvm::raw_string_ostream
OS(Buf
);
1324 logAllUnhandledErrors(FunctionNameOrErr
.takeError(), OS
);
1325 report_fatal_error(Twine(Buf
));
1327 FunctionName
= *FunctionNameOrErr
;
1330 SW
.printString("Function",
1331 formatSymbol(FunctionName
, FunctionAddress
, FunctionOffset
));
1332 SW
.printBoolean("Fragment",
1333 RF
.Flag() == RuntimeFunctionFlag::RFF_PackedFragment
);
1334 SW
.printNumber("FunctionLength", RF
.FunctionLength());
1335 SW
.printNumber("RegF", RF
.RegF());
1336 SW
.printNumber("RegI", RF
.RegI());
1337 SW
.printBoolean("HomedParameters", RF
.H());
1338 SW
.printNumber("CR", RF
.CR());
1339 SW
.printNumber("FrameSize", RF
.FrameSize() << 4);
1340 ListScope
PS(SW
, "Prologue");
1342 // Synthesize the equivalent prologue according to the documentation
1343 // at https://docs.microsoft.com/en-us/cpp/build/arm64-exception-handling,
1344 // printed in reverse order compared to the docs, to match how prologues
1345 // are printed for the non-packed case.
1346 int IntSZ
= 8 * RF
.RegI();
1349 int FpSZ
= 8 * RF
.RegF();
1352 int SavSZ
= (IntSZ
+ FpSZ
+ 8 * 8 * RF
.H() + 0xf) & ~0xf;
1353 int LocSZ
= (RF
.FrameSize() << 4) - SavSZ
;
1355 if (RF
.CR() == 2 || RF
.CR() == 3) {
1356 SW
.startLine() << "mov x29, sp\n";
1358 SW
.startLine() << format("stp x29, lr, [sp, #-%d]!\n", LocSZ
);
1360 SW
.startLine() << "stp x29, lr, [sp, #0]\n";
1364 SW
.startLine() << format("sub sp, sp, #%d\n", LocSZ
- 4080);
1365 SW
.startLine() << "sub sp, sp, #4080\n";
1366 } else if ((RF
.CR() != 3 && RF
.CR() != 2 && LocSZ
> 0) || LocSZ
> 512) {
1367 SW
.startLine() << format("sub sp, sp, #%d\n", LocSZ
);
1370 SW
.startLine() << format("stp x6, x7, [sp, #%d]\n", SavSZ
- 16);
1371 SW
.startLine() << format("stp x4, x5, [sp, #%d]\n", SavSZ
- 32);
1372 SW
.startLine() << format("stp x2, x3, [sp, #%d]\n", SavSZ
- 48);
1373 if (RF
.RegI() > 0 || RF
.RegF() > 0 || RF
.CR() == 1) {
1374 SW
.startLine() << format("stp x0, x1, [sp, #%d]\n", SavSZ
- 64);
1376 // This case isn't documented; if neither RegI nor RegF nor CR=1
1377 // have decremented the stack pointer by SavSZ, we need to do it here
1378 // (as the final stack adjustment of LocSZ excludes SavSZ).
1379 SW
.startLine() << format("stp x0, x1, [sp, #-%d]!\n", SavSZ
);
1382 int FloatRegs
= RF
.RegF() > 0 ? RF
.RegF() + 1 : 0;
1383 for (int I
= (FloatRegs
+ 1) / 2 - 1; I
>= 0; I
--) {
1384 if (I
== (FloatRegs
+ 1) / 2 - 1 && FloatRegs
% 2 == 1) {
1385 // The last register, an odd register without a pair
1386 SW
.startLine() << format("str d%d, [sp, #%d]\n", 8 + 2 * I
,
1388 } else if (I
== 0 && RF
.RegI() == 0 && RF
.CR() != 1) {
1389 SW
.startLine() << format("stp d%d, d%d, [sp, #-%d]!\n", 8 + 2 * I
,
1390 8 + 2 * I
+ 1, SavSZ
);
1392 SW
.startLine() << format("stp d%d, d%d, [sp, #%d]\n", 8 + 2 * I
,
1393 8 + 2 * I
+ 1, IntSZ
+ 16 * I
);
1396 if (RF
.CR() == 1 && (RF
.RegI() % 2) == 0) {
1398 SW
.startLine() << format("str lr, [sp, #-%d]!\n", SavSZ
);
1400 SW
.startLine() << format("str lr, [sp, #%d]\n", IntSZ
- 8);
1402 for (int I
= (RF
.RegI() + 1) / 2 - 1; I
>= 0; I
--) {
1403 if (I
== (RF
.RegI() + 1) / 2 - 1 && RF
.RegI() % 2 == 1) {
1404 // The last register, an odd register without a pair
1406 if (I
== 0) { // If this is the only register pair
1407 // CR=1 combined with RegI=1 doesn't map to a documented case;
1408 // it doesn't map to any regular unwind info opcode, and the
1409 // actual unwinder doesn't support it.
1410 SW
.startLine() << "INVALID!\n";
1412 SW
.startLine() << format("stp x%d, lr, [sp, #%d]\n", 19 + 2 * I
,
1416 SW
.startLine() << format("str x%d, [sp, #-%d]!\n", 19 + 2 * I
, SavSZ
);
1418 SW
.startLine() << format("str x%d, [sp, #%d]\n", 19 + 2 * I
, 16 * I
);
1420 } else if (I
== 0) {
1421 // The first register pair
1422 SW
.startLine() << format("stp x19, x20, [sp, #-%d]!\n", SavSZ
);
1424 SW
.startLine() << format("stp x%d, x%d, [sp, #%d]\n", 19 + 2 * I
,
1425 19 + 2 * I
+ 1, 16 * I
);
1428 // CR=2 is yet undocumented, see
1429 // https://github.com/MicrosoftDocs/cpp-docs/pull/4202 for upstream
1430 // progress on getting it documented.
1432 SW
.startLine() << "pacibsp\n";
1433 SW
.startLine() << "end\n";
1438 bool Decoder::dumpProcedureDataEntry(const COFFObjectFile
&COFF
,
1439 const SectionRef Section
, unsigned Index
,
1440 ArrayRef
<uint8_t> Contents
) {
1441 uint64_t Offset
= PDataEntrySize
* Index
;
1442 const ulittle32_t
*Data
=
1443 reinterpret_cast<const ulittle32_t
*>(Contents
.data() + Offset
);
1445 const RuntimeFunction
Entry(Data
);
1446 DictScope
RFS(SW
, "RuntimeFunction");
1447 if (Entry
.Flag() == RuntimeFunctionFlag::RFF_Unpacked
)
1448 return dumpUnpackedEntry(COFF
, Section
, Offset
, Index
, Entry
);
1450 const RuntimeFunctionARM64
EntryARM64(Data
);
1451 return dumpPackedARM64Entry(COFF
, Section
, Offset
, Index
, EntryARM64
);
1453 return dumpPackedEntry(COFF
, Section
, Offset
, Index
, Entry
);
1456 void Decoder::dumpProcedureData(const COFFObjectFile
&COFF
,
1457 const SectionRef Section
) {
1458 ArrayRef
<uint8_t> Contents
;
1459 if (COFF
.getSectionContents(COFF
.getCOFFSection(Section
), Contents
))
1462 if (Contents
.size() % PDataEntrySize
) {
1463 errs() << ".pdata content is not " << PDataEntrySize
<< "-byte aligned\n";
1467 for (unsigned EI
= 0, EE
= Contents
.size() / PDataEntrySize
; EI
< EE
; ++EI
)
1468 if (!dumpProcedureDataEntry(COFF
, Section
, EI
, Contents
))
1472 Error
Decoder::dumpProcedureData(const COFFObjectFile
&COFF
) {
1473 for (const auto &Section
: COFF
.sections()) {
1474 Expected
<StringRef
> NameOrErr
=
1475 COFF
.getSectionName(COFF
.getCOFFSection(Section
));
1477 return NameOrErr
.takeError();
1479 if (NameOrErr
->starts_with(".pdata"))
1480 dumpProcedureData(COFF
, Section
);
1482 return Error::success();