blob: 744561128291b859bafbd5dc9f39a4f0fdfd7fb8 [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 Ueyamac41cd6d2016-06-09 00:10:19 +000072// Verifies that a given type record matches with a given hash value.
73// Currently we only verify SRC_LINE records.
74static Error verifyTIHash(const codeview::CVType &Rec, uint32_t Expected,
75 uint32_t NumHashBuckets) {
Rui Ueyama9f3e9612016-06-14 22:25:07 +000076 using namespace codeview;
77
Rui Ueyamac41cd6d2016-06-09 00:10:19 +000078 ArrayRef<uint8_t> D = Rec.Data;
Rui Ueyama9f3e9612016-06-14 22:25:07 +000079 uint32_t Hash;
80
81 switch (Rec.Type) {
82 case LF_UDT_SRC_LINE:
83 case LF_UDT_MOD_SRC_LINE:
84 Hash = hashStringV1(StringRef((const char *)D.data(), 4));
85 break;
86 case LF_ENUM: {
87 ErrorOr<EnumRecord> Enum = EnumRecord::deserialize(TypeRecordKind::Enum, D);
88 if (Enum.getError())
Rui Ueyamac41cd6d2016-06-09 00:10:19 +000089 return make_error<RawError>(raw_error_code::corrupt_file,
90 "Corrupt TPI hash table.");
Rui Ueyama9f3e9612016-06-14 22:25:07 +000091 Hash = hashStringV1(Enum->getName());
92 break;
Rui Ueyamac41cd6d2016-06-09 00:10:19 +000093 }
Rui Ueyama9f3e9612016-06-14 22:25:07 +000094 default:
95 return Error::success();
96 }
97
98 if ((Hash % NumHashBuckets) != Expected)
99 return make_error<RawError>(raw_error_code::corrupt_file,
100 "Corrupt TPI hash table.");
Rui Ueyamac41cd6d2016-06-09 00:10:19 +0000101 return Error::success();
102}
103
Zachary Turner819e77d2016-05-06 20:51:57 +0000104Error TpiStream::reload() {
Zachary Turnera1657a92016-06-08 17:26:39 +0000105 codeview::StreamReader Reader(*Stream);
Zachary Turnerf5c59652016-05-03 00:28:21 +0000106
107 if (Reader.bytesRemaining() < sizeof(HeaderInfo))
Zachary Turner819e77d2016-05-06 20:51:57 +0000108 return make_error<RawError>(raw_error_code::corrupt_file,
109 "TPI Stream does not contain a header.");
Zachary Turnerf5c59652016-05-03 00:28:21 +0000110
Zachary Turner8dbe3622016-05-27 01:54:44 +0000111 if (Reader.readObject(Header))
Zachary Turner819e77d2016-05-06 20:51:57 +0000112 return make_error<RawError>(raw_error_code::corrupt_file,
113 "TPI Stream does not contain a header.");
Zachary Turnerf5c59652016-05-03 00:28:21 +0000114
115 if (Header->Version != PdbTpiV80)
Zachary Turner819e77d2016-05-06 20:51:57 +0000116 return make_error<RawError>(raw_error_code::corrupt_file,
117 "Unsupported TPI Version.");
Zachary Turnerf5c59652016-05-03 00:28:21 +0000118
119 if (Header->HeaderSize != sizeof(HeaderInfo))
Zachary Turner819e77d2016-05-06 20:51:57 +0000120 return make_error<RawError>(raw_error_code::corrupt_file,
121 "Corrupt TPI Header size.");
Zachary Turnerf5c59652016-05-03 00:28:21 +0000122
123 if (Header->HashKeySize != sizeof(ulittle32_t))
Zachary Turner819e77d2016-05-06 20:51:57 +0000124 return make_error<RawError>(raw_error_code::corrupt_file,
125 "TPI Stream expected 4 byte hash key size.");
Zachary Turnerf5c59652016-05-03 00:28:21 +0000126
127 if (Header->NumHashBuckets < MinHashBuckets ||
128 Header->NumHashBuckets > MaxHashBuckets)
Zachary Turner819e77d2016-05-06 20:51:57 +0000129 return make_error<RawError>(raw_error_code::corrupt_file,
130 "TPI Stream Invalid number of hash buckets.");
Zachary Turnerf5c59652016-05-03 00:28:21 +0000131
132 HashFunction = HashBufferV8;
133
134 // The actual type records themselves come from this stream
Zachary Turner0d43c1c2016-05-28 05:21:57 +0000135 if (auto EC = Reader.readArray(TypeRecords, Header->TypeRecordBytes))
Zachary Turner819e77d2016-05-06 20:51:57 +0000136 return EC;
Zachary Turnerf5c59652016-05-03 00:28:21 +0000137
138 // Hash indices, hash values, etc come from the hash stream.
Zachary Turnerd2b2bfe2016-06-08 00:25:08 +0000139 if (Header->HashStreamIndex >= Pdb.getNumStreams())
140 return make_error<RawError>(raw_error_code::corrupt_file,
141 "Invalid TPI hash stream index.");
142
Zachary Turnera1657a92016-06-08 17:26:39 +0000143 auto HS =
144 MappedBlockStream::createIndexedStream(Header->HashStreamIndex, Pdb);
145 if (!HS)
146 return HS.takeError();
147 codeview::StreamReader HSR(**HS);
Rui Ueyamaba0aab92016-06-06 23:19:23 +0000148
Rui Ueyamafd97bf12016-06-03 20:48:51 +0000149 uint32_t NumHashValues = Header->HashValueBuffer.Length / sizeof(ulittle32_t);
Rui Ueyamaba0aab92016-06-06 23:19:23 +0000150 if (NumHashValues != NumTypeRecords())
151 return make_error<RawError>(
152 raw_error_code::corrupt_file,
153 "TPI hash count does not match with the number of type records.");
Zachary Turnerf5c59652016-05-03 00:28:21 +0000154 HSR.setOffset(Header->HashValueBuffer.Off);
Rui Ueyamafd97bf12016-06-03 20:48:51 +0000155 if (auto EC = HSR.readArray(HashValues, NumHashValues))
Zachary Turner819e77d2016-05-06 20:51:57 +0000156 return EC;
Zachary Turnerf5c59652016-05-03 00:28:21 +0000157
158 HSR.setOffset(Header->IndexOffsetBuffer.Off);
Rui Ueyamafd97bf12016-06-03 20:48:51 +0000159 uint32_t NumTypeIndexOffsets =
160 Header->IndexOffsetBuffer.Length / sizeof(TypeIndexOffset);
161 if (auto EC = HSR.readArray(TypeIndexOffsets, NumTypeIndexOffsets))
162 return EC;
163
164 HSR.setOffset(Header->HashAdjBuffer.Off);
165 uint32_t NumHashAdjustments =
166 Header->HashAdjBuffer.Length / sizeof(TypeIndexOffset);
167 if (auto EC = HSR.readArray(HashAdjustments, NumHashAdjustments))
Zachary Turner819e77d2016-05-06 20:51:57 +0000168 return EC;
Zachary Turnerf5c59652016-05-03 00:28:21 +0000169
Zachary Turnera1657a92016-06-08 17:26:39 +0000170 HashStream = std::move(*HS);
Rui Ueyamac41cd6d2016-06-09 00:10:19 +0000171
172 // TPI hash table is a parallel array for the type records.
173 // Verify that the hash values match with type records.
174 size_t I = 0;
175 bool HasError;
176 for (const codeview::CVType &Rec : types(&HasError)) {
177 if (auto EC = verifyTIHash(Rec, HashValues[I], Header->NumHashBuckets))
178 return EC;
179 ++I;
180 }
181
Zachary Turner819e77d2016-05-06 20:51:57 +0000182 return Error::success();
Zachary Turnerf5c59652016-05-03 00:28:21 +0000183}
184
185PdbRaw_TpiVer TpiStream::getTpiVersion() const {
186 uint32_t Value = Header->Version;
187 return static_cast<PdbRaw_TpiVer>(Value);
188}
189
190uint32_t TpiStream::TypeIndexBegin() const { return Header->TypeIndexBegin; }
191
192uint32_t TpiStream::TypeIndexEnd() const { return Header->TypeIndexEnd; }
193
194uint32_t TpiStream::NumTypeRecords() const {
195 return TypeIndexEnd() - TypeIndexBegin();
196}
197
Zachary Turner85ed80b2016-05-25 03:43:17 +0000198uint16_t TpiStream::getTypeHashStreamIndex() const {
199 return Header->HashStreamIndex;
200}
201
202uint16_t TpiStream::getTypeHashStreamAuxIndex() const {
203 return Header->HashAuxStreamIndex;
204}
205
Rui Ueyamaf14a74c2016-06-07 23:53:43 +0000206uint32_t TpiStream::NumHashBuckets() const { return Header->NumHashBuckets; }
Rui Ueyamad8339172016-06-07 23:44:27 +0000207uint32_t TpiStream::getHashKeySize() const { return Header->HashKeySize; }
208
Rui Ueyamafd97bf12016-06-03 20:48:51 +0000209codeview::FixedStreamArray<support::ulittle32_t>
210TpiStream::getHashValues() const {
211 return HashValues;
212}
213
214codeview::FixedStreamArray<TypeIndexOffset>
215TpiStream::getTypeIndexOffsets() const {
216 return TypeIndexOffsets;
217}
218
219codeview::FixedStreamArray<TypeIndexOffset>
220TpiStream::getHashAdjustments() const {
221 return HashAdjustments;
222}
223
Zachary Turner0d43c1c2016-05-28 05:21:57 +0000224iterator_range<codeview::CVTypeArray::Iterator>
225TpiStream::types(bool *HadError) const {
226 return llvm::make_range(TypeRecords.begin(HadError), TypeRecords.end());
Zachary Turnerf5c59652016-05-03 00:28:21 +0000227}