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"
1381ad6265SDimitry Andric #include "llvm/DebugInfo/CodeView/CodeView.h"
140b57cec5SDimitry Andric #include "llvm/DebugInfo/CodeView/DebugSubsectionRecord.h"
150b57cec5SDimitry Andric #include "llvm/DebugInfo/MSF/MSFBuilder.h"
160b57cec5SDimitry Andric #include "llvm/DebugInfo/MSF/MappedBlockStream.h"
170b57cec5SDimitry Andric #include "llvm/DebugInfo/PDB/Native/RawConstants.h"
180b57cec5SDimitry Andric #include "llvm/DebugInfo/PDB/Native/RawError.h"
190b57cec5SDimitry Andric #include "llvm/Support/BinaryStreamWriter.h"
200b57cec5SDimitry Andric 
210b57cec5SDimitry Andric using namespace llvm;
220b57cec5SDimitry Andric using namespace llvm::codeview;
230b57cec5SDimitry Andric using namespace llvm::msf;
240b57cec5SDimitry Andric using namespace llvm::pdb;
250b57cec5SDimitry Andric 
2681ad6265SDimitry Andric namespace llvm {
2781ad6265SDimitry Andric namespace codeview {
2881ad6265SDimitry Andric class DebugSubsection;
2981ad6265SDimitry Andric }
3081ad6265SDimitry Andric } // namespace llvm
3181ad6265SDimitry Andric 
calculateDiSymbolStreamSize(uint32_t SymbolByteSize,uint32_t C13Size)320b57cec5SDimitry Andric static uint32_t calculateDiSymbolStreamSize(uint32_t SymbolByteSize,
330b57cec5SDimitry Andric                                             uint32_t C13Size) {
340b57cec5SDimitry Andric   uint32_t Size = sizeof(uint32_t);   // Signature
350b57cec5SDimitry Andric   Size += alignTo(SymbolByteSize, 4); // Symbol Data
360b57cec5SDimitry Andric   Size += 0;                          // TODO: Layout.C11Bytes
370b57cec5SDimitry Andric   Size += C13Size;                    // C13 Debug Info Size
380b57cec5SDimitry Andric   Size += sizeof(uint32_t);           // GlobalRefs substream size (always 0)
390b57cec5SDimitry Andric   Size += 0;                          // GlobalRefs substream bytes
400b57cec5SDimitry Andric   return Size;
410b57cec5SDimitry Andric }
420b57cec5SDimitry Andric 
DbiModuleDescriptorBuilder(StringRef ModuleName,uint32_t ModIndex,msf::MSFBuilder & Msf)430b57cec5SDimitry Andric DbiModuleDescriptorBuilder::DbiModuleDescriptorBuilder(StringRef ModuleName,
440b57cec5SDimitry Andric                                                        uint32_t ModIndex,
450b57cec5SDimitry Andric                                                        msf::MSFBuilder &Msf)
465ffd83dbSDimitry Andric     : MSF(Msf), ModuleName(std::string(ModuleName)) {
470b57cec5SDimitry Andric   ::memset(&Layout, 0, sizeof(Layout));
480b57cec5SDimitry Andric   Layout.Mod = ModIndex;
490b57cec5SDimitry Andric }
500b57cec5SDimitry Andric 
5181ad6265SDimitry Andric DbiModuleDescriptorBuilder::~DbiModuleDescriptorBuilder() = default;
520b57cec5SDimitry Andric 
getStreamIndex() const530b57cec5SDimitry Andric uint16_t DbiModuleDescriptorBuilder::getStreamIndex() const {
540b57cec5SDimitry Andric   return Layout.ModDiStream;
550b57cec5SDimitry Andric }
560b57cec5SDimitry Andric 
setObjFileName(StringRef Name)570b57cec5SDimitry Andric void DbiModuleDescriptorBuilder::setObjFileName(StringRef Name) {
585ffd83dbSDimitry Andric   ObjFileName = std::string(Name);
590b57cec5SDimitry Andric }
600b57cec5SDimitry Andric 
setPdbFilePathNI(uint32_t NI)610b57cec5SDimitry Andric void DbiModuleDescriptorBuilder::setPdbFilePathNI(uint32_t NI) {
620b57cec5SDimitry Andric   PdbFilePathNI = NI;
630b57cec5SDimitry Andric }
640b57cec5SDimitry Andric 
setFirstSectionContrib(const SectionContrib & SC)650b57cec5SDimitry Andric void DbiModuleDescriptorBuilder::setFirstSectionContrib(
660b57cec5SDimitry Andric     const SectionContrib &SC) {
670b57cec5SDimitry Andric   Layout.SC = SC;
680b57cec5SDimitry Andric }
690b57cec5SDimitry Andric 
addSymbol(CVSymbol Symbol)700b57cec5SDimitry Andric void DbiModuleDescriptorBuilder::addSymbol(CVSymbol Symbol) {
710b57cec5SDimitry Andric   // Defer to the bulk API. It does the same thing.
720b57cec5SDimitry Andric   addSymbolsInBulk(Symbol.data());
730b57cec5SDimitry Andric }
740b57cec5SDimitry Andric 
addSymbolsInBulk(ArrayRef<uint8_t> BulkSymbols)750b57cec5SDimitry Andric void DbiModuleDescriptorBuilder::addSymbolsInBulk(
760b57cec5SDimitry Andric     ArrayRef<uint8_t> BulkSymbols) {
770b57cec5SDimitry Andric   // Do nothing for empty runs of symbols.
780b57cec5SDimitry Andric   if (BulkSymbols.empty())
790b57cec5SDimitry Andric     return;
800b57cec5SDimitry Andric 
81e8d8bef9SDimitry Andric   Symbols.push_back(SymbolListWrapper(BulkSymbols));
820b57cec5SDimitry Andric   // Symbols written to a PDB file are required to be 4 byte aligned. The same
830b57cec5SDimitry Andric   // is not true of object files.
840b57cec5SDimitry Andric   assert(BulkSymbols.size() % alignOf(CodeViewContainer::Pdb) == 0 &&
850b57cec5SDimitry Andric          "Invalid Symbol alignment!");
860b57cec5SDimitry Andric   SymbolByteSize += BulkSymbols.size();
870b57cec5SDimitry Andric }
880b57cec5SDimitry Andric 
addUnmergedSymbols(void * SymSrc,uint32_t SymLength)89e8d8bef9SDimitry Andric void DbiModuleDescriptorBuilder::addUnmergedSymbols(void *SymSrc,
90e8d8bef9SDimitry Andric                                                     uint32_t SymLength) {
91e8d8bef9SDimitry Andric   assert(SymLength > 0);
92e8d8bef9SDimitry Andric   Symbols.push_back(SymbolListWrapper(SymSrc, SymLength));
93e8d8bef9SDimitry Andric 
94e8d8bef9SDimitry Andric   // Symbols written to a PDB file are required to be 4 byte aligned. The same
95e8d8bef9SDimitry Andric   // is not true of object files.
96e8d8bef9SDimitry Andric   assert(SymLength % alignOf(CodeViewContainer::Pdb) == 0 &&
97e8d8bef9SDimitry Andric          "Invalid Symbol alignment!");
98e8d8bef9SDimitry Andric   SymbolByteSize += SymLength;
99e8d8bef9SDimitry Andric }
100e8d8bef9SDimitry Andric 
addSourceFile(StringRef Path)1010b57cec5SDimitry Andric void DbiModuleDescriptorBuilder::addSourceFile(StringRef Path) {
1025ffd83dbSDimitry Andric   SourceFiles.push_back(std::string(Path));
1030b57cec5SDimitry Andric }
1040b57cec5SDimitry Andric 
calculateC13DebugInfoSize() const1050b57cec5SDimitry Andric uint32_t DbiModuleDescriptorBuilder::calculateC13DebugInfoSize() const {
1060b57cec5SDimitry Andric   uint32_t Result = 0;
1070b57cec5SDimitry Andric   for (const auto &Builder : C13Builders) {
1085ffd83dbSDimitry Andric     Result += Builder.calculateSerializedLength();
1090b57cec5SDimitry Andric   }
1100b57cec5SDimitry Andric   return Result;
1110b57cec5SDimitry Andric }
1120b57cec5SDimitry Andric 
calculateSerializedLength() const1130b57cec5SDimitry Andric uint32_t DbiModuleDescriptorBuilder::calculateSerializedLength() const {
1140b57cec5SDimitry Andric   uint32_t L = sizeof(Layout);
1150b57cec5SDimitry Andric   uint32_t M = ModuleName.size() + 1;
1160b57cec5SDimitry Andric   uint32_t O = ObjFileName.size() + 1;
1170b57cec5SDimitry Andric   return alignTo(L + M + O, sizeof(uint32_t));
1180b57cec5SDimitry Andric }
1190b57cec5SDimitry Andric 
finalize()1200b57cec5SDimitry Andric void DbiModuleDescriptorBuilder::finalize() {
1210b57cec5SDimitry Andric   Layout.FileNameOffs = 0; // TODO: Fix this
1220b57cec5SDimitry Andric   Layout.Flags = 0;        // TODO: Fix this
1230b57cec5SDimitry Andric   Layout.C11Bytes = 0;
1240b57cec5SDimitry Andric   Layout.C13Bytes = calculateC13DebugInfoSize();
1250b57cec5SDimitry Andric   (void)Layout.Mod;         // Set in constructor
1260b57cec5SDimitry Andric   (void)Layout.ModDiStream; // Set in finalizeMsfLayout
1270b57cec5SDimitry Andric   Layout.NumFiles = SourceFiles.size();
1280b57cec5SDimitry Andric   Layout.PdbFilePathNI = PdbFilePathNI;
1290b57cec5SDimitry Andric   Layout.SrcFileNameNI = 0;
1300b57cec5SDimitry Andric 
1310b57cec5SDimitry Andric   // This value includes both the signature field as well as the record bytes
1320b57cec5SDimitry Andric   // from the symbol stream.
1330b57cec5SDimitry Andric   Layout.SymBytes =
1340b57cec5SDimitry Andric       Layout.ModDiStream == kInvalidStreamIndex ? 0 : getNextSymbolOffset();
1350b57cec5SDimitry Andric }
1360b57cec5SDimitry Andric 
finalizeMsfLayout()1370b57cec5SDimitry Andric Error DbiModuleDescriptorBuilder::finalizeMsfLayout() {
1380b57cec5SDimitry Andric   this->Layout.ModDiStream = kInvalidStreamIndex;
1390b57cec5SDimitry Andric   uint32_t C13Size = calculateC13DebugInfoSize();
1400b57cec5SDimitry Andric   if (!C13Size && !SymbolByteSize)
1410b57cec5SDimitry Andric     return Error::success();
1420b57cec5SDimitry Andric   auto ExpectedSN =
1430b57cec5SDimitry Andric       MSF.addStream(calculateDiSymbolStreamSize(SymbolByteSize, C13Size));
1440b57cec5SDimitry Andric   if (!ExpectedSN)
1450b57cec5SDimitry Andric     return ExpectedSN.takeError();
1460b57cec5SDimitry Andric   Layout.ModDiStream = *ExpectedSN;
1470b57cec5SDimitry Andric   return Error::success();
1480b57cec5SDimitry Andric }
1490b57cec5SDimitry Andric 
commit(BinaryStreamWriter & ModiWriter)150e8d8bef9SDimitry Andric Error DbiModuleDescriptorBuilder::commit(BinaryStreamWriter &ModiWriter) {
1510b57cec5SDimitry Andric   // We write the Modi record to the `ModiWriter`, but we additionally write its
1520b57cec5SDimitry Andric   // symbol stream to a brand new stream.
1530b57cec5SDimitry Andric   if (auto EC = ModiWriter.writeObject(Layout))
1540b57cec5SDimitry Andric     return EC;
1550b57cec5SDimitry Andric   if (auto EC = ModiWriter.writeCString(ModuleName))
1560b57cec5SDimitry Andric     return EC;
1570b57cec5SDimitry Andric   if (auto EC = ModiWriter.writeCString(ObjFileName))
1580b57cec5SDimitry Andric     return EC;
1590b57cec5SDimitry Andric   if (auto EC = ModiWriter.padToAlignment(sizeof(uint32_t)))
1600b57cec5SDimitry Andric     return EC;
161e8d8bef9SDimitry Andric   return Error::success();
162e8d8bef9SDimitry Andric }
1630b57cec5SDimitry Andric 
commitSymbolStream(const msf::MSFLayout & MsfLayout,WritableBinaryStreamRef MsfBuffer)164e8d8bef9SDimitry Andric Error DbiModuleDescriptorBuilder::commitSymbolStream(
165e8d8bef9SDimitry Andric     const msf::MSFLayout &MsfLayout, WritableBinaryStreamRef MsfBuffer) {
166e8d8bef9SDimitry Andric   if (Layout.ModDiStream == kInvalidStreamIndex)
167e8d8bef9SDimitry Andric     return Error::success();
168e8d8bef9SDimitry Andric 
1690b57cec5SDimitry Andric   auto NS = WritableMappedBlockStream::createIndexedStream(
1700b57cec5SDimitry Andric       MsfLayout, MsfBuffer, Layout.ModDiStream, MSF.getAllocator());
1710b57cec5SDimitry Andric   WritableBinaryStreamRef Ref(*NS);
1720b57cec5SDimitry Andric   BinaryStreamWriter SymbolWriter(Ref);
1730b57cec5SDimitry Andric   // Write the symbols.
174e8d8bef9SDimitry Andric   if (auto EC = SymbolWriter.writeInteger<uint32_t>(COFF::DEBUG_SECTION_MAGIC))
1750b57cec5SDimitry Andric     return EC;
176e8d8bef9SDimitry Andric   for (const SymbolListWrapper &Sym : Symbols) {
177e8d8bef9SDimitry Andric     if (Sym.NeedsToBeMerged) {
178e8d8bef9SDimitry Andric       assert(MergeSymsCallback);
179e8d8bef9SDimitry Andric       if (auto EC = MergeSymsCallback(MergeSymsCtx, Sym.SymPtr, SymbolWriter))
180e8d8bef9SDimitry Andric         return EC;
181e8d8bef9SDimitry Andric     } else {
182e8d8bef9SDimitry Andric       if (auto EC = SymbolWriter.writeBytes(Sym.asArray()))
1830b57cec5SDimitry Andric         return EC;
1840b57cec5SDimitry Andric     }
185e8d8bef9SDimitry Andric   }
186e8d8bef9SDimitry Andric 
187e8d8bef9SDimitry Andric   // Apply the string table fixups.
188e8d8bef9SDimitry Andric   auto SavedOffset = SymbolWriter.getOffset();
189e8d8bef9SDimitry Andric   for (const StringTableFixup &Fixup : StringTableFixups) {
190e8d8bef9SDimitry Andric     SymbolWriter.setOffset(Fixup.SymOffsetOfReference);
191e8d8bef9SDimitry Andric     if (auto E = SymbolWriter.writeInteger<uint32_t>(Fixup.StrTabOffset))
192e8d8bef9SDimitry Andric       return E;
193e8d8bef9SDimitry Andric   }
194e8d8bef9SDimitry Andric   SymbolWriter.setOffset(SavedOffset);
195e8d8bef9SDimitry Andric 
1960b57cec5SDimitry Andric   assert(SymbolWriter.getOffset() % alignOf(CodeViewContainer::Pdb) == 0 &&
1970b57cec5SDimitry Andric          "Invalid debug section alignment!");
1980b57cec5SDimitry Andric   // TODO: Write C11 Line data
1990b57cec5SDimitry Andric   for (const auto &Builder : C13Builders) {
2005ffd83dbSDimitry Andric     if (auto EC = Builder.commit(SymbolWriter, CodeViewContainer::Pdb))
2010b57cec5SDimitry Andric       return EC;
2020b57cec5SDimitry Andric   }
2030b57cec5SDimitry Andric 
2040b57cec5SDimitry Andric   // TODO: Figure out what GlobalRefs substream actually is and populate it.
2050b57cec5SDimitry Andric   if (auto EC = SymbolWriter.writeInteger<uint32_t>(0))
2060b57cec5SDimitry Andric     return EC;
2070b57cec5SDimitry Andric   if (SymbolWriter.bytesRemaining() > 0)
2080b57cec5SDimitry Andric     return make_error<RawError>(raw_error_code::stream_too_long);
209e8d8bef9SDimitry Andric 
2100b57cec5SDimitry Andric   return Error::success();
2110b57cec5SDimitry Andric }
2120b57cec5SDimitry Andric 
addDebugSubsection(std::shared_ptr<DebugSubsection> Subsection)2130b57cec5SDimitry Andric void DbiModuleDescriptorBuilder::addDebugSubsection(
2140b57cec5SDimitry Andric     std::shared_ptr<DebugSubsection> Subsection) {
2150b57cec5SDimitry Andric   assert(Subsection);
2165ffd83dbSDimitry Andric   C13Builders.push_back(DebugSubsectionRecordBuilder(std::move(Subsection)));
2170b57cec5SDimitry Andric }
2180b57cec5SDimitry Andric 
addDebugSubsection(const DebugSubsectionRecord & SubsectionContents)2190b57cec5SDimitry Andric void DbiModuleDescriptorBuilder::addDebugSubsection(
2200b57cec5SDimitry Andric     const DebugSubsectionRecord &SubsectionContents) {
2215ffd83dbSDimitry Andric   C13Builders.push_back(DebugSubsectionRecordBuilder(SubsectionContents));
2220b57cec5SDimitry Andric }
223