blob: e7304b444f23f896e588b1597410ed014dbc0ecf [file] [log] [blame]
Zachary Turnerdbeaea72016-07-11 21:45:26 +00001//===- 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
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +000010#include "llvm/DebugInfo/PDB/Native/DbiStreamBuilder.h"
Zachary Turnerdbeaea72016-07-11 21:45:26 +000011
Rui Ueyamaf9904042016-10-11 19:43:12 +000012#include "llvm/ADT/ArrayRef.h"
Zachary Turner264b5d92017-06-07 03:48:56 +000013#include "llvm/BinaryFormat/COFF.h"
Zachary Turner620961d2016-09-14 23:00:02 +000014#include "llvm/DebugInfo/MSF/MSFBuilder.h"
Zachary Turnera3225b02016-07-29 20:56:36 +000015#include "llvm/DebugInfo/MSF/MappedBlockStream.h"
Zachary Turner67c56012017-04-27 16:11:19 +000016#include "llvm/DebugInfo/PDB/Native/DbiModuleDescriptorBuilder.h"
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +000017#include "llvm/DebugInfo/PDB/Native/DbiStream.h"
18#include "llvm/DebugInfo/PDB/Native/RawError.h"
Rui Ueyamaddc79222016-10-31 17:38:56 +000019#include "llvm/Object/COFF.h"
Zachary Turnerd9dc2822017-03-02 20:52:51 +000020#include "llvm/Support/BinaryStreamWriter.h"
Zachary Turnerdbeaea72016-07-11 21:45:26 +000021
22using namespace llvm;
23using namespace llvm::codeview;
Zachary Turnerbac69d32016-07-22 19:56:05 +000024using namespace llvm::msf;
Zachary Turnerdbeaea72016-07-11 21:45:26 +000025using namespace llvm::pdb;
26
Zachary Turner620961d2016-09-14 23:00:02 +000027DbiStreamBuilder::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),
Reid Kleckner5d0bc632016-10-11 20:02:57 +000030 Header(nullptr), DbgStreams((int)DbgHeaderType::Max) {}
Zachary Turnerdbeaea72016-07-11 21:45:26 +000031
Zachary Turnerea4e6072017-03-15 22:18:53 +000032DbiStreamBuilder::~DbiStreamBuilder() {}
33
Zachary Turnerdbeaea72016-07-11 21:45:26 +000034void DbiStreamBuilder::setVersionHeader(PdbRaw_DbiVer V) { VerHeader = V; }
35
36void DbiStreamBuilder::setAge(uint32_t A) { Age = A; }
37
38void DbiStreamBuilder::setBuildNumber(uint16_t B) { BuildNumber = B; }
39
40void DbiStreamBuilder::setPdbDllVersion(uint16_t V) { PdbDllVersion = V; }
41
42void DbiStreamBuilder::setPdbDllRbld(uint16_t R) { PdbDllRbld = R; }
43
44void DbiStreamBuilder::setFlags(uint16_t F) { Flags = F; }
45
46void DbiStreamBuilder::setMachineType(PDB_Machine M) { MachineType = M; }
47
Rui Ueyamaddc79222016-10-31 17:38:56 +000048void DbiStreamBuilder::setSectionMap(ArrayRef<SecMapEntry> SecMap) {
49 SectionMap = SecMap;
50}
51
Rui Ueyamaf9904042016-10-11 19:43:12 +000052Error DbiStreamBuilder::addDbgStream(pdb::DbgHeaderType Type,
53 ArrayRef<uint8_t> Data) {
54 if (DbgStreams[(int)Type].StreamNumber)
55 return make_error<RawError>(raw_error_code::duplicate_entry,
56 "The specified stream type already exists");
57 auto ExpectedIndex = Msf.addStream(Data.size());
58 if (!ExpectedIndex)
59 return ExpectedIndex.takeError();
60 uint32_t Index = std::move(*ExpectedIndex);
61 DbgStreams[(int)Type].Data = Data;
62 DbgStreams[(int)Type].StreamNumber = Index;
63 return Error::success();
64}
65
Zachary Turnerfaa554b2016-07-15 22:16:56 +000066uint32_t DbiStreamBuilder::calculateSerializedLength() const {
67 // For now we only support serializing the header.
Zachary Turnerb383d622016-07-22 15:46:46 +000068 return sizeof(DbiStreamHeader) + calculateFileInfoSubstreamSize() +
Rui Ueyamaf7c9c322016-11-11 23:41:13 +000069 calculateModiSubstreamSize() + calculateSectionContribsStreamSize() +
70 calculateSectionMapStreamSize() + calculateDbgStreamsSize();
Zachary Turnerd218c262016-07-22 15:46:37 +000071}
72
Zachary Turner67c56012017-04-27 16:11:19 +000073Expected<DbiModuleDescriptorBuilder &>
Zachary Turnerea4e6072017-03-15 22:18:53 +000074DbiStreamBuilder::addModuleInfo(StringRef ModuleName) {
75 uint32_t Index = ModiList.size();
Zachary Turner67c56012017-04-27 16:11:19 +000076 auto MIB =
77 llvm::make_unique<DbiModuleDescriptorBuilder>(ModuleName, Index, Msf);
Zachary Turnerea4e6072017-03-15 22:18:53 +000078 auto M = MIB.get();
79 auto Result = ModiMap.insert(std::make_pair(ModuleName, std::move(MIB)));
80
Zachary Turnerd218c262016-07-22 15:46:37 +000081 if (!Result.second)
82 return make_error<RawError>(raw_error_code::duplicate_entry,
83 "The specified module already exists");
Zachary Turnerea4e6072017-03-15 22:18:53 +000084 ModiList.push_back(M);
85 return *M;
Zachary Turnerd218c262016-07-22 15:46:37 +000086}
87
88Error DbiStreamBuilder::addModuleSourceFile(StringRef Module, StringRef File) {
Zachary Turnerea4e6072017-03-15 22:18:53 +000089 auto ModIter = ModiMap.find(Module);
90 if (ModIter == ModiMap.end())
Zachary Turnerd218c262016-07-22 15:46:37 +000091 return make_error<RawError>(raw_error_code::no_entry,
92 "The specified module was not found");
93 uint32_t Index = SourceFileNames.size();
94 SourceFileNames.insert(std::make_pair(File, Index));
95 auto &ModEntry = *ModIter;
Zachary Turnerea4e6072017-03-15 22:18:53 +000096 ModEntry.second->addSourceFile(File);
Zachary Turnerd218c262016-07-22 15:46:37 +000097 return Error::success();
98}
99
Zachary Turner8a2ebfb2017-05-01 23:27:42 +0000100Expected<uint32_t> DbiStreamBuilder::getSourceFileNameIndex(StringRef File) {
101 auto NameIter = SourceFileNames.find(File);
102 if (NameIter == SourceFileNames.end())
103 return make_error<RawError>(raw_error_code::no_entry,
104 "The specified source file was not found");
105 return NameIter->getValue();
106}
107
Zachary Turnerd218c262016-07-22 15:46:37 +0000108uint32_t DbiStreamBuilder::calculateModiSubstreamSize() const {
109 uint32_t Size = 0;
Zachary Turnerea4e6072017-03-15 22:18:53 +0000110 for (const auto &M : ModiList)
111 Size += M->calculateSerializedLength();
112 return Size;
Zachary Turnerd218c262016-07-22 15:46:37 +0000113}
114
Rui Ueyamaf7c9c322016-11-11 23:41:13 +0000115uint32_t DbiStreamBuilder::calculateSectionContribsStreamSize() const {
116 if (SectionContribs.empty())
117 return 0;
118 return sizeof(enum PdbRaw_DbiSecContribVer) +
119 sizeof(SectionContribs[0]) * SectionContribs.size();
Rui Ueyamaa8a68a92016-11-12 00:23:32 +0000120}
Rui Ueyamaf7c9c322016-11-11 23:41:13 +0000121
Rui Ueyamaddc79222016-10-31 17:38:56 +0000122uint32_t DbiStreamBuilder::calculateSectionMapStreamSize() const {
123 if (SectionMap.empty())
124 return 0;
125 return sizeof(SecMapHeader) + sizeof(SecMapEntry) * SectionMap.size();
126}
127
Bob Haarman55256ad2017-05-25 21:12:15 +0000128uint32_t DbiStreamBuilder::calculateNamesOffset() const {
129 uint32_t Offset = 0;
130 Offset += sizeof(ulittle16_t); // NumModules
131 Offset += sizeof(ulittle16_t); // NumSourceFiles
132 Offset += ModiList.size() * sizeof(ulittle16_t); // ModIndices
133 Offset += ModiList.size() * sizeof(ulittle16_t); // ModFileCounts
Zachary Turnerd218c262016-07-22 15:46:37 +0000134 uint32_t NumFileInfos = 0;
Zachary Turnerea4e6072017-03-15 22:18:53 +0000135 for (const auto &M : ModiList)
136 NumFileInfos += M->source_files().size();
Bob Haarman55256ad2017-05-25 21:12:15 +0000137 Offset += NumFileInfos * sizeof(ulittle32_t); // FileNameOffsets
138 return Offset;
139}
140
141uint32_t DbiStreamBuilder::calculateFileInfoSubstreamSize() const {
142 uint32_t Size = calculateNamesOffset();
Zachary Turnerd218c262016-07-22 15:46:37 +0000143 Size += calculateNamesBufferSize();
Rui Ueyamafb1e6d22016-11-16 00:59:27 +0000144 return alignTo(Size, sizeof(uint32_t));
Zachary Turnerd218c262016-07-22 15:46:37 +0000145}
146
147uint32_t DbiStreamBuilder::calculateNamesBufferSize() const {
148 uint32_t Size = 0;
149 for (const auto &F : SourceFileNames) {
150 Size += F.getKeyLength() + 1; // Names[I];
151 }
152 return Size;
153}
154
Rui Ueyama77be2402016-10-29 00:56:44 +0000155uint32_t DbiStreamBuilder::calculateDbgStreamsSize() const {
156 return DbgStreams.size() * sizeof(uint16_t);
157}
158
Zachary Turnerd218c262016-07-22 15:46:37 +0000159Error DbiStreamBuilder::generateFileInfoSubstream() {
160 uint32_t Size = calculateFileInfoSubstreamSize();
Zachary Turnerd218c262016-07-22 15:46:37 +0000161 auto Data = Allocator.Allocate<uint8_t>(Size);
Bob Haarman55256ad2017-05-25 21:12:15 +0000162 uint32_t NamesOffset = calculateNamesOffset();
Zachary Turnerd218c262016-07-22 15:46:37 +0000163
Zachary Turner695ed562017-02-28 00:04:07 +0000164 FileInfoBuffer = MutableBinaryByteStream(MutableArrayRef<uint8_t>(Data, Size),
165 llvm::support::little);
Zachary Turnerd218c262016-07-22 15:46:37 +0000166
Zachary Turner120faca2017-02-27 22:11:43 +0000167 WritableBinaryStreamRef MetadataBuffer =
168 WritableBinaryStreamRef(FileInfoBuffer).keep_front(NamesOffset);
169 BinaryStreamWriter MetadataWriter(MetadataBuffer);
Zachary Turnerd218c262016-07-22 15:46:37 +0000170
Zachary Turnerea4e6072017-03-15 22:18:53 +0000171 uint16_t ModiCount = std::min<uint32_t>(UINT16_MAX, ModiList.size());
Rui Ueyama50701312016-11-16 00:38:33 +0000172 uint16_t FileCount = std::min<uint32_t>(UINT16_MAX, SourceFileNames.size());
Zachary Turner695ed562017-02-28 00:04:07 +0000173 if (auto EC = MetadataWriter.writeInteger(ModiCount)) // NumModules
Zachary Turnerd218c262016-07-22 15:46:37 +0000174 return EC;
Zachary Turner695ed562017-02-28 00:04:07 +0000175 if (auto EC = MetadataWriter.writeInteger(FileCount)) // NumSourceFiles
Zachary Turnerd218c262016-07-22 15:46:37 +0000176 return EC;
177 for (uint16_t I = 0; I < ModiCount; ++I) {
Zachary Turner695ed562017-02-28 00:04:07 +0000178 if (auto EC = MetadataWriter.writeInteger(I)) // Mod Indices
Zachary Turnerd218c262016-07-22 15:46:37 +0000179 return EC;
180 }
Zachary Turnerea4e6072017-03-15 22:18:53 +0000181 for (const auto &MI : ModiList) {
182 FileCount = static_cast<uint16_t>(MI->source_files().size());
Zachary Turner695ed562017-02-28 00:04:07 +0000183 if (auto EC = MetadataWriter.writeInteger(FileCount)) // Mod File Counts
Zachary Turnerd218c262016-07-22 15:46:37 +0000184 return EC;
185 }
186
187 // Before writing the FileNameOffsets array, write the NamesBuffer array.
188 // A side effect of this is that this will actually compute the various
189 // file name offsets, so we can then go back and write the FileNameOffsets
190 // array to the other substream.
Zachary Turner120faca2017-02-27 22:11:43 +0000191 NamesBuffer = WritableBinaryStreamRef(FileInfoBuffer).drop_front(NamesOffset);
192 BinaryStreamWriter NameBufferWriter(NamesBuffer);
Zachary Turnerd218c262016-07-22 15:46:37 +0000193 for (auto &Name : SourceFileNames) {
194 Name.second = NameBufferWriter.getOffset();
Zachary Turner120faca2017-02-27 22:11:43 +0000195 if (auto EC = NameBufferWriter.writeCString(Name.getKey()))
Zachary Turnerd218c262016-07-22 15:46:37 +0000196 return EC;
197 }
198
Zachary Turnerea4e6072017-03-15 22:18:53 +0000199 for (const auto &MI : ModiList) {
200 for (StringRef Name : MI->source_files()) {
Zachary Turnerd218c262016-07-22 15:46:37 +0000201 auto Result = SourceFileNames.find(Name);
202 if (Result == SourceFileNames.end())
203 return make_error<RawError>(raw_error_code::no_entry,
204 "The source file was not found.");
Zachary Turner695ed562017-02-28 00:04:07 +0000205 if (auto EC = MetadataWriter.writeInteger(Result->second))
Zachary Turnerd218c262016-07-22 15:46:37 +0000206 return EC;
207 }
208 }
209
Bob Haarman55256ad2017-05-25 21:12:15 +0000210 if (auto EC = NameBufferWriter.padToAlignment(sizeof(uint32_t)))
211 return EC;
212
Zachary Turnerd218c262016-07-22 15:46:37 +0000213 if (NameBufferWriter.bytesRemaining() > 0)
214 return make_error<RawError>(raw_error_code::invalid_format,
215 "The names buffer contained unexpected data.");
216
Rui Ueyamafb1e6d22016-11-16 00:59:27 +0000217 if (MetadataWriter.bytesRemaining() > sizeof(uint32_t))
Zachary Turnerd218c262016-07-22 15:46:37 +0000218 return make_error<RawError>(
219 raw_error_code::invalid_format,
220 "The metadata buffer contained unexpected data.");
221
222 return Error::success();
Zachary Turnerfaa554b2016-07-15 22:16:56 +0000223}
224
Zachary Turnerd66889c2016-07-28 19:12:28 +0000225Error DbiStreamBuilder::finalize() {
226 if (Header)
227 return Error::success();
Zachary Turnerdbeaea72016-07-11 21:45:26 +0000228
Zachary Turnerea4e6072017-03-15 22:18:53 +0000229 for (auto &MI : ModiList)
230 MI->finalize();
Zachary Turnerd218c262016-07-22 15:46:37 +0000231
Zachary Turnerd218c262016-07-22 15:46:37 +0000232 if (auto EC = generateFileInfoSubstream())
Zachary Turnere98137c2016-07-28 19:18:02 +0000233 return EC;
Zachary Turnerd218c262016-07-22 15:46:37 +0000234
Zachary Turnerea4e6072017-03-15 22:18:53 +0000235 DbiStreamHeader *H = Allocator.Allocate<DbiStreamHeader>();
Zachary Turnerdbeaea72016-07-11 21:45:26 +0000236 H->VersionHeader = *VerHeader;
237 H->VersionSignature = -1;
238 H->Age = Age;
239 H->BuildNumber = BuildNumber;
240 H->Flags = Flags;
241 H->PdbDllRbld = PdbDllRbld;
242 H->PdbDllVersion = PdbDllVersion;
243 H->MachineType = static_cast<uint16_t>(MachineType);
244
245 H->ECSubstreamSize = 0;
Zachary Turnerd218c262016-07-22 15:46:37 +0000246 H->FileInfoSize = FileInfoBuffer.getLength();
Zachary Turnerea4e6072017-03-15 22:18:53 +0000247 H->ModiSubstreamSize = calculateModiSubstreamSize();
Rui Ueyamaf9904042016-10-11 19:43:12 +0000248 H->OptionalDbgHdrSize = DbgStreams.size() * sizeof(uint16_t);
Rui Ueyamaf7c9c322016-11-11 23:41:13 +0000249 H->SecContrSubstreamSize = calculateSectionContribsStreamSize();
Rui Ueyamaddc79222016-10-31 17:38:56 +0000250 H->SectionMapSize = calculateSectionMapStreamSize();
Zachary Turnerdbeaea72016-07-11 21:45:26 +0000251 H->TypeServerSize = 0;
Zachary Turnerb383d622016-07-22 15:46:46 +0000252 H->SymRecordStreamIndex = kInvalidStreamIndex;
253 H->PublicSymbolStreamIndex = kInvalidStreamIndex;
254 H->MFCTypeServerIndex = kInvalidStreamIndex;
255 H->GlobalSymbolStreamIndex = kInvalidStreamIndex;
Zachary Turnerdbeaea72016-07-11 21:45:26 +0000256
Zachary Turnerd66889c2016-07-28 19:12:28 +0000257 Header = H;
258 return Error::success();
259}
260
Zachary Turner620961d2016-09-14 23:00:02 +0000261Error DbiStreamBuilder::finalizeMsfLayout() {
Zachary Turnerea4e6072017-03-15 22:18:53 +0000262 for (auto &MI : ModiList) {
263 if (auto EC = MI->finalizeMsfLayout())
264 return EC;
265 }
266
Zachary Turner620961d2016-09-14 23:00:02 +0000267 uint32_t Length = calculateSerializedLength();
268 if (auto EC = Msf.setStreamSize(StreamDBI, Length))
269 return EC;
270 return Error::success();
271}
272
Rui Ueyamaddc79222016-10-31 17:38:56 +0000273static uint16_t toSecMapFlags(uint32_t Flags) {
274 uint16_t Ret = 0;
275 if (Flags & COFF::IMAGE_SCN_MEM_READ)
276 Ret |= static_cast<uint16_t>(OMFSegDescFlags::Read);
277 if (Flags & COFF::IMAGE_SCN_MEM_WRITE)
278 Ret |= static_cast<uint16_t>(OMFSegDescFlags::Write);
279 if (Flags & COFF::IMAGE_SCN_MEM_EXECUTE)
280 Ret |= static_cast<uint16_t>(OMFSegDescFlags::Execute);
281 if (Flags & COFF::IMAGE_SCN_MEM_EXECUTE)
282 Ret |= static_cast<uint16_t>(OMFSegDescFlags::Execute);
283 if (!(Flags & COFF::IMAGE_SCN_MEM_16BIT))
284 Ret |= static_cast<uint16_t>(OMFSegDescFlags::AddressIs32Bit);
285
286 // This seems always 1.
287 Ret |= static_cast<uint16_t>(OMFSegDescFlags::IsSelector);
288
289 return Ret;
290}
291
Reid Kleckner8cbdd0c2017-06-13 15:49:13 +0000292void DbiStreamBuilder::addSectionContrib(DbiModuleDescriptorBuilder *ModuleDbi,
293 const object::coff_section *SecHdr) {
294 SectionContrib SC;
295 memset(&SC, 0, sizeof(SC));
296 SC.ISect = (uint16_t)~0U; // This represents nil.
297 SC.Off = SecHdr->PointerToRawData;
298 SC.Size = SecHdr->SizeOfRawData;
299 SC.Characteristics = SecHdr->Characteristics;
300 // Use the module index in the module dbi stream or nil (-1).
301 SC.Imod = ModuleDbi ? ModuleDbi->getModuleIndex() : (uint16_t)~0U;
302 SectionContribs.emplace_back(SC);
Rui Ueyamaf7c9c322016-11-11 23:41:13 +0000303}
304
Rui Ueyamaddc79222016-10-31 17:38:56 +0000305// A utility function to create a Section Map for a given list of COFF sections.
306//
307// A Section Map seem to be a copy of a COFF section list in other format.
308// I don't know why a PDB file contains both a COFF section header and
309// a Section Map, but it seems it must be present in a PDB.
310std::vector<SecMapEntry> DbiStreamBuilder::createSectionMap(
311 ArrayRef<llvm::object::coff_section> SecHdrs) {
312 std::vector<SecMapEntry> Ret;
313 int Idx = 0;
314
315 auto Add = [&]() -> SecMapEntry & {
316 Ret.emplace_back();
317 auto &Entry = Ret.back();
318 memset(&Entry, 0, sizeof(Entry));
319
320 Entry.Frame = Idx + 1;
321
322 // We don't know the meaning of these fields yet.
323 Entry.SecName = UINT16_MAX;
324 Entry.ClassName = UINT16_MAX;
325
326 return Entry;
327 };
328
329 for (auto &Hdr : SecHdrs) {
330 auto &Entry = Add();
331 Entry.Flags = toSecMapFlags(Hdr.Characteristics);
332 Entry.SecByteLength = Hdr.VirtualSize;
333 ++Idx;
334 }
335
336 // The last entry is for absolute symbols.
337 auto &Entry = Add();
338 Entry.Flags = static_cast<uint16_t>(OMFSegDescFlags::AddressIs32Bit) |
339 static_cast<uint16_t>(OMFSegDescFlags::IsAbsoluteAddress);
340 Entry.SecByteLength = UINT32_MAX;
341
342 return Ret;
343}
344
Zachary Turnera3225b02016-07-29 20:56:36 +0000345Error DbiStreamBuilder::commit(const msf::MSFLayout &Layout,
Zachary Turnerea4e6072017-03-15 22:18:53 +0000346 WritableBinaryStreamRef MsfBuffer) {
Rui Ueyamad1d8c8312016-08-03 23:43:23 +0000347 if (auto EC = finalize())
348 return EC;
349
Zachary Turner5b74ff32017-06-03 00:33:35 +0000350 auto DbiS = WritableMappedBlockStream::createIndexedStream(
351 Layout, MsfBuffer, StreamDBI, Allocator);
Zachary Turnerd66889c2016-07-28 19:12:28 +0000352
Zachary Turnerea4e6072017-03-15 22:18:53 +0000353 BinaryStreamWriter Writer(*DbiS);
Zachary Turnerd66889c2016-07-28 19:12:28 +0000354 if (auto EC = Writer.writeObject(*Header))
355 return EC;
356
Zachary Turnerea4e6072017-03-15 22:18:53 +0000357 for (auto &M : ModiList) {
358 if (auto EC = M->commit(Writer, Layout, MsfBuffer))
359 return EC;
360 }
Rui Ueyamaddc79222016-10-31 17:38:56 +0000361
Rui Ueyamaf7c9c322016-11-11 23:41:13 +0000362 if (!SectionContribs.empty()) {
Zachary Turner695ed562017-02-28 00:04:07 +0000363 if (auto EC = Writer.writeEnum(DbiSecContribVer60))
Rui Ueyamaf7c9c322016-11-11 23:41:13 +0000364 return EC;
Reid Kleckner8cbdd0c2017-06-13 15:49:13 +0000365 if (auto EC = Writer.writeArray(makeArrayRef(SectionContribs)))
Rui Ueyamaf7c9c322016-11-11 23:41:13 +0000366 return EC;
367 }
368
Rui Ueyamaddc79222016-10-31 17:38:56 +0000369 if (!SectionMap.empty()) {
370 ulittle16_t Size = static_cast<ulittle16_t>(SectionMap.size());
371 SecMapHeader SMHeader = {Size, Size};
372 if (auto EC = Writer.writeObject(SMHeader))
373 return EC;
374 if (auto EC = Writer.writeArray(SectionMap))
375 return EC;
376 }
377
Zachary Turnerd66889c2016-07-28 19:12:28 +0000378 if (auto EC = Writer.writeStreamRef(FileInfoBuffer))
379 return EC;
Rui Ueyamaddc79222016-10-31 17:38:56 +0000380
Rui Ueyamaf9904042016-10-11 19:43:12 +0000381 for (auto &Stream : DbgStreams)
Zachary Turner695ed562017-02-28 00:04:07 +0000382 if (auto EC = Writer.writeInteger(Stream.StreamNumber))
Rui Ueyamaf9904042016-10-11 19:43:12 +0000383 return EC;
384
385 for (auto &Stream : DbgStreams) {
386 if (Stream.StreamNumber == kInvalidStreamIndex)
387 continue;
Zachary Turner695ed562017-02-28 00:04:07 +0000388 auto WritableStream = WritableMappedBlockStream::createIndexedStream(
Zachary Turner5b74ff32017-06-03 00:33:35 +0000389 Layout, MsfBuffer, Stream.StreamNumber, Allocator);
Zachary Turner695ed562017-02-28 00:04:07 +0000390 BinaryStreamWriter DbgStreamWriter(*WritableStream);
Rui Ueyamaf9904042016-10-11 19:43:12 +0000391 if (auto EC = DbgStreamWriter.writeArray(Stream.Data))
392 return EC;
393 }
Zachary Turnerd66889c2016-07-28 19:12:28 +0000394
395 if (Writer.bytesRemaining() > 0)
396 return make_error<RawError>(raw_error_code::invalid_format,
397 "Unexpected bytes found in DBI Stream");
398 return Error::success();
399}