blob: b7ade0072ee5a884809967c0b21a1295d05fa542 [file] [log] [blame]
Zachary Turnerdbeaea72016-07-11 21:45:26 +00001//===- DbiStreamBuilder.cpp - PDB Dbi Stream Creation -----------*- C++ -*-===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Zachary Turnerdbeaea72016-07-11 21:45:26 +00006//
7//===----------------------------------------------------------------------===//
8
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +00009#include "llvm/DebugInfo/PDB/Native/DbiStreamBuilder.h"
Zachary Turnerdbeaea72016-07-11 21:45:26 +000010
Rui Ueyamaf9904042016-10-11 19:43:12 +000011#include "llvm/ADT/ArrayRef.h"
Zachary Turner264b5d92017-06-07 03:48:56 +000012#include "llvm/BinaryFormat/COFF.h"
Zachary Turner42e7cc1b2018-09-11 22:35:01 +000013#include "llvm/DebugInfo/CodeView/DebugFrameDataSubsection.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
Zachary Turnerd8d97de2018-04-16 20:42:06 +000056void DbiStreamBuilder::setMachineType(COFF::MachineTypes M) {
57 // These enums are mirrors of each other, so we can just cast the value.
58 MachineType = static_cast<pdb::PDB_Machine>(static_cast<unsigned>(M));
59}
60
Rui Ueyamaddc79222016-10-31 17:38:56 +000061void DbiStreamBuilder::setSectionMap(ArrayRef<SecMapEntry> SecMap) {
62 SectionMap = SecMap;
63}
64
Zachary Turner8d927b62017-07-31 19:36:08 +000065void DbiStreamBuilder::setGlobalsStreamIndex(uint32_t Index) {
66 GlobalsStreamIndex = Index;
67}
68
Zachary Turner7eaf1d92017-07-10 22:40:20 +000069void DbiStreamBuilder::setSymbolRecordStreamIndex(uint32_t Index) {
70 SymRecordStreamIndex = Index;
71}
72
73void DbiStreamBuilder::setPublicsStreamIndex(uint32_t Index) {
74 PublicsStreamIndex = Index;
75}
76
Zachary Turnera1f85f82018-09-12 21:02:01 +000077void DbiStreamBuilder::addNewFpoData(const codeview::FrameData &FD) {
78 if (!NewFpoData.hasValue())
79 NewFpoData.emplace(false);
Zachary Turner42e7cc1b2018-09-11 22:35:01 +000080
Zachary Turnera1f85f82018-09-12 21:02:01 +000081 NewFpoData->addFrameData(FD);
82}
83
84void DbiStreamBuilder::addOldFpoData(const object::FpoData &FD) {
85 OldFpoData.push_back(FD);
Zachary Turner42e7cc1b2018-09-11 22:35:01 +000086}
87
Rui Ueyamaf9904042016-10-11 19:43:12 +000088Error DbiStreamBuilder::addDbgStream(pdb::DbgHeaderType Type,
89 ArrayRef<uint8_t> Data) {
Zachary Turner42e7cc1b2018-09-11 22:35:01 +000090 assert(Type != DbgHeaderType::NewFPO &&
91 "NewFPO data should be written via addFrameData()!");
92
Zachary Turnera6fb5362018-03-23 18:43:39 +000093 DbgStreams[(int)Type].emplace();
Zachary Turner42e7cc1b2018-09-11 22:35:01 +000094 DbgStreams[(int)Type]->Size = Data.size();
95 DbgStreams[(int)Type]->WriteFn = [Data](BinaryStreamWriter &Writer) {
96 return Writer.writeArray(Data);
97 };
Rui Ueyamaf9904042016-10-11 19:43:12 +000098 return Error::success();
99}
100
Zachary Turner6c4bfba2017-07-07 05:04:36 +0000101uint32_t DbiStreamBuilder::addECName(StringRef Name) {
102 return ECNamesBuilder.insert(Name);
103}
104
Zachary Turnerfaa554b2016-07-15 22:16:56 +0000105uint32_t DbiStreamBuilder::calculateSerializedLength() const {
106 // For now we only support serializing the header.
Zachary Turnerb383d622016-07-22 15:46:46 +0000107 return sizeof(DbiStreamHeader) + calculateFileInfoSubstreamSize() +
Rui Ueyamaf7c9c322016-11-11 23:41:13 +0000108 calculateModiSubstreamSize() + calculateSectionContribsStreamSize() +
Zachary Turner6c4bfba2017-07-07 05:04:36 +0000109 calculateSectionMapStreamSize() + calculateDbgStreamsSize() +
110 ECNamesBuilder.calculateSerializedSize();
Zachary Turnerd218c262016-07-22 15:46:37 +0000111}
112
Zachary Turner67c56012017-04-27 16:11:19 +0000113Expected<DbiModuleDescriptorBuilder &>
Zachary Turnerea4e6072017-03-15 22:18:53 +0000114DbiStreamBuilder::addModuleInfo(StringRef ModuleName) {
115 uint32_t Index = ModiList.size();
Peter Collingbourne9e26e972017-09-07 20:39:46 +0000116 ModiList.push_back(
117 llvm::make_unique<DbiModuleDescriptorBuilder>(ModuleName, Index, Msf));
118 return *ModiList.back();
Reid Kleckner44cdb102017-06-19 17:21:45 +0000119}
120
121Error DbiStreamBuilder::addModuleSourceFile(DbiModuleDescriptorBuilder &Module,
122 StringRef File) {
Zachary Turnerd218c262016-07-22 15:46:37 +0000123 uint32_t Index = SourceFileNames.size();
124 SourceFileNames.insert(std::make_pair(File, Index));
Reid Kleckner44cdb102017-06-19 17:21:45 +0000125 Module.addSourceFile(File);
Zachary Turnerd218c262016-07-22 15:46:37 +0000126 return Error::success();
127}
128
Zachary Turner8a2ebfb2017-05-01 23:27:42 +0000129Expected<uint32_t> DbiStreamBuilder::getSourceFileNameIndex(StringRef File) {
130 auto NameIter = SourceFileNames.find(File);
131 if (NameIter == SourceFileNames.end())
132 return make_error<RawError>(raw_error_code::no_entry,
133 "The specified source file was not found");
134 return NameIter->getValue();
135}
136
Zachary Turnerd218c262016-07-22 15:46:37 +0000137uint32_t DbiStreamBuilder::calculateModiSubstreamSize() const {
138 uint32_t Size = 0;
Zachary Turnerea4e6072017-03-15 22:18:53 +0000139 for (const auto &M : ModiList)
140 Size += M->calculateSerializedLength();
141 return Size;
Zachary Turnerd218c262016-07-22 15:46:37 +0000142}
143
Rui Ueyamaf7c9c322016-11-11 23:41:13 +0000144uint32_t DbiStreamBuilder::calculateSectionContribsStreamSize() const {
145 if (SectionContribs.empty())
146 return 0;
147 return sizeof(enum PdbRaw_DbiSecContribVer) +
148 sizeof(SectionContribs[0]) * SectionContribs.size();
Rui Ueyamaa8a68a92016-11-12 00:23:32 +0000149}
Rui Ueyamaf7c9c322016-11-11 23:41:13 +0000150
Rui Ueyamaddc79222016-10-31 17:38:56 +0000151uint32_t DbiStreamBuilder::calculateSectionMapStreamSize() const {
152 if (SectionMap.empty())
153 return 0;
154 return sizeof(SecMapHeader) + sizeof(SecMapEntry) * SectionMap.size();
155}
156
Bob Haarman55256ad2017-05-25 21:12:15 +0000157uint32_t DbiStreamBuilder::calculateNamesOffset() const {
158 uint32_t Offset = 0;
159 Offset += sizeof(ulittle16_t); // NumModules
160 Offset += sizeof(ulittle16_t); // NumSourceFiles
161 Offset += ModiList.size() * sizeof(ulittle16_t); // ModIndices
162 Offset += ModiList.size() * sizeof(ulittle16_t); // ModFileCounts
Zachary Turnerd218c262016-07-22 15:46:37 +0000163 uint32_t NumFileInfos = 0;
Zachary Turnerea4e6072017-03-15 22:18:53 +0000164 for (const auto &M : ModiList)
165 NumFileInfos += M->source_files().size();
Bob Haarman55256ad2017-05-25 21:12:15 +0000166 Offset += NumFileInfos * sizeof(ulittle32_t); // FileNameOffsets
167 return Offset;
168}
169
170uint32_t DbiStreamBuilder::calculateFileInfoSubstreamSize() const {
171 uint32_t Size = calculateNamesOffset();
Zachary Turnerd218c262016-07-22 15:46:37 +0000172 Size += calculateNamesBufferSize();
Rui Ueyamafb1e6d22016-11-16 00:59:27 +0000173 return alignTo(Size, sizeof(uint32_t));
Zachary Turnerd218c262016-07-22 15:46:37 +0000174}
175
176uint32_t DbiStreamBuilder::calculateNamesBufferSize() const {
177 uint32_t Size = 0;
178 for (const auto &F : SourceFileNames) {
179 Size += F.getKeyLength() + 1; // Names[I];
180 }
181 return Size;
182}
183
Rui Ueyama77be2402016-10-29 00:56:44 +0000184uint32_t DbiStreamBuilder::calculateDbgStreamsSize() const {
185 return DbgStreams.size() * sizeof(uint16_t);
186}
187
Zachary Turnerd218c262016-07-22 15:46:37 +0000188Error DbiStreamBuilder::generateFileInfoSubstream() {
189 uint32_t Size = calculateFileInfoSubstreamSize();
Zachary Turnerd218c262016-07-22 15:46:37 +0000190 auto Data = Allocator.Allocate<uint8_t>(Size);
Bob Haarman55256ad2017-05-25 21:12:15 +0000191 uint32_t NamesOffset = calculateNamesOffset();
Zachary Turnerd218c262016-07-22 15:46:37 +0000192
Zachary Turner695ed562017-02-28 00:04:07 +0000193 FileInfoBuffer = MutableBinaryByteStream(MutableArrayRef<uint8_t>(Data, Size),
194 llvm::support::little);
Zachary Turnerd218c262016-07-22 15:46:37 +0000195
Zachary Turner120faca2017-02-27 22:11:43 +0000196 WritableBinaryStreamRef MetadataBuffer =
197 WritableBinaryStreamRef(FileInfoBuffer).keep_front(NamesOffset);
198 BinaryStreamWriter MetadataWriter(MetadataBuffer);
Zachary Turnerd218c262016-07-22 15:46:37 +0000199
Zachary Turnerea4e6072017-03-15 22:18:53 +0000200 uint16_t ModiCount = std::min<uint32_t>(UINT16_MAX, ModiList.size());
Rui Ueyama50701312016-11-16 00:38:33 +0000201 uint16_t FileCount = std::min<uint32_t>(UINT16_MAX, SourceFileNames.size());
Zachary Turner695ed562017-02-28 00:04:07 +0000202 if (auto EC = MetadataWriter.writeInteger(ModiCount)) // NumModules
Zachary Turnerd218c262016-07-22 15:46:37 +0000203 return EC;
Zachary Turner695ed562017-02-28 00:04:07 +0000204 if (auto EC = MetadataWriter.writeInteger(FileCount)) // NumSourceFiles
Zachary Turnerd218c262016-07-22 15:46:37 +0000205 return EC;
206 for (uint16_t I = 0; I < ModiCount; ++I) {
Zachary Turner695ed562017-02-28 00:04:07 +0000207 if (auto EC = MetadataWriter.writeInteger(I)) // Mod Indices
Zachary Turnerd218c262016-07-22 15:46:37 +0000208 return EC;
209 }
Zachary Turnerea4e6072017-03-15 22:18:53 +0000210 for (const auto &MI : ModiList) {
211 FileCount = static_cast<uint16_t>(MI->source_files().size());
Zachary Turner695ed562017-02-28 00:04:07 +0000212 if (auto EC = MetadataWriter.writeInteger(FileCount)) // Mod File Counts
Zachary Turnerd218c262016-07-22 15:46:37 +0000213 return EC;
214 }
215
216 // Before writing the FileNameOffsets array, write the NamesBuffer array.
217 // A side effect of this is that this will actually compute the various
218 // file name offsets, so we can then go back and write the FileNameOffsets
219 // array to the other substream.
Zachary Turner120faca2017-02-27 22:11:43 +0000220 NamesBuffer = WritableBinaryStreamRef(FileInfoBuffer).drop_front(NamesOffset);
221 BinaryStreamWriter NameBufferWriter(NamesBuffer);
Zachary Turnerd218c262016-07-22 15:46:37 +0000222 for (auto &Name : SourceFileNames) {
223 Name.second = NameBufferWriter.getOffset();
Zachary Turner120faca2017-02-27 22:11:43 +0000224 if (auto EC = NameBufferWriter.writeCString(Name.getKey()))
Zachary Turnerd218c262016-07-22 15:46:37 +0000225 return EC;
226 }
227
Zachary Turnerea4e6072017-03-15 22:18:53 +0000228 for (const auto &MI : ModiList) {
229 for (StringRef Name : MI->source_files()) {
Zachary Turnerd218c262016-07-22 15:46:37 +0000230 auto Result = SourceFileNames.find(Name);
231 if (Result == SourceFileNames.end())
232 return make_error<RawError>(raw_error_code::no_entry,
233 "The source file was not found.");
Zachary Turner695ed562017-02-28 00:04:07 +0000234 if (auto EC = MetadataWriter.writeInteger(Result->second))
Zachary Turnerd218c262016-07-22 15:46:37 +0000235 return EC;
236 }
237 }
238
Bob Haarman55256ad2017-05-25 21:12:15 +0000239 if (auto EC = NameBufferWriter.padToAlignment(sizeof(uint32_t)))
240 return EC;
241
Zachary Turnerd218c262016-07-22 15:46:37 +0000242 if (NameBufferWriter.bytesRemaining() > 0)
243 return make_error<RawError>(raw_error_code::invalid_format,
244 "The names buffer contained unexpected data.");
245
Rui Ueyamafb1e6d22016-11-16 00:59:27 +0000246 if (MetadataWriter.bytesRemaining() > sizeof(uint32_t))
Zachary Turnerd218c262016-07-22 15:46:37 +0000247 return make_error<RawError>(
248 raw_error_code::invalid_format,
249 "The metadata buffer contained unexpected data.");
250
251 return Error::success();
Zachary Turnerfaa554b2016-07-15 22:16:56 +0000252}
253
Zachary Turnerd66889c2016-07-28 19:12:28 +0000254Error DbiStreamBuilder::finalize() {
255 if (Header)
256 return Error::success();
Zachary Turnerdbeaea72016-07-11 21:45:26 +0000257
Zachary Turnerea4e6072017-03-15 22:18:53 +0000258 for (auto &MI : ModiList)
259 MI->finalize();
Zachary Turnerd218c262016-07-22 15:46:37 +0000260
Zachary Turnerd218c262016-07-22 15:46:37 +0000261 if (auto EC = generateFileInfoSubstream())
Zachary Turnere98137c2016-07-28 19:18:02 +0000262 return EC;
Zachary Turnerd218c262016-07-22 15:46:37 +0000263
Zachary Turnerea4e6072017-03-15 22:18:53 +0000264 DbiStreamHeader *H = Allocator.Allocate<DbiStreamHeader>();
Zachary Turner297b6eb2017-06-20 18:50:55 +0000265 ::memset(H, 0, sizeof(DbiStreamHeader));
Zachary Turnerdbeaea72016-07-11 21:45:26 +0000266 H->VersionHeader = *VerHeader;
267 H->VersionSignature = -1;
268 H->Age = Age;
269 H->BuildNumber = BuildNumber;
270 H->Flags = Flags;
271 H->PdbDllRbld = PdbDllRbld;
272 H->PdbDllVersion = PdbDllVersion;
273 H->MachineType = static_cast<uint16_t>(MachineType);
274
Zachary Turner6c4bfba2017-07-07 05:04:36 +0000275 H->ECSubstreamSize = ECNamesBuilder.calculateSerializedSize();
Zachary Turnerd218c262016-07-22 15:46:37 +0000276 H->FileInfoSize = FileInfoBuffer.getLength();
Zachary Turnerea4e6072017-03-15 22:18:53 +0000277 H->ModiSubstreamSize = calculateModiSubstreamSize();
Rui Ueyamaf9904042016-10-11 19:43:12 +0000278 H->OptionalDbgHdrSize = DbgStreams.size() * sizeof(uint16_t);
Rui Ueyamaf7c9c322016-11-11 23:41:13 +0000279 H->SecContrSubstreamSize = calculateSectionContribsStreamSize();
Rui Ueyamaddc79222016-10-31 17:38:56 +0000280 H->SectionMapSize = calculateSectionMapStreamSize();
Zachary Turnerdbeaea72016-07-11 21:45:26 +0000281 H->TypeServerSize = 0;
Zachary Turner7eaf1d92017-07-10 22:40:20 +0000282 H->SymRecordStreamIndex = SymRecordStreamIndex;
283 H->PublicSymbolStreamIndex = PublicsStreamIndex;
Zachary Turnere3fe6692018-04-16 18:17:13 +0000284 H->MFCTypeServerIndex = 0; // Not sure what this is, but link.exe writes 0.
Zachary Turner8d927b62017-07-31 19:36:08 +0000285 H->GlobalSymbolStreamIndex = GlobalsStreamIndex;
Zachary Turnerdbeaea72016-07-11 21:45:26 +0000286
Zachary Turnerd66889c2016-07-28 19:12:28 +0000287 Header = H;
288 return Error::success();
289}
290
Zachary Turner620961d2016-09-14 23:00:02 +0000291Error DbiStreamBuilder::finalizeMsfLayout() {
Zachary Turnera1f85f82018-09-12 21:02:01 +0000292 if (NewFpoData.hasValue()) {
Zachary Turner42e7cc1b2018-09-11 22:35:01 +0000293 DbgStreams[(int)DbgHeaderType::NewFPO].emplace();
294 DbgStreams[(int)DbgHeaderType::NewFPO]->Size =
Zachary Turnera1f85f82018-09-12 21:02:01 +0000295 NewFpoData->calculateSerializedSize();
Zachary Turner42e7cc1b2018-09-11 22:35:01 +0000296 DbgStreams[(int)DbgHeaderType::NewFPO]->WriteFn =
297 [this](BinaryStreamWriter &Writer) {
Zachary Turnera1f85f82018-09-12 21:02:01 +0000298 return NewFpoData->commit(Writer);
299 };
300 }
301
302 if (!OldFpoData.empty()) {
303 DbgStreams[(int)DbgHeaderType::FPO].emplace();
304 DbgStreams[(int)DbgHeaderType::FPO]->Size =
305 sizeof(object::FpoData) * OldFpoData.size();
306 DbgStreams[(int)DbgHeaderType::FPO]->WriteFn =
307 [this](BinaryStreamWriter &Writer) {
308 return Writer.writeArray(makeArrayRef(OldFpoData));
Zachary Turner42e7cc1b2018-09-11 22:35:01 +0000309 };
310 }
311
Zachary Turnera6fb5362018-03-23 18:43:39 +0000312 for (auto &S : DbgStreams) {
313 if (!S.hasValue())
314 continue;
Zachary Turner42e7cc1b2018-09-11 22:35:01 +0000315 auto ExpectedIndex = Msf.addStream(S->Size);
Zachary Turnera6fb5362018-03-23 18:43:39 +0000316 if (!ExpectedIndex)
317 return ExpectedIndex.takeError();
318 S->StreamNumber = *ExpectedIndex;
319 }
320
Zachary Turnerea4e6072017-03-15 22:18:53 +0000321 for (auto &MI : ModiList) {
322 if (auto EC = MI->finalizeMsfLayout())
323 return EC;
324 }
325
Zachary Turner620961d2016-09-14 23:00:02 +0000326 uint32_t Length = calculateSerializedLength();
327 if (auto EC = Msf.setStreamSize(StreamDBI, Length))
328 return EC;
329 return Error::success();
330}
331
Rui Ueyamaddc79222016-10-31 17:38:56 +0000332static uint16_t toSecMapFlags(uint32_t Flags) {
333 uint16_t Ret = 0;
334 if (Flags & COFF::IMAGE_SCN_MEM_READ)
335 Ret |= static_cast<uint16_t>(OMFSegDescFlags::Read);
336 if (Flags & COFF::IMAGE_SCN_MEM_WRITE)
337 Ret |= static_cast<uint16_t>(OMFSegDescFlags::Write);
338 if (Flags & COFF::IMAGE_SCN_MEM_EXECUTE)
339 Ret |= static_cast<uint16_t>(OMFSegDescFlags::Execute);
340 if (Flags & COFF::IMAGE_SCN_MEM_EXECUTE)
341 Ret |= static_cast<uint16_t>(OMFSegDescFlags::Execute);
342 if (!(Flags & COFF::IMAGE_SCN_MEM_16BIT))
343 Ret |= static_cast<uint16_t>(OMFSegDescFlags::AddressIs32Bit);
344
345 // This seems always 1.
346 Ret |= static_cast<uint16_t>(OMFSegDescFlags::IsSelector);
347
348 return Ret;
349}
350
351// A utility function to create a Section Map for a given list of COFF sections.
352//
353// A Section Map seem to be a copy of a COFF section list in other format.
354// I don't know why a PDB file contains both a COFF section header and
355// a Section Map, but it seems it must be present in a PDB.
356std::vector<SecMapEntry> DbiStreamBuilder::createSectionMap(
357 ArrayRef<llvm::object::coff_section> SecHdrs) {
358 std::vector<SecMapEntry> Ret;
359 int Idx = 0;
360
361 auto Add = [&]() -> SecMapEntry & {
362 Ret.emplace_back();
363 auto &Entry = Ret.back();
364 memset(&Entry, 0, sizeof(Entry));
365
366 Entry.Frame = Idx + 1;
367
368 // We don't know the meaning of these fields yet.
369 Entry.SecName = UINT16_MAX;
370 Entry.ClassName = UINT16_MAX;
371
372 return Entry;
373 };
374
375 for (auto &Hdr : SecHdrs) {
376 auto &Entry = Add();
377 Entry.Flags = toSecMapFlags(Hdr.Characteristics);
378 Entry.SecByteLength = Hdr.VirtualSize;
379 ++Idx;
380 }
381
382 // The last entry is for absolute symbols.
383 auto &Entry = Add();
384 Entry.Flags = static_cast<uint16_t>(OMFSegDescFlags::AddressIs32Bit) |
385 static_cast<uint16_t>(OMFSegDescFlags::IsAbsoluteAddress);
386 Entry.SecByteLength = UINT32_MAX;
387
388 return Ret;
389}
390
Zachary Turnera3225b02016-07-29 20:56:36 +0000391Error DbiStreamBuilder::commit(const msf::MSFLayout &Layout,
Zachary Turnerea4e6072017-03-15 22:18:53 +0000392 WritableBinaryStreamRef MsfBuffer) {
Rui Ueyamad1d8c8312016-08-03 23:43:23 +0000393 if (auto EC = finalize())
394 return EC;
395
Zachary Turner5b74ff32017-06-03 00:33:35 +0000396 auto DbiS = WritableMappedBlockStream::createIndexedStream(
397 Layout, MsfBuffer, StreamDBI, Allocator);
Zachary Turnerd66889c2016-07-28 19:12:28 +0000398
Zachary Turnerea4e6072017-03-15 22:18:53 +0000399 BinaryStreamWriter Writer(*DbiS);
Zachary Turnerd66889c2016-07-28 19:12:28 +0000400 if (auto EC = Writer.writeObject(*Header))
401 return EC;
402
Zachary Turnerea4e6072017-03-15 22:18:53 +0000403 for (auto &M : ModiList) {
404 if (auto EC = M->commit(Writer, Layout, MsfBuffer))
405 return EC;
406 }
Rui Ueyamaddc79222016-10-31 17:38:56 +0000407
Rui Ueyamaf7c9c322016-11-11 23:41:13 +0000408 if (!SectionContribs.empty()) {
Zachary Turner695ed562017-02-28 00:04:07 +0000409 if (auto EC = Writer.writeEnum(DbiSecContribVer60))
Rui Ueyamaf7c9c322016-11-11 23:41:13 +0000410 return EC;
Reid Kleckner8cbdd0c2017-06-13 15:49:13 +0000411 if (auto EC = Writer.writeArray(makeArrayRef(SectionContribs)))
Rui Ueyamaf7c9c322016-11-11 23:41:13 +0000412 return EC;
413 }
414
Rui Ueyamaddc79222016-10-31 17:38:56 +0000415 if (!SectionMap.empty()) {
416 ulittle16_t Size = static_cast<ulittle16_t>(SectionMap.size());
417 SecMapHeader SMHeader = {Size, Size};
418 if (auto EC = Writer.writeObject(SMHeader))
419 return EC;
420 if (auto EC = Writer.writeArray(SectionMap))
421 return EC;
422 }
423
Zachary Turnerd66889c2016-07-28 19:12:28 +0000424 if (auto EC = Writer.writeStreamRef(FileInfoBuffer))
425 return EC;
Rui Ueyamaddc79222016-10-31 17:38:56 +0000426
Zachary Turner6c4bfba2017-07-07 05:04:36 +0000427 if (auto EC = ECNamesBuilder.commit(Writer))
428 return EC;
429
Zachary Turnera6fb5362018-03-23 18:43:39 +0000430 for (auto &Stream : DbgStreams) {
431 uint16_t StreamNumber = kInvalidStreamIndex;
432 if (Stream.hasValue())
433 StreamNumber = Stream->StreamNumber;
434 if (auto EC = Writer.writeInteger(StreamNumber))
Rui Ueyamaf9904042016-10-11 19:43:12 +0000435 return EC;
Zachary Turnera6fb5362018-03-23 18:43:39 +0000436 }
Rui Ueyamaf9904042016-10-11 19:43:12 +0000437
438 for (auto &Stream : DbgStreams) {
Zachary Turnera6fb5362018-03-23 18:43:39 +0000439 if (!Stream.hasValue())
Rui Ueyamaf9904042016-10-11 19:43:12 +0000440 continue;
Zachary Turnera6fb5362018-03-23 18:43:39 +0000441 assert(Stream->StreamNumber != kInvalidStreamIndex);
442
Zachary Turner695ed562017-02-28 00:04:07 +0000443 auto WritableStream = WritableMappedBlockStream::createIndexedStream(
Zachary Turnera6fb5362018-03-23 18:43:39 +0000444 Layout, MsfBuffer, Stream->StreamNumber, Allocator);
Zachary Turner695ed562017-02-28 00:04:07 +0000445 BinaryStreamWriter DbgStreamWriter(*WritableStream);
Zachary Turner42e7cc1b2018-09-11 22:35:01 +0000446
447 if (auto EC = Stream->WriteFn(DbgStreamWriter))
Rui Ueyamaf9904042016-10-11 19:43:12 +0000448 return EC;
449 }
Zachary Turnerd66889c2016-07-28 19:12:28 +0000450
451 if (Writer.bytesRemaining() > 0)
452 return make_error<RawError>(raw_error_code::invalid_format,
453 "Unexpected bytes found in DBI Stream");
454 return Error::success();
455}