ARMWinEHPrinter.cpp 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303
  1. //===-- ARMWinEHPrinter.cpp - Windows on ARM EH Data Printer ----*- C++ -*-===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. // Windows on ARM uses a series of serialised data structures (RuntimeFunction)
  9. // to create a table of information for unwinding. In order to conserve space,
  10. // there are two different ways that this data is represented.
  11. //
  12. // For functions with canonical forms for the prologue and epilogue, the data
  13. // can be stored in a "packed" form. In this case, the data is packed into the
  14. // RuntimeFunction's remaining 30-bits and can fully describe the entire frame.
  15. //
  16. // +---------------------------------------+
  17. // | Function Entry Address |
  18. // +---------------------------------------+
  19. // | Packed Form Data |
  20. // +---------------------------------------+
  21. //
  22. // This layout is parsed by Decoder::dumpPackedEntry. No unwind bytecode is
  23. // associated with such a frame as they can be derived from the provided data.
  24. // The decoder does not synthesize this data as it is unnecessary for the
  25. // purposes of validation, with the synthesis being required only by a proper
  26. // unwinder.
  27. //
  28. // For functions that are large or do not match canonical forms, the data is
  29. // split up into two portions, with the actual data residing in the "exception
  30. // data" table (.xdata) with a reference to the entry from the "procedure data"
  31. // (.pdata) entry.
  32. //
  33. // The exception data contains information about the frame setup, all of the
  34. // epilogue scopes (for functions for which there are multiple exit points) and
  35. // the associated exception handler. Additionally, the entry contains byte-code
  36. // describing how to unwind the function (c.f. Decoder::decodeOpcodes).
  37. //
  38. // +---------------------------------------+
  39. // | Function Entry Address |
  40. // +---------------------------------------+
  41. // | Exception Data Entry Address |
  42. // +---------------------------------------+
  43. //
  44. // This layout is parsed by Decoder::dumpUnpackedEntry. Such an entry must
  45. // first resolve the exception data entry address. This structure
  46. // (ExceptionDataRecord) has a variable sized header
  47. // (c.f. ARM::WinEH::HeaderWords) and encodes most of the same information as
  48. // the packed form. However, because this information is insufficient to
  49. // synthesize the unwinding, there are associated unwinding bytecode which make
  50. // up the bulk of the Decoder.
  51. //
  52. // The decoder itself is table-driven, using the first byte to determine the
  53. // opcode and dispatching to the associated printing routine. The bytecode
  54. // itself is a variable length instruction encoding that can fully describe the
  55. // state of the stack and the necessary operations for unwinding to the
  56. // beginning of the frame.
  57. //
  58. // The byte-code maintains a 1-1 instruction mapping, indicating both the width
  59. // of the instruction (Thumb2 instructions are variable length, 16 or 32 bits
  60. // wide) allowing the program to unwind from any point in the prologue, body, or
  61. // epilogue of the function.
  62. #include "ARMWinEHPrinter.h"
  63. #include "llvm/ADT/STLExtras.h"
  64. #include "llvm/ADT/StringExtras.h"
  65. #include "llvm/Support/ARMWinEH.h"
  66. #include "llvm/Support/Format.h"
  67. using namespace llvm;
  68. using namespace llvm::object;
  69. using namespace llvm::support;
  70. namespace llvm {
  71. raw_ostream &operator<<(raw_ostream &OS, const ARM::WinEH::ReturnType &RT) {
  72. switch (RT) {
  73. case ARM::WinEH::ReturnType::RT_POP:
  74. OS << "pop {pc}";
  75. break;
  76. case ARM::WinEH::ReturnType::RT_B:
  77. OS << "b target";
  78. break;
  79. case ARM::WinEH::ReturnType::RT_BW:
  80. OS << "b.w target";
  81. break;
  82. case ARM::WinEH::ReturnType::RT_NoEpilogue:
  83. OS << "(no epilogue)";
  84. break;
  85. }
  86. return OS;
  87. }
  88. }
  89. static std::string formatSymbol(StringRef Name, uint64_t Address,
  90. uint64_t Offset = 0) {
  91. std::string Buffer;
  92. raw_string_ostream OS(Buffer);
  93. if (!Name.empty())
  94. OS << Name << " ";
  95. if (Offset)
  96. OS << format("+0x%" PRIX64 " (0x%" PRIX64 ")", Offset, Address);
  97. else if (!Name.empty())
  98. OS << format("(0x%" PRIX64 ")", Address);
  99. else
  100. OS << format("0x%" PRIX64, Address);
  101. return OS.str();
  102. }
  103. namespace llvm {
  104. namespace ARM {
  105. namespace WinEH {
  106. const size_t Decoder::PDataEntrySize = sizeof(RuntimeFunction);
  107. // TODO name the uops more appropriately
  108. const Decoder::RingEntry Decoder::Ring[] = {
  109. { 0x80, 0x00, 1, &Decoder::opcode_0xxxxxxx }, // UOP_STACK_FREE (16-bit)
  110. { 0xc0, 0x80, 2, &Decoder::opcode_10Lxxxxx }, // UOP_POP (32-bit)
  111. { 0xf0, 0xc0, 1, &Decoder::opcode_1100xxxx }, // UOP_STACK_SAVE (16-bit)
  112. { 0xf8, 0xd0, 1, &Decoder::opcode_11010Lxx }, // UOP_POP (16-bit)
  113. { 0xf8, 0xd8, 1, &Decoder::opcode_11011Lxx }, // UOP_POP (32-bit)
  114. { 0xf8, 0xe0, 1, &Decoder::opcode_11100xxx }, // UOP_VPOP (32-bit)
  115. { 0xfc, 0xe8, 2, &Decoder::opcode_111010xx }, // UOP_STACK_FREE (32-bit)
  116. { 0xfe, 0xec, 2, &Decoder::opcode_1110110L }, // UOP_POP (16-bit)
  117. { 0xff, 0xee, 2, &Decoder::opcode_11101110 }, // UOP_MICROSOFT_SPECIFIC (16-bit)
  118. // UOP_PUSH_MACHINE_FRAME
  119. // UOP_PUSH_CONTEXT
  120. // UOP_PUSH_TRAP_FRAME
  121. // UOP_REDZONE_RESTORE_LR
  122. { 0xff, 0xef, 2, &Decoder::opcode_11101111 }, // UOP_LDRPC_POSTINC (32-bit)
  123. { 0xff, 0xf5, 2, &Decoder::opcode_11110101 }, // UOP_VPOP (32-bit)
  124. { 0xff, 0xf6, 2, &Decoder::opcode_11110110 }, // UOP_VPOP (32-bit)
  125. { 0xff, 0xf7, 3, &Decoder::opcode_11110111 }, // UOP_STACK_RESTORE (16-bit)
  126. { 0xff, 0xf8, 4, &Decoder::opcode_11111000 }, // UOP_STACK_RESTORE (16-bit)
  127. { 0xff, 0xf9, 3, &Decoder::opcode_11111001 }, // UOP_STACK_RESTORE (32-bit)
  128. { 0xff, 0xfa, 4, &Decoder::opcode_11111010 }, // UOP_STACK_RESTORE (32-bit)
  129. { 0xff, 0xfb, 1, &Decoder::opcode_11111011 }, // UOP_NOP (16-bit)
  130. { 0xff, 0xfc, 1, &Decoder::opcode_11111100 }, // UOP_NOP (32-bit)
  131. { 0xff, 0xfd, 1, &Decoder::opcode_11111101 }, // UOP_NOP (16-bit) / END
  132. { 0xff, 0xfe, 1, &Decoder::opcode_11111110 }, // UOP_NOP (32-bit) / END
  133. { 0xff, 0xff, 1, &Decoder::opcode_11111111 }, // UOP_END
  134. };
  135. // Unwind opcodes for ARM64.
  136. // https://docs.microsoft.com/en-us/cpp/build/arm64-exception-handling
  137. const Decoder::RingEntry Decoder::Ring64[] = {
  138. { 0xe0, 0x00, 1, &Decoder::opcode_alloc_s },
  139. { 0xe0, 0x20, 1, &Decoder::opcode_save_r19r20_x },
  140. { 0xc0, 0x40, 1, &Decoder::opcode_save_fplr },
  141. { 0xc0, 0x80, 1, &Decoder::opcode_save_fplr_x },
  142. { 0xf8, 0xc0, 2, &Decoder::opcode_alloc_m },
  143. { 0xfc, 0xc8, 2, &Decoder::opcode_save_regp },
  144. { 0xfc, 0xcc, 2, &Decoder::opcode_save_regp_x },
  145. { 0xfc, 0xd0, 2, &Decoder::opcode_save_reg },
  146. { 0xfe, 0xd4, 2, &Decoder::opcode_save_reg_x },
  147. { 0xfe, 0xd6, 2, &Decoder::opcode_save_lrpair },
  148. { 0xfe, 0xd8, 2, &Decoder::opcode_save_fregp },
  149. { 0xfe, 0xda, 2, &Decoder::opcode_save_fregp_x },
  150. { 0xfe, 0xdc, 2, &Decoder::opcode_save_freg },
  151. { 0xff, 0xde, 2, &Decoder::opcode_save_freg_x },
  152. { 0xff, 0xe0, 4, &Decoder::opcode_alloc_l },
  153. { 0xff, 0xe1, 1, &Decoder::opcode_setfp },
  154. { 0xff, 0xe2, 2, &Decoder::opcode_addfp },
  155. { 0xff, 0xe3, 1, &Decoder::opcode_nop },
  156. { 0xff, 0xe4, 1, &Decoder::opcode_end },
  157. { 0xff, 0xe5, 1, &Decoder::opcode_end_c },
  158. { 0xff, 0xe6, 1, &Decoder::opcode_save_next },
  159. { 0xff, 0xe8, 1, &Decoder::opcode_trap_frame },
  160. { 0xff, 0xe9, 1, &Decoder::opcode_machine_frame },
  161. { 0xff, 0xea, 1, &Decoder::opcode_context },
  162. { 0xff, 0xec, 1, &Decoder::opcode_clear_unwound_to_call },
  163. };
  164. void Decoder::printRegisters(const std::pair<uint16_t, uint32_t> &RegisterMask) {
  165. static const char * const GPRRegisterNames[16] = {
  166. "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7", "r8", "r9", "r10",
  167. "r11", "ip", "sp", "lr", "pc",
  168. };
  169. const uint16_t GPRMask = std::get<0>(RegisterMask);
  170. const uint16_t VFPMask = std::get<1>(RegisterMask);
  171. OS << '{';
  172. ListSeparator LS;
  173. for (unsigned RI = 0, RE = 11; RI < RE; ++RI)
  174. if (GPRMask & (1 << RI))
  175. OS << LS << GPRRegisterNames[RI];
  176. for (unsigned RI = 0, RE = 32; RI < RE; ++RI)
  177. if (VFPMask & (1 << RI))
  178. OS << LS << "d" << unsigned(RI);
  179. for (unsigned RI = 11, RE = 16; RI < RE; ++RI)
  180. if (GPRMask & (1 << RI))
  181. OS << LS << GPRRegisterNames[RI];
  182. OS << '}';
  183. }
  184. ErrorOr<object::SectionRef>
  185. Decoder::getSectionContaining(const COFFObjectFile &COFF, uint64_t VA) {
  186. for (const auto &Section : COFF.sections()) {
  187. uint64_t Address = Section.getAddress();
  188. uint64_t Size = Section.getSize();
  189. if (VA >= Address && (VA - Address) <= Size)
  190. return Section;
  191. }
  192. return inconvertibleErrorCode();
  193. }
  194. ErrorOr<object::SymbolRef> Decoder::getSymbol(const COFFObjectFile &COFF,
  195. uint64_t VA, bool FunctionOnly) {
  196. for (const auto &Symbol : COFF.symbols()) {
  197. Expected<SymbolRef::Type> Type = Symbol.getType();
  198. if (!Type)
  199. return errorToErrorCode(Type.takeError());
  200. if (FunctionOnly && *Type != SymbolRef::ST_Function)
  201. continue;
  202. Expected<uint64_t> Address = Symbol.getAddress();
  203. if (!Address)
  204. return errorToErrorCode(Address.takeError());
  205. if (*Address == VA)
  206. return Symbol;
  207. }
  208. return inconvertibleErrorCode();
  209. }
  210. ErrorOr<SymbolRef> Decoder::getRelocatedSymbol(const COFFObjectFile &,
  211. const SectionRef &Section,
  212. uint64_t Offset) {
  213. for (const auto &Relocation : Section.relocations()) {
  214. uint64_t RelocationOffset = Relocation.getOffset();
  215. if (RelocationOffset == Offset)
  216. return *Relocation.getSymbol();
  217. }
  218. return inconvertibleErrorCode();
  219. }
  220. SymbolRef Decoder::getPreferredSymbol(const COFFObjectFile &COFF, SymbolRef Sym,
  221. uint64_t &SymbolOffset) {
  222. // The symbol resolved by getRelocatedSymbol can be any internal
  223. // nondescriptive symbol; try to resolve a more descriptive one.
  224. COFFSymbolRef CoffSym = COFF.getCOFFSymbol(Sym);
  225. if (CoffSym.getStorageClass() != COFF::IMAGE_SYM_CLASS_LABEL &&
  226. CoffSym.getSectionDefinition() == nullptr)
  227. return Sym;
  228. for (const auto &S : COFF.symbols()) {
  229. COFFSymbolRef CS = COFF.getCOFFSymbol(S);
  230. if (CS.getSectionNumber() == CoffSym.getSectionNumber() &&
  231. CS.getValue() <= CoffSym.getValue() + SymbolOffset &&
  232. CS.getStorageClass() != COFF::IMAGE_SYM_CLASS_LABEL &&
  233. CS.getSectionDefinition() == nullptr) {
  234. uint32_t Offset = CoffSym.getValue() + SymbolOffset - CS.getValue();
  235. if (Offset <= SymbolOffset) {
  236. SymbolOffset = Offset;
  237. Sym = S;
  238. CoffSym = CS;
  239. if (CS.isExternal() && SymbolOffset == 0)
  240. return Sym;
  241. }
  242. }
  243. }
  244. return Sym;
  245. }
  246. ErrorOr<SymbolRef> Decoder::getSymbolForLocation(
  247. const COFFObjectFile &COFF, const SectionRef &Section,
  248. uint64_t OffsetInSection, uint64_t ImmediateOffset, uint64_t &SymbolAddress,
  249. uint64_t &SymbolOffset, bool FunctionOnly) {
  250. // Try to locate a relocation that points at the offset in the section
  251. ErrorOr<SymbolRef> SymOrErr =
  252. getRelocatedSymbol(COFF, Section, OffsetInSection);
  253. if (SymOrErr) {
  254. // We found a relocation symbol; the immediate offset needs to be added
  255. // to the symbol address.
  256. SymbolOffset = ImmediateOffset;
  257. Expected<uint64_t> AddressOrErr = SymOrErr->getAddress();
  258. if (!AddressOrErr) {
  259. std::string Buf;
  260. llvm::raw_string_ostream OS(Buf);
  261. logAllUnhandledErrors(AddressOrErr.takeError(), OS);
  262. report_fatal_error(Twine(OS.str()));
  263. }
  264. // We apply SymbolOffset here directly. We return it separately to allow
  265. // the caller to print it as an offset on the symbol name.
  266. SymbolAddress = *AddressOrErr + SymbolOffset;
  267. if (FunctionOnly) // Resolve label/section symbols into function names.
  268. SymOrErr = getPreferredSymbol(COFF, *SymOrErr, SymbolOffset);
  269. } else {
  270. // No matching relocation found; operating on a linked image. Try to
  271. // find a descriptive symbol if possible. The immediate offset contains
  272. // the image relative address, and we shouldn't add any offset to the
  273. // symbol.
  274. SymbolAddress = COFF.getImageBase() + ImmediateOffset;
  275. SymbolOffset = 0;
  276. SymOrErr = getSymbol(COFF, SymbolAddress, FunctionOnly);
  277. }
  278. return SymOrErr;
  279. }
  280. bool Decoder::opcode_0xxxxxxx(const uint8_t *OC, unsigned &Offset,
  281. unsigned Length, bool Prologue) {
  282. uint8_t Imm = OC[Offset] & 0x7f;
  283. SW.startLine() << format("0x%02x ; %s sp, #(%u * 4)\n",
  284. OC[Offset],
  285. static_cast<const char *>(Prologue ? "sub" : "add"),
  286. Imm);
  287. ++Offset;
  288. return false;
  289. }
  290. bool Decoder::opcode_10Lxxxxx(const uint8_t *OC, unsigned &Offset,
  291. unsigned Length, bool Prologue) {
  292. unsigned Link = (OC[Offset] & 0x20) >> 5;
  293. uint16_t RegisterMask = (Link << (Prologue ? 14 : 15))
  294. | ((OC[Offset + 0] & 0x1f) << 8)
  295. | ((OC[Offset + 1] & 0xff) << 0);
  296. assert((~RegisterMask & (1 << 13)) && "sp must not be set");
  297. assert((~RegisterMask & (1 << (Prologue ? 15 : 14))) && "pc must not be set");
  298. SW.startLine() << format("0x%02x 0x%02x ; %s.w ",
  299. OC[Offset + 0], OC[Offset + 1],
  300. Prologue ? "push" : "pop");
  301. printRegisters(std::make_pair(RegisterMask, 0));
  302. OS << '\n';
  303. Offset += 2;
  304. return false;
  305. }
  306. bool Decoder::opcode_1100xxxx(const uint8_t *OC, unsigned &Offset,
  307. unsigned Length, bool Prologue) {
  308. if (Prologue)
  309. SW.startLine() << format("0x%02x ; mov r%u, sp\n",
  310. OC[Offset], OC[Offset] & 0xf);
  311. else
  312. SW.startLine() << format("0x%02x ; mov sp, r%u\n",
  313. OC[Offset], OC[Offset] & 0xf);
  314. ++Offset;
  315. return false;
  316. }
  317. bool Decoder::opcode_11010Lxx(const uint8_t *OC, unsigned &Offset,
  318. unsigned Length, bool Prologue) {
  319. unsigned Link = (OC[Offset] & 0x4) >> 3;
  320. unsigned Count = (OC[Offset] & 0x3);
  321. uint16_t GPRMask = (Link << (Prologue ? 14 : 15))
  322. | (((1 << (Count + 1)) - 1) << 4);
  323. SW.startLine() << format("0x%02x ; %s ", OC[Offset],
  324. Prologue ? "push" : "pop");
  325. printRegisters(std::make_pair(GPRMask, 0));
  326. OS << '\n';
  327. ++Offset;
  328. return false;
  329. }
  330. bool Decoder::opcode_11011Lxx(const uint8_t *OC, unsigned &Offset,
  331. unsigned Length, bool Prologue) {
  332. unsigned Link = (OC[Offset] & 0x4) >> 2;
  333. unsigned Count = (OC[Offset] & 0x3) + 4;
  334. uint16_t GPRMask = (Link << (Prologue ? 14 : 15))
  335. | (((1 << (Count + 1)) - 1) << 4);
  336. SW.startLine() << format("0x%02x ; %s.w ", OC[Offset],
  337. Prologue ? "push" : "pop");
  338. printRegisters(std::make_pair(GPRMask, 0));
  339. OS << '\n';
  340. ++Offset;
  341. return false;
  342. }
  343. bool Decoder::opcode_11100xxx(const uint8_t *OC, unsigned &Offset,
  344. unsigned Length, bool Prologue) {
  345. unsigned High = (OC[Offset] & 0x7);
  346. uint32_t VFPMask = (((1 << (High + 1)) - 1) << 8);
  347. SW.startLine() << format("0x%02x ; %s ", OC[Offset],
  348. Prologue ? "vpush" : "vpop");
  349. printRegisters(std::make_pair(0, VFPMask));
  350. OS << '\n';
  351. ++Offset;
  352. return false;
  353. }
  354. bool Decoder::opcode_111010xx(const uint8_t *OC, unsigned &Offset,
  355. unsigned Length, bool Prologue) {
  356. uint16_t Imm = ((OC[Offset + 0] & 0x03) << 8) | ((OC[Offset + 1] & 0xff) << 0);
  357. SW.startLine() << format("0x%02x 0x%02x ; %s.w sp, #(%u * 4)\n",
  358. OC[Offset + 0], OC[Offset + 1],
  359. static_cast<const char *>(Prologue ? "sub" : "add"),
  360. Imm);
  361. Offset += 2;
  362. return false;
  363. }
  364. bool Decoder::opcode_1110110L(const uint8_t *OC, unsigned &Offset,
  365. unsigned Length, bool Prologue) {
  366. uint8_t GPRMask = ((OC[Offset + 0] & 0x01) << (Prologue ? 14 : 15))
  367. | ((OC[Offset + 1] & 0xff) << 0);
  368. SW.startLine() << format("0x%02x 0x%02x ; %s ", OC[Offset + 0],
  369. OC[Offset + 1], Prologue ? "push" : "pop");
  370. printRegisters(std::make_pair(GPRMask, 0));
  371. OS << '\n';
  372. Offset += 2;
  373. return false;
  374. }
  375. bool Decoder::opcode_11101110(const uint8_t *OC, unsigned &Offset,
  376. unsigned Length, bool Prologue) {
  377. assert(!Prologue && "may not be used in prologue");
  378. if (OC[Offset + 1] & 0xf0)
  379. SW.startLine() << format("0x%02x 0x%02x ; reserved\n",
  380. OC[Offset + 0], OC[Offset + 1]);
  381. else
  382. SW.startLine()
  383. << format("0x%02x 0x%02x ; microsoft-specific (type: %u)\n",
  384. OC[Offset + 0], OC[Offset + 1], OC[Offset + 1] & 0x0f);
  385. Offset += 2;
  386. return false;
  387. }
  388. bool Decoder::opcode_11101111(const uint8_t *OC, unsigned &Offset,
  389. unsigned Length, bool Prologue) {
  390. assert(!Prologue && "may not be used in prologue");
  391. if (OC[Offset + 1] & 0xf0)
  392. SW.startLine() << format("0x%02x 0x%02x ; reserved\n",
  393. OC[Offset + 0], OC[Offset + 1]);
  394. else
  395. SW.startLine()
  396. << format("0x%02x 0x%02x ; ldr.w lr, [sp], #%u\n",
  397. OC[Offset + 0], OC[Offset + 1], OC[Offset + 1] << 2);
  398. Offset += 2;
  399. return false;
  400. }
  401. bool Decoder::opcode_11110101(const uint8_t *OC, unsigned &Offset,
  402. unsigned Length, bool Prologue) {
  403. unsigned Start = (OC[Offset + 1] & 0xf0) >> 4;
  404. unsigned End = (OC[Offset + 1] & 0x0f) >> 0;
  405. uint32_t VFPMask = ((1 << (End - Start)) - 1) << Start;
  406. SW.startLine() << format("0x%02x 0x%02x ; %s ", OC[Offset + 0],
  407. OC[Offset + 1], Prologue ? "vpush" : "vpop");
  408. printRegisters(std::make_pair(0, VFPMask));
  409. OS << '\n';
  410. Offset += 2;
  411. return false;
  412. }
  413. bool Decoder::opcode_11110110(const uint8_t *OC, unsigned &Offset,
  414. unsigned Length, bool Prologue) {
  415. unsigned Start = (OC[Offset + 1] & 0xf0) >> 4;
  416. unsigned End = (OC[Offset + 1] & 0x0f) >> 0;
  417. uint32_t VFPMask = ((1 << (End - Start)) - 1) << 16;
  418. SW.startLine() << format("0x%02x 0x%02x ; %s ", OC[Offset + 0],
  419. OC[Offset + 1], Prologue ? "vpush" : "vpop");
  420. printRegisters(std::make_pair(0, VFPMask));
  421. OS << '\n';
  422. Offset += 2;
  423. return false;
  424. }
  425. bool Decoder::opcode_11110111(const uint8_t *OC, unsigned &Offset,
  426. unsigned Length, bool Prologue) {
  427. uint32_t Imm = (OC[Offset + 1] << 8) | (OC[Offset + 2] << 0);
  428. SW.startLine() << format("0x%02x 0x%02x 0x%02x ; %s sp, sp, #(%u * 4)\n",
  429. OC[Offset + 0], OC[Offset + 1], OC[Offset + 2],
  430. static_cast<const char *>(Prologue ? "sub" : "add"),
  431. Imm);
  432. Offset += 3;
  433. return false;
  434. }
  435. bool Decoder::opcode_11111000(const uint8_t *OC, unsigned &Offset,
  436. unsigned Length, bool Prologue) {
  437. uint32_t Imm = (OC[Offset + 1] << 16)
  438. | (OC[Offset + 2] << 8)
  439. | (OC[Offset + 3] << 0);
  440. SW.startLine()
  441. << format("0x%02x 0x%02x 0x%02x 0x%02x ; %s sp, sp, #(%u * 4)\n",
  442. OC[Offset + 0], OC[Offset + 1], OC[Offset + 2], OC[Offset + 3],
  443. static_cast<const char *>(Prologue ? "sub" : "add"), Imm);
  444. Offset += 4;
  445. return false;
  446. }
  447. bool Decoder::opcode_11111001(const uint8_t *OC, unsigned &Offset,
  448. unsigned Length, bool Prologue) {
  449. uint32_t Imm = (OC[Offset + 1] << 8) | (OC[Offset + 2] << 0);
  450. SW.startLine()
  451. << format("0x%02x 0x%02x 0x%02x ; %s.w sp, sp, #(%u * 4)\n",
  452. OC[Offset + 0], OC[Offset + 1], OC[Offset + 2],
  453. static_cast<const char *>(Prologue ? "sub" : "add"), Imm);
  454. Offset += 3;
  455. return false;
  456. }
  457. bool Decoder::opcode_11111010(const uint8_t *OC, unsigned &Offset,
  458. unsigned Length, bool Prologue) {
  459. uint32_t Imm = (OC[Offset + 1] << 16)
  460. | (OC[Offset + 2] << 8)
  461. | (OC[Offset + 3] << 0);
  462. SW.startLine()
  463. << format("0x%02x 0x%02x 0x%02x 0x%02x ; %s.w sp, sp, #(%u * 4)\n",
  464. OC[Offset + 0], OC[Offset + 1], OC[Offset + 2], OC[Offset + 3],
  465. static_cast<const char *>(Prologue ? "sub" : "add"), Imm);
  466. Offset += 4;
  467. return false;
  468. }
  469. bool Decoder::opcode_11111011(const uint8_t *OC, unsigned &Offset,
  470. unsigned Length, bool Prologue) {
  471. SW.startLine() << format("0x%02x ; nop\n", OC[Offset]);
  472. ++Offset;
  473. return false;
  474. }
  475. bool Decoder::opcode_11111100(const uint8_t *OC, unsigned &Offset,
  476. unsigned Length, bool Prologue) {
  477. SW.startLine() << format("0x%02x ; nop.w\n", OC[Offset]);
  478. ++Offset;
  479. return false;
  480. }
  481. bool Decoder::opcode_11111101(const uint8_t *OC, unsigned &Offset,
  482. unsigned Length, bool Prologue) {
  483. SW.startLine() << format("0x%02x ; b\n", OC[Offset]);
  484. ++Offset;
  485. return true;
  486. }
  487. bool Decoder::opcode_11111110(const uint8_t *OC, unsigned &Offset,
  488. unsigned Length, bool Prologue) {
  489. SW.startLine() << format("0x%02x ; b.w\n", OC[Offset]);
  490. ++Offset;
  491. return true;
  492. }
  493. bool Decoder::opcode_11111111(const uint8_t *OC, unsigned &Offset,
  494. unsigned Length, bool Prologue) {
  495. ++Offset;
  496. return true;
  497. }
  498. // ARM64 unwind codes start here.
  499. bool Decoder::opcode_alloc_s(const uint8_t *OC, unsigned &Offset,
  500. unsigned Length, bool Prologue) {
  501. uint32_t NumBytes = (OC[Offset] & 0x1F) << 4;
  502. SW.startLine() << format("0x%02x ; %s sp, #%u\n", OC[Offset],
  503. static_cast<const char *>(Prologue ? "sub" : "add"),
  504. NumBytes);
  505. ++Offset;
  506. return false;
  507. }
  508. bool Decoder::opcode_save_r19r20_x(const uint8_t *OC, unsigned &Offset,
  509. unsigned Length, bool Prologue) {
  510. uint32_t Off = (OC[Offset] & 0x1F) << 3;
  511. if (Prologue)
  512. SW.startLine() << format(
  513. "0x%02x ; stp x19, x20, [sp, #-%u]!\n", OC[Offset], Off);
  514. else
  515. SW.startLine() << format(
  516. "0x%02x ; ldp x19, x20, [sp], #%u\n", OC[Offset], Off);
  517. ++Offset;
  518. return false;
  519. }
  520. bool Decoder::opcode_save_fplr(const uint8_t *OC, unsigned &Offset,
  521. unsigned Length, bool Prologue) {
  522. uint32_t Off = (OC[Offset] & 0x3F) << 3;
  523. SW.startLine() << format(
  524. "0x%02x ; %s x29, x30, [sp, #%u]\n", OC[Offset],
  525. static_cast<const char *>(Prologue ? "stp" : "ldp"), Off);
  526. ++Offset;
  527. return false;
  528. }
  529. bool Decoder::opcode_save_fplr_x(const uint8_t *OC, unsigned &Offset,
  530. unsigned Length, bool Prologue) {
  531. uint32_t Off = ((OC[Offset] & 0x3F) + 1) << 3;
  532. if (Prologue)
  533. SW.startLine() << format(
  534. "0x%02x ; stp x29, x30, [sp, #-%u]!\n", OC[Offset], Off);
  535. else
  536. SW.startLine() << format(
  537. "0x%02x ; ldp x29, x30, [sp], #%u\n", OC[Offset], Off);
  538. ++Offset;
  539. return false;
  540. }
  541. bool Decoder::opcode_alloc_m(const uint8_t *OC, unsigned &Offset,
  542. unsigned Length, bool Prologue) {
  543. uint32_t NumBytes = ((OC[Offset] & 0x07) << 8);
  544. NumBytes |= (OC[Offset + 1] & 0xFF);
  545. NumBytes <<= 4;
  546. SW.startLine() << format("0x%02x%02x ; %s sp, #%u\n",
  547. OC[Offset], OC[Offset + 1],
  548. static_cast<const char *>(Prologue ? "sub" : "add"),
  549. NumBytes);
  550. Offset += 2;
  551. return false;
  552. }
  553. bool Decoder::opcode_save_regp(const uint8_t *OC, unsigned &Offset,
  554. unsigned Length, bool Prologue) {
  555. uint32_t Reg = ((OC[Offset] & 0x03) << 8);
  556. Reg |= (OC[Offset + 1] & 0xC0);
  557. Reg >>= 6;
  558. Reg += 19;
  559. uint32_t Off = (OC[Offset + 1] & 0x3F) << 3;
  560. SW.startLine() << format(
  561. "0x%02x%02x ; %s x%u, x%u, [sp, #%u]\n",
  562. OC[Offset], OC[Offset + 1],
  563. static_cast<const char *>(Prologue ? "stp" : "ldp"), Reg, Reg + 1, Off);
  564. Offset += 2;
  565. return false;
  566. }
  567. bool Decoder::opcode_save_regp_x(const uint8_t *OC, unsigned &Offset,
  568. unsigned Length, bool Prologue) {
  569. uint32_t Reg = ((OC[Offset] & 0x03) << 8);
  570. Reg |= (OC[Offset + 1] & 0xC0);
  571. Reg >>= 6;
  572. Reg += 19;
  573. uint32_t Off = ((OC[Offset + 1] & 0x3F) + 1) << 3;
  574. if (Prologue)
  575. SW.startLine() << format(
  576. "0x%02x%02x ; stp x%u, x%u, [sp, #-%u]!\n",
  577. OC[Offset], OC[Offset + 1], Reg,
  578. Reg + 1, Off);
  579. else
  580. SW.startLine() << format(
  581. "0x%02x%02x ; ldp x%u, x%u, [sp], #%u\n",
  582. OC[Offset], OC[Offset + 1], Reg,
  583. Reg + 1, Off);
  584. Offset += 2;
  585. return false;
  586. }
  587. bool Decoder::opcode_save_reg(const uint8_t *OC, unsigned &Offset,
  588. unsigned Length, bool Prologue) {
  589. uint32_t Reg = (OC[Offset] & 0x03) << 8;
  590. Reg |= (OC[Offset + 1] & 0xC0);
  591. Reg >>= 6;
  592. Reg += 19;
  593. uint32_t Off = (OC[Offset + 1] & 0x3F) << 3;
  594. SW.startLine() << format("0x%02x%02x ; %s x%u, [sp, #%u]\n",
  595. OC[Offset], OC[Offset + 1],
  596. static_cast<const char *>(Prologue ? "str" : "ldr"),
  597. Reg, Off);
  598. Offset += 2;
  599. return false;
  600. }
  601. bool Decoder::opcode_save_reg_x(const uint8_t *OC, unsigned &Offset,
  602. unsigned Length, bool Prologue) {
  603. uint32_t Reg = (OC[Offset] & 0x01) << 8;
  604. Reg |= (OC[Offset + 1] & 0xE0);
  605. Reg >>= 5;
  606. Reg += 19;
  607. uint32_t Off = ((OC[Offset + 1] & 0x1F) + 1) << 3;
  608. if (Prologue)
  609. SW.startLine() << format("0x%02x%02x ; str x%u, [sp, #-%u]!\n",
  610. OC[Offset], OC[Offset + 1], Reg, Off);
  611. else
  612. SW.startLine() << format("0x%02x%02x ; ldr x%u, [sp], #%u\n",
  613. OC[Offset], OC[Offset + 1], Reg, Off);
  614. Offset += 2;
  615. return false;
  616. }
  617. bool Decoder::opcode_save_lrpair(const uint8_t *OC, unsigned &Offset,
  618. unsigned Length, bool Prologue) {
  619. uint32_t Reg = (OC[Offset] & 0x01) << 8;
  620. Reg |= (OC[Offset + 1] & 0xC0);
  621. Reg >>= 6;
  622. Reg *= 2;
  623. Reg += 19;
  624. uint32_t Off = (OC[Offset + 1] & 0x3F) << 3;
  625. SW.startLine() << format("0x%02x%02x ; %s x%u, lr, [sp, #%u]\n",
  626. OC[Offset], OC[Offset + 1],
  627. static_cast<const char *>(Prologue ? "stp" : "ldp"),
  628. Reg, Off);
  629. Offset += 2;
  630. return false;
  631. }
  632. bool Decoder::opcode_save_fregp(const uint8_t *OC, unsigned &Offset,
  633. unsigned Length, bool Prologue) {
  634. uint32_t Reg = (OC[Offset] & 0x01) << 8;
  635. Reg |= (OC[Offset + 1] & 0xC0);
  636. Reg >>= 6;
  637. Reg += 8;
  638. uint32_t Off = (OC[Offset + 1] & 0x3F) << 3;
  639. SW.startLine() << format("0x%02x%02x ; %s d%u, d%u, [sp, #%u]\n",
  640. OC[Offset], OC[Offset + 1],
  641. static_cast<const char *>(Prologue ? "stp" : "ldp"),
  642. Reg, Reg + 1, Off);
  643. Offset += 2;
  644. return false;
  645. }
  646. bool Decoder::opcode_save_fregp_x(const uint8_t *OC, unsigned &Offset,
  647. unsigned Length, bool Prologue) {
  648. uint32_t Reg = (OC[Offset] & 0x01) << 8;
  649. Reg |= (OC[Offset + 1] & 0xC0);
  650. Reg >>= 6;
  651. Reg += 8;
  652. uint32_t Off = ((OC[Offset + 1] & 0x3F) + 1) << 3;
  653. if (Prologue)
  654. SW.startLine() << format(
  655. "0x%02x%02x ; stp d%u, d%u, [sp, #-%u]!\n", OC[Offset],
  656. OC[Offset + 1], Reg, Reg + 1, Off);
  657. else
  658. SW.startLine() << format(
  659. "0x%02x%02x ; ldp d%u, d%u, [sp], #%u\n", OC[Offset],
  660. OC[Offset + 1], Reg, Reg + 1, Off);
  661. Offset += 2;
  662. return false;
  663. }
  664. bool Decoder::opcode_save_freg(const uint8_t *OC, unsigned &Offset,
  665. unsigned Length, bool Prologue) {
  666. uint32_t Reg = (OC[Offset] & 0x01) << 8;
  667. Reg |= (OC[Offset + 1] & 0xC0);
  668. Reg >>= 6;
  669. Reg += 8;
  670. uint32_t Off = (OC[Offset + 1] & 0x3F) << 3;
  671. SW.startLine() << format("0x%02x%02x ; %s d%u, [sp, #%u]\n",
  672. OC[Offset], OC[Offset + 1],
  673. static_cast<const char *>(Prologue ? "str" : "ldr"),
  674. Reg, Off);
  675. Offset += 2;
  676. return false;
  677. }
  678. bool Decoder::opcode_save_freg_x(const uint8_t *OC, unsigned &Offset,
  679. unsigned Length, bool Prologue) {
  680. uint32_t Reg = ((OC[Offset + 1] & 0xE0) >> 5) + 8;
  681. uint32_t Off = ((OC[Offset + 1] & 0x1F) + 1) << 3;
  682. if (Prologue)
  683. SW.startLine() << format(
  684. "0x%02x%02x ; str d%u, [sp, #-%u]!\n", OC[Offset],
  685. OC[Offset + 1], Reg, Off);
  686. else
  687. SW.startLine() << format(
  688. "0x%02x%02x ; ldr d%u, [sp], #%u\n", OC[Offset],
  689. OC[Offset + 1], Reg, Off);
  690. Offset += 2;
  691. return false;
  692. }
  693. bool Decoder::opcode_alloc_l(const uint8_t *OC, unsigned &Offset,
  694. unsigned Length, bool Prologue) {
  695. unsigned Off =
  696. (OC[Offset + 1] << 16) | (OC[Offset + 2] << 8) | (OC[Offset + 3] << 0);
  697. Off <<= 4;
  698. SW.startLine() << format(
  699. "0x%02x%02x%02x%02x ; %s sp, #%u\n", OC[Offset], OC[Offset + 1],
  700. OC[Offset + 2], OC[Offset + 3],
  701. static_cast<const char *>(Prologue ? "sub" : "add"), Off);
  702. Offset += 4;
  703. return false;
  704. }
  705. bool Decoder::opcode_setfp(const uint8_t *OC, unsigned &Offset, unsigned Length,
  706. bool Prologue) {
  707. SW.startLine() << format("0x%02x ; mov %s, %s\n", OC[Offset],
  708. static_cast<const char *>(Prologue ? "fp" : "sp"),
  709. static_cast<const char *>(Prologue ? "sp" : "fp"));
  710. ++Offset;
  711. return false;
  712. }
  713. bool Decoder::opcode_addfp(const uint8_t *OC, unsigned &Offset, unsigned Length,
  714. bool Prologue) {
  715. unsigned NumBytes = OC[Offset + 1] << 3;
  716. SW.startLine() << format(
  717. "0x%02x%02x ; %s %s, %s, #%u\n", OC[Offset], OC[Offset + 1],
  718. static_cast<const char *>(Prologue ? "add" : "sub"),
  719. static_cast<const char *>(Prologue ? "fp" : "sp"),
  720. static_cast<const char *>(Prologue ? "sp" : "fp"), NumBytes);
  721. Offset += 2;
  722. return false;
  723. }
  724. bool Decoder::opcode_nop(const uint8_t *OC, unsigned &Offset, unsigned Length,
  725. bool Prologue) {
  726. SW.startLine() << format("0x%02x ; nop\n", OC[Offset]);
  727. ++Offset;
  728. return false;
  729. }
  730. bool Decoder::opcode_end(const uint8_t *OC, unsigned &Offset, unsigned Length,
  731. bool Prologue) {
  732. SW.startLine() << format("0x%02x ; end\n", OC[Offset]);
  733. ++Offset;
  734. return true;
  735. }
  736. bool Decoder::opcode_end_c(const uint8_t *OC, unsigned &Offset, unsigned Length,
  737. bool Prologue) {
  738. SW.startLine() << format("0x%02x ; end_c\n", OC[Offset]);
  739. ++Offset;
  740. return true;
  741. }
  742. bool Decoder::opcode_save_next(const uint8_t *OC, unsigned &Offset,
  743. unsigned Length, bool Prologue) {
  744. if (Prologue)
  745. SW.startLine() << format("0x%02x ; save next\n", OC[Offset]);
  746. else
  747. SW.startLine() << format("0x%02x ; restore next\n",
  748. OC[Offset]);
  749. ++Offset;
  750. return false;
  751. }
  752. bool Decoder::opcode_trap_frame(const uint8_t *OC, unsigned &Offset,
  753. unsigned Length, bool Prologue) {
  754. SW.startLine() << format("0x%02x ; trap frame\n", OC[Offset]);
  755. ++Offset;
  756. return false;
  757. }
  758. bool Decoder::opcode_machine_frame(const uint8_t *OC, unsigned &Offset,
  759. unsigned Length, bool Prologue) {
  760. SW.startLine() << format("0x%02x ; machine frame\n",
  761. OC[Offset]);
  762. ++Offset;
  763. return false;
  764. }
  765. bool Decoder::opcode_context(const uint8_t *OC, unsigned &Offset,
  766. unsigned Length, bool Prologue) {
  767. SW.startLine() << format("0x%02x ; context\n", OC[Offset]);
  768. ++Offset;
  769. return false;
  770. }
  771. bool Decoder::opcode_clear_unwound_to_call(const uint8_t *OC, unsigned &Offset,
  772. unsigned Length, bool Prologue) {
  773. SW.startLine() << format("0x%02x ; clear unwound to call\n",
  774. OC[Offset]);
  775. ++Offset;
  776. return false;
  777. }
  778. void Decoder::decodeOpcodes(ArrayRef<uint8_t> Opcodes, unsigned Offset,
  779. bool Prologue) {
  780. assert((!Prologue || Offset == 0) && "prologue should always use offset 0");
  781. const RingEntry* DecodeRing = isAArch64 ? Ring64 : Ring;
  782. bool Terminated = false;
  783. for (unsigned OI = Offset, OE = Opcodes.size(); !Terminated && OI < OE; ) {
  784. for (unsigned DI = 0;; ++DI) {
  785. if ((isAArch64 && (DI >= array_lengthof(Ring64))) ||
  786. (!isAArch64 && (DI >= array_lengthof(Ring)))) {
  787. SW.startLine() << format("0x%02x ; Bad opcode!\n",
  788. Opcodes.data()[OI]);
  789. ++OI;
  790. break;
  791. }
  792. if ((Opcodes[OI] & DecodeRing[DI].Mask) == DecodeRing[DI].Value) {
  793. if (OI + DecodeRing[DI].Length > OE) {
  794. SW.startLine() << format("Opcode 0x%02x goes past the unwind data\n",
  795. Opcodes[OI]);
  796. OI += DecodeRing[DI].Length;
  797. break;
  798. }
  799. Terminated =
  800. (this->*DecodeRing[DI].Routine)(Opcodes.data(), OI, 0, Prologue);
  801. break;
  802. }
  803. }
  804. }
  805. }
  806. bool Decoder::dumpXDataRecord(const COFFObjectFile &COFF,
  807. const SectionRef &Section,
  808. uint64_t FunctionAddress, uint64_t VA) {
  809. ArrayRef<uint8_t> Contents;
  810. if (COFF.getSectionContents(COFF.getCOFFSection(Section), Contents))
  811. return false;
  812. uint64_t SectionVA = Section.getAddress();
  813. uint64_t Offset = VA - SectionVA;
  814. const ulittle32_t *Data =
  815. reinterpret_cast<const ulittle32_t *>(Contents.data() + Offset);
  816. // Sanity check to ensure that the .xdata header is present.
  817. // A header is one or two words, followed by at least one word to describe
  818. // the unwind codes. Applicable to both ARM and AArch64.
  819. if (Contents.size() - Offset < 8)
  820. report_fatal_error(".xdata must be at least 8 bytes in size");
  821. const ExceptionDataRecord XData(Data, isAArch64);
  822. DictScope XRS(SW, "ExceptionData");
  823. SW.printNumber("FunctionLength",
  824. isAArch64 ? XData.FunctionLengthInBytesAArch64() :
  825. XData.FunctionLengthInBytesARM());
  826. SW.printNumber("Version", XData.Vers());
  827. SW.printBoolean("ExceptionData", XData.X());
  828. SW.printBoolean("EpiloguePacked", XData.E());
  829. if (!isAArch64)
  830. SW.printBoolean("Fragment", XData.F());
  831. SW.printNumber(XData.E() ? "EpilogueOffset" : "EpilogueScopes",
  832. XData.EpilogueCount());
  833. uint64_t ByteCodeLength = XData.CodeWords() * sizeof(uint32_t);
  834. SW.printNumber("ByteCodeLength", ByteCodeLength);
  835. if ((int64_t)(Contents.size() - Offset - 4 * HeaderWords(XData) -
  836. (XData.E() ? 0 : XData.EpilogueCount() * 4) -
  837. (XData.X() ? 8 : 0)) < (int64_t)ByteCodeLength) {
  838. SW.flush();
  839. report_fatal_error("Malformed unwind data");
  840. }
  841. if (XData.E()) {
  842. ArrayRef<uint8_t> UC = XData.UnwindByteCode();
  843. if (isAArch64 || !XData.F()) {
  844. ListScope PS(SW, "Prologue");
  845. decodeOpcodes(UC, 0, /*Prologue=*/true);
  846. }
  847. if (XData.EpilogueCount()) {
  848. ListScope ES(SW, "Epilogue");
  849. decodeOpcodes(UC, XData.EpilogueCount(), /*Prologue=*/false);
  850. }
  851. } else {
  852. {
  853. ListScope PS(SW, "Prologue");
  854. decodeOpcodes(XData.UnwindByteCode(), 0, /*Prologue=*/true);
  855. }
  856. ArrayRef<ulittle32_t> EpilogueScopes = XData.EpilogueScopes();
  857. ListScope ESS(SW, "EpilogueScopes");
  858. for (const EpilogueScope ES : EpilogueScopes) {
  859. DictScope ESES(SW, "EpilogueScope");
  860. SW.printNumber("StartOffset", ES.EpilogueStartOffset());
  861. if (!isAArch64)
  862. SW.printNumber("Condition", ES.Condition());
  863. SW.printNumber("EpilogueStartIndex",
  864. isAArch64 ? ES.EpilogueStartIndexAArch64()
  865. : ES.EpilogueStartIndexARM());
  866. if (ES.ES & ~0xffc3ffff)
  867. SW.printNumber("ReservedBits", (ES.ES >> 18) & 0xF);
  868. ListScope Opcodes(SW, "Opcodes");
  869. decodeOpcodes(XData.UnwindByteCode(),
  870. isAArch64 ? ES.EpilogueStartIndexAArch64()
  871. : ES.EpilogueStartIndexARM(),
  872. /*Prologue=*/false);
  873. }
  874. }
  875. if (XData.X()) {
  876. const uint32_t Parameter = XData.ExceptionHandlerParameter();
  877. const size_t HandlerOffset = HeaderWords(XData) +
  878. (XData.E() ? 0 : XData.EpilogueCount()) +
  879. XData.CodeWords();
  880. uint64_t Address, SymbolOffset;
  881. ErrorOr<SymbolRef> Symbol = getSymbolForLocation(
  882. COFF, Section, Offset + HandlerOffset * sizeof(uint32_t),
  883. XData.ExceptionHandlerRVA(), Address, SymbolOffset,
  884. /*FunctionOnly=*/true);
  885. if (!Symbol) {
  886. ListScope EHS(SW, "ExceptionHandler");
  887. SW.printHex("Routine", Address);
  888. SW.printHex("Parameter", Parameter);
  889. return true;
  890. }
  891. Expected<StringRef> Name = Symbol->getName();
  892. if (!Name) {
  893. std::string Buf;
  894. llvm::raw_string_ostream OS(Buf);
  895. logAllUnhandledErrors(Name.takeError(), OS);
  896. report_fatal_error(Twine(OS.str()));
  897. }
  898. ListScope EHS(SW, "ExceptionHandler");
  899. SW.printString("Routine", formatSymbol(*Name, Address, SymbolOffset));
  900. SW.printHex("Parameter", Parameter);
  901. }
  902. return true;
  903. }
  904. bool Decoder::dumpUnpackedEntry(const COFFObjectFile &COFF,
  905. const SectionRef Section, uint64_t Offset,
  906. unsigned Index, const RuntimeFunction &RF) {
  907. assert(RF.Flag() == RuntimeFunctionFlag::RFF_Unpacked &&
  908. "packed entry cannot be treated as an unpacked entry");
  909. uint64_t FunctionAddress, FunctionOffset;
  910. ErrorOr<SymbolRef> Function = getSymbolForLocation(
  911. COFF, Section, Offset, RF.BeginAddress, FunctionAddress, FunctionOffset,
  912. /*FunctionOnly=*/true);
  913. uint64_t XDataAddress, XDataOffset;
  914. ErrorOr<SymbolRef> XDataRecord = getSymbolForLocation(
  915. COFF, Section, Offset + 4, RF.ExceptionInformationRVA(), XDataAddress,
  916. XDataOffset);
  917. if (!RF.BeginAddress && !Function)
  918. return false;
  919. if (!RF.UnwindData && !XDataRecord)
  920. return false;
  921. StringRef FunctionName;
  922. if (Function) {
  923. Expected<StringRef> FunctionNameOrErr = Function->getName();
  924. if (!FunctionNameOrErr) {
  925. std::string Buf;
  926. llvm::raw_string_ostream OS(Buf);
  927. logAllUnhandledErrors(FunctionNameOrErr.takeError(), OS);
  928. report_fatal_error(Twine(OS.str()));
  929. }
  930. FunctionName = *FunctionNameOrErr;
  931. }
  932. SW.printString("Function",
  933. formatSymbol(FunctionName, FunctionAddress, FunctionOffset));
  934. if (XDataRecord) {
  935. Expected<StringRef> Name = XDataRecord->getName();
  936. if (!Name) {
  937. std::string Buf;
  938. llvm::raw_string_ostream OS(Buf);
  939. logAllUnhandledErrors(Name.takeError(), OS);
  940. report_fatal_error(Twine(OS.str()));
  941. }
  942. SW.printString("ExceptionRecord",
  943. formatSymbol(*Name, XDataAddress, XDataOffset));
  944. Expected<section_iterator> SIOrErr = XDataRecord->getSection();
  945. if (!SIOrErr) {
  946. // TODO: Actually report errors helpfully.
  947. consumeError(SIOrErr.takeError());
  948. return false;
  949. }
  950. section_iterator SI = *SIOrErr;
  951. return dumpXDataRecord(COFF, *SI, FunctionAddress, XDataAddress);
  952. } else {
  953. SW.printString("ExceptionRecord", formatSymbol("", XDataAddress));
  954. ErrorOr<SectionRef> Section = getSectionContaining(COFF, XDataAddress);
  955. if (!Section)
  956. return false;
  957. return dumpXDataRecord(COFF, *Section, FunctionAddress, XDataAddress);
  958. }
  959. }
  960. bool Decoder::dumpPackedEntry(const object::COFFObjectFile &COFF,
  961. const SectionRef Section, uint64_t Offset,
  962. unsigned Index, const RuntimeFunction &RF) {
  963. assert((RF.Flag() == RuntimeFunctionFlag::RFF_Packed ||
  964. RF.Flag() == RuntimeFunctionFlag::RFF_PackedFragment) &&
  965. "unpacked entry cannot be treated as a packed entry");
  966. uint64_t FunctionAddress, FunctionOffset;
  967. ErrorOr<SymbolRef> Function = getSymbolForLocation(
  968. COFF, Section, Offset, RF.BeginAddress, FunctionAddress, FunctionOffset,
  969. /*FunctionOnly=*/true);
  970. StringRef FunctionName;
  971. if (Function) {
  972. Expected<StringRef> FunctionNameOrErr = Function->getName();
  973. if (!FunctionNameOrErr) {
  974. std::string Buf;
  975. llvm::raw_string_ostream OS(Buf);
  976. logAllUnhandledErrors(FunctionNameOrErr.takeError(), OS);
  977. report_fatal_error(Twine(OS.str()));
  978. }
  979. FunctionName = *FunctionNameOrErr;
  980. }
  981. SW.printString("Function",
  982. formatSymbol(FunctionName, FunctionAddress, FunctionOffset));
  983. if (!isAArch64)
  984. SW.printBoolean("Fragment",
  985. RF.Flag() == RuntimeFunctionFlag::RFF_PackedFragment);
  986. SW.printNumber("FunctionLength", RF.FunctionLength());
  987. SW.startLine() << "ReturnType: " << RF.Ret() << '\n';
  988. SW.printBoolean("HomedParameters", RF.H());
  989. SW.startLine() << "SavedRegisters: ";
  990. printRegisters(SavedRegisterMask(RF));
  991. OS << '\n';
  992. SW.printNumber("StackAdjustment", StackAdjustment(RF) << 2);
  993. return true;
  994. }
  995. bool Decoder::dumpPackedARM64Entry(const object::COFFObjectFile &COFF,
  996. const SectionRef Section, uint64_t Offset,
  997. unsigned Index,
  998. const RuntimeFunctionARM64 &RF) {
  999. assert((RF.Flag() == RuntimeFunctionFlag::RFF_Packed ||
  1000. RF.Flag() == RuntimeFunctionFlag::RFF_PackedFragment) &&
  1001. "unpacked entry cannot be treated as a packed entry");
  1002. uint64_t FunctionAddress, FunctionOffset;
  1003. ErrorOr<SymbolRef> Function = getSymbolForLocation(
  1004. COFF, Section, Offset, RF.BeginAddress, FunctionAddress, FunctionOffset,
  1005. /*FunctionOnly=*/true);
  1006. StringRef FunctionName;
  1007. if (Function) {
  1008. Expected<StringRef> FunctionNameOrErr = Function->getName();
  1009. if (!FunctionNameOrErr) {
  1010. std::string Buf;
  1011. llvm::raw_string_ostream OS(Buf);
  1012. logAllUnhandledErrors(FunctionNameOrErr.takeError(), OS);
  1013. report_fatal_error(Twine(OS.str()));
  1014. }
  1015. FunctionName = *FunctionNameOrErr;
  1016. }
  1017. SW.printString("Function",
  1018. formatSymbol(FunctionName, FunctionAddress, FunctionOffset));
  1019. SW.printBoolean("Fragment",
  1020. RF.Flag() == RuntimeFunctionFlag::RFF_PackedFragment);
  1021. SW.printNumber("FunctionLength", RF.FunctionLength());
  1022. SW.printNumber("RegF", RF.RegF());
  1023. SW.printNumber("RegI", RF.RegI());
  1024. SW.printBoolean("HomedParameters", RF.H());
  1025. SW.printNumber("CR", RF.CR());
  1026. SW.printNumber("FrameSize", RF.FrameSize() << 4);
  1027. ListScope PS(SW, "Prologue");
  1028. // Synthesize the equivalent prologue according to the documentation
  1029. // at https://docs.microsoft.com/en-us/cpp/build/arm64-exception-handling,
  1030. // printed in reverse order compared to the docs, to match how prologues
  1031. // are printed for the non-packed case.
  1032. int IntSZ = 8 * RF.RegI();
  1033. if (RF.CR() == 1)
  1034. IntSZ += 8;
  1035. int FpSZ = 8 * RF.RegF();
  1036. if (RF.RegF())
  1037. FpSZ += 8;
  1038. int SavSZ = (IntSZ + FpSZ + 8 * 8 * RF.H() + 0xf) & ~0xf;
  1039. int LocSZ = (RF.FrameSize() << 4) - SavSZ;
  1040. if (RF.CR() == 3) {
  1041. SW.startLine() << "mov x29, sp\n";
  1042. if (LocSZ <= 512) {
  1043. SW.startLine() << format("stp x29, lr, [sp, #-%d]!\n", LocSZ);
  1044. } else {
  1045. SW.startLine() << "stp x29, lr, [sp, #0]\n";
  1046. }
  1047. }
  1048. if (LocSZ > 4080) {
  1049. SW.startLine() << format("sub sp, sp, #%d\n", LocSZ - 4080);
  1050. SW.startLine() << "sub sp, sp, #4080\n";
  1051. } else if ((RF.CR() != 3 && LocSZ > 0) || LocSZ > 512) {
  1052. SW.startLine() << format("sub sp, sp, #%d\n", LocSZ);
  1053. }
  1054. if (RF.H()) {
  1055. SW.startLine() << format("stp x6, x7, [sp, #%d]\n", IntSZ + FpSZ + 48);
  1056. SW.startLine() << format("stp x4, x5, [sp, #%d]\n", IntSZ + FpSZ + 32);
  1057. SW.startLine() << format("stp x2, x3, [sp, #%d]\n", IntSZ + FpSZ + 16);
  1058. if (RF.RegI() > 0 || RF.RegF() > 0 || RF.CR() == 1) {
  1059. SW.startLine() << format("stp x0, x1, [sp, #%d]\n", IntSZ + FpSZ);
  1060. } else {
  1061. // This case isn't documented; if neither RegI nor RegF nor CR=1
  1062. // have decremented the stack pointer by SavSZ, we need to do it here
  1063. // (as the final stack adjustment of LocSZ excludes SavSZ).
  1064. SW.startLine() << format("stp x0, x1, [sp, #-%d]!\n", SavSZ);
  1065. }
  1066. }
  1067. int FloatRegs = RF.RegF() > 0 ? RF.RegF() + 1 : 0;
  1068. for (int I = (FloatRegs + 1) / 2 - 1; I >= 0; I--) {
  1069. if (I == (FloatRegs + 1) / 2 - 1 && FloatRegs % 2 == 1) {
  1070. // The last register, an odd register without a pair
  1071. SW.startLine() << format("str d%d, [sp, #%d]\n", 8 + 2 * I,
  1072. IntSZ + 16 * I);
  1073. } else if (I == 0 && RF.RegI() == 0 && RF.CR() != 1) {
  1074. SW.startLine() << format("stp d%d, d%d, [sp, #-%d]!\n", 8 + 2 * I,
  1075. 8 + 2 * I + 1, SavSZ);
  1076. } else {
  1077. SW.startLine() << format("stp d%d, d%d, [sp, #%d]\n", 8 + 2 * I,
  1078. 8 + 2 * I + 1, IntSZ + 16 * I);
  1079. }
  1080. }
  1081. if (RF.CR() == 1 && (RF.RegI() % 2) == 0) {
  1082. if (RF.RegI() == 0)
  1083. SW.startLine() << format("str lr, [sp, #-%d]!\n", SavSZ);
  1084. else
  1085. SW.startLine() << format("str lr, [sp, #%d]\n", IntSZ - 8);
  1086. }
  1087. for (int I = (RF.RegI() + 1) / 2 - 1; I >= 0; I--) {
  1088. if (I == (RF.RegI() + 1) / 2 - 1 && RF.RegI() % 2 == 1) {
  1089. // The last register, an odd register without a pair
  1090. if (RF.CR() == 1) {
  1091. if (I == 0) { // If this is the only register pair
  1092. // CR=1 combined with RegI=1 doesn't map to a documented case;
  1093. // it doesn't map to any regular unwind info opcode, and the
  1094. // actual unwinder doesn't support it.
  1095. SW.startLine() << "INVALID!\n";
  1096. } else
  1097. SW.startLine() << format("stp x%d, lr, [sp, #%d]\n", 19 + 2 * I,
  1098. 16 * I);
  1099. } else {
  1100. if (I == 0)
  1101. SW.startLine() << format("str x%d, [sp, #-%d]!\n", 19 + 2 * I, SavSZ);
  1102. else
  1103. SW.startLine() << format("str x%d, [sp, #%d]\n", 19 + 2 * I, 16 * I);
  1104. }
  1105. } else if (I == 0) {
  1106. // The first register pair
  1107. SW.startLine() << format("stp x19, x20, [sp, #-%d]!\n", SavSZ);
  1108. } else {
  1109. SW.startLine() << format("stp x%d, x%d, [sp, #%d]\n", 19 + 2 * I,
  1110. 19 + 2 * I + 1, 16 * I);
  1111. }
  1112. }
  1113. SW.startLine() << "end\n";
  1114. return true;
  1115. }
  1116. bool Decoder::dumpProcedureDataEntry(const COFFObjectFile &COFF,
  1117. const SectionRef Section, unsigned Index,
  1118. ArrayRef<uint8_t> Contents) {
  1119. uint64_t Offset = PDataEntrySize * Index;
  1120. const ulittle32_t *Data =
  1121. reinterpret_cast<const ulittle32_t *>(Contents.data() + Offset);
  1122. const RuntimeFunction Entry(Data);
  1123. DictScope RFS(SW, "RuntimeFunction");
  1124. if (Entry.Flag() == RuntimeFunctionFlag::RFF_Unpacked)
  1125. return dumpUnpackedEntry(COFF, Section, Offset, Index, Entry);
  1126. if (isAArch64) {
  1127. const RuntimeFunctionARM64 EntryARM64(Data);
  1128. return dumpPackedARM64Entry(COFF, Section, Offset, Index, EntryARM64);
  1129. }
  1130. return dumpPackedEntry(COFF, Section, Offset, Index, Entry);
  1131. }
  1132. void Decoder::dumpProcedureData(const COFFObjectFile &COFF,
  1133. const SectionRef Section) {
  1134. ArrayRef<uint8_t> Contents;
  1135. if (COFF.getSectionContents(COFF.getCOFFSection(Section), Contents))
  1136. return;
  1137. if (Contents.size() % PDataEntrySize) {
  1138. errs() << ".pdata content is not " << PDataEntrySize << "-byte aligned\n";
  1139. return;
  1140. }
  1141. for (unsigned EI = 0, EE = Contents.size() / PDataEntrySize; EI < EE; ++EI)
  1142. if (!dumpProcedureDataEntry(COFF, Section, EI, Contents))
  1143. break;
  1144. }
  1145. Error Decoder::dumpProcedureData(const COFFObjectFile &COFF) {
  1146. for (const auto &Section : COFF.sections()) {
  1147. Expected<StringRef> NameOrErr =
  1148. COFF.getSectionName(COFF.getCOFFSection(Section));
  1149. if (!NameOrErr)
  1150. return NameOrErr.takeError();
  1151. if (NameOrErr->startswith(".pdata"))
  1152. dumpProcedureData(COFF, Section);
  1153. }
  1154. return Error::success();
  1155. }
  1156. }
  1157. }
  1158. }