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/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" 26 #include <algorithm> 27 #include <cstdint> 28 #include <vector> 29 30 using namespace llvm; 31 using namespace llvm::codeview; 32 using namespace llvm::support; 33 using namespace llvm::msf; 34 using namespace llvm::pdb; 35 36 TpiStream::TpiStream(const PDBFile &File, 37 std::unique_ptr<MappedBlockStream> Stream) 38 : Pdb(File), Stream(std::move(Stream)) {} 39 40 TpiStream::~TpiStream() = default; 41 42 Error TpiStream::reload() { 43 BinaryStreamReader Reader(*Stream); 44 45 if (Reader.bytesRemaining() < sizeof(TpiStreamHeader)) 46 return make_error<RawError>(raw_error_code::corrupt_file, 47 "TPI Stream does not contain a header."); 48 49 if (Reader.readObject(Header)) 50 return make_error<RawError>(raw_error_code::corrupt_file, 51 "TPI Stream does not contain a header."); 52 53 if (Header->Version != PdbTpiV80) 54 return make_error<RawError>(raw_error_code::corrupt_file, 55 "Unsupported TPI Version."); 56 57 if (Header->HeaderSize != sizeof(TpiStreamHeader)) 58 return make_error<RawError>(raw_error_code::corrupt_file, 59 "Corrupt TPI Header size."); 60 61 if (Header->HashKeySize != sizeof(ulittle32_t)) 62 return make_error<RawError>(raw_error_code::corrupt_file, 63 "TPI Stream expected 4 byte hash key size."); 64 65 if (Header->NumHashBuckets < MinTpiHashBuckets || 66 Header->NumHashBuckets > MaxTpiHashBuckets) 67 return make_error<RawError>(raw_error_code::corrupt_file, 68 "TPI Stream Invalid number of hash buckets."); 69 70 // The actual type records themselves come from this stream 71 if (auto EC = Reader.readArray(TypeRecords, Header->TypeRecordBytes)) 72 return EC; 73 74 // Hash indices, hash values, etc come from the hash stream. 75 if (Header->HashStreamIndex != kInvalidStreamIndex) { 76 if (Header->HashStreamIndex >= Pdb.getNumStreams()) 77 return make_error<RawError>(raw_error_code::corrupt_file, 78 "Invalid TPI hash stream index."); 79 80 auto HS = MappedBlockStream::createIndexedStream( 81 Pdb.getMsfLayout(), Pdb.getMsfBuffer(), Header->HashStreamIndex); 82 BinaryStreamReader HSR(*HS); 83 84 // There should be a hash value for every type record, or no hashes at all. 85 uint32_t NumHashValues = 86 Header->HashValueBuffer.Length / sizeof(ulittle32_t); 87 if (NumHashValues != getNumTypeRecords() && NumHashValues != 0) 88 return make_error<RawError>( 89 raw_error_code::corrupt_file, 90 "TPI hash count does not match with the number of type records."); 91 HSR.setOffset(Header->HashValueBuffer.Off); 92 if (auto EC = HSR.readArray(HashValues, NumHashValues)) 93 return EC; 94 std::vector<ulittle32_t> HashValueList; 95 for (auto I : HashValues) 96 HashValueList.push_back(I); 97 98 HSR.setOffset(Header->IndexOffsetBuffer.Off); 99 uint32_t NumTypeIndexOffsets = 100 Header->IndexOffsetBuffer.Length / sizeof(TypeIndexOffset); 101 if (auto EC = HSR.readArray(TypeIndexOffsets, NumTypeIndexOffsets)) 102 return EC; 103 104 if (Header->HashAdjBuffer.Length > 0) { 105 HSR.setOffset(Header->HashAdjBuffer.Off); 106 if (auto EC = HashAdjusters.load(HSR)) 107 return EC; 108 } 109 110 HashStream = std::move(HS); 111 } 112 113 return Error::success(); 114 } 115 116 PdbRaw_TpiVer TpiStream::getTpiVersion() const { 117 uint32_t Value = Header->Version; 118 return static_cast<PdbRaw_TpiVer>(Value); 119 } 120 121 uint32_t TpiStream::TypeIndexBegin() const { return Header->TypeIndexBegin; } 122 123 uint32_t TpiStream::TypeIndexEnd() const { return Header->TypeIndexEnd; } 124 125 uint32_t TpiStream::getNumTypeRecords() const { 126 return TypeIndexEnd() - TypeIndexBegin(); 127 } 128 129 uint16_t TpiStream::getTypeHashStreamIndex() const { 130 return Header->HashStreamIndex; 131 } 132 133 uint16_t TpiStream::getTypeHashStreamAuxIndex() const { 134 return Header->HashAuxStreamIndex; 135 } 136 137 uint32_t TpiStream::getNumHashBuckets() const { return Header->NumHashBuckets; } 138 uint32_t TpiStream::getHashKeySize() const { return Header->HashKeySize; } 139 140 FixedStreamArray<support::ulittle32_t> TpiStream::getHashValues() const { 141 return HashValues; 142 } 143 144 FixedStreamArray<TypeIndexOffset> TpiStream::getTypeIndexOffsets() const { 145 return TypeIndexOffsets; 146 } 147 148 HashTable &TpiStream::getHashAdjusters() { return HashAdjusters; } 149 150 CVTypeRange TpiStream::types(bool *HadError) const { 151 return make_range(TypeRecords.begin(HadError), TypeRecords.end()); 152 } 153 154 Error TpiStream::commit() { return Error::success(); } 155