1 //===-- COFFDumper.cpp - COFF-specific dumper -------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
10 /// This file implements the COFF-specific dumper for llvm-readobj.
12 //===----------------------------------------------------------------------===//
14 #include "ARMWinEHPrinter.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"
58 using namespace llvm::object;
59 using namespace llvm::codeview;
60 using namespace llvm::support;
61 using namespace llvm::Win64EH;
65 struct LoadConfigTables {
66 uint64_t SEHTableVA = 0;
67 uint64_t SEHTableCount = 0;
68 uint32_t GuardFlags = 0;
69 uint64_t GuardFidTableVA = 0;
70 uint64_t GuardFidTableCount = 0;
71 uint64_t GuardLJmpTableVA = 0;
72 uint64_t GuardLJmpTableCount = 0;
75 class COFFDumper : public ObjDumper {
77 friend class COFFObjectDumpDelegate;
78 COFFDumper(const llvm::object::COFFObjectFile *Obj, ScopedPrinter &Writer)
79 : ObjDumper(Writer), Obj(Obj), Writer(Writer), Types(100) {}
81 void printFileHeaders() override;
82 void printSectionHeaders() override;
83 void printRelocations() override;
84 void printUnwindInfo() override;
86 void printNeededLibraries() override;
88 void printCOFFImports() override;
89 void printCOFFExports() override;
90 void printCOFFDirectives() override;
91 void printCOFFBaseReloc() override;
92 void printCOFFDebugDirectory() override;
93 void printCOFFResources() override;
94 void printCOFFLoadConfig() override;
95 void printCodeViewDebugInfo() override;
96 void mergeCodeViewTypes(llvm::codeview::MergingTypeTableBuilder &CVIDs,
97 llvm::codeview::MergingTypeTableBuilder &CVTypes,
98 llvm::codeview::GlobalTypeTableBuilder &GlobalCVIDs,
99 llvm::codeview::GlobalTypeTableBuilder &GlobalCVTypes,
100 bool GHash) override;
101 void printStackMap() const override;
102 void printAddrsig() override;
104 void printSymbols() override;
105 void printDynamicSymbols() override;
106 void printSymbol(const SymbolRef &Sym);
107 void printRelocation(const SectionRef &Section, const RelocationRef &Reloc,
109 void printDataDirectory(uint32_t Index, const std::string &FieldName);
111 void printDOSHeader(const dos_header *DH);
112 template <class PEHeader> void printPEHeader(const PEHeader *Hdr);
113 void printBaseOfDataField(const pe32_header *Hdr);
114 void printBaseOfDataField(const pe32plus_header *Hdr);
115 template <typename T>
116 void printCOFFLoadConfig(const T *Conf, LoadConfigTables &Tables);
117 typedef void (*PrintExtraCB)(raw_ostream &, const uint8_t *);
118 void printRVATable(uint64_t TableVA, uint64_t Count, uint64_t EntrySize,
119 PrintExtraCB PrintExtra = 0);
121 void printCodeViewSymbolSection(StringRef SectionName, const SectionRef &Section);
122 void printCodeViewTypeSection(StringRef SectionName, const SectionRef &Section);
123 StringRef getTypeName(TypeIndex Ty);
124 StringRef getFileNameForFileOffset(uint32_t FileOffset);
125 void printFileNameForOffset(StringRef Label, uint32_t FileOffset);
126 void printTypeIndex(StringRef FieldName, TypeIndex TI) {
127 // Forward to CVTypeDumper for simplicity.
128 codeview::printTypeIndex(Writer, FieldName, TI, Types);
131 void printCodeViewSymbolsSubsection(StringRef Subsection,
132 const SectionRef &Section,
133 StringRef SectionContents);
135 void printCodeViewFileChecksums(StringRef Subsection);
137 void printCodeViewInlineeLines(StringRef Subsection);
139 void printRelocatedField(StringRef Label, const coff_section *Sec,
140 uint32_t RelocOffset, uint32_t Offset,
141 StringRef *RelocSym = nullptr);
143 uint32_t countTotalTableEntries(ResourceSectionRef RSF,
144 const coff_resource_dir_table &Table,
147 void printResourceDirectoryTable(ResourceSectionRef RSF,
148 const coff_resource_dir_table &Table,
151 void printBinaryBlockWithRelocs(StringRef Label, const SectionRef &Sec,
152 StringRef SectionContents, StringRef Block);
154 /// Given a .debug$S section, find the string table and file checksum table.
155 void initializeFileAndStringTables(BinaryStreamReader &Reader);
157 void cacheRelocations();
159 std::error_code resolveSymbol(const coff_section *Section, uint64_t Offset,
161 std::error_code resolveSymbolName(const coff_section *Section,
162 uint64_t Offset, StringRef &Name);
163 std::error_code resolveSymbolName(const coff_section *Section,
164 StringRef SectionContents,
165 const void *RelocPtr, StringRef &Name);
166 void printImportedSymbols(iterator_range<imported_symbol_iterator> Range);
167 void printDelayImportedSymbols(
168 const DelayImportDirectoryEntryRef &I,
169 iterator_range<imported_symbol_iterator> Range);
170 ErrorOr<const coff_resource_dir_entry &>
171 getResourceDirectoryTableEntry(const coff_resource_dir_table &Table,
174 typedef DenseMap<const coff_section*, std::vector<RelocationRef> > RelocMapTy;
176 const llvm::object::COFFObjectFile *Obj;
177 bool RelocCached = false;
180 DebugChecksumsSubsectionRef CVFileChecksumTable;
182 DebugStringTableSubsectionRef CVStringTable;
184 /// Track the compilation CPU type. S_COMPILE3 symbol records typically come
185 /// first, but if we don't see one, just assume an X64 CPU type. It is common.
186 CPUType CompilationCPUType = CPUType::X64;
188 ScopedPrinter &Writer;
189 BinaryByteStream TypeContents;
190 LazyRandomTypeCollection Types;
193 class COFFObjectDumpDelegate : public SymbolDumpDelegate {
195 COFFObjectDumpDelegate(COFFDumper &CD, const SectionRef &SR,
196 const COFFObjectFile *Obj, StringRef SectionContents)
197 : CD(CD), SR(SR), SectionContents(SectionContents) {
198 Sec = Obj->getCOFFSection(SR);
201 uint32_t getRecordOffset(BinaryStreamReader Reader) override {
202 ArrayRef<uint8_t> Data;
203 if (auto EC = Reader.readLongestContiguousChunk(Data)) {
204 llvm::consumeError(std::move(EC));
207 return Data.data() - SectionContents.bytes_begin();
210 void printRelocatedField(StringRef Label, uint32_t RelocOffset,
211 uint32_t Offset, StringRef *RelocSym) override {
212 CD.printRelocatedField(Label, Sec, RelocOffset, Offset, RelocSym);
215 void printBinaryBlockWithRelocs(StringRef Label,
216 ArrayRef<uint8_t> Block) override {
217 StringRef SBlock(reinterpret_cast<const char *>(Block.data()),
219 if (opts::CodeViewSubsectionBytes)
220 CD.printBinaryBlockWithRelocs(Label, SR, SectionContents, SBlock);
223 StringRef getFileNameForFileOffset(uint32_t FileOffset) override {
224 return CD.getFileNameForFileOffset(FileOffset);
227 DebugStringTableSubsectionRef getStringTable() override {
228 return CD.CVStringTable;
233 const SectionRef &SR;
234 const coff_section *Sec;
235 StringRef SectionContents;
242 std::error_code createCOFFDumper(const object::ObjectFile *Obj,
243 ScopedPrinter &Writer,
244 std::unique_ptr<ObjDumper> &Result) {
245 const COFFObjectFile *COFFObj = dyn_cast<COFFObjectFile>(Obj);
247 return readobj_error::unsupported_obj_file_format;
249 Result.reset(new COFFDumper(COFFObj, Writer));
250 return readobj_error::success;
255 // Given a section and an offset into this section the function returns the
256 // symbol used for the relocation at the offset.
257 std::error_code COFFDumper::resolveSymbol(const coff_section *Section,
258 uint64_t Offset, SymbolRef &Sym) {
260 const auto &Relocations = RelocMap[Section];
261 auto SymI = Obj->symbol_end();
262 for (const auto &Relocation : Relocations) {
263 uint64_t RelocationOffset = Relocation.getOffset();
265 if (RelocationOffset == Offset) {
266 SymI = Relocation.getSymbol();
270 if (SymI == Obj->symbol_end())
271 return readobj_error::unknown_symbol;
273 return readobj_error::success;
276 // Given a section and an offset into this section the function returns the name
277 // of the symbol used for the relocation at the offset.
278 std::error_code COFFDumper::resolveSymbolName(const coff_section *Section,
282 if (std::error_code EC = resolveSymbol(Section, Offset, Symbol))
284 Expected<StringRef> NameOrErr = Symbol.getName();
286 return errorToErrorCode(NameOrErr.takeError());
288 return std::error_code();
291 // Helper for when you have a pointer to real data and you want to know about
292 // relocations against it.
293 std::error_code COFFDumper::resolveSymbolName(const coff_section *Section,
294 StringRef SectionContents,
295 const void *RelocPtr,
297 assert(SectionContents.data() < RelocPtr &&
298 RelocPtr < SectionContents.data() + SectionContents.size() &&
299 "pointer to relocated object is not in section");
300 uint64_t Offset = ptrdiff_t(reinterpret_cast<const char *>(RelocPtr) -
301 SectionContents.data());
302 return resolveSymbolName(Section, Offset, Name);
305 void COFFDumper::printRelocatedField(StringRef Label, const coff_section *Sec,
306 uint32_t RelocOffset, uint32_t Offset,
307 StringRef *RelocSym) {
308 StringRef SymStorage;
309 StringRef &Symbol = RelocSym ? *RelocSym : SymStorage;
310 if (!resolveSymbolName(Sec, RelocOffset, Symbol))
311 W.printSymbolOffset(Label, Symbol, Offset);
313 W.printHex(Label, RelocOffset);
316 void COFFDumper::printBinaryBlockWithRelocs(StringRef Label,
317 const SectionRef &Sec,
318 StringRef SectionContents,
320 W.printBinaryBlock(Label, Block);
322 assert(SectionContents.begin() < Block.begin() &&
323 SectionContents.end() >= Block.end() &&
324 "Block is not contained in SectionContents");
325 uint64_t OffsetStart = Block.data() - SectionContents.data();
326 uint64_t OffsetEnd = OffsetStart + Block.size();
330 ListScope D(W, "BlockRelocations");
331 const coff_section *Section = Obj->getCOFFSection(Sec);
332 const auto &Relocations = RelocMap[Section];
333 for (const auto &Relocation : Relocations) {
334 uint64_t RelocationOffset = Relocation.getOffset();
335 if (OffsetStart <= RelocationOffset && RelocationOffset < OffsetEnd)
336 printRelocation(Sec, Relocation, OffsetStart);
340 static const EnumEntry<COFF::MachineTypes> ImageFileMachineType[] = {
341 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_UNKNOWN ),
342 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_AM33 ),
343 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_AMD64 ),
344 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_ARM ),
345 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_ARM64 ),
346 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_ARMNT ),
347 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_EBC ),
348 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_I386 ),
349 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_IA64 ),
350 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_M32R ),
351 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_MIPS16 ),
352 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_MIPSFPU ),
353 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_MIPSFPU16),
354 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_POWERPC ),
355 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_POWERPCFP),
356 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_R4000 ),
357 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_SH3 ),
358 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_SH3DSP ),
359 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_SH4 ),
360 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_SH5 ),
361 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_THUMB ),
362 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_WCEMIPSV2)
365 static const EnumEntry<COFF::Characteristics> ImageFileCharacteristics[] = {
366 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_RELOCS_STRIPPED ),
367 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_EXECUTABLE_IMAGE ),
368 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_LINE_NUMS_STRIPPED ),
369 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_LOCAL_SYMS_STRIPPED ),
370 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_AGGRESSIVE_WS_TRIM ),
371 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_LARGE_ADDRESS_AWARE ),
372 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_BYTES_REVERSED_LO ),
373 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_32BIT_MACHINE ),
374 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_DEBUG_STRIPPED ),
375 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_REMOVABLE_RUN_FROM_SWAP),
376 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_NET_RUN_FROM_SWAP ),
377 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_SYSTEM ),
378 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_DLL ),
379 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_UP_SYSTEM_ONLY ),
380 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_BYTES_REVERSED_HI )
383 static const EnumEntry<COFF::WindowsSubsystem> PEWindowsSubsystem[] = {
384 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SUBSYSTEM_UNKNOWN ),
385 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SUBSYSTEM_NATIVE ),
386 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SUBSYSTEM_WINDOWS_GUI ),
387 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SUBSYSTEM_WINDOWS_CUI ),
388 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SUBSYSTEM_POSIX_CUI ),
389 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SUBSYSTEM_WINDOWS_CE_GUI ),
390 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SUBSYSTEM_EFI_APPLICATION ),
391 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER),
392 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER ),
393 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SUBSYSTEM_EFI_ROM ),
394 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SUBSYSTEM_XBOX ),
397 static const EnumEntry<COFF::DLLCharacteristics> PEDLLCharacteristics[] = {
398 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_DLL_CHARACTERISTICS_HIGH_ENTROPY_VA ),
399 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_DLL_CHARACTERISTICS_DYNAMIC_BASE ),
400 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_DLL_CHARACTERISTICS_FORCE_INTEGRITY ),
401 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_DLL_CHARACTERISTICS_NX_COMPAT ),
402 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_DLL_CHARACTERISTICS_NO_ISOLATION ),
403 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_DLL_CHARACTERISTICS_NO_SEH ),
404 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_DLL_CHARACTERISTICS_NO_BIND ),
405 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_DLL_CHARACTERISTICS_APPCONTAINER ),
406 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_DLL_CHARACTERISTICS_WDM_DRIVER ),
407 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_DLL_CHARACTERISTICS_GUARD_CF ),
408 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_DLL_CHARACTERISTICS_TERMINAL_SERVER_AWARE),
411 static const EnumEntry<COFF::SectionCharacteristics>
412 ImageSectionCharacteristics[] = {
413 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_TYPE_NOLOAD ),
414 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_TYPE_NO_PAD ),
415 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_CNT_CODE ),
416 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_CNT_INITIALIZED_DATA ),
417 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_CNT_UNINITIALIZED_DATA),
418 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_LNK_OTHER ),
419 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_LNK_INFO ),
420 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_LNK_REMOVE ),
421 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_LNK_COMDAT ),
422 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_GPREL ),
423 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_MEM_PURGEABLE ),
424 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_MEM_16BIT ),
425 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_MEM_LOCKED ),
426 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_MEM_PRELOAD ),
427 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_ALIGN_1BYTES ),
428 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_ALIGN_2BYTES ),
429 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_ALIGN_4BYTES ),
430 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_ALIGN_8BYTES ),
431 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_ALIGN_16BYTES ),
432 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_ALIGN_32BYTES ),
433 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_ALIGN_64BYTES ),
434 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_ALIGN_128BYTES ),
435 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_ALIGN_256BYTES ),
436 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_ALIGN_512BYTES ),
437 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_ALIGN_1024BYTES ),
438 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_ALIGN_2048BYTES ),
439 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_ALIGN_4096BYTES ),
440 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_ALIGN_8192BYTES ),
441 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_LNK_NRELOC_OVFL ),
442 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_MEM_DISCARDABLE ),
443 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_MEM_NOT_CACHED ),
444 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_MEM_NOT_PAGED ),
445 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_MEM_SHARED ),
446 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_MEM_EXECUTE ),
447 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_MEM_READ ),
448 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_MEM_WRITE )
451 static const EnumEntry<COFF::SymbolBaseType> ImageSymType[] = {
452 { "Null" , COFF::IMAGE_SYM_TYPE_NULL },
453 { "Void" , COFF::IMAGE_SYM_TYPE_VOID },
454 { "Char" , COFF::IMAGE_SYM_TYPE_CHAR },
455 { "Short" , COFF::IMAGE_SYM_TYPE_SHORT },
456 { "Int" , COFF::IMAGE_SYM_TYPE_INT },
457 { "Long" , COFF::IMAGE_SYM_TYPE_LONG },
458 { "Float" , COFF::IMAGE_SYM_TYPE_FLOAT },
459 { "Double", COFF::IMAGE_SYM_TYPE_DOUBLE },
460 { "Struct", COFF::IMAGE_SYM_TYPE_STRUCT },
461 { "Union" , COFF::IMAGE_SYM_TYPE_UNION },
462 { "Enum" , COFF::IMAGE_SYM_TYPE_ENUM },
463 { "MOE" , COFF::IMAGE_SYM_TYPE_MOE },
464 { "Byte" , COFF::IMAGE_SYM_TYPE_BYTE },
465 { "Word" , COFF::IMAGE_SYM_TYPE_WORD },
466 { "UInt" , COFF::IMAGE_SYM_TYPE_UINT },
467 { "DWord" , COFF::IMAGE_SYM_TYPE_DWORD }
470 static const EnumEntry<COFF::SymbolComplexType> ImageSymDType[] = {
471 { "Null" , COFF::IMAGE_SYM_DTYPE_NULL },
472 { "Pointer" , COFF::IMAGE_SYM_DTYPE_POINTER },
473 { "Function", COFF::IMAGE_SYM_DTYPE_FUNCTION },
474 { "Array" , COFF::IMAGE_SYM_DTYPE_ARRAY }
477 static const EnumEntry<COFF::SymbolStorageClass> ImageSymClass[] = {
478 { "EndOfFunction" , COFF::IMAGE_SYM_CLASS_END_OF_FUNCTION },
479 { "Null" , COFF::IMAGE_SYM_CLASS_NULL },
480 { "Automatic" , COFF::IMAGE_SYM_CLASS_AUTOMATIC },
481 { "External" , COFF::IMAGE_SYM_CLASS_EXTERNAL },
482 { "Static" , COFF::IMAGE_SYM_CLASS_STATIC },
483 { "Register" , COFF::IMAGE_SYM_CLASS_REGISTER },
484 { "ExternalDef" , COFF::IMAGE_SYM_CLASS_EXTERNAL_DEF },
485 { "Label" , COFF::IMAGE_SYM_CLASS_LABEL },
486 { "UndefinedLabel" , COFF::IMAGE_SYM_CLASS_UNDEFINED_LABEL },
487 { "MemberOfStruct" , COFF::IMAGE_SYM_CLASS_MEMBER_OF_STRUCT },
488 { "Argument" , COFF::IMAGE_SYM_CLASS_ARGUMENT },
489 { "StructTag" , COFF::IMAGE_SYM_CLASS_STRUCT_TAG },
490 { "MemberOfUnion" , COFF::IMAGE_SYM_CLASS_MEMBER_OF_UNION },
491 { "UnionTag" , COFF::IMAGE_SYM_CLASS_UNION_TAG },
492 { "TypeDefinition" , COFF::IMAGE_SYM_CLASS_TYPE_DEFINITION },
493 { "UndefinedStatic", COFF::IMAGE_SYM_CLASS_UNDEFINED_STATIC },
494 { "EnumTag" , COFF::IMAGE_SYM_CLASS_ENUM_TAG },
495 { "MemberOfEnum" , COFF::IMAGE_SYM_CLASS_MEMBER_OF_ENUM },
496 { "RegisterParam" , COFF::IMAGE_SYM_CLASS_REGISTER_PARAM },
497 { "BitField" , COFF::IMAGE_SYM_CLASS_BIT_FIELD },
498 { "Block" , COFF::IMAGE_SYM_CLASS_BLOCK },
499 { "Function" , COFF::IMAGE_SYM_CLASS_FUNCTION },
500 { "EndOfStruct" , COFF::IMAGE_SYM_CLASS_END_OF_STRUCT },
501 { "File" , COFF::IMAGE_SYM_CLASS_FILE },
502 { "Section" , COFF::IMAGE_SYM_CLASS_SECTION },
503 { "WeakExternal" , COFF::IMAGE_SYM_CLASS_WEAK_EXTERNAL },
504 { "CLRToken" , COFF::IMAGE_SYM_CLASS_CLR_TOKEN }
507 static const EnumEntry<COFF::COMDATType> ImageCOMDATSelect[] = {
508 { "NoDuplicates", COFF::IMAGE_COMDAT_SELECT_NODUPLICATES },
509 { "Any" , COFF::IMAGE_COMDAT_SELECT_ANY },
510 { "SameSize" , COFF::IMAGE_COMDAT_SELECT_SAME_SIZE },
511 { "ExactMatch" , COFF::IMAGE_COMDAT_SELECT_EXACT_MATCH },
512 { "Associative" , COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE },
513 { "Largest" , COFF::IMAGE_COMDAT_SELECT_LARGEST },
514 { "Newest" , COFF::IMAGE_COMDAT_SELECT_NEWEST }
517 static const EnumEntry<COFF::DebugType> ImageDebugType[] = {
518 { "Unknown" , COFF::IMAGE_DEBUG_TYPE_UNKNOWN },
519 { "COFF" , COFF::IMAGE_DEBUG_TYPE_COFF },
520 { "CodeView" , COFF::IMAGE_DEBUG_TYPE_CODEVIEW },
521 { "FPO" , COFF::IMAGE_DEBUG_TYPE_FPO },
522 { "Misc" , COFF::IMAGE_DEBUG_TYPE_MISC },
523 { "Exception" , COFF::IMAGE_DEBUG_TYPE_EXCEPTION },
524 { "Fixup" , COFF::IMAGE_DEBUG_TYPE_FIXUP },
525 { "OmapToSrc" , COFF::IMAGE_DEBUG_TYPE_OMAP_TO_SRC },
526 { "OmapFromSrc", COFF::IMAGE_DEBUG_TYPE_OMAP_FROM_SRC },
527 { "Borland" , COFF::IMAGE_DEBUG_TYPE_BORLAND },
528 { "Reserved10" , COFF::IMAGE_DEBUG_TYPE_RESERVED10 },
529 { "CLSID" , COFF::IMAGE_DEBUG_TYPE_CLSID },
530 { "VCFeature" , COFF::IMAGE_DEBUG_TYPE_VC_FEATURE },
531 { "POGO" , COFF::IMAGE_DEBUG_TYPE_POGO },
532 { "ILTCG" , COFF::IMAGE_DEBUG_TYPE_ILTCG },
533 { "MPX" , COFF::IMAGE_DEBUG_TYPE_MPX },
534 { "Repro" , COFF::IMAGE_DEBUG_TYPE_REPRO },
537 static const EnumEntry<COFF::WeakExternalCharacteristics>
538 WeakExternalCharacteristics[] = {
539 { "NoLibrary", COFF::IMAGE_WEAK_EXTERN_SEARCH_NOLIBRARY },
540 { "Library" , COFF::IMAGE_WEAK_EXTERN_SEARCH_LIBRARY },
541 { "Alias" , COFF::IMAGE_WEAK_EXTERN_SEARCH_ALIAS }
544 static const EnumEntry<uint32_t> SubSectionTypes[] = {
545 LLVM_READOBJ_ENUM_CLASS_ENT(DebugSubsectionKind, Symbols),
546 LLVM_READOBJ_ENUM_CLASS_ENT(DebugSubsectionKind, Lines),
547 LLVM_READOBJ_ENUM_CLASS_ENT(DebugSubsectionKind, StringTable),
548 LLVM_READOBJ_ENUM_CLASS_ENT(DebugSubsectionKind, FileChecksums),
549 LLVM_READOBJ_ENUM_CLASS_ENT(DebugSubsectionKind, FrameData),
550 LLVM_READOBJ_ENUM_CLASS_ENT(DebugSubsectionKind, InlineeLines),
551 LLVM_READOBJ_ENUM_CLASS_ENT(DebugSubsectionKind, CrossScopeImports),
552 LLVM_READOBJ_ENUM_CLASS_ENT(DebugSubsectionKind, CrossScopeExports),
553 LLVM_READOBJ_ENUM_CLASS_ENT(DebugSubsectionKind, ILLines),
554 LLVM_READOBJ_ENUM_CLASS_ENT(DebugSubsectionKind, FuncMDTokenMap),
555 LLVM_READOBJ_ENUM_CLASS_ENT(DebugSubsectionKind, TypeMDTokenMap),
556 LLVM_READOBJ_ENUM_CLASS_ENT(DebugSubsectionKind, MergedAssemblyInput),
557 LLVM_READOBJ_ENUM_CLASS_ENT(DebugSubsectionKind, CoffSymbolRVA),
560 static const EnumEntry<uint32_t> FrameDataFlags[] = {
561 LLVM_READOBJ_ENUM_ENT(FrameData, HasSEH),
562 LLVM_READOBJ_ENUM_ENT(FrameData, HasEH),
563 LLVM_READOBJ_ENUM_ENT(FrameData, IsFunctionStart),
566 static const EnumEntry<uint8_t> FileChecksumKindNames[] = {
567 LLVM_READOBJ_ENUM_CLASS_ENT(FileChecksumKind, None),
568 LLVM_READOBJ_ENUM_CLASS_ENT(FileChecksumKind, MD5),
569 LLVM_READOBJ_ENUM_CLASS_ENT(FileChecksumKind, SHA1),
570 LLVM_READOBJ_ENUM_CLASS_ENT(FileChecksumKind, SHA256),
573 template <typename T>
574 static std::error_code getSymbolAuxData(const COFFObjectFile *Obj,
575 COFFSymbolRef Symbol,
576 uint8_t AuxSymbolIdx, const T *&Aux) {
577 ArrayRef<uint8_t> AuxData = Obj->getSymbolAuxData(Symbol);
578 AuxData = AuxData.slice(AuxSymbolIdx * Obj->getSymbolTableEntrySize());
579 Aux = reinterpret_cast<const T*>(AuxData.data());
580 return readobj_error::success;
583 void COFFDumper::cacheRelocations() {
588 for (const SectionRef &S : Obj->sections()) {
589 const coff_section *Section = Obj->getCOFFSection(S);
591 for (const RelocationRef &Reloc : S.relocations())
592 RelocMap[Section].push_back(Reloc);
594 // Sort relocations by address.
595 llvm::sort(RelocMap[Section], [](RelocationRef L, RelocationRef R) {
596 return L.getOffset() < R.getOffset();
601 void COFFDumper::printDataDirectory(uint32_t Index,
602 const std::string &FieldName) {
603 const data_directory *Data;
604 if (Obj->getDataDirectory(Index, Data))
606 W.printHex(FieldName + "RVA", Data->RelativeVirtualAddress);
607 W.printHex(FieldName + "Size", Data->Size);
610 void COFFDumper::printFileHeaders() {
611 time_t TDS = Obj->getTimeDateStamp();
612 char FormattedTime[20] = { };
613 strftime(FormattedTime, 20, "%Y-%m-%d %H:%M:%S", gmtime(&TDS));
616 DictScope D(W, "ImageFileHeader");
617 W.printEnum ("Machine", Obj->getMachine(),
618 makeArrayRef(ImageFileMachineType));
619 W.printNumber("SectionCount", Obj->getNumberOfSections());
620 W.printHex ("TimeDateStamp", FormattedTime, Obj->getTimeDateStamp());
621 W.printHex ("PointerToSymbolTable", Obj->getPointerToSymbolTable());
622 W.printNumber("SymbolCount", Obj->getNumberOfSymbols());
623 W.printNumber("OptionalHeaderSize", Obj->getSizeOfOptionalHeader());
624 W.printFlags ("Characteristics", Obj->getCharacteristics(),
625 makeArrayRef(ImageFileCharacteristics));
628 // Print PE header. This header does not exist if this is an object file and
629 // not an executable.
630 const pe32_header *PEHeader = nullptr;
631 error(Obj->getPE32Header(PEHeader));
633 printPEHeader<pe32_header>(PEHeader);
635 const pe32plus_header *PEPlusHeader = nullptr;
636 error(Obj->getPE32PlusHeader(PEPlusHeader));
638 printPEHeader<pe32plus_header>(PEPlusHeader);
640 if (const dos_header *DH = Obj->getDOSHeader())
644 void COFFDumper::printDOSHeader(const dos_header *DH) {
645 DictScope D(W, "DOSHeader");
646 W.printString("Magic", StringRef(DH->Magic, sizeof(DH->Magic)));
647 W.printNumber("UsedBytesInTheLastPage", DH->UsedBytesInTheLastPage);
648 W.printNumber("FileSizeInPages", DH->FileSizeInPages);
649 W.printNumber("NumberOfRelocationItems", DH->NumberOfRelocationItems);
650 W.printNumber("HeaderSizeInParagraphs", DH->HeaderSizeInParagraphs);
651 W.printNumber("MinimumExtraParagraphs", DH->MinimumExtraParagraphs);
652 W.printNumber("MaximumExtraParagraphs", DH->MaximumExtraParagraphs);
653 W.printNumber("InitialRelativeSS", DH->InitialRelativeSS);
654 W.printNumber("InitialSP", DH->InitialSP);
655 W.printNumber("Checksum", DH->Checksum);
656 W.printNumber("InitialIP", DH->InitialIP);
657 W.printNumber("InitialRelativeCS", DH->InitialRelativeCS);
658 W.printNumber("AddressOfRelocationTable", DH->AddressOfRelocationTable);
659 W.printNumber("OverlayNumber", DH->OverlayNumber);
660 W.printNumber("OEMid", DH->OEMid);
661 W.printNumber("OEMinfo", DH->OEMinfo);
662 W.printNumber("AddressOfNewExeHeader", DH->AddressOfNewExeHeader);
665 template <class PEHeader>
666 void COFFDumper::printPEHeader(const PEHeader *Hdr) {
667 DictScope D(W, "ImageOptionalHeader");
668 W.printHex ("Magic", Hdr->Magic);
669 W.printNumber("MajorLinkerVersion", Hdr->MajorLinkerVersion);
670 W.printNumber("MinorLinkerVersion", Hdr->MinorLinkerVersion);
671 W.printNumber("SizeOfCode", Hdr->SizeOfCode);
672 W.printNumber("SizeOfInitializedData", Hdr->SizeOfInitializedData);
673 W.printNumber("SizeOfUninitializedData", Hdr->SizeOfUninitializedData);
674 W.printHex ("AddressOfEntryPoint", Hdr->AddressOfEntryPoint);
675 W.printHex ("BaseOfCode", Hdr->BaseOfCode);
676 printBaseOfDataField(Hdr);
677 W.printHex ("ImageBase", Hdr->ImageBase);
678 W.printNumber("SectionAlignment", Hdr->SectionAlignment);
679 W.printNumber("FileAlignment", Hdr->FileAlignment);
680 W.printNumber("MajorOperatingSystemVersion",
681 Hdr->MajorOperatingSystemVersion);
682 W.printNumber("MinorOperatingSystemVersion",
683 Hdr->MinorOperatingSystemVersion);
684 W.printNumber("MajorImageVersion", Hdr->MajorImageVersion);
685 W.printNumber("MinorImageVersion", Hdr->MinorImageVersion);
686 W.printNumber("MajorSubsystemVersion", Hdr->MajorSubsystemVersion);
687 W.printNumber("MinorSubsystemVersion", Hdr->MinorSubsystemVersion);
688 W.printNumber("SizeOfImage", Hdr->SizeOfImage);
689 W.printNumber("SizeOfHeaders", Hdr->SizeOfHeaders);
690 W.printEnum ("Subsystem", Hdr->Subsystem, makeArrayRef(PEWindowsSubsystem));
691 W.printFlags ("Characteristics", Hdr->DLLCharacteristics,
692 makeArrayRef(PEDLLCharacteristics));
693 W.printNumber("SizeOfStackReserve", Hdr->SizeOfStackReserve);
694 W.printNumber("SizeOfStackCommit", Hdr->SizeOfStackCommit);
695 W.printNumber("SizeOfHeapReserve", Hdr->SizeOfHeapReserve);
696 W.printNumber("SizeOfHeapCommit", Hdr->SizeOfHeapCommit);
697 W.printNumber("NumberOfRvaAndSize", Hdr->NumberOfRvaAndSize);
699 if (Hdr->NumberOfRvaAndSize > 0) {
700 DictScope D(W, "DataDirectory");
701 static const char * const directory[] = {
702 "ExportTable", "ImportTable", "ResourceTable", "ExceptionTable",
703 "CertificateTable", "BaseRelocationTable", "Debug", "Architecture",
704 "GlobalPtr", "TLSTable", "LoadConfigTable", "BoundImport", "IAT",
705 "DelayImportDescriptor", "CLRRuntimeHeader", "Reserved"
708 for (uint32_t i = 0; i < Hdr->NumberOfRvaAndSize; ++i)
709 printDataDirectory(i, directory[i]);
713 void COFFDumper::printCOFFDebugDirectory() {
714 ListScope LS(W, "DebugDirectory");
715 for (const debug_directory &D : Obj->debug_directories()) {
716 char FormattedTime[20] = {};
717 time_t TDS = D.TimeDateStamp;
718 strftime(FormattedTime, 20, "%Y-%m-%d %H:%M:%S", gmtime(&TDS));
719 DictScope S(W, "DebugEntry");
720 W.printHex("Characteristics", D.Characteristics);
721 W.printHex("TimeDateStamp", FormattedTime, D.TimeDateStamp);
722 W.printHex("MajorVersion", D.MajorVersion);
723 W.printHex("MinorVersion", D.MinorVersion);
724 W.printEnum("Type", D.Type, makeArrayRef(ImageDebugType));
725 W.printHex("SizeOfData", D.SizeOfData);
726 W.printHex("AddressOfRawData", D.AddressOfRawData);
727 W.printHex("PointerToRawData", D.PointerToRawData);
728 if (D.Type == COFF::IMAGE_DEBUG_TYPE_CODEVIEW) {
729 const codeview::DebugInfo *DebugInfo;
730 StringRef PDBFileName;
731 error(Obj->getDebugPDBInfo(&D, DebugInfo, PDBFileName));
732 DictScope PDBScope(W, "PDBInfo");
733 W.printHex("PDBSignature", DebugInfo->Signature.CVSignature);
734 if (DebugInfo->Signature.CVSignature == OMF::Signature::PDB70) {
735 W.printBinary("PDBGUID", makeArrayRef(DebugInfo->PDB70.Signature));
736 W.printNumber("PDBAge", DebugInfo->PDB70.Age);
737 W.printString("PDBFileName", PDBFileName);
739 } else if (D.SizeOfData != 0) {
740 // FIXME: Type values of 12 and 13 are commonly observed but are not in
741 // the documented type enum. Figure out what they mean.
742 ArrayRef<uint8_t> RawData;
744 Obj->getRvaAndSizeAsBytes(D.AddressOfRawData, D.SizeOfData, RawData));
745 W.printBinaryBlock("RawData", RawData);
750 void COFFDumper::printRVATable(uint64_t TableVA, uint64_t Count,
751 uint64_t EntrySize, PrintExtraCB PrintExtra) {
752 uintptr_t TableStart, TableEnd;
753 error(Obj->getVaPtr(TableVA, TableStart));
754 error(Obj->getVaPtr(TableVA + Count * EntrySize - 1, TableEnd));
756 for (uintptr_t I = TableStart; I < TableEnd; I += EntrySize) {
757 uint32_t RVA = *reinterpret_cast<const ulittle32_t *>(I);
758 raw_ostream &OS = W.startLine();
759 OS << W.hex(Obj->getImageBase() + RVA);
761 PrintExtra(OS, reinterpret_cast<const uint8_t *>(I));
766 void COFFDumper::printCOFFLoadConfig() {
767 LoadConfigTables Tables;
769 printCOFFLoadConfig(Obj->getLoadConfig64(), Tables);
771 printCOFFLoadConfig(Obj->getLoadConfig32(), Tables);
773 if (Tables.SEHTableVA) {
774 ListScope LS(W, "SEHTable");
775 printRVATable(Tables.SEHTableVA, Tables.SEHTableCount, 4);
778 if (Tables.GuardFidTableVA) {
779 ListScope LS(W, "GuardFidTable");
780 if (Tables.GuardFlags & uint32_t(coff_guard_flags::FidTableHasFlags)) {
781 auto PrintGuardFlags = [](raw_ostream &OS, const uint8_t *Entry) {
782 uint8_t Flags = *reinterpret_cast<const uint8_t *>(Entry + 4);
784 OS << " flags " << utohexstr(Flags);
786 printRVATable(Tables.GuardFidTableVA, Tables.GuardFidTableCount, 5,
789 printRVATable(Tables.GuardFidTableVA, Tables.GuardFidTableCount, 4);
793 if (Tables.GuardLJmpTableVA) {
794 ListScope LS(W, "GuardLJmpTable");
795 printRVATable(Tables.GuardLJmpTableVA, Tables.GuardLJmpTableCount, 4);
799 template <typename T>
800 void COFFDumper::printCOFFLoadConfig(const T *Conf, LoadConfigTables &Tables) {
804 ListScope LS(W, "LoadConfig");
805 char FormattedTime[20] = {};
806 time_t TDS = Conf->TimeDateStamp;
807 strftime(FormattedTime, 20, "%Y-%m-%d %H:%M:%S", gmtime(&TDS));
808 W.printHex("Size", Conf->Size);
810 // Print everything before SecurityCookie. The vast majority of images today
811 // have all these fields.
812 if (Conf->Size < offsetof(T, SEHandlerTable))
814 W.printHex("TimeDateStamp", FormattedTime, TDS);
815 W.printHex("MajorVersion", Conf->MajorVersion);
816 W.printHex("MinorVersion", Conf->MinorVersion);
817 W.printHex("GlobalFlagsClear", Conf->GlobalFlagsClear);
818 W.printHex("GlobalFlagsSet", Conf->GlobalFlagsSet);
819 W.printHex("CriticalSectionDefaultTimeout",
820 Conf->CriticalSectionDefaultTimeout);
821 W.printHex("DeCommitFreeBlockThreshold", Conf->DeCommitFreeBlockThreshold);
822 W.printHex("DeCommitTotalFreeThreshold", Conf->DeCommitTotalFreeThreshold);
823 W.printHex("LockPrefixTable", Conf->LockPrefixTable);
824 W.printHex("MaximumAllocationSize", Conf->MaximumAllocationSize);
825 W.printHex("VirtualMemoryThreshold", Conf->VirtualMemoryThreshold);
826 W.printHex("ProcessHeapFlags", Conf->ProcessHeapFlags);
827 W.printHex("ProcessAffinityMask", Conf->ProcessAffinityMask);
828 W.printHex("CSDVersion", Conf->CSDVersion);
829 W.printHex("DependentLoadFlags", Conf->DependentLoadFlags);
830 W.printHex("EditList", Conf->EditList);
831 W.printHex("SecurityCookie", Conf->SecurityCookie);
833 // Print the safe SEH table if present.
834 if (Conf->Size < offsetof(coff_load_configuration32, GuardCFCheckFunction))
836 W.printHex("SEHandlerTable", Conf->SEHandlerTable);
837 W.printNumber("SEHandlerCount", Conf->SEHandlerCount);
839 Tables.SEHTableVA = Conf->SEHandlerTable;
840 Tables.SEHTableCount = Conf->SEHandlerCount;
842 // Print everything before CodeIntegrity. (2015)
843 if (Conf->Size < offsetof(T, CodeIntegrity))
845 W.printHex("GuardCFCheckFunction", Conf->GuardCFCheckFunction);
846 W.printHex("GuardCFCheckDispatch", Conf->GuardCFCheckDispatch);
847 W.printHex("GuardCFFunctionTable", Conf->GuardCFFunctionTable);
848 W.printNumber("GuardCFFunctionCount", Conf->GuardCFFunctionCount);
849 W.printHex("GuardFlags", Conf->GuardFlags);
851 Tables.GuardFidTableVA = Conf->GuardCFFunctionTable;
852 Tables.GuardFidTableCount = Conf->GuardCFFunctionCount;
853 Tables.GuardFlags = Conf->GuardFlags;
855 // Print the rest. (2017)
856 if (Conf->Size < sizeof(T))
858 W.printHex("GuardAddressTakenIatEntryTable",
859 Conf->GuardAddressTakenIatEntryTable);
860 W.printNumber("GuardAddressTakenIatEntryCount",
861 Conf->GuardAddressTakenIatEntryCount);
862 W.printHex("GuardLongJumpTargetTable", Conf->GuardLongJumpTargetTable);
863 W.printNumber("GuardLongJumpTargetCount", Conf->GuardLongJumpTargetCount);
864 W.printHex("DynamicValueRelocTable", Conf->DynamicValueRelocTable);
865 W.printHex("CHPEMetadataPointer", Conf->CHPEMetadataPointer);
866 W.printHex("GuardRFFailureRoutine", Conf->GuardRFFailureRoutine);
867 W.printHex("GuardRFFailureRoutineFunctionPointer",
868 Conf->GuardRFFailureRoutineFunctionPointer);
869 W.printHex("DynamicValueRelocTableOffset",
870 Conf->DynamicValueRelocTableOffset);
871 W.printNumber("DynamicValueRelocTableSection",
872 Conf->DynamicValueRelocTableSection);
873 W.printHex("GuardRFVerifyStackPointerFunctionPointer",
874 Conf->GuardRFVerifyStackPointerFunctionPointer);
875 W.printHex("HotPatchTableOffset", Conf->HotPatchTableOffset);
877 Tables.GuardLJmpTableVA = Conf->GuardLongJumpTargetTable;
878 Tables.GuardLJmpTableCount = Conf->GuardLongJumpTargetCount;
881 void COFFDumper::printBaseOfDataField(const pe32_header *Hdr) {
882 W.printHex("BaseOfData", Hdr->BaseOfData);
885 void COFFDumper::printBaseOfDataField(const pe32plus_header *) {}
887 void COFFDumper::printCodeViewDebugInfo() {
888 // Print types first to build CVUDTNames, then print symbols.
889 for (const SectionRef &S : Obj->sections()) {
890 StringRef SectionName;
891 error(S.getName(SectionName));
892 // .debug$T is a standard CodeView type section, while .debug$P is the same
893 // format but used for MSVC precompiled header object files.
894 if (SectionName == ".debug$T" || SectionName == ".debug$P")
895 printCodeViewTypeSection(SectionName, S);
897 for (const SectionRef &S : Obj->sections()) {
898 StringRef SectionName;
899 error(S.getName(SectionName));
900 if (SectionName == ".debug$S")
901 printCodeViewSymbolSection(SectionName, S);
905 void COFFDumper::initializeFileAndStringTables(BinaryStreamReader &Reader) {
906 while (Reader.bytesRemaining() > 0 &&
907 (!CVFileChecksumTable.valid() || !CVStringTable.valid())) {
908 // The section consists of a number of subsection in the following format:
909 // |SubSectionType|SubSectionSize|Contents...|
910 uint32_t SubType, SubSectionSize;
911 error(Reader.readInteger(SubType));
912 error(Reader.readInteger(SubSectionSize));
915 error(Reader.readFixedString(Contents, SubSectionSize));
917 BinaryStreamRef ST(Contents, support::little);
918 switch (DebugSubsectionKind(SubType)) {
919 case DebugSubsectionKind::FileChecksums:
920 error(CVFileChecksumTable.initialize(ST));
922 case DebugSubsectionKind::StringTable:
923 error(CVStringTable.initialize(ST));
929 uint32_t PaddedSize = alignTo(SubSectionSize, 4);
930 error(Reader.skip(PaddedSize - SubSectionSize));
934 void COFFDumper::printCodeViewSymbolSection(StringRef SectionName,
935 const SectionRef &Section) {
936 StringRef SectionContents = unwrapOrError(Section.getContents());
937 StringRef Data = SectionContents;
939 SmallVector<StringRef, 10> FunctionNames;
940 StringMap<StringRef> FunctionLineTables;
942 ListScope D(W, "CodeViewDebugInfo");
943 // Print the section to allow correlation with printSectionHeaders.
944 W.printNumber("Section", SectionName, Obj->getSectionID(Section));
947 error(consume(Data, Magic));
948 W.printHex("Magic", Magic);
949 if (Magic != COFF::DEBUG_SECTION_MAGIC)
950 return error(object_error::parse_failed);
952 BinaryStreamReader FSReader(Data, support::little);
953 initializeFileAndStringTables(FSReader);
955 // TODO: Convert this over to using ModuleSubstreamVisitor.
956 while (!Data.empty()) {
957 // The section consists of a number of subsection in the following format:
958 // |SubSectionType|SubSectionSize|Contents...|
959 uint32_t SubType, SubSectionSize;
960 error(consume(Data, SubType));
961 error(consume(Data, SubSectionSize));
963 ListScope S(W, "Subsection");
964 // Dump the subsection as normal even if the ignore bit is set.
965 if (SubType & SubsectionIgnoreFlag) {
966 W.printHex("IgnoredSubsectionKind", SubType);
967 SubType &= ~SubsectionIgnoreFlag;
969 W.printEnum("SubSectionType", SubType, makeArrayRef(SubSectionTypes));
970 W.printHex("SubSectionSize", SubSectionSize);
972 // Get the contents of the subsection.
973 if (SubSectionSize > Data.size())
974 return error(object_error::parse_failed);
975 StringRef Contents = Data.substr(0, SubSectionSize);
977 // Add SubSectionSize to the current offset and align that offset to find
978 // the next subsection.
979 size_t SectionOffset = Data.data() - SectionContents.data();
980 size_t NextOffset = SectionOffset + SubSectionSize;
981 NextOffset = alignTo(NextOffset, 4);
982 if (NextOffset > SectionContents.size())
983 return error(object_error::parse_failed);
984 Data = SectionContents.drop_front(NextOffset);
986 // Optionally print the subsection bytes in case our parsing gets confused
988 if (opts::CodeViewSubsectionBytes)
989 printBinaryBlockWithRelocs("SubSectionContents", Section, SectionContents,
992 switch (DebugSubsectionKind(SubType)) {
993 case DebugSubsectionKind::Symbols:
994 printCodeViewSymbolsSubsection(Contents, Section, SectionContents);
997 case DebugSubsectionKind::InlineeLines:
998 printCodeViewInlineeLines(Contents);
1001 case DebugSubsectionKind::FileChecksums:
1002 printCodeViewFileChecksums(Contents);
1005 case DebugSubsectionKind::Lines: {
1006 // Holds a PC to file:line table. Some data to parse this subsection is
1007 // stored in the other subsections, so just check sanity and store the
1008 // pointers for deferred processing.
1010 if (SubSectionSize < 12) {
1011 // There should be at least three words to store two function
1012 // relocations and size of the code.
1013 error(object_error::parse_failed);
1017 StringRef LinkageName;
1018 error(resolveSymbolName(Obj->getCOFFSection(Section), SectionOffset,
1020 W.printString("LinkageName", LinkageName);
1021 if (FunctionLineTables.count(LinkageName) != 0) {
1022 // Saw debug info for this function already?
1023 error(object_error::parse_failed);
1027 FunctionLineTables[LinkageName] = Contents;
1028 FunctionNames.push_back(LinkageName);
1031 case DebugSubsectionKind::FrameData: {
1032 // First four bytes is a relocation against the function.
1033 BinaryStreamReader SR(Contents, llvm::support::little);
1035 DebugFrameDataSubsectionRef FrameData;
1036 error(FrameData.initialize(SR));
1038 StringRef LinkageName;
1039 error(resolveSymbolName(Obj->getCOFFSection(Section), SectionContents,
1040 FrameData.getRelocPtr(), LinkageName));
1041 W.printString("LinkageName", LinkageName);
1043 // To find the active frame description, search this array for the
1044 // smallest PC range that includes the current PC.
1045 for (const auto &FD : FrameData) {
1046 StringRef FrameFunc = error(CVStringTable.getString(FD.FrameFunc));
1048 DictScope S(W, "FrameData");
1049 W.printHex("RvaStart", FD.RvaStart);
1050 W.printHex("CodeSize", FD.CodeSize);
1051 W.printHex("LocalSize", FD.LocalSize);
1052 W.printHex("ParamsSize", FD.ParamsSize);
1053 W.printHex("MaxStackSize", FD.MaxStackSize);
1054 W.printHex("PrologSize", FD.PrologSize);
1055 W.printHex("SavedRegsSize", FD.SavedRegsSize);
1056 W.printFlags("Flags", FD.Flags, makeArrayRef(FrameDataFlags));
1058 // The FrameFunc string is a small RPN program. It can be broken up into
1059 // statements that end in the '=' operator, which assigns the value on
1060 // the top of the stack to the previously pushed variable. Variables can
1061 // be temporary values ($T0) or physical registers ($esp). Print each
1062 // assignment on its own line to make these programs easier to read.
1064 ListScope FFS(W, "FrameFunc");
1065 while (!FrameFunc.empty()) {
1066 size_t EqOrEnd = FrameFunc.find('=');
1067 if (EqOrEnd == StringRef::npos)
1068 EqOrEnd = FrameFunc.size();
1071 StringRef Stmt = FrameFunc.substr(0, EqOrEnd);
1072 W.printString(Stmt);
1073 FrameFunc = FrameFunc.drop_front(EqOrEnd).trim();
1080 // Do nothing for unrecognized subsections.
1087 // Dump the line tables now that we've read all the subsections and know all
1088 // the required information.
1089 for (unsigned I = 0, E = FunctionNames.size(); I != E; ++I) {
1090 StringRef Name = FunctionNames[I];
1091 ListScope S(W, "FunctionLineTable");
1092 W.printString("LinkageName", Name);
1094 BinaryStreamReader Reader(FunctionLineTables[Name], support::little);
1096 DebugLinesSubsectionRef LineInfo;
1097 error(LineInfo.initialize(Reader));
1099 W.printHex("Flags", LineInfo.header()->Flags);
1100 W.printHex("CodeSize", LineInfo.header()->CodeSize);
1101 for (const auto &Entry : LineInfo) {
1103 ListScope S(W, "FilenameSegment");
1104 printFileNameForOffset("Filename", Entry.NameIndex);
1105 uint32_t ColumnIndex = 0;
1106 for (const auto &Line : Entry.LineNumbers) {
1107 if (Line.Offset >= LineInfo.header()->CodeSize) {
1108 error(object_error::parse_failed);
1112 std::string PC = formatv("+{0:X}", uint32_t(Line.Offset));
1113 ListScope PCScope(W, PC);
1114 codeview::LineInfo LI(Line.Flags);
1116 if (LI.isAlwaysStepInto())
1117 W.printString("StepInto", StringRef("Always"));
1118 else if (LI.isNeverStepInto())
1119 W.printString("StepInto", StringRef("Never"));
1121 W.printNumber("LineNumberStart", LI.getStartLine());
1122 W.printNumber("LineNumberEndDelta", LI.getLineDelta());
1123 W.printBoolean("IsStatement", LI.isStatement());
1124 if (LineInfo.hasColumnInfo()) {
1125 W.printNumber("ColStart", Entry.Columns[ColumnIndex].StartColumn);
1126 W.printNumber("ColEnd", Entry.Columns[ColumnIndex].EndColumn);
1134 void COFFDumper::printCodeViewSymbolsSubsection(StringRef Subsection,
1135 const SectionRef &Section,
1136 StringRef SectionContents) {
1137 ArrayRef<uint8_t> BinaryData(Subsection.bytes_begin(),
1138 Subsection.bytes_end());
1139 auto CODD = llvm::make_unique<COFFObjectDumpDelegate>(*this, Section, Obj,
1141 CVSymbolDumper CVSD(W, Types, CodeViewContainer::ObjectFile, std::move(CODD),
1142 CompilationCPUType, opts::CodeViewSubsectionBytes);
1143 CVSymbolArray Symbols;
1144 BinaryStreamReader Reader(BinaryData, llvm::support::little);
1145 if (auto EC = Reader.readArray(Symbols, Reader.getLength())) {
1146 consumeError(std::move(EC));
1148 error(object_error::parse_failed);
1151 if (auto EC = CVSD.dump(Symbols)) {
1153 error(std::move(EC));
1155 CompilationCPUType = CVSD.getCompilationCPUType();
1159 void COFFDumper::printCodeViewFileChecksums(StringRef Subsection) {
1160 BinaryStreamRef Stream(Subsection, llvm::support::little);
1161 DebugChecksumsSubsectionRef Checksums;
1162 error(Checksums.initialize(Stream));
1164 for (auto &FC : Checksums) {
1165 DictScope S(W, "FileChecksum");
1167 StringRef Filename = error(CVStringTable.getString(FC.FileNameOffset));
1168 W.printHex("Filename", Filename, FC.FileNameOffset);
1169 W.printHex("ChecksumSize", FC.Checksum.size());
1170 W.printEnum("ChecksumKind", uint8_t(FC.Kind),
1171 makeArrayRef(FileChecksumKindNames));
1173 W.printBinary("ChecksumBytes", FC.Checksum);
1177 void COFFDumper::printCodeViewInlineeLines(StringRef Subsection) {
1178 BinaryStreamReader SR(Subsection, llvm::support::little);
1179 DebugInlineeLinesSubsectionRef Lines;
1180 error(Lines.initialize(SR));
1182 for (auto &Line : Lines) {
1183 DictScope S(W, "InlineeSourceLine");
1184 printTypeIndex("Inlinee", Line.Header->Inlinee);
1185 printFileNameForOffset("FileID", Line.Header->FileID);
1186 W.printNumber("SourceLineNum", Line.Header->SourceLineNum);
1188 if (Lines.hasExtraFiles()) {
1189 W.printNumber("ExtraFileCount", Line.ExtraFiles.size());
1190 ListScope ExtraFiles(W, "ExtraFiles");
1191 for (const auto &FID : Line.ExtraFiles) {
1192 printFileNameForOffset("FileID", FID);
1198 StringRef COFFDumper::getFileNameForFileOffset(uint32_t FileOffset) {
1199 // The file checksum subsection should precede all references to it.
1200 if (!CVFileChecksumTable.valid() || !CVStringTable.valid())
1201 error(object_error::parse_failed);
1203 auto Iter = CVFileChecksumTable.getArray().at(FileOffset);
1205 // Check if the file checksum table offset is valid.
1206 if (Iter == CVFileChecksumTable.end())
1207 error(object_error::parse_failed);
1209 return error(CVStringTable.getString(Iter->FileNameOffset));
1212 void COFFDumper::printFileNameForOffset(StringRef Label, uint32_t FileOffset) {
1213 W.printHex(Label, getFileNameForFileOffset(FileOffset), FileOffset);
1216 void COFFDumper::mergeCodeViewTypes(MergingTypeTableBuilder &CVIDs,
1217 MergingTypeTableBuilder &CVTypes,
1218 GlobalTypeTableBuilder &GlobalCVIDs,
1219 GlobalTypeTableBuilder &GlobalCVTypes,
1221 for (const SectionRef &S : Obj->sections()) {
1222 StringRef SectionName;
1223 error(S.getName(SectionName));
1224 if (SectionName == ".debug$T") {
1225 StringRef Data = unwrapOrError(S.getContents());
1227 error(consume(Data, Magic));
1229 error(object_error::parse_failed);
1232 BinaryStreamReader Reader(Data, llvm::support::little);
1233 if (auto EC = Reader.readArray(Types, Reader.getLength())) {
1234 consumeError(std::move(EC));
1236 error(object_error::parse_failed);
1238 SmallVector<TypeIndex, 128> SourceToDest;
1239 Optional<uint32_t> PCHSignature;
1241 std::vector<GloballyHashedType> Hashes =
1242 GloballyHashedType::hashTypes(Types);
1244 mergeTypeAndIdRecords(GlobalCVIDs, GlobalCVTypes, SourceToDest,
1245 Types, Hashes, PCHSignature))
1246 return error(std::move(EC));
1248 if (auto EC = mergeTypeAndIdRecords(CVIDs, CVTypes, SourceToDest, Types,
1250 return error(std::move(EC));
1256 void COFFDumper::printCodeViewTypeSection(StringRef SectionName,
1257 const SectionRef &Section) {
1258 ListScope D(W, "CodeViewTypes");
1259 W.printNumber("Section", SectionName, Obj->getSectionID(Section));
1261 StringRef Data = unwrapOrError(Section.getContents());
1262 if (opts::CodeViewSubsectionBytes)
1263 W.printBinaryBlock("Data", Data);
1266 error(consume(Data, Magic));
1267 W.printHex("Magic", Magic);
1268 if (Magic != COFF::DEBUG_SECTION_MAGIC)
1269 return error(object_error::parse_failed);
1271 Types.reset(Data, 100);
1273 TypeDumpVisitor TDV(Types, &W, opts::CodeViewSubsectionBytes);
1274 error(codeview::visitTypeStream(Types, TDV));
1278 void COFFDumper::printSectionHeaders() {
1279 ListScope SectionsD(W, "Sections");
1280 int SectionNumber = 0;
1281 for (const SectionRef &Sec : Obj->sections()) {
1283 const coff_section *Section = Obj->getCOFFSection(Sec);
1286 error(Sec.getName(Name));
1288 DictScope D(W, "Section");
1289 W.printNumber("Number", SectionNumber);
1290 W.printBinary("Name", Name, Section->Name);
1291 W.printHex ("VirtualSize", Section->VirtualSize);
1292 W.printHex ("VirtualAddress", Section->VirtualAddress);
1293 W.printNumber("RawDataSize", Section->SizeOfRawData);
1294 W.printHex ("PointerToRawData", Section->PointerToRawData);
1295 W.printHex ("PointerToRelocations", Section->PointerToRelocations);
1296 W.printHex ("PointerToLineNumbers", Section->PointerToLinenumbers);
1297 W.printNumber("RelocationCount", Section->NumberOfRelocations);
1298 W.printNumber("LineNumberCount", Section->NumberOfLinenumbers);
1299 W.printFlags ("Characteristics", Section->Characteristics,
1300 makeArrayRef(ImageSectionCharacteristics),
1301 COFF::SectionCharacteristics(0x00F00000));
1303 if (opts::SectionRelocations) {
1304 ListScope D(W, "Relocations");
1305 for (const RelocationRef &Reloc : Sec.relocations())
1306 printRelocation(Sec, Reloc);
1309 if (opts::SectionSymbols) {
1310 ListScope D(W, "Symbols");
1311 for (const SymbolRef &Symbol : Obj->symbols()) {
1312 if (!Sec.containsSymbol(Symbol))
1315 printSymbol(Symbol);
1319 if (opts::SectionData &&
1320 !(Section->Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA)) {
1321 StringRef Data = unwrapOrError(Sec.getContents());
1322 W.printBinaryBlock("SectionData", Data);
1327 void COFFDumper::printRelocations() {
1328 ListScope D(W, "Relocations");
1330 int SectionNumber = 0;
1331 for (const SectionRef &Section : Obj->sections()) {
1334 error(Section.getName(Name));
1336 bool PrintedGroup = false;
1337 for (const RelocationRef &Reloc : Section.relocations()) {
1338 if (!PrintedGroup) {
1339 W.startLine() << "Section (" << SectionNumber << ") " << Name << " {\n";
1341 PrintedGroup = true;
1344 printRelocation(Section, Reloc);
1349 W.startLine() << "}\n";
1354 void COFFDumper::printRelocation(const SectionRef &Section,
1355 const RelocationRef &Reloc, uint64_t Bias) {
1356 uint64_t Offset = Reloc.getOffset() - Bias;
1357 uint64_t RelocType = Reloc.getType();
1358 SmallString<32> RelocName;
1359 StringRef SymbolName;
1360 Reloc.getTypeName(RelocName);
1361 symbol_iterator Symbol = Reloc.getSymbol();
1362 int64_t SymbolIndex = -1;
1363 if (Symbol != Obj->symbol_end()) {
1364 Expected<StringRef> SymbolNameOrErr = Symbol->getName();
1365 error(errorToErrorCode(SymbolNameOrErr.takeError()));
1366 SymbolName = *SymbolNameOrErr;
1367 SymbolIndex = Obj->getSymbolIndex(Obj->getCOFFSymbol(*Symbol));
1370 if (opts::ExpandRelocs) {
1371 DictScope Group(W, "Relocation");
1372 W.printHex("Offset", Offset);
1373 W.printNumber("Type", RelocName, RelocType);
1374 W.printString("Symbol", SymbolName.empty() ? "-" : SymbolName);
1375 W.printNumber("SymbolIndex", SymbolIndex);
1377 raw_ostream& OS = W.startLine();
1380 << " " << (SymbolName.empty() ? "-" : SymbolName)
1381 << " (" << SymbolIndex << ")"
1386 void COFFDumper::printSymbols() {
1387 ListScope Group(W, "Symbols");
1389 for (const SymbolRef &Symbol : Obj->symbols())
1390 printSymbol(Symbol);
1393 void COFFDumper::printDynamicSymbols() { ListScope Group(W, "DynamicSymbols"); }
1395 static Expected<StringRef>
1396 getSectionName(const llvm::object::COFFObjectFile *Obj, int32_t SectionNumber,
1397 const coff_section *Section) {
1399 return Obj->getSectionName(Section);
1400 if (SectionNumber == llvm::COFF::IMAGE_SYM_DEBUG)
1401 return StringRef("IMAGE_SYM_DEBUG");
1402 if (SectionNumber == llvm::COFF::IMAGE_SYM_ABSOLUTE)
1403 return StringRef("IMAGE_SYM_ABSOLUTE");
1404 if (SectionNumber == llvm::COFF::IMAGE_SYM_UNDEFINED)
1405 return StringRef("IMAGE_SYM_UNDEFINED");
1406 return StringRef("");
1409 void COFFDumper::printSymbol(const SymbolRef &Sym) {
1410 DictScope D(W, "Symbol");
1412 COFFSymbolRef Symbol = Obj->getCOFFSymbol(Sym);
1413 const coff_section *Section;
1414 if (std::error_code EC = Obj->getSection(Symbol.getSectionNumber(), Section)) {
1415 W.startLine() << "Invalid section number: " << EC.message() << "\n";
1420 StringRef SymbolName;
1421 if (Obj->getSymbolName(Symbol, SymbolName))
1424 StringRef SectionName;
1425 if (Expected<StringRef> NameOrErr =
1426 getSectionName(Obj, Symbol.getSectionNumber(), Section))
1427 SectionName = *NameOrErr;
1429 W.printString("Name", SymbolName);
1430 W.printNumber("Value", Symbol.getValue());
1431 W.printNumber("Section", SectionName, Symbol.getSectionNumber());
1432 W.printEnum ("BaseType", Symbol.getBaseType(), makeArrayRef(ImageSymType));
1433 W.printEnum ("ComplexType", Symbol.getComplexType(),
1434 makeArrayRef(ImageSymDType));
1435 W.printEnum ("StorageClass", Symbol.getStorageClass(),
1436 makeArrayRef(ImageSymClass));
1437 W.printNumber("AuxSymbolCount", Symbol.getNumberOfAuxSymbols());
1439 for (uint8_t I = 0; I < Symbol.getNumberOfAuxSymbols(); ++I) {
1440 if (Symbol.isFunctionDefinition()) {
1441 const coff_aux_function_definition *Aux;
1442 error(getSymbolAuxData(Obj, Symbol, I, Aux));
1444 DictScope AS(W, "AuxFunctionDef");
1445 W.printNumber("TagIndex", Aux->TagIndex);
1446 W.printNumber("TotalSize", Aux->TotalSize);
1447 W.printHex("PointerToLineNumber", Aux->PointerToLinenumber);
1448 W.printHex("PointerToNextFunction", Aux->PointerToNextFunction);
1450 } else if (Symbol.isAnyUndefined()) {
1451 const coff_aux_weak_external *Aux;
1452 error(getSymbolAuxData(Obj, Symbol, I, Aux));
1454 Expected<COFFSymbolRef> Linked = Obj->getSymbol(Aux->TagIndex);
1455 StringRef LinkedName;
1456 std::error_code EC = errorToErrorCode(Linked.takeError());
1457 if (EC || (EC = Obj->getSymbolName(*Linked, LinkedName))) {
1462 DictScope AS(W, "AuxWeakExternal");
1463 W.printNumber("Linked", LinkedName, Aux->TagIndex);
1464 W.printEnum ("Search", Aux->Characteristics,
1465 makeArrayRef(WeakExternalCharacteristics));
1467 } else if (Symbol.isFileRecord()) {
1468 const char *FileName;
1469 error(getSymbolAuxData(Obj, Symbol, I, FileName));
1471 DictScope AS(W, "AuxFileRecord");
1473 StringRef Name(FileName, Symbol.getNumberOfAuxSymbols() *
1474 Obj->getSymbolTableEntrySize());
1475 W.printString("FileName", Name.rtrim(StringRef("\0", 1)));
1477 } else if (Symbol.isSectionDefinition()) {
1478 const coff_aux_section_definition *Aux;
1479 error(getSymbolAuxData(Obj, Symbol, I, Aux));
1481 int32_t AuxNumber = Aux->getNumber(Symbol.isBigObj());
1483 DictScope AS(W, "AuxSectionDef");
1484 W.printNumber("Length", Aux->Length);
1485 W.printNumber("RelocationCount", Aux->NumberOfRelocations);
1486 W.printNumber("LineNumberCount", Aux->NumberOfLinenumbers);
1487 W.printHex("Checksum", Aux->CheckSum);
1488 W.printNumber("Number", AuxNumber);
1489 W.printEnum("Selection", Aux->Selection, makeArrayRef(ImageCOMDATSelect));
1491 if (Section && Section->Characteristics & COFF::IMAGE_SCN_LNK_COMDAT
1492 && Aux->Selection == COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE) {
1493 const coff_section *Assoc;
1494 StringRef AssocName = "";
1495 if (std::error_code EC = Obj->getSection(AuxNumber, Assoc))
1497 Expected<StringRef> Res = getSectionName(Obj, AuxNumber, Assoc);
1499 error(Res.takeError());
1502 W.printNumber("AssocSection", AssocName, AuxNumber);
1504 } else if (Symbol.isCLRToken()) {
1505 const coff_aux_clr_token *Aux;
1506 error(getSymbolAuxData(Obj, Symbol, I, Aux));
1508 Expected<COFFSymbolRef> ReferredSym =
1509 Obj->getSymbol(Aux->SymbolTableIndex);
1510 StringRef ReferredName;
1511 std::error_code EC = errorToErrorCode(ReferredSym.takeError());
1512 if (EC || (EC = Obj->getSymbolName(*ReferredSym, ReferredName))) {
1517 DictScope AS(W, "AuxCLRToken");
1518 W.printNumber("AuxType", Aux->AuxType);
1519 W.printNumber("Reserved", Aux->Reserved);
1520 W.printNumber("SymbolTableIndex", ReferredName, Aux->SymbolTableIndex);
1523 W.startLine() << "<unhandled auxiliary record>\n";
1528 void COFFDumper::printUnwindInfo() {
1529 ListScope D(W, "UnwindInformation");
1530 switch (Obj->getMachine()) {
1531 case COFF::IMAGE_FILE_MACHINE_AMD64: {
1532 Win64EH::Dumper Dumper(W);
1533 Win64EH::Dumper::SymbolResolver
1534 Resolver = [](const object::coff_section *Section, uint64_t Offset,
1535 SymbolRef &Symbol, void *user_data) -> std::error_code {
1536 COFFDumper *Dumper = reinterpret_cast<COFFDumper *>(user_data);
1537 return Dumper->resolveSymbol(Section, Offset, Symbol);
1539 Win64EH::Dumper::Context Ctx(*Obj, Resolver, this);
1540 Dumper.printData(Ctx);
1543 case COFF::IMAGE_FILE_MACHINE_ARM64:
1544 case COFF::IMAGE_FILE_MACHINE_ARMNT: {
1545 ARM::WinEH::Decoder Decoder(W, Obj->getMachine() ==
1546 COFF::IMAGE_FILE_MACHINE_ARM64);
1547 // TODO Propagate the error.
1548 consumeError(Decoder.dumpProcedureData(*Obj));
1552 W.printEnum("unsupported Image Machine", Obj->getMachine(),
1553 makeArrayRef(ImageFileMachineType));
1558 void COFFDumper::printNeededLibraries() {
1559 ListScope D(W, "NeededLibraries");
1561 using LibsTy = std::vector<StringRef>;
1564 for (const ImportDirectoryEntryRef &DirRef : Obj->import_directories()) {
1566 if (!DirRef.getName(Name))
1567 Libs.push_back(Name);
1570 llvm::stable_sort(Libs);
1572 for (const auto &L : Libs) {
1573 W.startLine() << L << "\n";
1577 void COFFDumper::printImportedSymbols(
1578 iterator_range<imported_symbol_iterator> Range) {
1579 for (const ImportedSymbolRef &I : Range) {
1581 error(I.getSymbolName(Sym));
1583 error(I.getOrdinal(Ordinal));
1584 W.printNumber("Symbol", Sym, Ordinal);
1588 void COFFDumper::printDelayImportedSymbols(
1589 const DelayImportDirectoryEntryRef &I,
1590 iterator_range<imported_symbol_iterator> Range) {
1592 for (const ImportedSymbolRef &S : Range) {
1593 DictScope Import(W, "Import");
1595 error(S.getSymbolName(Sym));
1597 error(S.getOrdinal(Ordinal));
1598 W.printNumber("Symbol", Sym, Ordinal);
1600 error(I.getImportAddress(Index++, Addr));
1601 W.printHex("Address", Addr);
1605 void COFFDumper::printCOFFImports() {
1607 for (const ImportDirectoryEntryRef &I : Obj->import_directories()) {
1608 DictScope Import(W, "Import");
1610 error(I.getName(Name));
1611 W.printString("Name", Name);
1613 error(I.getImportLookupTableRVA(ILTAddr));
1614 W.printHex("ImportLookupTableRVA", ILTAddr);
1616 error(I.getImportAddressTableRVA(IATAddr));
1617 W.printHex("ImportAddressTableRVA", IATAddr);
1618 // The import lookup table can be missing with certain older linkers, so
1619 // fall back to the import address table in that case.
1621 printImportedSymbols(I.lookup_table_symbols());
1623 printImportedSymbols(I.imported_symbols());
1627 for (const DelayImportDirectoryEntryRef &I : Obj->delay_import_directories()) {
1628 DictScope Import(W, "DelayImport");
1630 error(I.getName(Name));
1631 W.printString("Name", Name);
1632 const delay_import_directory_table_entry *Table;
1633 error(I.getDelayImportTable(Table));
1634 W.printHex("Attributes", Table->Attributes);
1635 W.printHex("ModuleHandle", Table->ModuleHandle);
1636 W.printHex("ImportAddressTable", Table->DelayImportAddressTable);
1637 W.printHex("ImportNameTable", Table->DelayImportNameTable);
1638 W.printHex("BoundDelayImportTable", Table->BoundDelayImportTable);
1639 W.printHex("UnloadDelayImportTable", Table->UnloadDelayImportTable);
1640 printDelayImportedSymbols(I, I.imported_symbols());
1644 void COFFDumper::printCOFFExports() {
1645 for (const ExportDirectoryEntryRef &E : Obj->export_directories()) {
1646 DictScope Export(W, "Export");
1649 uint32_t Ordinal, RVA;
1651 error(E.getSymbolName(Name));
1652 error(E.getOrdinal(Ordinal));
1653 error(E.getExportRVA(RVA));
1655 W.printNumber("Ordinal", Ordinal);
1656 W.printString("Name", Name);
1657 W.printHex("RVA", RVA);
1661 void COFFDumper::printCOFFDirectives() {
1662 for (const SectionRef &Section : Obj->sections()) {
1665 error(Section.getName(Name));
1666 if (Name != ".drectve")
1669 StringRef Contents = unwrapOrError(Section.getContents());
1670 W.printString("Directive(s)", Contents);
1674 static std::string getBaseRelocTypeName(uint8_t Type) {
1676 case COFF::IMAGE_REL_BASED_ABSOLUTE: return "ABSOLUTE";
1677 case COFF::IMAGE_REL_BASED_HIGH: return "HIGH";
1678 case COFF::IMAGE_REL_BASED_LOW: return "LOW";
1679 case COFF::IMAGE_REL_BASED_HIGHLOW: return "HIGHLOW";
1680 case COFF::IMAGE_REL_BASED_HIGHADJ: return "HIGHADJ";
1681 case COFF::IMAGE_REL_BASED_ARM_MOV32T: return "ARM_MOV32(T)";
1682 case COFF::IMAGE_REL_BASED_DIR64: return "DIR64";
1683 default: return "unknown (" + llvm::utostr(Type) + ")";
1687 void COFFDumper::printCOFFBaseReloc() {
1688 ListScope D(W, "BaseReloc");
1689 for (const BaseRelocRef &I : Obj->base_relocs()) {
1692 error(I.getRVA(RVA));
1693 error(I.getType(Type));
1694 DictScope Import(W, "Entry");
1695 W.printString("Type", getBaseRelocTypeName(Type));
1696 W.printHex("Address", RVA);
1700 void COFFDumper::printCOFFResources() {
1701 ListScope ResourcesD(W, "Resources");
1702 for (const SectionRef &S : Obj->sections()) {
1704 error(S.getName(Name));
1705 if (!Name.startswith(".rsrc"))
1708 StringRef Ref = unwrapOrError(S.getContents());
1710 if ((Name == ".rsrc") || (Name == ".rsrc$01")) {
1711 ResourceSectionRef RSF(Ref);
1712 auto &BaseTable = unwrapOrError(RSF.getBaseTable());
1713 W.printNumber("Total Number of Resources",
1714 countTotalTableEntries(RSF, BaseTable, "Type"));
1715 W.printHex("Base Table Address",
1716 Obj->getCOFFSection(S)->PointerToRawData);
1717 W.startLine() << "\n";
1718 printResourceDirectoryTable(RSF, BaseTable, "Type");
1720 if (opts::SectionData)
1721 W.printBinaryBlock(Name.str() + " Data", Ref);
1726 COFFDumper::countTotalTableEntries(ResourceSectionRef RSF,
1727 const coff_resource_dir_table &Table,
1729 uint32_t TotalEntries = 0;
1730 for (int i = 0; i < Table.NumberOfNameEntries + Table.NumberOfIDEntries;
1732 auto Entry = unwrapOrError(getResourceDirectoryTableEntry(Table, i));
1733 if (Entry.Offset.isSubDir()) {
1734 StringRef NextLevel;
1735 if (Level == "Name")
1736 NextLevel = "Language";
1739 auto &NextTable = unwrapOrError(RSF.getEntrySubDir(Entry));
1740 TotalEntries += countTotalTableEntries(RSF, NextTable, NextLevel);
1745 return TotalEntries;
1748 void COFFDumper::printResourceDirectoryTable(
1749 ResourceSectionRef RSF, const coff_resource_dir_table &Table,
1752 W.printNumber("Number of String Entries", Table.NumberOfNameEntries);
1753 W.printNumber("Number of ID Entries", Table.NumberOfIDEntries);
1755 // Iterate through level in resource directory tree.
1756 for (int i = 0; i < Table.NumberOfNameEntries + Table.NumberOfIDEntries;
1758 auto Entry = unwrapOrError(getResourceDirectoryTableEntry(Table, i));
1760 SmallString<20> IDStr;
1761 raw_svector_ostream OS(IDStr);
1762 if (i < Table.NumberOfNameEntries) {
1763 ArrayRef<UTF16> RawEntryNameString =
1764 unwrapOrError(RSF.getEntryNameString(Entry));
1765 std::vector<UTF16> EndianCorrectedNameString;
1766 if (llvm::sys::IsBigEndianHost) {
1767 EndianCorrectedNameString.resize(RawEntryNameString.size() + 1);
1768 std::copy(RawEntryNameString.begin(), RawEntryNameString.end(),
1769 EndianCorrectedNameString.begin() + 1);
1770 EndianCorrectedNameString[0] = UNI_UTF16_BYTE_ORDER_MARK_SWAPPED;
1771 RawEntryNameString = makeArrayRef(EndianCorrectedNameString);
1773 std::string EntryNameString;
1774 if (!llvm::convertUTF16ToUTF8String(RawEntryNameString, EntryNameString))
1775 error(object_error::parse_failed);
1777 OS << EntryNameString;
1779 if (Level == "Type") {
1781 printResourceTypeName(Entry.Identifier.ID, OS);
1782 IDStr = IDStr.slice(0, IDStr.find_first_of(")", 0) + 1);
1784 OS << ": (ID " << Entry.Identifier.ID << ")";
1787 Name = StringRef(IDStr);
1788 ListScope ResourceType(W, Level.str() + Name.str());
1789 if (Entry.Offset.isSubDir()) {
1790 W.printHex("Table Offset", Entry.Offset.value());
1791 StringRef NextLevel;
1792 if (Level == "Name")
1793 NextLevel = "Language";
1796 auto &NextTable = unwrapOrError(RSF.getEntrySubDir(Entry));
1797 printResourceDirectoryTable(RSF, NextTable, NextLevel);
1799 W.printHex("Entry Offset", Entry.Offset.value());
1800 char FormattedTime[20] = {};
1801 time_t TDS = time_t(Table.TimeDateStamp);
1802 strftime(FormattedTime, 20, "%Y-%m-%d %H:%M:%S", gmtime(&TDS));
1803 W.printHex("Time/Date Stamp", FormattedTime, Table.TimeDateStamp);
1804 W.printNumber("Major Version", Table.MajorVersion);
1805 W.printNumber("Minor Version", Table.MinorVersion);
1806 W.printNumber("Characteristics", Table.Characteristics);
1811 ErrorOr<const coff_resource_dir_entry &>
1812 COFFDumper::getResourceDirectoryTableEntry(const coff_resource_dir_table &Table,
1814 if (Index >= (uint32_t)(Table.NumberOfNameEntries + Table.NumberOfIDEntries))
1815 return object_error::parse_failed;
1816 auto TablePtr = reinterpret_cast<const coff_resource_dir_entry *>(&Table + 1);
1817 return TablePtr[Index];
1820 void COFFDumper::printStackMap() const {
1821 object::SectionRef StackMapSection;
1822 for (auto Sec : Obj->sections()) {
1825 if (Name == ".llvm_stackmaps") {
1826 StackMapSection = Sec;
1831 if (StackMapSection == object::SectionRef())
1834 StringRef StackMapContents = unwrapOrError(StackMapSection.getContents());
1835 ArrayRef<uint8_t> StackMapContentsArray =
1836 arrayRefFromStringRef(StackMapContents);
1838 if (Obj->isLittleEndian())
1839 prettyPrintStackMap(
1840 W, StackMapParser<support::little>(StackMapContentsArray));
1842 prettyPrintStackMap(
1843 W, StackMapParser<support::big>(StackMapContentsArray));
1846 void COFFDumper::printAddrsig() {
1847 object::SectionRef AddrsigSection;
1848 for (auto Sec : Obj->sections()) {
1851 if (Name == ".llvm_addrsig") {
1852 AddrsigSection = Sec;
1857 if (AddrsigSection == object::SectionRef())
1860 StringRef AddrsigContents = unwrapOrError(AddrsigSection.getContents());
1861 ArrayRef<uint8_t> AddrsigContentsArray(AddrsigContents.bytes_begin(),
1862 AddrsigContents.size());
1864 ListScope L(W, "Addrsig");
1865 const uint8_t *Cur = AddrsigContents.bytes_begin();
1866 const uint8_t *End = AddrsigContents.bytes_end();
1867 while (Cur != End) {
1870 uint64_t SymIndex = decodeULEB128(Cur, &Size, End, &Err);
1874 Expected<COFFSymbolRef> Sym = Obj->getSymbol(SymIndex);
1876 std::error_code EC = errorToErrorCode(Sym.takeError());
1877 if (EC || (EC = Obj->getSymbolName(*Sym, SymName))) {
1882 W.printNumber("Sym", SymName, SymIndex);
1887 void llvm::dumpCodeViewMergedTypes(ScopedPrinter &Writer,
1888 ArrayRef<ArrayRef<uint8_t>> IpiRecords,
1889 ArrayRef<ArrayRef<uint8_t>> TpiRecords) {
1890 TypeTableCollection TpiTypes(TpiRecords);
1892 ListScope S(Writer, "MergedTypeStream");
1893 TypeDumpVisitor TDV(TpiTypes, &Writer, opts::CodeViewSubsectionBytes);
1894 error(codeview::visitTypeStream(TpiTypes, TDV));
1898 // Flatten the id stream and print it next. The ID stream refers to names from
1900 TypeTableCollection IpiTypes(IpiRecords);
1902 ListScope S(Writer, "MergedIDStream");
1903 TypeDumpVisitor TDV(TpiTypes, &Writer, opts::CodeViewSubsectionBytes);
1904 TDV.setIpiTypes(IpiTypes);
1905 error(codeview::visitTypeStream(IpiTypes, TDV));