Win64EHDumper.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. //===- Win64EHDumper.cpp - Win64 EH 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. #include "Win64EHDumper.h"
  9. #include "llvm-readobj.h"
  10. #include "llvm/Object/COFF.h"
  11. #include "llvm/Support/ErrorHandling.h"
  12. #include "llvm/Support/Format.h"
  13. using namespace llvm;
  14. using namespace llvm::object;
  15. using namespace llvm::Win64EH;
  16. static const EnumEntry<unsigned> UnwindFlags[] = {
  17. { "ExceptionHandler", UNW_ExceptionHandler },
  18. { "TerminateHandler", UNW_TerminateHandler },
  19. { "ChainInfo" , UNW_ChainInfo }
  20. };
  21. static const EnumEntry<unsigned> UnwindOpInfo[] = {
  22. { "RAX", 0 },
  23. { "RCX", 1 },
  24. { "RDX", 2 },
  25. { "RBX", 3 },
  26. { "RSP", 4 },
  27. { "RBP", 5 },
  28. { "RSI", 6 },
  29. { "RDI", 7 },
  30. { "R8", 8 },
  31. { "R9", 9 },
  32. { "R10", 10 },
  33. { "R11", 11 },
  34. { "R12", 12 },
  35. { "R13", 13 },
  36. { "R14", 14 },
  37. { "R15", 15 }
  38. };
  39. static uint64_t getOffsetOfLSDA(const UnwindInfo& UI) {
  40. return static_cast<const char*>(UI.getLanguageSpecificData())
  41. - reinterpret_cast<const char*>(&UI);
  42. }
  43. static uint32_t getLargeSlotValue(ArrayRef<UnwindCode> UC) {
  44. if (UC.size() < 3)
  45. return 0;
  46. return UC[1].FrameOffset + (static_cast<uint32_t>(UC[2].FrameOffset) << 16);
  47. }
  48. // Returns the name of the unwind code.
  49. static StringRef getUnwindCodeTypeName(uint8_t Code) {
  50. switch (Code) {
  51. default: llvm_unreachable("Invalid unwind code");
  52. case UOP_PushNonVol: return "PUSH_NONVOL";
  53. case UOP_AllocLarge: return "ALLOC_LARGE";
  54. case UOP_AllocSmall: return "ALLOC_SMALL";
  55. case UOP_SetFPReg: return "SET_FPREG";
  56. case UOP_SaveNonVol: return "SAVE_NONVOL";
  57. case UOP_SaveNonVolBig: return "SAVE_NONVOL_FAR";
  58. case UOP_SaveXMM128: return "SAVE_XMM128";
  59. case UOP_SaveXMM128Big: return "SAVE_XMM128_FAR";
  60. case UOP_PushMachFrame: return "PUSH_MACHFRAME";
  61. }
  62. }
  63. // Returns the name of a referenced register.
  64. static StringRef getUnwindRegisterName(uint8_t Reg) {
  65. switch (Reg) {
  66. default: llvm_unreachable("Invalid register");
  67. case 0: return "RAX";
  68. case 1: return "RCX";
  69. case 2: return "RDX";
  70. case 3: return "RBX";
  71. case 4: return "RSP";
  72. case 5: return "RBP";
  73. case 6: return "RSI";
  74. case 7: return "RDI";
  75. case 8: return "R8";
  76. case 9: return "R9";
  77. case 10: return "R10";
  78. case 11: return "R11";
  79. case 12: return "R12";
  80. case 13: return "R13";
  81. case 14: return "R14";
  82. case 15: return "R15";
  83. }
  84. }
  85. // Calculates the number of array slots required for the unwind code.
  86. static unsigned getNumUsedSlots(const UnwindCode &UnwindCode) {
  87. switch (UnwindCode.getUnwindOp()) {
  88. default: llvm_unreachable("Invalid unwind code");
  89. case UOP_PushNonVol:
  90. case UOP_AllocSmall:
  91. case UOP_SetFPReg:
  92. case UOP_PushMachFrame:
  93. return 1;
  94. case UOP_SaveNonVol:
  95. case UOP_SaveXMM128:
  96. return 2;
  97. case UOP_SaveNonVolBig:
  98. case UOP_SaveXMM128Big:
  99. return 3;
  100. case UOP_AllocLarge:
  101. return (UnwindCode.getOpInfo() == 0) ? 2 : 3;
  102. }
  103. }
  104. static std::error_code getSymbol(const COFFObjectFile &COFF, uint64_t VA,
  105. object::SymbolRef &Sym) {
  106. for (const auto &Symbol : COFF.symbols()) {
  107. Expected<uint64_t> Address = Symbol.getAddress();
  108. if (!Address)
  109. return errorToErrorCode(Address.takeError());
  110. if (*Address == VA) {
  111. Sym = Symbol;
  112. return std::error_code();
  113. }
  114. }
  115. return inconvertibleErrorCode();
  116. }
  117. static std::string formatSymbol(const Dumper::Context &Ctx,
  118. const coff_section *Section, uint64_t Offset,
  119. uint32_t Displacement) {
  120. std::string Buffer;
  121. raw_string_ostream OS(Buffer);
  122. SymbolRef Symbol;
  123. if (!Ctx.ResolveSymbol(Section, Offset, Symbol, Ctx.UserData)) {
  124. Expected<StringRef> Name = Symbol.getName();
  125. if (Name) {
  126. OS << *Name;
  127. if (Displacement > 0)
  128. OS << format(" +0x%X (0x%" PRIX64 ")", Displacement, Offset);
  129. else
  130. OS << format(" (0x%" PRIX64 ")", Offset);
  131. return OS.str();
  132. } else {
  133. // TODO: Actually report errors helpfully.
  134. consumeError(Name.takeError());
  135. }
  136. } else if (!getSymbol(Ctx.COFF, Ctx.COFF.getImageBase() + Displacement,
  137. Symbol)) {
  138. Expected<StringRef> Name = Symbol.getName();
  139. if (Name) {
  140. OS << *Name;
  141. OS << format(" (0x%" PRIX64 ")", Ctx.COFF.getImageBase() + Displacement);
  142. return OS.str();
  143. } else {
  144. consumeError(Name.takeError());
  145. }
  146. }
  147. if (Displacement > 0)
  148. OS << format("(0x%" PRIX64 ")", Ctx.COFF.getImageBase() + Displacement);
  149. else
  150. OS << format("(0x%" PRIX64 ")", Offset);
  151. return OS.str();
  152. }
  153. static std::error_code resolveRelocation(const Dumper::Context &Ctx,
  154. const coff_section *Section,
  155. uint64_t Offset,
  156. const coff_section *&ResolvedSection,
  157. uint64_t &ResolvedAddress) {
  158. SymbolRef Symbol;
  159. if (std::error_code EC =
  160. Ctx.ResolveSymbol(Section, Offset, Symbol, Ctx.UserData))
  161. return EC;
  162. Expected<uint64_t> ResolvedAddressOrErr = Symbol.getAddress();
  163. if (!ResolvedAddressOrErr)
  164. return errorToErrorCode(ResolvedAddressOrErr.takeError());
  165. ResolvedAddress = *ResolvedAddressOrErr;
  166. Expected<section_iterator> SI = Symbol.getSection();
  167. if (!SI)
  168. return errorToErrorCode(SI.takeError());
  169. ResolvedSection = Ctx.COFF.getCOFFSection(**SI);
  170. return std::error_code();
  171. }
  172. static const object::coff_section *
  173. getSectionContaining(const COFFObjectFile &COFF, uint64_t VA) {
  174. for (const auto &Section : COFF.sections()) {
  175. uint64_t Address = Section.getAddress();
  176. uint64_t Size = Section.getSize();
  177. if (VA >= Address && (VA - Address) <= Size)
  178. return COFF.getCOFFSection(Section);
  179. }
  180. return nullptr;
  181. }
  182. namespace llvm {
  183. namespace Win64EH {
  184. void Dumper::printRuntimeFunctionEntry(const Context &Ctx,
  185. const coff_section *Section,
  186. uint64_t Offset,
  187. const RuntimeFunction &RF) {
  188. SW.printString("StartAddress",
  189. formatSymbol(Ctx, Section, Offset + 0, RF.StartAddress));
  190. SW.printString("EndAddress",
  191. formatSymbol(Ctx, Section, Offset + 4, RF.EndAddress));
  192. SW.printString("UnwindInfoAddress",
  193. formatSymbol(Ctx, Section, Offset + 8, RF.UnwindInfoOffset));
  194. }
  195. // Prints one unwind code. Because an unwind code can occupy up to 3 slots in
  196. // the unwind codes array, this function requires that the correct number of
  197. // slots is provided.
  198. void Dumper::printUnwindCode(const UnwindInfo& UI, ArrayRef<UnwindCode> UC) {
  199. assert(UC.size() >= getNumUsedSlots(UC[0]));
  200. SW.startLine() << format("0x%02X: ", unsigned(UC[0].u.CodeOffset))
  201. << getUnwindCodeTypeName(UC[0].getUnwindOp());
  202. switch (UC[0].getUnwindOp()) {
  203. case UOP_PushNonVol:
  204. OS << " reg=" << getUnwindRegisterName(UC[0].getOpInfo());
  205. break;
  206. case UOP_AllocLarge:
  207. OS << " size="
  208. << ((UC[0].getOpInfo() == 0) ? UC[1].FrameOffset * 8
  209. : getLargeSlotValue(UC));
  210. break;
  211. case UOP_AllocSmall:
  212. OS << " size=" << (UC[0].getOpInfo() + 1) * 8;
  213. break;
  214. case UOP_SetFPReg:
  215. if (UI.getFrameRegister() == 0)
  216. OS << " reg=<invalid>";
  217. else
  218. OS << " reg=" << getUnwindRegisterName(UI.getFrameRegister())
  219. << format(", offset=0x%X", UI.getFrameOffset() * 16);
  220. break;
  221. case UOP_SaveNonVol:
  222. OS << " reg=" << getUnwindRegisterName(UC[0].getOpInfo())
  223. << format(", offset=0x%X", UC[1].FrameOffset * 8);
  224. break;
  225. case UOP_SaveNonVolBig:
  226. OS << " reg=" << getUnwindRegisterName(UC[0].getOpInfo())
  227. << format(", offset=0x%X", getLargeSlotValue(UC));
  228. break;
  229. case UOP_SaveXMM128:
  230. OS << " reg=XMM" << static_cast<uint32_t>(UC[0].getOpInfo())
  231. << format(", offset=0x%X", UC[1].FrameOffset * 16);
  232. break;
  233. case UOP_SaveXMM128Big:
  234. OS << " reg=XMM" << static_cast<uint32_t>(UC[0].getOpInfo())
  235. << format(", offset=0x%X", getLargeSlotValue(UC));
  236. break;
  237. case UOP_PushMachFrame:
  238. OS << " errcode=" << (UC[0].getOpInfo() == 0 ? "no" : "yes");
  239. break;
  240. }
  241. OS << "\n";
  242. }
  243. void Dumper::printUnwindInfo(const Context &Ctx, const coff_section *Section,
  244. off_t Offset, const UnwindInfo &UI) {
  245. DictScope UIS(SW, "UnwindInfo");
  246. SW.printNumber("Version", UI.getVersion());
  247. SW.printFlags("Flags", UI.getFlags(), makeArrayRef(UnwindFlags));
  248. SW.printNumber("PrologSize", UI.PrologSize);
  249. if (UI.getFrameRegister()) {
  250. SW.printEnum("FrameRegister", UI.getFrameRegister(),
  251. makeArrayRef(UnwindOpInfo));
  252. SW.printHex("FrameOffset", UI.getFrameOffset());
  253. } else {
  254. SW.printString("FrameRegister", StringRef("-"));
  255. SW.printString("FrameOffset", StringRef("-"));
  256. }
  257. SW.printNumber("UnwindCodeCount", UI.NumCodes);
  258. {
  259. ListScope UCS(SW, "UnwindCodes");
  260. ArrayRef<UnwindCode> UC(&UI.UnwindCodes[0], UI.NumCodes);
  261. for (const UnwindCode *UCI = UC.begin(), *UCE = UC.end(); UCI < UCE; ++UCI) {
  262. unsigned UsedSlots = getNumUsedSlots(*UCI);
  263. if (UsedSlots > UC.size()) {
  264. errs() << "corrupt unwind data";
  265. return;
  266. }
  267. printUnwindCode(UI, makeArrayRef(UCI, UCE));
  268. UCI = UCI + UsedSlots - 1;
  269. }
  270. }
  271. uint64_t LSDAOffset = Offset + getOffsetOfLSDA(UI);
  272. if (UI.getFlags() & (UNW_ExceptionHandler | UNW_TerminateHandler)) {
  273. SW.printString("Handler",
  274. formatSymbol(Ctx, Section, LSDAOffset,
  275. UI.getLanguageSpecificHandlerOffset()));
  276. } else if (UI.getFlags() & UNW_ChainInfo) {
  277. if (const RuntimeFunction *Chained = UI.getChainedFunctionEntry()) {
  278. DictScope CS(SW, "Chained");
  279. printRuntimeFunctionEntry(Ctx, Section, LSDAOffset, *Chained);
  280. }
  281. }
  282. }
  283. void Dumper::printRuntimeFunction(const Context &Ctx,
  284. const coff_section *Section,
  285. uint64_t SectionOffset,
  286. const RuntimeFunction &RF) {
  287. DictScope RFS(SW, "RuntimeFunction");
  288. printRuntimeFunctionEntry(Ctx, Section, SectionOffset, RF);
  289. const coff_section *XData = nullptr;
  290. uint64_t Offset;
  291. resolveRelocation(Ctx, Section, SectionOffset + 8, XData, Offset);
  292. Offset = Offset + RF.UnwindInfoOffset;
  293. if (!XData) {
  294. uint64_t Address = Ctx.COFF.getImageBase() + RF.UnwindInfoOffset;
  295. XData = getSectionContaining(Ctx.COFF, Address);
  296. if (!XData)
  297. return;
  298. Offset = RF.UnwindInfoOffset - XData->VirtualAddress;
  299. }
  300. ArrayRef<uint8_t> Contents;
  301. if (Error E = Ctx.COFF.getSectionContents(XData, Contents))
  302. reportError(std::move(E), Ctx.COFF.getFileName());
  303. if (Contents.empty())
  304. return;
  305. if (Offset > Contents.size())
  306. return;
  307. const auto UI = reinterpret_cast<const UnwindInfo*>(Contents.data() + Offset);
  308. printUnwindInfo(Ctx, XData, Offset, *UI);
  309. }
  310. void Dumper::printData(const Context &Ctx) {
  311. for (const auto &Section : Ctx.COFF.sections()) {
  312. StringRef Name;
  313. if (Expected<StringRef> NameOrErr = Section.getName())
  314. Name = *NameOrErr;
  315. else
  316. consumeError(NameOrErr.takeError());
  317. if (Name != ".pdata" && !Name.startswith(".pdata$"))
  318. continue;
  319. const coff_section *PData = Ctx.COFF.getCOFFSection(Section);
  320. ArrayRef<uint8_t> Contents;
  321. if (Error E = Ctx.COFF.getSectionContents(PData, Contents))
  322. reportError(std::move(E), Ctx.COFF.getFileName());
  323. if (Contents.empty())
  324. continue;
  325. const RuntimeFunction *Entries =
  326. reinterpret_cast<const RuntimeFunction *>(Contents.data());
  327. const size_t Count = Contents.size() / sizeof(RuntimeFunction);
  328. ArrayRef<RuntimeFunction> RuntimeFunctions(Entries, Count);
  329. size_t Index = 0;
  330. for (const auto &RF : RuntimeFunctions) {
  331. printRuntimeFunction(Ctx, Ctx.COFF.getCOFFSection(Section),
  332. Index * sizeof(RuntimeFunction), RF);
  333. ++Index;
  334. }
  335. }
  336. }
  337. }
  338. }