1 //===- GlobalsStream.h - PDB Index of Symbols by Name -----------*- 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_GLOBALSSTREAM_H 10 #define LLVM_DEBUGINFO_PDB_NATIVE_GLOBALSSTREAM_H 11 12 #include "llvm/ADT/iterator.h" 13 #include "llvm/DebugInfo/CodeView/CVRecord.h" 14 #include "llvm/DebugInfo/PDB/Native/RawTypes.h" 15 #include "llvm/Support/BinaryStreamArray.h" 16 #include "llvm/Support/Compiler.h" 17 #include "llvm/Support/Endian.h" 18 #include "llvm/Support/Error.h" 19 20 namespace llvm { 21 class BinaryStreamReader; 22 namespace msf { 23 class MappedBlockStream; 24 } 25 namespace pdb { 26 class SymbolStream; 27 28 /// Iterator over hash records producing symbol record offsets. Abstracts away 29 /// the fact that symbol record offsets on disk are off-by-one. 30 class GSIHashIterator 31 : public iterator_adaptor_base< 32 GSIHashIterator, FixedStreamArrayIterator<PSHashRecord>, 33 std::random_access_iterator_tag, const uint32_t> { 34 public: 35 template <typename T> GSIHashIterator(T && v)36 GSIHashIterator(T &&v) 37 : GSIHashIterator::iterator_adaptor_base(std::forward<T &&>(v)) {} 38 39 uint32_t operator*() const { 40 uint32_t Off = this->I->Off; 41 return --Off; 42 } 43 }; 44 45 /// From https://github.com/Microsoft/microsoft-pdb/blob/master/PDB/dbi/gsi.cpp 46 enum : unsigned { IPHR_HASH = 4096 }; 47 48 /// A readonly view of a hash table used in the globals and publics streams. 49 /// Most clients will only want to iterate this to get symbol record offsets 50 /// into the PDB symbol stream. 51 class GSIHashTable { 52 public: 53 const GSIHashHeader *HashHdr; 54 FixedStreamArray<PSHashRecord> HashRecords; 55 FixedStreamArray<support::ulittle32_t> HashBitmap; 56 FixedStreamArray<support::ulittle32_t> HashBuckets; 57 std::array<int32_t, IPHR_HASH + 1> BucketMap; 58 59 LLVM_ABI Error read(BinaryStreamReader &Reader); 60 getVerSignature()61 uint32_t getVerSignature() const { return HashHdr->VerSignature; } getVerHeader()62 uint32_t getVerHeader() const { return HashHdr->VerHdr; } getHashRecordSize()63 uint32_t getHashRecordSize() const { return HashHdr->HrSize; } getNumBuckets()64 uint32_t getNumBuckets() const { return HashHdr->NumBuckets; } 65 66 typedef GSIHashHeader iterator; begin()67 GSIHashIterator begin() const { return GSIHashIterator(HashRecords.begin()); } end()68 GSIHashIterator end() const { return GSIHashIterator(HashRecords.end()); } 69 }; 70 71 class GlobalsStream { 72 public: 73 LLVM_ABI explicit GlobalsStream( 74 std::unique_ptr<msf::MappedBlockStream> Stream); 75 LLVM_ABI ~GlobalsStream(); getGlobalsTable()76 const GSIHashTable &getGlobalsTable() const { return GlobalsTable; } 77 LLVM_ABI Error reload(); 78 79 LLVM_ABI std::vector<std::pair<uint32_t, codeview::CVSymbol>> 80 findRecordsByName(StringRef Name, const SymbolStream &Symbols) const; 81 82 private: 83 GSIHashTable GlobalsTable; 84 std::unique_ptr<msf::MappedBlockStream> Stream; 85 }; 86 } // namespace pdb 87 } 88 89 #endif 90