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