1 //===- DebugChecksumsSubsection.h -------------------------------*- C++ -*-===//
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 #ifndef LLVM_DEBUGINFO_CODEVIEW_DEBUGCHECKSUMSSUBSECTION_H
10 #define LLVM_DEBUGINFO_CODEVIEW_DEBUGCHECKSUMSSUBSECTION_H
11 
12 #include "llvm/ADT/ArrayRef.h"
13 #include "llvm/ADT/DenseMap.h"
14 #include "llvm/ADT/StringRef.h"
15 #include "llvm/DebugInfo/CodeView/CodeView.h"
16 #include "llvm/DebugInfo/CodeView/DebugSubsection.h"
17 #include "llvm/Support/Allocator.h"
18 #include "llvm/Support/BinaryStreamArray.h"
19 #include "llvm/Support/BinaryStreamRef.h"
20 #include "llvm/Support/Error.h"
21 #include <cstdint>
22 #include <vector>
23 
24 namespace llvm {
25 
26 class BinaryStreamReader;
27 class BinaryStreamWriter;
28 
29 namespace codeview {
30 
31 class DebugStringTableSubsection;
32 
33 struct FileChecksumEntry {
34   uint32_t FileNameOffset;    // Byte offset of filename in global stringtable.
35   FileChecksumKind Kind;      // The type of checksum.
36   ArrayRef<uint8_t> Checksum; // The bytes of the checksum.
37 };
38 
39 } // end namespace codeview
40 
41 template <> struct VarStreamArrayExtractor<codeview::FileChecksumEntry> {
42 public:
43   using ContextType = void;
44 
45   Error operator()(BinaryStreamRef Stream, uint32_t &Len,
46                    codeview::FileChecksumEntry &Item);
47 };
48 
49 namespace codeview {
50 
51 class DebugChecksumsSubsectionRef final : public DebugSubsectionRef {
52   using FileChecksumArray = VarStreamArray<codeview::FileChecksumEntry>;
53   using Iterator = FileChecksumArray::Iterator;
54 
55 public:
56   DebugChecksumsSubsectionRef()
57       : DebugSubsectionRef(DebugSubsectionKind::FileChecksums) {}
58 
59   static bool classof(const DebugSubsectionRef *S) {
60     return S->kind() == DebugSubsectionKind::FileChecksums;
61   }
62 
63   bool valid() const { return Checksums.valid(); }
64 
65   Error initialize(BinaryStreamReader Reader);
66   Error initialize(BinaryStreamRef Stream);
67 
68   Iterator begin() const { return Checksums.begin(); }
69   Iterator end() const { return Checksums.end(); }
70 
71   const FileChecksumArray &getArray() const { return Checksums; }
72 
73 private:
74   FileChecksumArray Checksums;
75 };
76 
77 class DebugChecksumsSubsection final : public DebugSubsection {
78 public:
79   explicit DebugChecksumsSubsection(DebugStringTableSubsection &Strings);
80 
81   static bool classof(const DebugSubsection *S) {
82     return S->kind() == DebugSubsectionKind::FileChecksums;
83   }
84 
85   void addChecksum(StringRef FileName, FileChecksumKind Kind,
86                    ArrayRef<uint8_t> Bytes);
87 
88   uint32_t calculateSerializedSize() const override;
89   Error commit(BinaryStreamWriter &Writer) const override;
90   uint32_t mapChecksumOffset(StringRef FileName) const;
91 
92 private:
93   DebugStringTableSubsection &Strings;
94 
95   DenseMap<uint32_t, uint32_t> OffsetMap;
96   uint32_t SerializedSize = 0;
97   BumpPtrAllocator Storage;
98   std::vector<FileChecksumEntry> Checksums;
99 };
100 
101 } // end namespace codeview
102 
103 } // end namespace llvm
104 
105 #endif // LLVM_DEBUGINFO_CODEVIEW_DEBUGCHECKSUMSSUBSECTION_H
106