blob: e49a77b4fcec09ea4955eed7b65f2ee7806ec50b [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) {
Zachary Turnerc1e93e52017-07-07 18:45:56 +000054 if (DbgStreams[(int)Type].StreamNumber != kInvalidStreamIndex)
Rui Ueyamaf9904042016-10-11 19:43:12 +000055 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 Turner6c4bfba2017-07-07 05:04:36 +000066uint32_t DbiStreamBuilder::addECName(StringRef Name) {
67 return ECNamesBuilder.insert(Name);
68}
69
Zachary Turnerfaa554b2016-07-15 22:16:56 +000070uint32_t DbiStreamBuilder::calculateSerializedLength() const {
71 // For now we only support serializing the header.
Zachary Turnerb383d622016-07-22 15:46:46 +000072 return sizeof(DbiStreamHeader) + calculateFileInfoSubstreamSize() +
Rui Ueyamaf7c9c322016-11-11 23:41:13 +000073 calculateModiSubstreamSize() + calculateSectionContribsStreamSize() +
Zachary Turner6c4bfba2017-07-07 05:04:36 +000074 calculateSectionMapStreamSize() + calculateDbgStreamsSize() +
75 ECNamesBuilder.calculateSerializedSize();
Zachary Turnerd218c262016-07-22 15:46:37 +000076}
77
Zachary Turner67c56012017-04-27 16:11:19 +000078Expected<DbiModuleDescriptorBuilder &>
Zachary Turnerea4e6072017-03-15 22:18:53 +000079DbiStreamBuilder::addModuleInfo(StringRef ModuleName) {
80 uint32_t Index = ModiList.size();
Zachary Turner67c56012017-04-27 16:11:19 +000081 auto MIB =
82 llvm::make_unique<DbiModuleDescriptorBuilder>(ModuleName, Index, Msf);
Zachary Turnerea4e6072017-03-15 22:18:53 +000083 auto M = MIB.get();
84 auto Result = ModiMap.insert(std::make_pair(ModuleName, std::move(MIB)));
85
Zachary Turnerd218c262016-07-22 15:46:37 +000086 if (!Result.second)
87 return make_error<RawError>(raw_error_code::duplicate_entry,
88 "The specified module already exists");
Zachary Turnerea4e6072017-03-15 22:18:53 +000089 ModiList.push_back(M);
90 return *M;
Zachary Turnerd218c262016-07-22 15:46:37 +000091}
92
93Error DbiStreamBuilder::addModuleSourceFile(StringRef Module, StringRef File) {
Zachary Turnerea4e6072017-03-15 22:18:53 +000094 auto ModIter = ModiMap.find(Module);
95 if (ModIter == ModiMap.end())
Zachary Turnerd218c262016-07-22 15:46:37 +000096 return make_error<RawError>(raw_error_code::no_entry,
97 "The specified module was not found");
Reid Kleckner44cdb102017-06-19 17:21:45 +000098 return addModuleSourceFile(*ModIter->second, File);
99}
100
101Error DbiStreamBuilder::addModuleSourceFile(DbiModuleDescriptorBuilder &Module,
102 StringRef File) {
Zachary Turnerd218c262016-07-22 15:46:37 +0000103 uint32_t Index = SourceFileNames.size();
104 SourceFileNames.insert(std::make_pair(File, Index));
Reid Kleckner44cdb102017-06-19 17:21:45 +0000105 Module.addSourceFile(File);
Zachary Turnerd218c262016-07-22 15:46:37 +0000106 return Error::success();
107}
108
Zachary Turner8a2ebfb2017-05-01 23:27:42 +0000109Expected<uint32_t> DbiStreamBuilder::getSourceFileNameIndex(StringRef File) {
110 auto NameIter = SourceFileNames.find(File);
111 if (NameIter == SourceFileNames.end())
112 return make_error<RawError>(raw_error_code::no_entry,
113 "The specified source file was not found");
114 return NameIter->getValue();
115}
116
Zachary Turnerd218c262016-07-22 15:46:37 +0000117uint32_t DbiStreamBuilder::calculateModiSubstreamSize() const {
118 uint32_t Size = 0;
Zachary Turnerea4e6072017-03-15 22:18:53 +0000119 for (const auto &M : ModiList)
120 Size += M->calculateSerializedLength();
121 return Size;
Zachary Turnerd218c262016-07-22 15:46:37 +0000122}
123
Rui Ueyamaf7c9c322016-11-11 23:41:13 +0000124uint32_t DbiStreamBuilder::calculateSectionContribsStreamSize() const {
125 if (SectionContribs.empty())
126 return 0;
127 return sizeof(enum PdbRaw_DbiSecContribVer) +
128 sizeof(SectionContribs[0]) * SectionContribs.size();
Rui Ueyamaa8a68a92016-11-12 00:23:32 +0000129}
Rui Ueyamaf7c9c322016-11-11 23:41:13 +0000130
Rui Ueyamaddc79222016-10-31 17:38:56 +0000131uint32_t DbiStreamBuilder::calculateSectionMapStreamSize() const {
132 if (SectionMap.empty())
133 return 0;
134 return sizeof(SecMapHeader) + sizeof(SecMapEntry) * SectionMap.size();
135}
136
Bob Haarman55256ad2017-05-25 21:12:15 +0000137uint32_t DbiStreamBuilder::calculateNamesOffset() const {
138 uint32_t Offset = 0;
139 Offset += sizeof(ulittle16_t); // NumModules
140 Offset += sizeof(ulittle16_t); // NumSourceFiles
141 Offset += ModiList.size() * sizeof(ulittle16_t); // ModIndices
142 Offset += ModiList.size() * sizeof(ulittle16_t); // ModFileCounts
Zachary Turnerd218c262016-07-22 15:46:37 +0000143 uint32_t NumFileInfos = 0;
Zachary Turnerea4e6072017-03-15 22:18:53 +0000144 for (const auto &M : ModiList)
145 NumFileInfos += M->source_files().size();
Bob Haarman55256ad2017-05-25 21:12:15 +0000146 Offset += NumFileInfos * sizeof(ulittle32_t); // FileNameOffsets
147 return Offset;
148}
149
150uint32_t DbiStreamBuilder::calculateFileInfoSubstreamSize() const {
151 uint32_t Size = calculateNamesOffset();
Zachary Turnerd218c262016-07-22 15:46:37 +0000152 Size += calculateNamesBufferSize();
Rui Ueyamafb1e6d22016-11-16 00:59:27 +0000153 return alignTo(Size, sizeof(uint32_t));
Zachary Turnerd218c262016-07-22 15:46:37 +0000154}
155
156uint32_t DbiStreamBuilder::calculateNamesBufferSize() const {
157 uint32_t Size = 0;
158 for (const auto &F : SourceFileNames) {
159 Size += F.getKeyLength() + 1; // Names[I];
160 }
161 return Size;
162}
163
Rui Ueyama77be2402016-10-29 00:56:44 +0000164uint32_t DbiStreamBuilder::calculateDbgStreamsSize() const {
165 return DbgStreams.size() * sizeof(uint16_t);
166}
167
Zachary Turnerd218c262016-07-22 15:46:37 +0000168Error DbiStreamBuilder::generateFileInfoSubstream() {
169 uint32_t Size = calculateFileInfoSubstreamSize();
Zachary Turnerd218c262016-07-22 15:46:37 +0000170 auto Data = Allocator.Allocate<uint8_t>(Size);
Bob Haarman55256ad2017-05-25 21:12:15 +0000171 uint32_t NamesOffset = calculateNamesOffset();
Zachary Turnerd218c262016-07-22 15:46:37 +0000172
Zachary Turner695ed562017-02-28 00:04:07 +0000173 FileInfoBuffer = MutableBinaryByteStream(MutableArrayRef<uint8_t>(Data, Size),
174 llvm::support::little);
Zachary Turnerd218c262016-07-22 15:46:37 +0000175
Zachary Turner120faca2017-02-27 22:11:43 +0000176 WritableBinaryStreamRef MetadataBuffer =
177 WritableBinaryStreamRef(FileInfoBuffer).keep_front(NamesOffset);
178 BinaryStreamWriter MetadataWriter(MetadataBuffer);
Zachary Turnerd218c262016-07-22 15:46:37 +0000179
Zachary Turnerea4e6072017-03-15 22:18:53 +0000180 uint16_t ModiCount = std::min<uint32_t>(UINT16_MAX, ModiList.size());
Rui Ueyama50701312016-11-16 00:38:33 +0000181 uint16_t FileCount = std::min<uint32_t>(UINT16_MAX, SourceFileNames.size());
Zachary Turner695ed562017-02-28 00:04:07 +0000182 if (auto EC = MetadataWriter.writeInteger(ModiCount)) // NumModules
Zachary Turnerd218c262016-07-22 15:46:37 +0000183 return EC;
Zachary Turner695ed562017-02-28 00:04:07 +0000184 if (auto EC = MetadataWriter.writeInteger(FileCount)) // NumSourceFiles
Zachary Turnerd218c262016-07-22 15:46:37 +0000185 return EC;
186 for (uint16_t I = 0; I < ModiCount; ++I) {
Zachary Turner695ed562017-02-28 00:04:07 +0000187 if (auto EC = MetadataWriter.writeInteger(I)) // Mod Indices
Zachary Turnerd218c262016-07-22 15:46:37 +0000188 return EC;
189 }
Zachary Turnerea4e6072017-03-15 22:18:53 +0000190 for (const auto &MI : ModiList) {
191 FileCount = static_cast<uint16_t>(MI->source_files().size());
Zachary Turner695ed562017-02-28 00:04:07 +0000192 if (auto EC = MetadataWriter.writeInteger(FileCount)) // Mod File Counts
Zachary Turnerd218c262016-07-22 15:46:37 +0000193 return EC;
194 }
195
196 // Before writing the FileNameOffsets array, write the NamesBuffer array.
197 // A side effect of this is that this will actually compute the various
198 // file name offsets, so we can then go back and write the FileNameOffsets
199 // array to the other substream.
Zachary Turner120faca2017-02-27 22:11:43 +0000200 NamesBuffer = WritableBinaryStreamRef(FileInfoBuffer).drop_front(NamesOffset);
201 BinaryStreamWriter NameBufferWriter(NamesBuffer);
Zachary Turnerd218c262016-07-22 15:46:37 +0000202 for (auto &Name : SourceFileNames) {
203 Name.second = NameBufferWriter.getOffset();
Zachary Turner120faca2017-02-27 22:11:43 +0000204 if (auto EC = NameBufferWriter.writeCString(Name.getKey()))
Zachary Turnerd218c262016-07-22 15:46:37 +0000205 return EC;
206 }
207
Zachary Turnerea4e6072017-03-15 22:18:53 +0000208 for (const auto &MI : ModiList) {
209 for (StringRef Name : MI->source_files()) {
Zachary Turnerd218c262016-07-22 15:46:37 +0000210 auto Result = SourceFileNames.find(Name);
211 if (Result == SourceFileNames.end())
212 return make_error<RawError>(raw_error_code::no_entry,
213 "The source file was not found.");
Zachary Turner695ed562017-02-28 00:04:07 +0000214 if (auto EC = MetadataWriter.writeInteger(Result->second))
Zachary Turnerd218c262016-07-22 15:46:37 +0000215 return EC;
216 }
217 }
218
Bob Haarman55256ad2017-05-25 21:12:15 +0000219 if (auto EC = NameBufferWriter.padToAlignment(sizeof(uint32_t)))
220 return EC;
221
Zachary Turnerd218c262016-07-22 15:46:37 +0000222 if (NameBufferWriter.bytesRemaining() > 0)
223 return make_error<RawError>(raw_error_code::invalid_format,
224 "The names buffer contained unexpected data.");
225
Rui Ueyamafb1e6d22016-11-16 00:59:27 +0000226 if (MetadataWriter.bytesRemaining() > sizeof(uint32_t))
Zachary Turnerd218c262016-07-22 15:46:37 +0000227 return make_error<RawError>(
228 raw_error_code::invalid_format,
229 "The metadata buffer contained unexpected data.");
230
231 return Error::success();
Zachary Turnerfaa554b2016-07-15 22:16:56 +0000232}
233
Zachary Turnerd66889c2016-07-28 19:12:28 +0000234Error DbiStreamBuilder::finalize() {
235 if (Header)
236 return Error::success();
Zachary Turnerdbeaea72016-07-11 21:45:26 +0000237
Zachary Turnerea4e6072017-03-15 22:18:53 +0000238 for (auto &MI : ModiList)
239 MI->finalize();
Zachary Turnerd218c262016-07-22 15:46:37 +0000240
Zachary Turnerd218c262016-07-22 15:46:37 +0000241 if (auto EC = generateFileInfoSubstream())
Zachary Turnere98137c2016-07-28 19:18:02 +0000242 return EC;
Zachary Turnerd218c262016-07-22 15:46:37 +0000243
Zachary Turnerea4e6072017-03-15 22:18:53 +0000244 DbiStreamHeader *H = Allocator.Allocate<DbiStreamHeader>();
Zachary Turner297b6eb2017-06-20 18:50:55 +0000245 ::memset(H, 0, sizeof(DbiStreamHeader));
Zachary Turnerdbeaea72016-07-11 21:45:26 +0000246 H->VersionHeader = *VerHeader;
247 H->VersionSignature = -1;
248 H->Age = Age;
249 H->BuildNumber = BuildNumber;
250 H->Flags = Flags;
251 H->PdbDllRbld = PdbDllRbld;
252 H->PdbDllVersion = PdbDllVersion;
253 H->MachineType = static_cast<uint16_t>(MachineType);
254
Zachary Turner6c4bfba2017-07-07 05:04:36 +0000255 H->ECSubstreamSize = ECNamesBuilder.calculateSerializedSize();
Zachary Turnerd218c262016-07-22 15:46:37 +0000256 H->FileInfoSize = FileInfoBuffer.getLength();
Zachary Turnerea4e6072017-03-15 22:18:53 +0000257 H->ModiSubstreamSize = calculateModiSubstreamSize();
Rui Ueyamaf9904042016-10-11 19:43:12 +0000258 H->OptionalDbgHdrSize = DbgStreams.size() * sizeof(uint16_t);
Rui Ueyamaf7c9c322016-11-11 23:41:13 +0000259 H->SecContrSubstreamSize = calculateSectionContribsStreamSize();
Rui Ueyamaddc79222016-10-31 17:38:56 +0000260 H->SectionMapSize = calculateSectionMapStreamSize();
Zachary Turnerdbeaea72016-07-11 21:45:26 +0000261 H->TypeServerSize = 0;
Zachary Turnerb383d622016-07-22 15:46:46 +0000262 H->SymRecordStreamIndex = kInvalidStreamIndex;
263 H->PublicSymbolStreamIndex = kInvalidStreamIndex;
264 H->MFCTypeServerIndex = kInvalidStreamIndex;
265 H->GlobalSymbolStreamIndex = kInvalidStreamIndex;
Zachary Turnerdbeaea72016-07-11 21:45:26 +0000266
Zachary Turnerd66889c2016-07-28 19:12:28 +0000267 Header = H;
268 return Error::success();
269}
270
Zachary Turner620961d2016-09-14 23:00:02 +0000271Error DbiStreamBuilder::finalizeMsfLayout() {
Zachary Turnerea4e6072017-03-15 22:18:53 +0000272 for (auto &MI : ModiList) {
273 if (auto EC = MI->finalizeMsfLayout())
274 return EC;
275 }
276
Zachary Turner620961d2016-09-14 23:00:02 +0000277 uint32_t Length = calculateSerializedLength();
278 if (auto EC = Msf.setStreamSize(StreamDBI, Length))
279 return EC;
280 return Error::success();
281}
282
Rui Ueyamaddc79222016-10-31 17:38:56 +0000283static uint16_t toSecMapFlags(uint32_t Flags) {
284 uint16_t Ret = 0;
285 if (Flags & COFF::IMAGE_SCN_MEM_READ)
286 Ret |= static_cast<uint16_t>(OMFSegDescFlags::Read);
287 if (Flags & COFF::IMAGE_SCN_MEM_WRITE)
288 Ret |= static_cast<uint16_t>(OMFSegDescFlags::Write);
289 if (Flags & COFF::IMAGE_SCN_MEM_EXECUTE)
290 Ret |= static_cast<uint16_t>(OMFSegDescFlags::Execute);
291 if (Flags & COFF::IMAGE_SCN_MEM_EXECUTE)
292 Ret |= static_cast<uint16_t>(OMFSegDescFlags::Execute);
293 if (!(Flags & COFF::IMAGE_SCN_MEM_16BIT))
294 Ret |= static_cast<uint16_t>(OMFSegDescFlags::AddressIs32Bit);
295
296 // This seems always 1.
297 Ret |= static_cast<uint16_t>(OMFSegDescFlags::IsSelector);
298
299 return Ret;
300}
301
Reid Kleckner8cbdd0c2017-06-13 15:49:13 +0000302void DbiStreamBuilder::addSectionContrib(DbiModuleDescriptorBuilder *ModuleDbi,
303 const object::coff_section *SecHdr) {
304 SectionContrib SC;
305 memset(&SC, 0, sizeof(SC));
306 SC.ISect = (uint16_t)~0U; // This represents nil.
307 SC.Off = SecHdr->PointerToRawData;
308 SC.Size = SecHdr->SizeOfRawData;
309 SC.Characteristics = SecHdr->Characteristics;
310 // Use the module index in the module dbi stream or nil (-1).
311 SC.Imod = ModuleDbi ? ModuleDbi->getModuleIndex() : (uint16_t)~0U;
312 SectionContribs.emplace_back(SC);
Rui Ueyamaf7c9c322016-11-11 23:41:13 +0000313}
314
Rui Ueyamaddc79222016-10-31 17:38:56 +0000315// A utility function to create a Section Map for a given list of COFF sections.
316//
317// A Section Map seem to be a copy of a COFF section list in other format.
318// I don't know why a PDB file contains both a COFF section header and
319// a Section Map, but it seems it must be present in a PDB.
320std::vector<SecMapEntry> DbiStreamBuilder::createSectionMap(
321 ArrayRef<llvm::object::coff_section> SecHdrs) {
322 std::vector<SecMapEntry> Ret;
323 int Idx = 0;
324
325 auto Add = [&]() -> SecMapEntry & {
326 Ret.emplace_back();
327 auto &Entry = Ret.back();
328 memset(&Entry, 0, sizeof(Entry));
329
330 Entry.Frame = Idx + 1;
331
332 // We don't know the meaning of these fields yet.
333 Entry.SecName = UINT16_MAX;
334 Entry.ClassName = UINT16_MAX;
335
336 return Entry;
337 };
338
339 for (auto &Hdr : SecHdrs) {
340 auto &Entry = Add();
341 Entry.Flags = toSecMapFlags(Hdr.Characteristics);
342 Entry.SecByteLength = Hdr.VirtualSize;
343 ++Idx;
344 }
345
346 // The last entry is for absolute symbols.
347 auto &Entry = Add();
348 Entry.Flags = static_cast<uint16_t>(OMFSegDescFlags::AddressIs32Bit) |
349 static_cast<uint16_t>(OMFSegDescFlags::IsAbsoluteAddress);
350 Entry.SecByteLength = UINT32_MAX;
351
352 return Ret;
353}
354
Zachary Turnera3225b02016-07-29 20:56:36 +0000355Error DbiStreamBuilder::commit(const msf::MSFLayout &Layout,
Zachary Turnerea4e6072017-03-15 22:18:53 +0000356 WritableBinaryStreamRef MsfBuffer) {
Rui Ueyamad1d8c8312016-08-03 23:43:23 +0000357 if (auto EC = finalize())
358 return EC;
359
Zachary Turner5b74ff32017-06-03 00:33:35 +0000360 auto DbiS = WritableMappedBlockStream::createIndexedStream(
361 Layout, MsfBuffer, StreamDBI, Allocator);
Zachary Turnerd66889c2016-07-28 19:12:28 +0000362
Zachary Turnerea4e6072017-03-15 22:18:53 +0000363 BinaryStreamWriter Writer(*DbiS);
Zachary Turnerd66889c2016-07-28 19:12:28 +0000364 if (auto EC = Writer.writeObject(*Header))
365 return EC;
366
Zachary Turnerea4e6072017-03-15 22:18:53 +0000367 for (auto &M : ModiList) {
368 if (auto EC = M->commit(Writer, Layout, MsfBuffer))
369 return EC;
370 }
Rui Ueyamaddc79222016-10-31 17:38:56 +0000371
Rui Ueyamaf7c9c322016-11-11 23:41:13 +0000372 if (!SectionContribs.empty()) {
Zachary Turner695ed562017-02-28 00:04:07 +0000373 if (auto EC = Writer.writeEnum(DbiSecContribVer60))
Rui Ueyamaf7c9c322016-11-11 23:41:13 +0000374 return EC;
Reid Kleckner8cbdd0c2017-06-13 15:49:13 +0000375 if (auto EC = Writer.writeArray(makeArrayRef(SectionContribs)))
Rui Ueyamaf7c9c322016-11-11 23:41:13 +0000376 return EC;
377 }
378
Rui Ueyamaddc79222016-10-31 17:38:56 +0000379 if (!SectionMap.empty()) {
380 ulittle16_t Size = static_cast<ulittle16_t>(SectionMap.size());
381 SecMapHeader SMHeader = {Size, Size};
382 if (auto EC = Writer.writeObject(SMHeader))
383 return EC;
384 if (auto EC = Writer.writeArray(SectionMap))
385 return EC;
386 }
387
Zachary Turnerd66889c2016-07-28 19:12:28 +0000388 if (auto EC = Writer.writeStreamRef(FileInfoBuffer))
389 return EC;
Rui Ueyamaddc79222016-10-31 17:38:56 +0000390
Zachary Turner6c4bfba2017-07-07 05:04:36 +0000391 if (auto EC = ECNamesBuilder.commit(Writer))
392 return EC;
393
Rui Ueyamaf9904042016-10-11 19:43:12 +0000394 for (auto &Stream : DbgStreams)
Zachary Turner695ed562017-02-28 00:04:07 +0000395 if (auto EC = Writer.writeInteger(Stream.StreamNumber))
Rui Ueyamaf9904042016-10-11 19:43:12 +0000396 return EC;
397
398 for (auto &Stream : DbgStreams) {
399 if (Stream.StreamNumber == kInvalidStreamIndex)
400 continue;
Zachary Turner695ed562017-02-28 00:04:07 +0000401 auto WritableStream = WritableMappedBlockStream::createIndexedStream(
Zachary Turner5b74ff32017-06-03 00:33:35 +0000402 Layout, MsfBuffer, Stream.StreamNumber, Allocator);
Zachary Turner695ed562017-02-28 00:04:07 +0000403 BinaryStreamWriter DbgStreamWriter(*WritableStream);
Rui Ueyamaf9904042016-10-11 19:43:12 +0000404 if (auto EC = DbgStreamWriter.writeArray(Stream.Data))
405 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}