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