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(Key: "Version" , Val&: DebugH.Version); |
29 | io.mapRequired(Key: "HashAlgorithm" , Val&: DebugH.HashAlgorithm); |
30 | io.mapOptional(Key: "HashValues" , Val&: 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(Err: Reader.readInteger(Dest&: DHS.Magic)); |
53 | cantFail(Err: Reader.readInteger(Dest&: DHS.Version)); |
54 | cantFail(Err: Reader.readInteger(Dest&: DHS.HashAlgorithm)); |
55 | |
56 | while (Reader.bytesRemaining() != 0) { |
57 | ArrayRef<uint8_t> S; |
58 | cantFail(Err: Reader.readBytes(Buffer&: S, Size: 8)); |
59 | DHS.Hashes.emplace_back(args&: 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>(Num: Size); |
69 | MutableArrayRef<uint8_t> Buffer(Data, Size); |
70 | BinaryStreamWriter Writer(Buffer, llvm::endianness::little); |
71 | |
72 | cantFail(Err: Writer.writeInteger(Value: DebugH.Magic)); |
73 | cantFail(Err: Writer.writeInteger(Value: DebugH.Version)); |
74 | cantFail(Err: Writer.writeInteger(Value: 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(Err: Writer.writeFixedString(Str: Hash)); |
82 | } |
83 | assert(Writer.bytesRemaining() == 0); |
84 | return Buffer; |
85 | } |
86 | |