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