blob: 5fef3edf8c2db45e5b4056ccc05080f5979b0168 [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 Turnera3225b02016-07-29 20:56:36 +000016#include "llvm/DebugInfo/MSF/MappedBlockStream.h"
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +000017#include "llvm/DebugInfo/PDB/Native/PDBFile.h"
Zachary Turner7b327d02017-02-16 23:35:45 +000018#include "llvm/DebugInfo/PDB/Native/PDBTypeServerHandler.h"
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +000019#include "llvm/DebugInfo/PDB/Native/RawConstants.h"
20#include "llvm/DebugInfo/PDB/Native/RawError.h"
21#include "llvm/DebugInfo/PDB/Native/RawTypes.h"
22#include "llvm/DebugInfo/PDB/Native/TpiHashing.h"
Zachary Turnerd9dc2822017-03-02 20:52:51 +000023#include "llvm/Support/BinaryStreamReader.h"
Zachary Turnerf5c59652016-05-03 00:28:21 +000024#include "llvm/Support/Endian.h"
Eugene Zelenko570e39a2016-11-23 23:16:32 +000025#include "llvm/Support/Error.h"
26#include <algorithm>
27#include <cstdint>
28#include <vector>
Zachary Turnerf5c59652016-05-03 00:28:21 +000029
30using namespace llvm;
Rui Ueyama8b0ae132016-06-16 13:14:42 +000031using namespace llvm::codeview;
Zachary Turnerf5c59652016-05-03 00:28:21 +000032using namespace llvm::support;
Zachary Turnerbac69d32016-07-22 19:56:05 +000033using namespace llvm::msf;
Zachary Turnerf5c59652016-05-03 00:28:21 +000034using namespace llvm::pdb;
35
Zachary Turnera1657a92016-06-08 17:26:39 +000036TpiStream::TpiStream(const PDBFile &File,
37 std::unique_ptr<MappedBlockStream> Stream)
Rui Ueyama5c7248c2016-06-16 13:48:16 +000038 : Pdb(File), Stream(std::move(Stream)) {}
Zachary Turnerf5c59652016-05-03 00:28:21 +000039
Eugene Zelenko570e39a2016-11-23 23:16:32 +000040TpiStream::~TpiStream() = default;
Zachary Turnerf5c59652016-05-03 00:28:21 +000041
Rui Ueyamac41cd6d2016-06-09 00:10:19 +000042// Verifies that a given type record matches with a given hash value.
43// Currently we only verify SRC_LINE records.
Rui Ueyama8b0ae132016-06-16 13:14:42 +000044Error TpiStream::verifyHashValues() {
45 TpiHashVerifier Verifier(HashValues, Header->NumHashBuckets);
Zachary Turner2f951ce2016-08-31 21:42:26 +000046 TypeDeserializer Deserializer;
47
48 TypeVisitorCallbackPipeline Pipeline;
49 Pipeline.addCallbackToPipeline(Deserializer);
50 Pipeline.addCallbackToPipeline(Verifier);
51
52 CVTypeVisitor Visitor(Pipeline);
Zachary Turner01ee3dae2016-06-16 18:22:27 +000053 return Visitor.visitTypeStream(TypeRecords);
Rui Ueyamac41cd6d2016-06-09 00:10:19 +000054}
55
Zachary Turner819e77d2016-05-06 20:51:57 +000056Error TpiStream::reload() {
Zachary Turner120faca2017-02-27 22:11:43 +000057 BinaryStreamReader Reader(*Stream);
Zachary Turnerf5c59652016-05-03 00:28:21 +000058
Zachary Turnerc6d54da2016-09-09 17:46:17 +000059 if (Reader.bytesRemaining() < sizeof(TpiStreamHeader))
Zachary Turner819e77d2016-05-06 20:51:57 +000060 return make_error<RawError>(raw_error_code::corrupt_file,
61 "TPI Stream does not contain a header.");
Zachary Turnerf5c59652016-05-03 00:28:21 +000062
Zachary Turner8dbe3622016-05-27 01:54:44 +000063 if (Reader.readObject(Header))
Zachary Turner819e77d2016-05-06 20:51:57 +000064 return make_error<RawError>(raw_error_code::corrupt_file,
65 "TPI Stream does not contain a header.");
Zachary Turnerf5c59652016-05-03 00:28:21 +000066
67 if (Header->Version != PdbTpiV80)
Zachary Turner819e77d2016-05-06 20:51:57 +000068 return make_error<RawError>(raw_error_code::corrupt_file,
69 "Unsupported TPI Version.");
Zachary Turnerf5c59652016-05-03 00:28:21 +000070
Zachary Turnerc6d54da2016-09-09 17:46:17 +000071 if (Header->HeaderSize != sizeof(TpiStreamHeader))
Zachary Turner819e77d2016-05-06 20:51:57 +000072 return make_error<RawError>(raw_error_code::corrupt_file,
73 "Corrupt TPI Header size.");
Zachary Turnerf5c59652016-05-03 00:28:21 +000074
75 if (Header->HashKeySize != sizeof(ulittle32_t))
Zachary Turner819e77d2016-05-06 20:51:57 +000076 return make_error<RawError>(raw_error_code::corrupt_file,
77 "TPI Stream expected 4 byte hash key size.");
Zachary Turnerf5c59652016-05-03 00:28:21 +000078
Zachary Turnerc6d54da2016-09-09 17:46:17 +000079 if (Header->NumHashBuckets < MinTpiHashBuckets ||
80 Header->NumHashBuckets > MaxTpiHashBuckets)
Zachary Turner819e77d2016-05-06 20:51:57 +000081 return make_error<RawError>(raw_error_code::corrupt_file,
82 "TPI Stream Invalid number of hash buckets.");
Zachary Turnerf5c59652016-05-03 00:28:21 +000083
Zachary Turnerf5c59652016-05-03 00:28:21 +000084 // The actual type records themselves come from this stream
Zachary Turner0d43c1c2016-05-28 05:21:57 +000085 if (auto EC = Reader.readArray(TypeRecords, Header->TypeRecordBytes))
Zachary Turner819e77d2016-05-06 20:51:57 +000086 return EC;
Zachary Turnerf5c59652016-05-03 00:28:21 +000087
88 // Hash indices, hash values, etc come from the hash stream.
Zachary Turnerc6d54da2016-09-09 17:46:17 +000089 if (Header->HashStreamIndex != kInvalidStreamIndex) {
90 if (Header->HashStreamIndex >= Pdb.getNumStreams())
91 return make_error<RawError>(raw_error_code::corrupt_file,
92 "Invalid TPI hash stream index.");
Rui Ueyamaba0aab92016-06-06 23:19:23 +000093
Zachary Turnerc6d54da2016-09-09 17:46:17 +000094 auto HS = MappedBlockStream::createIndexedStream(
95 Pdb.getMsfLayout(), Pdb.getMsfBuffer(), Header->HashStreamIndex);
Zachary Turner120faca2017-02-27 22:11:43 +000096 BinaryStreamReader HSR(*HS);
Zachary Turnerf5c59652016-05-03 00:28:21 +000097
Reid Kleckner6e545ff2017-04-11 16:26:15 +000098 // There should be a hash value for every type record, or no hashes at all.
Zachary Turnerc6d54da2016-09-09 17:46:17 +000099 uint32_t NumHashValues =
100 Header->HashValueBuffer.Length / sizeof(ulittle32_t);
Reid Kleckner6e545ff2017-04-11 16:26:15 +0000101 if (NumHashValues != NumTypeRecords() && NumHashValues != 0)
Zachary Turnerc6d54da2016-09-09 17:46:17 +0000102 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.
Reid Kleckner6e545ff2017-04-11 16:26:15 +0000128 if (NumHashValues > 0)
129 if (auto EC = verifyHashValues())
130 return EC;
Zachary Turnerc6d54da2016-09-09 17:46:17 +0000131 }
Rui Ueyamac41cd6d2016-06-09 00:10:19 +0000132
Zachary Turner819e77d2016-05-06 20:51:57 +0000133 return Error::success();
Zachary Turnerf5c59652016-05-03 00:28:21 +0000134}
135
136PdbRaw_TpiVer TpiStream::getTpiVersion() const {
137 uint32_t Value = Header->Version;
138 return static_cast<PdbRaw_TpiVer>(Value);
139}
140
141uint32_t TpiStream::TypeIndexBegin() const { return Header->TypeIndexBegin; }
142
143uint32_t TpiStream::TypeIndexEnd() const { return Header->TypeIndexEnd; }
144
145uint32_t TpiStream::NumTypeRecords() const {
146 return TypeIndexEnd() - TypeIndexBegin();
147}
148
Zachary Turner85ed80b2016-05-25 03:43:17 +0000149uint16_t TpiStream::getTypeHashStreamIndex() const {
150 return Header->HashStreamIndex;
151}
152
153uint16_t TpiStream::getTypeHashStreamAuxIndex() const {
154 return Header->HashAuxStreamIndex;
155}
156
Rui Ueyamaf14a74c2016-06-07 23:53:43 +0000157uint32_t TpiStream::NumHashBuckets() const { return Header->NumHashBuckets; }
Rui Ueyamad8339172016-06-07 23:44:27 +0000158uint32_t TpiStream::getHashKeySize() const { return Header->HashKeySize; }
159
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +0000160FixedStreamArray<support::ulittle32_t> TpiStream::getHashValues() const {
Rui Ueyamafd97bf12016-06-03 20:48:51 +0000161 return HashValues;
162}
163
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +0000164FixedStreamArray<TypeIndexOffset> TpiStream::getTypeIndexOffsets() const {
Rui Ueyamafd97bf12016-06-03 20:48:51 +0000165 return TypeIndexOffsets;
166}
167
Zachary Turner29da5db2017-01-25 21:17:40 +0000168HashTable &TpiStream::getHashAdjusters() { return HashAdjusters; }
Rui Ueyamafd97bf12016-06-03 20:48:51 +0000169
Zachary Turner7b327d02017-02-16 23:35:45 +0000170CVTypeRange TpiStream::types(bool *HadError) const {
Eugene Zelenko570e39a2016-11-23 23:16:32 +0000171 return make_range(TypeRecords.begin(HadError), TypeRecords.end());
Zachary Turnerf5c59652016-05-03 00:28:21 +0000172}
Zachary Turner8848a7a2016-07-06 18:05:57 +0000173
174Error TpiStream::commit() { return Error::success(); }