1 //===- CodeViewYAMLTypeHashing.cpp - CodeView YAMLIO type hashing ---------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines classes for handling the YAML representation of CodeView
13 //===----------------------------------------------------------------------===//
15 #include "llvm/ObjectYAML/CodeViewYAMLTypeHashing.h"
17 #include "llvm/BinaryFormat/COFF.h"
18 #include "llvm/Support/BinaryByteStream.h"
19 #include "llvm/Support/BinaryStreamReader.h"
20 #include "llvm/Support/BinaryStreamWriter.h"
23 using namespace llvm::codeview
;
24 using namespace llvm::CodeViewYAML
;
25 using namespace llvm::yaml
;
30 void MappingTraits
<DebugHSection
>::mapping(IO
&io
, DebugHSection
&DebugH
) {
31 io
.mapRequired("Version", DebugH
.Version
);
32 io
.mapRequired("HashAlgorithm", DebugH
.HashAlgorithm
);
33 io
.mapOptional("HashValues", DebugH
.Hashes
);
36 void ScalarTraits
<GlobalHash
>::output(const GlobalHash
&GH
, void *Ctx
,
38 ScalarTraits
<BinaryRef
>::output(GH
.Hash
, Ctx
, OS
);
41 StringRef ScalarTraits
<GlobalHash
>::input(StringRef Scalar
, void *Ctx
,
43 return ScalarTraits
<BinaryRef
>::input(Scalar
, Ctx
, GH
.Hash
);
46 } // end namespace yaml
47 } // end namespace llvm
49 DebugHSection
llvm::CodeViewYAML::fromDebugH(ArrayRef
<uint8_t> DebugH
) {
50 assert(DebugH
.size() >= 8);
51 assert((DebugH
.size() - 8) % 8 == 0);
53 BinaryStreamReader
Reader(DebugH
, llvm::support::little
);
55 cantFail(Reader
.readInteger(DHS
.Magic
));
56 cantFail(Reader
.readInteger(DHS
.Version
));
57 cantFail(Reader
.readInteger(DHS
.HashAlgorithm
));
59 while (Reader
.bytesRemaining() != 0) {
61 cantFail(Reader
.readBytes(S
, 8));
62 DHS
.Hashes
.emplace_back(S
);
64 assert(Reader
.bytesRemaining() == 0);
68 ArrayRef
<uint8_t> llvm::CodeViewYAML::toDebugH(const DebugHSection
&DebugH
,
69 BumpPtrAllocator
&Alloc
) {
70 uint32_t Size
= 8 + 8 * DebugH
.Hashes
.size();
71 uint8_t *Data
= Alloc
.Allocate
<uint8_t>(Size
);
72 MutableArrayRef
<uint8_t> Buffer(Data
, Size
);
73 BinaryStreamWriter
Writer(Buffer
, llvm::support::little
);
75 cantFail(Writer
.writeInteger(DebugH
.Magic
));
76 cantFail(Writer
.writeInteger(DebugH
.Version
));
77 cantFail(Writer
.writeInteger(DebugH
.HashAlgorithm
));
79 for (const auto &H
: DebugH
.Hashes
) {
81 raw_svector_ostream
OS(Hash
);
82 H
.Hash
.writeAsBinary(OS
);
83 assert((Hash
.size() == 8) && "Invalid hash size!");
84 cantFail(Writer
.writeFixedString(Hash
));
86 assert(Writer
.bytesRemaining() == 0);