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