]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/lib/DebugInfo/PDB/Native/TpiStream.cpp
Merge llvm, clang, lld, lldb, compiler-rt and libc++ r301441, and update
[FreeBSD/FreeBSD.git] / contrib / llvm / lib / DebugInfo / PDB / Native / TpiStream.cpp
1 //===- 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/Native/TpiStream.h"
11 #include "llvm/ADT/iterator_range.h"
12 #include "llvm/DebugInfo/CodeView/CVTypeVisitor.h"
13 #include "llvm/DebugInfo/CodeView/TypeDeserializer.h"
14 #include "llvm/DebugInfo/CodeView/TypeRecord.h"
15 #include "llvm/DebugInfo/CodeView/TypeVisitorCallbackPipeline.h"
16 #include "llvm/DebugInfo/MSF/MappedBlockStream.h"
17 #include "llvm/DebugInfo/PDB/Native/PDBFile.h"
18 #include "llvm/DebugInfo/PDB/Native/PDBTypeServerHandler.h"
19 #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"
23 #include "llvm/Support/BinaryStreamReader.h"
24 #include "llvm/Support/Endian.h"
25 #include "llvm/Support/Error.h"
26 #include <algorithm>
27 #include <cstdint>
28 #include <vector>
29
30 using namespace llvm;
31 using namespace llvm::codeview;
32 using namespace llvm::support;
33 using namespace llvm::msf;
34 using namespace llvm::pdb;
35
36 TpiStream::TpiStream(const PDBFile &File,
37                      std::unique_ptr<MappedBlockStream> Stream)
38     : Pdb(File), Stream(std::move(Stream)) {}
39
40 TpiStream::~TpiStream() = default;
41
42 // Verifies that a given type record matches with a given hash value.
43 // Currently we only verify SRC_LINE records.
44 Error TpiStream::verifyHashValues() {
45   TpiHashVerifier Verifier(HashValues, Header->NumHashBuckets);
46   TypeDeserializer Deserializer;
47
48   TypeVisitorCallbackPipeline Pipeline;
49   Pipeline.addCallbackToPipeline(Deserializer);
50   Pipeline.addCallbackToPipeline(Verifier);
51
52   CVTypeVisitor Visitor(Pipeline);
53   return Visitor.visitTypeStream(TypeRecords);
54 }
55
56 Error TpiStream::reload() {
57   BinaryStreamReader Reader(*Stream);
58
59   if (Reader.bytesRemaining() < sizeof(TpiStreamHeader))
60     return make_error<RawError>(raw_error_code::corrupt_file,
61                                 "TPI Stream does not contain a header.");
62
63   if (Reader.readObject(Header))
64     return make_error<RawError>(raw_error_code::corrupt_file,
65                                 "TPI Stream does not contain a header.");
66
67   if (Header->Version != PdbTpiV80)
68     return make_error<RawError>(raw_error_code::corrupt_file,
69                                 "Unsupported TPI Version.");
70
71   if (Header->HeaderSize != sizeof(TpiStreamHeader))
72     return make_error<RawError>(raw_error_code::corrupt_file,
73                                 "Corrupt TPI Header size.");
74
75   if (Header->HashKeySize != sizeof(ulittle32_t))
76     return make_error<RawError>(raw_error_code::corrupt_file,
77                                 "TPI Stream expected 4 byte hash key size.");
78
79   if (Header->NumHashBuckets < MinTpiHashBuckets ||
80       Header->NumHashBuckets > MaxTpiHashBuckets)
81     return make_error<RawError>(raw_error_code::corrupt_file,
82                                 "TPI Stream Invalid number of hash buckets.");
83
84   // The actual type records themselves come from this stream
85   if (auto EC = Reader.readArray(TypeRecords, Header->TypeRecordBytes))
86     return EC;
87
88   // Hash indices, hash values, etc come from the hash stream.
89   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.");
93
94     auto HS = MappedBlockStream::createIndexedStream(
95         Pdb.getMsfLayout(), Pdb.getMsfBuffer(), Header->HashStreamIndex);
96     BinaryStreamReader HSR(*HS);
97
98     // There should be a hash value for every type record, or no hashes at all.
99     uint32_t NumHashValues =
100         Header->HashValueBuffer.Length / sizeof(ulittle32_t);
101     if (NumHashValues != NumTypeRecords() && NumHashValues != 0)
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;
108     std::vector<ulittle32_t> HashValueList;
109     for (auto I : HashValues)
110       HashValueList.push_back(I);
111
112     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;
117
118     if (Header->HashAdjBuffer.Length > 0) {
119       HSR.setOffset(Header->HashAdjBuffer.Off);
120       if (auto EC = HashAdjusters.load(HSR))
121         return EC;
122     }
123
124     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 (NumHashValues > 0)
129       if (auto EC = verifyHashValues())
130         return EC;
131   }
132
133   return Error::success();
134 }
135
136 PdbRaw_TpiVer TpiStream::getTpiVersion() const {
137   uint32_t Value = Header->Version;
138   return static_cast<PdbRaw_TpiVer>(Value);
139 }
140
141 uint32_t TpiStream::TypeIndexBegin() const { return Header->TypeIndexBegin; }
142
143 uint32_t TpiStream::TypeIndexEnd() const { return Header->TypeIndexEnd; }
144
145 uint32_t TpiStream::NumTypeRecords() const {
146   return TypeIndexEnd() - TypeIndexBegin();
147 }
148
149 uint16_t TpiStream::getTypeHashStreamIndex() const {
150   return Header->HashStreamIndex;
151 }
152
153 uint16_t TpiStream::getTypeHashStreamAuxIndex() const {
154   return Header->HashAuxStreamIndex;
155 }
156
157 uint32_t TpiStream::NumHashBuckets() const { return Header->NumHashBuckets; }
158 uint32_t TpiStream::getHashKeySize() const { return Header->HashKeySize; }
159
160 FixedStreamArray<support::ulittle32_t> TpiStream::getHashValues() const {
161   return HashValues;
162 }
163
164 FixedStreamArray<TypeIndexOffset> TpiStream::getTypeIndexOffsets() const {
165   return TypeIndexOffsets;
166 }
167
168 HashTable &TpiStream::getHashAdjusters() { return HashAdjusters; }
169
170 CVTypeRange TpiStream::types(bool *HadError) const {
171   return make_range(TypeRecords.begin(HadError), TypeRecords.end());
172 }
173
174 Error TpiStream::commit() { return Error::success(); }