xref: /freebsd/contrib/llvm-project/llvm/tools/llvm-readobj/ARMWinEHPrinter.cpp (revision 349cc55c9796c4596a5b9904cd3281af295f878f)
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)
103fe6060f1SDimitry Andric     OS << format("+0x%" PRIX64 " (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 },
170e8d8bef9SDimitry Andric   { 0xff, 0xe6, 1, &Decoder::opcode_save_next },
171e8d8bef9SDimitry Andric   { 0xff, 0xe8, 1, &Decoder::opcode_trap_frame },
172e8d8bef9SDimitry Andric   { 0xff, 0xe9, 1, &Decoder::opcode_machine_frame },
173e8d8bef9SDimitry Andric   { 0xff, 0xea, 1, &Decoder::opcode_context },
174e8d8bef9SDimitry 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 << '{';
187fe6060f1SDimitry Andric   ListSeparator LS;
188fe6060f1SDimitry Andric   for (unsigned RI = 0, RE = 11; RI < RE; ++RI)
189fe6060f1SDimitry Andric     if (GPRMask & (1 << RI))
190fe6060f1SDimitry Andric       OS << LS << GPRRegisterNames[RI];
191fe6060f1SDimitry Andric   for (unsigned RI = 0, RE = 32; RI < RE; ++RI)
192fe6060f1SDimitry Andric     if (VFPMask & (1 << RI))
193fe6060f1SDimitry Andric       OS << LS << "d" << unsigned(RI);
194fe6060f1SDimitry Andric   for (unsigned RI = 11, RE = 16; RI < RE; ++RI)
195fe6060f1SDimitry Andric     if (GPRMask & (1 << RI))
196fe6060f1SDimitry Andric       OS << LS << GPRRegisterNames[RI];
1970b57cec5SDimitry Andric   OS << '}';
1980b57cec5SDimitry Andric }
1990b57cec5SDimitry Andric 
2000b57cec5SDimitry Andric ErrorOr<object::SectionRef>
2010b57cec5SDimitry Andric Decoder::getSectionContaining(const COFFObjectFile &COFF, uint64_t VA) {
2020b57cec5SDimitry Andric   for (const auto &Section : COFF.sections()) {
2030b57cec5SDimitry Andric     uint64_t Address = Section.getAddress();
2040b57cec5SDimitry Andric     uint64_t Size = Section.getSize();
2050b57cec5SDimitry Andric 
2060b57cec5SDimitry Andric     if (VA >= Address && (VA - Address) <= Size)
2070b57cec5SDimitry Andric       return Section;
2080b57cec5SDimitry Andric   }
209e8d8bef9SDimitry Andric   return inconvertibleErrorCode();
2100b57cec5SDimitry Andric }
2110b57cec5SDimitry Andric 
2120b57cec5SDimitry Andric ErrorOr<object::SymbolRef> Decoder::getSymbol(const COFFObjectFile &COFF,
2130b57cec5SDimitry Andric                                               uint64_t VA, bool FunctionOnly) {
2140b57cec5SDimitry Andric   for (const auto &Symbol : COFF.symbols()) {
2150b57cec5SDimitry Andric     Expected<SymbolRef::Type> Type = Symbol.getType();
2160b57cec5SDimitry Andric     if (!Type)
2170b57cec5SDimitry Andric       return errorToErrorCode(Type.takeError());
2180b57cec5SDimitry Andric     if (FunctionOnly && *Type != SymbolRef::ST_Function)
2190b57cec5SDimitry Andric       continue;
2200b57cec5SDimitry Andric 
2210b57cec5SDimitry Andric     Expected<uint64_t> Address = Symbol.getAddress();
2220b57cec5SDimitry Andric     if (!Address)
2230b57cec5SDimitry Andric       return errorToErrorCode(Address.takeError());
2240b57cec5SDimitry Andric     if (*Address == VA)
2250b57cec5SDimitry Andric       return Symbol;
2260b57cec5SDimitry Andric   }
227e8d8bef9SDimitry Andric   return inconvertibleErrorCode();
2280b57cec5SDimitry Andric }
2290b57cec5SDimitry Andric 
2300b57cec5SDimitry Andric ErrorOr<SymbolRef> Decoder::getRelocatedSymbol(const COFFObjectFile &,
2310b57cec5SDimitry Andric                                                const SectionRef &Section,
2320b57cec5SDimitry Andric                                                uint64_t Offset) {
2330b57cec5SDimitry Andric   for (const auto &Relocation : Section.relocations()) {
2340b57cec5SDimitry Andric     uint64_t RelocationOffset = Relocation.getOffset();
2350b57cec5SDimitry Andric     if (RelocationOffset == Offset)
2360b57cec5SDimitry Andric       return *Relocation.getSymbol();
2370b57cec5SDimitry Andric   }
238e8d8bef9SDimitry Andric   return inconvertibleErrorCode();
2390b57cec5SDimitry Andric }
2400b57cec5SDimitry Andric 
241*349cc55cSDimitry Andric SymbolRef Decoder::getPreferredSymbol(const COFFObjectFile &COFF, SymbolRef Sym,
242*349cc55cSDimitry Andric                                       uint64_t &SymbolOffset) {
243fe6060f1SDimitry Andric   // The symbol resolved by getRelocatedSymbol can be any internal
244fe6060f1SDimitry Andric   // nondescriptive symbol; try to resolve a more descriptive one.
245fe6060f1SDimitry Andric   COFFSymbolRef CoffSym = COFF.getCOFFSymbol(Sym);
246*349cc55cSDimitry Andric   if (CoffSym.getStorageClass() != COFF::IMAGE_SYM_CLASS_LABEL &&
247*349cc55cSDimitry Andric       CoffSym.getSectionDefinition() == nullptr)
248fe6060f1SDimitry Andric     return Sym;
249fe6060f1SDimitry Andric   for (const auto &S : COFF.symbols()) {
250fe6060f1SDimitry Andric     COFFSymbolRef CS = COFF.getCOFFSymbol(S);
251fe6060f1SDimitry Andric     if (CS.getSectionNumber() == CoffSym.getSectionNumber() &&
252*349cc55cSDimitry Andric         CS.getValue() <= CoffSym.getValue() + SymbolOffset &&
253*349cc55cSDimitry Andric         CS.getStorageClass() != COFF::IMAGE_SYM_CLASS_LABEL &&
254*349cc55cSDimitry Andric         CS.getSectionDefinition() == nullptr) {
255*349cc55cSDimitry Andric       uint32_t Offset = CoffSym.getValue() + SymbolOffset - CS.getValue();
256*349cc55cSDimitry Andric       if (Offset <= SymbolOffset) {
257*349cc55cSDimitry Andric         SymbolOffset = Offset;
258fe6060f1SDimitry Andric         Sym = S;
259fe6060f1SDimitry Andric         CoffSym = CS;
260*349cc55cSDimitry Andric         if (CS.isExternal() && SymbolOffset == 0)
261*349cc55cSDimitry Andric           return Sym;
262fe6060f1SDimitry Andric       }
263fe6060f1SDimitry Andric     }
264fe6060f1SDimitry Andric   }
265fe6060f1SDimitry Andric   return Sym;
266fe6060f1SDimitry Andric }
267fe6060f1SDimitry Andric 
268fe6060f1SDimitry Andric ErrorOr<SymbolRef> Decoder::getSymbolForLocation(
269fe6060f1SDimitry Andric     const COFFObjectFile &COFF, const SectionRef &Section,
270fe6060f1SDimitry Andric     uint64_t OffsetInSection, uint64_t ImmediateOffset, uint64_t &SymbolAddress,
271fe6060f1SDimitry Andric     uint64_t &SymbolOffset, bool FunctionOnly) {
272fe6060f1SDimitry Andric   // Try to locate a relocation that points at the offset in the section
273fe6060f1SDimitry Andric   ErrorOr<SymbolRef> SymOrErr =
274fe6060f1SDimitry Andric       getRelocatedSymbol(COFF, Section, OffsetInSection);
275fe6060f1SDimitry Andric   if (SymOrErr) {
276fe6060f1SDimitry Andric     // We found a relocation symbol; the immediate offset needs to be added
277fe6060f1SDimitry Andric     // to the symbol address.
278fe6060f1SDimitry Andric     SymbolOffset = ImmediateOffset;
279fe6060f1SDimitry Andric 
280fe6060f1SDimitry Andric     Expected<uint64_t> AddressOrErr = SymOrErr->getAddress();
281fe6060f1SDimitry Andric     if (!AddressOrErr) {
282fe6060f1SDimitry Andric       std::string Buf;
283fe6060f1SDimitry Andric       llvm::raw_string_ostream OS(Buf);
284fe6060f1SDimitry Andric       logAllUnhandledErrors(AddressOrErr.takeError(), OS);
285*349cc55cSDimitry Andric       report_fatal_error(Twine(OS.str()));
286fe6060f1SDimitry Andric     }
287fe6060f1SDimitry Andric     // We apply SymbolOffset here directly. We return it separately to allow
288fe6060f1SDimitry Andric     // the caller to print it as an offset on the symbol name.
289fe6060f1SDimitry Andric     SymbolAddress = *AddressOrErr + SymbolOffset;
290*349cc55cSDimitry Andric 
291*349cc55cSDimitry Andric     if (FunctionOnly) // Resolve label/section symbols into function names.
292*349cc55cSDimitry Andric       SymOrErr = getPreferredSymbol(COFF, *SymOrErr, SymbolOffset);
293fe6060f1SDimitry Andric   } else {
294fe6060f1SDimitry Andric     // No matching relocation found; operating on a linked image. Try to
295fe6060f1SDimitry Andric     // find a descriptive symbol if possible. The immediate offset contains
296fe6060f1SDimitry Andric     // the image relative address, and we shouldn't add any offset to the
297fe6060f1SDimitry Andric     // symbol.
298fe6060f1SDimitry Andric     SymbolAddress = COFF.getImageBase() + ImmediateOffset;
299fe6060f1SDimitry Andric     SymbolOffset = 0;
300fe6060f1SDimitry Andric     SymOrErr = getSymbol(COFF, SymbolAddress, FunctionOnly);
301fe6060f1SDimitry Andric   }
302fe6060f1SDimitry Andric   return SymOrErr;
303fe6060f1SDimitry Andric }
304fe6060f1SDimitry Andric 
3050b57cec5SDimitry Andric bool Decoder::opcode_0xxxxxxx(const uint8_t *OC, unsigned &Offset,
3060b57cec5SDimitry Andric                               unsigned Length, bool Prologue) {
3070b57cec5SDimitry Andric   uint8_t Imm = OC[Offset] & 0x7f;
3080b57cec5SDimitry Andric   SW.startLine() << format("0x%02x                ; %s sp, #(%u * 4)\n",
3090b57cec5SDimitry Andric                            OC[Offset],
3100b57cec5SDimitry Andric                            static_cast<const char *>(Prologue ? "sub" : "add"),
3110b57cec5SDimitry Andric                            Imm);
3120b57cec5SDimitry Andric   ++Offset;
3130b57cec5SDimitry Andric   return false;
3140b57cec5SDimitry Andric }
3150b57cec5SDimitry Andric 
3160b57cec5SDimitry Andric bool Decoder::opcode_10Lxxxxx(const uint8_t *OC, unsigned &Offset,
3170b57cec5SDimitry Andric                               unsigned Length, bool Prologue) {
3180b57cec5SDimitry Andric   unsigned Link = (OC[Offset] & 0x20) >> 5;
3190b57cec5SDimitry Andric   uint16_t RegisterMask = (Link << (Prologue ? 14 : 15))
3200b57cec5SDimitry Andric                         | ((OC[Offset + 0] & 0x1f) << 8)
3210b57cec5SDimitry Andric                         | ((OC[Offset + 1] & 0xff) << 0);
3220b57cec5SDimitry Andric   assert((~RegisterMask & (1 << 13)) && "sp must not be set");
3230b57cec5SDimitry Andric   assert((~RegisterMask & (1 << (Prologue ? 15 : 14))) && "pc must not be set");
3240b57cec5SDimitry Andric 
3250b57cec5SDimitry Andric   SW.startLine() << format("0x%02x 0x%02x           ; %s.w ",
3260b57cec5SDimitry Andric                            OC[Offset + 0], OC[Offset + 1],
3270b57cec5SDimitry Andric                            Prologue ? "push" : "pop");
3280b57cec5SDimitry Andric   printRegisters(std::make_pair(RegisterMask, 0));
3290b57cec5SDimitry Andric   OS << '\n';
3300b57cec5SDimitry Andric 
3310b57cec5SDimitry Andric   Offset += 2;
3320b57cec5SDimitry Andric   return false;
3330b57cec5SDimitry Andric }
3340b57cec5SDimitry Andric 
3350b57cec5SDimitry Andric bool Decoder::opcode_1100xxxx(const uint8_t *OC, unsigned &Offset,
3360b57cec5SDimitry Andric                               unsigned Length, bool Prologue) {
3370b57cec5SDimitry Andric   if (Prologue)
3380b57cec5SDimitry Andric     SW.startLine() << format("0x%02x                ; mov r%u, sp\n",
3390b57cec5SDimitry Andric                              OC[Offset], OC[Offset] & 0xf);
3400b57cec5SDimitry Andric   else
3410b57cec5SDimitry Andric     SW.startLine() << format("0x%02x                ; mov sp, r%u\n",
3420b57cec5SDimitry Andric                              OC[Offset], OC[Offset] & 0xf);
3430b57cec5SDimitry Andric   ++Offset;
3440b57cec5SDimitry Andric   return false;
3450b57cec5SDimitry Andric }
3460b57cec5SDimitry Andric 
3470b57cec5SDimitry Andric bool Decoder::opcode_11010Lxx(const uint8_t *OC, unsigned &Offset,
3480b57cec5SDimitry Andric                               unsigned Length, bool Prologue) {
3490b57cec5SDimitry Andric   unsigned Link = (OC[Offset] & 0x4) >> 3;
3500b57cec5SDimitry Andric   unsigned Count = (OC[Offset] & 0x3);
3510b57cec5SDimitry Andric 
3520b57cec5SDimitry Andric   uint16_t GPRMask = (Link << (Prologue ? 14 : 15))
3530b57cec5SDimitry Andric                    | (((1 << (Count + 1)) - 1) << 4);
3540b57cec5SDimitry Andric 
3550b57cec5SDimitry Andric   SW.startLine() << format("0x%02x                ; %s ", OC[Offset],
3560b57cec5SDimitry Andric                            Prologue ? "push" : "pop");
3570b57cec5SDimitry Andric   printRegisters(std::make_pair(GPRMask, 0));
3580b57cec5SDimitry Andric   OS << '\n';
3590b57cec5SDimitry Andric 
3600b57cec5SDimitry Andric   ++Offset;
3610b57cec5SDimitry Andric   return false;
3620b57cec5SDimitry Andric }
3630b57cec5SDimitry Andric 
3640b57cec5SDimitry Andric bool Decoder::opcode_11011Lxx(const uint8_t *OC, unsigned &Offset,
3650b57cec5SDimitry Andric                               unsigned Length, bool Prologue) {
3660b57cec5SDimitry Andric   unsigned Link = (OC[Offset] & 0x4) >> 2;
3670b57cec5SDimitry Andric   unsigned Count = (OC[Offset] & 0x3) + 4;
3680b57cec5SDimitry Andric 
3690b57cec5SDimitry Andric   uint16_t GPRMask = (Link << (Prologue ? 14 : 15))
3700b57cec5SDimitry Andric                    | (((1 << (Count + 1)) - 1) << 4);
3710b57cec5SDimitry Andric 
3720b57cec5SDimitry Andric   SW.startLine() << format("0x%02x                ; %s.w ", OC[Offset],
3730b57cec5SDimitry Andric                            Prologue ? "push" : "pop");
3740b57cec5SDimitry Andric   printRegisters(std::make_pair(GPRMask, 0));
3750b57cec5SDimitry Andric   OS << '\n';
3760b57cec5SDimitry Andric 
3770b57cec5SDimitry Andric   ++Offset;
3780b57cec5SDimitry Andric   return false;
3790b57cec5SDimitry Andric }
3800b57cec5SDimitry Andric 
3810b57cec5SDimitry Andric bool Decoder::opcode_11100xxx(const uint8_t *OC, unsigned &Offset,
3820b57cec5SDimitry Andric                               unsigned Length, bool Prologue) {
3830b57cec5SDimitry Andric   unsigned High = (OC[Offset] & 0x7);
3840b57cec5SDimitry Andric   uint32_t VFPMask = (((1 << (High + 1)) - 1) << 8);
3850b57cec5SDimitry Andric 
3860b57cec5SDimitry Andric   SW.startLine() << format("0x%02x                ; %s ", OC[Offset],
3870b57cec5SDimitry Andric                            Prologue ? "vpush" : "vpop");
3880b57cec5SDimitry Andric   printRegisters(std::make_pair(0, VFPMask));
3890b57cec5SDimitry Andric   OS << '\n';
3900b57cec5SDimitry Andric 
3910b57cec5SDimitry Andric   ++Offset;
3920b57cec5SDimitry Andric   return false;
3930b57cec5SDimitry Andric }
3940b57cec5SDimitry Andric 
3950b57cec5SDimitry Andric bool Decoder::opcode_111010xx(const uint8_t *OC, unsigned &Offset,
3960b57cec5SDimitry Andric                               unsigned Length, bool Prologue) {
3970b57cec5SDimitry Andric   uint16_t Imm = ((OC[Offset + 0] & 0x03) << 8) | ((OC[Offset + 1] & 0xff) << 0);
3980b57cec5SDimitry Andric 
3990b57cec5SDimitry Andric   SW.startLine() << format("0x%02x 0x%02x           ; %s.w sp, #(%u * 4)\n",
4000b57cec5SDimitry Andric                            OC[Offset + 0], OC[Offset + 1],
4010b57cec5SDimitry Andric                            static_cast<const char *>(Prologue ? "sub" : "add"),
4020b57cec5SDimitry Andric                            Imm);
4030b57cec5SDimitry Andric 
4040b57cec5SDimitry Andric   Offset += 2;
4050b57cec5SDimitry Andric   return false;
4060b57cec5SDimitry Andric }
4070b57cec5SDimitry Andric 
4080b57cec5SDimitry Andric bool Decoder::opcode_1110110L(const uint8_t *OC, unsigned &Offset,
4090b57cec5SDimitry Andric                               unsigned Length, bool Prologue) {
4100b57cec5SDimitry Andric   uint8_t GPRMask = ((OC[Offset + 0] & 0x01) << (Prologue ? 14 : 15))
4110b57cec5SDimitry Andric                   | ((OC[Offset + 1] & 0xff) << 0);
4120b57cec5SDimitry Andric 
4130b57cec5SDimitry Andric   SW.startLine() << format("0x%02x 0x%02x           ; %s ", OC[Offset + 0],
4140b57cec5SDimitry Andric                            OC[Offset + 1], Prologue ? "push" : "pop");
4150b57cec5SDimitry Andric   printRegisters(std::make_pair(GPRMask, 0));
4160b57cec5SDimitry Andric   OS << '\n';
4170b57cec5SDimitry Andric 
4180b57cec5SDimitry Andric   Offset += 2;
4190b57cec5SDimitry Andric   return false;
4200b57cec5SDimitry Andric }
4210b57cec5SDimitry Andric 
4220b57cec5SDimitry Andric bool Decoder::opcode_11101110(const uint8_t *OC, unsigned &Offset,
4230b57cec5SDimitry Andric                               unsigned Length, bool Prologue) {
4240b57cec5SDimitry Andric   assert(!Prologue && "may not be used in prologue");
4250b57cec5SDimitry Andric 
4260b57cec5SDimitry Andric   if (OC[Offset + 1] & 0xf0)
4270b57cec5SDimitry Andric     SW.startLine() << format("0x%02x 0x%02x           ; reserved\n",
4280b57cec5SDimitry Andric                              OC[Offset + 0], OC[Offset +  1]);
4290b57cec5SDimitry Andric   else
4300b57cec5SDimitry Andric     SW.startLine()
4310b57cec5SDimitry Andric       << format("0x%02x 0x%02x           ; microsoft-specific (type: %u)\n",
4320b57cec5SDimitry Andric                 OC[Offset + 0], OC[Offset + 1], OC[Offset + 1] & 0x0f);
4330b57cec5SDimitry Andric 
4340b57cec5SDimitry Andric   Offset += 2;
4350b57cec5SDimitry Andric   return false;
4360b57cec5SDimitry Andric }
4370b57cec5SDimitry Andric 
4380b57cec5SDimitry Andric bool Decoder::opcode_11101111(const uint8_t *OC, unsigned &Offset,
4390b57cec5SDimitry Andric                               unsigned Length, bool Prologue) {
4400b57cec5SDimitry Andric   assert(!Prologue && "may not be used in prologue");
4410b57cec5SDimitry Andric 
4420b57cec5SDimitry Andric   if (OC[Offset + 1] & 0xf0)
4430b57cec5SDimitry Andric     SW.startLine() << format("0x%02x 0x%02x           ; reserved\n",
4440b57cec5SDimitry Andric                              OC[Offset + 0], OC[Offset +  1]);
4450b57cec5SDimitry Andric   else
4460b57cec5SDimitry Andric     SW.startLine()
4470b57cec5SDimitry Andric       << format("0x%02x 0x%02x           ; ldr.w lr, [sp], #%u\n",
4480b57cec5SDimitry Andric                 OC[Offset + 0], OC[Offset + 1], OC[Offset + 1] << 2);
4490b57cec5SDimitry Andric 
4500b57cec5SDimitry Andric   Offset += 2;
4510b57cec5SDimitry Andric   return false;
4520b57cec5SDimitry Andric }
4530b57cec5SDimitry Andric 
4540b57cec5SDimitry Andric bool Decoder::opcode_11110101(const uint8_t *OC, unsigned &Offset,
4550b57cec5SDimitry Andric                               unsigned Length, bool Prologue) {
4560b57cec5SDimitry Andric   unsigned Start = (OC[Offset + 1] & 0xf0) >> 4;
4570b57cec5SDimitry Andric   unsigned End = (OC[Offset + 1] & 0x0f) >> 0;
4580b57cec5SDimitry Andric   uint32_t VFPMask = ((1 << (End - Start)) - 1) << Start;
4590b57cec5SDimitry Andric 
4600b57cec5SDimitry Andric   SW.startLine() << format("0x%02x 0x%02x           ; %s ", OC[Offset + 0],
4610b57cec5SDimitry Andric                            OC[Offset + 1], Prologue ? "vpush" : "vpop");
4620b57cec5SDimitry Andric   printRegisters(std::make_pair(0, VFPMask));
4630b57cec5SDimitry Andric   OS << '\n';
4640b57cec5SDimitry Andric 
4650b57cec5SDimitry Andric   Offset += 2;
4660b57cec5SDimitry Andric   return false;
4670b57cec5SDimitry Andric }
4680b57cec5SDimitry Andric 
4690b57cec5SDimitry Andric bool Decoder::opcode_11110110(const uint8_t *OC, unsigned &Offset,
4700b57cec5SDimitry Andric                               unsigned Length, bool Prologue) {
4710b57cec5SDimitry Andric   unsigned Start = (OC[Offset + 1] & 0xf0) >> 4;
4720b57cec5SDimitry Andric   unsigned End = (OC[Offset + 1] & 0x0f) >> 0;
4730b57cec5SDimitry Andric   uint32_t VFPMask = ((1 << (End - Start)) - 1) << 16;
4740b57cec5SDimitry Andric 
4750b57cec5SDimitry Andric   SW.startLine() << format("0x%02x 0x%02x           ; %s ", OC[Offset + 0],
4760b57cec5SDimitry Andric                            OC[Offset + 1], Prologue ? "vpush" : "vpop");
4770b57cec5SDimitry Andric   printRegisters(std::make_pair(0, VFPMask));
4780b57cec5SDimitry Andric   OS << '\n';
4790b57cec5SDimitry Andric 
4800b57cec5SDimitry Andric   Offset += 2;
4810b57cec5SDimitry Andric   return false;
4820b57cec5SDimitry Andric }
4830b57cec5SDimitry Andric 
4840b57cec5SDimitry Andric bool Decoder::opcode_11110111(const uint8_t *OC, unsigned &Offset,
4850b57cec5SDimitry Andric                               unsigned Length, bool Prologue) {
4860b57cec5SDimitry Andric   uint32_t Imm = (OC[Offset + 1] << 8) | (OC[Offset + 2] << 0);
4870b57cec5SDimitry Andric 
4880b57cec5SDimitry Andric   SW.startLine() << format("0x%02x 0x%02x 0x%02x      ; %s sp, sp, #(%u * 4)\n",
4890b57cec5SDimitry Andric                            OC[Offset + 0], OC[Offset + 1], OC[Offset + 2],
4900b57cec5SDimitry Andric                            static_cast<const char *>(Prologue ? "sub" : "add"),
4910b57cec5SDimitry Andric                            Imm);
4920b57cec5SDimitry Andric 
4930b57cec5SDimitry Andric   Offset += 3;
4940b57cec5SDimitry Andric   return false;
4950b57cec5SDimitry Andric }
4960b57cec5SDimitry Andric 
4970b57cec5SDimitry Andric bool Decoder::opcode_11111000(const uint8_t *OC, unsigned &Offset,
4980b57cec5SDimitry Andric                               unsigned Length, bool Prologue) {
4990b57cec5SDimitry Andric   uint32_t Imm = (OC[Offset + 1] << 16)
5000b57cec5SDimitry Andric                | (OC[Offset + 2] << 8)
5010b57cec5SDimitry Andric                | (OC[Offset + 3] << 0);
5020b57cec5SDimitry Andric 
5030b57cec5SDimitry Andric   SW.startLine()
5040b57cec5SDimitry Andric     << format("0x%02x 0x%02x 0x%02x 0x%02x ; %s sp, sp, #(%u * 4)\n",
5050b57cec5SDimitry Andric               OC[Offset + 0], OC[Offset + 1], OC[Offset + 2], OC[Offset + 3],
5060b57cec5SDimitry Andric               static_cast<const char *>(Prologue ? "sub" : "add"), Imm);
5070b57cec5SDimitry Andric 
5080b57cec5SDimitry Andric   Offset += 4;
5090b57cec5SDimitry Andric   return false;
5100b57cec5SDimitry Andric }
5110b57cec5SDimitry Andric 
5120b57cec5SDimitry Andric bool Decoder::opcode_11111001(const uint8_t *OC, unsigned &Offset,
5130b57cec5SDimitry Andric                               unsigned Length, bool Prologue) {
5140b57cec5SDimitry Andric   uint32_t Imm = (OC[Offset + 1] << 8) | (OC[Offset + 2] << 0);
5150b57cec5SDimitry Andric 
5160b57cec5SDimitry Andric   SW.startLine()
5170b57cec5SDimitry Andric     << format("0x%02x 0x%02x 0x%02x      ; %s.w sp, sp, #(%u * 4)\n",
5180b57cec5SDimitry Andric               OC[Offset + 0], OC[Offset + 1], OC[Offset + 2],
5190b57cec5SDimitry Andric               static_cast<const char *>(Prologue ? "sub" : "add"), Imm);
5200b57cec5SDimitry Andric 
5210b57cec5SDimitry Andric   Offset += 3;
5220b57cec5SDimitry Andric   return false;
5230b57cec5SDimitry Andric }
5240b57cec5SDimitry Andric 
5250b57cec5SDimitry Andric bool Decoder::opcode_11111010(const uint8_t *OC, unsigned &Offset,
5260b57cec5SDimitry Andric                               unsigned Length, bool Prologue) {
5270b57cec5SDimitry Andric   uint32_t Imm = (OC[Offset + 1] << 16)
5280b57cec5SDimitry Andric                | (OC[Offset + 2] << 8)
5290b57cec5SDimitry Andric                | (OC[Offset + 3] << 0);
5300b57cec5SDimitry Andric 
5310b57cec5SDimitry Andric   SW.startLine()
5320b57cec5SDimitry Andric     << format("0x%02x 0x%02x 0x%02x 0x%02x ; %s.w sp, sp, #(%u * 4)\n",
5330b57cec5SDimitry Andric               OC[Offset + 0], OC[Offset + 1], OC[Offset + 2], OC[Offset + 3],
5340b57cec5SDimitry Andric               static_cast<const char *>(Prologue ? "sub" : "add"), Imm);
5350b57cec5SDimitry Andric 
5360b57cec5SDimitry Andric   Offset += 4;
5370b57cec5SDimitry Andric   return false;
5380b57cec5SDimitry Andric }
5390b57cec5SDimitry Andric 
5400b57cec5SDimitry Andric bool Decoder::opcode_11111011(const uint8_t *OC, unsigned &Offset,
5410b57cec5SDimitry Andric                               unsigned Length, bool Prologue) {
5420b57cec5SDimitry Andric   SW.startLine() << format("0x%02x                ; nop\n", OC[Offset]);
5430b57cec5SDimitry Andric   ++Offset;
5440b57cec5SDimitry Andric   return false;
5450b57cec5SDimitry Andric }
5460b57cec5SDimitry Andric 
5470b57cec5SDimitry Andric bool Decoder::opcode_11111100(const uint8_t *OC, unsigned &Offset,
5480b57cec5SDimitry Andric                               unsigned Length, bool Prologue) {
5490b57cec5SDimitry Andric   SW.startLine() << format("0x%02x                ; nop.w\n", OC[Offset]);
5500b57cec5SDimitry Andric   ++Offset;
5510b57cec5SDimitry Andric   return false;
5520b57cec5SDimitry Andric }
5530b57cec5SDimitry Andric 
5540b57cec5SDimitry Andric bool Decoder::opcode_11111101(const uint8_t *OC, unsigned &Offset,
5550b57cec5SDimitry Andric                               unsigned Length, bool Prologue) {
5560b57cec5SDimitry Andric   SW.startLine() << format("0x%02x                ; b\n", OC[Offset]);
5570b57cec5SDimitry Andric   ++Offset;
5580b57cec5SDimitry Andric   return true;
5590b57cec5SDimitry Andric }
5600b57cec5SDimitry Andric 
5610b57cec5SDimitry Andric bool Decoder::opcode_11111110(const uint8_t *OC, unsigned &Offset,
5620b57cec5SDimitry Andric                               unsigned Length, bool Prologue) {
5630b57cec5SDimitry Andric   SW.startLine() << format("0x%02x                ; b.w\n", OC[Offset]);
5640b57cec5SDimitry Andric   ++Offset;
5650b57cec5SDimitry Andric   return true;
5660b57cec5SDimitry Andric }
5670b57cec5SDimitry Andric 
5680b57cec5SDimitry Andric bool Decoder::opcode_11111111(const uint8_t *OC, unsigned &Offset,
5690b57cec5SDimitry Andric                               unsigned Length, bool Prologue) {
5700b57cec5SDimitry Andric   ++Offset;
5710b57cec5SDimitry Andric   return true;
5720b57cec5SDimitry Andric }
5730b57cec5SDimitry Andric 
5740b57cec5SDimitry Andric // ARM64 unwind codes start here.
5750b57cec5SDimitry Andric bool Decoder::opcode_alloc_s(const uint8_t *OC, unsigned &Offset,
5760b57cec5SDimitry Andric                              unsigned Length, bool Prologue) {
5770b57cec5SDimitry Andric   uint32_t NumBytes = (OC[Offset] & 0x1F) << 4;
5780b57cec5SDimitry Andric   SW.startLine() << format("0x%02x                ; %s sp, #%u\n", OC[Offset],
5790b57cec5SDimitry Andric                            static_cast<const char *>(Prologue ? "sub" : "add"),
5800b57cec5SDimitry Andric                            NumBytes);
5810b57cec5SDimitry Andric   ++Offset;
5820b57cec5SDimitry Andric   return false;
5830b57cec5SDimitry Andric }
5840b57cec5SDimitry Andric 
5850b57cec5SDimitry Andric bool Decoder::opcode_save_r19r20_x(const uint8_t *OC, unsigned &Offset,
5860b57cec5SDimitry Andric                                    unsigned Length, bool Prologue) {
5870b57cec5SDimitry Andric   uint32_t Off = (OC[Offset] & 0x1F) << 3;
5880b57cec5SDimitry Andric   if (Prologue)
5890b57cec5SDimitry Andric     SW.startLine() << format(
5900b57cec5SDimitry Andric         "0x%02x                ; stp x19, x20, [sp, #-%u]!\n", OC[Offset], Off);
5910b57cec5SDimitry Andric   else
5920b57cec5SDimitry Andric     SW.startLine() << format(
5930b57cec5SDimitry Andric         "0x%02x                ; ldp x19, x20, [sp], #%u\n", OC[Offset], Off);
5940b57cec5SDimitry Andric   ++Offset;
5950b57cec5SDimitry Andric   return false;
5960b57cec5SDimitry Andric }
5970b57cec5SDimitry Andric 
5980b57cec5SDimitry Andric bool Decoder::opcode_save_fplr(const uint8_t *OC, unsigned &Offset,
5990b57cec5SDimitry Andric                                unsigned Length, bool Prologue) {
6000b57cec5SDimitry Andric   uint32_t Off = (OC[Offset] & 0x3F) << 3;
6010b57cec5SDimitry Andric   SW.startLine() << format(
6020b57cec5SDimitry Andric       "0x%02x                ; %s x29, x30, [sp, #%u]\n", OC[Offset],
6030b57cec5SDimitry Andric       static_cast<const char *>(Prologue ? "stp" : "ldp"), Off);
6040b57cec5SDimitry Andric   ++Offset;
6050b57cec5SDimitry Andric   return false;
6060b57cec5SDimitry Andric }
6070b57cec5SDimitry Andric 
6080b57cec5SDimitry Andric bool Decoder::opcode_save_fplr_x(const uint8_t *OC, unsigned &Offset,
6090b57cec5SDimitry Andric                                  unsigned Length, bool Prologue) {
6100b57cec5SDimitry Andric   uint32_t Off = ((OC[Offset] & 0x3F) + 1) << 3;
6110b57cec5SDimitry Andric   if (Prologue)
6120b57cec5SDimitry Andric     SW.startLine() << format(
6130b57cec5SDimitry Andric         "0x%02x                ; stp x29, x30, [sp, #-%u]!\n", OC[Offset], Off);
6140b57cec5SDimitry Andric   else
6150b57cec5SDimitry Andric     SW.startLine() << format(
6160b57cec5SDimitry Andric         "0x%02x                ; ldp x29, x30, [sp], #%u\n", OC[Offset], Off);
6170b57cec5SDimitry Andric   ++Offset;
6180b57cec5SDimitry Andric   return false;
6190b57cec5SDimitry Andric }
6200b57cec5SDimitry Andric 
6210b57cec5SDimitry Andric bool Decoder::opcode_alloc_m(const uint8_t *OC, unsigned &Offset,
6220b57cec5SDimitry Andric                              unsigned Length, bool Prologue) {
6230b57cec5SDimitry Andric   uint32_t NumBytes = ((OC[Offset] & 0x07) << 8);
6240b57cec5SDimitry Andric   NumBytes |= (OC[Offset + 1] & 0xFF);
6250b57cec5SDimitry Andric   NumBytes <<= 4;
6260b57cec5SDimitry Andric   SW.startLine() << format("0x%02x%02x              ; %s sp, #%u\n",
6270b57cec5SDimitry Andric                            OC[Offset], OC[Offset + 1],
6280b57cec5SDimitry Andric                            static_cast<const char *>(Prologue ? "sub" : "add"),
6290b57cec5SDimitry Andric                            NumBytes);
6300b57cec5SDimitry Andric   Offset += 2;
6310b57cec5SDimitry Andric   return false;
6320b57cec5SDimitry Andric }
6330b57cec5SDimitry Andric 
6340b57cec5SDimitry Andric bool Decoder::opcode_save_regp(const uint8_t *OC, unsigned &Offset,
6350b57cec5SDimitry Andric                                unsigned Length, bool Prologue) {
6360b57cec5SDimitry Andric   uint32_t Reg = ((OC[Offset] & 0x03) << 8);
6370b57cec5SDimitry Andric   Reg |= (OC[Offset + 1] & 0xC0);
6380b57cec5SDimitry Andric   Reg >>= 6;
6390b57cec5SDimitry Andric   Reg += 19;
6400b57cec5SDimitry Andric   uint32_t Off = (OC[Offset + 1] & 0x3F) << 3;
6410b57cec5SDimitry Andric   SW.startLine() << format(
6420b57cec5SDimitry Andric       "0x%02x%02x              ; %s x%u, x%u, [sp, #%u]\n",
6430b57cec5SDimitry Andric       OC[Offset], OC[Offset + 1],
6440b57cec5SDimitry Andric       static_cast<const char *>(Prologue ? "stp" : "ldp"), Reg, Reg + 1, Off);
6450b57cec5SDimitry Andric   Offset += 2;
6460b57cec5SDimitry Andric   return false;
6470b57cec5SDimitry Andric }
6480b57cec5SDimitry Andric 
6490b57cec5SDimitry Andric bool Decoder::opcode_save_regp_x(const uint8_t *OC, unsigned &Offset,
6500b57cec5SDimitry Andric                                  unsigned Length, bool Prologue) {
6510b57cec5SDimitry Andric   uint32_t Reg = ((OC[Offset] & 0x03) << 8);
6520b57cec5SDimitry Andric   Reg |= (OC[Offset + 1] & 0xC0);
6530b57cec5SDimitry Andric   Reg >>= 6;
6540b57cec5SDimitry Andric   Reg += 19;
6550b57cec5SDimitry Andric   uint32_t Off = ((OC[Offset + 1] & 0x3F) + 1) << 3;
6560b57cec5SDimitry Andric   if (Prologue)
6570b57cec5SDimitry Andric     SW.startLine() << format(
6580b57cec5SDimitry Andric         "0x%02x%02x              ; stp x%u, x%u, [sp, #-%u]!\n",
6590b57cec5SDimitry Andric         OC[Offset], OC[Offset + 1], Reg,
6600b57cec5SDimitry Andric         Reg + 1, Off);
6610b57cec5SDimitry Andric   else
6620b57cec5SDimitry Andric     SW.startLine() << format(
6630b57cec5SDimitry Andric         "0x%02x%02x              ; ldp x%u, x%u, [sp], #%u\n",
6640b57cec5SDimitry Andric         OC[Offset], OC[Offset + 1], Reg,
6650b57cec5SDimitry Andric         Reg + 1, Off);
6660b57cec5SDimitry Andric   Offset += 2;
6670b57cec5SDimitry Andric   return false;
6680b57cec5SDimitry Andric }
6690b57cec5SDimitry Andric 
6700b57cec5SDimitry Andric bool Decoder::opcode_save_reg(const uint8_t *OC, unsigned &Offset,
6710b57cec5SDimitry Andric                               unsigned Length, bool Prologue) {
6720b57cec5SDimitry Andric   uint32_t Reg = (OC[Offset] & 0x03) << 8;
6730b57cec5SDimitry Andric   Reg |= (OC[Offset + 1] & 0xC0);
6740b57cec5SDimitry Andric   Reg >>= 6;
6750b57cec5SDimitry Andric   Reg += 19;
6760b57cec5SDimitry Andric   uint32_t Off = (OC[Offset + 1] & 0x3F) << 3;
6770b57cec5SDimitry Andric   SW.startLine() << format("0x%02x%02x              ; %s x%u, [sp, #%u]\n",
6780b57cec5SDimitry Andric                            OC[Offset], OC[Offset + 1],
6790b57cec5SDimitry Andric                            static_cast<const char *>(Prologue ? "str" : "ldr"),
6800b57cec5SDimitry Andric                            Reg, Off);
6810b57cec5SDimitry Andric   Offset += 2;
6820b57cec5SDimitry Andric   return false;
6830b57cec5SDimitry Andric }
6840b57cec5SDimitry Andric 
6850b57cec5SDimitry Andric bool Decoder::opcode_save_reg_x(const uint8_t *OC, unsigned &Offset,
6860b57cec5SDimitry Andric                                 unsigned Length, bool Prologue) {
6870b57cec5SDimitry Andric   uint32_t Reg = (OC[Offset] & 0x01) << 8;
6880b57cec5SDimitry Andric   Reg |= (OC[Offset + 1] & 0xE0);
6890b57cec5SDimitry Andric   Reg >>= 5;
6900b57cec5SDimitry Andric   Reg += 19;
6910b57cec5SDimitry Andric   uint32_t Off = ((OC[Offset + 1] & 0x1F) + 1) << 3;
6920b57cec5SDimitry Andric   if (Prologue)
693e8d8bef9SDimitry Andric     SW.startLine() << format("0x%02x%02x              ; str x%u, [sp, #-%u]!\n",
6940b57cec5SDimitry Andric                              OC[Offset], OC[Offset + 1], Reg, Off);
6950b57cec5SDimitry Andric   else
6960b57cec5SDimitry Andric     SW.startLine() << format("0x%02x%02x              ; ldr x%u, [sp], #%u\n",
6970b57cec5SDimitry Andric                              OC[Offset], OC[Offset + 1], Reg, Off);
6980b57cec5SDimitry Andric   Offset += 2;
6990b57cec5SDimitry Andric   return false;
7000b57cec5SDimitry Andric }
7010b57cec5SDimitry Andric 
7020b57cec5SDimitry Andric bool Decoder::opcode_save_lrpair(const uint8_t *OC, unsigned &Offset,
7030b57cec5SDimitry Andric                                  unsigned Length, bool Prologue) {
7040b57cec5SDimitry Andric   uint32_t Reg = (OC[Offset] & 0x01) << 8;
7050b57cec5SDimitry Andric   Reg |= (OC[Offset + 1] & 0xC0);
7060b57cec5SDimitry Andric   Reg >>= 6;
7070b57cec5SDimitry Andric   Reg *= 2;
7080b57cec5SDimitry Andric   Reg += 19;
7090b57cec5SDimitry Andric   uint32_t Off = (OC[Offset + 1] & 0x3F) << 3;
7100b57cec5SDimitry Andric   SW.startLine() << format("0x%02x%02x              ; %s x%u, lr, [sp, #%u]\n",
7110b57cec5SDimitry Andric                            OC[Offset], OC[Offset + 1],
7120b57cec5SDimitry Andric                            static_cast<const char *>(Prologue ? "stp" : "ldp"),
7130b57cec5SDimitry Andric                            Reg, Off);
7140b57cec5SDimitry Andric   Offset += 2;
7150b57cec5SDimitry Andric   return false;
7160b57cec5SDimitry Andric }
7170b57cec5SDimitry Andric 
7180b57cec5SDimitry Andric bool Decoder::opcode_save_fregp(const uint8_t *OC, unsigned &Offset,
7190b57cec5SDimitry Andric                                 unsigned Length, bool Prologue) {
7200b57cec5SDimitry Andric   uint32_t Reg = (OC[Offset] & 0x01) << 8;
7210b57cec5SDimitry Andric   Reg |= (OC[Offset + 1] & 0xC0);
7220b57cec5SDimitry Andric   Reg >>= 6;
7230b57cec5SDimitry Andric   Reg += 8;
7240b57cec5SDimitry Andric   uint32_t Off = (OC[Offset + 1] & 0x3F) << 3;
7250b57cec5SDimitry Andric   SW.startLine() << format("0x%02x%02x              ; %s d%u, d%u, [sp, #%u]\n",
7260b57cec5SDimitry Andric                            OC[Offset], OC[Offset + 1],
7270b57cec5SDimitry Andric                            static_cast<const char *>(Prologue ? "stp" : "ldp"),
7280b57cec5SDimitry Andric                            Reg, Reg + 1, Off);
7290b57cec5SDimitry Andric   Offset += 2;
7300b57cec5SDimitry Andric   return false;
7310b57cec5SDimitry Andric }
7320b57cec5SDimitry Andric 
7330b57cec5SDimitry Andric bool Decoder::opcode_save_fregp_x(const uint8_t *OC, unsigned &Offset,
7340b57cec5SDimitry Andric                                   unsigned Length, bool Prologue) {
7350b57cec5SDimitry Andric   uint32_t Reg = (OC[Offset] & 0x01) << 8;
7360b57cec5SDimitry Andric   Reg |= (OC[Offset + 1] & 0xC0);
7370b57cec5SDimitry Andric   Reg >>= 6;
7380b57cec5SDimitry Andric   Reg += 8;
7390b57cec5SDimitry Andric   uint32_t Off = ((OC[Offset + 1] & 0x3F) + 1) << 3;
7400b57cec5SDimitry Andric   if (Prologue)
7410b57cec5SDimitry Andric     SW.startLine() << format(
7420b57cec5SDimitry Andric         "0x%02x%02x              ; stp d%u, d%u, [sp, #-%u]!\n", OC[Offset],
7430b57cec5SDimitry Andric         OC[Offset + 1], Reg, Reg + 1, Off);
7440b57cec5SDimitry Andric   else
7450b57cec5SDimitry Andric     SW.startLine() << format(
7460b57cec5SDimitry Andric         "0x%02x%02x              ; ldp d%u, d%u, [sp], #%u\n", OC[Offset],
7470b57cec5SDimitry Andric         OC[Offset + 1], Reg, Reg + 1, Off);
7480b57cec5SDimitry Andric   Offset += 2;
7490b57cec5SDimitry Andric   return false;
7500b57cec5SDimitry Andric }
7510b57cec5SDimitry Andric 
7520b57cec5SDimitry Andric bool Decoder::opcode_save_freg(const uint8_t *OC, unsigned &Offset,
7530b57cec5SDimitry Andric                                unsigned Length, bool Prologue) {
7540b57cec5SDimitry Andric   uint32_t Reg = (OC[Offset] & 0x01) << 8;
7550b57cec5SDimitry Andric   Reg |= (OC[Offset + 1] & 0xC0);
7560b57cec5SDimitry Andric   Reg >>= 6;
7570b57cec5SDimitry Andric   Reg += 8;
7580b57cec5SDimitry Andric   uint32_t Off = (OC[Offset + 1] & 0x3F) << 3;
7590b57cec5SDimitry Andric   SW.startLine() << format("0x%02x%02x              ; %s d%u, [sp, #%u]\n",
7600b57cec5SDimitry Andric                            OC[Offset], OC[Offset + 1],
7610b57cec5SDimitry Andric                            static_cast<const char *>(Prologue ? "str" : "ldr"),
7620b57cec5SDimitry Andric                            Reg, Off);
7630b57cec5SDimitry Andric   Offset += 2;
7640b57cec5SDimitry Andric   return false;
7650b57cec5SDimitry Andric }
7660b57cec5SDimitry Andric 
7670b57cec5SDimitry Andric bool Decoder::opcode_save_freg_x(const uint8_t *OC, unsigned &Offset,
7680b57cec5SDimitry Andric                                  unsigned Length, bool Prologue) {
7690b57cec5SDimitry Andric   uint32_t Reg = ((OC[Offset + 1] & 0xE0) >> 5) + 8;
7700b57cec5SDimitry Andric   uint32_t Off = ((OC[Offset + 1] & 0x1F) + 1) << 3;
7710b57cec5SDimitry Andric   if (Prologue)
7720b57cec5SDimitry Andric     SW.startLine() << format(
7730b57cec5SDimitry Andric         "0x%02x%02x              ; str d%u, [sp, #-%u]!\n", OC[Offset],
7740b57cec5SDimitry Andric         OC[Offset + 1], Reg, Off);
7750b57cec5SDimitry Andric   else
7760b57cec5SDimitry Andric     SW.startLine() << format(
7770b57cec5SDimitry Andric         "0x%02x%02x              ; ldr d%u, [sp], #%u\n", OC[Offset],
7780b57cec5SDimitry Andric         OC[Offset + 1], Reg, Off);
7790b57cec5SDimitry Andric   Offset += 2;
7800b57cec5SDimitry Andric   return false;
7810b57cec5SDimitry Andric }
7820b57cec5SDimitry Andric 
7830b57cec5SDimitry Andric bool Decoder::opcode_alloc_l(const uint8_t *OC, unsigned &Offset,
7840b57cec5SDimitry Andric                              unsigned Length, bool Prologue) {
7850b57cec5SDimitry Andric   unsigned Off =
7860b57cec5SDimitry Andric       (OC[Offset + 1] << 16) | (OC[Offset + 2] << 8) | (OC[Offset + 3] << 0);
7870b57cec5SDimitry Andric   Off <<= 4;
7880b57cec5SDimitry Andric   SW.startLine() << format(
7890b57cec5SDimitry Andric       "0x%02x%02x%02x%02x          ; %s sp, #%u\n", OC[Offset], OC[Offset + 1],
7900b57cec5SDimitry Andric       OC[Offset + 2], OC[Offset + 3],
7910b57cec5SDimitry Andric       static_cast<const char *>(Prologue ? "sub" : "add"), Off);
7920b57cec5SDimitry Andric   Offset += 4;
7930b57cec5SDimitry Andric   return false;
7940b57cec5SDimitry Andric }
7950b57cec5SDimitry Andric 
7960b57cec5SDimitry Andric bool Decoder::opcode_setfp(const uint8_t *OC, unsigned &Offset, unsigned Length,
7970b57cec5SDimitry Andric                            bool Prologue) {
798e8d8bef9SDimitry Andric   SW.startLine() << format("0x%02x                ; mov %s, %s\n", OC[Offset],
799e8d8bef9SDimitry Andric                            static_cast<const char *>(Prologue ? "fp" : "sp"),
800e8d8bef9SDimitry Andric                            static_cast<const char *>(Prologue ? "sp" : "fp"));
8010b57cec5SDimitry Andric   ++Offset;
8020b57cec5SDimitry Andric   return false;
8030b57cec5SDimitry Andric }
8040b57cec5SDimitry Andric 
8050b57cec5SDimitry Andric bool Decoder::opcode_addfp(const uint8_t *OC, unsigned &Offset, unsigned Length,
8060b57cec5SDimitry Andric                            bool Prologue) {
8070b57cec5SDimitry Andric   unsigned NumBytes = OC[Offset + 1] << 3;
808e8d8bef9SDimitry Andric   SW.startLine() << format(
809e8d8bef9SDimitry Andric       "0x%02x%02x              ; %s %s, %s, #%u\n", OC[Offset], OC[Offset + 1],
810e8d8bef9SDimitry Andric       static_cast<const char *>(Prologue ? "add" : "sub"),
811e8d8bef9SDimitry Andric       static_cast<const char *>(Prologue ? "fp" : "sp"),
812e8d8bef9SDimitry Andric       static_cast<const char *>(Prologue ? "sp" : "fp"), NumBytes);
8130b57cec5SDimitry Andric   Offset += 2;
8140b57cec5SDimitry Andric   return false;
8150b57cec5SDimitry Andric }
8160b57cec5SDimitry Andric 
8170b57cec5SDimitry Andric bool Decoder::opcode_nop(const uint8_t *OC, unsigned &Offset, unsigned Length,
8180b57cec5SDimitry Andric                          bool Prologue) {
8190b57cec5SDimitry Andric   SW.startLine() << format("0x%02x                ; nop\n", OC[Offset]);
8200b57cec5SDimitry Andric   ++Offset;
8210b57cec5SDimitry Andric   return false;
8220b57cec5SDimitry Andric }
8230b57cec5SDimitry Andric 
8240b57cec5SDimitry Andric bool Decoder::opcode_end(const uint8_t *OC, unsigned &Offset, unsigned Length,
8250b57cec5SDimitry Andric                          bool Prologue) {
8260b57cec5SDimitry Andric   SW.startLine() << format("0x%02x                ; end\n", OC[Offset]);
8270b57cec5SDimitry Andric   ++Offset;
8280b57cec5SDimitry Andric   return true;
8290b57cec5SDimitry Andric }
8300b57cec5SDimitry Andric 
8310b57cec5SDimitry Andric bool Decoder::opcode_end_c(const uint8_t *OC, unsigned &Offset, unsigned Length,
8320b57cec5SDimitry Andric                            bool Prologue) {
8330b57cec5SDimitry Andric   SW.startLine() << format("0x%02x                ; end_c\n", OC[Offset]);
8340b57cec5SDimitry Andric   ++Offset;
8350b57cec5SDimitry Andric   return true;
8360b57cec5SDimitry Andric }
8370b57cec5SDimitry Andric 
838e8d8bef9SDimitry Andric bool Decoder::opcode_save_next(const uint8_t *OC, unsigned &Offset,
839e8d8bef9SDimitry Andric                                unsigned Length, bool Prologue) {
840e8d8bef9SDimitry Andric   if (Prologue)
841e8d8bef9SDimitry Andric     SW.startLine() << format("0x%02x                ; save next\n", OC[Offset]);
842e8d8bef9SDimitry Andric   else
843e8d8bef9SDimitry Andric     SW.startLine() << format("0x%02x                ; restore next\n",
844e8d8bef9SDimitry Andric                              OC[Offset]);
845e8d8bef9SDimitry Andric   ++Offset;
846e8d8bef9SDimitry Andric   return false;
847e8d8bef9SDimitry Andric }
848e8d8bef9SDimitry Andric 
849e8d8bef9SDimitry Andric bool Decoder::opcode_trap_frame(const uint8_t *OC, unsigned &Offset,
850e8d8bef9SDimitry Andric                                 unsigned Length, bool Prologue) {
851e8d8bef9SDimitry Andric   SW.startLine() << format("0x%02x                ; trap frame\n", OC[Offset]);
852e8d8bef9SDimitry Andric   ++Offset;
853e8d8bef9SDimitry Andric   return false;
854e8d8bef9SDimitry Andric }
855e8d8bef9SDimitry Andric 
856e8d8bef9SDimitry Andric bool Decoder::opcode_machine_frame(const uint8_t *OC, unsigned &Offset,
857e8d8bef9SDimitry Andric                                    unsigned Length, bool Prologue) {
858e8d8bef9SDimitry Andric   SW.startLine() << format("0x%02x                ; machine frame\n",
859e8d8bef9SDimitry Andric                            OC[Offset]);
860e8d8bef9SDimitry Andric   ++Offset;
861e8d8bef9SDimitry Andric   return false;
862e8d8bef9SDimitry Andric }
863e8d8bef9SDimitry Andric 
864e8d8bef9SDimitry Andric bool Decoder::opcode_context(const uint8_t *OC, unsigned &Offset,
865e8d8bef9SDimitry Andric                              unsigned Length, bool Prologue) {
866e8d8bef9SDimitry Andric   SW.startLine() << format("0x%02x                ; context\n", OC[Offset]);
867e8d8bef9SDimitry Andric   ++Offset;
868e8d8bef9SDimitry Andric   return false;
869e8d8bef9SDimitry Andric }
870e8d8bef9SDimitry Andric 
871e8d8bef9SDimitry Andric bool Decoder::opcode_clear_unwound_to_call(const uint8_t *OC, unsigned &Offset,
872e8d8bef9SDimitry Andric                                            unsigned Length, bool Prologue) {
873e8d8bef9SDimitry Andric   SW.startLine() << format("0x%02x                ; clear unwound to call\n",
874e8d8bef9SDimitry Andric                            OC[Offset]);
875e8d8bef9SDimitry Andric   ++Offset;
876e8d8bef9SDimitry Andric   return false;
877e8d8bef9SDimitry Andric }
878e8d8bef9SDimitry Andric 
8790b57cec5SDimitry Andric void Decoder::decodeOpcodes(ArrayRef<uint8_t> Opcodes, unsigned Offset,
8800b57cec5SDimitry Andric                             bool Prologue) {
8810b57cec5SDimitry Andric   assert((!Prologue || Offset == 0) && "prologue should always use offset 0");
8820b57cec5SDimitry Andric   const RingEntry* DecodeRing = isAArch64 ? Ring64 : Ring;
8830b57cec5SDimitry Andric   bool Terminated = false;
8840b57cec5SDimitry Andric   for (unsigned OI = Offset, OE = Opcodes.size(); !Terminated && OI < OE; ) {
8850b57cec5SDimitry Andric     for (unsigned DI = 0;; ++DI) {
8860b57cec5SDimitry Andric       if ((isAArch64 && (DI >= array_lengthof(Ring64))) ||
8870b57cec5SDimitry Andric           (!isAArch64 && (DI >= array_lengthof(Ring)))) {
8880b57cec5SDimitry Andric         SW.startLine() << format("0x%02x                ; Bad opcode!\n",
8890b57cec5SDimitry Andric                                  Opcodes.data()[OI]);
8900b57cec5SDimitry Andric         ++OI;
8910b57cec5SDimitry Andric         break;
8920b57cec5SDimitry Andric       }
8930b57cec5SDimitry Andric 
8940b57cec5SDimitry Andric       if ((Opcodes[OI] & DecodeRing[DI].Mask) == DecodeRing[DI].Value) {
8950b57cec5SDimitry Andric         if (OI + DecodeRing[DI].Length > OE) {
8960b57cec5SDimitry Andric           SW.startLine() << format("Opcode 0x%02x goes past the unwind data\n",
8970b57cec5SDimitry Andric                                     Opcodes[OI]);
8980b57cec5SDimitry Andric           OI += DecodeRing[DI].Length;
8990b57cec5SDimitry Andric           break;
9000b57cec5SDimitry Andric         }
9010b57cec5SDimitry Andric         Terminated =
9020b57cec5SDimitry Andric             (this->*DecodeRing[DI].Routine)(Opcodes.data(), OI, 0, Prologue);
9030b57cec5SDimitry Andric         break;
9040b57cec5SDimitry Andric       }
9050b57cec5SDimitry Andric     }
9060b57cec5SDimitry Andric   }
9070b57cec5SDimitry Andric }
9080b57cec5SDimitry Andric 
9090b57cec5SDimitry Andric bool Decoder::dumpXDataRecord(const COFFObjectFile &COFF,
9100b57cec5SDimitry Andric                               const SectionRef &Section,
9110b57cec5SDimitry Andric                               uint64_t FunctionAddress, uint64_t VA) {
9120b57cec5SDimitry Andric   ArrayRef<uint8_t> Contents;
9130b57cec5SDimitry Andric   if (COFF.getSectionContents(COFF.getCOFFSection(Section), Contents))
9140b57cec5SDimitry Andric     return false;
9150b57cec5SDimitry Andric 
9160b57cec5SDimitry Andric   uint64_t SectionVA = Section.getAddress();
9170b57cec5SDimitry Andric   uint64_t Offset = VA - SectionVA;
9180b57cec5SDimitry Andric   const ulittle32_t *Data =
9190b57cec5SDimitry Andric     reinterpret_cast<const ulittle32_t *>(Contents.data() + Offset);
9200b57cec5SDimitry Andric 
9210b57cec5SDimitry Andric   // Sanity check to ensure that the .xdata header is present.
9220b57cec5SDimitry Andric   // A header is one or two words, followed by at least one word to describe
9230b57cec5SDimitry Andric   // the unwind codes. Applicable to both ARM and AArch64.
9240b57cec5SDimitry Andric   if (Contents.size() - Offset < 8)
9250b57cec5SDimitry Andric     report_fatal_error(".xdata must be at least 8 bytes in size");
9260b57cec5SDimitry Andric 
9270b57cec5SDimitry Andric   const ExceptionDataRecord XData(Data, isAArch64);
9280b57cec5SDimitry Andric   DictScope XRS(SW, "ExceptionData");
9290b57cec5SDimitry Andric   SW.printNumber("FunctionLength",
9300b57cec5SDimitry Andric                  isAArch64 ? XData.FunctionLengthInBytesAArch64() :
9310b57cec5SDimitry Andric                  XData.FunctionLengthInBytesARM());
9320b57cec5SDimitry Andric   SW.printNumber("Version", XData.Vers());
9330b57cec5SDimitry Andric   SW.printBoolean("ExceptionData", XData.X());
9340b57cec5SDimitry Andric   SW.printBoolean("EpiloguePacked", XData.E());
9350b57cec5SDimitry Andric   if (!isAArch64)
9360b57cec5SDimitry Andric     SW.printBoolean("Fragment", XData.F());
9370b57cec5SDimitry Andric   SW.printNumber(XData.E() ? "EpilogueOffset" : "EpilogueScopes",
9380b57cec5SDimitry Andric                  XData.EpilogueCount());
9390b57cec5SDimitry Andric   uint64_t ByteCodeLength = XData.CodeWords() * sizeof(uint32_t);
9400b57cec5SDimitry Andric   SW.printNumber("ByteCodeLength", ByteCodeLength);
9410b57cec5SDimitry Andric 
9420b57cec5SDimitry Andric   if ((int64_t)(Contents.size() - Offset - 4 * HeaderWords(XData) -
9430b57cec5SDimitry Andric                 (XData.E() ? 0 : XData.EpilogueCount() * 4) -
9448bcb0991SDimitry Andric                 (XData.X() ? 8 : 0)) < (int64_t)ByteCodeLength) {
9458bcb0991SDimitry Andric     SW.flush();
9460b57cec5SDimitry Andric     report_fatal_error("Malformed unwind data");
9478bcb0991SDimitry Andric   }
9480b57cec5SDimitry Andric 
9490b57cec5SDimitry Andric   if (XData.E()) {
9500b57cec5SDimitry Andric     ArrayRef<uint8_t> UC = XData.UnwindByteCode();
9510b57cec5SDimitry Andric     if (isAArch64 || !XData.F()) {
9520b57cec5SDimitry Andric       ListScope PS(SW, "Prologue");
9530b57cec5SDimitry Andric       decodeOpcodes(UC, 0, /*Prologue=*/true);
9540b57cec5SDimitry Andric     }
9550b57cec5SDimitry Andric     if (XData.EpilogueCount()) {
9560b57cec5SDimitry Andric       ListScope ES(SW, "Epilogue");
9570b57cec5SDimitry Andric       decodeOpcodes(UC, XData.EpilogueCount(), /*Prologue=*/false);
9580b57cec5SDimitry Andric     }
9590b57cec5SDimitry Andric   } else {
9600b57cec5SDimitry Andric     {
9610b57cec5SDimitry Andric       ListScope PS(SW, "Prologue");
9620b57cec5SDimitry Andric       decodeOpcodes(XData.UnwindByteCode(), 0, /*Prologue=*/true);
9630b57cec5SDimitry Andric     }
9640b57cec5SDimitry Andric     ArrayRef<ulittle32_t> EpilogueScopes = XData.EpilogueScopes();
9650b57cec5SDimitry Andric     ListScope ESS(SW, "EpilogueScopes");
9660b57cec5SDimitry Andric     for (const EpilogueScope ES : EpilogueScopes) {
9670b57cec5SDimitry Andric       DictScope ESES(SW, "EpilogueScope");
9680b57cec5SDimitry Andric       SW.printNumber("StartOffset", ES.EpilogueStartOffset());
9690b57cec5SDimitry Andric       if (!isAArch64)
9700b57cec5SDimitry Andric         SW.printNumber("Condition", ES.Condition());
9710b57cec5SDimitry Andric       SW.printNumber("EpilogueStartIndex",
9720b57cec5SDimitry Andric                      isAArch64 ? ES.EpilogueStartIndexAArch64()
9730b57cec5SDimitry Andric                                : ES.EpilogueStartIndexARM());
9740b57cec5SDimitry Andric       if (ES.ES & ~0xffc3ffff)
9750b57cec5SDimitry Andric         SW.printNumber("ReservedBits", (ES.ES >> 18) & 0xF);
9760b57cec5SDimitry Andric 
9770b57cec5SDimitry Andric       ListScope Opcodes(SW, "Opcodes");
9780b57cec5SDimitry Andric       decodeOpcodes(XData.UnwindByteCode(),
9790b57cec5SDimitry Andric                     isAArch64 ? ES.EpilogueStartIndexAArch64()
9800b57cec5SDimitry Andric                               : ES.EpilogueStartIndexARM(),
9810b57cec5SDimitry Andric                     /*Prologue=*/false);
9820b57cec5SDimitry Andric     }
9830b57cec5SDimitry Andric   }
9840b57cec5SDimitry Andric 
9850b57cec5SDimitry Andric   if (XData.X()) {
9860b57cec5SDimitry Andric     const uint32_t Parameter = XData.ExceptionHandlerParameter();
987fe6060f1SDimitry Andric     const size_t HandlerOffset = HeaderWords(XData) +
988fe6060f1SDimitry Andric                                  (XData.E() ? 0 : XData.EpilogueCount()) +
989fe6060f1SDimitry Andric                                  XData.CodeWords();
9900b57cec5SDimitry Andric 
991fe6060f1SDimitry Andric     uint64_t Address, SymbolOffset;
992fe6060f1SDimitry Andric     ErrorOr<SymbolRef> Symbol = getSymbolForLocation(
993fe6060f1SDimitry Andric         COFF, Section, Offset + HandlerOffset * sizeof(uint32_t),
994fe6060f1SDimitry Andric         XData.ExceptionHandlerRVA(), Address, SymbolOffset,
995fe6060f1SDimitry Andric         /*FunctionOnly=*/true);
9960b57cec5SDimitry Andric     if (!Symbol) {
9970b57cec5SDimitry Andric       ListScope EHS(SW, "ExceptionHandler");
998480093f4SDimitry Andric       SW.printHex("Routine", Address);
999480093f4SDimitry Andric       SW.printHex("Parameter", Parameter);
10000b57cec5SDimitry Andric       return true;
10010b57cec5SDimitry Andric     }
10020b57cec5SDimitry Andric 
10030b57cec5SDimitry Andric     Expected<StringRef> Name = Symbol->getName();
10040b57cec5SDimitry Andric     if (!Name) {
10050b57cec5SDimitry Andric       std::string Buf;
10060b57cec5SDimitry Andric       llvm::raw_string_ostream OS(Buf);
10070b57cec5SDimitry Andric       logAllUnhandledErrors(Name.takeError(), OS);
1008*349cc55cSDimitry Andric       report_fatal_error(Twine(OS.str()));
10090b57cec5SDimitry Andric     }
10100b57cec5SDimitry Andric 
10110b57cec5SDimitry Andric     ListScope EHS(SW, "ExceptionHandler");
1012fe6060f1SDimitry Andric     SW.printString("Routine", formatSymbol(*Name, Address, SymbolOffset));
10130b57cec5SDimitry Andric     SW.printHex("Parameter", Parameter);
10140b57cec5SDimitry Andric   }
10150b57cec5SDimitry Andric 
10160b57cec5SDimitry Andric   return true;
10170b57cec5SDimitry Andric }
10180b57cec5SDimitry Andric 
10190b57cec5SDimitry Andric bool Decoder::dumpUnpackedEntry(const COFFObjectFile &COFF,
10200b57cec5SDimitry Andric                                 const SectionRef Section, uint64_t Offset,
10210b57cec5SDimitry Andric                                 unsigned Index, const RuntimeFunction &RF) {
10220b57cec5SDimitry Andric   assert(RF.Flag() == RuntimeFunctionFlag::RFF_Unpacked &&
10230b57cec5SDimitry Andric          "packed entry cannot be treated as an unpacked entry");
10240b57cec5SDimitry Andric 
1025fe6060f1SDimitry Andric   uint64_t FunctionAddress, FunctionOffset;
1026fe6060f1SDimitry Andric   ErrorOr<SymbolRef> Function = getSymbolForLocation(
1027fe6060f1SDimitry Andric       COFF, Section, Offset, RF.BeginAddress, FunctionAddress, FunctionOffset,
1028480093f4SDimitry Andric       /*FunctionOnly=*/true);
10290b57cec5SDimitry Andric 
1030fe6060f1SDimitry Andric   uint64_t XDataAddress, XDataOffset;
1031fe6060f1SDimitry Andric   ErrorOr<SymbolRef> XDataRecord = getSymbolForLocation(
1032fe6060f1SDimitry Andric       COFF, Section, Offset + 4, RF.ExceptionInformationRVA(), XDataAddress,
1033fe6060f1SDimitry Andric       XDataOffset);
10340b57cec5SDimitry Andric 
10350b57cec5SDimitry Andric   if (!RF.BeginAddress && !Function)
10360b57cec5SDimitry Andric     return false;
10370b57cec5SDimitry Andric   if (!RF.UnwindData && !XDataRecord)
10380b57cec5SDimitry Andric     return false;
10390b57cec5SDimitry Andric 
10400b57cec5SDimitry Andric   StringRef FunctionName;
10410b57cec5SDimitry Andric   if (Function) {
10420b57cec5SDimitry Andric     Expected<StringRef> FunctionNameOrErr = Function->getName();
10430b57cec5SDimitry Andric     if (!FunctionNameOrErr) {
10440b57cec5SDimitry Andric       std::string Buf;
10450b57cec5SDimitry Andric       llvm::raw_string_ostream OS(Buf);
10460b57cec5SDimitry Andric       logAllUnhandledErrors(FunctionNameOrErr.takeError(), OS);
1047*349cc55cSDimitry Andric       report_fatal_error(Twine(OS.str()));
10480b57cec5SDimitry Andric     }
10490b57cec5SDimitry Andric     FunctionName = *FunctionNameOrErr;
10500b57cec5SDimitry Andric   }
10510b57cec5SDimitry Andric 
1052fe6060f1SDimitry Andric   SW.printString("Function",
1053fe6060f1SDimitry Andric                  formatSymbol(FunctionName, FunctionAddress, FunctionOffset));
10540b57cec5SDimitry Andric 
10550b57cec5SDimitry Andric   if (XDataRecord) {
10560b57cec5SDimitry Andric     Expected<StringRef> Name = XDataRecord->getName();
10570b57cec5SDimitry Andric     if (!Name) {
10580b57cec5SDimitry Andric       std::string Buf;
10590b57cec5SDimitry Andric       llvm::raw_string_ostream OS(Buf);
10600b57cec5SDimitry Andric       logAllUnhandledErrors(Name.takeError(), OS);
1061*349cc55cSDimitry Andric       report_fatal_error(Twine(OS.str()));
10620b57cec5SDimitry Andric     }
10630b57cec5SDimitry Andric 
1064fe6060f1SDimitry Andric     SW.printString("ExceptionRecord",
1065fe6060f1SDimitry Andric                    formatSymbol(*Name, XDataAddress, XDataOffset));
10660b57cec5SDimitry Andric 
10670b57cec5SDimitry Andric     Expected<section_iterator> SIOrErr = XDataRecord->getSection();
10680b57cec5SDimitry Andric     if (!SIOrErr) {
10690b57cec5SDimitry Andric       // TODO: Actually report errors helpfully.
10700b57cec5SDimitry Andric       consumeError(SIOrErr.takeError());
10710b57cec5SDimitry Andric       return false;
10720b57cec5SDimitry Andric     }
10730b57cec5SDimitry Andric     section_iterator SI = *SIOrErr;
10740b57cec5SDimitry Andric 
1075fe6060f1SDimitry Andric     return dumpXDataRecord(COFF, *SI, FunctionAddress, XDataAddress);
10760b57cec5SDimitry Andric   } else {
1077fe6060f1SDimitry Andric     SW.printString("ExceptionRecord", formatSymbol("", XDataAddress));
10780b57cec5SDimitry Andric 
1079fe6060f1SDimitry Andric     ErrorOr<SectionRef> Section = getSectionContaining(COFF, XDataAddress);
10800b57cec5SDimitry Andric     if (!Section)
10810b57cec5SDimitry Andric       return false;
10820b57cec5SDimitry Andric 
1083fe6060f1SDimitry Andric     return dumpXDataRecord(COFF, *Section, FunctionAddress, XDataAddress);
10840b57cec5SDimitry Andric   }
10850b57cec5SDimitry Andric }
10860b57cec5SDimitry Andric 
10870b57cec5SDimitry Andric bool Decoder::dumpPackedEntry(const object::COFFObjectFile &COFF,
10880b57cec5SDimitry Andric                               const SectionRef Section, uint64_t Offset,
10890b57cec5SDimitry Andric                               unsigned Index, const RuntimeFunction &RF) {
10900b57cec5SDimitry Andric   assert((RF.Flag() == RuntimeFunctionFlag::RFF_Packed ||
10910b57cec5SDimitry Andric           RF.Flag() == RuntimeFunctionFlag::RFF_PackedFragment) &&
10920b57cec5SDimitry Andric          "unpacked entry cannot be treated as a packed entry");
10930b57cec5SDimitry Andric 
1094fe6060f1SDimitry Andric   uint64_t FunctionAddress, FunctionOffset;
1095fe6060f1SDimitry Andric   ErrorOr<SymbolRef> Function = getSymbolForLocation(
1096fe6060f1SDimitry Andric       COFF, Section, Offset, RF.BeginAddress, FunctionAddress, FunctionOffset,
1097fe6060f1SDimitry Andric       /*FunctionOnly=*/true);
10980b57cec5SDimitry Andric 
10990b57cec5SDimitry Andric   StringRef FunctionName;
11000b57cec5SDimitry Andric   if (Function) {
11010b57cec5SDimitry Andric     Expected<StringRef> FunctionNameOrErr = Function->getName();
11020b57cec5SDimitry Andric     if (!FunctionNameOrErr) {
11030b57cec5SDimitry Andric       std::string Buf;
11040b57cec5SDimitry Andric       llvm::raw_string_ostream OS(Buf);
11050b57cec5SDimitry Andric       logAllUnhandledErrors(FunctionNameOrErr.takeError(), OS);
1106*349cc55cSDimitry Andric       report_fatal_error(Twine(OS.str()));
11070b57cec5SDimitry Andric     }
11080b57cec5SDimitry Andric     FunctionName = *FunctionNameOrErr;
11090b57cec5SDimitry Andric   }
11100b57cec5SDimitry Andric 
1111fe6060f1SDimitry Andric   SW.printString("Function",
1112fe6060f1SDimitry Andric                  formatSymbol(FunctionName, FunctionAddress, FunctionOffset));
11130b57cec5SDimitry Andric   if (!isAArch64)
11140b57cec5SDimitry Andric     SW.printBoolean("Fragment",
11150b57cec5SDimitry Andric                     RF.Flag() == RuntimeFunctionFlag::RFF_PackedFragment);
11160b57cec5SDimitry Andric   SW.printNumber("FunctionLength", RF.FunctionLength());
11170b57cec5SDimitry Andric   SW.startLine() << "ReturnType: " << RF.Ret() << '\n';
11180b57cec5SDimitry Andric   SW.printBoolean("HomedParameters", RF.H());
11190b57cec5SDimitry Andric   SW.startLine() << "SavedRegisters: ";
11200b57cec5SDimitry Andric                  printRegisters(SavedRegisterMask(RF));
11210b57cec5SDimitry Andric   OS << '\n';
11220b57cec5SDimitry Andric   SW.printNumber("StackAdjustment", StackAdjustment(RF) << 2);
11230b57cec5SDimitry Andric 
11240b57cec5SDimitry Andric   return true;
11250b57cec5SDimitry Andric }
11260b57cec5SDimitry Andric 
1127e8d8bef9SDimitry Andric bool Decoder::dumpPackedARM64Entry(const object::COFFObjectFile &COFF,
1128e8d8bef9SDimitry Andric                                    const SectionRef Section, uint64_t Offset,
1129e8d8bef9SDimitry Andric                                    unsigned Index,
1130e8d8bef9SDimitry Andric                                    const RuntimeFunctionARM64 &RF) {
1131e8d8bef9SDimitry Andric   assert((RF.Flag() == RuntimeFunctionFlag::RFF_Packed ||
1132e8d8bef9SDimitry Andric           RF.Flag() == RuntimeFunctionFlag::RFF_PackedFragment) &&
1133e8d8bef9SDimitry Andric          "unpacked entry cannot be treated as a packed entry");
1134e8d8bef9SDimitry Andric 
1135fe6060f1SDimitry Andric   uint64_t FunctionAddress, FunctionOffset;
1136fe6060f1SDimitry Andric   ErrorOr<SymbolRef> Function = getSymbolForLocation(
1137fe6060f1SDimitry Andric       COFF, Section, Offset, RF.BeginAddress, FunctionAddress, FunctionOffset,
1138fe6060f1SDimitry Andric       /*FunctionOnly=*/true);
1139e8d8bef9SDimitry Andric 
1140e8d8bef9SDimitry Andric   StringRef FunctionName;
1141e8d8bef9SDimitry Andric   if (Function) {
1142e8d8bef9SDimitry Andric     Expected<StringRef> FunctionNameOrErr = Function->getName();
1143e8d8bef9SDimitry Andric     if (!FunctionNameOrErr) {
1144e8d8bef9SDimitry Andric       std::string Buf;
1145e8d8bef9SDimitry Andric       llvm::raw_string_ostream OS(Buf);
1146e8d8bef9SDimitry Andric       logAllUnhandledErrors(FunctionNameOrErr.takeError(), OS);
1147*349cc55cSDimitry Andric       report_fatal_error(Twine(OS.str()));
1148e8d8bef9SDimitry Andric     }
1149e8d8bef9SDimitry Andric     FunctionName = *FunctionNameOrErr;
1150e8d8bef9SDimitry Andric   }
1151e8d8bef9SDimitry Andric 
1152fe6060f1SDimitry Andric   SW.printString("Function",
1153fe6060f1SDimitry Andric                  formatSymbol(FunctionName, FunctionAddress, FunctionOffset));
1154e8d8bef9SDimitry Andric   SW.printBoolean("Fragment",
1155e8d8bef9SDimitry Andric                   RF.Flag() == RuntimeFunctionFlag::RFF_PackedFragment);
1156e8d8bef9SDimitry Andric   SW.printNumber("FunctionLength", RF.FunctionLength());
1157e8d8bef9SDimitry Andric   SW.printNumber("RegF", RF.RegF());
1158e8d8bef9SDimitry Andric   SW.printNumber("RegI", RF.RegI());
1159e8d8bef9SDimitry Andric   SW.printBoolean("HomedParameters", RF.H());
1160e8d8bef9SDimitry Andric   SW.printNumber("CR", RF.CR());
1161e8d8bef9SDimitry Andric   SW.printNumber("FrameSize", RF.FrameSize() << 4);
1162e8d8bef9SDimitry Andric   ListScope PS(SW, "Prologue");
1163e8d8bef9SDimitry Andric 
1164e8d8bef9SDimitry Andric   // Synthesize the equivalent prologue according to the documentation
1165e8d8bef9SDimitry Andric   // at https://docs.microsoft.com/en-us/cpp/build/arm64-exception-handling,
1166e8d8bef9SDimitry Andric   // printed in reverse order compared to the docs, to match how prologues
1167e8d8bef9SDimitry Andric   // are printed for the non-packed case.
1168e8d8bef9SDimitry Andric   int IntSZ = 8 * RF.RegI();
1169e8d8bef9SDimitry Andric   if (RF.CR() == 1)
1170e8d8bef9SDimitry Andric     IntSZ += 8;
1171e8d8bef9SDimitry Andric   int FpSZ = 8 * RF.RegF();
1172e8d8bef9SDimitry Andric   if (RF.RegF())
1173e8d8bef9SDimitry Andric     FpSZ += 8;
1174e8d8bef9SDimitry Andric   int SavSZ = (IntSZ + FpSZ + 8 * 8 * RF.H() + 0xf) & ~0xf;
1175e8d8bef9SDimitry Andric   int LocSZ = (RF.FrameSize() << 4) - SavSZ;
1176e8d8bef9SDimitry Andric 
1177e8d8bef9SDimitry Andric   if (RF.CR() == 3) {
1178e8d8bef9SDimitry Andric     SW.startLine() << "mov x29, sp\n";
1179e8d8bef9SDimitry Andric     if (LocSZ <= 512) {
1180e8d8bef9SDimitry Andric       SW.startLine() << format("stp x29, lr, [sp, #-%d]!\n", LocSZ);
1181e8d8bef9SDimitry Andric     } else {
1182e8d8bef9SDimitry Andric       SW.startLine() << "stp x29, lr, [sp, #0]\n";
1183e8d8bef9SDimitry Andric     }
1184e8d8bef9SDimitry Andric   }
1185e8d8bef9SDimitry Andric   if (LocSZ > 4080) {
1186e8d8bef9SDimitry Andric     SW.startLine() << format("sub sp, sp, #%d\n", LocSZ - 4080);
1187e8d8bef9SDimitry Andric     SW.startLine() << "sub sp, sp, #4080\n";
1188e8d8bef9SDimitry Andric   } else if ((RF.CR() != 3 && LocSZ > 0) || LocSZ > 512) {
1189e8d8bef9SDimitry Andric     SW.startLine() << format("sub sp, sp, #%d\n", LocSZ);
1190e8d8bef9SDimitry Andric   }
1191e8d8bef9SDimitry Andric   if (RF.H()) {
1192e8d8bef9SDimitry Andric     SW.startLine() << format("stp x6, x7, [sp, #%d]\n", IntSZ + FpSZ + 48);
1193e8d8bef9SDimitry Andric     SW.startLine() << format("stp x4, x5, [sp, #%d]\n", IntSZ + FpSZ + 32);
1194e8d8bef9SDimitry Andric     SW.startLine() << format("stp x2, x3, [sp, #%d]\n", IntSZ + FpSZ + 16);
1195e8d8bef9SDimitry Andric     if (RF.RegI() > 0 || RF.RegF() > 0 || RF.CR() == 1) {
1196e8d8bef9SDimitry Andric       SW.startLine() << format("stp x0, x1, [sp, #%d]\n", IntSZ + FpSZ);
1197e8d8bef9SDimitry Andric     } else {
1198e8d8bef9SDimitry Andric       // This case isn't documented; if neither RegI nor RegF nor CR=1
1199e8d8bef9SDimitry Andric       // have decremented the stack pointer by SavSZ, we need to do it here
1200e8d8bef9SDimitry Andric       // (as the final stack adjustment of LocSZ excludes SavSZ).
1201e8d8bef9SDimitry Andric       SW.startLine() << format("stp x0, x1, [sp, #-%d]!\n", SavSZ);
1202e8d8bef9SDimitry Andric     }
1203e8d8bef9SDimitry Andric   }
1204e8d8bef9SDimitry Andric   int FloatRegs = RF.RegF() > 0 ? RF.RegF() + 1 : 0;
1205e8d8bef9SDimitry Andric   for (int I = (FloatRegs + 1) / 2 - 1; I >= 0; I--) {
1206e8d8bef9SDimitry Andric     if (I == (FloatRegs + 1) / 2 - 1 && FloatRegs % 2 == 1) {
1207e8d8bef9SDimitry Andric       // The last register, an odd register without a pair
1208e8d8bef9SDimitry Andric       SW.startLine() << format("str d%d, [sp, #%d]\n", 8 + 2 * I,
1209e8d8bef9SDimitry Andric                                IntSZ + 16 * I);
1210e8d8bef9SDimitry Andric     } else if (I == 0 && RF.RegI() == 0 && RF.CR() != 1) {
1211e8d8bef9SDimitry Andric       SW.startLine() << format("stp d%d, d%d, [sp, #-%d]!\n", 8 + 2 * I,
1212e8d8bef9SDimitry Andric                                8 + 2 * I + 1, SavSZ);
1213e8d8bef9SDimitry Andric     } else {
1214e8d8bef9SDimitry Andric       SW.startLine() << format("stp d%d, d%d, [sp, #%d]\n", 8 + 2 * I,
1215e8d8bef9SDimitry Andric                                8 + 2 * I + 1, IntSZ + 16 * I);
1216e8d8bef9SDimitry Andric     }
1217e8d8bef9SDimitry Andric   }
1218e8d8bef9SDimitry Andric   if (RF.CR() == 1 && (RF.RegI() % 2) == 0) {
1219e8d8bef9SDimitry Andric     if (RF.RegI() == 0)
1220e8d8bef9SDimitry Andric       SW.startLine() << format("str lr, [sp, #-%d]!\n", SavSZ);
1221e8d8bef9SDimitry Andric     else
1222e8d8bef9SDimitry Andric       SW.startLine() << format("str lr, [sp, #%d]\n", IntSZ - 8);
1223e8d8bef9SDimitry Andric   }
1224e8d8bef9SDimitry Andric   for (int I = (RF.RegI() + 1) / 2 - 1; I >= 0; I--) {
1225e8d8bef9SDimitry Andric     if (I == (RF.RegI() + 1) / 2 - 1 && RF.RegI() % 2 == 1) {
1226e8d8bef9SDimitry Andric       // The last register, an odd register without a pair
1227e8d8bef9SDimitry Andric       if (RF.CR() == 1) {
1228e8d8bef9SDimitry Andric         if (I == 0) { // If this is the only register pair
1229e8d8bef9SDimitry Andric           // CR=1 combined with RegI=1 doesn't map to a documented case;
1230e8d8bef9SDimitry Andric           // it doesn't map to any regular unwind info opcode, and the
1231e8d8bef9SDimitry Andric           // actual unwinder doesn't support it.
1232e8d8bef9SDimitry Andric           SW.startLine() << "INVALID!\n";
1233e8d8bef9SDimitry Andric         } else
1234e8d8bef9SDimitry Andric           SW.startLine() << format("stp x%d, lr, [sp, #%d]\n", 19 + 2 * I,
1235e8d8bef9SDimitry Andric                                    16 * I);
1236e8d8bef9SDimitry Andric       } else {
1237e8d8bef9SDimitry Andric         if (I == 0)
1238e8d8bef9SDimitry Andric           SW.startLine() << format("str x%d, [sp, #-%d]!\n", 19 + 2 * I, SavSZ);
1239e8d8bef9SDimitry Andric         else
1240e8d8bef9SDimitry Andric           SW.startLine() << format("str x%d, [sp, #%d]\n", 19 + 2 * I, 16 * I);
1241e8d8bef9SDimitry Andric       }
1242e8d8bef9SDimitry Andric     } else if (I == 0) {
1243e8d8bef9SDimitry Andric       // The first register pair
1244e8d8bef9SDimitry Andric       SW.startLine() << format("stp x19, x20, [sp, #-%d]!\n", SavSZ);
1245e8d8bef9SDimitry Andric     } else {
1246e8d8bef9SDimitry Andric       SW.startLine() << format("stp x%d, x%d, [sp, #%d]\n", 19 + 2 * I,
1247e8d8bef9SDimitry Andric                                19 + 2 * I + 1, 16 * I);
1248e8d8bef9SDimitry Andric     }
1249e8d8bef9SDimitry Andric   }
1250e8d8bef9SDimitry Andric   SW.startLine() << "end\n";
1251e8d8bef9SDimitry Andric 
1252e8d8bef9SDimitry Andric   return true;
1253e8d8bef9SDimitry Andric }
1254e8d8bef9SDimitry Andric 
12550b57cec5SDimitry Andric bool Decoder::dumpProcedureDataEntry(const COFFObjectFile &COFF,
12560b57cec5SDimitry Andric                                      const SectionRef Section, unsigned Index,
12570b57cec5SDimitry Andric                                      ArrayRef<uint8_t> Contents) {
12580b57cec5SDimitry Andric   uint64_t Offset = PDataEntrySize * Index;
12590b57cec5SDimitry Andric   const ulittle32_t *Data =
12600b57cec5SDimitry Andric     reinterpret_cast<const ulittle32_t *>(Contents.data() + Offset);
12610b57cec5SDimitry Andric 
12620b57cec5SDimitry Andric   const RuntimeFunction Entry(Data);
12630b57cec5SDimitry Andric   DictScope RFS(SW, "RuntimeFunction");
12640b57cec5SDimitry Andric   if (Entry.Flag() == RuntimeFunctionFlag::RFF_Unpacked)
12650b57cec5SDimitry Andric     return dumpUnpackedEntry(COFF, Section, Offset, Index, Entry);
12660b57cec5SDimitry Andric   if (isAArch64) {
1267e8d8bef9SDimitry Andric     const RuntimeFunctionARM64 EntryARM64(Data);
1268e8d8bef9SDimitry Andric     return dumpPackedARM64Entry(COFF, Section, Offset, Index, EntryARM64);
12690b57cec5SDimitry Andric   }
12700b57cec5SDimitry Andric   return dumpPackedEntry(COFF, Section, Offset, Index, Entry);
12710b57cec5SDimitry Andric }
12720b57cec5SDimitry Andric 
12730b57cec5SDimitry Andric void Decoder::dumpProcedureData(const COFFObjectFile &COFF,
12740b57cec5SDimitry Andric                                 const SectionRef Section) {
12750b57cec5SDimitry Andric   ArrayRef<uint8_t> Contents;
12760b57cec5SDimitry Andric   if (COFF.getSectionContents(COFF.getCOFFSection(Section), Contents))
12770b57cec5SDimitry Andric     return;
12780b57cec5SDimitry Andric 
12790b57cec5SDimitry Andric   if (Contents.size() % PDataEntrySize) {
12800b57cec5SDimitry Andric     errs() << ".pdata content is not " << PDataEntrySize << "-byte aligned\n";
12810b57cec5SDimitry Andric     return;
12820b57cec5SDimitry Andric   }
12830b57cec5SDimitry Andric 
12840b57cec5SDimitry Andric   for (unsigned EI = 0, EE = Contents.size() / PDataEntrySize; EI < EE; ++EI)
12850b57cec5SDimitry Andric     if (!dumpProcedureDataEntry(COFF, Section, EI, Contents))
12860b57cec5SDimitry Andric       break;
12870b57cec5SDimitry Andric }
12880b57cec5SDimitry Andric 
12890b57cec5SDimitry Andric Error Decoder::dumpProcedureData(const COFFObjectFile &COFF) {
12900b57cec5SDimitry Andric   for (const auto &Section : COFF.sections()) {
12910b57cec5SDimitry Andric     Expected<StringRef> NameOrErr =
12920b57cec5SDimitry Andric         COFF.getSectionName(COFF.getCOFFSection(Section));
12930b57cec5SDimitry Andric     if (!NameOrErr)
12940b57cec5SDimitry Andric       return NameOrErr.takeError();
12950b57cec5SDimitry Andric 
12960b57cec5SDimitry Andric     if (NameOrErr->startswith(".pdata"))
12970b57cec5SDimitry Andric       dumpProcedureData(COFF, Section);
12980b57cec5SDimitry Andric   }
12990b57cec5SDimitry Andric   return Error::success();
13000b57cec5SDimitry Andric }
13010b57cec5SDimitry Andric }
13020b57cec5SDimitry Andric }
13030b57cec5SDimitry Andric }
1304