LLVM  8.0.1
DbiStream.cpp
Go to the documentation of this file.
1 //===- DbiStream.cpp - PDB Dbi Stream (Stream 3) 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 #include "llvm/ADT/StringRef.h"
20 #include "llvm/Object/COFF.h"
23 #include "llvm/Support/Error.h"
24 #include <algorithm>
25 #include <cstddef>
26 #include <cstdint>
27 
28 using namespace llvm;
29 using namespace llvm::codeview;
30 using namespace llvm::msf;
31 using namespace llvm::pdb;
32 using namespace llvm::support;
33 
34 template <typename ContribType>
36  BinaryStreamReader &Reader) {
37  if (Reader.bytesRemaining() % sizeof(ContribType) != 0)
38  return make_error<RawError>(
39  raw_error_code::corrupt_file,
40  "Invalid number of bytes of section contributions");
41 
42  uint32_t Count = Reader.bytesRemaining() / sizeof(ContribType);
43  if (auto EC = Reader.readArray(Output, Count))
44  return EC;
45  return Error::success();
46 }
47 
48 DbiStream::DbiStream(std::unique_ptr<BinaryStream> Stream)
49  : Stream(std::move(Stream)), Header(nullptr) {}
50 
51 DbiStream::~DbiStream() = default;
52 
54  BinaryStreamReader Reader(*Stream);
55 
56  if (Stream->getLength() < sizeof(DbiStreamHeader))
57  return make_error<RawError>(raw_error_code::corrupt_file,
58  "DBI Stream does not contain a header.");
59  if (auto EC = Reader.readObject(Header))
60  return make_error<RawError>(raw_error_code::corrupt_file,
61  "DBI Stream does not contain a header.");
62 
63  if (Header->VersionSignature != -1)
64  return make_error<RawError>(raw_error_code::corrupt_file,
65  "Invalid DBI version signature.");
66 
67  // Require at least version 7, which should be present in all PDBs
68  // produced in the last decade and allows us to avoid having to
69  // special case all kinds of complicated arcane formats.
70  if (Header->VersionHeader < PdbDbiV70)
71  return make_error<RawError>(raw_error_code::feature_unsupported,
72  "Unsupported DBI version.");
73 
74  if (Stream->getLength() !=
75  sizeof(DbiStreamHeader) + Header->ModiSubstreamSize +
76  Header->SecContrSubstreamSize + Header->SectionMapSize +
77  Header->FileInfoSize + Header->TypeServerSize +
78  Header->OptionalDbgHdrSize + Header->ECSubstreamSize)
79  return make_error<RawError>(raw_error_code::corrupt_file,
80  "DBI Length does not equal sum of substreams.");
81 
82  // Only certain substreams are guaranteed to be aligned. Validate
83  // them here.
84  if (Header->ModiSubstreamSize % sizeof(uint32_t) != 0)
85  return make_error<RawError>(raw_error_code::corrupt_file,
86  "DBI MODI substream not aligned.");
87  if (Header->SecContrSubstreamSize % sizeof(uint32_t) != 0)
88  return make_error<RawError>(
89  raw_error_code::corrupt_file,
90  "DBI section contribution substream not aligned.");
91  if (Header->SectionMapSize % sizeof(uint32_t) != 0)
92  return make_error<RawError>(raw_error_code::corrupt_file,
93  "DBI section map substream not aligned.");
94  if (Header->FileInfoSize % sizeof(uint32_t) != 0)
95  return make_error<RawError>(raw_error_code::corrupt_file,
96  "DBI file info substream not aligned.");
97  if (Header->TypeServerSize % sizeof(uint32_t) != 0)
98  return make_error<RawError>(raw_error_code::corrupt_file,
99  "DBI type server substream not aligned.");
100 
101  if (auto EC = Reader.readSubstream(ModiSubstream, Header->ModiSubstreamSize))
102  return EC;
103 
104  if (auto EC = Reader.readSubstream(SecContrSubstream,
105  Header->SecContrSubstreamSize))
106  return EC;
107  if (auto EC = Reader.readSubstream(SecMapSubstream, Header->SectionMapSize))
108  return EC;
109  if (auto EC = Reader.readSubstream(FileInfoSubstream, Header->FileInfoSize))
110  return EC;
111  if (auto EC =
112  Reader.readSubstream(TypeServerMapSubstream, Header->TypeServerSize))
113  return EC;
114  if (auto EC = Reader.readSubstream(ECSubstream, Header->ECSubstreamSize))
115  return EC;
116  if (auto EC = Reader.readArray(
117  DbgStreams, Header->OptionalDbgHdrSize / sizeof(ulittle16_t)))
118  return EC;
119 
120  if (auto EC = Modules.initialize(ModiSubstream.StreamData,
121  FileInfoSubstream.StreamData))
122  return EC;
123 
124  if (auto EC = initializeSectionContributionData())
125  return EC;
126  if (auto EC = initializeSectionHeadersData(Pdb))
127  return EC;
128  if (auto EC = initializeSectionMapData())
129  return EC;
130  if (auto EC = initializeFpoRecords(Pdb))
131  return EC;
132 
133  if (Reader.bytesRemaining() > 0)
134  return make_error<RawError>(raw_error_code::corrupt_file,
135  "Found unexpected bytes in DBI Stream.");
136 
137  if (!ECSubstream.empty()) {
138  BinaryStreamReader ECReader(ECSubstream.StreamData);
139  if (auto EC = ECNames.reload(ECReader))
140  return EC;
141  }
142 
143  return Error::success();
144 }
145 
147  uint32_t Value = Header->VersionHeader;
148  return static_cast<PdbRaw_DbiVer>(Value);
149 }
150 
151 uint32_t DbiStream::getAge() const { return Header->Age; }
152 
154  return Header->PublicSymbolStreamIndex;
155 }
156 
158  return Header->GlobalSymbolStreamIndex;
159 }
160 
161 uint16_t DbiStream::getFlags() const { return Header->Flags; }
162 
164  return (Header->Flags & DbiFlags::FlagIncrementalMask) != 0;
165 }
166 
167 bool DbiStream::hasCTypes() const {
168  return (Header->Flags & DbiFlags::FlagHasCTypesMask) != 0;
169 }
170 
171 bool DbiStream::isStripped() const {
172  return (Header->Flags & DbiFlags::FlagStrippedMask) != 0;
173 }
174 
175 uint16_t DbiStream::getBuildNumber() const { return Header->BuildNumber; }
176 
178  return (Header->BuildNumber & DbiBuildNo::BuildMajorMask) >>
180 }
181 
183  return (Header->BuildNumber & DbiBuildNo::BuildMinorMask) >>
185 }
186 
187 uint16_t DbiStream::getPdbDllRbld() const { return Header->PdbDllRbld; }
188 
190 
192  return Header->SymRecordStreamIndex;
193 }
194 
196  uint16_t Machine = Header->MachineType;
197  return static_cast<PDB_Machine>(Machine);
198 }
199 
201  return SectionHeaders;
202 }
203 
205  return FpoRecords;
206 }
207 
208 const DbiModuleList &DbiStream::modules() const { return Modules; }
209 
211  return SectionMap;
212 }
213 
215  ISectionContribVisitor &Visitor) const {
216  if (!SectionContribs.empty()) {
217  assert(SectionContribVersion == DbiSecContribVer60);
218  for (auto &SC : SectionContribs)
219  Visitor.visit(SC);
220  } else if (!SectionContribs2.empty()) {
221  assert(SectionContribVersion == DbiSecContribV2);
222  for (auto &SC : SectionContribs2)
223  Visitor.visit(SC);
224  }
225 }
226 
228  return ECNames.getStringForID(NI);
229 }
230 
231 Error DbiStream::initializeSectionContributionData() {
232  if (SecContrSubstream.empty())
233  return Error::success();
234 
235  BinaryStreamReader SCReader(SecContrSubstream.StreamData);
236  if (auto EC = SCReader.readEnum(SectionContribVersion))
237  return EC;
238 
239  if (SectionContribVersion == DbiSecContribVer60)
240  return loadSectionContribs<SectionContrib>(SectionContribs, SCReader);
241  if (SectionContribVersion == DbiSecContribV2)
242  return loadSectionContribs<SectionContrib2>(SectionContribs2, SCReader);
243 
244  return make_error<RawError>(raw_error_code::feature_unsupported,
245  "Unsupported DBI Section Contribution version");
246 }
247 
248 // Initializes this->SectionHeaders.
249 Error DbiStream::initializeSectionHeadersData(PDBFile *Pdb) {
250  if (!Pdb)
251  return Error::success();
252 
253  if (DbgStreams.size() == 0)
254  return Error::success();
255 
257  if (StreamNum == kInvalidStreamIndex)
258  return Error::success();
259 
260  if (StreamNum >= Pdb->getNumStreams())
261  return make_error<RawError>(raw_error_code::no_stream);
262 
263  auto SHS = MappedBlockStream::createIndexedStream(
264  Pdb->getMsfLayout(), Pdb->getMsfBuffer(), StreamNum, Pdb->getAllocator());
265 
266  size_t StreamLen = SHS->getLength();
267  if (StreamLen % sizeof(object::coff_section))
268  return make_error<RawError>(raw_error_code::corrupt_file,
269  "Corrupted section header stream.");
270 
271  size_t NumSections = StreamLen / sizeof(object::coff_section);
272  BinaryStreamReader Reader(*SHS);
273  if (auto EC = Reader.readArray(SectionHeaders, NumSections))
274  return make_error<RawError>(raw_error_code::corrupt_file,
275  "Could not read a bitmap.");
276 
277  SectionHeaderStream = std::move(SHS);
278  return Error::success();
279 }
280 
281 // Initializes this->Fpos.
282 Error DbiStream::initializeFpoRecords(PDBFile *Pdb) {
283  if (!Pdb)
284  return Error::success();
285 
286  if (DbgStreams.size() == 0)
287  return Error::success();
288 
290 
291  // This means there is no FPO data.
292  if (StreamNum == kInvalidStreamIndex)
293  return Error::success();
294 
295  if (StreamNum >= Pdb->getNumStreams())
296  return make_error<RawError>(raw_error_code::no_stream);
297 
298  auto FS = MappedBlockStream::createIndexedStream(
299  Pdb->getMsfLayout(), Pdb->getMsfBuffer(), StreamNum, Pdb->getAllocator());
300 
301  size_t StreamLen = FS->getLength();
302  if (StreamLen % sizeof(object::FpoData))
303  return make_error<RawError>(raw_error_code::corrupt_file,
304  "Corrupted New FPO stream.");
305 
306  size_t NumRecords = StreamLen / sizeof(object::FpoData);
307  BinaryStreamReader Reader(*FS);
308  if (auto EC = Reader.readArray(FpoRecords, NumRecords))
309  return make_error<RawError>(raw_error_code::corrupt_file,
310  "Corrupted New FPO stream.");
311  FpoStream = std::move(FS);
312  return Error::success();
313 }
314 
316  return SecContrSubstream;
317 }
318 
320  return SecMapSubstream;
321 }
322 
324  return ModiSubstream;
325 }
326 
328  return FileInfoSubstream;
329 }
330 
332  return TypeServerMapSubstream;
333 }
334 
335 BinarySubstreamRef DbiStream::getECSubstreamData() const { return ECSubstream; }
336 
337 Error DbiStream::initializeSectionMapData() {
338  if (SecMapSubstream.empty())
339  return Error::success();
340 
341  BinaryStreamReader SMReader(SecMapSubstream.StreamData);
342  const SecMapHeader *Header;
343  if (auto EC = SMReader.readObject(Header))
344  return EC;
345  if (auto EC = SMReader.readArray(SectionMap, Header->SecCount))
346  return EC;
347  return Error::success();
348 }
349 
351  uint16_t T = static_cast<uint16_t>(Type);
352  if (T >= DbgStreams.size())
353  return kInvalidStreamIndex;
354  return DbgStreams[T];
355 }
support::little32_t FileInfoSize
Size of file info substream.
Definition: RawTypes.h:155
virtual void visit(const SectionContrib &C)=0
Error readSubstream(BinarySubstreamRef &Stream, uint32_t Size)
Read Length bytes from the underlying stream into Stream.
static const uint16_t FlagHasCTypesMask
Definition: RawTypes.h:103
static Error loadSectionContribs(FixedStreamArray< ContribType > &Output, BinaryStreamReader &Reader)
Definition: DbiStream.cpp:35
This class represents lattice values for constants.
Definition: AllocatorList.h:24
Expected< StringRef > getECName(uint32_t NI) const
Definition: DbiStream.cpp:227
BinarySubstreamRef getECSubstreamData() const
Definition: DbiStream.cpp:335
support::little32_t VersionSignature
Definition: RawTypes.h:121
static const uint16_t BuildMinorMask
uint16_t MinorVersion : 8; uint16_t MajorVersion : 7; uint16_t NewVersionFormat : 1; ...
Definition: RawTypes.h:110
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...
support::ulittle16_t GlobalSymbolStreamIndex
Global symbol stream #.
Definition: RawTypes.h:128
Error reload(BinaryStreamReader &Reader)
FixedStreamArray is similar to VarStreamArray, except with each record having a fixed-length.
uint16_t getGlobalSymbolStreamIndex() const
Definition: DbiStream.cpp:157
Error reload(PDBFile *Pdb)
Definition: DbiStream.cpp:53
support::ulittle16_t BuildNumber
See DbiBuildNo structure.
Definition: RawTypes.h:131
FixedStreamArray< object::coff_section > getSectionHeaders() const
Definition: DbiStream.cpp:200
uint32_t getNumStreams() const override
Definition: PDBFile.cpp:84
BinarySubstreamRef getSectionContributionData() const
Definition: DbiStream.cpp:315
Definition: BitVector.h:938
bool isStripped() const
Definition: DbiStream.cpp:171
support::little32_t TypeServerSize
Size of type server map.
Definition: RawTypes.h:158
support::little32_t SectionMapSize
Size of sec. map substream.
Definition: RawTypes.h:152
FixedStreamArray< object::FpoData > getFpoRecords()
Definition: DbiStream.cpp:204
PDB_Machine getMachineType() const
Definition: DbiStream.cpp:195
Tagged union holding either a T or a Error.
Definition: CachePruning.h:23
The fixed size header that appears at the beginning of the DBI Stream.
Definition: RawTypes.h:120
BinarySubstreamRef getSecMapSubstreamData() const
Definition: DbiStream.cpp:319
uint32_t getSymRecordStreamIndex() const
Definition: DbiStream.cpp:191
support::little32_t ECSubstreamSize
Size of EC stream (what is EC?)
Definition: RawTypes.h:167
Expected< StringRef > getStringForID(uint32_t ID) const
uint16_t getBuildNumber() const
Definition: DbiStream.cpp:175
#define T
BinaryStreamRef getMsfBuffer() const
Definition: PDBFile.h:83
COFF::MachineTypes Machine
Definition: COFFYAML.cpp:363
BinarySubstreamRef getTypeServerMapSubstreamData() const
Definition: DbiStream.cpp:331
const uint16_t kInvalidStreamIndex
Definition: RawConstants.h:20
BumpPtrAllocator & getAllocator()
Definition: PDBFile.h:104
support::ulittle16_t PdbDllRbld
rbld number of mspdbNNN.dll
Definition: RawTypes.h:143
static const uint16_t BuildMajorShift
Definition: RawTypes.h:114
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:46
uint32_t getAge() const
Definition: DbiStream.cpp:151
uint16_t getBuildMajorVersion() const
Definition: DbiStream.cpp:177
Error initialize(BinaryStreamRef ModInfo, BinaryStreamRef FileInfo)
uint32_t getDebugStreamIndex(DbgHeaderType Type) const
If the given stream type is present, returns its stream index.
Definition: DbiStream.cpp:350
uint16_t getBuildMinorVersion() const
Definition: DbiStream.cpp:182
const DbiModuleList & modules() const
Definition: DbiStream.cpp:208
uint32_t getPdbDllVersion() const
Definition: DbiStream.cpp:189
support::ulittle16_t PdbDllVersion
version of mspdbNNN.dll
Definition: RawTypes.h:137
BinaryStreamRef StreamData
static const uint16_t FlagIncrementalMask
uint16_t IncrementalLinking : 1; // True if linked incrementally uint16_t IsStripped : 1; // True if ...
Definition: RawTypes.h:101
support::little32_t OptionalDbgHdrSize
Size of DbgHeader info.
Definition: RawTypes.h:164
support::ulittle16_t Flags
See DbiFlags enum.
Definition: RawTypes.h:170
static ErrorSuccess success()
Create a success value.
Definition: Error.h:327
CHAIN = SC CHAIN, Imm128 - System call.
support::ulittle16_t SecCount
Definition: RawTypes.h:68
BinarySubstreamRef getFileInfoSubstreamData() const
Definition: DbiStream.cpp:327
static const uint16_t FlagStrippedMask
Definition: RawTypes.h:102
support::little32_t ModiSubstreamSize
Size of module info stream.
Definition: RawTypes.h:146
PdbRaw_DbiVer getDbiVersion() const
Definition: DbiStream.cpp:146
support::little32_t SecContrSubstreamSize
Size of sec. contrib stream.
Definition: RawTypes.h:149
static const uint16_t BuildMajorMask
Definition: RawTypes.h:113
support::ulittle32_t Age
How "old" is this DBI Stream. Should match the age of the PDB InfoStream.
Definition: RawTypes.h:125
support::ulittle16_t SymRecordStreamIndex
Symbol records stream #.
Definition: RawTypes.h:140
support::ulittle32_t VersionHeader
Definition: RawTypes.h:122
static const uint16_t BuildMinorShift
Definition: RawTypes.h:111
uint32_t bytesRemaining() const
uint16_t getPdbDllRbld() const
Definition: DbiStream.cpp:187
uint16_t getPublicSymbolStreamIndex() const
Definition: DbiStream.cpp:153
uint16_t getFlags() const
Definition: DbiStream.cpp:161
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
const msf::MSFLayout & getMsfLayout() const
Definition: PDBFile.h:82
LLVM Value Representation.
Definition: Value.h:73
Lightweight error class with error context and mandatory checking.
Definition: Error.h:158
support::ulittle16_t PublicSymbolStreamIndex
Public symbols stream #.
Definition: RawTypes.h:134
Provides read only access to a subclass of BinaryStream.
BinarySubstreamRef getModiSubstreamData() const
Definition: DbiStream.cpp:323
Error readEnum(T &Dest)
Similar to readInteger.
void visitSectionContributions(ISectionContribVisitor &Visitor) const
Definition: DbiStream.cpp:214
support::ulittle16_t MachineType
See PDB_MachineType enum.
Definition: RawTypes.h:173
FixedStreamArray< SecMapEntry > getSectionMap() const
Definition: DbiStream.cpp:210
bool hasCTypes() const
Definition: DbiStream.cpp:167
bool isIncrementallyLinked() const
Definition: DbiStream.cpp:163
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 ...