InputFile.h 4.3 KB

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