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