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

source code of llvm/include/llvm/DebugInfo/PDB/Native/DbiStreamBuilder.h