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