]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/lib/DebugInfo/PDB/Native/TpiStream.cpp
Merge llvm, clang, lld, lldb, compiler-rt and libc++ r304659, 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
12 #include "llvm/ADT/iterator_range.h"
13 #include "llvm/DebugInfo/CodeView/LazyRandomTypeCollection.h"
14 #include "llvm/DebugInfo/CodeView/TypeRecord.h"
15 #include "llvm/DebugInfo/MSF/MappedBlockStream.h"
16 #include "llvm/DebugInfo/PDB/Native/PDBFile.h"
17 #include "llvm/DebugInfo/PDB/Native/PDBTypeServerHandler.h"
18 #include "llvm/DebugInfo/PDB/Native/RawConstants.h"
19 #include "llvm/DebugInfo/PDB/Native/RawError.h"
20 #include "llvm/DebugInfo/PDB/Native/RawTypes.h"
21 #include "llvm/DebugInfo/PDB/Native/TpiHashing.h"
22 #include "llvm/Support/BinaryStreamReader.h"
23 #include "llvm/Support/Endian.h"
24 #include "llvm/Support/Error.h"
25 #include <algorithm>
26 #include <cstdint>
27 #include <vector>
28
29 using namespace llvm;
30 using namespace llvm::codeview;
31 using namespace llvm::support;
32 using namespace llvm::msf;
33 using namespace llvm::pdb;
34
35 TpiStream::TpiStream(PDBFile &File, std::unique_ptr<MappedBlockStream> Stream)
36     : Pdb(File), Stream(std::move(Stream)) {}
37
38 TpiStream::~TpiStream() = default;
39
40 Error TpiStream::reload() {
41   BinaryStreamReader Reader(*Stream);
42
43   if (Reader.bytesRemaining() < sizeof(TpiStreamHeader))
44     return make_error<RawError>(raw_error_code::corrupt_file,
45                                 "TPI Stream does not contain a header.");
46
47   if (Reader.readObject(Header))
48     return make_error<RawError>(raw_error_code::corrupt_file,
49                                 "TPI Stream does not contain a header.");
50
51   if (Header->Version != PdbTpiV80)
52     return make_error<RawError>(raw_error_code::corrupt_file,
53                                 "Unsupported TPI Version.");
54
55   if (Header->HeaderSize != sizeof(TpiStreamHeader))
56     return make_error<RawError>(raw_error_code::corrupt_file,
57                                 "Corrupt TPI Header size.");
58
59   if (Header->HashKeySize != sizeof(ulittle32_t))
60     return make_error<RawError>(raw_error_code::corrupt_file,
61                                 "TPI Stream expected 4 byte hash key size.");
62
63   if (Header->NumHashBuckets < MinTpiHashBuckets ||
64       Header->NumHashBuckets > MaxTpiHashBuckets)
65     return make_error<RawError>(raw_error_code::corrupt_file,
66                                 "TPI Stream Invalid number of hash buckets.");
67
68   // The actual type records themselves come from this stream
69   if (auto EC = Reader.readArray(TypeRecords, Header->TypeRecordBytes))
70     return EC;
71
72   // Hash indices, hash values, etc come from the hash stream.
73   if (Header->HashStreamIndex != kInvalidStreamIndex) {
74     if (Header->HashStreamIndex >= Pdb.getNumStreams())
75       return make_error<RawError>(raw_error_code::corrupt_file,
76                                   "Invalid TPI hash stream index.");
77
78     auto HS = MappedBlockStream::createIndexedStream(
79         Pdb.getMsfLayout(), Pdb.getMsfBuffer(), Header->HashStreamIndex,
80         Pdb.getAllocator());
81     BinaryStreamReader HSR(*HS);
82
83     // There should be a hash value for every type record, or no hashes at all.
84     uint32_t NumHashValues =
85         Header->HashValueBuffer.Length / sizeof(ulittle32_t);
86     if (NumHashValues != getNumTypeRecords() && NumHashValues != 0)
87       return make_error<RawError>(
88           raw_error_code::corrupt_file,
89           "TPI hash count does not match with the number of type records.");
90     HSR.setOffset(Header->HashValueBuffer.Off);
91     if (auto EC = HSR.readArray(HashValues, NumHashValues))
92       return EC;
93
94     HSR.setOffset(Header->IndexOffsetBuffer.Off);
95     uint32_t NumTypeIndexOffsets =
96         Header->IndexOffsetBuffer.Length / sizeof(TypeIndexOffset);
97     if (auto EC = HSR.readArray(TypeIndexOffsets, NumTypeIndexOffsets))
98       return EC;
99
100     if (Header->HashAdjBuffer.Length > 0) {
101       HSR.setOffset(Header->HashAdjBuffer.Off);
102       if (auto EC = HashAdjusters.load(HSR))
103         return EC;
104     }
105
106     HashStream = std::move(HS);
107   }
108
109   Types = llvm::make_unique<LazyRandomTypeCollection>(
110       TypeRecords, getNumTypeRecords(), getTypeIndexOffsets());
111   return Error::success();
112 }
113
114 PdbRaw_TpiVer TpiStream::getTpiVersion() const {
115   uint32_t Value = Header->Version;
116   return static_cast<PdbRaw_TpiVer>(Value);
117 }
118
119 uint32_t TpiStream::TypeIndexBegin() const { return Header->TypeIndexBegin; }
120
121 uint32_t TpiStream::TypeIndexEnd() const { return Header->TypeIndexEnd; }
122
123 uint32_t TpiStream::getNumTypeRecords() const {
124   return TypeIndexEnd() - TypeIndexBegin();
125 }
126
127 uint16_t TpiStream::getTypeHashStreamIndex() const {
128   return Header->HashStreamIndex;
129 }
130
131 uint16_t TpiStream::getTypeHashStreamAuxIndex() const {
132   return Header->HashAuxStreamIndex;
133 }
134
135 uint32_t TpiStream::getNumHashBuckets() const { return Header->NumHashBuckets; }
136 uint32_t TpiStream::getHashKeySize() const { return Header->HashKeySize; }
137
138 FixedStreamArray<support::ulittle32_t> TpiStream::getHashValues() const {
139   return HashValues;
140 }
141
142 FixedStreamArray<TypeIndexOffset> TpiStream::getTypeIndexOffsets() const {
143   return TypeIndexOffsets;
144 }
145
146 HashTable &TpiStream::getHashAdjusters() { return HashAdjusters; }
147
148 CVTypeRange TpiStream::types(bool *HadError) const {
149   return make_range(TypeRecords.begin(HadError), TypeRecords.end());
150 }
151
152 Error TpiStream::commit() { return Error::success(); }