1317017Sdim//===- InfoStreamBuilder.cpp - PDB Info Stream Creation ---------*- C++ -*-===//
2317017Sdim//
3353358Sdim// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4353358Sdim// See https://llvm.org/LICENSE.txt for license information.
5353358Sdim// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6317017Sdim//
7317017Sdim//===----------------------------------------------------------------------===//
8317017Sdim
9317017Sdim#include "llvm/DebugInfo/PDB/Native/InfoStreamBuilder.h"
10317017Sdim
11317017Sdim#include "llvm/DebugInfo/MSF/MSFBuilder.h"
12317017Sdim#include "llvm/DebugInfo/MSF/MappedBlockStream.h"
13317017Sdim#include "llvm/DebugInfo/PDB/Native/InfoStream.h"
14317017Sdim#include "llvm/DebugInfo/PDB/Native/NamedStreamMap.h"
15317017Sdim#include "llvm/DebugInfo/PDB/Native/PDBFileBuilder.h"
16317017Sdim#include "llvm/DebugInfo/PDB/Native/RawError.h"
17317017Sdim#include "llvm/DebugInfo/PDB/Native/RawTypes.h"
18317017Sdim#include "llvm/Support/BinaryStreamWriter.h"
19317017Sdim
20317017Sdimusing namespace llvm;
21317017Sdimusing namespace llvm::codeview;
22317017Sdimusing namespace llvm::msf;
23317017Sdimusing namespace llvm::pdb;
24317017Sdim
25317017SdimInfoStreamBuilder::InfoStreamBuilder(msf::MSFBuilder &Msf,
26317017Sdim                                     NamedStreamMap &NamedStreams)
27341825Sdim    : Msf(Msf), Ver(PdbRaw_ImplVer::PdbImplVC70), Age(0),
28341825Sdim      NamedStreams(NamedStreams) {
29341825Sdim  ::memset(&Guid, 0, sizeof(Guid));
30341825Sdim}
31317017Sdim
32317017Sdimvoid InfoStreamBuilder::setVersion(PdbRaw_ImplVer V) { Ver = V; }
33317017Sdim
34344779Sdimvoid InfoStreamBuilder::addFeature(PdbRaw_FeatureSig Sig) {
35344779Sdim  Features.push_back(Sig);
36344779Sdim}
37344779Sdim
38344779Sdimvoid InfoStreamBuilder::setHashPDBContentsToGUID(bool B) {
39344779Sdim  HashPDBContentsToGUID = B;
40344779Sdim}
41344779Sdim
42317017Sdimvoid InfoStreamBuilder::setAge(uint32_t A) { Age = A; }
43317017Sdim
44341825Sdimvoid InfoStreamBuilder::setSignature(uint32_t S) { Signature = S; }
45341825Sdim
46321238Sdimvoid InfoStreamBuilder::setGuid(GUID G) { Guid = G; }
47317017Sdim
48317017Sdim
49317017SdimError InfoStreamBuilder::finalizeMsfLayout() {
50341825Sdim  uint32_t Length = sizeof(InfoStreamHeader) +
51341825Sdim                    NamedStreams.calculateSerializedLength() +
52317017Sdim                    (Features.size() + 1) * sizeof(uint32_t);
53317017Sdim  if (auto EC = Msf.setStreamSize(StreamPDB, Length))
54317017Sdim    return EC;
55317017Sdim  return Error::success();
56317017Sdim}
57317017Sdim
58317017SdimError InfoStreamBuilder::commit(const msf::MSFLayout &Layout,
59317017Sdim                                WritableBinaryStreamRef Buffer) const {
60319547Sdim  auto InfoS = WritableMappedBlockStream::createIndexedStream(
61319547Sdim      Layout, Buffer, StreamPDB, Msf.getAllocator());
62317017Sdim  BinaryStreamWriter Writer(*InfoS);
63317017Sdim
64317017Sdim  InfoStreamHeader H;
65341825Sdim  // Leave the build id fields 0 so they can be set as the last step before
66341825Sdim  // committing the file to disk.
67341825Sdim  ::memset(&H, 0, sizeof(H));
68317017Sdim  H.Version = Ver;
69317017Sdim  if (auto EC = Writer.writeObject(H))
70317017Sdim    return EC;
71317017Sdim
72317017Sdim  if (auto EC = NamedStreams.commit(Writer))
73317017Sdim    return EC;
74317017Sdim  if (auto EC = Writer.writeInteger(0))
75317017Sdim    return EC;
76317017Sdim  for (auto E : Features) {
77317017Sdim    if (auto EC = Writer.writeEnum(E))
78317017Sdim      return EC;
79317017Sdim  }
80341825Sdim  assert(Writer.bytesRemaining() == 0);
81317017Sdim  return Error::success();
82317017Sdim}
83