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 const pe32_header *PEHeader = nullptr; 635 error(Obj->getPE32Header(PEHeader)); 636 if (PEHeader) 637 printPEHeader<pe32_header>(PEHeader); 638 639 const pe32plus_header *PEPlusHeader = nullptr; 640 error(Obj->getPE32PlusHeader(PEPlusHeader)); 641 if (PEPlusHeader) 642 printPEHeader<pe32plus_header>(PEPlusHeader); 643 644 if (const dos_header *DH = Obj->getDOSHeader()) 645 printDOSHeader(DH); 646 } 647 648 void COFFDumper::printDOSHeader(const dos_header *DH) { 649 DictScope D(W, "DOSHeader"); 650 W.printString("Magic", StringRef(DH->Magic, sizeof(DH->Magic))); 651 W.printNumber("UsedBytesInTheLastPage", DH->UsedBytesInTheLastPage); 652 W.printNumber("FileSizeInPages", DH->FileSizeInPages); 653 W.printNumber("NumberOfRelocationItems", DH->NumberOfRelocationItems); 654 W.printNumber("HeaderSizeInParagraphs", DH->HeaderSizeInParagraphs); 655 W.printNumber("MinimumExtraParagraphs", DH->MinimumExtraParagraphs); 656 W.printNumber("MaximumExtraParagraphs", DH->MaximumExtraParagraphs); 657 W.printNumber("InitialRelativeSS", DH->InitialRelativeSS); 658 W.printNumber("InitialSP", DH->InitialSP); 659 W.printNumber("Checksum", DH->Checksum); 660 W.printNumber("InitialIP", DH->InitialIP); 661 W.printNumber("InitialRelativeCS", DH->InitialRelativeCS); 662 W.printNumber("AddressOfRelocationTable", DH->AddressOfRelocationTable); 663 W.printNumber("OverlayNumber", DH->OverlayNumber); 664 W.printNumber("OEMid", DH->OEMid); 665 W.printNumber("OEMinfo", DH->OEMinfo); 666 W.printNumber("AddressOfNewExeHeader", DH->AddressOfNewExeHeader); 667 } 668 669 template <class PEHeader> 670 void COFFDumper::printPEHeader(const PEHeader *Hdr) { 671 DictScope D(W, "ImageOptionalHeader"); 672 W.printHex ("Magic", Hdr->Magic); 673 W.printNumber("MajorLinkerVersion", Hdr->MajorLinkerVersion); 674 W.printNumber("MinorLinkerVersion", Hdr->MinorLinkerVersion); 675 W.printNumber("SizeOfCode", Hdr->SizeOfCode); 676 W.printNumber("SizeOfInitializedData", Hdr->SizeOfInitializedData); 677 W.printNumber("SizeOfUninitializedData", Hdr->SizeOfUninitializedData); 678 W.printHex ("AddressOfEntryPoint", Hdr->AddressOfEntryPoint); 679 W.printHex ("BaseOfCode", Hdr->BaseOfCode); 680 printBaseOfDataField(Hdr); 681 W.printHex ("ImageBase", Hdr->ImageBase); 682 W.printNumber("SectionAlignment", Hdr->SectionAlignment); 683 W.printNumber("FileAlignment", Hdr->FileAlignment); 684 W.printNumber("MajorOperatingSystemVersion", 685 Hdr->MajorOperatingSystemVersion); 686 W.printNumber("MinorOperatingSystemVersion", 687 Hdr->MinorOperatingSystemVersion); 688 W.printNumber("MajorImageVersion", Hdr->MajorImageVersion); 689 W.printNumber("MinorImageVersion", Hdr->MinorImageVersion); 690 W.printNumber("MajorSubsystemVersion", Hdr->MajorSubsystemVersion); 691 W.printNumber("MinorSubsystemVersion", Hdr->MinorSubsystemVersion); 692 W.printNumber("SizeOfImage", Hdr->SizeOfImage); 693 W.printNumber("SizeOfHeaders", Hdr->SizeOfHeaders); 694 W.printEnum ("Subsystem", Hdr->Subsystem, makeArrayRef(PEWindowsSubsystem)); 695 W.printFlags ("Characteristics", Hdr->DLLCharacteristics, 696 makeArrayRef(PEDLLCharacteristics)); 697 W.printNumber("SizeOfStackReserve", Hdr->SizeOfStackReserve); 698 W.printNumber("SizeOfStackCommit", Hdr->SizeOfStackCommit); 699 W.printNumber("SizeOfHeapReserve", Hdr->SizeOfHeapReserve); 700 W.printNumber("SizeOfHeapCommit", Hdr->SizeOfHeapCommit); 701 W.printNumber("NumberOfRvaAndSize", Hdr->NumberOfRvaAndSize); 702 703 if (Hdr->NumberOfRvaAndSize > 0) { 704 DictScope D(W, "DataDirectory"); 705 static const char * const directory[] = { 706 "ExportTable", "ImportTable", "ResourceTable", "ExceptionTable", 707 "CertificateTable", "BaseRelocationTable", "Debug", "Architecture", 708 "GlobalPtr", "TLSTable", "LoadConfigTable", "BoundImport", "IAT", 709 "DelayImportDescriptor", "CLRRuntimeHeader", "Reserved" 710 }; 711 712 for (uint32_t i = 0; i < Hdr->NumberOfRvaAndSize; ++i) 713 printDataDirectory(i, directory[i]); 714 } 715 } 716 717 void COFFDumper::printCOFFDebugDirectory() { 718 ListScope LS(W, "DebugDirectory"); 719 for (const debug_directory &D : Obj->debug_directories()) { 720 char FormattedTime[20] = {}; 721 time_t TDS = D.TimeDateStamp; 722 strftime(FormattedTime, 20, "%Y-%m-%d %H:%M:%S", gmtime(&TDS)); 723 DictScope S(W, "DebugEntry"); 724 W.printHex("Characteristics", D.Characteristics); 725 W.printHex("TimeDateStamp", FormattedTime, D.TimeDateStamp); 726 W.printHex("MajorVersion", D.MajorVersion); 727 W.printHex("MinorVersion", D.MinorVersion); 728 W.printEnum("Type", D.Type, makeArrayRef(ImageDebugType)); 729 W.printHex("SizeOfData", D.SizeOfData); 730 W.printHex("AddressOfRawData", D.AddressOfRawData); 731 W.printHex("PointerToRawData", D.PointerToRawData); 732 if (D.Type == COFF::IMAGE_DEBUG_TYPE_CODEVIEW) { 733 const codeview::DebugInfo *DebugInfo; 734 StringRef PDBFileName; 735 error(Obj->getDebugPDBInfo(&D, DebugInfo, PDBFileName)); 736 DictScope PDBScope(W, "PDBInfo"); 737 W.printHex("PDBSignature", DebugInfo->Signature.CVSignature); 738 if (DebugInfo->Signature.CVSignature == OMF::Signature::PDB70) { 739 W.printBinary("PDBGUID", makeArrayRef(DebugInfo->PDB70.Signature)); 740 W.printNumber("PDBAge", DebugInfo->PDB70.Age); 741 W.printString("PDBFileName", PDBFileName); 742 } 743 } else if (D.SizeOfData != 0) { 744 // FIXME: Type values of 12 and 13 are commonly observed but are not in 745 // the documented type enum. Figure out what they mean. 746 ArrayRef<uint8_t> RawData; 747 error( 748 Obj->getRvaAndSizeAsBytes(D.AddressOfRawData, D.SizeOfData, RawData)); 749 W.printBinaryBlock("RawData", RawData); 750 } 751 } 752 } 753 754 void COFFDumper::printRVATable(uint64_t TableVA, uint64_t Count, 755 uint64_t EntrySize, PrintExtraCB PrintExtra) { 756 uintptr_t TableStart, TableEnd; 757 error(Obj->getVaPtr(TableVA, TableStart)); 758 error(Obj->getVaPtr(TableVA + Count * EntrySize - 1, TableEnd)); 759 TableEnd++; 760 for (uintptr_t I = TableStart; I < TableEnd; I += EntrySize) { 761 uint32_t RVA = *reinterpret_cast<const ulittle32_t *>(I); 762 raw_ostream &OS = W.startLine(); 763 OS << W.hex(Obj->getImageBase() + RVA); 764 if (PrintExtra) 765 PrintExtra(OS, reinterpret_cast<const uint8_t *>(I)); 766 OS << '\n'; 767 } 768 } 769 770 void COFFDumper::printCOFFLoadConfig() { 771 LoadConfigTables Tables; 772 if (Obj->is64()) 773 printCOFFLoadConfig(Obj->getLoadConfig64(), Tables); 774 else 775 printCOFFLoadConfig(Obj->getLoadConfig32(), Tables); 776 777 if (Tables.SEHTableVA) { 778 ListScope LS(W, "SEHTable"); 779 printRVATable(Tables.SEHTableVA, Tables.SEHTableCount, 4); 780 } 781 782 if (Tables.GuardFidTableVA) { 783 ListScope LS(W, "GuardFidTable"); 784 if (Tables.GuardFlags & uint32_t(coff_guard_flags::FidTableHasFlags)) { 785 auto PrintGuardFlags = [](raw_ostream &OS, const uint8_t *Entry) { 786 uint8_t Flags = *reinterpret_cast<const uint8_t *>(Entry + 4); 787 if (Flags) 788 OS << " flags " << utohexstr(Flags); 789 }; 790 printRVATable(Tables.GuardFidTableVA, Tables.GuardFidTableCount, 5, 791 PrintGuardFlags); 792 } else { 793 printRVATable(Tables.GuardFidTableVA, Tables.GuardFidTableCount, 4); 794 } 795 } 796 797 if (Tables.GuardLJmpTableVA) { 798 ListScope LS(W, "GuardLJmpTable"); 799 printRVATable(Tables.GuardLJmpTableVA, Tables.GuardLJmpTableCount, 4); 800 } 801 } 802 803 template <typename T> 804 void COFFDumper::printCOFFLoadConfig(const T *Conf, LoadConfigTables &Tables) { 805 if (!Conf) 806 return; 807 808 ListScope LS(W, "LoadConfig"); 809 char FormattedTime[20] = {}; 810 time_t TDS = Conf->TimeDateStamp; 811 strftime(FormattedTime, 20, "%Y-%m-%d %H:%M:%S", gmtime(&TDS)); 812 W.printHex("Size", Conf->Size); 813 814 // Print everything before SecurityCookie. The vast majority of images today 815 // have all these fields. 816 if (Conf->Size < offsetof(T, SEHandlerTable)) 817 return; 818 W.printHex("TimeDateStamp", FormattedTime, TDS); 819 W.printHex("MajorVersion", Conf->MajorVersion); 820 W.printHex("MinorVersion", Conf->MinorVersion); 821 W.printHex("GlobalFlagsClear", Conf->GlobalFlagsClear); 822 W.printHex("GlobalFlagsSet", Conf->GlobalFlagsSet); 823 W.printHex("CriticalSectionDefaultTimeout", 824 Conf->CriticalSectionDefaultTimeout); 825 W.printHex("DeCommitFreeBlockThreshold", Conf->DeCommitFreeBlockThreshold); 826 W.printHex("DeCommitTotalFreeThreshold", Conf->DeCommitTotalFreeThreshold); 827 W.printHex("LockPrefixTable", Conf->LockPrefixTable); 828 W.printHex("MaximumAllocationSize", Conf->MaximumAllocationSize); 829 W.printHex("VirtualMemoryThreshold", Conf->VirtualMemoryThreshold); 830 W.printHex("ProcessHeapFlags", Conf->ProcessHeapFlags); 831 W.printHex("ProcessAffinityMask", Conf->ProcessAffinityMask); 832 W.printHex("CSDVersion", Conf->CSDVersion); 833 W.printHex("DependentLoadFlags", Conf->DependentLoadFlags); 834 W.printHex("EditList", Conf->EditList); 835 W.printHex("SecurityCookie", Conf->SecurityCookie); 836 837 // Print the safe SEH table if present. 838 if (Conf->Size < offsetof(coff_load_configuration32, GuardCFCheckFunction)) 839 return; 840 W.printHex("SEHandlerTable", Conf->SEHandlerTable); 841 W.printNumber("SEHandlerCount", Conf->SEHandlerCount); 842 843 Tables.SEHTableVA = Conf->SEHandlerTable; 844 Tables.SEHTableCount = Conf->SEHandlerCount; 845 846 // Print everything before CodeIntegrity. (2015) 847 if (Conf->Size < offsetof(T, CodeIntegrity)) 848 return; 849 W.printHex("GuardCFCheckFunction", Conf->GuardCFCheckFunction); 850 W.printHex("GuardCFCheckDispatch", Conf->GuardCFCheckDispatch); 851 W.printHex("GuardCFFunctionTable", Conf->GuardCFFunctionTable); 852 W.printNumber("GuardCFFunctionCount", Conf->GuardCFFunctionCount); 853 W.printHex("GuardFlags", Conf->GuardFlags); 854 855 Tables.GuardFidTableVA = Conf->GuardCFFunctionTable; 856 Tables.GuardFidTableCount = Conf->GuardCFFunctionCount; 857 Tables.GuardFlags = Conf->GuardFlags; 858 859 // Print the rest. (2017) 860 if (Conf->Size < sizeof(T)) 861 return; 862 W.printHex("GuardAddressTakenIatEntryTable", 863 Conf->GuardAddressTakenIatEntryTable); 864 W.printNumber("GuardAddressTakenIatEntryCount", 865 Conf->GuardAddressTakenIatEntryCount); 866 W.printHex("GuardLongJumpTargetTable", Conf->GuardLongJumpTargetTable); 867 W.printNumber("GuardLongJumpTargetCount", Conf->GuardLongJumpTargetCount); 868 W.printHex("DynamicValueRelocTable", Conf->DynamicValueRelocTable); 869 W.printHex("CHPEMetadataPointer", Conf->CHPEMetadataPointer); 870 W.printHex("GuardRFFailureRoutine", Conf->GuardRFFailureRoutine); 871 W.printHex("GuardRFFailureRoutineFunctionPointer", 872 Conf->GuardRFFailureRoutineFunctionPointer); 873 W.printHex("DynamicValueRelocTableOffset", 874 Conf->DynamicValueRelocTableOffset); 875 W.printNumber("DynamicValueRelocTableSection", 876 Conf->DynamicValueRelocTableSection); 877 W.printHex("GuardRFVerifyStackPointerFunctionPointer", 878 Conf->GuardRFVerifyStackPointerFunctionPointer); 879 W.printHex("HotPatchTableOffset", Conf->HotPatchTableOffset); 880 881 Tables.GuardLJmpTableVA = Conf->GuardLongJumpTargetTable; 882 Tables.GuardLJmpTableCount = Conf->GuardLongJumpTargetCount; 883 } 884 885 void COFFDumper::printBaseOfDataField(const pe32_header *Hdr) { 886 W.printHex("BaseOfData", Hdr->BaseOfData); 887 } 888 889 void COFFDumper::printBaseOfDataField(const pe32plus_header *) {} 890 891 void COFFDumper::printCodeViewDebugInfo() { 892 // Print types first to build CVUDTNames, then print symbols. 893 for (const SectionRef &S : Obj->sections()) { 894 StringRef SectionName; 895 error(S.getName(SectionName)); 896 // .debug$T is a standard CodeView type section, while .debug$P is the same 897 // format but used for MSVC precompiled header object files. 898 if (SectionName == ".debug$T" || SectionName == ".debug$P") 899 printCodeViewTypeSection(SectionName, S); 900 } 901 for (const SectionRef &S : Obj->sections()) { 902 StringRef SectionName; 903 error(S.getName(SectionName)); 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 return error(object_error::parse_failed); 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 error(object_error::parse_failed); 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 error(object_error::parse_failed); 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 error(object_error::parse_failed); 1030 return; 1031 } 1032 1033 StringRef LinkageName; 1034 error(resolveSymbolName(Obj->getCOFFSection(Section), SectionOffset, 1035 LinkageName)); 1036 W.printString("LinkageName", LinkageName); 1037 if (FunctionLineTables.count(LinkageName) != 0) { 1038 // Saw debug info for this function already? 1039 error(object_error::parse_failed); 1040 return; 1041 } 1042 1043 FunctionLineTables[LinkageName] = Contents; 1044 FunctionNames.push_back(LinkageName); 1045 break; 1046 } 1047 case DebugSubsectionKind::FrameData: { 1048 // First four bytes is a relocation against the function. 1049 BinaryStreamReader SR(Contents, llvm::support::little); 1050 1051 DebugFrameDataSubsectionRef FrameData; 1052 if (Error E = FrameData.initialize(SR)) 1053 reportError(std::move(E), Obj->getFileName()); 1054 1055 StringRef LinkageName; 1056 error(resolveSymbolName(Obj->getCOFFSection(Section), SectionContents, 1057 FrameData.getRelocPtr(), LinkageName)); 1058 W.printString("LinkageName", LinkageName); 1059 1060 // To find the active frame description, search this array for the 1061 // smallest PC range that includes the current PC. 1062 for (const auto &FD : FrameData) { 1063 StringRef FrameFunc = unwrapOrError( 1064 Obj->getFileName(), CVStringTable.getString(FD.FrameFunc)); 1065 1066 DictScope S(W, "FrameData"); 1067 W.printHex("RvaStart", FD.RvaStart); 1068 W.printHex("CodeSize", FD.CodeSize); 1069 W.printHex("LocalSize", FD.LocalSize); 1070 W.printHex("ParamsSize", FD.ParamsSize); 1071 W.printHex("MaxStackSize", FD.MaxStackSize); 1072 W.printHex("PrologSize", FD.PrologSize); 1073 W.printHex("SavedRegsSize", FD.SavedRegsSize); 1074 W.printFlags("Flags", FD.Flags, makeArrayRef(FrameDataFlags)); 1075 1076 // The FrameFunc string is a small RPN program. It can be broken up into 1077 // statements that end in the '=' operator, which assigns the value on 1078 // the top of the stack to the previously pushed variable. Variables can 1079 // be temporary values ($T0) or physical registers ($esp). Print each 1080 // assignment on its own line to make these programs easier to read. 1081 { 1082 ListScope FFS(W, "FrameFunc"); 1083 while (!FrameFunc.empty()) { 1084 size_t EqOrEnd = FrameFunc.find('='); 1085 if (EqOrEnd == StringRef::npos) 1086 EqOrEnd = FrameFunc.size(); 1087 else 1088 ++EqOrEnd; 1089 StringRef Stmt = FrameFunc.substr(0, EqOrEnd); 1090 W.printString(Stmt); 1091 FrameFunc = FrameFunc.drop_front(EqOrEnd).trim(); 1092 } 1093 } 1094 } 1095 break; 1096 } 1097 1098 // Do nothing for unrecognized subsections. 1099 default: 1100 break; 1101 } 1102 W.flush(); 1103 } 1104 1105 // Dump the line tables now that we've read all the subsections and know all 1106 // the required information. 1107 for (unsigned I = 0, E = FunctionNames.size(); I != E; ++I) { 1108 StringRef Name = FunctionNames[I]; 1109 ListScope S(W, "FunctionLineTable"); 1110 W.printString("LinkageName", Name); 1111 1112 BinaryStreamReader Reader(FunctionLineTables[Name], support::little); 1113 1114 DebugLinesSubsectionRef LineInfo; 1115 if (Error E = LineInfo.initialize(Reader)) 1116 reportError(std::move(E), Obj->getFileName()); 1117 1118 W.printHex("Flags", LineInfo.header()->Flags); 1119 W.printHex("CodeSize", LineInfo.header()->CodeSize); 1120 for (const auto &Entry : LineInfo) { 1121 1122 ListScope S(W, "FilenameSegment"); 1123 printFileNameForOffset("Filename", Entry.NameIndex); 1124 uint32_t ColumnIndex = 0; 1125 for (const auto &Line : Entry.LineNumbers) { 1126 if (Line.Offset >= LineInfo.header()->CodeSize) { 1127 error(object_error::parse_failed); 1128 return; 1129 } 1130 1131 std::string PC = formatv("+{0:X}", uint32_t(Line.Offset)); 1132 ListScope PCScope(W, PC); 1133 codeview::LineInfo LI(Line.Flags); 1134 1135 if (LI.isAlwaysStepInto()) 1136 W.printString("StepInto", StringRef("Always")); 1137 else if (LI.isNeverStepInto()) 1138 W.printString("StepInto", StringRef("Never")); 1139 else 1140 W.printNumber("LineNumberStart", LI.getStartLine()); 1141 W.printNumber("LineNumberEndDelta", LI.getLineDelta()); 1142 W.printBoolean("IsStatement", LI.isStatement()); 1143 if (LineInfo.hasColumnInfo()) { 1144 W.printNumber("ColStart", Entry.Columns[ColumnIndex].StartColumn); 1145 W.printNumber("ColEnd", Entry.Columns[ColumnIndex].EndColumn); 1146 ++ColumnIndex; 1147 } 1148 } 1149 } 1150 } 1151 } 1152 1153 void COFFDumper::printCodeViewSymbolsSubsection(StringRef Subsection, 1154 const SectionRef &Section, 1155 StringRef SectionContents) { 1156 ArrayRef<uint8_t> BinaryData(Subsection.bytes_begin(), 1157 Subsection.bytes_end()); 1158 auto CODD = llvm::make_unique<COFFObjectDumpDelegate>(*this, Section, Obj, 1159 SectionContents); 1160 CVSymbolDumper CVSD(W, Types, CodeViewContainer::ObjectFile, std::move(CODD), 1161 CompilationCPUType, opts::CodeViewSubsectionBytes); 1162 CVSymbolArray Symbols; 1163 BinaryStreamReader Reader(BinaryData, llvm::support::little); 1164 if (auto EC = Reader.readArray(Symbols, Reader.getLength())) { 1165 consumeError(std::move(EC)); 1166 W.flush(); 1167 error(object_error::parse_failed); 1168 } 1169 1170 if (Error E = CVSD.dump(Symbols)) { 1171 W.flush(); 1172 reportError(std::move(E), Obj->getFileName()); 1173 } 1174 CompilationCPUType = CVSD.getCompilationCPUType(); 1175 W.flush(); 1176 } 1177 1178 void COFFDumper::printCodeViewFileChecksums(StringRef Subsection) { 1179 BinaryStreamRef Stream(Subsection, llvm::support::little); 1180 DebugChecksumsSubsectionRef Checksums; 1181 if (Error E = Checksums.initialize(Stream)) 1182 reportError(std::move(E), Obj->getFileName()); 1183 1184 for (auto &FC : Checksums) { 1185 DictScope S(W, "FileChecksum"); 1186 1187 StringRef Filename = unwrapOrError( 1188 Obj->getFileName(), CVStringTable.getString(FC.FileNameOffset)); 1189 W.printHex("Filename", Filename, FC.FileNameOffset); 1190 W.printHex("ChecksumSize", FC.Checksum.size()); 1191 W.printEnum("ChecksumKind", uint8_t(FC.Kind), 1192 makeArrayRef(FileChecksumKindNames)); 1193 1194 W.printBinary("ChecksumBytes", FC.Checksum); 1195 } 1196 } 1197 1198 void COFFDumper::printCodeViewInlineeLines(StringRef Subsection) { 1199 BinaryStreamReader SR(Subsection, llvm::support::little); 1200 DebugInlineeLinesSubsectionRef Lines; 1201 if (Error E = Lines.initialize(SR)) 1202 reportError(std::move(E), Obj->getFileName()); 1203 1204 for (auto &Line : Lines) { 1205 DictScope S(W, "InlineeSourceLine"); 1206 printTypeIndex("Inlinee", Line.Header->Inlinee); 1207 printFileNameForOffset("FileID", Line.Header->FileID); 1208 W.printNumber("SourceLineNum", Line.Header->SourceLineNum); 1209 1210 if (Lines.hasExtraFiles()) { 1211 W.printNumber("ExtraFileCount", Line.ExtraFiles.size()); 1212 ListScope ExtraFiles(W, "ExtraFiles"); 1213 for (const auto &FID : Line.ExtraFiles) { 1214 printFileNameForOffset("FileID", FID); 1215 } 1216 } 1217 } 1218 } 1219 1220 StringRef COFFDumper::getFileNameForFileOffset(uint32_t FileOffset) { 1221 // The file checksum subsection should precede all references to it. 1222 if (!CVFileChecksumTable.valid() || !CVStringTable.valid()) 1223 error(object_error::parse_failed); 1224 1225 auto Iter = CVFileChecksumTable.getArray().at(FileOffset); 1226 1227 // Check if the file checksum table offset is valid. 1228 if (Iter == CVFileChecksumTable.end()) 1229 error(object_error::parse_failed); 1230 1231 return unwrapOrError(Obj->getFileName(), 1232 CVStringTable.getString(Iter->FileNameOffset)); 1233 } 1234 1235 void COFFDumper::printFileNameForOffset(StringRef Label, uint32_t FileOffset) { 1236 W.printHex(Label, getFileNameForFileOffset(FileOffset), FileOffset); 1237 } 1238 1239 void COFFDumper::mergeCodeViewTypes(MergingTypeTableBuilder &CVIDs, 1240 MergingTypeTableBuilder &CVTypes, 1241 GlobalTypeTableBuilder &GlobalCVIDs, 1242 GlobalTypeTableBuilder &GlobalCVTypes, 1243 bool GHash) { 1244 for (const SectionRef &S : Obj->sections()) { 1245 StringRef SectionName; 1246 error(S.getName(SectionName)); 1247 if (SectionName == ".debug$T") { 1248 StringRef Data = unwrapOrError(Obj->getFileName(), S.getContents()); 1249 uint32_t Magic; 1250 if (Error E = consume(Data, Magic)) 1251 reportError(std::move(E), Obj->getFileName()); 1252 1253 if (Magic != 4) 1254 error(object_error::parse_failed); 1255 1256 CVTypeArray Types; 1257 BinaryStreamReader Reader(Data, llvm::support::little); 1258 if (auto EC = Reader.readArray(Types, Reader.getLength())) { 1259 consumeError(std::move(EC)); 1260 W.flush(); 1261 error(object_error::parse_failed); 1262 } 1263 SmallVector<TypeIndex, 128> SourceToDest; 1264 Optional<uint32_t> PCHSignature; 1265 if (GHash) { 1266 std::vector<GloballyHashedType> Hashes = 1267 GloballyHashedType::hashTypes(Types); 1268 if (Error E = 1269 mergeTypeAndIdRecords(GlobalCVIDs, GlobalCVTypes, SourceToDest, 1270 Types, Hashes, PCHSignature)) 1271 return reportError(std::move(E), Obj->getFileName()); 1272 } else { 1273 if (Error E = mergeTypeAndIdRecords(CVIDs, CVTypes, SourceToDest, Types, 1274 PCHSignature)) 1275 return reportError(std::move(E), Obj->getFileName()); 1276 } 1277 } 1278 } 1279 } 1280 1281 void COFFDumper::printCodeViewTypeSection(StringRef SectionName, 1282 const SectionRef &Section) { 1283 ListScope D(W, "CodeViewTypes"); 1284 W.printNumber("Section", SectionName, Obj->getSectionID(Section)); 1285 1286 StringRef Data = unwrapOrError(Obj->getFileName(), Section.getContents()); 1287 if (opts::CodeViewSubsectionBytes) 1288 W.printBinaryBlock("Data", Data); 1289 1290 uint32_t Magic; 1291 if (Error E = consume(Data, Magic)) 1292 reportError(std::move(E), Obj->getFileName()); 1293 1294 W.printHex("Magic", Magic); 1295 if (Magic != COFF::DEBUG_SECTION_MAGIC) 1296 return error(object_error::parse_failed); 1297 1298 Types.reset(Data, 100); 1299 1300 TypeDumpVisitor TDV(Types, &W, opts::CodeViewSubsectionBytes); 1301 if (Error E = codeview::visitTypeStream(Types, TDV)) 1302 reportError(std::move(E), Obj->getFileName()); 1303 1304 W.flush(); 1305 } 1306 1307 void COFFDumper::printSectionHeaders() { 1308 ListScope SectionsD(W, "Sections"); 1309 int SectionNumber = 0; 1310 for (const SectionRef &Sec : Obj->sections()) { 1311 ++SectionNumber; 1312 const coff_section *Section = Obj->getCOFFSection(Sec); 1313 1314 StringRef Name; 1315 error(Sec.getName(Name)); 1316 1317 DictScope D(W, "Section"); 1318 W.printNumber("Number", SectionNumber); 1319 W.printBinary("Name", Name, Section->Name); 1320 W.printHex ("VirtualSize", Section->VirtualSize); 1321 W.printHex ("VirtualAddress", Section->VirtualAddress); 1322 W.printNumber("RawDataSize", Section->SizeOfRawData); 1323 W.printHex ("PointerToRawData", Section->PointerToRawData); 1324 W.printHex ("PointerToRelocations", Section->PointerToRelocations); 1325 W.printHex ("PointerToLineNumbers", Section->PointerToLinenumbers); 1326 W.printNumber("RelocationCount", Section->NumberOfRelocations); 1327 W.printNumber("LineNumberCount", Section->NumberOfLinenumbers); 1328 W.printFlags ("Characteristics", Section->Characteristics, 1329 makeArrayRef(ImageSectionCharacteristics), 1330 COFF::SectionCharacteristics(0x00F00000)); 1331 1332 if (opts::SectionRelocations) { 1333 ListScope D(W, "Relocations"); 1334 for (const RelocationRef &Reloc : Sec.relocations()) 1335 printRelocation(Sec, Reloc); 1336 } 1337 1338 if (opts::SectionSymbols) { 1339 ListScope D(W, "Symbols"); 1340 for (const SymbolRef &Symbol : Obj->symbols()) { 1341 if (!Sec.containsSymbol(Symbol)) 1342 continue; 1343 1344 printSymbol(Symbol); 1345 } 1346 } 1347 1348 if (opts::SectionData && 1349 !(Section->Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA)) { 1350 StringRef Data = unwrapOrError(Obj->getFileName(), Sec.getContents()); 1351 W.printBinaryBlock("SectionData", Data); 1352 } 1353 } 1354 } 1355 1356 void COFFDumper::printRelocations() { 1357 ListScope D(W, "Relocations"); 1358 1359 int SectionNumber = 0; 1360 for (const SectionRef &Section : Obj->sections()) { 1361 ++SectionNumber; 1362 StringRef Name; 1363 error(Section.getName(Name)); 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 error(errorToErrorCode(SymbolNameOrErr.takeError())); 1395 SymbolName = *SymbolNameOrErr; 1396 SymbolIndex = Obj->getSymbolIndex(Obj->getCOFFSymbol(*Symbol)); 1397 } 1398 1399 if (opts::ExpandRelocs) { 1400 DictScope Group(W, "Relocation"); 1401 W.printHex("Offset", Offset); 1402 W.printNumber("Type", RelocName, RelocType); 1403 W.printString("Symbol", SymbolName.empty() ? "-" : SymbolName); 1404 W.printNumber("SymbolIndex", SymbolIndex); 1405 } else { 1406 raw_ostream& OS = W.startLine(); 1407 OS << W.hex(Offset) 1408 << " " << RelocName 1409 << " " << (SymbolName.empty() ? "-" : SymbolName) 1410 << " (" << SymbolIndex << ")" 1411 << "\n"; 1412 } 1413 } 1414 1415 void COFFDumper::printSymbols() { 1416 ListScope Group(W, "Symbols"); 1417 1418 for (const SymbolRef &Symbol : Obj->symbols()) 1419 printSymbol(Symbol); 1420 } 1421 1422 void COFFDumper::printDynamicSymbols() { ListScope Group(W, "DynamicSymbols"); } 1423 1424 static Expected<StringRef> 1425 getSectionName(const llvm::object::COFFObjectFile *Obj, int32_t SectionNumber, 1426 const coff_section *Section) { 1427 if (Section) 1428 return Obj->getSectionName(Section); 1429 if (SectionNumber == llvm::COFF::IMAGE_SYM_DEBUG) 1430 return StringRef("IMAGE_SYM_DEBUG"); 1431 if (SectionNumber == llvm::COFF::IMAGE_SYM_ABSOLUTE) 1432 return StringRef("IMAGE_SYM_ABSOLUTE"); 1433 if (SectionNumber == llvm::COFF::IMAGE_SYM_UNDEFINED) 1434 return StringRef("IMAGE_SYM_UNDEFINED"); 1435 return StringRef(""); 1436 } 1437 1438 void COFFDumper::printSymbol(const SymbolRef &Sym) { 1439 DictScope D(W, "Symbol"); 1440 1441 COFFSymbolRef Symbol = Obj->getCOFFSymbol(Sym); 1442 const coff_section *Section; 1443 if (std::error_code EC = Obj->getSection(Symbol.getSectionNumber(), Section)) { 1444 W.startLine() << "Invalid section number: " << EC.message() << "\n"; 1445 W.flush(); 1446 return; 1447 } 1448 1449 StringRef SymbolName; 1450 if (Obj->getSymbolName(Symbol, SymbolName)) 1451 SymbolName = ""; 1452 1453 StringRef SectionName; 1454 if (Expected<StringRef> NameOrErr = 1455 getSectionName(Obj, Symbol.getSectionNumber(), Section)) 1456 SectionName = *NameOrErr; 1457 1458 W.printString("Name", SymbolName); 1459 W.printNumber("Value", Symbol.getValue()); 1460 W.printNumber("Section", SectionName, Symbol.getSectionNumber()); 1461 W.printEnum ("BaseType", Symbol.getBaseType(), makeArrayRef(ImageSymType)); 1462 W.printEnum ("ComplexType", Symbol.getComplexType(), 1463 makeArrayRef(ImageSymDType)); 1464 W.printEnum ("StorageClass", Symbol.getStorageClass(), 1465 makeArrayRef(ImageSymClass)); 1466 W.printNumber("AuxSymbolCount", Symbol.getNumberOfAuxSymbols()); 1467 1468 for (uint8_t I = 0; I < Symbol.getNumberOfAuxSymbols(); ++I) { 1469 if (Symbol.isFunctionDefinition()) { 1470 const coff_aux_function_definition *Aux; 1471 error(getSymbolAuxData(Obj, Symbol, I, Aux)); 1472 1473 DictScope AS(W, "AuxFunctionDef"); 1474 W.printNumber("TagIndex", Aux->TagIndex); 1475 W.printNumber("TotalSize", Aux->TotalSize); 1476 W.printHex("PointerToLineNumber", Aux->PointerToLinenumber); 1477 W.printHex("PointerToNextFunction", Aux->PointerToNextFunction); 1478 1479 } else if (Symbol.isAnyUndefined()) { 1480 const coff_aux_weak_external *Aux; 1481 error(getSymbolAuxData(Obj, Symbol, I, Aux)); 1482 1483 Expected<COFFSymbolRef> Linked = Obj->getSymbol(Aux->TagIndex); 1484 StringRef LinkedName; 1485 std::error_code EC = errorToErrorCode(Linked.takeError()); 1486 if (EC || (EC = Obj->getSymbolName(*Linked, LinkedName))) { 1487 LinkedName = ""; 1488 error(EC); 1489 } 1490 1491 DictScope AS(W, "AuxWeakExternal"); 1492 W.printNumber("Linked", LinkedName, Aux->TagIndex); 1493 W.printEnum ("Search", Aux->Characteristics, 1494 makeArrayRef(WeakExternalCharacteristics)); 1495 1496 } else if (Symbol.isFileRecord()) { 1497 const char *FileName; 1498 error(getSymbolAuxData(Obj, Symbol, I, FileName)); 1499 1500 DictScope AS(W, "AuxFileRecord"); 1501 1502 StringRef Name(FileName, Symbol.getNumberOfAuxSymbols() * 1503 Obj->getSymbolTableEntrySize()); 1504 W.printString("FileName", Name.rtrim(StringRef("\0", 1))); 1505 break; 1506 } else if (Symbol.isSectionDefinition()) { 1507 const coff_aux_section_definition *Aux; 1508 error(getSymbolAuxData(Obj, Symbol, I, Aux)); 1509 1510 int32_t AuxNumber = Aux->getNumber(Symbol.isBigObj()); 1511 1512 DictScope AS(W, "AuxSectionDef"); 1513 W.printNumber("Length", Aux->Length); 1514 W.printNumber("RelocationCount", Aux->NumberOfRelocations); 1515 W.printNumber("LineNumberCount", Aux->NumberOfLinenumbers); 1516 W.printHex("Checksum", Aux->CheckSum); 1517 W.printNumber("Number", AuxNumber); 1518 W.printEnum("Selection", Aux->Selection, makeArrayRef(ImageCOMDATSelect)); 1519 1520 if (Section && Section->Characteristics & COFF::IMAGE_SCN_LNK_COMDAT 1521 && Aux->Selection == COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE) { 1522 const coff_section *Assoc; 1523 StringRef AssocName = ""; 1524 if (std::error_code EC = Obj->getSection(AuxNumber, Assoc)) 1525 error(EC); 1526 Expected<StringRef> Res = getSectionName(Obj, AuxNumber, Assoc); 1527 if (!Res) 1528 reportError(Res.takeError(), Obj->getFileName()); 1529 AssocName = *Res; 1530 1531 W.printNumber("AssocSection", AssocName, AuxNumber); 1532 } 1533 } else if (Symbol.isCLRToken()) { 1534 const coff_aux_clr_token *Aux; 1535 error(getSymbolAuxData(Obj, Symbol, I, Aux)); 1536 1537 Expected<COFFSymbolRef> ReferredSym = 1538 Obj->getSymbol(Aux->SymbolTableIndex); 1539 StringRef ReferredName; 1540 std::error_code EC = errorToErrorCode(ReferredSym.takeError()); 1541 if (EC || (EC = Obj->getSymbolName(*ReferredSym, ReferredName))) { 1542 ReferredName = ""; 1543 error(EC); 1544 } 1545 1546 DictScope AS(W, "AuxCLRToken"); 1547 W.printNumber("AuxType", Aux->AuxType); 1548 W.printNumber("Reserved", Aux->Reserved); 1549 W.printNumber("SymbolTableIndex", ReferredName, Aux->SymbolTableIndex); 1550 1551 } else { 1552 W.startLine() << "<unhandled auxiliary record>\n"; 1553 } 1554 } 1555 } 1556 1557 void COFFDumper::printUnwindInfo() { 1558 ListScope D(W, "UnwindInformation"); 1559 switch (Obj->getMachine()) { 1560 case COFF::IMAGE_FILE_MACHINE_AMD64: { 1561 Win64EH::Dumper Dumper(W); 1562 Win64EH::Dumper::SymbolResolver 1563 Resolver = [](const object::coff_section *Section, uint64_t Offset, 1564 SymbolRef &Symbol, void *user_data) -> std::error_code { 1565 COFFDumper *Dumper = reinterpret_cast<COFFDumper *>(user_data); 1566 return Dumper->resolveSymbol(Section, Offset, Symbol); 1567 }; 1568 Win64EH::Dumper::Context Ctx(*Obj, Resolver, this); 1569 Dumper.printData(Ctx); 1570 break; 1571 } 1572 case COFF::IMAGE_FILE_MACHINE_ARM64: 1573 case COFF::IMAGE_FILE_MACHINE_ARMNT: { 1574 ARM::WinEH::Decoder Decoder(W, Obj->getMachine() == 1575 COFF::IMAGE_FILE_MACHINE_ARM64); 1576 // TODO Propagate the error. 1577 consumeError(Decoder.dumpProcedureData(*Obj)); 1578 break; 1579 } 1580 default: 1581 W.printEnum("unsupported Image Machine", Obj->getMachine(), 1582 makeArrayRef(ImageFileMachineType)); 1583 break; 1584 } 1585 } 1586 1587 void COFFDumper::printNeededLibraries() { 1588 ListScope D(W, "NeededLibraries"); 1589 1590 using LibsTy = std::vector<StringRef>; 1591 LibsTy Libs; 1592 1593 for (const ImportDirectoryEntryRef &DirRef : Obj->import_directories()) { 1594 StringRef Name; 1595 if (!DirRef.getName(Name)) 1596 Libs.push_back(Name); 1597 } 1598 1599 llvm::stable_sort(Libs); 1600 1601 for (const auto &L : Libs) { 1602 W.startLine() << L << "\n"; 1603 } 1604 } 1605 1606 void COFFDumper::printImportedSymbols( 1607 iterator_range<imported_symbol_iterator> Range) { 1608 for (const ImportedSymbolRef &I : Range) { 1609 StringRef Sym; 1610 error(I.getSymbolName(Sym)); 1611 uint16_t Ordinal; 1612 error(I.getOrdinal(Ordinal)); 1613 W.printNumber("Symbol", Sym, Ordinal); 1614 } 1615 } 1616 1617 void COFFDumper::printDelayImportedSymbols( 1618 const DelayImportDirectoryEntryRef &I, 1619 iterator_range<imported_symbol_iterator> Range) { 1620 int Index = 0; 1621 for (const ImportedSymbolRef &S : Range) { 1622 DictScope Import(W, "Import"); 1623 StringRef Sym; 1624 error(S.getSymbolName(Sym)); 1625 uint16_t Ordinal; 1626 error(S.getOrdinal(Ordinal)); 1627 W.printNumber("Symbol", Sym, Ordinal); 1628 uint64_t Addr; 1629 error(I.getImportAddress(Index++, Addr)); 1630 W.printHex("Address", Addr); 1631 } 1632 } 1633 1634 void COFFDumper::printCOFFImports() { 1635 // Regular imports 1636 for (const ImportDirectoryEntryRef &I : Obj->import_directories()) { 1637 DictScope Import(W, "Import"); 1638 StringRef Name; 1639 error(I.getName(Name)); 1640 W.printString("Name", Name); 1641 uint32_t ILTAddr; 1642 error(I.getImportLookupTableRVA(ILTAddr)); 1643 W.printHex("ImportLookupTableRVA", ILTAddr); 1644 uint32_t IATAddr; 1645 error(I.getImportAddressTableRVA(IATAddr)); 1646 W.printHex("ImportAddressTableRVA", IATAddr); 1647 // The import lookup table can be missing with certain older linkers, so 1648 // fall back to the import address table in that case. 1649 if (ILTAddr) 1650 printImportedSymbols(I.lookup_table_symbols()); 1651 else 1652 printImportedSymbols(I.imported_symbols()); 1653 } 1654 1655 // Delay imports 1656 for (const DelayImportDirectoryEntryRef &I : Obj->delay_import_directories()) { 1657 DictScope Import(W, "DelayImport"); 1658 StringRef Name; 1659 error(I.getName(Name)); 1660 W.printString("Name", Name); 1661 const delay_import_directory_table_entry *Table; 1662 error(I.getDelayImportTable(Table)); 1663 W.printHex("Attributes", Table->Attributes); 1664 W.printHex("ModuleHandle", Table->ModuleHandle); 1665 W.printHex("ImportAddressTable", Table->DelayImportAddressTable); 1666 W.printHex("ImportNameTable", Table->DelayImportNameTable); 1667 W.printHex("BoundDelayImportTable", Table->BoundDelayImportTable); 1668 W.printHex("UnloadDelayImportTable", Table->UnloadDelayImportTable); 1669 printDelayImportedSymbols(I, I.imported_symbols()); 1670 } 1671 } 1672 1673 void COFFDumper::printCOFFExports() { 1674 for (const ExportDirectoryEntryRef &E : Obj->export_directories()) { 1675 DictScope Export(W, "Export"); 1676 1677 StringRef Name; 1678 uint32_t Ordinal, RVA; 1679 1680 error(E.getSymbolName(Name)); 1681 error(E.getOrdinal(Ordinal)); 1682 error(E.getExportRVA(RVA)); 1683 1684 W.printNumber("Ordinal", Ordinal); 1685 W.printString("Name", Name); 1686 W.printHex("RVA", RVA); 1687 } 1688 } 1689 1690 void COFFDumper::printCOFFDirectives() { 1691 for (const SectionRef &Section : Obj->sections()) { 1692 StringRef Name; 1693 1694 error(Section.getName(Name)); 1695 if (Name != ".drectve") 1696 continue; 1697 1698 StringRef Contents = 1699 unwrapOrError(Obj->getFileName(), Section.getContents()); 1700 W.printString("Directive(s)", Contents); 1701 } 1702 } 1703 1704 static std::string getBaseRelocTypeName(uint8_t Type) { 1705 switch (Type) { 1706 case COFF::IMAGE_REL_BASED_ABSOLUTE: return "ABSOLUTE"; 1707 case COFF::IMAGE_REL_BASED_HIGH: return "HIGH"; 1708 case COFF::IMAGE_REL_BASED_LOW: return "LOW"; 1709 case COFF::IMAGE_REL_BASED_HIGHLOW: return "HIGHLOW"; 1710 case COFF::IMAGE_REL_BASED_HIGHADJ: return "HIGHADJ"; 1711 case COFF::IMAGE_REL_BASED_ARM_MOV32T: return "ARM_MOV32(T)"; 1712 case COFF::IMAGE_REL_BASED_DIR64: return "DIR64"; 1713 default: return "unknown (" + llvm::utostr(Type) + ")"; 1714 } 1715 } 1716 1717 void COFFDumper::printCOFFBaseReloc() { 1718 ListScope D(W, "BaseReloc"); 1719 for (const BaseRelocRef &I : Obj->base_relocs()) { 1720 uint8_t Type; 1721 uint32_t RVA; 1722 error(I.getRVA(RVA)); 1723 error(I.getType(Type)); 1724 DictScope Import(W, "Entry"); 1725 W.printString("Type", getBaseRelocTypeName(Type)); 1726 W.printHex("Address", RVA); 1727 } 1728 } 1729 1730 void COFFDumper::printCOFFResources() { 1731 ListScope ResourcesD(W, "Resources"); 1732 for (const SectionRef &S : Obj->sections()) { 1733 StringRef Name; 1734 error(S.getName(Name)); 1735 if (!Name.startswith(".rsrc")) 1736 continue; 1737 1738 StringRef Ref = unwrapOrError(Obj->getFileName(), S.getContents()); 1739 1740 if ((Name == ".rsrc") || (Name == ".rsrc$01")) { 1741 ResourceSectionRef RSF(Ref); 1742 auto &BaseTable = unwrapOrError(Obj->getFileName(), RSF.getBaseTable()); 1743 W.printNumber("Total Number of Resources", 1744 countTotalTableEntries(RSF, BaseTable, "Type")); 1745 W.printHex("Base Table Address", 1746 Obj->getCOFFSection(S)->PointerToRawData); 1747 W.startLine() << "\n"; 1748 printResourceDirectoryTable(RSF, BaseTable, "Type"); 1749 } 1750 if (opts::SectionData) 1751 W.printBinaryBlock(Name.str() + " Data", Ref); 1752 } 1753 } 1754 1755 uint32_t 1756 COFFDumper::countTotalTableEntries(ResourceSectionRef RSF, 1757 const coff_resource_dir_table &Table, 1758 StringRef Level) { 1759 uint32_t TotalEntries = 0; 1760 for (int i = 0; i < Table.NumberOfNameEntries + Table.NumberOfIDEntries; 1761 i++) { 1762 auto Entry = unwrapOrError(Obj->getFileName(), 1763 getResourceDirectoryTableEntry(Table, i)); 1764 if (Entry.Offset.isSubDir()) { 1765 StringRef NextLevel; 1766 if (Level == "Name") 1767 NextLevel = "Language"; 1768 else 1769 NextLevel = "Name"; 1770 auto &NextTable = 1771 unwrapOrError(Obj->getFileName(), RSF.getEntrySubDir(Entry)); 1772 TotalEntries += countTotalTableEntries(RSF, NextTable, NextLevel); 1773 } else { 1774 TotalEntries += 1; 1775 } 1776 } 1777 return TotalEntries; 1778 } 1779 1780 void COFFDumper::printResourceDirectoryTable( 1781 ResourceSectionRef RSF, const coff_resource_dir_table &Table, 1782 StringRef Level) { 1783 1784 W.printNumber("Number of String Entries", Table.NumberOfNameEntries); 1785 W.printNumber("Number of ID Entries", Table.NumberOfIDEntries); 1786 1787 // Iterate through level in resource directory tree. 1788 for (int i = 0; i < Table.NumberOfNameEntries + Table.NumberOfIDEntries; 1789 i++) { 1790 auto Entry = unwrapOrError(Obj->getFileName(), 1791 getResourceDirectoryTableEntry(Table, i)); 1792 StringRef Name; 1793 SmallString<20> IDStr; 1794 raw_svector_ostream OS(IDStr); 1795 if (i < Table.NumberOfNameEntries) { 1796 ArrayRef<UTF16> RawEntryNameString = 1797 unwrapOrError(Obj->getFileName(), RSF.getEntryNameString(Entry)); 1798 std::vector<UTF16> EndianCorrectedNameString; 1799 if (llvm::sys::IsBigEndianHost) { 1800 EndianCorrectedNameString.resize(RawEntryNameString.size() + 1); 1801 std::copy(RawEntryNameString.begin(), RawEntryNameString.end(), 1802 EndianCorrectedNameString.begin() + 1); 1803 EndianCorrectedNameString[0] = UNI_UTF16_BYTE_ORDER_MARK_SWAPPED; 1804 RawEntryNameString = makeArrayRef(EndianCorrectedNameString); 1805 } 1806 std::string EntryNameString; 1807 if (!llvm::convertUTF16ToUTF8String(RawEntryNameString, EntryNameString)) 1808 error(object_error::parse_failed); 1809 OS << ": "; 1810 OS << EntryNameString; 1811 } else { 1812 if (Level == "Type") { 1813 OS << ": "; 1814 printResourceTypeName(Entry.Identifier.ID, OS); 1815 IDStr = IDStr.slice(0, IDStr.find_first_of(")", 0) + 1); 1816 } else { 1817 OS << ": (ID " << Entry.Identifier.ID << ")"; 1818 } 1819 } 1820 Name = StringRef(IDStr); 1821 ListScope ResourceType(W, Level.str() + Name.str()); 1822 if (Entry.Offset.isSubDir()) { 1823 W.printHex("Table Offset", Entry.Offset.value()); 1824 StringRef NextLevel; 1825 if (Level == "Name") 1826 NextLevel = "Language"; 1827 else 1828 NextLevel = "Name"; 1829 auto &NextTable = 1830 unwrapOrError(Obj->getFileName(), RSF.getEntrySubDir(Entry)); 1831 printResourceDirectoryTable(RSF, NextTable, NextLevel); 1832 } else { 1833 W.printHex("Entry Offset", Entry.Offset.value()); 1834 char FormattedTime[20] = {}; 1835 time_t TDS = time_t(Table.TimeDateStamp); 1836 strftime(FormattedTime, 20, "%Y-%m-%d %H:%M:%S", gmtime(&TDS)); 1837 W.printHex("Time/Date Stamp", FormattedTime, Table.TimeDateStamp); 1838 W.printNumber("Major Version", Table.MajorVersion); 1839 W.printNumber("Minor Version", Table.MinorVersion); 1840 W.printNumber("Characteristics", Table.Characteristics); 1841 } 1842 } 1843 } 1844 1845 Expected<const coff_resource_dir_entry &> 1846 COFFDumper::getResourceDirectoryTableEntry(const coff_resource_dir_table &Table, 1847 uint32_t Index) { 1848 if (Index >= (uint32_t)(Table.NumberOfNameEntries + Table.NumberOfIDEntries)) 1849 return createError("can't get resource directory table entry"); 1850 auto TablePtr = reinterpret_cast<const coff_resource_dir_entry *>(&Table + 1); 1851 return TablePtr[Index]; 1852 } 1853 1854 void COFFDumper::printStackMap() const { 1855 object::SectionRef StackMapSection; 1856 for (auto Sec : Obj->sections()) { 1857 StringRef Name; 1858 Sec.getName(Name); 1859 if (Name == ".llvm_stackmaps") { 1860 StackMapSection = Sec; 1861 break; 1862 } 1863 } 1864 1865 if (StackMapSection == object::SectionRef()) 1866 return; 1867 1868 StringRef StackMapContents = 1869 unwrapOrError(Obj->getFileName(), StackMapSection.getContents()); 1870 ArrayRef<uint8_t> StackMapContentsArray = 1871 arrayRefFromStringRef(StackMapContents); 1872 1873 if (Obj->isLittleEndian()) 1874 prettyPrintStackMap( 1875 W, StackMapParser<support::little>(StackMapContentsArray)); 1876 else 1877 prettyPrintStackMap( 1878 W, StackMapParser<support::big>(StackMapContentsArray)); 1879 } 1880 1881 void COFFDumper::printAddrsig() { 1882 object::SectionRef AddrsigSection; 1883 for (auto Sec : Obj->sections()) { 1884 StringRef Name; 1885 Sec.getName(Name); 1886 if (Name == ".llvm_addrsig") { 1887 AddrsigSection = Sec; 1888 break; 1889 } 1890 } 1891 1892 if (AddrsigSection == object::SectionRef()) 1893 return; 1894 1895 StringRef AddrsigContents = 1896 unwrapOrError(Obj->getFileName(), AddrsigSection.getContents()); 1897 ArrayRef<uint8_t> AddrsigContentsArray(AddrsigContents.bytes_begin(), 1898 AddrsigContents.size()); 1899 1900 ListScope L(W, "Addrsig"); 1901 const uint8_t *Cur = AddrsigContents.bytes_begin(); 1902 const uint8_t *End = AddrsigContents.bytes_end(); 1903 while (Cur != End) { 1904 unsigned Size; 1905 const char *Err; 1906 uint64_t SymIndex = decodeULEB128(Cur, &Size, End, &Err); 1907 if (Err) 1908 reportError(Err); 1909 1910 Expected<COFFSymbolRef> Sym = Obj->getSymbol(SymIndex); 1911 StringRef SymName; 1912 std::error_code EC = errorToErrorCode(Sym.takeError()); 1913 if (EC || (EC = Obj->getSymbolName(*Sym, SymName))) { 1914 SymName = ""; 1915 error(EC); 1916 } 1917 1918 W.printNumber("Sym", SymName, SymIndex); 1919 Cur += Size; 1920 } 1921 } 1922 1923 void llvm::dumpCodeViewMergedTypes(ScopedPrinter &Writer, 1924 ArrayRef<ArrayRef<uint8_t>> IpiRecords, 1925 ArrayRef<ArrayRef<uint8_t>> TpiRecords) { 1926 TypeTableCollection TpiTypes(TpiRecords); 1927 { 1928 ListScope S(Writer, "MergedTypeStream"); 1929 TypeDumpVisitor TDV(TpiTypes, &Writer, opts::CodeViewSubsectionBytes); 1930 if (Error Err = codeview::visitTypeStream(TpiTypes, TDV)) 1931 reportError(std::move(Err), "<?>"); 1932 Writer.flush(); 1933 } 1934 1935 // Flatten the id stream and print it next. The ID stream refers to names from 1936 // the type stream. 1937 TypeTableCollection IpiTypes(IpiRecords); 1938 { 1939 ListScope S(Writer, "MergedIDStream"); 1940 TypeDumpVisitor TDV(TpiTypes, &Writer, opts::CodeViewSubsectionBytes); 1941 TDV.setIpiTypes(IpiTypes); 1942 if (Error Err = codeview::visitTypeStream(IpiTypes, TDV)) 1943 reportError(std::move(Err), "<?>"); 1944 Writer.flush(); 1945 } 1946 } 1947