blob: c96553ff9b168af3b9e75e38b482315438df9cf2 [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
Zachary Turner8d927b62017-07-31 19:36:08 +000052void DbiStreamBuilder::setGlobalsStreamIndex(uint32_t Index) {
53 GlobalsStreamIndex = Index;
54}
55
Zachary Turner7eaf1d92017-07-10 22:40:20 +000056void DbiStreamBuilder::setSymbolRecordStreamIndex(uint32_t Index) {
57 SymRecordStreamIndex = Index;
58}
59
60void DbiStreamBuilder::setPublicsStreamIndex(uint32_t Index) {
61 PublicsStreamIndex = Index;
62}
63
Rui Ueyamaf9904042016-10-11 19:43:12 +000064Error DbiStreamBuilder::addDbgStream(pdb::DbgHeaderType Type,
65 ArrayRef<uint8_t> Data) {
Zachary Turnerc1e93e52017-07-07 18:45:56 +000066 if (DbgStreams[(int)Type].StreamNumber != kInvalidStreamIndex)
Rui Ueyamaf9904042016-10-11 19:43:12 +000067 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
Zachary Turner6c4bfba2017-07-07 05:04:36 +000078uint32_t DbiStreamBuilder::addECName(StringRef Name) {
79 return ECNamesBuilder.insert(Name);
80}
81
Zachary Turnerfaa554b2016-07-15 22:16:56 +000082uint32_t DbiStreamBuilder::calculateSerializedLength() const {
83 // For now we only support serializing the header.
Zachary Turnerb383d622016-07-22 15:46:46 +000084 return sizeof(DbiStreamHeader) + calculateFileInfoSubstreamSize() +
Rui Ueyamaf7c9c322016-11-11 23:41:13 +000085 calculateModiSubstreamSize() + calculateSectionContribsStreamSize() +
Zachary Turner6c4bfba2017-07-07 05:04:36 +000086 calculateSectionMapStreamSize() + calculateDbgStreamsSize() +
87 ECNamesBuilder.calculateSerializedSize();
Zachary Turnerd218c262016-07-22 15:46:37 +000088}
89
Zachary Turner67c56012017-04-27 16:11:19 +000090Expected<DbiModuleDescriptorBuilder &>
Zachary Turnerea4e6072017-03-15 22:18:53 +000091DbiStreamBuilder::addModuleInfo(StringRef ModuleName) {
92 uint32_t Index = ModiList.size();
Peter Collingbourne9e26e972017-09-07 20:39:46 +000093 ModiList.push_back(
94 llvm::make_unique<DbiModuleDescriptorBuilder>(ModuleName, Index, Msf));
95 return *ModiList.back();
Reid Kleckner44cdb102017-06-19 17:21:45 +000096}
97
98Error DbiStreamBuilder::addModuleSourceFile(DbiModuleDescriptorBuilder &Module,
99 StringRef File) {
Zachary Turnerd218c262016-07-22 15:46:37 +0000100 uint32_t Index = SourceFileNames.size();
101 SourceFileNames.insert(std::make_pair(File, Index));
Reid Kleckner44cdb102017-06-19 17:21:45 +0000102 Module.addSourceFile(File);
Zachary Turnerd218c262016-07-22 15:46:37 +0000103 return Error::success();
104}
105
Zachary Turner8a2ebfb2017-05-01 23:27:42 +0000106Expected<uint32_t> DbiStreamBuilder::getSourceFileNameIndex(StringRef File) {
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
Zachary Turnerd218c262016-07-22 15:46:37 +0000114uint32_t DbiStreamBuilder::calculateModiSubstreamSize() const {
115 uint32_t Size = 0;
Zachary Turnerea4e6072017-03-15 22:18:53 +0000116 for (const auto &M : ModiList)
117 Size += M->calculateSerializedLength();
118 return Size;
Zachary Turnerd218c262016-07-22 15:46:37 +0000119}
120
Rui Ueyamaf7c9c322016-11-11 23:41:13 +0000121uint32_t DbiStreamBuilder::calculateSectionContribsStreamSize() const {
122 if (SectionContribs.empty())
123 return 0;
124 return sizeof(enum PdbRaw_DbiSecContribVer) +
125 sizeof(SectionContribs[0]) * SectionContribs.size();
Rui Ueyamaa8a68a92016-11-12 00:23:32 +0000126}
Rui Ueyamaf7c9c322016-11-11 23:41:13 +0000127
Rui Ueyamaddc79222016-10-31 17:38:56 +0000128uint32_t DbiStreamBuilder::calculateSectionMapStreamSize() const {
129 if (SectionMap.empty())
130 return 0;
131 return sizeof(SecMapHeader) + sizeof(SecMapEntry) * SectionMap.size();
132}
133
Bob Haarman55256ad2017-05-25 21:12:15 +0000134uint32_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
Zachary Turnerd218c262016-07-22 15:46:37 +0000140 uint32_t NumFileInfos = 0;
Zachary Turnerea4e6072017-03-15 22:18:53 +0000141 for (const auto &M : ModiList)
142 NumFileInfos += M->source_files().size();
Bob Haarman55256ad2017-05-25 21:12:15 +0000143 Offset += NumFileInfos * sizeof(ulittle32_t); // FileNameOffsets
144 return Offset;
145}
146
147uint32_t DbiStreamBuilder::calculateFileInfoSubstreamSize() const {
148 uint32_t Size = calculateNamesOffset();
Zachary Turnerd218c262016-07-22 15:46:37 +0000149 Size += calculateNamesBufferSize();
Rui Ueyamafb1e6d22016-11-16 00:59:27 +0000150 return alignTo(Size, sizeof(uint32_t));
Zachary Turnerd218c262016-07-22 15:46:37 +0000151}
152
153uint32_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
Rui Ueyama77be2402016-10-29 00:56:44 +0000161uint32_t DbiStreamBuilder::calculateDbgStreamsSize() const {
162 return DbgStreams.size() * sizeof(uint16_t);
163}
164
Zachary Turnerd218c262016-07-22 15:46:37 +0000165Error DbiStreamBuilder::generateFileInfoSubstream() {
166 uint32_t Size = calculateFileInfoSubstreamSize();
Zachary Turnerd218c262016-07-22 15:46:37 +0000167 auto Data = Allocator.Allocate<uint8_t>(Size);
Bob Haarman55256ad2017-05-25 21:12:15 +0000168 uint32_t NamesOffset = calculateNamesOffset();
Zachary Turnerd218c262016-07-22 15:46:37 +0000169
Zachary Turner695ed562017-02-28 00:04:07 +0000170 FileInfoBuffer = MutableBinaryByteStream(MutableArrayRef<uint8_t>(Data, Size),
171 llvm::support::little);
Zachary Turnerd218c262016-07-22 15:46:37 +0000172
Zachary Turner120faca2017-02-27 22:11:43 +0000173 WritableBinaryStreamRef MetadataBuffer =
174 WritableBinaryStreamRef(FileInfoBuffer).keep_front(NamesOffset);
175 BinaryStreamWriter MetadataWriter(MetadataBuffer);
Zachary Turnerd218c262016-07-22 15:46:37 +0000176
Zachary Turnerea4e6072017-03-15 22:18:53 +0000177 uint16_t ModiCount = std::min<uint32_t>(UINT16_MAX, ModiList.size());
Rui Ueyama50701312016-11-16 00:38:33 +0000178 uint16_t FileCount = std::min<uint32_t>(UINT16_MAX, SourceFileNames.size());
Zachary Turner695ed562017-02-28 00:04:07 +0000179 if (auto EC = MetadataWriter.writeInteger(ModiCount)) // NumModules
Zachary Turnerd218c262016-07-22 15:46:37 +0000180 return EC;
Zachary Turner695ed562017-02-28 00:04:07 +0000181 if (auto EC = MetadataWriter.writeInteger(FileCount)) // NumSourceFiles
Zachary Turnerd218c262016-07-22 15:46:37 +0000182 return EC;
183 for (uint16_t I = 0; I < ModiCount; ++I) {
Zachary Turner695ed562017-02-28 00:04:07 +0000184 if (auto EC = MetadataWriter.writeInteger(I)) // Mod Indices
Zachary Turnerd218c262016-07-22 15:46:37 +0000185 return EC;
186 }
Zachary Turnerea4e6072017-03-15 22:18:53 +0000187 for (const auto &MI : ModiList) {
188 FileCount = static_cast<uint16_t>(MI->source_files().size());
Zachary Turner695ed562017-02-28 00:04:07 +0000189 if (auto EC = MetadataWriter.writeInteger(FileCount)) // Mod File Counts
Zachary Turnerd218c262016-07-22 15:46:37 +0000190 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.
Zachary Turner120faca2017-02-27 22:11:43 +0000197 NamesBuffer = WritableBinaryStreamRef(FileInfoBuffer).drop_front(NamesOffset);
198 BinaryStreamWriter NameBufferWriter(NamesBuffer);
Zachary Turnerd218c262016-07-22 15:46:37 +0000199 for (auto &Name : SourceFileNames) {
200 Name.second = NameBufferWriter.getOffset();
Zachary Turner120faca2017-02-27 22:11:43 +0000201 if (auto EC = NameBufferWriter.writeCString(Name.getKey()))
Zachary Turnerd218c262016-07-22 15:46:37 +0000202 return EC;
203 }
204
Zachary Turnerea4e6072017-03-15 22:18:53 +0000205 for (const auto &MI : ModiList) {
206 for (StringRef Name : MI->source_files()) {
Zachary Turnerd218c262016-07-22 15:46:37 +0000207 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.");
Zachary Turner695ed562017-02-28 00:04:07 +0000211 if (auto EC = MetadataWriter.writeInteger(Result->second))
Zachary Turnerd218c262016-07-22 15:46:37 +0000212 return EC;
213 }
214 }
215
Bob Haarman55256ad2017-05-25 21:12:15 +0000216 if (auto EC = NameBufferWriter.padToAlignment(sizeof(uint32_t)))
217 return EC;
218
Zachary Turnerd218c262016-07-22 15:46:37 +0000219 if (NameBufferWriter.bytesRemaining() > 0)
220 return make_error<RawError>(raw_error_code::invalid_format,
221 "The names buffer contained unexpected data.");
222
Rui Ueyamafb1e6d22016-11-16 00:59:27 +0000223 if (MetadataWriter.bytesRemaining() > sizeof(uint32_t))
Zachary Turnerd218c262016-07-22 15:46:37 +0000224 return make_error<RawError>(
225 raw_error_code::invalid_format,
226 "The metadata buffer contained unexpected data.");
227
228 return Error::success();
Zachary Turnerfaa554b2016-07-15 22:16:56 +0000229}
230
Zachary Turnerd66889c2016-07-28 19:12:28 +0000231Error DbiStreamBuilder::finalize() {
232 if (Header)
233 return Error::success();
Zachary Turnerdbeaea72016-07-11 21:45:26 +0000234
Zachary Turnerea4e6072017-03-15 22:18:53 +0000235 for (auto &MI : ModiList)
236 MI->finalize();
Zachary Turnerd218c262016-07-22 15:46:37 +0000237
Zachary Turnerd218c262016-07-22 15:46:37 +0000238 if (auto EC = generateFileInfoSubstream())
Zachary Turnere98137c2016-07-28 19:18:02 +0000239 return EC;
Zachary Turnerd218c262016-07-22 15:46:37 +0000240
Zachary Turnerea4e6072017-03-15 22:18:53 +0000241 DbiStreamHeader *H = Allocator.Allocate<DbiStreamHeader>();
Zachary Turner297b6eb2017-06-20 18:50:55 +0000242 ::memset(H, 0, sizeof(DbiStreamHeader));
Zachary Turnerdbeaea72016-07-11 21:45:26 +0000243 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
Zachary Turner6c4bfba2017-07-07 05:04:36 +0000252 H->ECSubstreamSize = ECNamesBuilder.calculateSerializedSize();
Zachary Turnerd218c262016-07-22 15:46:37 +0000253 H->FileInfoSize = FileInfoBuffer.getLength();
Zachary Turnerea4e6072017-03-15 22:18:53 +0000254 H->ModiSubstreamSize = calculateModiSubstreamSize();
Rui Ueyamaf9904042016-10-11 19:43:12 +0000255 H->OptionalDbgHdrSize = DbgStreams.size() * sizeof(uint16_t);
Rui Ueyamaf7c9c322016-11-11 23:41:13 +0000256 H->SecContrSubstreamSize = calculateSectionContribsStreamSize();
Rui Ueyamaddc79222016-10-31 17:38:56 +0000257 H->SectionMapSize = calculateSectionMapStreamSize();
Zachary Turnerdbeaea72016-07-11 21:45:26 +0000258 H->TypeServerSize = 0;
Zachary Turner7eaf1d92017-07-10 22:40:20 +0000259 H->SymRecordStreamIndex = SymRecordStreamIndex;
260 H->PublicSymbolStreamIndex = PublicsStreamIndex;
Zachary Turnerb383d622016-07-22 15:46:46 +0000261 H->MFCTypeServerIndex = kInvalidStreamIndex;
Zachary Turner8d927b62017-07-31 19:36:08 +0000262 H->GlobalSymbolStreamIndex = GlobalsStreamIndex;
Zachary Turnerdbeaea72016-07-11 21:45:26 +0000263
Zachary Turnerd66889c2016-07-28 19:12:28 +0000264 Header = H;
265 return Error::success();
266}
267
Zachary Turner620961d2016-09-14 23:00:02 +0000268Error DbiStreamBuilder::finalizeMsfLayout() {
Zachary Turnerea4e6072017-03-15 22:18:53 +0000269 for (auto &MI : ModiList) {
270 if (auto EC = MI->finalizeMsfLayout())
271 return EC;
272 }
273
Zachary Turner620961d2016-09-14 23:00:02 +0000274 uint32_t Length = calculateSerializedLength();
275 if (auto EC = Msf.setStreamSize(StreamDBI, Length))
276 return EC;
277 return Error::success();
278}
279
Rui Ueyamaddc79222016-10-31 17:38:56 +0000280static 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.
304std::vector<SecMapEntry> DbiStreamBuilder::createSectionMap(
305 ArrayRef<llvm::object::coff_section> SecHdrs) {
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
Zachary Turnera3225b02016-07-29 20:56:36 +0000339Error DbiStreamBuilder::commit(const msf::MSFLayout &Layout,
Zachary Turnerea4e6072017-03-15 22:18:53 +0000340 WritableBinaryStreamRef MsfBuffer) {
Rui Ueyamad1d8c8312016-08-03 23:43:23 +0000341 if (auto EC = finalize())
342 return EC;
343
Zachary Turner5b74ff32017-06-03 00:33:35 +0000344 auto DbiS = WritableMappedBlockStream::createIndexedStream(
345 Layout, MsfBuffer, StreamDBI, Allocator);
Zachary Turnerd66889c2016-07-28 19:12:28 +0000346
Zachary Turnerea4e6072017-03-15 22:18:53 +0000347 BinaryStreamWriter Writer(*DbiS);
Zachary Turnerd66889c2016-07-28 19:12:28 +0000348 if (auto EC = Writer.writeObject(*Header))
349 return EC;
350
Zachary Turnerea4e6072017-03-15 22:18:53 +0000351 for (auto &M : ModiList) {
352 if (auto EC = M->commit(Writer, Layout, MsfBuffer))
353 return EC;
354 }
Rui Ueyamaddc79222016-10-31 17:38:56 +0000355
Rui Ueyamaf7c9c322016-11-11 23:41:13 +0000356 if (!SectionContribs.empty()) {
Zachary Turner695ed562017-02-28 00:04:07 +0000357 if (auto EC = Writer.writeEnum(DbiSecContribVer60))
Rui Ueyamaf7c9c322016-11-11 23:41:13 +0000358 return EC;
Reid Kleckner8cbdd0c2017-06-13 15:49:13 +0000359 if (auto EC = Writer.writeArray(makeArrayRef(SectionContribs)))
Rui Ueyamaf7c9c322016-11-11 23:41:13 +0000360 return EC;
361 }
362
Rui Ueyamaddc79222016-10-31 17:38:56 +0000363 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
Zachary Turnerd66889c2016-07-28 19:12:28 +0000372 if (auto EC = Writer.writeStreamRef(FileInfoBuffer))
373 return EC;
Rui Ueyamaddc79222016-10-31 17:38:56 +0000374
Zachary Turner6c4bfba2017-07-07 05:04:36 +0000375 if (auto EC = ECNamesBuilder.commit(Writer))
376 return EC;
377
Rui Ueyamaf9904042016-10-11 19:43:12 +0000378 for (auto &Stream : DbgStreams)
Zachary Turner695ed562017-02-28 00:04:07 +0000379 if (auto EC = Writer.writeInteger(Stream.StreamNumber))
Rui Ueyamaf9904042016-10-11 19:43:12 +0000380 return EC;
381
382 for (auto &Stream : DbgStreams) {
383 if (Stream.StreamNumber == kInvalidStreamIndex)
384 continue;
Zachary Turner695ed562017-02-28 00:04:07 +0000385 auto WritableStream = WritableMappedBlockStream::createIndexedStream(
Zachary Turner5b74ff32017-06-03 00:33:35 +0000386 Layout, MsfBuffer, Stream.StreamNumber, Allocator);
Zachary Turner695ed562017-02-28 00:04:07 +0000387 BinaryStreamWriter DbgStreamWriter(*WritableStream);
Rui Ueyamaf9904042016-10-11 19:43:12 +0000388 if (auto EC = DbgStreamWriter.writeArray(Stream.Data))
389 return EC;
390 }
Zachary Turnerd66889c2016-07-28 19:12:28 +0000391
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}