1*0b57cec5SDimitry Andric //===- DbiModuleDescriptorBuilder.cpp - PDB Mod Info Creation ---*- C++ -*-===//
2*0b57cec5SDimitry Andric //
3*0b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*0b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*0b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*0b57cec5SDimitry Andric //
7*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
8*0b57cec5SDimitry Andric
9*0b57cec5SDimitry Andric #include "llvm/DebugInfo/PDB/Native/DbiModuleDescriptorBuilder.h"
10*0b57cec5SDimitry Andric
11*0b57cec5SDimitry Andric #include "llvm/ADT/ArrayRef.h"
12*0b57cec5SDimitry Andric #include "llvm/BinaryFormat/COFF.h"
13*0b57cec5SDimitry Andric #include "llvm/DebugInfo/CodeView/CodeView.h"
14*0b57cec5SDimitry Andric #include "llvm/DebugInfo/CodeView/DebugSubsectionRecord.h"
15*0b57cec5SDimitry Andric #include "llvm/DebugInfo/MSF/MSFBuilder.h"
16*0b57cec5SDimitry Andric #include "llvm/DebugInfo/MSF/MappedBlockStream.h"
17*0b57cec5SDimitry Andric #include "llvm/DebugInfo/PDB/Native/RawConstants.h"
18*0b57cec5SDimitry Andric #include "llvm/DebugInfo/PDB/Native/RawError.h"
19*0b57cec5SDimitry Andric #include "llvm/Support/BinaryStreamWriter.h"
20*0b57cec5SDimitry Andric
21*0b57cec5SDimitry Andric using namespace llvm;
22*0b57cec5SDimitry Andric using namespace llvm::codeview;
23*0b57cec5SDimitry Andric using namespace llvm::msf;
24*0b57cec5SDimitry Andric using namespace llvm::pdb;
25*0b57cec5SDimitry Andric
26*0b57cec5SDimitry Andric namespace llvm {
27*0b57cec5SDimitry Andric namespace codeview {
28*0b57cec5SDimitry Andric class DebugSubsection;
29*0b57cec5SDimitry Andric }
30*0b57cec5SDimitry Andric } // namespace llvm
31*0b57cec5SDimitry Andric
calculateDiSymbolStreamSize(uint32_t SymbolByteSize,uint32_t C13Size)32*0b57cec5SDimitry Andric static uint32_t calculateDiSymbolStreamSize(uint32_t SymbolByteSize,
33*0b57cec5SDimitry Andric uint32_t C13Size) {
34*0b57cec5SDimitry Andric uint32_t Size = sizeof(uint32_t); // Signature
35*0b57cec5SDimitry Andric Size += alignTo(SymbolByteSize, 4); // Symbol Data
36*0b57cec5SDimitry Andric Size += 0; // TODO: Layout.C11Bytes
37*0b57cec5SDimitry Andric Size += C13Size; // C13 Debug Info Size
38*0b57cec5SDimitry Andric Size += sizeof(uint32_t); // GlobalRefs substream size (always 0)
39*0b57cec5SDimitry Andric Size += 0; // GlobalRefs substream bytes
40*0b57cec5SDimitry Andric return Size;
41*0b57cec5SDimitry Andric }
42*0b57cec5SDimitry Andric
DbiModuleDescriptorBuilder(StringRef ModuleName,uint32_t ModIndex,msf::MSFBuilder & Msf)43*0b57cec5SDimitry Andric DbiModuleDescriptorBuilder::DbiModuleDescriptorBuilder(StringRef ModuleName,
44*0b57cec5SDimitry Andric uint32_t ModIndex,
45*0b57cec5SDimitry Andric msf::MSFBuilder &Msf)
46*0b57cec5SDimitry Andric : MSF(Msf), ModuleName(std::string(ModuleName)) {
47*0b57cec5SDimitry Andric ::memset(&Layout, 0, sizeof(Layout));
48*0b57cec5SDimitry Andric Layout.Mod = ModIndex;
49*0b57cec5SDimitry Andric }
50*0b57cec5SDimitry Andric
51*0b57cec5SDimitry Andric DbiModuleDescriptorBuilder::~DbiModuleDescriptorBuilder() = default;
52*0b57cec5SDimitry Andric
getStreamIndex() const53*0b57cec5SDimitry Andric uint16_t DbiModuleDescriptorBuilder::getStreamIndex() const {
54*0b57cec5SDimitry Andric return Layout.ModDiStream;
55*0b57cec5SDimitry Andric }
56*0b57cec5SDimitry Andric
setObjFileName(StringRef Name)57*0b57cec5SDimitry Andric void DbiModuleDescriptorBuilder::setObjFileName(StringRef Name) {
58*0b57cec5SDimitry Andric ObjFileName = std::string(Name);
59*0b57cec5SDimitry Andric }
60*0b57cec5SDimitry Andric
setPdbFilePathNI(uint32_t NI)61*0b57cec5SDimitry Andric void DbiModuleDescriptorBuilder::setPdbFilePathNI(uint32_t NI) {
62*0b57cec5SDimitry Andric PdbFilePathNI = NI;
63*0b57cec5SDimitry Andric }
64*0b57cec5SDimitry Andric
setFirstSectionContrib(const SectionContrib & SC)65*0b57cec5SDimitry Andric void DbiModuleDescriptorBuilder::setFirstSectionContrib(
66*0b57cec5SDimitry Andric const SectionContrib &SC) {
67*0b57cec5SDimitry Andric Layout.SC = SC;
68*0b57cec5SDimitry Andric }
69*0b57cec5SDimitry Andric
addSymbol(CVSymbol Symbol)70*0b57cec5SDimitry Andric void DbiModuleDescriptorBuilder::addSymbol(CVSymbol Symbol) {
71*0b57cec5SDimitry Andric // Defer to the bulk API. It does the same thing.
72*0b57cec5SDimitry Andric addSymbolsInBulk(Symbol.data());
73*0b57cec5SDimitry Andric }
74*0b57cec5SDimitry Andric
addSymbolsInBulk(ArrayRef<uint8_t> BulkSymbols)75*0b57cec5SDimitry Andric void DbiModuleDescriptorBuilder::addSymbolsInBulk(
76*0b57cec5SDimitry Andric ArrayRef<uint8_t> BulkSymbols) {
77*0b57cec5SDimitry Andric // Do nothing for empty runs of symbols.
78*0b57cec5SDimitry Andric if (BulkSymbols.empty())
79*0b57cec5SDimitry Andric return;
80*0b57cec5SDimitry Andric
81*0b57cec5SDimitry Andric Symbols.push_back(SymbolListWrapper(BulkSymbols));
82*0b57cec5SDimitry Andric // Symbols written to a PDB file are required to be 4 byte aligned. The same
83*0b57cec5SDimitry Andric // is not true of object files.
84*0b57cec5SDimitry Andric assert(BulkSymbols.size() % alignOf(CodeViewContainer::Pdb) == 0 &&
85*0b57cec5SDimitry Andric "Invalid Symbol alignment!");
86*0b57cec5SDimitry Andric SymbolByteSize += BulkSymbols.size();
87*0b57cec5SDimitry Andric }
88*0b57cec5SDimitry Andric
addUnmergedSymbols(void * SymSrc,uint32_t SymLength)89*0b57cec5SDimitry Andric void DbiModuleDescriptorBuilder::addUnmergedSymbols(void *SymSrc,
90*0b57cec5SDimitry Andric uint32_t SymLength) {
91*0b57cec5SDimitry Andric assert(SymLength > 0);
92*0b57cec5SDimitry Andric Symbols.push_back(SymbolListWrapper(SymSrc, SymLength));
93*0b57cec5SDimitry Andric
94*0b57cec5SDimitry Andric // Symbols written to a PDB file are required to be 4 byte aligned. The same
95*0b57cec5SDimitry Andric // is not true of object files.
96*0b57cec5SDimitry Andric assert(SymLength % alignOf(CodeViewContainer::Pdb) == 0 &&
97*0b57cec5SDimitry Andric "Invalid Symbol alignment!");
98*0b57cec5SDimitry Andric SymbolByteSize += SymLength;
99*0b57cec5SDimitry Andric }
100*0b57cec5SDimitry Andric
addSourceFile(StringRef Path)101*0b57cec5SDimitry Andric void DbiModuleDescriptorBuilder::addSourceFile(StringRef Path) {
102*0b57cec5SDimitry Andric SourceFiles.push_back(std::string(Path));
103*0b57cec5SDimitry Andric }
104*0b57cec5SDimitry Andric
calculateC13DebugInfoSize() const105*0b57cec5SDimitry Andric uint32_t DbiModuleDescriptorBuilder::calculateC13DebugInfoSize() const {
106*0b57cec5SDimitry Andric uint32_t Result = 0;
107*0b57cec5SDimitry Andric for (const auto &Builder : C13Builders) {
108*0b57cec5SDimitry Andric Result += Builder.calculateSerializedLength();
109*0b57cec5SDimitry Andric }
110*0b57cec5SDimitry Andric return Result;
111*0b57cec5SDimitry Andric }
112*0b57cec5SDimitry Andric
calculateSerializedLength() const113*0b57cec5SDimitry Andric uint32_t DbiModuleDescriptorBuilder::calculateSerializedLength() const {
114*0b57cec5SDimitry Andric uint32_t L = sizeof(Layout);
115*0b57cec5SDimitry Andric uint32_t M = ModuleName.size() + 1;
116*0b57cec5SDimitry Andric uint32_t O = ObjFileName.size() + 1;
117*0b57cec5SDimitry Andric return alignTo(L + M + O, sizeof(uint32_t));
118*0b57cec5SDimitry Andric }
119*0b57cec5SDimitry Andric
finalize()120*0b57cec5SDimitry Andric void DbiModuleDescriptorBuilder::finalize() {
121*0b57cec5SDimitry Andric Layout.FileNameOffs = 0; // TODO: Fix this
122*0b57cec5SDimitry Andric Layout.Flags = 0; // TODO: Fix this
123*0b57cec5SDimitry Andric Layout.C11Bytes = 0;
124*0b57cec5SDimitry Andric Layout.C13Bytes = calculateC13DebugInfoSize();
125*0b57cec5SDimitry Andric (void)Layout.Mod; // Set in constructor
126*0b57cec5SDimitry Andric (void)Layout.ModDiStream; // Set in finalizeMsfLayout
127*0b57cec5SDimitry Andric Layout.NumFiles = SourceFiles.size();
128*0b57cec5SDimitry Andric Layout.PdbFilePathNI = PdbFilePathNI;
129*0b57cec5SDimitry Andric Layout.SrcFileNameNI = 0;
130*0b57cec5SDimitry Andric
131*0b57cec5SDimitry Andric // This value includes both the signature field as well as the record bytes
132*0b57cec5SDimitry Andric // from the symbol stream.
133*0b57cec5SDimitry Andric Layout.SymBytes =
134*0b57cec5SDimitry Andric Layout.ModDiStream == kInvalidStreamIndex ? 0 : getNextSymbolOffset();
135*0b57cec5SDimitry Andric }
136*0b57cec5SDimitry Andric
finalizeMsfLayout()137*0b57cec5SDimitry Andric Error DbiModuleDescriptorBuilder::finalizeMsfLayout() {
138*0b57cec5SDimitry Andric this->Layout.ModDiStream = kInvalidStreamIndex;
139*0b57cec5SDimitry Andric uint32_t C13Size = calculateC13DebugInfoSize();
140*0b57cec5SDimitry Andric if (!C13Size && !SymbolByteSize)
141*0b57cec5SDimitry Andric return Error::success();
142*0b57cec5SDimitry Andric auto ExpectedSN =
143*0b57cec5SDimitry Andric MSF.addStream(calculateDiSymbolStreamSize(SymbolByteSize, C13Size));
144*0b57cec5SDimitry Andric if (!ExpectedSN)
145*0b57cec5SDimitry Andric return ExpectedSN.takeError();
146*0b57cec5SDimitry Andric Layout.ModDiStream = *ExpectedSN;
147*0b57cec5SDimitry Andric return Error::success();
148*0b57cec5SDimitry Andric }
149*0b57cec5SDimitry Andric
commit(BinaryStreamWriter & ModiWriter)150*0b57cec5SDimitry Andric Error DbiModuleDescriptorBuilder::commit(BinaryStreamWriter &ModiWriter) {
151*0b57cec5SDimitry Andric // We write the Modi record to the `ModiWriter`, but we additionally write its
152*0b57cec5SDimitry Andric // symbol stream to a brand new stream.
153*0b57cec5SDimitry Andric if (auto EC = ModiWriter.writeObject(Layout))
154*0b57cec5SDimitry Andric return EC;
155*0b57cec5SDimitry Andric if (auto EC = ModiWriter.writeCString(ModuleName))
156*0b57cec5SDimitry Andric return EC;
157*0b57cec5SDimitry Andric if (auto EC = ModiWriter.writeCString(ObjFileName))
158*0b57cec5SDimitry Andric return EC;
159*0b57cec5SDimitry Andric if (auto EC = ModiWriter.padToAlignment(sizeof(uint32_t)))
160*0b57cec5SDimitry Andric return EC;
161*0b57cec5SDimitry Andric return Error::success();
162*0b57cec5SDimitry Andric }
163*0b57cec5SDimitry Andric
commitSymbolStream(const msf::MSFLayout & MsfLayout,WritableBinaryStreamRef MsfBuffer)164*0b57cec5SDimitry Andric Error DbiModuleDescriptorBuilder::commitSymbolStream(
165*0b57cec5SDimitry Andric const msf::MSFLayout &MsfLayout, WritableBinaryStreamRef MsfBuffer) {
166*0b57cec5SDimitry Andric if (Layout.ModDiStream == kInvalidStreamIndex)
167*0b57cec5SDimitry Andric return Error::success();
168*0b57cec5SDimitry Andric
169*0b57cec5SDimitry Andric auto NS = WritableMappedBlockStream::createIndexedStream(
170*0b57cec5SDimitry Andric MsfLayout, MsfBuffer, Layout.ModDiStream, MSF.getAllocator());
171*0b57cec5SDimitry Andric WritableBinaryStreamRef Ref(*NS);
172*0b57cec5SDimitry Andric BinaryStreamWriter SymbolWriter(Ref);
173*0b57cec5SDimitry Andric // Write the symbols.
174*0b57cec5SDimitry Andric if (auto EC = SymbolWriter.writeInteger<uint32_t>(COFF::DEBUG_SECTION_MAGIC))
175*0b57cec5SDimitry Andric return EC;
176*0b57cec5SDimitry Andric for (const SymbolListWrapper &Sym : Symbols) {
177*0b57cec5SDimitry Andric if (Sym.NeedsToBeMerged) {
178*0b57cec5SDimitry Andric assert(MergeSymsCallback);
179*0b57cec5SDimitry Andric if (auto EC = MergeSymsCallback(MergeSymsCtx, Sym.SymPtr, SymbolWriter))
180*0b57cec5SDimitry Andric return EC;
181*0b57cec5SDimitry Andric } else {
182*0b57cec5SDimitry Andric if (auto EC = SymbolWriter.writeBytes(Sym.asArray()))
183*0b57cec5SDimitry Andric return EC;
184*0b57cec5SDimitry Andric }
185*0b57cec5SDimitry Andric }
186*0b57cec5SDimitry Andric
187*0b57cec5SDimitry Andric // Apply the string table fixups.
188*0b57cec5SDimitry Andric auto SavedOffset = SymbolWriter.getOffset();
189*0b57cec5SDimitry Andric for (const StringTableFixup &Fixup : StringTableFixups) {
190*0b57cec5SDimitry Andric SymbolWriter.setOffset(Fixup.SymOffsetOfReference);
191*0b57cec5SDimitry Andric if (auto E = SymbolWriter.writeInteger<uint32_t>(Fixup.StrTabOffset))
192 return E;
193 }
194 SymbolWriter.setOffset(SavedOffset);
195
196 assert(SymbolWriter.getOffset() % alignOf(CodeViewContainer::Pdb) == 0 &&
197 "Invalid debug section alignment!");
198 // TODO: Write C11 Line data
199 for (const auto &Builder : C13Builders) {
200 if (auto EC = Builder.commit(SymbolWriter, CodeViewContainer::Pdb))
201 return EC;
202 }
203
204 // TODO: Figure out what GlobalRefs substream actually is and populate it.
205 if (auto EC = SymbolWriter.writeInteger<uint32_t>(0))
206 return EC;
207 if (SymbolWriter.bytesRemaining() > 0)
208 return make_error<RawError>(raw_error_code::stream_too_long);
209
210 return Error::success();
211 }
212
addDebugSubsection(std::shared_ptr<DebugSubsection> Subsection)213 void DbiModuleDescriptorBuilder::addDebugSubsection(
214 std::shared_ptr<DebugSubsection> Subsection) {
215 assert(Subsection);
216 C13Builders.push_back(DebugSubsectionRecordBuilder(std::move(Subsection)));
217 }
218
addDebugSubsection(const DebugSubsectionRecord & SubsectionContents)219 void DbiModuleDescriptorBuilder::addDebugSubsection(
220 const DebugSubsectionRecord &SubsectionContents) {
221 C13Builders.push_back(DebugSubsectionRecordBuilder(SubsectionContents));
222 }
223