xref: /freebsd/contrib/llvm-project/llvm/include/llvm/DebugInfo/PDB/Native/DbiStreamBuilder.h (revision 32100375a661c1e16588ddfa7b90ca8d26cb9786)
1 //===- DbiStreamBuilder.h - PDB Dbi Stream 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_RAW_PDBDBISTREAMBUILDER_H
10 #define LLVM_DEBUGINFO_PDB_RAW_PDBDBISTREAMBUILDER_H
11 
12 #include "llvm/ADT/Optional.h"
13 #include "llvm/ADT/StringSet.h"
14 #include "llvm/BinaryFormat/COFF.h"
15 #include "llvm/Support/Error.h"
16 
17 #include "llvm/DebugInfo/CodeView/DebugFrameDataSubsection.h"
18 #include "llvm/DebugInfo/PDB/Native/PDBFile.h"
19 #include "llvm/DebugInfo/PDB/Native/PDBStringTableBuilder.h"
20 #include "llvm/DebugInfo/PDB/Native/RawConstants.h"
21 #include "llvm/DebugInfo/PDB/PDBTypes.h"
22 #include "llvm/Support/BinaryByteStream.h"
23 #include "llvm/Support/BinaryStreamReader.h"
24 #include "llvm/Support/Endian.h"
25 
26 namespace llvm {
27 namespace codeview {
28 struct FrameData;
29 }
30 namespace msf {
31 class MSFBuilder;
32 }
33 namespace object {
34 struct coff_section;
35 struct FpoData;
36 }
37 namespace pdb {
38 class DbiStream;
39 struct DbiStreamHeader;
40 class DbiModuleDescriptorBuilder;
41 class PDBFile;
42 
43 class DbiStreamBuilder {
44 public:
45   DbiStreamBuilder(msf::MSFBuilder &Msf);
46   ~DbiStreamBuilder();
47 
48   DbiStreamBuilder(const DbiStreamBuilder &) = delete;
49   DbiStreamBuilder &operator=(const DbiStreamBuilder &) = delete;
50 
51   void setVersionHeader(PdbRaw_DbiVer V);
52   void setAge(uint32_t A);
53   void setBuildNumber(uint16_t B);
54   void setBuildNumber(uint8_t Major, uint8_t Minor);
55   void setPdbDllVersion(uint16_t V);
56   void setPdbDllRbld(uint16_t R);
57   void setFlags(uint16_t F);
58   void setMachineType(PDB_Machine M);
59   void setMachineType(COFF::MachineTypes M);
60   void setSectionMap(ArrayRef<SecMapEntry> SecMap);
61 
62   // Add given bytes as a new stream.
63   Error addDbgStream(pdb::DbgHeaderType Type, ArrayRef<uint8_t> Data);
64 
65   uint32_t addECName(StringRef Name);
66 
67   uint32_t calculateSerializedLength() const;
68 
69   void setGlobalsStreamIndex(uint32_t Index);
70   void setPublicsStreamIndex(uint32_t Index);
71   void setSymbolRecordStreamIndex(uint32_t Index);
72   void addNewFpoData(const codeview::FrameData &FD);
73   void addOldFpoData(const object::FpoData &Fpo);
74 
75   Expected<DbiModuleDescriptorBuilder &> addModuleInfo(StringRef ModuleName);
76   Error addModuleSourceFile(DbiModuleDescriptorBuilder &Module, StringRef File);
77   Expected<uint32_t> getSourceFileNameIndex(StringRef FileName);
78 
79   Error finalizeMsfLayout();
80 
81   Error commit(const msf::MSFLayout &Layout, WritableBinaryStreamRef MsfBuffer);
82 
83   void addSectionContrib(const SectionContrib &SC) {
84     SectionContribs.emplace_back(SC);
85   }
86 
87   // A helper function to create a Section Map from a COFF section header.
88   static std::vector<SecMapEntry>
89   createSectionMap(ArrayRef<llvm::object::coff_section> SecHdrs);
90 
91 private:
92   struct DebugStream {
93     std::function<Error(BinaryStreamWriter &)> WriteFn;
94     uint32_t Size = 0;
95     uint16_t StreamNumber = kInvalidStreamIndex;
96   };
97 
98   Error finalize();
99   uint32_t calculateModiSubstreamSize() const;
100   uint32_t calculateNamesOffset() const;
101   uint32_t calculateSectionContribsStreamSize() const;
102   uint32_t calculateSectionMapStreamSize() const;
103   uint32_t calculateFileInfoSubstreamSize() const;
104   uint32_t calculateNamesBufferSize() const;
105   uint32_t calculateDbgStreamsSize() const;
106 
107   Error generateFileInfoSubstream();
108 
109   msf::MSFBuilder &Msf;
110   BumpPtrAllocator &Allocator;
111 
112   Optional<PdbRaw_DbiVer> VerHeader;
113   uint32_t Age;
114   uint16_t BuildNumber;
115   uint16_t PdbDllVersion;
116   uint16_t PdbDllRbld;
117   uint16_t Flags;
118   PDB_Machine MachineType;
119   uint32_t GlobalsStreamIndex = kInvalidStreamIndex;
120   uint32_t PublicsStreamIndex = kInvalidStreamIndex;
121   uint32_t SymRecordStreamIndex = kInvalidStreamIndex;
122 
123   const DbiStreamHeader *Header;
124 
125   std::vector<std::unique_ptr<DbiModuleDescriptorBuilder>> ModiList;
126 
127   Optional<codeview::DebugFrameDataSubsection> NewFpoData;
128   std::vector<object::FpoData> OldFpoData;
129 
130   StringMap<uint32_t> SourceFileNames;
131 
132   PDBStringTableBuilder ECNamesBuilder;
133   WritableBinaryStreamRef NamesBuffer;
134   MutableBinaryByteStream FileInfoBuffer;
135   std::vector<SectionContrib> SectionContribs;
136   ArrayRef<SecMapEntry> SectionMap;
137   std::array<Optional<DebugStream>, (int)DbgHeaderType::Max> DbgStreams;
138 };
139 }
140 }
141 
142 #endif
143