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