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