Do plumbing work for CodeView debug info.

Previously, we discarded .debug$ sections. This patch adds them to
files so that PDB.cpp can access them.

This patch also adds a debug option, /dumppdb, to dump debug info
fed to createPDB so that we can verify that valid data has been passed.

llvm-svn: 287555
diff --git a/lld/COFF/PDB.cpp b/lld/COFF/PDB.cpp
index 470c07a..97b7f69 100644
--- a/lld/COFF/PDB.cpp
+++ b/lld/COFF/PDB.cpp
@@ -9,9 +9,11 @@
 
 #include "PDB.h"
 #include "Chunks.h"
+#include "Config.h"
 #include "Error.h"
 #include "SymbolTable.h"
 #include "Symbols.h"
+#include "llvm/DebugInfo/CodeView/TypeDumper.h"
 #include "llvm/DebugInfo/MSF/MSFBuilder.h"
 #include "llvm/DebugInfo/MSF/MSFCommon.h"
 #include "llvm/DebugInfo/PDB/Raw/DbiStream.h"
@@ -25,11 +27,13 @@
 #include "llvm/Object/COFF.h"
 #include "llvm/Support/Endian.h"
 #include "llvm/Support/FileOutputBuffer.h"
+#include "llvm/Support/ScopedPrinter.h"
 #include <memory>
 
 using namespace lld;
 using namespace lld::coff;
 using namespace llvm;
+using namespace llvm::codeview;
 using namespace llvm::support;
 using namespace llvm::support::endian;
 
@@ -46,9 +50,35 @@
   return V;
 }
 
+static SectionChunk *findByName(std::vector<SectionChunk *> &Sections,
+                                StringRef Name) {
+  for (SectionChunk *C : Sections)
+    if (C->getSectionName() == Name)
+      return C;
+  return nullptr;
+}
+
+// Dump CodeView debug info. This is for debugging.
+static void dumpCodeView(SymbolTable *Symtab) {
+  ScopedPrinter W(outs());
+
+  for (ObjectFile *File : Symtab->ObjectFiles) {
+    SectionChunk *C = findByName(File->getDebugChunks(), ".debug$T");
+    if (!C)
+      continue;
+
+    CVTypeDumper TypeDumper(&W, false);
+    if (auto EC = TypeDumper.dump(C->getContents()))
+      fatal(EC, "CVTypeDumper::dump failed");
+  }
+}
+
 // Creates a PDB file.
 void coff::createPDB(StringRef Path, SymbolTable *Symtab,
                      ArrayRef<uint8_t> SectionTable) {
+  if (Config->DumpPdb)
+    dumpCodeView(Symtab);
+
   BumpPtrAllocator Alloc;
   pdb::PDBFileBuilder Builder(Alloc);
   ExitOnErr(Builder.initialize(4096)); // 4096 is blocksize