10b57cec5SDimitry Andric //===- DbiModuleDescriptorBuilder.cpp - PDB Mod Info Creation ---*- C++ -*-===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric
90b57cec5SDimitry Andric #include "llvm/DebugInfo/PDB/Native/DbiModuleDescriptorBuilder.h"
100b57cec5SDimitry Andric
110b57cec5SDimitry Andric #include "llvm/ADT/ArrayRef.h"
120b57cec5SDimitry Andric #include "llvm/BinaryFormat/COFF.h"
130b57cec5SDimitry Andric #include "llvm/DebugInfo/CodeView/DebugSubsectionRecord.h"
140b57cec5SDimitry Andric #include "llvm/DebugInfo/MSF/MSFBuilder.h"
150b57cec5SDimitry Andric #include "llvm/DebugInfo/MSF/MSFCommon.h"
160b57cec5SDimitry Andric #include "llvm/DebugInfo/MSF/MappedBlockStream.h"
170b57cec5SDimitry Andric #include "llvm/DebugInfo/PDB/Native/DbiModuleDescriptor.h"
180b57cec5SDimitry Andric #include "llvm/DebugInfo/PDB/Native/GSIStreamBuilder.h"
190b57cec5SDimitry Andric #include "llvm/DebugInfo/PDB/Native/RawConstants.h"
200b57cec5SDimitry Andric #include "llvm/DebugInfo/PDB/Native/RawError.h"
210b57cec5SDimitry Andric #include "llvm/Support/BinaryStreamWriter.h"
220b57cec5SDimitry Andric
230b57cec5SDimitry Andric using namespace llvm;
240b57cec5SDimitry Andric using namespace llvm::codeview;
250b57cec5SDimitry Andric using namespace llvm::msf;
260b57cec5SDimitry Andric using namespace llvm::pdb;
270b57cec5SDimitry Andric
calculateDiSymbolStreamSize(uint32_t SymbolByteSize,uint32_t C13Size)280b57cec5SDimitry Andric static uint32_t calculateDiSymbolStreamSize(uint32_t SymbolByteSize,
290b57cec5SDimitry Andric uint32_t C13Size) {
300b57cec5SDimitry Andric uint32_t Size = sizeof(uint32_t); // Signature
310b57cec5SDimitry Andric Size += alignTo(SymbolByteSize, 4); // Symbol Data
320b57cec5SDimitry Andric Size += 0; // TODO: Layout.C11Bytes
330b57cec5SDimitry Andric Size += C13Size; // C13 Debug Info Size
340b57cec5SDimitry Andric Size += sizeof(uint32_t); // GlobalRefs substream size (always 0)
350b57cec5SDimitry Andric Size += 0; // GlobalRefs substream bytes
360b57cec5SDimitry Andric return Size;
370b57cec5SDimitry Andric }
380b57cec5SDimitry Andric
DbiModuleDescriptorBuilder(StringRef ModuleName,uint32_t ModIndex,msf::MSFBuilder & Msf)390b57cec5SDimitry Andric DbiModuleDescriptorBuilder::DbiModuleDescriptorBuilder(StringRef ModuleName,
400b57cec5SDimitry Andric uint32_t ModIndex,
410b57cec5SDimitry Andric msf::MSFBuilder &Msf)
425ffd83dbSDimitry Andric : MSF(Msf), ModuleName(std::string(ModuleName)) {
430b57cec5SDimitry Andric ::memset(&Layout, 0, sizeof(Layout));
440b57cec5SDimitry Andric Layout.Mod = ModIndex;
450b57cec5SDimitry Andric }
460b57cec5SDimitry Andric
~DbiModuleDescriptorBuilder()470b57cec5SDimitry Andric DbiModuleDescriptorBuilder::~DbiModuleDescriptorBuilder() {}
480b57cec5SDimitry Andric
getStreamIndex() const490b57cec5SDimitry Andric uint16_t DbiModuleDescriptorBuilder::getStreamIndex() const {
500b57cec5SDimitry Andric return Layout.ModDiStream;
510b57cec5SDimitry Andric }
520b57cec5SDimitry Andric
setObjFileName(StringRef Name)530b57cec5SDimitry Andric void DbiModuleDescriptorBuilder::setObjFileName(StringRef Name) {
545ffd83dbSDimitry Andric ObjFileName = std::string(Name);
550b57cec5SDimitry Andric }
560b57cec5SDimitry Andric
setPdbFilePathNI(uint32_t NI)570b57cec5SDimitry Andric void DbiModuleDescriptorBuilder::setPdbFilePathNI(uint32_t NI) {
580b57cec5SDimitry Andric PdbFilePathNI = NI;
590b57cec5SDimitry Andric }
600b57cec5SDimitry Andric
setFirstSectionContrib(const SectionContrib & SC)610b57cec5SDimitry Andric void DbiModuleDescriptorBuilder::setFirstSectionContrib(
620b57cec5SDimitry Andric const SectionContrib &SC) {
630b57cec5SDimitry Andric Layout.SC = SC;
640b57cec5SDimitry Andric }
650b57cec5SDimitry Andric
addSymbol(CVSymbol Symbol)660b57cec5SDimitry Andric void DbiModuleDescriptorBuilder::addSymbol(CVSymbol Symbol) {
670b57cec5SDimitry Andric // Defer to the bulk API. It does the same thing.
680b57cec5SDimitry Andric addSymbolsInBulk(Symbol.data());
690b57cec5SDimitry Andric }
700b57cec5SDimitry Andric
addSymbolsInBulk(ArrayRef<uint8_t> BulkSymbols)710b57cec5SDimitry Andric void DbiModuleDescriptorBuilder::addSymbolsInBulk(
720b57cec5SDimitry Andric ArrayRef<uint8_t> BulkSymbols) {
730b57cec5SDimitry Andric // Do nothing for empty runs of symbols.
740b57cec5SDimitry Andric if (BulkSymbols.empty())
750b57cec5SDimitry Andric return;
760b57cec5SDimitry Andric
77*af732203SDimitry Andric Symbols.push_back(SymbolListWrapper(BulkSymbols));
780b57cec5SDimitry Andric // Symbols written to a PDB file are required to be 4 byte aligned. The same
790b57cec5SDimitry Andric // is not true of object files.
800b57cec5SDimitry Andric assert(BulkSymbols.size() % alignOf(CodeViewContainer::Pdb) == 0 &&
810b57cec5SDimitry Andric "Invalid Symbol alignment!");
820b57cec5SDimitry Andric SymbolByteSize += BulkSymbols.size();
830b57cec5SDimitry Andric }
840b57cec5SDimitry Andric
addUnmergedSymbols(void * SymSrc,uint32_t SymLength)85*af732203SDimitry Andric void DbiModuleDescriptorBuilder::addUnmergedSymbols(void *SymSrc,
86*af732203SDimitry Andric uint32_t SymLength) {
87*af732203SDimitry Andric assert(SymLength > 0);
88*af732203SDimitry Andric Symbols.push_back(SymbolListWrapper(SymSrc, SymLength));
89*af732203SDimitry Andric
90*af732203SDimitry Andric // Symbols written to a PDB file are required to be 4 byte aligned. The same
91*af732203SDimitry Andric // is not true of object files.
92*af732203SDimitry Andric assert(SymLength % alignOf(CodeViewContainer::Pdb) == 0 &&
93*af732203SDimitry Andric "Invalid Symbol alignment!");
94*af732203SDimitry Andric SymbolByteSize += SymLength;
95*af732203SDimitry Andric }
96*af732203SDimitry Andric
addSourceFile(StringRef Path)970b57cec5SDimitry Andric void DbiModuleDescriptorBuilder::addSourceFile(StringRef Path) {
985ffd83dbSDimitry Andric SourceFiles.push_back(std::string(Path));
990b57cec5SDimitry Andric }
1000b57cec5SDimitry Andric
calculateC13DebugInfoSize() const1010b57cec5SDimitry Andric uint32_t DbiModuleDescriptorBuilder::calculateC13DebugInfoSize() const {
1020b57cec5SDimitry Andric uint32_t Result = 0;
1030b57cec5SDimitry Andric for (const auto &Builder : C13Builders) {
1045ffd83dbSDimitry Andric Result += Builder.calculateSerializedLength();
1050b57cec5SDimitry Andric }
1060b57cec5SDimitry Andric return Result;
1070b57cec5SDimitry Andric }
1080b57cec5SDimitry Andric
calculateSerializedLength() const1090b57cec5SDimitry Andric uint32_t DbiModuleDescriptorBuilder::calculateSerializedLength() const {
1100b57cec5SDimitry Andric uint32_t L = sizeof(Layout);
1110b57cec5SDimitry Andric uint32_t M = ModuleName.size() + 1;
1120b57cec5SDimitry Andric uint32_t O = ObjFileName.size() + 1;
1130b57cec5SDimitry Andric return alignTo(L + M + O, sizeof(uint32_t));
1140b57cec5SDimitry Andric }
1150b57cec5SDimitry Andric
finalize()1160b57cec5SDimitry Andric void DbiModuleDescriptorBuilder::finalize() {
1170b57cec5SDimitry Andric Layout.FileNameOffs = 0; // TODO: Fix this
1180b57cec5SDimitry Andric Layout.Flags = 0; // TODO: Fix this
1190b57cec5SDimitry Andric Layout.C11Bytes = 0;
1200b57cec5SDimitry Andric Layout.C13Bytes = calculateC13DebugInfoSize();
1210b57cec5SDimitry Andric (void)Layout.Mod; // Set in constructor
1220b57cec5SDimitry Andric (void)Layout.ModDiStream; // Set in finalizeMsfLayout
1230b57cec5SDimitry Andric Layout.NumFiles = SourceFiles.size();
1240b57cec5SDimitry Andric Layout.PdbFilePathNI = PdbFilePathNI;
1250b57cec5SDimitry Andric Layout.SrcFileNameNI = 0;
1260b57cec5SDimitry Andric
1270b57cec5SDimitry Andric // This value includes both the signature field as well as the record bytes
1280b57cec5SDimitry Andric // from the symbol stream.
1290b57cec5SDimitry Andric Layout.SymBytes =
1300b57cec5SDimitry Andric Layout.ModDiStream == kInvalidStreamIndex ? 0 : getNextSymbolOffset();
1310b57cec5SDimitry Andric }
1320b57cec5SDimitry Andric
finalizeMsfLayout()1330b57cec5SDimitry Andric Error DbiModuleDescriptorBuilder::finalizeMsfLayout() {
1340b57cec5SDimitry Andric this->Layout.ModDiStream = kInvalidStreamIndex;
1350b57cec5SDimitry Andric uint32_t C13Size = calculateC13DebugInfoSize();
1360b57cec5SDimitry Andric if (!C13Size && !SymbolByteSize)
1370b57cec5SDimitry Andric return Error::success();
1380b57cec5SDimitry Andric auto ExpectedSN =
1390b57cec5SDimitry Andric MSF.addStream(calculateDiSymbolStreamSize(SymbolByteSize, C13Size));
1400b57cec5SDimitry Andric if (!ExpectedSN)
1410b57cec5SDimitry Andric return ExpectedSN.takeError();
1420b57cec5SDimitry Andric Layout.ModDiStream = *ExpectedSN;
1430b57cec5SDimitry Andric return Error::success();
1440b57cec5SDimitry Andric }
1450b57cec5SDimitry Andric
commit(BinaryStreamWriter & ModiWriter)146*af732203SDimitry Andric Error DbiModuleDescriptorBuilder::commit(BinaryStreamWriter &ModiWriter) {
1470b57cec5SDimitry Andric // We write the Modi record to the `ModiWriter`, but we additionally write its
1480b57cec5SDimitry Andric // symbol stream to a brand new stream.
1490b57cec5SDimitry Andric if (auto EC = ModiWriter.writeObject(Layout))
1500b57cec5SDimitry Andric return EC;
1510b57cec5SDimitry Andric if (auto EC = ModiWriter.writeCString(ModuleName))
1520b57cec5SDimitry Andric return EC;
1530b57cec5SDimitry Andric if (auto EC = ModiWriter.writeCString(ObjFileName))
1540b57cec5SDimitry Andric return EC;
1550b57cec5SDimitry Andric if (auto EC = ModiWriter.padToAlignment(sizeof(uint32_t)))
1560b57cec5SDimitry Andric return EC;
157*af732203SDimitry Andric return Error::success();
158*af732203SDimitry Andric }
1590b57cec5SDimitry Andric
commitSymbolStream(const msf::MSFLayout & MsfLayout,WritableBinaryStreamRef MsfBuffer)160*af732203SDimitry Andric Error DbiModuleDescriptorBuilder::commitSymbolStream(
161*af732203SDimitry Andric const msf::MSFLayout &MsfLayout, WritableBinaryStreamRef MsfBuffer) {
162*af732203SDimitry Andric if (Layout.ModDiStream == kInvalidStreamIndex)
163*af732203SDimitry Andric return Error::success();
164*af732203SDimitry Andric
1650b57cec5SDimitry Andric auto NS = WritableMappedBlockStream::createIndexedStream(
1660b57cec5SDimitry Andric MsfLayout, MsfBuffer, Layout.ModDiStream, MSF.getAllocator());
1670b57cec5SDimitry Andric WritableBinaryStreamRef Ref(*NS);
1680b57cec5SDimitry Andric BinaryStreamWriter SymbolWriter(Ref);
1690b57cec5SDimitry Andric // Write the symbols.
170*af732203SDimitry Andric if (auto EC = SymbolWriter.writeInteger<uint32_t>(COFF::DEBUG_SECTION_MAGIC))
1710b57cec5SDimitry Andric return EC;
172*af732203SDimitry Andric for (const SymbolListWrapper &Sym : Symbols) {
173*af732203SDimitry Andric if (Sym.NeedsToBeMerged) {
174*af732203SDimitry Andric assert(MergeSymsCallback);
175*af732203SDimitry Andric if (auto EC = MergeSymsCallback(MergeSymsCtx, Sym.SymPtr, SymbolWriter))
176*af732203SDimitry Andric return EC;
177*af732203SDimitry Andric } else {
178*af732203SDimitry Andric if (auto EC = SymbolWriter.writeBytes(Sym.asArray()))
1790b57cec5SDimitry Andric return EC;
1800b57cec5SDimitry Andric }
181*af732203SDimitry Andric }
182*af732203SDimitry Andric
183*af732203SDimitry Andric // Apply the string table fixups.
184*af732203SDimitry Andric auto SavedOffset = SymbolWriter.getOffset();
185*af732203SDimitry Andric for (const StringTableFixup &Fixup : StringTableFixups) {
186*af732203SDimitry Andric SymbolWriter.setOffset(Fixup.SymOffsetOfReference);
187*af732203SDimitry Andric if (auto E = SymbolWriter.writeInteger<uint32_t>(Fixup.StrTabOffset))
188*af732203SDimitry Andric return E;
189*af732203SDimitry Andric }
190*af732203SDimitry Andric SymbolWriter.setOffset(SavedOffset);
191*af732203SDimitry Andric
1920b57cec5SDimitry Andric assert(SymbolWriter.getOffset() % alignOf(CodeViewContainer::Pdb) == 0 &&
1930b57cec5SDimitry Andric "Invalid debug section alignment!");
1940b57cec5SDimitry Andric // TODO: Write C11 Line data
1950b57cec5SDimitry Andric for (const auto &Builder : C13Builders) {
1965ffd83dbSDimitry Andric if (auto EC = Builder.commit(SymbolWriter, CodeViewContainer::Pdb))
1970b57cec5SDimitry Andric return EC;
1980b57cec5SDimitry Andric }
1990b57cec5SDimitry Andric
2000b57cec5SDimitry Andric // TODO: Figure out what GlobalRefs substream actually is and populate it.
2010b57cec5SDimitry Andric if (auto EC = SymbolWriter.writeInteger<uint32_t>(0))
2020b57cec5SDimitry Andric return EC;
2030b57cec5SDimitry Andric if (SymbolWriter.bytesRemaining() > 0)
2040b57cec5SDimitry Andric return make_error<RawError>(raw_error_code::stream_too_long);
205*af732203SDimitry Andric
2060b57cec5SDimitry Andric return Error::success();
2070b57cec5SDimitry Andric }
2080b57cec5SDimitry Andric
addDebugSubsection(std::shared_ptr<DebugSubsection> Subsection)2090b57cec5SDimitry Andric void DbiModuleDescriptorBuilder::addDebugSubsection(
2100b57cec5SDimitry Andric std::shared_ptr<DebugSubsection> Subsection) {
2110b57cec5SDimitry Andric assert(Subsection);
2125ffd83dbSDimitry Andric C13Builders.push_back(DebugSubsectionRecordBuilder(std::move(Subsection)));
2130b57cec5SDimitry Andric }
2140b57cec5SDimitry Andric
addDebugSubsection(const DebugSubsectionRecord & SubsectionContents)2150b57cec5SDimitry Andric void DbiModuleDescriptorBuilder::addDebugSubsection(
2160b57cec5SDimitry Andric const DebugSubsectionRecord &SubsectionContents) {
2175ffd83dbSDimitry Andric C13Builders.push_back(DebugSubsectionRecordBuilder(SubsectionContents));
2180b57cec5SDimitry Andric }
219