Win64EHDumper.cpp 10 KB

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