blob: 79fcfc7440fc2a9ee8c2763b181a3ce7f25dcab8 [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) {
76 ArrayRef<uint8_t> D = Rec.Data;
77 if (Rec.Type == codeview::LF_UDT_SRC_LINE ||
78 Rec.Type == codeview::LF_UDT_MOD_SRC_LINE) {
79 uint32_t Hash =
80 hashStringV1(StringRef((const char *)D.data(), 4)) % NumHashBuckets;
81 if (Hash != Expected)
82 return make_error<RawError>(raw_error_code::corrupt_file,
83 "Corrupt TPI hash table.");
84 }
85 return Error::success();
86}
87
Zachary Turner819e77d2016-05-06 20:51:57 +000088Error TpiStream::reload() {
Zachary Turnera1657a92016-06-08 17:26:39 +000089 codeview::StreamReader Reader(*Stream);
Zachary Turnerf5c59652016-05-03 00:28:21 +000090
91 if (Reader.bytesRemaining() < sizeof(HeaderInfo))
Zachary Turner819e77d2016-05-06 20:51:57 +000092 return make_error<RawError>(raw_error_code::corrupt_file,
93 "TPI Stream does not contain a header.");
Zachary Turnerf5c59652016-05-03 00:28:21 +000094
Zachary Turner8dbe3622016-05-27 01:54:44 +000095 if (Reader.readObject(Header))
Zachary Turner819e77d2016-05-06 20:51:57 +000096 return make_error<RawError>(raw_error_code::corrupt_file,
97 "TPI Stream does not contain a header.");
Zachary Turnerf5c59652016-05-03 00:28:21 +000098
99 if (Header->Version != PdbTpiV80)
Zachary Turner819e77d2016-05-06 20:51:57 +0000100 return make_error<RawError>(raw_error_code::corrupt_file,
101 "Unsupported TPI Version.");
Zachary Turnerf5c59652016-05-03 00:28:21 +0000102
103 if (Header->HeaderSize != sizeof(HeaderInfo))
Zachary Turner819e77d2016-05-06 20:51:57 +0000104 return make_error<RawError>(raw_error_code::corrupt_file,
105 "Corrupt TPI Header size.");
Zachary Turnerf5c59652016-05-03 00:28:21 +0000106
107 if (Header->HashKeySize != sizeof(ulittle32_t))
Zachary Turner819e77d2016-05-06 20:51:57 +0000108 return make_error<RawError>(raw_error_code::corrupt_file,
109 "TPI Stream expected 4 byte hash key size.");
Zachary Turnerf5c59652016-05-03 00:28:21 +0000110
111 if (Header->NumHashBuckets < MinHashBuckets ||
112 Header->NumHashBuckets > MaxHashBuckets)
Zachary Turner819e77d2016-05-06 20:51:57 +0000113 return make_error<RawError>(raw_error_code::corrupt_file,
114 "TPI Stream Invalid number of hash buckets.");
Zachary Turnerf5c59652016-05-03 00:28:21 +0000115
116 HashFunction = HashBufferV8;
117
118 // The actual type records themselves come from this stream
Zachary Turner0d43c1c2016-05-28 05:21:57 +0000119 if (auto EC = Reader.readArray(TypeRecords, Header->TypeRecordBytes))
Zachary Turner819e77d2016-05-06 20:51:57 +0000120 return EC;
Zachary Turnerf5c59652016-05-03 00:28:21 +0000121
122 // Hash indices, hash values, etc come from the hash stream.
Zachary Turnerd2b2bfe2016-06-08 00:25:08 +0000123 if (Header->HashStreamIndex >= Pdb.getNumStreams())
124 return make_error<RawError>(raw_error_code::corrupt_file,
125 "Invalid TPI hash stream index.");
126
Zachary Turnera1657a92016-06-08 17:26:39 +0000127 auto HS =
128 MappedBlockStream::createIndexedStream(Header->HashStreamIndex, Pdb);
129 if (!HS)
130 return HS.takeError();
131 codeview::StreamReader HSR(**HS);
Rui Ueyamaba0aab92016-06-06 23:19:23 +0000132
Rui Ueyamafd97bf12016-06-03 20:48:51 +0000133 uint32_t NumHashValues = Header->HashValueBuffer.Length / sizeof(ulittle32_t);
Rui Ueyamaba0aab92016-06-06 23:19:23 +0000134 if (NumHashValues != NumTypeRecords())
135 return make_error<RawError>(
136 raw_error_code::corrupt_file,
137 "TPI hash count does not match with the number of type records.");
Zachary Turnerf5c59652016-05-03 00:28:21 +0000138 HSR.setOffset(Header->HashValueBuffer.Off);
Rui Ueyamafd97bf12016-06-03 20:48:51 +0000139 if (auto EC = HSR.readArray(HashValues, NumHashValues))
Zachary Turner819e77d2016-05-06 20:51:57 +0000140 return EC;
Zachary Turnerf5c59652016-05-03 00:28:21 +0000141
142 HSR.setOffset(Header->IndexOffsetBuffer.Off);
Rui Ueyamafd97bf12016-06-03 20:48:51 +0000143 uint32_t NumTypeIndexOffsets =
144 Header->IndexOffsetBuffer.Length / sizeof(TypeIndexOffset);
145 if (auto EC = HSR.readArray(TypeIndexOffsets, NumTypeIndexOffsets))
146 return EC;
147
148 HSR.setOffset(Header->HashAdjBuffer.Off);
149 uint32_t NumHashAdjustments =
150 Header->HashAdjBuffer.Length / sizeof(TypeIndexOffset);
151 if (auto EC = HSR.readArray(HashAdjustments, NumHashAdjustments))
Zachary Turner819e77d2016-05-06 20:51:57 +0000152 return EC;
Zachary Turnerf5c59652016-05-03 00:28:21 +0000153
Zachary Turnera1657a92016-06-08 17:26:39 +0000154 HashStream = std::move(*HS);
Rui Ueyamac41cd6d2016-06-09 00:10:19 +0000155
156 // TPI hash table is a parallel array for the type records.
157 // Verify that the hash values match with type records.
158 size_t I = 0;
159 bool HasError;
160 for (const codeview::CVType &Rec : types(&HasError)) {
161 if (auto EC = verifyTIHash(Rec, HashValues[I], Header->NumHashBuckets))
162 return EC;
163 ++I;
164 }
165
Zachary Turner819e77d2016-05-06 20:51:57 +0000166 return Error::success();
Zachary Turnerf5c59652016-05-03 00:28:21 +0000167}
168
169PdbRaw_TpiVer TpiStream::getTpiVersion() const {
170 uint32_t Value = Header->Version;
171 return static_cast<PdbRaw_TpiVer>(Value);
172}
173
174uint32_t TpiStream::TypeIndexBegin() const { return Header->TypeIndexBegin; }
175
176uint32_t TpiStream::TypeIndexEnd() const { return Header->TypeIndexEnd; }
177
178uint32_t TpiStream::NumTypeRecords() const {
179 return TypeIndexEnd() - TypeIndexBegin();
180}
181
Zachary Turner85ed80b2016-05-25 03:43:17 +0000182uint16_t TpiStream::getTypeHashStreamIndex() const {
183 return Header->HashStreamIndex;
184}
185
186uint16_t TpiStream::getTypeHashStreamAuxIndex() const {
187 return Header->HashAuxStreamIndex;
188}
189
Rui Ueyamaf14a74c2016-06-07 23:53:43 +0000190uint32_t TpiStream::NumHashBuckets() const { return Header->NumHashBuckets; }
Rui Ueyamad8339172016-06-07 23:44:27 +0000191uint32_t TpiStream::getHashKeySize() const { return Header->HashKeySize; }
192
Rui Ueyamafd97bf12016-06-03 20:48:51 +0000193codeview::FixedStreamArray<support::ulittle32_t>
194TpiStream::getHashValues() const {
195 return HashValues;
196}
197
198codeview::FixedStreamArray<TypeIndexOffset>
199TpiStream::getTypeIndexOffsets() const {
200 return TypeIndexOffsets;
201}
202
203codeview::FixedStreamArray<TypeIndexOffset>
204TpiStream::getHashAdjustments() const {
205 return HashAdjustments;
206}
207
Zachary Turner0d43c1c2016-05-28 05:21:57 +0000208iterator_range<codeview::CVTypeArray::Iterator>
209TpiStream::types(bool *HadError) const {
210 return llvm::make_range(TypeRecords.begin(HadError), TypeRecords.end());
Zachary Turnerf5c59652016-05-03 00:28:21 +0000211}