LLVM  5.0.0
PDBFileBuilder.cpp
Go to the documentation of this file.
1 //===- PDBFileBuilder.cpp - PDB File 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/BitVector.h"
13 
27 
28 using namespace llvm;
29 using namespace llvm::codeview;
30 using namespace llvm::msf;
31 using namespace llvm::pdb;
32 using namespace llvm::support;
33 
34 PDBFileBuilder::PDBFileBuilder(BumpPtrAllocator &Allocator)
35  : Allocator(Allocator) {}
36 
38 
40  auto ExpectedMsf = MSFBuilder::create(Allocator, BlockSize);
41  if (!ExpectedMsf)
42  return ExpectedMsf.takeError();
43  Msf = llvm::make_unique<MSFBuilder>(std::move(*ExpectedMsf));
44  return Error::success();
45 }
46 
48 
50  if (!Info)
51  Info = llvm::make_unique<InfoStreamBuilder>(*Msf, NamedStreams);
52  return *Info;
53 }
54 
56  if (!Dbi)
57  Dbi = llvm::make_unique<DbiStreamBuilder>(*Msf);
58  return *Dbi;
59 }
60 
62  if (!Tpi)
63  Tpi = llvm::make_unique<TpiStreamBuilder>(*Msf, StreamTPI);
64  return *Tpi;
65 }
66 
68  if (!Ipi)
69  Ipi = llvm::make_unique<TpiStreamBuilder>(*Msf, StreamIPI);
70  return *Ipi;
71 }
72 
74  return Strings;
75 }
76 
78  if (!Publics)
79  Publics = llvm::make_unique<PublicsStreamBuilder>(*Msf);
80  return *Publics;
81 }
82 
84  auto ExpectedStream = Msf->addStream(Size);
85  if (!ExpectedStream)
86  return ExpectedStream.takeError();
87  NamedStreams.set(Name, *ExpectedStream);
88  return Error::success();
89 }
90 
91 Expected<msf::MSFLayout> PDBFileBuilder::finalizeMsfLayout() {
92 
93  if (Ipi && Ipi->getRecordCount() > 0) {
94  // In theory newer PDBs always have an ID stream, but by saying that we're
95  // only going to *really* have an ID stream if there is at least one ID
96  // record, we leave open the opportunity to test older PDBs such as those
97  // that don't have an ID stream.
98  auto &Info = getInfoBuilder();
99  Info.addFeature(PdbRaw_FeatureSig::VC140);
100  }
101 
102  uint32_t StringsLen = Strings.calculateSerializedSize();
103 
104  if (auto EC = addNamedStream("/names", StringsLen))
105  return std::move(EC);
106  if (auto EC = addNamedStream("/LinkInfo", 0))
107  return std::move(EC);
108 
109  if (Info) {
110  if (auto EC = Info->finalizeMsfLayout())
111  return std::move(EC);
112  }
113  if (Dbi) {
114  if (auto EC = Dbi->finalizeMsfLayout())
115  return std::move(EC);
116  }
117  if (Tpi) {
118  if (auto EC = Tpi->finalizeMsfLayout())
119  return std::move(EC);
120  }
121  if (Ipi) {
122  if (auto EC = Ipi->finalizeMsfLayout())
123  return std::move(EC);
124  }
125  if (Publics) {
126  if (auto EC = Publics->finalizeMsfLayout())
127  return std::move(EC);
128  if (Dbi) {
129  Dbi->setPublicsStreamIndex(Publics->getStreamIndex());
130  Dbi->setSymbolRecordStreamIndex(Publics->getRecordStreamIdx());
131  }
132  }
133 
134  return Msf->build();
135 }
136 
138  uint32_t SN = 0;
139  if (!NamedStreams.get(Name, SN))
140  return llvm::make_error<pdb::RawError>(raw_error_code::no_stream);
141  return SN;
142 }
143 
145  assert(!Filename.empty());
146  auto ExpectedLayout = finalizeMsfLayout();
147  if (!ExpectedLayout)
148  return ExpectedLayout.takeError();
149  auto &Layout = *ExpectedLayout;
150 
151  uint64_t Filesize = Layout.SB->BlockSize * Layout.SB->NumBlocks;
152  auto OutFileOrError = FileOutputBuffer::create(Filename, Filesize);
153  if (OutFileOrError.getError())
154  return llvm::make_error<pdb::GenericError>(generic_error_code::invalid_path,
155  Filename);
156  FileBufferByteStream Buffer(std::move(*OutFileOrError),
158  BinaryStreamWriter Writer(Buffer);
159 
160  if (auto EC = Writer.writeObject(*Layout.SB))
161  return EC;
162  uint32_t BlockMapOffset =
163  msf::blockToOffset(Layout.SB->BlockMapAddr, Layout.SB->BlockSize);
164  Writer.setOffset(BlockMapOffset);
165  if (auto EC = Writer.writeArray(Layout.DirectoryBlocks))
166  return EC;
167 
168  auto DirStream = WritableMappedBlockStream::createDirectoryStream(
169  Layout, Buffer, Allocator);
170  BinaryStreamWriter DW(*DirStream);
171  if (auto EC = DW.writeInteger<uint32_t>(Layout.StreamSizes.size()))
172  return EC;
173 
174  if (auto EC = DW.writeArray(Layout.StreamSizes))
175  return EC;
176 
177  for (const auto &Blocks : Layout.StreamMap) {
178  if (auto EC = DW.writeArray(Blocks))
179  return EC;
180  }
181 
182  auto ExpectedSN = getNamedStreamIndex("/names");
183  if (!ExpectedSN)
184  return ExpectedSN.takeError();
185 
186  auto NS = WritableMappedBlockStream::createIndexedStream(
187  Layout, Buffer, *ExpectedSN, Allocator);
188  BinaryStreamWriter NSWriter(*NS);
189  if (auto EC = Strings.commit(NSWriter))
190  return EC;
191 
192  if (Info) {
193  if (auto EC = Info->commit(Layout, Buffer))
194  return EC;
195  }
196 
197  if (Dbi) {
198  if (auto EC = Dbi->commit(Layout, Buffer))
199  return EC;
200  }
201 
202  if (Tpi) {
203  if (auto EC = Tpi->commit(Layout, Buffer))
204  return EC;
205  }
206 
207  if (Ipi) {
208  if (auto EC = Ipi->commit(Layout, Buffer))
209  return EC;
210  }
211 
212  if (Publics) {
213  auto PS = WritableMappedBlockStream::createIndexedStream(
214  Layout, Buffer, Publics->getStreamIndex(), Allocator);
215  BinaryStreamWriter PSWriter(*PS);
216  if (auto EC = Publics->commit(PSWriter))
217  return EC;
218  }
219 
220  return Buffer.commit();
221 }
Error writeObject(const T &Obj)
Writes the object Obj to the underlying stream, as if by using memcpy.
uint64_t blockToOffset(uint64_t BlockNumber, uint64_t BlockSize)
Definition: MSFCommon.h:84
PDBStringTableBuilder & getStringTableBuilder()
Expected< uint32_t > getNamedStreamIndex(StringRef Name) const
void set(StringRef Stream, uint32_t StreamNo)
Error commit(BinaryStreamWriter &Writer) const
DbiStreamBuilder & getDbiBuilder()
Tagged union holding either a T or a Error.
Definition: CachePruning.h:23
TpiStreamBuilder & getIpiBuilder()
Error commit(StringRef Filename)
Error writeArray(ArrayRef< T > Array)
Writes an array of objects of type T to the underlying stream, as if by using memcpy.
const char * Name
Error commit() override
For buffered streams, commits changes to the backing store.
Error initialize(uint32_t BlockSize)
Allocate memory in an ever growing pool, as if by bump-pointer.
Definition: Allocator.h:138
Provides write only access to a subclass of WritableBinaryStream.
static ErrorOr< std::unique_ptr< FileOutputBuffer > > create(StringRef FilePath, size_t Size, unsigned Flags=0)
Factory method to create an OutputBuffer object which manages a read/write buffer of the specified si...
Error writeInteger(T Value)
Write the the integer Value to the underlying stream in the specified endianness. ...
PublicsStreamBuilder & getPublicsBuilder()
static const int BlockSize
Definition: TarWriter.cpp:34
Basic Register Allocator
static ErrorSuccess success()
Create a success value.
Definition: Error.h:311
InfoStreamBuilder & getInfoBuilder()
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:133
constexpr char Size[]
Key for Kernel::Arg::Metadata::mSize.
void setOffset(uint32_t Off)
msf::MSFBuilder & getMsfBuilder()
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
Error addNamedStream(StringRef Name, uint32_t Size)
Lightweight error class with error context and mandatory checking.
Definition: Error.h:156
bool get(StringRef Stream, uint32_t &StreamNo) const
An implementation of WritableBinaryStream backed by an llvm FileOutputBuffer.
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
TpiStreamBuilder & getTpiBuilder()