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