LLVM  6.0.0svn
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"
13 #include "llvm/BinaryFormat/COFF.h"
19 #include "llvm/Object/COFF.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 
33 
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 
49  SectionMap = SecMap;
50 }
51 
53  GlobalsStreamIndex = Index;
54 }
55 
57  SymRecordStreamIndex = Index;
58 }
59 
61  PublicsStreamIndex = Index;
62 }
63 
66  if (DbgStreams[(int)Type].StreamNumber != kInvalidStreamIndex)
67  return make_error<RawError>(raw_error_code::duplicate_entry,
68  "The specified stream type already exists");
69  auto ExpectedIndex = Msf.addStream(Data.size());
70  if (!ExpectedIndex)
71  return ExpectedIndex.takeError();
72  uint32_t Index = std::move(*ExpectedIndex);
73  DbgStreams[(int)Type].Data = Data;
74  DbgStreams[(int)Type].StreamNumber = Index;
75  return Error::success();
76 }
77 
79  return ECNamesBuilder.insert(Name);
80 }
81 
83  // For now we only support serializing the header.
84  return sizeof(DbiStreamHeader) + calculateFileInfoSubstreamSize() +
85  calculateModiSubstreamSize() + calculateSectionContribsStreamSize() +
86  calculateSectionMapStreamSize() + calculateDbgStreamsSize() +
87  ECNamesBuilder.calculateSerializedSize();
88 }
89 
92  uint32_t Index = ModiList.size();
93  ModiList.push_back(
94  llvm::make_unique<DbiModuleDescriptorBuilder>(ModuleName, Index, Msf));
95  return *ModiList.back();
96 }
97 
99  StringRef File) {
100  uint32_t Index = SourceFileNames.size();
101  SourceFileNames.insert(std::make_pair(File, Index));
102  Module.addSourceFile(File);
103  return Error::success();
104 }
105 
107  auto NameIter = SourceFileNames.find(File);
108  if (NameIter == SourceFileNames.end())
109  return make_error<RawError>(raw_error_code::no_entry,
110  "The specified source file was not found");
111  return NameIter->getValue();
112 }
113 
114 uint32_t DbiStreamBuilder::calculateModiSubstreamSize() const {
115  uint32_t Size = 0;
116  for (const auto &M : ModiList)
117  Size += M->calculateSerializedLength();
118  return Size;
119 }
120 
121 uint32_t DbiStreamBuilder::calculateSectionContribsStreamSize() const {
122  if (SectionContribs.empty())
123  return 0;
124  return sizeof(enum PdbRaw_DbiSecContribVer) +
125  sizeof(SectionContribs[0]) * SectionContribs.size();
126 }
127 
128 uint32_t DbiStreamBuilder::calculateSectionMapStreamSize() const {
129  if (SectionMap.empty())
130  return 0;
131  return sizeof(SecMapHeader) + sizeof(SecMapEntry) * SectionMap.size();
132 }
133 
134 uint32_t DbiStreamBuilder::calculateNamesOffset() const {
135  uint32_t Offset = 0;
136  Offset += sizeof(ulittle16_t); // NumModules
137  Offset += sizeof(ulittle16_t); // NumSourceFiles
138  Offset += ModiList.size() * sizeof(ulittle16_t); // ModIndices
139  Offset += ModiList.size() * sizeof(ulittle16_t); // ModFileCounts
140  uint32_t NumFileInfos = 0;
141  for (const auto &M : ModiList)
142  NumFileInfos += M->source_files().size();
143  Offset += NumFileInfos * sizeof(ulittle32_t); // FileNameOffsets
144  return Offset;
145 }
146 
147 uint32_t DbiStreamBuilder::calculateFileInfoSubstreamSize() const {
148  uint32_t Size = calculateNamesOffset();
149  Size += calculateNamesBufferSize();
150  return alignTo(Size, sizeof(uint32_t));
151 }
152 
153 uint32_t DbiStreamBuilder::calculateNamesBufferSize() const {
154  uint32_t Size = 0;
155  for (const auto &F : SourceFileNames) {
156  Size += F.getKeyLength() + 1; // Names[I];
157  }
158  return Size;
159 }
160 
161 uint32_t DbiStreamBuilder::calculateDbgStreamsSize() const {
162  return DbgStreams.size() * sizeof(uint16_t);
163 }
164 
165 Error DbiStreamBuilder::generateFileInfoSubstream() {
166  uint32_t Size = calculateFileInfoSubstreamSize();
167  auto Data = Allocator.Allocate<uint8_t>(Size);
168  uint32_t NamesOffset = calculateNamesOffset();
169 
170  FileInfoBuffer = MutableBinaryByteStream(MutableArrayRef<uint8_t>(Data, Size),
172 
173  WritableBinaryStreamRef MetadataBuffer =
174  WritableBinaryStreamRef(FileInfoBuffer).keep_front(NamesOffset);
175  BinaryStreamWriter MetadataWriter(MetadataBuffer);
176 
177  uint16_t ModiCount = std::min<uint32_t>(UINT16_MAX, ModiList.size());
178  uint16_t FileCount = std::min<uint32_t>(UINT16_MAX, SourceFileNames.size());
179  if (auto EC = MetadataWriter.writeInteger(ModiCount)) // NumModules
180  return EC;
181  if (auto EC = MetadataWriter.writeInteger(FileCount)) // NumSourceFiles
182  return EC;
183  for (uint16_t I = 0; I < ModiCount; ++I) {
184  if (auto EC = MetadataWriter.writeInteger(I)) // Mod Indices
185  return EC;
186  }
187  for (const auto &MI : ModiList) {
188  FileCount = static_cast<uint16_t>(MI->source_files().size());
189  if (auto EC = MetadataWriter.writeInteger(FileCount)) // Mod File Counts
190  return EC;
191  }
192 
193  // Before writing the FileNameOffsets array, write the NamesBuffer array.
194  // A side effect of this is that this will actually compute the various
195  // file name offsets, so we can then go back and write the FileNameOffsets
196  // array to the other substream.
197  NamesBuffer = WritableBinaryStreamRef(FileInfoBuffer).drop_front(NamesOffset);
198  BinaryStreamWriter NameBufferWriter(NamesBuffer);
199  for (auto &Name : SourceFileNames) {
200  Name.second = NameBufferWriter.getOffset();
201  if (auto EC = NameBufferWriter.writeCString(Name.getKey()))
202  return EC;
203  }
204 
205  for (const auto &MI : ModiList) {
206  for (StringRef Name : MI->source_files()) {
207  auto Result = SourceFileNames.find(Name);
208  if (Result == SourceFileNames.end())
209  return make_error<RawError>(raw_error_code::no_entry,
210  "The source file was not found.");
211  if (auto EC = MetadataWriter.writeInteger(Result->second))
212  return EC;
213  }
214  }
215 
216  if (auto EC = NameBufferWriter.padToAlignment(sizeof(uint32_t)))
217  return EC;
218 
219  if (NameBufferWriter.bytesRemaining() > 0)
220  return make_error<RawError>(raw_error_code::invalid_format,
221  "The names buffer contained unexpected data.");
222 
223  if (MetadataWriter.bytesRemaining() > sizeof(uint32_t))
224  return make_error<RawError>(
226  "The metadata buffer contained unexpected data.");
227 
228  return Error::success();
229 }
230 
231 Error DbiStreamBuilder::finalize() {
232  if (Header)
233  return Error::success();
234 
235  for (auto &MI : ModiList)
236  MI->finalize();
237 
238  if (auto EC = generateFileInfoSubstream())
239  return EC;
240 
241  DbiStreamHeader *H = Allocator.Allocate<DbiStreamHeader>();
242  ::memset(H, 0, sizeof(DbiStreamHeader));
243  H->VersionHeader = *VerHeader;
244  H->VersionSignature = -1;
245  H->Age = Age;
246  H->BuildNumber = BuildNumber;
247  H->Flags = Flags;
248  H->PdbDllRbld = PdbDllRbld;
249  H->PdbDllVersion = PdbDllVersion;
250  H->MachineType = static_cast<uint16_t>(MachineType);
251 
252  H->ECSubstreamSize = ECNamesBuilder.calculateSerializedSize();
253  H->FileInfoSize = FileInfoBuffer.getLength();
254  H->ModiSubstreamSize = calculateModiSubstreamSize();
255  H->OptionalDbgHdrSize = DbgStreams.size() * sizeof(uint16_t);
256  H->SecContrSubstreamSize = calculateSectionContribsStreamSize();
257  H->SectionMapSize = calculateSectionMapStreamSize();
258  H->TypeServerSize = 0;
259  H->SymRecordStreamIndex = SymRecordStreamIndex;
260  H->PublicSymbolStreamIndex = PublicsStreamIndex;
262  H->GlobalSymbolStreamIndex = GlobalsStreamIndex;
263 
264  Header = H;
265  return Error::success();
266 }
267 
269  for (auto &MI : ModiList) {
270  if (auto EC = MI->finalizeMsfLayout())
271  return EC;
272  }
273 
275  if (auto EC = Msf.setStreamSize(StreamDBI, Length))
276  return EC;
277  return Error::success();
278 }
279 
280 static uint16_t toSecMapFlags(uint32_t Flags) {
281  uint16_t Ret = 0;
282  if (Flags & COFF::IMAGE_SCN_MEM_READ)
283  Ret |= static_cast<uint16_t>(OMFSegDescFlags::Read);
284  if (Flags & COFF::IMAGE_SCN_MEM_WRITE)
285  Ret |= static_cast<uint16_t>(OMFSegDescFlags::Write);
286  if (Flags & COFF::IMAGE_SCN_MEM_EXECUTE)
287  Ret |= static_cast<uint16_t>(OMFSegDescFlags::Execute);
288  if (Flags & COFF::IMAGE_SCN_MEM_EXECUTE)
289  Ret |= static_cast<uint16_t>(OMFSegDescFlags::Execute);
290  if (!(Flags & COFF::IMAGE_SCN_MEM_16BIT))
291  Ret |= static_cast<uint16_t>(OMFSegDescFlags::AddressIs32Bit);
292 
293  // This seems always 1.
294  Ret |= static_cast<uint16_t>(OMFSegDescFlags::IsSelector);
295 
296  return Ret;
297 }
298 
299 // A utility function to create a Section Map for a given list of COFF sections.
300 //
301 // A Section Map seem to be a copy of a COFF section list in other format.
302 // I don't know why a PDB file contains both a COFF section header and
303 // a Section Map, but it seems it must be present in a PDB.
304 std::vector<SecMapEntry> DbiStreamBuilder::createSectionMap(
306  std::vector<SecMapEntry> Ret;
307  int Idx = 0;
308 
309  auto Add = [&]() -> SecMapEntry & {
310  Ret.emplace_back();
311  auto &Entry = Ret.back();
312  memset(&Entry, 0, sizeof(Entry));
313 
314  Entry.Frame = Idx + 1;
315 
316  // We don't know the meaning of these fields yet.
317  Entry.SecName = UINT16_MAX;
318  Entry.ClassName = UINT16_MAX;
319 
320  return Entry;
321  };
322 
323  for (auto &Hdr : SecHdrs) {
324  auto &Entry = Add();
325  Entry.Flags = toSecMapFlags(Hdr.Characteristics);
326  Entry.SecByteLength = Hdr.VirtualSize;
327  ++Idx;
328  }
329 
330  // The last entry is for absolute symbols.
331  auto &Entry = Add();
332  Entry.Flags = static_cast<uint16_t>(OMFSegDescFlags::AddressIs32Bit) |
333  static_cast<uint16_t>(OMFSegDescFlags::IsAbsoluteAddress);
334  Entry.SecByteLength = UINT32_MAX;
335 
336  return Ret;
337 }
338 
340  WritableBinaryStreamRef MsfBuffer) {
341  if (auto EC = finalize())
342  return EC;
343 
344  auto DbiS = WritableMappedBlockStream::createIndexedStream(
345  Layout, MsfBuffer, StreamDBI, Allocator);
346 
347  BinaryStreamWriter Writer(*DbiS);
348  if (auto EC = Writer.writeObject(*Header))
349  return EC;
350 
351  for (auto &M : ModiList) {
352  if (auto EC = M->commit(Writer, Layout, MsfBuffer))
353  return EC;
354  }
355 
356  if (!SectionContribs.empty()) {
357  if (auto EC = Writer.writeEnum(DbiSecContribVer60))
358  return EC;
359  if (auto EC = Writer.writeArray(makeArrayRef(SectionContribs)))
360  return EC;
361  }
362 
363  if (!SectionMap.empty()) {
364  ulittle16_t Size = static_cast<ulittle16_t>(SectionMap.size());
365  SecMapHeader SMHeader = {Size, Size};
366  if (auto EC = Writer.writeObject(SMHeader))
367  return EC;
368  if (auto EC = Writer.writeArray(SectionMap))
369  return EC;
370  }
371 
372  if (auto EC = Writer.writeStreamRef(FileInfoBuffer))
373  return EC;
374 
375  if (auto EC = ECNamesBuilder.commit(Writer))
376  return EC;
377 
378  for (auto &Stream : DbgStreams)
379  if (auto EC = Writer.writeInteger(Stream.StreamNumber))
380  return EC;
381 
382  for (auto &Stream : DbgStreams) {
383  if (Stream.StreamNumber == kInvalidStreamIndex)
384  continue;
385  auto WritableStream = WritableMappedBlockStream::createIndexedStream(
386  Layout, MsfBuffer, Stream.StreamNumber, Allocator);
387  BinaryStreamWriter DbgStreamWriter(*WritableStream);
388  if (auto EC = DbgStreamWriter.writeArray(Stream.Data))
389  return EC;
390  }
391 
392  if (Writer.bytesRemaining() > 0)
393  return make_error<RawError>(raw_error_code::invalid_format,
394  "Unexpected bytes found in DBI Stream");
395  return Error::success();
396 }
support::little32_t FileInfoSize
Size of file info substream.
Definition: RawTypes.h:153
Error writeObject(const T &Obj)
Writes the object Obj to the underlying stream, as if by using memcpy.
uint32_t addECName(StringRef Name)
An implementation of BinaryStream which holds its entire data set in a single contiguous buffer...
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:63
LLVM_ATTRIBUTE_ALWAYS_INLINE size_type size() const
Definition: SmallVector.h:136
support::little32_t VersionSignature
Definition: RawTypes.h:119
void setSectionMap(ArrayRef< SecMapEntry > SecMap)
void setMachineType(PDB_Machine M)
support::ulittle16_t GlobalSymbolStreamIndex
Global symbol stream #.
Definition: RawTypes.h:126
static std::vector< SecMapEntry > createSectionMap(ArrayRef< llvm::object::coff_section > SecHdrs)
support::ulittle32_t MFCTypeServerIndex
Index of MFC Type Server.
Definition: RawTypes.h:159
iterator find(StringRef Key)
Definition: StringMap.h:337
F(f)
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:677
detail::packed_endian_specific_integral< uint16_t, little, unaligned > ulittle16_t
Definition: Endian.h:269
void setGlobalsStreamIndex(uint32_t Index)
support::ulittle16_t BuildNumber
See DbiBuildNo structure.
Definition: RawTypes.h:129
Error commit(BinaryStreamWriter &Writer) const
unsigned size() const
Definition: StringMap.h:112
support::little32_t TypeServerSize
Size of type server map.
Definition: RawTypes.h:156
ArrayRef< T > makeArrayRef(const T &OneElt)
Construct an ArrayRef from a single element.
Definition: ArrayRef.h:451
support::little32_t SectionMapSize
Size of sec. map substream.
Definition: RawTypes.h:150
Tagged union holding either a T or a Error.
Definition: CachePruning.h:23
The fixed size header that appears at the beginning of the DBI Stream.
Definition: RawTypes.h:118
void setSymbolRecordStreamIndex(uint32_t Index)
support::little32_t ECSubstreamSize
Size of EC stream (what is EC?)
Definition: RawTypes.h:165
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 writeArray(ArrayRef< T > Array)
Writes an array of objects of type T to the underlying stream, as if by using memcpy.
const uint16_t kInvalidStreamIndex
Definition: RawConstants.h:20
support::ulittle16_t PdbDllRbld
rbld number of mspdbNNN.dll
Definition: RawTypes.h:141
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:46
detail::packed_endian_specific_integral< uint32_t, little, unaligned > ulittle32_t
Definition: Endian.h:271
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:149
Expected< uint32_t > getSourceFileNameIndex(StringRef FileName)
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:57
Error writeStreamRef(BinaryStreamRef Ref)
Efficiently reads all data from Ref, and writes it to this stream.
uint32_t calculateSerializedLength() const
RefType drop_front(uint32_t N) const
Return a new BinaryStreamRef with the first N elements removed.
Provides write only access to a subclass of WritableBinaryStream.
void setVersionHeader(PdbRaw_DbiVer V)
Error writeInteger(T Value)
Write the the integer Value to the underlying stream in the specified endianness. ...
Error commit(const msf::MSFLayout &Layout, WritableBinaryStreamRef MsfBuffer)
support::ulittle16_t PdbDllVersion
version of mspdbNNN.dll
Definition: RawTypes.h:135
Error setStreamSize(uint32_t Idx, uint32_t Size)
Update the size of an existing stream.
Definition: MSFBuilder.cpp:188
uint32_t bytesRemaining() const
Error writeCString(StringRef Str)
Write the the string Str to the underlying stream followed by a null terminator.
support::little32_t OptionalDbgHdrSize
Size of DbgHeader info.
Definition: RawTypes.h:162
support::ulittle16_t Flags
See DbiFlags enum.
Definition: RawTypes.h:168
Basic Register Allocator
static ErrorSuccess success()
Create a success value.
Definition: Error.h:313
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:151
PdbRaw_DbiSecContribVer
Definition: RawConstants.h:66
bool insert(MapEntryTy *KeyValue)
insert - Insert the specified key/value pair into the map.
Definition: StringMap.h:370
Expected< DbiModuleDescriptorBuilder & > addModuleInfo(StringRef ModuleName)
support::little32_t ModiSubstreamSize
Size of module info stream.
Definition: RawTypes.h:144
Error writeEnum(T Num)
Similar to writeInteger.
void setPublicsStreamIndex(uint32_t Index)
static uint16_t toSecMapFlags(uint32_t Flags)
support::little32_t SecContrSubstreamSize
Size of sec. contrib stream.
Definition: RawTypes.h:147
#define I(x, y, z)
Definition: MD5.cpp:58
support::ulittle32_t Age
How "old" is this DBI Stream. Should match the age of the PDB InfoStream.
Definition: RawTypes.h:123
Error padToAlignment(uint32_t Align)
support::ulittle16_t SymRecordStreamIndex
Symbol records stream #.
Definition: RawTypes.h:138
support::ulittle32_t VersionHeader
Definition: RawTypes.h:120
constexpr char Size[]
Key for Kernel::Arg::Metadata::mSize.
RefType keep_front(uint32_t N) const
Return a new BinaryStreamRef with only the first N elements remaining.
Lightweight error class with error context and mandatory checking.
Definition: Error.h:156
IRTranslator LLVM IR MI
support::ulittle16_t PublicSymbolStreamIndex
Public symbols stream #.
Definition: RawTypes.h:132
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
support::ulittle16_t MachineType
See PDB_MachineType enum.
Definition: RawTypes.h:171
Error addModuleSourceFile(DbiModuleDescriptorBuilder &Module, StringRef File)
uint32_t getLength() override
Return the number of bytes of data in this stream.
iterator end()
Definition: StringMap.h:322