blob: 74a0427480e558670e5613b520c7e01edd1ef86f [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"
Zachary Turner90b8b8d2016-05-31 22:41:52 +000016#include "llvm/DebugInfo/PDB/Raw/PDBFile.h"
Zachary Turnerf5c59652016-05-03 00:28:21 +000017#include "llvm/DebugInfo/PDB/Raw/RawConstants.h"
Zachary Turner819e77d2016-05-06 20:51:57 +000018#include "llvm/DebugInfo/PDB/Raw/RawError.h"
Zachary Turnerf5c59652016-05-03 00:28:21 +000019
20#include "llvm/Support/Endian.h"
21
22using namespace llvm;
23using namespace llvm::support;
24using namespace llvm::pdb;
25
26namespace {
Zachary Turnerf5c59652016-05-03 00:28:21 +000027const uint32_t MinHashBuckets = 0x1000;
28const uint32_t MaxHashBuckets = 0x40000;
29}
30
31static uint32_t HashBufferV8(uint8_t *buffer, uint32_t NumBuckets) {
32 // Not yet implemented, this is probably some variation of CRC32 but we need
33 // to be sure of the precise implementation otherwise we won't be able to work
34 // with persisted hash values.
35 return 0;
36}
37
Rui Ueyama0350bf02016-06-02 21:13:47 +000038// This corresponds to `HDR` in PDB/dbi/tpi.h.
Zachary Turnerf5c59652016-05-03 00:28:21 +000039struct TpiStream::HeaderInfo {
40 struct EmbeddedBuf {
41 little32_t Off;
42 ulittle32_t Length;
43 };
44
45 ulittle32_t Version;
46 ulittle32_t HeaderSize;
47 ulittle32_t TypeIndexBegin;
48 ulittle32_t TypeIndexEnd;
49 ulittle32_t TypeRecordBytes;
50
Rui Ueyama0350bf02016-06-02 21:13:47 +000051 // The following members correspond to `TpiHash` in PDB/dbi/tpi.h.
Zachary Turnerf5c59652016-05-03 00:28:21 +000052 ulittle16_t HashStreamIndex;
53 ulittle16_t HashAuxStreamIndex;
54 ulittle32_t HashKeySize;
55 ulittle32_t NumHashBuckets;
56
57 EmbeddedBuf HashValueBuffer;
58 EmbeddedBuf IndexOffsetBuffer;
59 EmbeddedBuf HashAdjBuffer;
60};
61
Zachary Turnerc9972c62016-05-25 04:35:22 +000062TpiStream::TpiStream(PDBFile &File, uint32_t StreamIdx)
63 : Pdb(File), Stream(StreamIdx, File), HashFunction(nullptr) {}
Zachary Turnerf5c59652016-05-03 00:28:21 +000064
65TpiStream::~TpiStream() {}
66
Zachary Turner819e77d2016-05-06 20:51:57 +000067Error TpiStream::reload() {
Zachary Turnerd5d37dc2016-05-25 20:37:03 +000068 codeview::StreamReader Reader(Stream);
Zachary Turnerf5c59652016-05-03 00:28:21 +000069
70 if (Reader.bytesRemaining() < sizeof(HeaderInfo))
Zachary Turner819e77d2016-05-06 20:51:57 +000071 return make_error<RawError>(raw_error_code::corrupt_file,
72 "TPI Stream does not contain a header.");
Zachary Turnerf5c59652016-05-03 00:28:21 +000073
Zachary Turner8dbe3622016-05-27 01:54:44 +000074 if (Reader.readObject(Header))
Zachary Turner819e77d2016-05-06 20:51:57 +000075 return make_error<RawError>(raw_error_code::corrupt_file,
76 "TPI Stream does not contain a header.");
Zachary Turnerf5c59652016-05-03 00:28:21 +000077
78 if (Header->Version != PdbTpiV80)
Zachary Turner819e77d2016-05-06 20:51:57 +000079 return make_error<RawError>(raw_error_code::corrupt_file,
80 "Unsupported TPI Version.");
Zachary Turnerf5c59652016-05-03 00:28:21 +000081
82 if (Header->HeaderSize != sizeof(HeaderInfo))
Zachary Turner819e77d2016-05-06 20:51:57 +000083 return make_error<RawError>(raw_error_code::corrupt_file,
84 "Corrupt TPI Header size.");
Zachary Turnerf5c59652016-05-03 00:28:21 +000085
86 if (Header->HashKeySize != sizeof(ulittle32_t))
Zachary Turner819e77d2016-05-06 20:51:57 +000087 return make_error<RawError>(raw_error_code::corrupt_file,
88 "TPI Stream expected 4 byte hash key size.");
Zachary Turnerf5c59652016-05-03 00:28:21 +000089
90 if (Header->NumHashBuckets < MinHashBuckets ||
91 Header->NumHashBuckets > MaxHashBuckets)
Zachary Turner819e77d2016-05-06 20:51:57 +000092 return make_error<RawError>(raw_error_code::corrupt_file,
93 "TPI Stream Invalid number of hash buckets.");
Zachary Turnerf5c59652016-05-03 00:28:21 +000094
95 HashFunction = HashBufferV8;
96
97 // The actual type records themselves come from this stream
Zachary Turner0d43c1c2016-05-28 05:21:57 +000098 if (auto EC = Reader.readArray(TypeRecords, Header->TypeRecordBytes))
Zachary Turner819e77d2016-05-06 20:51:57 +000099 return EC;
Zachary Turnerf5c59652016-05-03 00:28:21 +0000100
101 // Hash indices, hash values, etc come from the hash stream.
102 MappedBlockStream HS(Header->HashStreamIndex, Pdb);
Zachary Turnerd5d37dc2016-05-25 20:37:03 +0000103 codeview::StreamReader HSR(HS);
Zachary Turnerf5c59652016-05-03 00:28:21 +0000104 HSR.setOffset(Header->HashValueBuffer.Off);
Zachary Turner819e77d2016-05-06 20:51:57 +0000105 if (auto EC =
Zachary Turner8dbe3622016-05-27 01:54:44 +0000106 HSR.readStreamRef(HashValuesBuffer, Header->HashValueBuffer.Length))
Zachary Turner819e77d2016-05-06 20:51:57 +0000107 return EC;
Zachary Turnerf5c59652016-05-03 00:28:21 +0000108
109 HSR.setOffset(Header->HashAdjBuffer.Off);
Zachary Turner8dbe3622016-05-27 01:54:44 +0000110 if (auto EC = HSR.readStreamRef(HashAdjBuffer, Header->HashAdjBuffer.Length))
Zachary Turner819e77d2016-05-06 20:51:57 +0000111 return EC;
Zachary Turnerf5c59652016-05-03 00:28:21 +0000112
113 HSR.setOffset(Header->IndexOffsetBuffer.Off);
Zachary Turner8dbe3622016-05-27 01:54:44 +0000114 if (auto EC = HSR.readStreamRef(TypeIndexOffsetBuffer,
115 Header->IndexOffsetBuffer.Length))
Zachary Turner819e77d2016-05-06 20:51:57 +0000116 return EC;
Zachary Turnerf5c59652016-05-03 00:28:21 +0000117
Zachary Turner819e77d2016-05-06 20:51:57 +0000118 return Error::success();
Zachary Turnerf5c59652016-05-03 00:28:21 +0000119}
120
121PdbRaw_TpiVer TpiStream::getTpiVersion() const {
122 uint32_t Value = Header->Version;
123 return static_cast<PdbRaw_TpiVer>(Value);
124}
125
126uint32_t TpiStream::TypeIndexBegin() const { return Header->TypeIndexBegin; }
127
128uint32_t TpiStream::TypeIndexEnd() const { return Header->TypeIndexEnd; }
129
130uint32_t TpiStream::NumTypeRecords() const {
131 return TypeIndexEnd() - TypeIndexBegin();
132}
133
Zachary Turner85ed80b2016-05-25 03:43:17 +0000134uint16_t TpiStream::getTypeHashStreamIndex() const {
135 return Header->HashStreamIndex;
136}
137
138uint16_t TpiStream::getTypeHashStreamAuxIndex() const {
139 return Header->HashAuxStreamIndex;
140}
141
Zachary Turner0d43c1c2016-05-28 05:21:57 +0000142iterator_range<codeview::CVTypeArray::Iterator>
143TpiStream::types(bool *HadError) const {
144 return llvm::make_range(TypeRecords.begin(HadError), TypeRecords.end());
Zachary Turnerf5c59652016-05-03 00:28:21 +0000145}