InputFile.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. //===- InputFile.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_TOOLS_LLVMPDBDUMP_INPUTFILE_H
  9. #define LLVM_TOOLS_LLVMPDBDUMP_INPUTFILE_H
  10. #include "llvm/ADT/Optional.h"
  11. #include "llvm/ADT/PointerUnion.h"
  12. #include "llvm/ADT/StringMap.h"
  13. #include "llvm/ADT/iterator.h"
  14. #include "llvm/DebugInfo/CodeView/DebugChecksumsSubsection.h"
  15. #include "llvm/DebugInfo/CodeView/StringsAndChecksums.h"
  16. #include "llvm/DebugInfo/PDB/Native/ModuleDebugStream.h"
  17. #include "llvm/Object/Binary.h"
  18. #include "llvm/Object/ObjectFile.h"
  19. #include "llvm/Support/Error.h"
  20. namespace llvm {
  21. namespace codeview {
  22. class LazyRandomTypeCollection;
  23. }
  24. namespace object {
  25. class COFFObjectFile;
  26. class SectionRef;
  27. } // namespace object
  28. namespace pdb {
  29. class InputFile;
  30. class LinePrinter;
  31. class PDBFile;
  32. class NativeSession;
  33. class SymbolGroupIterator;
  34. class SymbolGroup;
  35. class InputFile {
  36. InputFile();
  37. std::unique_ptr<NativeSession> PdbSession;
  38. object::OwningBinary<object::Binary> CoffObject;
  39. std::unique_ptr<MemoryBuffer> UnknownFile;
  40. PointerUnion<PDBFile *, object::COFFObjectFile *, MemoryBuffer *> PdbOrObj;
  41. using TypeCollectionPtr = std::unique_ptr<codeview::LazyRandomTypeCollection>;
  42. TypeCollectionPtr Types;
  43. TypeCollectionPtr Ids;
  44. enum TypeCollectionKind { kTypes, kIds };
  45. codeview::LazyRandomTypeCollection &
  46. getOrCreateTypeCollection(TypeCollectionKind Kind);
  47. public:
  48. ~InputFile();
  49. InputFile(InputFile &&Other) = default;
  50. static Expected<InputFile> open(StringRef Path,
  51. bool AllowUnknownFile = false);
  52. PDBFile &pdb();
  53. const PDBFile &pdb() const;
  54. object::COFFObjectFile &obj();
  55. const object::COFFObjectFile &obj() const;
  56. MemoryBuffer &unknown();
  57. const MemoryBuffer &unknown() const;
  58. StringRef getFilePath() const;
  59. bool hasTypes() const;
  60. bool hasIds() const;
  61. codeview::LazyRandomTypeCollection &types();
  62. codeview::LazyRandomTypeCollection &ids();
  63. iterator_range<SymbolGroupIterator> symbol_groups();
  64. SymbolGroupIterator symbol_groups_begin();
  65. SymbolGroupIterator symbol_groups_end();
  66. bool isPdb() const;
  67. bool isObj() const;
  68. bool isUnknown() const;
  69. };
  70. class SymbolGroup {
  71. friend class SymbolGroupIterator;
  72. public:
  73. explicit SymbolGroup(InputFile *File, uint32_t GroupIndex = 0);
  74. Expected<StringRef> getNameFromStringTable(uint32_t Offset) const;
  75. void formatFromFileName(LinePrinter &Printer, StringRef File,
  76. bool Append = false) const;
  77. void formatFromChecksumsOffset(LinePrinter &Printer, uint32_t Offset,
  78. bool Append = false) const;
  79. StringRef name() const;
  80. codeview::DebugSubsectionArray getDebugSubsections() const {
  81. return Subsections;
  82. }
  83. const ModuleDebugStreamRef &getPdbModuleStream() const;
  84. const InputFile &getFile() const { return *File; }
  85. InputFile &getFile() { return *File; }
  86. bool hasDebugStream() const { return DebugStream != nullptr; }
  87. private:
  88. void initializeForPdb(uint32_t Modi);
  89. void updatePdbModi(uint32_t Modi);
  90. void updateDebugS(const codeview::DebugSubsectionArray &SS);
  91. void rebuildChecksumMap();
  92. InputFile *File = nullptr;
  93. StringRef Name;
  94. codeview::DebugSubsectionArray Subsections;
  95. std::shared_ptr<ModuleDebugStreamRef> DebugStream;
  96. codeview::StringsAndChecksumsRef SC;
  97. StringMap<codeview::FileChecksumEntry> ChecksumsByFile;
  98. };
  99. class SymbolGroupIterator
  100. : public iterator_facade_base<SymbolGroupIterator,
  101. std::forward_iterator_tag, SymbolGroup> {
  102. public:
  103. SymbolGroupIterator();
  104. explicit SymbolGroupIterator(InputFile &File);
  105. SymbolGroupIterator(const SymbolGroupIterator &Other) = default;
  106. SymbolGroupIterator &operator=(const SymbolGroupIterator &R) = default;
  107. const SymbolGroup &operator*() const;
  108. SymbolGroup &operator*();
  109. bool operator==(const SymbolGroupIterator &R) const;
  110. SymbolGroupIterator &operator++();
  111. private:
  112. void scanToNextDebugS();
  113. bool isEnd() const;
  114. uint32_t Index = 0;
  115. Optional<object::section_iterator> SectionIter;
  116. SymbolGroup Value;
  117. };
  118. } // namespace pdb
  119. } // namespace llvm
  120. #endif