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