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