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