1 //===-- ARMWinEHPrinter.cpp - Windows on ARM EH Data Printer ----*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Windows on ARM uses a series of serialised data structures (RuntimeFunction)
11 // to create a table of information for unwinding. In order to conserve space,
12 // there are two different ways that this data is represented.
14 // For functions with canonical forms for the prologue and epilogue, the data
15 // can be stored in a "packed" form. In this case, the data is packed into the
16 // RuntimeFunction's remaining 30-bits and can fully describe the entire frame.
18 // +---------------------------------------+
19 // | Function Entry Address |
20 // +---------------------------------------+
21 // | Packed Form Data |
22 // +---------------------------------------+
24 // This layout is parsed by Decoder::dumpPackedEntry. No unwind bytecode is
25 // associated with such a frame as they can be derived from the provided data.
26 // The decoder does not synthesize this data as it is unnecessary for the
27 // purposes of validation, with the synthesis being required only by a proper
30 // For functions that are large or do not match canonical forms, the data is
31 // split up into two portions, with the actual data residing in the "exception
32 // data" table (.xdata) with a reference to the entry from the "procedure data"
35 // The exception data contains information about the frame setup, all of the
36 // epilogue scopes (for functions for which there are multiple exit points) and
37 // the associated exception handler. Additionally, the entry contains byte-code
38 // describing how to unwind the function (c.f. Decoder::decodeOpcodes).
40 // +---------------------------------------+
41 // | Function Entry Address |
42 // +---------------------------------------+
43 // | Exception Data Entry Address |
44 // +---------------------------------------+
46 // This layout is parsed by Decoder::dumpUnpackedEntry. Such an entry must
47 // first resolve the exception data entry address. This structure
48 // (ExceptionDataRecord) has a variable sized header
49 // (c.f. ARM::WinEH::HeaderWords) and encodes most of the same information as
50 // the packed form. However, because this information is insufficient to
51 // synthesize the unwinding, there are associated unwinding bytecode which make
52 // up the bulk of the Decoder.
54 // The decoder itself is table-driven, using the first byte to determine the
55 // opcode and dispatching to the associated printing routine. The bytecode
56 // itself is a variable length instruction encoding that can fully describe the
57 // state of the stack and the necessary operations for unwinding to the
58 // beginning of the frame.
60 // The byte-code maintains a 1-1 instruction mapping, indicating both the width
61 // of the instruction (Thumb2 instructions are variable length, 16 or 32 bits
62 // wide) allowing the program to unwind from any point in the prologue, body, or
63 // epilogue of the function.
65 #include "ARMWinEHPrinter.h"
67 #include "llvm/ADT/STLExtras.h"
68 #include "llvm/ADT/StringExtras.h"
69 #include "llvm/Support/ARMWinEH.h"
70 #include "llvm/Support/Format.h"
73 using namespace llvm::object;
74 using namespace llvm::support;
77 raw_ostream &operator<<(raw_ostream &OS, const ARM::WinEH::ReturnType &RT) {
79 case ARM::WinEH::ReturnType::RT_POP:
82 case ARM::WinEH::ReturnType::RT_B:
85 case ARM::WinEH::ReturnType::RT_BW:
88 case ARM::WinEH::ReturnType::RT_NoEpilogue:
89 OS << "(no epilogue)";
96 static std::string formatSymbol(StringRef Name, uint64_t Address,
97 uint64_t Offset = 0) {
99 raw_string_ostream OS(Buffer);
105 OS << format("+0x%X (0x%" PRIX64 ")", Offset, Address);
106 else if (!Name.empty())
107 OS << format("(0x%" PRIX64 ")", Address);
109 OS << format("0x%" PRIX64, Address);
117 const size_t Decoder::PDataEntrySize = sizeof(RuntimeFunction);
119 // TODO name the uops more appropriately
120 const Decoder::RingEntry Decoder::Ring[] = {
121 { 0x80, 0x00, 1, &Decoder::opcode_0xxxxxxx }, // UOP_STACK_FREE (16-bit)
122 { 0xc0, 0x80, 2, &Decoder::opcode_10Lxxxxx }, // UOP_POP (32-bit)
123 { 0xf0, 0xc0, 1, &Decoder::opcode_1100xxxx }, // UOP_STACK_SAVE (16-bit)
124 { 0xf8, 0xd0, 1, &Decoder::opcode_11010Lxx }, // UOP_POP (16-bit)
125 { 0xf8, 0xd8, 1, &Decoder::opcode_11011Lxx }, // UOP_POP (32-bit)
126 { 0xf8, 0xe0, 1, &Decoder::opcode_11100xxx }, // UOP_VPOP (32-bit)
127 { 0xfc, 0xe8, 2, &Decoder::opcode_111010xx }, // UOP_STACK_FREE (32-bit)
128 { 0xfe, 0xec, 2, &Decoder::opcode_1110110L }, // UOP_POP (16-bit)
129 { 0xff, 0xee, 2, &Decoder::opcode_11101110 }, // UOP_MICROSOFT_SPECIFIC (16-bit)
130 // UOP_PUSH_MACHINE_FRAME
132 // UOP_PUSH_TRAP_FRAME
133 // UOP_REDZONE_RESTORE_LR
134 { 0xff, 0xef, 2, &Decoder::opcode_11101111 }, // UOP_LDRPC_POSTINC (32-bit)
135 { 0xff, 0xf5, 2, &Decoder::opcode_11110101 }, // UOP_VPOP (32-bit)
136 { 0xff, 0xf6, 2, &Decoder::opcode_11110110 }, // UOP_VPOP (32-bit)
137 { 0xff, 0xf7, 3, &Decoder::opcode_11110111 }, // UOP_STACK_RESTORE (16-bit)
138 { 0xff, 0xf8, 4, &Decoder::opcode_11111000 }, // UOP_STACK_RESTORE (16-bit)
139 { 0xff, 0xf9, 3, &Decoder::opcode_11111001 }, // UOP_STACK_RESTORE (32-bit)
140 { 0xff, 0xfa, 4, &Decoder::opcode_11111010 }, // UOP_STACK_RESTORE (32-bit)
141 { 0xff, 0xfb, 1, &Decoder::opcode_11111011 }, // UOP_NOP (16-bit)
142 { 0xff, 0xfc, 1, &Decoder::opcode_11111100 }, // UOP_NOP (32-bit)
143 { 0xff, 0xfd, 1, &Decoder::opcode_11111101 }, // UOP_NOP (16-bit) / END
144 { 0xff, 0xfe, 1, &Decoder::opcode_11111110 }, // UOP_NOP (32-bit) / END
145 { 0xff, 0xff, 1, &Decoder::opcode_11111111 }, // UOP_END
149 // Unwind opcodes for ARM64.
150 // https://docs.microsoft.com/en-us/cpp/build/arm64-exception-handling
151 const Decoder::RingEntry Decoder::Ring64[] = {
152 { 0xe0, 0x00, 1, &Decoder::opcode_alloc_s },
153 { 0xe0, 0x20, 1, &Decoder::opcode_save_r19r20_x },
154 { 0xc0, 0x40, 1, &Decoder::opcode_save_fplr },
155 { 0xc0, 0x80, 1, &Decoder::opcode_save_fplr_x },
156 { 0xf8, 0xc0, 2, &Decoder::opcode_alloc_m },
157 { 0xfc, 0xc8, 2, &Decoder::opcode_save_regp },
158 { 0xfc, 0xcc, 2, &Decoder::opcode_save_regp_x },
159 { 0xfc, 0xd0, 2, &Decoder::opcode_save_reg },
160 { 0xfe, 0xd4, 2, &Decoder::opcode_save_reg_x },
161 { 0xfe, 0xd6, 2, &Decoder::opcode_save_lrpair },
162 { 0xfe, 0xd8, 2, &Decoder::opcode_save_fregp },
163 { 0xfe, 0xda, 2, &Decoder::opcode_save_fregp_x },
164 { 0xfe, 0xdc, 2, &Decoder::opcode_save_freg },
165 { 0xff, 0xde, 2, &Decoder::opcode_save_freg_x },
166 { 0xff, 0xe0, 4, &Decoder::opcode_alloc_l },
167 { 0xff, 0xe1, 1, &Decoder::opcode_setfp },
168 { 0xff, 0xe2, 2, &Decoder::opcode_addfp },
169 { 0xff, 0xe3, 1, &Decoder::opcode_nop },
170 { 0xff, 0xe4, 1, &Decoder::opcode_end },
171 { 0xff, 0xe5, 1, &Decoder::opcode_end_c },
174 void Decoder::printRegisters(const std::pair<uint16_t, uint32_t> &RegisterMask) {
175 static const char * const GPRRegisterNames[16] = {
176 "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7", "r8", "r9", "r10",
177 "r11", "ip", "sp", "lr", "pc",
180 const uint16_t GPRMask = std::get<0>(RegisterMask);
181 const uint16_t VFPMask = std::get<1>(RegisterMask);
185 for (unsigned RI = 0, RE = 11; RI < RE; ++RI) {
186 if (GPRMask & (1 << RI)) {
189 OS << GPRRegisterNames[RI];
193 for (unsigned RI = 0, RE = 32; RI < RE; ++RI) {
194 if (VFPMask & (1 << RI)) {
197 OS << "d" << unsigned(RI);
201 for (unsigned RI = 11, RE = 16; RI < RE; ++RI) {
202 if (GPRMask & (1 << RI)) {
205 OS << GPRRegisterNames[RI];
212 ErrorOr<object::SectionRef>
213 Decoder::getSectionContaining(const COFFObjectFile &COFF, uint64_t VA) {
214 for (const auto &Section : COFF.sections()) {
215 uint64_t Address = Section.getAddress();
216 uint64_t Size = Section.getSize();
218 if (VA >= Address && (VA - Address) <= Size)
221 return readobj_error::unknown_symbol;
224 ErrorOr<object::SymbolRef> Decoder::getSymbol(const COFFObjectFile &COFF,
225 uint64_t VA, bool FunctionOnly) {
226 for (const auto &Symbol : COFF.symbols()) {
227 Expected<SymbolRef::Type> Type = Symbol.getType();
229 return errorToErrorCode(Type.takeError());
230 if (FunctionOnly && *Type != SymbolRef::ST_Function)
233 Expected<uint64_t> Address = Symbol.getAddress();
235 return errorToErrorCode(Address.takeError());
239 return readobj_error::unknown_symbol;
242 ErrorOr<SymbolRef> Decoder::getRelocatedSymbol(const COFFObjectFile &,
243 const SectionRef &Section,
245 for (const auto &Relocation : Section.relocations()) {
246 uint64_t RelocationOffset = Relocation.getOffset();
247 if (RelocationOffset == Offset)
248 return *Relocation.getSymbol();
250 return readobj_error::unknown_symbol;
253 bool Decoder::opcode_0xxxxxxx(const uint8_t *OC, unsigned &Offset,
254 unsigned Length, bool Prologue) {
255 uint8_t Imm = OC[Offset] & 0x7f;
256 SW.startLine() << format("0x%02x ; %s sp, #(%u * 4)\n",
258 static_cast<const char *>(Prologue ? "sub" : "add"),
264 bool Decoder::opcode_10Lxxxxx(const uint8_t *OC, unsigned &Offset,
265 unsigned Length, bool Prologue) {
266 unsigned Link = (OC[Offset] & 0x20) >> 5;
267 uint16_t RegisterMask = (Link << (Prologue ? 14 : 15))
268 | ((OC[Offset + 0] & 0x1f) << 8)
269 | ((OC[Offset + 1] & 0xff) << 0);
270 assert((~RegisterMask & (1 << 13)) && "sp must not be set");
271 assert((~RegisterMask & (1 << (Prologue ? 15 : 14))) && "pc must not be set");
273 SW.startLine() << format("0x%02x 0x%02x ; %s.w ",
274 OC[Offset + 0], OC[Offset + 1],
275 Prologue ? "push" : "pop");
276 printRegisters(std::make_pair(RegisterMask, 0));
283 bool Decoder::opcode_1100xxxx(const uint8_t *OC, unsigned &Offset,
284 unsigned Length, bool Prologue) {
286 SW.startLine() << format("0x%02x ; mov r%u, sp\n",
287 OC[Offset], OC[Offset] & 0xf);
289 SW.startLine() << format("0x%02x ; mov sp, r%u\n",
290 OC[Offset], OC[Offset] & 0xf);
295 bool Decoder::opcode_11010Lxx(const uint8_t *OC, unsigned &Offset,
296 unsigned Length, bool Prologue) {
297 unsigned Link = (OC[Offset] & 0x4) >> 3;
298 unsigned Count = (OC[Offset] & 0x3);
300 uint16_t GPRMask = (Link << (Prologue ? 14 : 15))
301 | (((1 << (Count + 1)) - 1) << 4);
303 SW.startLine() << format("0x%02x ; %s ", OC[Offset],
304 Prologue ? "push" : "pop");
305 printRegisters(std::make_pair(GPRMask, 0));
312 bool Decoder::opcode_11011Lxx(const uint8_t *OC, unsigned &Offset,
313 unsigned Length, bool Prologue) {
314 unsigned Link = (OC[Offset] & 0x4) >> 2;
315 unsigned Count = (OC[Offset] & 0x3) + 4;
317 uint16_t GPRMask = (Link << (Prologue ? 14 : 15))
318 | (((1 << (Count + 1)) - 1) << 4);
320 SW.startLine() << format("0x%02x ; %s.w ", OC[Offset],
321 Prologue ? "push" : "pop");
322 printRegisters(std::make_pair(GPRMask, 0));
329 bool Decoder::opcode_11100xxx(const uint8_t *OC, unsigned &Offset,
330 unsigned Length, bool Prologue) {
331 unsigned High = (OC[Offset] & 0x7);
332 uint32_t VFPMask = (((1 << (High + 1)) - 1) << 8);
334 SW.startLine() << format("0x%02x ; %s ", OC[Offset],
335 Prologue ? "vpush" : "vpop");
336 printRegisters(std::make_pair(0, VFPMask));
343 bool Decoder::opcode_111010xx(const uint8_t *OC, unsigned &Offset,
344 unsigned Length, bool Prologue) {
345 uint16_t Imm = ((OC[Offset + 0] & 0x03) << 8) | ((OC[Offset + 1] & 0xff) << 0);
347 SW.startLine() << format("0x%02x 0x%02x ; %s.w sp, #(%u * 4)\n",
348 OC[Offset + 0], OC[Offset + 1],
349 static_cast<const char *>(Prologue ? "sub" : "add"),
356 bool Decoder::opcode_1110110L(const uint8_t *OC, unsigned &Offset,
357 unsigned Length, bool Prologue) {
358 uint8_t GPRMask = ((OC[Offset + 0] & 0x01) << (Prologue ? 14 : 15))
359 | ((OC[Offset + 1] & 0xff) << 0);
361 SW.startLine() << format("0x%02x 0x%02x ; %s ", OC[Offset + 0],
362 OC[Offset + 1], Prologue ? "push" : "pop");
363 printRegisters(std::make_pair(GPRMask, 0));
370 bool Decoder::opcode_11101110(const uint8_t *OC, unsigned &Offset,
371 unsigned Length, bool Prologue) {
372 assert(!Prologue && "may not be used in prologue");
374 if (OC[Offset + 1] & 0xf0)
375 SW.startLine() << format("0x%02x 0x%02x ; reserved\n",
376 OC[Offset + 0], OC[Offset + 1]);
379 << format("0x%02x 0x%02x ; microsoft-specific (type: %u)\n",
380 OC[Offset + 0], OC[Offset + 1], OC[Offset + 1] & 0x0f);
386 bool Decoder::opcode_11101111(const uint8_t *OC, unsigned &Offset,
387 unsigned Length, bool Prologue) {
388 assert(!Prologue && "may not be used in prologue");
390 if (OC[Offset + 1] & 0xf0)
391 SW.startLine() << format("0x%02x 0x%02x ; reserved\n",
392 OC[Offset + 0], OC[Offset + 1]);
395 << format("0x%02x 0x%02x ; ldr.w lr, [sp], #%u\n",
396 OC[Offset + 0], OC[Offset + 1], OC[Offset + 1] << 2);
402 bool Decoder::opcode_11110101(const uint8_t *OC, unsigned &Offset,
403 unsigned Length, bool Prologue) {
404 unsigned Start = (OC[Offset + 1] & 0xf0) >> 4;
405 unsigned End = (OC[Offset + 1] & 0x0f) >> 0;
406 uint32_t VFPMask = ((1 << (End - Start)) - 1) << Start;
408 SW.startLine() << format("0x%02x 0x%02x ; %s ", OC[Offset + 0],
409 OC[Offset + 1], Prologue ? "vpush" : "vpop");
410 printRegisters(std::make_pair(0, VFPMask));
417 bool Decoder::opcode_11110110(const uint8_t *OC, unsigned &Offset,
418 unsigned Length, bool Prologue) {
419 unsigned Start = (OC[Offset + 1] & 0xf0) >> 4;
420 unsigned End = (OC[Offset + 1] & 0x0f) >> 0;
421 uint32_t VFPMask = ((1 << (End - Start)) - 1) << 16;
423 SW.startLine() << format("0x%02x 0x%02x ; %s ", OC[Offset + 0],
424 OC[Offset + 1], Prologue ? "vpush" : "vpop");
425 printRegisters(std::make_pair(0, VFPMask));
432 bool Decoder::opcode_11110111(const uint8_t *OC, unsigned &Offset,
433 unsigned Length, bool Prologue) {
434 uint32_t Imm = (OC[Offset + 1] << 8) | (OC[Offset + 2] << 0);
436 SW.startLine() << format("0x%02x 0x%02x 0x%02x ; %s sp, sp, #(%u * 4)\n",
437 OC[Offset + 0], OC[Offset + 1], OC[Offset + 2],
438 static_cast<const char *>(Prologue ? "sub" : "add"),
445 bool Decoder::opcode_11111000(const uint8_t *OC, unsigned &Offset,
446 unsigned Length, bool Prologue) {
447 uint32_t Imm = (OC[Offset + 1] << 16)
448 | (OC[Offset + 2] << 8)
449 | (OC[Offset + 3] << 0);
452 << format("0x%02x 0x%02x 0x%02x 0x%02x ; %s sp, sp, #(%u * 4)\n",
453 OC[Offset + 0], OC[Offset + 1], OC[Offset + 2], OC[Offset + 3],
454 static_cast<const char *>(Prologue ? "sub" : "add"), Imm);
460 bool Decoder::opcode_11111001(const uint8_t *OC, unsigned &Offset,
461 unsigned Length, bool Prologue) {
462 uint32_t Imm = (OC[Offset + 1] << 8) | (OC[Offset + 2] << 0);
465 << format("0x%02x 0x%02x 0x%02x ; %s.w sp, sp, #(%u * 4)\n",
466 OC[Offset + 0], OC[Offset + 1], OC[Offset + 2],
467 static_cast<const char *>(Prologue ? "sub" : "add"), Imm);
473 bool Decoder::opcode_11111010(const uint8_t *OC, unsigned &Offset,
474 unsigned Length, bool Prologue) {
475 uint32_t Imm = (OC[Offset + 1] << 16)
476 | (OC[Offset + 2] << 8)
477 | (OC[Offset + 3] << 0);
480 << format("0x%02x 0x%02x 0x%02x 0x%02x ; %s.w sp, sp, #(%u * 4)\n",
481 OC[Offset + 0], OC[Offset + 1], OC[Offset + 2], OC[Offset + 3],
482 static_cast<const char *>(Prologue ? "sub" : "add"), Imm);
488 bool Decoder::opcode_11111011(const uint8_t *OC, unsigned &Offset,
489 unsigned Length, bool Prologue) {
490 SW.startLine() << format("0x%02x ; nop\n", OC[Offset]);
495 bool Decoder::opcode_11111100(const uint8_t *OC, unsigned &Offset,
496 unsigned Length, bool Prologue) {
497 SW.startLine() << format("0x%02x ; nop.w\n", OC[Offset]);
502 bool Decoder::opcode_11111101(const uint8_t *OC, unsigned &Offset,
503 unsigned Length, bool Prologue) {
504 SW.startLine() << format("0x%02x ; b\n", OC[Offset]);
509 bool Decoder::opcode_11111110(const uint8_t *OC, unsigned &Offset,
510 unsigned Length, bool Prologue) {
511 SW.startLine() << format("0x%02x ; b.w\n", OC[Offset]);
516 bool Decoder::opcode_11111111(const uint8_t *OC, unsigned &Offset,
517 unsigned Length, bool Prologue) {
522 // ARM64 unwind codes start here.
523 bool Decoder::opcode_alloc_s(const uint8_t *OC, unsigned &Offset,
524 unsigned Length, bool Prologue) {
525 uint32_t NumBytes = (OC[Offset] & 0x1F) << 4;
526 SW.startLine() << format("0x%02x ; %s sp, #%u\n", OC[Offset],
527 static_cast<const char *>(Prologue ? "sub" : "add"),
533 bool Decoder::opcode_save_r19r20_x(const uint8_t *OC, unsigned &Offset,
534 unsigned Length, bool Prologue) {
535 uint32_t Off = (OC[Offset] & 0x1F) << 3;
537 SW.startLine() << format(
538 "0x%02x ; stp x19, x20, [sp, #-%u]!\n", OC[Offset], Off);
540 SW.startLine() << format(
541 "0x%02x ; ldp x19, x20, [sp], #%u\n", OC[Offset], Off);
546 bool Decoder::opcode_save_fplr(const uint8_t *OC, unsigned &Offset,
547 unsigned Length, bool Prologue) {
548 uint32_t Off = (OC[Offset] & 0x3F) << 3;
549 SW.startLine() << format(
550 "0x%02x ; %s x29, x30, [sp, #%u]\n", OC[Offset],
551 static_cast<const char *>(Prologue ? "stp" : "ldp"), Off);
556 bool Decoder::opcode_save_fplr_x(const uint8_t *OC, unsigned &Offset,
557 unsigned Length, bool Prologue) {
558 uint32_t Off = ((OC[Offset] & 0x3F) + 1) << 3;
560 SW.startLine() << format(
561 "0x%02x ; stp x29, x30, [sp, #-%u]!\n", OC[Offset], Off);
563 SW.startLine() << format(
564 "0x%02x ; ldp x29, x30, [sp], #%u\n", OC[Offset], Off);
569 bool Decoder::opcode_alloc_m(const uint8_t *OC, unsigned &Offset,
570 unsigned Length, bool Prologue) {
571 uint32_t NumBytes = ((OC[Offset] & 0x07) << 8);
572 NumBytes |= (OC[Offset + 1] & 0xFF);
574 SW.startLine() << format("0x%02x%02x ; %s sp, #%u\n",
575 OC[Offset], OC[Offset + 1],
576 static_cast<const char *>(Prologue ? "sub" : "add"),
582 bool Decoder::opcode_save_regp(const uint8_t *OC, unsigned &Offset,
583 unsigned Length, bool Prologue) {
584 uint32_t Reg = ((OC[Offset] & 0x03) << 8);
585 Reg |= (OC[Offset + 1] & 0xC0);
588 uint32_t Off = (OC[Offset + 1] & 0x3F) << 3;
589 SW.startLine() << format(
590 "0x%02x%02x ; %s x%u, x%u, [sp, #%u]\n",
591 OC[Offset], OC[Offset + 1],
592 static_cast<const char *>(Prologue ? "stp" : "ldp"), Reg, Reg + 1, Off);
597 bool Decoder::opcode_save_regp_x(const uint8_t *OC, unsigned &Offset,
598 unsigned Length, bool Prologue) {
599 uint32_t Reg = ((OC[Offset] & 0x03) << 8);
600 Reg |= (OC[Offset + 1] & 0xC0);
603 uint32_t Off = ((OC[Offset + 1] & 0x3F) + 1) << 3;
605 SW.startLine() << format(
606 "0x%02x%02x ; stp x%u, x%u, [sp, #-%u]!\n",
607 OC[Offset], OC[Offset + 1], Reg,
610 SW.startLine() << format(
611 "0x%02x%02x ; ldp x%u, x%u, [sp], #%u\n",
612 OC[Offset], OC[Offset + 1], Reg,
618 bool Decoder::opcode_save_reg(const uint8_t *OC, unsigned &Offset,
619 unsigned Length, bool Prologue) {
620 uint32_t Reg = (OC[Offset] & 0x03) << 8;
621 Reg |= (OC[Offset + 1] & 0xC0);
624 uint32_t Off = (OC[Offset + 1] & 0x3F) << 3;
625 SW.startLine() << format("0x%02x%02x ; %s x%u, [sp, #%u]\n",
626 OC[Offset], OC[Offset + 1],
627 static_cast<const char *>(Prologue ? "str" : "ldr"),
633 bool Decoder::opcode_save_reg_x(const uint8_t *OC, unsigned &Offset,
634 unsigned Length, bool Prologue) {
635 uint32_t Reg = (OC[Offset] & 0x01) << 8;
636 Reg |= (OC[Offset + 1] & 0xE0);
639 uint32_t Off = ((OC[Offset + 1] & 0x1F) + 1) << 3;
641 SW.startLine() << format("0x%02x%02x ; str x%u, [sp, #%u]!\n",
642 OC[Offset], OC[Offset + 1], Reg, Off);
644 SW.startLine() << format("0x%02x%02x ; ldr x%u, [sp], #%u\n",
645 OC[Offset], OC[Offset + 1], Reg, Off);
650 bool Decoder::opcode_save_lrpair(const uint8_t *OC, unsigned &Offset,
651 unsigned Length, bool Prologue) {
652 uint32_t Reg = (OC[Offset] & 0x01) << 8;
653 Reg |= (OC[Offset + 1] & 0xC0);
657 uint32_t Off = (OC[Offset + 1] & 0x3F) << 3;
658 SW.startLine() << format("0x%02x%02x ; %s x%u, lr, [sp, #%u]\n",
659 OC[Offset], OC[Offset + 1],
660 static_cast<const char *>(Prologue ? "stp" : "ldp"),
666 bool Decoder::opcode_save_fregp(const uint8_t *OC, unsigned &Offset,
667 unsigned Length, bool Prologue) {
668 uint32_t Reg = (OC[Offset] & 0x01) << 8;
669 Reg |= (OC[Offset + 1] & 0xC0);
672 uint32_t Off = (OC[Offset + 1] & 0x3F) << 3;
673 SW.startLine() << format("0x%02x%02x ; %s d%u, d%u, [sp, #%u]\n",
674 OC[Offset], OC[Offset + 1],
675 static_cast<const char *>(Prologue ? "stp" : "ldp"),
681 bool Decoder::opcode_save_fregp_x(const uint8_t *OC, unsigned &Offset,
682 unsigned Length, bool Prologue) {
683 uint32_t Reg = (OC[Offset] & 0x01) << 8;
684 Reg |= (OC[Offset + 1] & 0xC0);
687 uint32_t Off = ((OC[Offset + 1] & 0x3F) + 1) << 3;
689 SW.startLine() << format(
690 "0x%02x%02x ; stp d%u, d%u, [sp, #-%u]!\n", OC[Offset],
691 OC[Offset + 1], Reg, Reg + 1, Off);
693 SW.startLine() << format(
694 "0x%02x%02x ; ldp d%u, d%u, [sp], #%u\n", OC[Offset],
695 OC[Offset + 1], Reg, Reg + 1, Off);
700 bool Decoder::opcode_save_freg(const uint8_t *OC, unsigned &Offset,
701 unsigned Length, bool Prologue) {
702 uint32_t Reg = (OC[Offset] & 0x01) << 8;
703 Reg |= (OC[Offset + 1] & 0xC0);
706 uint32_t Off = (OC[Offset + 1] & 0x3F) << 3;
707 SW.startLine() << format("0x%02x%02x ; %s d%u, [sp, #%u]\n",
708 OC[Offset], OC[Offset + 1],
709 static_cast<const char *>(Prologue ? "str" : "ldr"),
715 bool Decoder::opcode_save_freg_x(const uint8_t *OC, unsigned &Offset,
716 unsigned Length, bool Prologue) {
717 uint32_t Reg = ((OC[Offset + 1] & 0xE0) >> 5) + 8;
718 uint32_t Off = ((OC[Offset + 1] & 0x1F) + 1) << 3;
720 SW.startLine() << format(
721 "0x%02x%02x ; str d%u, [sp, #-%u]!\n", OC[Offset],
722 OC[Offset + 1], Reg, Off);
724 SW.startLine() << format(
725 "0x%02x%02x ; ldr d%u, [sp], #%u\n", OC[Offset],
726 OC[Offset + 1], Reg, Off);
731 bool Decoder::opcode_alloc_l(const uint8_t *OC, unsigned &Offset,
732 unsigned Length, bool Prologue) {
734 (OC[Offset + 1] << 16) | (OC[Offset + 2] << 8) | (OC[Offset + 3] << 0);
736 SW.startLine() << format(
737 "0x%02x%02x%02x%02x ; %s sp, #%u\n", OC[Offset], OC[Offset + 1],
738 OC[Offset + 2], OC[Offset + 3],
739 static_cast<const char *>(Prologue ? "sub" : "add"), Off);
744 bool Decoder::opcode_setfp(const uint8_t *OC, unsigned &Offset, unsigned Length,
746 SW.startLine() << format("0x%02x ; mov fp, sp\n", OC[Offset]);
751 bool Decoder::opcode_addfp(const uint8_t *OC, unsigned &Offset, unsigned Length,
753 unsigned NumBytes = OC[Offset + 1] << 3;
754 SW.startLine() << format("0x%02x%02x ; add fp, sp, #%u\n",
755 OC[Offset], OC[Offset + 1], NumBytes);
760 bool Decoder::opcode_nop(const uint8_t *OC, unsigned &Offset, unsigned Length,
762 SW.startLine() << format("0x%02x ; nop\n", OC[Offset]);
767 bool Decoder::opcode_end(const uint8_t *OC, unsigned &Offset, unsigned Length,
769 SW.startLine() << format("0x%02x ; end\n", OC[Offset]);
774 bool Decoder::opcode_end_c(const uint8_t *OC, unsigned &Offset, unsigned Length,
776 SW.startLine() << format("0x%02x ; end_c\n", OC[Offset]);
781 void Decoder::decodeOpcodes(ArrayRef<uint8_t> Opcodes, unsigned Offset,
783 assert((!Prologue || Offset == 0) && "prologue should always use offset 0");
784 const RingEntry* DecodeRing = isAArch64 ? Ring64 : Ring;
785 bool Terminated = false;
786 for (unsigned OI = Offset, OE = Opcodes.size(); !Terminated && OI < OE; ) {
787 for (unsigned DI = 0;; ++DI) {
788 if ((isAArch64 && (DI >= array_lengthof(Ring64))) ||
789 (!isAArch64 && (DI >= array_lengthof(Ring)))) {
790 SW.startLine() << format("0x%02x ; Bad opcode!\n",
796 if ((Opcodes[OI] & DecodeRing[DI].Mask) == DecodeRing[DI].Value) {
797 if (OI + DecodeRing[DI].Length > OE) {
798 SW.startLine() << format("Opcode 0x%02x goes past the unwind data\n",
800 OI += DecodeRing[DI].Length;
804 (this->*DecodeRing[DI].Routine)(Opcodes.data(), OI, 0, Prologue);
811 bool Decoder::dumpXDataRecord(const COFFObjectFile &COFF,
812 const SectionRef &Section,
813 uint64_t FunctionAddress, uint64_t VA) {
814 ArrayRef<uint8_t> Contents;
815 if (COFF.getSectionContents(COFF.getCOFFSection(Section), Contents))
818 uint64_t SectionVA = Section.getAddress();
819 uint64_t Offset = VA - SectionVA;
820 const ulittle32_t *Data =
821 reinterpret_cast<const ulittle32_t *>(Contents.data() + Offset);
823 // Sanity check to ensure that the .xdata header is present.
824 // A header is one or two words, followed by at least one word to describe
825 // the unwind codes. Applicable to both ARM and AArch64.
826 if (Contents.size() - Offset < 8)
827 report_fatal_error(".xdata must be at least 8 bytes in size");
829 const ExceptionDataRecord XData(Data, isAArch64);
830 DictScope XRS(SW, "ExceptionData");
831 SW.printNumber("FunctionLength",
832 isAArch64 ? XData.FunctionLengthInBytesAArch64() :
833 XData.FunctionLengthInBytesARM());
834 SW.printNumber("Version", XData.Vers());
835 SW.printBoolean("ExceptionData", XData.X());
836 SW.printBoolean("EpiloguePacked", XData.E());
838 SW.printBoolean("Fragment", XData.F());
839 SW.printNumber(XData.E() ? "EpilogueOffset" : "EpilogueScopes",
840 XData.EpilogueCount());
841 uint64_t ByteCodeLength = XData.CodeWords() * sizeof(uint32_t);
842 SW.printNumber("ByteCodeLength", ByteCodeLength);
844 if ((int64_t)(Contents.size() - Offset - 4 * HeaderWords(XData) -
845 (XData.E() ? 0 : XData.EpilogueCount() * 4) -
846 (XData.X() ? 8 : 0)) < (int64_t)ByteCodeLength)
847 report_fatal_error("Malformed unwind data");
850 ArrayRef<uint8_t> UC = XData.UnwindByteCode();
851 if (isAArch64 || !XData.F()) {
852 ListScope PS(SW, "Prologue");
853 decodeOpcodes(UC, 0, /*Prologue=*/true);
855 if (XData.EpilogueCount()) {
856 ListScope ES(SW, "Epilogue");
857 decodeOpcodes(UC, XData.EpilogueCount(), /*Prologue=*/false);
861 ListScope PS(SW, "Prologue");
862 decodeOpcodes(XData.UnwindByteCode(), 0, /*Prologue=*/true);
864 ArrayRef<ulittle32_t> EpilogueScopes = XData.EpilogueScopes();
865 ListScope ESS(SW, "EpilogueScopes");
866 for (const EpilogueScope ES : EpilogueScopes) {
867 DictScope ESES(SW, "EpilogueScope");
868 SW.printNumber("StartOffset", ES.EpilogueStartOffset());
870 SW.printNumber("Condition", ES.Condition());
871 SW.printNumber("EpilogueStartIndex",
872 isAArch64 ? ES.EpilogueStartIndexAArch64()
873 : ES.EpilogueStartIndexARM());
874 if (ES.ES & ~0xffc3ffff)
875 SW.printNumber("ReservedBits", (ES.ES >> 18) & 0xF);
877 ListScope Opcodes(SW, "Opcodes");
878 decodeOpcodes(XData.UnwindByteCode(),
879 isAArch64 ? ES.EpilogueStartIndexAArch64()
880 : ES.EpilogueStartIndexARM(),
886 const uint32_t Address = XData.ExceptionHandlerRVA();
887 const uint32_t Parameter = XData.ExceptionHandlerParameter();
888 const size_t HandlerOffset = HeaderWords(XData)
889 + (XData.E() ? 0 : XData.EpilogueCount())
892 ErrorOr<SymbolRef> Symbol = getRelocatedSymbol(
893 COFF, Section, Offset + HandlerOffset * sizeof(uint32_t));
895 Symbol = getSymbol(COFF, Address, /*FunctionOnly=*/true);
897 ListScope EHS(SW, "ExceptionHandler");
898 SW.printString("Routine", "(null)");
902 Expected<StringRef> Name = Symbol->getName();
905 llvm::raw_string_ostream OS(Buf);
906 logAllUnhandledErrors(Name.takeError(), OS);
908 report_fatal_error(Buf);
911 ListScope EHS(SW, "ExceptionHandler");
912 SW.printString("Routine", formatSymbol(*Name, Address));
913 SW.printHex("Parameter", Parameter);
919 bool Decoder::dumpUnpackedEntry(const COFFObjectFile &COFF,
920 const SectionRef Section, uint64_t Offset,
921 unsigned Index, const RuntimeFunction &RF) {
922 assert(RF.Flag() == RuntimeFunctionFlag::RFF_Unpacked &&
923 "packed entry cannot be treated as an unpacked entry");
925 ErrorOr<SymbolRef> Function = getRelocatedSymbol(COFF, Section, Offset);
927 Function = getSymbol(COFF, RF.BeginAddress, /*FunctionOnly=*/true);
929 ErrorOr<SymbolRef> XDataRecord = getRelocatedSymbol(COFF, Section, Offset + 4);
931 XDataRecord = getSymbol(COFF, RF.ExceptionInformationRVA());
933 if (!RF.BeginAddress && !Function)
935 if (!RF.UnwindData && !XDataRecord)
938 StringRef FunctionName;
939 uint64_t FunctionAddress;
941 Expected<StringRef> FunctionNameOrErr = Function->getName();
942 if (!FunctionNameOrErr) {
944 llvm::raw_string_ostream OS(Buf);
945 logAllUnhandledErrors(FunctionNameOrErr.takeError(), OS);
947 report_fatal_error(Buf);
949 FunctionName = *FunctionNameOrErr;
950 Expected<uint64_t> FunctionAddressOrErr = Function->getAddress();
951 if (!FunctionAddressOrErr) {
953 llvm::raw_string_ostream OS(Buf);
954 logAllUnhandledErrors(FunctionAddressOrErr.takeError(), OS);
956 report_fatal_error(Buf);
958 FunctionAddress = *FunctionAddressOrErr;
960 FunctionAddress = COFF.getImageBase() + RF.BeginAddress;
963 SW.printString("Function", formatSymbol(FunctionName, FunctionAddress));
966 Expected<StringRef> Name = XDataRecord->getName();
969 llvm::raw_string_ostream OS(Buf);
970 logAllUnhandledErrors(Name.takeError(), OS);
972 report_fatal_error(Buf);
975 Expected<uint64_t> AddressOrErr = XDataRecord->getAddress();
978 llvm::raw_string_ostream OS(Buf);
979 logAllUnhandledErrors(AddressOrErr.takeError(), OS);
981 report_fatal_error(Buf);
983 uint64_t Address = *AddressOrErr;
985 SW.printString("ExceptionRecord", formatSymbol(*Name, Address));
987 Expected<section_iterator> SIOrErr = XDataRecord->getSection();
989 // TODO: Actually report errors helpfully.
990 consumeError(SIOrErr.takeError());
993 section_iterator SI = *SIOrErr;
995 // FIXME: Do we need to add an offset from the relocation?
996 return dumpXDataRecord(COFF, *SI, FunctionAddress,
997 RF.ExceptionInformationRVA());
999 uint64_t Address = COFF.getImageBase() + RF.ExceptionInformationRVA();
1000 SW.printString("ExceptionRecord", formatSymbol("", Address));
1002 ErrorOr<SectionRef> Section = getSectionContaining(COFF, Address);
1006 return dumpXDataRecord(COFF, *Section, FunctionAddress, Address);
1010 bool Decoder::dumpPackedEntry(const object::COFFObjectFile &COFF,
1011 const SectionRef Section, uint64_t Offset,
1012 unsigned Index, const RuntimeFunction &RF) {
1013 assert((RF.Flag() == RuntimeFunctionFlag::RFF_Packed ||
1014 RF.Flag() == RuntimeFunctionFlag::RFF_PackedFragment) &&
1015 "unpacked entry cannot be treated as a packed entry");
1017 ErrorOr<SymbolRef> Function = getRelocatedSymbol(COFF, Section, Offset);
1019 Function = getSymbol(COFF, RF.BeginAddress, /*FunctionOnly=*/true);
1021 StringRef FunctionName;
1022 uint64_t FunctionAddress;
1024 Expected<StringRef> FunctionNameOrErr = Function->getName();
1025 if (!FunctionNameOrErr) {
1027 llvm::raw_string_ostream OS(Buf);
1028 logAllUnhandledErrors(FunctionNameOrErr.takeError(), OS);
1030 report_fatal_error(Buf);
1032 FunctionName = *FunctionNameOrErr;
1033 Expected<uint64_t> FunctionAddressOrErr = Function->getAddress();
1034 if (!FunctionAddressOrErr) {
1036 llvm::raw_string_ostream OS(Buf);
1037 logAllUnhandledErrors(FunctionAddressOrErr.takeError(), OS);
1039 report_fatal_error(Buf);
1041 FunctionAddress = *FunctionAddressOrErr;
1043 const pe32_header *PEHeader;
1044 if (COFF.getPE32Header(PEHeader))
1046 FunctionAddress = PEHeader->ImageBase + RF.BeginAddress;
1049 SW.printString("Function", formatSymbol(FunctionName, FunctionAddress));
1051 SW.printBoolean("Fragment",
1052 RF.Flag() == RuntimeFunctionFlag::RFF_PackedFragment);
1053 SW.printNumber("FunctionLength", RF.FunctionLength());
1054 SW.startLine() << "ReturnType: " << RF.Ret() << '\n';
1055 SW.printBoolean("HomedParameters", RF.H());
1056 SW.startLine() << "SavedRegisters: ";
1057 printRegisters(SavedRegisterMask(RF));
1059 SW.printNumber("StackAdjustment", StackAdjustment(RF) << 2);
1064 bool Decoder::dumpProcedureDataEntry(const COFFObjectFile &COFF,
1065 const SectionRef Section, unsigned Index,
1066 ArrayRef<uint8_t> Contents) {
1067 uint64_t Offset = PDataEntrySize * Index;
1068 const ulittle32_t *Data =
1069 reinterpret_cast<const ulittle32_t *>(Contents.data() + Offset);
1071 const RuntimeFunction Entry(Data);
1072 DictScope RFS(SW, "RuntimeFunction");
1073 if (Entry.Flag() == RuntimeFunctionFlag::RFF_Unpacked)
1074 return dumpUnpackedEntry(COFF, Section, Offset, Index, Entry);
1076 SW.startLine() << "Packed unwind data not yet supported for ARM64\n";
1079 return dumpPackedEntry(COFF, Section, Offset, Index, Entry);
1082 void Decoder::dumpProcedureData(const COFFObjectFile &COFF,
1083 const SectionRef Section) {
1084 ArrayRef<uint8_t> Contents;
1085 if (COFF.getSectionContents(COFF.getCOFFSection(Section), Contents))
1088 if (Contents.size() % PDataEntrySize) {
1089 errs() << ".pdata content is not " << PDataEntrySize << "-byte aligned\n";
1093 for (unsigned EI = 0, EE = Contents.size() / PDataEntrySize; EI < EE; ++EI)
1094 if (!dumpProcedureDataEntry(COFF, Section, EI, Contents))
1098 std::error_code Decoder::dumpProcedureData(const COFFObjectFile &COFF) {
1099 for (const auto &Section : COFF.sections()) {
1100 StringRef SectionName;
1101 if (std::error_code EC =
1102 COFF.getSectionName(COFF.getCOFFSection(Section), SectionName))
1105 if (SectionName.startswith(".pdata"))
1106 dumpProcedureData(COFF, Section);
1108 return std::error_code();