blob: 0853202f34e92bfdfbaec6ef1b31d05640350dfe [file] [log] [blame]
Zachary Turnerf5c59652016-05-03 00:28:21 +00001//===- 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/Raw/TpiStream.h"
11
12#include "llvm/DebugInfo/CodeView/CodeView.h"
Zachary Turnerd5d37dc2016-05-25 20:37:03 +000013#include "llvm/DebugInfo/CodeView/StreamReader.h"
Rui Ueyamafd97bf12016-06-03 20:48:51 +000014#include "llvm/DebugInfo/CodeView/TypeIndex.h"
Zachary Turnerf5c59652016-05-03 00:28:21 +000015#include "llvm/DebugInfo/CodeView/TypeRecord.h"
Rui Ueyamac41cd6d2016-06-09 00:10:19 +000016#include "llvm/DebugInfo/PDB/Raw/Hash.h"
Zachary Turnerd8447992016-06-07 05:28:55 +000017#include "llvm/DebugInfo/PDB/Raw/IndexedStreamData.h"
Zachary Turnerf5c59652016-05-03 00:28:21 +000018#include "llvm/DebugInfo/PDB/Raw/MappedBlockStream.h"
Zachary Turner90b8b8d2016-05-31 22:41:52 +000019#include "llvm/DebugInfo/PDB/Raw/PDBFile.h"
Zachary Turnerf5c59652016-05-03 00:28:21 +000020#include "llvm/DebugInfo/PDB/Raw/RawConstants.h"
Zachary Turner819e77d2016-05-06 20:51:57 +000021#include "llvm/DebugInfo/PDB/Raw/RawError.h"
Rui Ueyamafd97bf12016-06-03 20:48:51 +000022#include "llvm/DebugInfo/PDB/Raw/RawTypes.h"
Zachary Turnerf5c59652016-05-03 00:28:21 +000023
24#include "llvm/Support/Endian.h"
25
26using namespace llvm;
27using namespace llvm::support;
28using namespace llvm::pdb;
29
30namespace {
Zachary Turnerf5c59652016-05-03 00:28:21 +000031const uint32_t MinHashBuckets = 0x1000;
32const uint32_t MaxHashBuckets = 0x40000;
33}
34
35static uint32_t HashBufferV8(uint8_t *buffer, uint32_t NumBuckets) {
36 // Not yet implemented, this is probably some variation of CRC32 but we need
37 // to be sure of the precise implementation otherwise we won't be able to work
38 // with persisted hash values.
39 return 0;
40}
41
Rui Ueyama0350bf02016-06-02 21:13:47 +000042// This corresponds to `HDR` in PDB/dbi/tpi.h.
Zachary Turnerf5c59652016-05-03 00:28:21 +000043struct TpiStream::HeaderInfo {
44 struct EmbeddedBuf {
45 little32_t Off;
46 ulittle32_t Length;
47 };
48
49 ulittle32_t Version;
50 ulittle32_t HeaderSize;
51 ulittle32_t TypeIndexBegin;
52 ulittle32_t TypeIndexEnd;
53 ulittle32_t TypeRecordBytes;
54
Rui Ueyama0350bf02016-06-02 21:13:47 +000055 // The following members correspond to `TpiHash` in PDB/dbi/tpi.h.
Zachary Turnerf5c59652016-05-03 00:28:21 +000056 ulittle16_t HashStreamIndex;
57 ulittle16_t HashAuxStreamIndex;
58 ulittle32_t HashKeySize;
59 ulittle32_t NumHashBuckets;
60
61 EmbeddedBuf HashValueBuffer;
62 EmbeddedBuf IndexOffsetBuffer;
63 EmbeddedBuf HashAdjBuffer;
64};
65
Zachary Turnera1657a92016-06-08 17:26:39 +000066TpiStream::TpiStream(const PDBFile &File,
67 std::unique_ptr<MappedBlockStream> Stream)
68 : Pdb(File), Stream(std::move(Stream)), HashFunction(nullptr) {}
Zachary Turnerf5c59652016-05-03 00:28:21 +000069
70TpiStream::~TpiStream() {}
71
Rui Ueyama41974f12016-06-15 18:26:59 +000072// Computes a hash for a given TPI record.
73template <typename T, codeview::TypeRecordKind K>
74static Error getTpiHash(const codeview::CVType &Rec, uint32_t &Hash) {
75 ArrayRef<uint8_t> Data = Rec.Data;
76 ErrorOr<T> Obj = T::deserialize(K, Data);
77 if (Obj.getError())
78 return llvm::make_error<codeview::CodeViewError>(
79 codeview::cv_error_code::corrupt_record);
80
81 auto Opts = static_cast<uint16_t>(Obj->getOptions());
82 if (Opts & static_cast<uint16_t>(codeview::ClassOptions::ForwardReference)) {
83 // We don't know how to calculate a hash value for this yet.
84 // Currently we just skip it.
85 Hash = 0;
86 return Error::success();
87 }
88
89 if (!(Opts & static_cast<uint16_t>(codeview::ClassOptions::Scoped))) {
90 Hash = hashStringV1(Obj->getName());
91 return Error::success();
92 }
93
94 if (Opts & static_cast<uint16_t>(codeview::ClassOptions::HasUniqueName)) {
95 Hash = hashStringV1(Obj->getUniqueName());
96 return Error::success();
97 }
98
99 // This case is not implemented yet.
100 Hash = 0;
101 return Error::success();
102}
103
Rui Ueyamac41cd6d2016-06-09 00:10:19 +0000104// Verifies that a given type record matches with a given hash value.
105// Currently we only verify SRC_LINE records.
106static Error verifyTIHash(const codeview::CVType &Rec, uint32_t Expected,
107 uint32_t NumHashBuckets) {
Rui Ueyama9f3e9612016-06-14 22:25:07 +0000108 using namespace codeview;
109
Rui Ueyamac41cd6d2016-06-09 00:10:19 +0000110 ArrayRef<uint8_t> D = Rec.Data;
Rui Ueyama9f3e9612016-06-14 22:25:07 +0000111 uint32_t Hash;
112
113 switch (Rec.Type) {
114 case LF_UDT_SRC_LINE:
115 case LF_UDT_MOD_SRC_LINE:
116 Hash = hashStringV1(StringRef((const char *)D.data(), 4));
117 break;
Rui Ueyama41974f12016-06-15 18:26:59 +0000118 case LF_CLASS:
119 if (auto EC = getTpiHash<ClassRecord, TypeRecordKind::Class>(Rec, Hash))
120 return EC;
Rui Ueyama9f3e9612016-06-14 22:25:07 +0000121 break;
Rui Ueyama41974f12016-06-15 18:26:59 +0000122 case LF_ENUM:
123 if (auto EC = getTpiHash<EnumRecord, TypeRecordKind::Enum>(Rec, Hash))
124 return EC;
125 break;
126 case LF_INTERFACE:
127 if (auto EC = getTpiHash<ClassRecord, TypeRecordKind::Interface>(Rec, Hash))
128 return EC;
129 break;
130 case LF_STRUCTURE:
131 if (auto EC = getTpiHash<ClassRecord, TypeRecordKind::Struct>(Rec, Hash))
132 return EC;
133 break;
134 case LF_UNION:
135 if (auto EC = getTpiHash<UnionRecord, TypeRecordKind::Union>(Rec, Hash))
136 return EC;
137 break;
Rui Ueyama9f3e9612016-06-14 22:25:07 +0000138 default:
Rui Ueyama41974f12016-06-15 18:26:59 +0000139 // This pattern is not implemented yet.
Rui Ueyama9f3e9612016-06-14 22:25:07 +0000140 return Error::success();
141 }
142
Rui Ueyama41974f12016-06-15 18:26:59 +0000143 if (Hash && (Hash % NumHashBuckets) != Expected)
Rui Ueyama9f3e9612016-06-14 22:25:07 +0000144 return make_error<RawError>(raw_error_code::corrupt_file,
145 "Corrupt TPI hash table.");
Rui Ueyamac41cd6d2016-06-09 00:10:19 +0000146 return Error::success();
147}
148
Zachary Turner819e77d2016-05-06 20:51:57 +0000149Error TpiStream::reload() {
Zachary Turnera1657a92016-06-08 17:26:39 +0000150 codeview::StreamReader Reader(*Stream);
Zachary Turnerf5c59652016-05-03 00:28:21 +0000151
152 if (Reader.bytesRemaining() < sizeof(HeaderInfo))
Zachary Turner819e77d2016-05-06 20:51:57 +0000153 return make_error<RawError>(raw_error_code::corrupt_file,
154 "TPI Stream does not contain a header.");
Zachary Turnerf5c59652016-05-03 00:28:21 +0000155
Zachary Turner8dbe3622016-05-27 01:54:44 +0000156 if (Reader.readObject(Header))
Zachary Turner819e77d2016-05-06 20:51:57 +0000157 return make_error<RawError>(raw_error_code::corrupt_file,
158 "TPI Stream does not contain a header.");
Zachary Turnerf5c59652016-05-03 00:28:21 +0000159
160 if (Header->Version != PdbTpiV80)
Zachary Turner819e77d2016-05-06 20:51:57 +0000161 return make_error<RawError>(raw_error_code::corrupt_file,
162 "Unsupported TPI Version.");
Zachary Turnerf5c59652016-05-03 00:28:21 +0000163
164 if (Header->HeaderSize != sizeof(HeaderInfo))
Zachary Turner819e77d2016-05-06 20:51:57 +0000165 return make_error<RawError>(raw_error_code::corrupt_file,
166 "Corrupt TPI Header size.");
Zachary Turnerf5c59652016-05-03 00:28:21 +0000167
168 if (Header->HashKeySize != sizeof(ulittle32_t))
Zachary Turner819e77d2016-05-06 20:51:57 +0000169 return make_error<RawError>(raw_error_code::corrupt_file,
170 "TPI Stream expected 4 byte hash key size.");
Zachary Turnerf5c59652016-05-03 00:28:21 +0000171
172 if (Header->NumHashBuckets < MinHashBuckets ||
173 Header->NumHashBuckets > MaxHashBuckets)
Zachary Turner819e77d2016-05-06 20:51:57 +0000174 return make_error<RawError>(raw_error_code::corrupt_file,
175 "TPI Stream Invalid number of hash buckets.");
Zachary Turnerf5c59652016-05-03 00:28:21 +0000176
177 HashFunction = HashBufferV8;
178
179 // The actual type records themselves come from this stream
Zachary Turner0d43c1c2016-05-28 05:21:57 +0000180 if (auto EC = Reader.readArray(TypeRecords, Header->TypeRecordBytes))
Zachary Turner819e77d2016-05-06 20:51:57 +0000181 return EC;
Zachary Turnerf5c59652016-05-03 00:28:21 +0000182
183 // Hash indices, hash values, etc come from the hash stream.
Zachary Turnerd2b2bfe2016-06-08 00:25:08 +0000184 if (Header->HashStreamIndex >= Pdb.getNumStreams())
185 return make_error<RawError>(raw_error_code::corrupt_file,
186 "Invalid TPI hash stream index.");
187
Zachary Turnera1657a92016-06-08 17:26:39 +0000188 auto HS =
189 MappedBlockStream::createIndexedStream(Header->HashStreamIndex, Pdb);
190 if (!HS)
191 return HS.takeError();
192 codeview::StreamReader HSR(**HS);
Rui Ueyamaba0aab92016-06-06 23:19:23 +0000193
Rui Ueyamafd97bf12016-06-03 20:48:51 +0000194 uint32_t NumHashValues = Header->HashValueBuffer.Length / sizeof(ulittle32_t);
Rui Ueyamaba0aab92016-06-06 23:19:23 +0000195 if (NumHashValues != NumTypeRecords())
196 return make_error<RawError>(
197 raw_error_code::corrupt_file,
198 "TPI hash count does not match with the number of type records.");
Zachary Turnerf5c59652016-05-03 00:28:21 +0000199 HSR.setOffset(Header->HashValueBuffer.Off);
Rui Ueyamafd97bf12016-06-03 20:48:51 +0000200 if (auto EC = HSR.readArray(HashValues, NumHashValues))
Zachary Turner819e77d2016-05-06 20:51:57 +0000201 return EC;
Zachary Turnerf5c59652016-05-03 00:28:21 +0000202
203 HSR.setOffset(Header->IndexOffsetBuffer.Off);
Rui Ueyamafd97bf12016-06-03 20:48:51 +0000204 uint32_t NumTypeIndexOffsets =
205 Header->IndexOffsetBuffer.Length / sizeof(TypeIndexOffset);
206 if (auto EC = HSR.readArray(TypeIndexOffsets, NumTypeIndexOffsets))
207 return EC;
208
209 HSR.setOffset(Header->HashAdjBuffer.Off);
210 uint32_t NumHashAdjustments =
211 Header->HashAdjBuffer.Length / sizeof(TypeIndexOffset);
212 if (auto EC = HSR.readArray(HashAdjustments, NumHashAdjustments))
Zachary Turner819e77d2016-05-06 20:51:57 +0000213 return EC;
Zachary Turnerf5c59652016-05-03 00:28:21 +0000214
Zachary Turnera1657a92016-06-08 17:26:39 +0000215 HashStream = std::move(*HS);
Rui Ueyamac41cd6d2016-06-09 00:10:19 +0000216
217 // TPI hash table is a parallel array for the type records.
218 // Verify that the hash values match with type records.
219 size_t I = 0;
220 bool HasError;
221 for (const codeview::CVType &Rec : types(&HasError)) {
222 if (auto EC = verifyTIHash(Rec, HashValues[I], Header->NumHashBuckets))
223 return EC;
224 ++I;
225 }
226
Zachary Turner819e77d2016-05-06 20:51:57 +0000227 return Error::success();
Zachary Turnerf5c59652016-05-03 00:28:21 +0000228}
229
230PdbRaw_TpiVer TpiStream::getTpiVersion() const {
231 uint32_t Value = Header->Version;
232 return static_cast<PdbRaw_TpiVer>(Value);
233}
234
235uint32_t TpiStream::TypeIndexBegin() const { return Header->TypeIndexBegin; }
236
237uint32_t TpiStream::TypeIndexEnd() const { return Header->TypeIndexEnd; }
238
239uint32_t TpiStream::NumTypeRecords() const {
240 return TypeIndexEnd() - TypeIndexBegin();
241}
242
Zachary Turner85ed80b2016-05-25 03:43:17 +0000243uint16_t TpiStream::getTypeHashStreamIndex() const {
244 return Header->HashStreamIndex;
245}
246
247uint16_t TpiStream::getTypeHashStreamAuxIndex() const {
248 return Header->HashAuxStreamIndex;
249}
250
Rui Ueyamaf14a74c2016-06-07 23:53:43 +0000251uint32_t TpiStream::NumHashBuckets() const { return Header->NumHashBuckets; }
Rui Ueyamad8339172016-06-07 23:44:27 +0000252uint32_t TpiStream::getHashKeySize() const { return Header->HashKeySize; }
253
Rui Ueyamafd97bf12016-06-03 20:48:51 +0000254codeview::FixedStreamArray<support::ulittle32_t>
255TpiStream::getHashValues() const {
256 return HashValues;
257}
258
259codeview::FixedStreamArray<TypeIndexOffset>
260TpiStream::getTypeIndexOffsets() const {
261 return TypeIndexOffsets;
262}
263
264codeview::FixedStreamArray<TypeIndexOffset>
265TpiStream::getHashAdjustments() const {
266 return HashAdjustments;
267}
268
Zachary Turner0d43c1c2016-05-28 05:21:57 +0000269iterator_range<codeview::CVTypeArray::Iterator>
270TpiStream::types(bool *HadError) const {
271 return llvm::make_range(TypeRecords.begin(HadError), TypeRecords.end());
Zachary Turnerf5c59652016-05-03 00:28:21 +0000272}