1 //===-- COFFDumper.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-readobj. 11 /// 12 //===----------------------------------------------------------------------===// 13 14 #include "ARMWinEHPrinter.h" 15 #include "ObjDumper.h" 16 #include "StackMapPrinter.h" 17 #include "Win64EHDumper.h" 18 #include "llvm-readobj.h" 19 #include "llvm/ADT/DenseMap.h" 20 #include "llvm/ADT/SmallString.h" 21 #include "llvm/ADT/StringExtras.h" 22 #include "llvm/BinaryFormat/COFF.h" 23 #include "llvm/DebugInfo/CodeView/CVTypeVisitor.h" 24 #include "llvm/DebugInfo/CodeView/CodeView.h" 25 #include "llvm/DebugInfo/CodeView/DebugChecksumsSubsection.h" 26 #include "llvm/DebugInfo/CodeView/DebugFrameDataSubsection.h" 27 #include "llvm/DebugInfo/CodeView/DebugInlineeLinesSubsection.h" 28 #include "llvm/DebugInfo/CodeView/DebugLinesSubsection.h" 29 #include "llvm/DebugInfo/CodeView/DebugStringTableSubsection.h" 30 #include "llvm/DebugInfo/CodeView/LazyRandomTypeCollection.h" 31 #include "llvm/DebugInfo/CodeView/Line.h" 32 #include "llvm/DebugInfo/CodeView/MergingTypeTableBuilder.h" 33 #include "llvm/DebugInfo/CodeView/RecordSerialization.h" 34 #include "llvm/DebugInfo/CodeView/SymbolDumpDelegate.h" 35 #include "llvm/DebugInfo/CodeView/SymbolDumper.h" 36 #include "llvm/DebugInfo/CodeView/SymbolRecord.h" 37 #include "llvm/DebugInfo/CodeView/TypeDumpVisitor.h" 38 #include "llvm/DebugInfo/CodeView/TypeHashing.h" 39 #include "llvm/DebugInfo/CodeView/TypeIndex.h" 40 #include "llvm/DebugInfo/CodeView/TypeRecord.h" 41 #include "llvm/DebugInfo/CodeView/TypeStreamMerger.h" 42 #include "llvm/DebugInfo/CodeView/TypeTableCollection.h" 43 #include "llvm/Object/COFF.h" 44 #include "llvm/Object/ObjectFile.h" 45 #include "llvm/Object/WindowsResource.h" 46 #include "llvm/Support/BinaryStreamReader.h" 47 #include "llvm/Support/Casting.h" 48 #include "llvm/Support/Compiler.h" 49 #include "llvm/Support/ConvertUTF.h" 50 #include "llvm/Support/FormatVariadic.h" 51 #include "llvm/Support/LEB128.h" 52 #include "llvm/Support/ScopedPrinter.h" 53 #include "llvm/Support/Win64EH.h" 54 #include "llvm/Support/raw_ostream.h" 55 56 using namespace llvm; 57 using namespace llvm::object; 58 using namespace llvm::codeview; 59 using namespace llvm::support; 60 using namespace llvm::Win64EH; 61 62 namespace { 63 64 struct LoadConfigTables { 65 uint64_t SEHTableVA = 0; 66 uint64_t SEHTableCount = 0; 67 uint32_t GuardFlags = 0; 68 uint64_t GuardFidTableVA = 0; 69 uint64_t GuardFidTableCount = 0; 70 uint64_t GuardIatTableVA = 0; 71 uint64_t GuardIatTableCount = 0; 72 uint64_t GuardLJmpTableVA = 0; 73 uint64_t GuardLJmpTableCount = 0; 74 uint64_t GuardEHContTableVA = 0; 75 uint64_t GuardEHContTableCount = 0; 76 }; 77 78 class COFFDumper : public ObjDumper { 79 public: 80 friend class COFFObjectDumpDelegate; 81 COFFDumper(const llvm::object::COFFObjectFile *Obj, ScopedPrinter &Writer) 82 : ObjDumper(Writer, Obj->getFileName()), Obj(Obj), Writer(Writer), 83 Types(100) {} 84 85 void printFileHeaders() override; 86 void printSectionHeaders() override; 87 void printRelocations() override; 88 void printUnwindInfo() override; 89 90 void printNeededLibraries() override; 91 92 void printCOFFImports() override; 93 void printCOFFExports() override; 94 void printCOFFDirectives() override; 95 void printCOFFBaseReloc() override; 96 void printCOFFDebugDirectory() override; 97 void printCOFFTLSDirectory() override; 98 void printCOFFResources() override; 99 void printCOFFLoadConfig() override; 100 void printCodeViewDebugInfo() override; 101 void mergeCodeViewTypes(llvm::codeview::MergingTypeTableBuilder &CVIDs, 102 llvm::codeview::MergingTypeTableBuilder &CVTypes, 103 llvm::codeview::GlobalTypeTableBuilder &GlobalCVIDs, 104 llvm::codeview::GlobalTypeTableBuilder &GlobalCVTypes, 105 bool GHash) override; 106 void printStackMap() const override; 107 void printAddrsig() override; 108 void printCGProfile() override; 109 110 private: 111 StringRef getSymbolName(uint32_t Index); 112 void printSymbols() override; 113 void printDynamicSymbols() override; 114 void printSymbol(const SymbolRef &Sym); 115 void printRelocation(const SectionRef &Section, const RelocationRef &Reloc, 116 uint64_t Bias = 0); 117 void printDataDirectory(uint32_t Index, const std::string &FieldName); 118 119 void printDOSHeader(const dos_header *DH); 120 template <class PEHeader> void printPEHeader(const PEHeader *Hdr); 121 void printBaseOfDataField(const pe32_header *Hdr); 122 void printBaseOfDataField(const pe32plus_header *Hdr); 123 template <typename T> 124 void printCOFFLoadConfig(const T *Conf, LoadConfigTables &Tables); 125 template <typename IntTy> 126 void printCOFFTLSDirectory(const coff_tls_directory<IntTy> *TlsTable); 127 typedef void (*PrintExtraCB)(raw_ostream &, const uint8_t *); 128 void printRVATable(uint64_t TableVA, uint64_t Count, uint64_t EntrySize, 129 PrintExtraCB PrintExtra = 0); 130 131 void printCodeViewSymbolSection(StringRef SectionName, const SectionRef &Section); 132 void printCodeViewTypeSection(StringRef SectionName, const SectionRef &Section); 133 StringRef getFileNameForFileOffset(uint32_t FileOffset); 134 void printFileNameForOffset(StringRef Label, uint32_t FileOffset); 135 void printTypeIndex(StringRef FieldName, TypeIndex TI) { 136 // Forward to CVTypeDumper for simplicity. 137 codeview::printTypeIndex(Writer, FieldName, TI, Types); 138 } 139 140 void printCodeViewSymbolsSubsection(StringRef Subsection, 141 const SectionRef &Section, 142 StringRef SectionContents); 143 144 void printCodeViewFileChecksums(StringRef Subsection); 145 146 void printCodeViewInlineeLines(StringRef Subsection); 147 148 void printRelocatedField(StringRef Label, const coff_section *Sec, 149 uint32_t RelocOffset, uint32_t Offset, 150 StringRef *RelocSym = nullptr); 151 152 uint32_t countTotalTableEntries(ResourceSectionRef RSF, 153 const coff_resource_dir_table &Table, 154 StringRef Level); 155 156 void printResourceDirectoryTable(ResourceSectionRef RSF, 157 const coff_resource_dir_table &Table, 158 StringRef Level); 159 160 void printBinaryBlockWithRelocs(StringRef Label, const SectionRef &Sec, 161 StringRef SectionContents, StringRef Block); 162 163 /// Given a .debug$S section, find the string table and file checksum table. 164 void initializeFileAndStringTables(BinaryStreamReader &Reader); 165 166 void cacheRelocations(); 167 168 std::error_code resolveSymbol(const coff_section *Section, uint64_t Offset, 169 SymbolRef &Sym); 170 std::error_code resolveSymbolName(const coff_section *Section, 171 uint64_t Offset, StringRef &Name); 172 std::error_code resolveSymbolName(const coff_section *Section, 173 StringRef SectionContents, 174 const void *RelocPtr, StringRef &Name); 175 void printImportedSymbols(iterator_range<imported_symbol_iterator> Range); 176 void printDelayImportedSymbols( 177 const DelayImportDirectoryEntryRef &I, 178 iterator_range<imported_symbol_iterator> Range); 179 180 typedef DenseMap<const coff_section*, std::vector<RelocationRef> > RelocMapTy; 181 182 const llvm::object::COFFObjectFile *Obj; 183 bool RelocCached = false; 184 RelocMapTy RelocMap; 185 186 DebugChecksumsSubsectionRef CVFileChecksumTable; 187 188 DebugStringTableSubsectionRef CVStringTable; 189 190 /// Track the compilation CPU type. S_COMPILE3 symbol records typically come 191 /// first, but if we don't see one, just assume an X64 CPU type. It is common. 192 CPUType CompilationCPUType = CPUType::X64; 193 194 ScopedPrinter &Writer; 195 BinaryByteStream TypeContents; 196 LazyRandomTypeCollection Types; 197 }; 198 199 class COFFObjectDumpDelegate : public SymbolDumpDelegate { 200 public: 201 COFFObjectDumpDelegate(COFFDumper &CD, const SectionRef &SR, 202 const COFFObjectFile *Obj, StringRef SectionContents) 203 : CD(CD), SR(SR), SectionContents(SectionContents) { 204 Sec = Obj->getCOFFSection(SR); 205 } 206 207 uint32_t getRecordOffset(BinaryStreamReader Reader) override { 208 ArrayRef<uint8_t> Data; 209 if (auto EC = Reader.readLongestContiguousChunk(Data)) { 210 llvm::consumeError(std::move(EC)); 211 return 0; 212 } 213 return Data.data() - SectionContents.bytes_begin(); 214 } 215 216 void printRelocatedField(StringRef Label, uint32_t RelocOffset, 217 uint32_t Offset, StringRef *RelocSym) override { 218 CD.printRelocatedField(Label, Sec, RelocOffset, Offset, RelocSym); 219 } 220 221 void printBinaryBlockWithRelocs(StringRef Label, 222 ArrayRef<uint8_t> Block) override { 223 StringRef SBlock(reinterpret_cast<const char *>(Block.data()), 224 Block.size()); 225 if (opts::CodeViewSubsectionBytes) 226 CD.printBinaryBlockWithRelocs(Label, SR, SectionContents, SBlock); 227 } 228 229 StringRef getFileNameForFileOffset(uint32_t FileOffset) override { 230 return CD.getFileNameForFileOffset(FileOffset); 231 } 232 233 DebugStringTableSubsectionRef getStringTable() override { 234 return CD.CVStringTable; 235 } 236 237 private: 238 COFFDumper &CD; 239 const SectionRef &SR; 240 const coff_section *Sec; 241 StringRef SectionContents; 242 }; 243 244 } // end namespace 245 246 namespace llvm { 247 248 std::unique_ptr<ObjDumper> createCOFFDumper(const object::COFFObjectFile &Obj, 249 ScopedPrinter &Writer) { 250 return std::make_unique<COFFDumper>(&Obj, Writer); 251 } 252 253 } // namespace llvm 254 255 // Given a section and an offset into this section the function returns the 256 // symbol used for the relocation at the offset. 257 std::error_code COFFDumper::resolveSymbol(const coff_section *Section, 258 uint64_t Offset, SymbolRef &Sym) { 259 cacheRelocations(); 260 const auto &Relocations = RelocMap[Section]; 261 auto SymI = Obj->symbol_end(); 262 for (const auto &Relocation : Relocations) { 263 uint64_t RelocationOffset = Relocation.getOffset(); 264 265 if (RelocationOffset == Offset) { 266 SymI = Relocation.getSymbol(); 267 break; 268 } 269 } 270 if (SymI == Obj->symbol_end()) 271 return inconvertibleErrorCode(); 272 Sym = *SymI; 273 return std::error_code(); 274 } 275 276 // Given a section and an offset into this section the function returns the name 277 // of the symbol used for the relocation at the offset. 278 std::error_code COFFDumper::resolveSymbolName(const coff_section *Section, 279 uint64_t Offset, 280 StringRef &Name) { 281 SymbolRef Symbol; 282 if (std::error_code EC = resolveSymbol(Section, Offset, Symbol)) 283 return EC; 284 Expected<StringRef> NameOrErr = Symbol.getName(); 285 if (!NameOrErr) 286 return errorToErrorCode(NameOrErr.takeError()); 287 Name = *NameOrErr; 288 return std::error_code(); 289 } 290 291 // Helper for when you have a pointer to real data and you want to know about 292 // relocations against it. 293 std::error_code COFFDumper::resolveSymbolName(const coff_section *Section, 294 StringRef SectionContents, 295 const void *RelocPtr, 296 StringRef &Name) { 297 assert(SectionContents.data() < RelocPtr && 298 RelocPtr < SectionContents.data() + SectionContents.size() && 299 "pointer to relocated object is not in section"); 300 uint64_t Offset = ptrdiff_t(reinterpret_cast<const char *>(RelocPtr) - 301 SectionContents.data()); 302 return resolveSymbolName(Section, Offset, Name); 303 } 304 305 void COFFDumper::printRelocatedField(StringRef Label, const coff_section *Sec, 306 uint32_t RelocOffset, uint32_t Offset, 307 StringRef *RelocSym) { 308 StringRef SymStorage; 309 StringRef &Symbol = RelocSym ? *RelocSym : SymStorage; 310 if (!resolveSymbolName(Sec, RelocOffset, Symbol)) 311 W.printSymbolOffset(Label, Symbol, Offset); 312 else 313 W.printHex(Label, RelocOffset); 314 } 315 316 void COFFDumper::printBinaryBlockWithRelocs(StringRef Label, 317 const SectionRef &Sec, 318 StringRef SectionContents, 319 StringRef Block) { 320 W.printBinaryBlock(Label, Block); 321 322 assert(SectionContents.begin() < Block.begin() && 323 SectionContents.end() >= Block.end() && 324 "Block is not contained in SectionContents"); 325 uint64_t OffsetStart = Block.data() - SectionContents.data(); 326 uint64_t OffsetEnd = OffsetStart + Block.size(); 327 328 W.flush(); 329 cacheRelocations(); 330 ListScope D(W, "BlockRelocations"); 331 const coff_section *Section = Obj->getCOFFSection(Sec); 332 const auto &Relocations = RelocMap[Section]; 333 for (const auto &Relocation : Relocations) { 334 uint64_t RelocationOffset = Relocation.getOffset(); 335 if (OffsetStart <= RelocationOffset && RelocationOffset < OffsetEnd) 336 printRelocation(Sec, Relocation, OffsetStart); 337 } 338 } 339 340 const EnumEntry<COFF::MachineTypes> ImageFileMachineType[] = { 341 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_UNKNOWN ), 342 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_AM33 ), 343 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_AMD64 ), 344 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_ARM ), 345 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_ARM64 ), 346 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_ARMNT ), 347 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_EBC ), 348 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_I386 ), 349 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_IA64 ), 350 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_M32R ), 351 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_MIPS16 ), 352 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_MIPSFPU ), 353 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_MIPSFPU16), 354 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_POWERPC ), 355 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_POWERPCFP), 356 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_R4000 ), 357 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_SH3 ), 358 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_SH3DSP ), 359 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_SH4 ), 360 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_SH5 ), 361 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_THUMB ), 362 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_WCEMIPSV2) 363 }; 364 365 const EnumEntry<COFF::Characteristics> ImageFileCharacteristics[] = { 366 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_RELOCS_STRIPPED ), 367 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_EXECUTABLE_IMAGE ), 368 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_LINE_NUMS_STRIPPED ), 369 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_LOCAL_SYMS_STRIPPED ), 370 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_AGGRESSIVE_WS_TRIM ), 371 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_LARGE_ADDRESS_AWARE ), 372 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_BYTES_REVERSED_LO ), 373 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_32BIT_MACHINE ), 374 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_DEBUG_STRIPPED ), 375 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_REMOVABLE_RUN_FROM_SWAP), 376 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_NET_RUN_FROM_SWAP ), 377 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_SYSTEM ), 378 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_DLL ), 379 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_UP_SYSTEM_ONLY ), 380 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_BYTES_REVERSED_HI ) 381 }; 382 383 const EnumEntry<COFF::WindowsSubsystem> PEWindowsSubsystem[] = { 384 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SUBSYSTEM_UNKNOWN ), 385 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SUBSYSTEM_NATIVE ), 386 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SUBSYSTEM_WINDOWS_GUI ), 387 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SUBSYSTEM_WINDOWS_CUI ), 388 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SUBSYSTEM_POSIX_CUI ), 389 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SUBSYSTEM_WINDOWS_CE_GUI ), 390 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SUBSYSTEM_EFI_APPLICATION ), 391 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER), 392 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER ), 393 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SUBSYSTEM_EFI_ROM ), 394 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SUBSYSTEM_XBOX ), 395 }; 396 397 const EnumEntry<COFF::DLLCharacteristics> PEDLLCharacteristics[] = { 398 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_DLL_CHARACTERISTICS_HIGH_ENTROPY_VA ), 399 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_DLL_CHARACTERISTICS_DYNAMIC_BASE ), 400 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_DLL_CHARACTERISTICS_FORCE_INTEGRITY ), 401 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_DLL_CHARACTERISTICS_NX_COMPAT ), 402 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_DLL_CHARACTERISTICS_NO_ISOLATION ), 403 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_DLL_CHARACTERISTICS_NO_SEH ), 404 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_DLL_CHARACTERISTICS_NO_BIND ), 405 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_DLL_CHARACTERISTICS_APPCONTAINER ), 406 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_DLL_CHARACTERISTICS_WDM_DRIVER ), 407 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_DLL_CHARACTERISTICS_GUARD_CF ), 408 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_DLL_CHARACTERISTICS_TERMINAL_SERVER_AWARE), 409 }; 410 411 static const EnumEntry<COFF::ExtendedDLLCharacteristics> 412 PEExtendedDLLCharacteristics[] = { 413 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_DLL_CHARACTERISTICS_EX_CET_COMPAT), 414 }; 415 416 static const EnumEntry<COFF::SectionCharacteristics> 417 ImageSectionCharacteristics[] = { 418 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_TYPE_NOLOAD ), 419 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_TYPE_NO_PAD ), 420 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_CNT_CODE ), 421 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_CNT_INITIALIZED_DATA ), 422 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_CNT_UNINITIALIZED_DATA), 423 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_LNK_OTHER ), 424 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_LNK_INFO ), 425 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_LNK_REMOVE ), 426 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_LNK_COMDAT ), 427 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_GPREL ), 428 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_MEM_PURGEABLE ), 429 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_MEM_16BIT ), 430 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_MEM_LOCKED ), 431 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_MEM_PRELOAD ), 432 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_ALIGN_1BYTES ), 433 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_ALIGN_2BYTES ), 434 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_ALIGN_4BYTES ), 435 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_ALIGN_8BYTES ), 436 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_ALIGN_16BYTES ), 437 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_ALIGN_32BYTES ), 438 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_ALIGN_64BYTES ), 439 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_ALIGN_128BYTES ), 440 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_ALIGN_256BYTES ), 441 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_ALIGN_512BYTES ), 442 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_ALIGN_1024BYTES ), 443 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_ALIGN_2048BYTES ), 444 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_ALIGN_4096BYTES ), 445 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_ALIGN_8192BYTES ), 446 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_LNK_NRELOC_OVFL ), 447 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_MEM_DISCARDABLE ), 448 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_MEM_NOT_CACHED ), 449 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_MEM_NOT_PAGED ), 450 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_MEM_SHARED ), 451 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_MEM_EXECUTE ), 452 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_MEM_READ ), 453 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_MEM_WRITE ) 454 }; 455 456 const EnumEntry<COFF::SymbolBaseType> ImageSymType[] = { 457 { "Null" , COFF::IMAGE_SYM_TYPE_NULL }, 458 { "Void" , COFF::IMAGE_SYM_TYPE_VOID }, 459 { "Char" , COFF::IMAGE_SYM_TYPE_CHAR }, 460 { "Short" , COFF::IMAGE_SYM_TYPE_SHORT }, 461 { "Int" , COFF::IMAGE_SYM_TYPE_INT }, 462 { "Long" , COFF::IMAGE_SYM_TYPE_LONG }, 463 { "Float" , COFF::IMAGE_SYM_TYPE_FLOAT }, 464 { "Double", COFF::IMAGE_SYM_TYPE_DOUBLE }, 465 { "Struct", COFF::IMAGE_SYM_TYPE_STRUCT }, 466 { "Union" , COFF::IMAGE_SYM_TYPE_UNION }, 467 { "Enum" , COFF::IMAGE_SYM_TYPE_ENUM }, 468 { "MOE" , COFF::IMAGE_SYM_TYPE_MOE }, 469 { "Byte" , COFF::IMAGE_SYM_TYPE_BYTE }, 470 { "Word" , COFF::IMAGE_SYM_TYPE_WORD }, 471 { "UInt" , COFF::IMAGE_SYM_TYPE_UINT }, 472 { "DWord" , COFF::IMAGE_SYM_TYPE_DWORD } 473 }; 474 475 const EnumEntry<COFF::SymbolComplexType> ImageSymDType[] = { 476 { "Null" , COFF::IMAGE_SYM_DTYPE_NULL }, 477 { "Pointer" , COFF::IMAGE_SYM_DTYPE_POINTER }, 478 { "Function", COFF::IMAGE_SYM_DTYPE_FUNCTION }, 479 { "Array" , COFF::IMAGE_SYM_DTYPE_ARRAY } 480 }; 481 482 const EnumEntry<COFF::SymbolStorageClass> ImageSymClass[] = { 483 { "EndOfFunction" , COFF::IMAGE_SYM_CLASS_END_OF_FUNCTION }, 484 { "Null" , COFF::IMAGE_SYM_CLASS_NULL }, 485 { "Automatic" , COFF::IMAGE_SYM_CLASS_AUTOMATIC }, 486 { "External" , COFF::IMAGE_SYM_CLASS_EXTERNAL }, 487 { "Static" , COFF::IMAGE_SYM_CLASS_STATIC }, 488 { "Register" , COFF::IMAGE_SYM_CLASS_REGISTER }, 489 { "ExternalDef" , COFF::IMAGE_SYM_CLASS_EXTERNAL_DEF }, 490 { "Label" , COFF::IMAGE_SYM_CLASS_LABEL }, 491 { "UndefinedLabel" , COFF::IMAGE_SYM_CLASS_UNDEFINED_LABEL }, 492 { "MemberOfStruct" , COFF::IMAGE_SYM_CLASS_MEMBER_OF_STRUCT }, 493 { "Argument" , COFF::IMAGE_SYM_CLASS_ARGUMENT }, 494 { "StructTag" , COFF::IMAGE_SYM_CLASS_STRUCT_TAG }, 495 { "MemberOfUnion" , COFF::IMAGE_SYM_CLASS_MEMBER_OF_UNION }, 496 { "UnionTag" , COFF::IMAGE_SYM_CLASS_UNION_TAG }, 497 { "TypeDefinition" , COFF::IMAGE_SYM_CLASS_TYPE_DEFINITION }, 498 { "UndefinedStatic", COFF::IMAGE_SYM_CLASS_UNDEFINED_STATIC }, 499 { "EnumTag" , COFF::IMAGE_SYM_CLASS_ENUM_TAG }, 500 { "MemberOfEnum" , COFF::IMAGE_SYM_CLASS_MEMBER_OF_ENUM }, 501 { "RegisterParam" , COFF::IMAGE_SYM_CLASS_REGISTER_PARAM }, 502 { "BitField" , COFF::IMAGE_SYM_CLASS_BIT_FIELD }, 503 { "Block" , COFF::IMAGE_SYM_CLASS_BLOCK }, 504 { "Function" , COFF::IMAGE_SYM_CLASS_FUNCTION }, 505 { "EndOfStruct" , COFF::IMAGE_SYM_CLASS_END_OF_STRUCT }, 506 { "File" , COFF::IMAGE_SYM_CLASS_FILE }, 507 { "Section" , COFF::IMAGE_SYM_CLASS_SECTION }, 508 { "WeakExternal" , COFF::IMAGE_SYM_CLASS_WEAK_EXTERNAL }, 509 { "CLRToken" , COFF::IMAGE_SYM_CLASS_CLR_TOKEN } 510 }; 511 512 const EnumEntry<COFF::COMDATType> ImageCOMDATSelect[] = { 513 { "NoDuplicates", COFF::IMAGE_COMDAT_SELECT_NODUPLICATES }, 514 { "Any" , COFF::IMAGE_COMDAT_SELECT_ANY }, 515 { "SameSize" , COFF::IMAGE_COMDAT_SELECT_SAME_SIZE }, 516 { "ExactMatch" , COFF::IMAGE_COMDAT_SELECT_EXACT_MATCH }, 517 { "Associative" , COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE }, 518 { "Largest" , COFF::IMAGE_COMDAT_SELECT_LARGEST }, 519 { "Newest" , COFF::IMAGE_COMDAT_SELECT_NEWEST } 520 }; 521 522 const EnumEntry<COFF::DebugType> ImageDebugType[] = { 523 {"Unknown", COFF::IMAGE_DEBUG_TYPE_UNKNOWN}, 524 {"COFF", COFF::IMAGE_DEBUG_TYPE_COFF}, 525 {"CodeView", COFF::IMAGE_DEBUG_TYPE_CODEVIEW}, 526 {"FPO", COFF::IMAGE_DEBUG_TYPE_FPO}, 527 {"Misc", COFF::IMAGE_DEBUG_TYPE_MISC}, 528 {"Exception", COFF::IMAGE_DEBUG_TYPE_EXCEPTION}, 529 {"Fixup", COFF::IMAGE_DEBUG_TYPE_FIXUP}, 530 {"OmapToSrc", COFF::IMAGE_DEBUG_TYPE_OMAP_TO_SRC}, 531 {"OmapFromSrc", COFF::IMAGE_DEBUG_TYPE_OMAP_FROM_SRC}, 532 {"Borland", COFF::IMAGE_DEBUG_TYPE_BORLAND}, 533 {"Reserved10", COFF::IMAGE_DEBUG_TYPE_RESERVED10}, 534 {"CLSID", COFF::IMAGE_DEBUG_TYPE_CLSID}, 535 {"VCFeature", COFF::IMAGE_DEBUG_TYPE_VC_FEATURE}, 536 {"POGO", COFF::IMAGE_DEBUG_TYPE_POGO}, 537 {"ILTCG", COFF::IMAGE_DEBUG_TYPE_ILTCG}, 538 {"MPX", COFF::IMAGE_DEBUG_TYPE_MPX}, 539 {"Repro", COFF::IMAGE_DEBUG_TYPE_REPRO}, 540 {"ExtendedDLLCharacteristics", 541 COFF::IMAGE_DEBUG_TYPE_EX_DLLCHARACTERISTICS}, 542 }; 543 544 static const EnumEntry<COFF::WeakExternalCharacteristics> 545 WeakExternalCharacteristics[] = { 546 { "NoLibrary", COFF::IMAGE_WEAK_EXTERN_SEARCH_NOLIBRARY }, 547 { "Library" , COFF::IMAGE_WEAK_EXTERN_SEARCH_LIBRARY }, 548 { "Alias" , COFF::IMAGE_WEAK_EXTERN_SEARCH_ALIAS } 549 }; 550 551 const EnumEntry<uint32_t> SubSectionTypes[] = { 552 LLVM_READOBJ_ENUM_CLASS_ENT(DebugSubsectionKind, Symbols), 553 LLVM_READOBJ_ENUM_CLASS_ENT(DebugSubsectionKind, Lines), 554 LLVM_READOBJ_ENUM_CLASS_ENT(DebugSubsectionKind, StringTable), 555 LLVM_READOBJ_ENUM_CLASS_ENT(DebugSubsectionKind, FileChecksums), 556 LLVM_READOBJ_ENUM_CLASS_ENT(DebugSubsectionKind, FrameData), 557 LLVM_READOBJ_ENUM_CLASS_ENT(DebugSubsectionKind, InlineeLines), 558 LLVM_READOBJ_ENUM_CLASS_ENT(DebugSubsectionKind, CrossScopeImports), 559 LLVM_READOBJ_ENUM_CLASS_ENT(DebugSubsectionKind, CrossScopeExports), 560 LLVM_READOBJ_ENUM_CLASS_ENT(DebugSubsectionKind, ILLines), 561 LLVM_READOBJ_ENUM_CLASS_ENT(DebugSubsectionKind, FuncMDTokenMap), 562 LLVM_READOBJ_ENUM_CLASS_ENT(DebugSubsectionKind, TypeMDTokenMap), 563 LLVM_READOBJ_ENUM_CLASS_ENT(DebugSubsectionKind, MergedAssemblyInput), 564 LLVM_READOBJ_ENUM_CLASS_ENT(DebugSubsectionKind, CoffSymbolRVA), 565 }; 566 567 const EnumEntry<uint32_t> FrameDataFlags[] = { 568 LLVM_READOBJ_ENUM_ENT(FrameData, HasSEH), 569 LLVM_READOBJ_ENUM_ENT(FrameData, HasEH), 570 LLVM_READOBJ_ENUM_ENT(FrameData, IsFunctionStart), 571 }; 572 573 const EnumEntry<uint8_t> FileChecksumKindNames[] = { 574 LLVM_READOBJ_ENUM_CLASS_ENT(FileChecksumKind, None), 575 LLVM_READOBJ_ENUM_CLASS_ENT(FileChecksumKind, MD5), 576 LLVM_READOBJ_ENUM_CLASS_ENT(FileChecksumKind, SHA1), 577 LLVM_READOBJ_ENUM_CLASS_ENT(FileChecksumKind, SHA256), 578 }; 579 580 template <typename T> 581 static std::error_code getSymbolAuxData(const COFFObjectFile *Obj, 582 COFFSymbolRef Symbol, 583 uint8_t AuxSymbolIdx, const T *&Aux) { 584 ArrayRef<uint8_t> AuxData = Obj->getSymbolAuxData(Symbol); 585 AuxData = AuxData.slice(AuxSymbolIdx * Obj->getSymbolTableEntrySize()); 586 Aux = reinterpret_cast<const T*>(AuxData.data()); 587 return std::error_code(); 588 } 589 590 void COFFDumper::cacheRelocations() { 591 if (RelocCached) 592 return; 593 RelocCached = true; 594 595 for (const SectionRef &S : Obj->sections()) { 596 const coff_section *Section = Obj->getCOFFSection(S); 597 598 append_range(RelocMap[Section], S.relocations()); 599 600 // Sort relocations by address. 601 llvm::sort(RelocMap[Section], [](RelocationRef L, RelocationRef R) { 602 return L.getOffset() < R.getOffset(); 603 }); 604 } 605 } 606 607 void COFFDumper::printDataDirectory(uint32_t Index, 608 const std::string &FieldName) { 609 const data_directory *Data = Obj->getDataDirectory(Index); 610 if (!Data) 611 return; 612 W.printHex(FieldName + "RVA", Data->RelativeVirtualAddress); 613 W.printHex(FieldName + "Size", Data->Size); 614 } 615 616 void COFFDumper::printFileHeaders() { 617 time_t TDS = Obj->getTimeDateStamp(); 618 char FormattedTime[20] = { }; 619 strftime(FormattedTime, 20, "%Y-%m-%d %H:%M:%S", gmtime(&TDS)); 620 621 { 622 DictScope D(W, "ImageFileHeader"); 623 W.printEnum ("Machine", Obj->getMachine(), 624 makeArrayRef(ImageFileMachineType)); 625 W.printNumber("SectionCount", Obj->getNumberOfSections()); 626 W.printHex ("TimeDateStamp", FormattedTime, Obj->getTimeDateStamp()); 627 W.printHex ("PointerToSymbolTable", Obj->getPointerToSymbolTable()); 628 W.printNumber("SymbolCount", Obj->getNumberOfSymbols()); 629 W.printNumber("StringTableSize", Obj->getStringTableSize()); 630 W.printNumber("OptionalHeaderSize", Obj->getSizeOfOptionalHeader()); 631 W.printFlags ("Characteristics", Obj->getCharacteristics(), 632 makeArrayRef(ImageFileCharacteristics)); 633 } 634 635 // Print PE header. This header does not exist if this is an object file and 636 // not an executable. 637 if (const pe32_header *PEHeader = Obj->getPE32Header()) 638 printPEHeader<pe32_header>(PEHeader); 639 640 if (const pe32plus_header *PEPlusHeader = Obj->getPE32PlusHeader()) 641 printPEHeader<pe32plus_header>(PEPlusHeader); 642 643 if (const dos_header *DH = Obj->getDOSHeader()) 644 printDOSHeader(DH); 645 } 646 647 void COFFDumper::printDOSHeader(const dos_header *DH) { 648 DictScope D(W, "DOSHeader"); 649 W.printString("Magic", StringRef(DH->Magic, sizeof(DH->Magic))); 650 W.printNumber("UsedBytesInTheLastPage", DH->UsedBytesInTheLastPage); 651 W.printNumber("FileSizeInPages", DH->FileSizeInPages); 652 W.printNumber("NumberOfRelocationItems", DH->NumberOfRelocationItems); 653 W.printNumber("HeaderSizeInParagraphs", DH->HeaderSizeInParagraphs); 654 W.printNumber("MinimumExtraParagraphs", DH->MinimumExtraParagraphs); 655 W.printNumber("MaximumExtraParagraphs", DH->MaximumExtraParagraphs); 656 W.printNumber("InitialRelativeSS", DH->InitialRelativeSS); 657 W.printNumber("InitialSP", DH->InitialSP); 658 W.printNumber("Checksum", DH->Checksum); 659 W.printNumber("InitialIP", DH->InitialIP); 660 W.printNumber("InitialRelativeCS", DH->InitialRelativeCS); 661 W.printNumber("AddressOfRelocationTable", DH->AddressOfRelocationTable); 662 W.printNumber("OverlayNumber", DH->OverlayNumber); 663 W.printNumber("OEMid", DH->OEMid); 664 W.printNumber("OEMinfo", DH->OEMinfo); 665 W.printNumber("AddressOfNewExeHeader", DH->AddressOfNewExeHeader); 666 } 667 668 template <class PEHeader> 669 void COFFDumper::printPEHeader(const PEHeader *Hdr) { 670 DictScope D(W, "ImageOptionalHeader"); 671 W.printHex ("Magic", Hdr->Magic); 672 W.printNumber("MajorLinkerVersion", Hdr->MajorLinkerVersion); 673 W.printNumber("MinorLinkerVersion", Hdr->MinorLinkerVersion); 674 W.printNumber("SizeOfCode", Hdr->SizeOfCode); 675 W.printNumber("SizeOfInitializedData", Hdr->SizeOfInitializedData); 676 W.printNumber("SizeOfUninitializedData", Hdr->SizeOfUninitializedData); 677 W.printHex ("AddressOfEntryPoint", Hdr->AddressOfEntryPoint); 678 W.printHex ("BaseOfCode", Hdr->BaseOfCode); 679 printBaseOfDataField(Hdr); 680 W.printHex ("ImageBase", Hdr->ImageBase); 681 W.printNumber("SectionAlignment", Hdr->SectionAlignment); 682 W.printNumber("FileAlignment", Hdr->FileAlignment); 683 W.printNumber("MajorOperatingSystemVersion", 684 Hdr->MajorOperatingSystemVersion); 685 W.printNumber("MinorOperatingSystemVersion", 686 Hdr->MinorOperatingSystemVersion); 687 W.printNumber("MajorImageVersion", Hdr->MajorImageVersion); 688 W.printNumber("MinorImageVersion", Hdr->MinorImageVersion); 689 W.printNumber("MajorSubsystemVersion", Hdr->MajorSubsystemVersion); 690 W.printNumber("MinorSubsystemVersion", Hdr->MinorSubsystemVersion); 691 W.printNumber("SizeOfImage", Hdr->SizeOfImage); 692 W.printNumber("SizeOfHeaders", Hdr->SizeOfHeaders); 693 W.printEnum ("Subsystem", Hdr->Subsystem, makeArrayRef(PEWindowsSubsystem)); 694 W.printFlags ("Characteristics", Hdr->DLLCharacteristics, 695 makeArrayRef(PEDLLCharacteristics)); 696 W.printNumber("SizeOfStackReserve", Hdr->SizeOfStackReserve); 697 W.printNumber("SizeOfStackCommit", Hdr->SizeOfStackCommit); 698 W.printNumber("SizeOfHeapReserve", Hdr->SizeOfHeapReserve); 699 W.printNumber("SizeOfHeapCommit", Hdr->SizeOfHeapCommit); 700 W.printNumber("NumberOfRvaAndSize", Hdr->NumberOfRvaAndSize); 701 702 if (Hdr->NumberOfRvaAndSize > 0) { 703 DictScope D(W, "DataDirectory"); 704 static const char * const directory[] = { 705 "ExportTable", "ImportTable", "ResourceTable", "ExceptionTable", 706 "CertificateTable", "BaseRelocationTable", "Debug", "Architecture", 707 "GlobalPtr", "TLSTable", "LoadConfigTable", "BoundImport", "IAT", 708 "DelayImportDescriptor", "CLRRuntimeHeader", "Reserved" 709 }; 710 711 for (uint32_t i = 0; i < Hdr->NumberOfRvaAndSize; ++i) 712 if (i < sizeof(directory) / sizeof(char *)) 713 printDataDirectory(i, directory[i]); 714 else 715 printDataDirectory(i, "Unknown"); 716 } 717 } 718 719 void COFFDumper::printCOFFDebugDirectory() { 720 ListScope LS(W, "DebugDirectory"); 721 for (const debug_directory &D : Obj->debug_directories()) { 722 char FormattedTime[20] = {}; 723 time_t TDS = D.TimeDateStamp; 724 strftime(FormattedTime, 20, "%Y-%m-%d %H:%M:%S", gmtime(&TDS)); 725 DictScope S(W, "DebugEntry"); 726 W.printHex("Characteristics", D.Characteristics); 727 W.printHex("TimeDateStamp", FormattedTime, D.TimeDateStamp); 728 W.printHex("MajorVersion", D.MajorVersion); 729 W.printHex("MinorVersion", D.MinorVersion); 730 W.printEnum("Type", D.Type, makeArrayRef(ImageDebugType)); 731 W.printHex("SizeOfData", D.SizeOfData); 732 W.printHex("AddressOfRawData", D.AddressOfRawData); 733 W.printHex("PointerToRawData", D.PointerToRawData); 734 // Ideally, if D.AddressOfRawData == 0, we should try to load the payload 735 // using D.PointerToRawData instead. 736 if (D.AddressOfRawData == 0) 737 continue; 738 if (D.Type == COFF::IMAGE_DEBUG_TYPE_CODEVIEW) { 739 const codeview::DebugInfo *DebugInfo; 740 StringRef PDBFileName; 741 if (Error E = Obj->getDebugPDBInfo(&D, DebugInfo, PDBFileName)) 742 reportError(std::move(E), Obj->getFileName()); 743 744 DictScope PDBScope(W, "PDBInfo"); 745 W.printHex("PDBSignature", DebugInfo->Signature.CVSignature); 746 if (DebugInfo->Signature.CVSignature == OMF::Signature::PDB70) { 747 W.printBinary("PDBGUID", makeArrayRef(DebugInfo->PDB70.Signature)); 748 W.printNumber("PDBAge", DebugInfo->PDB70.Age); 749 W.printString("PDBFileName", PDBFileName); 750 } 751 } else if (D.SizeOfData != 0) { 752 // FIXME: Data visualization for IMAGE_DEBUG_TYPE_VC_FEATURE and 753 // IMAGE_DEBUG_TYPE_POGO? 754 ArrayRef<uint8_t> RawData; 755 if (Error E = Obj->getRvaAndSizeAsBytes(D.AddressOfRawData, 756 D.SizeOfData, RawData)) 757 reportError(std::move(E), Obj->getFileName()); 758 if (D.Type == COFF::IMAGE_DEBUG_TYPE_EX_DLLCHARACTERISTICS) { 759 // FIXME right now the only possible value would fit in 8 bits, 760 // but that might change in the future 761 uint16_t Characteristics = RawData[0]; 762 W.printFlags("ExtendedCharacteristics", Characteristics, 763 makeArrayRef(PEExtendedDLLCharacteristics)); 764 } 765 W.printBinaryBlock("RawData", RawData); 766 } 767 } 768 } 769 770 void COFFDumper::printRVATable(uint64_t TableVA, uint64_t Count, 771 uint64_t EntrySize, PrintExtraCB PrintExtra) { 772 uintptr_t TableStart, TableEnd; 773 if (Error E = Obj->getVaPtr(TableVA, TableStart)) 774 reportError(std::move(E), Obj->getFileName()); 775 if (Error E = 776 Obj->getVaPtr(TableVA + Count * EntrySize - 1, TableEnd)) 777 reportError(std::move(E), Obj->getFileName()); 778 TableEnd++; 779 for (uintptr_t I = TableStart; I < TableEnd; I += EntrySize) { 780 uint32_t RVA = *reinterpret_cast<const ulittle32_t *>(I); 781 raw_ostream &OS = W.startLine(); 782 OS << W.hex(Obj->getImageBase() + RVA); 783 if (PrintExtra) 784 PrintExtra(OS, reinterpret_cast<const uint8_t *>(I)); 785 OS << '\n'; 786 } 787 } 788 789 void COFFDumper::printCOFFLoadConfig() { 790 LoadConfigTables Tables; 791 if (Obj->is64()) 792 printCOFFLoadConfig(Obj->getLoadConfig64(), Tables); 793 else 794 printCOFFLoadConfig(Obj->getLoadConfig32(), Tables); 795 796 if (Tables.SEHTableVA) { 797 ListScope LS(W, "SEHTable"); 798 printRVATable(Tables.SEHTableVA, Tables.SEHTableCount, 4); 799 } 800 801 auto PrintGuardFlags = [](raw_ostream &OS, const uint8_t *Entry) { 802 uint8_t Flags = *reinterpret_cast<const uint8_t *>(Entry + 4); 803 if (Flags) 804 OS << " flags " << utohexstr(Flags); 805 }; 806 807 if (Tables.GuardFidTableVA) { 808 ListScope LS(W, "GuardFidTable"); 809 if (Tables.GuardFlags & uint32_t(coff_guard_flags::FidTableHasFlags)) 810 printRVATable(Tables.GuardFidTableVA, Tables.GuardFidTableCount, 5, 811 PrintGuardFlags); 812 else 813 printRVATable(Tables.GuardFidTableVA, Tables.GuardFidTableCount, 4); 814 } 815 816 if (Tables.GuardIatTableVA) { 817 ListScope LS(W, "GuardIatTable"); 818 printRVATable(Tables.GuardIatTableVA, Tables.GuardIatTableCount, 4); 819 } 820 821 if (Tables.GuardLJmpTableVA) { 822 ListScope LS(W, "GuardLJmpTable"); 823 printRVATable(Tables.GuardLJmpTableVA, Tables.GuardLJmpTableCount, 4); 824 } 825 826 if (Tables.GuardEHContTableVA) { 827 ListScope LS(W, "GuardEHContTable"); 828 printRVATable(Tables.GuardEHContTableVA, Tables.GuardEHContTableCount, 5, 829 PrintGuardFlags); 830 } 831 } 832 833 template <typename T> 834 void COFFDumper::printCOFFLoadConfig(const T *Conf, LoadConfigTables &Tables) { 835 if (!Conf) 836 return; 837 838 ListScope LS(W, "LoadConfig"); 839 char FormattedTime[20] = {}; 840 time_t TDS = Conf->TimeDateStamp; 841 strftime(FormattedTime, 20, "%Y-%m-%d %H:%M:%S", gmtime(&TDS)); 842 W.printHex("Size", Conf->Size); 843 844 // Print everything before SecurityCookie. The vast majority of images today 845 // have all these fields. 846 if (Conf->Size < offsetof(T, SEHandlerTable)) 847 return; 848 W.printHex("TimeDateStamp", FormattedTime, TDS); 849 W.printHex("MajorVersion", Conf->MajorVersion); 850 W.printHex("MinorVersion", Conf->MinorVersion); 851 W.printHex("GlobalFlagsClear", Conf->GlobalFlagsClear); 852 W.printHex("GlobalFlagsSet", Conf->GlobalFlagsSet); 853 W.printHex("CriticalSectionDefaultTimeout", 854 Conf->CriticalSectionDefaultTimeout); 855 W.printHex("DeCommitFreeBlockThreshold", Conf->DeCommitFreeBlockThreshold); 856 W.printHex("DeCommitTotalFreeThreshold", Conf->DeCommitTotalFreeThreshold); 857 W.printHex("LockPrefixTable", Conf->LockPrefixTable); 858 W.printHex("MaximumAllocationSize", Conf->MaximumAllocationSize); 859 W.printHex("VirtualMemoryThreshold", Conf->VirtualMemoryThreshold); 860 W.printHex("ProcessHeapFlags", Conf->ProcessHeapFlags); 861 W.printHex("ProcessAffinityMask", Conf->ProcessAffinityMask); 862 W.printHex("CSDVersion", Conf->CSDVersion); 863 W.printHex("DependentLoadFlags", Conf->DependentLoadFlags); 864 W.printHex("EditList", Conf->EditList); 865 W.printHex("SecurityCookie", Conf->SecurityCookie); 866 867 // Print the safe SEH table if present. 868 if (Conf->Size < offsetof(coff_load_configuration32, GuardCFCheckFunction)) 869 return; 870 W.printHex("SEHandlerTable", Conf->SEHandlerTable); 871 W.printNumber("SEHandlerCount", Conf->SEHandlerCount); 872 873 Tables.SEHTableVA = Conf->SEHandlerTable; 874 Tables.SEHTableCount = Conf->SEHandlerCount; 875 876 // Print everything before CodeIntegrity. (2015) 877 if (Conf->Size < offsetof(T, CodeIntegrity)) 878 return; 879 W.printHex("GuardCFCheckFunction", Conf->GuardCFCheckFunction); 880 W.printHex("GuardCFCheckDispatch", Conf->GuardCFCheckDispatch); 881 W.printHex("GuardCFFunctionTable", Conf->GuardCFFunctionTable); 882 W.printNumber("GuardCFFunctionCount", Conf->GuardCFFunctionCount); 883 W.printHex("GuardFlags", Conf->GuardFlags); 884 885 Tables.GuardFidTableVA = Conf->GuardCFFunctionTable; 886 Tables.GuardFidTableCount = Conf->GuardCFFunctionCount; 887 Tables.GuardFlags = Conf->GuardFlags; 888 889 // Print everything before Reserved3. (2017) 890 if (Conf->Size < offsetof(T, Reserved3)) 891 return; 892 W.printHex("GuardAddressTakenIatEntryTable", 893 Conf->GuardAddressTakenIatEntryTable); 894 W.printNumber("GuardAddressTakenIatEntryCount", 895 Conf->GuardAddressTakenIatEntryCount); 896 W.printHex("GuardLongJumpTargetTable", Conf->GuardLongJumpTargetTable); 897 W.printNumber("GuardLongJumpTargetCount", Conf->GuardLongJumpTargetCount); 898 W.printHex("DynamicValueRelocTable", Conf->DynamicValueRelocTable); 899 W.printHex("CHPEMetadataPointer", Conf->CHPEMetadataPointer); 900 W.printHex("GuardRFFailureRoutine", Conf->GuardRFFailureRoutine); 901 W.printHex("GuardRFFailureRoutineFunctionPointer", 902 Conf->GuardRFFailureRoutineFunctionPointer); 903 W.printHex("DynamicValueRelocTableOffset", 904 Conf->DynamicValueRelocTableOffset); 905 W.printNumber("DynamicValueRelocTableSection", 906 Conf->DynamicValueRelocTableSection); 907 W.printHex("GuardRFVerifyStackPointerFunctionPointer", 908 Conf->GuardRFVerifyStackPointerFunctionPointer); 909 W.printHex("HotPatchTableOffset", Conf->HotPatchTableOffset); 910 911 Tables.GuardIatTableVA = Conf->GuardAddressTakenIatEntryTable; 912 Tables.GuardIatTableCount = Conf->GuardAddressTakenIatEntryCount; 913 914 Tables.GuardLJmpTableVA = Conf->GuardLongJumpTargetTable; 915 Tables.GuardLJmpTableCount = Conf->GuardLongJumpTargetCount; 916 917 // Print the rest. (2019) 918 if (Conf->Size < sizeof(T)) 919 return; 920 W.printHex("EnclaveConfigurationPointer", Conf->EnclaveConfigurationPointer); 921 W.printHex("VolatileMetadataPointer", Conf->VolatileMetadataPointer); 922 W.printHex("GuardEHContinuationTable", Conf->GuardEHContinuationTable); 923 W.printNumber("GuardEHContinuationCount", Conf->GuardEHContinuationCount); 924 925 Tables.GuardEHContTableVA = Conf->GuardEHContinuationTable; 926 Tables.GuardEHContTableCount = Conf->GuardEHContinuationCount; 927 } 928 929 void COFFDumper::printBaseOfDataField(const pe32_header *Hdr) { 930 W.printHex("BaseOfData", Hdr->BaseOfData); 931 } 932 933 void COFFDumper::printBaseOfDataField(const pe32plus_header *) {} 934 935 void COFFDumper::printCodeViewDebugInfo() { 936 // Print types first to build CVUDTNames, then print symbols. 937 for (const SectionRef &S : Obj->sections()) { 938 StringRef SectionName = unwrapOrError(Obj->getFileName(), S.getName()); 939 // .debug$T is a standard CodeView type section, while .debug$P is the same 940 // format but used for MSVC precompiled header object files. 941 if (SectionName == ".debug$T" || SectionName == ".debug$P") 942 printCodeViewTypeSection(SectionName, S); 943 } 944 for (const SectionRef &S : Obj->sections()) { 945 StringRef SectionName = unwrapOrError(Obj->getFileName(), S.getName()); 946 if (SectionName == ".debug$S") 947 printCodeViewSymbolSection(SectionName, S); 948 } 949 } 950 951 void COFFDumper::initializeFileAndStringTables(BinaryStreamReader &Reader) { 952 while (Reader.bytesRemaining() > 0 && 953 (!CVFileChecksumTable.valid() || !CVStringTable.valid())) { 954 // The section consists of a number of subsection in the following format: 955 // |SubSectionType|SubSectionSize|Contents...| 956 uint32_t SubType, SubSectionSize; 957 958 if (Error E = Reader.readInteger(SubType)) 959 reportError(std::move(E), Obj->getFileName()); 960 if (Error E = Reader.readInteger(SubSectionSize)) 961 reportError(std::move(E), Obj->getFileName()); 962 963 StringRef Contents; 964 if (Error E = Reader.readFixedString(Contents, SubSectionSize)) 965 reportError(std::move(E), Obj->getFileName()); 966 967 BinaryStreamRef ST(Contents, support::little); 968 switch (DebugSubsectionKind(SubType)) { 969 case DebugSubsectionKind::FileChecksums: 970 if (Error E = CVFileChecksumTable.initialize(ST)) 971 reportError(std::move(E), Obj->getFileName()); 972 break; 973 case DebugSubsectionKind::StringTable: 974 if (Error E = CVStringTable.initialize(ST)) 975 reportError(std::move(E), Obj->getFileName()); 976 break; 977 default: 978 break; 979 } 980 981 uint32_t PaddedSize = alignTo(SubSectionSize, 4); 982 if (Error E = Reader.skip(PaddedSize - SubSectionSize)) 983 reportError(std::move(E), Obj->getFileName()); 984 } 985 } 986 987 void COFFDumper::printCodeViewSymbolSection(StringRef SectionName, 988 const SectionRef &Section) { 989 StringRef SectionContents = 990 unwrapOrError(Obj->getFileName(), Section.getContents()); 991 StringRef Data = SectionContents; 992 993 SmallVector<StringRef, 10> FunctionNames; 994 StringMap<StringRef> FunctionLineTables; 995 996 ListScope D(W, "CodeViewDebugInfo"); 997 // Print the section to allow correlation with printSectionHeaders. 998 W.printNumber("Section", SectionName, Obj->getSectionID(Section)); 999 1000 uint32_t Magic; 1001 if (Error E = consume(Data, Magic)) 1002 reportError(std::move(E), Obj->getFileName()); 1003 1004 W.printHex("Magic", Magic); 1005 if (Magic != COFF::DEBUG_SECTION_MAGIC) 1006 reportError(errorCodeToError(object_error::parse_failed), 1007 Obj->getFileName()); 1008 1009 BinaryStreamReader FSReader(Data, support::little); 1010 initializeFileAndStringTables(FSReader); 1011 1012 // TODO: Convert this over to using ModuleSubstreamVisitor. 1013 while (!Data.empty()) { 1014 // The section consists of a number of subsection in the following format: 1015 // |SubSectionType|SubSectionSize|Contents...| 1016 uint32_t SubType, SubSectionSize; 1017 if (Error E = consume(Data, SubType)) 1018 reportError(std::move(E), Obj->getFileName()); 1019 if (Error E = consume(Data, SubSectionSize)) 1020 reportError(std::move(E), Obj->getFileName()); 1021 1022 ListScope S(W, "Subsection"); 1023 // Dump the subsection as normal even if the ignore bit is set. 1024 if (SubType & SubsectionIgnoreFlag) { 1025 W.printHex("IgnoredSubsectionKind", SubType); 1026 SubType &= ~SubsectionIgnoreFlag; 1027 } 1028 W.printEnum("SubSectionType", SubType, makeArrayRef(SubSectionTypes)); 1029 W.printHex("SubSectionSize", SubSectionSize); 1030 1031 // Get the contents of the subsection. 1032 if (SubSectionSize > Data.size()) 1033 return reportError(errorCodeToError(object_error::parse_failed), 1034 Obj->getFileName()); 1035 StringRef Contents = Data.substr(0, SubSectionSize); 1036 1037 // Add SubSectionSize to the current offset and align that offset to find 1038 // the next subsection. 1039 size_t SectionOffset = Data.data() - SectionContents.data(); 1040 size_t NextOffset = SectionOffset + SubSectionSize; 1041 NextOffset = alignTo(NextOffset, 4); 1042 if (NextOffset > SectionContents.size()) 1043 return reportError(errorCodeToError(object_error::parse_failed), 1044 Obj->getFileName()); 1045 Data = SectionContents.drop_front(NextOffset); 1046 1047 // Optionally print the subsection bytes in case our parsing gets confused 1048 // later. 1049 if (opts::CodeViewSubsectionBytes) 1050 printBinaryBlockWithRelocs("SubSectionContents", Section, SectionContents, 1051 Contents); 1052 1053 switch (DebugSubsectionKind(SubType)) { 1054 case DebugSubsectionKind::Symbols: 1055 printCodeViewSymbolsSubsection(Contents, Section, SectionContents); 1056 break; 1057 1058 case DebugSubsectionKind::InlineeLines: 1059 printCodeViewInlineeLines(Contents); 1060 break; 1061 1062 case DebugSubsectionKind::FileChecksums: 1063 printCodeViewFileChecksums(Contents); 1064 break; 1065 1066 case DebugSubsectionKind::Lines: { 1067 // Holds a PC to file:line table. Some data to parse this subsection is 1068 // stored in the other subsections, so just check sanity and store the 1069 // pointers for deferred processing. 1070 1071 if (SubSectionSize < 12) { 1072 // There should be at least three words to store two function 1073 // relocations and size of the code. 1074 reportError(errorCodeToError(object_error::parse_failed), 1075 Obj->getFileName()); 1076 return; 1077 } 1078 1079 StringRef LinkageName; 1080 if (std::error_code EC = resolveSymbolName(Obj->getCOFFSection(Section), 1081 SectionOffset, LinkageName)) 1082 reportError(errorCodeToError(EC), Obj->getFileName()); 1083 1084 W.printString("LinkageName", LinkageName); 1085 if (FunctionLineTables.count(LinkageName) != 0) { 1086 // Saw debug info for this function already? 1087 reportError(errorCodeToError(object_error::parse_failed), 1088 Obj->getFileName()); 1089 return; 1090 } 1091 1092 FunctionLineTables[LinkageName] = Contents; 1093 FunctionNames.push_back(LinkageName); 1094 break; 1095 } 1096 case DebugSubsectionKind::FrameData: { 1097 // First four bytes is a relocation against the function. 1098 BinaryStreamReader SR(Contents, llvm::support::little); 1099 1100 DebugFrameDataSubsectionRef FrameData; 1101 if (Error E = FrameData.initialize(SR)) 1102 reportError(std::move(E), Obj->getFileName()); 1103 1104 StringRef LinkageName; 1105 if (std::error_code EC = 1106 resolveSymbolName(Obj->getCOFFSection(Section), SectionContents, 1107 FrameData.getRelocPtr(), LinkageName)) 1108 reportError(errorCodeToError(EC), Obj->getFileName()); 1109 W.printString("LinkageName", LinkageName); 1110 1111 // To find the active frame description, search this array for the 1112 // smallest PC range that includes the current PC. 1113 for (const auto &FD : FrameData) { 1114 StringRef FrameFunc = unwrapOrError( 1115 Obj->getFileName(), CVStringTable.getString(FD.FrameFunc)); 1116 1117 DictScope S(W, "FrameData"); 1118 W.printHex("RvaStart", FD.RvaStart); 1119 W.printHex("CodeSize", FD.CodeSize); 1120 W.printHex("LocalSize", FD.LocalSize); 1121 W.printHex("ParamsSize", FD.ParamsSize); 1122 W.printHex("MaxStackSize", FD.MaxStackSize); 1123 W.printHex("PrologSize", FD.PrologSize); 1124 W.printHex("SavedRegsSize", FD.SavedRegsSize); 1125 W.printFlags("Flags", FD.Flags, makeArrayRef(FrameDataFlags)); 1126 1127 // The FrameFunc string is a small RPN program. It can be broken up into 1128 // statements that end in the '=' operator, which assigns the value on 1129 // the top of the stack to the previously pushed variable. Variables can 1130 // be temporary values ($T0) or physical registers ($esp). Print each 1131 // assignment on its own line to make these programs easier to read. 1132 { 1133 ListScope FFS(W, "FrameFunc"); 1134 while (!FrameFunc.empty()) { 1135 size_t EqOrEnd = FrameFunc.find('='); 1136 if (EqOrEnd == StringRef::npos) 1137 EqOrEnd = FrameFunc.size(); 1138 else 1139 ++EqOrEnd; 1140 StringRef Stmt = FrameFunc.substr(0, EqOrEnd); 1141 W.printString(Stmt); 1142 FrameFunc = FrameFunc.drop_front(EqOrEnd).trim(); 1143 } 1144 } 1145 } 1146 break; 1147 } 1148 1149 // Do nothing for unrecognized subsections. 1150 default: 1151 break; 1152 } 1153 W.flush(); 1154 } 1155 1156 // Dump the line tables now that we've read all the subsections and know all 1157 // the required information. 1158 for (unsigned I = 0, E = FunctionNames.size(); I != E; ++I) { 1159 StringRef Name = FunctionNames[I]; 1160 ListScope S(W, "FunctionLineTable"); 1161 W.printString("LinkageName", Name); 1162 1163 BinaryStreamReader Reader(FunctionLineTables[Name], support::little); 1164 1165 DebugLinesSubsectionRef LineInfo; 1166 if (Error E = LineInfo.initialize(Reader)) 1167 reportError(std::move(E), Obj->getFileName()); 1168 1169 W.printHex("Flags", LineInfo.header()->Flags); 1170 W.printHex("CodeSize", LineInfo.header()->CodeSize); 1171 for (const auto &Entry : LineInfo) { 1172 1173 ListScope S(W, "FilenameSegment"); 1174 printFileNameForOffset("Filename", Entry.NameIndex); 1175 uint32_t ColumnIndex = 0; 1176 for (const auto &Line : Entry.LineNumbers) { 1177 if (Line.Offset >= LineInfo.header()->CodeSize) { 1178 reportError(errorCodeToError(object_error::parse_failed), 1179 Obj->getFileName()); 1180 return; 1181 } 1182 1183 std::string PC = std::string(formatv("+{0:X}", uint32_t(Line.Offset))); 1184 ListScope PCScope(W, PC); 1185 codeview::LineInfo LI(Line.Flags); 1186 1187 if (LI.isAlwaysStepInto()) 1188 W.printString("StepInto", StringRef("Always")); 1189 else if (LI.isNeverStepInto()) 1190 W.printString("StepInto", StringRef("Never")); 1191 else 1192 W.printNumber("LineNumberStart", LI.getStartLine()); 1193 W.printNumber("LineNumberEndDelta", LI.getLineDelta()); 1194 W.printBoolean("IsStatement", LI.isStatement()); 1195 if (LineInfo.hasColumnInfo()) { 1196 W.printNumber("ColStart", Entry.Columns[ColumnIndex].StartColumn); 1197 W.printNumber("ColEnd", Entry.Columns[ColumnIndex].EndColumn); 1198 ++ColumnIndex; 1199 } 1200 } 1201 } 1202 } 1203 } 1204 1205 void COFFDumper::printCodeViewSymbolsSubsection(StringRef Subsection, 1206 const SectionRef &Section, 1207 StringRef SectionContents) { 1208 ArrayRef<uint8_t> BinaryData(Subsection.bytes_begin(), 1209 Subsection.bytes_end()); 1210 auto CODD = std::make_unique<COFFObjectDumpDelegate>(*this, Section, Obj, 1211 SectionContents); 1212 CVSymbolDumper CVSD(W, Types, CodeViewContainer::ObjectFile, std::move(CODD), 1213 CompilationCPUType, opts::CodeViewSubsectionBytes); 1214 CVSymbolArray Symbols; 1215 BinaryStreamReader Reader(BinaryData, llvm::support::little); 1216 if (Error E = Reader.readArray(Symbols, Reader.getLength())) { 1217 W.flush(); 1218 reportError(std::move(E), Obj->getFileName()); 1219 } 1220 1221 if (Error E = CVSD.dump(Symbols)) { 1222 W.flush(); 1223 reportError(std::move(E), Obj->getFileName()); 1224 } 1225 CompilationCPUType = CVSD.getCompilationCPUType(); 1226 W.flush(); 1227 } 1228 1229 void COFFDumper::printCodeViewFileChecksums(StringRef Subsection) { 1230 BinaryStreamRef Stream(Subsection, llvm::support::little); 1231 DebugChecksumsSubsectionRef Checksums; 1232 if (Error E = Checksums.initialize(Stream)) 1233 reportError(std::move(E), Obj->getFileName()); 1234 1235 for (auto &FC : Checksums) { 1236 DictScope S(W, "FileChecksum"); 1237 1238 StringRef Filename = unwrapOrError( 1239 Obj->getFileName(), CVStringTable.getString(FC.FileNameOffset)); 1240 W.printHex("Filename", Filename, FC.FileNameOffset); 1241 W.printHex("ChecksumSize", FC.Checksum.size()); 1242 W.printEnum("ChecksumKind", uint8_t(FC.Kind), 1243 makeArrayRef(FileChecksumKindNames)); 1244 1245 W.printBinary("ChecksumBytes", FC.Checksum); 1246 } 1247 } 1248 1249 void COFFDumper::printCodeViewInlineeLines(StringRef Subsection) { 1250 BinaryStreamReader SR(Subsection, llvm::support::little); 1251 DebugInlineeLinesSubsectionRef Lines; 1252 if (Error E = Lines.initialize(SR)) 1253 reportError(std::move(E), Obj->getFileName()); 1254 1255 for (auto &Line : Lines) { 1256 DictScope S(W, "InlineeSourceLine"); 1257 printTypeIndex("Inlinee", Line.Header->Inlinee); 1258 printFileNameForOffset("FileID", Line.Header->FileID); 1259 W.printNumber("SourceLineNum", Line.Header->SourceLineNum); 1260 1261 if (Lines.hasExtraFiles()) { 1262 W.printNumber("ExtraFileCount", Line.ExtraFiles.size()); 1263 ListScope ExtraFiles(W, "ExtraFiles"); 1264 for (const auto &FID : Line.ExtraFiles) { 1265 printFileNameForOffset("FileID", FID); 1266 } 1267 } 1268 } 1269 } 1270 1271 StringRef COFFDumper::getFileNameForFileOffset(uint32_t FileOffset) { 1272 // The file checksum subsection should precede all references to it. 1273 if (!CVFileChecksumTable.valid() || !CVStringTable.valid()) 1274 reportError(errorCodeToError(object_error::parse_failed), 1275 Obj->getFileName()); 1276 1277 auto Iter = CVFileChecksumTable.getArray().at(FileOffset); 1278 1279 // Check if the file checksum table offset is valid. 1280 if (Iter == CVFileChecksumTable.end()) 1281 reportError(errorCodeToError(object_error::parse_failed), 1282 Obj->getFileName()); 1283 1284 return unwrapOrError(Obj->getFileName(), 1285 CVStringTable.getString(Iter->FileNameOffset)); 1286 } 1287 1288 void COFFDumper::printFileNameForOffset(StringRef Label, uint32_t FileOffset) { 1289 W.printHex(Label, getFileNameForFileOffset(FileOffset), FileOffset); 1290 } 1291 1292 void COFFDumper::mergeCodeViewTypes(MergingTypeTableBuilder &CVIDs, 1293 MergingTypeTableBuilder &CVTypes, 1294 GlobalTypeTableBuilder &GlobalCVIDs, 1295 GlobalTypeTableBuilder &GlobalCVTypes, 1296 bool GHash) { 1297 for (const SectionRef &S : Obj->sections()) { 1298 StringRef SectionName = unwrapOrError(Obj->getFileName(), S.getName()); 1299 if (SectionName == ".debug$T") { 1300 StringRef Data = unwrapOrError(Obj->getFileName(), S.getContents()); 1301 uint32_t Magic; 1302 if (Error E = consume(Data, Magic)) 1303 reportError(std::move(E), Obj->getFileName()); 1304 1305 if (Magic != 4) 1306 reportError(errorCodeToError(object_error::parse_failed), 1307 Obj->getFileName()); 1308 1309 CVTypeArray Types; 1310 BinaryStreamReader Reader(Data, llvm::support::little); 1311 if (auto EC = Reader.readArray(Types, Reader.getLength())) { 1312 consumeError(std::move(EC)); 1313 W.flush(); 1314 reportError(errorCodeToError(object_error::parse_failed), 1315 Obj->getFileName()); 1316 } 1317 SmallVector<TypeIndex, 128> SourceToDest; 1318 Optional<uint32_t> PCHSignature; 1319 if (GHash) { 1320 std::vector<GloballyHashedType> Hashes = 1321 GloballyHashedType::hashTypes(Types); 1322 if (Error E = 1323 mergeTypeAndIdRecords(GlobalCVIDs, GlobalCVTypes, SourceToDest, 1324 Types, Hashes, PCHSignature)) 1325 return reportError(std::move(E), Obj->getFileName()); 1326 } else { 1327 if (Error E = mergeTypeAndIdRecords(CVIDs, CVTypes, SourceToDest, Types, 1328 PCHSignature)) 1329 return reportError(std::move(E), Obj->getFileName()); 1330 } 1331 } 1332 } 1333 } 1334 1335 void COFFDumper::printCodeViewTypeSection(StringRef SectionName, 1336 const SectionRef &Section) { 1337 ListScope D(W, "CodeViewTypes"); 1338 W.printNumber("Section", SectionName, Obj->getSectionID(Section)); 1339 1340 StringRef Data = unwrapOrError(Obj->getFileName(), Section.getContents()); 1341 if (opts::CodeViewSubsectionBytes) 1342 W.printBinaryBlock("Data", Data); 1343 1344 uint32_t Magic; 1345 if (Error E = consume(Data, Magic)) 1346 reportError(std::move(E), Obj->getFileName()); 1347 1348 W.printHex("Magic", Magic); 1349 if (Magic != COFF::DEBUG_SECTION_MAGIC) 1350 reportError(errorCodeToError(object_error::parse_failed), 1351 Obj->getFileName()); 1352 1353 Types.reset(Data, 100); 1354 1355 TypeDumpVisitor TDV(Types, &W, opts::CodeViewSubsectionBytes); 1356 if (Error E = codeview::visitTypeStream(Types, TDV)) 1357 reportError(std::move(E), Obj->getFileName()); 1358 1359 W.flush(); 1360 } 1361 1362 void COFFDumper::printSectionHeaders() { 1363 ListScope SectionsD(W, "Sections"); 1364 int SectionNumber = 0; 1365 for (const SectionRef &Sec : Obj->sections()) { 1366 ++SectionNumber; 1367 const coff_section *Section = Obj->getCOFFSection(Sec); 1368 1369 StringRef Name = unwrapOrError(Obj->getFileName(), Sec.getName()); 1370 1371 DictScope D(W, "Section"); 1372 W.printNumber("Number", SectionNumber); 1373 W.printBinary("Name", Name, Section->Name); 1374 W.printHex ("VirtualSize", Section->VirtualSize); 1375 W.printHex ("VirtualAddress", Section->VirtualAddress); 1376 W.printNumber("RawDataSize", Section->SizeOfRawData); 1377 W.printHex ("PointerToRawData", Section->PointerToRawData); 1378 W.printHex ("PointerToRelocations", Section->PointerToRelocations); 1379 W.printHex ("PointerToLineNumbers", Section->PointerToLinenumbers); 1380 W.printNumber("RelocationCount", Section->NumberOfRelocations); 1381 W.printNumber("LineNumberCount", Section->NumberOfLinenumbers); 1382 W.printFlags ("Characteristics", Section->Characteristics, 1383 makeArrayRef(ImageSectionCharacteristics), 1384 COFF::SectionCharacteristics(0x00F00000)); 1385 1386 if (opts::SectionRelocations) { 1387 ListScope D(W, "Relocations"); 1388 for (const RelocationRef &Reloc : Sec.relocations()) 1389 printRelocation(Sec, Reloc); 1390 } 1391 1392 if (opts::SectionSymbols) { 1393 ListScope D(W, "Symbols"); 1394 for (const SymbolRef &Symbol : Obj->symbols()) { 1395 if (!Sec.containsSymbol(Symbol)) 1396 continue; 1397 1398 printSymbol(Symbol); 1399 } 1400 } 1401 1402 if (opts::SectionData && 1403 !(Section->Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA)) { 1404 StringRef Data = unwrapOrError(Obj->getFileName(), Sec.getContents()); 1405 W.printBinaryBlock("SectionData", Data); 1406 } 1407 } 1408 } 1409 1410 void COFFDumper::printRelocations() { 1411 ListScope D(W, "Relocations"); 1412 1413 int SectionNumber = 0; 1414 for (const SectionRef &Section : Obj->sections()) { 1415 ++SectionNumber; 1416 StringRef Name = unwrapOrError(Obj->getFileName(), Section.getName()); 1417 1418 bool PrintedGroup = false; 1419 for (const RelocationRef &Reloc : Section.relocations()) { 1420 if (!PrintedGroup) { 1421 W.startLine() << "Section (" << SectionNumber << ") " << Name << " {\n"; 1422 W.indent(); 1423 PrintedGroup = true; 1424 } 1425 1426 printRelocation(Section, Reloc); 1427 } 1428 1429 if (PrintedGroup) { 1430 W.unindent(); 1431 W.startLine() << "}\n"; 1432 } 1433 } 1434 } 1435 1436 void COFFDumper::printRelocation(const SectionRef &Section, 1437 const RelocationRef &Reloc, uint64_t Bias) { 1438 uint64_t Offset = Reloc.getOffset() - Bias; 1439 uint64_t RelocType = Reloc.getType(); 1440 SmallString<32> RelocName; 1441 StringRef SymbolName; 1442 Reloc.getTypeName(RelocName); 1443 symbol_iterator Symbol = Reloc.getSymbol(); 1444 int64_t SymbolIndex = -1; 1445 if (Symbol != Obj->symbol_end()) { 1446 Expected<StringRef> SymbolNameOrErr = Symbol->getName(); 1447 if (!SymbolNameOrErr) 1448 reportError(SymbolNameOrErr.takeError(), Obj->getFileName()); 1449 1450 SymbolName = *SymbolNameOrErr; 1451 SymbolIndex = Obj->getSymbolIndex(Obj->getCOFFSymbol(*Symbol)); 1452 } 1453 1454 if (opts::ExpandRelocs) { 1455 DictScope Group(W, "Relocation"); 1456 W.printHex("Offset", Offset); 1457 W.printNumber("Type", RelocName, RelocType); 1458 W.printString("Symbol", SymbolName.empty() ? "-" : SymbolName); 1459 W.printNumber("SymbolIndex", SymbolIndex); 1460 } else { 1461 raw_ostream& OS = W.startLine(); 1462 OS << W.hex(Offset) 1463 << " " << RelocName 1464 << " " << (SymbolName.empty() ? "-" : SymbolName) 1465 << " (" << SymbolIndex << ")" 1466 << "\n"; 1467 } 1468 } 1469 1470 void COFFDumper::printSymbols() { 1471 ListScope Group(W, "Symbols"); 1472 1473 for (const SymbolRef &Symbol : Obj->symbols()) 1474 printSymbol(Symbol); 1475 } 1476 1477 void COFFDumper::printDynamicSymbols() { ListScope Group(W, "DynamicSymbols"); } 1478 1479 static Expected<StringRef> 1480 getSectionName(const llvm::object::COFFObjectFile *Obj, int32_t SectionNumber, 1481 const coff_section *Section) { 1482 if (Section) 1483 return Obj->getSectionName(Section); 1484 if (SectionNumber == llvm::COFF::IMAGE_SYM_DEBUG) 1485 return StringRef("IMAGE_SYM_DEBUG"); 1486 if (SectionNumber == llvm::COFF::IMAGE_SYM_ABSOLUTE) 1487 return StringRef("IMAGE_SYM_ABSOLUTE"); 1488 if (SectionNumber == llvm::COFF::IMAGE_SYM_UNDEFINED) 1489 return StringRef("IMAGE_SYM_UNDEFINED"); 1490 return StringRef(""); 1491 } 1492 1493 void COFFDumper::printSymbol(const SymbolRef &Sym) { 1494 DictScope D(W, "Symbol"); 1495 1496 COFFSymbolRef Symbol = Obj->getCOFFSymbol(Sym); 1497 Expected<const coff_section *> SecOrErr = 1498 Obj->getSection(Symbol.getSectionNumber()); 1499 if (!SecOrErr) { 1500 W.startLine() << "Invalid section number: " << Symbol.getSectionNumber() 1501 << "\n"; 1502 W.flush(); 1503 consumeError(SecOrErr.takeError()); 1504 return; 1505 } 1506 const coff_section *Section = *SecOrErr; 1507 1508 StringRef SymbolName; 1509 if (Expected<StringRef> SymNameOrErr = Obj->getSymbolName(Symbol)) 1510 SymbolName = *SymNameOrErr; 1511 1512 StringRef SectionName; 1513 if (Expected<StringRef> SecNameOrErr = 1514 getSectionName(Obj, Symbol.getSectionNumber(), Section)) 1515 SectionName = *SecNameOrErr; 1516 1517 W.printString("Name", SymbolName); 1518 W.printNumber("Value", Symbol.getValue()); 1519 W.printNumber("Section", SectionName, Symbol.getSectionNumber()); 1520 W.printEnum ("BaseType", Symbol.getBaseType(), makeArrayRef(ImageSymType)); 1521 W.printEnum ("ComplexType", Symbol.getComplexType(), 1522 makeArrayRef(ImageSymDType)); 1523 W.printEnum ("StorageClass", Symbol.getStorageClass(), 1524 makeArrayRef(ImageSymClass)); 1525 W.printNumber("AuxSymbolCount", Symbol.getNumberOfAuxSymbols()); 1526 1527 for (uint8_t I = 0; I < Symbol.getNumberOfAuxSymbols(); ++I) { 1528 if (Symbol.isFunctionDefinition()) { 1529 const coff_aux_function_definition *Aux; 1530 if (std::error_code EC = getSymbolAuxData(Obj, Symbol, I, Aux)) 1531 reportError(errorCodeToError(EC), Obj->getFileName()); 1532 1533 DictScope AS(W, "AuxFunctionDef"); 1534 W.printNumber("TagIndex", Aux->TagIndex); 1535 W.printNumber("TotalSize", Aux->TotalSize); 1536 W.printHex("PointerToLineNumber", Aux->PointerToLinenumber); 1537 W.printHex("PointerToNextFunction", Aux->PointerToNextFunction); 1538 1539 } else if (Symbol.isAnyUndefined()) { 1540 const coff_aux_weak_external *Aux; 1541 if (std::error_code EC = getSymbolAuxData(Obj, Symbol, I, Aux)) 1542 reportError(errorCodeToError(EC), Obj->getFileName()); 1543 1544 DictScope AS(W, "AuxWeakExternal"); 1545 W.printNumber("Linked", getSymbolName(Aux->TagIndex), Aux->TagIndex); 1546 W.printEnum ("Search", Aux->Characteristics, 1547 makeArrayRef(WeakExternalCharacteristics)); 1548 1549 } else if (Symbol.isFileRecord()) { 1550 const char *FileName; 1551 if (std::error_code EC = getSymbolAuxData(Obj, Symbol, I, FileName)) 1552 reportError(errorCodeToError(EC), Obj->getFileName()); 1553 DictScope AS(W, "AuxFileRecord"); 1554 1555 StringRef Name(FileName, Symbol.getNumberOfAuxSymbols() * 1556 Obj->getSymbolTableEntrySize()); 1557 W.printString("FileName", Name.rtrim(StringRef("\0", 1))); 1558 break; 1559 } else if (Symbol.isSectionDefinition()) { 1560 const coff_aux_section_definition *Aux; 1561 if (std::error_code EC = getSymbolAuxData(Obj, Symbol, I, Aux)) 1562 reportError(errorCodeToError(EC), Obj->getFileName()); 1563 1564 int32_t AuxNumber = Aux->getNumber(Symbol.isBigObj()); 1565 1566 DictScope AS(W, "AuxSectionDef"); 1567 W.printNumber("Length", Aux->Length); 1568 W.printNumber("RelocationCount", Aux->NumberOfRelocations); 1569 W.printNumber("LineNumberCount", Aux->NumberOfLinenumbers); 1570 W.printHex("Checksum", Aux->CheckSum); 1571 W.printNumber("Number", AuxNumber); 1572 W.printEnum("Selection", Aux->Selection, makeArrayRef(ImageCOMDATSelect)); 1573 1574 if (Section && Section->Characteristics & COFF::IMAGE_SCN_LNK_COMDAT 1575 && Aux->Selection == COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE) { 1576 Expected<const coff_section *> Assoc = Obj->getSection(AuxNumber); 1577 if (!Assoc) 1578 reportError(Assoc.takeError(), Obj->getFileName()); 1579 Expected<StringRef> AssocName = getSectionName(Obj, AuxNumber, *Assoc); 1580 if (!AssocName) 1581 reportError(AssocName.takeError(), Obj->getFileName()); 1582 1583 W.printNumber("AssocSection", *AssocName, AuxNumber); 1584 } 1585 } else if (Symbol.isCLRToken()) { 1586 const coff_aux_clr_token *Aux; 1587 if (std::error_code EC = getSymbolAuxData(Obj, Symbol, I, Aux)) 1588 reportError(errorCodeToError(EC), Obj->getFileName()); 1589 1590 DictScope AS(W, "AuxCLRToken"); 1591 W.printNumber("AuxType", Aux->AuxType); 1592 W.printNumber("Reserved", Aux->Reserved); 1593 W.printNumber("SymbolTableIndex", getSymbolName(Aux->SymbolTableIndex), 1594 Aux->SymbolTableIndex); 1595 1596 } else { 1597 W.startLine() << "<unhandled auxiliary record>\n"; 1598 } 1599 } 1600 } 1601 1602 void COFFDumper::printUnwindInfo() { 1603 ListScope D(W, "UnwindInformation"); 1604 switch (Obj->getMachine()) { 1605 case COFF::IMAGE_FILE_MACHINE_AMD64: { 1606 Win64EH::Dumper Dumper(W); 1607 Win64EH::Dumper::SymbolResolver 1608 Resolver = [](const object::coff_section *Section, uint64_t Offset, 1609 SymbolRef &Symbol, void *user_data) -> std::error_code { 1610 COFFDumper *Dumper = reinterpret_cast<COFFDumper *>(user_data); 1611 return Dumper->resolveSymbol(Section, Offset, Symbol); 1612 }; 1613 Win64EH::Dumper::Context Ctx(*Obj, Resolver, this); 1614 Dumper.printData(Ctx); 1615 break; 1616 } 1617 case COFF::IMAGE_FILE_MACHINE_ARM64: 1618 case COFF::IMAGE_FILE_MACHINE_ARMNT: { 1619 ARM::WinEH::Decoder Decoder(W, Obj->getMachine() == 1620 COFF::IMAGE_FILE_MACHINE_ARM64); 1621 // TODO Propagate the error. 1622 consumeError(Decoder.dumpProcedureData(*Obj)); 1623 break; 1624 } 1625 default: 1626 W.printEnum("unsupported Image Machine", Obj->getMachine(), 1627 makeArrayRef(ImageFileMachineType)); 1628 break; 1629 } 1630 } 1631 1632 void COFFDumper::printNeededLibraries() { 1633 ListScope D(W, "NeededLibraries"); 1634 1635 using LibsTy = std::vector<StringRef>; 1636 LibsTy Libs; 1637 1638 for (const ImportDirectoryEntryRef &DirRef : Obj->import_directories()) { 1639 StringRef Name; 1640 if (!DirRef.getName(Name)) 1641 Libs.push_back(Name); 1642 } 1643 1644 llvm::stable_sort(Libs); 1645 1646 for (const auto &L : Libs) { 1647 W.startLine() << L << "\n"; 1648 } 1649 } 1650 1651 void COFFDumper::printImportedSymbols( 1652 iterator_range<imported_symbol_iterator> Range) { 1653 for (const ImportedSymbolRef &I : Range) { 1654 StringRef Sym; 1655 if (Error E = I.getSymbolName(Sym)) 1656 reportError(std::move(E), Obj->getFileName()); 1657 uint16_t Ordinal; 1658 if (Error E = I.getOrdinal(Ordinal)) 1659 reportError(std::move(E), Obj->getFileName()); 1660 W.printNumber("Symbol", Sym, Ordinal); 1661 } 1662 } 1663 1664 void COFFDumper::printDelayImportedSymbols( 1665 const DelayImportDirectoryEntryRef &I, 1666 iterator_range<imported_symbol_iterator> Range) { 1667 int Index = 0; 1668 for (const ImportedSymbolRef &S : Range) { 1669 DictScope Import(W, "Import"); 1670 StringRef Sym; 1671 if (Error E = S.getSymbolName(Sym)) 1672 reportError(std::move(E), Obj->getFileName()); 1673 1674 uint16_t Ordinal; 1675 if (Error E = S.getOrdinal(Ordinal)) 1676 reportError(std::move(E), Obj->getFileName()); 1677 W.printNumber("Symbol", Sym, Ordinal); 1678 1679 uint64_t Addr; 1680 if (Error E = I.getImportAddress(Index++, Addr)) 1681 reportError(std::move(E), Obj->getFileName()); 1682 W.printHex("Address", Addr); 1683 } 1684 } 1685 1686 void COFFDumper::printCOFFImports() { 1687 // Regular imports 1688 for (const ImportDirectoryEntryRef &I : Obj->import_directories()) { 1689 DictScope Import(W, "Import"); 1690 StringRef Name; 1691 if (Error E = I.getName(Name)) 1692 reportError(std::move(E), Obj->getFileName()); 1693 W.printString("Name", Name); 1694 uint32_t ILTAddr; 1695 if (Error E = I.getImportLookupTableRVA(ILTAddr)) 1696 reportError(std::move(E), Obj->getFileName()); 1697 W.printHex("ImportLookupTableRVA", ILTAddr); 1698 uint32_t IATAddr; 1699 if (Error E = I.getImportAddressTableRVA(IATAddr)) 1700 reportError(std::move(E), Obj->getFileName()); 1701 W.printHex("ImportAddressTableRVA", IATAddr); 1702 // The import lookup table can be missing with certain older linkers, so 1703 // fall back to the import address table in that case. 1704 if (ILTAddr) 1705 printImportedSymbols(I.lookup_table_symbols()); 1706 else 1707 printImportedSymbols(I.imported_symbols()); 1708 } 1709 1710 // Delay imports 1711 for (const DelayImportDirectoryEntryRef &I : Obj->delay_import_directories()) { 1712 DictScope Import(W, "DelayImport"); 1713 StringRef Name; 1714 if (Error E = I.getName(Name)) 1715 reportError(std::move(E), Obj->getFileName()); 1716 W.printString("Name", Name); 1717 const delay_import_directory_table_entry *Table; 1718 if (Error E = I.getDelayImportTable(Table)) 1719 reportError(std::move(E), Obj->getFileName()); 1720 W.printHex("Attributes", Table->Attributes); 1721 W.printHex("ModuleHandle", Table->ModuleHandle); 1722 W.printHex("ImportAddressTable", Table->DelayImportAddressTable); 1723 W.printHex("ImportNameTable", Table->DelayImportNameTable); 1724 W.printHex("BoundDelayImportTable", Table->BoundDelayImportTable); 1725 W.printHex("UnloadDelayImportTable", Table->UnloadDelayImportTable); 1726 printDelayImportedSymbols(I, I.imported_symbols()); 1727 } 1728 } 1729 1730 void COFFDumper::printCOFFExports() { 1731 for (const ExportDirectoryEntryRef &Exp : Obj->export_directories()) { 1732 DictScope Export(W, "Export"); 1733 1734 StringRef Name; 1735 uint32_t Ordinal, RVA; 1736 1737 if (Error E = Exp.getSymbolName(Name)) 1738 reportError(std::move(E), Obj->getFileName()); 1739 if (Error E = Exp.getOrdinal(Ordinal)) 1740 reportError(std::move(E), Obj->getFileName()); 1741 if (Error E = Exp.getExportRVA(RVA)) 1742 reportError(std::move(E), Obj->getFileName()); 1743 1744 W.printNumber("Ordinal", Ordinal); 1745 W.printString("Name", Name); 1746 W.printHex("RVA", RVA); 1747 } 1748 } 1749 1750 void COFFDumper::printCOFFDirectives() { 1751 for (const SectionRef &Section : Obj->sections()) { 1752 StringRef Name = unwrapOrError(Obj->getFileName(), Section.getName()); 1753 if (Name != ".drectve") 1754 continue; 1755 1756 StringRef Contents = 1757 unwrapOrError(Obj->getFileName(), Section.getContents()); 1758 W.printString("Directive(s)", Contents); 1759 } 1760 } 1761 1762 static std::string getBaseRelocTypeName(uint8_t Type) { 1763 switch (Type) { 1764 case COFF::IMAGE_REL_BASED_ABSOLUTE: return "ABSOLUTE"; 1765 case COFF::IMAGE_REL_BASED_HIGH: return "HIGH"; 1766 case COFF::IMAGE_REL_BASED_LOW: return "LOW"; 1767 case COFF::IMAGE_REL_BASED_HIGHLOW: return "HIGHLOW"; 1768 case COFF::IMAGE_REL_BASED_HIGHADJ: return "HIGHADJ"; 1769 case COFF::IMAGE_REL_BASED_ARM_MOV32T: return "ARM_MOV32(T)"; 1770 case COFF::IMAGE_REL_BASED_DIR64: return "DIR64"; 1771 default: return "unknown (" + llvm::utostr(Type) + ")"; 1772 } 1773 } 1774 1775 void COFFDumper::printCOFFBaseReloc() { 1776 ListScope D(W, "BaseReloc"); 1777 for (const BaseRelocRef &I : Obj->base_relocs()) { 1778 uint8_t Type; 1779 uint32_t RVA; 1780 if (Error E = I.getRVA(RVA)) 1781 reportError(std::move(E), Obj->getFileName()); 1782 if (Error E = I.getType(Type)) 1783 reportError(std::move(E), Obj->getFileName()); 1784 DictScope Import(W, "Entry"); 1785 W.printString("Type", getBaseRelocTypeName(Type)); 1786 W.printHex("Address", RVA); 1787 } 1788 } 1789 1790 void COFFDumper::printCOFFResources() { 1791 ListScope ResourcesD(W, "Resources"); 1792 for (const SectionRef &S : Obj->sections()) { 1793 StringRef Name = unwrapOrError(Obj->getFileName(), S.getName()); 1794 if (!Name.startswith(".rsrc")) 1795 continue; 1796 1797 StringRef Ref = unwrapOrError(Obj->getFileName(), S.getContents()); 1798 1799 if ((Name == ".rsrc") || (Name == ".rsrc$01")) { 1800 ResourceSectionRef RSF; 1801 Error E = RSF.load(Obj, S); 1802 if (E) 1803 reportError(std::move(E), Obj->getFileName()); 1804 auto &BaseTable = unwrapOrError(Obj->getFileName(), RSF.getBaseTable()); 1805 W.printNumber("Total Number of Resources", 1806 countTotalTableEntries(RSF, BaseTable, "Type")); 1807 W.printHex("Base Table Address", 1808 Obj->getCOFFSection(S)->PointerToRawData); 1809 W.startLine() << "\n"; 1810 printResourceDirectoryTable(RSF, BaseTable, "Type"); 1811 } 1812 if (opts::SectionData) 1813 W.printBinaryBlock(Name.str() + " Data", Ref); 1814 } 1815 } 1816 1817 uint32_t 1818 COFFDumper::countTotalTableEntries(ResourceSectionRef RSF, 1819 const coff_resource_dir_table &Table, 1820 StringRef Level) { 1821 uint32_t TotalEntries = 0; 1822 for (int i = 0; i < Table.NumberOfNameEntries + Table.NumberOfIDEntries; 1823 i++) { 1824 auto Entry = unwrapOrError(Obj->getFileName(), RSF.getTableEntry(Table, i)); 1825 if (Entry.Offset.isSubDir()) { 1826 StringRef NextLevel; 1827 if (Level == "Name") 1828 NextLevel = "Language"; 1829 else 1830 NextLevel = "Name"; 1831 auto &NextTable = 1832 unwrapOrError(Obj->getFileName(), RSF.getEntrySubDir(Entry)); 1833 TotalEntries += countTotalTableEntries(RSF, NextTable, NextLevel); 1834 } else { 1835 TotalEntries += 1; 1836 } 1837 } 1838 return TotalEntries; 1839 } 1840 1841 void COFFDumper::printResourceDirectoryTable( 1842 ResourceSectionRef RSF, const coff_resource_dir_table &Table, 1843 StringRef Level) { 1844 1845 W.printNumber("Number of String Entries", Table.NumberOfNameEntries); 1846 W.printNumber("Number of ID Entries", Table.NumberOfIDEntries); 1847 1848 // Iterate through level in resource directory tree. 1849 for (int i = 0; i < Table.NumberOfNameEntries + Table.NumberOfIDEntries; 1850 i++) { 1851 auto Entry = unwrapOrError(Obj->getFileName(), RSF.getTableEntry(Table, i)); 1852 StringRef Name; 1853 SmallString<20> IDStr; 1854 raw_svector_ostream OS(IDStr); 1855 if (i < Table.NumberOfNameEntries) { 1856 ArrayRef<UTF16> RawEntryNameString = 1857 unwrapOrError(Obj->getFileName(), RSF.getEntryNameString(Entry)); 1858 std::vector<UTF16> EndianCorrectedNameString; 1859 if (llvm::sys::IsBigEndianHost) { 1860 EndianCorrectedNameString.resize(RawEntryNameString.size() + 1); 1861 std::copy(RawEntryNameString.begin(), RawEntryNameString.end(), 1862 EndianCorrectedNameString.begin() + 1); 1863 EndianCorrectedNameString[0] = UNI_UTF16_BYTE_ORDER_MARK_SWAPPED; 1864 RawEntryNameString = makeArrayRef(EndianCorrectedNameString); 1865 } 1866 std::string EntryNameString; 1867 if (!llvm::convertUTF16ToUTF8String(RawEntryNameString, EntryNameString)) 1868 reportError(errorCodeToError(object_error::parse_failed), 1869 Obj->getFileName()); 1870 OS << ": "; 1871 OS << EntryNameString; 1872 } else { 1873 if (Level == "Type") { 1874 OS << ": "; 1875 printResourceTypeName(Entry.Identifier.ID, OS); 1876 } else { 1877 OS << ": (ID " << Entry.Identifier.ID << ")"; 1878 } 1879 } 1880 Name = IDStr; 1881 ListScope ResourceType(W, Level.str() + Name.str()); 1882 if (Entry.Offset.isSubDir()) { 1883 W.printHex("Table Offset", Entry.Offset.value()); 1884 StringRef NextLevel; 1885 if (Level == "Name") 1886 NextLevel = "Language"; 1887 else 1888 NextLevel = "Name"; 1889 auto &NextTable = 1890 unwrapOrError(Obj->getFileName(), RSF.getEntrySubDir(Entry)); 1891 printResourceDirectoryTable(RSF, NextTable, NextLevel); 1892 } else { 1893 W.printHex("Entry Offset", Entry.Offset.value()); 1894 char FormattedTime[20] = {}; 1895 time_t TDS = time_t(Table.TimeDateStamp); 1896 strftime(FormattedTime, 20, "%Y-%m-%d %H:%M:%S", gmtime(&TDS)); 1897 W.printHex("Time/Date Stamp", FormattedTime, Table.TimeDateStamp); 1898 W.printNumber("Major Version", Table.MajorVersion); 1899 W.printNumber("Minor Version", Table.MinorVersion); 1900 W.printNumber("Characteristics", Table.Characteristics); 1901 ListScope DataScope(W, "Data"); 1902 auto &DataEntry = 1903 unwrapOrError(Obj->getFileName(), RSF.getEntryData(Entry)); 1904 W.printHex("DataRVA", DataEntry.DataRVA); 1905 W.printNumber("DataSize", DataEntry.DataSize); 1906 W.printNumber("Codepage", DataEntry.Codepage); 1907 W.printNumber("Reserved", DataEntry.Reserved); 1908 StringRef Contents = 1909 unwrapOrError(Obj->getFileName(), RSF.getContents(DataEntry)); 1910 W.printBinaryBlock("Data", Contents); 1911 } 1912 } 1913 } 1914 1915 void COFFDumper::printStackMap() const { 1916 SectionRef StackMapSection; 1917 for (auto Sec : Obj->sections()) { 1918 StringRef Name; 1919 if (Expected<StringRef> NameOrErr = Sec.getName()) 1920 Name = *NameOrErr; 1921 else 1922 consumeError(NameOrErr.takeError()); 1923 1924 if (Name == ".llvm_stackmaps") { 1925 StackMapSection = Sec; 1926 break; 1927 } 1928 } 1929 1930 if (StackMapSection == SectionRef()) 1931 return; 1932 1933 StringRef StackMapContents = 1934 unwrapOrError(Obj->getFileName(), StackMapSection.getContents()); 1935 ArrayRef<uint8_t> StackMapContentsArray = 1936 arrayRefFromStringRef(StackMapContents); 1937 1938 if (Obj->isLittleEndian()) 1939 prettyPrintStackMap( 1940 W, StackMapParser<support::little>(StackMapContentsArray)); 1941 else 1942 prettyPrintStackMap( 1943 W, StackMapParser<support::big>(StackMapContentsArray)); 1944 } 1945 1946 void COFFDumper::printAddrsig() { 1947 SectionRef AddrsigSection; 1948 for (auto Sec : Obj->sections()) { 1949 StringRef Name; 1950 if (Expected<StringRef> NameOrErr = Sec.getName()) 1951 Name = *NameOrErr; 1952 else 1953 consumeError(NameOrErr.takeError()); 1954 1955 if (Name == ".llvm_addrsig") { 1956 AddrsigSection = Sec; 1957 break; 1958 } 1959 } 1960 1961 if (AddrsigSection == SectionRef()) 1962 return; 1963 1964 StringRef AddrsigContents = 1965 unwrapOrError(Obj->getFileName(), AddrsigSection.getContents()); 1966 ArrayRef<uint8_t> AddrsigContentsArray(AddrsigContents.bytes_begin(), 1967 AddrsigContents.size()); 1968 1969 ListScope L(W, "Addrsig"); 1970 const uint8_t *Cur = AddrsigContents.bytes_begin(); 1971 const uint8_t *End = AddrsigContents.bytes_end(); 1972 while (Cur != End) { 1973 unsigned Size; 1974 const char *Err; 1975 uint64_t SymIndex = decodeULEB128(Cur, &Size, End, &Err); 1976 if (Err) 1977 reportError(createError(Err), Obj->getFileName()); 1978 1979 W.printNumber("Sym", getSymbolName(SymIndex), SymIndex); 1980 Cur += Size; 1981 } 1982 } 1983 1984 void COFFDumper::printCGProfile() { 1985 SectionRef CGProfileSection; 1986 for (SectionRef Sec : Obj->sections()) { 1987 StringRef Name = unwrapOrError(Obj->getFileName(), Sec.getName()); 1988 if (Name == ".llvm.call-graph-profile") { 1989 CGProfileSection = Sec; 1990 break; 1991 } 1992 } 1993 1994 if (CGProfileSection == SectionRef()) 1995 return; 1996 1997 StringRef CGProfileContents = 1998 unwrapOrError(Obj->getFileName(), CGProfileSection.getContents()); 1999 BinaryStreamReader Reader(CGProfileContents, llvm::support::little); 2000 2001 ListScope L(W, "CGProfile"); 2002 while (!Reader.empty()) { 2003 uint32_t FromIndex, ToIndex; 2004 uint64_t Count; 2005 if (Error Err = Reader.readInteger(FromIndex)) 2006 reportError(std::move(Err), Obj->getFileName()); 2007 if (Error Err = Reader.readInteger(ToIndex)) 2008 reportError(std::move(Err), Obj->getFileName()); 2009 if (Error Err = Reader.readInteger(Count)) 2010 reportError(std::move(Err), Obj->getFileName()); 2011 2012 DictScope D(W, "CGProfileEntry"); 2013 W.printNumber("From", getSymbolName(FromIndex), FromIndex); 2014 W.printNumber("To", getSymbolName(ToIndex), ToIndex); 2015 W.printNumber("Weight", Count); 2016 } 2017 } 2018 2019 StringRef COFFDumper::getSymbolName(uint32_t Index) { 2020 Expected<COFFSymbolRef> Sym = Obj->getSymbol(Index); 2021 if (!Sym) 2022 reportError(Sym.takeError(), Obj->getFileName()); 2023 2024 Expected<StringRef> SymName = Obj->getSymbolName(*Sym); 2025 if (!SymName) 2026 reportError(SymName.takeError(), Obj->getFileName()); 2027 2028 return *SymName; 2029 } 2030 2031 void llvm::dumpCodeViewMergedTypes(ScopedPrinter &Writer, 2032 ArrayRef<ArrayRef<uint8_t>> IpiRecords, 2033 ArrayRef<ArrayRef<uint8_t>> TpiRecords) { 2034 TypeTableCollection TpiTypes(TpiRecords); 2035 { 2036 ListScope S(Writer, "MergedTypeStream"); 2037 TypeDumpVisitor TDV(TpiTypes, &Writer, opts::CodeViewSubsectionBytes); 2038 if (Error Err = codeview::visitTypeStream(TpiTypes, TDV)) 2039 reportError(std::move(Err), "<?>"); 2040 Writer.flush(); 2041 } 2042 2043 // Flatten the id stream and print it next. The ID stream refers to names from 2044 // the type stream. 2045 TypeTableCollection IpiTypes(IpiRecords); 2046 { 2047 ListScope S(Writer, "MergedIDStream"); 2048 TypeDumpVisitor TDV(TpiTypes, &Writer, opts::CodeViewSubsectionBytes); 2049 TDV.setIpiTypes(IpiTypes); 2050 if (Error Err = codeview::visitTypeStream(IpiTypes, TDV)) 2051 reportError(std::move(Err), "<?>"); 2052 Writer.flush(); 2053 } 2054 } 2055 2056 void COFFDumper::printCOFFTLSDirectory() { 2057 if (Obj->is64()) 2058 printCOFFTLSDirectory(Obj->getTLSDirectory64()); 2059 else 2060 printCOFFTLSDirectory(Obj->getTLSDirectory32()); 2061 } 2062 2063 template <typename IntTy> 2064 void COFFDumper::printCOFFTLSDirectory( 2065 const coff_tls_directory<IntTy> *TlsTable) { 2066 DictScope D(W, "TLSDirectory"); 2067 if (!TlsTable) 2068 return; 2069 2070 W.printHex("StartAddressOfRawData", TlsTable->StartAddressOfRawData); 2071 W.printHex("EndAddressOfRawData", TlsTable->EndAddressOfRawData); 2072 W.printHex("AddressOfIndex", TlsTable->AddressOfIndex); 2073 W.printHex("AddressOfCallBacks", TlsTable->AddressOfCallBacks); 2074 W.printHex("SizeOfZeroFill", TlsTable->SizeOfZeroFill); 2075 W.printFlags("Characteristics", TlsTable->Characteristics, 2076 makeArrayRef(ImageSectionCharacteristics), 2077 COFF::SectionCharacteristics(COFF::IMAGE_SCN_ALIGN_MASK)); 2078 } 2079