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