blob: 25076e40fc98c1ab6f1d2615d62100c2fcec9c1e [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 Turner7eaf1d92017-07-10 22:40:20 +000052void DbiStreamBuilder::setSymbolRecordStreamIndex(uint32_t Index) {
53 SymRecordStreamIndex = Index;
54}
55
56void DbiStreamBuilder::setPublicsStreamIndex(uint32_t Index) {
57 PublicsStreamIndex = Index;
58}
59
Rui Ueyamaf9904042016-10-11 19:43:12 +000060Error DbiStreamBuilder::addDbgStream(pdb::DbgHeaderType Type,
61 ArrayRef<uint8_t> Data) {
Zachary Turnerc1e93e52017-07-07 18:45:56 +000062 if (DbgStreams[(int)Type].StreamNumber != kInvalidStreamIndex)
Rui Ueyamaf9904042016-10-11 19:43:12 +000063 return make_error<RawError>(raw_error_code::duplicate_entry,
64 "The specified stream type already exists");
65 auto ExpectedIndex = Msf.addStream(Data.size());
66 if (!ExpectedIndex)
67 return ExpectedIndex.takeError();
68 uint32_t Index = std::move(*ExpectedIndex);
69 DbgStreams[(int)Type].Data = Data;
70 DbgStreams[(int)Type].StreamNumber = Index;
71 return Error::success();
72}
73
Zachary Turner6c4bfba2017-07-07 05:04:36 +000074uint32_t DbiStreamBuilder::addECName(StringRef Name) {
75 return ECNamesBuilder.insert(Name);
76}
77
Zachary Turnerfaa554b2016-07-15 22:16:56 +000078uint32_t DbiStreamBuilder::calculateSerializedLength() const {
79 // For now we only support serializing the header.
Zachary Turnerb383d622016-07-22 15:46:46 +000080 return sizeof(DbiStreamHeader) + calculateFileInfoSubstreamSize() +
Rui Ueyamaf7c9c322016-11-11 23:41:13 +000081 calculateModiSubstreamSize() + calculateSectionContribsStreamSize() +
Zachary Turner6c4bfba2017-07-07 05:04:36 +000082 calculateSectionMapStreamSize() + calculateDbgStreamsSize() +
83 ECNamesBuilder.calculateSerializedSize();
Zachary Turnerd218c262016-07-22 15:46:37 +000084}
85
Zachary Turner67c56012017-04-27 16:11:19 +000086Expected<DbiModuleDescriptorBuilder &>
Zachary Turnerea4e6072017-03-15 22:18:53 +000087DbiStreamBuilder::addModuleInfo(StringRef ModuleName) {
88 uint32_t Index = ModiList.size();
Zachary Turner67c56012017-04-27 16:11:19 +000089 auto MIB =
90 llvm::make_unique<DbiModuleDescriptorBuilder>(ModuleName, Index, Msf);
Zachary Turnerea4e6072017-03-15 22:18:53 +000091 auto M = MIB.get();
92 auto Result = ModiMap.insert(std::make_pair(ModuleName, std::move(MIB)));
93
Zachary Turnerd218c262016-07-22 15:46:37 +000094 if (!Result.second)
95 return make_error<RawError>(raw_error_code::duplicate_entry,
96 "The specified module already exists");
Zachary Turnerea4e6072017-03-15 22:18:53 +000097 ModiList.push_back(M);
98 return *M;
Zachary Turnerd218c262016-07-22 15:46:37 +000099}
100
101Error DbiStreamBuilder::addModuleSourceFile(StringRef Module, StringRef File) {
Zachary Turnerea4e6072017-03-15 22:18:53 +0000102 auto ModIter = ModiMap.find(Module);
103 if (ModIter == ModiMap.end())
Zachary Turnerd218c262016-07-22 15:46:37 +0000104 return make_error<RawError>(raw_error_code::no_entry,
105 "The specified module was not found");
Reid Kleckner44cdb102017-06-19 17:21:45 +0000106 return addModuleSourceFile(*ModIter->second, File);
107}
108
109Error DbiStreamBuilder::addModuleSourceFile(DbiModuleDescriptorBuilder &Module,
110 StringRef File) {
Zachary Turnerd218c262016-07-22 15:46:37 +0000111 uint32_t Index = SourceFileNames.size();
112 SourceFileNames.insert(std::make_pair(File, Index));
Reid Kleckner44cdb102017-06-19 17:21:45 +0000113 Module.addSourceFile(File);
Zachary Turnerd218c262016-07-22 15:46:37 +0000114 return Error::success();
115}
116
Zachary Turner8a2ebfb2017-05-01 23:27:42 +0000117Expected<uint32_t> DbiStreamBuilder::getSourceFileNameIndex(StringRef File) {
118 auto NameIter = SourceFileNames.find(File);
119 if (NameIter == SourceFileNames.end())
120 return make_error<RawError>(raw_error_code::no_entry,
121 "The specified source file was not found");
122 return NameIter->getValue();
123}
124
Zachary Turnerd218c262016-07-22 15:46:37 +0000125uint32_t DbiStreamBuilder::calculateModiSubstreamSize() const {
126 uint32_t Size = 0;
Zachary Turnerea4e6072017-03-15 22:18:53 +0000127 for (const auto &M : ModiList)
128 Size += M->calculateSerializedLength();
129 return Size;
Zachary Turnerd218c262016-07-22 15:46:37 +0000130}
131
Rui Ueyamaf7c9c322016-11-11 23:41:13 +0000132uint32_t DbiStreamBuilder::calculateSectionContribsStreamSize() const {
133 if (SectionContribs.empty())
134 return 0;
135 return sizeof(enum PdbRaw_DbiSecContribVer) +
136 sizeof(SectionContribs[0]) * SectionContribs.size();
Rui Ueyamaa8a68a92016-11-12 00:23:32 +0000137}
Rui Ueyamaf7c9c322016-11-11 23:41:13 +0000138
Rui Ueyamaddc79222016-10-31 17:38:56 +0000139uint32_t DbiStreamBuilder::calculateSectionMapStreamSize() const {
140 if (SectionMap.empty())
141 return 0;
142 return sizeof(SecMapHeader) + sizeof(SecMapEntry) * SectionMap.size();
143}
144
Bob Haarman55256ad2017-05-25 21:12:15 +0000145uint32_t DbiStreamBuilder::calculateNamesOffset() const {
146 uint32_t Offset = 0;
147 Offset += sizeof(ulittle16_t); // NumModules
148 Offset += sizeof(ulittle16_t); // NumSourceFiles
149 Offset += ModiList.size() * sizeof(ulittle16_t); // ModIndices
150 Offset += ModiList.size() * sizeof(ulittle16_t); // ModFileCounts
Zachary Turnerd218c262016-07-22 15:46:37 +0000151 uint32_t NumFileInfos = 0;
Zachary Turnerea4e6072017-03-15 22:18:53 +0000152 for (const auto &M : ModiList)
153 NumFileInfos += M->source_files().size();
Bob Haarman55256ad2017-05-25 21:12:15 +0000154 Offset += NumFileInfos * sizeof(ulittle32_t); // FileNameOffsets
155 return Offset;
156}
157
158uint32_t DbiStreamBuilder::calculateFileInfoSubstreamSize() const {
159 uint32_t Size = calculateNamesOffset();
Zachary Turnerd218c262016-07-22 15:46:37 +0000160 Size += calculateNamesBufferSize();
Rui Ueyamafb1e6d22016-11-16 00:59:27 +0000161 return alignTo(Size, sizeof(uint32_t));
Zachary Turnerd218c262016-07-22 15:46:37 +0000162}
163
164uint32_t DbiStreamBuilder::calculateNamesBufferSize() const {
165 uint32_t Size = 0;
166 for (const auto &F : SourceFileNames) {
167 Size += F.getKeyLength() + 1; // Names[I];
168 }
169 return Size;
170}
171
Rui Ueyama77be2402016-10-29 00:56:44 +0000172uint32_t DbiStreamBuilder::calculateDbgStreamsSize() const {
173 return DbgStreams.size() * sizeof(uint16_t);
174}
175
Zachary Turnerd218c262016-07-22 15:46:37 +0000176Error DbiStreamBuilder::generateFileInfoSubstream() {
177 uint32_t Size = calculateFileInfoSubstreamSize();
Zachary Turnerd218c262016-07-22 15:46:37 +0000178 auto Data = Allocator.Allocate<uint8_t>(Size);
Bob Haarman55256ad2017-05-25 21:12:15 +0000179 uint32_t NamesOffset = calculateNamesOffset();
Zachary Turnerd218c262016-07-22 15:46:37 +0000180
Zachary Turner695ed562017-02-28 00:04:07 +0000181 FileInfoBuffer = MutableBinaryByteStream(MutableArrayRef<uint8_t>(Data, Size),
182 llvm::support::little);
Zachary Turnerd218c262016-07-22 15:46:37 +0000183
Zachary Turner120faca2017-02-27 22:11:43 +0000184 WritableBinaryStreamRef MetadataBuffer =
185 WritableBinaryStreamRef(FileInfoBuffer).keep_front(NamesOffset);
186 BinaryStreamWriter MetadataWriter(MetadataBuffer);
Zachary Turnerd218c262016-07-22 15:46:37 +0000187
Zachary Turnerea4e6072017-03-15 22:18:53 +0000188 uint16_t ModiCount = std::min<uint32_t>(UINT16_MAX, ModiList.size());
Rui Ueyama50701312016-11-16 00:38:33 +0000189 uint16_t FileCount = std::min<uint32_t>(UINT16_MAX, SourceFileNames.size());
Zachary Turner695ed562017-02-28 00:04:07 +0000190 if (auto EC = MetadataWriter.writeInteger(ModiCount)) // NumModules
Zachary Turnerd218c262016-07-22 15:46:37 +0000191 return EC;
Zachary Turner695ed562017-02-28 00:04:07 +0000192 if (auto EC = MetadataWriter.writeInteger(FileCount)) // NumSourceFiles
Zachary Turnerd218c262016-07-22 15:46:37 +0000193 return EC;
194 for (uint16_t I = 0; I < ModiCount; ++I) {
Zachary Turner695ed562017-02-28 00:04:07 +0000195 if (auto EC = MetadataWriter.writeInteger(I)) // Mod Indices
Zachary Turnerd218c262016-07-22 15:46:37 +0000196 return EC;
197 }
Zachary Turnerea4e6072017-03-15 22:18:53 +0000198 for (const auto &MI : ModiList) {
199 FileCount = static_cast<uint16_t>(MI->source_files().size());
Zachary Turner695ed562017-02-28 00:04:07 +0000200 if (auto EC = MetadataWriter.writeInteger(FileCount)) // Mod File Counts
Zachary Turnerd218c262016-07-22 15:46:37 +0000201 return EC;
202 }
203
204 // Before writing the FileNameOffsets array, write the NamesBuffer array.
205 // A side effect of this is that this will actually compute the various
206 // file name offsets, so we can then go back and write the FileNameOffsets
207 // array to the other substream.
Zachary Turner120faca2017-02-27 22:11:43 +0000208 NamesBuffer = WritableBinaryStreamRef(FileInfoBuffer).drop_front(NamesOffset);
209 BinaryStreamWriter NameBufferWriter(NamesBuffer);
Zachary Turnerd218c262016-07-22 15:46:37 +0000210 for (auto &Name : SourceFileNames) {
211 Name.second = NameBufferWriter.getOffset();
Zachary Turner120faca2017-02-27 22:11:43 +0000212 if (auto EC = NameBufferWriter.writeCString(Name.getKey()))
Zachary Turnerd218c262016-07-22 15:46:37 +0000213 return EC;
214 }
215
Zachary Turnerea4e6072017-03-15 22:18:53 +0000216 for (const auto &MI : ModiList) {
217 for (StringRef Name : MI->source_files()) {
Zachary Turnerd218c262016-07-22 15:46:37 +0000218 auto Result = SourceFileNames.find(Name);
219 if (Result == SourceFileNames.end())
220 return make_error<RawError>(raw_error_code::no_entry,
221 "The source file was not found.");
Zachary Turner695ed562017-02-28 00:04:07 +0000222 if (auto EC = MetadataWriter.writeInteger(Result->second))
Zachary Turnerd218c262016-07-22 15:46:37 +0000223 return EC;
224 }
225 }
226
Bob Haarman55256ad2017-05-25 21:12:15 +0000227 if (auto EC = NameBufferWriter.padToAlignment(sizeof(uint32_t)))
228 return EC;
229
Zachary Turnerd218c262016-07-22 15:46:37 +0000230 if (NameBufferWriter.bytesRemaining() > 0)
231 return make_error<RawError>(raw_error_code::invalid_format,
232 "The names buffer contained unexpected data.");
233
Rui Ueyamafb1e6d22016-11-16 00:59:27 +0000234 if (MetadataWriter.bytesRemaining() > sizeof(uint32_t))
Zachary Turnerd218c262016-07-22 15:46:37 +0000235 return make_error<RawError>(
236 raw_error_code::invalid_format,
237 "The metadata buffer contained unexpected data.");
238
239 return Error::success();
Zachary Turnerfaa554b2016-07-15 22:16:56 +0000240}
241
Zachary Turnerd66889c2016-07-28 19:12:28 +0000242Error DbiStreamBuilder::finalize() {
243 if (Header)
244 return Error::success();
Zachary Turnerdbeaea72016-07-11 21:45:26 +0000245
Zachary Turnerea4e6072017-03-15 22:18:53 +0000246 for (auto &MI : ModiList)
247 MI->finalize();
Zachary Turnerd218c262016-07-22 15:46:37 +0000248
Zachary Turnerd218c262016-07-22 15:46:37 +0000249 if (auto EC = generateFileInfoSubstream())
Zachary Turnere98137c2016-07-28 19:18:02 +0000250 return EC;
Zachary Turnerd218c262016-07-22 15:46:37 +0000251
Zachary Turnerea4e6072017-03-15 22:18:53 +0000252 DbiStreamHeader *H = Allocator.Allocate<DbiStreamHeader>();
Zachary Turner297b6eb2017-06-20 18:50:55 +0000253 ::memset(H, 0, sizeof(DbiStreamHeader));
Zachary Turnerdbeaea72016-07-11 21:45:26 +0000254 H->VersionHeader = *VerHeader;
255 H->VersionSignature = -1;
256 H->Age = Age;
257 H->BuildNumber = BuildNumber;
258 H->Flags = Flags;
259 H->PdbDllRbld = PdbDllRbld;
260 H->PdbDllVersion = PdbDllVersion;
261 H->MachineType = static_cast<uint16_t>(MachineType);
262
Zachary Turner6c4bfba2017-07-07 05:04:36 +0000263 H->ECSubstreamSize = ECNamesBuilder.calculateSerializedSize();
Zachary Turnerd218c262016-07-22 15:46:37 +0000264 H->FileInfoSize = FileInfoBuffer.getLength();
Zachary Turnerea4e6072017-03-15 22:18:53 +0000265 H->ModiSubstreamSize = calculateModiSubstreamSize();
Rui Ueyamaf9904042016-10-11 19:43:12 +0000266 H->OptionalDbgHdrSize = DbgStreams.size() * sizeof(uint16_t);
Rui Ueyamaf7c9c322016-11-11 23:41:13 +0000267 H->SecContrSubstreamSize = calculateSectionContribsStreamSize();
Rui Ueyamaddc79222016-10-31 17:38:56 +0000268 H->SectionMapSize = calculateSectionMapStreamSize();
Zachary Turnerdbeaea72016-07-11 21:45:26 +0000269 H->TypeServerSize = 0;
Zachary Turner7eaf1d92017-07-10 22:40:20 +0000270 H->SymRecordStreamIndex = SymRecordStreamIndex;
271 H->PublicSymbolStreamIndex = PublicsStreamIndex;
Zachary Turnerb383d622016-07-22 15:46:46 +0000272 H->MFCTypeServerIndex = kInvalidStreamIndex;
273 H->GlobalSymbolStreamIndex = kInvalidStreamIndex;
Zachary Turnerdbeaea72016-07-11 21:45:26 +0000274
Zachary Turnerd66889c2016-07-28 19:12:28 +0000275 Header = H;
276 return Error::success();
277}
278
Zachary Turner620961d2016-09-14 23:00:02 +0000279Error DbiStreamBuilder::finalizeMsfLayout() {
Zachary Turnerea4e6072017-03-15 22:18:53 +0000280 for (auto &MI : ModiList) {
281 if (auto EC = MI->finalizeMsfLayout())
282 return EC;
283 }
284
Zachary Turner620961d2016-09-14 23:00:02 +0000285 uint32_t Length = calculateSerializedLength();
286 if (auto EC = Msf.setStreamSize(StreamDBI, Length))
287 return EC;
288 return Error::success();
289}
290
Rui Ueyamaddc79222016-10-31 17:38:56 +0000291static uint16_t toSecMapFlags(uint32_t Flags) {
292 uint16_t Ret = 0;
293 if (Flags & COFF::IMAGE_SCN_MEM_READ)
294 Ret |= static_cast<uint16_t>(OMFSegDescFlags::Read);
295 if (Flags & COFF::IMAGE_SCN_MEM_WRITE)
296 Ret |= static_cast<uint16_t>(OMFSegDescFlags::Write);
297 if (Flags & COFF::IMAGE_SCN_MEM_EXECUTE)
298 Ret |= static_cast<uint16_t>(OMFSegDescFlags::Execute);
299 if (Flags & COFF::IMAGE_SCN_MEM_EXECUTE)
300 Ret |= static_cast<uint16_t>(OMFSegDescFlags::Execute);
301 if (!(Flags & COFF::IMAGE_SCN_MEM_16BIT))
302 Ret |= static_cast<uint16_t>(OMFSegDescFlags::AddressIs32Bit);
303
304 // This seems always 1.
305 Ret |= static_cast<uint16_t>(OMFSegDescFlags::IsSelector);
306
307 return Ret;
308}
309
Reid Kleckner8cbdd0c2017-06-13 15:49:13 +0000310void DbiStreamBuilder::addSectionContrib(DbiModuleDescriptorBuilder *ModuleDbi,
311 const object::coff_section *SecHdr) {
312 SectionContrib SC;
313 memset(&SC, 0, sizeof(SC));
314 SC.ISect = (uint16_t)~0U; // This represents nil.
315 SC.Off = SecHdr->PointerToRawData;
316 SC.Size = SecHdr->SizeOfRawData;
317 SC.Characteristics = SecHdr->Characteristics;
318 // Use the module index in the module dbi stream or nil (-1).
319 SC.Imod = ModuleDbi ? ModuleDbi->getModuleIndex() : (uint16_t)~0U;
320 SectionContribs.emplace_back(SC);
Rui Ueyamaf7c9c322016-11-11 23:41:13 +0000321}
322
Rui Ueyamaddc79222016-10-31 17:38:56 +0000323// A utility function to create a Section Map for a given list of COFF sections.
324//
325// A Section Map seem to be a copy of a COFF section list in other format.
326// I don't know why a PDB file contains both a COFF section header and
327// a Section Map, but it seems it must be present in a PDB.
328std::vector<SecMapEntry> DbiStreamBuilder::createSectionMap(
329 ArrayRef<llvm::object::coff_section> SecHdrs) {
330 std::vector<SecMapEntry> Ret;
331 int Idx = 0;
332
333 auto Add = [&]() -> SecMapEntry & {
334 Ret.emplace_back();
335 auto &Entry = Ret.back();
336 memset(&Entry, 0, sizeof(Entry));
337
338 Entry.Frame = Idx + 1;
339
340 // We don't know the meaning of these fields yet.
341 Entry.SecName = UINT16_MAX;
342 Entry.ClassName = UINT16_MAX;
343
344 return Entry;
345 };
346
347 for (auto &Hdr : SecHdrs) {
348 auto &Entry = Add();
349 Entry.Flags = toSecMapFlags(Hdr.Characteristics);
350 Entry.SecByteLength = Hdr.VirtualSize;
351 ++Idx;
352 }
353
354 // The last entry is for absolute symbols.
355 auto &Entry = Add();
356 Entry.Flags = static_cast<uint16_t>(OMFSegDescFlags::AddressIs32Bit) |
357 static_cast<uint16_t>(OMFSegDescFlags::IsAbsoluteAddress);
358 Entry.SecByteLength = UINT32_MAX;
359
360 return Ret;
361}
362
Zachary Turnera3225b02016-07-29 20:56:36 +0000363Error DbiStreamBuilder::commit(const msf::MSFLayout &Layout,
Zachary Turnerea4e6072017-03-15 22:18:53 +0000364 WritableBinaryStreamRef MsfBuffer) {
Rui Ueyamad1d8c8312016-08-03 23:43:23 +0000365 if (auto EC = finalize())
366 return EC;
367
Zachary Turner5b74ff32017-06-03 00:33:35 +0000368 auto DbiS = WritableMappedBlockStream::createIndexedStream(
369 Layout, MsfBuffer, StreamDBI, Allocator);
Zachary Turnerd66889c2016-07-28 19:12:28 +0000370
Zachary Turnerea4e6072017-03-15 22:18:53 +0000371 BinaryStreamWriter Writer(*DbiS);
Zachary Turnerd66889c2016-07-28 19:12:28 +0000372 if (auto EC = Writer.writeObject(*Header))
373 return EC;
374
Zachary Turnerea4e6072017-03-15 22:18:53 +0000375 for (auto &M : ModiList) {
376 if (auto EC = M->commit(Writer, Layout, MsfBuffer))
377 return EC;
378 }
Rui Ueyamaddc79222016-10-31 17:38:56 +0000379
Rui Ueyamaf7c9c322016-11-11 23:41:13 +0000380 if (!SectionContribs.empty()) {
Zachary Turner695ed562017-02-28 00:04:07 +0000381 if (auto EC = Writer.writeEnum(DbiSecContribVer60))
Rui Ueyamaf7c9c322016-11-11 23:41:13 +0000382 return EC;
Reid Kleckner8cbdd0c2017-06-13 15:49:13 +0000383 if (auto EC = Writer.writeArray(makeArrayRef(SectionContribs)))
Rui Ueyamaf7c9c322016-11-11 23:41:13 +0000384 return EC;
385 }
386
Rui Ueyamaddc79222016-10-31 17:38:56 +0000387 if (!SectionMap.empty()) {
388 ulittle16_t Size = static_cast<ulittle16_t>(SectionMap.size());
389 SecMapHeader SMHeader = {Size, Size};
390 if (auto EC = Writer.writeObject(SMHeader))
391 return EC;
392 if (auto EC = Writer.writeArray(SectionMap))
393 return EC;
394 }
395
Zachary Turnerd66889c2016-07-28 19:12:28 +0000396 if (auto EC = Writer.writeStreamRef(FileInfoBuffer))
397 return EC;
Rui Ueyamaddc79222016-10-31 17:38:56 +0000398
Zachary Turner6c4bfba2017-07-07 05:04:36 +0000399 if (auto EC = ECNamesBuilder.commit(Writer))
400 return EC;
401
Rui Ueyamaf9904042016-10-11 19:43:12 +0000402 for (auto &Stream : DbgStreams)
Zachary Turner695ed562017-02-28 00:04:07 +0000403 if (auto EC = Writer.writeInteger(Stream.StreamNumber))
Rui Ueyamaf9904042016-10-11 19:43:12 +0000404 return EC;
405
406 for (auto &Stream : DbgStreams) {
407 if (Stream.StreamNumber == kInvalidStreamIndex)
408 continue;
Zachary Turner695ed562017-02-28 00:04:07 +0000409 auto WritableStream = WritableMappedBlockStream::createIndexedStream(
Zachary Turner5b74ff32017-06-03 00:33:35 +0000410 Layout, MsfBuffer, Stream.StreamNumber, Allocator);
Zachary Turner695ed562017-02-28 00:04:07 +0000411 BinaryStreamWriter DbgStreamWriter(*WritableStream);
Rui Ueyamaf9904042016-10-11 19:43:12 +0000412 if (auto EC = DbgStreamWriter.writeArray(Stream.Data))
413 return EC;
414 }
Zachary Turnerd66889c2016-07-28 19:12:28 +0000415
416 if (Writer.bytesRemaining() > 0)
417 return make_error<RawError>(raw_error_code::invalid_format,
418 "Unexpected bytes found in DBI Stream");
419 return Error::success();
420}