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