xref: /freebsd/contrib/llvm-project/llvm/include/llvm/DebugInfo/PDB/Native/PDBFileBuilder.h (revision 700637cbb5e582861067a11aaca4d053546871d2)
1 //===- PDBFileBuilder.h - PDB File Creation ---------------------*- 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_PDB_NATIVE_PDBFILEBUILDER_H
10 #define LLVM_DEBUGINFO_PDB_NATIVE_PDBFILEBUILDER_H
11 
12 #include "llvm/ADT/DenseMap.h"
13 #include "llvm/ADT/SmallVector.h"
14 #include "llvm/DebugInfo/PDB/Native/HashTable.h"
15 #include "llvm/DebugInfo/PDB/Native/NamedStreamMap.h"
16 #include "llvm/DebugInfo/PDB/Native/PDBStringTableBuilder.h"
17 #include "llvm/Support/Allocator.h"
18 #include "llvm/Support/Compiler.h"
19 #include "llvm/Support/Error.h"
20 #include "llvm/Support/MemoryBuffer.h"
21 #include <memory>
22 
23 namespace llvm {
24 class WritableBinaryStream;
25 namespace codeview {
26 struct GUID;
27 }
28 
29 namespace msf {
30 class MSFBuilder;
31 struct MSFLayout;
32 }
33 namespace pdb {
34 struct SrcHeaderBlockEntry;
35 class DbiStreamBuilder;
36 class InfoStreamBuilder;
37 class GSIStreamBuilder;
38 class TpiStreamBuilder;
39 
40 class PDBFileBuilder {
41 public:
42   LLVM_ABI explicit PDBFileBuilder(BumpPtrAllocator &Allocator);
43   LLVM_ABI ~PDBFileBuilder();
44   PDBFileBuilder(const PDBFileBuilder &) = delete;
45   PDBFileBuilder &operator=(const PDBFileBuilder &) = delete;
46 
47   LLVM_ABI Error initialize(uint32_t BlockSize);
48 
49   LLVM_ABI msf::MSFBuilder &getMsfBuilder();
50   LLVM_ABI InfoStreamBuilder &getInfoBuilder();
51   LLVM_ABI DbiStreamBuilder &getDbiBuilder();
52   LLVM_ABI TpiStreamBuilder &getTpiBuilder();
53   LLVM_ABI TpiStreamBuilder &getIpiBuilder();
54   LLVM_ABI PDBStringTableBuilder &getStringTableBuilder();
55   LLVM_ABI GSIStreamBuilder &getGsiBuilder();
56 
57   // If HashPDBContentsToGUID is true on the InfoStreamBuilder, Guid is filled
58   // with the computed PDB GUID on return.
59   LLVM_ABI Error commit(StringRef Filename, codeview::GUID *Guid);
60 
61   LLVM_ABI Expected<uint32_t> getNamedStreamIndex(StringRef Name) const;
62   LLVM_ABI Error addNamedStream(StringRef Name, StringRef Data);
63   LLVM_ABI void addInjectedSource(StringRef Name,
64                                   std::unique_ptr<MemoryBuffer> Buffer);
65 
66 private:
67   struct InjectedSourceDescriptor {
68     // The full name of the stream that contains the contents of this injected
69     // source.  This is built as a concatenation of the literal "/src/files"
70     // plus the "vname".
71     std::string StreamName;
72 
73     // The exact name of the file name as specified by the user.
74     uint32_t NameIndex;
75 
76     // The string table index of the "vname" of the file.  As far as we
77     // understand, this is the same as the name, except it is lowercased and
78     // forward slashes are converted to backslashes.
79     uint32_t VNameIndex;
80     std::unique_ptr<MemoryBuffer> Content;
81   };
82 
83   Error finalizeMsfLayout();
84   Expected<uint32_t> allocateNamedStream(StringRef Name, uint32_t Size);
85 
86   void commitInjectedSources(WritableBinaryStream &MsfBuffer,
87                              const msf::MSFLayout &Layout);
88   void commitSrcHeaderBlock(WritableBinaryStream &MsfBuffer,
89                             const msf::MSFLayout &Layout);
90 
91   BumpPtrAllocator &Allocator;
92 
93   std::unique_ptr<msf::MSFBuilder> Msf;
94   std::unique_ptr<InfoStreamBuilder> Info;
95   std::unique_ptr<DbiStreamBuilder> Dbi;
96   std::unique_ptr<GSIStreamBuilder> Gsi;
97   std::unique_ptr<TpiStreamBuilder> Tpi;
98   std::unique_ptr<TpiStreamBuilder> Ipi;
99 
100   PDBStringTableBuilder Strings;
101   StringTableHashTraits InjectedSourceHashTraits;
102   HashTable<SrcHeaderBlockEntry> InjectedSourceTable;
103 
104   SmallVector<InjectedSourceDescriptor, 2> InjectedSources;
105 
106   NamedStreamMap NamedStreams;
107   DenseMap<uint32_t, std::string> NamedStreamData;
108 };
109 }
110 }
111 
112 #endif
113