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