Zachary Turner | dbeaea7 | 2016-07-11 21:45:26 +0000 | [diff] [blame] | 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 | |
Adrian McCarthy | 6b6b8c4 | 2017-01-25 22:38:55 +0000 | [diff] [blame] | 10 | #include "llvm/DebugInfo/PDB/Native/PDBFileBuilder.h" |
Zachary Turner | dbeaea7 | 2016-07-11 21:45:26 +0000 | [diff] [blame] | 11 | |
Zachary Turner | faa554b | 2016-07-15 22:16:56 +0000 | [diff] [blame] | 12 | #include "llvm/ADT/BitVector.h" |
| 13 | |
Zachary Turner | a3225b0 | 2016-07-29 20:56:36 +0000 | [diff] [blame] | 14 | #include "llvm/DebugInfo/MSF/MSFBuilder.h" |
Rui Ueyama | fc22cef | 2016-09-30 20:34:44 +0000 | [diff] [blame] | 15 | #include "llvm/DebugInfo/PDB/GenericError.h" |
Adrian McCarthy | 6b6b8c4 | 2017-01-25 22:38:55 +0000 | [diff] [blame] | 16 | #include "llvm/DebugInfo/PDB/Native/DbiStream.h" |
| 17 | #include "llvm/DebugInfo/PDB/Native/DbiStreamBuilder.h" |
Zachary Turner | 946204c | 2017-08-09 04:23:25 +0000 | [diff] [blame] | 18 | #include "llvm/DebugInfo/PDB/Native/GSIStreamBuilder.h" |
Adrian McCarthy | 6b6b8c4 | 2017-01-25 22:38:55 +0000 | [diff] [blame] | 19 | #include "llvm/DebugInfo/PDB/Native/InfoStream.h" |
| 20 | #include "llvm/DebugInfo/PDB/Native/InfoStreamBuilder.h" |
Zachary Turner | e204a6c | 2017-05-02 18:00:13 +0000 | [diff] [blame] | 21 | #include "llvm/DebugInfo/PDB/Native/PDBStringTableBuilder.h" |
Adrian McCarthy | 6b6b8c4 | 2017-01-25 22:38:55 +0000 | [diff] [blame] | 22 | #include "llvm/DebugInfo/PDB/Native/RawError.h" |
Adrian McCarthy | 6b6b8c4 | 2017-01-25 22:38:55 +0000 | [diff] [blame] | 23 | #include "llvm/DebugInfo/PDB/Native/TpiStream.h" |
| 24 | #include "llvm/DebugInfo/PDB/Native/TpiStreamBuilder.h" |
Zachary Turner | d9dc282 | 2017-03-02 20:52:51 +0000 | [diff] [blame] | 25 | #include "llvm/Support/BinaryStream.h" |
| 26 | #include "llvm/Support/BinaryStreamWriter.h" |
Zachary Turner | f228276 | 2018-03-23 19:57:25 +0000 | [diff] [blame^] | 27 | #include "llvm/Support/JamCRC.h" |
| 28 | #include "llvm/Support/Path.h" |
Zachary Turner | dbeaea7 | 2016-07-11 21:45:26 +0000 | [diff] [blame] | 29 | |
| 30 | using namespace llvm; |
| 31 | using namespace llvm::codeview; |
Zachary Turner | bac69d3 | 2016-07-22 19:56:05 +0000 | [diff] [blame] | 32 | using namespace llvm::msf; |
Zachary Turner | dbeaea7 | 2016-07-11 21:45:26 +0000 | [diff] [blame] | 33 | using namespace llvm::pdb; |
Zachary Turner | faa554b | 2016-07-15 22:16:56 +0000 | [diff] [blame] | 34 | using namespace llvm::support; |
Zachary Turner | dbeaea7 | 2016-07-11 21:45:26 +0000 | [diff] [blame] | 35 | |
Zachary Turner | e109dc6 | 2016-07-22 19:56:26 +0000 | [diff] [blame] | 36 | PDBFileBuilder::PDBFileBuilder(BumpPtrAllocator &Allocator) |
Zachary Turner | f228276 | 2018-03-23 19:57:25 +0000 | [diff] [blame^] | 37 | : Allocator(Allocator), InjectedSourceHashTraits(Strings), |
| 38 | InjectedSourceTable(2, InjectedSourceHashTraits) {} |
Zachary Turner | dbeaea7 | 2016-07-11 21:45:26 +0000 | [diff] [blame] | 39 | |
Zachary Turner | 7eaf1d9 | 2017-07-10 22:40:20 +0000 | [diff] [blame] | 40 | PDBFileBuilder::~PDBFileBuilder() {} |
| 41 | |
Rui Ueyama | 5d6714e | 2016-09-30 20:52:12 +0000 | [diff] [blame] | 42 | Error PDBFileBuilder::initialize(uint32_t BlockSize) { |
| 43 | auto ExpectedMsf = MSFBuilder::create(Allocator, BlockSize); |
Zachary Turner | faa554b | 2016-07-15 22:16:56 +0000 | [diff] [blame] | 44 | if (!ExpectedMsf) |
| 45 | return ExpectedMsf.takeError(); |
Rui Ueyama | 5d6714e | 2016-09-30 20:52:12 +0000 | [diff] [blame] | 46 | Msf = llvm::make_unique<MSFBuilder>(std::move(*ExpectedMsf)); |
Zachary Turner | dbeaea7 | 2016-07-11 21:45:26 +0000 | [diff] [blame] | 47 | return Error::success(); |
| 48 | } |
| 49 | |
Zachary Turner | a3225b0 | 2016-07-29 20:56:36 +0000 | [diff] [blame] | 50 | MSFBuilder &PDBFileBuilder::getMsfBuilder() { return *Msf; } |
Zachary Turner | faa554b | 2016-07-15 22:16:56 +0000 | [diff] [blame] | 51 | |
Zachary Turner | dbeaea7 | 2016-07-11 21:45:26 +0000 | [diff] [blame] | 52 | InfoStreamBuilder &PDBFileBuilder::getInfoBuilder() { |
| 53 | if (!Info) |
Zachary Turner | 760ad4d | 2017-01-20 22:42:09 +0000 | [diff] [blame] | 54 | Info = llvm::make_unique<InfoStreamBuilder>(*Msf, NamedStreams); |
Zachary Turner | dbeaea7 | 2016-07-11 21:45:26 +0000 | [diff] [blame] | 55 | return *Info; |
| 56 | } |
| 57 | |
| 58 | DbiStreamBuilder &PDBFileBuilder::getDbiBuilder() { |
| 59 | if (!Dbi) |
Zachary Turner | 620961d | 2016-09-14 23:00:02 +0000 | [diff] [blame] | 60 | Dbi = llvm::make_unique<DbiStreamBuilder>(*Msf); |
Zachary Turner | dbeaea7 | 2016-07-11 21:45:26 +0000 | [diff] [blame] | 61 | return *Dbi; |
| 62 | } |
| 63 | |
Zachary Turner | c6d54da | 2016-09-09 17:46:17 +0000 | [diff] [blame] | 64 | TpiStreamBuilder &PDBFileBuilder::getTpiBuilder() { |
| 65 | if (!Tpi) |
Zachary Turner | de9ba15 | 2016-09-15 18:22:31 +0000 | [diff] [blame] | 66 | Tpi = llvm::make_unique<TpiStreamBuilder>(*Msf, StreamTPI); |
Zachary Turner | c6d54da | 2016-09-09 17:46:17 +0000 | [diff] [blame] | 67 | return *Tpi; |
| 68 | } |
| 69 | |
Zachary Turner | de9ba15 | 2016-09-15 18:22:31 +0000 | [diff] [blame] | 70 | TpiStreamBuilder &PDBFileBuilder::getIpiBuilder() { |
| 71 | if (!Ipi) |
| 72 | Ipi = llvm::make_unique<TpiStreamBuilder>(*Msf, StreamIPI); |
| 73 | return *Ipi; |
| 74 | } |
| 75 | |
Zachary Turner | e204a6c | 2017-05-02 18:00:13 +0000 | [diff] [blame] | 76 | PDBStringTableBuilder &PDBFileBuilder::getStringTableBuilder() { |
| 77 | return Strings; |
| 78 | } |
Zachary Turner | 760ad4d | 2017-01-20 22:42:09 +0000 | [diff] [blame] | 79 | |
Zachary Turner | 946204c | 2017-08-09 04:23:25 +0000 | [diff] [blame] | 80 | GSIStreamBuilder &PDBFileBuilder::getGsiBuilder() { |
| 81 | if (!Gsi) |
| 82 | Gsi = llvm::make_unique<GSIStreamBuilder>(*Msf); |
| 83 | return *Gsi; |
Zachary Turner | 8d927b6 | 2017-07-31 19:36:08 +0000 | [diff] [blame] | 84 | } |
| 85 | |
Zachary Turner | a6fb536 | 2018-03-23 18:43:39 +0000 | [diff] [blame] | 86 | Expected<uint32_t> PDBFileBuilder::allocateNamedStream(StringRef Name, |
| 87 | uint32_t Size) { |
Zachary Turner | 760ad4d | 2017-01-20 22:42:09 +0000 | [diff] [blame] | 88 | auto ExpectedStream = Msf->addStream(Size); |
Zachary Turner | a6fb536 | 2018-03-23 18:43:39 +0000 | [diff] [blame] | 89 | if (ExpectedStream) |
| 90 | NamedStreams.set(Name, *ExpectedStream); |
| 91 | return ExpectedStream; |
| 92 | } |
| 93 | |
| 94 | Error PDBFileBuilder::addNamedStream(StringRef Name, StringRef Data) { |
| 95 | Expected<uint32_t> ExpectedIndex = allocateNamedStream(Name, Data.size()); |
| 96 | if (!ExpectedIndex) |
| 97 | return ExpectedIndex.takeError(); |
| 98 | assert(NamedStreamData.count(*ExpectedIndex) == 0); |
| 99 | NamedStreamData[*ExpectedIndex] = Data; |
Zachary Turner | 760ad4d | 2017-01-20 22:42:09 +0000 | [diff] [blame] | 100 | return Error::success(); |
| 101 | } |
| 102 | |
Zachary Turner | f228276 | 2018-03-23 19:57:25 +0000 | [diff] [blame^] | 103 | void PDBFileBuilder::addInjectedSource(StringRef Name, |
| 104 | std::unique_ptr<MemoryBuffer> Buffer) { |
| 105 | // Stream names must be exact matches, since they get looked up in a hash |
| 106 | // table and the hash value is dependent on the exact contents of the string. |
| 107 | // link.exe lowercases a path and converts / to \, so we must do the same. |
| 108 | SmallString<64> VName; |
| 109 | sys::path::native(Name.lower(), VName); |
| 110 | |
| 111 | uint32_t NI = getStringTableBuilder().insert(Name); |
| 112 | uint32_t VNI = getStringTableBuilder().insert(VName); |
| 113 | |
| 114 | InjectedSourceDescriptor Desc; |
| 115 | Desc.Content = std::move(Buffer); |
| 116 | Desc.NameIndex = NI; |
| 117 | Desc.VNameIndex = VNI; |
| 118 | Desc.StreamName = "/src/files/"; |
| 119 | |
| 120 | Desc.StreamName += VName; |
| 121 | |
| 122 | InjectedSources.push_back(std::move(Desc)); |
| 123 | } |
| 124 | |
Zachary Turner | 760ad4d | 2017-01-20 22:42:09 +0000 | [diff] [blame] | 125 | Expected<msf::MSFLayout> PDBFileBuilder::finalizeMsfLayout() { |
Zachary Turner | 68ea80d | 2017-06-12 21:46:51 +0000 | [diff] [blame] | 126 | |
| 127 | if (Ipi && Ipi->getRecordCount() > 0) { |
| 128 | // In theory newer PDBs always have an ID stream, but by saying that we're |
| 129 | // only going to *really* have an ID stream if there is at least one ID |
| 130 | // record, we leave open the opportunity to test older PDBs such as those |
| 131 | // that don't have an ID stream. |
| 132 | auto &Info = getInfoBuilder(); |
| 133 | Info.addFeature(PdbRaw_FeatureSig::VC140); |
| 134 | } |
| 135 | |
Zachary Turner | c504ae3 | 2017-05-03 15:58:37 +0000 | [diff] [blame] | 136 | uint32_t StringsLen = Strings.calculateSerializedSize(); |
Zachary Turner | 760ad4d | 2017-01-20 22:42:09 +0000 | [diff] [blame] | 137 | |
Zachary Turner | a6fb536 | 2018-03-23 18:43:39 +0000 | [diff] [blame] | 138 | Expected<uint32_t> SN = allocateNamedStream("/LinkInfo", 0); |
| 139 | if (!SN) |
| 140 | return SN.takeError(); |
Zachary Turner | 760ad4d | 2017-01-20 22:42:09 +0000 | [diff] [blame] | 141 | |
Zachary Turner | 946204c | 2017-08-09 04:23:25 +0000 | [diff] [blame] | 142 | if (Gsi) { |
| 143 | if (auto EC = Gsi->finalizeMsfLayout()) |
Zachary Turner | 7eaf1d9 | 2017-07-10 22:40:20 +0000 | [diff] [blame] | 144 | return std::move(EC); |
| 145 | if (Dbi) { |
Zachary Turner | 946204c | 2017-08-09 04:23:25 +0000 | [diff] [blame] | 146 | Dbi->setPublicsStreamIndex(Gsi->getPublicsStreamIndex()); |
| 147 | Dbi->setGlobalsStreamIndex(Gsi->getGlobalsStreamIndex()); |
| 148 | Dbi->setSymbolRecordStreamIndex(Gsi->getRecordStreamIdx()); |
Zachary Turner | 7eaf1d9 | 2017-07-10 22:40:20 +0000 | [diff] [blame] | 149 | } |
| 150 | } |
Zachary Turner | a6fb536 | 2018-03-23 18:43:39 +0000 | [diff] [blame] | 151 | if (Tpi) { |
| 152 | if (auto EC = Tpi->finalizeMsfLayout()) |
| 153 | return std::move(EC); |
| 154 | } |
| 155 | if (Dbi) { |
| 156 | if (auto EC = Dbi->finalizeMsfLayout()) |
| 157 | return std::move(EC); |
| 158 | } |
| 159 | SN = allocateNamedStream("/names", StringsLen); |
| 160 | if (!SN) |
| 161 | return SN.takeError(); |
| 162 | |
| 163 | if (Ipi) { |
| 164 | if (auto EC = Ipi->finalizeMsfLayout()) |
| 165 | return std::move(EC); |
| 166 | } |
| 167 | |
| 168 | // Do this last, since it relies on the named stream map being complete, and |
| 169 | // that can be updated by previous steps in the finalization. |
| 170 | if (Info) { |
| 171 | if (auto EC = Info->finalizeMsfLayout()) |
| 172 | return std::move(EC); |
| 173 | } |
Zachary Turner | faa554b | 2016-07-15 22:16:56 +0000 | [diff] [blame] | 174 | |
Zachary Turner | f228276 | 2018-03-23 19:57:25 +0000 | [diff] [blame^] | 175 | if (!InjectedSources.empty()) { |
| 176 | for (const auto &IS : InjectedSources) { |
| 177 | JamCRC CRC(0); |
| 178 | CRC.update(makeArrayRef(IS.Content->getBufferStart(), |
| 179 | IS.Content->getBufferSize())); |
| 180 | |
| 181 | SrcHeaderBlockEntry Entry; |
| 182 | ::memset(&Entry, 0, sizeof(SrcHeaderBlockEntry)); |
| 183 | Entry.Size = sizeof(SrcHeaderBlockEntry); |
| 184 | Entry.FileSize = IS.Content->getBufferSize(); |
| 185 | Entry.FileNI = IS.NameIndex; |
| 186 | Entry.VFileNI = IS.VNameIndex; |
| 187 | Entry.ObjNI = 1; |
| 188 | Entry.IsVirtual = 0; |
| 189 | Entry.Version = |
| 190 | static_cast<uint32_t>(PdbRaw_SrcHeaderBlockVer::SrcVerOne); |
| 191 | Entry.CRC = CRC.getCRC(); |
| 192 | StringRef VName = getStringTableBuilder().getStringForId(IS.VNameIndex); |
| 193 | InjectedSourceTable.set_as(VName, std::move(Entry)); |
| 194 | } |
| 195 | |
| 196 | uint32_t SrcHeaderBlockSize = |
| 197 | sizeof(SrcHeaderBlockHeader) + |
| 198 | InjectedSourceTable.calculateSerializedLength(); |
| 199 | SN = allocateNamedStream("/src/headerblock", SrcHeaderBlockSize); |
| 200 | if (!SN) |
| 201 | return SN.takeError(); |
| 202 | for (const auto &IS : InjectedSources) { |
| 203 | SN = allocateNamedStream(IS.StreamName, IS.Content->getBufferSize()); |
| 204 | if (!SN) |
| 205 | return SN.takeError(); |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | // Do this last, since it relies on the named stream map being complete, and |
| 210 | // that can be updated by previous steps in the finalization. |
| 211 | if (Info) { |
| 212 | if (auto EC = Info->finalizeMsfLayout()) |
| 213 | return std::move(EC); |
| 214 | } |
| 215 | |
Zachary Turner | 199f48a | 2016-07-28 19:11:09 +0000 | [diff] [blame] | 216 | return Msf->build(); |
| 217 | } |
| 218 | |
Zachary Turner | c504ae3 | 2017-05-03 15:58:37 +0000 | [diff] [blame] | 219 | Expected<uint32_t> PDBFileBuilder::getNamedStreamIndex(StringRef Name) const { |
| 220 | uint32_t SN = 0; |
| 221 | if (!NamedStreams.get(Name, SN)) |
| 222 | return llvm::make_error<pdb::RawError>(raw_error_code::no_stream); |
| 223 | return SN; |
| 224 | } |
| 225 | |
Zachary Turner | 9fb9d71 | 2017-08-02 22:31:39 +0000 | [diff] [blame] | 226 | void PDBFileBuilder::commitFpm(WritableBinaryStream &MsfBuffer, |
| 227 | const MSFLayout &Layout) { |
| 228 | auto FpmStream = |
| 229 | WritableMappedBlockStream::createFpmStream(Layout, MsfBuffer, Allocator); |
| 230 | |
| 231 | // We only need to create the alt fpm stream so that it gets initialized. |
| 232 | WritableMappedBlockStream::createFpmStream(Layout, MsfBuffer, Allocator, |
| 233 | true); |
| 234 | |
| 235 | uint32_t BI = 0; |
| 236 | BinaryStreamWriter FpmWriter(*FpmStream); |
| 237 | while (BI < Layout.SB->NumBlocks) { |
| 238 | uint8_t ThisByte = 0; |
| 239 | for (uint32_t I = 0; I < 8; ++I) { |
| 240 | bool IsFree = |
| 241 | (BI < Layout.SB->NumBlocks) ? Layout.FreePageMap.test(BI) : true; |
| 242 | uint8_t Mask = uint8_t(IsFree) << I; |
| 243 | ThisByte |= Mask; |
| 244 | ++BI; |
| 245 | } |
| 246 | cantFail(FpmWriter.writeObject(ThisByte)); |
| 247 | } |
| 248 | assert(FpmWriter.bytesRemaining() == 0); |
| 249 | } |
| 250 | |
Zachary Turner | f228276 | 2018-03-23 19:57:25 +0000 | [diff] [blame^] | 251 | void PDBFileBuilder::commitSrcHeaderBlock(WritableBinaryStream &MsfBuffer, |
| 252 | const msf::MSFLayout &Layout) { |
| 253 | assert(!InjectedSourceTable.empty()); |
| 254 | |
| 255 | uint32_t SN = cantFail(getNamedStreamIndex("/src/headerblock")); |
| 256 | auto Stream = WritableMappedBlockStream::createIndexedStream( |
| 257 | Layout, MsfBuffer, SN, Allocator); |
| 258 | BinaryStreamWriter Writer(*Stream); |
| 259 | |
| 260 | SrcHeaderBlockHeader Header; |
| 261 | ::memset(&Header, 0, sizeof(Header)); |
| 262 | Header.Version = static_cast<uint32_t>(PdbRaw_SrcHeaderBlockVer::SrcVerOne); |
| 263 | Header.Size = Writer.bytesRemaining(); |
| 264 | |
| 265 | cantFail(Writer.writeObject(Header)); |
| 266 | cantFail(InjectedSourceTable.commit(Writer)); |
| 267 | |
| 268 | assert(Writer.bytesRemaining() == 0); |
| 269 | } |
| 270 | |
| 271 | void PDBFileBuilder::commitInjectedSources(WritableBinaryStream &MsfBuffer, |
| 272 | const msf::MSFLayout &Layout) { |
| 273 | if (InjectedSourceTable.empty()) |
| 274 | return; |
| 275 | |
| 276 | commitSrcHeaderBlock(MsfBuffer, Layout); |
| 277 | |
| 278 | for (const auto &IS : InjectedSources) { |
| 279 | uint32_t SN = cantFail(getNamedStreamIndex(IS.StreamName)); |
| 280 | |
| 281 | auto SourceStream = WritableMappedBlockStream::createIndexedStream( |
| 282 | Layout, MsfBuffer, SN, Allocator); |
| 283 | BinaryStreamWriter SourceWriter(*SourceStream); |
| 284 | assert(SourceWriter.bytesRemaining() == IS.Content->getBufferSize()); |
| 285 | cantFail(SourceWriter.writeBytes( |
| 286 | arrayRefFromStringRef(IS.Content->getBuffer()))); |
| 287 | } |
| 288 | } |
| 289 | |
Rui Ueyama | fc22cef | 2016-09-30 20:34:44 +0000 | [diff] [blame] | 290 | Error PDBFileBuilder::commit(StringRef Filename) { |
Bob Haarman | de33a637 | 2017-05-17 20:46:48 +0000 | [diff] [blame] | 291 | assert(!Filename.empty()); |
Zachary Turner | d66889c | 2016-07-28 19:12:28 +0000 | [diff] [blame] | 292 | auto ExpectedLayout = finalizeMsfLayout(); |
| 293 | if (!ExpectedLayout) |
| 294 | return ExpectedLayout.takeError(); |
| 295 | auto &Layout = *ExpectedLayout; |
| 296 | |
Rui Ueyama | fc22cef | 2016-09-30 20:34:44 +0000 | [diff] [blame] | 297 | uint64_t Filesize = Layout.SB->BlockSize * Layout.SB->NumBlocks; |
| 298 | auto OutFileOrError = FileOutputBuffer::create(Filename, Filesize); |
Rafael Espindola | e0df357 | 2017-11-08 01:05:44 +0000 | [diff] [blame] | 299 | if (auto E = OutFileOrError.takeError()) |
| 300 | return E; |
Zachary Turner | c6a75a6 | 2018-03-01 18:00:29 +0000 | [diff] [blame] | 301 | FileOutputBuffer *FOB = OutFileOrError->get(); |
| 302 | |
Zachary Turner | 695ed56 | 2017-02-28 00:04:07 +0000 | [diff] [blame] | 303 | FileBufferByteStream Buffer(std::move(*OutFileOrError), |
| 304 | llvm::support::little); |
Zachary Turner | 120faca | 2017-02-27 22:11:43 +0000 | [diff] [blame] | 305 | BinaryStreamWriter Writer(Buffer); |
Rui Ueyama | fc22cef | 2016-09-30 20:34:44 +0000 | [diff] [blame] | 306 | |
Zachary Turner | d66889c | 2016-07-28 19:12:28 +0000 | [diff] [blame] | 307 | if (auto EC = Writer.writeObject(*Layout.SB)) |
| 308 | return EC; |
Zachary Turner | 9fb9d71 | 2017-08-02 22:31:39 +0000 | [diff] [blame] | 309 | |
| 310 | commitFpm(Buffer, Layout); |
| 311 | |
Zachary Turner | d66889c | 2016-07-28 19:12:28 +0000 | [diff] [blame] | 312 | uint32_t BlockMapOffset = |
| 313 | msf::blockToOffset(Layout.SB->BlockMapAddr, Layout.SB->BlockSize); |
| 314 | Writer.setOffset(BlockMapOffset); |
| 315 | if (auto EC = Writer.writeArray(Layout.DirectoryBlocks)) |
| 316 | return EC; |
| 317 | |
Zachary Turner | 5b74ff3 | 2017-06-03 00:33:35 +0000 | [diff] [blame] | 318 | auto DirStream = WritableMappedBlockStream::createDirectoryStream( |
| 319 | Layout, Buffer, Allocator); |
Zachary Turner | 120faca | 2017-02-27 22:11:43 +0000 | [diff] [blame] | 320 | BinaryStreamWriter DW(*DirStream); |
Zachary Turner | 695ed56 | 2017-02-28 00:04:07 +0000 | [diff] [blame] | 321 | if (auto EC = DW.writeInteger<uint32_t>(Layout.StreamSizes.size())) |
Zachary Turner | d66889c | 2016-07-28 19:12:28 +0000 | [diff] [blame] | 322 | return EC; |
| 323 | |
| 324 | if (auto EC = DW.writeArray(Layout.StreamSizes)) |
| 325 | return EC; |
| 326 | |
| 327 | for (const auto &Blocks : Layout.StreamMap) { |
| 328 | if (auto EC = DW.writeArray(Blocks)) |
| 329 | return EC; |
| 330 | } |
| 331 | |
Zachary Turner | c504ae3 | 2017-05-03 15:58:37 +0000 | [diff] [blame] | 332 | auto ExpectedSN = getNamedStreamIndex("/names"); |
| 333 | if (!ExpectedSN) |
| 334 | return ExpectedSN.takeError(); |
Zachary Turner | 760ad4d | 2017-01-20 22:42:09 +0000 | [diff] [blame] | 335 | |
Zachary Turner | 5b74ff3 | 2017-06-03 00:33:35 +0000 | [diff] [blame] | 336 | auto NS = WritableMappedBlockStream::createIndexedStream( |
| 337 | Layout, Buffer, *ExpectedSN, Allocator); |
Zachary Turner | 120faca | 2017-02-27 22:11:43 +0000 | [diff] [blame] | 338 | BinaryStreamWriter NSWriter(*NS); |
Zachary Turner | 760ad4d | 2017-01-20 22:42:09 +0000 | [diff] [blame] | 339 | if (auto EC = Strings.commit(NSWriter)) |
| 340 | return EC; |
| 341 | |
Zachary Turner | a6fb536 | 2018-03-23 18:43:39 +0000 | [diff] [blame] | 342 | for (const auto &NSE : NamedStreamData) { |
| 343 | if (NSE.second.empty()) |
| 344 | continue; |
| 345 | |
| 346 | auto NS = WritableMappedBlockStream::createIndexedStream( |
| 347 | Layout, Buffer, NSE.first, Allocator); |
| 348 | BinaryStreamWriter NSW(*NS); |
| 349 | if (auto EC = NSW.writeBytes(arrayRefFromStringRef(NSE.second))) |
| 350 | return EC; |
| 351 | } |
| 352 | |
Zachary Turner | d66889c | 2016-07-28 19:12:28 +0000 | [diff] [blame] | 353 | if (Info) { |
| 354 | if (auto EC = Info->commit(Layout, Buffer)) |
| 355 | return EC; |
| 356 | } |
| 357 | |
| 358 | if (Dbi) { |
| 359 | if (auto EC = Dbi->commit(Layout, Buffer)) |
| 360 | return EC; |
| 361 | } |
| 362 | |
Zachary Turner | c6d54da | 2016-09-09 17:46:17 +0000 | [diff] [blame] | 363 | if (Tpi) { |
| 364 | if (auto EC = Tpi->commit(Layout, Buffer)) |
| 365 | return EC; |
| 366 | } |
| 367 | |
Zachary Turner | de9ba15 | 2016-09-15 18:22:31 +0000 | [diff] [blame] | 368 | if (Ipi) { |
| 369 | if (auto EC = Ipi->commit(Layout, Buffer)) |
| 370 | return EC; |
| 371 | } |
| 372 | |
Zachary Turner | 946204c | 2017-08-09 04:23:25 +0000 | [diff] [blame] | 373 | if (Gsi) { |
| 374 | if (auto EC = Gsi->commit(Layout, Buffer)) |
Zachary Turner | 8d927b6 | 2017-07-31 19:36:08 +0000 | [diff] [blame] | 375 | return EC; |
| 376 | } |
| 377 | |
Zachary Turner | c6a75a6 | 2018-03-01 18:00:29 +0000 | [diff] [blame] | 378 | auto InfoStreamBlocks = Layout.StreamMap[StreamPDB]; |
| 379 | assert(!InfoStreamBlocks.empty()); |
| 380 | uint64_t InfoStreamFileOffset = |
| 381 | blockToOffset(InfoStreamBlocks.front(), Layout.SB->BlockSize); |
| 382 | InfoStreamHeader *H = reinterpret_cast<InfoStreamHeader *>( |
| 383 | FOB->getBufferStart() + InfoStreamFileOffset); |
| 384 | |
Zachary Turner | f228276 | 2018-03-23 19:57:25 +0000 | [diff] [blame^] | 385 | commitInjectedSources(Buffer, Layout); |
| 386 | |
Zachary Turner | c6a75a6 | 2018-03-01 18:00:29 +0000 | [diff] [blame] | 387 | // Set the build id at the very end, after every other byte of the PDB |
| 388 | // has been written. |
| 389 | // FIXME: Use a hash of the PDB rather than time(nullptr) for the signature. |
| 390 | H->Age = Info->getAge(); |
| 391 | H->Guid = Info->getGuid(); |
| 392 | Optional<uint32_t> Sig = Info->getSignature(); |
| 393 | H->Signature = Sig.hasValue() ? *Sig : time(nullptr); |
| 394 | |
Zachary Turner | d66889c | 2016-07-28 19:12:28 +0000 | [diff] [blame] | 395 | return Buffer.commit(); |
Rui Ueyama | fc22cef | 2016-09-30 20:34:44 +0000 | [diff] [blame] | 396 | } |