1 //===- DbiStreamBuilder.h - PDB Dbi Stream Creation -------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 #ifndef LLVM_DEBUGINFO_PDB_RAW_PDBDBISTREAMBUILDER_H
10 #define LLVM_DEBUGINFO_PDB_RAW_PDBDBISTREAMBUILDER_H
12 #include "llvm/ADT/Optional.h"
13 #include "llvm/ADT/StringSet.h"
14 #include "llvm/BinaryFormat/COFF.h"
15 #include "llvm/Support/Error.h"
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"
39 struct DbiStreamHeader
;
40 class DbiModuleDescriptorBuilder
;
43 class DbiStreamBuilder
{
45 DbiStreamBuilder(msf::MSFBuilder
&Msf
);
48 DbiStreamBuilder(const DbiStreamBuilder
&) = delete;
49 DbiStreamBuilder
&operator=(const DbiStreamBuilder
&) = delete;
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
);
62 // Add given bytes as a new stream.
63 Error
addDbgStream(pdb::DbgHeaderType Type
, ArrayRef
<uint8_t> Data
);
65 uint32_t addECName(StringRef Name
);
67 uint32_t calculateSerializedLength() const;
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
);
75 Expected
<DbiModuleDescriptorBuilder
&> addModuleInfo(StringRef ModuleName
);
76 Error
addModuleSourceFile(DbiModuleDescriptorBuilder
&Module
, StringRef File
);
77 Expected
<uint32_t> getSourceFileNameIndex(StringRef FileName
);
79 Error
finalizeMsfLayout();
81 Error
commit(const msf::MSFLayout
&Layout
, WritableBinaryStreamRef MsfBuffer
);
83 void addSectionContrib(const SectionContrib
&SC
) {
84 SectionContribs
.emplace_back(SC
);
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
);
93 std::function
<Error(BinaryStreamWriter
&)> WriteFn
;
95 uint16_t StreamNumber
= kInvalidStreamIndex
;
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;
107 Error
generateFileInfoSubstream();
109 msf::MSFBuilder
&Msf
;
110 BumpPtrAllocator
&Allocator
;
112 Optional
<PdbRaw_DbiVer
> VerHeader
;
114 uint16_t BuildNumber
;
115 uint16_t PdbDllVersion
;
118 PDB_Machine MachineType
;
119 uint32_t GlobalsStreamIndex
= kInvalidStreamIndex
;
120 uint32_t PublicsStreamIndex
= kInvalidStreamIndex
;
121 uint32_t SymRecordStreamIndex
= kInvalidStreamIndex
;
123 const DbiStreamHeader
*Header
;
125 std::vector
<std::unique_ptr
<DbiModuleDescriptorBuilder
>> ModiList
;
127 Optional
<codeview::DebugFrameDataSubsection
> NewFpoData
;
128 std::vector
<object::FpoData
> OldFpoData
;
130 StringMap
<uint32_t> SourceFileNames
;
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
;