1 //===- DbiStreamBuilder.cpp - PDB Dbi Stream Creation -----------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "llvm/DebugInfo/PDB/Native/DbiStreamBuilder.h"
11 
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"
21 
22 using namespace llvm;
23 using namespace llvm::codeview;
24 using namespace llvm::msf;
25 using namespace llvm::pdb;
26 
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),
30       Header(nullptr), DbgStreams((int)DbgHeaderType::Max) {}
31 
32 DbiStreamBuilder::~DbiStreamBuilder() {}
33 
34 void DbiStreamBuilder::setVersionHeader(PdbRaw_DbiVer V) { VerHeader = V; }
35 
36 void DbiStreamBuilder::setAge(uint32_t A) { Age = A; }
37 
38 void DbiStreamBuilder::setBuildNumber(uint16_t B) { BuildNumber = B; }
39 
40 void DbiStreamBuilder::setPdbDllVersion(uint16_t V) { PdbDllVersion = V; }
41 
42 void DbiStreamBuilder::setPdbDllRbld(uint16_t R) { PdbDllRbld = R; }
43 
44 void DbiStreamBuilder::setFlags(uint16_t F) { Flags = F; }
45 
46 void DbiStreamBuilder::setMachineType(PDB_Machine M) { MachineType = M; }
47 
48 void DbiStreamBuilder::setSectionMap(ArrayRef<SecMapEntry> SecMap) {
49   SectionMap = SecMap;
50 }
51 
52 Error DbiStreamBuilder::addDbgStream(pdb::DbgHeaderType Type,
53                                      ArrayRef<uint8_t> Data) {
54   if (DbgStreams[(int)Type].StreamNumber)
55     return make_error<RawError>(raw_error_code::duplicate_entry,
56                                 "The specified stream type already exists");
57   auto ExpectedIndex = Msf.addStream(Data.size());
58   if (!ExpectedIndex)
59     return ExpectedIndex.takeError();
60   uint32_t Index = std::move(*ExpectedIndex);
61   DbgStreams[(int)Type].Data = Data;
62   DbgStreams[(int)Type].StreamNumber = Index;
63   return Error::success();
64 }
65 
66 uint32_t DbiStreamBuilder::calculateSerializedLength() const {
67   // For now we only support serializing the header.
68   return sizeof(DbiStreamHeader) + calculateFileInfoSubstreamSize() +
69          calculateModiSubstreamSize() + calculateSectionContribsStreamSize() +
70          calculateSectionMapStreamSize() + calculateDbgStreamsSize();
71 }
72 
73 Expected<DbiModuleDescriptorBuilder &>
74 DbiStreamBuilder::addModuleInfo(StringRef ModuleName) {
75   uint32_t Index = ModiList.size();
76   auto MIB =
77       llvm::make_unique<DbiModuleDescriptorBuilder>(ModuleName, Index, Msf);
78   auto M = MIB.get();
79   auto Result = ModiMap.insert(std::make_pair(ModuleName, std::move(MIB)));
80 
81   if (!Result.second)
82     return make_error<RawError>(raw_error_code::duplicate_entry,
83                                 "The specified module already exists");
84   ModiList.push_back(M);
85   return *M;
86 }
87 
88 Error DbiStreamBuilder::addModuleSourceFile(StringRef Module, StringRef File) {
89   auto ModIter = ModiMap.find(Module);
90   if (ModIter == ModiMap.end())
91     return make_error<RawError>(raw_error_code::no_entry,
92                                 "The specified module was not found");
93   return addModuleSourceFile(*ModIter->second, File);
94 }
95 
96 Error DbiStreamBuilder::addModuleSourceFile(DbiModuleDescriptorBuilder &Module,
97                                             StringRef File) {
98   uint32_t Index = SourceFileNames.size();
99   SourceFileNames.insert(std::make_pair(File, Index));
100   Module.addSourceFile(File);
101   return Error::success();
102 }
103 
104 Expected<uint32_t> DbiStreamBuilder::getSourceFileNameIndex(StringRef File) {
105   auto NameIter = SourceFileNames.find(File);
106   if (NameIter == SourceFileNames.end())
107     return make_error<RawError>(raw_error_code::no_entry,
108                                 "The specified source file was not found");
109   return NameIter->getValue();
110 }
111 
112 uint32_t DbiStreamBuilder::calculateModiSubstreamSize() const {
113   uint32_t Size = 0;
114   for (const auto &M : ModiList)
115     Size += M->calculateSerializedLength();
116   return Size;
117 }
118 
119 uint32_t DbiStreamBuilder::calculateSectionContribsStreamSize() const {
120   if (SectionContribs.empty())
121     return 0;
122   return sizeof(enum PdbRaw_DbiSecContribVer) +
123          sizeof(SectionContribs[0]) * SectionContribs.size();
124 }
125 
126 uint32_t DbiStreamBuilder::calculateSectionMapStreamSize() const {
127   if (SectionMap.empty())
128     return 0;
129   return sizeof(SecMapHeader) + sizeof(SecMapEntry) * SectionMap.size();
130 }
131 
132 uint32_t DbiStreamBuilder::calculateNamesOffset() const {
133   uint32_t Offset = 0;
134   Offset += sizeof(ulittle16_t);                         // NumModules
135   Offset += sizeof(ulittle16_t);                         // NumSourceFiles
136   Offset += ModiList.size() * sizeof(ulittle16_t);       // ModIndices
137   Offset += ModiList.size() * sizeof(ulittle16_t);       // ModFileCounts
138   uint32_t NumFileInfos = 0;
139   for (const auto &M : ModiList)
140     NumFileInfos += M->source_files().size();
141   Offset += NumFileInfos * sizeof(ulittle32_t); // FileNameOffsets
142   return Offset;
143 }
144 
145 uint32_t DbiStreamBuilder::calculateFileInfoSubstreamSize() const {
146   uint32_t Size = calculateNamesOffset();
147   Size += calculateNamesBufferSize();
148   return alignTo(Size, sizeof(uint32_t));
149 }
150 
151 uint32_t DbiStreamBuilder::calculateNamesBufferSize() const {
152   uint32_t Size = 0;
153   for (const auto &F : SourceFileNames) {
154     Size += F.getKeyLength() + 1; // Names[I];
155   }
156   return Size;
157 }
158 
159 uint32_t DbiStreamBuilder::calculateDbgStreamsSize() const {
160   return DbgStreams.size() * sizeof(uint16_t);
161 }
162 
163 Error DbiStreamBuilder::generateFileInfoSubstream() {
164   uint32_t Size = calculateFileInfoSubstreamSize();
165   auto Data = Allocator.Allocate<uint8_t>(Size);
166   uint32_t NamesOffset = calculateNamesOffset();
167 
168   FileInfoBuffer = MutableBinaryByteStream(MutableArrayRef<uint8_t>(Data, Size),
169                                            llvm::support::little);
170 
171   WritableBinaryStreamRef MetadataBuffer =
172       WritableBinaryStreamRef(FileInfoBuffer).keep_front(NamesOffset);
173   BinaryStreamWriter MetadataWriter(MetadataBuffer);
174 
175   uint16_t ModiCount = std::min<uint32_t>(UINT16_MAX, ModiList.size());
176   uint16_t FileCount = std::min<uint32_t>(UINT16_MAX, SourceFileNames.size());
177   if (auto EC = MetadataWriter.writeInteger(ModiCount)) // NumModules
178     return EC;
179   if (auto EC = MetadataWriter.writeInteger(FileCount)) // NumSourceFiles
180     return EC;
181   for (uint16_t I = 0; I < ModiCount; ++I) {
182     if (auto EC = MetadataWriter.writeInteger(I)) // Mod Indices
183       return EC;
184   }
185   for (const auto &MI : ModiList) {
186     FileCount = static_cast<uint16_t>(MI->source_files().size());
187     if (auto EC = MetadataWriter.writeInteger(FileCount)) // Mod File Counts
188       return EC;
189   }
190 
191   // Before writing the FileNameOffsets array, write the NamesBuffer array.
192   // A side effect of this is that this will actually compute the various
193   // file name offsets, so we can then go back and write the FileNameOffsets
194   // array to the other substream.
195   NamesBuffer = WritableBinaryStreamRef(FileInfoBuffer).drop_front(NamesOffset);
196   BinaryStreamWriter NameBufferWriter(NamesBuffer);
197   for (auto &Name : SourceFileNames) {
198     Name.second = NameBufferWriter.getOffset();
199     if (auto EC = NameBufferWriter.writeCString(Name.getKey()))
200       return EC;
201   }
202 
203   for (const auto &MI : ModiList) {
204     for (StringRef Name : MI->source_files()) {
205       auto Result = SourceFileNames.find(Name);
206       if (Result == SourceFileNames.end())
207         return make_error<RawError>(raw_error_code::no_entry,
208                                     "The source file was not found.");
209       if (auto EC = MetadataWriter.writeInteger(Result->second))
210         return EC;
211     }
212   }
213 
214   if (auto EC = NameBufferWriter.padToAlignment(sizeof(uint32_t)))
215     return EC;
216 
217   if (NameBufferWriter.bytesRemaining() > 0)
218     return make_error<RawError>(raw_error_code::invalid_format,
219                                 "The names buffer contained unexpected data.");
220 
221   if (MetadataWriter.bytesRemaining() > sizeof(uint32_t))
222     return make_error<RawError>(
223         raw_error_code::invalid_format,
224         "The metadata buffer contained unexpected data.");
225 
226   return Error::success();
227 }
228 
229 Error DbiStreamBuilder::finalize() {
230   if (Header)
231     return Error::success();
232 
233   for (auto &MI : ModiList)
234     MI->finalize();
235 
236   if (auto EC = generateFileInfoSubstream())
237     return EC;
238 
239   DbiStreamHeader *H = Allocator.Allocate<DbiStreamHeader>();
240   ::memset(H, 0, sizeof(DbiStreamHeader));
241   H->VersionHeader = *VerHeader;
242   H->VersionSignature = -1;
243   H->Age = Age;
244   H->BuildNumber = BuildNumber;
245   H->Flags = Flags;
246   H->PdbDllRbld = PdbDllRbld;
247   H->PdbDllVersion = PdbDllVersion;
248   H->MachineType = static_cast<uint16_t>(MachineType);
249 
250   H->ECSubstreamSize = 0;
251   H->FileInfoSize = FileInfoBuffer.getLength();
252   H->ModiSubstreamSize = calculateModiSubstreamSize();
253   H->OptionalDbgHdrSize = DbgStreams.size() * sizeof(uint16_t);
254   H->SecContrSubstreamSize = calculateSectionContribsStreamSize();
255   H->SectionMapSize = calculateSectionMapStreamSize();
256   H->TypeServerSize = 0;
257   H->SymRecordStreamIndex = kInvalidStreamIndex;
258   H->PublicSymbolStreamIndex = kInvalidStreamIndex;
259   H->MFCTypeServerIndex = kInvalidStreamIndex;
260   H->GlobalSymbolStreamIndex = kInvalidStreamIndex;
261 
262   Header = H;
263   return Error::success();
264 }
265 
266 Error DbiStreamBuilder::finalizeMsfLayout() {
267   for (auto &MI : ModiList) {
268     if (auto EC = MI->finalizeMsfLayout())
269       return EC;
270   }
271 
272   uint32_t Length = calculateSerializedLength();
273   if (auto EC = Msf.setStreamSize(StreamDBI, Length))
274     return EC;
275   return Error::success();
276 }
277 
278 static uint16_t toSecMapFlags(uint32_t Flags) {
279   uint16_t Ret = 0;
280   if (Flags & COFF::IMAGE_SCN_MEM_READ)
281     Ret |= static_cast<uint16_t>(OMFSegDescFlags::Read);
282   if (Flags & COFF::IMAGE_SCN_MEM_WRITE)
283     Ret |= static_cast<uint16_t>(OMFSegDescFlags::Write);
284   if (Flags & COFF::IMAGE_SCN_MEM_EXECUTE)
285     Ret |= static_cast<uint16_t>(OMFSegDescFlags::Execute);
286   if (Flags & COFF::IMAGE_SCN_MEM_EXECUTE)
287     Ret |= static_cast<uint16_t>(OMFSegDescFlags::Execute);
288   if (!(Flags & COFF::IMAGE_SCN_MEM_16BIT))
289     Ret |= static_cast<uint16_t>(OMFSegDescFlags::AddressIs32Bit);
290 
291   // This seems always 1.
292   Ret |= static_cast<uint16_t>(OMFSegDescFlags::IsSelector);
293 
294   return Ret;
295 }
296 
297 void DbiStreamBuilder::addSectionContrib(DbiModuleDescriptorBuilder *ModuleDbi,
298                                          const object::coff_section *SecHdr) {
299   SectionContrib SC;
300   memset(&SC, 0, sizeof(SC));
301   SC.ISect = (uint16_t)~0U; // This represents nil.
302   SC.Off = SecHdr->PointerToRawData;
303   SC.Size = SecHdr->SizeOfRawData;
304   SC.Characteristics = SecHdr->Characteristics;
305   // Use the module index in the module dbi stream or nil (-1).
306   SC.Imod = ModuleDbi ? ModuleDbi->getModuleIndex() : (uint16_t)~0U;
307   SectionContribs.emplace_back(SC);
308 }
309 
310 // A utility function to create a Section Map for a given list of COFF sections.
311 //
312 // A Section Map seem to be a copy of a COFF section list in other format.
313 // I don't know why a PDB file contains both a COFF section header and
314 // a Section Map, but it seems it must be present in a PDB.
315 std::vector<SecMapEntry> DbiStreamBuilder::createSectionMap(
316     ArrayRef<llvm::object::coff_section> SecHdrs) {
317   std::vector<SecMapEntry> Ret;
318   int Idx = 0;
319 
320   auto Add = [&]() -> SecMapEntry & {
321     Ret.emplace_back();
322     auto &Entry = Ret.back();
323     memset(&Entry, 0, sizeof(Entry));
324 
325     Entry.Frame = Idx + 1;
326 
327     // We don't know the meaning of these fields yet.
328     Entry.SecName = UINT16_MAX;
329     Entry.ClassName = UINT16_MAX;
330 
331     return Entry;
332   };
333 
334   for (auto &Hdr : SecHdrs) {
335     auto &Entry = Add();
336     Entry.Flags = toSecMapFlags(Hdr.Characteristics);
337     Entry.SecByteLength = Hdr.VirtualSize;
338     ++Idx;
339   }
340 
341   // The last entry is for absolute symbols.
342   auto &Entry = Add();
343   Entry.Flags = static_cast<uint16_t>(OMFSegDescFlags::AddressIs32Bit) |
344                 static_cast<uint16_t>(OMFSegDescFlags::IsAbsoluteAddress);
345   Entry.SecByteLength = UINT32_MAX;
346 
347   return Ret;
348 }
349 
350 Error DbiStreamBuilder::commit(const msf::MSFLayout &Layout,
351                                WritableBinaryStreamRef MsfBuffer) {
352   if (auto EC = finalize())
353     return EC;
354 
355   auto DbiS = WritableMappedBlockStream::createIndexedStream(
356       Layout, MsfBuffer, StreamDBI, Allocator);
357 
358   BinaryStreamWriter Writer(*DbiS);
359   if (auto EC = Writer.writeObject(*Header))
360     return EC;
361 
362   for (auto &M : ModiList) {
363     if (auto EC = M->commit(Writer, Layout, MsfBuffer))
364       return EC;
365   }
366 
367   if (!SectionContribs.empty()) {
368     if (auto EC = Writer.writeEnum(DbiSecContribVer60))
369       return EC;
370     if (auto EC = Writer.writeArray(makeArrayRef(SectionContribs)))
371       return EC;
372   }
373 
374   if (!SectionMap.empty()) {
375     ulittle16_t Size = static_cast<ulittle16_t>(SectionMap.size());
376     SecMapHeader SMHeader = {Size, Size};
377     if (auto EC = Writer.writeObject(SMHeader))
378       return EC;
379     if (auto EC = Writer.writeArray(SectionMap))
380       return EC;
381   }
382 
383   if (auto EC = Writer.writeStreamRef(FileInfoBuffer))
384     return EC;
385 
386   for (auto &Stream : DbgStreams)
387     if (auto EC = Writer.writeInteger(Stream.StreamNumber))
388       return EC;
389 
390   for (auto &Stream : DbgStreams) {
391     if (Stream.StreamNumber == kInvalidStreamIndex)
392       continue;
393     auto WritableStream = WritableMappedBlockStream::createIndexedStream(
394         Layout, MsfBuffer, Stream.StreamNumber, Allocator);
395     BinaryStreamWriter DbgStreamWriter(*WritableStream);
396     if (auto EC = DbgStreamWriter.writeArray(Stream.Data))
397       return EC;
398   }
399 
400   if (Writer.bytesRemaining() > 0)
401     return make_error<RawError>(raw_error_code::invalid_format,
402                                 "Unexpected bytes found in DBI Stream");
403   return Error::success();
404 }
405