blob: fc5e7ce5a836bf3b877befab723538ec31240e27 [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
Rui Ueyama8b0ae132016-06-16 13:14:42 +000012#include "llvm/DebugInfo/CodeView/CVTypeVisitor.h"
Zachary Turnerf5c59652016-05-03 00:28:21 +000013#include "llvm/DebugInfo/CodeView/CodeView.h"
Zachary Turner5e3e4bb2016-08-05 21:45:34 +000014#include "llvm/DebugInfo/CodeView/TypeDeserializer.h"
Rui Ueyamafd97bf12016-06-03 20:48:51 +000015#include "llvm/DebugInfo/CodeView/TypeIndex.h"
Zachary Turnerf5c59652016-05-03 00:28:21 +000016#include "llvm/DebugInfo/CodeView/TypeRecord.h"
Zachary Turner2f951ce2016-08-31 21:42:26 +000017#include "llvm/DebugInfo/CodeView/TypeVisitorCallbackPipeline.h"
Zachary Turnera3225b02016-07-29 20:56:36 +000018#include "llvm/DebugInfo/MSF/MappedBlockStream.h"
19#include "llvm/DebugInfo/MSF/StreamReader.h"
Zachary Turner90b8b8d2016-05-31 22:41:52 +000020#include "llvm/DebugInfo/PDB/Raw/PDBFile.h"
Zachary Turnerf5c59652016-05-03 00:28:21 +000021#include "llvm/DebugInfo/PDB/Raw/RawConstants.h"
Zachary Turner819e77d2016-05-06 20:51:57 +000022#include "llvm/DebugInfo/PDB/Raw/RawError.h"
Rui Ueyamafd97bf12016-06-03 20:48:51 +000023#include "llvm/DebugInfo/PDB/Raw/RawTypes.h"
Zachary Turner620961d2016-09-14 23:00:02 +000024#include "llvm/DebugInfo/PDB/Raw/TpiHashing.h"
Zachary Turnerf5c59652016-05-03 00:28:21 +000025
26#include "llvm/Support/Endian.h"
27
28using namespace llvm;
Rui Ueyama8b0ae132016-06-16 13:14:42 +000029using namespace llvm::codeview;
Zachary Turnerf5c59652016-05-03 00:28:21 +000030using namespace llvm::support;
Zachary Turnerbac69d32016-07-22 19:56:05 +000031using namespace llvm::msf;
Zachary Turnerf5c59652016-05-03 00:28:21 +000032using namespace llvm::pdb;
33
Zachary Turnera1657a92016-06-08 17:26:39 +000034TpiStream::TpiStream(const PDBFile &File,
35 std::unique_ptr<MappedBlockStream> Stream)
Rui Ueyama5c7248c2016-06-16 13:48:16 +000036 : Pdb(File), Stream(std::move(Stream)) {}
Zachary Turnerf5c59652016-05-03 00:28:21 +000037
38TpiStream::~TpiStream() {}
39
Rui Ueyamac41cd6d2016-06-09 00:10:19 +000040// Verifies that a given type record matches with a given hash value.
41// Currently we only verify SRC_LINE records.
Rui Ueyama8b0ae132016-06-16 13:14:42 +000042Error TpiStream::verifyHashValues() {
43 TpiHashVerifier Verifier(HashValues, Header->NumHashBuckets);
Zachary Turner2f951ce2016-08-31 21:42:26 +000044 TypeDeserializer Deserializer;
45
46 TypeVisitorCallbackPipeline Pipeline;
47 Pipeline.addCallbackToPipeline(Deserializer);
48 Pipeline.addCallbackToPipeline(Verifier);
49
50 CVTypeVisitor Visitor(Pipeline);
Zachary Turner01ee3dae2016-06-16 18:22:27 +000051 return Visitor.visitTypeStream(TypeRecords);
Rui Ueyamac41cd6d2016-06-09 00:10:19 +000052}
53
Zachary Turner819e77d2016-05-06 20:51:57 +000054Error TpiStream::reload() {
Rui Ueyama9caea822016-06-16 13:17:59 +000055 StreamReader Reader(*Stream);
Zachary Turnerf5c59652016-05-03 00:28:21 +000056
Zachary Turnerc6d54da2016-09-09 17:46:17 +000057 if (Reader.bytesRemaining() < sizeof(TpiStreamHeader))
Zachary Turner819e77d2016-05-06 20:51:57 +000058 return make_error<RawError>(raw_error_code::corrupt_file,
59 "TPI Stream does not contain a header.");
Zachary Turnerf5c59652016-05-03 00:28:21 +000060
Zachary Turner8dbe3622016-05-27 01:54:44 +000061 if (Reader.readObject(Header))
Zachary Turner819e77d2016-05-06 20:51:57 +000062 return make_error<RawError>(raw_error_code::corrupt_file,
63 "TPI Stream does not contain a header.");
Zachary Turnerf5c59652016-05-03 00:28:21 +000064
65 if (Header->Version != PdbTpiV80)
Zachary Turner819e77d2016-05-06 20:51:57 +000066 return make_error<RawError>(raw_error_code::corrupt_file,
67 "Unsupported TPI Version.");
Zachary Turnerf5c59652016-05-03 00:28:21 +000068
Zachary Turnerc6d54da2016-09-09 17:46:17 +000069 if (Header->HeaderSize != sizeof(TpiStreamHeader))
Zachary Turner819e77d2016-05-06 20:51:57 +000070 return make_error<RawError>(raw_error_code::corrupt_file,
71 "Corrupt TPI Header size.");
Zachary Turnerf5c59652016-05-03 00:28:21 +000072
73 if (Header->HashKeySize != sizeof(ulittle32_t))
Zachary Turner819e77d2016-05-06 20:51:57 +000074 return make_error<RawError>(raw_error_code::corrupt_file,
75 "TPI Stream expected 4 byte hash key size.");
Zachary Turnerf5c59652016-05-03 00:28:21 +000076
Zachary Turnerc6d54da2016-09-09 17:46:17 +000077 if (Header->NumHashBuckets < MinTpiHashBuckets ||
78 Header->NumHashBuckets > MaxTpiHashBuckets)
Zachary Turner819e77d2016-05-06 20:51:57 +000079 return make_error<RawError>(raw_error_code::corrupt_file,
80 "TPI Stream Invalid number of hash buckets.");
Zachary Turnerf5c59652016-05-03 00:28:21 +000081
Zachary Turnerf5c59652016-05-03 00:28:21 +000082 // The actual type records themselves come from this stream
Zachary Turner0d43c1c2016-05-28 05:21:57 +000083 if (auto EC = Reader.readArray(TypeRecords, Header->TypeRecordBytes))
Zachary Turner819e77d2016-05-06 20:51:57 +000084 return EC;
Zachary Turnerf5c59652016-05-03 00:28:21 +000085
86 // Hash indices, hash values, etc come from the hash stream.
Zachary Turnerc6d54da2016-09-09 17:46:17 +000087 if (Header->HashStreamIndex != kInvalidStreamIndex) {
88 if (Header->HashStreamIndex >= Pdb.getNumStreams())
89 return make_error<RawError>(raw_error_code::corrupt_file,
90 "Invalid TPI hash stream index.");
Rui Ueyamaba0aab92016-06-06 23:19:23 +000091
Zachary Turnerc6d54da2016-09-09 17:46:17 +000092 auto HS = MappedBlockStream::createIndexedStream(
93 Pdb.getMsfLayout(), Pdb.getMsfBuffer(), Header->HashStreamIndex);
94 StreamReader HSR(*HS);
Zachary Turnerf5c59652016-05-03 00:28:21 +000095
Zachary Turnerc6d54da2016-09-09 17:46:17 +000096 uint32_t NumHashValues =
97 Header->HashValueBuffer.Length / sizeof(ulittle32_t);
98 if (NumHashValues != NumTypeRecords())
99 return make_error<RawError>(
100 raw_error_code::corrupt_file,
101 "TPI hash count does not match with the number of type records.");
102 HSR.setOffset(Header->HashValueBuffer.Off);
103 if (auto EC = HSR.readArray(HashValues, NumHashValues))
104 return EC;
Zachary Turner620961d2016-09-14 23:00:02 +0000105 std::vector<ulittle32_t> HashValueList;
106 for (auto I : HashValues)
107 HashValueList.push_back(I);
Rui Ueyamafd97bf12016-06-03 20:48:51 +0000108
Zachary Turnerc6d54da2016-09-09 17:46:17 +0000109 HSR.setOffset(Header->IndexOffsetBuffer.Off);
110 uint32_t NumTypeIndexOffsets =
111 Header->IndexOffsetBuffer.Length / sizeof(TypeIndexOffset);
112 if (auto EC = HSR.readArray(TypeIndexOffsets, NumTypeIndexOffsets))
113 return EC;
Zachary Turnerf5c59652016-05-03 00:28:21 +0000114
Zachary Turnerc6d54da2016-09-09 17:46:17 +0000115 HSR.setOffset(Header->HashAdjBuffer.Off);
116 uint32_t NumHashAdjustments =
117 Header->HashAdjBuffer.Length / sizeof(TypeIndexOffset);
118 if (auto EC = HSR.readArray(HashAdjustments, NumHashAdjustments))
119 return EC;
Rui Ueyamac41cd6d2016-06-09 00:10:19 +0000120
Zachary Turnerc6d54da2016-09-09 17:46:17 +0000121 HashStream = std::move(HS);
122
123 // TPI hash table is a parallel array for the type records.
124 // Verify that the hash values match with type records.
125 if (auto EC = verifyHashValues())
126 return EC;
127 }
Rui Ueyamac41cd6d2016-06-09 00:10:19 +0000128
Zachary Turner819e77d2016-05-06 20:51:57 +0000129 return Error::success();
Zachary Turnerf5c59652016-05-03 00:28:21 +0000130}
131
132PdbRaw_TpiVer TpiStream::getTpiVersion() const {
133 uint32_t Value = Header->Version;
134 return static_cast<PdbRaw_TpiVer>(Value);
135}
136
137uint32_t TpiStream::TypeIndexBegin() const { return Header->TypeIndexBegin; }
138
139uint32_t TpiStream::TypeIndexEnd() const { return Header->TypeIndexEnd; }
140
141uint32_t TpiStream::NumTypeRecords() const {
142 return TypeIndexEnd() - TypeIndexBegin();
143}
144
Zachary Turner85ed80b2016-05-25 03:43:17 +0000145uint16_t TpiStream::getTypeHashStreamIndex() const {
146 return Header->HashStreamIndex;
147}
148
149uint16_t TpiStream::getTypeHashStreamAuxIndex() const {
150 return Header->HashAuxStreamIndex;
151}
152
Rui Ueyamaf14a74c2016-06-07 23:53:43 +0000153uint32_t TpiStream::NumHashBuckets() const { return Header->NumHashBuckets; }
Rui Ueyamad8339172016-06-07 23:44:27 +0000154uint32_t TpiStream::getHashKeySize() const { return Header->HashKeySize; }
155
Rui Ueyama9caea822016-06-16 13:17:59 +0000156FixedStreamArray<support::ulittle32_t>
Rui Ueyamafd97bf12016-06-03 20:48:51 +0000157TpiStream::getHashValues() const {
158 return HashValues;
159}
160
Rui Ueyama9caea822016-06-16 13:17:59 +0000161FixedStreamArray<TypeIndexOffset>
Rui Ueyamafd97bf12016-06-03 20:48:51 +0000162TpiStream::getTypeIndexOffsets() const {
163 return TypeIndexOffsets;
164}
165
Rui Ueyama9caea822016-06-16 13:17:59 +0000166FixedStreamArray<TypeIndexOffset>
Rui Ueyamafd97bf12016-06-03 20:48:51 +0000167TpiStream::getHashAdjustments() const {
168 return HashAdjustments;
169}
170
Rui Ueyama9caea822016-06-16 13:17:59 +0000171iterator_range<CVTypeArray::Iterator>
Zachary Turner0d43c1c2016-05-28 05:21:57 +0000172TpiStream::types(bool *HadError) const {
173 return llvm::make_range(TypeRecords.begin(HadError), TypeRecords.end());
Zachary Turnerf5c59652016-05-03 00:28:21 +0000174}
Zachary Turner8848a7a2016-07-06 18:05:57 +0000175
176Error TpiStream::commit() { return Error::success(); }