xref: /llvm-project/llvm/lib/ObjectYAML/CodeViewYAMLTypeHashing.cpp (revision 5b7102d1f37eab7a8f17b7bf4124ca76fbdbd66d)
1 //===- CodeViewYAMLTypeHashing.cpp - CodeView YAMLIO type hashing ---------===//
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 // This file defines classes for handling the YAML representation of CodeView
10 // Debug Info.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "llvm/ObjectYAML/CodeViewYAMLTypeHashing.h"
15 
16 #include "llvm/Support/BinaryStreamReader.h"
17 #include "llvm/Support/BinaryStreamWriter.h"
18 
19 using namespace llvm;
20 using namespace llvm::codeview;
21 using namespace llvm::CodeViewYAML;
22 using namespace llvm::yaml;
23 
24 namespace llvm {
25 namespace yaml {
26 
27 void MappingTraits<DebugHSection>::mapping(IO &io, DebugHSection &DebugH) {
28   io.mapRequired("Version", DebugH.Version);
29   io.mapRequired("HashAlgorithm", DebugH.HashAlgorithm);
30   io.mapOptional("HashValues", DebugH.Hashes);
31 }
32 
33 void ScalarTraits<GlobalHash>::output(const GlobalHash &GH, void *Ctx,
34                                       raw_ostream &OS) {
35   ScalarTraits<BinaryRef>::output(GH.Hash, Ctx, OS);
36 }
37 
38 StringRef ScalarTraits<GlobalHash>::input(StringRef Scalar, void *Ctx,
39                                           GlobalHash &GH) {
40   return ScalarTraits<BinaryRef>::input(Scalar, Ctx, GH.Hash);
41 }
42 
43 } // end namespace yaml
44 } // end namespace llvm
45 
46 DebugHSection llvm::CodeViewYAML::fromDebugH(ArrayRef<uint8_t> DebugH) {
47   assert(DebugH.size() >= 8);
48   assert((DebugH.size() - 8) % 8 == 0);
49 
50   BinaryStreamReader Reader(DebugH, llvm::endianness::little);
51   DebugHSection DHS;
52   cantFail(Reader.readInteger(DHS.Magic));
53   cantFail(Reader.readInteger(DHS.Version));
54   cantFail(Reader.readInteger(DHS.HashAlgorithm));
55 
56   while (Reader.bytesRemaining() != 0) {
57     ArrayRef<uint8_t> S;
58     cantFail(Reader.readBytes(S, 8));
59     DHS.Hashes.emplace_back(S);
60   }
61   assert(Reader.bytesRemaining() == 0);
62   return DHS;
63 }
64 
65 ArrayRef<uint8_t> llvm::CodeViewYAML::toDebugH(const DebugHSection &DebugH,
66                                                BumpPtrAllocator &Alloc) {
67   uint32_t Size = 8 + 8 * DebugH.Hashes.size();
68   uint8_t *Data = Alloc.Allocate<uint8_t>(Size);
69   MutableArrayRef<uint8_t> Buffer(Data, Size);
70   BinaryStreamWriter Writer(Buffer, llvm::endianness::little);
71 
72   cantFail(Writer.writeInteger(DebugH.Magic));
73   cantFail(Writer.writeInteger(DebugH.Version));
74   cantFail(Writer.writeInteger(DebugH.HashAlgorithm));
75   SmallString<8> Hash;
76   for (const auto &H : DebugH.Hashes) {
77     Hash.clear();
78     raw_svector_ostream OS(Hash);
79     H.Hash.writeAsBinary(OS);
80     assert((Hash.size() == 8) && "Invalid hash size!");
81     cantFail(Writer.writeFixedString(Hash));
82   }
83   assert(Writer.bytesRemaining() == 0);
84   return Buffer;
85 }
86