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