1 //===- TpiStream.cpp - PDB Type Info (TPI) Stream 2 Access ----------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/DebugInfo/PDB/Native/TpiStream.h"
11 #include "llvm/ADT/iterator_range.h"
12 #include "llvm/DebugInfo/CodeView/CVTypeVisitor.h"
13 #include "llvm/DebugInfo/CodeView/TypeDeserializer.h"
14 #include "llvm/DebugInfo/CodeView/TypeRecord.h"
15 #include "llvm/DebugInfo/CodeView/TypeVisitorCallbackPipeline.h"
16 #include "llvm/DebugInfo/MSF/MappedBlockStream.h"
17 #include "llvm/DebugInfo/PDB/Native/PDBFile.h"
18 #include "llvm/DebugInfo/PDB/Native/PDBTypeServerHandler.h"
19 #include "llvm/DebugInfo/PDB/Native/RawConstants.h"
20 #include "llvm/DebugInfo/PDB/Native/RawError.h"
21 #include "llvm/DebugInfo/PDB/Native/RawTypes.h"
22 #include "llvm/DebugInfo/PDB/Native/TpiHashing.h"
23 #include "llvm/Support/BinaryStreamReader.h"
24 #include "llvm/Support/Endian.h"
25 #include "llvm/Support/Error.h"
31 using namespace llvm::codeview;
32 using namespace llvm::support;
33 using namespace llvm::msf;
34 using namespace llvm::pdb;
36 TpiStream::TpiStream(const PDBFile &File,
37 std::unique_ptr<MappedBlockStream> Stream)
38 : Pdb(File), Stream(std::move(Stream)) {}
40 TpiStream::~TpiStream() = default;
42 // Verifies that a given type record matches with a given hash value.
43 // Currently we only verify SRC_LINE records.
44 Error TpiStream::verifyHashValues() {
45 TpiHashVerifier Verifier(HashValues, Header->NumHashBuckets);
46 TypeDeserializer Deserializer;
48 TypeVisitorCallbackPipeline Pipeline;
49 Pipeline.addCallbackToPipeline(Deserializer);
50 Pipeline.addCallbackToPipeline(Verifier);
52 CVTypeVisitor Visitor(Pipeline);
53 return Visitor.visitTypeStream(TypeRecords);
56 Error TpiStream::reload() {
57 BinaryStreamReader Reader(*Stream);
59 if (Reader.bytesRemaining() < sizeof(TpiStreamHeader))
60 return make_error<RawError>(raw_error_code::corrupt_file,
61 "TPI Stream does not contain a header.");
63 if (Reader.readObject(Header))
64 return make_error<RawError>(raw_error_code::corrupt_file,
65 "TPI Stream does not contain a header.");
67 if (Header->Version != PdbTpiV80)
68 return make_error<RawError>(raw_error_code::corrupt_file,
69 "Unsupported TPI Version.");
71 if (Header->HeaderSize != sizeof(TpiStreamHeader))
72 return make_error<RawError>(raw_error_code::corrupt_file,
73 "Corrupt TPI Header size.");
75 if (Header->HashKeySize != sizeof(ulittle32_t))
76 return make_error<RawError>(raw_error_code::corrupt_file,
77 "TPI Stream expected 4 byte hash key size.");
79 if (Header->NumHashBuckets < MinTpiHashBuckets ||
80 Header->NumHashBuckets > MaxTpiHashBuckets)
81 return make_error<RawError>(raw_error_code::corrupt_file,
82 "TPI Stream Invalid number of hash buckets.");
84 // The actual type records themselves come from this stream
85 if (auto EC = Reader.readArray(TypeRecords, Header->TypeRecordBytes))
88 // Hash indices, hash values, etc come from the hash stream.
89 if (Header->HashStreamIndex != kInvalidStreamIndex) {
90 if (Header->HashStreamIndex >= Pdb.getNumStreams())
91 return make_error<RawError>(raw_error_code::corrupt_file,
92 "Invalid TPI hash stream index.");
94 auto HS = MappedBlockStream::createIndexedStream(
95 Pdb.getMsfLayout(), Pdb.getMsfBuffer(), Header->HashStreamIndex);
96 BinaryStreamReader HSR(*HS);
98 // There should be a hash value for every type record, or no hashes at all.
99 uint32_t NumHashValues =
100 Header->HashValueBuffer.Length / sizeof(ulittle32_t);
101 if (NumHashValues != NumTypeRecords() && NumHashValues != 0)
102 return make_error<RawError>(
103 raw_error_code::corrupt_file,
104 "TPI hash count does not match with the number of type records.");
105 HSR.setOffset(Header->HashValueBuffer.Off);
106 if (auto EC = HSR.readArray(HashValues, NumHashValues))
108 std::vector<ulittle32_t> HashValueList;
109 for (auto I : HashValues)
110 HashValueList.push_back(I);
112 HSR.setOffset(Header->IndexOffsetBuffer.Off);
113 uint32_t NumTypeIndexOffsets =
114 Header->IndexOffsetBuffer.Length / sizeof(TypeIndexOffset);
115 if (auto EC = HSR.readArray(TypeIndexOffsets, NumTypeIndexOffsets))
118 if (Header->HashAdjBuffer.Length > 0) {
119 HSR.setOffset(Header->HashAdjBuffer.Off);
120 if (auto EC = HashAdjusters.load(HSR))
124 HashStream = std::move(HS);
126 // TPI hash table is a parallel array for the type records.
127 // Verify that the hash values match with type records.
128 if (NumHashValues > 0)
129 if (auto EC = verifyHashValues())
133 return Error::success();
136 PdbRaw_TpiVer TpiStream::getTpiVersion() const {
137 uint32_t Value = Header->Version;
138 return static_cast<PdbRaw_TpiVer>(Value);
141 uint32_t TpiStream::TypeIndexBegin() const { return Header->TypeIndexBegin; }
143 uint32_t TpiStream::TypeIndexEnd() const { return Header->TypeIndexEnd; }
145 uint32_t TpiStream::NumTypeRecords() const {
146 return TypeIndexEnd() - TypeIndexBegin();
149 uint16_t TpiStream::getTypeHashStreamIndex() const {
150 return Header->HashStreamIndex;
153 uint16_t TpiStream::getTypeHashStreamAuxIndex() const {
154 return Header->HashAuxStreamIndex;
157 uint32_t TpiStream::NumHashBuckets() const { return Header->NumHashBuckets; }
158 uint32_t TpiStream::getHashKeySize() const { return Header->HashKeySize; }
160 FixedStreamArray<support::ulittle32_t> TpiStream::getHashValues() const {
164 FixedStreamArray<TypeIndexOffset> TpiStream::getTypeIndexOffsets() const {
165 return TypeIndexOffsets;
168 HashTable &TpiStream::getHashAdjusters() { return HashAdjusters; }
170 CVTypeRange TpiStream::types(bool *HadError) const {
171 return make_range(TypeRecords.begin(HadError), TypeRecords.end());
174 Error TpiStream::commit() { return Error::success(); }