1 //===- TpiStream.cpp - PDB Type Info (TPI) Stream 2 Access ----------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 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/BinaryStreamReader.h" 17 #include "llvm/DebugInfo/MSF/BinaryStreamReader.h" 18 #include "llvm/DebugInfo/MSF/MappedBlockStream.h" 19 #include "llvm/DebugInfo/PDB/Native/PDBFile.h" 20 #include "llvm/DebugInfo/PDB/Native/PDBTypeServerHandler.h" 21 #include "llvm/DebugInfo/PDB/Native/RawConstants.h" 22 #include "llvm/DebugInfo/PDB/Native/RawError.h" 23 #include "llvm/DebugInfo/PDB/Native/RawTypes.h" 24 #include "llvm/DebugInfo/PDB/Native/TpiHashing.h" 25 #include "llvm/Support/Endian.h" 26 #include "llvm/Support/Error.h" 27 #include <algorithm> 28 #include <cstdint> 29 #include <vector> 30 31 using namespace llvm; 32 using namespace llvm::codeview; 33 using namespace llvm::support; 34 using namespace llvm::msf; 35 using namespace llvm::pdb; 36 37 TpiStream::TpiStream(const PDBFile &File, 38 std::unique_ptr<MappedBlockStream> Stream) 39 : Pdb(File), Stream(std::move(Stream)) {} 40 41 TpiStream::~TpiStream() = default; 42 43 // Verifies that a given type record matches with a given hash value. 44 // Currently we only verify SRC_LINE records. 45 Error TpiStream::verifyHashValues() { 46 TpiHashVerifier Verifier(HashValues, Header->NumHashBuckets); 47 TypeDeserializer Deserializer; 48 49 TypeVisitorCallbackPipeline Pipeline; 50 Pipeline.addCallbackToPipeline(Deserializer); 51 Pipeline.addCallbackToPipeline(Verifier); 52 53 CVTypeVisitor Visitor(Pipeline); 54 return Visitor.visitTypeStream(TypeRecords); 55 } 56 57 Error TpiStream::reload() { 58 BinaryStreamReader Reader(*Stream); 59 60 if (Reader.bytesRemaining() < sizeof(TpiStreamHeader)) 61 return make_error<RawError>(raw_error_code::corrupt_file, 62 "TPI Stream does not contain a header."); 63 64 if (Reader.readObject(Header)) 65 return make_error<RawError>(raw_error_code::corrupt_file, 66 "TPI Stream does not contain a header."); 67 68 if (Header->Version != PdbTpiV80) 69 return make_error<RawError>(raw_error_code::corrupt_file, 70 "Unsupported TPI Version."); 71 72 if (Header->HeaderSize != sizeof(TpiStreamHeader)) 73 return make_error<RawError>(raw_error_code::corrupt_file, 74 "Corrupt TPI Header size."); 75 76 if (Header->HashKeySize != sizeof(ulittle32_t)) 77 return make_error<RawError>(raw_error_code::corrupt_file, 78 "TPI Stream expected 4 byte hash key size."); 79 80 if (Header->NumHashBuckets < MinTpiHashBuckets || 81 Header->NumHashBuckets > MaxTpiHashBuckets) 82 return make_error<RawError>(raw_error_code::corrupt_file, 83 "TPI Stream Invalid number of hash buckets."); 84 85 // The actual type records themselves come from this stream 86 if (auto EC = Reader.readArray(TypeRecords, Header->TypeRecordBytes)) 87 return EC; 88 89 // Hash indices, hash values, etc come from the hash stream. 90 if (Header->HashStreamIndex != kInvalidStreamIndex) { 91 if (Header->HashStreamIndex >= Pdb.getNumStreams()) 92 return make_error<RawError>(raw_error_code::corrupt_file, 93 "Invalid TPI hash stream index."); 94 95 auto HS = MappedBlockStream::createIndexedStream( 96 Pdb.getMsfLayout(), Pdb.getMsfBuffer(), Header->HashStreamIndex); 97 BinaryStreamReader HSR(*HS); 98 99 uint32_t NumHashValues = 100 Header->HashValueBuffer.Length / sizeof(ulittle32_t); 101 if (NumHashValues != NumTypeRecords()) 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)) 107 return EC; 108 std::vector<ulittle32_t> HashValueList; 109 for (auto I : HashValues) 110 HashValueList.push_back(I); 111 112 HSR.setOffset(Header->IndexOffsetBuffer.Off); 113 uint32_t NumTypeIndexOffsets = 114 Header->IndexOffsetBuffer.Length / sizeof(TypeIndexOffset); 115 if (auto EC = HSR.readArray(TypeIndexOffsets, NumTypeIndexOffsets)) 116 return EC; 117 118 if (Header->HashAdjBuffer.Length > 0) { 119 HSR.setOffset(Header->HashAdjBuffer.Off); 120 if (auto EC = HashAdjusters.load(HSR)) 121 return EC; 122 } 123 124 HashStream = std::move(HS); 125 126 // TPI hash table is a parallel array for the type records. 127 // Verify that the hash values match with type records. 128 if (auto EC = verifyHashValues()) 129 return EC; 130 } 131 132 return Error::success(); 133 } 134 135 PdbRaw_TpiVer TpiStream::getTpiVersion() const { 136 uint32_t Value = Header->Version; 137 return static_cast<PdbRaw_TpiVer>(Value); 138 } 139 140 uint32_t TpiStream::TypeIndexBegin() const { return Header->TypeIndexBegin; } 141 142 uint32_t TpiStream::TypeIndexEnd() const { return Header->TypeIndexEnd; } 143 144 uint32_t TpiStream::NumTypeRecords() const { 145 return TypeIndexEnd() - TypeIndexBegin(); 146 } 147 148 uint16_t TpiStream::getTypeHashStreamIndex() const { 149 return Header->HashStreamIndex; 150 } 151 152 uint16_t TpiStream::getTypeHashStreamAuxIndex() const { 153 return Header->HashAuxStreamIndex; 154 } 155 156 uint32_t TpiStream::NumHashBuckets() const { return Header->NumHashBuckets; } 157 uint32_t TpiStream::getHashKeySize() const { return Header->HashKeySize; } 158 159 FixedStreamArray<support::ulittle32_t> TpiStream::getHashValues() const { 160 return HashValues; 161 } 162 163 FixedStreamArray<TypeIndexOffset> TpiStream::getTypeIndexOffsets() const { 164 return TypeIndexOffsets; 165 } 166 167 HashTable &TpiStream::getHashAdjusters() { return HashAdjusters; } 168 169 CVTypeRange TpiStream::types(bool *HadError) const { 170 return make_range(TypeRecords.begin(HadError), TypeRecords.end()); 171 } 172 173 Error TpiStream::commit() { return Error::success(); } 174