blob: 954374ca4dfef8a81368f31caa7e1e2fa1648189 [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
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +000010#include "llvm/DebugInfo/PDB/Native/TpiStream.h"
Eugene Zelenko570e39a2016-11-23 23:16:32 +000011#include "llvm/ADT/iterator_range.h"
Rui Ueyama8b0ae132016-06-16 13:14:42 +000012#include "llvm/DebugInfo/CodeView/CVTypeVisitor.h"
Zachary Turner5e3e4bb2016-08-05 21:45:34 +000013#include "llvm/DebugInfo/CodeView/TypeDeserializer.h"
Zachary Turnerf5c59652016-05-03 00:28:21 +000014#include "llvm/DebugInfo/CodeView/TypeRecord.h"
Zachary Turner2f951ce2016-08-31 21:42:26 +000015#include "llvm/DebugInfo/CodeView/TypeVisitorCallbackPipeline.h"
Zachary Turnerd2684b72017-02-25 00:33:34 +000016#include "llvm/DebugInfo/MSF/BinaryStreamReader.h"
Zachary Turneraf299ea2017-02-25 00:44:30 +000017#include "llvm/DebugInfo/MSF/BinaryStreamReader.h"
Zachary Turnera3225b02016-07-29 20:56:36 +000018#include "llvm/DebugInfo/MSF/MappedBlockStream.h"
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +000019#include "llvm/DebugInfo/PDB/Native/PDBFile.h"
Zachary Turner7b327d02017-02-16 23:35:45 +000020#include "llvm/DebugInfo/PDB/Native/PDBTypeServerHandler.h"
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +000021#include "llvm/DebugInfo/PDB/Native/RawConstants.h"
22#include "llvm/DebugInfo/PDB/Native/RawError.h"
23#include "llvm/DebugInfo/PDB/Native/RawTypes.h"
24#include "llvm/DebugInfo/PDB/Native/TpiHashing.h"
Zachary Turnerf5c59652016-05-03 00:28:21 +000025#include "llvm/Support/Endian.h"
Eugene Zelenko570e39a2016-11-23 23:16:32 +000026#include "llvm/Support/Error.h"
27#include <algorithm>
28#include <cstdint>
29#include <vector>
Zachary Turnerf5c59652016-05-03 00:28:21 +000030
31using namespace llvm;
Rui Ueyama8b0ae132016-06-16 13:14:42 +000032using namespace llvm::codeview;
Zachary Turnerf5c59652016-05-03 00:28:21 +000033using namespace llvm::support;
Zachary Turnerbac69d32016-07-22 19:56:05 +000034using namespace llvm::msf;
Zachary Turnerf5c59652016-05-03 00:28:21 +000035using namespace llvm::pdb;
36
Zachary Turnera1657a92016-06-08 17:26:39 +000037TpiStream::TpiStream(const PDBFile &File,
38 std::unique_ptr<MappedBlockStream> Stream)
Rui Ueyama5c7248c2016-06-16 13:48:16 +000039 : Pdb(File), Stream(std::move(Stream)) {}
Zachary Turnerf5c59652016-05-03 00:28:21 +000040
Eugene Zelenko570e39a2016-11-23 23:16:32 +000041TpiStream::~TpiStream() = default;
Zachary Turnerf5c59652016-05-03 00:28:21 +000042
Rui Ueyamac41cd6d2016-06-09 00:10:19 +000043// Verifies that a given type record matches with a given hash value.
44// Currently we only verify SRC_LINE records.
Rui Ueyama8b0ae132016-06-16 13:14:42 +000045Error TpiStream::verifyHashValues() {
46 TpiHashVerifier Verifier(HashValues, Header->NumHashBuckets);
Zachary Turner2f951ce2016-08-31 21:42:26 +000047 TypeDeserializer Deserializer;
48
49 TypeVisitorCallbackPipeline Pipeline;
50 Pipeline.addCallbackToPipeline(Deserializer);
51 Pipeline.addCallbackToPipeline(Verifier);
52
53 CVTypeVisitor Visitor(Pipeline);
Zachary Turner01ee3dae2016-06-16 18:22:27 +000054 return Visitor.visitTypeStream(TypeRecords);
Rui Ueyamac41cd6d2016-06-09 00:10:19 +000055}
56
Zachary Turner819e77d2016-05-06 20:51:57 +000057Error TpiStream::reload() {
Zachary Turneraf299ea2017-02-25 00:44:30 +000058 BinaryStreamReader Reader(*Stream);
Zachary Turnerf5c59652016-05-03 00:28:21 +000059
Zachary Turnerc6d54da2016-09-09 17:46:17 +000060 if (Reader.bytesRemaining() < sizeof(TpiStreamHeader))
Zachary Turner819e77d2016-05-06 20:51:57 +000061 return make_error<RawError>(raw_error_code::corrupt_file,
62 "TPI Stream does not contain a header.");
Zachary Turnerf5c59652016-05-03 00:28:21 +000063
Zachary Turner8dbe3622016-05-27 01:54:44 +000064 if (Reader.readObject(Header))
Zachary Turner819e77d2016-05-06 20:51:57 +000065 return make_error<RawError>(raw_error_code::corrupt_file,
66 "TPI Stream does not contain a header.");
Zachary Turnerf5c59652016-05-03 00:28:21 +000067
68 if (Header->Version != PdbTpiV80)
Zachary Turner819e77d2016-05-06 20:51:57 +000069 return make_error<RawError>(raw_error_code::corrupt_file,
70 "Unsupported TPI Version.");
Zachary Turnerf5c59652016-05-03 00:28:21 +000071
Zachary Turnerc6d54da2016-09-09 17:46:17 +000072 if (Header->HeaderSize != sizeof(TpiStreamHeader))
Zachary Turner819e77d2016-05-06 20:51:57 +000073 return make_error<RawError>(raw_error_code::corrupt_file,
74 "Corrupt TPI Header size.");
Zachary Turnerf5c59652016-05-03 00:28:21 +000075
76 if (Header->HashKeySize != sizeof(ulittle32_t))
Zachary Turner819e77d2016-05-06 20:51:57 +000077 return make_error<RawError>(raw_error_code::corrupt_file,
78 "TPI Stream expected 4 byte hash key size.");
Zachary Turnerf5c59652016-05-03 00:28:21 +000079
Zachary Turnerc6d54da2016-09-09 17:46:17 +000080 if (Header->NumHashBuckets < MinTpiHashBuckets ||
81 Header->NumHashBuckets > MaxTpiHashBuckets)
Zachary Turner819e77d2016-05-06 20:51:57 +000082 return make_error<RawError>(raw_error_code::corrupt_file,
83 "TPI Stream Invalid number of hash buckets.");
Zachary Turnerf5c59652016-05-03 00:28:21 +000084
Zachary Turnerf5c59652016-05-03 00:28:21 +000085 // The actual type records themselves come from this stream
Zachary Turner0d43c1c2016-05-28 05:21:57 +000086 if (auto EC = Reader.readArray(TypeRecords, Header->TypeRecordBytes))
Zachary Turner819e77d2016-05-06 20:51:57 +000087 return EC;
Zachary Turnerf5c59652016-05-03 00:28:21 +000088
89 // Hash indices, hash values, etc come from the hash stream.
Zachary Turnerc6d54da2016-09-09 17:46:17 +000090 if (Header->HashStreamIndex != kInvalidStreamIndex) {
91 if (Header->HashStreamIndex >= Pdb.getNumStreams())
92 return make_error<RawError>(raw_error_code::corrupt_file,
93 "Invalid TPI hash stream index.");
Rui Ueyamaba0aab92016-06-06 23:19:23 +000094
Zachary Turnerc6d54da2016-09-09 17:46:17 +000095 auto HS = MappedBlockStream::createIndexedStream(
96 Pdb.getMsfLayout(), Pdb.getMsfBuffer(), Header->HashStreamIndex);
Zachary Turneraf299ea2017-02-25 00:44:30 +000097 BinaryStreamReader HSR(*HS);
Zachary Turnerf5c59652016-05-03 00:28:21 +000098
Zachary Turnerc6d54da2016-09-09 17:46:17 +000099 uint32_t NumHashValues =
100 Header->HashValueBuffer.Length / sizeof(ulittle32_t);
101 if (NumHashValues != NumTypeRecords())
102 return make_error<RawError>(
103 raw_error_code::corrupt_file,
104 "TPI hash count does not match with the number of type records.");
105 HSR.setOffset(Header->HashValueBuffer.Off);
106 if (auto EC = HSR.readArray(HashValues, NumHashValues))
107 return EC;
Zachary Turner620961d2016-09-14 23:00:02 +0000108 std::vector<ulittle32_t> HashValueList;
109 for (auto I : HashValues)
110 HashValueList.push_back(I);
Rui Ueyamafd97bf12016-06-03 20:48:51 +0000111
Zachary Turnerc6d54da2016-09-09 17:46:17 +0000112 HSR.setOffset(Header->IndexOffsetBuffer.Off);
113 uint32_t NumTypeIndexOffsets =
114 Header->IndexOffsetBuffer.Length / sizeof(TypeIndexOffset);
115 if (auto EC = HSR.readArray(TypeIndexOffsets, NumTypeIndexOffsets))
116 return EC;
Zachary Turnerf5c59652016-05-03 00:28:21 +0000117
Zachary Turner29da5db2017-01-25 21:17:40 +0000118 if (Header->HashAdjBuffer.Length > 0) {
119 HSR.setOffset(Header->HashAdjBuffer.Off);
120 if (auto EC = HashAdjusters.load(HSR))
121 return EC;
122 }
Rui Ueyamac41cd6d2016-06-09 00:10:19 +0000123
Zachary Turnerc6d54da2016-09-09 17:46:17 +0000124 HashStream = std::move(HS);
125
126 // TPI hash table is a parallel array for the type records.
127 // Verify that the hash values match with type records.
128 if (auto EC = verifyHashValues())
129 return EC;
130 }
Rui Ueyamac41cd6d2016-06-09 00:10:19 +0000131
Zachary Turner819e77d2016-05-06 20:51:57 +0000132 return Error::success();
Zachary Turnerf5c59652016-05-03 00:28:21 +0000133}
134
135PdbRaw_TpiVer TpiStream::getTpiVersion() const {
136 uint32_t Value = Header->Version;
137 return static_cast<PdbRaw_TpiVer>(Value);
138}
139
140uint32_t TpiStream::TypeIndexBegin() const { return Header->TypeIndexBegin; }
141
142uint32_t TpiStream::TypeIndexEnd() const { return Header->TypeIndexEnd; }
143
144uint32_t TpiStream::NumTypeRecords() const {
145 return TypeIndexEnd() - TypeIndexBegin();
146}
147
Zachary Turner85ed80b2016-05-25 03:43:17 +0000148uint16_t TpiStream::getTypeHashStreamIndex() const {
149 return Header->HashStreamIndex;
150}
151
152uint16_t TpiStream::getTypeHashStreamAuxIndex() const {
153 return Header->HashAuxStreamIndex;
154}
155
Rui Ueyamaf14a74c2016-06-07 23:53:43 +0000156uint32_t TpiStream::NumHashBuckets() const { return Header->NumHashBuckets; }
Rui Ueyamad8339172016-06-07 23:44:27 +0000157uint32_t TpiStream::getHashKeySize() const { return Header->HashKeySize; }
158
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +0000159FixedStreamArray<support::ulittle32_t> TpiStream::getHashValues() const {
Rui Ueyamafd97bf12016-06-03 20:48:51 +0000160 return HashValues;
161}
162
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +0000163FixedStreamArray<TypeIndexOffset> TpiStream::getTypeIndexOffsets() const {
Rui Ueyamafd97bf12016-06-03 20:48:51 +0000164 return TypeIndexOffsets;
165}
166
Zachary Turner29da5db2017-01-25 21:17:40 +0000167HashTable &TpiStream::getHashAdjusters() { return HashAdjusters; }
Rui Ueyamafd97bf12016-06-03 20:48:51 +0000168
Zachary Turner7b327d02017-02-16 23:35:45 +0000169CVTypeRange TpiStream::types(bool *HadError) const {
Eugene Zelenko570e39a2016-11-23 23:16:32 +0000170 return make_range(TypeRecords.begin(HadError), TypeRecords.end());
Zachary Turnerf5c59652016-05-03 00:28:21 +0000171}
Zachary Turner8848a7a2016-07-06 18:05:57 +0000172
173Error TpiStream::commit() { return Error::success(); }