xref: /freebsd/contrib/llvm-project/llvm/tools/llvm-readobj/ARMWinEHPrinter.cpp (revision e8d8bef961a50d4dc22501cde4fb9fb0be1b2532)
10b57cec5SDimitry Andric //===-- ARMWinEHPrinter.cpp - Windows on ARM EH Data Printer ----*- C++ -*-===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric 
90b57cec5SDimitry Andric // Windows on ARM uses a series of serialised data structures (RuntimeFunction)
100b57cec5SDimitry Andric // to create a table of information for unwinding.  In order to conserve space,
110b57cec5SDimitry Andric // there are two different ways that this data is represented.
120b57cec5SDimitry Andric //
130b57cec5SDimitry Andric // For functions with canonical forms for the prologue and epilogue, the data
140b57cec5SDimitry Andric // can be stored in a "packed" form.  In this case, the data is packed into the
150b57cec5SDimitry Andric // RuntimeFunction's remaining 30-bits and can fully describe the entire frame.
160b57cec5SDimitry Andric //
170b57cec5SDimitry Andric //        +---------------------------------------+
180b57cec5SDimitry Andric //        |         Function Entry Address        |
190b57cec5SDimitry Andric //        +---------------------------------------+
200b57cec5SDimitry Andric //        |           Packed Form Data            |
210b57cec5SDimitry Andric //        +---------------------------------------+
220b57cec5SDimitry Andric //
230b57cec5SDimitry Andric // This layout is parsed by Decoder::dumpPackedEntry.  No unwind bytecode is
240b57cec5SDimitry Andric // associated with such a frame as they can be derived from the provided data.
250b57cec5SDimitry Andric // The decoder does not synthesize this data as it is unnecessary for the
260b57cec5SDimitry Andric // purposes of validation, with the synthesis being required only by a proper
270b57cec5SDimitry Andric // unwinder.
280b57cec5SDimitry Andric //
290b57cec5SDimitry Andric // For functions that are large or do not match canonical forms, the data is
300b57cec5SDimitry Andric // split up into two portions, with the actual data residing in the "exception
310b57cec5SDimitry Andric // data" table (.xdata) with a reference to the entry from the "procedure data"
320b57cec5SDimitry Andric // (.pdata) entry.
330b57cec5SDimitry Andric //
340b57cec5SDimitry Andric // The exception data contains information about the frame setup, all of the
350b57cec5SDimitry Andric // epilogue scopes (for functions for which there are multiple exit points) and
360b57cec5SDimitry Andric // the associated exception handler.  Additionally, the entry contains byte-code
370b57cec5SDimitry Andric // describing how to unwind the function (c.f. Decoder::decodeOpcodes).
380b57cec5SDimitry Andric //
390b57cec5SDimitry Andric //        +---------------------------------------+
400b57cec5SDimitry Andric //        |         Function Entry Address        |
410b57cec5SDimitry Andric //        +---------------------------------------+
420b57cec5SDimitry Andric //        |      Exception Data Entry Address     |
430b57cec5SDimitry Andric //        +---------------------------------------+
440b57cec5SDimitry Andric //
450b57cec5SDimitry Andric // This layout is parsed by Decoder::dumpUnpackedEntry.  Such an entry must
460b57cec5SDimitry Andric // first resolve the exception data entry address.  This structure
470b57cec5SDimitry Andric // (ExceptionDataRecord) has a variable sized header
480b57cec5SDimitry Andric // (c.f. ARM::WinEH::HeaderWords) and encodes most of the same information as
490b57cec5SDimitry Andric // the packed form.  However, because this information is insufficient to
500b57cec5SDimitry Andric // synthesize the unwinding, there are associated unwinding bytecode which make
510b57cec5SDimitry Andric // up the bulk of the Decoder.
520b57cec5SDimitry Andric //
530b57cec5SDimitry Andric // The decoder itself is table-driven, using the first byte to determine the
540b57cec5SDimitry Andric // opcode and dispatching to the associated printing routine.  The bytecode
550b57cec5SDimitry Andric // itself is a variable length instruction encoding that can fully describe the
560b57cec5SDimitry Andric // state of the stack and the necessary operations for unwinding to the
570b57cec5SDimitry Andric // beginning of the frame.
580b57cec5SDimitry Andric //
590b57cec5SDimitry Andric // The byte-code maintains a 1-1 instruction mapping, indicating both the width
600b57cec5SDimitry Andric // of the instruction (Thumb2 instructions are variable length, 16 or 32 bits
610b57cec5SDimitry Andric // wide) allowing the program to unwind from any point in the prologue, body, or
620b57cec5SDimitry Andric // epilogue of the function.
630b57cec5SDimitry Andric 
640b57cec5SDimitry Andric #include "ARMWinEHPrinter.h"
650b57cec5SDimitry Andric #include "llvm/ADT/STLExtras.h"
660b57cec5SDimitry Andric #include "llvm/ADT/StringExtras.h"
670b57cec5SDimitry Andric #include "llvm/Support/ARMWinEH.h"
680b57cec5SDimitry Andric #include "llvm/Support/Format.h"
690b57cec5SDimitry Andric 
700b57cec5SDimitry Andric using namespace llvm;
710b57cec5SDimitry Andric using namespace llvm::object;
720b57cec5SDimitry Andric using namespace llvm::support;
730b57cec5SDimitry Andric 
740b57cec5SDimitry Andric namespace llvm {
750b57cec5SDimitry Andric raw_ostream &operator<<(raw_ostream &OS, const ARM::WinEH::ReturnType &RT) {
760b57cec5SDimitry Andric   switch (RT) {
770b57cec5SDimitry Andric   case ARM::WinEH::ReturnType::RT_POP:
780b57cec5SDimitry Andric     OS << "pop {pc}";
790b57cec5SDimitry Andric     break;
800b57cec5SDimitry Andric   case ARM::WinEH::ReturnType::RT_B:
810b57cec5SDimitry Andric     OS << "b target";
820b57cec5SDimitry Andric     break;
830b57cec5SDimitry Andric   case ARM::WinEH::ReturnType::RT_BW:
840b57cec5SDimitry Andric     OS << "b.w target";
850b57cec5SDimitry Andric     break;
860b57cec5SDimitry Andric   case ARM::WinEH::ReturnType::RT_NoEpilogue:
870b57cec5SDimitry Andric     OS << "(no epilogue)";
880b57cec5SDimitry Andric     break;
890b57cec5SDimitry Andric   }
900b57cec5SDimitry Andric   return OS;
910b57cec5SDimitry Andric }
920b57cec5SDimitry Andric }
930b57cec5SDimitry Andric 
940b57cec5SDimitry Andric static std::string formatSymbol(StringRef Name, uint64_t Address,
950b57cec5SDimitry Andric                                 uint64_t Offset = 0) {
960b57cec5SDimitry Andric   std::string Buffer;
970b57cec5SDimitry Andric   raw_string_ostream OS(Buffer);
980b57cec5SDimitry Andric 
990b57cec5SDimitry Andric   if (!Name.empty())
1000b57cec5SDimitry Andric     OS << Name << " ";
1010b57cec5SDimitry Andric 
1020b57cec5SDimitry Andric   if (Offset)
1030b57cec5SDimitry Andric     OS << format("+0x%X (0x%" PRIX64 ")", Offset, Address);
1040b57cec5SDimitry Andric   else if (!Name.empty())
1050b57cec5SDimitry Andric     OS << format("(0x%" PRIX64 ")", Address);
1060b57cec5SDimitry Andric   else
1070b57cec5SDimitry Andric     OS << format("0x%" PRIX64, Address);
1080b57cec5SDimitry Andric 
1090b57cec5SDimitry Andric   return OS.str();
1100b57cec5SDimitry Andric }
1110b57cec5SDimitry Andric 
1120b57cec5SDimitry Andric namespace llvm {
1130b57cec5SDimitry Andric namespace ARM {
1140b57cec5SDimitry Andric namespace WinEH {
1150b57cec5SDimitry Andric const size_t Decoder::PDataEntrySize = sizeof(RuntimeFunction);
1160b57cec5SDimitry Andric 
1170b57cec5SDimitry Andric // TODO name the uops more appropriately
1180b57cec5SDimitry Andric const Decoder::RingEntry Decoder::Ring[] = {
1190b57cec5SDimitry Andric   { 0x80, 0x00, 1, &Decoder::opcode_0xxxxxxx },  // UOP_STACK_FREE (16-bit)
1200b57cec5SDimitry Andric   { 0xc0, 0x80, 2, &Decoder::opcode_10Lxxxxx },  // UOP_POP (32-bit)
1210b57cec5SDimitry Andric   { 0xf0, 0xc0, 1, &Decoder::opcode_1100xxxx },  // UOP_STACK_SAVE (16-bit)
1220b57cec5SDimitry Andric   { 0xf8, 0xd0, 1, &Decoder::opcode_11010Lxx },  // UOP_POP (16-bit)
1230b57cec5SDimitry Andric   { 0xf8, 0xd8, 1, &Decoder::opcode_11011Lxx },  // UOP_POP (32-bit)
1240b57cec5SDimitry Andric   { 0xf8, 0xe0, 1, &Decoder::opcode_11100xxx },  // UOP_VPOP (32-bit)
1250b57cec5SDimitry Andric   { 0xfc, 0xe8, 2, &Decoder::opcode_111010xx },  // UOP_STACK_FREE (32-bit)
1260b57cec5SDimitry Andric   { 0xfe, 0xec, 2, &Decoder::opcode_1110110L },  // UOP_POP (16-bit)
1270b57cec5SDimitry Andric   { 0xff, 0xee, 2, &Decoder::opcode_11101110 },  // UOP_MICROSOFT_SPECIFIC (16-bit)
1280b57cec5SDimitry Andric                                               // UOP_PUSH_MACHINE_FRAME
1290b57cec5SDimitry Andric                                               // UOP_PUSH_CONTEXT
1300b57cec5SDimitry Andric                                               // UOP_PUSH_TRAP_FRAME
1310b57cec5SDimitry Andric                                               // UOP_REDZONE_RESTORE_LR
1320b57cec5SDimitry Andric   { 0xff, 0xef, 2, &Decoder::opcode_11101111 },  // UOP_LDRPC_POSTINC (32-bit)
1330b57cec5SDimitry Andric   { 0xff, 0xf5, 2, &Decoder::opcode_11110101 },  // UOP_VPOP (32-bit)
1340b57cec5SDimitry Andric   { 0xff, 0xf6, 2, &Decoder::opcode_11110110 },  // UOP_VPOP (32-bit)
1350b57cec5SDimitry Andric   { 0xff, 0xf7, 3, &Decoder::opcode_11110111 },  // UOP_STACK_RESTORE (16-bit)
1360b57cec5SDimitry Andric   { 0xff, 0xf8, 4, &Decoder::opcode_11111000 },  // UOP_STACK_RESTORE (16-bit)
1370b57cec5SDimitry Andric   { 0xff, 0xf9, 3, &Decoder::opcode_11111001 },  // UOP_STACK_RESTORE (32-bit)
1380b57cec5SDimitry Andric   { 0xff, 0xfa, 4, &Decoder::opcode_11111010 },  // UOP_STACK_RESTORE (32-bit)
1390b57cec5SDimitry Andric   { 0xff, 0xfb, 1, &Decoder::opcode_11111011 },  // UOP_NOP (16-bit)
1400b57cec5SDimitry Andric   { 0xff, 0xfc, 1, &Decoder::opcode_11111100 },  // UOP_NOP (32-bit)
1410b57cec5SDimitry Andric   { 0xff, 0xfd, 1, &Decoder::opcode_11111101 },  // UOP_NOP (16-bit) / END
1420b57cec5SDimitry Andric   { 0xff, 0xfe, 1, &Decoder::opcode_11111110 },  // UOP_NOP (32-bit) / END
1430b57cec5SDimitry Andric   { 0xff, 0xff, 1, &Decoder::opcode_11111111 },  // UOP_END
1440b57cec5SDimitry Andric };
1450b57cec5SDimitry Andric 
1460b57cec5SDimitry Andric 
1470b57cec5SDimitry Andric // Unwind opcodes for ARM64.
1480b57cec5SDimitry Andric // https://docs.microsoft.com/en-us/cpp/build/arm64-exception-handling
1490b57cec5SDimitry Andric const Decoder::RingEntry Decoder::Ring64[] = {
1500b57cec5SDimitry Andric   { 0xe0, 0x00, 1, &Decoder::opcode_alloc_s },
1510b57cec5SDimitry Andric   { 0xe0, 0x20, 1, &Decoder::opcode_save_r19r20_x },
1520b57cec5SDimitry Andric   { 0xc0, 0x40, 1, &Decoder::opcode_save_fplr },
1530b57cec5SDimitry Andric   { 0xc0, 0x80, 1, &Decoder::opcode_save_fplr_x },
1540b57cec5SDimitry Andric   { 0xf8, 0xc0, 2, &Decoder::opcode_alloc_m },
1550b57cec5SDimitry Andric   { 0xfc, 0xc8, 2, &Decoder::opcode_save_regp },
1560b57cec5SDimitry Andric   { 0xfc, 0xcc, 2, &Decoder::opcode_save_regp_x },
1570b57cec5SDimitry Andric   { 0xfc, 0xd0, 2, &Decoder::opcode_save_reg },
1580b57cec5SDimitry Andric   { 0xfe, 0xd4, 2, &Decoder::opcode_save_reg_x },
1590b57cec5SDimitry Andric   { 0xfe, 0xd6, 2, &Decoder::opcode_save_lrpair },
1600b57cec5SDimitry Andric   { 0xfe, 0xd8, 2, &Decoder::opcode_save_fregp },
1610b57cec5SDimitry Andric   { 0xfe, 0xda, 2, &Decoder::opcode_save_fregp_x },
1620b57cec5SDimitry Andric   { 0xfe, 0xdc, 2, &Decoder::opcode_save_freg },
1630b57cec5SDimitry Andric   { 0xff, 0xde, 2, &Decoder::opcode_save_freg_x },
1640b57cec5SDimitry Andric   { 0xff, 0xe0, 4, &Decoder::opcode_alloc_l },
1650b57cec5SDimitry Andric   { 0xff, 0xe1, 1, &Decoder::opcode_setfp },
1660b57cec5SDimitry Andric   { 0xff, 0xe2, 2, &Decoder::opcode_addfp },
1670b57cec5SDimitry Andric   { 0xff, 0xe3, 1, &Decoder::opcode_nop },
1680b57cec5SDimitry Andric   { 0xff, 0xe4, 1, &Decoder::opcode_end },
1690b57cec5SDimitry Andric   { 0xff, 0xe5, 1, &Decoder::opcode_end_c },
170*e8d8bef9SDimitry Andric   { 0xff, 0xe6, 1, &Decoder::opcode_save_next },
171*e8d8bef9SDimitry Andric   { 0xff, 0xe8, 1, &Decoder::opcode_trap_frame },
172*e8d8bef9SDimitry Andric   { 0xff, 0xe9, 1, &Decoder::opcode_machine_frame },
173*e8d8bef9SDimitry Andric   { 0xff, 0xea, 1, &Decoder::opcode_context },
174*e8d8bef9SDimitry Andric   { 0xff, 0xec, 1, &Decoder::opcode_clear_unwound_to_call },
1750b57cec5SDimitry Andric };
1760b57cec5SDimitry Andric 
1770b57cec5SDimitry Andric void Decoder::printRegisters(const std::pair<uint16_t, uint32_t> &RegisterMask) {
1780b57cec5SDimitry Andric   static const char * const GPRRegisterNames[16] = {
1790b57cec5SDimitry Andric     "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7", "r8", "r9", "r10",
1800b57cec5SDimitry Andric     "r11", "ip", "sp", "lr", "pc",
1810b57cec5SDimitry Andric   };
1820b57cec5SDimitry Andric 
1830b57cec5SDimitry Andric   const uint16_t GPRMask = std::get<0>(RegisterMask);
1840b57cec5SDimitry Andric   const uint16_t VFPMask = std::get<1>(RegisterMask);
1850b57cec5SDimitry Andric 
1860b57cec5SDimitry Andric   OS << '{';
1870b57cec5SDimitry Andric   bool Comma = false;
1880b57cec5SDimitry Andric   for (unsigned RI = 0, RE = 11; RI < RE; ++RI) {
1890b57cec5SDimitry Andric     if (GPRMask & (1 << RI)) {
1900b57cec5SDimitry Andric       if (Comma)
1910b57cec5SDimitry Andric         OS << ", ";
1920b57cec5SDimitry Andric       OS << GPRRegisterNames[RI];
1930b57cec5SDimitry Andric       Comma = true;
1940b57cec5SDimitry Andric     }
1950b57cec5SDimitry Andric   }
1960b57cec5SDimitry Andric   for (unsigned RI = 0, RE = 32; RI < RE; ++RI) {
1970b57cec5SDimitry Andric     if (VFPMask & (1 << RI)) {
1980b57cec5SDimitry Andric       if (Comma)
1990b57cec5SDimitry Andric         OS << ", ";
2000b57cec5SDimitry Andric       OS << "d" << unsigned(RI);
2010b57cec5SDimitry Andric       Comma = true;
2020b57cec5SDimitry Andric     }
2030b57cec5SDimitry Andric   }
2040b57cec5SDimitry Andric   for (unsigned RI = 11, RE = 16; RI < RE; ++RI) {
2050b57cec5SDimitry Andric     if (GPRMask & (1 << RI)) {
2060b57cec5SDimitry Andric       if (Comma)
2070b57cec5SDimitry Andric         OS << ", ";
2080b57cec5SDimitry Andric       OS << GPRRegisterNames[RI];
2090b57cec5SDimitry Andric       Comma = true;
2100b57cec5SDimitry Andric     }
2110b57cec5SDimitry Andric   }
2120b57cec5SDimitry Andric   OS << '}';
2130b57cec5SDimitry Andric }
2140b57cec5SDimitry Andric 
2150b57cec5SDimitry Andric ErrorOr<object::SectionRef>
2160b57cec5SDimitry Andric Decoder::getSectionContaining(const COFFObjectFile &COFF, uint64_t VA) {
2170b57cec5SDimitry Andric   for (const auto &Section : COFF.sections()) {
2180b57cec5SDimitry Andric     uint64_t Address = Section.getAddress();
2190b57cec5SDimitry Andric     uint64_t Size = Section.getSize();
2200b57cec5SDimitry Andric 
2210b57cec5SDimitry Andric     if (VA >= Address && (VA - Address) <= Size)
2220b57cec5SDimitry Andric       return Section;
2230b57cec5SDimitry Andric   }
224*e8d8bef9SDimitry Andric   return inconvertibleErrorCode();
2250b57cec5SDimitry Andric }
2260b57cec5SDimitry Andric 
2270b57cec5SDimitry Andric ErrorOr<object::SymbolRef> Decoder::getSymbol(const COFFObjectFile &COFF,
2280b57cec5SDimitry Andric                                               uint64_t VA, bool FunctionOnly) {
2290b57cec5SDimitry Andric   for (const auto &Symbol : COFF.symbols()) {
2300b57cec5SDimitry Andric     Expected<SymbolRef::Type> Type = Symbol.getType();
2310b57cec5SDimitry Andric     if (!Type)
2320b57cec5SDimitry Andric       return errorToErrorCode(Type.takeError());
2330b57cec5SDimitry Andric     if (FunctionOnly && *Type != SymbolRef::ST_Function)
2340b57cec5SDimitry Andric       continue;
2350b57cec5SDimitry Andric 
2360b57cec5SDimitry Andric     Expected<uint64_t> Address = Symbol.getAddress();
2370b57cec5SDimitry Andric     if (!Address)
2380b57cec5SDimitry Andric       return errorToErrorCode(Address.takeError());
2390b57cec5SDimitry Andric     if (*Address == VA)
2400b57cec5SDimitry Andric       return Symbol;
2410b57cec5SDimitry Andric   }
242*e8d8bef9SDimitry Andric   return inconvertibleErrorCode();
2430b57cec5SDimitry Andric }
2440b57cec5SDimitry Andric 
2450b57cec5SDimitry Andric ErrorOr<SymbolRef> Decoder::getRelocatedSymbol(const COFFObjectFile &,
2460b57cec5SDimitry Andric                                                const SectionRef &Section,
2470b57cec5SDimitry Andric                                                uint64_t Offset) {
2480b57cec5SDimitry Andric   for (const auto &Relocation : Section.relocations()) {
2490b57cec5SDimitry Andric     uint64_t RelocationOffset = Relocation.getOffset();
2500b57cec5SDimitry Andric     if (RelocationOffset == Offset)
2510b57cec5SDimitry Andric       return *Relocation.getSymbol();
2520b57cec5SDimitry Andric   }
253*e8d8bef9SDimitry Andric   return inconvertibleErrorCode();
2540b57cec5SDimitry Andric }
2550b57cec5SDimitry Andric 
2560b57cec5SDimitry Andric bool Decoder::opcode_0xxxxxxx(const uint8_t *OC, unsigned &Offset,
2570b57cec5SDimitry Andric                               unsigned Length, bool Prologue) {
2580b57cec5SDimitry Andric   uint8_t Imm = OC[Offset] & 0x7f;
2590b57cec5SDimitry Andric   SW.startLine() << format("0x%02x                ; %s sp, #(%u * 4)\n",
2600b57cec5SDimitry Andric                            OC[Offset],
2610b57cec5SDimitry Andric                            static_cast<const char *>(Prologue ? "sub" : "add"),
2620b57cec5SDimitry Andric                            Imm);
2630b57cec5SDimitry Andric   ++Offset;
2640b57cec5SDimitry Andric   return false;
2650b57cec5SDimitry Andric }
2660b57cec5SDimitry Andric 
2670b57cec5SDimitry Andric bool Decoder::opcode_10Lxxxxx(const uint8_t *OC, unsigned &Offset,
2680b57cec5SDimitry Andric                               unsigned Length, bool Prologue) {
2690b57cec5SDimitry Andric   unsigned Link = (OC[Offset] & 0x20) >> 5;
2700b57cec5SDimitry Andric   uint16_t RegisterMask = (Link << (Prologue ? 14 : 15))
2710b57cec5SDimitry Andric                         | ((OC[Offset + 0] & 0x1f) << 8)
2720b57cec5SDimitry Andric                         | ((OC[Offset + 1] & 0xff) << 0);
2730b57cec5SDimitry Andric   assert((~RegisterMask & (1 << 13)) && "sp must not be set");
2740b57cec5SDimitry Andric   assert((~RegisterMask & (1 << (Prologue ? 15 : 14))) && "pc must not be set");
2750b57cec5SDimitry Andric 
2760b57cec5SDimitry Andric   SW.startLine() << format("0x%02x 0x%02x           ; %s.w ",
2770b57cec5SDimitry Andric                            OC[Offset + 0], OC[Offset + 1],
2780b57cec5SDimitry Andric                            Prologue ? "push" : "pop");
2790b57cec5SDimitry Andric   printRegisters(std::make_pair(RegisterMask, 0));
2800b57cec5SDimitry Andric   OS << '\n';
2810b57cec5SDimitry Andric 
2820b57cec5SDimitry Andric   Offset += 2;
2830b57cec5SDimitry Andric   return false;
2840b57cec5SDimitry Andric }
2850b57cec5SDimitry Andric 
2860b57cec5SDimitry Andric bool Decoder::opcode_1100xxxx(const uint8_t *OC, unsigned &Offset,
2870b57cec5SDimitry Andric                               unsigned Length, bool Prologue) {
2880b57cec5SDimitry Andric   if (Prologue)
2890b57cec5SDimitry Andric     SW.startLine() << format("0x%02x                ; mov r%u, sp\n",
2900b57cec5SDimitry Andric                              OC[Offset], OC[Offset] & 0xf);
2910b57cec5SDimitry Andric   else
2920b57cec5SDimitry Andric     SW.startLine() << format("0x%02x                ; mov sp, r%u\n",
2930b57cec5SDimitry Andric                              OC[Offset], OC[Offset] & 0xf);
2940b57cec5SDimitry Andric   ++Offset;
2950b57cec5SDimitry Andric   return false;
2960b57cec5SDimitry Andric }
2970b57cec5SDimitry Andric 
2980b57cec5SDimitry Andric bool Decoder::opcode_11010Lxx(const uint8_t *OC, unsigned &Offset,
2990b57cec5SDimitry Andric                               unsigned Length, bool Prologue) {
3000b57cec5SDimitry Andric   unsigned Link = (OC[Offset] & 0x4) >> 3;
3010b57cec5SDimitry Andric   unsigned Count = (OC[Offset] & 0x3);
3020b57cec5SDimitry Andric 
3030b57cec5SDimitry Andric   uint16_t GPRMask = (Link << (Prologue ? 14 : 15))
3040b57cec5SDimitry Andric                    | (((1 << (Count + 1)) - 1) << 4);
3050b57cec5SDimitry Andric 
3060b57cec5SDimitry Andric   SW.startLine() << format("0x%02x                ; %s ", OC[Offset],
3070b57cec5SDimitry Andric                            Prologue ? "push" : "pop");
3080b57cec5SDimitry Andric   printRegisters(std::make_pair(GPRMask, 0));
3090b57cec5SDimitry Andric   OS << '\n';
3100b57cec5SDimitry Andric 
3110b57cec5SDimitry Andric   ++Offset;
3120b57cec5SDimitry Andric   return false;
3130b57cec5SDimitry Andric }
3140b57cec5SDimitry Andric 
3150b57cec5SDimitry Andric bool Decoder::opcode_11011Lxx(const uint8_t *OC, unsigned &Offset,
3160b57cec5SDimitry Andric                               unsigned Length, bool Prologue) {
3170b57cec5SDimitry Andric   unsigned Link = (OC[Offset] & 0x4) >> 2;
3180b57cec5SDimitry Andric   unsigned Count = (OC[Offset] & 0x3) + 4;
3190b57cec5SDimitry Andric 
3200b57cec5SDimitry Andric   uint16_t GPRMask = (Link << (Prologue ? 14 : 15))
3210b57cec5SDimitry Andric                    | (((1 << (Count + 1)) - 1) << 4);
3220b57cec5SDimitry Andric 
3230b57cec5SDimitry Andric   SW.startLine() << format("0x%02x                ; %s.w ", OC[Offset],
3240b57cec5SDimitry Andric                            Prologue ? "push" : "pop");
3250b57cec5SDimitry Andric   printRegisters(std::make_pair(GPRMask, 0));
3260b57cec5SDimitry Andric   OS << '\n';
3270b57cec5SDimitry Andric 
3280b57cec5SDimitry Andric   ++Offset;
3290b57cec5SDimitry Andric   return false;
3300b57cec5SDimitry Andric }
3310b57cec5SDimitry Andric 
3320b57cec5SDimitry Andric bool Decoder::opcode_11100xxx(const uint8_t *OC, unsigned &Offset,
3330b57cec5SDimitry Andric                               unsigned Length, bool Prologue) {
3340b57cec5SDimitry Andric   unsigned High = (OC[Offset] & 0x7);
3350b57cec5SDimitry Andric   uint32_t VFPMask = (((1 << (High + 1)) - 1) << 8);
3360b57cec5SDimitry Andric 
3370b57cec5SDimitry Andric   SW.startLine() << format("0x%02x                ; %s ", OC[Offset],
3380b57cec5SDimitry Andric                            Prologue ? "vpush" : "vpop");
3390b57cec5SDimitry Andric   printRegisters(std::make_pair(0, VFPMask));
3400b57cec5SDimitry Andric   OS << '\n';
3410b57cec5SDimitry Andric 
3420b57cec5SDimitry Andric   ++Offset;
3430b57cec5SDimitry Andric   return false;
3440b57cec5SDimitry Andric }
3450b57cec5SDimitry Andric 
3460b57cec5SDimitry Andric bool Decoder::opcode_111010xx(const uint8_t *OC, unsigned &Offset,
3470b57cec5SDimitry Andric                               unsigned Length, bool Prologue) {
3480b57cec5SDimitry Andric   uint16_t Imm = ((OC[Offset + 0] & 0x03) << 8) | ((OC[Offset + 1] & 0xff) << 0);
3490b57cec5SDimitry Andric 
3500b57cec5SDimitry Andric   SW.startLine() << format("0x%02x 0x%02x           ; %s.w sp, #(%u * 4)\n",
3510b57cec5SDimitry Andric                            OC[Offset + 0], OC[Offset + 1],
3520b57cec5SDimitry Andric                            static_cast<const char *>(Prologue ? "sub" : "add"),
3530b57cec5SDimitry Andric                            Imm);
3540b57cec5SDimitry Andric 
3550b57cec5SDimitry Andric   Offset += 2;
3560b57cec5SDimitry Andric   return false;
3570b57cec5SDimitry Andric }
3580b57cec5SDimitry Andric 
3590b57cec5SDimitry Andric bool Decoder::opcode_1110110L(const uint8_t *OC, unsigned &Offset,
3600b57cec5SDimitry Andric                               unsigned Length, bool Prologue) {
3610b57cec5SDimitry Andric   uint8_t GPRMask = ((OC[Offset + 0] & 0x01) << (Prologue ? 14 : 15))
3620b57cec5SDimitry Andric                   | ((OC[Offset + 1] & 0xff) << 0);
3630b57cec5SDimitry Andric 
3640b57cec5SDimitry Andric   SW.startLine() << format("0x%02x 0x%02x           ; %s ", OC[Offset + 0],
3650b57cec5SDimitry Andric                            OC[Offset + 1], Prologue ? "push" : "pop");
3660b57cec5SDimitry Andric   printRegisters(std::make_pair(GPRMask, 0));
3670b57cec5SDimitry Andric   OS << '\n';
3680b57cec5SDimitry Andric 
3690b57cec5SDimitry Andric   Offset += 2;
3700b57cec5SDimitry Andric   return false;
3710b57cec5SDimitry Andric }
3720b57cec5SDimitry Andric 
3730b57cec5SDimitry Andric bool Decoder::opcode_11101110(const uint8_t *OC, unsigned &Offset,
3740b57cec5SDimitry Andric                               unsigned Length, bool Prologue) {
3750b57cec5SDimitry Andric   assert(!Prologue && "may not be used in prologue");
3760b57cec5SDimitry Andric 
3770b57cec5SDimitry Andric   if (OC[Offset + 1] & 0xf0)
3780b57cec5SDimitry Andric     SW.startLine() << format("0x%02x 0x%02x           ; reserved\n",
3790b57cec5SDimitry Andric                              OC[Offset + 0], OC[Offset +  1]);
3800b57cec5SDimitry Andric   else
3810b57cec5SDimitry Andric     SW.startLine()
3820b57cec5SDimitry Andric       << format("0x%02x 0x%02x           ; microsoft-specific (type: %u)\n",
3830b57cec5SDimitry Andric                 OC[Offset + 0], OC[Offset + 1], OC[Offset + 1] & 0x0f);
3840b57cec5SDimitry Andric 
3850b57cec5SDimitry Andric   Offset += 2;
3860b57cec5SDimitry Andric   return false;
3870b57cec5SDimitry Andric }
3880b57cec5SDimitry Andric 
3890b57cec5SDimitry Andric bool Decoder::opcode_11101111(const uint8_t *OC, unsigned &Offset,
3900b57cec5SDimitry Andric                               unsigned Length, bool Prologue) {
3910b57cec5SDimitry Andric   assert(!Prologue && "may not be used in prologue");
3920b57cec5SDimitry Andric 
3930b57cec5SDimitry Andric   if (OC[Offset + 1] & 0xf0)
3940b57cec5SDimitry Andric     SW.startLine() << format("0x%02x 0x%02x           ; reserved\n",
3950b57cec5SDimitry Andric                              OC[Offset + 0], OC[Offset +  1]);
3960b57cec5SDimitry Andric   else
3970b57cec5SDimitry Andric     SW.startLine()
3980b57cec5SDimitry Andric       << format("0x%02x 0x%02x           ; ldr.w lr, [sp], #%u\n",
3990b57cec5SDimitry Andric                 OC[Offset + 0], OC[Offset + 1], OC[Offset + 1] << 2);
4000b57cec5SDimitry Andric 
4010b57cec5SDimitry Andric   Offset += 2;
4020b57cec5SDimitry Andric   return false;
4030b57cec5SDimitry Andric }
4040b57cec5SDimitry Andric 
4050b57cec5SDimitry Andric bool Decoder::opcode_11110101(const uint8_t *OC, unsigned &Offset,
4060b57cec5SDimitry Andric                               unsigned Length, bool Prologue) {
4070b57cec5SDimitry Andric   unsigned Start = (OC[Offset + 1] & 0xf0) >> 4;
4080b57cec5SDimitry Andric   unsigned End = (OC[Offset + 1] & 0x0f) >> 0;
4090b57cec5SDimitry Andric   uint32_t VFPMask = ((1 << (End - Start)) - 1) << Start;
4100b57cec5SDimitry Andric 
4110b57cec5SDimitry Andric   SW.startLine() << format("0x%02x 0x%02x           ; %s ", OC[Offset + 0],
4120b57cec5SDimitry Andric                            OC[Offset + 1], Prologue ? "vpush" : "vpop");
4130b57cec5SDimitry Andric   printRegisters(std::make_pair(0, VFPMask));
4140b57cec5SDimitry Andric   OS << '\n';
4150b57cec5SDimitry Andric 
4160b57cec5SDimitry Andric   Offset += 2;
4170b57cec5SDimitry Andric   return false;
4180b57cec5SDimitry Andric }
4190b57cec5SDimitry Andric 
4200b57cec5SDimitry Andric bool Decoder::opcode_11110110(const uint8_t *OC, unsigned &Offset,
4210b57cec5SDimitry Andric                               unsigned Length, bool Prologue) {
4220b57cec5SDimitry Andric   unsigned Start = (OC[Offset + 1] & 0xf0) >> 4;
4230b57cec5SDimitry Andric   unsigned End = (OC[Offset + 1] & 0x0f) >> 0;
4240b57cec5SDimitry Andric   uint32_t VFPMask = ((1 << (End - Start)) - 1) << 16;
4250b57cec5SDimitry Andric 
4260b57cec5SDimitry Andric   SW.startLine() << format("0x%02x 0x%02x           ; %s ", OC[Offset + 0],
4270b57cec5SDimitry Andric                            OC[Offset + 1], Prologue ? "vpush" : "vpop");
4280b57cec5SDimitry Andric   printRegisters(std::make_pair(0, VFPMask));
4290b57cec5SDimitry Andric   OS << '\n';
4300b57cec5SDimitry Andric 
4310b57cec5SDimitry Andric   Offset += 2;
4320b57cec5SDimitry Andric   return false;
4330b57cec5SDimitry Andric }
4340b57cec5SDimitry Andric 
4350b57cec5SDimitry Andric bool Decoder::opcode_11110111(const uint8_t *OC, unsigned &Offset,
4360b57cec5SDimitry Andric                               unsigned Length, bool Prologue) {
4370b57cec5SDimitry Andric   uint32_t Imm = (OC[Offset + 1] << 8) | (OC[Offset + 2] << 0);
4380b57cec5SDimitry Andric 
4390b57cec5SDimitry Andric   SW.startLine() << format("0x%02x 0x%02x 0x%02x      ; %s sp, sp, #(%u * 4)\n",
4400b57cec5SDimitry Andric                            OC[Offset + 0], OC[Offset + 1], OC[Offset + 2],
4410b57cec5SDimitry Andric                            static_cast<const char *>(Prologue ? "sub" : "add"),
4420b57cec5SDimitry Andric                            Imm);
4430b57cec5SDimitry Andric 
4440b57cec5SDimitry Andric   Offset += 3;
4450b57cec5SDimitry Andric   return false;
4460b57cec5SDimitry Andric }
4470b57cec5SDimitry Andric 
4480b57cec5SDimitry Andric bool Decoder::opcode_11111000(const uint8_t *OC, unsigned &Offset,
4490b57cec5SDimitry Andric                               unsigned Length, bool Prologue) {
4500b57cec5SDimitry Andric   uint32_t Imm = (OC[Offset + 1] << 16)
4510b57cec5SDimitry Andric                | (OC[Offset + 2] << 8)
4520b57cec5SDimitry Andric                | (OC[Offset + 3] << 0);
4530b57cec5SDimitry Andric 
4540b57cec5SDimitry Andric   SW.startLine()
4550b57cec5SDimitry Andric     << format("0x%02x 0x%02x 0x%02x 0x%02x ; %s sp, sp, #(%u * 4)\n",
4560b57cec5SDimitry Andric               OC[Offset + 0], OC[Offset + 1], OC[Offset + 2], OC[Offset + 3],
4570b57cec5SDimitry Andric               static_cast<const char *>(Prologue ? "sub" : "add"), Imm);
4580b57cec5SDimitry Andric 
4590b57cec5SDimitry Andric   Offset += 4;
4600b57cec5SDimitry Andric   return false;
4610b57cec5SDimitry Andric }
4620b57cec5SDimitry Andric 
4630b57cec5SDimitry Andric bool Decoder::opcode_11111001(const uint8_t *OC, unsigned &Offset,
4640b57cec5SDimitry Andric                               unsigned Length, bool Prologue) {
4650b57cec5SDimitry Andric   uint32_t Imm = (OC[Offset + 1] << 8) | (OC[Offset + 2] << 0);
4660b57cec5SDimitry Andric 
4670b57cec5SDimitry Andric   SW.startLine()
4680b57cec5SDimitry Andric     << format("0x%02x 0x%02x 0x%02x      ; %s.w sp, sp, #(%u * 4)\n",
4690b57cec5SDimitry Andric               OC[Offset + 0], OC[Offset + 1], OC[Offset + 2],
4700b57cec5SDimitry Andric               static_cast<const char *>(Prologue ? "sub" : "add"), Imm);
4710b57cec5SDimitry Andric 
4720b57cec5SDimitry Andric   Offset += 3;
4730b57cec5SDimitry Andric   return false;
4740b57cec5SDimitry Andric }
4750b57cec5SDimitry Andric 
4760b57cec5SDimitry Andric bool Decoder::opcode_11111010(const uint8_t *OC, unsigned &Offset,
4770b57cec5SDimitry Andric                               unsigned Length, bool Prologue) {
4780b57cec5SDimitry Andric   uint32_t Imm = (OC[Offset + 1] << 16)
4790b57cec5SDimitry Andric                | (OC[Offset + 2] << 8)
4800b57cec5SDimitry Andric                | (OC[Offset + 3] << 0);
4810b57cec5SDimitry Andric 
4820b57cec5SDimitry Andric   SW.startLine()
4830b57cec5SDimitry Andric     << format("0x%02x 0x%02x 0x%02x 0x%02x ; %s.w sp, sp, #(%u * 4)\n",
4840b57cec5SDimitry Andric               OC[Offset + 0], OC[Offset + 1], OC[Offset + 2], OC[Offset + 3],
4850b57cec5SDimitry Andric               static_cast<const char *>(Prologue ? "sub" : "add"), Imm);
4860b57cec5SDimitry Andric 
4870b57cec5SDimitry Andric   Offset += 4;
4880b57cec5SDimitry Andric   return false;
4890b57cec5SDimitry Andric }
4900b57cec5SDimitry Andric 
4910b57cec5SDimitry Andric bool Decoder::opcode_11111011(const uint8_t *OC, unsigned &Offset,
4920b57cec5SDimitry Andric                               unsigned Length, bool Prologue) {
4930b57cec5SDimitry Andric   SW.startLine() << format("0x%02x                ; nop\n", OC[Offset]);
4940b57cec5SDimitry Andric   ++Offset;
4950b57cec5SDimitry Andric   return false;
4960b57cec5SDimitry Andric }
4970b57cec5SDimitry Andric 
4980b57cec5SDimitry Andric bool Decoder::opcode_11111100(const uint8_t *OC, unsigned &Offset,
4990b57cec5SDimitry Andric                               unsigned Length, bool Prologue) {
5000b57cec5SDimitry Andric   SW.startLine() << format("0x%02x                ; nop.w\n", OC[Offset]);
5010b57cec5SDimitry Andric   ++Offset;
5020b57cec5SDimitry Andric   return false;
5030b57cec5SDimitry Andric }
5040b57cec5SDimitry Andric 
5050b57cec5SDimitry Andric bool Decoder::opcode_11111101(const uint8_t *OC, unsigned &Offset,
5060b57cec5SDimitry Andric                               unsigned Length, bool Prologue) {
5070b57cec5SDimitry Andric   SW.startLine() << format("0x%02x                ; b\n", OC[Offset]);
5080b57cec5SDimitry Andric   ++Offset;
5090b57cec5SDimitry Andric   return true;
5100b57cec5SDimitry Andric }
5110b57cec5SDimitry Andric 
5120b57cec5SDimitry Andric bool Decoder::opcode_11111110(const uint8_t *OC, unsigned &Offset,
5130b57cec5SDimitry Andric                               unsigned Length, bool Prologue) {
5140b57cec5SDimitry Andric   SW.startLine() << format("0x%02x                ; b.w\n", OC[Offset]);
5150b57cec5SDimitry Andric   ++Offset;
5160b57cec5SDimitry Andric   return true;
5170b57cec5SDimitry Andric }
5180b57cec5SDimitry Andric 
5190b57cec5SDimitry Andric bool Decoder::opcode_11111111(const uint8_t *OC, unsigned &Offset,
5200b57cec5SDimitry Andric                               unsigned Length, bool Prologue) {
5210b57cec5SDimitry Andric   ++Offset;
5220b57cec5SDimitry Andric   return true;
5230b57cec5SDimitry Andric }
5240b57cec5SDimitry Andric 
5250b57cec5SDimitry Andric // ARM64 unwind codes start here.
5260b57cec5SDimitry Andric bool Decoder::opcode_alloc_s(const uint8_t *OC, unsigned &Offset,
5270b57cec5SDimitry Andric                              unsigned Length, bool Prologue) {
5280b57cec5SDimitry Andric   uint32_t NumBytes = (OC[Offset] & 0x1F) << 4;
5290b57cec5SDimitry Andric   SW.startLine() << format("0x%02x                ; %s sp, #%u\n", OC[Offset],
5300b57cec5SDimitry Andric                            static_cast<const char *>(Prologue ? "sub" : "add"),
5310b57cec5SDimitry Andric                            NumBytes);
5320b57cec5SDimitry Andric   ++Offset;
5330b57cec5SDimitry Andric   return false;
5340b57cec5SDimitry Andric }
5350b57cec5SDimitry Andric 
5360b57cec5SDimitry Andric bool Decoder::opcode_save_r19r20_x(const uint8_t *OC, unsigned &Offset,
5370b57cec5SDimitry Andric                                    unsigned Length, bool Prologue) {
5380b57cec5SDimitry Andric   uint32_t Off = (OC[Offset] & 0x1F) << 3;
5390b57cec5SDimitry Andric   if (Prologue)
5400b57cec5SDimitry Andric     SW.startLine() << format(
5410b57cec5SDimitry Andric         "0x%02x                ; stp x19, x20, [sp, #-%u]!\n", OC[Offset], Off);
5420b57cec5SDimitry Andric   else
5430b57cec5SDimitry Andric     SW.startLine() << format(
5440b57cec5SDimitry Andric         "0x%02x                ; ldp x19, x20, [sp], #%u\n", OC[Offset], Off);
5450b57cec5SDimitry Andric   ++Offset;
5460b57cec5SDimitry Andric   return false;
5470b57cec5SDimitry Andric }
5480b57cec5SDimitry Andric 
5490b57cec5SDimitry Andric bool Decoder::opcode_save_fplr(const uint8_t *OC, unsigned &Offset,
5500b57cec5SDimitry Andric                                unsigned Length, bool Prologue) {
5510b57cec5SDimitry Andric   uint32_t Off = (OC[Offset] & 0x3F) << 3;
5520b57cec5SDimitry Andric   SW.startLine() << format(
5530b57cec5SDimitry Andric       "0x%02x                ; %s x29, x30, [sp, #%u]\n", OC[Offset],
5540b57cec5SDimitry Andric       static_cast<const char *>(Prologue ? "stp" : "ldp"), Off);
5550b57cec5SDimitry Andric   ++Offset;
5560b57cec5SDimitry Andric   return false;
5570b57cec5SDimitry Andric }
5580b57cec5SDimitry Andric 
5590b57cec5SDimitry Andric bool Decoder::opcode_save_fplr_x(const uint8_t *OC, unsigned &Offset,
5600b57cec5SDimitry Andric                                  unsigned Length, bool Prologue) {
5610b57cec5SDimitry Andric   uint32_t Off = ((OC[Offset] & 0x3F) + 1) << 3;
5620b57cec5SDimitry Andric   if (Prologue)
5630b57cec5SDimitry Andric     SW.startLine() << format(
5640b57cec5SDimitry Andric         "0x%02x                ; stp x29, x30, [sp, #-%u]!\n", OC[Offset], Off);
5650b57cec5SDimitry Andric   else
5660b57cec5SDimitry Andric     SW.startLine() << format(
5670b57cec5SDimitry Andric         "0x%02x                ; ldp x29, x30, [sp], #%u\n", OC[Offset], Off);
5680b57cec5SDimitry Andric   ++Offset;
5690b57cec5SDimitry Andric   return false;
5700b57cec5SDimitry Andric }
5710b57cec5SDimitry Andric 
5720b57cec5SDimitry Andric bool Decoder::opcode_alloc_m(const uint8_t *OC, unsigned &Offset,
5730b57cec5SDimitry Andric                              unsigned Length, bool Prologue) {
5740b57cec5SDimitry Andric   uint32_t NumBytes = ((OC[Offset] & 0x07) << 8);
5750b57cec5SDimitry Andric   NumBytes |= (OC[Offset + 1] & 0xFF);
5760b57cec5SDimitry Andric   NumBytes <<= 4;
5770b57cec5SDimitry Andric   SW.startLine() << format("0x%02x%02x              ; %s sp, #%u\n",
5780b57cec5SDimitry Andric                            OC[Offset], OC[Offset + 1],
5790b57cec5SDimitry Andric                            static_cast<const char *>(Prologue ? "sub" : "add"),
5800b57cec5SDimitry Andric                            NumBytes);
5810b57cec5SDimitry Andric   Offset += 2;
5820b57cec5SDimitry Andric   return false;
5830b57cec5SDimitry Andric }
5840b57cec5SDimitry Andric 
5850b57cec5SDimitry Andric bool Decoder::opcode_save_regp(const uint8_t *OC, unsigned &Offset,
5860b57cec5SDimitry Andric                                unsigned Length, bool Prologue) {
5870b57cec5SDimitry Andric   uint32_t Reg = ((OC[Offset] & 0x03) << 8);
5880b57cec5SDimitry Andric   Reg |= (OC[Offset + 1] & 0xC0);
5890b57cec5SDimitry Andric   Reg >>= 6;
5900b57cec5SDimitry Andric   Reg += 19;
5910b57cec5SDimitry Andric   uint32_t Off = (OC[Offset + 1] & 0x3F) << 3;
5920b57cec5SDimitry Andric   SW.startLine() << format(
5930b57cec5SDimitry Andric       "0x%02x%02x              ; %s x%u, x%u, [sp, #%u]\n",
5940b57cec5SDimitry Andric       OC[Offset], OC[Offset + 1],
5950b57cec5SDimitry Andric       static_cast<const char *>(Prologue ? "stp" : "ldp"), Reg, Reg + 1, Off);
5960b57cec5SDimitry Andric   Offset += 2;
5970b57cec5SDimitry Andric   return false;
5980b57cec5SDimitry Andric }
5990b57cec5SDimitry Andric 
6000b57cec5SDimitry Andric bool Decoder::opcode_save_regp_x(const uint8_t *OC, unsigned &Offset,
6010b57cec5SDimitry Andric                                  unsigned Length, bool Prologue) {
6020b57cec5SDimitry Andric   uint32_t Reg = ((OC[Offset] & 0x03) << 8);
6030b57cec5SDimitry Andric   Reg |= (OC[Offset + 1] & 0xC0);
6040b57cec5SDimitry Andric   Reg >>= 6;
6050b57cec5SDimitry Andric   Reg += 19;
6060b57cec5SDimitry Andric   uint32_t Off = ((OC[Offset + 1] & 0x3F) + 1) << 3;
6070b57cec5SDimitry Andric   if (Prologue)
6080b57cec5SDimitry Andric     SW.startLine() << format(
6090b57cec5SDimitry Andric         "0x%02x%02x              ; stp x%u, x%u, [sp, #-%u]!\n",
6100b57cec5SDimitry Andric         OC[Offset], OC[Offset + 1], Reg,
6110b57cec5SDimitry Andric         Reg + 1, Off);
6120b57cec5SDimitry Andric   else
6130b57cec5SDimitry Andric     SW.startLine() << format(
6140b57cec5SDimitry Andric         "0x%02x%02x              ; ldp x%u, x%u, [sp], #%u\n",
6150b57cec5SDimitry Andric         OC[Offset], OC[Offset + 1], Reg,
6160b57cec5SDimitry Andric         Reg + 1, Off);
6170b57cec5SDimitry Andric   Offset += 2;
6180b57cec5SDimitry Andric   return false;
6190b57cec5SDimitry Andric }
6200b57cec5SDimitry Andric 
6210b57cec5SDimitry Andric bool Decoder::opcode_save_reg(const uint8_t *OC, unsigned &Offset,
6220b57cec5SDimitry Andric                               unsigned Length, bool Prologue) {
6230b57cec5SDimitry Andric   uint32_t Reg = (OC[Offset] & 0x03) << 8;
6240b57cec5SDimitry Andric   Reg |= (OC[Offset + 1] & 0xC0);
6250b57cec5SDimitry Andric   Reg >>= 6;
6260b57cec5SDimitry Andric   Reg += 19;
6270b57cec5SDimitry Andric   uint32_t Off = (OC[Offset + 1] & 0x3F) << 3;
6280b57cec5SDimitry Andric   SW.startLine() << format("0x%02x%02x              ; %s x%u, [sp, #%u]\n",
6290b57cec5SDimitry Andric                            OC[Offset], OC[Offset + 1],
6300b57cec5SDimitry Andric                            static_cast<const char *>(Prologue ? "str" : "ldr"),
6310b57cec5SDimitry Andric                            Reg, Off);
6320b57cec5SDimitry Andric   Offset += 2;
6330b57cec5SDimitry Andric   return false;
6340b57cec5SDimitry Andric }
6350b57cec5SDimitry Andric 
6360b57cec5SDimitry Andric bool Decoder::opcode_save_reg_x(const uint8_t *OC, unsigned &Offset,
6370b57cec5SDimitry Andric                                 unsigned Length, bool Prologue) {
6380b57cec5SDimitry Andric   uint32_t Reg = (OC[Offset] & 0x01) << 8;
6390b57cec5SDimitry Andric   Reg |= (OC[Offset + 1] & 0xE0);
6400b57cec5SDimitry Andric   Reg >>= 5;
6410b57cec5SDimitry Andric   Reg += 19;
6420b57cec5SDimitry Andric   uint32_t Off = ((OC[Offset + 1] & 0x1F) + 1) << 3;
6430b57cec5SDimitry Andric   if (Prologue)
644*e8d8bef9SDimitry Andric     SW.startLine() << format("0x%02x%02x              ; str x%u, [sp, #-%u]!\n",
6450b57cec5SDimitry Andric                              OC[Offset], OC[Offset + 1], Reg, Off);
6460b57cec5SDimitry Andric   else
6470b57cec5SDimitry Andric     SW.startLine() << format("0x%02x%02x              ; ldr x%u, [sp], #%u\n",
6480b57cec5SDimitry Andric                              OC[Offset], OC[Offset + 1], Reg, Off);
6490b57cec5SDimitry Andric   Offset += 2;
6500b57cec5SDimitry Andric   return false;
6510b57cec5SDimitry Andric }
6520b57cec5SDimitry Andric 
6530b57cec5SDimitry Andric bool Decoder::opcode_save_lrpair(const uint8_t *OC, unsigned &Offset,
6540b57cec5SDimitry Andric                                  unsigned Length, bool Prologue) {
6550b57cec5SDimitry Andric   uint32_t Reg = (OC[Offset] & 0x01) << 8;
6560b57cec5SDimitry Andric   Reg |= (OC[Offset + 1] & 0xC0);
6570b57cec5SDimitry Andric   Reg >>= 6;
6580b57cec5SDimitry Andric   Reg *= 2;
6590b57cec5SDimitry Andric   Reg += 19;
6600b57cec5SDimitry Andric   uint32_t Off = (OC[Offset + 1] & 0x3F) << 3;
6610b57cec5SDimitry Andric   SW.startLine() << format("0x%02x%02x              ; %s x%u, lr, [sp, #%u]\n",
6620b57cec5SDimitry Andric                            OC[Offset], OC[Offset + 1],
6630b57cec5SDimitry Andric                            static_cast<const char *>(Prologue ? "stp" : "ldp"),
6640b57cec5SDimitry Andric                            Reg, Off);
6650b57cec5SDimitry Andric   Offset += 2;
6660b57cec5SDimitry Andric   return false;
6670b57cec5SDimitry Andric }
6680b57cec5SDimitry Andric 
6690b57cec5SDimitry Andric bool Decoder::opcode_save_fregp(const uint8_t *OC, unsigned &Offset,
6700b57cec5SDimitry Andric                                 unsigned Length, bool Prologue) {
6710b57cec5SDimitry Andric   uint32_t Reg = (OC[Offset] & 0x01) << 8;
6720b57cec5SDimitry Andric   Reg |= (OC[Offset + 1] & 0xC0);
6730b57cec5SDimitry Andric   Reg >>= 6;
6740b57cec5SDimitry Andric   Reg += 8;
6750b57cec5SDimitry Andric   uint32_t Off = (OC[Offset + 1] & 0x3F) << 3;
6760b57cec5SDimitry Andric   SW.startLine() << format("0x%02x%02x              ; %s d%u, d%u, [sp, #%u]\n",
6770b57cec5SDimitry Andric                            OC[Offset], OC[Offset + 1],
6780b57cec5SDimitry Andric                            static_cast<const char *>(Prologue ? "stp" : "ldp"),
6790b57cec5SDimitry Andric                            Reg, Reg + 1, Off);
6800b57cec5SDimitry Andric   Offset += 2;
6810b57cec5SDimitry Andric   return false;
6820b57cec5SDimitry Andric }
6830b57cec5SDimitry Andric 
6840b57cec5SDimitry Andric bool Decoder::opcode_save_fregp_x(const uint8_t *OC, unsigned &Offset,
6850b57cec5SDimitry Andric                                   unsigned Length, bool Prologue) {
6860b57cec5SDimitry Andric   uint32_t Reg = (OC[Offset] & 0x01) << 8;
6870b57cec5SDimitry Andric   Reg |= (OC[Offset + 1] & 0xC0);
6880b57cec5SDimitry Andric   Reg >>= 6;
6890b57cec5SDimitry Andric   Reg += 8;
6900b57cec5SDimitry Andric   uint32_t Off = ((OC[Offset + 1] & 0x3F) + 1) << 3;
6910b57cec5SDimitry Andric   if (Prologue)
6920b57cec5SDimitry Andric     SW.startLine() << format(
6930b57cec5SDimitry Andric         "0x%02x%02x              ; stp d%u, d%u, [sp, #-%u]!\n", OC[Offset],
6940b57cec5SDimitry Andric         OC[Offset + 1], Reg, Reg + 1, Off);
6950b57cec5SDimitry Andric   else
6960b57cec5SDimitry Andric     SW.startLine() << format(
6970b57cec5SDimitry Andric         "0x%02x%02x              ; ldp d%u, d%u, [sp], #%u\n", OC[Offset],
6980b57cec5SDimitry Andric         OC[Offset + 1], Reg, Reg + 1, Off);
6990b57cec5SDimitry Andric   Offset += 2;
7000b57cec5SDimitry Andric   return false;
7010b57cec5SDimitry Andric }
7020b57cec5SDimitry Andric 
7030b57cec5SDimitry Andric bool Decoder::opcode_save_freg(const uint8_t *OC, unsigned &Offset,
7040b57cec5SDimitry Andric                                unsigned Length, bool Prologue) {
7050b57cec5SDimitry Andric   uint32_t Reg = (OC[Offset] & 0x01) << 8;
7060b57cec5SDimitry Andric   Reg |= (OC[Offset + 1] & 0xC0);
7070b57cec5SDimitry Andric   Reg >>= 6;
7080b57cec5SDimitry Andric   Reg += 8;
7090b57cec5SDimitry Andric   uint32_t Off = (OC[Offset + 1] & 0x3F) << 3;
7100b57cec5SDimitry Andric   SW.startLine() << format("0x%02x%02x              ; %s d%u, [sp, #%u]\n",
7110b57cec5SDimitry Andric                            OC[Offset], OC[Offset + 1],
7120b57cec5SDimitry Andric                            static_cast<const char *>(Prologue ? "str" : "ldr"),
7130b57cec5SDimitry Andric                            Reg, Off);
7140b57cec5SDimitry Andric   Offset += 2;
7150b57cec5SDimitry Andric   return false;
7160b57cec5SDimitry Andric }
7170b57cec5SDimitry Andric 
7180b57cec5SDimitry Andric bool Decoder::opcode_save_freg_x(const uint8_t *OC, unsigned &Offset,
7190b57cec5SDimitry Andric                                  unsigned Length, bool Prologue) {
7200b57cec5SDimitry Andric   uint32_t Reg = ((OC[Offset + 1] & 0xE0) >> 5) + 8;
7210b57cec5SDimitry Andric   uint32_t Off = ((OC[Offset + 1] & 0x1F) + 1) << 3;
7220b57cec5SDimitry Andric   if (Prologue)
7230b57cec5SDimitry Andric     SW.startLine() << format(
7240b57cec5SDimitry Andric         "0x%02x%02x              ; str d%u, [sp, #-%u]!\n", OC[Offset],
7250b57cec5SDimitry Andric         OC[Offset + 1], Reg, Off);
7260b57cec5SDimitry Andric   else
7270b57cec5SDimitry Andric     SW.startLine() << format(
7280b57cec5SDimitry Andric         "0x%02x%02x              ; ldr d%u, [sp], #%u\n", OC[Offset],
7290b57cec5SDimitry Andric         OC[Offset + 1], Reg, Off);
7300b57cec5SDimitry Andric   Offset += 2;
7310b57cec5SDimitry Andric   return false;
7320b57cec5SDimitry Andric }
7330b57cec5SDimitry Andric 
7340b57cec5SDimitry Andric bool Decoder::opcode_alloc_l(const uint8_t *OC, unsigned &Offset,
7350b57cec5SDimitry Andric                              unsigned Length, bool Prologue) {
7360b57cec5SDimitry Andric   unsigned Off =
7370b57cec5SDimitry Andric       (OC[Offset + 1] << 16) | (OC[Offset + 2] << 8) | (OC[Offset + 3] << 0);
7380b57cec5SDimitry Andric   Off <<= 4;
7390b57cec5SDimitry Andric   SW.startLine() << format(
7400b57cec5SDimitry Andric       "0x%02x%02x%02x%02x          ; %s sp, #%u\n", OC[Offset], OC[Offset + 1],
7410b57cec5SDimitry Andric       OC[Offset + 2], OC[Offset + 3],
7420b57cec5SDimitry Andric       static_cast<const char *>(Prologue ? "sub" : "add"), Off);
7430b57cec5SDimitry Andric   Offset += 4;
7440b57cec5SDimitry Andric   return false;
7450b57cec5SDimitry Andric }
7460b57cec5SDimitry Andric 
7470b57cec5SDimitry Andric bool Decoder::opcode_setfp(const uint8_t *OC, unsigned &Offset, unsigned Length,
7480b57cec5SDimitry Andric                            bool Prologue) {
749*e8d8bef9SDimitry Andric   SW.startLine() << format("0x%02x                ; mov %s, %s\n", OC[Offset],
750*e8d8bef9SDimitry Andric                            static_cast<const char *>(Prologue ? "fp" : "sp"),
751*e8d8bef9SDimitry Andric                            static_cast<const char *>(Prologue ? "sp" : "fp"));
7520b57cec5SDimitry Andric   ++Offset;
7530b57cec5SDimitry Andric   return false;
7540b57cec5SDimitry Andric }
7550b57cec5SDimitry Andric 
7560b57cec5SDimitry Andric bool Decoder::opcode_addfp(const uint8_t *OC, unsigned &Offset, unsigned Length,
7570b57cec5SDimitry Andric                            bool Prologue) {
7580b57cec5SDimitry Andric   unsigned NumBytes = OC[Offset + 1] << 3;
759*e8d8bef9SDimitry Andric   SW.startLine() << format(
760*e8d8bef9SDimitry Andric       "0x%02x%02x              ; %s %s, %s, #%u\n", OC[Offset], OC[Offset + 1],
761*e8d8bef9SDimitry Andric       static_cast<const char *>(Prologue ? "add" : "sub"),
762*e8d8bef9SDimitry Andric       static_cast<const char *>(Prologue ? "fp" : "sp"),
763*e8d8bef9SDimitry Andric       static_cast<const char *>(Prologue ? "sp" : "fp"), NumBytes);
7640b57cec5SDimitry Andric   Offset += 2;
7650b57cec5SDimitry Andric   return false;
7660b57cec5SDimitry Andric }
7670b57cec5SDimitry Andric 
7680b57cec5SDimitry Andric bool Decoder::opcode_nop(const uint8_t *OC, unsigned &Offset, unsigned Length,
7690b57cec5SDimitry Andric                          bool Prologue) {
7700b57cec5SDimitry Andric   SW.startLine() << format("0x%02x                ; nop\n", OC[Offset]);
7710b57cec5SDimitry Andric   ++Offset;
7720b57cec5SDimitry Andric   return false;
7730b57cec5SDimitry Andric }
7740b57cec5SDimitry Andric 
7750b57cec5SDimitry Andric bool Decoder::opcode_end(const uint8_t *OC, unsigned &Offset, unsigned Length,
7760b57cec5SDimitry Andric                          bool Prologue) {
7770b57cec5SDimitry Andric   SW.startLine() << format("0x%02x                ; end\n", OC[Offset]);
7780b57cec5SDimitry Andric   ++Offset;
7790b57cec5SDimitry Andric   return true;
7800b57cec5SDimitry Andric }
7810b57cec5SDimitry Andric 
7820b57cec5SDimitry Andric bool Decoder::opcode_end_c(const uint8_t *OC, unsigned &Offset, unsigned Length,
7830b57cec5SDimitry Andric                            bool Prologue) {
7840b57cec5SDimitry Andric   SW.startLine() << format("0x%02x                ; end_c\n", OC[Offset]);
7850b57cec5SDimitry Andric   ++Offset;
7860b57cec5SDimitry Andric   return true;
7870b57cec5SDimitry Andric }
7880b57cec5SDimitry Andric 
789*e8d8bef9SDimitry Andric bool Decoder::opcode_save_next(const uint8_t *OC, unsigned &Offset,
790*e8d8bef9SDimitry Andric                                unsigned Length, bool Prologue) {
791*e8d8bef9SDimitry Andric   if (Prologue)
792*e8d8bef9SDimitry Andric     SW.startLine() << format("0x%02x                ; save next\n", OC[Offset]);
793*e8d8bef9SDimitry Andric   else
794*e8d8bef9SDimitry Andric     SW.startLine() << format("0x%02x                ; restore next\n",
795*e8d8bef9SDimitry Andric                              OC[Offset]);
796*e8d8bef9SDimitry Andric   ++Offset;
797*e8d8bef9SDimitry Andric   return false;
798*e8d8bef9SDimitry Andric }
799*e8d8bef9SDimitry Andric 
800*e8d8bef9SDimitry Andric bool Decoder::opcode_trap_frame(const uint8_t *OC, unsigned &Offset,
801*e8d8bef9SDimitry Andric                                 unsigned Length, bool Prologue) {
802*e8d8bef9SDimitry Andric   SW.startLine() << format("0x%02x                ; trap frame\n", OC[Offset]);
803*e8d8bef9SDimitry Andric   ++Offset;
804*e8d8bef9SDimitry Andric   return false;
805*e8d8bef9SDimitry Andric }
806*e8d8bef9SDimitry Andric 
807*e8d8bef9SDimitry Andric bool Decoder::opcode_machine_frame(const uint8_t *OC, unsigned &Offset,
808*e8d8bef9SDimitry Andric                                    unsigned Length, bool Prologue) {
809*e8d8bef9SDimitry Andric   SW.startLine() << format("0x%02x                ; machine frame\n",
810*e8d8bef9SDimitry Andric                            OC[Offset]);
811*e8d8bef9SDimitry Andric   ++Offset;
812*e8d8bef9SDimitry Andric   return false;
813*e8d8bef9SDimitry Andric }
814*e8d8bef9SDimitry Andric 
815*e8d8bef9SDimitry Andric bool Decoder::opcode_context(const uint8_t *OC, unsigned &Offset,
816*e8d8bef9SDimitry Andric                              unsigned Length, bool Prologue) {
817*e8d8bef9SDimitry Andric   SW.startLine() << format("0x%02x                ; context\n", OC[Offset]);
818*e8d8bef9SDimitry Andric   ++Offset;
819*e8d8bef9SDimitry Andric   return false;
820*e8d8bef9SDimitry Andric }
821*e8d8bef9SDimitry Andric 
822*e8d8bef9SDimitry Andric bool Decoder::opcode_clear_unwound_to_call(const uint8_t *OC, unsigned &Offset,
823*e8d8bef9SDimitry Andric                                            unsigned Length, bool Prologue) {
824*e8d8bef9SDimitry Andric   SW.startLine() << format("0x%02x                ; clear unwound to call\n",
825*e8d8bef9SDimitry Andric                            OC[Offset]);
826*e8d8bef9SDimitry Andric   ++Offset;
827*e8d8bef9SDimitry Andric   return false;
828*e8d8bef9SDimitry Andric }
829*e8d8bef9SDimitry Andric 
8300b57cec5SDimitry Andric void Decoder::decodeOpcodes(ArrayRef<uint8_t> Opcodes, unsigned Offset,
8310b57cec5SDimitry Andric                             bool Prologue) {
8320b57cec5SDimitry Andric   assert((!Prologue || Offset == 0) && "prologue should always use offset 0");
8330b57cec5SDimitry Andric   const RingEntry* DecodeRing = isAArch64 ? Ring64 : Ring;
8340b57cec5SDimitry Andric   bool Terminated = false;
8350b57cec5SDimitry Andric   for (unsigned OI = Offset, OE = Opcodes.size(); !Terminated && OI < OE; ) {
8360b57cec5SDimitry Andric     for (unsigned DI = 0;; ++DI) {
8370b57cec5SDimitry Andric       if ((isAArch64 && (DI >= array_lengthof(Ring64))) ||
8380b57cec5SDimitry Andric           (!isAArch64 && (DI >= array_lengthof(Ring)))) {
8390b57cec5SDimitry Andric         SW.startLine() << format("0x%02x                ; Bad opcode!\n",
8400b57cec5SDimitry Andric                                  Opcodes.data()[OI]);
8410b57cec5SDimitry Andric         ++OI;
8420b57cec5SDimitry Andric         break;
8430b57cec5SDimitry Andric       }
8440b57cec5SDimitry Andric 
8450b57cec5SDimitry Andric       if ((Opcodes[OI] & DecodeRing[DI].Mask) == DecodeRing[DI].Value) {
8460b57cec5SDimitry Andric         if (OI + DecodeRing[DI].Length > OE) {
8470b57cec5SDimitry Andric           SW.startLine() << format("Opcode 0x%02x goes past the unwind data\n",
8480b57cec5SDimitry Andric                                     Opcodes[OI]);
8490b57cec5SDimitry Andric           OI += DecodeRing[DI].Length;
8500b57cec5SDimitry Andric           break;
8510b57cec5SDimitry Andric         }
8520b57cec5SDimitry Andric         Terminated =
8530b57cec5SDimitry Andric             (this->*DecodeRing[DI].Routine)(Opcodes.data(), OI, 0, Prologue);
8540b57cec5SDimitry Andric         break;
8550b57cec5SDimitry Andric       }
8560b57cec5SDimitry Andric     }
8570b57cec5SDimitry Andric   }
8580b57cec5SDimitry Andric }
8590b57cec5SDimitry Andric 
8600b57cec5SDimitry Andric bool Decoder::dumpXDataRecord(const COFFObjectFile &COFF,
8610b57cec5SDimitry Andric                               const SectionRef &Section,
8620b57cec5SDimitry Andric                               uint64_t FunctionAddress, uint64_t VA) {
8630b57cec5SDimitry Andric   ArrayRef<uint8_t> Contents;
8640b57cec5SDimitry Andric   if (COFF.getSectionContents(COFF.getCOFFSection(Section), Contents))
8650b57cec5SDimitry Andric     return false;
8660b57cec5SDimitry Andric 
8670b57cec5SDimitry Andric   uint64_t SectionVA = Section.getAddress();
8680b57cec5SDimitry Andric   uint64_t Offset = VA - SectionVA;
8690b57cec5SDimitry Andric   const ulittle32_t *Data =
8700b57cec5SDimitry Andric     reinterpret_cast<const ulittle32_t *>(Contents.data() + Offset);
8710b57cec5SDimitry Andric 
8720b57cec5SDimitry Andric   // Sanity check to ensure that the .xdata header is present.
8730b57cec5SDimitry Andric   // A header is one or two words, followed by at least one word to describe
8740b57cec5SDimitry Andric   // the unwind codes. Applicable to both ARM and AArch64.
8750b57cec5SDimitry Andric   if (Contents.size() - Offset < 8)
8760b57cec5SDimitry Andric     report_fatal_error(".xdata must be at least 8 bytes in size");
8770b57cec5SDimitry Andric 
8780b57cec5SDimitry Andric   const ExceptionDataRecord XData(Data, isAArch64);
8790b57cec5SDimitry Andric   DictScope XRS(SW, "ExceptionData");
8800b57cec5SDimitry Andric   SW.printNumber("FunctionLength",
8810b57cec5SDimitry Andric                  isAArch64 ? XData.FunctionLengthInBytesAArch64() :
8820b57cec5SDimitry Andric                  XData.FunctionLengthInBytesARM());
8830b57cec5SDimitry Andric   SW.printNumber("Version", XData.Vers());
8840b57cec5SDimitry Andric   SW.printBoolean("ExceptionData", XData.X());
8850b57cec5SDimitry Andric   SW.printBoolean("EpiloguePacked", XData.E());
8860b57cec5SDimitry Andric   if (!isAArch64)
8870b57cec5SDimitry Andric     SW.printBoolean("Fragment", XData.F());
8880b57cec5SDimitry Andric   SW.printNumber(XData.E() ? "EpilogueOffset" : "EpilogueScopes",
8890b57cec5SDimitry Andric                  XData.EpilogueCount());
8900b57cec5SDimitry Andric   uint64_t ByteCodeLength = XData.CodeWords() * sizeof(uint32_t);
8910b57cec5SDimitry Andric   SW.printNumber("ByteCodeLength", ByteCodeLength);
8920b57cec5SDimitry Andric 
8930b57cec5SDimitry Andric   if ((int64_t)(Contents.size() - Offset - 4 * HeaderWords(XData) -
8940b57cec5SDimitry Andric                 (XData.E() ? 0 : XData.EpilogueCount() * 4) -
8958bcb0991SDimitry Andric                 (XData.X() ? 8 : 0)) < (int64_t)ByteCodeLength) {
8968bcb0991SDimitry Andric     SW.flush();
8970b57cec5SDimitry Andric     report_fatal_error("Malformed unwind data");
8988bcb0991SDimitry Andric   }
8990b57cec5SDimitry Andric 
9000b57cec5SDimitry Andric   if (XData.E()) {
9010b57cec5SDimitry Andric     ArrayRef<uint8_t> UC = XData.UnwindByteCode();
9020b57cec5SDimitry Andric     if (isAArch64 || !XData.F()) {
9030b57cec5SDimitry Andric       ListScope PS(SW, "Prologue");
9040b57cec5SDimitry Andric       decodeOpcodes(UC, 0, /*Prologue=*/true);
9050b57cec5SDimitry Andric     }
9060b57cec5SDimitry Andric     if (XData.EpilogueCount()) {
9070b57cec5SDimitry Andric       ListScope ES(SW, "Epilogue");
9080b57cec5SDimitry Andric       decodeOpcodes(UC, XData.EpilogueCount(), /*Prologue=*/false);
9090b57cec5SDimitry Andric     }
9100b57cec5SDimitry Andric   } else {
9110b57cec5SDimitry Andric     {
9120b57cec5SDimitry Andric       ListScope PS(SW, "Prologue");
9130b57cec5SDimitry Andric       decodeOpcodes(XData.UnwindByteCode(), 0, /*Prologue=*/true);
9140b57cec5SDimitry Andric     }
9150b57cec5SDimitry Andric     ArrayRef<ulittle32_t> EpilogueScopes = XData.EpilogueScopes();
9160b57cec5SDimitry Andric     ListScope ESS(SW, "EpilogueScopes");
9170b57cec5SDimitry Andric     for (const EpilogueScope ES : EpilogueScopes) {
9180b57cec5SDimitry Andric       DictScope ESES(SW, "EpilogueScope");
9190b57cec5SDimitry Andric       SW.printNumber("StartOffset", ES.EpilogueStartOffset());
9200b57cec5SDimitry Andric       if (!isAArch64)
9210b57cec5SDimitry Andric         SW.printNumber("Condition", ES.Condition());
9220b57cec5SDimitry Andric       SW.printNumber("EpilogueStartIndex",
9230b57cec5SDimitry Andric                      isAArch64 ? ES.EpilogueStartIndexAArch64()
9240b57cec5SDimitry Andric                                : ES.EpilogueStartIndexARM());
9250b57cec5SDimitry Andric       if (ES.ES & ~0xffc3ffff)
9260b57cec5SDimitry Andric         SW.printNumber("ReservedBits", (ES.ES >> 18) & 0xF);
9270b57cec5SDimitry Andric 
9280b57cec5SDimitry Andric       ListScope Opcodes(SW, "Opcodes");
9290b57cec5SDimitry Andric       decodeOpcodes(XData.UnwindByteCode(),
9300b57cec5SDimitry Andric                     isAArch64 ? ES.EpilogueStartIndexAArch64()
9310b57cec5SDimitry Andric                               : ES.EpilogueStartIndexARM(),
9320b57cec5SDimitry Andric                     /*Prologue=*/false);
9330b57cec5SDimitry Andric     }
9340b57cec5SDimitry Andric   }
9350b57cec5SDimitry Andric 
9360b57cec5SDimitry Andric   if (XData.X()) {
937480093f4SDimitry Andric     const uint64_t Address = COFF.getImageBase() + XData.ExceptionHandlerRVA();
9380b57cec5SDimitry Andric     const uint32_t Parameter = XData.ExceptionHandlerParameter();
9390b57cec5SDimitry Andric     const size_t HandlerOffset = HeaderWords(XData)
9400b57cec5SDimitry Andric                                + (XData.E() ? 0 : XData.EpilogueCount())
9410b57cec5SDimitry Andric                                + XData.CodeWords();
9420b57cec5SDimitry Andric 
9430b57cec5SDimitry Andric     ErrorOr<SymbolRef> Symbol = getRelocatedSymbol(
9440b57cec5SDimitry Andric         COFF, Section, Offset + HandlerOffset * sizeof(uint32_t));
9450b57cec5SDimitry Andric     if (!Symbol)
9460b57cec5SDimitry Andric       Symbol = getSymbol(COFF, Address, /*FunctionOnly=*/true);
9470b57cec5SDimitry Andric     if (!Symbol) {
9480b57cec5SDimitry Andric       ListScope EHS(SW, "ExceptionHandler");
949480093f4SDimitry Andric       SW.printHex("Routine", Address);
950480093f4SDimitry Andric       SW.printHex("Parameter", Parameter);
9510b57cec5SDimitry Andric       return true;
9520b57cec5SDimitry Andric     }
9530b57cec5SDimitry Andric 
9540b57cec5SDimitry Andric     Expected<StringRef> Name = Symbol->getName();
9550b57cec5SDimitry Andric     if (!Name) {
9560b57cec5SDimitry Andric       std::string Buf;
9570b57cec5SDimitry Andric       llvm::raw_string_ostream OS(Buf);
9580b57cec5SDimitry Andric       logAllUnhandledErrors(Name.takeError(), OS);
9590b57cec5SDimitry Andric       OS.flush();
9600b57cec5SDimitry Andric       report_fatal_error(Buf);
9610b57cec5SDimitry Andric     }
9620b57cec5SDimitry Andric 
9630b57cec5SDimitry Andric     ListScope EHS(SW, "ExceptionHandler");
9640b57cec5SDimitry Andric     SW.printString("Routine", formatSymbol(*Name, Address));
9650b57cec5SDimitry Andric     SW.printHex("Parameter", Parameter);
9660b57cec5SDimitry Andric   }
9670b57cec5SDimitry Andric 
9680b57cec5SDimitry Andric   return true;
9690b57cec5SDimitry Andric }
9700b57cec5SDimitry Andric 
9710b57cec5SDimitry Andric bool Decoder::dumpUnpackedEntry(const COFFObjectFile &COFF,
9720b57cec5SDimitry Andric                                 const SectionRef Section, uint64_t Offset,
9730b57cec5SDimitry Andric                                 unsigned Index, const RuntimeFunction &RF) {
9740b57cec5SDimitry Andric   assert(RF.Flag() == RuntimeFunctionFlag::RFF_Unpacked &&
9750b57cec5SDimitry Andric          "packed entry cannot be treated as an unpacked entry");
9760b57cec5SDimitry Andric 
9770b57cec5SDimitry Andric   ErrorOr<SymbolRef> Function = getRelocatedSymbol(COFF, Section, Offset);
9780b57cec5SDimitry Andric   if (!Function)
979480093f4SDimitry Andric     Function = getSymbol(COFF, COFF.getImageBase() + RF.BeginAddress,
980480093f4SDimitry Andric                          /*FunctionOnly=*/true);
9810b57cec5SDimitry Andric 
9820b57cec5SDimitry Andric   ErrorOr<SymbolRef> XDataRecord = getRelocatedSymbol(COFF, Section, Offset + 4);
9830b57cec5SDimitry Andric   if (!XDataRecord)
9840b57cec5SDimitry Andric     XDataRecord = getSymbol(COFF, RF.ExceptionInformationRVA());
9850b57cec5SDimitry Andric 
9860b57cec5SDimitry Andric   if (!RF.BeginAddress && !Function)
9870b57cec5SDimitry Andric     return false;
9880b57cec5SDimitry Andric   if (!RF.UnwindData && !XDataRecord)
9890b57cec5SDimitry Andric     return false;
9900b57cec5SDimitry Andric 
9910b57cec5SDimitry Andric   StringRef FunctionName;
9920b57cec5SDimitry Andric   uint64_t FunctionAddress;
9930b57cec5SDimitry Andric   if (Function) {
9940b57cec5SDimitry Andric     Expected<StringRef> FunctionNameOrErr = Function->getName();
9950b57cec5SDimitry Andric     if (!FunctionNameOrErr) {
9960b57cec5SDimitry Andric       std::string Buf;
9970b57cec5SDimitry Andric       llvm::raw_string_ostream OS(Buf);
9980b57cec5SDimitry Andric       logAllUnhandledErrors(FunctionNameOrErr.takeError(), OS);
9990b57cec5SDimitry Andric       OS.flush();
10000b57cec5SDimitry Andric       report_fatal_error(Buf);
10010b57cec5SDimitry Andric     }
10020b57cec5SDimitry Andric     FunctionName = *FunctionNameOrErr;
10030b57cec5SDimitry Andric     Expected<uint64_t> FunctionAddressOrErr = Function->getAddress();
10040b57cec5SDimitry Andric     if (!FunctionAddressOrErr) {
10050b57cec5SDimitry Andric       std::string Buf;
10060b57cec5SDimitry Andric       llvm::raw_string_ostream OS(Buf);
10070b57cec5SDimitry Andric       logAllUnhandledErrors(FunctionAddressOrErr.takeError(), OS);
10080b57cec5SDimitry Andric       OS.flush();
10090b57cec5SDimitry Andric       report_fatal_error(Buf);
10100b57cec5SDimitry Andric     }
10110b57cec5SDimitry Andric     FunctionAddress = *FunctionAddressOrErr;
10120b57cec5SDimitry Andric   } else {
10130b57cec5SDimitry Andric     FunctionAddress = COFF.getImageBase() + RF.BeginAddress;
10140b57cec5SDimitry Andric   }
10150b57cec5SDimitry Andric 
10160b57cec5SDimitry Andric   SW.printString("Function", formatSymbol(FunctionName, FunctionAddress));
10170b57cec5SDimitry Andric 
10180b57cec5SDimitry Andric   if (XDataRecord) {
10190b57cec5SDimitry Andric     Expected<StringRef> Name = XDataRecord->getName();
10200b57cec5SDimitry Andric     if (!Name) {
10210b57cec5SDimitry Andric       std::string Buf;
10220b57cec5SDimitry Andric       llvm::raw_string_ostream OS(Buf);
10230b57cec5SDimitry Andric       logAllUnhandledErrors(Name.takeError(), OS);
10240b57cec5SDimitry Andric       OS.flush();
10250b57cec5SDimitry Andric       report_fatal_error(Buf);
10260b57cec5SDimitry Andric     }
10270b57cec5SDimitry Andric 
10280b57cec5SDimitry Andric     Expected<uint64_t> AddressOrErr = XDataRecord->getAddress();
10290b57cec5SDimitry Andric     if (!AddressOrErr) {
10300b57cec5SDimitry Andric       std::string Buf;
10310b57cec5SDimitry Andric       llvm::raw_string_ostream OS(Buf);
10320b57cec5SDimitry Andric       logAllUnhandledErrors(AddressOrErr.takeError(), OS);
10330b57cec5SDimitry Andric       OS.flush();
10340b57cec5SDimitry Andric       report_fatal_error(Buf);
10350b57cec5SDimitry Andric     }
10360b57cec5SDimitry Andric     uint64_t Address = *AddressOrErr;
10370b57cec5SDimitry Andric 
10380b57cec5SDimitry Andric     SW.printString("ExceptionRecord", formatSymbol(*Name, Address));
10390b57cec5SDimitry Andric 
10400b57cec5SDimitry Andric     Expected<section_iterator> SIOrErr = XDataRecord->getSection();
10410b57cec5SDimitry Andric     if (!SIOrErr) {
10420b57cec5SDimitry Andric       // TODO: Actually report errors helpfully.
10430b57cec5SDimitry Andric       consumeError(SIOrErr.takeError());
10440b57cec5SDimitry Andric       return false;
10450b57cec5SDimitry Andric     }
10460b57cec5SDimitry Andric     section_iterator SI = *SIOrErr;
10470b57cec5SDimitry Andric 
10480b57cec5SDimitry Andric     // FIXME: Do we need to add an offset from the relocation?
10490b57cec5SDimitry Andric     return dumpXDataRecord(COFF, *SI, FunctionAddress,
10500b57cec5SDimitry Andric                            RF.ExceptionInformationRVA());
10510b57cec5SDimitry Andric   } else {
10520b57cec5SDimitry Andric     uint64_t Address = COFF.getImageBase() + RF.ExceptionInformationRVA();
10530b57cec5SDimitry Andric     SW.printString("ExceptionRecord", formatSymbol("", Address));
10540b57cec5SDimitry Andric 
10550b57cec5SDimitry Andric     ErrorOr<SectionRef> Section = getSectionContaining(COFF, Address);
10560b57cec5SDimitry Andric     if (!Section)
10570b57cec5SDimitry Andric       return false;
10580b57cec5SDimitry Andric 
10590b57cec5SDimitry Andric     return dumpXDataRecord(COFF, *Section, FunctionAddress, Address);
10600b57cec5SDimitry Andric   }
10610b57cec5SDimitry Andric }
10620b57cec5SDimitry Andric 
10630b57cec5SDimitry Andric bool Decoder::dumpPackedEntry(const object::COFFObjectFile &COFF,
10640b57cec5SDimitry Andric                               const SectionRef Section, uint64_t Offset,
10650b57cec5SDimitry Andric                               unsigned Index, const RuntimeFunction &RF) {
10660b57cec5SDimitry Andric   assert((RF.Flag() == RuntimeFunctionFlag::RFF_Packed ||
10670b57cec5SDimitry Andric           RF.Flag() == RuntimeFunctionFlag::RFF_PackedFragment) &&
10680b57cec5SDimitry Andric          "unpacked entry cannot be treated as a packed entry");
10690b57cec5SDimitry Andric 
10700b57cec5SDimitry Andric   ErrorOr<SymbolRef> Function = getRelocatedSymbol(COFF, Section, Offset);
10710b57cec5SDimitry Andric   if (!Function)
10720b57cec5SDimitry Andric     Function = getSymbol(COFF, RF.BeginAddress, /*FunctionOnly=*/true);
10730b57cec5SDimitry Andric 
10740b57cec5SDimitry Andric   StringRef FunctionName;
10750b57cec5SDimitry Andric   uint64_t FunctionAddress;
10760b57cec5SDimitry Andric   if (Function) {
10770b57cec5SDimitry Andric     Expected<StringRef> FunctionNameOrErr = Function->getName();
10780b57cec5SDimitry Andric     if (!FunctionNameOrErr) {
10790b57cec5SDimitry Andric       std::string Buf;
10800b57cec5SDimitry Andric       llvm::raw_string_ostream OS(Buf);
10810b57cec5SDimitry Andric       logAllUnhandledErrors(FunctionNameOrErr.takeError(), OS);
10820b57cec5SDimitry Andric       OS.flush();
10830b57cec5SDimitry Andric       report_fatal_error(Buf);
10840b57cec5SDimitry Andric     }
10850b57cec5SDimitry Andric     FunctionName = *FunctionNameOrErr;
10860b57cec5SDimitry Andric     Expected<uint64_t> FunctionAddressOrErr = Function->getAddress();
10870b57cec5SDimitry Andric     if (!FunctionAddressOrErr) {
10880b57cec5SDimitry Andric       std::string Buf;
10890b57cec5SDimitry Andric       llvm::raw_string_ostream OS(Buf);
10900b57cec5SDimitry Andric       logAllUnhandledErrors(FunctionAddressOrErr.takeError(), OS);
10910b57cec5SDimitry Andric       OS.flush();
10920b57cec5SDimitry Andric       report_fatal_error(Buf);
10930b57cec5SDimitry Andric     }
10940b57cec5SDimitry Andric     FunctionAddress = *FunctionAddressOrErr;
10950b57cec5SDimitry Andric   } else {
10968bcb0991SDimitry Andric     FunctionAddress = COFF.getPE32Header()->ImageBase + RF.BeginAddress;
10970b57cec5SDimitry Andric   }
10980b57cec5SDimitry Andric 
10990b57cec5SDimitry Andric   SW.printString("Function", formatSymbol(FunctionName, FunctionAddress));
11000b57cec5SDimitry Andric   if (!isAArch64)
11010b57cec5SDimitry Andric     SW.printBoolean("Fragment",
11020b57cec5SDimitry Andric                     RF.Flag() == RuntimeFunctionFlag::RFF_PackedFragment);
11030b57cec5SDimitry Andric   SW.printNumber("FunctionLength", RF.FunctionLength());
11040b57cec5SDimitry Andric   SW.startLine() << "ReturnType: " << RF.Ret() << '\n';
11050b57cec5SDimitry Andric   SW.printBoolean("HomedParameters", RF.H());
11060b57cec5SDimitry Andric   SW.startLine() << "SavedRegisters: ";
11070b57cec5SDimitry Andric                  printRegisters(SavedRegisterMask(RF));
11080b57cec5SDimitry Andric   OS << '\n';
11090b57cec5SDimitry Andric   SW.printNumber("StackAdjustment", StackAdjustment(RF) << 2);
11100b57cec5SDimitry Andric 
11110b57cec5SDimitry Andric   return true;
11120b57cec5SDimitry Andric }
11130b57cec5SDimitry Andric 
1114*e8d8bef9SDimitry Andric bool Decoder::dumpPackedARM64Entry(const object::COFFObjectFile &COFF,
1115*e8d8bef9SDimitry Andric                                    const SectionRef Section, uint64_t Offset,
1116*e8d8bef9SDimitry Andric                                    unsigned Index,
1117*e8d8bef9SDimitry Andric                                    const RuntimeFunctionARM64 &RF) {
1118*e8d8bef9SDimitry Andric   assert((RF.Flag() == RuntimeFunctionFlag::RFF_Packed ||
1119*e8d8bef9SDimitry Andric           RF.Flag() == RuntimeFunctionFlag::RFF_PackedFragment) &&
1120*e8d8bef9SDimitry Andric          "unpacked entry cannot be treated as a packed entry");
1121*e8d8bef9SDimitry Andric 
1122*e8d8bef9SDimitry Andric   ErrorOr<SymbolRef> Function = getRelocatedSymbol(COFF, Section, Offset);
1123*e8d8bef9SDimitry Andric   if (!Function)
1124*e8d8bef9SDimitry Andric     Function = getSymbol(COFF, RF.BeginAddress, /*FunctionOnly=*/true);
1125*e8d8bef9SDimitry Andric 
1126*e8d8bef9SDimitry Andric   StringRef FunctionName;
1127*e8d8bef9SDimitry Andric   uint64_t FunctionAddress;
1128*e8d8bef9SDimitry Andric   if (Function) {
1129*e8d8bef9SDimitry Andric     Expected<StringRef> FunctionNameOrErr = Function->getName();
1130*e8d8bef9SDimitry Andric     if (!FunctionNameOrErr) {
1131*e8d8bef9SDimitry Andric       std::string Buf;
1132*e8d8bef9SDimitry Andric       llvm::raw_string_ostream OS(Buf);
1133*e8d8bef9SDimitry Andric       logAllUnhandledErrors(FunctionNameOrErr.takeError(), OS);
1134*e8d8bef9SDimitry Andric       OS.flush();
1135*e8d8bef9SDimitry Andric       report_fatal_error(Buf);
1136*e8d8bef9SDimitry Andric     }
1137*e8d8bef9SDimitry Andric     FunctionName = *FunctionNameOrErr;
1138*e8d8bef9SDimitry Andric     Expected<uint64_t> FunctionAddressOrErr = Function->getAddress();
1139*e8d8bef9SDimitry Andric     if (!FunctionAddressOrErr) {
1140*e8d8bef9SDimitry Andric       std::string Buf;
1141*e8d8bef9SDimitry Andric       llvm::raw_string_ostream OS(Buf);
1142*e8d8bef9SDimitry Andric       logAllUnhandledErrors(FunctionAddressOrErr.takeError(), OS);
1143*e8d8bef9SDimitry Andric       OS.flush();
1144*e8d8bef9SDimitry Andric       report_fatal_error(Buf);
1145*e8d8bef9SDimitry Andric     }
1146*e8d8bef9SDimitry Andric     FunctionAddress = *FunctionAddressOrErr;
1147*e8d8bef9SDimitry Andric   } else {
1148*e8d8bef9SDimitry Andric     FunctionAddress = COFF.getPE32PlusHeader()->ImageBase + RF.BeginAddress;
1149*e8d8bef9SDimitry Andric   }
1150*e8d8bef9SDimitry Andric 
1151*e8d8bef9SDimitry Andric   SW.printString("Function", formatSymbol(FunctionName, FunctionAddress));
1152*e8d8bef9SDimitry Andric   SW.printBoolean("Fragment",
1153*e8d8bef9SDimitry Andric                   RF.Flag() == RuntimeFunctionFlag::RFF_PackedFragment);
1154*e8d8bef9SDimitry Andric   SW.printNumber("FunctionLength", RF.FunctionLength());
1155*e8d8bef9SDimitry Andric   SW.printNumber("RegF", RF.RegF());
1156*e8d8bef9SDimitry Andric   SW.printNumber("RegI", RF.RegI());
1157*e8d8bef9SDimitry Andric   SW.printBoolean("HomedParameters", RF.H());
1158*e8d8bef9SDimitry Andric   SW.printNumber("CR", RF.CR());
1159*e8d8bef9SDimitry Andric   SW.printNumber("FrameSize", RF.FrameSize() << 4);
1160*e8d8bef9SDimitry Andric   ListScope PS(SW, "Prologue");
1161*e8d8bef9SDimitry Andric 
1162*e8d8bef9SDimitry Andric   // Synthesize the equivalent prologue according to the documentation
1163*e8d8bef9SDimitry Andric   // at https://docs.microsoft.com/en-us/cpp/build/arm64-exception-handling,
1164*e8d8bef9SDimitry Andric   // printed in reverse order compared to the docs, to match how prologues
1165*e8d8bef9SDimitry Andric   // are printed for the non-packed case.
1166*e8d8bef9SDimitry Andric   int IntSZ = 8 * RF.RegI();
1167*e8d8bef9SDimitry Andric   if (RF.CR() == 1)
1168*e8d8bef9SDimitry Andric     IntSZ += 8;
1169*e8d8bef9SDimitry Andric   int FpSZ = 8 * RF.RegF();
1170*e8d8bef9SDimitry Andric   if (RF.RegF())
1171*e8d8bef9SDimitry Andric     FpSZ += 8;
1172*e8d8bef9SDimitry Andric   int SavSZ = (IntSZ + FpSZ + 8 * 8 * RF.H() + 0xf) & ~0xf;
1173*e8d8bef9SDimitry Andric   int LocSZ = (RF.FrameSize() << 4) - SavSZ;
1174*e8d8bef9SDimitry Andric 
1175*e8d8bef9SDimitry Andric   if (RF.CR() == 3) {
1176*e8d8bef9SDimitry Andric     SW.startLine() << "mov x29, sp\n";
1177*e8d8bef9SDimitry Andric     if (LocSZ <= 512) {
1178*e8d8bef9SDimitry Andric       SW.startLine() << format("stp x29, lr, [sp, #-%d]!\n", LocSZ);
1179*e8d8bef9SDimitry Andric     } else {
1180*e8d8bef9SDimitry Andric       SW.startLine() << "stp x29, lr, [sp, #0]\n";
1181*e8d8bef9SDimitry Andric     }
1182*e8d8bef9SDimitry Andric   }
1183*e8d8bef9SDimitry Andric   if (LocSZ > 4080) {
1184*e8d8bef9SDimitry Andric     SW.startLine() << format("sub sp, sp, #%d\n", LocSZ - 4080);
1185*e8d8bef9SDimitry Andric     SW.startLine() << "sub sp, sp, #4080\n";
1186*e8d8bef9SDimitry Andric   } else if ((RF.CR() != 3 && LocSZ > 0) || LocSZ > 512) {
1187*e8d8bef9SDimitry Andric     SW.startLine() << format("sub sp, sp, #%d\n", LocSZ);
1188*e8d8bef9SDimitry Andric   }
1189*e8d8bef9SDimitry Andric   if (RF.H()) {
1190*e8d8bef9SDimitry Andric     SW.startLine() << format("stp x6, x7, [sp, #%d]\n", IntSZ + FpSZ + 48);
1191*e8d8bef9SDimitry Andric     SW.startLine() << format("stp x4, x5, [sp, #%d]\n", IntSZ + FpSZ + 32);
1192*e8d8bef9SDimitry Andric     SW.startLine() << format("stp x2, x3, [sp, #%d]\n", IntSZ + FpSZ + 16);
1193*e8d8bef9SDimitry Andric     if (RF.RegI() > 0 || RF.RegF() > 0 || RF.CR() == 1) {
1194*e8d8bef9SDimitry Andric       SW.startLine() << format("stp x0, x1, [sp, #%d]\n", IntSZ + FpSZ);
1195*e8d8bef9SDimitry Andric     } else {
1196*e8d8bef9SDimitry Andric       // This case isn't documented; if neither RegI nor RegF nor CR=1
1197*e8d8bef9SDimitry Andric       // have decremented the stack pointer by SavSZ, we need to do it here
1198*e8d8bef9SDimitry Andric       // (as the final stack adjustment of LocSZ excludes SavSZ).
1199*e8d8bef9SDimitry Andric       SW.startLine() << format("stp x0, x1, [sp, #-%d]!\n", SavSZ);
1200*e8d8bef9SDimitry Andric     }
1201*e8d8bef9SDimitry Andric   }
1202*e8d8bef9SDimitry Andric   int FloatRegs = RF.RegF() > 0 ? RF.RegF() + 1 : 0;
1203*e8d8bef9SDimitry Andric   for (int I = (FloatRegs + 1) / 2 - 1; I >= 0; I--) {
1204*e8d8bef9SDimitry Andric     if (I == (FloatRegs + 1) / 2 - 1 && FloatRegs % 2 == 1) {
1205*e8d8bef9SDimitry Andric       // The last register, an odd register without a pair
1206*e8d8bef9SDimitry Andric       SW.startLine() << format("str d%d, [sp, #%d]\n", 8 + 2 * I,
1207*e8d8bef9SDimitry Andric                                IntSZ + 16 * I);
1208*e8d8bef9SDimitry Andric     } else if (I == 0 && RF.RegI() == 0 && RF.CR() != 1) {
1209*e8d8bef9SDimitry Andric       SW.startLine() << format("stp d%d, d%d, [sp, #-%d]!\n", 8 + 2 * I,
1210*e8d8bef9SDimitry Andric                                8 + 2 * I + 1, SavSZ);
1211*e8d8bef9SDimitry Andric     } else {
1212*e8d8bef9SDimitry Andric       SW.startLine() << format("stp d%d, d%d, [sp, #%d]\n", 8 + 2 * I,
1213*e8d8bef9SDimitry Andric                                8 + 2 * I + 1, IntSZ + 16 * I);
1214*e8d8bef9SDimitry Andric     }
1215*e8d8bef9SDimitry Andric   }
1216*e8d8bef9SDimitry Andric   if (RF.CR() == 1 && (RF.RegI() % 2) == 0) {
1217*e8d8bef9SDimitry Andric     if (RF.RegI() == 0)
1218*e8d8bef9SDimitry Andric       SW.startLine() << format("str lr, [sp, #-%d]!\n", SavSZ);
1219*e8d8bef9SDimitry Andric     else
1220*e8d8bef9SDimitry Andric       SW.startLine() << format("str lr, [sp, #%d]\n", IntSZ - 8);
1221*e8d8bef9SDimitry Andric   }
1222*e8d8bef9SDimitry Andric   for (int I = (RF.RegI() + 1) / 2 - 1; I >= 0; I--) {
1223*e8d8bef9SDimitry Andric     if (I == (RF.RegI() + 1) / 2 - 1 && RF.RegI() % 2 == 1) {
1224*e8d8bef9SDimitry Andric       // The last register, an odd register without a pair
1225*e8d8bef9SDimitry Andric       if (RF.CR() == 1) {
1226*e8d8bef9SDimitry Andric         if (I == 0) { // If this is the only register pair
1227*e8d8bef9SDimitry Andric           // CR=1 combined with RegI=1 doesn't map to a documented case;
1228*e8d8bef9SDimitry Andric           // it doesn't map to any regular unwind info opcode, and the
1229*e8d8bef9SDimitry Andric           // actual unwinder doesn't support it.
1230*e8d8bef9SDimitry Andric           SW.startLine() << "INVALID!\n";
1231*e8d8bef9SDimitry Andric         } else
1232*e8d8bef9SDimitry Andric           SW.startLine() << format("stp x%d, lr, [sp, #%d]\n", 19 + 2 * I,
1233*e8d8bef9SDimitry Andric                                    16 * I);
1234*e8d8bef9SDimitry Andric       } else {
1235*e8d8bef9SDimitry Andric         if (I == 0)
1236*e8d8bef9SDimitry Andric           SW.startLine() << format("str x%d, [sp, #-%d]!\n", 19 + 2 * I, SavSZ);
1237*e8d8bef9SDimitry Andric         else
1238*e8d8bef9SDimitry Andric           SW.startLine() << format("str x%d, [sp, #%d]\n", 19 + 2 * I, 16 * I);
1239*e8d8bef9SDimitry Andric       }
1240*e8d8bef9SDimitry Andric     } else if (I == 0) {
1241*e8d8bef9SDimitry Andric       // The first register pair
1242*e8d8bef9SDimitry Andric       SW.startLine() << format("stp x19, x20, [sp, #-%d]!\n", SavSZ);
1243*e8d8bef9SDimitry Andric     } else {
1244*e8d8bef9SDimitry Andric       SW.startLine() << format("stp x%d, x%d, [sp, #%d]\n", 19 + 2 * I,
1245*e8d8bef9SDimitry Andric                                19 + 2 * I + 1, 16 * I);
1246*e8d8bef9SDimitry Andric     }
1247*e8d8bef9SDimitry Andric   }
1248*e8d8bef9SDimitry Andric   SW.startLine() << "end\n";
1249*e8d8bef9SDimitry Andric 
1250*e8d8bef9SDimitry Andric   return true;
1251*e8d8bef9SDimitry Andric }
1252*e8d8bef9SDimitry Andric 
12530b57cec5SDimitry Andric bool Decoder::dumpProcedureDataEntry(const COFFObjectFile &COFF,
12540b57cec5SDimitry Andric                                      const SectionRef Section, unsigned Index,
12550b57cec5SDimitry Andric                                      ArrayRef<uint8_t> Contents) {
12560b57cec5SDimitry Andric   uint64_t Offset = PDataEntrySize * Index;
12570b57cec5SDimitry Andric   const ulittle32_t *Data =
12580b57cec5SDimitry Andric     reinterpret_cast<const ulittle32_t *>(Contents.data() + Offset);
12590b57cec5SDimitry Andric 
12600b57cec5SDimitry Andric   const RuntimeFunction Entry(Data);
12610b57cec5SDimitry Andric   DictScope RFS(SW, "RuntimeFunction");
12620b57cec5SDimitry Andric   if (Entry.Flag() == RuntimeFunctionFlag::RFF_Unpacked)
12630b57cec5SDimitry Andric     return dumpUnpackedEntry(COFF, Section, Offset, Index, Entry);
12640b57cec5SDimitry Andric   if (isAArch64) {
1265*e8d8bef9SDimitry Andric     const RuntimeFunctionARM64 EntryARM64(Data);
1266*e8d8bef9SDimitry Andric     return dumpPackedARM64Entry(COFF, Section, Offset, Index, EntryARM64);
12670b57cec5SDimitry Andric   }
12680b57cec5SDimitry Andric   return dumpPackedEntry(COFF, Section, Offset, Index, Entry);
12690b57cec5SDimitry Andric }
12700b57cec5SDimitry Andric 
12710b57cec5SDimitry Andric void Decoder::dumpProcedureData(const COFFObjectFile &COFF,
12720b57cec5SDimitry Andric                                 const SectionRef Section) {
12730b57cec5SDimitry Andric   ArrayRef<uint8_t> Contents;
12740b57cec5SDimitry Andric   if (COFF.getSectionContents(COFF.getCOFFSection(Section), Contents))
12750b57cec5SDimitry Andric     return;
12760b57cec5SDimitry Andric 
12770b57cec5SDimitry Andric   if (Contents.size() % PDataEntrySize) {
12780b57cec5SDimitry Andric     errs() << ".pdata content is not " << PDataEntrySize << "-byte aligned\n";
12790b57cec5SDimitry Andric     return;
12800b57cec5SDimitry Andric   }
12810b57cec5SDimitry Andric 
12820b57cec5SDimitry Andric   for (unsigned EI = 0, EE = Contents.size() / PDataEntrySize; EI < EE; ++EI)
12830b57cec5SDimitry Andric     if (!dumpProcedureDataEntry(COFF, Section, EI, Contents))
12840b57cec5SDimitry Andric       break;
12850b57cec5SDimitry Andric }
12860b57cec5SDimitry Andric 
12870b57cec5SDimitry Andric Error Decoder::dumpProcedureData(const COFFObjectFile &COFF) {
12880b57cec5SDimitry Andric   for (const auto &Section : COFF.sections()) {
12890b57cec5SDimitry Andric     Expected<StringRef> NameOrErr =
12900b57cec5SDimitry Andric         COFF.getSectionName(COFF.getCOFFSection(Section));
12910b57cec5SDimitry Andric     if (!NameOrErr)
12920b57cec5SDimitry Andric       return NameOrErr.takeError();
12930b57cec5SDimitry Andric 
12940b57cec5SDimitry Andric     if (NameOrErr->startswith(".pdata"))
12950b57cec5SDimitry Andric       dumpProcedureData(COFF, Section);
12960b57cec5SDimitry Andric   }
12970b57cec5SDimitry Andric   return Error::success();
12980b57cec5SDimitry Andric }
12990b57cec5SDimitry Andric }
13000b57cec5SDimitry Andric }
13010b57cec5SDimitry Andric }
1302