ARMWinEHPrinter.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686
  1. //===-- ARMWinEHPrinter.cpp - Windows on ARM EH Data Printer ----*- C++ -*-===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #include "ARMWinEHPrinter.h"
  10. #include "Error.h"
  11. #include "llvm/ADT/STLExtras.h"
  12. #include "llvm/ADT/StringExtras.h"
  13. #include "llvm/Support/ARMWinEH.h"
  14. #include "llvm/Support/Format.h"
  15. using namespace llvm;
  16. using namespace llvm::object;
  17. using namespace llvm::support;
  18. namespace llvm {
  19. raw_ostream &operator<<(raw_ostream &OS, const ARM::WinEH::ReturnType &RT) {
  20. switch (RT) {
  21. case ARM::WinEH::ReturnType::RT_POP:
  22. OS << "pop {pc}";
  23. break;
  24. case ARM::WinEH::ReturnType::RT_B:
  25. OS << "b target";
  26. break;
  27. case ARM::WinEH::ReturnType::RT_BW:
  28. OS << "b.w target";
  29. break;
  30. case ARM::WinEH::ReturnType::RT_NoEpilogue:
  31. OS << "(no epilogue)";
  32. break;
  33. }
  34. return OS;
  35. }
  36. }
  37. static std::string formatSymbol(StringRef Name, uint64_t Address,
  38. uint64_t Offset = 0) {
  39. std::string Buffer;
  40. raw_string_ostream OS(Buffer);
  41. if (!Name.empty())
  42. OS << Name << " ";
  43. if (Offset)
  44. OS << format("+0x%X (0x%" PRIX64 ")", Offset, Address);
  45. else if (!Name.empty())
  46. OS << format("(0x%" PRIX64 ")", Address);
  47. else
  48. OS << format("0x%" PRIX64, Address);
  49. return OS.str();
  50. }
  51. namespace llvm {
  52. namespace ARM {
  53. namespace WinEH {
  54. const size_t Decoder::PDataEntrySize = sizeof(RuntimeFunction);
  55. // TODO name the uops more appropriately
  56. const Decoder::RingEntry Decoder::Ring[] = {
  57. { 0x80, 0x00, &Decoder::opcode_0xxxxxxx }, // UOP_STACK_FREE (16-bit)
  58. { 0xc0, 0x80, &Decoder::opcode_10Lxxxxx }, // UOP_POP (32-bit)
  59. { 0xf0, 0xc0, &Decoder::opcode_1100xxxx }, // UOP_STACK_SAVE (16-bit)
  60. { 0xf8, 0xd0, &Decoder::opcode_11010Lxx }, // UOP_POP (16-bit)
  61. { 0xf8, 0xd8, &Decoder::opcode_11011Lxx }, // UOP_POP (32-bit)
  62. { 0xf8, 0xe0, &Decoder::opcode_11100xxx }, // UOP_VPOP (32-bit)
  63. { 0xfc, 0xe8, &Decoder::opcode_111010xx }, // UOP_STACK_FREE (32-bit)
  64. { 0xfe, 0xec, &Decoder::opcode_1110110L }, // UOP_POP (16-bit)
  65. { 0xff, 0xee, &Decoder::opcode_11101110 }, // UOP_MICROSOFT_SPECIFIC (16-bit)
  66. // UOP_PUSH_MACHINE_FRAME
  67. // UOP_PUSH_CONTEXT
  68. // UOP_PUSH_TRAP_FRAME
  69. // UOP_REDZONE_RESTORE_LR
  70. { 0xff, 0xef, &Decoder::opcode_11101111 }, // UOP_LDRPC_POSTINC (32-bit)
  71. { 0xff, 0xf5, &Decoder::opcode_11110101 }, // UOP_VPOP (32-bit)
  72. { 0xff, 0xf6, &Decoder::opcode_11110110 }, // UOP_VPOP (32-bit)
  73. { 0xff, 0xf7, &Decoder::opcode_11110111 }, // UOP_STACK_RESTORE (16-bit)
  74. { 0xff, 0xf8, &Decoder::opcode_11111000 }, // UOP_STACK_RESTORE (16-bit)
  75. { 0xff, 0xf9, &Decoder::opcode_11111001 }, // UOP_STACK_RESTORE (32-bit)
  76. { 0xff, 0xfa, &Decoder::opcode_11111010 }, // UOP_STACK_RESTORE (32-bit)
  77. { 0xff, 0xfb, &Decoder::opcode_11111011 }, // UOP_NOP (16-bit)
  78. { 0xff, 0xfc, &Decoder::opcode_11111100 }, // UOP_NOP (32-bit)
  79. { 0xff, 0xfd, &Decoder::opcode_11111101 }, // UOP_NOP (16-bit) / END
  80. { 0xff, 0xfe, &Decoder::opcode_11111110 }, // UOP_NOP (32-bit) / END
  81. { 0xff, 0xff, &Decoder::opcode_11111111 }, // UOP_END
  82. };
  83. void Decoder::printRegisters(const std::pair<uint16_t, uint32_t> &RegisterMask) {
  84. static const char * const GPRRegisterNames[16] = {
  85. "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7", "r8", "r9", "r10",
  86. "r11", "ip", "sp", "lr", "pc",
  87. };
  88. const uint16_t GPRMask = std::get<0>(RegisterMask);
  89. const uint16_t VFPMask = std::get<1>(RegisterMask);
  90. OS << '{';
  91. bool Comma = false;
  92. for (unsigned RI = 0, RE = 11; RI < RE; ++RI) {
  93. if (GPRMask & (1 << RI)) {
  94. if (Comma)
  95. OS << ", ";
  96. OS << GPRRegisterNames[RI];
  97. Comma = true;
  98. }
  99. }
  100. for (unsigned RI = 0, RE = 32; RI < RE; ++RI) {
  101. if (VFPMask & (1 << RI)) {
  102. if (Comma)
  103. OS << ", ";
  104. OS << "d" << unsigned(RI);
  105. Comma = true;
  106. }
  107. }
  108. for (unsigned RI = 11, RE = 16; RI < RE; ++RI) {
  109. if (GPRMask & (1 << RI)) {
  110. if (Comma)
  111. OS << ", ";
  112. OS << GPRRegisterNames[RI];
  113. Comma = true;
  114. }
  115. }
  116. OS << '}';
  117. }
  118. ErrorOr<object::SectionRef>
  119. Decoder::getSectionContaining(const COFFObjectFile &COFF, uint64_t VA) {
  120. for (const auto &Section : COFF.sections()) {
  121. uint64_t Address = Section.getAddress();
  122. uint64_t Size = Section.getSize();
  123. if (VA >= Address && (VA - Address) <= Size)
  124. return Section;
  125. }
  126. return readobj_error::unknown_symbol;
  127. }
  128. ErrorOr<object::SymbolRef> Decoder::getSymbol(const COFFObjectFile &COFF,
  129. uint64_t VA, bool FunctionOnly) {
  130. for (const auto &Symbol : COFF.symbols()) {
  131. if (FunctionOnly && Symbol.getType() != SymbolRef::ST_Function)
  132. continue;
  133. ErrorOr<uint64_t> Address = Symbol.getAddress();
  134. if (std::error_code EC = Address.getError())
  135. return EC;
  136. if (*Address == VA)
  137. return Symbol;
  138. }
  139. return readobj_error::unknown_symbol;
  140. }
  141. ErrorOr<SymbolRef> Decoder::getRelocatedSymbol(const COFFObjectFile &,
  142. const SectionRef &Section,
  143. uint64_t Offset) {
  144. for (const auto &Relocation : Section.relocations()) {
  145. uint64_t RelocationOffset = Relocation.getOffset();
  146. if (RelocationOffset == Offset)
  147. return *Relocation.getSymbol();
  148. }
  149. return readobj_error::unknown_symbol;
  150. }
  151. bool Decoder::opcode_0xxxxxxx(const uint8_t *OC, unsigned &Offset,
  152. unsigned Length, bool Prologue) {
  153. uint8_t Imm = OC[Offset] & 0x7f;
  154. SW.startLine() << format("0x%02x ; %s sp, #(%u * 4)\n",
  155. OC[Offset],
  156. static_cast<const char *>(Prologue ? "sub" : "add"),
  157. Imm);
  158. ++Offset;
  159. return false;
  160. }
  161. bool Decoder::opcode_10Lxxxxx(const uint8_t *OC, unsigned &Offset,
  162. unsigned Length, bool Prologue) {
  163. unsigned Link = (OC[Offset] & 0x20) >> 5;
  164. uint16_t RegisterMask = (Link << (Prologue ? 14 : 15))
  165. | ((OC[Offset + 0] & 0x1f) << 8)
  166. | ((OC[Offset + 1] & 0xff) << 0);
  167. assert((~RegisterMask & (1 << 13)) && "sp must not be set");
  168. assert((~RegisterMask & (1 << (Prologue ? 15 : 14))) && "pc must not be set");
  169. SW.startLine() << format("0x%02x 0x%02x ; %s.w ",
  170. OC[Offset + 0], OC[Offset + 1],
  171. Prologue ? "push" : "pop");
  172. printRegisters(std::make_pair(RegisterMask, 0));
  173. OS << '\n';
  174. ++Offset, ++Offset;
  175. return false;
  176. }
  177. bool Decoder::opcode_1100xxxx(const uint8_t *OC, unsigned &Offset,
  178. unsigned Length, bool Prologue) {
  179. if (Prologue)
  180. SW.startLine() << format("0x%02x ; mov r%u, sp\n",
  181. OC[Offset], OC[Offset] & 0xf);
  182. else
  183. SW.startLine() << format("0x%02x ; mov sp, r%u\n",
  184. OC[Offset], OC[Offset] & 0xf);
  185. ++Offset;
  186. return false;
  187. }
  188. bool Decoder::opcode_11010Lxx(const uint8_t *OC, unsigned &Offset,
  189. unsigned Length, bool Prologue) {
  190. unsigned Link = (OC[Offset] & 0x4) >> 3;
  191. unsigned Count = (OC[Offset] & 0x3);
  192. uint16_t GPRMask = (Link << (Prologue ? 14 : 15))
  193. | (((1 << (Count + 1)) - 1) << 4);
  194. SW.startLine() << format("0x%02x ; %s ", OC[Offset],
  195. Prologue ? "push" : "pop");
  196. printRegisters(std::make_pair(GPRMask, 0));
  197. OS << '\n';
  198. ++Offset;
  199. return false;
  200. }
  201. bool Decoder::opcode_11011Lxx(const uint8_t *OC, unsigned &Offset,
  202. unsigned Length, bool Prologue) {
  203. unsigned Link = (OC[Offset] & 0x4) >> 2;
  204. unsigned Count = (OC[Offset] & 0x3) + 4;
  205. uint16_t GPRMask = (Link << (Prologue ? 14 : 15))
  206. | (((1 << (Count + 1)) - 1) << 4);
  207. SW.startLine() << format("0x%02x ; %s.w ", OC[Offset],
  208. Prologue ? "push" : "pop");
  209. printRegisters(std::make_pair(GPRMask, 0));
  210. OS << '\n';
  211. ++Offset;
  212. return false;
  213. }
  214. bool Decoder::opcode_11100xxx(const uint8_t *OC, unsigned &Offset,
  215. unsigned Length, bool Prologue) {
  216. unsigned High = (OC[Offset] & 0x7);
  217. uint32_t VFPMask = (((1 << (High + 1)) - 1) << 8);
  218. SW.startLine() << format("0x%02x ; %s ", OC[Offset],
  219. Prologue ? "vpush" : "vpop");
  220. printRegisters(std::make_pair(0, VFPMask));
  221. OS << '\n';
  222. ++Offset;
  223. return false;
  224. }
  225. bool Decoder::opcode_111010xx(const uint8_t *OC, unsigned &Offset,
  226. unsigned Length, bool Prologue) {
  227. uint16_t Imm = ((OC[Offset + 0] & 0x03) << 8) | ((OC[Offset + 1] & 0xff) << 0);
  228. SW.startLine() << format("0x%02x 0x%02x ; %s.w sp, #(%u * 4)\n",
  229. OC[Offset + 0], OC[Offset + 1],
  230. static_cast<const char *>(Prologue ? "sub" : "add"),
  231. Imm);
  232. ++Offset, ++Offset;
  233. return false;
  234. }
  235. bool Decoder::opcode_1110110L(const uint8_t *OC, unsigned &Offset,
  236. unsigned Length, bool Prologue) {
  237. uint8_t GPRMask = ((OC[Offset + 0] & 0x01) << (Prologue ? 14 : 15))
  238. | ((OC[Offset + 1] & 0xff) << 0);
  239. SW.startLine() << format("0x%02x 0x%02x ; %s ", OC[Offset + 0],
  240. OC[Offset + 1], Prologue ? "push" : "pop");
  241. printRegisters(std::make_pair(GPRMask, 0));
  242. OS << '\n';
  243. ++Offset, ++Offset;
  244. return false;
  245. }
  246. bool Decoder::opcode_11101110(const uint8_t *OC, unsigned &Offset,
  247. unsigned Length, bool Prologue) {
  248. assert(!Prologue && "may not be used in prologue");
  249. if (OC[Offset + 1] & 0xf0)
  250. SW.startLine() << format("0x%02x 0x%02x ; reserved\n",
  251. OC[Offset + 0], OC[Offset + 1]);
  252. else
  253. SW.startLine()
  254. << format("0x%02x 0x%02x ; microsoft-specific (type: %u)\n",
  255. OC[Offset + 0], OC[Offset + 1], OC[Offset + 1] & 0x0f);
  256. ++Offset, ++Offset;
  257. return false;
  258. }
  259. bool Decoder::opcode_11101111(const uint8_t *OC, unsigned &Offset,
  260. unsigned Length, bool Prologue) {
  261. assert(!Prologue && "may not be used in prologue");
  262. if (OC[Offset + 1] & 0xf0)
  263. SW.startLine() << format("0x%02x 0x%02x ; reserved\n",
  264. OC[Offset + 0], OC[Offset + 1]);
  265. else
  266. SW.startLine()
  267. << format("0x%02x 0x%02x ; ldr.w lr, [sp], #%u\n",
  268. OC[Offset + 0], OC[Offset + 1], OC[Offset + 1] << 2);
  269. ++Offset, ++Offset;
  270. return false;
  271. }
  272. bool Decoder::opcode_11110101(const uint8_t *OC, unsigned &Offset,
  273. unsigned Length, bool Prologue) {
  274. unsigned Start = (OC[Offset + 1] & 0xf0) >> 4;
  275. unsigned End = (OC[Offset + 1] & 0x0f) >> 0;
  276. uint32_t VFPMask = ((1 << (End - Start)) - 1) << Start;
  277. SW.startLine() << format("0x%02x 0x%02x ; %s ", OC[Offset + 0],
  278. OC[Offset + 1], Prologue ? "vpush" : "vpop");
  279. printRegisters(std::make_pair(0, VFPMask));
  280. OS << '\n';
  281. ++Offset, ++Offset;
  282. return false;
  283. }
  284. bool Decoder::opcode_11110110(const uint8_t *OC, unsigned &Offset,
  285. unsigned Length, bool Prologue) {
  286. unsigned Start = (OC[Offset + 1] & 0xf0) >> 4;
  287. unsigned End = (OC[Offset + 1] & 0x0f) >> 0;
  288. uint32_t VFPMask = ((1 << (End - Start)) - 1) << 16;
  289. SW.startLine() << format("0x%02x 0x%02x ; %s ", OC[Offset + 0],
  290. OC[Offset + 1], Prologue ? "vpush" : "vpop");
  291. printRegisters(std::make_pair(0, VFPMask));
  292. OS << '\n';
  293. ++Offset, ++Offset;
  294. return false;
  295. }
  296. bool Decoder::opcode_11110111(const uint8_t *OC, unsigned &Offset,
  297. unsigned Length, bool Prologue) {
  298. uint32_t Imm = (OC[Offset + 1] << 8) | (OC[Offset + 2] << 0);
  299. SW.startLine() << format("0x%02x 0x%02x 0x%02x ; %s sp, sp, #(%u * 4)\n",
  300. OC[Offset + 0], OC[Offset + 1], OC[Offset + 2],
  301. static_cast<const char *>(Prologue ? "sub" : "add"),
  302. Imm);
  303. ++Offset, ++Offset, ++Offset;
  304. return false;
  305. }
  306. bool Decoder::opcode_11111000(const uint8_t *OC, unsigned &Offset,
  307. unsigned Length, bool Prologue) {
  308. uint32_t Imm = (OC[Offset + 1] << 16)
  309. | (OC[Offset + 2] << 8)
  310. | (OC[Offset + 3] << 0);
  311. SW.startLine()
  312. << format("0x%02x 0x%02x 0x%02x 0x%02x ; %s sp, sp, #(%u * 4)\n",
  313. OC[Offset + 0], OC[Offset + 1], OC[Offset + 2], OC[Offset + 3],
  314. static_cast<const char *>(Prologue ? "sub" : "add"), Imm);
  315. ++Offset, ++Offset, ++Offset, ++Offset;
  316. return false;
  317. }
  318. bool Decoder::opcode_11111001(const uint8_t *OC, unsigned &Offset,
  319. unsigned Length, bool Prologue) {
  320. uint32_t Imm = (OC[Offset + 1] << 8) | (OC[Offset + 2] << 0);
  321. SW.startLine()
  322. << format("0x%02x 0x%02x 0x%02x ; %s.w sp, sp, #(%u * 4)\n",
  323. OC[Offset + 0], OC[Offset + 1], OC[Offset + 2],
  324. static_cast<const char *>(Prologue ? "sub" : "add"), Imm);
  325. ++Offset, ++Offset, ++Offset;
  326. return false;
  327. }
  328. bool Decoder::opcode_11111010(const uint8_t *OC, unsigned &Offset,
  329. unsigned Length, bool Prologue) {
  330. uint32_t Imm = (OC[Offset + 1] << 16)
  331. | (OC[Offset + 2] << 8)
  332. | (OC[Offset + 3] << 0);
  333. SW.startLine()
  334. << format("0x%02x 0x%02x 0x%02x 0x%02x ; %s.w sp, sp, #(%u * 4)\n",
  335. OC[Offset + 0], OC[Offset + 1], OC[Offset + 2], OC[Offset + 3],
  336. static_cast<const char *>(Prologue ? "sub" : "add"), Imm);
  337. ++Offset, ++Offset, ++Offset, ++Offset;
  338. return false;
  339. }
  340. bool Decoder::opcode_11111011(const uint8_t *OC, unsigned &Offset,
  341. unsigned Length, bool Prologue) {
  342. SW.startLine() << format("0x%02x ; nop\n", OC[Offset]);
  343. ++Offset;
  344. return false;
  345. }
  346. bool Decoder::opcode_11111100(const uint8_t *OC, unsigned &Offset,
  347. unsigned Length, bool Prologue) {
  348. SW.startLine() << format("0x%02x ; nop.w\n", OC[Offset]);
  349. ++Offset;
  350. return false;
  351. }
  352. bool Decoder::opcode_11111101(const uint8_t *OC, unsigned &Offset,
  353. unsigned Length, bool Prologue) {
  354. SW.startLine() << format("0x%02x ; b\n", OC[Offset]);
  355. ++Offset;
  356. return true;
  357. }
  358. bool Decoder::opcode_11111110(const uint8_t *OC, unsigned &Offset,
  359. unsigned Length, bool Prologue) {
  360. SW.startLine() << format("0x%02x ; b.w\n", OC[Offset]);
  361. ++Offset;
  362. return true;
  363. }
  364. bool Decoder::opcode_11111111(const uint8_t *OC, unsigned &Offset,
  365. unsigned Length, bool Prologue) {
  366. ++Offset;
  367. return true;
  368. }
  369. void Decoder::decodeOpcodes(ArrayRef<uint8_t> Opcodes, unsigned Offset,
  370. bool Prologue) {
  371. assert((!Prologue || Offset == 0) && "prologue should always use offset 0");
  372. bool Terminated = false;
  373. for (unsigned OI = Offset, OE = Opcodes.size(); !Terminated && OI < OE; ) {
  374. for (unsigned DI = 0;; ++DI) {
  375. if ((Opcodes[OI] & Ring[DI].Mask) == Ring[DI].Value) {
  376. Terminated = (this->*Ring[DI].Routine)(Opcodes.data(), OI, 0, Prologue);
  377. break;
  378. }
  379. assert(DI < array_lengthof(Ring) && "unhandled opcode");
  380. }
  381. }
  382. }
  383. bool Decoder::dumpXDataRecord(const COFFObjectFile &COFF,
  384. const SectionRef &Section,
  385. uint64_t FunctionAddress, uint64_t VA) {
  386. ArrayRef<uint8_t> Contents;
  387. if (COFF.getSectionContents(COFF.getCOFFSection(Section), Contents))
  388. return false;
  389. uint64_t SectionVA = Section.getAddress();
  390. uint64_t Offset = VA - SectionVA;
  391. const ulittle32_t *Data =
  392. reinterpret_cast<const ulittle32_t *>(Contents.data() + Offset);
  393. const ExceptionDataRecord XData(Data);
  394. DictScope XRS(SW, "ExceptionData");
  395. SW.printNumber("FunctionLength", XData.FunctionLength() << 1);
  396. SW.printNumber("Version", XData.Vers());
  397. SW.printBoolean("ExceptionData", XData.X());
  398. SW.printBoolean("EpiloguePacked", XData.E());
  399. SW.printBoolean("Fragment", XData.F());
  400. SW.printNumber(XData.E() ? "EpilogueOffset" : "EpilogueScopes",
  401. XData.EpilogueCount());
  402. SW.printNumber("ByteCodeLength",
  403. static_cast<uint64_t>(XData.CodeWords() * sizeof(uint32_t)));
  404. if (XData.E()) {
  405. ArrayRef<uint8_t> UC = XData.UnwindByteCode();
  406. if (!XData.F()) {
  407. ListScope PS(SW, "Prologue");
  408. decodeOpcodes(UC, 0, /*Prologue=*/true);
  409. }
  410. if (XData.EpilogueCount()) {
  411. ListScope ES(SW, "Epilogue");
  412. decodeOpcodes(UC, XData.EpilogueCount(), /*Prologue=*/false);
  413. }
  414. } else {
  415. ArrayRef<ulittle32_t> EpilogueScopes = XData.EpilogueScopes();
  416. ListScope ESS(SW, "EpilogueScopes");
  417. for (const EpilogueScope ES : EpilogueScopes) {
  418. DictScope ESES(SW, "EpilogueScope");
  419. SW.printNumber("StartOffset", ES.EpilogueStartOffset());
  420. SW.printNumber("Condition", ES.Condition());
  421. SW.printNumber("EpilogueStartIndex", ES.EpilogueStartIndex());
  422. ListScope Opcodes(SW, "Opcodes");
  423. decodeOpcodes(XData.UnwindByteCode(), ES.EpilogueStartIndex(),
  424. /*Prologue=*/false);
  425. }
  426. }
  427. if (XData.X()) {
  428. const uint32_t Address = XData.ExceptionHandlerRVA();
  429. const uint32_t Parameter = XData.ExceptionHandlerParameter();
  430. const size_t HandlerOffset = HeaderWords(XData)
  431. + (XData.E() ? 0 : XData.EpilogueCount())
  432. + XData.CodeWords();
  433. ErrorOr<SymbolRef> Symbol =
  434. getRelocatedSymbol(COFF, Section, HandlerOffset * sizeof(uint32_t));
  435. if (!Symbol)
  436. Symbol = getSymbol(COFF, Address, /*FunctionOnly=*/true);
  437. ErrorOr<StringRef> Name = Symbol->getName();
  438. if (std::error_code EC = Name.getError())
  439. report_fatal_error(EC.message());
  440. ListScope EHS(SW, "ExceptionHandler");
  441. SW.printString("Routine", formatSymbol(*Name, Address));
  442. SW.printHex("Parameter", Parameter);
  443. }
  444. return true;
  445. }
  446. bool Decoder::dumpUnpackedEntry(const COFFObjectFile &COFF,
  447. const SectionRef Section, uint64_t Offset,
  448. unsigned Index, const RuntimeFunction &RF) {
  449. assert(RF.Flag() == RuntimeFunctionFlag::RFF_Unpacked &&
  450. "packed entry cannot be treated as an unpacked entry");
  451. ErrorOr<SymbolRef> Function = getRelocatedSymbol(COFF, Section, Offset);
  452. if (!Function)
  453. Function = getSymbol(COFF, RF.BeginAddress, /*FunctionOnly=*/true);
  454. ErrorOr<SymbolRef> XDataRecord = getRelocatedSymbol(COFF, Section, Offset + 4);
  455. if (!XDataRecord)
  456. XDataRecord = getSymbol(COFF, RF.ExceptionInformationRVA());
  457. if (!RF.BeginAddress && !Function)
  458. return false;
  459. if (!RF.UnwindData && !XDataRecord)
  460. return false;
  461. StringRef FunctionName;
  462. uint64_t FunctionAddress;
  463. if (Function) {
  464. ErrorOr<StringRef> FunctionNameOrErr = Function->getName();
  465. if (std::error_code EC = FunctionNameOrErr.getError())
  466. report_fatal_error(EC.message());
  467. FunctionName = *FunctionNameOrErr;
  468. ErrorOr<uint64_t> FunctionAddressOrErr = Function->getAddress();
  469. if (std::error_code EC = FunctionAddressOrErr.getError())
  470. report_fatal_error(EC.message());
  471. FunctionAddress = *FunctionAddressOrErr;
  472. } else {
  473. const pe32_header *PEHeader;
  474. if (COFF.getPE32Header(PEHeader))
  475. return false;
  476. FunctionAddress = PEHeader->ImageBase + RF.BeginAddress;
  477. }
  478. SW.printString("Function", formatSymbol(FunctionName, FunctionAddress));
  479. if (XDataRecord) {
  480. ErrorOr<StringRef> Name = XDataRecord->getName();
  481. if (std::error_code EC = Name.getError())
  482. report_fatal_error(EC.message());
  483. ErrorOr<uint64_t> AddressOrErr = XDataRecord->getAddress();
  484. if (std::error_code EC = AddressOrErr.getError())
  485. report_fatal_error(EC.message());
  486. uint64_t Address = *AddressOrErr;
  487. SW.printString("ExceptionRecord", formatSymbol(*Name, Address));
  488. section_iterator SI = COFF.section_end();
  489. if (XDataRecord->getSection(SI))
  490. return false;
  491. return dumpXDataRecord(COFF, *SI, FunctionAddress, Address);
  492. } else {
  493. const pe32_header *PEHeader;
  494. if (COFF.getPE32Header(PEHeader))
  495. return false;
  496. uint64_t Address = PEHeader->ImageBase + RF.ExceptionInformationRVA();
  497. SW.printString("ExceptionRecord", formatSymbol("", Address));
  498. ErrorOr<SectionRef> Section =
  499. getSectionContaining(COFF, RF.ExceptionInformationRVA());
  500. if (!Section)
  501. return false;
  502. return dumpXDataRecord(COFF, *Section, FunctionAddress,
  503. RF.ExceptionInformationRVA());
  504. }
  505. }
  506. bool Decoder::dumpPackedEntry(const object::COFFObjectFile &COFF,
  507. const SectionRef Section, uint64_t Offset,
  508. unsigned Index, const RuntimeFunction &RF) {
  509. assert((RF.Flag() == RuntimeFunctionFlag::RFF_Packed ||
  510. RF.Flag() == RuntimeFunctionFlag::RFF_PackedFragment) &&
  511. "unpacked entry cannot be treated as a packed entry");
  512. ErrorOr<SymbolRef> Function = getRelocatedSymbol(COFF, Section, Offset);
  513. if (!Function)
  514. Function = getSymbol(COFF, RF.BeginAddress, /*FunctionOnly=*/true);
  515. StringRef FunctionName;
  516. uint64_t FunctionAddress;
  517. if (Function) {
  518. ErrorOr<StringRef> FunctionNameOrErr = Function->getName();
  519. if (std::error_code EC = FunctionNameOrErr.getError())
  520. report_fatal_error(EC.message());
  521. FunctionName = *FunctionNameOrErr;
  522. ErrorOr<uint64_t> FunctionAddressOrErr = Function->getAddress();
  523. FunctionAddress = *FunctionAddressOrErr;
  524. } else {
  525. const pe32_header *PEHeader;
  526. if (COFF.getPE32Header(PEHeader))
  527. return false;
  528. FunctionAddress = PEHeader->ImageBase + RF.BeginAddress;
  529. }
  530. SW.printString("Function", formatSymbol(FunctionName, FunctionAddress));
  531. SW.printBoolean("Fragment",
  532. RF.Flag() == RuntimeFunctionFlag::RFF_PackedFragment);
  533. SW.printNumber("FunctionLength", RF.FunctionLength());
  534. SW.startLine() << "ReturnType: " << RF.Ret() << '\n';
  535. SW.printBoolean("HomedParameters", RF.H());
  536. SW.startLine() << "SavedRegisters: ";
  537. printRegisters(SavedRegisterMask(RF));
  538. OS << '\n';
  539. SW.printNumber("StackAdjustment", StackAdjustment(RF) << 2);
  540. return true;
  541. }
  542. bool Decoder::dumpProcedureDataEntry(const COFFObjectFile &COFF,
  543. const SectionRef Section, unsigned Index,
  544. ArrayRef<uint8_t> Contents) {
  545. uint64_t Offset = PDataEntrySize * Index;
  546. const ulittle32_t *Data =
  547. reinterpret_cast<const ulittle32_t *>(Contents.data() + Offset);
  548. const RuntimeFunction Entry(Data);
  549. DictScope RFS(SW, "RuntimeFunction");
  550. if (Entry.Flag() == RuntimeFunctionFlag::RFF_Unpacked)
  551. return dumpUnpackedEntry(COFF, Section, Offset, Index, Entry);
  552. return dumpPackedEntry(COFF, Section, Offset, Index, Entry);
  553. }
  554. void Decoder::dumpProcedureData(const COFFObjectFile &COFF,
  555. const SectionRef Section) {
  556. ArrayRef<uint8_t> Contents;
  557. if (COFF.getSectionContents(COFF.getCOFFSection(Section), Contents))
  558. return;
  559. if (Contents.size() % PDataEntrySize) {
  560. errs() << ".pdata content is not " << PDataEntrySize << "-byte aligned\n";
  561. return;
  562. }
  563. for (unsigned EI = 0, EE = Contents.size() / PDataEntrySize; EI < EE; ++EI)
  564. if (!dumpProcedureDataEntry(COFF, Section, EI, Contents))
  565. break;
  566. }
  567. std::error_code Decoder::dumpProcedureData(const COFFObjectFile &COFF) {
  568. for (const auto &Section : COFF.sections()) {
  569. StringRef SectionName;
  570. if (std::error_code EC =
  571. COFF.getSectionName(COFF.getCOFFSection(Section), SectionName))
  572. return EC;
  573. if (SectionName.startswith(".pdata"))
  574. dumpProcedureData(COFF, Section);
  575. }
  576. return std::error_code();
  577. }
  578. }
  579. }
  580. }