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