1 //===- DbiModuleDescriptorBuilder.cpp - PDB Mod Info Creation ---*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/DebugInfo/PDB/Native/DbiModuleDescriptorBuilder.h"
12 #include "llvm/ADT/ArrayRef.h"
13 #include "llvm/BinaryFormat/COFF.h"
14 #include "llvm/DebugInfo/CodeView/DebugSubsectionRecord.h"
15 #include "llvm/DebugInfo/MSF/MSFBuilder.h"
16 #include "llvm/DebugInfo/MSF/MSFCommon.h"
17 #include "llvm/DebugInfo/MSF/MappedBlockStream.h"
18 #include "llvm/DebugInfo/PDB/Native/DbiModuleDescriptor.h"
19 #include "llvm/DebugInfo/PDB/Native/RawConstants.h"
20 #include "llvm/DebugInfo/PDB/Native/RawError.h"
21 #include "llvm/Support/BinaryItemStream.h"
22 #include "llvm/Support/BinaryStreamWriter.h"
25 using namespace llvm::codeview;
26 using namespace llvm::msf;
27 using namespace llvm::pdb;
30 template <> struct BinaryItemTraits<CVSymbol> {
31 static size_t length(const CVSymbol &Item) { return Item.RecordData.size(); }
33 static ArrayRef<uint8_t> bytes(const CVSymbol &Item) {
34 return Item.RecordData;
39 static uint32_t calculateDiSymbolStreamSize(uint32_t SymbolByteSize,
41 uint32_t Size = sizeof(uint32_t); // Signature
42 Size += alignTo(SymbolByteSize, 4); // Symbol Data
43 Size += 0; // TODO: Layout.C11Bytes
44 Size += C13Size; // C13 Debug Info Size
45 Size += sizeof(uint32_t); // GlobalRefs substream size (always 0)
46 Size += 0; // GlobalRefs substream bytes
50 DbiModuleDescriptorBuilder::DbiModuleDescriptorBuilder(StringRef ModuleName,
53 : MSF(Msf), ModuleName(ModuleName) {
54 ::memset(&Layout, 0, sizeof(Layout));
55 Layout.Mod = ModIndex;
58 DbiModuleDescriptorBuilder::~DbiModuleDescriptorBuilder() {}
60 uint16_t DbiModuleDescriptorBuilder::getStreamIndex() const {
61 return Layout.ModDiStream;
64 void DbiModuleDescriptorBuilder::setObjFileName(StringRef Name) {
68 void DbiModuleDescriptorBuilder::setPdbFilePathNI(uint32_t NI) {
72 void DbiModuleDescriptorBuilder::addSymbol(CVSymbol Symbol) {
73 Symbols.push_back(Symbol);
74 // Symbols written to a PDB file are required to be 4 byte aligned. The same
75 // is not true of object files.
76 assert(Symbol.length() % alignOf(CodeViewContainer::Pdb) == 0 &&
77 "Invalid Symbol alignment!");
78 SymbolByteSize += Symbol.length();
81 void DbiModuleDescriptorBuilder::addSourceFile(StringRef Path) {
82 SourceFiles.push_back(Path);
85 uint32_t DbiModuleDescriptorBuilder::calculateC13DebugInfoSize() const {
87 for (const auto &Builder : C13Builders) {
88 assert(Builder && "Empty C13 Fragment Builder!");
89 Result += Builder->calculateSerializedLength();
94 uint32_t DbiModuleDescriptorBuilder::calculateSerializedLength() const {
95 uint32_t L = sizeof(Layout);
96 uint32_t M = ModuleName.size() + 1;
97 uint32_t O = ObjFileName.size() + 1;
98 return alignTo(L + M + O, sizeof(uint32_t));
101 template <typename T> struct Foo {
102 explicit Foo(T &&Answer) : Answer(Answer) {}
107 template <typename T> Foo<T> makeFoo(T &&t) { return Foo<T>(std::move(t)); }
109 void DbiModuleDescriptorBuilder::finalize() {
110 Layout.SC.ModuleIndex = Layout.Mod;
111 Layout.FileNameOffs = 0; // TODO: Fix this
112 Layout.Flags = 0; // TODO: Fix this
114 Layout.C13Bytes = calculateC13DebugInfoSize();
115 (void)Layout.Mod; // Set in constructor
116 (void)Layout.ModDiStream; // Set in finalizeMsfLayout
117 Layout.NumFiles = SourceFiles.size();
118 Layout.PdbFilePathNI = PdbFilePathNI;
119 Layout.SrcFileNameNI = 0;
121 // This value includes both the signature field as well as the record bytes
122 // from the symbol stream.
123 Layout.SymBytes = SymbolByteSize + sizeof(uint32_t);
126 Error DbiModuleDescriptorBuilder::finalizeMsfLayout() {
127 this->Layout.ModDiStream = kInvalidStreamIndex;
128 uint32_t C13Size = calculateC13DebugInfoSize();
130 MSF.addStream(calculateDiSymbolStreamSize(SymbolByteSize, C13Size));
132 return ExpectedSN.takeError();
133 Layout.ModDiStream = *ExpectedSN;
134 return Error::success();
137 Error DbiModuleDescriptorBuilder::commit(BinaryStreamWriter &ModiWriter,
138 const msf::MSFLayout &MsfLayout,
139 WritableBinaryStreamRef MsfBuffer) {
140 // We write the Modi record to the `ModiWriter`, but we additionally write its
141 // symbol stream to a brand new stream.
142 if (auto EC = ModiWriter.writeObject(Layout))
144 if (auto EC = ModiWriter.writeCString(ModuleName))
146 if (auto EC = ModiWriter.writeCString(ObjFileName))
148 if (auto EC = ModiWriter.padToAlignment(sizeof(uint32_t)))
151 if (Layout.ModDiStream != kInvalidStreamIndex) {
152 auto NS = WritableMappedBlockStream::createIndexedStream(
153 MsfLayout, MsfBuffer, Layout.ModDiStream, MSF.getAllocator());
154 WritableBinaryStreamRef Ref(*NS);
155 BinaryStreamWriter SymbolWriter(Ref);
156 // Write the symbols.
158 SymbolWriter.writeInteger<uint32_t>(COFF::DEBUG_SECTION_MAGIC))
160 BinaryItemStream<CVSymbol> Records(llvm::support::endianness::little);
161 Records.setItems(Symbols);
162 BinaryStreamRef RecordsRef(Records);
163 if (auto EC = SymbolWriter.writeStreamRef(RecordsRef))
165 if (auto EC = SymbolWriter.padToAlignment(4))
167 // TODO: Write C11 Line data
168 assert(SymbolWriter.getOffset() % alignOf(CodeViewContainer::Pdb) == 0 &&
169 "Invalid debug section alignment!");
170 for (const auto &Builder : C13Builders) {
171 assert(Builder && "Empty C13 Fragment Builder!");
172 if (auto EC = Builder->commit(SymbolWriter))
176 // TODO: Figure out what GlobalRefs substream actually is and populate it.
177 if (auto EC = SymbolWriter.writeInteger<uint32_t>(0))
179 if (SymbolWriter.bytesRemaining() > 0)
180 return make_error<RawError>(raw_error_code::stream_too_long);
182 return Error::success();
185 void DbiModuleDescriptorBuilder::addDebugSubsection(
186 std::shared_ptr<DebugSubsection> Subsection) {
188 C13Builders.push_back(llvm::make_unique<DebugSubsectionRecordBuilder>(
189 std::move(Subsection), CodeViewContainer::Pdb));
192 void DbiModuleDescriptorBuilder::addDebugSubsection(
193 const DebugSubsectionRecord &SubsectionContents) {
194 C13Builders.push_back(llvm::make_unique<DebugSubsectionRecordBuilder>(
195 SubsectionContents, CodeViewContainer::Pdb));