LLVM 20.0.0git
TpiStream.cpp
Go to the documentation of this file.
1//===- TpiStream.cpp - PDB Type Info (TPI) Stream 2 Access ----------------===//
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
10
24#include "llvm/Support/Endian.h"
25#include "llvm/Support/Error.h"
26#include <cstdint>
27#include <vector>
28
29using namespace llvm;
30using namespace llvm::codeview;
31using namespace llvm::support;
32using namespace llvm::msf;
33using namespace llvm::pdb;
34
35TpiStream::TpiStream(PDBFile &File, std::unique_ptr<MappedBlockStream> Stream)
36 : Pdb(File), Stream(std::move(Stream)) {}
37
38TpiStream::~TpiStream() = default;
39
41 BinaryStreamReader Reader(*Stream);
42
43 if (Reader.bytesRemaining() < sizeof(TpiStreamHeader))
44 return make_error<RawError>(raw_error_code::corrupt_file,
45 "TPI Stream does not contain a header.");
46
47 if (Reader.readObject(Header))
48 return make_error<RawError>(raw_error_code::corrupt_file,
49 "TPI Stream does not contain a header.");
50
51 if (Header->Version != PdbTpiV80)
52 return make_error<RawError>(raw_error_code::corrupt_file,
53 "Unsupported TPI Version.");
54
55 if (Header->HeaderSize != sizeof(TpiStreamHeader))
56 return make_error<RawError>(raw_error_code::corrupt_file,
57 "Corrupt TPI Header size.");
58
59 if (Header->HashKeySize != sizeof(ulittle32_t))
60 return make_error<RawError>(raw_error_code::corrupt_file,
61 "TPI Stream expected 4 byte hash key size.");
62
63 if (Header->NumHashBuckets < MinTpiHashBuckets ||
65 return make_error<RawError>(raw_error_code::corrupt_file,
66 "TPI Stream Invalid number of hash buckets.");
67
68 // The actual type records themselves come from this stream
69 if (auto EC =
70 Reader.readSubstream(TypeRecordsSubstream, Header->TypeRecordBytes))
71 return EC;
72
73 BinaryStreamReader RecordReader(TypeRecordsSubstream.StreamData);
74 if (auto EC =
75 RecordReader.readArray(TypeRecords, TypeRecordsSubstream.size()))
76 return EC;
77
78 // Hash indices, hash values, etc come from the hash stream.
79 if (Header->HashStreamIndex != kInvalidStreamIndex) {
80 auto HS = Pdb.safelyCreateIndexedStream(Header->HashStreamIndex);
81 if (!HS) {
82 consumeError(HS.takeError());
83 return make_error<RawError>(raw_error_code::corrupt_file,
84 "Invalid TPI hash stream index.");
85 }
86 BinaryStreamReader HSR(**HS);
87
88 // There should be a hash value for every type record, or no hashes at all.
89 uint32_t NumHashValues =
90 Header->HashValueBuffer.Length / sizeof(ulittle32_t);
91 if (NumHashValues != getNumTypeRecords() && NumHashValues != 0)
92 return make_error<RawError>(
94 "TPI hash count does not match with the number of type records.");
95 HSR.setOffset(Header->HashValueBuffer.Off);
96 if (auto EC = HSR.readArray(HashValues, NumHashValues))
97 return EC;
98
99 HSR.setOffset(Header->IndexOffsetBuffer.Off);
100 uint32_t NumTypeIndexOffsets =
101 Header->IndexOffsetBuffer.Length / sizeof(TypeIndexOffset);
102 if (auto EC = HSR.readArray(TypeIndexOffsets, NumTypeIndexOffsets))
103 return EC;
104
105 if (Header->HashAdjBuffer.Length > 0) {
106 HSR.setOffset(Header->HashAdjBuffer.Off);
107 if (auto EC = HashAdjusters.load(HSR))
108 return EC;
109 }
110
111 HashStream = std::move(*HS);
112 }
113
114 Types = std::make_unique<LazyRandomTypeCollection>(
115 TypeRecords, getNumTypeRecords(), getTypeIndexOffsets());
116 return Error::success();
117}
118
120 uint32_t Value = Header->Version;
121 return static_cast<PdbRaw_TpiVer>(Value);
122}
123
125
127
129 return TypeIndexEnd() - TypeIndexBegin();
130}
131
133 return Header->HashStreamIndex;
134}
135
137 return Header->HashAuxStreamIndex;
138}
139
142
144 if (!HashMap.empty())
145 return;
146 if (HashValues.empty())
147 return;
148
149 HashMap.resize(Header->NumHashBuckets);
150
151 TypeIndex TIB{Header->TypeIndexBegin};
152 TypeIndex TIE{Header->TypeIndexEnd};
153 while (TIB < TIE) {
154 uint32_t HV = HashValues[TIB.toArrayIndex()];
155 HashMap[HV].push_back(TIB++);
156 }
157}
158
159std::vector<TypeIndex> TpiStream::findRecordsByName(StringRef Name) const {
160 if (!supportsTypeLookup())
161 const_cast<TpiStream*>(this)->buildHashMap();
162
163 uint32_t Bucket = hashStringV1(Name) % Header->NumHashBuckets;
164 if (Bucket > HashMap.size())
165 return {};
166
167 std::vector<TypeIndex> Result;
168 for (TypeIndex TI : HashMap[Bucket]) {
169 std::string ThisName = computeTypeName(*Types, TI);
170 if (ThisName == Name)
171 Result.push_back(TI);
172 }
173 return Result;
174}
175
176bool TpiStream::supportsTypeLookup() const { return !HashMap.empty(); }
177
180 if (!supportsTypeLookup())
181 const_cast<TpiStream*>(this)->buildHashMap();
182
183 CVType F = Types->getType(ForwardRefTI);
184 if (!isUdtForwardRef(F))
185 return ForwardRefTI;
186
188 if (!ForwardTRH)
189 return ForwardTRH.takeError();
190
191 uint32_t BucketIdx = ForwardTRH->FullRecordHash % Header->NumHashBuckets;
192
193 for (TypeIndex TI : HashMap[BucketIdx]) {
194 CVType CVT = Types->getType(TI);
195 if (CVT.kind() != F.kind())
196 continue;
197
199 if (!FullTRH)
200 return FullTRH.takeError();
201 if (ForwardTRH->FullRecordHash != FullTRH->FullRecordHash)
202 continue;
203 TagRecord &ForwardTR = ForwardTRH->getRecord();
204 TagRecord &FullTR = FullTRH->getRecord();
205
206 if (!ForwardTR.hasUniqueName()) {
207 if (ForwardTR.getName() == FullTR.getName())
208 return TI;
209 continue;
210 }
211
212 if (!FullTR.hasUniqueName())
213 continue;
214 if (ForwardTR.getUniqueName() == FullTR.getUniqueName())
215 return TI;
216 }
217 return ForwardRefTI;
218}
219
221 assert(!Index.isSimple());
222 return Types->getType(Index);
223}
224
226 return TypeRecordsSubstream;
227}
228
230 return HashValues;
231}
232
234 return TypeIndexOffsets;
235}
236
238 return HashAdjusters;
239}
240
241CVTypeRange TpiStream::types(bool *HadError) const {
242 return make_range(TypeRecords.begin(HadError), TypeRecords.end());
243}
244
std::string Name
#define F(x, y, z)
Definition: MD5.cpp:55
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
Provides read only access to a subclass of BinaryStream.
Error readObject(const T *&Dest)
Get a pointer to an object of type T from the underlying stream, as if by memcpy, and store the resul...
Error readSubstream(BinarySubstreamRef &Ref, uint32_t Length)
Read Length bytes from the underlying stream into Ref.
uint64_t bytesRemaining() const
void setOffset(uint64_t Off)
Error readArray(ArrayRef< T > &Array, uint32_t NumElements)
Get a reference to a NumElements element array of objects of type T from the underlying stream as if ...
Lightweight error class with error context and mandatory checking.
Definition: Error.h:160
static ErrorSuccess success()
Create a success value.
Definition: Error.h:337
Tagged union holding either a T or a Error.
Definition: Error.h:481
Error takeError()
Take ownership of the stored error.
Definition: Error.h:608
FixedStreamArray is similar to VarStreamArray, except with each record having a fixed-length.
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:51
LLVM Value Representation.
Definition: Value.h:74
Iterator end() const
Iterator begin(bool *HadError=nullptr) const
Kind kind() const
Definition: CVRecord.h:42
StringRef getName() const
Definition: TypeRecord.h:454
StringRef getUniqueName() const
Definition: TypeRecord.h:455
bool hasUniqueName() const
Definition: TypeRecord.h:431
A 32-bit type reference.
Definition: TypeIndex.h:96
A range adaptor for a pair of iterators.
Expected< std::unique_ptr< msf::MappedBlockStream > > safelyCreateIndexedStream(uint32_t StreamIndex) const
Wrapper around MappedBlockStream::createIndexedStream() that checks if a stream with that index actua...
Definition: PDBFile.cpp:487
uint32_t getHashKeySize() const
Definition: TpiStream.cpp:141
BinarySubstreamRef getTypeRecordsSubstream() const
Definition: TpiStream.cpp:225
HashTable< support::ulittle32_t > & getHashAdjusters()
Definition: TpiStream.cpp:237
std::vector< codeview::TypeIndex > findRecordsByName(StringRef Name) const
Definition: TpiStream.cpp:159
uint32_t TypeIndexBegin() const
Definition: TpiStream.cpp:124
PdbRaw_TpiVer getTpiVersion() const
Definition: TpiStream.cpp:119
codeview::CVTypeRange types(bool *HadError) const
Definition: TpiStream.cpp:241
TpiStream(PDBFile &File, std::unique_ptr< msf::MappedBlockStream > Stream)
Definition: TpiStream.cpp:35
FixedStreamArray< codeview::TypeIndexOffset > getTypeIndexOffsets() const
Definition: TpiStream.cpp:233
codeview::CVType getType(codeview::TypeIndex Index)
Definition: TpiStream.cpp:220
uint16_t getTypeHashStreamAuxIndex() const
Definition: TpiStream.cpp:136
uint16_t getTypeHashStreamIndex() const
Definition: TpiStream.cpp:132
bool supportsTypeLookup() const
Definition: TpiStream.cpp:176
FixedStreamArray< support::ulittle32_t > getHashValues() const
Definition: TpiStream.cpp:229
Expected< codeview::TypeIndex > findFullDeclForForwardRef(codeview::TypeIndex ForwardRefTI) const
Definition: TpiStream.cpp:179
uint32_t getNumTypeRecords() const
Definition: TpiStream.cpp:128
uint32_t TypeIndexEnd() const
Definition: TpiStream.cpp:126
uint32_t getNumHashBuckets() const
Definition: TpiStream.cpp:140
This provides a very simple, boring adaptor for a begin and end iterator into a range type.
bool isUdtForwardRef(CVType CVT)
Given an arbitrary codeview type, determine if it is an LF_STRUCTURE, LF_CLASS, LF_INTERFACE,...
std::string computeTypeName(TypeCollection &Types, TypeIndex Index)
Definition: RecordName.cpp:254
const uint16_t kInvalidStreamIndex
Definition: RawConstants.h:19
uint32_t hashStringV1(StringRef Str)
Definition: Hash.cpp:20
const uint32_t MaxTpiHashBuckets
Definition: RawTypes.h:301
const uint32_t MinTpiHashBuckets
Definition: RawTypes.h:300
Expected< TagRecordHash > hashTagRecord(const codeview::CVType &Type)
Given a CVType referring to a class, structure, union, or enum, compute the hash of its forward decl ...
Definition: TpiHashing.cpp:88
detail::packed_endian_specific_integral< uint32_t, llvm::endianness::little, unaligned > ulittle32_t
Definition: Endian.h:285
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1873
void consumeError(Error Err)
Consume a Error without doing anything.
Definition: Error.h:1069
Implement std::hash so that hash_code can be used in STL containers.
Definition: BitVector.h:858
BinaryStreamRef StreamData
support::ulittle32_t TypeIndexBegin
Definition: RawTypes.h:285
support::ulittle32_t TypeIndexEnd
Definition: RawTypes.h:286
support::ulittle16_t HashStreamIndex
Definition: RawTypes.h:290
support::ulittle16_t HashAuxStreamIndex
Definition: RawTypes.h:291
support::ulittle32_t HashKeySize
Definition: RawTypes.h:292
EmbeddedBuf HashAdjBuffer
Definition: RawTypes.h:297
EmbeddedBuf IndexOffsetBuffer
Definition: RawTypes.h:296
EmbeddedBuf HashValueBuffer
Definition: RawTypes.h:295
support::ulittle32_t NumHashBuckets
Definition: RawTypes.h:293
support::ulittle32_t Version
Definition: RawTypes.h:283
support::ulittle32_t TypeRecordBytes
Definition: RawTypes.h:287
support::ulittle32_t HeaderSize
Definition: RawTypes.h:284