StringsAndChecksums.h 3.9 KB

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