blob: 3b5a2accdba6506ace55cc906db49516586e1640 [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
Nico Weber205ca682018-09-15 18:35:51 +000035void InfoStreamBuilder::addFeature(PdbRaw_FeatureSig Sig) {
36 Features.push_back(Sig);
37}
38
39void InfoStreamBuilder::setHashPDBContentsToGUID(bool B) {
40 HashPDBContentsToGUID = B;
41}
42
Zachary Turnerdbeaea72016-07-11 21:45:26 +000043void InfoStreamBuilder::setAge(uint32_t A) { Age = A; }
44
Zachary Turnerc6a75a62018-03-01 18:00:29 +000045void InfoStreamBuilder::setSignature(uint32_t S) { Signature = S; }
46
Reid Kleckner67653ee2017-07-17 23:59:44 +000047void InfoStreamBuilder::setGuid(GUID G) { Guid = G; }
Zachary Turnerdbeaea72016-07-11 21:45:26 +000048
Zachary Turner05d5e612017-03-16 20:19:11 +000049
Zachary Turner620961d2016-09-14 23:00:02 +000050Error InfoStreamBuilder::finalizeMsfLayout() {
Zachary Turnercafd4762018-02-16 20:46:04 +000051 uint32_t Length = sizeof(InfoStreamHeader) +
52 NamedStreams.calculateSerializedLength() +
Zachary Turner05d5e612017-03-16 20:19:11 +000053 (Features.size() + 1) * sizeof(uint32_t);
Zachary Turner620961d2016-09-14 23:00:02 +000054 if (auto EC = Msf.setStreamSize(StreamPDB, Length))
55 return EC;
56 return Error::success();
57}
58
Zachary Turnera3225b02016-07-29 20:56:36 +000059Error InfoStreamBuilder::commit(const msf::MSFLayout &Layout,
Zachary Turner120faca2017-02-27 22:11:43 +000060 WritableBinaryStreamRef Buffer) const {
Zachary Turner5b74ff32017-06-03 00:33:35 +000061 auto InfoS = WritableMappedBlockStream::createIndexedStream(
62 Layout, Buffer, StreamPDB, Msf.getAllocator());
Zachary Turner120faca2017-02-27 22:11:43 +000063 BinaryStreamWriter Writer(*InfoS);
Zachary Turnerd66889c2016-07-28 19:12:28 +000064
65 InfoStreamHeader H;
Zachary Turnerc6a75a62018-03-01 18:00:29 +000066 // Leave the build id fields 0 so they can be set as the last step before
67 // committing the file to disk.
68 ::memset(&H, 0, sizeof(H));
Zachary Turnerd66889c2016-07-28 19:12:28 +000069 H.Version = Ver;
Zachary Turnerd66889c2016-07-28 19:12:28 +000070 if (auto EC = Writer.writeObject(H))
71 return EC;
72
Zachary Turner05d5e612017-03-16 20:19:11 +000073 if (auto EC = NamedStreams.commit(Writer))
74 return EC;
75 if (auto EC = Writer.writeInteger(0))
76 return EC;
77 for (auto E : Features) {
78 if (auto EC = Writer.writeEnum(E))
79 return EC;
80 }
Zachary Turnera6fb5362018-03-23 18:43:39 +000081 assert(Writer.bytesRemaining() == 0);
Zachary Turner05d5e612017-03-16 20:19:11 +000082 return Error::success();
Zachary Turnerd66889c2016-07-28 19:12:28 +000083}