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