]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/llvm-readobj/COFFDumper.cpp
Merge clang trunk r300422 and resolve conflicts.
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / llvm-readobj / COFFDumper.cpp
1 //===-- COFFDumper.cpp - COFF-specific dumper -------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 ///
10 /// \file
11 /// \brief This file implements the COFF-specific dumper for llvm-readobj.
12 ///
13 //===----------------------------------------------------------------------===//
14
15 #include "ARMWinEHPrinter.h"
16 #include "CodeView.h"
17 #include "Error.h"
18 #include "ObjDumper.h"
19 #include "StackMapPrinter.h"
20 #include "Win64EHDumper.h"
21 #include "llvm-readobj.h"
22 #include "llvm/ADT/DenseMap.h"
23 #include "llvm/ADT/SmallString.h"
24 #include "llvm/ADT/StringExtras.h"
25 #include "llvm/DebugInfo/CodeView/CVTypeDumper.h"
26 #include "llvm/DebugInfo/CodeView/CodeView.h"
27 #include "llvm/DebugInfo/CodeView/Line.h"
28 #include "llvm/DebugInfo/CodeView/RecordSerialization.h"
29 #include "llvm/DebugInfo/CodeView/SymbolDeserializer.h"
30 #include "llvm/DebugInfo/CodeView/SymbolDumpDelegate.h"
31 #include "llvm/DebugInfo/CodeView/SymbolDumper.h"
32 #include "llvm/DebugInfo/CodeView/SymbolRecord.h"
33 #include "llvm/DebugInfo/CodeView/TypeDumpVisitor.h"
34 #include "llvm/DebugInfo/CodeView/TypeIndex.h"
35 #include "llvm/DebugInfo/CodeView/TypeRecord.h"
36 #include "llvm/DebugInfo/CodeView/TypeStreamMerger.h"
37 #include "llvm/DebugInfo/CodeView/TypeTableBuilder.h"
38 #include "llvm/Object/COFF.h"
39 #include "llvm/Object/ObjectFile.h"
40 #include "llvm/Support/BinaryByteStream.h"
41 #include "llvm/Support/COFF.h"
42 #include "llvm/Support/Casting.h"
43 #include "llvm/Support/Compiler.h"
44 #include "llvm/Support/DataExtractor.h"
45 #include "llvm/Support/Format.h"
46 #include "llvm/Support/Path.h"
47 #include "llvm/Support/ScopedPrinter.h"
48 #include "llvm/Support/SourceMgr.h"
49 #include "llvm/Support/Win64EH.h"
50 #include "llvm/Support/raw_ostream.h"
51 #include <algorithm>
52 #include <cstring>
53 #include <system_error>
54 #include <time.h>
55
56 using namespace llvm;
57 using namespace llvm::object;
58 using namespace llvm::codeview;
59 using namespace llvm::support;
60 using namespace llvm::Win64EH;
61
62 namespace {
63
64 class COFFDumper : public ObjDumper {
65 public:
66   friend class COFFObjectDumpDelegate;
67   COFFDumper(const llvm::object::COFFObjectFile *Obj, ScopedPrinter &Writer)
68       : ObjDumper(Writer), Obj(Obj), Writer(Writer) {}
69
70   void printFileHeaders() override;
71   void printSections() override;
72   void printRelocations() override;
73   void printSymbols() override;
74   void printDynamicSymbols() override;
75   void printUnwindInfo() override;
76   void printCOFFImports() override;
77   void printCOFFExports() override;
78   void printCOFFDirectives() override;
79   void printCOFFBaseReloc() override;
80   void printCOFFDebugDirectory() override;
81   void printCodeViewDebugInfo() override;
82   void mergeCodeViewTypes(llvm::codeview::TypeTableBuilder &CVIDs,
83                           llvm::codeview::TypeTableBuilder &CVTypes) override;
84   void printStackMap() const override;
85 private:
86   void printSymbol(const SymbolRef &Sym);
87   void printRelocation(const SectionRef &Section, const RelocationRef &Reloc,
88                        uint64_t Bias = 0);
89   void printDataDirectory(uint32_t Index, const std::string &FieldName);
90
91   void printDOSHeader(const dos_header *DH);
92   template <class PEHeader> void printPEHeader(const PEHeader *Hdr);
93   void printBaseOfDataField(const pe32_header *Hdr);
94   void printBaseOfDataField(const pe32plus_header *Hdr);
95
96   void printCodeViewSymbolSection(StringRef SectionName, const SectionRef &Section);
97   void printCodeViewTypeSection(StringRef SectionName, const SectionRef &Section);
98   StringRef getTypeName(TypeIndex Ty);
99   StringRef getFileNameForFileOffset(uint32_t FileOffset);
100   void printFileNameForOffset(StringRef Label, uint32_t FileOffset);
101   void printTypeIndex(StringRef FieldName, TypeIndex TI) {
102     // Forward to CVTypeDumper for simplicity.
103     CVTypeDumper::printTypeIndex(Writer, FieldName, TI, TypeDB);
104   }
105
106   void printCodeViewSymbolsSubsection(StringRef Subsection,
107                                       const SectionRef &Section,
108                                       StringRef SectionContents);
109
110   void printCodeViewFileChecksums(StringRef Subsection);
111
112   void printCodeViewInlineeLines(StringRef Subsection);
113
114   void printRelocatedField(StringRef Label, const coff_section *Sec,
115                            uint32_t RelocOffset, uint32_t Offset,
116                            StringRef *RelocSym = nullptr);
117
118   void printBinaryBlockWithRelocs(StringRef Label, const SectionRef &Sec,
119                                   StringRef SectionContents, StringRef Block);
120
121   /// Given a .debug$S section, find the string table and file checksum table.
122   void initializeFileAndStringTables(StringRef Data);
123
124   void cacheRelocations();
125
126   std::error_code resolveSymbol(const coff_section *Section, uint64_t Offset,
127                                 SymbolRef &Sym);
128   std::error_code resolveSymbolName(const coff_section *Section,
129                                     uint64_t Offset, StringRef &Name);
130   std::error_code resolveSymbolName(const coff_section *Section,
131                                     StringRef SectionContents,
132                                     const void *RelocPtr, StringRef &Name);
133   void printImportedSymbols(iterator_range<imported_symbol_iterator> Range);
134   void printDelayImportedSymbols(
135       const DelayImportDirectoryEntryRef &I,
136       iterator_range<imported_symbol_iterator> Range);
137
138   typedef DenseMap<const coff_section*, std::vector<RelocationRef> > RelocMapTy;
139
140   const llvm::object::COFFObjectFile *Obj;
141   bool RelocCached = false;
142   RelocMapTy RelocMap;
143   StringRef CVFileChecksumTable;
144   StringRef CVStringTable;
145
146   ScopedPrinter &Writer;
147   TypeDatabase TypeDB;
148 };
149
150 class COFFObjectDumpDelegate : public SymbolDumpDelegate {
151 public:
152   COFFObjectDumpDelegate(COFFDumper &CD, const SectionRef &SR,
153                          const COFFObjectFile *Obj, StringRef SectionContents)
154       : CD(CD), SR(SR), SectionContents(SectionContents) {
155     Sec = Obj->getCOFFSection(SR);
156   }
157
158   uint32_t getRecordOffset(BinaryStreamReader Reader) override {
159     ArrayRef<uint8_t> Data;
160     if (auto EC = Reader.readLongestContiguousChunk(Data)) {
161       llvm::consumeError(std::move(EC));
162       return 0;
163     }
164     return Data.data() - SectionContents.bytes_begin();
165   }
166
167   void printRelocatedField(StringRef Label, uint32_t RelocOffset,
168                            uint32_t Offset, StringRef *RelocSym) override {
169     CD.printRelocatedField(Label, Sec, RelocOffset, Offset, RelocSym);
170   }
171
172   void printBinaryBlockWithRelocs(StringRef Label,
173                                   ArrayRef<uint8_t> Block) override {
174     StringRef SBlock(reinterpret_cast<const char *>(Block.data()),
175                      Block.size());
176     if (opts::CodeViewSubsectionBytes)
177       CD.printBinaryBlockWithRelocs(Label, SR, SectionContents, SBlock);
178   }
179
180   StringRef getFileNameForFileOffset(uint32_t FileOffset) override {
181     return CD.getFileNameForFileOffset(FileOffset);
182   }
183
184   StringRef getStringTable() override { return CD.CVStringTable; }
185
186 private:
187   COFFDumper &CD;
188   const SectionRef &SR;
189   const coff_section *Sec;
190   StringRef SectionContents;
191 };
192
193 } // end namespace
194
195 namespace llvm {
196
197 std::error_code createCOFFDumper(const object::ObjectFile *Obj,
198                                  ScopedPrinter &Writer,
199                                  std::unique_ptr<ObjDumper> &Result) {
200   const COFFObjectFile *COFFObj = dyn_cast<COFFObjectFile>(Obj);
201   if (!COFFObj)
202     return readobj_error::unsupported_obj_file_format;
203
204   Result.reset(new COFFDumper(COFFObj, Writer));
205   return readobj_error::success;
206 }
207
208 } // namespace llvm
209
210 // Given a a section and an offset into this section the function returns the
211 // symbol used for the relocation at the offset.
212 std::error_code COFFDumper::resolveSymbol(const coff_section *Section,
213                                           uint64_t Offset, SymbolRef &Sym) {
214   cacheRelocations();
215   const auto &Relocations = RelocMap[Section];
216   auto SymI = Obj->symbol_end();
217   for (const auto &Relocation : Relocations) {
218     uint64_t RelocationOffset = Relocation.getOffset();
219
220     if (RelocationOffset == Offset) {
221       SymI = Relocation.getSymbol();
222       break;
223     }
224   }
225   if (SymI == Obj->symbol_end())
226     return readobj_error::unknown_symbol;
227   Sym = *SymI;
228   return readobj_error::success;
229 }
230
231 // Given a section and an offset into this section the function returns the name
232 // of the symbol used for the relocation at the offset.
233 std::error_code COFFDumper::resolveSymbolName(const coff_section *Section,
234                                               uint64_t Offset,
235                                               StringRef &Name) {
236   SymbolRef Symbol;
237   if (std::error_code EC = resolveSymbol(Section, Offset, Symbol))
238     return EC;
239   Expected<StringRef> NameOrErr = Symbol.getName();
240   if (!NameOrErr)
241     return errorToErrorCode(NameOrErr.takeError());
242   Name = *NameOrErr;
243   return std::error_code();
244 }
245
246 // Helper for when you have a pointer to real data and you want to know about
247 // relocations against it.
248 std::error_code COFFDumper::resolveSymbolName(const coff_section *Section,
249                                               StringRef SectionContents,
250                                               const void *RelocPtr,
251                                               StringRef &Name) {
252   assert(SectionContents.data() < RelocPtr &&
253          RelocPtr < SectionContents.data() + SectionContents.size() &&
254          "pointer to relocated object is not in section");
255   uint64_t Offset = ptrdiff_t(reinterpret_cast<const char *>(RelocPtr) -
256                               SectionContents.data());
257   return resolveSymbolName(Section, Offset, Name);
258 }
259
260 void COFFDumper::printRelocatedField(StringRef Label, const coff_section *Sec,
261                                      uint32_t RelocOffset, uint32_t Offset,
262                                      StringRef *RelocSym) {
263   StringRef SymStorage;
264   StringRef &Symbol = RelocSym ? *RelocSym : SymStorage;
265   if (!resolveSymbolName(Sec, RelocOffset, Symbol))
266     W.printSymbolOffset(Label, Symbol, Offset);
267   else
268     W.printHex(Label, RelocOffset);
269 }
270
271 void COFFDumper::printBinaryBlockWithRelocs(StringRef Label,
272                                             const SectionRef &Sec,
273                                             StringRef SectionContents,
274                                             StringRef Block) {
275   W.printBinaryBlock(Label, Block);
276
277   assert(SectionContents.begin() < Block.begin() &&
278          SectionContents.end() >= Block.end() &&
279          "Block is not contained in SectionContents");
280   uint64_t OffsetStart = Block.data() - SectionContents.data();
281   uint64_t OffsetEnd = OffsetStart + Block.size();
282
283   W.flush();
284   cacheRelocations();
285   ListScope D(W, "BlockRelocations");
286   const coff_section *Section = Obj->getCOFFSection(Sec);
287   const auto &Relocations = RelocMap[Section];
288   for (const auto &Relocation : Relocations) {
289     uint64_t RelocationOffset = Relocation.getOffset();
290     if (OffsetStart <= RelocationOffset && RelocationOffset < OffsetEnd)
291       printRelocation(Sec, Relocation, OffsetStart);
292   }
293 }
294
295 static const EnumEntry<COFF::MachineTypes> ImageFileMachineType[] = {
296   LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_UNKNOWN  ),
297   LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_AM33     ),
298   LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_AMD64    ),
299   LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_ARM      ),
300   LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_ARMNT    ),
301   LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_EBC      ),
302   LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_I386     ),
303   LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_IA64     ),
304   LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_M32R     ),
305   LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_MIPS16   ),
306   LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_MIPSFPU  ),
307   LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_MIPSFPU16),
308   LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_POWERPC  ),
309   LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_POWERPCFP),
310   LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_R4000    ),
311   LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_SH3      ),
312   LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_SH3DSP   ),
313   LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_SH4      ),
314   LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_SH5      ),
315   LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_THUMB    ),
316   LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_WCEMIPSV2)
317 };
318
319 static const EnumEntry<COFF::Characteristics> ImageFileCharacteristics[] = {
320   LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_RELOCS_STRIPPED        ),
321   LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_EXECUTABLE_IMAGE       ),
322   LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_LINE_NUMS_STRIPPED     ),
323   LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_LOCAL_SYMS_STRIPPED    ),
324   LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_AGGRESSIVE_WS_TRIM     ),
325   LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_LARGE_ADDRESS_AWARE    ),
326   LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_BYTES_REVERSED_LO      ),
327   LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_32BIT_MACHINE          ),
328   LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_DEBUG_STRIPPED         ),
329   LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_REMOVABLE_RUN_FROM_SWAP),
330   LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_NET_RUN_FROM_SWAP      ),
331   LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_SYSTEM                 ),
332   LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_DLL                    ),
333   LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_UP_SYSTEM_ONLY         ),
334   LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_BYTES_REVERSED_HI      )
335 };
336
337 static const EnumEntry<COFF::WindowsSubsystem> PEWindowsSubsystem[] = {
338   LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SUBSYSTEM_UNKNOWN                ),
339   LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SUBSYSTEM_NATIVE                 ),
340   LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SUBSYSTEM_WINDOWS_GUI            ),
341   LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SUBSYSTEM_WINDOWS_CUI            ),
342   LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SUBSYSTEM_POSIX_CUI              ),
343   LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SUBSYSTEM_WINDOWS_CE_GUI         ),
344   LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SUBSYSTEM_EFI_APPLICATION        ),
345   LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER),
346   LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER     ),
347   LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SUBSYSTEM_EFI_ROM                ),
348   LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SUBSYSTEM_XBOX                   ),
349 };
350
351 static const EnumEntry<COFF::DLLCharacteristics> PEDLLCharacteristics[] = {
352   LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_DLL_CHARACTERISTICS_HIGH_ENTROPY_VA      ),
353   LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_DLL_CHARACTERISTICS_DYNAMIC_BASE         ),
354   LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_DLL_CHARACTERISTICS_FORCE_INTEGRITY      ),
355   LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_DLL_CHARACTERISTICS_NX_COMPAT            ),
356   LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_DLL_CHARACTERISTICS_NO_ISOLATION         ),
357   LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_DLL_CHARACTERISTICS_NO_SEH               ),
358   LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_DLL_CHARACTERISTICS_NO_BIND              ),
359   LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_DLL_CHARACTERISTICS_APPCONTAINER         ),
360   LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_DLL_CHARACTERISTICS_WDM_DRIVER           ),
361   LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_DLL_CHARACTERISTICS_GUARD_CF             ),
362   LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_DLL_CHARACTERISTICS_TERMINAL_SERVER_AWARE),
363 };
364
365 static const EnumEntry<COFF::SectionCharacteristics>
366 ImageSectionCharacteristics[] = {
367   LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_TYPE_NOLOAD           ),
368   LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_TYPE_NO_PAD           ),
369   LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_CNT_CODE              ),
370   LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_CNT_INITIALIZED_DATA  ),
371   LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_CNT_UNINITIALIZED_DATA),
372   LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_LNK_OTHER             ),
373   LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_LNK_INFO              ),
374   LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_LNK_REMOVE            ),
375   LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_LNK_COMDAT            ),
376   LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_GPREL                 ),
377   LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_MEM_PURGEABLE         ),
378   LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_MEM_16BIT             ),
379   LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_MEM_LOCKED            ),
380   LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_MEM_PRELOAD           ),
381   LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_ALIGN_1BYTES          ),
382   LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_ALIGN_2BYTES          ),
383   LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_ALIGN_4BYTES          ),
384   LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_ALIGN_8BYTES          ),
385   LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_ALIGN_16BYTES         ),
386   LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_ALIGN_32BYTES         ),
387   LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_ALIGN_64BYTES         ),
388   LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_ALIGN_128BYTES        ),
389   LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_ALIGN_256BYTES        ),
390   LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_ALIGN_512BYTES        ),
391   LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_ALIGN_1024BYTES       ),
392   LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_ALIGN_2048BYTES       ),
393   LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_ALIGN_4096BYTES       ),
394   LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_ALIGN_8192BYTES       ),
395   LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_LNK_NRELOC_OVFL       ),
396   LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_MEM_DISCARDABLE       ),
397   LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_MEM_NOT_CACHED        ),
398   LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_MEM_NOT_PAGED         ),
399   LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_MEM_SHARED            ),
400   LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_MEM_EXECUTE           ),
401   LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_MEM_READ              ),
402   LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_MEM_WRITE             )
403 };
404
405 static const EnumEntry<COFF::SymbolBaseType> ImageSymType[] = {
406   { "Null"  , COFF::IMAGE_SYM_TYPE_NULL   },
407   { "Void"  , COFF::IMAGE_SYM_TYPE_VOID   },
408   { "Char"  , COFF::IMAGE_SYM_TYPE_CHAR   },
409   { "Short" , COFF::IMAGE_SYM_TYPE_SHORT  },
410   { "Int"   , COFF::IMAGE_SYM_TYPE_INT    },
411   { "Long"  , COFF::IMAGE_SYM_TYPE_LONG   },
412   { "Float" , COFF::IMAGE_SYM_TYPE_FLOAT  },
413   { "Double", COFF::IMAGE_SYM_TYPE_DOUBLE },
414   { "Struct", COFF::IMAGE_SYM_TYPE_STRUCT },
415   { "Union" , COFF::IMAGE_SYM_TYPE_UNION  },
416   { "Enum"  , COFF::IMAGE_SYM_TYPE_ENUM   },
417   { "MOE"   , COFF::IMAGE_SYM_TYPE_MOE    },
418   { "Byte"  , COFF::IMAGE_SYM_TYPE_BYTE   },
419   { "Word"  , COFF::IMAGE_SYM_TYPE_WORD   },
420   { "UInt"  , COFF::IMAGE_SYM_TYPE_UINT   },
421   { "DWord" , COFF::IMAGE_SYM_TYPE_DWORD  }
422 };
423
424 static const EnumEntry<COFF::SymbolComplexType> ImageSymDType[] = {
425   { "Null"    , COFF::IMAGE_SYM_DTYPE_NULL     },
426   { "Pointer" , COFF::IMAGE_SYM_DTYPE_POINTER  },
427   { "Function", COFF::IMAGE_SYM_DTYPE_FUNCTION },
428   { "Array"   , COFF::IMAGE_SYM_DTYPE_ARRAY    }
429 };
430
431 static const EnumEntry<COFF::SymbolStorageClass> ImageSymClass[] = {
432   { "EndOfFunction"  , COFF::IMAGE_SYM_CLASS_END_OF_FUNCTION  },
433   { "Null"           , COFF::IMAGE_SYM_CLASS_NULL             },
434   { "Automatic"      , COFF::IMAGE_SYM_CLASS_AUTOMATIC        },
435   { "External"       , COFF::IMAGE_SYM_CLASS_EXTERNAL         },
436   { "Static"         , COFF::IMAGE_SYM_CLASS_STATIC           },
437   { "Register"       , COFF::IMAGE_SYM_CLASS_REGISTER         },
438   { "ExternalDef"    , COFF::IMAGE_SYM_CLASS_EXTERNAL_DEF     },
439   { "Label"          , COFF::IMAGE_SYM_CLASS_LABEL            },
440   { "UndefinedLabel" , COFF::IMAGE_SYM_CLASS_UNDEFINED_LABEL  },
441   { "MemberOfStruct" , COFF::IMAGE_SYM_CLASS_MEMBER_OF_STRUCT },
442   { "Argument"       , COFF::IMAGE_SYM_CLASS_ARGUMENT         },
443   { "StructTag"      , COFF::IMAGE_SYM_CLASS_STRUCT_TAG       },
444   { "MemberOfUnion"  , COFF::IMAGE_SYM_CLASS_MEMBER_OF_UNION  },
445   { "UnionTag"       , COFF::IMAGE_SYM_CLASS_UNION_TAG        },
446   { "TypeDefinition" , COFF::IMAGE_SYM_CLASS_TYPE_DEFINITION  },
447   { "UndefinedStatic", COFF::IMAGE_SYM_CLASS_UNDEFINED_STATIC },
448   { "EnumTag"        , COFF::IMAGE_SYM_CLASS_ENUM_TAG         },
449   { "MemberOfEnum"   , COFF::IMAGE_SYM_CLASS_MEMBER_OF_ENUM   },
450   { "RegisterParam"  , COFF::IMAGE_SYM_CLASS_REGISTER_PARAM   },
451   { "BitField"       , COFF::IMAGE_SYM_CLASS_BIT_FIELD        },
452   { "Block"          , COFF::IMAGE_SYM_CLASS_BLOCK            },
453   { "Function"       , COFF::IMAGE_SYM_CLASS_FUNCTION         },
454   { "EndOfStruct"    , COFF::IMAGE_SYM_CLASS_END_OF_STRUCT    },
455   { "File"           , COFF::IMAGE_SYM_CLASS_FILE             },
456   { "Section"        , COFF::IMAGE_SYM_CLASS_SECTION          },
457   { "WeakExternal"   , COFF::IMAGE_SYM_CLASS_WEAK_EXTERNAL    },
458   { "CLRToken"       , COFF::IMAGE_SYM_CLASS_CLR_TOKEN        }
459 };
460
461 static const EnumEntry<COFF::COMDATType> ImageCOMDATSelect[] = {
462   { "NoDuplicates", COFF::IMAGE_COMDAT_SELECT_NODUPLICATES },
463   { "Any"         , COFF::IMAGE_COMDAT_SELECT_ANY          },
464   { "SameSize"    , COFF::IMAGE_COMDAT_SELECT_SAME_SIZE    },
465   { "ExactMatch"  , COFF::IMAGE_COMDAT_SELECT_EXACT_MATCH  },
466   { "Associative" , COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE  },
467   { "Largest"     , COFF::IMAGE_COMDAT_SELECT_LARGEST      },
468   { "Newest"      , COFF::IMAGE_COMDAT_SELECT_NEWEST       }
469 };
470
471 static const EnumEntry<COFF::DebugType> ImageDebugType[] = {
472   { "Unknown"    , COFF::IMAGE_DEBUG_TYPE_UNKNOWN       },
473   { "COFF"       , COFF::IMAGE_DEBUG_TYPE_COFF          },
474   { "CodeView"   , COFF::IMAGE_DEBUG_TYPE_CODEVIEW      },
475   { "FPO"        , COFF::IMAGE_DEBUG_TYPE_FPO           },
476   { "Misc"       , COFF::IMAGE_DEBUG_TYPE_MISC          },
477   { "Exception"  , COFF::IMAGE_DEBUG_TYPE_EXCEPTION     },
478   { "Fixup"      , COFF::IMAGE_DEBUG_TYPE_FIXUP         },
479   { "OmapToSrc"  , COFF::IMAGE_DEBUG_TYPE_OMAP_TO_SRC   },
480   { "OmapFromSrc", COFF::IMAGE_DEBUG_TYPE_OMAP_FROM_SRC },
481   { "Borland"    , COFF::IMAGE_DEBUG_TYPE_BORLAND       },
482   { "Reserved10" , COFF::IMAGE_DEBUG_TYPE_RESERVED10    },
483   { "CLSID"      , COFF::IMAGE_DEBUG_TYPE_CLSID         },
484   { "VCFeature"  , COFF::IMAGE_DEBUG_TYPE_VC_FEATURE    },
485   { "POGO"       , COFF::IMAGE_DEBUG_TYPE_POGO          },
486   { "ILTCG"      , COFF::IMAGE_DEBUG_TYPE_ILTCG         },
487   { "MPX"        , COFF::IMAGE_DEBUG_TYPE_MPX           },
488   { "Repro"      , COFF::IMAGE_DEBUG_TYPE_REPRO         },
489 };
490
491 static const EnumEntry<COFF::WeakExternalCharacteristics>
492 WeakExternalCharacteristics[] = {
493   { "NoLibrary", COFF::IMAGE_WEAK_EXTERN_SEARCH_NOLIBRARY },
494   { "Library"  , COFF::IMAGE_WEAK_EXTERN_SEARCH_LIBRARY   },
495   { "Alias"    , COFF::IMAGE_WEAK_EXTERN_SEARCH_ALIAS     }
496 };
497
498 static const EnumEntry<uint32_t> SubSectionTypes[] = {
499   LLVM_READOBJ_ENUM_CLASS_ENT(ModuleSubstreamKind, Symbols),
500   LLVM_READOBJ_ENUM_CLASS_ENT(ModuleSubstreamKind, Lines),
501   LLVM_READOBJ_ENUM_CLASS_ENT(ModuleSubstreamKind, StringTable),
502   LLVM_READOBJ_ENUM_CLASS_ENT(ModuleSubstreamKind, FileChecksums),
503   LLVM_READOBJ_ENUM_CLASS_ENT(ModuleSubstreamKind, FrameData),
504   LLVM_READOBJ_ENUM_CLASS_ENT(ModuleSubstreamKind, InlineeLines),
505   LLVM_READOBJ_ENUM_CLASS_ENT(ModuleSubstreamKind, CrossScopeImports),
506   LLVM_READOBJ_ENUM_CLASS_ENT(ModuleSubstreamKind, CrossScopeExports),
507   LLVM_READOBJ_ENUM_CLASS_ENT(ModuleSubstreamKind, ILLines),
508   LLVM_READOBJ_ENUM_CLASS_ENT(ModuleSubstreamKind, FuncMDTokenMap),
509   LLVM_READOBJ_ENUM_CLASS_ENT(ModuleSubstreamKind, TypeMDTokenMap),
510   LLVM_READOBJ_ENUM_CLASS_ENT(ModuleSubstreamKind, MergedAssemblyInput),
511   LLVM_READOBJ_ENUM_CLASS_ENT(ModuleSubstreamKind, CoffSymbolRVA),
512 };
513
514 static const EnumEntry<uint32_t> FrameDataFlags[] = {
515     LLVM_READOBJ_ENUM_ENT(FrameData, HasSEH),
516     LLVM_READOBJ_ENUM_ENT(FrameData, HasEH),
517     LLVM_READOBJ_ENUM_ENT(FrameData, IsFunctionStart),
518 };
519
520 static const EnumEntry<uint8_t> FileChecksumKindNames[] = {
521   LLVM_READOBJ_ENUM_CLASS_ENT(FileChecksumKind, None),
522   LLVM_READOBJ_ENUM_CLASS_ENT(FileChecksumKind, MD5),
523   LLVM_READOBJ_ENUM_CLASS_ENT(FileChecksumKind, SHA1),
524   LLVM_READOBJ_ENUM_CLASS_ENT(FileChecksumKind, SHA256),
525 };
526
527 template <typename T>
528 static std::error_code getSymbolAuxData(const COFFObjectFile *Obj,
529                                         COFFSymbolRef Symbol,
530                                         uint8_t AuxSymbolIdx, const T *&Aux) {
531   ArrayRef<uint8_t> AuxData = Obj->getSymbolAuxData(Symbol);
532   AuxData = AuxData.slice(AuxSymbolIdx * Obj->getSymbolTableEntrySize());
533   Aux = reinterpret_cast<const T*>(AuxData.data());
534   return readobj_error::success;
535 }
536
537 void COFFDumper::cacheRelocations() {
538   if (RelocCached)
539     return;
540   RelocCached = true;
541
542   for (const SectionRef &S : Obj->sections()) {
543     const coff_section *Section = Obj->getCOFFSection(S);
544
545     for (const RelocationRef &Reloc : S.relocations())
546       RelocMap[Section].push_back(Reloc);
547
548     // Sort relocations by address.
549     std::sort(RelocMap[Section].begin(), RelocMap[Section].end(),
550               relocAddressLess);
551   }
552 }
553
554 void COFFDumper::printDataDirectory(uint32_t Index, const std::string &FieldName) {
555   const data_directory *Data;
556   if (Obj->getDataDirectory(Index, Data))
557     return;
558   W.printHex(FieldName + "RVA", Data->RelativeVirtualAddress);
559   W.printHex(FieldName + "Size", Data->Size);
560 }
561
562 void COFFDumper::printFileHeaders() {
563   time_t TDS = Obj->getTimeDateStamp();
564   char FormattedTime[20] = { };
565   strftime(FormattedTime, 20, "%Y-%m-%d %H:%M:%S", gmtime(&TDS));
566
567   {
568     DictScope D(W, "ImageFileHeader");
569     W.printEnum  ("Machine", Obj->getMachine(),
570                     makeArrayRef(ImageFileMachineType));
571     W.printNumber("SectionCount", Obj->getNumberOfSections());
572     W.printHex   ("TimeDateStamp", FormattedTime, Obj->getTimeDateStamp());
573     W.printHex   ("PointerToSymbolTable", Obj->getPointerToSymbolTable());
574     W.printNumber("SymbolCount", Obj->getNumberOfSymbols());
575     W.printNumber("OptionalHeaderSize", Obj->getSizeOfOptionalHeader());
576     W.printFlags ("Characteristics", Obj->getCharacteristics(),
577                     makeArrayRef(ImageFileCharacteristics));
578   }
579
580   // Print PE header. This header does not exist if this is an object file and
581   // not an executable.
582   const pe32_header *PEHeader = nullptr;
583   error(Obj->getPE32Header(PEHeader));
584   if (PEHeader)
585     printPEHeader<pe32_header>(PEHeader);
586
587   const pe32plus_header *PEPlusHeader = nullptr;
588   error(Obj->getPE32PlusHeader(PEPlusHeader));
589   if (PEPlusHeader)
590     printPEHeader<pe32plus_header>(PEPlusHeader);
591
592   if (const dos_header *DH = Obj->getDOSHeader())
593     printDOSHeader(DH);
594 }
595
596 void COFFDumper::printDOSHeader(const dos_header *DH) {
597   DictScope D(W, "DOSHeader");
598   W.printString("Magic", StringRef(DH->Magic, sizeof(DH->Magic)));
599   W.printNumber("UsedBytesInTheLastPage", DH->UsedBytesInTheLastPage);
600   W.printNumber("FileSizeInPages", DH->FileSizeInPages);
601   W.printNumber("NumberOfRelocationItems", DH->NumberOfRelocationItems);
602   W.printNumber("HeaderSizeInParagraphs", DH->HeaderSizeInParagraphs);
603   W.printNumber("MinimumExtraParagraphs", DH->MinimumExtraParagraphs);
604   W.printNumber("MaximumExtraParagraphs", DH->MaximumExtraParagraphs);
605   W.printNumber("InitialRelativeSS", DH->InitialRelativeSS);
606   W.printNumber("InitialSP", DH->InitialSP);
607   W.printNumber("Checksum", DH->Checksum);
608   W.printNumber("InitialIP", DH->InitialIP);
609   W.printNumber("InitialRelativeCS", DH->InitialRelativeCS);
610   W.printNumber("AddressOfRelocationTable", DH->AddressOfRelocationTable);
611   W.printNumber("OverlayNumber", DH->OverlayNumber);
612   W.printNumber("OEMid", DH->OEMid);
613   W.printNumber("OEMinfo", DH->OEMinfo);
614   W.printNumber("AddressOfNewExeHeader", DH->AddressOfNewExeHeader);
615 }
616
617 template <class PEHeader>
618 void COFFDumper::printPEHeader(const PEHeader *Hdr) {
619   DictScope D(W, "ImageOptionalHeader");
620   W.printNumber("MajorLinkerVersion", Hdr->MajorLinkerVersion);
621   W.printNumber("MinorLinkerVersion", Hdr->MinorLinkerVersion);
622   W.printNumber("SizeOfCode", Hdr->SizeOfCode);
623   W.printNumber("SizeOfInitializedData", Hdr->SizeOfInitializedData);
624   W.printNumber("SizeOfUninitializedData", Hdr->SizeOfUninitializedData);
625   W.printHex   ("AddressOfEntryPoint", Hdr->AddressOfEntryPoint);
626   W.printHex   ("BaseOfCode", Hdr->BaseOfCode);
627   printBaseOfDataField(Hdr);
628   W.printHex   ("ImageBase", Hdr->ImageBase);
629   W.printNumber("SectionAlignment", Hdr->SectionAlignment);
630   W.printNumber("FileAlignment", Hdr->FileAlignment);
631   W.printNumber("MajorOperatingSystemVersion",
632                 Hdr->MajorOperatingSystemVersion);
633   W.printNumber("MinorOperatingSystemVersion",
634                 Hdr->MinorOperatingSystemVersion);
635   W.printNumber("MajorImageVersion", Hdr->MajorImageVersion);
636   W.printNumber("MinorImageVersion", Hdr->MinorImageVersion);
637   W.printNumber("MajorSubsystemVersion", Hdr->MajorSubsystemVersion);
638   W.printNumber("MinorSubsystemVersion", Hdr->MinorSubsystemVersion);
639   W.printNumber("SizeOfImage", Hdr->SizeOfImage);
640   W.printNumber("SizeOfHeaders", Hdr->SizeOfHeaders);
641   W.printEnum  ("Subsystem", Hdr->Subsystem, makeArrayRef(PEWindowsSubsystem));
642   W.printFlags ("Characteristics", Hdr->DLLCharacteristics,
643                 makeArrayRef(PEDLLCharacteristics));
644   W.printNumber("SizeOfStackReserve", Hdr->SizeOfStackReserve);
645   W.printNumber("SizeOfStackCommit", Hdr->SizeOfStackCommit);
646   W.printNumber("SizeOfHeapReserve", Hdr->SizeOfHeapReserve);
647   W.printNumber("SizeOfHeapCommit", Hdr->SizeOfHeapCommit);
648   W.printNumber("NumberOfRvaAndSize", Hdr->NumberOfRvaAndSize);
649
650   if (Hdr->NumberOfRvaAndSize > 0) {
651     DictScope D(W, "DataDirectory");
652     static const char * const directory[] = {
653       "ExportTable", "ImportTable", "ResourceTable", "ExceptionTable",
654       "CertificateTable", "BaseRelocationTable", "Debug", "Architecture",
655       "GlobalPtr", "TLSTable", "LoadConfigTable", "BoundImport", "IAT",
656       "DelayImportDescriptor", "CLRRuntimeHeader", "Reserved"
657     };
658
659     for (uint32_t i = 0; i < Hdr->NumberOfRvaAndSize; ++i)
660       printDataDirectory(i, directory[i]);
661   }
662 }
663
664 void COFFDumper::printCOFFDebugDirectory() {
665   ListScope LS(W, "DebugDirectory");
666   for (const debug_directory &D : Obj->debug_directories()) {
667     char FormattedTime[20] = {};
668     time_t TDS = D.TimeDateStamp;
669     strftime(FormattedTime, 20, "%Y-%m-%d %H:%M:%S", gmtime(&TDS));
670     DictScope S(W, "DebugEntry");
671     W.printHex("Characteristics", D.Characteristics);
672     W.printHex("TimeDateStamp", FormattedTime, D.TimeDateStamp);
673     W.printHex("MajorVersion", D.MajorVersion);
674     W.printHex("MinorVersion", D.MinorVersion);
675     W.printEnum("Type", D.Type, makeArrayRef(ImageDebugType));
676     W.printHex("SizeOfData", D.SizeOfData);
677     W.printHex("AddressOfRawData", D.AddressOfRawData);
678     W.printHex("PointerToRawData", D.PointerToRawData);
679     if (D.Type == COFF::IMAGE_DEBUG_TYPE_CODEVIEW) {
680       const codeview::DebugInfo *DebugInfo;
681       StringRef PDBFileName;
682       error(Obj->getDebugPDBInfo(&D, DebugInfo, PDBFileName));
683       DictScope PDBScope(W, "PDBInfo");
684       W.printHex("PDBSignature", DebugInfo->Signature.CVSignature);
685       if (DebugInfo->Signature.CVSignature == OMF::Signature::PDB70) {
686         W.printBinary("PDBGUID", makeArrayRef(DebugInfo->PDB70.Signature));
687         W.printNumber("PDBAge", DebugInfo->PDB70.Age);
688         W.printString("PDBFileName", PDBFileName);
689       }
690     } else {
691       // FIXME: Type values of 12 and 13 are commonly observed but are not in
692       // the documented type enum.  Figure out what they mean.
693       ArrayRef<uint8_t> RawData;
694       error(
695           Obj->getRvaAndSizeAsBytes(D.AddressOfRawData, D.SizeOfData, RawData));
696       W.printBinaryBlock("RawData", RawData);
697     }
698   }
699 }
700
701 void COFFDumper::printBaseOfDataField(const pe32_header *Hdr) {
702   W.printHex("BaseOfData", Hdr->BaseOfData);
703 }
704
705 void COFFDumper::printBaseOfDataField(const pe32plus_header *) {}
706
707 void COFFDumper::printCodeViewDebugInfo() {
708   // Print types first to build CVUDTNames, then print symbols.
709   for (const SectionRef &S : Obj->sections()) {
710     StringRef SectionName;
711     error(S.getName(SectionName));
712     if (SectionName == ".debug$T")
713       printCodeViewTypeSection(SectionName, S);
714   }
715   for (const SectionRef &S : Obj->sections()) {
716     StringRef SectionName;
717     error(S.getName(SectionName));
718     if (SectionName == ".debug$S")
719       printCodeViewSymbolSection(SectionName, S);
720   }
721 }
722
723 void COFFDumper::initializeFileAndStringTables(StringRef Data) {
724   while (!Data.empty() && (CVFileChecksumTable.data() == nullptr ||
725                            CVStringTable.data() == nullptr)) {
726     // The section consists of a number of subsection in the following format:
727     // |SubSectionType|SubSectionSize|Contents...|
728     uint32_t SubType, SubSectionSize;
729     error(consume(Data, SubType));
730     error(consume(Data, SubSectionSize));
731     if (SubSectionSize > Data.size())
732       return error(object_error::parse_failed);
733     switch (ModuleSubstreamKind(SubType)) {
734     case ModuleSubstreamKind::FileChecksums:
735       CVFileChecksumTable = Data.substr(0, SubSectionSize);
736       break;
737     case ModuleSubstreamKind::StringTable:
738       CVStringTable = Data.substr(0, SubSectionSize);
739       break;
740     default:
741       break;
742     }
743     uint32_t PaddedSize = alignTo(SubSectionSize, 4);
744     if (PaddedSize > Data.size())
745       error(object_error::parse_failed);
746     Data = Data.drop_front(PaddedSize);
747   }
748 }
749
750 void COFFDumper::printCodeViewSymbolSection(StringRef SectionName,
751                                             const SectionRef &Section) {
752   StringRef SectionContents;
753   error(Section.getContents(SectionContents));
754   StringRef Data = SectionContents;
755
756   SmallVector<StringRef, 10> FunctionNames;
757   StringMap<StringRef> FunctionLineTables;
758
759   ListScope D(W, "CodeViewDebugInfo");
760   // Print the section to allow correlation with printSections.
761   W.printNumber("Section", SectionName, Obj->getSectionID(Section));
762
763   uint32_t Magic;
764   error(consume(Data, Magic));
765   W.printHex("Magic", Magic);
766   if (Magic != COFF::DEBUG_SECTION_MAGIC)
767     return error(object_error::parse_failed);
768
769   initializeFileAndStringTables(Data);
770
771   // TODO: Convert this over to using ModuleSubstreamVisitor.
772   while (!Data.empty()) {
773     // The section consists of a number of subsection in the following format:
774     // |SubSectionType|SubSectionSize|Contents...|
775     uint32_t SubType, SubSectionSize;
776     error(consume(Data, SubType));
777     error(consume(Data, SubSectionSize));
778
779     ListScope S(W, "Subsection");
780     W.printEnum("SubSectionType", SubType, makeArrayRef(SubSectionTypes));
781     W.printHex("SubSectionSize", SubSectionSize);
782
783     // Get the contents of the subsection.
784     if (SubSectionSize > Data.size())
785       return error(object_error::parse_failed);
786     StringRef Contents = Data.substr(0, SubSectionSize);
787
788     // Add SubSectionSize to the current offset and align that offset to find
789     // the next subsection.
790     size_t SectionOffset = Data.data() - SectionContents.data();
791     size_t NextOffset = SectionOffset + SubSectionSize;
792     NextOffset = alignTo(NextOffset, 4);
793     if (NextOffset > SectionContents.size())
794       return error(object_error::parse_failed);
795     Data = SectionContents.drop_front(NextOffset);
796
797     // Optionally print the subsection bytes in case our parsing gets confused
798     // later.
799     if (opts::CodeViewSubsectionBytes)
800       printBinaryBlockWithRelocs("SubSectionContents", Section, SectionContents,
801                                  Contents);
802
803     switch (ModuleSubstreamKind(SubType)) {
804     case ModuleSubstreamKind::Symbols:
805       printCodeViewSymbolsSubsection(Contents, Section, SectionContents);
806       break;
807
808     case ModuleSubstreamKind::InlineeLines:
809       printCodeViewInlineeLines(Contents);
810       break;
811
812     case ModuleSubstreamKind::FileChecksums:
813       printCodeViewFileChecksums(Contents);
814       break;
815
816     case ModuleSubstreamKind::Lines: {
817       // Holds a PC to file:line table.  Some data to parse this subsection is
818       // stored in the other subsections, so just check sanity and store the
819       // pointers for deferred processing.
820
821       if (SubSectionSize < 12) {
822         // There should be at least three words to store two function
823         // relocations and size of the code.
824         error(object_error::parse_failed);
825         return;
826       }
827
828       StringRef LinkageName;
829       error(resolveSymbolName(Obj->getCOFFSection(Section), SectionOffset,
830                               LinkageName));
831       W.printString("LinkageName", LinkageName);
832       if (FunctionLineTables.count(LinkageName) != 0) {
833         // Saw debug info for this function already?
834         error(object_error::parse_failed);
835         return;
836       }
837
838       FunctionLineTables[LinkageName] = Contents;
839       FunctionNames.push_back(LinkageName);
840       break;
841     }
842     case ModuleSubstreamKind::FrameData: {
843       // First four bytes is a relocation against the function.
844       BinaryByteStream S(Contents, llvm::support::little);
845       BinaryStreamReader SR(S);
846       const uint32_t *CodePtr;
847       error(SR.readObject(CodePtr));
848       StringRef LinkageName;
849       error(resolveSymbolName(Obj->getCOFFSection(Section), SectionContents,
850                               CodePtr, LinkageName));
851       W.printString("LinkageName", LinkageName);
852
853       // To find the active frame description, search this array for the
854       // smallest PC range that includes the current PC.
855       while (!SR.empty()) {
856         const FrameData *FD;
857         error(SR.readObject(FD));
858
859         if (FD->FrameFunc >= CVStringTable.size())
860           error(object_error::parse_failed);
861
862         StringRef FrameFunc =
863             CVStringTable.drop_front(FD->FrameFunc).split('\0').first;
864
865         DictScope S(W, "FrameData");
866         W.printHex("RvaStart", FD->RvaStart);
867         W.printHex("CodeSize", FD->CodeSize);
868         W.printHex("LocalSize", FD->LocalSize);
869         W.printHex("ParamsSize", FD->ParamsSize);
870         W.printHex("MaxStackSize", FD->MaxStackSize);
871         W.printString("FrameFunc", FrameFunc);
872         W.printHex("PrologSize", FD->PrologSize);
873         W.printHex("SavedRegsSize", FD->SavedRegsSize);
874         W.printFlags("Flags", FD->Flags, makeArrayRef(FrameDataFlags));
875       }
876       break;
877     }
878
879     // Do nothing for unrecognized subsections.
880     default:
881       break;
882     }
883     W.flush();
884   }
885
886   // Dump the line tables now that we've read all the subsections and know all
887   // the required information.
888   for (unsigned I = 0, E = FunctionNames.size(); I != E; ++I) {
889     StringRef Name = FunctionNames[I];
890     ListScope S(W, "FunctionLineTable");
891     W.printString("LinkageName", Name);
892
893     DataExtractor DE(FunctionLineTables[Name], true, 4);
894     uint32_t Offset = 6;  // Skip relocations.
895     uint16_t Flags = DE.getU16(&Offset);
896     W.printHex("Flags", Flags);
897     bool HasColumnInformation = Flags & codeview::LineFlags::HaveColumns;
898     uint32_t FunctionSize = DE.getU32(&Offset);
899     W.printHex("CodeSize", FunctionSize);
900     while (DE.isValidOffset(Offset)) {
901       // For each range of lines with the same filename, we have a segment
902       // in the line table.  The filename string is accessed using double
903       // indirection to the string table subsection using the index subsection.
904       uint32_t OffsetInIndex = DE.getU32(&Offset),
905                NumLines = DE.getU32(&Offset),
906                FullSegmentSize = DE.getU32(&Offset);
907
908       uint32_t ColumnOffset = Offset + 8 * NumLines;
909       DataExtractor ColumnDE(DE.getData(), true, 4);
910
911       if (FullSegmentSize !=
912           12 + 8 * NumLines + (HasColumnInformation ? 4 * NumLines : 0)) {
913         error(object_error::parse_failed);
914         return;
915       }
916
917       ListScope S(W, "FilenameSegment");
918       printFileNameForOffset("Filename", OffsetInIndex);
919       for (unsigned LineIdx = 0;
920            LineIdx != NumLines && DE.isValidOffset(Offset); ++LineIdx) {
921         // Then go the (PC, LineNumber) pairs.  The line number is stored in the
922         // least significant 31 bits of the respective word in the table.
923         uint32_t PC = DE.getU32(&Offset), LineData = DE.getU32(&Offset);
924         if (PC >= FunctionSize) {
925           error(object_error::parse_failed);
926           return;
927         }
928         char Buffer[32];
929         format("+0x%X", PC).snprint(Buffer, 32);
930         ListScope PCScope(W, Buffer);
931         LineInfo LI(LineData);
932         if (LI.isAlwaysStepInto())
933           W.printString("StepInto", StringRef("Always"));
934         else if (LI.isNeverStepInto())
935           W.printString("StepInto", StringRef("Never"));
936         else
937           W.printNumber("LineNumberStart", LI.getStartLine());
938         W.printNumber("LineNumberEndDelta", LI.getLineDelta());
939         W.printBoolean("IsStatement", LI.isStatement());
940         if (HasColumnInformation &&
941             ColumnDE.isValidOffsetForDataOfSize(ColumnOffset, 4)) {
942           uint16_t ColStart = ColumnDE.getU16(&ColumnOffset);
943           W.printNumber("ColStart", ColStart);
944           uint16_t ColEnd = ColumnDE.getU16(&ColumnOffset);
945           W.printNumber("ColEnd", ColEnd);
946         }
947       }
948       // Skip over the column data.
949       if (HasColumnInformation) {
950         for (unsigned LineIdx = 0;
951              LineIdx != NumLines && DE.isValidOffset(Offset); ++LineIdx) {
952           DE.getU32(&Offset);
953         }
954       }
955     }
956   }
957 }
958
959 void COFFDumper::printCodeViewSymbolsSubsection(StringRef Subsection,
960                                                 const SectionRef &Section,
961                                                 StringRef SectionContents) {
962   ArrayRef<uint8_t> BinaryData(Subsection.bytes_begin(),
963                                Subsection.bytes_end());
964   auto CODD = llvm::make_unique<COFFObjectDumpDelegate>(*this, Section, Obj,
965                                                         SectionContents);
966
967   CVSymbolDumper CVSD(W, TypeDB, std::move(CODD),
968                       opts::CodeViewSubsectionBytes);
969   BinaryByteStream Stream(BinaryData, llvm::support::little);
970   CVSymbolArray Symbols;
971   BinaryStreamReader Reader(Stream);
972   if (auto EC = Reader.readArray(Symbols, Reader.getLength())) {
973     consumeError(std::move(EC));
974     W.flush();
975     error(object_error::parse_failed);
976   }
977
978   if (auto EC = CVSD.dump(Symbols)) {
979     W.flush();
980     error(std::move(EC));
981   }
982   W.flush();
983 }
984
985 void COFFDumper::printCodeViewFileChecksums(StringRef Subsection) {
986   BinaryByteStream S(Subsection, llvm::support::little);
987   BinaryStreamReader SR(S);
988   while (!SR.empty()) {
989     DictScope S(W, "FileChecksum");
990     const FileChecksum *FC;
991     error(SR.readObject(FC));
992     if (FC->FileNameOffset >= CVStringTable.size())
993       error(object_error::parse_failed);
994     StringRef Filename =
995         CVStringTable.drop_front(FC->FileNameOffset).split('\0').first;
996     W.printHex("Filename", Filename, FC->FileNameOffset);
997     W.printHex("ChecksumSize", FC->ChecksumSize);
998     W.printEnum("ChecksumKind", uint8_t(FC->ChecksumKind),
999                 makeArrayRef(FileChecksumKindNames));
1000     if (FC->ChecksumSize >= SR.bytesRemaining())
1001       error(object_error::parse_failed);
1002     ArrayRef<uint8_t> ChecksumBytes;
1003     error(SR.readBytes(ChecksumBytes, FC->ChecksumSize));
1004     W.printBinary("ChecksumBytes", ChecksumBytes);
1005     unsigned PaddedSize = alignTo(FC->ChecksumSize + sizeof(FileChecksum), 4) -
1006                           sizeof(FileChecksum);
1007     PaddedSize -= ChecksumBytes.size();
1008     if (PaddedSize > SR.bytesRemaining())
1009       error(object_error::parse_failed);
1010     error(SR.skip(PaddedSize));
1011   }
1012 }
1013
1014 void COFFDumper::printCodeViewInlineeLines(StringRef Subsection) {
1015   BinaryByteStream S(Subsection, llvm::support::little);
1016   BinaryStreamReader SR(S);
1017   uint32_t Signature;
1018   error(SR.readInteger(Signature));
1019   bool HasExtraFiles = Signature == unsigned(InlineeLinesSignature::ExtraFiles);
1020
1021   while (!SR.empty()) {
1022     const InlineeSourceLine *ISL;
1023     error(SR.readObject(ISL));
1024     DictScope S(W, "InlineeSourceLine");
1025     printTypeIndex("Inlinee", ISL->Inlinee);
1026     printFileNameForOffset("FileID", ISL->FileID);
1027     W.printNumber("SourceLineNum", ISL->SourceLineNum);
1028
1029     if (HasExtraFiles) {
1030       uint32_t ExtraFileCount;
1031       error(SR.readInteger(ExtraFileCount));
1032       W.printNumber("ExtraFileCount", ExtraFileCount);
1033       ListScope ExtraFiles(W, "ExtraFiles");
1034       for (unsigned I = 0; I < ExtraFileCount; ++I) {
1035         uint32_t FileID;
1036         error(SR.readInteger(FileID));
1037         printFileNameForOffset("FileID", FileID);
1038       }
1039     }
1040   }
1041 }
1042
1043 StringRef COFFDumper::getFileNameForFileOffset(uint32_t FileOffset) {
1044   // The file checksum subsection should precede all references to it.
1045   if (!CVFileChecksumTable.data() || !CVStringTable.data())
1046     error(object_error::parse_failed);
1047   // Check if the file checksum table offset is valid.
1048   if (FileOffset >= CVFileChecksumTable.size())
1049     error(object_error::parse_failed);
1050
1051   // The string table offset comes first before the file checksum.
1052   StringRef Data = CVFileChecksumTable.drop_front(FileOffset);
1053   uint32_t StringOffset;
1054   error(consume(Data, StringOffset));
1055
1056   // Check if the string table offset is valid.
1057   if (StringOffset >= CVStringTable.size())
1058     error(object_error::parse_failed);
1059
1060   // Return the null-terminated string.
1061   return CVStringTable.drop_front(StringOffset).split('\0').first;
1062 }
1063
1064 void COFFDumper::printFileNameForOffset(StringRef Label, uint32_t FileOffset) {
1065   W.printHex(Label, getFileNameForFileOffset(FileOffset), FileOffset);
1066 }
1067
1068 void COFFDumper::mergeCodeViewTypes(TypeTableBuilder &CVIDs,
1069                                     TypeTableBuilder &CVTypes) {
1070   for (const SectionRef &S : Obj->sections()) {
1071     StringRef SectionName;
1072     error(S.getName(SectionName));
1073     if (SectionName == ".debug$T") {
1074       StringRef Data;
1075       error(S.getContents(Data));
1076       uint32_t Magic;
1077       error(consume(Data, Magic));
1078       if (Magic != 4)
1079         error(object_error::parse_failed);
1080       ArrayRef<uint8_t> Bytes(reinterpret_cast<const uint8_t *>(Data.data()),
1081                               Data.size());
1082       BinaryByteStream Stream(Bytes, llvm::support::little);
1083       CVTypeArray Types;
1084       BinaryStreamReader Reader(Stream);
1085       if (auto EC = Reader.readArray(Types, Reader.getLength())) {
1086         consumeError(std::move(EC));
1087         W.flush();
1088         error(object_error::parse_failed);
1089       }
1090
1091       if (auto EC = mergeTypeStreams(CVIDs, CVTypes, nullptr, Types))
1092         return error(std::move(EC));
1093     }
1094   }
1095 }
1096
1097 void COFFDumper::printCodeViewTypeSection(StringRef SectionName,
1098                                           const SectionRef &Section) {
1099   ListScope D(W, "CodeViewTypes");
1100   W.printNumber("Section", SectionName, Obj->getSectionID(Section));
1101
1102   StringRef Data;
1103   error(Section.getContents(Data));
1104   if (opts::CodeViewSubsectionBytes)
1105     W.printBinaryBlock("Data", Data);
1106
1107   uint32_t Magic;
1108   error(consume(Data, Magic));
1109   W.printHex("Magic", Magic);
1110   if (Magic != COFF::DEBUG_SECTION_MAGIC)
1111     return error(object_error::parse_failed);
1112
1113   CVTypeDumper CVTD(TypeDB);
1114   TypeDumpVisitor TDV(TypeDB, &W, opts::CodeViewSubsectionBytes);
1115   if (auto EC = CVTD.dump({Data.bytes_begin(), Data.bytes_end()}, TDV)) {
1116     W.flush();
1117     error(llvm::errorToErrorCode(std::move(EC)));
1118   }
1119 }
1120
1121 void COFFDumper::printSections() {
1122   ListScope SectionsD(W, "Sections");
1123   int SectionNumber = 0;
1124   for (const SectionRef &Sec : Obj->sections()) {
1125     ++SectionNumber;
1126     const coff_section *Section = Obj->getCOFFSection(Sec);
1127
1128     StringRef Name;
1129     error(Sec.getName(Name));
1130
1131     DictScope D(W, "Section");
1132     W.printNumber("Number", SectionNumber);
1133     W.printBinary("Name", Name, Section->Name);
1134     W.printHex   ("VirtualSize", Section->VirtualSize);
1135     W.printHex   ("VirtualAddress", Section->VirtualAddress);
1136     W.printNumber("RawDataSize", Section->SizeOfRawData);
1137     W.printHex   ("PointerToRawData", Section->PointerToRawData);
1138     W.printHex   ("PointerToRelocations", Section->PointerToRelocations);
1139     W.printHex   ("PointerToLineNumbers", Section->PointerToLinenumbers);
1140     W.printNumber("RelocationCount", Section->NumberOfRelocations);
1141     W.printNumber("LineNumberCount", Section->NumberOfLinenumbers);
1142     W.printFlags ("Characteristics", Section->Characteristics,
1143                     makeArrayRef(ImageSectionCharacteristics),
1144                     COFF::SectionCharacteristics(0x00F00000));
1145
1146     if (opts::SectionRelocations) {
1147       ListScope D(W, "Relocations");
1148       for (const RelocationRef &Reloc : Sec.relocations())
1149         printRelocation(Sec, Reloc);
1150     }
1151
1152     if (opts::SectionSymbols) {
1153       ListScope D(W, "Symbols");
1154       for (const SymbolRef &Symbol : Obj->symbols()) {
1155         if (!Sec.containsSymbol(Symbol))
1156           continue;
1157
1158         printSymbol(Symbol);
1159       }
1160     }
1161
1162     if (opts::SectionData &&
1163         !(Section->Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA)) {
1164       StringRef Data;
1165       error(Sec.getContents(Data));
1166
1167       W.printBinaryBlock("SectionData", Data);
1168     }
1169   }
1170 }
1171
1172 void COFFDumper::printRelocations() {
1173   ListScope D(W, "Relocations");
1174
1175   int SectionNumber = 0;
1176   for (const SectionRef &Section : Obj->sections()) {
1177     ++SectionNumber;
1178     StringRef Name;
1179     error(Section.getName(Name));
1180
1181     bool PrintedGroup = false;
1182     for (const RelocationRef &Reloc : Section.relocations()) {
1183       if (!PrintedGroup) {
1184         W.startLine() << "Section (" << SectionNumber << ") " << Name << " {\n";
1185         W.indent();
1186         PrintedGroup = true;
1187       }
1188
1189       printRelocation(Section, Reloc);
1190     }
1191
1192     if (PrintedGroup) {
1193       W.unindent();
1194       W.startLine() << "}\n";
1195     }
1196   }
1197 }
1198
1199 void COFFDumper::printRelocation(const SectionRef &Section,
1200                                  const RelocationRef &Reloc, uint64_t Bias) {
1201   uint64_t Offset = Reloc.getOffset() - Bias;
1202   uint64_t RelocType = Reloc.getType();
1203   SmallString<32> RelocName;
1204   StringRef SymbolName;
1205   Reloc.getTypeName(RelocName);
1206   symbol_iterator Symbol = Reloc.getSymbol();
1207   if (Symbol != Obj->symbol_end()) {
1208     Expected<StringRef> SymbolNameOrErr = Symbol->getName();
1209     error(errorToErrorCode(SymbolNameOrErr.takeError()));
1210     SymbolName = *SymbolNameOrErr;
1211   }
1212
1213   if (opts::ExpandRelocs) {
1214     DictScope Group(W, "Relocation");
1215     W.printHex("Offset", Offset);
1216     W.printNumber("Type", RelocName, RelocType);
1217     W.printString("Symbol", SymbolName.empty() ? "-" : SymbolName);
1218   } else {
1219     raw_ostream& OS = W.startLine();
1220     OS << W.hex(Offset)
1221        << " " << RelocName
1222        << " " << (SymbolName.empty() ? "-" : SymbolName)
1223        << "\n";
1224   }
1225 }
1226
1227 void COFFDumper::printSymbols() {
1228   ListScope Group(W, "Symbols");
1229
1230   for (const SymbolRef &Symbol : Obj->symbols())
1231     printSymbol(Symbol);
1232 }
1233
1234 void COFFDumper::printDynamicSymbols() { ListScope Group(W, "DynamicSymbols"); }
1235
1236 static ErrorOr<StringRef>
1237 getSectionName(const llvm::object::COFFObjectFile *Obj, int32_t SectionNumber,
1238                const coff_section *Section) {
1239   if (Section) {
1240     StringRef SectionName;
1241     if (std::error_code EC = Obj->getSectionName(Section, SectionName))
1242       return EC;
1243     return SectionName;
1244   }
1245   if (SectionNumber == llvm::COFF::IMAGE_SYM_DEBUG)
1246     return StringRef("IMAGE_SYM_DEBUG");
1247   if (SectionNumber == llvm::COFF::IMAGE_SYM_ABSOLUTE)
1248     return StringRef("IMAGE_SYM_ABSOLUTE");
1249   if (SectionNumber == llvm::COFF::IMAGE_SYM_UNDEFINED)
1250     return StringRef("IMAGE_SYM_UNDEFINED");
1251   return StringRef("");
1252 }
1253
1254 void COFFDumper::printSymbol(const SymbolRef &Sym) {
1255   DictScope D(W, "Symbol");
1256
1257   COFFSymbolRef Symbol = Obj->getCOFFSymbol(Sym);
1258   const coff_section *Section;
1259   if (std::error_code EC = Obj->getSection(Symbol.getSectionNumber(), Section)) {
1260     W.startLine() << "Invalid section number: " << EC.message() << "\n";
1261     W.flush();
1262     return;
1263   }
1264
1265   StringRef SymbolName;
1266   if (Obj->getSymbolName(Symbol, SymbolName))
1267     SymbolName = "";
1268
1269   StringRef SectionName = "";
1270   ErrorOr<StringRef> Res =
1271       getSectionName(Obj, Symbol.getSectionNumber(), Section);
1272   if (Res)
1273     SectionName = *Res;
1274
1275   W.printString("Name", SymbolName);
1276   W.printNumber("Value", Symbol.getValue());
1277   W.printNumber("Section", SectionName, Symbol.getSectionNumber());
1278   W.printEnum  ("BaseType", Symbol.getBaseType(), makeArrayRef(ImageSymType));
1279   W.printEnum  ("ComplexType", Symbol.getComplexType(),
1280                                                    makeArrayRef(ImageSymDType));
1281   W.printEnum  ("StorageClass", Symbol.getStorageClass(),
1282                                                    makeArrayRef(ImageSymClass));
1283   W.printNumber("AuxSymbolCount", Symbol.getNumberOfAuxSymbols());
1284
1285   for (uint8_t I = 0; I < Symbol.getNumberOfAuxSymbols(); ++I) {
1286     if (Symbol.isFunctionDefinition()) {
1287       const coff_aux_function_definition *Aux;
1288       error(getSymbolAuxData(Obj, Symbol, I, Aux));
1289
1290       DictScope AS(W, "AuxFunctionDef");
1291       W.printNumber("TagIndex", Aux->TagIndex);
1292       W.printNumber("TotalSize", Aux->TotalSize);
1293       W.printHex("PointerToLineNumber", Aux->PointerToLinenumber);
1294       W.printHex("PointerToNextFunction", Aux->PointerToNextFunction);
1295
1296     } else if (Symbol.isAnyUndefined()) {
1297       const coff_aux_weak_external *Aux;
1298       error(getSymbolAuxData(Obj, Symbol, I, Aux));
1299
1300       ErrorOr<COFFSymbolRef> Linked = Obj->getSymbol(Aux->TagIndex);
1301       StringRef LinkedName;
1302       std::error_code EC = Linked.getError();
1303       if (EC || (EC = Obj->getSymbolName(*Linked, LinkedName))) {
1304         LinkedName = "";
1305         error(EC);
1306       }
1307
1308       DictScope AS(W, "AuxWeakExternal");
1309       W.printNumber("Linked", LinkedName, Aux->TagIndex);
1310       W.printEnum  ("Search", Aux->Characteristics,
1311                     makeArrayRef(WeakExternalCharacteristics));
1312
1313     } else if (Symbol.isFileRecord()) {
1314       const char *FileName;
1315       error(getSymbolAuxData(Obj, Symbol, I, FileName));
1316
1317       DictScope AS(W, "AuxFileRecord");
1318
1319       StringRef Name(FileName, Symbol.getNumberOfAuxSymbols() *
1320                                    Obj->getSymbolTableEntrySize());
1321       W.printString("FileName", Name.rtrim(StringRef("\0", 1)));
1322       break;
1323     } else if (Symbol.isSectionDefinition()) {
1324       const coff_aux_section_definition *Aux;
1325       error(getSymbolAuxData(Obj, Symbol, I, Aux));
1326
1327       int32_t AuxNumber = Aux->getNumber(Symbol.isBigObj());
1328
1329       DictScope AS(W, "AuxSectionDef");
1330       W.printNumber("Length", Aux->Length);
1331       W.printNumber("RelocationCount", Aux->NumberOfRelocations);
1332       W.printNumber("LineNumberCount", Aux->NumberOfLinenumbers);
1333       W.printHex("Checksum", Aux->CheckSum);
1334       W.printNumber("Number", AuxNumber);
1335       W.printEnum("Selection", Aux->Selection, makeArrayRef(ImageCOMDATSelect));
1336
1337       if (Section && Section->Characteristics & COFF::IMAGE_SCN_LNK_COMDAT
1338           && Aux->Selection == COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE) {
1339         const coff_section *Assoc;
1340         StringRef AssocName = "";
1341         std::error_code EC = Obj->getSection(AuxNumber, Assoc);
1342         ErrorOr<StringRef> Res = getSectionName(Obj, AuxNumber, Assoc);
1343         if (Res)
1344           AssocName = *Res;
1345         if (!EC)
1346           EC = Res.getError();
1347         if (EC) {
1348           AssocName = "";
1349           error(EC);
1350         }
1351
1352         W.printNumber("AssocSection", AssocName, AuxNumber);
1353       }
1354     } else if (Symbol.isCLRToken()) {
1355       const coff_aux_clr_token *Aux;
1356       error(getSymbolAuxData(Obj, Symbol, I, Aux));
1357
1358       ErrorOr<COFFSymbolRef> ReferredSym =
1359           Obj->getSymbol(Aux->SymbolTableIndex);
1360       StringRef ReferredName;
1361       std::error_code EC = ReferredSym.getError();
1362       if (EC || (EC = Obj->getSymbolName(*ReferredSym, ReferredName))) {
1363         ReferredName = "";
1364         error(EC);
1365       }
1366
1367       DictScope AS(W, "AuxCLRToken");
1368       W.printNumber("AuxType", Aux->AuxType);
1369       W.printNumber("Reserved", Aux->Reserved);
1370       W.printNumber("SymbolTableIndex", ReferredName, Aux->SymbolTableIndex);
1371
1372     } else {
1373       W.startLine() << "<unhandled auxiliary record>\n";
1374     }
1375   }
1376 }
1377
1378 void COFFDumper::printUnwindInfo() {
1379   ListScope D(W, "UnwindInformation");
1380   switch (Obj->getMachine()) {
1381   case COFF::IMAGE_FILE_MACHINE_AMD64: {
1382     Win64EH::Dumper Dumper(W);
1383     Win64EH::Dumper::SymbolResolver
1384     Resolver = [](const object::coff_section *Section, uint64_t Offset,
1385                   SymbolRef &Symbol, void *user_data) -> std::error_code {
1386       COFFDumper *Dumper = reinterpret_cast<COFFDumper *>(user_data);
1387       return Dumper->resolveSymbol(Section, Offset, Symbol);
1388     };
1389     Win64EH::Dumper::Context Ctx(*Obj, Resolver, this);
1390     Dumper.printData(Ctx);
1391     break;
1392   }
1393   case COFF::IMAGE_FILE_MACHINE_ARMNT: {
1394     ARM::WinEH::Decoder Decoder(W);
1395     Decoder.dumpProcedureData(*Obj);
1396     break;
1397   }
1398   default:
1399     W.printEnum("unsupported Image Machine", Obj->getMachine(),
1400                 makeArrayRef(ImageFileMachineType));
1401     break;
1402   }
1403 }
1404
1405 void COFFDumper::printImportedSymbols(
1406     iterator_range<imported_symbol_iterator> Range) {
1407   for (const ImportedSymbolRef &I : Range) {
1408     StringRef Sym;
1409     error(I.getSymbolName(Sym));
1410     uint16_t Ordinal;
1411     error(I.getOrdinal(Ordinal));
1412     W.printNumber("Symbol", Sym, Ordinal);
1413   }
1414 }
1415
1416 void COFFDumper::printDelayImportedSymbols(
1417     const DelayImportDirectoryEntryRef &I,
1418     iterator_range<imported_symbol_iterator> Range) {
1419   int Index = 0;
1420   for (const ImportedSymbolRef &S : Range) {
1421     DictScope Import(W, "Import");
1422     StringRef Sym;
1423     error(S.getSymbolName(Sym));
1424     uint16_t Ordinal;
1425     error(S.getOrdinal(Ordinal));
1426     W.printNumber("Symbol", Sym, Ordinal);
1427     uint64_t Addr;
1428     error(I.getImportAddress(Index++, Addr));
1429     W.printHex("Address", Addr);
1430   }
1431 }
1432
1433 void COFFDumper::printCOFFImports() {
1434   // Regular imports
1435   for (const ImportDirectoryEntryRef &I : Obj->import_directories()) {
1436     DictScope Import(W, "Import");
1437     StringRef Name;
1438     error(I.getName(Name));
1439     W.printString("Name", Name);
1440     uint32_t ILTAddr;
1441     error(I.getImportLookupTableRVA(ILTAddr));
1442     W.printHex("ImportLookupTableRVA", ILTAddr);
1443     uint32_t IATAddr;
1444     error(I.getImportAddressTableRVA(IATAddr));
1445     W.printHex("ImportAddressTableRVA", IATAddr);
1446     // The import lookup table can be missing with certain older linkers, so
1447     // fall back to the import address table in that case.
1448     if (ILTAddr)
1449       printImportedSymbols(I.lookup_table_symbols());
1450     else
1451       printImportedSymbols(I.imported_symbols());
1452   }
1453
1454   // Delay imports
1455   for (const DelayImportDirectoryEntryRef &I : Obj->delay_import_directories()) {
1456     DictScope Import(W, "DelayImport");
1457     StringRef Name;
1458     error(I.getName(Name));
1459     W.printString("Name", Name);
1460     const delay_import_directory_table_entry *Table;
1461     error(I.getDelayImportTable(Table));
1462     W.printHex("Attributes", Table->Attributes);
1463     W.printHex("ModuleHandle", Table->ModuleHandle);
1464     W.printHex("ImportAddressTable", Table->DelayImportAddressTable);
1465     W.printHex("ImportNameTable", Table->DelayImportNameTable);
1466     W.printHex("BoundDelayImportTable", Table->BoundDelayImportTable);
1467     W.printHex("UnloadDelayImportTable", Table->UnloadDelayImportTable);
1468     printDelayImportedSymbols(I, I.imported_symbols());
1469   }
1470 }
1471
1472 void COFFDumper::printCOFFExports() {
1473   for (const ExportDirectoryEntryRef &E : Obj->export_directories()) {
1474     DictScope Export(W, "Export");
1475
1476     StringRef Name;
1477     uint32_t Ordinal, RVA;
1478
1479     error(E.getSymbolName(Name));
1480     error(E.getOrdinal(Ordinal));
1481     error(E.getExportRVA(RVA));
1482
1483     W.printNumber("Ordinal", Ordinal);
1484     W.printString("Name", Name);
1485     W.printHex("RVA", RVA);
1486   }
1487 }
1488
1489 void COFFDumper::printCOFFDirectives() {
1490   for (const SectionRef &Section : Obj->sections()) {
1491     StringRef Contents;
1492     StringRef Name;
1493
1494     error(Section.getName(Name));
1495     if (Name != ".drectve")
1496       continue;
1497
1498     error(Section.getContents(Contents));
1499
1500     W.printString("Directive(s)", Contents);
1501   }
1502 }
1503
1504 static StringRef getBaseRelocTypeName(uint8_t Type) {
1505   switch (Type) {
1506   case COFF::IMAGE_REL_BASED_ABSOLUTE: return "ABSOLUTE";
1507   case COFF::IMAGE_REL_BASED_HIGH: return "HIGH";
1508   case COFF::IMAGE_REL_BASED_LOW: return "LOW";
1509   case COFF::IMAGE_REL_BASED_HIGHLOW: return "HIGHLOW";
1510   case COFF::IMAGE_REL_BASED_HIGHADJ: return "HIGHADJ";
1511   case COFF::IMAGE_REL_BASED_ARM_MOV32T: return "ARM_MOV32(T)";
1512   case COFF::IMAGE_REL_BASED_DIR64: return "DIR64";
1513   default: return "unknown (" + llvm::utostr(Type) + ")";
1514   }
1515 }
1516
1517 void COFFDumper::printCOFFBaseReloc() {
1518   ListScope D(W, "BaseReloc");
1519   for (const BaseRelocRef &I : Obj->base_relocs()) {
1520     uint8_t Type;
1521     uint32_t RVA;
1522     error(I.getRVA(RVA));
1523     error(I.getType(Type));
1524     DictScope Import(W, "Entry");
1525     W.printString("Type", getBaseRelocTypeName(Type));
1526     W.printHex("Address", RVA);
1527   }
1528 }
1529
1530 void COFFDumper::printStackMap() const {
1531   object::SectionRef StackMapSection;
1532   for (auto Sec : Obj->sections()) {
1533     StringRef Name;
1534     Sec.getName(Name);
1535     if (Name == ".llvm_stackmaps") {
1536       StackMapSection = Sec;
1537       break;
1538     }
1539   }
1540
1541   if (StackMapSection == object::SectionRef())
1542     return;
1543
1544   StringRef StackMapContents;
1545   StackMapSection.getContents(StackMapContents);
1546   ArrayRef<uint8_t> StackMapContentsArray(
1547       reinterpret_cast<const uint8_t*>(StackMapContents.data()),
1548       StackMapContents.size());
1549
1550   if (Obj->isLittleEndian())
1551     prettyPrintStackMap(
1552                       llvm::outs(),
1553                       StackMapV2Parser<support::little>(StackMapContentsArray));
1554   else
1555     prettyPrintStackMap(llvm::outs(),
1556                         StackMapV2Parser<support::big>(StackMapContentsArray));
1557 }
1558
1559 void llvm::dumpCodeViewMergedTypes(ScopedPrinter &Writer,
1560                                    llvm::codeview::TypeTableBuilder &IDTable,
1561                                    llvm::codeview::TypeTableBuilder &CVTypes) {
1562   // Flatten it first, then run our dumper on it.
1563   SmallString<0> TypeBuf;
1564   CVTypes.ForEachRecord([&](TypeIndex TI, ArrayRef<uint8_t> Record) {
1565     TypeBuf.append(Record.begin(), Record.end());
1566   });
1567
1568   TypeDatabase TypeDB;
1569   {
1570     ListScope S(Writer, "MergedTypeStream");
1571     CVTypeDumper CVTD(TypeDB);
1572     TypeDumpVisitor TDV(TypeDB, &Writer, opts::CodeViewSubsectionBytes);
1573     if (auto EC = CVTD.dump(
1574             {TypeBuf.str().bytes_begin(), TypeBuf.str().bytes_end()}, TDV)) {
1575       Writer.flush();
1576       error(std::move(EC));
1577     }
1578   }
1579
1580   // Flatten the id stream and print it next. The ID stream refers to names from
1581   // the type stream.
1582   SmallString<0> IDBuf;
1583   IDTable.ForEachRecord([&](TypeIndex TI, ArrayRef<uint8_t> Record) {
1584     IDBuf.append(Record.begin(), Record.end());
1585   });
1586
1587   {
1588     ListScope S(Writer, "MergedIDStream");
1589     TypeDatabase IDDB;
1590     CVTypeDumper CVTD(IDDB);
1591     TypeDumpVisitor TDV(TypeDB, &Writer, opts::CodeViewSubsectionBytes);
1592     TDV.setItemDB(IDDB);
1593     if (auto EC = CVTD.dump(
1594             {IDBuf.str().bytes_begin(), IDBuf.str().bytes_end()}, TDV)) {
1595       Writer.flush();
1596       error(std::move(EC));
1597     }
1598   }
1599 }