[sanitizer] Improve FreeBSD ASLR detection
[llvm-project.git] / llvm / tools / llvm-pdbutil / InputFile.h
blob633ab34a54d440126391959ad735ebb7aa710aa1
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 //===----------------------------------------------------------------------===//
9 #ifndef LLVM_TOOLS_LLVMPDBDUMP_INPUTFILE_H
10 #define LLVM_TOOLS_LLVMPDBDUMP_INPUTFILE_H
12 #include "llvm/ADT/Optional.h"
13 #include "llvm/ADT/PointerUnion.h"
14 #include "llvm/ADT/StringMap.h"
15 #include "llvm/ADT/iterator.h"
16 #include "llvm/DebugInfo/CodeView/DebugChecksumsSubsection.h"
17 #include "llvm/DebugInfo/CodeView/StringsAndChecksums.h"
18 #include "llvm/DebugInfo/PDB/Native/ModuleDebugStream.h"
19 #include "llvm/Object/Binary.h"
20 #include "llvm/Object/ObjectFile.h"
21 #include "llvm/Support/Error.h"
23 namespace llvm {
24 namespace codeview {
25 class LazyRandomTypeCollection;
27 namespace object {
28 class COFFObjectFile;
29 } // namespace object
31 namespace pdb {
32 class InputFile;
33 class LinePrinter;
34 class PDBFile;
35 class NativeSession;
36 class SymbolGroupIterator;
37 class SymbolGroup;
39 class InputFile {
40 InputFile();
42 std::unique_ptr<NativeSession> PdbSession;
43 object::OwningBinary<object::Binary> CoffObject;
44 std::unique_ptr<MemoryBuffer> UnknownFile;
45 PointerUnion<PDBFile *, object::COFFObjectFile *, MemoryBuffer *> PdbOrObj;
47 using TypeCollectionPtr = std::unique_ptr<codeview::LazyRandomTypeCollection>;
49 TypeCollectionPtr Types;
50 TypeCollectionPtr Ids;
52 enum TypeCollectionKind { kTypes, kIds };
53 codeview::LazyRandomTypeCollection &
54 getOrCreateTypeCollection(TypeCollectionKind Kind);
56 public:
57 ~InputFile();
58 InputFile(InputFile &&Other) = default;
60 static Expected<InputFile> open(StringRef Path,
61 bool AllowUnknownFile = false);
63 PDBFile &pdb();
64 const PDBFile &pdb() const;
65 object::COFFObjectFile &obj();
66 const object::COFFObjectFile &obj() const;
67 MemoryBuffer &unknown();
68 const MemoryBuffer &unknown() const;
70 StringRef getFilePath() const;
72 bool hasTypes() const;
73 bool hasIds() const;
75 codeview::LazyRandomTypeCollection &types();
76 codeview::LazyRandomTypeCollection &ids();
78 iterator_range<SymbolGroupIterator> symbol_groups();
79 SymbolGroupIterator symbol_groups_begin();
80 SymbolGroupIterator symbol_groups_end();
82 bool isPdb() const;
83 bool isObj() const;
84 bool isUnknown() const;
87 class SymbolGroup {
88 friend class SymbolGroupIterator;
90 public:
91 explicit SymbolGroup(InputFile *File, uint32_t GroupIndex = 0);
93 Expected<StringRef> getNameFromStringTable(uint32_t Offset) const;
95 void formatFromFileName(LinePrinter &Printer, StringRef File,
96 bool Append = false) const;
98 void formatFromChecksumsOffset(LinePrinter &Printer, uint32_t Offset,
99 bool Append = false) const;
101 StringRef name() const;
103 codeview::DebugSubsectionArray getDebugSubsections() const {
104 return Subsections;
106 const ModuleDebugStreamRef &getPdbModuleStream() const;
108 const InputFile &getFile() const { return *File; }
109 InputFile &getFile() { return *File; }
111 bool hasDebugStream() const { return DebugStream != nullptr; }
113 private:
114 void initializeForPdb(uint32_t Modi);
115 void updatePdbModi(uint32_t Modi);
116 void updateDebugS(const codeview::DebugSubsectionArray &SS);
118 void rebuildChecksumMap();
119 InputFile *File = nullptr;
120 StringRef Name;
121 codeview::DebugSubsectionArray Subsections;
122 std::shared_ptr<ModuleDebugStreamRef> DebugStream;
123 codeview::StringsAndChecksumsRef SC;
124 StringMap<codeview::FileChecksumEntry> ChecksumsByFile;
127 class SymbolGroupIterator
128 : public iterator_facade_base<SymbolGroupIterator,
129 std::forward_iterator_tag, SymbolGroup> {
130 public:
131 SymbolGroupIterator();
132 explicit SymbolGroupIterator(InputFile &File);
133 SymbolGroupIterator(const SymbolGroupIterator &Other) = default;
134 SymbolGroupIterator &operator=(const SymbolGroupIterator &R) = default;
136 const SymbolGroup &operator*() const;
137 SymbolGroup &operator*();
139 bool operator==(const SymbolGroupIterator &R) const;
140 SymbolGroupIterator &operator++();
142 private:
143 void scanToNextDebugS();
144 bool isEnd() const;
146 uint32_t Index = 0;
147 Optional<object::section_iterator> SectionIter;
148 SymbolGroup Value;
151 } // namespace pdb
152 } // namespace llvm
154 #endif