Update obj2yaml and yaml2obj for .debug$H section.

Differential Revision: https://reviews.llvm.org/D40842

llvm-svn: 319925
diff --git a/llvm/lib/ObjectYAML/CMakeLists.txt b/llvm/lib/ObjectYAML/CMakeLists.txt
index 7af0b9c..d24f879 100644
--- a/llvm/lib/ObjectYAML/CMakeLists.txt
+++ b/llvm/lib/ObjectYAML/CMakeLists.txt
@@ -1,7 +1,8 @@
 add_llvm_library(LLVMObjectYAML
-  CodeViewYAMLTypes.cpp
-  CodeViewYAMLSymbols.cpp
   CodeViewYAMLDebugSections.cpp
+  CodeViewYAMLSymbols.cpp
+  CodeViewYAMLTypeHashing.cpp
+  CodeViewYAMLTypes.cpp
   COFFYAML.cpp
   DWARFEmitter.cpp
   DWARFVisitor.cpp
diff --git a/llvm/lib/ObjectYAML/COFFYAML.cpp b/llvm/lib/ObjectYAML/COFFYAML.cpp
index 056a1aa..937b8dc 100644
--- a/llvm/lib/ObjectYAML/COFFYAML.cpp
+++ b/llvm/lib/ObjectYAML/COFFYAML.cpp
@@ -562,14 +562,16 @@
   IO.mapOptional("VirtualSize", Sec.Header.VirtualSize, 0U);
   IO.mapOptional("Alignment", Sec.Alignment, 0U);
 
-  // If this is a .debug$S or .debug$T section parse the semantic representation
-  // of the symbols/types.  If it is any other kind of section, just deal in raw
-  // bytes.
+  // If this is a .debug$S .debug$T, or .debug$H section parse the semantic
+  // representation of the symbols/types.  If it is any other kind of section,
+  // just deal in raw bytes.
   IO.mapOptional("SectionData", Sec.SectionData);
   if (Sec.Name == ".debug$S")
     IO.mapOptional("Subsections", Sec.DebugS);
   else if (Sec.Name == ".debug$T")
     IO.mapOptional("Types", Sec.DebugT);
+  else if (Sec.Name == ".debug$H")
+    IO.mapOptional("GlobalHashes", Sec.DebugH);
 
   IO.mapOptional("Relocations", Sec.Relocations);
 }
diff --git a/llvm/lib/ObjectYAML/CodeViewYAMLTypeHashing.cpp b/llvm/lib/ObjectYAML/CodeViewYAMLTypeHashing.cpp
new file mode 100644
index 0000000..b35e8ab
--- /dev/null
+++ b/llvm/lib/ObjectYAML/CodeViewYAMLTypeHashing.cpp
@@ -0,0 +1,84 @@
+//===- CodeViewYAMLTypeHashing.cpp - CodeView YAMLIO type hashing ---------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// This file defines classes for handling the YAML representation of CodeView
+// Debug Info.
+//
+//===----------------------------------------------------------------------===//
+
+#include "llvm/ObjectYAML/CodeViewYAMLTypeHashing.h"
+#include "llvm/Support/BinaryByteStream.h"
+#include "llvm/Support/BinaryStreamReader.h"
+#include "llvm/Support/BinaryStreamWriter.h"
+
+using namespace llvm;
+using namespace llvm::codeview;
+using namespace llvm::CodeViewYAML;
+using namespace llvm::yaml;
+
+namespace llvm {
+namespace yaml {
+
+void MappingTraits<DebugHSection>::mapping(IO &io, DebugHSection &DebugH) {
+  io.mapRequired("Version", DebugH.Version);
+  io.mapRequired("HashAlgorithm", DebugH.HashAlgorithm);
+  io.mapOptional("HashValues", DebugH.Hashes);
+}
+
+void ScalarTraits<GlobalHash>::output(const GlobalHash &GH, void *Ctx,
+                                      raw_ostream &OS) {
+  ScalarTraits<BinaryRef>::output(GH.Hash, Ctx, OS);
+}
+
+StringRef ScalarTraits<GlobalHash>::input(StringRef Scalar, void *Ctx,
+                                          GlobalHash &GH) {
+  return ScalarTraits<BinaryRef>::input(Scalar, Ctx, GH.Hash);
+}
+
+} // end namespace yaml
+} // end namespace llvm
+
+DebugHSection llvm::CodeViewYAML::fromDebugH(ArrayRef<uint8_t> DebugT) {
+  assert(DebugT.size() >= 8);
+  assert((DebugT.size() - 8) % 20 == 0);
+
+  BinaryStreamReader Reader(DebugT, llvm::support::little);
+  DebugHSection DHS;
+  cantFail(Reader.readInteger(DHS.Magic));
+  cantFail(Reader.readInteger(DHS.Version));
+  cantFail(Reader.readInteger(DHS.HashAlgorithm));
+  while (Reader.bytesRemaining() != 0) {
+    ArrayRef<uint8_t> S;
+    cantFail(Reader.readBytes(S, 20));
+    DHS.Hashes.emplace_back(S);
+  }
+  assert(Reader.bytesRemaining() == 0);
+  return DHS;
+}
+
+ArrayRef<uint8_t> llvm::CodeViewYAML::toDebugH(const DebugHSection &DebugH,
+                                               BumpPtrAllocator &Alloc) {
+  uint32_t Size = 8 + 20 * DebugH.Hashes.size();
+  uint8_t *Data = Alloc.Allocate<uint8_t>(Size);
+  MutableArrayRef<uint8_t> Buffer(Data, Size);
+  BinaryStreamWriter Writer(Buffer, llvm::support::little);
+  cantFail(Writer.writeInteger(DebugH.Magic));
+  cantFail(Writer.writeInteger(DebugH.Version));
+  cantFail(Writer.writeInteger(DebugH.HashAlgorithm));
+  SmallString<20> Hash;
+  for (const auto &H : DebugH.Hashes) {
+    Hash.clear();
+    raw_svector_ostream OS(Hash);
+    H.Hash.writeAsBinary(OS);
+    assert((Hash.size() == 20) && "Invalid hash size!");
+    cantFail(Writer.writeFixedString(Hash));
+  }
+  assert(Writer.bytesRemaining() == 0);
+  return Buffer;
+}