1 //===- DbiStreamBuilder.cpp - PDB Dbi Stream 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/DbiStreamBuilder.h"
12 #include "llvm/ADT/ArrayRef.h"
13 #include "llvm/BinaryFormat/COFF.h"
14 #include "llvm/DebugInfo/MSF/MSFBuilder.h"
15 #include "llvm/DebugInfo/MSF/MappedBlockStream.h"
16 #include "llvm/DebugInfo/PDB/Native/DbiModuleDescriptorBuilder.h"
17 #include "llvm/DebugInfo/PDB/Native/DbiStream.h"
18 #include "llvm/DebugInfo/PDB/Native/RawError.h"
19 #include "llvm/Object/COFF.h"
20 #include "llvm/Support/BinaryStreamWriter.h"
23 using namespace llvm::codeview;
24 using namespace llvm::msf;
25 using namespace llvm::pdb;
27 DbiStreamBuilder::DbiStreamBuilder(msf::MSFBuilder &Msf)
28 : Msf(Msf), Allocator(Msf.getAllocator()), Age(1), BuildNumber(0),
29 PdbDllVersion(0), PdbDllRbld(0), Flags(0), MachineType(PDB_Machine::x86),
32 DbiStreamBuilder::~DbiStreamBuilder() {}
34 void DbiStreamBuilder::setVersionHeader(PdbRaw_DbiVer V) { VerHeader = V; }
36 void DbiStreamBuilder::setAge(uint32_t A) { Age = A; }
38 void DbiStreamBuilder::setBuildNumber(uint16_t B) { BuildNumber = B; }
40 void DbiStreamBuilder::setBuildNumber(uint8_t Major, uint8_t Minor) {
41 BuildNumber = (uint16_t(Major) << DbiBuildNo::BuildMajorShift) &
42 DbiBuildNo::BuildMajorMask;
43 BuildNumber |= (uint16_t(Minor) << DbiBuildNo::BuildMinorShift) &
44 DbiBuildNo::BuildMinorMask;
45 BuildNumber |= DbiBuildNo::NewVersionFormatMask;
48 void DbiStreamBuilder::setPdbDllVersion(uint16_t V) { PdbDllVersion = V; }
50 void DbiStreamBuilder::setPdbDllRbld(uint16_t R) { PdbDllRbld = R; }
52 void DbiStreamBuilder::setFlags(uint16_t F) { Flags = F; }
54 void DbiStreamBuilder::setMachineType(PDB_Machine M) { MachineType = M; }
56 void DbiStreamBuilder::setMachineType(COFF::MachineTypes M) {
57 // These enums are mirrors of each other, so we can just cast the value.
58 MachineType = static_cast<pdb::PDB_Machine>(static_cast<unsigned>(M));
61 void DbiStreamBuilder::setSectionMap(ArrayRef<SecMapEntry> SecMap) {
65 void DbiStreamBuilder::setGlobalsStreamIndex(uint32_t Index) {
66 GlobalsStreamIndex = Index;
69 void DbiStreamBuilder::setSymbolRecordStreamIndex(uint32_t Index) {
70 SymRecordStreamIndex = Index;
73 void DbiStreamBuilder::setPublicsStreamIndex(uint32_t Index) {
74 PublicsStreamIndex = Index;
77 Error DbiStreamBuilder::addDbgStream(pdb::DbgHeaderType Type,
78 ArrayRef<uint8_t> Data) {
79 DbgStreams[(int)Type].emplace();
80 DbgStreams[(int)Type]->Data = Data;
81 return Error::success();
84 uint32_t DbiStreamBuilder::addECName(StringRef Name) {
85 return ECNamesBuilder.insert(Name);
88 uint32_t DbiStreamBuilder::calculateSerializedLength() const {
89 // For now we only support serializing the header.
90 return sizeof(DbiStreamHeader) + calculateFileInfoSubstreamSize() +
91 calculateModiSubstreamSize() + calculateSectionContribsStreamSize() +
92 calculateSectionMapStreamSize() + calculateDbgStreamsSize() +
93 ECNamesBuilder.calculateSerializedSize();
96 Expected<DbiModuleDescriptorBuilder &>
97 DbiStreamBuilder::addModuleInfo(StringRef ModuleName) {
98 uint32_t Index = ModiList.size();
100 llvm::make_unique<DbiModuleDescriptorBuilder>(ModuleName, Index, Msf));
101 return *ModiList.back();
104 Error DbiStreamBuilder::addModuleSourceFile(DbiModuleDescriptorBuilder &Module,
106 uint32_t Index = SourceFileNames.size();
107 SourceFileNames.insert(std::make_pair(File, Index));
108 Module.addSourceFile(File);
109 return Error::success();
112 Expected<uint32_t> DbiStreamBuilder::getSourceFileNameIndex(StringRef File) {
113 auto NameIter = SourceFileNames.find(File);
114 if (NameIter == SourceFileNames.end())
115 return make_error<RawError>(raw_error_code::no_entry,
116 "The specified source file was not found");
117 return NameIter->getValue();
120 uint32_t DbiStreamBuilder::calculateModiSubstreamSize() const {
122 for (const auto &M : ModiList)
123 Size += M->calculateSerializedLength();
127 uint32_t DbiStreamBuilder::calculateSectionContribsStreamSize() const {
128 if (SectionContribs.empty())
130 return sizeof(enum PdbRaw_DbiSecContribVer) +
131 sizeof(SectionContribs[0]) * SectionContribs.size();
134 uint32_t DbiStreamBuilder::calculateSectionMapStreamSize() const {
135 if (SectionMap.empty())
137 return sizeof(SecMapHeader) + sizeof(SecMapEntry) * SectionMap.size();
140 uint32_t DbiStreamBuilder::calculateNamesOffset() const {
142 Offset += sizeof(ulittle16_t); // NumModules
143 Offset += sizeof(ulittle16_t); // NumSourceFiles
144 Offset += ModiList.size() * sizeof(ulittle16_t); // ModIndices
145 Offset += ModiList.size() * sizeof(ulittle16_t); // ModFileCounts
146 uint32_t NumFileInfos = 0;
147 for (const auto &M : ModiList)
148 NumFileInfos += M->source_files().size();
149 Offset += NumFileInfos * sizeof(ulittle32_t); // FileNameOffsets
153 uint32_t DbiStreamBuilder::calculateFileInfoSubstreamSize() const {
154 uint32_t Size = calculateNamesOffset();
155 Size += calculateNamesBufferSize();
156 return alignTo(Size, sizeof(uint32_t));
159 uint32_t DbiStreamBuilder::calculateNamesBufferSize() const {
161 for (const auto &F : SourceFileNames) {
162 Size += F.getKeyLength() + 1; // Names[I];
167 uint32_t DbiStreamBuilder::calculateDbgStreamsSize() const {
168 return DbgStreams.size() * sizeof(uint16_t);
171 Error DbiStreamBuilder::generateFileInfoSubstream() {
172 uint32_t Size = calculateFileInfoSubstreamSize();
173 auto Data = Allocator.Allocate<uint8_t>(Size);
174 uint32_t NamesOffset = calculateNamesOffset();
176 FileInfoBuffer = MutableBinaryByteStream(MutableArrayRef<uint8_t>(Data, Size),
177 llvm::support::little);
179 WritableBinaryStreamRef MetadataBuffer =
180 WritableBinaryStreamRef(FileInfoBuffer).keep_front(NamesOffset);
181 BinaryStreamWriter MetadataWriter(MetadataBuffer);
183 uint16_t ModiCount = std::min<uint32_t>(UINT16_MAX, ModiList.size());
184 uint16_t FileCount = std::min<uint32_t>(UINT16_MAX, SourceFileNames.size());
185 if (auto EC = MetadataWriter.writeInteger(ModiCount)) // NumModules
187 if (auto EC = MetadataWriter.writeInteger(FileCount)) // NumSourceFiles
189 for (uint16_t I = 0; I < ModiCount; ++I) {
190 if (auto EC = MetadataWriter.writeInteger(I)) // Mod Indices
193 for (const auto &MI : ModiList) {
194 FileCount = static_cast<uint16_t>(MI->source_files().size());
195 if (auto EC = MetadataWriter.writeInteger(FileCount)) // Mod File Counts
199 // Before writing the FileNameOffsets array, write the NamesBuffer array.
200 // A side effect of this is that this will actually compute the various
201 // file name offsets, so we can then go back and write the FileNameOffsets
202 // array to the other substream.
203 NamesBuffer = WritableBinaryStreamRef(FileInfoBuffer).drop_front(NamesOffset);
204 BinaryStreamWriter NameBufferWriter(NamesBuffer);
205 for (auto &Name : SourceFileNames) {
206 Name.second = NameBufferWriter.getOffset();
207 if (auto EC = NameBufferWriter.writeCString(Name.getKey()))
211 for (const auto &MI : ModiList) {
212 for (StringRef Name : MI->source_files()) {
213 auto Result = SourceFileNames.find(Name);
214 if (Result == SourceFileNames.end())
215 return make_error<RawError>(raw_error_code::no_entry,
216 "The source file was not found.");
217 if (auto EC = MetadataWriter.writeInteger(Result->second))
222 if (auto EC = NameBufferWriter.padToAlignment(sizeof(uint32_t)))
225 if (NameBufferWriter.bytesRemaining() > 0)
226 return make_error<RawError>(raw_error_code::invalid_format,
227 "The names buffer contained unexpected data.");
229 if (MetadataWriter.bytesRemaining() > sizeof(uint32_t))
230 return make_error<RawError>(
231 raw_error_code::invalid_format,
232 "The metadata buffer contained unexpected data.");
234 return Error::success();
237 Error DbiStreamBuilder::finalize() {
239 return Error::success();
241 for (auto &MI : ModiList)
244 if (auto EC = generateFileInfoSubstream())
247 DbiStreamHeader *H = Allocator.Allocate<DbiStreamHeader>();
248 ::memset(H, 0, sizeof(DbiStreamHeader));
249 H->VersionHeader = *VerHeader;
250 H->VersionSignature = -1;
252 H->BuildNumber = BuildNumber;
254 H->PdbDllRbld = PdbDllRbld;
255 H->PdbDllVersion = PdbDllVersion;
256 H->MachineType = static_cast<uint16_t>(MachineType);
258 H->ECSubstreamSize = ECNamesBuilder.calculateSerializedSize();
259 H->FileInfoSize = FileInfoBuffer.getLength();
260 H->ModiSubstreamSize = calculateModiSubstreamSize();
261 H->OptionalDbgHdrSize = DbgStreams.size() * sizeof(uint16_t);
262 H->SecContrSubstreamSize = calculateSectionContribsStreamSize();
263 H->SectionMapSize = calculateSectionMapStreamSize();
264 H->TypeServerSize = 0;
265 H->SymRecordStreamIndex = SymRecordStreamIndex;
266 H->PublicSymbolStreamIndex = PublicsStreamIndex;
267 H->MFCTypeServerIndex = 0; // Not sure what this is, but link.exe writes 0.
268 H->GlobalSymbolStreamIndex = GlobalsStreamIndex;
271 return Error::success();
274 Error DbiStreamBuilder::finalizeMsfLayout() {
275 for (auto &S : DbgStreams) {
278 auto ExpectedIndex = Msf.addStream(S->Data.size());
280 return ExpectedIndex.takeError();
281 S->StreamNumber = *ExpectedIndex;
284 for (auto &MI : ModiList) {
285 if (auto EC = MI->finalizeMsfLayout())
289 uint32_t Length = calculateSerializedLength();
290 if (auto EC = Msf.setStreamSize(StreamDBI, Length))
292 return Error::success();
295 static uint16_t toSecMapFlags(uint32_t Flags) {
297 if (Flags & COFF::IMAGE_SCN_MEM_READ)
298 Ret |= static_cast<uint16_t>(OMFSegDescFlags::Read);
299 if (Flags & COFF::IMAGE_SCN_MEM_WRITE)
300 Ret |= static_cast<uint16_t>(OMFSegDescFlags::Write);
301 if (Flags & COFF::IMAGE_SCN_MEM_EXECUTE)
302 Ret |= static_cast<uint16_t>(OMFSegDescFlags::Execute);
303 if (Flags & COFF::IMAGE_SCN_MEM_EXECUTE)
304 Ret |= static_cast<uint16_t>(OMFSegDescFlags::Execute);
305 if (!(Flags & COFF::IMAGE_SCN_MEM_16BIT))
306 Ret |= static_cast<uint16_t>(OMFSegDescFlags::AddressIs32Bit);
308 // This seems always 1.
309 Ret |= static_cast<uint16_t>(OMFSegDescFlags::IsSelector);
314 // A utility function to create a Section Map for a given list of COFF sections.
316 // A Section Map seem to be a copy of a COFF section list in other format.
317 // I don't know why a PDB file contains both a COFF section header and
318 // a Section Map, but it seems it must be present in a PDB.
319 std::vector<SecMapEntry> DbiStreamBuilder::createSectionMap(
320 ArrayRef<llvm::object::coff_section> SecHdrs) {
321 std::vector<SecMapEntry> Ret;
324 auto Add = [&]() -> SecMapEntry & {
326 auto &Entry = Ret.back();
327 memset(&Entry, 0, sizeof(Entry));
329 Entry.Frame = Idx + 1;
331 // We don't know the meaning of these fields yet.
332 Entry.SecName = UINT16_MAX;
333 Entry.ClassName = UINT16_MAX;
338 for (auto &Hdr : SecHdrs) {
340 Entry.Flags = toSecMapFlags(Hdr.Characteristics);
341 Entry.SecByteLength = Hdr.VirtualSize;
345 // The last entry is for absolute symbols.
347 Entry.Flags = static_cast<uint16_t>(OMFSegDescFlags::AddressIs32Bit) |
348 static_cast<uint16_t>(OMFSegDescFlags::IsAbsoluteAddress);
349 Entry.SecByteLength = UINT32_MAX;
354 Error DbiStreamBuilder::commit(const msf::MSFLayout &Layout,
355 WritableBinaryStreamRef MsfBuffer) {
356 if (auto EC = finalize())
359 auto DbiS = WritableMappedBlockStream::createIndexedStream(
360 Layout, MsfBuffer, StreamDBI, Allocator);
362 BinaryStreamWriter Writer(*DbiS);
363 if (auto EC = Writer.writeObject(*Header))
366 for (auto &M : ModiList) {
367 if (auto EC = M->commit(Writer, Layout, MsfBuffer))
371 if (!SectionContribs.empty()) {
372 if (auto EC = Writer.writeEnum(DbiSecContribVer60))
374 if (auto EC = Writer.writeArray(makeArrayRef(SectionContribs)))
378 if (!SectionMap.empty()) {
379 ulittle16_t Size = static_cast<ulittle16_t>(SectionMap.size());
380 SecMapHeader SMHeader = {Size, Size};
381 if (auto EC = Writer.writeObject(SMHeader))
383 if (auto EC = Writer.writeArray(SectionMap))
387 if (auto EC = Writer.writeStreamRef(FileInfoBuffer))
390 if (auto EC = ECNamesBuilder.commit(Writer))
393 for (auto &Stream : DbgStreams) {
394 uint16_t StreamNumber = kInvalidStreamIndex;
395 if (Stream.hasValue())
396 StreamNumber = Stream->StreamNumber;
397 if (auto EC = Writer.writeInteger(StreamNumber))
401 for (auto &Stream : DbgStreams) {
402 if (!Stream.hasValue())
404 assert(Stream->StreamNumber != kInvalidStreamIndex);
406 auto WritableStream = WritableMappedBlockStream::createIndexedStream(
407 Layout, MsfBuffer, Stream->StreamNumber, Allocator);
408 BinaryStreamWriter DbgStreamWriter(*WritableStream);
409 if (auto EC = DbgStreamWriter.writeArray(Stream->Data))
413 if (Writer.bytesRemaining() > 0)
414 return make_error<RawError>(raw_error_code::invalid_format,
415 "Unexpected bytes found in DBI Stream");
416 return Error::success();