1 //===- DbiStreamBuilder.h - PDB Dbi Stream Creation -------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef LLVM_DEBUGINFO_PDB_RAW_PDBDBISTREAMBUILDER_H
11 #define LLVM_DEBUGINFO_PDB_RAW_PDBDBISTREAMBUILDER_H
13 #include "llvm/ADT/Optional.h"
14 #include "llvm/ADT/StringSet.h"
15 #include "llvm/BinaryFormat/COFF.h"
16 #include "llvm/Support/Error.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"
35 struct DbiStreamHeader;
36 class DbiModuleDescriptorBuilder;
39 class DbiStreamBuilder {
41 DbiStreamBuilder(msf::MSFBuilder &Msf);
44 DbiStreamBuilder(const DbiStreamBuilder &) = delete;
45 DbiStreamBuilder &operator=(const DbiStreamBuilder &) = delete;
47 void setVersionHeader(PdbRaw_DbiVer V);
48 void setAge(uint32_t A);
49 void setBuildNumber(uint16_t B);
50 void setBuildNumber(uint8_t Major, uint8_t Minor);
51 void setPdbDllVersion(uint16_t V);
52 void setPdbDllRbld(uint16_t R);
53 void setFlags(uint16_t F);
54 void setMachineType(PDB_Machine M);
55 void setMachineType(COFF::MachineTypes M);
56 void setSectionMap(ArrayRef<SecMapEntry> SecMap);
58 // Add given bytes as a new stream.
59 Error addDbgStream(pdb::DbgHeaderType Type, ArrayRef<uint8_t> Data);
61 uint32_t addECName(StringRef Name);
63 uint32_t calculateSerializedLength() const;
65 void setGlobalsStreamIndex(uint32_t Index);
66 void setPublicsStreamIndex(uint32_t Index);
67 void setSymbolRecordStreamIndex(uint32_t Index);
69 Expected<DbiModuleDescriptorBuilder &> addModuleInfo(StringRef ModuleName);
70 Error addModuleSourceFile(DbiModuleDescriptorBuilder &Module, StringRef File);
71 Expected<uint32_t> getSourceFileNameIndex(StringRef FileName);
73 Error finalizeMsfLayout();
75 Error commit(const msf::MSFLayout &Layout, WritableBinaryStreamRef MsfBuffer);
77 void addSectionContrib(const SectionContrib &SC) {
78 SectionContribs.emplace_back(SC);
81 // A helper function to create a Section Map from a COFF section header.
82 static std::vector<SecMapEntry>
83 createSectionMap(ArrayRef<llvm::object::coff_section> SecHdrs);
87 ArrayRef<uint8_t> Data;
88 uint16_t StreamNumber = kInvalidStreamIndex;
92 uint32_t calculateModiSubstreamSize() const;
93 uint32_t calculateNamesOffset() const;
94 uint32_t calculateSectionContribsStreamSize() const;
95 uint32_t calculateSectionMapStreamSize() const;
96 uint32_t calculateFileInfoSubstreamSize() const;
97 uint32_t calculateNamesBufferSize() const;
98 uint32_t calculateDbgStreamsSize() const;
100 Error generateFileInfoSubstream();
102 msf::MSFBuilder &Msf;
103 BumpPtrAllocator &Allocator;
105 Optional<PdbRaw_DbiVer> VerHeader;
107 uint16_t BuildNumber;
108 uint16_t PdbDllVersion;
111 PDB_Machine MachineType;
112 uint32_t GlobalsStreamIndex = kInvalidStreamIndex;
113 uint32_t PublicsStreamIndex = kInvalidStreamIndex;
114 uint32_t SymRecordStreamIndex = kInvalidStreamIndex;
116 const DbiStreamHeader *Header;
118 std::vector<std::unique_ptr<DbiModuleDescriptorBuilder>> ModiList;
120 StringMap<uint32_t> SourceFileNames;
122 PDBStringTableBuilder ECNamesBuilder;
123 WritableBinaryStreamRef NamesBuffer;
124 MutableBinaryByteStream FileInfoBuffer;
125 std::vector<SectionContrib> SectionContribs;
126 ArrayRef<SecMapEntry> SectionMap;
127 std::array<Optional<DebugStream>, (int)DbgHeaderType::Max> DbgStreams;