blob: 54d6835f11215691fc846233625015c2ded680e9 [file] [log] [blame]
Zachary Turnerdbeaea72016-07-11 21:45:26 +00001//===- InfoStreamBuilder.cpp - PDB Info 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/InfoStreamBuilder.h"
Zachary Turnerdbeaea72016-07-11 21:45:26 +000011
Zachary Turner620961d2016-09-14 23:00:02 +000012#include "llvm/DebugInfo/MSF/MSFBuilder.h"
Zachary Turnera3225b02016-07-29 20:56:36 +000013#include "llvm/DebugInfo/MSF/MappedBlockStream.h"
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +000014#include "llvm/DebugInfo/PDB/Native/InfoStream.h"
15#include "llvm/DebugInfo/PDB/Native/NamedStreamMap.h"
16#include "llvm/DebugInfo/PDB/Native/PDBFileBuilder.h"
17#include "llvm/DebugInfo/PDB/Native/RawError.h"
18#include "llvm/DebugInfo/PDB/Native/RawTypes.h"
Zachary Turnerd9dc2822017-03-02 20:52:51 +000019#include "llvm/Support/BinaryStreamWriter.h"
Zachary Turnerdbeaea72016-07-11 21:45:26 +000020
21using namespace llvm;
22using namespace llvm::codeview;
Zachary Turnerbac69d32016-07-22 19:56:05 +000023using namespace llvm::msf;
Zachary Turnerdbeaea72016-07-11 21:45:26 +000024using namespace llvm::pdb;
25
Zachary Turner760ad4d2017-01-20 22:42:09 +000026InfoStreamBuilder::InfoStreamBuilder(msf::MSFBuilder &Msf,
27 NamedStreamMap &NamedStreams)
Zachary Turnerc6a75a62018-03-01 18:00:29 +000028 : Msf(Msf), Ver(PdbRaw_ImplVer::PdbImplVC70), Age(0),
29 NamedStreams(NamedStreams) {
30 ::memset(&Guid, 0, sizeof(Guid));
31}
Zachary Turnerdbeaea72016-07-11 21:45:26 +000032
33void InfoStreamBuilder::setVersion(PdbRaw_ImplVer V) { Ver = V; }
34
Zachary Turnerdbeaea72016-07-11 21:45:26 +000035void InfoStreamBuilder::setAge(uint32_t A) { Age = A; }
36
Zachary Turnerc6a75a62018-03-01 18:00:29 +000037void InfoStreamBuilder::setSignature(uint32_t S) { Signature = S; }
38
Reid Kleckner67653ee2017-07-17 23:59:44 +000039void InfoStreamBuilder::setGuid(GUID G) { Guid = G; }
Zachary Turnerdbeaea72016-07-11 21:45:26 +000040
Zachary Turner05d5e612017-03-16 20:19:11 +000041void InfoStreamBuilder::addFeature(PdbRaw_FeatureSig Sig) {
42 Features.push_back(Sig);
43}
44
Zachary Turner620961d2016-09-14 23:00:02 +000045Error InfoStreamBuilder::finalizeMsfLayout() {
Zachary Turnercafd4762018-02-16 20:46:04 +000046 uint32_t Length = sizeof(InfoStreamHeader) +
47 NamedStreams.calculateSerializedLength() +
Zachary Turner05d5e612017-03-16 20:19:11 +000048 (Features.size() + 1) * sizeof(uint32_t);
Zachary Turner620961d2016-09-14 23:00:02 +000049 if (auto EC = Msf.setStreamSize(StreamPDB, Length))
50 return EC;
51 return Error::success();
52}
53
Zachary Turnera3225b02016-07-29 20:56:36 +000054Error InfoStreamBuilder::commit(const msf::MSFLayout &Layout,
Zachary Turner120faca2017-02-27 22:11:43 +000055 WritableBinaryStreamRef Buffer) const {
Zachary Turner5b74ff32017-06-03 00:33:35 +000056 auto InfoS = WritableMappedBlockStream::createIndexedStream(
57 Layout, Buffer, StreamPDB, Msf.getAllocator());
Zachary Turner120faca2017-02-27 22:11:43 +000058 BinaryStreamWriter Writer(*InfoS);
Zachary Turnerd66889c2016-07-28 19:12:28 +000059
60 InfoStreamHeader H;
Zachary Turnerc6a75a62018-03-01 18:00:29 +000061 // Leave the build id fields 0 so they can be set as the last step before
62 // committing the file to disk.
63 ::memset(&H, 0, sizeof(H));
Zachary Turnerd66889c2016-07-28 19:12:28 +000064 H.Version = Ver;
Zachary Turnerd66889c2016-07-28 19:12:28 +000065 if (auto EC = Writer.writeObject(H))
66 return EC;
67
Zachary Turner05d5e612017-03-16 20:19:11 +000068 if (auto EC = NamedStreams.commit(Writer))
69 return EC;
70 if (auto EC = Writer.writeInteger(0))
71 return EC;
72 for (auto E : Features) {
73 if (auto EC = Writer.writeEnum(E))
74 return EC;
75 }
Zachary Turnera6fb5362018-03-23 18:43:39 +000076 assert(Writer.bytesRemaining() == 0);
Zachary Turner05d5e612017-03-16 20:19:11 +000077 return Error::success();
Zachary Turnerd66889c2016-07-28 19:12:28 +000078}