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
12 #include "llvm/ADT/iterator_range.h"
13 #include "llvm/DebugInfo/CodeView/LazyRandomTypeCollection.h"
14 #include "llvm/DebugInfo/CodeView/TypeRecord.h"
15 #include "llvm/DebugInfo/MSF/MappedBlockStream.h"
16 #include "llvm/DebugInfo/PDB/Native/PDBFile.h"
17 #include "llvm/DebugInfo/PDB/Native/RawConstants.h"
18 #include "llvm/DebugInfo/PDB/Native/RawError.h"
19 #include "llvm/DebugInfo/PDB/Native/RawTypes.h"
20 #include "llvm/DebugInfo/PDB/Native/TpiHashing.h"
21 #include "llvm/Support/BinaryStreamReader.h"
22 #include "llvm/Support/Endian.h"
23 #include "llvm/Support/Error.h"
24 #include <algorithm>
25 #include <cstdint>
26 #include <vector>
27
28 using namespace llvm;
29 using namespace llvm::codeview;
30 using namespace llvm::support;
31 using namespace llvm::msf;
32 using namespace llvm::pdb;
33
TpiStream(PDBFile & File,std::unique_ptr<MappedBlockStream> Stream)34 TpiStream::TpiStream(PDBFile &File, std::unique_ptr<MappedBlockStream> Stream)
35 : Pdb(File), Stream(std::move(Stream)) {}
36
37 TpiStream::~TpiStream() = default;
38
reload()39 Error TpiStream::reload() {
40 BinaryStreamReader Reader(*Stream);
41
42 if (Reader.bytesRemaining() < sizeof(TpiStreamHeader))
43 return make_error<RawError>(raw_error_code::corrupt_file,
44 "TPI Stream does not contain a header.");
45
46 if (Reader.readObject(Header))
47 return make_error<RawError>(raw_error_code::corrupt_file,
48 "TPI Stream does not contain a header.");
49
50 if (Header->Version != PdbTpiV80)
51 return make_error<RawError>(raw_error_code::corrupt_file,
52 "Unsupported TPI Version.");
53
54 if (Header->HeaderSize != sizeof(TpiStreamHeader))
55 return make_error<RawError>(raw_error_code::corrupt_file,
56 "Corrupt TPI Header size.");
57
58 if (Header->HashKeySize != sizeof(ulittle32_t))
59 return make_error<RawError>(raw_error_code::corrupt_file,
60 "TPI Stream expected 4 byte hash key size.");
61
62 if (Header->NumHashBuckets < MinTpiHashBuckets ||
63 Header->NumHashBuckets > MaxTpiHashBuckets)
64 return make_error<RawError>(raw_error_code::corrupt_file,
65 "TPI Stream Invalid number of hash buckets.");
66
67 // The actual type records themselves come from this stream
68 if (auto EC =
69 Reader.readSubstream(TypeRecordsSubstream, Header->TypeRecordBytes))
70 return EC;
71
72 BinaryStreamReader RecordReader(TypeRecordsSubstream.StreamData);
73 if (auto EC =
74 RecordReader.readArray(TypeRecords, TypeRecordsSubstream.size()))
75 return EC;
76
77 // Hash indices, hash values, etc come from the hash stream.
78 if (Header->HashStreamIndex != kInvalidStreamIndex) {
79 if (Header->HashStreamIndex >= Pdb.getNumStreams())
80 return make_error<RawError>(raw_error_code::corrupt_file,
81 "Invalid TPI hash stream index.");
82
83 auto HS = MappedBlockStream::createIndexedStream(
84 Pdb.getMsfLayout(), Pdb.getMsfBuffer(), Header->HashStreamIndex,
85 Pdb.getAllocator());
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>(
93 raw_error_code::corrupt_file,
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 = llvm::make_unique<LazyRandomTypeCollection>(
115 TypeRecords, getNumTypeRecords(), getTypeIndexOffsets());
116 return Error::success();
117 }
118
getTpiVersion() const119 PdbRaw_TpiVer TpiStream::getTpiVersion() const {
120 uint32_t Value = Header->Version;
121 return static_cast<PdbRaw_TpiVer>(Value);
122 }
123
TypeIndexBegin() const124 uint32_t TpiStream::TypeIndexBegin() const { return Header->TypeIndexBegin; }
125
TypeIndexEnd() const126 uint32_t TpiStream::TypeIndexEnd() const { return Header->TypeIndexEnd; }
127
getNumTypeRecords() const128 uint32_t TpiStream::getNumTypeRecords() const {
129 return TypeIndexEnd() - TypeIndexBegin();
130 }
131
getTypeHashStreamIndex() const132 uint16_t TpiStream::getTypeHashStreamIndex() const {
133 return Header->HashStreamIndex;
134 }
135
getTypeHashStreamAuxIndex() const136 uint16_t TpiStream::getTypeHashStreamAuxIndex() const {
137 return Header->HashAuxStreamIndex;
138 }
139
getNumHashBuckets() const140 uint32_t TpiStream::getNumHashBuckets() const { return Header->NumHashBuckets; }
getHashKeySize() const141 uint32_t TpiStream::getHashKeySize() const { return Header->HashKeySize; }
142
getTypeRecordsSubstream() const143 BinarySubstreamRef TpiStream::getTypeRecordsSubstream() const {
144 return TypeRecordsSubstream;
145 }
146
getHashValues() const147 FixedStreamArray<support::ulittle32_t> TpiStream::getHashValues() const {
148 return HashValues;
149 }
150
getTypeIndexOffsets() const151 FixedStreamArray<TypeIndexOffset> TpiStream::getTypeIndexOffsets() const {
152 return TypeIndexOffsets;
153 }
154
getHashAdjusters()155 HashTable<support::ulittle32_t> &TpiStream::getHashAdjusters() {
156 return HashAdjusters;
157 }
158
types(bool * HadError) const159 CVTypeRange TpiStream::types(bool *HadError) const {
160 return make_range(TypeRecords.begin(HadError), TypeRecords.end());
161 }
162
commit()163 Error TpiStream::commit() { return Error::success(); }
164