ARMWinEHPrinter.cpp 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478
  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 << "bx <reg>";
  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, 0xe7, 3, &Decoder::opcode_save_any_reg},
  160. {0xff, 0xe8, 1, &Decoder::opcode_trap_frame},
  161. {0xff, 0xe9, 1, &Decoder::opcode_machine_frame},
  162. {0xff, 0xea, 1, &Decoder::opcode_context},
  163. {0xff, 0xec, 1, &Decoder::opcode_clear_unwound_to_call},
  164. {0xff, 0xfc, 1, &Decoder::opcode_pac_sign_lr},
  165. };
  166. static void printRange(raw_ostream &OS, ListSeparator &LS, unsigned First,
  167. unsigned Last, char Letter) {
  168. if (First == Last)
  169. OS << LS << Letter << First;
  170. else
  171. OS << LS << Letter << First << "-" << Letter << Last;
  172. }
  173. static void printRange(raw_ostream &OS, uint32_t Mask, ListSeparator &LS,
  174. unsigned Start, unsigned End, char Letter) {
  175. int First = -1;
  176. for (unsigned RI = Start; RI <= End; ++RI) {
  177. if (Mask & (1 << RI)) {
  178. if (First < 0)
  179. First = RI;
  180. } else {
  181. if (First >= 0) {
  182. printRange(OS, LS, First, RI - 1, Letter);
  183. First = -1;
  184. }
  185. }
  186. }
  187. if (First >= 0)
  188. printRange(OS, LS, First, End, Letter);
  189. }
  190. void Decoder::printGPRMask(uint16_t GPRMask) {
  191. OS << '{';
  192. ListSeparator LS;
  193. printRange(OS, GPRMask, LS, 0, 12, 'r');
  194. if (GPRMask & (1 << 14))
  195. OS << LS << "lr";
  196. if (GPRMask & (1 << 15))
  197. OS << LS << "pc";
  198. OS << '}';
  199. }
  200. void Decoder::printVFPMask(uint32_t VFPMask) {
  201. OS << '{';
  202. ListSeparator LS;
  203. printRange(OS, VFPMask, LS, 0, 31, 'd');
  204. OS << '}';
  205. }
  206. ErrorOr<object::SectionRef>
  207. Decoder::getSectionContaining(const COFFObjectFile &COFF, uint64_t VA) {
  208. for (const auto &Section : COFF.sections()) {
  209. uint64_t Address = Section.getAddress();
  210. uint64_t Size = Section.getSize();
  211. if (VA >= Address && (VA - Address) <= Size)
  212. return Section;
  213. }
  214. return inconvertibleErrorCode();
  215. }
  216. ErrorOr<object::SymbolRef> Decoder::getSymbol(const COFFObjectFile &COFF,
  217. uint64_t VA, bool FunctionOnly) {
  218. for (const auto &Symbol : COFF.symbols()) {
  219. Expected<SymbolRef::Type> Type = Symbol.getType();
  220. if (!Type)
  221. return errorToErrorCode(Type.takeError());
  222. if (FunctionOnly && *Type != SymbolRef::ST_Function)
  223. continue;
  224. Expected<uint64_t> Address = Symbol.getAddress();
  225. if (!Address)
  226. return errorToErrorCode(Address.takeError());
  227. if (*Address == VA)
  228. return Symbol;
  229. }
  230. return inconvertibleErrorCode();
  231. }
  232. ErrorOr<SymbolRef> Decoder::getRelocatedSymbol(const COFFObjectFile &,
  233. const SectionRef &Section,
  234. uint64_t Offset) {
  235. for (const auto &Relocation : Section.relocations()) {
  236. uint64_t RelocationOffset = Relocation.getOffset();
  237. if (RelocationOffset == Offset)
  238. return *Relocation.getSymbol();
  239. }
  240. return inconvertibleErrorCode();
  241. }
  242. SymbolRef Decoder::getPreferredSymbol(const COFFObjectFile &COFF, SymbolRef Sym,
  243. uint64_t &SymbolOffset) {
  244. // The symbol resolved by getRelocatedSymbol can be any internal
  245. // nondescriptive symbol; try to resolve a more descriptive one.
  246. COFFSymbolRef CoffSym = COFF.getCOFFSymbol(Sym);
  247. if (CoffSym.getStorageClass() != COFF::IMAGE_SYM_CLASS_LABEL &&
  248. CoffSym.getSectionDefinition() == nullptr)
  249. return Sym;
  250. for (const auto &S : COFF.symbols()) {
  251. COFFSymbolRef CS = COFF.getCOFFSymbol(S);
  252. if (CS.getSectionNumber() == CoffSym.getSectionNumber() &&
  253. CS.getValue() <= CoffSym.getValue() + SymbolOffset &&
  254. CS.getStorageClass() != COFF::IMAGE_SYM_CLASS_LABEL &&
  255. CS.getSectionDefinition() == nullptr) {
  256. uint32_t Offset = CoffSym.getValue() + SymbolOffset - CS.getValue();
  257. if (Offset <= SymbolOffset) {
  258. SymbolOffset = Offset;
  259. Sym = S;
  260. CoffSym = CS;
  261. if (CS.isExternal() && SymbolOffset == 0)
  262. return Sym;
  263. }
  264. }
  265. }
  266. return Sym;
  267. }
  268. ErrorOr<SymbolRef> Decoder::getSymbolForLocation(
  269. const COFFObjectFile &COFF, const SectionRef &Section,
  270. uint64_t OffsetInSection, uint64_t ImmediateOffset, uint64_t &SymbolAddress,
  271. uint64_t &SymbolOffset, bool FunctionOnly) {
  272. // Try to locate a relocation that points at the offset in the section
  273. ErrorOr<SymbolRef> SymOrErr =
  274. getRelocatedSymbol(COFF, Section, OffsetInSection);
  275. if (SymOrErr) {
  276. // We found a relocation symbol; the immediate offset needs to be added
  277. // to the symbol address.
  278. SymbolOffset = ImmediateOffset;
  279. Expected<uint64_t> AddressOrErr = SymOrErr->getAddress();
  280. if (!AddressOrErr) {
  281. std::string Buf;
  282. llvm::raw_string_ostream OS(Buf);
  283. logAllUnhandledErrors(AddressOrErr.takeError(), OS);
  284. report_fatal_error(Twine(OS.str()));
  285. }
  286. // We apply SymbolOffset here directly. We return it separately to allow
  287. // the caller to print it as an offset on the symbol name.
  288. SymbolAddress = *AddressOrErr + SymbolOffset;
  289. if (FunctionOnly) // Resolve label/section symbols into function names.
  290. SymOrErr = getPreferredSymbol(COFF, *SymOrErr, SymbolOffset);
  291. } else {
  292. // No matching relocation found; operating on a linked image. Try to
  293. // find a descriptive symbol if possible. The immediate offset contains
  294. // the image relative address, and we shouldn't add any offset to the
  295. // symbol.
  296. SymbolAddress = COFF.getImageBase() + ImmediateOffset;
  297. SymbolOffset = 0;
  298. SymOrErr = getSymbol(COFF, SymbolAddress, FunctionOnly);
  299. }
  300. return SymOrErr;
  301. }
  302. bool Decoder::opcode_0xxxxxxx(const uint8_t *OC, unsigned &Offset,
  303. unsigned Length, bool Prologue) {
  304. uint8_t Imm = OC[Offset] & 0x7f;
  305. SW.startLine() << format("0x%02x ; %s sp, #(%u * 4)\n",
  306. OC[Offset],
  307. static_cast<const char *>(Prologue ? "sub" : "add"),
  308. Imm);
  309. ++Offset;
  310. return false;
  311. }
  312. bool Decoder::opcode_10Lxxxxx(const uint8_t *OC, unsigned &Offset,
  313. unsigned Length, bool Prologue) {
  314. unsigned Link = (OC[Offset] & 0x20) >> 5;
  315. uint16_t RegisterMask = (Link << (Prologue ? 14 : 15))
  316. | ((OC[Offset + 0] & 0x1f) << 8)
  317. | ((OC[Offset + 1] & 0xff) << 0);
  318. assert((~RegisterMask & (1 << 13)) && "sp must not be set");
  319. assert((~RegisterMask & (1 << (Prologue ? 15 : 14))) && "pc must not be set");
  320. SW.startLine() << format("0x%02x 0x%02x ; %s.w ",
  321. OC[Offset + 0], OC[Offset + 1],
  322. Prologue ? "push" : "pop");
  323. printGPRMask(RegisterMask);
  324. OS << '\n';
  325. Offset += 2;
  326. return false;
  327. }
  328. bool Decoder::opcode_1100xxxx(const uint8_t *OC, unsigned &Offset,
  329. unsigned Length, bool Prologue) {
  330. if (Prologue)
  331. SW.startLine() << format("0x%02x ; mov r%u, sp\n",
  332. OC[Offset], OC[Offset] & 0xf);
  333. else
  334. SW.startLine() << format("0x%02x ; mov sp, r%u\n",
  335. OC[Offset], OC[Offset] & 0xf);
  336. ++Offset;
  337. return false;
  338. }
  339. bool Decoder::opcode_11010Lxx(const uint8_t *OC, unsigned &Offset,
  340. unsigned Length, bool Prologue) {
  341. unsigned Link = (OC[Offset] & 0x4) >> 2;
  342. unsigned Count = (OC[Offset] & 0x3);
  343. uint16_t GPRMask = (Link << (Prologue ? 14 : 15))
  344. | (((1 << (Count + 1)) - 1) << 4);
  345. SW.startLine() << format("0x%02x ; %s ", OC[Offset],
  346. Prologue ? "push" : "pop");
  347. printGPRMask(GPRMask);
  348. OS << '\n';
  349. ++Offset;
  350. return false;
  351. }
  352. bool Decoder::opcode_11011Lxx(const uint8_t *OC, unsigned &Offset,
  353. unsigned Length, bool Prologue) {
  354. unsigned Link = (OC[Offset] & 0x4) >> 2;
  355. unsigned Count = (OC[Offset] & 0x3) + 4;
  356. uint16_t GPRMask = (Link << (Prologue ? 14 : 15))
  357. | (((1 << (Count + 1)) - 1) << 4);
  358. SW.startLine() << format("0x%02x ; %s.w ", OC[Offset],
  359. Prologue ? "push" : "pop");
  360. printGPRMask(GPRMask);
  361. OS << '\n';
  362. ++Offset;
  363. return false;
  364. }
  365. bool Decoder::opcode_11100xxx(const uint8_t *OC, unsigned &Offset,
  366. unsigned Length, bool Prologue) {
  367. unsigned High = (OC[Offset] & 0x7);
  368. uint32_t VFPMask = (((1 << (High + 1)) - 1) << 8);
  369. SW.startLine() << format("0x%02x ; %s ", OC[Offset],
  370. Prologue ? "vpush" : "vpop");
  371. printVFPMask(VFPMask);
  372. OS << '\n';
  373. ++Offset;
  374. return false;
  375. }
  376. bool Decoder::opcode_111010xx(const uint8_t *OC, unsigned &Offset,
  377. unsigned Length, bool Prologue) {
  378. uint16_t Imm = ((OC[Offset + 0] & 0x03) << 8) | ((OC[Offset + 1] & 0xff) << 0);
  379. SW.startLine() << format("0x%02x 0x%02x ; %s.w sp, #(%u * 4)\n",
  380. OC[Offset + 0], OC[Offset + 1],
  381. static_cast<const char *>(Prologue ? "sub" : "add"),
  382. Imm);
  383. Offset += 2;
  384. return false;
  385. }
  386. bool Decoder::opcode_1110110L(const uint8_t *OC, unsigned &Offset,
  387. unsigned Length, bool Prologue) {
  388. uint16_t GPRMask = ((OC[Offset + 0] & 0x01) << (Prologue ? 14 : 15))
  389. | ((OC[Offset + 1] & 0xff) << 0);
  390. SW.startLine() << format("0x%02x 0x%02x ; %s ", OC[Offset + 0],
  391. OC[Offset + 1], Prologue ? "push" : "pop");
  392. printGPRMask(GPRMask);
  393. OS << '\n';
  394. Offset += 2;
  395. return false;
  396. }
  397. bool Decoder::opcode_11101110(const uint8_t *OC, unsigned &Offset,
  398. unsigned Length, bool Prologue) {
  399. assert(!Prologue && "may not be used in prologue");
  400. if (OC[Offset + 1] & 0xf0)
  401. SW.startLine() << format("0x%02x 0x%02x ; reserved\n",
  402. OC[Offset + 0], OC[Offset + 1]);
  403. else
  404. SW.startLine()
  405. << format("0x%02x 0x%02x ; microsoft-specific (type: %u)\n",
  406. OC[Offset + 0], OC[Offset + 1], OC[Offset + 1] & 0x0f);
  407. Offset += 2;
  408. return false;
  409. }
  410. bool Decoder::opcode_11101111(const uint8_t *OC, unsigned &Offset,
  411. unsigned Length, bool Prologue) {
  412. if (OC[Offset + 1] & 0xf0)
  413. SW.startLine() << format("0x%02x 0x%02x ; reserved\n",
  414. OC[Offset + 0], OC[Offset + 1]);
  415. else if (Prologue)
  416. SW.startLine()
  417. << format("0x%02x 0x%02x ; str.w lr, [sp, #-%u]!\n",
  418. OC[Offset + 0], OC[Offset + 1], OC[Offset + 1] << 2);
  419. else
  420. SW.startLine()
  421. << format("0x%02x 0x%02x ; ldr.w lr, [sp], #%u\n",
  422. OC[Offset + 0], OC[Offset + 1], OC[Offset + 1] << 2);
  423. Offset += 2;
  424. return false;
  425. }
  426. bool Decoder::opcode_11110101(const uint8_t *OC, unsigned &Offset,
  427. unsigned Length, bool Prologue) {
  428. unsigned Start = (OC[Offset + 1] & 0xf0) >> 4;
  429. unsigned End = (OC[Offset + 1] & 0x0f) >> 0;
  430. uint32_t VFPMask = ((1 << (End + 1 - Start)) - 1) << Start;
  431. SW.startLine() << format("0x%02x 0x%02x ; %s ", OC[Offset + 0],
  432. OC[Offset + 1], Prologue ? "vpush" : "vpop");
  433. printVFPMask(VFPMask);
  434. OS << '\n';
  435. Offset += 2;
  436. return false;
  437. }
  438. bool Decoder::opcode_11110110(const uint8_t *OC, unsigned &Offset,
  439. unsigned Length, bool Prologue) {
  440. unsigned Start = (OC[Offset + 1] & 0xf0) >> 4;
  441. unsigned End = (OC[Offset + 1] & 0x0f) >> 0;
  442. uint32_t VFPMask = ((1 << (End + 1 - Start)) - 1) << (16 + Start);
  443. SW.startLine() << format("0x%02x 0x%02x ; %s ", OC[Offset + 0],
  444. OC[Offset + 1], Prologue ? "vpush" : "vpop");
  445. printVFPMask(VFPMask);
  446. OS << '\n';
  447. Offset += 2;
  448. return false;
  449. }
  450. bool Decoder::opcode_11110111(const uint8_t *OC, unsigned &Offset,
  451. unsigned Length, bool Prologue) {
  452. uint32_t Imm = (OC[Offset + 1] << 8) | (OC[Offset + 2] << 0);
  453. SW.startLine() << format("0x%02x 0x%02x 0x%02x ; %s sp, sp, #(%u * 4)\n",
  454. OC[Offset + 0], OC[Offset + 1], OC[Offset + 2],
  455. static_cast<const char *>(Prologue ? "sub" : "add"),
  456. Imm);
  457. Offset += 3;
  458. return false;
  459. }
  460. bool Decoder::opcode_11111000(const uint8_t *OC, unsigned &Offset,
  461. unsigned Length, bool Prologue) {
  462. uint32_t Imm = (OC[Offset + 1] << 16)
  463. | (OC[Offset + 2] << 8)
  464. | (OC[Offset + 3] << 0);
  465. SW.startLine()
  466. << format("0x%02x 0x%02x 0x%02x 0x%02x ; %s sp, sp, #(%u * 4)\n",
  467. OC[Offset + 0], OC[Offset + 1], OC[Offset + 2], OC[Offset + 3],
  468. static_cast<const char *>(Prologue ? "sub" : "add"), Imm);
  469. Offset += 4;
  470. return false;
  471. }
  472. bool Decoder::opcode_11111001(const uint8_t *OC, unsigned &Offset,
  473. unsigned Length, bool Prologue) {
  474. uint32_t Imm = (OC[Offset + 1] << 8) | (OC[Offset + 2] << 0);
  475. SW.startLine()
  476. << format("0x%02x 0x%02x 0x%02x ; %s.w sp, sp, #(%u * 4)\n",
  477. OC[Offset + 0], OC[Offset + 1], OC[Offset + 2],
  478. static_cast<const char *>(Prologue ? "sub" : "add"), Imm);
  479. Offset += 3;
  480. return false;
  481. }
  482. bool Decoder::opcode_11111010(const uint8_t *OC, unsigned &Offset,
  483. unsigned Length, bool Prologue) {
  484. uint32_t Imm = (OC[Offset + 1] << 16)
  485. | (OC[Offset + 2] << 8)
  486. | (OC[Offset + 3] << 0);
  487. SW.startLine()
  488. << format("0x%02x 0x%02x 0x%02x 0x%02x ; %s.w sp, sp, #(%u * 4)\n",
  489. OC[Offset + 0], OC[Offset + 1], OC[Offset + 2], OC[Offset + 3],
  490. static_cast<const char *>(Prologue ? "sub" : "add"), Imm);
  491. Offset += 4;
  492. return false;
  493. }
  494. bool Decoder::opcode_11111011(const uint8_t *OC, unsigned &Offset,
  495. unsigned Length, bool Prologue) {
  496. SW.startLine() << format("0x%02x ; nop\n", OC[Offset]);
  497. ++Offset;
  498. return false;
  499. }
  500. bool Decoder::opcode_11111100(const uint8_t *OC, unsigned &Offset,
  501. unsigned Length, bool Prologue) {
  502. SW.startLine() << format("0x%02x ; nop.w\n", OC[Offset]);
  503. ++Offset;
  504. return false;
  505. }
  506. bool Decoder::opcode_11111101(const uint8_t *OC, unsigned &Offset,
  507. unsigned Length, bool Prologue) {
  508. SW.startLine() << format("0x%02x ; bx <reg>\n", OC[Offset]);
  509. ++Offset;
  510. return true;
  511. }
  512. bool Decoder::opcode_11111110(const uint8_t *OC, unsigned &Offset,
  513. unsigned Length, bool Prologue) {
  514. SW.startLine() << format("0x%02x ; b.w <target>\n", OC[Offset]);
  515. ++Offset;
  516. return true;
  517. }
  518. bool Decoder::opcode_11111111(const uint8_t *OC, unsigned &Offset,
  519. unsigned Length, bool Prologue) {
  520. ++Offset;
  521. return true;
  522. }
  523. // ARM64 unwind codes start here.
  524. bool Decoder::opcode_alloc_s(const uint8_t *OC, unsigned &Offset,
  525. unsigned Length, bool Prologue) {
  526. uint32_t NumBytes = (OC[Offset] & 0x1F) << 4;
  527. SW.startLine() << format("0x%02x ; %s sp, #%u\n", OC[Offset],
  528. static_cast<const char *>(Prologue ? "sub" : "add"),
  529. NumBytes);
  530. ++Offset;
  531. return false;
  532. }
  533. bool Decoder::opcode_save_r19r20_x(const uint8_t *OC, unsigned &Offset,
  534. unsigned Length, bool Prologue) {
  535. uint32_t Off = (OC[Offset] & 0x1F) << 3;
  536. if (Prologue)
  537. SW.startLine() << format(
  538. "0x%02x ; stp x19, x20, [sp, #-%u]!\n", OC[Offset], Off);
  539. else
  540. SW.startLine() << format(
  541. "0x%02x ; ldp x19, x20, [sp], #%u\n", OC[Offset], Off);
  542. ++Offset;
  543. return false;
  544. }
  545. bool Decoder::opcode_save_fplr(const uint8_t *OC, unsigned &Offset,
  546. unsigned Length, bool Prologue) {
  547. uint32_t Off = (OC[Offset] & 0x3F) << 3;
  548. SW.startLine() << format(
  549. "0x%02x ; %s x29, x30, [sp, #%u]\n", OC[Offset],
  550. static_cast<const char *>(Prologue ? "stp" : "ldp"), Off);
  551. ++Offset;
  552. return false;
  553. }
  554. bool Decoder::opcode_save_fplr_x(const uint8_t *OC, unsigned &Offset,
  555. unsigned Length, bool Prologue) {
  556. uint32_t Off = ((OC[Offset] & 0x3F) + 1) << 3;
  557. if (Prologue)
  558. SW.startLine() << format(
  559. "0x%02x ; stp x29, x30, [sp, #-%u]!\n", OC[Offset], Off);
  560. else
  561. SW.startLine() << format(
  562. "0x%02x ; ldp x29, x30, [sp], #%u\n", OC[Offset], Off);
  563. ++Offset;
  564. return false;
  565. }
  566. bool Decoder::opcode_alloc_m(const uint8_t *OC, unsigned &Offset,
  567. unsigned Length, bool Prologue) {
  568. uint32_t NumBytes = ((OC[Offset] & 0x07) << 8);
  569. NumBytes |= (OC[Offset + 1] & 0xFF);
  570. NumBytes <<= 4;
  571. SW.startLine() << format("0x%02x%02x ; %s sp, #%u\n",
  572. OC[Offset], OC[Offset + 1],
  573. static_cast<const char *>(Prologue ? "sub" : "add"),
  574. NumBytes);
  575. Offset += 2;
  576. return false;
  577. }
  578. bool Decoder::opcode_save_regp(const uint8_t *OC, unsigned &Offset,
  579. unsigned Length, bool Prologue) {
  580. uint32_t Reg = ((OC[Offset] & 0x03) << 8);
  581. Reg |= (OC[Offset + 1] & 0xC0);
  582. Reg >>= 6;
  583. Reg += 19;
  584. uint32_t Off = (OC[Offset + 1] & 0x3F) << 3;
  585. SW.startLine() << format(
  586. "0x%02x%02x ; %s x%u, x%u, [sp, #%u]\n",
  587. OC[Offset], OC[Offset + 1],
  588. static_cast<const char *>(Prologue ? "stp" : "ldp"), Reg, Reg + 1, Off);
  589. Offset += 2;
  590. return false;
  591. }
  592. bool Decoder::opcode_save_regp_x(const uint8_t *OC, unsigned &Offset,
  593. unsigned Length, bool Prologue) {
  594. uint32_t Reg = ((OC[Offset] & 0x03) << 8);
  595. Reg |= (OC[Offset + 1] & 0xC0);
  596. Reg >>= 6;
  597. Reg += 19;
  598. uint32_t Off = ((OC[Offset + 1] & 0x3F) + 1) << 3;
  599. if (Prologue)
  600. SW.startLine() << format(
  601. "0x%02x%02x ; stp x%u, x%u, [sp, #-%u]!\n",
  602. OC[Offset], OC[Offset + 1], Reg,
  603. Reg + 1, Off);
  604. else
  605. SW.startLine() << format(
  606. "0x%02x%02x ; ldp x%u, x%u, [sp], #%u\n",
  607. OC[Offset], OC[Offset + 1], Reg,
  608. Reg + 1, Off);
  609. Offset += 2;
  610. return false;
  611. }
  612. bool Decoder::opcode_save_reg(const uint8_t *OC, unsigned &Offset,
  613. unsigned Length, bool Prologue) {
  614. uint32_t Reg = (OC[Offset] & 0x03) << 8;
  615. Reg |= (OC[Offset + 1] & 0xC0);
  616. Reg >>= 6;
  617. Reg += 19;
  618. uint32_t Off = (OC[Offset + 1] & 0x3F) << 3;
  619. SW.startLine() << format("0x%02x%02x ; %s x%u, [sp, #%u]\n",
  620. OC[Offset], OC[Offset + 1],
  621. static_cast<const char *>(Prologue ? "str" : "ldr"),
  622. Reg, Off);
  623. Offset += 2;
  624. return false;
  625. }
  626. bool Decoder::opcode_save_reg_x(const uint8_t *OC, unsigned &Offset,
  627. unsigned Length, bool Prologue) {
  628. uint32_t Reg = (OC[Offset] & 0x01) << 8;
  629. Reg |= (OC[Offset + 1] & 0xE0);
  630. Reg >>= 5;
  631. Reg += 19;
  632. uint32_t Off = ((OC[Offset + 1] & 0x1F) + 1) << 3;
  633. if (Prologue)
  634. SW.startLine() << format("0x%02x%02x ; str x%u, [sp, #-%u]!\n",
  635. OC[Offset], OC[Offset + 1], Reg, Off);
  636. else
  637. SW.startLine() << format("0x%02x%02x ; ldr x%u, [sp], #%u\n",
  638. OC[Offset], OC[Offset + 1], Reg, Off);
  639. Offset += 2;
  640. return false;
  641. }
  642. bool Decoder::opcode_save_lrpair(const uint8_t *OC, unsigned &Offset,
  643. unsigned Length, bool Prologue) {
  644. uint32_t Reg = (OC[Offset] & 0x01) << 8;
  645. Reg |= (OC[Offset + 1] & 0xC0);
  646. Reg >>= 6;
  647. Reg *= 2;
  648. Reg += 19;
  649. uint32_t Off = (OC[Offset + 1] & 0x3F) << 3;
  650. SW.startLine() << format("0x%02x%02x ; %s x%u, lr, [sp, #%u]\n",
  651. OC[Offset], OC[Offset + 1],
  652. static_cast<const char *>(Prologue ? "stp" : "ldp"),
  653. Reg, Off);
  654. Offset += 2;
  655. return false;
  656. }
  657. bool Decoder::opcode_save_fregp(const uint8_t *OC, unsigned &Offset,
  658. unsigned Length, bool Prologue) {
  659. uint32_t Reg = (OC[Offset] & 0x01) << 8;
  660. Reg |= (OC[Offset + 1] & 0xC0);
  661. Reg >>= 6;
  662. Reg += 8;
  663. uint32_t Off = (OC[Offset + 1] & 0x3F) << 3;
  664. SW.startLine() << format("0x%02x%02x ; %s d%u, d%u, [sp, #%u]\n",
  665. OC[Offset], OC[Offset + 1],
  666. static_cast<const char *>(Prologue ? "stp" : "ldp"),
  667. Reg, Reg + 1, Off);
  668. Offset += 2;
  669. return false;
  670. }
  671. bool Decoder::opcode_save_fregp_x(const uint8_t *OC, unsigned &Offset,
  672. unsigned Length, bool Prologue) {
  673. uint32_t Reg = (OC[Offset] & 0x01) << 8;
  674. Reg |= (OC[Offset + 1] & 0xC0);
  675. Reg >>= 6;
  676. Reg += 8;
  677. uint32_t Off = ((OC[Offset + 1] & 0x3F) + 1) << 3;
  678. if (Prologue)
  679. SW.startLine() << format(
  680. "0x%02x%02x ; stp d%u, d%u, [sp, #-%u]!\n", OC[Offset],
  681. OC[Offset + 1], Reg, Reg + 1, Off);
  682. else
  683. SW.startLine() << format(
  684. "0x%02x%02x ; ldp d%u, d%u, [sp], #%u\n", OC[Offset],
  685. OC[Offset + 1], Reg, Reg + 1, Off);
  686. Offset += 2;
  687. return false;
  688. }
  689. bool Decoder::opcode_save_freg(const uint8_t *OC, unsigned &Offset,
  690. unsigned Length, bool Prologue) {
  691. uint32_t Reg = (OC[Offset] & 0x01) << 8;
  692. Reg |= (OC[Offset + 1] & 0xC0);
  693. Reg >>= 6;
  694. Reg += 8;
  695. uint32_t Off = (OC[Offset + 1] & 0x3F) << 3;
  696. SW.startLine() << format("0x%02x%02x ; %s d%u, [sp, #%u]\n",
  697. OC[Offset], OC[Offset + 1],
  698. static_cast<const char *>(Prologue ? "str" : "ldr"),
  699. Reg, Off);
  700. Offset += 2;
  701. return false;
  702. }
  703. bool Decoder::opcode_save_freg_x(const uint8_t *OC, unsigned &Offset,
  704. unsigned Length, bool Prologue) {
  705. uint32_t Reg = ((OC[Offset + 1] & 0xE0) >> 5) + 8;
  706. uint32_t Off = ((OC[Offset + 1] & 0x1F) + 1) << 3;
  707. if (Prologue)
  708. SW.startLine() << format(
  709. "0x%02x%02x ; str d%u, [sp, #-%u]!\n", OC[Offset],
  710. OC[Offset + 1], Reg, Off);
  711. else
  712. SW.startLine() << format(
  713. "0x%02x%02x ; ldr d%u, [sp], #%u\n", OC[Offset],
  714. OC[Offset + 1], Reg, Off);
  715. Offset += 2;
  716. return false;
  717. }
  718. bool Decoder::opcode_alloc_l(const uint8_t *OC, unsigned &Offset,
  719. unsigned Length, bool Prologue) {
  720. unsigned Off =
  721. (OC[Offset + 1] << 16) | (OC[Offset + 2] << 8) | (OC[Offset + 3] << 0);
  722. Off <<= 4;
  723. SW.startLine() << format(
  724. "0x%02x%02x%02x%02x ; %s sp, #%u\n", OC[Offset], OC[Offset + 1],
  725. OC[Offset + 2], OC[Offset + 3],
  726. static_cast<const char *>(Prologue ? "sub" : "add"), Off);
  727. Offset += 4;
  728. return false;
  729. }
  730. bool Decoder::opcode_setfp(const uint8_t *OC, unsigned &Offset, unsigned Length,
  731. bool Prologue) {
  732. SW.startLine() << format("0x%02x ; mov %s, %s\n", OC[Offset],
  733. static_cast<const char *>(Prologue ? "fp" : "sp"),
  734. static_cast<const char *>(Prologue ? "sp" : "fp"));
  735. ++Offset;
  736. return false;
  737. }
  738. bool Decoder::opcode_addfp(const uint8_t *OC, unsigned &Offset, unsigned Length,
  739. bool Prologue) {
  740. unsigned NumBytes = OC[Offset + 1] << 3;
  741. SW.startLine() << format(
  742. "0x%02x%02x ; %s %s, %s, #%u\n", OC[Offset], OC[Offset + 1],
  743. static_cast<const char *>(Prologue ? "add" : "sub"),
  744. static_cast<const char *>(Prologue ? "fp" : "sp"),
  745. static_cast<const char *>(Prologue ? "sp" : "fp"), NumBytes);
  746. Offset += 2;
  747. return false;
  748. }
  749. bool Decoder::opcode_nop(const uint8_t *OC, unsigned &Offset, unsigned Length,
  750. bool Prologue) {
  751. SW.startLine() << format("0x%02x ; nop\n", OC[Offset]);
  752. ++Offset;
  753. return false;
  754. }
  755. bool Decoder::opcode_end(const uint8_t *OC, unsigned &Offset, unsigned Length,
  756. bool Prologue) {
  757. SW.startLine() << format("0x%02x ; end\n", OC[Offset]);
  758. ++Offset;
  759. return true;
  760. }
  761. bool Decoder::opcode_end_c(const uint8_t *OC, unsigned &Offset, unsigned Length,
  762. bool Prologue) {
  763. SW.startLine() << format("0x%02x ; end_c\n", OC[Offset]);
  764. ++Offset;
  765. return false;
  766. }
  767. bool Decoder::opcode_save_next(const uint8_t *OC, unsigned &Offset,
  768. unsigned Length, bool Prologue) {
  769. if (Prologue)
  770. SW.startLine() << format("0x%02x ; save next\n", OC[Offset]);
  771. else
  772. SW.startLine() << format("0x%02x ; restore next\n",
  773. OC[Offset]);
  774. ++Offset;
  775. return false;
  776. }
  777. bool Decoder::opcode_save_any_reg(const uint8_t *OC, unsigned &Offset,
  778. unsigned Length, bool Prologue) {
  779. // Whether the instruction has writeback
  780. bool Writeback = (OC[Offset + 1] & 0x20) == 0x20;
  781. // Whether the instruction is paired. (Paired instructions are required
  782. // to save/restore adjacent registers.)
  783. bool Paired = (OC[Offset + 1] & 0x40) == 0x40;
  784. // The kind of register saved:
  785. // - 0 is an x register
  786. // - 1 is the low half of a q register
  787. // - 2 is a whole q register
  788. int RegKind = (OC[Offset + 2] & 0xC0) >> 6;
  789. // Encoded register name (0 -> x0/q0, 1 -> x1/q1, etc.)
  790. int Reg = OC[Offset + 1] & 0x1F;
  791. // Encoded stack offset of load/store instruction; decoding varies by mode.
  792. int StackOffset = OC[Offset + 2] & 0x3F;
  793. if (Writeback)
  794. StackOffset++;
  795. if (!Writeback && !Paired && RegKind != 2)
  796. StackOffset *= 8;
  797. else
  798. StackOffset *= 16;
  799. SW.startLine() << format("0x%02x%02x%02x ; ", OC[Offset],
  800. OC[Offset + 1], OC[Offset + 2]);
  801. // Verify the encoding is in a form we understand. The high bit of the first
  802. // byte, and mode 3 for the register kind are apparently reserved. The
  803. // encoded register must refer to a valid register.
  804. int MaxReg = 0x1F;
  805. if (Paired)
  806. --MaxReg;
  807. if (RegKind == 0)
  808. --MaxReg;
  809. if ((OC[Offset + 1] & 0x80) == 0x80 || RegKind == 3 || Reg > MaxReg) {
  810. SW.getOStream() << "invalid save_any_reg encoding\n";
  811. Offset += 3;
  812. return false;
  813. }
  814. if (Paired) {
  815. if (Prologue)
  816. SW.getOStream() << "stp ";
  817. else
  818. SW.getOStream() << "ldp ";
  819. } else {
  820. if (Prologue)
  821. SW.getOStream() << "str ";
  822. else
  823. SW.getOStream() << "ldr ";
  824. }
  825. char RegChar = 'x';
  826. if (RegKind == 1) {
  827. RegChar = 'd';
  828. } else if (RegKind == 2) {
  829. RegChar = 'q';
  830. }
  831. if (Paired)
  832. SW.getOStream() << format("%c%d, %c%d, ", RegChar, Reg, RegChar, Reg + 1);
  833. else
  834. SW.getOStream() << format("%c%d, ", RegChar, Reg);
  835. if (Writeback) {
  836. if (Prologue)
  837. SW.getOStream() << format("[sp, #-%d]!\n", StackOffset);
  838. else
  839. SW.getOStream() << format("[sp], #%d\n", StackOffset);
  840. } else {
  841. SW.getOStream() << format("[sp, #%d]\n", StackOffset);
  842. }
  843. Offset += 3;
  844. return false;
  845. }
  846. bool Decoder::opcode_trap_frame(const uint8_t *OC, unsigned &Offset,
  847. unsigned Length, bool Prologue) {
  848. SW.startLine() << format("0x%02x ; trap frame\n", OC[Offset]);
  849. ++Offset;
  850. return false;
  851. }
  852. bool Decoder::opcode_machine_frame(const uint8_t *OC, unsigned &Offset,
  853. unsigned Length, bool Prologue) {
  854. SW.startLine() << format("0x%02x ; machine frame\n",
  855. OC[Offset]);
  856. ++Offset;
  857. return false;
  858. }
  859. bool Decoder::opcode_context(const uint8_t *OC, unsigned &Offset,
  860. unsigned Length, bool Prologue) {
  861. SW.startLine() << format("0x%02x ; context\n", OC[Offset]);
  862. ++Offset;
  863. return false;
  864. }
  865. bool Decoder::opcode_clear_unwound_to_call(const uint8_t *OC, unsigned &Offset,
  866. unsigned Length, bool Prologue) {
  867. SW.startLine() << format("0x%02x ; clear unwound to call\n",
  868. OC[Offset]);
  869. ++Offset;
  870. return false;
  871. }
  872. bool Decoder::opcode_pac_sign_lr(const uint8_t *OC, unsigned &Offset,
  873. unsigned Length, bool Prologue) {
  874. if (Prologue)
  875. SW.startLine() << format("0x%02x ; pacibsp\n", OC[Offset]);
  876. else
  877. SW.startLine() << format("0x%02x ; autibsp\n", OC[Offset]);
  878. ++Offset;
  879. return false;
  880. }
  881. void Decoder::decodeOpcodes(ArrayRef<uint8_t> Opcodes, unsigned Offset,
  882. bool Prologue) {
  883. assert((!Prologue || Offset == 0) && "prologue should always use offset 0");
  884. const RingEntry* DecodeRing = isAArch64 ? Ring64 : Ring;
  885. bool Terminated = false;
  886. for (unsigned OI = Offset, OE = Opcodes.size(); !Terminated && OI < OE; ) {
  887. for (unsigned DI = 0;; ++DI) {
  888. if ((isAArch64 && (DI >= std::size(Ring64))) ||
  889. (!isAArch64 && (DI >= std::size(Ring)))) {
  890. SW.startLine() << format("0x%02x ; Bad opcode!\n",
  891. Opcodes.data()[OI]);
  892. ++OI;
  893. break;
  894. }
  895. if ((Opcodes[OI] & DecodeRing[DI].Mask) == DecodeRing[DI].Value) {
  896. if (OI + DecodeRing[DI].Length > OE) {
  897. SW.startLine() << format("Opcode 0x%02x goes past the unwind data\n",
  898. Opcodes[OI]);
  899. OI += DecodeRing[DI].Length;
  900. break;
  901. }
  902. Terminated =
  903. (this->*DecodeRing[DI].Routine)(Opcodes.data(), OI, 0, Prologue);
  904. break;
  905. }
  906. }
  907. }
  908. }
  909. bool Decoder::dumpXDataRecord(const COFFObjectFile &COFF,
  910. const SectionRef &Section,
  911. uint64_t FunctionAddress, uint64_t VA) {
  912. ArrayRef<uint8_t> Contents;
  913. if (COFF.getSectionContents(COFF.getCOFFSection(Section), Contents))
  914. return false;
  915. uint64_t SectionVA = Section.getAddress();
  916. uint64_t Offset = VA - SectionVA;
  917. const ulittle32_t *Data =
  918. reinterpret_cast<const ulittle32_t *>(Contents.data() + Offset);
  919. // Sanity check to ensure that the .xdata header is present.
  920. // A header is one or two words, followed by at least one word to describe
  921. // the unwind codes. Applicable to both ARM and AArch64.
  922. if (Contents.size() - Offset < 8)
  923. report_fatal_error(".xdata must be at least 8 bytes in size");
  924. const ExceptionDataRecord XData(Data, isAArch64);
  925. DictScope XRS(SW, "ExceptionData");
  926. SW.printNumber("FunctionLength",
  927. isAArch64 ? XData.FunctionLengthInBytesAArch64() :
  928. XData.FunctionLengthInBytesARM());
  929. SW.printNumber("Version", XData.Vers());
  930. SW.printBoolean("ExceptionData", XData.X());
  931. SW.printBoolean("EpiloguePacked", XData.E());
  932. if (!isAArch64)
  933. SW.printBoolean("Fragment", XData.F());
  934. SW.printNumber(XData.E() ? "EpilogueOffset" : "EpilogueScopes",
  935. XData.EpilogueCount());
  936. uint64_t ByteCodeLength = XData.CodeWords() * sizeof(uint32_t);
  937. SW.printNumber("ByteCodeLength", ByteCodeLength);
  938. if ((int64_t)(Contents.size() - Offset - 4 * HeaderWords(XData) -
  939. (XData.E() ? 0 : XData.EpilogueCount() * 4) -
  940. (XData.X() ? 8 : 0)) < (int64_t)ByteCodeLength) {
  941. SW.flush();
  942. report_fatal_error("Malformed unwind data");
  943. }
  944. if (XData.E()) {
  945. ArrayRef<uint8_t> UC = XData.UnwindByteCode();
  946. {
  947. ListScope PS(SW, "Prologue");
  948. decodeOpcodes(UC, 0, /*Prologue=*/true);
  949. }
  950. if (XData.EpilogueCount()) {
  951. ListScope ES(SW, "Epilogue");
  952. decodeOpcodes(UC, XData.EpilogueCount(), /*Prologue=*/false);
  953. }
  954. } else {
  955. {
  956. ListScope PS(SW, "Prologue");
  957. decodeOpcodes(XData.UnwindByteCode(), 0, /*Prologue=*/true);
  958. }
  959. ArrayRef<ulittle32_t> EpilogueScopes = XData.EpilogueScopes();
  960. ListScope ESS(SW, "EpilogueScopes");
  961. for (const EpilogueScope ES : EpilogueScopes) {
  962. DictScope ESES(SW, "EpilogueScope");
  963. SW.printNumber("StartOffset", ES.EpilogueStartOffset());
  964. if (!isAArch64)
  965. SW.printNumber("Condition", ES.Condition());
  966. SW.printNumber("EpilogueStartIndex",
  967. isAArch64 ? ES.EpilogueStartIndexAArch64()
  968. : ES.EpilogueStartIndexARM());
  969. unsigned ReservedMask = isAArch64 ? 0xF : 0x3;
  970. if ((ES.ES >> 18) & ReservedMask)
  971. SW.printNumber("ReservedBits", (ES.ES >> 18) & ReservedMask);
  972. ListScope Opcodes(SW, "Opcodes");
  973. decodeOpcodes(XData.UnwindByteCode(),
  974. isAArch64 ? ES.EpilogueStartIndexAArch64()
  975. : ES.EpilogueStartIndexARM(),
  976. /*Prologue=*/false);
  977. }
  978. }
  979. if (XData.X()) {
  980. const uint32_t Parameter = XData.ExceptionHandlerParameter();
  981. const size_t HandlerOffset = HeaderWords(XData) +
  982. (XData.E() ? 0 : XData.EpilogueCount()) +
  983. XData.CodeWords();
  984. uint64_t Address, SymbolOffset;
  985. ErrorOr<SymbolRef> Symbol = getSymbolForLocation(
  986. COFF, Section, Offset + HandlerOffset * sizeof(uint32_t),
  987. XData.ExceptionHandlerRVA(), Address, SymbolOffset,
  988. /*FunctionOnly=*/true);
  989. if (!Symbol) {
  990. ListScope EHS(SW, "ExceptionHandler");
  991. SW.printHex("Routine", Address);
  992. SW.printHex("Parameter", Parameter);
  993. return true;
  994. }
  995. Expected<StringRef> Name = Symbol->getName();
  996. if (!Name) {
  997. std::string Buf;
  998. llvm::raw_string_ostream OS(Buf);
  999. logAllUnhandledErrors(Name.takeError(), OS);
  1000. report_fatal_error(Twine(OS.str()));
  1001. }
  1002. ListScope EHS(SW, "ExceptionHandler");
  1003. SW.printString("Routine", formatSymbol(*Name, Address, SymbolOffset));
  1004. SW.printHex("Parameter", Parameter);
  1005. }
  1006. return true;
  1007. }
  1008. bool Decoder::dumpUnpackedEntry(const COFFObjectFile &COFF,
  1009. const SectionRef Section, uint64_t Offset,
  1010. unsigned Index, const RuntimeFunction &RF) {
  1011. assert(RF.Flag() == RuntimeFunctionFlag::RFF_Unpacked &&
  1012. "packed entry cannot be treated as an unpacked entry");
  1013. uint64_t FunctionAddress, FunctionOffset;
  1014. ErrorOr<SymbolRef> Function = getSymbolForLocation(
  1015. COFF, Section, Offset, RF.BeginAddress, FunctionAddress, FunctionOffset,
  1016. /*FunctionOnly=*/true);
  1017. uint64_t XDataAddress, XDataOffset;
  1018. ErrorOr<SymbolRef> XDataRecord = getSymbolForLocation(
  1019. COFF, Section, Offset + 4, RF.ExceptionInformationRVA(), XDataAddress,
  1020. XDataOffset);
  1021. if (!RF.BeginAddress && !Function)
  1022. return false;
  1023. if (!RF.UnwindData && !XDataRecord)
  1024. return false;
  1025. StringRef FunctionName;
  1026. if (Function) {
  1027. Expected<StringRef> FunctionNameOrErr = Function->getName();
  1028. if (!FunctionNameOrErr) {
  1029. std::string Buf;
  1030. llvm::raw_string_ostream OS(Buf);
  1031. logAllUnhandledErrors(FunctionNameOrErr.takeError(), OS);
  1032. report_fatal_error(Twine(OS.str()));
  1033. }
  1034. FunctionName = *FunctionNameOrErr;
  1035. }
  1036. SW.printString("Function",
  1037. formatSymbol(FunctionName, FunctionAddress, FunctionOffset));
  1038. if (XDataRecord) {
  1039. Expected<StringRef> Name = XDataRecord->getName();
  1040. if (!Name) {
  1041. std::string Buf;
  1042. llvm::raw_string_ostream OS(Buf);
  1043. logAllUnhandledErrors(Name.takeError(), OS);
  1044. report_fatal_error(Twine(OS.str()));
  1045. }
  1046. SW.printString("ExceptionRecord",
  1047. formatSymbol(*Name, XDataAddress, XDataOffset));
  1048. Expected<section_iterator> SIOrErr = XDataRecord->getSection();
  1049. if (!SIOrErr) {
  1050. // TODO: Actually report errors helpfully.
  1051. consumeError(SIOrErr.takeError());
  1052. return false;
  1053. }
  1054. section_iterator SI = *SIOrErr;
  1055. return dumpXDataRecord(COFF, *SI, FunctionAddress, XDataAddress);
  1056. } else {
  1057. SW.printString("ExceptionRecord", formatSymbol("", XDataAddress));
  1058. ErrorOr<SectionRef> Section = getSectionContaining(COFF, XDataAddress);
  1059. if (!Section)
  1060. return false;
  1061. return dumpXDataRecord(COFF, *Section, FunctionAddress, XDataAddress);
  1062. }
  1063. }
  1064. bool Decoder::dumpPackedEntry(const object::COFFObjectFile &COFF,
  1065. const SectionRef Section, uint64_t Offset,
  1066. unsigned Index, const RuntimeFunction &RF) {
  1067. assert((RF.Flag() == RuntimeFunctionFlag::RFF_Packed ||
  1068. RF.Flag() == RuntimeFunctionFlag::RFF_PackedFragment) &&
  1069. "unpacked entry cannot be treated as a packed entry");
  1070. uint64_t FunctionAddress, FunctionOffset;
  1071. ErrorOr<SymbolRef> Function = getSymbolForLocation(
  1072. COFF, Section, Offset, RF.BeginAddress, FunctionAddress, FunctionOffset,
  1073. /*FunctionOnly=*/true);
  1074. StringRef FunctionName;
  1075. if (Function) {
  1076. Expected<StringRef> FunctionNameOrErr = Function->getName();
  1077. if (!FunctionNameOrErr) {
  1078. std::string Buf;
  1079. llvm::raw_string_ostream OS(Buf);
  1080. logAllUnhandledErrors(FunctionNameOrErr.takeError(), OS);
  1081. report_fatal_error(Twine(OS.str()));
  1082. }
  1083. FunctionName = *FunctionNameOrErr;
  1084. }
  1085. SW.printString("Function",
  1086. formatSymbol(FunctionName, FunctionAddress, FunctionOffset));
  1087. SW.printBoolean("Fragment",
  1088. RF.Flag() == RuntimeFunctionFlag::RFF_PackedFragment);
  1089. SW.printNumber("FunctionLength", RF.FunctionLength());
  1090. SW.startLine() << "ReturnType: " << RF.Ret() << '\n';
  1091. SW.printBoolean("HomedParameters", RF.H());
  1092. SW.printNumber("Reg", RF.Reg());
  1093. SW.printNumber("R", RF.R());
  1094. SW.printBoolean("LinkRegister", RF.L());
  1095. SW.printBoolean("Chaining", RF.C());
  1096. SW.printNumber("StackAdjustment", StackAdjustment(RF) << 2);
  1097. {
  1098. ListScope PS(SW, "Prologue");
  1099. uint16_t GPRMask, VFPMask;
  1100. std::tie(GPRMask, VFPMask) = SavedRegisterMask(RF, /*Prologue=*/true);
  1101. if (StackAdjustment(RF) && !PrologueFolding(RF))
  1102. SW.startLine() << "sub sp, sp, #" << StackAdjustment(RF) * 4 << "\n";
  1103. if (VFPMask) {
  1104. SW.startLine() << "vpush ";
  1105. printVFPMask(VFPMask);
  1106. OS << "\n";
  1107. }
  1108. if (RF.C()) {
  1109. // Count the number of registers pushed below R11
  1110. int FpOffset = 4 * llvm::popcount(GPRMask & ((1U << 11) - 1));
  1111. if (FpOffset)
  1112. SW.startLine() << "add.w r11, sp, #" << FpOffset << "\n";
  1113. else
  1114. SW.startLine() << "mov r11, sp\n";
  1115. }
  1116. if (GPRMask) {
  1117. SW.startLine() << "push ";
  1118. printGPRMask(GPRMask);
  1119. OS << "\n";
  1120. }
  1121. if (RF.H())
  1122. SW.startLine() << "push {r0-r3}\n";
  1123. }
  1124. if (RF.Ret() != ReturnType::RT_NoEpilogue) {
  1125. ListScope PS(SW, "Epilogue");
  1126. uint16_t GPRMask, VFPMask;
  1127. std::tie(GPRMask, VFPMask) = SavedRegisterMask(RF, /*Prologue=*/false);
  1128. if (StackAdjustment(RF) && !EpilogueFolding(RF))
  1129. SW.startLine() << "add sp, sp, #" << StackAdjustment(RF) * 4 << "\n";
  1130. if (VFPMask) {
  1131. SW.startLine() << "vpop ";
  1132. printVFPMask(VFPMask);
  1133. OS << "\n";
  1134. }
  1135. if (GPRMask) {
  1136. SW.startLine() << "pop ";
  1137. printGPRMask(GPRMask);
  1138. OS << "\n";
  1139. }
  1140. if (RF.H()) {
  1141. if (RF.L() == 0 || RF.Ret() != ReturnType::RT_POP)
  1142. SW.startLine() << "add sp, sp, #16\n";
  1143. else
  1144. SW.startLine() << "ldr pc, [sp], #20\n";
  1145. }
  1146. if (RF.Ret() != ReturnType::RT_POP)
  1147. SW.startLine() << RF.Ret() << '\n';
  1148. }
  1149. return true;
  1150. }
  1151. bool Decoder::dumpPackedARM64Entry(const object::COFFObjectFile &COFF,
  1152. const SectionRef Section, uint64_t Offset,
  1153. unsigned Index,
  1154. const RuntimeFunctionARM64 &RF) {
  1155. assert((RF.Flag() == RuntimeFunctionFlag::RFF_Packed ||
  1156. RF.Flag() == RuntimeFunctionFlag::RFF_PackedFragment) &&
  1157. "unpacked entry cannot be treated as a packed entry");
  1158. uint64_t FunctionAddress, FunctionOffset;
  1159. ErrorOr<SymbolRef> Function = getSymbolForLocation(
  1160. COFF, Section, Offset, RF.BeginAddress, FunctionAddress, FunctionOffset,
  1161. /*FunctionOnly=*/true);
  1162. StringRef FunctionName;
  1163. if (Function) {
  1164. Expected<StringRef> FunctionNameOrErr = Function->getName();
  1165. if (!FunctionNameOrErr) {
  1166. std::string Buf;
  1167. llvm::raw_string_ostream OS(Buf);
  1168. logAllUnhandledErrors(FunctionNameOrErr.takeError(), OS);
  1169. report_fatal_error(Twine(OS.str()));
  1170. }
  1171. FunctionName = *FunctionNameOrErr;
  1172. }
  1173. SW.printString("Function",
  1174. formatSymbol(FunctionName, FunctionAddress, FunctionOffset));
  1175. SW.printBoolean("Fragment",
  1176. RF.Flag() == RuntimeFunctionFlag::RFF_PackedFragment);
  1177. SW.printNumber("FunctionLength", RF.FunctionLength());
  1178. SW.printNumber("RegF", RF.RegF());
  1179. SW.printNumber("RegI", RF.RegI());
  1180. SW.printBoolean("HomedParameters", RF.H());
  1181. SW.printNumber("CR", RF.CR());
  1182. SW.printNumber("FrameSize", RF.FrameSize() << 4);
  1183. ListScope PS(SW, "Prologue");
  1184. // Synthesize the equivalent prologue according to the documentation
  1185. // at https://docs.microsoft.com/en-us/cpp/build/arm64-exception-handling,
  1186. // printed in reverse order compared to the docs, to match how prologues
  1187. // are printed for the non-packed case.
  1188. int IntSZ = 8 * RF.RegI();
  1189. if (RF.CR() == 1)
  1190. IntSZ += 8;
  1191. int FpSZ = 8 * RF.RegF();
  1192. if (RF.RegF())
  1193. FpSZ += 8;
  1194. int SavSZ = (IntSZ + FpSZ + 8 * 8 * RF.H() + 0xf) & ~0xf;
  1195. int LocSZ = (RF.FrameSize() << 4) - SavSZ;
  1196. if (RF.CR() == 2 || RF.CR() == 3) {
  1197. SW.startLine() << "mov x29, sp\n";
  1198. if (LocSZ <= 512) {
  1199. SW.startLine() << format("stp x29, lr, [sp, #-%d]!\n", LocSZ);
  1200. } else {
  1201. SW.startLine() << "stp x29, lr, [sp, #0]\n";
  1202. }
  1203. }
  1204. if (LocSZ > 4080) {
  1205. SW.startLine() << format("sub sp, sp, #%d\n", LocSZ - 4080);
  1206. SW.startLine() << "sub sp, sp, #4080\n";
  1207. } else if ((RF.CR() != 3 && RF.CR() != 2 && LocSZ > 0) || LocSZ > 512) {
  1208. SW.startLine() << format("sub sp, sp, #%d\n", LocSZ);
  1209. }
  1210. if (RF.H()) {
  1211. SW.startLine() << format("stp x6, x7, [sp, #%d]\n", SavSZ - 16);
  1212. SW.startLine() << format("stp x4, x5, [sp, #%d]\n", SavSZ - 32);
  1213. SW.startLine() << format("stp x2, x3, [sp, #%d]\n", SavSZ - 48);
  1214. if (RF.RegI() > 0 || RF.RegF() > 0 || RF.CR() == 1) {
  1215. SW.startLine() << format("stp x0, x1, [sp, #%d]\n", SavSZ - 64);
  1216. } else {
  1217. // This case isn't documented; if neither RegI nor RegF nor CR=1
  1218. // have decremented the stack pointer by SavSZ, we need to do it here
  1219. // (as the final stack adjustment of LocSZ excludes SavSZ).
  1220. SW.startLine() << format("stp x0, x1, [sp, #-%d]!\n", SavSZ);
  1221. }
  1222. }
  1223. int FloatRegs = RF.RegF() > 0 ? RF.RegF() + 1 : 0;
  1224. for (int I = (FloatRegs + 1) / 2 - 1; I >= 0; I--) {
  1225. if (I == (FloatRegs + 1) / 2 - 1 && FloatRegs % 2 == 1) {
  1226. // The last register, an odd register without a pair
  1227. SW.startLine() << format("str d%d, [sp, #%d]\n", 8 + 2 * I,
  1228. IntSZ + 16 * I);
  1229. } else if (I == 0 && RF.RegI() == 0 && RF.CR() != 1) {
  1230. SW.startLine() << format("stp d%d, d%d, [sp, #-%d]!\n", 8 + 2 * I,
  1231. 8 + 2 * I + 1, SavSZ);
  1232. } else {
  1233. SW.startLine() << format("stp d%d, d%d, [sp, #%d]\n", 8 + 2 * I,
  1234. 8 + 2 * I + 1, IntSZ + 16 * I);
  1235. }
  1236. }
  1237. if (RF.CR() == 1 && (RF.RegI() % 2) == 0) {
  1238. if (RF.RegI() == 0)
  1239. SW.startLine() << format("str lr, [sp, #-%d]!\n", SavSZ);
  1240. else
  1241. SW.startLine() << format("str lr, [sp, #%d]\n", IntSZ - 8);
  1242. }
  1243. for (int I = (RF.RegI() + 1) / 2 - 1; I >= 0; I--) {
  1244. if (I == (RF.RegI() + 1) / 2 - 1 && RF.RegI() % 2 == 1) {
  1245. // The last register, an odd register without a pair
  1246. if (RF.CR() == 1) {
  1247. if (I == 0) { // If this is the only register pair
  1248. // CR=1 combined with RegI=1 doesn't map to a documented case;
  1249. // it doesn't map to any regular unwind info opcode, and the
  1250. // actual unwinder doesn't support it.
  1251. SW.startLine() << "INVALID!\n";
  1252. } else
  1253. SW.startLine() << format("stp x%d, lr, [sp, #%d]\n", 19 + 2 * I,
  1254. 16 * I);
  1255. } else {
  1256. if (I == 0)
  1257. SW.startLine() << format("str x%d, [sp, #-%d]!\n", 19 + 2 * I, SavSZ);
  1258. else
  1259. SW.startLine() << format("str x%d, [sp, #%d]\n", 19 + 2 * I, 16 * I);
  1260. }
  1261. } else if (I == 0) {
  1262. // The first register pair
  1263. SW.startLine() << format("stp x19, x20, [sp, #-%d]!\n", SavSZ);
  1264. } else {
  1265. SW.startLine() << format("stp x%d, x%d, [sp, #%d]\n", 19 + 2 * I,
  1266. 19 + 2 * I + 1, 16 * I);
  1267. }
  1268. }
  1269. // CR=2 is yet undocumented, see
  1270. // https://github.com/MicrosoftDocs/cpp-docs/pull/4202 for upstream
  1271. // progress on getting it documented.
  1272. if (RF.CR() == 2)
  1273. SW.startLine() << "pacibsp\n";
  1274. SW.startLine() << "end\n";
  1275. return true;
  1276. }
  1277. bool Decoder::dumpProcedureDataEntry(const COFFObjectFile &COFF,
  1278. const SectionRef Section, unsigned Index,
  1279. ArrayRef<uint8_t> Contents) {
  1280. uint64_t Offset = PDataEntrySize * Index;
  1281. const ulittle32_t *Data =
  1282. reinterpret_cast<const ulittle32_t *>(Contents.data() + Offset);
  1283. const RuntimeFunction Entry(Data);
  1284. DictScope RFS(SW, "RuntimeFunction");
  1285. if (Entry.Flag() == RuntimeFunctionFlag::RFF_Unpacked)
  1286. return dumpUnpackedEntry(COFF, Section, Offset, Index, Entry);
  1287. if (isAArch64) {
  1288. const RuntimeFunctionARM64 EntryARM64(Data);
  1289. return dumpPackedARM64Entry(COFF, Section, Offset, Index, EntryARM64);
  1290. }
  1291. return dumpPackedEntry(COFF, Section, Offset, Index, Entry);
  1292. }
  1293. void Decoder::dumpProcedureData(const COFFObjectFile &COFF,
  1294. const SectionRef Section) {
  1295. ArrayRef<uint8_t> Contents;
  1296. if (COFF.getSectionContents(COFF.getCOFFSection(Section), Contents))
  1297. return;
  1298. if (Contents.size() % PDataEntrySize) {
  1299. errs() << ".pdata content is not " << PDataEntrySize << "-byte aligned\n";
  1300. return;
  1301. }
  1302. for (unsigned EI = 0, EE = Contents.size() / PDataEntrySize; EI < EE; ++EI)
  1303. if (!dumpProcedureDataEntry(COFF, Section, EI, Contents))
  1304. break;
  1305. }
  1306. Error Decoder::dumpProcedureData(const COFFObjectFile &COFF) {
  1307. for (const auto &Section : COFF.sections()) {
  1308. Expected<StringRef> NameOrErr =
  1309. COFF.getSectionName(COFF.getCOFFSection(Section));
  1310. if (!NameOrErr)
  1311. return NameOrErr.takeError();
  1312. if (NameOrErr->startswith(".pdata"))
  1313. dumpProcedureData(COFF, Section);
  1314. }
  1315. return Error::success();
  1316. }
  1317. }
  1318. }
  1319. }