Michael J. Spencer | 0c6ec48 | 2012-12-05 20:12:35 +0000 | [diff] [blame] | 1 | //===-- COFFDump.cpp - COFF-specific dumper ---------------------*- 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 | /// |
| 10 | /// \file |
| 11 | /// \brief This file implements the COFF-specific dumper for llvm-objdump. |
| 12 | /// It outputs the Win64 EH data structures as plain text. |
Alp Toker | cb40291 | 2014-01-24 17:20:08 +0000 | [diff] [blame] | 13 | /// The encoding of the unwind codes is described in MSDN: |
Michael J. Spencer | 0c6ec48 | 2012-12-05 20:12:35 +0000 | [diff] [blame] | 14 | /// http://msdn.microsoft.com/en-us/library/ck9asaa9.aspx |
| 15 | /// |
| 16 | //===----------------------------------------------------------------------===// |
| 17 | |
| 18 | #include "llvm-objdump.h" |
| 19 | #include "llvm/Object/COFF.h" |
| 20 | #include "llvm/Object/ObjectFile.h" |
| 21 | #include "llvm/Support/Format.h" |
| 22 | #include "llvm/Support/SourceMgr.h" |
Chandler Carruth | b034cb7 | 2013-01-02 10:26:28 +0000 | [diff] [blame] | 23 | #include "llvm/Support/Win64EH.h" |
Michael J. Spencer | 0c6ec48 | 2012-12-05 20:12:35 +0000 | [diff] [blame] | 24 | #include "llvm/Support/raw_ostream.h" |
| 25 | #include "llvm/Support/system_error.h" |
Michael J. Spencer | 0c6ec48 | 2012-12-05 20:12:35 +0000 | [diff] [blame] | 26 | #include <algorithm> |
| 27 | #include <cstring> |
| 28 | |
| 29 | using namespace llvm; |
| 30 | using namespace object; |
| 31 | using namespace llvm::Win64EH; |
| 32 | |
| 33 | // Returns the name of the unwind code. |
| 34 | static StringRef getUnwindCodeTypeName(uint8_t Code) { |
| 35 | switch(Code) { |
| 36 | default: llvm_unreachable("Invalid unwind code"); |
| 37 | case UOP_PushNonVol: return "UOP_PushNonVol"; |
| 38 | case UOP_AllocLarge: return "UOP_AllocLarge"; |
| 39 | case UOP_AllocSmall: return "UOP_AllocSmall"; |
| 40 | case UOP_SetFPReg: return "UOP_SetFPReg"; |
| 41 | case UOP_SaveNonVol: return "UOP_SaveNonVol"; |
| 42 | case UOP_SaveNonVolBig: return "UOP_SaveNonVolBig"; |
| 43 | case UOP_SaveXMM128: return "UOP_SaveXMM128"; |
| 44 | case UOP_SaveXMM128Big: return "UOP_SaveXMM128Big"; |
| 45 | case UOP_PushMachFrame: return "UOP_PushMachFrame"; |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | // Returns the name of a referenced register. |
| 50 | static StringRef getUnwindRegisterName(uint8_t Reg) { |
| 51 | switch(Reg) { |
| 52 | default: llvm_unreachable("Invalid register"); |
| 53 | case 0: return "RAX"; |
| 54 | case 1: return "RCX"; |
| 55 | case 2: return "RDX"; |
| 56 | case 3: return "RBX"; |
| 57 | case 4: return "RSP"; |
| 58 | case 5: return "RBP"; |
| 59 | case 6: return "RSI"; |
| 60 | case 7: return "RDI"; |
| 61 | case 8: return "R8"; |
| 62 | case 9: return "R9"; |
| 63 | case 10: return "R10"; |
| 64 | case 11: return "R11"; |
| 65 | case 12: return "R12"; |
| 66 | case 13: return "R13"; |
| 67 | case 14: return "R14"; |
| 68 | case 15: return "R15"; |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | // Calculates the number of array slots required for the unwind code. |
| 73 | static unsigned getNumUsedSlots(const UnwindCode &UnwindCode) { |
| 74 | switch (UnwindCode.getUnwindOp()) { |
| 75 | default: llvm_unreachable("Invalid unwind code"); |
| 76 | case UOP_PushNonVol: |
| 77 | case UOP_AllocSmall: |
| 78 | case UOP_SetFPReg: |
| 79 | case UOP_PushMachFrame: |
| 80 | return 1; |
| 81 | case UOP_SaveNonVol: |
| 82 | case UOP_SaveXMM128: |
| 83 | return 2; |
| 84 | case UOP_SaveNonVolBig: |
| 85 | case UOP_SaveXMM128Big: |
| 86 | return 3; |
| 87 | case UOP_AllocLarge: |
| 88 | return (UnwindCode.getOpInfo() == 0) ? 2 : 3; |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | // Prints one unwind code. Because an unwind code can occupy up to 3 slots in |
| 93 | // the unwind codes array, this function requires that the correct number of |
| 94 | // slots is provided. |
| 95 | static void printUnwindCode(ArrayRef<UnwindCode> UCs) { |
| 96 | assert(UCs.size() >= getNumUsedSlots(UCs[0])); |
Rui Ueyama | 595932f | 2014-03-04 19:23:56 +0000 | [diff] [blame] | 97 | outs() << format(" 0x%02x: ", unsigned(UCs[0].u.CodeOffset)) |
Michael J. Spencer | 0c6ec48 | 2012-12-05 20:12:35 +0000 | [diff] [blame] | 98 | << getUnwindCodeTypeName(UCs[0].getUnwindOp()); |
| 99 | switch (UCs[0].getUnwindOp()) { |
| 100 | case UOP_PushNonVol: |
| 101 | outs() << " " << getUnwindRegisterName(UCs[0].getOpInfo()); |
| 102 | break; |
| 103 | case UOP_AllocLarge: |
| 104 | if (UCs[0].getOpInfo() == 0) { |
| 105 | outs() << " " << UCs[1].FrameOffset; |
| 106 | } else { |
| 107 | outs() << " " << UCs[1].FrameOffset |
| 108 | + (static_cast<uint32_t>(UCs[2].FrameOffset) << 16); |
| 109 | } |
| 110 | break; |
| 111 | case UOP_AllocSmall: |
| 112 | outs() << " " << ((UCs[0].getOpInfo() + 1) * 8); |
| 113 | break; |
| 114 | case UOP_SetFPReg: |
| 115 | outs() << " "; |
| 116 | break; |
| 117 | case UOP_SaveNonVol: |
| 118 | outs() << " " << getUnwindRegisterName(UCs[0].getOpInfo()) |
| 119 | << format(" [0x%04x]", 8 * UCs[1].FrameOffset); |
| 120 | break; |
| 121 | case UOP_SaveNonVolBig: |
| 122 | outs() << " " << getUnwindRegisterName(UCs[0].getOpInfo()) |
| 123 | << format(" [0x%08x]", UCs[1].FrameOffset |
| 124 | + (static_cast<uint32_t>(UCs[2].FrameOffset) << 16)); |
| 125 | break; |
| 126 | case UOP_SaveXMM128: |
| 127 | outs() << " XMM" << static_cast<uint32_t>(UCs[0].getOpInfo()) |
| 128 | << format(" [0x%04x]", 16 * UCs[1].FrameOffset); |
| 129 | break; |
| 130 | case UOP_SaveXMM128Big: |
| 131 | outs() << " XMM" << UCs[0].getOpInfo() |
| 132 | << format(" [0x%08x]", UCs[1].FrameOffset |
| 133 | + (static_cast<uint32_t>(UCs[2].FrameOffset) << 16)); |
| 134 | break; |
| 135 | case UOP_PushMachFrame: |
| 136 | outs() << " " << (UCs[0].getOpInfo() ? "w/o" : "w") |
| 137 | << " error code"; |
| 138 | break; |
| 139 | } |
| 140 | outs() << "\n"; |
| 141 | } |
| 142 | |
| 143 | static void printAllUnwindCodes(ArrayRef<UnwindCode> UCs) { |
| 144 | for (const UnwindCode *I = UCs.begin(), *E = UCs.end(); I < E; ) { |
| 145 | unsigned UsedSlots = getNumUsedSlots(*I); |
| 146 | if (UsedSlots > UCs.size()) { |
| 147 | outs() << "Unwind data corrupted: Encountered unwind op " |
| 148 | << getUnwindCodeTypeName((*I).getUnwindOp()) |
| 149 | << " which requires " << UsedSlots |
| 150 | << " slots, but only " << UCs.size() |
| 151 | << " remaining in buffer"; |
| 152 | return ; |
| 153 | } |
| 154 | printUnwindCode(ArrayRef<UnwindCode>(I, E)); |
| 155 | I += UsedSlots; |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | // Given a symbol sym this functions returns the address and section of it. |
| 160 | static error_code resolveSectionAndAddress(const COFFObjectFile *Obj, |
| 161 | const SymbolRef &Sym, |
| 162 | const coff_section *&ResolvedSection, |
| 163 | uint64_t &ResolvedAddr) { |
Rui Ueyama | ef8dede | 2014-01-16 20:57:55 +0000 | [diff] [blame] | 164 | if (error_code EC = Sym.getAddress(ResolvedAddr)) |
| 165 | return EC; |
Rafael Espindola | b5155a5 | 2014-02-10 20:24:04 +0000 | [diff] [blame] | 166 | section_iterator iter(Obj->section_begin()); |
Rui Ueyama | ef8dede | 2014-01-16 20:57:55 +0000 | [diff] [blame] | 167 | if (error_code EC = Sym.getSection(iter)) |
| 168 | return EC; |
Michael J. Spencer | 0c6ec48 | 2012-12-05 20:12:35 +0000 | [diff] [blame] | 169 | ResolvedSection = Obj->getCOFFSection(iter); |
| 170 | return object_error::success; |
| 171 | } |
| 172 | |
| 173 | // Given a vector of relocations for a section and an offset into this section |
| 174 | // the function returns the symbol used for the relocation at the offset. |
| 175 | static error_code resolveSymbol(const std::vector<RelocationRef> &Rels, |
| 176 | uint64_t Offset, SymbolRef &Sym) { |
| 177 | for (std::vector<RelocationRef>::const_iterator I = Rels.begin(), |
| 178 | E = Rels.end(); |
| 179 | I != E; ++I) { |
| 180 | uint64_t Ofs; |
Rui Ueyama | ef8dede | 2014-01-16 20:57:55 +0000 | [diff] [blame] | 181 | if (error_code EC = I->getOffset(Ofs)) |
| 182 | return EC; |
Michael J. Spencer | 0c6ec48 | 2012-12-05 20:12:35 +0000 | [diff] [blame] | 183 | if (Ofs == Offset) { |
Rafael Espindola | 806f006 | 2013-06-05 01:33:53 +0000 | [diff] [blame] | 184 | Sym = *I->getSymbol(); |
Rui Ueyama | cf39784 | 2014-02-28 05:21:29 +0000 | [diff] [blame] | 185 | return object_error::success; |
Michael J. Spencer | 0c6ec48 | 2012-12-05 20:12:35 +0000 | [diff] [blame] | 186 | } |
| 187 | } |
Rui Ueyama | cf39784 | 2014-02-28 05:21:29 +0000 | [diff] [blame] | 188 | return object_error::parse_failed; |
Michael J. Spencer | 0c6ec48 | 2012-12-05 20:12:35 +0000 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | // Given a vector of relocations for a section and an offset into this section |
| 192 | // the function resolves the symbol used for the relocation at the offset and |
| 193 | // returns the section content and the address inside the content pointed to |
| 194 | // by the symbol. |
| 195 | static error_code getSectionContents(const COFFObjectFile *Obj, |
| 196 | const std::vector<RelocationRef> &Rels, |
| 197 | uint64_t Offset, |
| 198 | ArrayRef<uint8_t> &Contents, |
| 199 | uint64_t &Addr) { |
| 200 | SymbolRef Sym; |
Rui Ueyama | 1618fe2 | 2014-02-28 05:21:26 +0000 | [diff] [blame] | 201 | if (error_code EC = resolveSymbol(Rels, Offset, Sym)) |
| 202 | return EC; |
Michael J. Spencer | 0c6ec48 | 2012-12-05 20:12:35 +0000 | [diff] [blame] | 203 | const coff_section *Section; |
Rui Ueyama | ef8dede | 2014-01-16 20:57:55 +0000 | [diff] [blame] | 204 | if (error_code EC = resolveSectionAndAddress(Obj, Sym, Section, Addr)) |
| 205 | return EC; |
| 206 | if (error_code EC = Obj->getSectionContents(Section, Contents)) |
| 207 | return EC; |
Michael J. Spencer | 0c6ec48 | 2012-12-05 20:12:35 +0000 | [diff] [blame] | 208 | return object_error::success; |
| 209 | } |
| 210 | |
| 211 | // Given a vector of relocations for a section and an offset into this section |
| 212 | // the function returns the name of the symbol used for the relocation at the |
| 213 | // offset. |
| 214 | static error_code resolveSymbolName(const std::vector<RelocationRef> &Rels, |
| 215 | uint64_t Offset, StringRef &Name) { |
| 216 | SymbolRef Sym; |
Rui Ueyama | ef8dede | 2014-01-16 20:57:55 +0000 | [diff] [blame] | 217 | if (error_code EC = resolveSymbol(Rels, Offset, Sym)) |
| 218 | return EC; |
| 219 | if (error_code EC = Sym.getName(Name)) |
| 220 | return EC; |
Michael J. Spencer | 0c6ec48 | 2012-12-05 20:12:35 +0000 | [diff] [blame] | 221 | return object_error::success; |
| 222 | } |
| 223 | |
| 224 | static void printCOFFSymbolAddress(llvm::raw_ostream &Out, |
| 225 | const std::vector<RelocationRef> &Rels, |
| 226 | uint64_t Offset, uint32_t Disp) { |
| 227 | StringRef Sym; |
Rui Ueyama | cf39784 | 2014-02-28 05:21:29 +0000 | [diff] [blame] | 228 | if (!resolveSymbolName(Rels, Offset, Sym)) { |
| 229 | Out << Sym; |
| 230 | if (Disp > 0) |
| 231 | Out << format(" + 0x%04x", Disp); |
| 232 | } else { |
| 233 | Out << format("0x%04x", Disp); |
| 234 | } |
Michael J. Spencer | 0c6ec48 | 2012-12-05 20:12:35 +0000 | [diff] [blame] | 235 | } |
| 236 | |
Rui Ueyama | 215a586 | 2014-02-20 06:51:07 +0000 | [diff] [blame] | 237 | static void |
| 238 | printSEHTable(const COFFObjectFile *Obj, uint32_t TableVA, int Count) { |
| 239 | if (Count == 0) |
| 240 | return; |
| 241 | |
| 242 | const pe32_header *PE32Header; |
| 243 | if (error(Obj->getPE32Header(PE32Header))) |
| 244 | return; |
| 245 | uint32_t ImageBase = PE32Header->ImageBase; |
| 246 | uintptr_t IntPtr = 0; |
| 247 | if (error(Obj->getVaPtr(TableVA, IntPtr))) |
| 248 | return; |
| 249 | const support::ulittle32_t *P = (const support::ulittle32_t *)IntPtr; |
| 250 | outs() << "SEH Table:"; |
| 251 | for (int I = 0; I < Count; ++I) |
| 252 | outs() << format(" 0x%x", P[I] + ImageBase); |
| 253 | outs() << "\n\n"; |
| 254 | } |
| 255 | |
Rui Ueyama | c514a80 | 2014-02-19 03:53:11 +0000 | [diff] [blame] | 256 | static void printLoadConfiguration(const COFFObjectFile *Obj) { |
Rui Ueyama | 5cf3285 | 2014-02-21 20:27:15 +0000 | [diff] [blame] | 257 | // Skip if it's not executable. |
| 258 | const pe32_header *PE32Header; |
| 259 | if (error(Obj->getPE32Header(PE32Header))) |
| 260 | return; |
| 261 | if (!PE32Header) |
| 262 | return; |
| 263 | |
Rui Ueyama | c514a80 | 2014-02-19 03:53:11 +0000 | [diff] [blame] | 264 | const coff_file_header *Header; |
| 265 | if (error(Obj->getCOFFHeader(Header))) |
| 266 | return; |
| 267 | // Currently only x86 is supported |
| 268 | if (Header->Machine != COFF::IMAGE_FILE_MACHINE_I386) |
| 269 | return; |
| 270 | |
| 271 | const data_directory *DataDir; |
| 272 | if (error(Obj->getDataDirectory(COFF::LOAD_CONFIG_TABLE, DataDir))) |
| 273 | return; |
| 274 | uintptr_t IntPtr = 0; |
| 275 | if (DataDir->RelativeVirtualAddress == 0) |
| 276 | return; |
| 277 | if (error(Obj->getRvaPtr(DataDir->RelativeVirtualAddress, IntPtr))) |
| 278 | return; |
Rui Ueyama | 215a586 | 2014-02-20 06:51:07 +0000 | [diff] [blame] | 279 | |
Rui Ueyama | 8c1d4c9 | 2014-03-04 04:15:59 +0000 | [diff] [blame] | 280 | auto *LoadConf = reinterpret_cast<const coff_load_configuration32 *>(IntPtr); |
Rui Ueyama | c514a80 | 2014-02-19 03:53:11 +0000 | [diff] [blame] | 281 | outs() << "Load configuration:" |
| 282 | << "\n Timestamp: " << LoadConf->TimeDateStamp |
| 283 | << "\n Major Version: " << LoadConf->MajorVersion |
| 284 | << "\n Minor Version: " << LoadConf->MinorVersion |
| 285 | << "\n GlobalFlags Clear: " << LoadConf->GlobalFlagsClear |
| 286 | << "\n GlobalFlags Set: " << LoadConf->GlobalFlagsSet |
| 287 | << "\n Critical Section Default Timeout: " << LoadConf->CriticalSectionDefaultTimeout |
| 288 | << "\n Decommit Free Block Threshold: " << LoadConf->DeCommitFreeBlockThreshold |
| 289 | << "\n Decommit Total Free Threshold: " << LoadConf->DeCommitTotalFreeThreshold |
| 290 | << "\n Lock Prefix Table: " << LoadConf->LockPrefixTable |
| 291 | << "\n Maximum Allocation Size: " << LoadConf->MaximumAllocationSize |
| 292 | << "\n Virtual Memory Threshold: " << LoadConf->VirtualMemoryThreshold |
| 293 | << "\n Process Affinity Mask: " << LoadConf->ProcessAffinityMask |
| 294 | << "\n Process Heap Flags: " << LoadConf->ProcessHeapFlags |
| 295 | << "\n CSD Version: " << LoadConf->CSDVersion |
| 296 | << "\n Security Cookie: " << LoadConf->SecurityCookie |
| 297 | << "\n SEH Table: " << LoadConf->SEHandlerTable |
| 298 | << "\n SEH Count: " << LoadConf->SEHandlerCount |
| 299 | << "\n\n"; |
Rui Ueyama | 215a586 | 2014-02-20 06:51:07 +0000 | [diff] [blame] | 300 | printSEHTable(Obj, LoadConf->SEHandlerTable, LoadConf->SEHandlerCount); |
| 301 | outs() << "\n"; |
Rui Ueyama | c514a80 | 2014-02-19 03:53:11 +0000 | [diff] [blame] | 302 | } |
| 303 | |
Rui Ueyama | c2bed42 | 2013-09-27 21:04:00 +0000 | [diff] [blame] | 304 | // Prints import tables. The import table is a table containing the list of |
| 305 | // DLL name and symbol names which will be linked by the loader. |
| 306 | static void printImportTables(const COFFObjectFile *Obj) { |
Rui Ueyama | ef8dede | 2014-01-16 20:57:55 +0000 | [diff] [blame] | 307 | import_directory_iterator I = Obj->import_directory_begin(); |
| 308 | import_directory_iterator E = Obj->import_directory_end(); |
| 309 | if (I == E) |
Rui Ueyama | 908dfcd | 2014-01-15 23:46:18 +0000 | [diff] [blame] | 310 | return; |
Rui Ueyama | c2bed42 | 2013-09-27 21:04:00 +0000 | [diff] [blame] | 311 | outs() << "The Import Tables:\n"; |
Rafael Espindola | 5e812af | 2014-01-30 02:49:50 +0000 | [diff] [blame] | 312 | for (; I != E; I = ++I) { |
Rui Ueyama | c2bed42 | 2013-09-27 21:04:00 +0000 | [diff] [blame] | 313 | const import_directory_table_entry *Dir; |
| 314 | StringRef Name; |
Rui Ueyama | ef8dede | 2014-01-16 20:57:55 +0000 | [diff] [blame] | 315 | if (I->getImportTableEntry(Dir)) return; |
| 316 | if (I->getName(Name)) return; |
Rui Ueyama | c2bed42 | 2013-09-27 21:04:00 +0000 | [diff] [blame] | 317 | |
| 318 | outs() << format(" lookup %08x time %08x fwd %08x name %08x addr %08x\n\n", |
| 319 | static_cast<uint32_t>(Dir->ImportLookupTableRVA), |
| 320 | static_cast<uint32_t>(Dir->TimeDateStamp), |
| 321 | static_cast<uint32_t>(Dir->ForwarderChain), |
| 322 | static_cast<uint32_t>(Dir->NameRVA), |
| 323 | static_cast<uint32_t>(Dir->ImportAddressTableRVA)); |
| 324 | outs() << " DLL Name: " << Name << "\n"; |
| 325 | outs() << " Hint/Ord Name\n"; |
| 326 | const import_lookup_table_entry32 *entry; |
Rui Ueyama | ef8dede | 2014-01-16 20:57:55 +0000 | [diff] [blame] | 327 | if (I->getImportLookupEntry(entry)) |
Rui Ueyama | c2bed42 | 2013-09-27 21:04:00 +0000 | [diff] [blame] | 328 | return; |
| 329 | for (; entry->data; ++entry) { |
| 330 | if (entry->isOrdinal()) { |
| 331 | outs() << format(" % 6d\n", entry->getOrdinal()); |
| 332 | continue; |
| 333 | } |
| 334 | uint16_t Hint; |
| 335 | StringRef Name; |
| 336 | if (Obj->getHintName(entry->getHintNameRVA(), Hint, Name)) |
| 337 | return; |
| 338 | outs() << format(" % 6d ", Hint) << Name << "\n"; |
| 339 | } |
| 340 | outs() << "\n"; |
| 341 | } |
| 342 | } |
| 343 | |
Rui Ueyama | ad882ba | 2014-01-16 07:05:49 +0000 | [diff] [blame] | 344 | // Prints export tables. The export table is a table containing the list of |
| 345 | // exported symbol from the DLL. |
| 346 | static void printExportTable(const COFFObjectFile *Obj) { |
| 347 | outs() << "Export Table:\n"; |
| 348 | export_directory_iterator I = Obj->export_directory_begin(); |
| 349 | export_directory_iterator E = Obj->export_directory_end(); |
| 350 | if (I == E) |
| 351 | return; |
Rui Ueyama | da49d0d | 2014-01-16 20:50:34 +0000 | [diff] [blame] | 352 | StringRef DllName; |
Rui Ueyama | e5df609 | 2014-01-17 22:02:24 +0000 | [diff] [blame] | 353 | uint32_t OrdinalBase; |
Rui Ueyama | da49d0d | 2014-01-16 20:50:34 +0000 | [diff] [blame] | 354 | if (I->getDllName(DllName)) |
| 355 | return; |
Rui Ueyama | e5df609 | 2014-01-17 22:02:24 +0000 | [diff] [blame] | 356 | if (I->getOrdinalBase(OrdinalBase)) |
| 357 | return; |
Rui Ueyama | da49d0d | 2014-01-16 20:50:34 +0000 | [diff] [blame] | 358 | outs() << " DLL name: " << DllName << "\n"; |
Rui Ueyama | e5df609 | 2014-01-17 22:02:24 +0000 | [diff] [blame] | 359 | outs() << " Ordinal base: " << OrdinalBase << "\n"; |
Rui Ueyama | ad882ba | 2014-01-16 07:05:49 +0000 | [diff] [blame] | 360 | outs() << " Ordinal RVA Name\n"; |
Rafael Espindola | 5e812af | 2014-01-30 02:49:50 +0000 | [diff] [blame] | 361 | for (; I != E; I = ++I) { |
Rui Ueyama | ad882ba | 2014-01-16 07:05:49 +0000 | [diff] [blame] | 362 | uint32_t Ordinal; |
| 363 | if (I->getOrdinal(Ordinal)) |
| 364 | return; |
| 365 | uint32_t RVA; |
| 366 | if (I->getExportRVA(RVA)) |
| 367 | return; |
| 368 | outs() << format(" % 4d %# 8x", Ordinal, RVA); |
| 369 | |
| 370 | StringRef Name; |
Rui Ueyama | da49d0d | 2014-01-16 20:50:34 +0000 | [diff] [blame] | 371 | if (I->getSymbolName(Name)) |
Rui Ueyama | ad882ba | 2014-01-16 07:05:49 +0000 | [diff] [blame] | 372 | continue; |
| 373 | if (!Name.empty()) |
| 374 | outs() << " " << Name; |
| 375 | outs() << "\n"; |
| 376 | } |
| 377 | } |
| 378 | |
Rui Ueyama | 39f1b97 | 2014-03-04 00:31:48 +0000 | [diff] [blame] | 379 | // Given the COFF object file, this function returns the relocations for .pdata |
| 380 | // and the pointer to "runtime function" structs. |
| 381 | static bool getPDataSection(const COFFObjectFile *Obj, |
| 382 | std::vector<RelocationRef> &Rels, |
| 383 | const RuntimeFunction *&RFStart, int &NumRFs) { |
Rafael Espindola | b5155a5 | 2014-02-10 20:24:04 +0000 | [diff] [blame] | 384 | for (section_iterator SI = Obj->section_begin(), SE = Obj->section_end(); |
Rafael Espindola | 5e812af | 2014-01-30 02:49:50 +0000 | [diff] [blame] | 385 | SI != SE; ++SI) { |
Michael J. Spencer | 0c6ec48 | 2012-12-05 20:12:35 +0000 | [diff] [blame] | 386 | StringRef Name; |
Rui Ueyama | 39f1b97 | 2014-03-04 00:31:48 +0000 | [diff] [blame] | 387 | if (error(SI->getName(Name))) |
| 388 | continue; |
| 389 | if (Name != ".pdata") |
| 390 | continue; |
Michael J. Spencer | 0c6ec48 | 2012-12-05 20:12:35 +0000 | [diff] [blame] | 391 | |
Rui Ueyama | 39f1b97 | 2014-03-04 00:31:48 +0000 | [diff] [blame] | 392 | const coff_section *Pdata = Obj->getCOFFSection(SI); |
Rafael Espindola | b5155a5 | 2014-02-10 20:24:04 +0000 | [diff] [blame] | 393 | for (relocation_iterator RI = SI->relocation_begin(), |
| 394 | RE = SI->relocation_end(); |
Rafael Espindola | 5e812af | 2014-01-30 02:49:50 +0000 | [diff] [blame] | 395 | RI != RE; ++RI) |
Michael J. Spencer | 0c6ec48 | 2012-12-05 20:12:35 +0000 | [diff] [blame] | 396 | Rels.push_back(*RI); |
Michael J. Spencer | 0c6ec48 | 2012-12-05 20:12:35 +0000 | [diff] [blame] | 397 | |
| 398 | // Sort relocations by address. |
| 399 | std::sort(Rels.begin(), Rels.end(), RelocAddressLess); |
| 400 | |
| 401 | ArrayRef<uint8_t> Contents; |
Rui Ueyama | 39f1b97 | 2014-03-04 00:31:48 +0000 | [diff] [blame] | 402 | if (error(Obj->getSectionContents(Pdata, Contents))) |
| 403 | continue; |
| 404 | if (Contents.empty()) |
| 405 | continue; |
Michael J. Spencer | 0c6ec48 | 2012-12-05 20:12:35 +0000 | [diff] [blame] | 406 | |
Rui Ueyama | 39f1b97 | 2014-03-04 00:31:48 +0000 | [diff] [blame] | 407 | RFStart = reinterpret_cast<const RuntimeFunction *>(Contents.data()); |
| 408 | NumRFs = Contents.size() / sizeof(RuntimeFunction); |
| 409 | return true; |
| 410 | } |
| 411 | return false; |
| 412 | } |
Michael J. Spencer | 0c6ec48 | 2012-12-05 20:12:35 +0000 | [diff] [blame] | 413 | |
Rui Ueyama | 6dfd4e1 | 2014-03-04 03:08:45 +0000 | [diff] [blame] | 414 | static void printWin64EHUnwindInfo(const Win64EH::UnwindInfo *UI) { |
Rui Ueyama | 39f1b97 | 2014-03-04 00:31:48 +0000 | [diff] [blame] | 415 | // The casts to int are required in order to output the value as number. |
| 416 | // Without the casts the value would be interpreted as char data (which |
| 417 | // results in garbage output). |
Rui Ueyama | 595932f | 2014-03-04 19:23:56 +0000 | [diff] [blame] | 418 | outs() << " Version: " << static_cast<int>(UI->getVersion()) << "\n"; |
| 419 | outs() << " Flags: " << static_cast<int>(UI->getFlags()); |
Rui Ueyama | 39f1b97 | 2014-03-04 00:31:48 +0000 | [diff] [blame] | 420 | if (UI->getFlags()) { |
| 421 | if (UI->getFlags() & UNW_ExceptionHandler) |
| 422 | outs() << " UNW_ExceptionHandler"; |
| 423 | if (UI->getFlags() & UNW_TerminateHandler) |
| 424 | outs() << " UNW_TerminateHandler"; |
| 425 | if (UI->getFlags() & UNW_ChainInfo) |
| 426 | outs() << " UNW_ChainInfo"; |
| 427 | } |
| 428 | outs() << "\n"; |
Rui Ueyama | 595932f | 2014-03-04 19:23:56 +0000 | [diff] [blame] | 429 | outs() << " Size of prolog: " << static_cast<int>(UI->PrologSize) << "\n"; |
| 430 | outs() << " Number of Codes: " << static_cast<int>(UI->NumCodes) << "\n"; |
Rui Ueyama | 39f1b97 | 2014-03-04 00:31:48 +0000 | [diff] [blame] | 431 | // Maybe this should move to output of UOP_SetFPReg? |
| 432 | if (UI->getFrameRegister()) { |
Rui Ueyama | 595932f | 2014-03-04 19:23:56 +0000 | [diff] [blame] | 433 | outs() << " Frame register: " |
Rui Ueyama | 39f1b97 | 2014-03-04 00:31:48 +0000 | [diff] [blame] | 434 | << getUnwindRegisterName(UI->getFrameRegister()) << "\n"; |
Rui Ueyama | 595932f | 2014-03-04 19:23:56 +0000 | [diff] [blame] | 435 | outs() << " Frame offset: " << 16 * UI->getFrameOffset() << "\n"; |
Rui Ueyama | 39f1b97 | 2014-03-04 00:31:48 +0000 | [diff] [blame] | 436 | } else { |
Rui Ueyama | 595932f | 2014-03-04 19:23:56 +0000 | [diff] [blame] | 437 | outs() << " No frame pointer used\n"; |
Rui Ueyama | 39f1b97 | 2014-03-04 00:31:48 +0000 | [diff] [blame] | 438 | } |
| 439 | if (UI->getFlags() & (UNW_ExceptionHandler | UNW_TerminateHandler)) { |
| 440 | // FIXME: Output exception handler data |
| 441 | } else if (UI->getFlags() & UNW_ChainInfo) { |
| 442 | // FIXME: Output chained unwind info |
| 443 | } |
Michael J. Spencer | 0c6ec48 | 2012-12-05 20:12:35 +0000 | [diff] [blame] | 444 | |
Rui Ueyama | 39f1b97 | 2014-03-04 00:31:48 +0000 | [diff] [blame] | 445 | if (UI->NumCodes) |
Rui Ueyama | 595932f | 2014-03-04 19:23:56 +0000 | [diff] [blame] | 446 | outs() << " Unwind Codes:\n"; |
Michael J. Spencer | 0c6ec48 | 2012-12-05 20:12:35 +0000 | [diff] [blame] | 447 | |
Rui Ueyama | 39f1b97 | 2014-03-04 00:31:48 +0000 | [diff] [blame] | 448 | printAllUnwindCodes(ArrayRef<UnwindCode>(&UI->UnwindCodes[0], UI->NumCodes)); |
Michael J. Spencer | 0c6ec48 | 2012-12-05 20:12:35 +0000 | [diff] [blame] | 449 | |
Rui Ueyama | 595932f | 2014-03-04 19:23:56 +0000 | [diff] [blame] | 450 | outs() << "\n"; |
Rui Ueyama | 39f1b97 | 2014-03-04 00:31:48 +0000 | [diff] [blame] | 451 | outs().flush(); |
| 452 | } |
| 453 | |
Rui Ueyama | fd3cb9e | 2014-03-04 04:22:41 +0000 | [diff] [blame] | 454 | /// Prints out the given RuntimeFunction struct for x64, assuming that Obj is |
Rui Ueyama | 9c674e6 | 2014-03-04 04:00:55 +0000 | [diff] [blame] | 455 | /// pointing to an executable file. |
Rui Ueyama | 6dfd4e1 | 2014-03-04 03:08:45 +0000 | [diff] [blame] | 456 | static void printRuntimeFunction(const COFFObjectFile *Obj, |
Rui Ueyama | 9c674e6 | 2014-03-04 04:00:55 +0000 | [diff] [blame] | 457 | const RuntimeFunction &RF) { |
| 458 | if (!RF.StartAddress) |
| 459 | return; |
| 460 | outs() << "Function Table:\n" |
Rui Ueyama | ad80765 | 2014-03-05 23:03:37 +0000 | [diff] [blame^] | 461 | << format(" Start Address: 0x%04x\n", |
| 462 | static_cast<uint32_t>(RF.StartAddress)) |
| 463 | << format(" End Address: 0x%04x\n", |
| 464 | static_cast<uint32_t>(RF.EndAddress)) |
| 465 | << format(" Unwind Info Address: 0x%04x\n", |
| 466 | static_cast<uint32_t>(RF.UnwindInfoOffset)); |
Rui Ueyama | 9c674e6 | 2014-03-04 04:00:55 +0000 | [diff] [blame] | 467 | uintptr_t addr; |
| 468 | if (Obj->getRvaPtr(RF.UnwindInfoOffset, addr)) |
| 469 | return; |
| 470 | printWin64EHUnwindInfo(reinterpret_cast<const Win64EH::UnwindInfo *>(addr)); |
| 471 | } |
| 472 | |
Rui Ueyama | fd3cb9e | 2014-03-04 04:22:41 +0000 | [diff] [blame] | 473 | /// Prints out the given RuntimeFunction struct for x64, assuming that Obj is |
| 474 | /// pointing to an object file. Unlike executable, fields in RuntimeFunction |
Rui Ueyama | 9c674e6 | 2014-03-04 04:00:55 +0000 | [diff] [blame] | 475 | /// struct are filled with zeros, but instead there are relocations pointing to |
| 476 | /// them so that the linker will fill targets' RVAs to the fields at link |
| 477 | /// time. This function interprets the relocations to find the data to be used |
| 478 | /// in the resulting executable. |
| 479 | static void printRuntimeFunctionRels(const COFFObjectFile *Obj, |
| 480 | const RuntimeFunction &RF, |
| 481 | uint64_t SectionOffset, |
| 482 | const std::vector<RelocationRef> &Rels) { |
Rui Ueyama | 6dfd4e1 | 2014-03-04 03:08:45 +0000 | [diff] [blame] | 483 | outs() << "Function Table:\n"; |
| 484 | outs() << " Start Address: "; |
| 485 | printCOFFSymbolAddress(outs(), Rels, |
| 486 | SectionOffset + |
| 487 | /*offsetof(RuntimeFunction, StartAddress)*/ 0, |
| 488 | RF.StartAddress); |
| 489 | outs() << "\n"; |
| 490 | |
| 491 | outs() << " End Address: "; |
| 492 | printCOFFSymbolAddress(outs(), Rels, |
| 493 | SectionOffset + |
| 494 | /*offsetof(RuntimeFunction, EndAddress)*/ 4, |
| 495 | RF.EndAddress); |
| 496 | outs() << "\n"; |
| 497 | |
| 498 | outs() << " Unwind Info Address: "; |
| 499 | printCOFFSymbolAddress(outs(), Rels, |
| 500 | SectionOffset + |
| 501 | /*offsetof(RuntimeFunction, UnwindInfoOffset)*/ 8, |
| 502 | RF.UnwindInfoOffset); |
| 503 | outs() << "\n"; |
| 504 | |
| 505 | ArrayRef<uint8_t> XContents; |
| 506 | uint64_t UnwindInfoOffset = 0; |
| 507 | if (error(getSectionContents( |
| 508 | Obj, Rels, SectionOffset + |
| 509 | /*offsetof(RuntimeFunction, UnwindInfoOffset)*/ 8, |
| 510 | XContents, UnwindInfoOffset))) |
| 511 | return; |
| 512 | if (XContents.empty()) |
| 513 | return; |
| 514 | |
| 515 | UnwindInfoOffset += RF.UnwindInfoOffset; |
| 516 | if (UnwindInfoOffset > XContents.size()) |
| 517 | return; |
| 518 | |
Rui Ueyama | 8c1d4c9 | 2014-03-04 04:15:59 +0000 | [diff] [blame] | 519 | auto *UI = reinterpret_cast<const Win64EH::UnwindInfo *>(XContents.data() + |
| 520 | UnwindInfoOffset); |
Rui Ueyama | 6dfd4e1 | 2014-03-04 03:08:45 +0000 | [diff] [blame] | 521 | printWin64EHUnwindInfo(UI); |
| 522 | } |
| 523 | |
Rui Ueyama | 39f1b97 | 2014-03-04 00:31:48 +0000 | [diff] [blame] | 524 | void llvm::printCOFFUnwindInfo(const COFFObjectFile *Obj) { |
| 525 | const coff_file_header *Header; |
| 526 | if (error(Obj->getCOFFHeader(Header))) |
| 527 | return; |
| 528 | |
| 529 | if (Header->Machine != COFF::IMAGE_FILE_MACHINE_AMD64) { |
| 530 | errs() << "Unsupported image machine type " |
| 531 | "(currently only AMD64 is supported).\n"; |
| 532 | return; |
| 533 | } |
| 534 | |
| 535 | std::vector<RelocationRef> Rels; |
| 536 | const RuntimeFunction *RFStart; |
| 537 | int NumRFs; |
| 538 | if (!getPDataSection(Obj, Rels, RFStart, NumRFs)) |
| 539 | return; |
| 540 | ArrayRef<RuntimeFunction> RFs(RFStart, NumRFs); |
| 541 | |
Rui Ueyama | 9c674e6 | 2014-03-04 04:00:55 +0000 | [diff] [blame] | 542 | bool IsExecutable = Rels.empty(); |
| 543 | if (IsExecutable) { |
| 544 | for (const RuntimeFunction &RF : RFs) |
| 545 | printRuntimeFunction(Obj, RF); |
| 546 | return; |
| 547 | } |
| 548 | |
Rui Ueyama | 39f1b97 | 2014-03-04 00:31:48 +0000 | [diff] [blame] | 549 | for (const RuntimeFunction &RF : RFs) { |
| 550 | uint64_t SectionOffset = |
| 551 | std::distance(RFs.begin(), &RF) * sizeof(RuntimeFunction); |
Rui Ueyama | 9c674e6 | 2014-03-04 04:00:55 +0000 | [diff] [blame] | 552 | printRuntimeFunctionRels(Obj, RF, SectionOffset, Rels); |
Michael J. Spencer | 0c6ec48 | 2012-12-05 20:12:35 +0000 | [diff] [blame] | 553 | } |
| 554 | } |
Rui Ueyama | c2bed42 | 2013-09-27 21:04:00 +0000 | [diff] [blame] | 555 | |
| 556 | void llvm::printCOFFFileHeader(const object::ObjectFile *Obj) { |
Rui Ueyama | ad882ba | 2014-01-16 07:05:49 +0000 | [diff] [blame] | 557 | const COFFObjectFile *file = dyn_cast<const COFFObjectFile>(Obj); |
Rui Ueyama | c514a80 | 2014-02-19 03:53:11 +0000 | [diff] [blame] | 558 | printLoadConfiguration(file); |
Rui Ueyama | ad882ba | 2014-01-16 07:05:49 +0000 | [diff] [blame] | 559 | printImportTables(file); |
| 560 | printExportTable(file); |
Rui Ueyama | c2bed42 | 2013-09-27 21:04:00 +0000 | [diff] [blame] | 561 | } |