blob: c0999d93dbb981f9642f9ed2fff6f09a85fcde1b [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
Zachary Turner819e77d2016-05-06 20:51:57 +000042Error TpiStream::reload() {
Zachary Turner120faca2017-02-27 22:11:43 +000043 BinaryStreamReader Reader(*Stream);
Zachary Turnerf5c59652016-05-03 00:28:21 +000044
Zachary Turnerc6d54da2016-09-09 17:46:17 +000045 if (Reader.bytesRemaining() < sizeof(TpiStreamHeader))
Zachary Turner819e77d2016-05-06 20:51:57 +000046 return make_error<RawError>(raw_error_code::corrupt_file,
47 "TPI Stream does not contain a header.");
Zachary Turnerf5c59652016-05-03 00:28:21 +000048
Zachary Turner8dbe3622016-05-27 01:54:44 +000049 if (Reader.readObject(Header))
Zachary Turner819e77d2016-05-06 20:51:57 +000050 return make_error<RawError>(raw_error_code::corrupt_file,
51 "TPI Stream does not contain a header.");
Zachary Turnerf5c59652016-05-03 00:28:21 +000052
53 if (Header->Version != PdbTpiV80)
Zachary Turner819e77d2016-05-06 20:51:57 +000054 return make_error<RawError>(raw_error_code::corrupt_file,
55 "Unsupported TPI Version.");
Zachary Turnerf5c59652016-05-03 00:28:21 +000056
Zachary Turnerc6d54da2016-09-09 17:46:17 +000057 if (Header->HeaderSize != sizeof(TpiStreamHeader))
Zachary Turner819e77d2016-05-06 20:51:57 +000058 return make_error<RawError>(raw_error_code::corrupt_file,
59 "Corrupt TPI Header size.");
Zachary Turnerf5c59652016-05-03 00:28:21 +000060
61 if (Header->HashKeySize != sizeof(ulittle32_t))
Zachary Turner819e77d2016-05-06 20:51:57 +000062 return make_error<RawError>(raw_error_code::corrupt_file,
63 "TPI Stream expected 4 byte hash key size.");
Zachary Turnerf5c59652016-05-03 00:28:21 +000064
Zachary Turnerc6d54da2016-09-09 17:46:17 +000065 if (Header->NumHashBuckets < MinTpiHashBuckets ||
66 Header->NumHashBuckets > MaxTpiHashBuckets)
Zachary Turner819e77d2016-05-06 20:51:57 +000067 return make_error<RawError>(raw_error_code::corrupt_file,
68 "TPI Stream Invalid number of hash buckets.");
Zachary Turnerf5c59652016-05-03 00:28:21 +000069
Zachary Turnerf5c59652016-05-03 00:28:21 +000070 // The actual type records themselves come from this stream
Zachary Turner0d43c1c2016-05-28 05:21:57 +000071 if (auto EC = Reader.readArray(TypeRecords, Header->TypeRecordBytes))
Zachary Turner819e77d2016-05-06 20:51:57 +000072 return EC;
Zachary Turnerf5c59652016-05-03 00:28:21 +000073
74 // Hash indices, hash values, etc come from the hash stream.
Zachary Turnerc6d54da2016-09-09 17:46:17 +000075 if (Header->HashStreamIndex != kInvalidStreamIndex) {
76 if (Header->HashStreamIndex >= Pdb.getNumStreams())
77 return make_error<RawError>(raw_error_code::corrupt_file,
78 "Invalid TPI hash stream index.");
Rui Ueyamaba0aab92016-06-06 23:19:23 +000079
Zachary Turnerc6d54da2016-09-09 17:46:17 +000080 auto HS = MappedBlockStream::createIndexedStream(
81 Pdb.getMsfLayout(), Pdb.getMsfBuffer(), Header->HashStreamIndex);
Zachary Turner120faca2017-02-27 22:11:43 +000082 BinaryStreamReader HSR(*HS);
Zachary Turnerf5c59652016-05-03 00:28:21 +000083
Reid Kleckner6e545ff2017-04-11 16:26:15 +000084 // There should be a hash value for every type record, or no hashes at all.
Zachary Turnerc6d54da2016-09-09 17:46:17 +000085 uint32_t NumHashValues =
86 Header->HashValueBuffer.Length / sizeof(ulittle32_t);
Zachary Turnerbedc85f2017-05-04 23:53:54 +000087 if (NumHashValues != getNumTypeRecords() && NumHashValues != 0)
Zachary Turnerc6d54da2016-09-09 17:46:17 +000088 return make_error<RawError>(
89 raw_error_code::corrupt_file,
90 "TPI hash count does not match with the number of type records.");
91 HSR.setOffset(Header->HashValueBuffer.Off);
92 if (auto EC = HSR.readArray(HashValues, NumHashValues))
93 return EC;
Zachary Turner620961d2016-09-14 23:00:02 +000094 std::vector<ulittle32_t> HashValueList;
95 for (auto I : HashValues)
96 HashValueList.push_back(I);
Rui Ueyamafd97bf12016-06-03 20:48:51 +000097
Zachary Turnerc6d54da2016-09-09 17:46:17 +000098 HSR.setOffset(Header->IndexOffsetBuffer.Off);
99 uint32_t NumTypeIndexOffsets =
100 Header->IndexOffsetBuffer.Length / sizeof(TypeIndexOffset);
101 if (auto EC = HSR.readArray(TypeIndexOffsets, NumTypeIndexOffsets))
102 return EC;
Zachary Turnerf5c59652016-05-03 00:28:21 +0000103
Zachary Turner29da5db2017-01-25 21:17:40 +0000104 if (Header->HashAdjBuffer.Length > 0) {
105 HSR.setOffset(Header->HashAdjBuffer.Off);
106 if (auto EC = HashAdjusters.load(HSR))
107 return EC;
108 }
Rui Ueyamac41cd6d2016-06-09 00:10:19 +0000109
Zachary Turnerc6d54da2016-09-09 17:46:17 +0000110 HashStream = std::move(HS);
Zachary Turnerc6d54da2016-09-09 17:46:17 +0000111 }
Rui Ueyamac41cd6d2016-06-09 00:10:19 +0000112
Zachary Turner819e77d2016-05-06 20:51:57 +0000113 return Error::success();
Zachary Turnerf5c59652016-05-03 00:28:21 +0000114}
115
116PdbRaw_TpiVer TpiStream::getTpiVersion() const {
117 uint32_t Value = Header->Version;
118 return static_cast<PdbRaw_TpiVer>(Value);
119}
120
121uint32_t TpiStream::TypeIndexBegin() const { return Header->TypeIndexBegin; }
122
123uint32_t TpiStream::TypeIndexEnd() const { return Header->TypeIndexEnd; }
124
Zachary Turnerbedc85f2017-05-04 23:53:54 +0000125uint32_t TpiStream::getNumTypeRecords() const {
Zachary Turnerf5c59652016-05-03 00:28:21 +0000126 return TypeIndexEnd() - TypeIndexBegin();
127}
128
Zachary Turner85ed80b2016-05-25 03:43:17 +0000129uint16_t TpiStream::getTypeHashStreamIndex() const {
130 return Header->HashStreamIndex;
131}
132
133uint16_t TpiStream::getTypeHashStreamAuxIndex() const {
134 return Header->HashAuxStreamIndex;
135}
136
Zachary Turnerbedc85f2017-05-04 23:53:54 +0000137uint32_t TpiStream::getNumHashBuckets() const { return Header->NumHashBuckets; }
Rui Ueyamad8339172016-06-07 23:44:27 +0000138uint32_t TpiStream::getHashKeySize() const { return Header->HashKeySize; }
139
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +0000140FixedStreamArray<support::ulittle32_t> TpiStream::getHashValues() const {
Rui Ueyamafd97bf12016-06-03 20:48:51 +0000141 return HashValues;
142}
143
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +0000144FixedStreamArray<TypeIndexOffset> TpiStream::getTypeIndexOffsets() const {
Rui Ueyamafd97bf12016-06-03 20:48:51 +0000145 return TypeIndexOffsets;
146}
147
Zachary Turner29da5db2017-01-25 21:17:40 +0000148HashTable &TpiStream::getHashAdjusters() { return HashAdjusters; }
Rui Ueyamafd97bf12016-06-03 20:48:51 +0000149
Zachary Turner7b327d02017-02-16 23:35:45 +0000150CVTypeRange TpiStream::types(bool *HadError) const {
Eugene Zelenko570e39a2016-11-23 23:16:32 +0000151 return make_range(TypeRecords.begin(HadError), TypeRecords.end());
Zachary Turnerf5c59652016-05-03 00:28:21 +0000152}
Zachary Turner8848a7a2016-07-06 18:05:57 +0000153
154Error TpiStream::commit() { return Error::success(); }