1 //===- CodeViewYAMLTypeHashing.cpp - CodeView YAMLIO type hashing ---------===//
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
7 //===----------------------------------------------------------------------===//
9 // This file defines classes for handling the YAML representation of CodeView
12 //===----------------------------------------------------------------------===//
14 #include "llvm/ObjectYAML/CodeViewYAMLTypeHashing.h"
16 #include "llvm/Support/BinaryStreamReader.h"
17 #include "llvm/Support/BinaryStreamWriter.h"
20 using namespace llvm::codeview
;
21 using namespace llvm::CodeViewYAML
;
22 using namespace llvm::yaml
;
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
);
33 void ScalarTraits
<GlobalHash
>::output(const GlobalHash
&GH
, void *Ctx
,
35 ScalarTraits
<BinaryRef
>::output(GH
.Hash
, Ctx
, OS
);
38 StringRef ScalarTraits
<GlobalHash
>::input(StringRef Scalar
, void *Ctx
,
40 return ScalarTraits
<BinaryRef
>::input(Scalar
, Ctx
, GH
.Hash
);
43 } // end namespace yaml
44 } // end namespace llvm
46 DebugHSection
llvm::CodeViewYAML::fromDebugH(ArrayRef
<uint8_t> DebugH
) {
47 assert(DebugH
.size() >= 8);
48 assert((DebugH
.size() - 8) % 8 == 0);
50 BinaryStreamReader
Reader(DebugH
, llvm::endianness::little
);
52 cantFail(Reader
.readInteger(DHS
.Magic
));
53 cantFail(Reader
.readInteger(DHS
.Version
));
54 cantFail(Reader
.readInteger(DHS
.HashAlgorithm
));
56 while (Reader
.bytesRemaining() != 0) {
58 cantFail(Reader
.readBytes(S
, 8));
59 DHS
.Hashes
.emplace_back(S
);
61 assert(Reader
.bytesRemaining() == 0);
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
);
72 cantFail(Writer
.writeInteger(DebugH
.Magic
));
73 cantFail(Writer
.writeInteger(DebugH
.Version
));
74 cantFail(Writer
.writeInteger(DebugH
.HashAlgorithm
));
76 for (const auto &H
: DebugH
.Hashes
) {
78 raw_svector_ostream
OS(Hash
);
79 H
.Hash
.writeAsBinary(OS
);
80 assert((Hash
.size() == 8) && "Invalid hash size!");
81 cantFail(Writer
.writeFixedString(Hash
));
83 assert(Writer
.bytesRemaining() == 0);