blob: 52886d566e768b3ea071b3b63bf6c4014def3417 [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),
Zachary Turnera6fb5362018-03-23 18:43:39 +000030 Header(nullptr) {}
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
Zachary Turnere3fe6692018-04-16 18:17:13 +000040void DbiStreamBuilder::setBuildNumber(uint8_t Major, uint8_t Minor) {
41 BuildNumber = (uint16_t(Major) << DbiBuildNo::BuildMajorShift) &
42 DbiBuildNo::BuildMajorMask;
43 BuildNumber |= (uint16_t(Minor) << DbiBuildNo::BuildMinorShift) &
44 DbiBuildNo::BuildMinorMask;
45 BuildNumber |= DbiBuildNo::NewVersionFormatMask;
46}
47
Zachary Turnerdbeaea72016-07-11 21:45:26 +000048void DbiStreamBuilder::setPdbDllVersion(uint16_t V) { PdbDllVersion = V; }
49
50void DbiStreamBuilder::setPdbDllRbld(uint16_t R) { PdbDllRbld = R; }
51
52void DbiStreamBuilder::setFlags(uint16_t F) { Flags = F; }
53
54void DbiStreamBuilder::setMachineType(PDB_Machine M) { MachineType = M; }
55
Rui Ueyamaddc79222016-10-31 17:38:56 +000056void DbiStreamBuilder::setSectionMap(ArrayRef<SecMapEntry> SecMap) {
57 SectionMap = SecMap;
58}
59
Zachary Turner8d927b62017-07-31 19:36:08 +000060void DbiStreamBuilder::setGlobalsStreamIndex(uint32_t Index) {
61 GlobalsStreamIndex = Index;
62}
63
Zachary Turner7eaf1d92017-07-10 22:40:20 +000064void DbiStreamBuilder::setSymbolRecordStreamIndex(uint32_t Index) {
65 SymRecordStreamIndex = Index;
66}
67
68void DbiStreamBuilder::setPublicsStreamIndex(uint32_t Index) {
69 PublicsStreamIndex = Index;
70}
71
Rui Ueyamaf9904042016-10-11 19:43:12 +000072Error DbiStreamBuilder::addDbgStream(pdb::DbgHeaderType Type,
73 ArrayRef<uint8_t> Data) {
Zachary Turnera6fb5362018-03-23 18:43:39 +000074 DbgStreams[(int)Type].emplace();
75 DbgStreams[(int)Type]->Data = Data;
Rui Ueyamaf9904042016-10-11 19:43:12 +000076 return Error::success();
77}
78
Zachary Turner6c4bfba2017-07-07 05:04:36 +000079uint32_t DbiStreamBuilder::addECName(StringRef Name) {
80 return ECNamesBuilder.insert(Name);
81}
82
Zachary Turnerfaa554b2016-07-15 22:16:56 +000083uint32_t DbiStreamBuilder::calculateSerializedLength() const {
84 // For now we only support serializing the header.
Zachary Turnerb383d622016-07-22 15:46:46 +000085 return sizeof(DbiStreamHeader) + calculateFileInfoSubstreamSize() +
Rui Ueyamaf7c9c322016-11-11 23:41:13 +000086 calculateModiSubstreamSize() + calculateSectionContribsStreamSize() +
Zachary Turner6c4bfba2017-07-07 05:04:36 +000087 calculateSectionMapStreamSize() + calculateDbgStreamsSize() +
88 ECNamesBuilder.calculateSerializedSize();
Zachary Turnerd218c262016-07-22 15:46:37 +000089}
90
Zachary Turner67c56012017-04-27 16:11:19 +000091Expected<DbiModuleDescriptorBuilder &>
Zachary Turnerea4e6072017-03-15 22:18:53 +000092DbiStreamBuilder::addModuleInfo(StringRef ModuleName) {
93 uint32_t Index = ModiList.size();
Peter Collingbourne9e26e972017-09-07 20:39:46 +000094 ModiList.push_back(
95 llvm::make_unique<DbiModuleDescriptorBuilder>(ModuleName, Index, Msf));
96 return *ModiList.back();
Reid Kleckner44cdb102017-06-19 17:21:45 +000097}
98
99Error DbiStreamBuilder::addModuleSourceFile(DbiModuleDescriptorBuilder &Module,
100 StringRef File) {
Zachary Turnerd218c262016-07-22 15:46:37 +0000101 uint32_t Index = SourceFileNames.size();
102 SourceFileNames.insert(std::make_pair(File, Index));
Reid Kleckner44cdb102017-06-19 17:21:45 +0000103 Module.addSourceFile(File);
Zachary Turnerd218c262016-07-22 15:46:37 +0000104 return Error::success();
105}
106
Zachary Turner8a2ebfb2017-05-01 23:27:42 +0000107Expected<uint32_t> DbiStreamBuilder::getSourceFileNameIndex(StringRef File) {
108 auto NameIter = SourceFileNames.find(File);
109 if (NameIter == SourceFileNames.end())
110 return make_error<RawError>(raw_error_code::no_entry,
111 "The specified source file was not found");
112 return NameIter->getValue();
113}
114
Zachary Turnerd218c262016-07-22 15:46:37 +0000115uint32_t DbiStreamBuilder::calculateModiSubstreamSize() const {
116 uint32_t Size = 0;
Zachary Turnerea4e6072017-03-15 22:18:53 +0000117 for (const auto &M : ModiList)
118 Size += M->calculateSerializedLength();
119 return Size;
Zachary Turnerd218c262016-07-22 15:46:37 +0000120}
121
Rui Ueyamaf7c9c322016-11-11 23:41:13 +0000122uint32_t DbiStreamBuilder::calculateSectionContribsStreamSize() const {
123 if (SectionContribs.empty())
124 return 0;
125 return sizeof(enum PdbRaw_DbiSecContribVer) +
126 sizeof(SectionContribs[0]) * SectionContribs.size();
Rui Ueyamaa8a68a92016-11-12 00:23:32 +0000127}
Rui Ueyamaf7c9c322016-11-11 23:41:13 +0000128
Rui Ueyamaddc79222016-10-31 17:38:56 +0000129uint32_t DbiStreamBuilder::calculateSectionMapStreamSize() const {
130 if (SectionMap.empty())
131 return 0;
132 return sizeof(SecMapHeader) + sizeof(SecMapEntry) * SectionMap.size();
133}
134
Bob Haarman55256ad2017-05-25 21:12:15 +0000135uint32_t DbiStreamBuilder::calculateNamesOffset() const {
136 uint32_t Offset = 0;
137 Offset += sizeof(ulittle16_t); // NumModules
138 Offset += sizeof(ulittle16_t); // NumSourceFiles
139 Offset += ModiList.size() * sizeof(ulittle16_t); // ModIndices
140 Offset += ModiList.size() * sizeof(ulittle16_t); // ModFileCounts
Zachary Turnerd218c262016-07-22 15:46:37 +0000141 uint32_t NumFileInfos = 0;
Zachary Turnerea4e6072017-03-15 22:18:53 +0000142 for (const auto &M : ModiList)
143 NumFileInfos += M->source_files().size();
Bob Haarman55256ad2017-05-25 21:12:15 +0000144 Offset += NumFileInfos * sizeof(ulittle32_t); // FileNameOffsets
145 return Offset;
146}
147
148uint32_t DbiStreamBuilder::calculateFileInfoSubstreamSize() const {
149 uint32_t Size = calculateNamesOffset();
Zachary Turnerd218c262016-07-22 15:46:37 +0000150 Size += calculateNamesBufferSize();
Rui Ueyamafb1e6d22016-11-16 00:59:27 +0000151 return alignTo(Size, sizeof(uint32_t));
Zachary Turnerd218c262016-07-22 15:46:37 +0000152}
153
154uint32_t DbiStreamBuilder::calculateNamesBufferSize() const {
155 uint32_t Size = 0;
156 for (const auto &F : SourceFileNames) {
157 Size += F.getKeyLength() + 1; // Names[I];
158 }
159 return Size;
160}
161
Rui Ueyama77be2402016-10-29 00:56:44 +0000162uint32_t DbiStreamBuilder::calculateDbgStreamsSize() const {
163 return DbgStreams.size() * sizeof(uint16_t);
164}
165
Zachary Turnerd218c262016-07-22 15:46:37 +0000166Error DbiStreamBuilder::generateFileInfoSubstream() {
167 uint32_t Size = calculateFileInfoSubstreamSize();
Zachary Turnerd218c262016-07-22 15:46:37 +0000168 auto Data = Allocator.Allocate<uint8_t>(Size);
Bob Haarman55256ad2017-05-25 21:12:15 +0000169 uint32_t NamesOffset = calculateNamesOffset();
Zachary Turnerd218c262016-07-22 15:46:37 +0000170
Zachary Turner695ed562017-02-28 00:04:07 +0000171 FileInfoBuffer = MutableBinaryByteStream(MutableArrayRef<uint8_t>(Data, Size),
172 llvm::support::little);
Zachary Turnerd218c262016-07-22 15:46:37 +0000173
Zachary Turner120faca2017-02-27 22:11:43 +0000174 WritableBinaryStreamRef MetadataBuffer =
175 WritableBinaryStreamRef(FileInfoBuffer).keep_front(NamesOffset);
176 BinaryStreamWriter MetadataWriter(MetadataBuffer);
Zachary Turnerd218c262016-07-22 15:46:37 +0000177
Zachary Turnerea4e6072017-03-15 22:18:53 +0000178 uint16_t ModiCount = std::min<uint32_t>(UINT16_MAX, ModiList.size());
Rui Ueyama50701312016-11-16 00:38:33 +0000179 uint16_t FileCount = std::min<uint32_t>(UINT16_MAX, SourceFileNames.size());
Zachary Turner695ed562017-02-28 00:04:07 +0000180 if (auto EC = MetadataWriter.writeInteger(ModiCount)) // NumModules
Zachary Turnerd218c262016-07-22 15:46:37 +0000181 return EC;
Zachary Turner695ed562017-02-28 00:04:07 +0000182 if (auto EC = MetadataWriter.writeInteger(FileCount)) // NumSourceFiles
Zachary Turnerd218c262016-07-22 15:46:37 +0000183 return EC;
184 for (uint16_t I = 0; I < ModiCount; ++I) {
Zachary Turner695ed562017-02-28 00:04:07 +0000185 if (auto EC = MetadataWriter.writeInteger(I)) // Mod Indices
Zachary Turnerd218c262016-07-22 15:46:37 +0000186 return EC;
187 }
Zachary Turnerea4e6072017-03-15 22:18:53 +0000188 for (const auto &MI : ModiList) {
189 FileCount = static_cast<uint16_t>(MI->source_files().size());
Zachary Turner695ed562017-02-28 00:04:07 +0000190 if (auto EC = MetadataWriter.writeInteger(FileCount)) // Mod File Counts
Zachary Turnerd218c262016-07-22 15:46:37 +0000191 return EC;
192 }
193
194 // Before writing the FileNameOffsets array, write the NamesBuffer array.
195 // A side effect of this is that this will actually compute the various
196 // file name offsets, so we can then go back and write the FileNameOffsets
197 // array to the other substream.
Zachary Turner120faca2017-02-27 22:11:43 +0000198 NamesBuffer = WritableBinaryStreamRef(FileInfoBuffer).drop_front(NamesOffset);
199 BinaryStreamWriter NameBufferWriter(NamesBuffer);
Zachary Turnerd218c262016-07-22 15:46:37 +0000200 for (auto &Name : SourceFileNames) {
201 Name.second = NameBufferWriter.getOffset();
Zachary Turner120faca2017-02-27 22:11:43 +0000202 if (auto EC = NameBufferWriter.writeCString(Name.getKey()))
Zachary Turnerd218c262016-07-22 15:46:37 +0000203 return EC;
204 }
205
Zachary Turnerea4e6072017-03-15 22:18:53 +0000206 for (const auto &MI : ModiList) {
207 for (StringRef Name : MI->source_files()) {
Zachary Turnerd218c262016-07-22 15:46:37 +0000208 auto Result = SourceFileNames.find(Name);
209 if (Result == SourceFileNames.end())
210 return make_error<RawError>(raw_error_code::no_entry,
211 "The source file was not found.");
Zachary Turner695ed562017-02-28 00:04:07 +0000212 if (auto EC = MetadataWriter.writeInteger(Result->second))
Zachary Turnerd218c262016-07-22 15:46:37 +0000213 return EC;
214 }
215 }
216
Bob Haarman55256ad2017-05-25 21:12:15 +0000217 if (auto EC = NameBufferWriter.padToAlignment(sizeof(uint32_t)))
218 return EC;
219
Zachary Turnerd218c262016-07-22 15:46:37 +0000220 if (NameBufferWriter.bytesRemaining() > 0)
221 return make_error<RawError>(raw_error_code::invalid_format,
222 "The names buffer contained unexpected data.");
223
Rui Ueyamafb1e6d22016-11-16 00:59:27 +0000224 if (MetadataWriter.bytesRemaining() > sizeof(uint32_t))
Zachary Turnerd218c262016-07-22 15:46:37 +0000225 return make_error<RawError>(
226 raw_error_code::invalid_format,
227 "The metadata buffer contained unexpected data.");
228
229 return Error::success();
Zachary Turnerfaa554b2016-07-15 22:16:56 +0000230}
231
Zachary Turnerd66889c2016-07-28 19:12:28 +0000232Error DbiStreamBuilder::finalize() {
233 if (Header)
234 return Error::success();
Zachary Turnerdbeaea72016-07-11 21:45:26 +0000235
Zachary Turnerea4e6072017-03-15 22:18:53 +0000236 for (auto &MI : ModiList)
237 MI->finalize();
Zachary Turnerd218c262016-07-22 15:46:37 +0000238
Zachary Turnerd218c262016-07-22 15:46:37 +0000239 if (auto EC = generateFileInfoSubstream())
Zachary Turnere98137c2016-07-28 19:18:02 +0000240 return EC;
Zachary Turnerd218c262016-07-22 15:46:37 +0000241
Zachary Turnerea4e6072017-03-15 22:18:53 +0000242 DbiStreamHeader *H = Allocator.Allocate<DbiStreamHeader>();
Zachary Turner297b6eb2017-06-20 18:50:55 +0000243 ::memset(H, 0, sizeof(DbiStreamHeader));
Zachary Turnerdbeaea72016-07-11 21:45:26 +0000244 H->VersionHeader = *VerHeader;
245 H->VersionSignature = -1;
246 H->Age = Age;
247 H->BuildNumber = BuildNumber;
248 H->Flags = Flags;
249 H->PdbDllRbld = PdbDllRbld;
250 H->PdbDllVersion = PdbDllVersion;
251 H->MachineType = static_cast<uint16_t>(MachineType);
252
Zachary Turner6c4bfba2017-07-07 05:04:36 +0000253 H->ECSubstreamSize = ECNamesBuilder.calculateSerializedSize();
Zachary Turnerd218c262016-07-22 15:46:37 +0000254 H->FileInfoSize = FileInfoBuffer.getLength();
Zachary Turnerea4e6072017-03-15 22:18:53 +0000255 H->ModiSubstreamSize = calculateModiSubstreamSize();
Rui Ueyamaf9904042016-10-11 19:43:12 +0000256 H->OptionalDbgHdrSize = DbgStreams.size() * sizeof(uint16_t);
Rui Ueyamaf7c9c322016-11-11 23:41:13 +0000257 H->SecContrSubstreamSize = calculateSectionContribsStreamSize();
Rui Ueyamaddc79222016-10-31 17:38:56 +0000258 H->SectionMapSize = calculateSectionMapStreamSize();
Zachary Turnerdbeaea72016-07-11 21:45:26 +0000259 H->TypeServerSize = 0;
Zachary Turner7eaf1d92017-07-10 22:40:20 +0000260 H->SymRecordStreamIndex = SymRecordStreamIndex;
261 H->PublicSymbolStreamIndex = PublicsStreamIndex;
Zachary Turnere3fe6692018-04-16 18:17:13 +0000262 H->MFCTypeServerIndex = 0; // Not sure what this is, but link.exe writes 0.
Zachary Turner8d927b62017-07-31 19:36:08 +0000263 H->GlobalSymbolStreamIndex = GlobalsStreamIndex;
Zachary Turnerdbeaea72016-07-11 21:45:26 +0000264
Zachary Turnerd66889c2016-07-28 19:12:28 +0000265 Header = H;
266 return Error::success();
267}
268
Zachary Turner620961d2016-09-14 23:00:02 +0000269Error DbiStreamBuilder::finalizeMsfLayout() {
Zachary Turnera6fb5362018-03-23 18:43:39 +0000270 for (auto &S : DbgStreams) {
271 if (!S.hasValue())
272 continue;
273 auto ExpectedIndex = Msf.addStream(S->Data.size());
274 if (!ExpectedIndex)
275 return ExpectedIndex.takeError();
276 S->StreamNumber = *ExpectedIndex;
277 }
278
Zachary Turnerea4e6072017-03-15 22:18:53 +0000279 for (auto &MI : ModiList) {
280 if (auto EC = MI->finalizeMsfLayout())
281 return EC;
282 }
283
Zachary Turner620961d2016-09-14 23:00:02 +0000284 uint32_t Length = calculateSerializedLength();
285 if (auto EC = Msf.setStreamSize(StreamDBI, Length))
286 return EC;
287 return Error::success();
288}
289
Rui Ueyamaddc79222016-10-31 17:38:56 +0000290static uint16_t toSecMapFlags(uint32_t Flags) {
291 uint16_t Ret = 0;
292 if (Flags & COFF::IMAGE_SCN_MEM_READ)
293 Ret |= static_cast<uint16_t>(OMFSegDescFlags::Read);
294 if (Flags & COFF::IMAGE_SCN_MEM_WRITE)
295 Ret |= static_cast<uint16_t>(OMFSegDescFlags::Write);
296 if (Flags & COFF::IMAGE_SCN_MEM_EXECUTE)
297 Ret |= static_cast<uint16_t>(OMFSegDescFlags::Execute);
298 if (Flags & COFF::IMAGE_SCN_MEM_EXECUTE)
299 Ret |= static_cast<uint16_t>(OMFSegDescFlags::Execute);
300 if (!(Flags & COFF::IMAGE_SCN_MEM_16BIT))
301 Ret |= static_cast<uint16_t>(OMFSegDescFlags::AddressIs32Bit);
302
303 // This seems always 1.
304 Ret |= static_cast<uint16_t>(OMFSegDescFlags::IsSelector);
305
306 return Ret;
307}
308
309// A utility function to create a Section Map for a given list of COFF sections.
310//
311// A Section Map seem to be a copy of a COFF section list in other format.
312// I don't know why a PDB file contains both a COFF section header and
313// a Section Map, but it seems it must be present in a PDB.
314std::vector<SecMapEntry> DbiStreamBuilder::createSectionMap(
315 ArrayRef<llvm::object::coff_section> SecHdrs) {
316 std::vector<SecMapEntry> Ret;
317 int Idx = 0;
318
319 auto Add = [&]() -> SecMapEntry & {
320 Ret.emplace_back();
321 auto &Entry = Ret.back();
322 memset(&Entry, 0, sizeof(Entry));
323
324 Entry.Frame = Idx + 1;
325
326 // We don't know the meaning of these fields yet.
327 Entry.SecName = UINT16_MAX;
328 Entry.ClassName = UINT16_MAX;
329
330 return Entry;
331 };
332
333 for (auto &Hdr : SecHdrs) {
334 auto &Entry = Add();
335 Entry.Flags = toSecMapFlags(Hdr.Characteristics);
336 Entry.SecByteLength = Hdr.VirtualSize;
337 ++Idx;
338 }
339
340 // The last entry is for absolute symbols.
341 auto &Entry = Add();
342 Entry.Flags = static_cast<uint16_t>(OMFSegDescFlags::AddressIs32Bit) |
343 static_cast<uint16_t>(OMFSegDescFlags::IsAbsoluteAddress);
344 Entry.SecByteLength = UINT32_MAX;
345
346 return Ret;
347}
348
Zachary Turnera3225b02016-07-29 20:56:36 +0000349Error DbiStreamBuilder::commit(const msf::MSFLayout &Layout,
Zachary Turnerea4e6072017-03-15 22:18:53 +0000350 WritableBinaryStreamRef MsfBuffer) {
Rui Ueyamad1d8c8312016-08-03 23:43:23 +0000351 if (auto EC = finalize())
352 return EC;
353
Zachary Turner5b74ff32017-06-03 00:33:35 +0000354 auto DbiS = WritableMappedBlockStream::createIndexedStream(
355 Layout, MsfBuffer, StreamDBI, Allocator);
Zachary Turnerd66889c2016-07-28 19:12:28 +0000356
Zachary Turnerea4e6072017-03-15 22:18:53 +0000357 BinaryStreamWriter Writer(*DbiS);
Zachary Turnerd66889c2016-07-28 19:12:28 +0000358 if (auto EC = Writer.writeObject(*Header))
359 return EC;
360
Zachary Turnerea4e6072017-03-15 22:18:53 +0000361 for (auto &M : ModiList) {
362 if (auto EC = M->commit(Writer, Layout, MsfBuffer))
363 return EC;
364 }
Rui Ueyamaddc79222016-10-31 17:38:56 +0000365
Rui Ueyamaf7c9c322016-11-11 23:41:13 +0000366 if (!SectionContribs.empty()) {
Zachary Turner695ed562017-02-28 00:04:07 +0000367 if (auto EC = Writer.writeEnum(DbiSecContribVer60))
Rui Ueyamaf7c9c322016-11-11 23:41:13 +0000368 return EC;
Reid Kleckner8cbdd0c2017-06-13 15:49:13 +0000369 if (auto EC = Writer.writeArray(makeArrayRef(SectionContribs)))
Rui Ueyamaf7c9c322016-11-11 23:41:13 +0000370 return EC;
371 }
372
Rui Ueyamaddc79222016-10-31 17:38:56 +0000373 if (!SectionMap.empty()) {
374 ulittle16_t Size = static_cast<ulittle16_t>(SectionMap.size());
375 SecMapHeader SMHeader = {Size, Size};
376 if (auto EC = Writer.writeObject(SMHeader))
377 return EC;
378 if (auto EC = Writer.writeArray(SectionMap))
379 return EC;
380 }
381
Zachary Turnerd66889c2016-07-28 19:12:28 +0000382 if (auto EC = Writer.writeStreamRef(FileInfoBuffer))
383 return EC;
Rui Ueyamaddc79222016-10-31 17:38:56 +0000384
Zachary Turner6c4bfba2017-07-07 05:04:36 +0000385 if (auto EC = ECNamesBuilder.commit(Writer))
386 return EC;
387
Zachary Turnera6fb5362018-03-23 18:43:39 +0000388 for (auto &Stream : DbgStreams) {
389 uint16_t StreamNumber = kInvalidStreamIndex;
390 if (Stream.hasValue())
391 StreamNumber = Stream->StreamNumber;
392 if (auto EC = Writer.writeInteger(StreamNumber))
Rui Ueyamaf9904042016-10-11 19:43:12 +0000393 return EC;
Zachary Turnera6fb5362018-03-23 18:43:39 +0000394 }
Rui Ueyamaf9904042016-10-11 19:43:12 +0000395
396 for (auto &Stream : DbgStreams) {
Zachary Turnera6fb5362018-03-23 18:43:39 +0000397 if (!Stream.hasValue())
Rui Ueyamaf9904042016-10-11 19:43:12 +0000398 continue;
Zachary Turnera6fb5362018-03-23 18:43:39 +0000399 assert(Stream->StreamNumber != kInvalidStreamIndex);
400
Zachary Turner695ed562017-02-28 00:04:07 +0000401 auto WritableStream = WritableMappedBlockStream::createIndexedStream(
Zachary Turnera6fb5362018-03-23 18:43:39 +0000402 Layout, MsfBuffer, Stream->StreamNumber, Allocator);
Zachary Turner695ed562017-02-28 00:04:07 +0000403 BinaryStreamWriter DbgStreamWriter(*WritableStream);
Zachary Turnera6fb5362018-03-23 18:43:39 +0000404 if (auto EC = DbgStreamWriter.writeArray(Stream->Data))
Rui Ueyamaf9904042016-10-11 19:43:12 +0000405 return EC;
406 }
Zachary Turnerd66889c2016-07-28 19:12:28 +0000407
408 if (Writer.bytesRemaining() > 0)
409 return make_error<RawError>(raw_error_code::invalid_format,
410 "Unexpected bytes found in DBI Stream");
411 return Error::success();
412}