LLVM  6.0.0svn
TpiStream.cpp
Go to the documentation of this file.
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 
11 
22 #include "llvm/Support/Endian.h"
23 #include "llvm/Support/Error.h"
24 #include <algorithm>
25 #include <cstdint>
26 #include <vector>
27 
28 using namespace llvm;
29 using namespace llvm::codeview;
30 using namespace llvm::support;
31 using namespace llvm::msf;
32 using namespace llvm::pdb;
33 
34 TpiStream::TpiStream(PDBFile &File, std::unique_ptr<MappedBlockStream> Stream)
35  : Pdb(File), Stream(std::move(Stream)) {}
36 
37 TpiStream::~TpiStream() = default;
38 
40  BinaryStreamReader Reader(*Stream);
41 
42  if (Reader.bytesRemaining() < sizeof(TpiStreamHeader))
43  return make_error<RawError>(raw_error_code::corrupt_file,
44  "TPI Stream does not contain a header.");
45 
46  if (Reader.readObject(Header))
47  return make_error<RawError>(raw_error_code::corrupt_file,
48  "TPI Stream does not contain a header.");
49 
50  if (Header->Version != PdbTpiV80)
51  return make_error<RawError>(raw_error_code::corrupt_file,
52  "Unsupported TPI Version.");
53 
54  if (Header->HeaderSize != sizeof(TpiStreamHeader))
55  return make_error<RawError>(raw_error_code::corrupt_file,
56  "Corrupt TPI Header size.");
57 
58  if (Header->HashKeySize != sizeof(ulittle32_t))
59  return make_error<RawError>(raw_error_code::corrupt_file,
60  "TPI Stream expected 4 byte hash key size.");
61 
62  if (Header->NumHashBuckets < MinTpiHashBuckets ||
64  return make_error<RawError>(raw_error_code::corrupt_file,
65  "TPI Stream Invalid number of hash buckets.");
66 
67  // The actual type records themselves come from this stream
68  if (auto EC =
69  Reader.readSubstream(TypeRecordsSubstream, Header->TypeRecordBytes))
70  return EC;
71 
72  BinaryStreamReader RecordReader(TypeRecordsSubstream.StreamData);
73  if (auto EC =
74  RecordReader.readArray(TypeRecords, TypeRecordsSubstream.size()))
75  return EC;
76 
77  // Hash indices, hash values, etc come from the hash stream.
78  if (Header->HashStreamIndex != kInvalidStreamIndex) {
79  if (Header->HashStreamIndex >= Pdb.getNumStreams())
80  return make_error<RawError>(raw_error_code::corrupt_file,
81  "Invalid TPI hash stream index.");
82 
83  auto HS = MappedBlockStream::createIndexedStream(
84  Pdb.getMsfLayout(), Pdb.getMsfBuffer(), Header->HashStreamIndex,
85  Pdb.getAllocator());
86  BinaryStreamReader HSR(*HS);
87 
88  // There should be a hash value for every type record, or no hashes at all.
89  uint32_t NumHashValues =
90  Header->HashValueBuffer.Length / sizeof(ulittle32_t);
91  if (NumHashValues != getNumTypeRecords() && NumHashValues != 0)
92  return make_error<RawError>(
93  raw_error_code::corrupt_file,
94  "TPI hash count does not match with the number of type records.");
95  HSR.setOffset(Header->HashValueBuffer.Off);
96  if (auto EC = HSR.readArray(HashValues, NumHashValues))
97  return EC;
98 
99  HSR.setOffset(Header->IndexOffsetBuffer.Off);
100  uint32_t NumTypeIndexOffsets =
101  Header->IndexOffsetBuffer.Length / sizeof(TypeIndexOffset);
102  if (auto EC = HSR.readArray(TypeIndexOffsets, NumTypeIndexOffsets))
103  return EC;
104 
105  if (Header->HashAdjBuffer.Length > 0) {
106  HSR.setOffset(Header->HashAdjBuffer.Off);
107  if (auto EC = HashAdjusters.load(HSR))
108  return EC;
109  }
110 
111  HashStream = std::move(HS);
112  }
113 
114  Types = llvm::make_unique<LazyRandomTypeCollection>(
115  TypeRecords, getNumTypeRecords(), getTypeIndexOffsets());
116  return Error::success();
117 }
118 
120  uint32_t Value = Header->Version;
121  return static_cast<PdbRaw_TpiVer>(Value);
122 }
123 
125 
126 uint32_t TpiStream::TypeIndexEnd() const { return Header->TypeIndexEnd; }
127 
129  return TypeIndexEnd() - TypeIndexBegin();
130 }
131 
133  return Header->HashStreamIndex;
134 }
135 
137  return Header->HashAuxStreamIndex;
138 }
139 
141 uint32_t TpiStream::getHashKeySize() const { return Header->HashKeySize; }
142 
144  return TypeRecordsSubstream;
145 }
146 
148  return HashValues;
149 }
150 
152  return TypeIndexOffsets;
153 }
154 
155 HashTable &TpiStream::getHashAdjusters() { return HashAdjusters; }
156 
157 CVTypeRange TpiStream::types(bool *HadError) const {
158  return make_range(TypeRecords.begin(HadError), TypeRecords.end());
159 }
160 
Error readSubstream(BinarySubstreamRef &Stream, uint32_t Size)
Read Length bytes from the underlying stream into Stream.
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
support::ulittle16_t HashStreamIndex
Definition: RawTypes.h:301
EmbeddedBuf IndexOffsetBuffer
Definition: RawTypes.h:307
support::ulittle32_t NumHashBuckets
Definition: RawTypes.h:304
Iterator end() const
This provides a very simple, boring adaptor for a begin and end iterator into a range type...
Error readObject(const T *&Dest)
Get a pointer to an object of type T from the underlying stream, as if by memcpy, and store the resul...
FixedStreamArray< support::ulittle32_t > getHashValues() const
Definition: TpiStream.cpp:147
support::ulittle32_t TypeRecordBytes
Definition: RawTypes.h:298
uint32_t TypeIndexBegin() const
Definition: TpiStream.cpp:124
uint32_t getNumStreams() const override
Definition: PDBFile.cpp:84
HashTable & getHashAdjusters()
Definition: TpiStream.cpp:155
Definition: BitVector.h:920
PdbRaw_TpiVer getTpiVersion() const
Definition: TpiStream.cpp:119
BinaryStreamRef getMsfBuffer() const
Definition: PDBFile.h:83
codeview::CVTypeRange types(bool *HadError) const
Definition: TpiStream.cpp:157
const uint16_t kInvalidStreamIndex
Definition: RawConstants.h:20
BumpPtrAllocator & getAllocator()
Definition: PDBFile.h:104
support::ulittle32_t TypeIndexEnd
Definition: RawTypes.h:297
detail::packed_endian_specific_integral< uint32_t, little, unaligned > ulittle32_t
Definition: Endian.h:271
BinarySubstreamRef getTypeRecordsSubstream() const
Definition: TpiStream.cpp:143
uint32_t getHashKeySize() const
Definition: TpiStream.cpp:141
support::ulittle32_t TypeIndexBegin
Definition: RawTypes.h:296
uint16_t getTypeHashStreamAuxIndex() const
Definition: TpiStream.cpp:136
Error load(BinaryStreamReader &Stream)
Definition: HashTable.cpp:29
BinaryStreamRef StreamData
const uint32_t MinTpiHashBuckets
Definition: RawTypes.h:311
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
static ErrorSuccess success()
Create a success value.
Definition: Error.h:313
EmbeddedBuf HashValueBuffer
Definition: RawTypes.h:306
A range adaptor for a pair of iterators.
support::ulittle16_t HashAuxStreamIndex
Definition: RawTypes.h:302
uint32_t TypeIndexEnd() const
Definition: TpiStream.cpp:126
support::ulittle32_t HashKeySize
Definition: RawTypes.h:303
uint16_t getTypeHashStreamIndex() const
Definition: TpiStream.cpp:132
uint32_t bytesRemaining() const
uint32_t getNumTypeRecords() const
Definition: TpiStream.cpp:128
support::ulittle32_t HeaderSize
Definition: RawTypes.h:295
const msf::MSFLayout & getMsfLayout() const
Definition: PDBFile.h:82
Iterator begin(bool *HadError=nullptr) const
LLVM Value Representation.
Definition: Value.h:73
support::ulittle32_t Version
Definition: RawTypes.h:294
Lightweight error class with error context and mandatory checking.
Definition: Error.h:156
Provides read only access to a subclass of BinaryStream.
uint32_t getNumHashBuckets() const
Definition: TpiStream.cpp:140
const uint32_t MaxTpiHashBuckets
Definition: RawTypes.h:312
EmbeddedBuf HashAdjBuffer
Definition: RawTypes.h:308
Error readArray(ArrayRef< T > &Array, uint32_t NumElements)
Get a reference to a NumElements element array of objects of type T from the underlying stream as if ...
FixedStreamArray< codeview::TypeIndexOffset > getTypeIndexOffsets() const
Definition: TpiStream.cpp:151