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"
66 #include "llvm/ADT/STLExtras.h"
67 #include "llvm/ADT/StringExtras.h"
68 #include "llvm/Support/ARMWinEH.h"
69 #include "llvm/Support/Format.h"
72 using namespace llvm::object
;
73 using namespace llvm::support
;
76 raw_ostream
&operator<<(raw_ostream
&OS
, const ARM::WinEH::ReturnType
&RT
) {
78 case ARM::WinEH::ReturnType::RT_POP
:
81 case ARM::WinEH::ReturnType::RT_B
:
84 case ARM::WinEH::ReturnType::RT_BW
:
87 case ARM::WinEH::ReturnType::RT_NoEpilogue
:
88 OS
<< "(no epilogue)";
95 static std::string
formatSymbol(StringRef Name
, uint64_t Address
,
96 uint64_t Offset
= 0) {
98 raw_string_ostream
OS(Buffer
);
104 OS
<< format("+0x%X (0x%" PRIX64
")", Offset
, Address
);
105 else if (!Name
.empty())
106 OS
<< format("(0x%" PRIX64
")", Address
);
108 OS
<< format("0x%" PRIX64
, Address
);
116 const size_t Decoder::PDataEntrySize
= sizeof(RuntimeFunction
);
118 // TODO name the uops more appropriately
119 const Decoder::RingEntry
Decoder::Ring
[] = {
120 { 0x80, 0x00, 1, &Decoder::opcode_0xxxxxxx
}, // UOP_STACK_FREE (16-bit)
121 { 0xc0, 0x80, 2, &Decoder::opcode_10Lxxxxx
}, // UOP_POP (32-bit)
122 { 0xf0, 0xc0, 1, &Decoder::opcode_1100xxxx
}, // UOP_STACK_SAVE (16-bit)
123 { 0xf8, 0xd0, 1, &Decoder::opcode_11010Lxx
}, // UOP_POP (16-bit)
124 { 0xf8, 0xd8, 1, &Decoder::opcode_11011Lxx
}, // UOP_POP (32-bit)
125 { 0xf8, 0xe0, 1, &Decoder::opcode_11100xxx
}, // UOP_VPOP (32-bit)
126 { 0xfc, 0xe8, 2, &Decoder::opcode_111010xx
}, // UOP_STACK_FREE (32-bit)
127 { 0xfe, 0xec, 2, &Decoder::opcode_1110110L
}, // UOP_POP (16-bit)
128 { 0xff, 0xee, 2, &Decoder::opcode_11101110
}, // UOP_MICROSOFT_SPECIFIC (16-bit)
129 // UOP_PUSH_MACHINE_FRAME
131 // UOP_PUSH_TRAP_FRAME
132 // UOP_REDZONE_RESTORE_LR
133 { 0xff, 0xef, 2, &Decoder::opcode_11101111
}, // UOP_LDRPC_POSTINC (32-bit)
134 { 0xff, 0xf5, 2, &Decoder::opcode_11110101
}, // UOP_VPOP (32-bit)
135 { 0xff, 0xf6, 2, &Decoder::opcode_11110110
}, // UOP_VPOP (32-bit)
136 { 0xff, 0xf7, 3, &Decoder::opcode_11110111
}, // UOP_STACK_RESTORE (16-bit)
137 { 0xff, 0xf8, 4, &Decoder::opcode_11111000
}, // UOP_STACK_RESTORE (16-bit)
138 { 0xff, 0xf9, 3, &Decoder::opcode_11111001
}, // UOP_STACK_RESTORE (32-bit)
139 { 0xff, 0xfa, 4, &Decoder::opcode_11111010
}, // UOP_STACK_RESTORE (32-bit)
140 { 0xff, 0xfb, 1, &Decoder::opcode_11111011
}, // UOP_NOP (16-bit)
141 { 0xff, 0xfc, 1, &Decoder::opcode_11111100
}, // UOP_NOP (32-bit)
142 { 0xff, 0xfd, 1, &Decoder::opcode_11111101
}, // UOP_NOP (16-bit) / END
143 { 0xff, 0xfe, 1, &Decoder::opcode_11111110
}, // UOP_NOP (32-bit) / END
144 { 0xff, 0xff, 1, &Decoder::opcode_11111111
}, // UOP_END
148 // Unwind opcodes for ARM64.
149 // https://docs.microsoft.com/en-us/cpp/build/arm64-exception-handling
150 const Decoder::RingEntry
Decoder::Ring64
[] = {
151 { 0xe0, 0x00, 1, &Decoder::opcode_alloc_s
},
152 { 0xe0, 0x20, 1, &Decoder::opcode_save_r19r20_x
},
153 { 0xc0, 0x40, 1, &Decoder::opcode_save_fplr
},
154 { 0xc0, 0x80, 1, &Decoder::opcode_save_fplr_x
},
155 { 0xf8, 0xc0, 2, &Decoder::opcode_alloc_m
},
156 { 0xfc, 0xc8, 2, &Decoder::opcode_save_regp
},
157 { 0xfc, 0xcc, 2, &Decoder::opcode_save_regp_x
},
158 { 0xfc, 0xd0, 2, &Decoder::opcode_save_reg
},
159 { 0xfe, 0xd4, 2, &Decoder::opcode_save_reg_x
},
160 { 0xfe, 0xd6, 2, &Decoder::opcode_save_lrpair
},
161 { 0xfe, 0xd8, 2, &Decoder::opcode_save_fregp
},
162 { 0xfe, 0xda, 2, &Decoder::opcode_save_fregp_x
},
163 { 0xfe, 0xdc, 2, &Decoder::opcode_save_freg
},
164 { 0xff, 0xde, 2, &Decoder::opcode_save_freg_x
},
165 { 0xff, 0xe0, 4, &Decoder::opcode_alloc_l
},
166 { 0xff, 0xe1, 1, &Decoder::opcode_setfp
},
167 { 0xff, 0xe2, 2, &Decoder::opcode_addfp
},
168 { 0xff, 0xe3, 1, &Decoder::opcode_nop
},
169 { 0xff, 0xe4, 1, &Decoder::opcode_end
},
170 { 0xff, 0xe5, 1, &Decoder::opcode_end_c
},
173 void Decoder::printRegisters(const std::pair
<uint16_t, uint32_t> &RegisterMask
) {
174 static const char * const GPRRegisterNames
[16] = {
175 "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7", "r8", "r9", "r10",
176 "r11", "ip", "sp", "lr", "pc",
179 const uint16_t GPRMask
= std::get
<0>(RegisterMask
);
180 const uint16_t VFPMask
= std::get
<1>(RegisterMask
);
184 for (unsigned RI
= 0, RE
= 11; RI
< RE
; ++RI
) {
185 if (GPRMask
& (1 << RI
)) {
188 OS
<< GPRRegisterNames
[RI
];
192 for (unsigned RI
= 0, RE
= 32; RI
< RE
; ++RI
) {
193 if (VFPMask
& (1 << RI
)) {
196 OS
<< "d" << unsigned(RI
);
200 for (unsigned RI
= 11, RE
= 16; RI
< RE
; ++RI
) {
201 if (GPRMask
& (1 << RI
)) {
204 OS
<< GPRRegisterNames
[RI
];
211 ErrorOr
<object::SectionRef
>
212 Decoder::getSectionContaining(const COFFObjectFile
&COFF
, uint64_t VA
) {
213 for (const auto &Section
: COFF
.sections()) {
214 uint64_t Address
= Section
.getAddress();
215 uint64_t Size
= Section
.getSize();
217 if (VA
>= Address
&& (VA
- Address
) <= Size
)
220 return readobj_error::unknown_symbol
;
223 ErrorOr
<object::SymbolRef
> Decoder::getSymbol(const COFFObjectFile
&COFF
,
224 uint64_t VA
, bool FunctionOnly
) {
225 for (const auto &Symbol
: COFF
.symbols()) {
226 Expected
<SymbolRef::Type
> Type
= Symbol
.getType();
228 return errorToErrorCode(Type
.takeError());
229 if (FunctionOnly
&& *Type
!= SymbolRef::ST_Function
)
232 Expected
<uint64_t> Address
= Symbol
.getAddress();
234 return errorToErrorCode(Address
.takeError());
238 return readobj_error::unknown_symbol
;
241 ErrorOr
<SymbolRef
> Decoder::getRelocatedSymbol(const COFFObjectFile
&,
242 const SectionRef
&Section
,
244 for (const auto &Relocation
: Section
.relocations()) {
245 uint64_t RelocationOffset
= Relocation
.getOffset();
246 if (RelocationOffset
== Offset
)
247 return *Relocation
.getSymbol();
249 return readobj_error::unknown_symbol
;
252 bool Decoder::opcode_0xxxxxxx(const uint8_t *OC
, unsigned &Offset
,
253 unsigned Length
, bool Prologue
) {
254 uint8_t Imm
= OC
[Offset
] & 0x7f;
255 SW
.startLine() << format("0x%02x ; %s sp, #(%u * 4)\n",
257 static_cast<const char *>(Prologue
? "sub" : "add"),
263 bool Decoder::opcode_10Lxxxxx(const uint8_t *OC
, unsigned &Offset
,
264 unsigned Length
, bool Prologue
) {
265 unsigned Link
= (OC
[Offset
] & 0x20) >> 5;
266 uint16_t RegisterMask
= (Link
<< (Prologue
? 14 : 15))
267 | ((OC
[Offset
+ 0] & 0x1f) << 8)
268 | ((OC
[Offset
+ 1] & 0xff) << 0);
269 assert((~RegisterMask
& (1 << 13)) && "sp must not be set");
270 assert((~RegisterMask
& (1 << (Prologue
? 15 : 14))) && "pc must not be set");
272 SW
.startLine() << format("0x%02x 0x%02x ; %s.w ",
273 OC
[Offset
+ 0], OC
[Offset
+ 1],
274 Prologue
? "push" : "pop");
275 printRegisters(std::make_pair(RegisterMask
, 0));
282 bool Decoder::opcode_1100xxxx(const uint8_t *OC
, unsigned &Offset
,
283 unsigned Length
, bool Prologue
) {
285 SW
.startLine() << format("0x%02x ; mov r%u, sp\n",
286 OC
[Offset
], OC
[Offset
] & 0xf);
288 SW
.startLine() << format("0x%02x ; mov sp, r%u\n",
289 OC
[Offset
], OC
[Offset
] & 0xf);
294 bool Decoder::opcode_11010Lxx(const uint8_t *OC
, unsigned &Offset
,
295 unsigned Length
, bool Prologue
) {
296 unsigned Link
= (OC
[Offset
] & 0x4) >> 3;
297 unsigned Count
= (OC
[Offset
] & 0x3);
299 uint16_t GPRMask
= (Link
<< (Prologue
? 14 : 15))
300 | (((1 << (Count
+ 1)) - 1) << 4);
302 SW
.startLine() << format("0x%02x ; %s ", OC
[Offset
],
303 Prologue
? "push" : "pop");
304 printRegisters(std::make_pair(GPRMask
, 0));
311 bool Decoder::opcode_11011Lxx(const uint8_t *OC
, unsigned &Offset
,
312 unsigned Length
, bool Prologue
) {
313 unsigned Link
= (OC
[Offset
] & 0x4) >> 2;
314 unsigned Count
= (OC
[Offset
] & 0x3) + 4;
316 uint16_t GPRMask
= (Link
<< (Prologue
? 14 : 15))
317 | (((1 << (Count
+ 1)) - 1) << 4);
319 SW
.startLine() << format("0x%02x ; %s.w ", OC
[Offset
],
320 Prologue
? "push" : "pop");
321 printRegisters(std::make_pair(GPRMask
, 0));
328 bool Decoder::opcode_11100xxx(const uint8_t *OC
, unsigned &Offset
,
329 unsigned Length
, bool Prologue
) {
330 unsigned High
= (OC
[Offset
] & 0x7);
331 uint32_t VFPMask
= (((1 << (High
+ 1)) - 1) << 8);
333 SW
.startLine() << format("0x%02x ; %s ", OC
[Offset
],
334 Prologue
? "vpush" : "vpop");
335 printRegisters(std::make_pair(0, VFPMask
));
342 bool Decoder::opcode_111010xx(const uint8_t *OC
, unsigned &Offset
,
343 unsigned Length
, bool Prologue
) {
344 uint16_t Imm
= ((OC
[Offset
+ 0] & 0x03) << 8) | ((OC
[Offset
+ 1] & 0xff) << 0);
346 SW
.startLine() << format("0x%02x 0x%02x ; %s.w sp, #(%u * 4)\n",
347 OC
[Offset
+ 0], OC
[Offset
+ 1],
348 static_cast<const char *>(Prologue
? "sub" : "add"),
355 bool Decoder::opcode_1110110L(const uint8_t *OC
, unsigned &Offset
,
356 unsigned Length
, bool Prologue
) {
357 uint8_t GPRMask
= ((OC
[Offset
+ 0] & 0x01) << (Prologue
? 14 : 15))
358 | ((OC
[Offset
+ 1] & 0xff) << 0);
360 SW
.startLine() << format("0x%02x 0x%02x ; %s ", OC
[Offset
+ 0],
361 OC
[Offset
+ 1], Prologue
? "push" : "pop");
362 printRegisters(std::make_pair(GPRMask
, 0));
369 bool Decoder::opcode_11101110(const uint8_t *OC
, unsigned &Offset
,
370 unsigned Length
, bool Prologue
) {
371 assert(!Prologue
&& "may not be used in prologue");
373 if (OC
[Offset
+ 1] & 0xf0)
374 SW
.startLine() << format("0x%02x 0x%02x ; reserved\n",
375 OC
[Offset
+ 0], OC
[Offset
+ 1]);
378 << format("0x%02x 0x%02x ; microsoft-specific (type: %u)\n",
379 OC
[Offset
+ 0], OC
[Offset
+ 1], OC
[Offset
+ 1] & 0x0f);
385 bool Decoder::opcode_11101111(const uint8_t *OC
, unsigned &Offset
,
386 unsigned Length
, bool Prologue
) {
387 assert(!Prologue
&& "may not be used in prologue");
389 if (OC
[Offset
+ 1] & 0xf0)
390 SW
.startLine() << format("0x%02x 0x%02x ; reserved\n",
391 OC
[Offset
+ 0], OC
[Offset
+ 1]);
394 << format("0x%02x 0x%02x ; ldr.w lr, [sp], #%u\n",
395 OC
[Offset
+ 0], OC
[Offset
+ 1], OC
[Offset
+ 1] << 2);
401 bool Decoder::opcode_11110101(const uint8_t *OC
, unsigned &Offset
,
402 unsigned Length
, bool Prologue
) {
403 unsigned Start
= (OC
[Offset
+ 1] & 0xf0) >> 4;
404 unsigned End
= (OC
[Offset
+ 1] & 0x0f) >> 0;
405 uint32_t VFPMask
= ((1 << (End
- Start
)) - 1) << Start
;
407 SW
.startLine() << format("0x%02x 0x%02x ; %s ", OC
[Offset
+ 0],
408 OC
[Offset
+ 1], Prologue
? "vpush" : "vpop");
409 printRegisters(std::make_pair(0, VFPMask
));
416 bool Decoder::opcode_11110110(const uint8_t *OC
, unsigned &Offset
,
417 unsigned Length
, bool Prologue
) {
418 unsigned Start
= (OC
[Offset
+ 1] & 0xf0) >> 4;
419 unsigned End
= (OC
[Offset
+ 1] & 0x0f) >> 0;
420 uint32_t VFPMask
= ((1 << (End
- Start
)) - 1) << 16;
422 SW
.startLine() << format("0x%02x 0x%02x ; %s ", OC
[Offset
+ 0],
423 OC
[Offset
+ 1], Prologue
? "vpush" : "vpop");
424 printRegisters(std::make_pair(0, VFPMask
));
431 bool Decoder::opcode_11110111(const uint8_t *OC
, unsigned &Offset
,
432 unsigned Length
, bool Prologue
) {
433 uint32_t Imm
= (OC
[Offset
+ 1] << 8) | (OC
[Offset
+ 2] << 0);
435 SW
.startLine() << format("0x%02x 0x%02x 0x%02x ; %s sp, sp, #(%u * 4)\n",
436 OC
[Offset
+ 0], OC
[Offset
+ 1], OC
[Offset
+ 2],
437 static_cast<const char *>(Prologue
? "sub" : "add"),
444 bool Decoder::opcode_11111000(const uint8_t *OC
, unsigned &Offset
,
445 unsigned Length
, bool Prologue
) {
446 uint32_t Imm
= (OC
[Offset
+ 1] << 16)
447 | (OC
[Offset
+ 2] << 8)
448 | (OC
[Offset
+ 3] << 0);
451 << format("0x%02x 0x%02x 0x%02x 0x%02x ; %s sp, sp, #(%u * 4)\n",
452 OC
[Offset
+ 0], OC
[Offset
+ 1], OC
[Offset
+ 2], OC
[Offset
+ 3],
453 static_cast<const char *>(Prologue
? "sub" : "add"), Imm
);
459 bool Decoder::opcode_11111001(const uint8_t *OC
, unsigned &Offset
,
460 unsigned Length
, bool Prologue
) {
461 uint32_t Imm
= (OC
[Offset
+ 1] << 8) | (OC
[Offset
+ 2] << 0);
464 << format("0x%02x 0x%02x 0x%02x ; %s.w sp, sp, #(%u * 4)\n",
465 OC
[Offset
+ 0], OC
[Offset
+ 1], OC
[Offset
+ 2],
466 static_cast<const char *>(Prologue
? "sub" : "add"), Imm
);
472 bool Decoder::opcode_11111010(const uint8_t *OC
, unsigned &Offset
,
473 unsigned Length
, bool Prologue
) {
474 uint32_t Imm
= (OC
[Offset
+ 1] << 16)
475 | (OC
[Offset
+ 2] << 8)
476 | (OC
[Offset
+ 3] << 0);
479 << format("0x%02x 0x%02x 0x%02x 0x%02x ; %s.w sp, sp, #(%u * 4)\n",
480 OC
[Offset
+ 0], OC
[Offset
+ 1], OC
[Offset
+ 2], OC
[Offset
+ 3],
481 static_cast<const char *>(Prologue
? "sub" : "add"), Imm
);
487 bool Decoder::opcode_11111011(const uint8_t *OC
, unsigned &Offset
,
488 unsigned Length
, bool Prologue
) {
489 SW
.startLine() << format("0x%02x ; nop\n", OC
[Offset
]);
494 bool Decoder::opcode_11111100(const uint8_t *OC
, unsigned &Offset
,
495 unsigned Length
, bool Prologue
) {
496 SW
.startLine() << format("0x%02x ; nop.w\n", OC
[Offset
]);
501 bool Decoder::opcode_11111101(const uint8_t *OC
, unsigned &Offset
,
502 unsigned Length
, bool Prologue
) {
503 SW
.startLine() << format("0x%02x ; b\n", OC
[Offset
]);
508 bool Decoder::opcode_11111110(const uint8_t *OC
, unsigned &Offset
,
509 unsigned Length
, bool Prologue
) {
510 SW
.startLine() << format("0x%02x ; b.w\n", OC
[Offset
]);
515 bool Decoder::opcode_11111111(const uint8_t *OC
, unsigned &Offset
,
516 unsigned Length
, bool Prologue
) {
521 // ARM64 unwind codes start here.
522 bool Decoder::opcode_alloc_s(const uint8_t *OC
, unsigned &Offset
,
523 unsigned Length
, bool Prologue
) {
524 uint32_t NumBytes
= (OC
[Offset
] & 0x1F) << 4;
525 SW
.startLine() << format("0x%02x ; %s sp, #%u\n", OC
[Offset
],
526 static_cast<const char *>(Prologue
? "sub" : "add"),
532 bool Decoder::opcode_save_r19r20_x(const uint8_t *OC
, unsigned &Offset
,
533 unsigned Length
, bool Prologue
) {
534 uint32_t Off
= (OC
[Offset
] & 0x1F) << 3;
536 SW
.startLine() << format(
537 "0x%02x ; stp x19, x20, [sp, #-%u]!\n", OC
[Offset
], Off
);
539 SW
.startLine() << format(
540 "0x%02x ; ldp x19, x20, [sp], #%u\n", OC
[Offset
], Off
);
545 bool Decoder::opcode_save_fplr(const uint8_t *OC
, unsigned &Offset
,
546 unsigned Length
, bool Prologue
) {
547 uint32_t Off
= (OC
[Offset
] & 0x3F) << 3;
548 SW
.startLine() << format(
549 "0x%02x ; %s x29, x30, [sp, #%u]\n", OC
[Offset
],
550 static_cast<const char *>(Prologue
? "stp" : "ldp"), Off
);
555 bool Decoder::opcode_save_fplr_x(const uint8_t *OC
, unsigned &Offset
,
556 unsigned Length
, bool Prologue
) {
557 uint32_t Off
= ((OC
[Offset
] & 0x3F) + 1) << 3;
559 SW
.startLine() << format(
560 "0x%02x ; stp x29, x30, [sp, #-%u]!\n", OC
[Offset
], Off
);
562 SW
.startLine() << format(
563 "0x%02x ; ldp x29, x30, [sp], #%u\n", OC
[Offset
], Off
);
568 bool Decoder::opcode_alloc_m(const uint8_t *OC
, unsigned &Offset
,
569 unsigned Length
, bool Prologue
) {
570 uint32_t NumBytes
= ((OC
[Offset
] & 0x07) << 8);
571 NumBytes
|= (OC
[Offset
+ 1] & 0xFF);
573 SW
.startLine() << format("0x%02x%02x ; %s sp, #%u\n",
574 OC
[Offset
], OC
[Offset
+ 1],
575 static_cast<const char *>(Prologue
? "sub" : "add"),
581 bool Decoder::opcode_save_regp(const uint8_t *OC
, unsigned &Offset
,
582 unsigned Length
, bool Prologue
) {
583 uint32_t Reg
= ((OC
[Offset
] & 0x03) << 8);
584 Reg
|= (OC
[Offset
+ 1] & 0xC0);
587 uint32_t Off
= (OC
[Offset
+ 1] & 0x3F) << 3;
588 SW
.startLine() << format(
589 "0x%02x%02x ; %s x%u, x%u, [sp, #%u]\n",
590 OC
[Offset
], OC
[Offset
+ 1],
591 static_cast<const char *>(Prologue
? "stp" : "ldp"), Reg
, Reg
+ 1, Off
);
596 bool Decoder::opcode_save_regp_x(const uint8_t *OC
, unsigned &Offset
,
597 unsigned Length
, bool Prologue
) {
598 uint32_t Reg
= ((OC
[Offset
] & 0x03) << 8);
599 Reg
|= (OC
[Offset
+ 1] & 0xC0);
602 uint32_t Off
= ((OC
[Offset
+ 1] & 0x3F) + 1) << 3;
604 SW
.startLine() << format(
605 "0x%02x%02x ; stp x%u, x%u, [sp, #-%u]!\n",
606 OC
[Offset
], OC
[Offset
+ 1], Reg
,
609 SW
.startLine() << format(
610 "0x%02x%02x ; ldp x%u, x%u, [sp], #%u\n",
611 OC
[Offset
], OC
[Offset
+ 1], Reg
,
617 bool Decoder::opcode_save_reg(const uint8_t *OC
, unsigned &Offset
,
618 unsigned Length
, bool Prologue
) {
619 uint32_t Reg
= (OC
[Offset
] & 0x03) << 8;
620 Reg
|= (OC
[Offset
+ 1] & 0xC0);
623 uint32_t Off
= (OC
[Offset
+ 1] & 0x3F) << 3;
624 SW
.startLine() << format("0x%02x%02x ; %s x%u, [sp, #%u]\n",
625 OC
[Offset
], OC
[Offset
+ 1],
626 static_cast<const char *>(Prologue
? "str" : "ldr"),
632 bool Decoder::opcode_save_reg_x(const uint8_t *OC
, unsigned &Offset
,
633 unsigned Length
, bool Prologue
) {
634 uint32_t Reg
= (OC
[Offset
] & 0x01) << 8;
635 Reg
|= (OC
[Offset
+ 1] & 0xE0);
638 uint32_t Off
= ((OC
[Offset
+ 1] & 0x1F) + 1) << 3;
640 SW
.startLine() << format("0x%02x%02x ; str x%u, [sp, #%u]!\n",
641 OC
[Offset
], OC
[Offset
+ 1], Reg
, Off
);
643 SW
.startLine() << format("0x%02x%02x ; ldr x%u, [sp], #%u\n",
644 OC
[Offset
], OC
[Offset
+ 1], Reg
, Off
);
649 bool Decoder::opcode_save_lrpair(const uint8_t *OC
, unsigned &Offset
,
650 unsigned Length
, bool Prologue
) {
651 uint32_t Reg
= (OC
[Offset
] & 0x01) << 8;
652 Reg
|= (OC
[Offset
+ 1] & 0xC0);
656 uint32_t Off
= (OC
[Offset
+ 1] & 0x3F) << 3;
657 SW
.startLine() << format("0x%02x%02x ; %s x%u, lr, [sp, #%u]\n",
658 OC
[Offset
], OC
[Offset
+ 1],
659 static_cast<const char *>(Prologue
? "stp" : "ldp"),
665 bool Decoder::opcode_save_fregp(const uint8_t *OC
, unsigned &Offset
,
666 unsigned Length
, bool Prologue
) {
667 uint32_t Reg
= (OC
[Offset
] & 0x01) << 8;
668 Reg
|= (OC
[Offset
+ 1] & 0xC0);
671 uint32_t Off
= (OC
[Offset
+ 1] & 0x3F) << 3;
672 SW
.startLine() << format("0x%02x%02x ; %s d%u, d%u, [sp, #%u]\n",
673 OC
[Offset
], OC
[Offset
+ 1],
674 static_cast<const char *>(Prologue
? "stp" : "ldp"),
680 bool Decoder::opcode_save_fregp_x(const uint8_t *OC
, unsigned &Offset
,
681 unsigned Length
, bool Prologue
) {
682 uint32_t Reg
= (OC
[Offset
] & 0x01) << 8;
683 Reg
|= (OC
[Offset
+ 1] & 0xC0);
686 uint32_t Off
= ((OC
[Offset
+ 1] & 0x3F) + 1) << 3;
688 SW
.startLine() << format(
689 "0x%02x%02x ; stp d%u, d%u, [sp, #-%u]!\n", OC
[Offset
],
690 OC
[Offset
+ 1], Reg
, Reg
+ 1, Off
);
692 SW
.startLine() << format(
693 "0x%02x%02x ; ldp d%u, d%u, [sp], #%u\n", OC
[Offset
],
694 OC
[Offset
+ 1], Reg
, Reg
+ 1, Off
);
699 bool Decoder::opcode_save_freg(const uint8_t *OC
, unsigned &Offset
,
700 unsigned Length
, bool Prologue
) {
701 uint32_t Reg
= (OC
[Offset
] & 0x01) << 8;
702 Reg
|= (OC
[Offset
+ 1] & 0xC0);
705 uint32_t Off
= (OC
[Offset
+ 1] & 0x3F) << 3;
706 SW
.startLine() << format("0x%02x%02x ; %s d%u, [sp, #%u]\n",
707 OC
[Offset
], OC
[Offset
+ 1],
708 static_cast<const char *>(Prologue
? "str" : "ldr"),
714 bool Decoder::opcode_save_freg_x(const uint8_t *OC
, unsigned &Offset
,
715 unsigned Length
, bool Prologue
) {
716 uint32_t Reg
= ((OC
[Offset
+ 1] & 0xE0) >> 5) + 8;
717 uint32_t Off
= ((OC
[Offset
+ 1] & 0x1F) + 1) << 3;
719 SW
.startLine() << format(
720 "0x%02x%02x ; str d%u, [sp, #-%u]!\n", OC
[Offset
],
721 OC
[Offset
+ 1], Reg
, Off
);
723 SW
.startLine() << format(
724 "0x%02x%02x ; ldr d%u, [sp], #%u\n", OC
[Offset
],
725 OC
[Offset
+ 1], Reg
, Off
);
730 bool Decoder::opcode_alloc_l(const uint8_t *OC
, unsigned &Offset
,
731 unsigned Length
, bool Prologue
) {
733 (OC
[Offset
+ 1] << 16) | (OC
[Offset
+ 2] << 8) | (OC
[Offset
+ 3] << 0);
735 SW
.startLine() << format(
736 "0x%02x%02x%02x%02x ; %s sp, #%u\n", OC
[Offset
], OC
[Offset
+ 1],
737 OC
[Offset
+ 2], OC
[Offset
+ 3],
738 static_cast<const char *>(Prologue
? "sub" : "add"), Off
);
743 bool Decoder::opcode_setfp(const uint8_t *OC
, unsigned &Offset
, unsigned Length
,
745 SW
.startLine() << format("0x%02x ; mov fp, sp\n", OC
[Offset
]);
750 bool Decoder::opcode_addfp(const uint8_t *OC
, unsigned &Offset
, unsigned Length
,
752 unsigned NumBytes
= OC
[Offset
+ 1] << 3;
753 SW
.startLine() << format("0x%02x%02x ; add fp, sp, #%u\n",
754 OC
[Offset
], OC
[Offset
+ 1], NumBytes
);
759 bool Decoder::opcode_nop(const uint8_t *OC
, unsigned &Offset
, unsigned Length
,
761 SW
.startLine() << format("0x%02x ; nop\n", OC
[Offset
]);
766 bool Decoder::opcode_end(const uint8_t *OC
, unsigned &Offset
, unsigned Length
,
768 SW
.startLine() << format("0x%02x ; end\n", OC
[Offset
]);
773 bool Decoder::opcode_end_c(const uint8_t *OC
, unsigned &Offset
, unsigned Length
,
775 SW
.startLine() << format("0x%02x ; end_c\n", OC
[Offset
]);
780 void Decoder::decodeOpcodes(ArrayRef
<uint8_t> Opcodes
, unsigned Offset
,
782 assert((!Prologue
|| Offset
== 0) && "prologue should always use offset 0");
783 const RingEntry
* DecodeRing
= isAArch64
? Ring64
: Ring
;
784 bool Terminated
= false;
785 for (unsigned OI
= Offset
, OE
= Opcodes
.size(); !Terminated
&& OI
< OE
; ) {
786 for (unsigned DI
= 0;; ++DI
) {
787 if ((isAArch64
&& (DI
>= array_lengthof(Ring64
))) ||
788 (!isAArch64
&& (DI
>= array_lengthof(Ring
)))) {
789 SW
.startLine() << format("0x%02x ; Bad opcode!\n",
795 if ((Opcodes
[OI
] & DecodeRing
[DI
].Mask
) == DecodeRing
[DI
].Value
) {
796 if (OI
+ DecodeRing
[DI
].Length
> OE
) {
797 SW
.startLine() << format("Opcode 0x%02x goes past the unwind data\n",
799 OI
+= DecodeRing
[DI
].Length
;
803 (this->*DecodeRing
[DI
].Routine
)(Opcodes
.data(), OI
, 0, Prologue
);
810 bool Decoder::dumpXDataRecord(const COFFObjectFile
&COFF
,
811 const SectionRef
&Section
,
812 uint64_t FunctionAddress
, uint64_t VA
) {
813 ArrayRef
<uint8_t> Contents
;
814 if (COFF
.getSectionContents(COFF
.getCOFFSection(Section
), Contents
))
817 uint64_t SectionVA
= Section
.getAddress();
818 uint64_t Offset
= VA
- SectionVA
;
819 const ulittle32_t
*Data
=
820 reinterpret_cast<const ulittle32_t
*>(Contents
.data() + Offset
);
822 // Sanity check to ensure that the .xdata header is present.
823 // A header is one or two words, followed by at least one word to describe
824 // the unwind codes. Applicable to both ARM and AArch64.
825 if (Contents
.size() - Offset
< 8)
826 report_fatal_error(".xdata must be at least 8 bytes in size");
828 const ExceptionDataRecord
XData(Data
, isAArch64
);
829 DictScope
XRS(SW
, "ExceptionData");
830 SW
.printNumber("FunctionLength",
831 isAArch64
? XData
.FunctionLengthInBytesAArch64() :
832 XData
.FunctionLengthInBytesARM());
833 SW
.printNumber("Version", XData
.Vers());
834 SW
.printBoolean("ExceptionData", XData
.X());
835 SW
.printBoolean("EpiloguePacked", XData
.E());
837 SW
.printBoolean("Fragment", XData
.F());
838 SW
.printNumber(XData
.E() ? "EpilogueOffset" : "EpilogueScopes",
839 XData
.EpilogueCount());
840 uint64_t ByteCodeLength
= XData
.CodeWords() * sizeof(uint32_t);
841 SW
.printNumber("ByteCodeLength", ByteCodeLength
);
843 if ((int64_t)(Contents
.size() - Offset
- 4 * HeaderWords(XData
) -
844 (XData
.E() ? 0 : XData
.EpilogueCount() * 4) -
845 (XData
.X() ? 8 : 0)) < (int64_t)ByteCodeLength
) {
847 report_fatal_error("Malformed unwind data");
851 ArrayRef
<uint8_t> UC
= XData
.UnwindByteCode();
852 if (isAArch64
|| !XData
.F()) {
853 ListScope
PS(SW
, "Prologue");
854 decodeOpcodes(UC
, 0, /*Prologue=*/true);
856 if (XData
.EpilogueCount()) {
857 ListScope
ES(SW
, "Epilogue");
858 decodeOpcodes(UC
, XData
.EpilogueCount(), /*Prologue=*/false);
862 ListScope
PS(SW
, "Prologue");
863 decodeOpcodes(XData
.UnwindByteCode(), 0, /*Prologue=*/true);
865 ArrayRef
<ulittle32_t
> EpilogueScopes
= XData
.EpilogueScopes();
866 ListScope
ESS(SW
, "EpilogueScopes");
867 for (const EpilogueScope ES
: EpilogueScopes
) {
868 DictScope
ESES(SW
, "EpilogueScope");
869 SW
.printNumber("StartOffset", ES
.EpilogueStartOffset());
871 SW
.printNumber("Condition", ES
.Condition());
872 SW
.printNumber("EpilogueStartIndex",
873 isAArch64
? ES
.EpilogueStartIndexAArch64()
874 : ES
.EpilogueStartIndexARM());
875 if (ES
.ES
& ~0xffc3ffff)
876 SW
.printNumber("ReservedBits", (ES
.ES
>> 18) & 0xF);
878 ListScope
Opcodes(SW
, "Opcodes");
879 decodeOpcodes(XData
.UnwindByteCode(),
880 isAArch64
? ES
.EpilogueStartIndexAArch64()
881 : ES
.EpilogueStartIndexARM(),
887 const uint32_t Address
= XData
.ExceptionHandlerRVA();
888 const uint32_t Parameter
= XData
.ExceptionHandlerParameter();
889 const size_t HandlerOffset
= HeaderWords(XData
)
890 + (XData
.E() ? 0 : XData
.EpilogueCount())
893 ErrorOr
<SymbolRef
> Symbol
= getRelocatedSymbol(
894 COFF
, Section
, Offset
+ HandlerOffset
* sizeof(uint32_t));
896 Symbol
= getSymbol(COFF
, Address
, /*FunctionOnly=*/true);
898 ListScope
EHS(SW
, "ExceptionHandler");
899 SW
.printString("Routine", "(null)");
903 Expected
<StringRef
> Name
= Symbol
->getName();
906 llvm::raw_string_ostream
OS(Buf
);
907 logAllUnhandledErrors(Name
.takeError(), OS
);
909 report_fatal_error(Buf
);
912 ListScope
EHS(SW
, "ExceptionHandler");
913 SW
.printString("Routine", formatSymbol(*Name
, Address
));
914 SW
.printHex("Parameter", Parameter
);
920 bool Decoder::dumpUnpackedEntry(const COFFObjectFile
&COFF
,
921 const SectionRef Section
, uint64_t Offset
,
922 unsigned Index
, const RuntimeFunction
&RF
) {
923 assert(RF
.Flag() == RuntimeFunctionFlag::RFF_Unpacked
&&
924 "packed entry cannot be treated as an unpacked entry");
926 ErrorOr
<SymbolRef
> Function
= getRelocatedSymbol(COFF
, Section
, Offset
);
928 Function
= getSymbol(COFF
, RF
.BeginAddress
, /*FunctionOnly=*/true);
930 ErrorOr
<SymbolRef
> XDataRecord
= getRelocatedSymbol(COFF
, Section
, Offset
+ 4);
932 XDataRecord
= getSymbol(COFF
, RF
.ExceptionInformationRVA());
934 if (!RF
.BeginAddress
&& !Function
)
936 if (!RF
.UnwindData
&& !XDataRecord
)
939 StringRef FunctionName
;
940 uint64_t FunctionAddress
;
942 Expected
<StringRef
> FunctionNameOrErr
= Function
->getName();
943 if (!FunctionNameOrErr
) {
945 llvm::raw_string_ostream
OS(Buf
);
946 logAllUnhandledErrors(FunctionNameOrErr
.takeError(), OS
);
948 report_fatal_error(Buf
);
950 FunctionName
= *FunctionNameOrErr
;
951 Expected
<uint64_t> FunctionAddressOrErr
= Function
->getAddress();
952 if (!FunctionAddressOrErr
) {
954 llvm::raw_string_ostream
OS(Buf
);
955 logAllUnhandledErrors(FunctionAddressOrErr
.takeError(), OS
);
957 report_fatal_error(Buf
);
959 FunctionAddress
= *FunctionAddressOrErr
;
961 FunctionAddress
= COFF
.getImageBase() + RF
.BeginAddress
;
964 SW
.printString("Function", formatSymbol(FunctionName
, FunctionAddress
));
967 Expected
<StringRef
> Name
= XDataRecord
->getName();
970 llvm::raw_string_ostream
OS(Buf
);
971 logAllUnhandledErrors(Name
.takeError(), OS
);
973 report_fatal_error(Buf
);
976 Expected
<uint64_t> AddressOrErr
= XDataRecord
->getAddress();
979 llvm::raw_string_ostream
OS(Buf
);
980 logAllUnhandledErrors(AddressOrErr
.takeError(), OS
);
982 report_fatal_error(Buf
);
984 uint64_t Address
= *AddressOrErr
;
986 SW
.printString("ExceptionRecord", formatSymbol(*Name
, Address
));
988 Expected
<section_iterator
> SIOrErr
= XDataRecord
->getSection();
990 // TODO: Actually report errors helpfully.
991 consumeError(SIOrErr
.takeError());
994 section_iterator SI
= *SIOrErr
;
996 // FIXME: Do we need to add an offset from the relocation?
997 return dumpXDataRecord(COFF
, *SI
, FunctionAddress
,
998 RF
.ExceptionInformationRVA());
1000 uint64_t Address
= COFF
.getImageBase() + RF
.ExceptionInformationRVA();
1001 SW
.printString("ExceptionRecord", formatSymbol("", Address
));
1003 ErrorOr
<SectionRef
> Section
= getSectionContaining(COFF
, Address
);
1007 return dumpXDataRecord(COFF
, *Section
, FunctionAddress
, Address
);
1011 bool Decoder::dumpPackedEntry(const object::COFFObjectFile
&COFF
,
1012 const SectionRef Section
, uint64_t Offset
,
1013 unsigned Index
, const RuntimeFunction
&RF
) {
1014 assert((RF
.Flag() == RuntimeFunctionFlag::RFF_Packed
||
1015 RF
.Flag() == RuntimeFunctionFlag::RFF_PackedFragment
) &&
1016 "unpacked entry cannot be treated as a packed entry");
1018 ErrorOr
<SymbolRef
> Function
= getRelocatedSymbol(COFF
, Section
, Offset
);
1020 Function
= getSymbol(COFF
, RF
.BeginAddress
, /*FunctionOnly=*/true);
1022 StringRef FunctionName
;
1023 uint64_t FunctionAddress
;
1025 Expected
<StringRef
> FunctionNameOrErr
= Function
->getName();
1026 if (!FunctionNameOrErr
) {
1028 llvm::raw_string_ostream
OS(Buf
);
1029 logAllUnhandledErrors(FunctionNameOrErr
.takeError(), OS
);
1031 report_fatal_error(Buf
);
1033 FunctionName
= *FunctionNameOrErr
;
1034 Expected
<uint64_t> FunctionAddressOrErr
= Function
->getAddress();
1035 if (!FunctionAddressOrErr
) {
1037 llvm::raw_string_ostream
OS(Buf
);
1038 logAllUnhandledErrors(FunctionAddressOrErr
.takeError(), OS
);
1040 report_fatal_error(Buf
);
1042 FunctionAddress
= *FunctionAddressOrErr
;
1044 FunctionAddress
= COFF
.getPE32Header()->ImageBase
+ RF
.BeginAddress
;
1047 SW
.printString("Function", formatSymbol(FunctionName
, FunctionAddress
));
1049 SW
.printBoolean("Fragment",
1050 RF
.Flag() == RuntimeFunctionFlag::RFF_PackedFragment
);
1051 SW
.printNumber("FunctionLength", RF
.FunctionLength());
1052 SW
.startLine() << "ReturnType: " << RF
.Ret() << '\n';
1053 SW
.printBoolean("HomedParameters", RF
.H());
1054 SW
.startLine() << "SavedRegisters: ";
1055 printRegisters(SavedRegisterMask(RF
));
1057 SW
.printNumber("StackAdjustment", StackAdjustment(RF
) << 2);
1062 bool Decoder::dumpProcedureDataEntry(const COFFObjectFile
&COFF
,
1063 const SectionRef Section
, unsigned Index
,
1064 ArrayRef
<uint8_t> Contents
) {
1065 uint64_t Offset
= PDataEntrySize
* Index
;
1066 const ulittle32_t
*Data
=
1067 reinterpret_cast<const ulittle32_t
*>(Contents
.data() + Offset
);
1069 const RuntimeFunction
Entry(Data
);
1070 DictScope
RFS(SW
, "RuntimeFunction");
1071 if (Entry
.Flag() == RuntimeFunctionFlag::RFF_Unpacked
)
1072 return dumpUnpackedEntry(COFF
, Section
, Offset
, Index
, Entry
);
1074 SW
.startLine() << "Packed unwind data not yet supported for ARM64\n";
1077 return dumpPackedEntry(COFF
, Section
, Offset
, Index
, Entry
);
1080 void Decoder::dumpProcedureData(const COFFObjectFile
&COFF
,
1081 const SectionRef Section
) {
1082 ArrayRef
<uint8_t> Contents
;
1083 if (COFF
.getSectionContents(COFF
.getCOFFSection(Section
), Contents
))
1086 if (Contents
.size() % PDataEntrySize
) {
1087 errs() << ".pdata content is not " << PDataEntrySize
<< "-byte aligned\n";
1091 for (unsigned EI
= 0, EE
= Contents
.size() / PDataEntrySize
; EI
< EE
; ++EI
)
1092 if (!dumpProcedureDataEntry(COFF
, Section
, EI
, Contents
))
1096 Error
Decoder::dumpProcedureData(const COFFObjectFile
&COFF
) {
1097 for (const auto &Section
: COFF
.sections()) {
1098 Expected
<StringRef
> NameOrErr
=
1099 COFF
.getSectionName(COFF
.getCOFFSection(Section
));
1101 return NameOrErr
.takeError();
1103 if (NameOrErr
->startswith(".pdata"))
1104 dumpProcedureData(COFF
, Section
);
1106 return Error::success();