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