blob: 6478ad1f77f3b04ed4528fd8dc07e273f01785ec [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"
Zachary Turnerf5c59652016-05-03 00:28:21 +000014#include "llvm/DebugInfo/CodeView/TypeRecord.h"
15#include "llvm/DebugInfo/PDB/Raw/MappedBlockStream.h"
16#include "llvm/DebugInfo/PDB/Raw/RawConstants.h"
Zachary Turner819e77d2016-05-06 20:51:57 +000017#include "llvm/DebugInfo/PDB/Raw/RawError.h"
Zachary Turnerf5c59652016-05-03 00:28:21 +000018
19#include "llvm/Support/Endian.h"
20
21using namespace llvm;
22using namespace llvm::support;
23using namespace llvm::pdb;
24
25namespace {
Zachary Turnerf5c59652016-05-03 00:28:21 +000026const uint32_t MinHashBuckets = 0x1000;
27const uint32_t MaxHashBuckets = 0x40000;
28}
29
30static uint32_t HashBufferV8(uint8_t *buffer, uint32_t NumBuckets) {
31 // Not yet implemented, this is probably some variation of CRC32 but we need
32 // to be sure of the precise implementation otherwise we won't be able to work
33 // with persisted hash values.
34 return 0;
35}
36
37struct TpiStream::HeaderInfo {
38 struct EmbeddedBuf {
39 little32_t Off;
40 ulittle32_t Length;
41 };
42
43 ulittle32_t Version;
44 ulittle32_t HeaderSize;
45 ulittle32_t TypeIndexBegin;
46 ulittle32_t TypeIndexEnd;
47 ulittle32_t TypeRecordBytes;
48
49 ulittle16_t HashStreamIndex;
50 ulittle16_t HashAuxStreamIndex;
51 ulittle32_t HashKeySize;
52 ulittle32_t NumHashBuckets;
53
54 EmbeddedBuf HashValueBuffer;
55 EmbeddedBuf IndexOffsetBuffer;
56 EmbeddedBuf HashAdjBuffer;
57};
58
Zachary Turnerc9972c62016-05-25 04:35:22 +000059TpiStream::TpiStream(PDBFile &File, uint32_t StreamIdx)
60 : Pdb(File), Stream(StreamIdx, File), HashFunction(nullptr) {}
Zachary Turnerf5c59652016-05-03 00:28:21 +000061
62TpiStream::~TpiStream() {}
63
Zachary Turner819e77d2016-05-06 20:51:57 +000064Error TpiStream::reload() {
Zachary Turnerd5d37dc2016-05-25 20:37:03 +000065 codeview::StreamReader Reader(Stream);
Zachary Turnerf5c59652016-05-03 00:28:21 +000066
67 if (Reader.bytesRemaining() < sizeof(HeaderInfo))
Zachary Turner819e77d2016-05-06 20:51:57 +000068 return make_error<RawError>(raw_error_code::corrupt_file,
69 "TPI Stream does not contain a header.");
Zachary Turnerf5c59652016-05-03 00:28:21 +000070
Zachary Turner8dbe3622016-05-27 01:54:44 +000071 if (Reader.readObject(Header))
Zachary Turner819e77d2016-05-06 20:51:57 +000072 return make_error<RawError>(raw_error_code::corrupt_file,
73 "TPI Stream does not contain a header.");
Zachary Turnerf5c59652016-05-03 00:28:21 +000074
75 if (Header->Version != PdbTpiV80)
Zachary Turner819e77d2016-05-06 20:51:57 +000076 return make_error<RawError>(raw_error_code::corrupt_file,
77 "Unsupported TPI Version.");
Zachary Turnerf5c59652016-05-03 00:28:21 +000078
79 if (Header->HeaderSize != sizeof(HeaderInfo))
Zachary Turner819e77d2016-05-06 20:51:57 +000080 return make_error<RawError>(raw_error_code::corrupt_file,
81 "Corrupt TPI Header size.");
Zachary Turnerf5c59652016-05-03 00:28:21 +000082
83 if (Header->HashKeySize != sizeof(ulittle32_t))
Zachary Turner819e77d2016-05-06 20:51:57 +000084 return make_error<RawError>(raw_error_code::corrupt_file,
85 "TPI Stream expected 4 byte hash key size.");
Zachary Turnerf5c59652016-05-03 00:28:21 +000086
87 if (Header->NumHashBuckets < MinHashBuckets ||
88 Header->NumHashBuckets > MaxHashBuckets)
Zachary Turner819e77d2016-05-06 20:51:57 +000089 return make_error<RawError>(raw_error_code::corrupt_file,
90 "TPI Stream Invalid number of hash buckets.");
Zachary Turnerf5c59652016-05-03 00:28:21 +000091
92 HashFunction = HashBufferV8;
93
94 // The actual type records themselves come from this stream
Zachary Turner0d43c1c2016-05-28 05:21:57 +000095 if (auto EC = Reader.readArray(TypeRecords, Header->TypeRecordBytes))
Zachary Turner819e77d2016-05-06 20:51:57 +000096 return EC;
Zachary Turnerf5c59652016-05-03 00:28:21 +000097
98 // Hash indices, hash values, etc come from the hash stream.
99 MappedBlockStream HS(Header->HashStreamIndex, Pdb);
Zachary Turnerd5d37dc2016-05-25 20:37:03 +0000100 codeview::StreamReader HSR(HS);
Zachary Turnerf5c59652016-05-03 00:28:21 +0000101 HSR.setOffset(Header->HashValueBuffer.Off);
Zachary Turner819e77d2016-05-06 20:51:57 +0000102 if (auto EC =
Zachary Turner8dbe3622016-05-27 01:54:44 +0000103 HSR.readStreamRef(HashValuesBuffer, Header->HashValueBuffer.Length))
Zachary Turner819e77d2016-05-06 20:51:57 +0000104 return EC;
Zachary Turnerf5c59652016-05-03 00:28:21 +0000105
106 HSR.setOffset(Header->HashAdjBuffer.Off);
Zachary Turner8dbe3622016-05-27 01:54:44 +0000107 if (auto EC = HSR.readStreamRef(HashAdjBuffer, Header->HashAdjBuffer.Length))
Zachary Turner819e77d2016-05-06 20:51:57 +0000108 return EC;
Zachary Turnerf5c59652016-05-03 00:28:21 +0000109
110 HSR.setOffset(Header->IndexOffsetBuffer.Off);
Zachary Turner8dbe3622016-05-27 01:54:44 +0000111 if (auto EC = HSR.readStreamRef(TypeIndexOffsetBuffer,
112 Header->IndexOffsetBuffer.Length))
Zachary Turner819e77d2016-05-06 20:51:57 +0000113 return EC;
Zachary Turnerf5c59652016-05-03 00:28:21 +0000114
Zachary Turner819e77d2016-05-06 20:51:57 +0000115 return Error::success();
Zachary Turnerf5c59652016-05-03 00:28:21 +0000116}
117
118PdbRaw_TpiVer TpiStream::getTpiVersion() const {
119 uint32_t Value = Header->Version;
120 return static_cast<PdbRaw_TpiVer>(Value);
121}
122
123uint32_t TpiStream::TypeIndexBegin() const { return Header->TypeIndexBegin; }
124
125uint32_t TpiStream::TypeIndexEnd() const { return Header->TypeIndexEnd; }
126
127uint32_t TpiStream::NumTypeRecords() const {
128 return TypeIndexEnd() - TypeIndexBegin();
129}
130
Zachary Turner85ed80b2016-05-25 03:43:17 +0000131uint16_t TpiStream::getTypeHashStreamIndex() const {
132 return Header->HashStreamIndex;
133}
134
135uint16_t TpiStream::getTypeHashStreamAuxIndex() const {
136 return Header->HashAuxStreamIndex;
137}
138
Zachary Turner0d43c1c2016-05-28 05:21:57 +0000139iterator_range<codeview::CVTypeArray::Iterator>
140TpiStream::types(bool *HadError) const {
141 return llvm::make_range(TypeRecords.begin(HadError), TypeRecords.end());
Zachary Turnerf5c59652016-05-03 00:28:21 +0000142}