1 //===-- COFFDump.cpp - COFF-specific dumper ---------------------*- 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 /// 9 /// \file 10 /// This file implements the COFF-specific dumper for llvm-objdump. 11 /// It outputs the Win64 EH data structures as plain text. 12 /// The encoding of the unwind codes is described in MSDN: 13 /// http://msdn.microsoft.com/en-us/library/ck9asaa9.aspx 14 /// 15 //===----------------------------------------------------------------------===// 16 17 #include "COFFDump.h" 18 19 #include "llvm-objdump.h" 20 #include "llvm/Demangle/Demangle.h" 21 #include "llvm/Object/COFF.h" 22 #include "llvm/Object/COFFImportFile.h" 23 #include "llvm/Object/ObjectFile.h" 24 #include "llvm/Support/Format.h" 25 #include "llvm/Support/Win64EH.h" 26 #include "llvm/Support/WithColor.h" 27 #include "llvm/Support/raw_ostream.h" 28 29 using namespace llvm; 30 using namespace llvm::objdump; 31 using namespace llvm::object; 32 using namespace llvm::Win64EH; 33 34 // Returns the name of the unwind code. 35 static StringRef getUnwindCodeTypeName(uint8_t Code) { 36 switch(Code) { 37 default: llvm_unreachable("Invalid unwind code"); 38 case UOP_PushNonVol: return "UOP_PushNonVol"; 39 case UOP_AllocLarge: return "UOP_AllocLarge"; 40 case UOP_AllocSmall: return "UOP_AllocSmall"; 41 case UOP_SetFPReg: return "UOP_SetFPReg"; 42 case UOP_SaveNonVol: return "UOP_SaveNonVol"; 43 case UOP_SaveNonVolBig: return "UOP_SaveNonVolBig"; 44 case UOP_SaveXMM128: return "UOP_SaveXMM128"; 45 case UOP_SaveXMM128Big: return "UOP_SaveXMM128Big"; 46 case UOP_PushMachFrame: return "UOP_PushMachFrame"; 47 } 48 } 49 50 // Returns the name of a referenced register. 51 static StringRef getUnwindRegisterName(uint8_t Reg) { 52 switch(Reg) { 53 default: llvm_unreachable("Invalid register"); 54 case 0: return "RAX"; 55 case 1: return "RCX"; 56 case 2: return "RDX"; 57 case 3: return "RBX"; 58 case 4: return "RSP"; 59 case 5: return "RBP"; 60 case 6: return "RSI"; 61 case 7: return "RDI"; 62 case 8: return "R8"; 63 case 9: return "R9"; 64 case 10: return "R10"; 65 case 11: return "R11"; 66 case 12: return "R12"; 67 case 13: return "R13"; 68 case 14: return "R14"; 69 case 15: return "R15"; 70 } 71 } 72 73 // Calculates the number of array slots required for the unwind code. 74 static unsigned getNumUsedSlots(const UnwindCode &UnwindCode) { 75 switch (UnwindCode.getUnwindOp()) { 76 default: llvm_unreachable("Invalid unwind code"); 77 case UOP_PushNonVol: 78 case UOP_AllocSmall: 79 case UOP_SetFPReg: 80 case UOP_PushMachFrame: 81 return 1; 82 case UOP_SaveNonVol: 83 case UOP_SaveXMM128: 84 return 2; 85 case UOP_SaveNonVolBig: 86 case UOP_SaveXMM128Big: 87 return 3; 88 case UOP_AllocLarge: 89 return (UnwindCode.getOpInfo() == 0) ? 2 : 3; 90 } 91 } 92 93 // Prints one unwind code. Because an unwind code can occupy up to 3 slots in 94 // the unwind codes array, this function requires that the correct number of 95 // slots is provided. 96 static void printUnwindCode(ArrayRef<UnwindCode> UCs) { 97 assert(UCs.size() >= getNumUsedSlots(UCs[0])); 98 outs() << format(" 0x%02x: ", unsigned(UCs[0].u.CodeOffset)) 99 << getUnwindCodeTypeName(UCs[0].getUnwindOp()); 100 switch (UCs[0].getUnwindOp()) { 101 case UOP_PushNonVol: 102 outs() << " " << getUnwindRegisterName(UCs[0].getOpInfo()); 103 break; 104 case UOP_AllocLarge: 105 if (UCs[0].getOpInfo() == 0) { 106 outs() << " " << UCs[1].FrameOffset; 107 } else { 108 outs() << " " << UCs[1].FrameOffset 109 + (static_cast<uint32_t>(UCs[2].FrameOffset) << 16); 110 } 111 break; 112 case UOP_AllocSmall: 113 outs() << " " << ((UCs[0].getOpInfo() + 1) * 8); 114 break; 115 case UOP_SetFPReg: 116 outs() << " "; 117 break; 118 case UOP_SaveNonVol: 119 outs() << " " << getUnwindRegisterName(UCs[0].getOpInfo()) 120 << format(" [0x%04x]", 8 * UCs[1].FrameOffset); 121 break; 122 case UOP_SaveNonVolBig: 123 outs() << " " << getUnwindRegisterName(UCs[0].getOpInfo()) 124 << format(" [0x%08x]", UCs[1].FrameOffset 125 + (static_cast<uint32_t>(UCs[2].FrameOffset) << 16)); 126 break; 127 case UOP_SaveXMM128: 128 outs() << " XMM" << static_cast<uint32_t>(UCs[0].getOpInfo()) 129 << format(" [0x%04x]", 16 * UCs[1].FrameOffset); 130 break; 131 case UOP_SaveXMM128Big: 132 outs() << " XMM" << UCs[0].getOpInfo() 133 << format(" [0x%08x]", UCs[1].FrameOffset 134 + (static_cast<uint32_t>(UCs[2].FrameOffset) << 16)); 135 break; 136 case UOP_PushMachFrame: 137 outs() << " " << (UCs[0].getOpInfo() ? "w/o" : "w") 138 << " error code"; 139 break; 140 } 141 outs() << "\n"; 142 } 143 144 static void printAllUnwindCodes(ArrayRef<UnwindCode> UCs) { 145 for (const UnwindCode *I = UCs.begin(), *E = UCs.end(); I < E; ) { 146 unsigned UsedSlots = getNumUsedSlots(*I); 147 if (UsedSlots > UCs.size()) { 148 outs() << "Unwind data corrupted: Encountered unwind op " 149 << getUnwindCodeTypeName((*I).getUnwindOp()) 150 << " which requires " << UsedSlots 151 << " slots, but only " << UCs.size() 152 << " remaining in buffer"; 153 return ; 154 } 155 printUnwindCode(makeArrayRef(I, E)); 156 I += UsedSlots; 157 } 158 } 159 160 // Given a symbol sym this functions returns the address and section of it. 161 static Error resolveSectionAndAddress(const COFFObjectFile *Obj, 162 const SymbolRef &Sym, 163 const coff_section *&ResolvedSection, 164 uint64_t &ResolvedAddr) { 165 Expected<uint64_t> ResolvedAddrOrErr = Sym.getAddress(); 166 if (!ResolvedAddrOrErr) 167 return ResolvedAddrOrErr.takeError(); 168 ResolvedAddr = *ResolvedAddrOrErr; 169 Expected<section_iterator> Iter = Sym.getSection(); 170 if (!Iter) 171 return Iter.takeError(); 172 ResolvedSection = Obj->getCOFFSection(**Iter); 173 return Error::success(); 174 } 175 176 // Given a vector of relocations for a section and an offset into this section 177 // the function returns the symbol used for the relocation at the offset. 178 static Error resolveSymbol(const std::vector<RelocationRef> &Rels, 179 uint64_t Offset, SymbolRef &Sym) { 180 for (auto &R : Rels) { 181 uint64_t Ofs = R.getOffset(); 182 if (Ofs == Offset) { 183 Sym = *R.getSymbol(); 184 return Error::success(); 185 } 186 } 187 return make_error<BinaryError>(); 188 } 189 190 // Given a vector of relocations for a section and an offset into this section 191 // the function resolves the symbol used for the relocation at the offset and 192 // returns the section content and the address inside the content pointed to 193 // by the symbol. 194 static Error 195 getSectionContents(const COFFObjectFile *Obj, 196 const std::vector<RelocationRef> &Rels, uint64_t Offset, 197 ArrayRef<uint8_t> &Contents, uint64_t &Addr) { 198 SymbolRef Sym; 199 if (Error E = resolveSymbol(Rels, Offset, Sym)) 200 return E; 201 const coff_section *Section; 202 if (Error E = resolveSectionAndAddress(Obj, Sym, Section, Addr)) 203 return E; 204 return Obj->getSectionContents(Section, Contents); 205 } 206 207 // Given a vector of relocations for a section and an offset into this section 208 // the function returns the name of the symbol used for the relocation at the 209 // offset. 210 static Error resolveSymbolName(const std::vector<RelocationRef> &Rels, 211 uint64_t Offset, StringRef &Name) { 212 SymbolRef Sym; 213 if (Error EC = resolveSymbol(Rels, Offset, Sym)) 214 return EC; 215 Expected<StringRef> NameOrErr = Sym.getName(); 216 if (!NameOrErr) 217 return NameOrErr.takeError(); 218 Name = *NameOrErr; 219 return Error::success(); 220 } 221 222 static void printCOFFSymbolAddress(raw_ostream &Out, 223 const std::vector<RelocationRef> &Rels, 224 uint64_t Offset, uint32_t Disp) { 225 StringRef Sym; 226 if (!resolveSymbolName(Rels, Offset, Sym)) { 227 Out << Sym; 228 if (Disp > 0) 229 Out << format(" + 0x%04x", Disp); 230 } else { 231 Out << format("0x%04x", Disp); 232 } 233 } 234 235 static void 236 printSEHTable(const COFFObjectFile *Obj, uint32_t TableVA, int Count) { 237 if (Count == 0) 238 return; 239 240 uintptr_t IntPtr = 0; 241 if (std::error_code EC = Obj->getVaPtr(TableVA, IntPtr)) 242 reportError(errorCodeToError(EC), Obj->getFileName()); 243 244 const support::ulittle32_t *P = (const support::ulittle32_t *)IntPtr; 245 outs() << "SEH Table:"; 246 for (int I = 0; I < Count; ++I) 247 outs() << format(" 0x%x", P[I] + Obj->getPE32Header()->ImageBase); 248 outs() << "\n\n"; 249 } 250 251 template <typename T> 252 static void printTLSDirectoryT(const coff_tls_directory<T> *TLSDir) { 253 size_t FormatWidth = sizeof(T) * 2; 254 outs() << "TLS directory:" 255 << "\n StartAddressOfRawData: " 256 << format_hex(TLSDir->StartAddressOfRawData, FormatWidth) 257 << "\n EndAddressOfRawData: " 258 << format_hex(TLSDir->EndAddressOfRawData, FormatWidth) 259 << "\n AddressOfIndex: " 260 << format_hex(TLSDir->AddressOfIndex, FormatWidth) 261 << "\n AddressOfCallBacks: " 262 << format_hex(TLSDir->AddressOfCallBacks, FormatWidth) 263 << "\n SizeOfZeroFill: " 264 << TLSDir->SizeOfZeroFill 265 << "\n Characteristics: " 266 << TLSDir->Characteristics 267 << "\n Alignment: " 268 << TLSDir->getAlignment() 269 << "\n\n"; 270 } 271 272 static void printTLSDirectory(const COFFObjectFile *Obj) { 273 const pe32_header *PE32Header = Obj->getPE32Header(); 274 const pe32plus_header *PE32PlusHeader = Obj->getPE32PlusHeader(); 275 276 // Skip if it's not executable. 277 if (!PE32Header && !PE32PlusHeader) 278 return; 279 280 const data_directory *DataDir; 281 if (std::error_code EC = Obj->getDataDirectory(COFF::TLS_TABLE, DataDir)) 282 reportError(errorCodeToError(EC), Obj->getFileName()); 283 284 if (DataDir->RelativeVirtualAddress == 0) 285 return; 286 287 uintptr_t IntPtr = 0; 288 if (std::error_code EC = 289 Obj->getRvaPtr(DataDir->RelativeVirtualAddress, IntPtr)) 290 reportError(errorCodeToError(EC), Obj->getFileName()); 291 292 if (PE32Header) { 293 auto *TLSDir = reinterpret_cast<const coff_tls_directory32 *>(IntPtr); 294 printTLSDirectoryT(TLSDir); 295 } else { 296 auto *TLSDir = reinterpret_cast<const coff_tls_directory64 *>(IntPtr); 297 printTLSDirectoryT(TLSDir); 298 } 299 300 outs() << "\n"; 301 } 302 303 static void printLoadConfiguration(const COFFObjectFile *Obj) { 304 // Skip if it's not executable. 305 if (!Obj->getPE32Header()) 306 return; 307 308 // Currently only x86 is supported 309 if (Obj->getMachine() != COFF::IMAGE_FILE_MACHINE_I386) 310 return; 311 312 const data_directory *DataDir; 313 314 if (std::error_code EC = 315 Obj->getDataDirectory(COFF::LOAD_CONFIG_TABLE, DataDir)) 316 reportError(errorCodeToError(EC), Obj->getFileName()); 317 318 uintptr_t IntPtr = 0; 319 if (DataDir->RelativeVirtualAddress == 0) 320 return; 321 322 if (std::error_code EC = 323 Obj->getRvaPtr(DataDir->RelativeVirtualAddress, IntPtr)) 324 reportError(errorCodeToError(EC), Obj->getFileName()); 325 326 auto *LoadConf = reinterpret_cast<const coff_load_configuration32 *>(IntPtr); 327 outs() << "Load configuration:" 328 << "\n Timestamp: " << LoadConf->TimeDateStamp 329 << "\n Major Version: " << LoadConf->MajorVersion 330 << "\n Minor Version: " << LoadConf->MinorVersion 331 << "\n GlobalFlags Clear: " << LoadConf->GlobalFlagsClear 332 << "\n GlobalFlags Set: " << LoadConf->GlobalFlagsSet 333 << "\n Critical Section Default Timeout: " << LoadConf->CriticalSectionDefaultTimeout 334 << "\n Decommit Free Block Threshold: " << LoadConf->DeCommitFreeBlockThreshold 335 << "\n Decommit Total Free Threshold: " << LoadConf->DeCommitTotalFreeThreshold 336 << "\n Lock Prefix Table: " << LoadConf->LockPrefixTable 337 << "\n Maximum Allocation Size: " << LoadConf->MaximumAllocationSize 338 << "\n Virtual Memory Threshold: " << LoadConf->VirtualMemoryThreshold 339 << "\n Process Affinity Mask: " << LoadConf->ProcessAffinityMask 340 << "\n Process Heap Flags: " << LoadConf->ProcessHeapFlags 341 << "\n CSD Version: " << LoadConf->CSDVersion 342 << "\n Security Cookie: " << LoadConf->SecurityCookie 343 << "\n SEH Table: " << LoadConf->SEHandlerTable 344 << "\n SEH Count: " << LoadConf->SEHandlerCount 345 << "\n\n"; 346 printSEHTable(Obj, LoadConf->SEHandlerTable, LoadConf->SEHandlerCount); 347 outs() << "\n"; 348 } 349 350 // Prints import tables. The import table is a table containing the list of 351 // DLL name and symbol names which will be linked by the loader. 352 static void printImportTables(const COFFObjectFile *Obj) { 353 import_directory_iterator I = Obj->import_directory_begin(); 354 import_directory_iterator E = Obj->import_directory_end(); 355 if (I == E) 356 return; 357 outs() << "The Import Tables:\n"; 358 for (const ImportDirectoryEntryRef &DirRef : Obj->import_directories()) { 359 const coff_import_directory_table_entry *Dir; 360 StringRef Name; 361 if (DirRef.getImportTableEntry(Dir)) return; 362 if (DirRef.getName(Name)) return; 363 364 outs() << format(" lookup %08x time %08x fwd %08x name %08x addr %08x\n\n", 365 static_cast<uint32_t>(Dir->ImportLookupTableRVA), 366 static_cast<uint32_t>(Dir->TimeDateStamp), 367 static_cast<uint32_t>(Dir->ForwarderChain), 368 static_cast<uint32_t>(Dir->NameRVA), 369 static_cast<uint32_t>(Dir->ImportAddressTableRVA)); 370 outs() << " DLL Name: " << Name << "\n"; 371 outs() << " Hint/Ord Name\n"; 372 for (const ImportedSymbolRef &Entry : DirRef.imported_symbols()) { 373 bool IsOrdinal; 374 if (Entry.isOrdinal(IsOrdinal)) 375 return; 376 if (IsOrdinal) { 377 uint16_t Ordinal; 378 if (Entry.getOrdinal(Ordinal)) 379 return; 380 outs() << format(" % 6d\n", Ordinal); 381 continue; 382 } 383 uint32_t HintNameRVA; 384 if (Entry.getHintNameRVA(HintNameRVA)) 385 return; 386 uint16_t Hint; 387 StringRef Name; 388 if (Obj->getHintName(HintNameRVA, Hint, Name)) 389 return; 390 outs() << format(" % 6d ", Hint) << Name << "\n"; 391 } 392 outs() << "\n"; 393 } 394 } 395 396 // Prints export tables. The export table is a table containing the list of 397 // exported symbol from the DLL. 398 static void printExportTable(const COFFObjectFile *Obj) { 399 outs() << "Export Table:\n"; 400 export_directory_iterator I = Obj->export_directory_begin(); 401 export_directory_iterator E = Obj->export_directory_end(); 402 if (I == E) 403 return; 404 StringRef DllName; 405 uint32_t OrdinalBase; 406 if (I->getDllName(DllName)) 407 return; 408 if (I->getOrdinalBase(OrdinalBase)) 409 return; 410 outs() << " DLL name: " << DllName << "\n"; 411 outs() << " Ordinal base: " << OrdinalBase << "\n"; 412 outs() << " Ordinal RVA Name\n"; 413 for (; I != E; I = ++I) { 414 uint32_t Ordinal; 415 if (I->getOrdinal(Ordinal)) 416 return; 417 uint32_t RVA; 418 if (I->getExportRVA(RVA)) 419 return; 420 bool IsForwarder; 421 if (I->isForwarder(IsForwarder)) 422 return; 423 424 if (IsForwarder) { 425 // Export table entries can be used to re-export symbols that 426 // this COFF file is imported from some DLLs. This is rare. 427 // In most cases IsForwarder is false. 428 outs() << format(" % 4d ", Ordinal); 429 } else { 430 outs() << format(" % 4d %# 8x", Ordinal, RVA); 431 } 432 433 StringRef Name; 434 if (I->getSymbolName(Name)) 435 continue; 436 if (!Name.empty()) 437 outs() << " " << Name; 438 if (IsForwarder) { 439 StringRef S; 440 if (I->getForwardTo(S)) 441 return; 442 outs() << " (forwarded to " << S << ")"; 443 } 444 outs() << "\n"; 445 } 446 } 447 448 // Given the COFF object file, this function returns the relocations for .pdata 449 // and the pointer to "runtime function" structs. 450 static bool getPDataSection(const COFFObjectFile *Obj, 451 std::vector<RelocationRef> &Rels, 452 const RuntimeFunction *&RFStart, int &NumRFs) { 453 for (const SectionRef &Section : Obj->sections()) { 454 StringRef Name = unwrapOrError(Section.getName(), Obj->getFileName()); 455 if (Name != ".pdata") 456 continue; 457 458 const coff_section *Pdata = Obj->getCOFFSection(Section); 459 for (const RelocationRef &Reloc : Section.relocations()) 460 Rels.push_back(Reloc); 461 462 // Sort relocations by address. 463 llvm::sort(Rels, isRelocAddressLess); 464 465 ArrayRef<uint8_t> Contents; 466 if (Error E = Obj->getSectionContents(Pdata, Contents)) 467 reportError(std::move(E), Obj->getFileName()); 468 469 if (Contents.empty()) 470 continue; 471 472 RFStart = reinterpret_cast<const RuntimeFunction *>(Contents.data()); 473 NumRFs = Contents.size() / sizeof(RuntimeFunction); 474 return true; 475 } 476 return false; 477 } 478 479 Error objdump::getCOFFRelocationValueString(const COFFObjectFile *Obj, 480 const RelocationRef &Rel, 481 SmallVectorImpl<char> &Result) { 482 symbol_iterator SymI = Rel.getSymbol(); 483 Expected<StringRef> SymNameOrErr = SymI->getName(); 484 if (!SymNameOrErr) 485 return SymNameOrErr.takeError(); 486 StringRef SymName = *SymNameOrErr; 487 Result.append(SymName.begin(), SymName.end()); 488 return Error::success(); 489 } 490 491 static void printWin64EHUnwindInfo(const Win64EH::UnwindInfo *UI) { 492 // The casts to int are required in order to output the value as number. 493 // Without the casts the value would be interpreted as char data (which 494 // results in garbage output). 495 outs() << " Version: " << static_cast<int>(UI->getVersion()) << "\n"; 496 outs() << " Flags: " << static_cast<int>(UI->getFlags()); 497 if (UI->getFlags()) { 498 if (UI->getFlags() & UNW_ExceptionHandler) 499 outs() << " UNW_ExceptionHandler"; 500 if (UI->getFlags() & UNW_TerminateHandler) 501 outs() << " UNW_TerminateHandler"; 502 if (UI->getFlags() & UNW_ChainInfo) 503 outs() << " UNW_ChainInfo"; 504 } 505 outs() << "\n"; 506 outs() << " Size of prolog: " << static_cast<int>(UI->PrologSize) << "\n"; 507 outs() << " Number of Codes: " << static_cast<int>(UI->NumCodes) << "\n"; 508 // Maybe this should move to output of UOP_SetFPReg? 509 if (UI->getFrameRegister()) { 510 outs() << " Frame register: " 511 << getUnwindRegisterName(UI->getFrameRegister()) << "\n"; 512 outs() << " Frame offset: " << 16 * UI->getFrameOffset() << "\n"; 513 } else { 514 outs() << " No frame pointer used\n"; 515 } 516 if (UI->getFlags() & (UNW_ExceptionHandler | UNW_TerminateHandler)) { 517 // FIXME: Output exception handler data 518 } else if (UI->getFlags() & UNW_ChainInfo) { 519 // FIXME: Output chained unwind info 520 } 521 522 if (UI->NumCodes) 523 outs() << " Unwind Codes:\n"; 524 525 printAllUnwindCodes(makeArrayRef(&UI->UnwindCodes[0], UI->NumCodes)); 526 527 outs() << "\n"; 528 outs().flush(); 529 } 530 531 /// Prints out the given RuntimeFunction struct for x64, assuming that Obj is 532 /// pointing to an executable file. 533 static void printRuntimeFunction(const COFFObjectFile *Obj, 534 const RuntimeFunction &RF) { 535 if (!RF.StartAddress) 536 return; 537 outs() << "Function Table:\n" 538 << format(" Start Address: 0x%04x\n", 539 static_cast<uint32_t>(RF.StartAddress)) 540 << format(" End Address: 0x%04x\n", 541 static_cast<uint32_t>(RF.EndAddress)) 542 << format(" Unwind Info Address: 0x%04x\n", 543 static_cast<uint32_t>(RF.UnwindInfoOffset)); 544 uintptr_t addr; 545 if (Obj->getRvaPtr(RF.UnwindInfoOffset, addr)) 546 return; 547 printWin64EHUnwindInfo(reinterpret_cast<const Win64EH::UnwindInfo *>(addr)); 548 } 549 550 /// Prints out the given RuntimeFunction struct for x64, assuming that Obj is 551 /// pointing to an object file. Unlike executable, fields in RuntimeFunction 552 /// struct are filled with zeros, but instead there are relocations pointing to 553 /// them so that the linker will fill targets' RVAs to the fields at link 554 /// time. This function interprets the relocations to find the data to be used 555 /// in the resulting executable. 556 static void printRuntimeFunctionRels(const COFFObjectFile *Obj, 557 const RuntimeFunction &RF, 558 uint64_t SectionOffset, 559 const std::vector<RelocationRef> &Rels) { 560 outs() << "Function Table:\n"; 561 outs() << " Start Address: "; 562 printCOFFSymbolAddress(outs(), Rels, 563 SectionOffset + 564 /*offsetof(RuntimeFunction, StartAddress)*/ 0, 565 RF.StartAddress); 566 outs() << "\n"; 567 568 outs() << " End Address: "; 569 printCOFFSymbolAddress(outs(), Rels, 570 SectionOffset + 571 /*offsetof(RuntimeFunction, EndAddress)*/ 4, 572 RF.EndAddress); 573 outs() << "\n"; 574 575 outs() << " Unwind Info Address: "; 576 printCOFFSymbolAddress(outs(), Rels, 577 SectionOffset + 578 /*offsetof(RuntimeFunction, UnwindInfoOffset)*/ 8, 579 RF.UnwindInfoOffset); 580 outs() << "\n"; 581 582 ArrayRef<uint8_t> XContents; 583 uint64_t UnwindInfoOffset = 0; 584 if (Error E = getSectionContents( 585 Obj, Rels, 586 SectionOffset + 587 /*offsetof(RuntimeFunction, UnwindInfoOffset)*/ 8, 588 XContents, UnwindInfoOffset)) 589 reportError(std::move(E), Obj->getFileName()); 590 if (XContents.empty()) 591 return; 592 593 UnwindInfoOffset += RF.UnwindInfoOffset; 594 if (UnwindInfoOffset > XContents.size()) 595 return; 596 597 auto *UI = reinterpret_cast<const Win64EH::UnwindInfo *>(XContents.data() + 598 UnwindInfoOffset); 599 printWin64EHUnwindInfo(UI); 600 } 601 602 void objdump::printCOFFUnwindInfo(const COFFObjectFile *Obj) { 603 if (Obj->getMachine() != COFF::IMAGE_FILE_MACHINE_AMD64) { 604 WithColor::error(errs(), "llvm-objdump") 605 << "unsupported image machine type " 606 "(currently only AMD64 is supported).\n"; 607 return; 608 } 609 610 std::vector<RelocationRef> Rels; 611 const RuntimeFunction *RFStart; 612 int NumRFs; 613 if (!getPDataSection(Obj, Rels, RFStart, NumRFs)) 614 return; 615 ArrayRef<RuntimeFunction> RFs(RFStart, NumRFs); 616 617 bool IsExecutable = Rels.empty(); 618 if (IsExecutable) { 619 for (const RuntimeFunction &RF : RFs) 620 printRuntimeFunction(Obj, RF); 621 return; 622 } 623 624 for (const RuntimeFunction &RF : RFs) { 625 uint64_t SectionOffset = 626 std::distance(RFs.begin(), &RF) * sizeof(RuntimeFunction); 627 printRuntimeFunctionRels(Obj, RF, SectionOffset, Rels); 628 } 629 } 630 631 void objdump::printCOFFFileHeader(const object::ObjectFile *Obj) { 632 const COFFObjectFile *file = dyn_cast<const COFFObjectFile>(Obj); 633 printTLSDirectory(file); 634 printLoadConfiguration(file); 635 printImportTables(file); 636 printExportTable(file); 637 } 638 639 void objdump::printCOFFSymbolTable(const object::COFFImportFile *i) { 640 unsigned Index = 0; 641 bool IsCode = i->getCOFFImportHeader()->getType() == COFF::IMPORT_CODE; 642 643 for (const object::BasicSymbolRef &Sym : i->symbols()) { 644 std::string Name; 645 raw_string_ostream NS(Name); 646 647 cantFail(Sym.printName(NS)); 648 NS.flush(); 649 650 outs() << "[" << format("%2d", Index) << "]" 651 << "(sec " << format("%2d", 0) << ")" 652 << "(fl 0x00)" // Flag bits, which COFF doesn't have. 653 << "(ty " << format("%3x", (IsCode && Index) ? 32 : 0) << ")" 654 << "(scl " << format("%3x", 0) << ") " 655 << "(nx " << 0 << ") " 656 << "0x" << format("%08x", 0) << " " << Name << '\n'; 657 658 ++Index; 659 } 660 } 661 662 void objdump::printCOFFSymbolTable(const COFFObjectFile *coff) { 663 for (unsigned SI = 0, SE = coff->getNumberOfSymbols(); SI != SE; ++SI) { 664 Expected<COFFSymbolRef> Symbol = coff->getSymbol(SI); 665 if (!Symbol) 666 reportError(Symbol.takeError(), coff->getFileName()); 667 668 Expected<StringRef> NameOrErr = coff->getSymbolName(*Symbol); 669 if (!NameOrErr) 670 reportError(NameOrErr.takeError(), coff->getFileName()); 671 StringRef Name = *NameOrErr; 672 673 outs() << "[" << format("%2d", SI) << "]" 674 << "(sec " << format("%2d", int(Symbol->getSectionNumber())) << ")" 675 << "(fl 0x00)" // Flag bits, which COFF doesn't have. 676 << "(ty " << format("%3x", unsigned(Symbol->getType())) << ")" 677 << "(scl " << format("%3x", unsigned(Symbol->getStorageClass())) 678 << ") " 679 << "(nx " << unsigned(Symbol->getNumberOfAuxSymbols()) << ") " 680 << "0x" << format("%08x", unsigned(Symbol->getValue())) << " " 681 << Name; 682 if (Demangle && Name.startswith("?")) { 683 int Status = -1; 684 char *DemangledSymbol = 685 microsoftDemangle(Name.data(), nullptr, nullptr, nullptr, &Status); 686 687 if (Status == 0 && DemangledSymbol) { 688 outs() << " (" << StringRef(DemangledSymbol) << ")"; 689 std::free(DemangledSymbol); 690 } else { 691 outs() << " (invalid mangled name)"; 692 } 693 } 694 outs() << "\n"; 695 696 for (unsigned AI = 0, AE = Symbol->getNumberOfAuxSymbols(); AI < AE; ++AI, ++SI) { 697 if (Symbol->isSectionDefinition()) { 698 const coff_aux_section_definition *asd; 699 if (std::error_code EC = 700 coff->getAuxSymbol<coff_aux_section_definition>(SI + 1, asd)) 701 reportError(errorCodeToError(EC), coff->getFileName()); 702 703 int32_t AuxNumber = asd->getNumber(Symbol->isBigObj()); 704 705 outs() << "AUX " 706 << format("scnlen 0x%x nreloc %d nlnno %d checksum 0x%x " 707 , unsigned(asd->Length) 708 , unsigned(asd->NumberOfRelocations) 709 , unsigned(asd->NumberOfLinenumbers) 710 , unsigned(asd->CheckSum)) 711 << format("assoc %d comdat %d\n" 712 , unsigned(AuxNumber) 713 , unsigned(asd->Selection)); 714 } else if (Symbol->isFileRecord()) { 715 const char *FileName; 716 if (std::error_code EC = coff->getAuxSymbol<char>(SI + 1, FileName)) 717 reportError(errorCodeToError(EC), coff->getFileName()); 718 719 StringRef Name(FileName, Symbol->getNumberOfAuxSymbols() * 720 coff->getSymbolTableEntrySize()); 721 outs() << "AUX " << Name.rtrim(StringRef("\0", 1)) << '\n'; 722 723 SI = SI + Symbol->getNumberOfAuxSymbols(); 724 break; 725 } else if (Symbol->isWeakExternal()) { 726 const coff_aux_weak_external *awe; 727 if (std::error_code EC = 728 coff->getAuxSymbol<coff_aux_weak_external>(SI + 1, awe)) 729 reportError(errorCodeToError(EC), coff->getFileName()); 730 731 outs() << "AUX " << format("indx %d srch %d\n", 732 static_cast<uint32_t>(awe->TagIndex), 733 static_cast<uint32_t>(awe->Characteristics)); 734 } else { 735 outs() << "AUX Unknown\n"; 736 } 737 } 738 } 739 } 740