1//===- InfoStreamBuilder.cpp - PDB Info Stream Creation ---------*- C++ -*-===//
2//
3// 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
6//
7//===----------------------------------------------------------------------===//
8
9#include "llvm/DebugInfo/PDB/Native/InfoStreamBuilder.h"
10
11#include "llvm/DebugInfo/MSF/MSFBuilder.h"
12#include "llvm/DebugInfo/MSF/MappedBlockStream.h"
13#include "llvm/DebugInfo/PDB/Native/NamedStreamMap.h"
14#include "llvm/DebugInfo/PDB/Native/RawTypes.h"
15#include "llvm/Support/BinaryStreamWriter.h"
16#include "llvm/Support/TimeProfiler.h"
17
18using namespace llvm;
19using namespace llvm::codeview;
20using namespace llvm::msf;
21using namespace llvm::pdb;
22
23InfoStreamBuilder::InfoStreamBuilder(msf::MSFBuilder &Msf,
24 NamedStreamMap &NamedStreams)
25 : Msf(Msf), Ver(PdbRaw_ImplVer::PdbImplVC70), Age(0),
26 NamedStreams(NamedStreams) {
27 ::memset(s: &Guid, c: 0, n: sizeof(Guid));
28}
29
30void InfoStreamBuilder::setVersion(PdbRaw_ImplVer V) { Ver = V; }
31
32void InfoStreamBuilder::addFeature(PdbRaw_FeatureSig Sig) {
33 Features.push_back(x: Sig);
34}
35
36void InfoStreamBuilder::setHashPDBContentsToGUID(bool B) {
37 HashPDBContentsToGUID = B;
38}
39
40void InfoStreamBuilder::setAge(uint32_t A) { Age = A; }
41
42void InfoStreamBuilder::setSignature(uint32_t S) { Signature = S; }
43
44void InfoStreamBuilder::setGuid(GUID G) { Guid = G; }
45
46
47Error InfoStreamBuilder::finalizeMsfLayout() {
48 uint32_t Length = sizeof(InfoStreamHeader) +
49 NamedStreams.calculateSerializedLength() +
50 (Features.size() + 1) * sizeof(uint32_t);
51 if (auto EC = Msf.setStreamSize(Idx: StreamPDB, Size: Length))
52 return EC;
53 return Error::success();
54}
55
56Error InfoStreamBuilder::commit(const msf::MSFLayout &Layout,
57 WritableBinaryStreamRef Buffer) const {
58 llvm::TimeTraceScope timeScope("Commit info stream");
59 auto InfoS = WritableMappedBlockStream::createIndexedStream(
60 Layout, MsfData: Buffer, StreamIndex: StreamPDB, Allocator&: Msf.getAllocator());
61 BinaryStreamWriter Writer(*InfoS);
62
63 InfoStreamHeader H;
64 // Leave the build id fields 0 so they can be set as the last step before
65 // committing the file to disk.
66 ::memset(s: &H, c: 0, n: sizeof(H));
67 H.Version = Ver;
68 if (auto EC = Writer.writeObject(Obj: H))
69 return EC;
70
71 if (auto EC = NamedStreams.commit(Writer))
72 return EC;
73 if (auto EC = Writer.writeInteger(Value: 0))
74 return EC;
75 for (auto E : Features) {
76 if (auto EC = Writer.writeEnum(Num: E))
77 return EC;
78 }
79 assert(Writer.bytesRemaining() == 0);
80 return Error::success();
81}
82