LLVM  4.0.0
DbiStreamBuilder.cpp
Go to the documentation of this file.
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 
11 
12 #include "llvm/ADT/ArrayRef.h"
18 #include "llvm/Object/COFF.h"
19 #include "llvm/Support/COFF.h"
20 
21 using namespace llvm;
22 using namespace llvm::codeview;
23 using namespace llvm::msf;
24 using namespace llvm::pdb;
25 
26 namespace {
27 class ModiSubstreamBuilder {};
28 }
29 
30 DbiStreamBuilder::DbiStreamBuilder(msf::MSFBuilder &Msf)
31  : Msf(Msf), Allocator(Msf.getAllocator()), Age(1), BuildNumber(0),
32  PdbDllVersion(0), PdbDllRbld(0), Flags(0), MachineType(PDB_Machine::x86),
33  Header(nullptr), DbgStreams((int)DbgHeaderType::Max) {}
34 
36 
38 
39 void DbiStreamBuilder::setBuildNumber(uint16_t B) { BuildNumber = B; }
40 
41 void DbiStreamBuilder::setPdbDllVersion(uint16_t V) { PdbDllVersion = V; }
42 
43 void DbiStreamBuilder::setPdbDllRbld(uint16_t R) { PdbDllRbld = R; }
44 
45 void DbiStreamBuilder::setFlags(uint16_t F) { Flags = F; }
46 
47 void DbiStreamBuilder::setMachineType(PDB_Machine M) { MachineType = M; }
48 
50  SectionContribs = Arr;
51 }
52 
54  SectionMap = SecMap;
55 }
56 
58  ArrayRef<uint8_t> Data) {
59  if (DbgStreams[(int)Type].StreamNumber)
60  return make_error<RawError>(raw_error_code::duplicate_entry,
61  "The specified stream type already exists");
62  auto ExpectedIndex = Msf.addStream(Data.size());
63  if (!ExpectedIndex)
64  return ExpectedIndex.takeError();
65  uint32_t Index = std::move(*ExpectedIndex);
66  DbgStreams[(int)Type].Data = Data;
67  DbgStreams[(int)Type].StreamNumber = Index;
68  return Error::success();
69 }
70 
72  // For now we only support serializing the header.
73  return sizeof(DbiStreamHeader) + calculateFileInfoSubstreamSize() +
74  calculateModiSubstreamSize() + calculateSectionContribsStreamSize() +
75  calculateSectionMapStreamSize() + calculateDbgStreamsSize();
76 }
77 
79  auto Entry = llvm::make_unique<ModuleInfo>();
80  ModuleInfo *M = Entry.get();
81  Entry->Mod = Module;
82  Entry->Obj = ObjFile;
83  auto Result = ModuleInfos.insert(std::make_pair(Module, std::move(Entry)));
84  if (!Result.second)
85  return make_error<RawError>(raw_error_code::duplicate_entry,
86  "The specified module already exists");
87  ModuleInfoList.push_back(M);
88  return Error::success();
89 }
90 
92  auto ModIter = ModuleInfos.find(Module);
93  if (ModIter == ModuleInfos.end())
94  return make_error<RawError>(raw_error_code::no_entry,
95  "The specified module was not found");
96  uint32_t Index = SourceFileNames.size();
97  SourceFileNames.insert(std::make_pair(File, Index));
98  auto &ModEntry = *ModIter;
99  ModEntry.second->SourceFiles.push_back(File);
100  return Error::success();
101 }
102 
103 uint32_t DbiStreamBuilder::calculateModiSubstreamSize() const {
104  uint32_t Size = 0;
105  for (const auto &M : ModuleInfoList) {
106  Size += sizeof(ModuleInfoHeader);
107  Size += M->Mod.size() + 1;
108  Size += M->Obj.size() + 1;
109  }
110  return alignTo(Size, sizeof(uint32_t));
111 }
112 
113 uint32_t DbiStreamBuilder::calculateSectionContribsStreamSize() const {
114  if (SectionContribs.empty())
115  return 0;
116  return sizeof(enum PdbRaw_DbiSecContribVer) +
117  sizeof(SectionContribs[0]) * SectionContribs.size();
118 }
119 
120 uint32_t DbiStreamBuilder::calculateSectionMapStreamSize() const {
121  if (SectionMap.empty())
122  return 0;
123  return sizeof(SecMapHeader) + sizeof(SecMapEntry) * SectionMap.size();
124 }
125 
126 uint32_t DbiStreamBuilder::calculateFileInfoSubstreamSize() const {
127  uint32_t Size = 0;
128  Size += sizeof(ulittle16_t); // NumModules
129  Size += sizeof(ulittle16_t); // NumSourceFiles
130  Size += ModuleInfoList.size() * sizeof(ulittle16_t); // ModIndices
131  Size += ModuleInfoList.size() * sizeof(ulittle16_t); // ModFileCounts
132  uint32_t NumFileInfos = 0;
133  for (const auto &M : ModuleInfoList)
134  NumFileInfos += M->SourceFiles.size();
135  Size += NumFileInfos * sizeof(ulittle32_t); // FileNameOffsets
136  Size += calculateNamesBufferSize();
137  return alignTo(Size, sizeof(uint32_t));
138 }
139 
140 uint32_t DbiStreamBuilder::calculateNamesBufferSize() const {
141  uint32_t Size = 0;
142  for (const auto &F : SourceFileNames) {
143  Size += F.getKeyLength() + 1; // Names[I];
144  }
145  return Size;
146 }
147 
148 uint32_t DbiStreamBuilder::calculateDbgStreamsSize() const {
149  return DbgStreams.size() * sizeof(uint16_t);
150 }
151 
152 Error DbiStreamBuilder::generateModiSubstream() {
153  uint32_t Size = calculateModiSubstreamSize();
154  auto Data = Allocator.Allocate<uint8_t>(Size);
155 
156  ModInfoBuffer = MutableByteStream(MutableArrayRef<uint8_t>(Data, Size));
157 
158  StreamWriter ModiWriter(ModInfoBuffer);
159  for (const auto &M : ModuleInfoList) {
160  ModuleInfoHeader Layout = {};
162  Layout.NumFiles = M->SourceFiles.size();
163  if (auto EC = ModiWriter.writeObject(Layout))
164  return EC;
165  if (auto EC = ModiWriter.writeZeroString(M->Mod))
166  return EC;
167  if (auto EC = ModiWriter.writeZeroString(M->Obj))
168  return EC;
169  }
170  if (ModiWriter.bytesRemaining() > sizeof(uint32_t))
171  return make_error<RawError>(raw_error_code::invalid_format,
172  "Unexpected bytes in Modi Stream Data");
173  return Error::success();
174 }
175 
176 Error DbiStreamBuilder::generateFileInfoSubstream() {
177  uint32_t Size = calculateFileInfoSubstreamSize();
178  uint32_t NameSize = calculateNamesBufferSize();
179  auto Data = Allocator.Allocate<uint8_t>(Size);
180  uint32_t NamesOffset = Size - NameSize;
181 
182  FileInfoBuffer = MutableByteStream(MutableArrayRef<uint8_t>(Data, Size));
183 
184  WritableStreamRef MetadataBuffer =
185  WritableStreamRef(FileInfoBuffer).keep_front(NamesOffset);
186  StreamWriter MetadataWriter(MetadataBuffer);
187 
188  uint16_t ModiCount = std::min<uint32_t>(UINT16_MAX, ModuleInfos.size());
189  uint16_t FileCount = std::min<uint32_t>(UINT16_MAX, SourceFileNames.size());
190  if (auto EC = MetadataWriter.writeInteger(ModiCount)) // NumModules
191  return EC;
192  if (auto EC = MetadataWriter.writeInteger(FileCount)) // NumSourceFiles
193  return EC;
194  for (uint16_t I = 0; I < ModiCount; ++I) {
195  if (auto EC = MetadataWriter.writeInteger(I)) // Mod Indices
196  return EC;
197  }
198  for (const auto MI : ModuleInfoList) {
199  FileCount = static_cast<uint16_t>(MI->SourceFiles.size());
200  if (auto EC = MetadataWriter.writeInteger(FileCount)) // Mod File Counts
201  return EC;
202  }
203 
204  // Before writing the FileNameOffsets array, write the NamesBuffer array.
205  // A side effect of this is that this will actually compute the various
206  // file name offsets, so we can then go back and write the FileNameOffsets
207  // array to the other substream.
208  NamesBuffer = WritableStreamRef(FileInfoBuffer).drop_front(NamesOffset);
209  StreamWriter NameBufferWriter(NamesBuffer);
210  for (auto &Name : SourceFileNames) {
211  Name.second = NameBufferWriter.getOffset();
212  if (auto EC = NameBufferWriter.writeZeroString(Name.getKey()))
213  return EC;
214  }
215 
216  for (const auto MI : ModuleInfoList) {
217  for (StringRef Name : MI->SourceFiles) {
218  auto Result = SourceFileNames.find(Name);
219  if (Result == SourceFileNames.end())
220  return make_error<RawError>(raw_error_code::no_entry,
221  "The source file was not found.");
222  if (auto EC = MetadataWriter.writeInteger(Result->second))
223  return EC;
224  }
225  }
226 
227  if (NameBufferWriter.bytesRemaining() > 0)
228  return make_error<RawError>(raw_error_code::invalid_format,
229  "The names buffer contained unexpected data.");
230 
231  if (MetadataWriter.bytesRemaining() > sizeof(uint32_t))
232  return make_error<RawError>(
234  "The metadata buffer contained unexpected data.");
235 
236  return Error::success();
237 }
238 
239 Error DbiStreamBuilder::finalize() {
240  if (Header)
241  return Error::success();
242 
243  DbiStreamHeader *H = Allocator.Allocate<DbiStreamHeader>();
244 
245  if (auto EC = generateModiSubstream())
246  return EC;
247  if (auto EC = generateFileInfoSubstream())
248  return EC;
249 
250  H->VersionHeader = *VerHeader;
251  H->VersionSignature = -1;
252  H->Age = Age;
253  H->BuildNumber = BuildNumber;
254  H->Flags = Flags;
255  H->PdbDllRbld = PdbDllRbld;
256  H->PdbDllVersion = PdbDllVersion;
257  H->MachineType = static_cast<uint16_t>(MachineType);
258 
259  H->ECSubstreamSize = 0;
260  H->FileInfoSize = FileInfoBuffer.getLength();
261  H->ModiSubstreamSize = ModInfoBuffer.getLength();
262  H->OptionalDbgHdrSize = DbgStreams.size() * sizeof(uint16_t);
263  H->SecContrSubstreamSize = calculateSectionContribsStreamSize();
264  H->SectionMapSize = calculateSectionMapStreamSize();
265  H->TypeServerSize = 0;
270 
271  Header = H;
272  return Error::success();
273 }
274 
277  if (auto EC = Msf.setStreamSize(StreamDBI, Length))
278  return EC;
279  return Error::success();
280 }
281 
282 static uint16_t toSecMapFlags(uint32_t Flags) {
283  uint16_t Ret = 0;
284  if (Flags & COFF::IMAGE_SCN_MEM_READ)
285  Ret |= static_cast<uint16_t>(OMFSegDescFlags::Read);
286  if (Flags & COFF::IMAGE_SCN_MEM_WRITE)
287  Ret |= static_cast<uint16_t>(OMFSegDescFlags::Write);
288  if (Flags & COFF::IMAGE_SCN_MEM_EXECUTE)
289  Ret |= static_cast<uint16_t>(OMFSegDescFlags::Execute);
290  if (Flags & COFF::IMAGE_SCN_MEM_EXECUTE)
291  Ret |= static_cast<uint16_t>(OMFSegDescFlags::Execute);
292  if (!(Flags & COFF::IMAGE_SCN_MEM_16BIT))
293  Ret |= static_cast<uint16_t>(OMFSegDescFlags::AddressIs32Bit);
294 
295  // This seems always 1.
296  Ret |= static_cast<uint16_t>(OMFSegDescFlags::IsSelector);
297 
298  return Ret;
299 }
300 
301 // A utility function to create Section Contributions
302 // for a given input sections.
303 std::vector<SectionContrib> DbiStreamBuilder::createSectionContribs(
305  std::vector<SectionContrib> Ret;
306 
307  // Create a SectionContrib for each input section.
308  for (auto &Sec : SecHdrs) {
309  Ret.emplace_back();
310  auto &Entry = Ret.back();
311  memset(&Entry, 0, sizeof(Entry));
312 
313  Entry.Off = Sec.PointerToRawData;
314  Entry.Size = Sec.SizeOfRawData;
315  Entry.Characteristics = Sec.Characteristics;
316  }
317  return Ret;
318 }
319 
320 // A utility function to create a Section Map for a given list of COFF sections.
321 //
322 // A Section Map seem to be a copy of a COFF section list in other format.
323 // I don't know why a PDB file contains both a COFF section header and
324 // a Section Map, but it seems it must be present in a PDB.
325 std::vector<SecMapEntry> DbiStreamBuilder::createSectionMap(
327  std::vector<SecMapEntry> Ret;
328  int Idx = 0;
329 
330  auto Add = [&]() -> SecMapEntry & {
331  Ret.emplace_back();
332  auto &Entry = Ret.back();
333  memset(&Entry, 0, sizeof(Entry));
334 
335  Entry.Frame = Idx + 1;
336 
337  // We don't know the meaning of these fields yet.
338  Entry.SecName = UINT16_MAX;
339  Entry.ClassName = UINT16_MAX;
340 
341  return Entry;
342  };
343 
344  for (auto &Hdr : SecHdrs) {
345  auto &Entry = Add();
346  Entry.Flags = toSecMapFlags(Hdr.Characteristics);
347  Entry.SecByteLength = Hdr.VirtualSize;
348  ++Idx;
349  }
350 
351  // The last entry is for absolute symbols.
352  auto &Entry = Add();
353  Entry.Flags = static_cast<uint16_t>(OMFSegDescFlags::AddressIs32Bit) |
354  static_cast<uint16_t>(OMFSegDescFlags::IsAbsoluteAddress);
355  Entry.SecByteLength = UINT32_MAX;
356 
357  return Ret;
358 }
359 
361  const msf::WritableStream &Buffer) {
362  if (auto EC = finalize())
363  return EC;
364 
365  auto InfoS =
366  WritableMappedBlockStream::createIndexedStream(Layout, Buffer, StreamDBI);
367 
368  StreamWriter Writer(*InfoS);
369  if (auto EC = Writer.writeObject(*Header))
370  return EC;
371 
372  if (auto EC = Writer.writeStreamRef(ModInfoBuffer))
373  return EC;
374 
375  if (!SectionContribs.empty()) {
376  if (auto EC = Writer.writeEnum(DbiSecContribVer60))
377  return EC;
378  if (auto EC = Writer.writeArray(SectionContribs))
379  return EC;
380  }
381 
382  if (!SectionMap.empty()) {
383  ulittle16_t Size = static_cast<ulittle16_t>(SectionMap.size());
384  SecMapHeader SMHeader = {Size, Size};
385  if (auto EC = Writer.writeObject(SMHeader))
386  return EC;
387  if (auto EC = Writer.writeArray(SectionMap))
388  return EC;
389  }
390 
391  if (auto EC = Writer.writeStreamRef(FileInfoBuffer))
392  return EC;
393 
394  for (auto &Stream : DbgStreams)
395  if (auto EC = Writer.writeInteger(Stream.StreamNumber))
396  return EC;
397 
398  for (auto &Stream : DbgStreams) {
399  if (Stream.StreamNumber == kInvalidStreamIndex)
400  continue;
401  auto WritableStream = WritableMappedBlockStream::createIndexedStream(
402  Layout, Buffer, Stream.StreamNumber);
403  StreamWriter DbgStreamWriter(*WritableStream);
404  if (auto EC = DbgStreamWriter.writeArray(Stream.Data))
405  return EC;
406  }
407 
408  if (Writer.bytesRemaining() > 0)
409  return make_error<RawError>(raw_error_code::invalid_format,
410  "Unexpected bytes found in DBI Stream");
411  return Error::success();
412 }
support::little32_t FileInfoSize
Size of file info substream.
Definition: RawTypes.h:145
uint32_t calculateSerializedLength() const
support::ulittle16_t NumFiles
Number of files contributing to this module.
Definition: RawTypes.h:241
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:52
support::little32_t VersionSignature
Definition: RawTypes.h:111
void setSectionMap(ArrayRef< SecMapEntry > SecMap)
void setMachineType(PDB_Machine M)
support::ulittle16_t GlobalSymbolStreamIndex
Global symbol stream #.
Definition: RawTypes.h:118
static std::vector< SecMapEntry > createSectionMap(ArrayRef< llvm::object::coff_section > SecHdrs)
support::ulittle32_t MFCTypeServerIndex
Index of MFC Type Server.
Definition: RawTypes.h:151
iterator find(StringRef Key)
Definition: StringMap.h:315
uint64_t alignTo(uint64_t Value, uint64_t Align, uint64_t Skew=0)
Returns the next integer (mod 2**64) that is greater than or equal to Value and is a multiple of Alig...
Definition: MathExtras.h:664
support::ulittle16_t BuildNumber
See DbiBuildNo structure.
Definition: RawTypes.h:121
struct fuzzer::@269 Flags
support::little32_t TypeServerSize
Size of type server map.
Definition: RawTypes.h:148
support::little32_t SectionMapSize
Size of sec. map substream.
Definition: RawTypes.h:142
Error writeStreamRef(ReadableStreamRef Ref)
RefType keep_front(uint32_t N) const
Definition: StreamRef.h:40
The fixed size header that appears at the beginning of the DBI Stream.
Definition: RawTypes.h:110
Error writeObject(const T &Obj)
Definition: StreamWriter.h:49
support::little32_t ECSubstreamSize
Size of EC stream (what is EC?)
Definition: RawTypes.h:157
#define F(x, y, z)
Definition: MD5.cpp:51
Error addDbgStream(pdb::DbgHeaderType Type, ArrayRef< uint8_t > Data)
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: APInt.h:33
Error commit(const msf::MSFLayout &Layout, const msf::WritableStream &Buffer)
static GCRegistry::Add< OcamlGC > B("ocaml","ocaml 3.10-compatible GC")
uint32_t getLength() const override
Definition: ByteStream.h:94
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:141
const uint16_t kInvalidStreamIndex
Definition: RawConstants.h:20
support::ulittle16_t PdbDllRbld
rbld number of mspdbNNN.dll
Definition: RawTypes.h:133
RefType drop_front(uint32_t N) const
Definition: StreamRef.h:32
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:45
The header preceeding each entry in the Module Info substream of the DBI stream.
Definition: RawTypes.h:215
LLVM_ATTRIBUTE_RETURNS_NONNULL LLVM_ATTRIBUTE_RETURNS_NOALIAS void * Allocate(size_t Size, size_t Alignment)
Allocate space at the specified alignment.
Definition: Allocator.h:212
#define H(x, y, z)
Definition: MD5.cpp:53
unsigned size() const
Definition: StringMap.h:114
Greedy Register Allocator
void setVersionHeader(PdbRaw_DbiVer V)
Error writeInteger(uint8_t Int)
support::ulittle16_t PdbDllVersion
version of mspdbNNN.dll
Definition: RawTypes.h:127
Error setStreamSize(uint32_t Idx, uint32_t Size)
Update the size of an existing stream.
Definition: MSFBuilder.cpp:164
Error writeArray(ArrayRef< T > Array)
Definition: StreamWriter.h:58
support::little32_t OptionalDbgHdrSize
Size of DbgHeader info.
Definition: RawTypes.h:154
static std::vector< SectionContrib > createSectionContribs(ArrayRef< llvm::object::coff_section > SecHdrs)
support::ulittle16_t Flags
See DbiFlags enum.
Definition: RawTypes.h:160
static ErrorSuccess success()
Create a success value.
Expected< uint32_t > addStream(uint32_t Size, ArrayRef< uint32_t > Blocks)
Add a stream to the MSF file with the given size, occupying the given list of blocks.
Definition: MSFBuilder.cpp:127
PdbRaw_DbiSecContribVer
Definition: RawConstants.h:51
bool insert(MapEntryTy *KeyValue)
insert - Insert the specified key/value pair into the map.
Definition: StringMap.h:348
support::little32_t ModiSubstreamSize
Size of module info stream.
Definition: RawTypes.h:136
detail::packed_endian_specific_integral< uint16_t, little, unaligned > ulittle16_t
Definition: Endian.h:233
Error addModuleSourceFile(StringRef Module, StringRef File)
void setSectionContribs(ArrayRef< SectionContrib > SecMap)
support::ulittle16_t ModDiStream
Stream Number of module debug info.
Definition: RawTypes.h:229
static uint16_t toSecMapFlags(uint32_t Flags)
Error addModuleInfo(StringRef ObjFile, StringRef Module)
support::little32_t SecContrSubstreamSize
Size of sec. contrib stream.
Definition: RawTypes.h:139
#define I(x, y, z)
Definition: MD5.cpp:54
LLVM_ATTRIBUTE_ALWAYS_INLINE size_type size() const
Definition: SmallVector.h:135
support::ulittle32_t Age
How "old" is this DBI Stream. Should match the age of the PDB InfoStream.
Definition: RawTypes.h:115
support::ulittle16_t SymRecordStreamIndex
Symbol records stream #.
Definition: RawTypes.h:130
support::ulittle32_t VersionHeader
Definition: RawTypes.h:112
Error writeEnum(T Num)
Definition: StreamWriter.h:44
detail::packed_endian_specific_integral< uint32_t, little, unaligned > ulittle32_t
Definition: Endian.h:235
Lightweight error class with error context and mandatory checking.
IRTranslator LLVM IR MI
support::ulittle16_t PublicSymbolStreamIndex
Public symbols stream #.
Definition: RawTypes.h:124
uint32_t bytesRemaining() const
Definition: StreamWriter.h:82
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:47
support::ulittle16_t MachineType
See PDB_MachineType enum.
Definition: RawTypes.h:163
static GCRegistry::Add< ErlangGC > A("erlang","erlang-compatible garbage collector")
iterator end()
Definition: StringMap.h:305