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