1 //===- StringsAndChecksums.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_STRINGSANDCHECKSUMS_H
10 #define LLVM_DEBUGINFO_CODEVIEW_STRINGSANDCHECKSUMS_H
11 
12 #include "llvm/DebugInfo/CodeView/CodeView.h"
13 #include "llvm/DebugInfo/CodeView/DebugChecksumsSubsection.h"
14 #include "llvm/DebugInfo/CodeView/DebugStringTableSubsection.h"
15 #include "llvm/DebugInfo/CodeView/DebugSubsectionRecord.h"
16 #include <memory>
17 
18 namespace llvm {
19 namespace codeview {
20 
21 class StringsAndChecksumsRef {
22 public:
23   // If no subsections are known about initially, we find as much as we can.
24   StringsAndChecksumsRef();
25 
26   // If only a string table subsection is given, we find a checksums subsection.
27   explicit StringsAndChecksumsRef(const DebugStringTableSubsectionRef &Strings);
28 
29   // If both subsections are given, we don't need to find anything.
30   StringsAndChecksumsRef(const DebugStringTableSubsectionRef &Strings,
31                          const DebugChecksumsSubsectionRef &Checksums);
32 
33   void setStrings(const DebugStringTableSubsectionRef &Strings);
34   void setChecksums(const DebugChecksumsSubsectionRef &CS);
35 
36   void reset();
37   void resetStrings();
38   void resetChecksums();
39 
40   template <typename T> void initialize(T &&FragmentRange) {
41     for (const DebugSubsectionRecord &R : FragmentRange) {
42       if (Strings && Checksums)
43         return;
44       if (R.kind() == DebugSubsectionKind::FileChecksums) {
45         initializeChecksums(R);
46         continue;
47       }
48       if (R.kind() == DebugSubsectionKind::StringTable && !Strings) {
49         // While in practice we should never encounter a string table even
50         // though the string table is already initialized, in theory it's
51         // possible.  PDBs are supposed to have one global string table and
52         // then this subsection should not appear.  Whereas object files are
53         // supposed to have this subsection appear exactly once.  However,
54         // for testing purposes it's nice to be able to test this subsection
55         // independently of one format or the other, so for some tests we
56         // manually construct a PDB that contains this subsection in addition
57         // to a global string table.
58         initializeStrings(R);
59         continue;
60       }
61     }
62   }
63 
64   const DebugStringTableSubsectionRef &strings() const { return *Strings; }
65   const DebugChecksumsSubsectionRef &checksums() const { return *Checksums; }
66 
67   bool hasStrings() const { return Strings != nullptr; }
68   bool hasChecksums() const { return Checksums != nullptr; }
69 
70 private:
71   void initializeStrings(const DebugSubsectionRecord &SR);
72   void initializeChecksums(const DebugSubsectionRecord &FCR);
73 
74   std::shared_ptr<DebugStringTableSubsectionRef> OwnedStrings;
75   std::shared_ptr<DebugChecksumsSubsectionRef> OwnedChecksums;
76 
77   const DebugStringTableSubsectionRef *Strings = nullptr;
78   const DebugChecksumsSubsectionRef *Checksums = nullptr;
79 };
80 
81 class StringsAndChecksums {
82 public:
83   using StringsPtr = std::shared_ptr<DebugStringTableSubsection>;
84   using ChecksumsPtr = std::shared_ptr<DebugChecksumsSubsection>;
85 
86   // If no subsections are known about initially, we find as much as we can.
87   StringsAndChecksums() = default;
88 
89   void setStrings(const StringsPtr &SP) { Strings = SP; }
90   void setChecksums(const ChecksumsPtr &CP) { Checksums = CP; }
91 
92   const StringsPtr &strings() const { return Strings; }
93   const ChecksumsPtr &checksums() const { return Checksums; }
94 
95   bool hasStrings() const { return Strings != nullptr; }
96   bool hasChecksums() const { return Checksums != nullptr; }
97 
98 private:
99   StringsPtr Strings;
100   ChecksumsPtr Checksums;
101 };
102 
103 } // end namespace codeview
104 } // end namespace llvm
105 
106 #endif // LLVM_DEBUGINFO_CODEVIEW_STRINGSANDCHECKSUMS_H
107