LLVM  16.0.0git
GSIStreamBuilder.h
Go to the documentation of this file.
1 //===- GSIStreamBuilder.h - PDB Publics/Globals 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 #ifndef LLVM_DEBUGINFO_PDB_NATIVE_GSISTREAMBUILDER_H
10 #define LLVM_DEBUGINFO_PDB_NATIVE_GSISTREAMBUILDER_H
11 
12 #include "llvm/ADT/DenseSet.h"
18 #include "llvm/Support/Error.h"
19 
20 namespace llvm {
21 namespace codeview {
22 class ConstantSym;
23 class DataSym;
24 class ProcRefSym;
25 } // namespace codeview
26 template <typename T> struct BinaryItemTraits;
27 
28 template <> struct BinaryItemTraits<codeview::CVSymbol> {
29  static size_t length(const codeview::CVSymbol &Item) {
30  return Item.RecordData.size();
31  }
33  return Item.RecordData;
34  }
35 };
36 
37 namespace msf {
38 class MSFBuilder;
39 struct MSFLayout;
40 } // namespace msf
41 namespace pdb {
42 struct GSIHashStreamBuilder;
43 struct BulkPublic;
44 struct SymbolDenseMapInfo;
45 
47 
48 public:
49  explicit GSIStreamBuilder(msf::MSFBuilder &Msf);
51 
52  GSIStreamBuilder(const GSIStreamBuilder &) = delete;
53  GSIStreamBuilder &operator=(const GSIStreamBuilder &) = delete;
54 
56 
57  Error commit(const msf::MSFLayout &Layout, WritableBinaryStreamRef Buffer);
58 
59  uint32_t getPublicsStreamIndex() const { return PublicsStreamIndex; }
60  uint32_t getGlobalsStreamIndex() const { return GlobalsStreamIndex; }
61  uint32_t getRecordStreamIndex() const { return RecordStreamIndex; }
62 
63  // Add public symbols in bulk.
64  void addPublicSymbols(std::vector<BulkPublic> &&PublicsIn);
65 
66  void addGlobalSymbol(const codeview::ProcRefSym &Sym);
67  void addGlobalSymbol(const codeview::DataSym &Sym);
68  void addGlobalSymbol(const codeview::ConstantSym &Sym);
69 
70  // Add a pre-serialized global symbol record. The caller must ensure that the
71  // symbol data remains alive until the global stream is committed to disk.
72  void addGlobalSymbol(const codeview::CVSymbol &Sym);
73 
74 private:
75  void finalizePublicBuckets();
76  void finalizeGlobalBuckets(uint32_t RecordZeroOffset);
77 
78  template <typename T> void serializeAndAddGlobal(const T &Symbol);
79 
80  uint32_t calculatePublicsHashStreamSize() const;
81  uint32_t calculateGlobalsHashStreamSize() const;
82  Error commitSymbolRecordStream(WritableBinaryStreamRef Stream);
83  Error commitPublicsHashStream(WritableBinaryStreamRef Stream);
84  Error commitGlobalsHashStream(WritableBinaryStreamRef Stream);
85 
86  uint32_t PublicsStreamIndex = kInvalidStreamIndex;
87  uint32_t GlobalsStreamIndex = kInvalidStreamIndex;
88  uint32_t RecordStreamIndex = kInvalidStreamIndex;
89  msf::MSFBuilder &Msf;
90  std::unique_ptr<GSIHashStreamBuilder> PSH;
91  std::unique_ptr<GSIHashStreamBuilder> GSH;
92 
93  // List of all of the public records. These are stored unserialized so that we
94  // can defer copying the names until we are ready to commit the PDB.
95  std::vector<BulkPublic> Publics;
96 
97  // List of all of the global records.
98  std::vector<codeview::CVSymbol> Globals;
99 
100  // Hash table for deduplicating global typedef and constant records. Only used
101  // for globals.
103 };
104 
105 /// This struct is equivalent to codeview::PublicSym32, but it has been
106 /// optimized for size to speed up bulk serialization and sorting operations
107 /// during PDB writing.
108 struct BulkPublic {
109  BulkPublic() : Flags(0), BucketIdx(0) {}
110 
111  const char *Name = nullptr;
113 
114  // Offset of the symbol record in the publics stream.
116 
117  // Section offset of the symbol in the image.
119 
120  // Section index of the section containing the symbol.
122 
123  // PublicSymFlags.
125 
126  // GSI hash table bucket index. The maximum value is IPHR_HASH.
128  static_assert(IPHR_HASH <= 1 << 12, "bitfield too small");
129 
131  Flags = uint32_t(F);
132  assert(Flags == uint32_t(F) && "truncated");
133  }
134 
136  assert(B < IPHR_HASH);
137  BucketIdx = B;
138  }
139 
140  StringRef getName() const { return StringRef(Name, NameLen); }
141 };
142 
143 static_assert(sizeof(BulkPublic) <= 24, "unexpected size increase");
144 static_assert(std::is_trivially_copyable<BulkPublic>::value,
145  "should be trivial");
146 
147 } // namespace pdb
148 } // namespace llvm
149 
150 #endif
llvm::pdb::BulkPublic::Offset
uint32_t Offset
Definition: GSIStreamBuilder.h:118
RawConstants.h
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
llvm::pdb::BulkPublic::setFlags
void setFlags(codeview::PublicSymFlags F)
Definition: GSIStreamBuilder.h:130
llvm::pdb::BulkPublic::Name
const char * Name
Definition: GSIStreamBuilder.h:111
llvm::BinaryItemTraits< codeview::CVSymbol >::length
static size_t length(const codeview::CVSymbol &Item)
Definition: GSIStreamBuilder.h:29
llvm::pdb::GSIStreamBuilder::getGlobalsStreamIndex
uint32_t getGlobalsStreamIndex() const
Definition: GSIStreamBuilder.h:60
llvm::pdb::GSIStreamBuilder::getPublicsStreamIndex
uint32_t getPublicsStreamIndex() const
Definition: GSIStreamBuilder.h:59
llvm::codeview::DataSym
Definition: SymbolRecord.h:936
llvm::pdb::kInvalidStreamIndex
const uint16_t kInvalidStreamIndex
Definition: RawConstants.h:19
Error.h
llvm::pdb::GSIStreamBuilder::~GSIStreamBuilder
~GSIStreamBuilder()
llvm::codeview::CVRecord::RecordData
ArrayRef< uint8_t > RecordData
Definition: CVRecord.h:60
llvm::msf::MSFBuilder
Definition: MSFBuilder.h:27
llvm::BinaryItemTraits< codeview::CVSymbol >::bytes
static ArrayRef< uint8_t > bytes(const codeview::CVSymbol &Item)
Definition: GSIStreamBuilder.h:32
llvm::pdb::BulkPublic::SymOffset
uint32_t SymOffset
Definition: GSIStreamBuilder.h:115
F
#define F(x, y, z)
Definition: MD5.cpp:55
llvm::pdb::BulkPublic::getName
StringRef getName() const
Definition: GSIStreamBuilder.h:140
llvm::pdb::GSIStreamBuilder::getRecordStreamIndex
uint32_t getRecordStreamIndex() const
Definition: GSIStreamBuilder.h:61
llvm::codeview::ConstantSym
Definition: SymbolRecord.h:921
GlobalsStream.h
llvm::msf::MSFLayout
Definition: MSFCommon.h:51
llvm::codeview::CVSymbol
CVRecord< SymbolKind > CVSymbol
Definition: CVRecord.h:65
DenseSet.h
B
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
llvm::pdb::GSIStreamBuilder::addGlobalSymbol
void addGlobalSymbol(const codeview::ProcRefSym &Sym)
Definition: GSIStreamBuilder.cpp:351
CodeView.h
llvm::DenseSet
Implements a dense probed hash-table based set.
Definition: DenseSet.h:268
llvm::pdb::GSIStreamBuilder
Definition: GSIStreamBuilder.h:46
llvm::pdb::BulkPublic
This struct is equivalent to codeview::PublicSym32, but it has been optimized for size to speed up bu...
Definition: GSIStreamBuilder.h:108
llvm::codeview::PublicSymFlags
PublicSymFlags
Corresponds to the CV_PUBSYMFLAGS bitfield.
Definition: CodeView.h:423
llvm::pdb::BulkPublic::BulkPublic
BulkPublic()
Definition: GSIStreamBuilder.h:109
llvm::pdb::BulkPublic::Segment
uint16_t Segment
Definition: GSIStreamBuilder.h:121
llvm::pdb::GSIStreamBuilder::finalizeMsfLayout
Error finalizeMsfLayout()
Definition: GSIStreamBuilder.cpp:306
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::pdb::BulkPublic::NameLen
uint32_t NameLen
Definition: GSIStreamBuilder.h:112
llvm::pdb::GSIStreamBuilder::commit
Error commit(const msf::MSFLayout &Layout, WritableBinaryStreamRef Buffer)
Definition: GSIStreamBuilder.cpp:479
llvm::pdb::BulkPublic::setBucketIdx
void setBucketIdx(uint16_t B)
Definition: GSIStreamBuilder.h:135
llvm::pdb::GSIStreamBuilder::addPublicSymbols
void addPublicSymbols(std::vector< BulkPublic > &&PublicsIn)
Definition: GSIStreamBuilder.cpp:330
llvm::pdb::BulkPublic::BucketIdx
uint16_t BucketIdx
Definition: GSIStreamBuilder.h:127
llvm::pdb::GSIStreamBuilder::GSIStreamBuilder
GSIStreamBuilder(msf::MSFBuilder &Msf)
Definition: GSIStreamBuilder.cpp:286
llvm::ArrayRef< uint8_t >
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
BinaryStreamRef.h
llvm::codeview::CVRecord
CVRecord is a fat pointer (base + size pair) to a symbol or type record.
Definition: CVRecord.h:29
uint32_t
uint16_t
llvm::Error
Lightweight error class with error context and mandatory checking.
Definition: Error.h:155
CVRecord.h
llvm::pdb::GSIStreamBuilder::operator=
GSIStreamBuilder & operator=(const GSIStreamBuilder &)=delete
llvm::pdb::IPHR_HASH
@ IPHR_HASH
Definition: GlobalsStream.h:45
llvm::ArrayRef::size
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:164
llvm::pdb::BulkPublic::Flags
uint16_t Flags
Definition: GSIStreamBuilder.h:124
llvm::codeview::ProcRefSym
Definition: SymbolRecord.h:393
llvm::BinaryItemTraits
Definition: GSIStreamBuilder.h:26
llvm::WritableBinaryStreamRef
Definition: BinaryStreamRef.h:220