1 //===- DbiStream.cpp - PDB Dbi Stream (Stream 3) Access -------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/DebugInfo/PDB/Native/DbiStream.h"
11 #include "llvm/ADT/StringRef.h"
12 #include "llvm/DebugInfo/MSF/MappedBlockStream.h"
13 #include "llvm/DebugInfo/PDB/Native/DbiModuleDescriptor.h"
14 #include "llvm/DebugInfo/PDB/Native/ISectionContribVisitor.h"
15 #include "llvm/DebugInfo/PDB/Native/PDBFile.h"
16 #include "llvm/DebugInfo/PDB/Native/RawConstants.h"
17 #include "llvm/DebugInfo/PDB/Native/RawError.h"
18 #include "llvm/DebugInfo/PDB/Native/RawTypes.h"
19 #include "llvm/DebugInfo/PDB/PDBTypes.h"
20 #include "llvm/Object/COFF.h"
21 #include "llvm/Support/BinaryStreamArray.h"
22 #include "llvm/Support/BinaryStreamReader.h"
23 #include "llvm/Support/Error.h"
29 using namespace llvm::codeview;
30 using namespace llvm::msf;
31 using namespace llvm::pdb;
32 using namespace llvm::support;
34 template <typename ContribType>
35 static Error loadSectionContribs(FixedStreamArray<ContribType> &Output,
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");
42 uint32_t Count = Reader.bytesRemaining() / sizeof(ContribType);
43 if (auto EC = Reader.readArray(Output, Count))
45 return Error::success();
48 DbiStream::DbiStream(PDBFile &File, std::unique_ptr<MappedBlockStream> Stream)
49 : Pdb(File), Stream(std::move(Stream)), Header(nullptr) {}
51 DbiStream::~DbiStream() = default;
53 Error DbiStream::reload() {
54 BinaryStreamReader Reader(*Stream);
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.");
63 if (Header->VersionSignature != -1)
64 return make_error<RawError>(raw_error_code::corrupt_file,
65 "Invalid DBI version signature.");
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.");
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.");
82 // Only certain substreams are guaranteed to be aligned. Validate
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.");
101 if (auto EC = Reader.readSubstream(ModiSubstream, Header->ModiSubstreamSize))
104 if (auto EC = Reader.readSubstream(SecContrSubstream,
105 Header->SecContrSubstreamSize))
107 if (auto EC = Reader.readSubstream(SecMapSubstream, Header->SectionMapSize))
109 if (auto EC = Reader.readSubstream(FileInfoSubstream, Header->FileInfoSize))
112 Reader.readSubstream(TypeServerMapSubstream, Header->TypeServerSize))
114 if (auto EC = Reader.readSubstream(ECSubstream, Header->ECSubstreamSize))
116 if (auto EC = Reader.readArray(
117 DbgStreams, Header->OptionalDbgHdrSize / sizeof(ulittle16_t)))
120 if (auto EC = Modules.initialize(ModiSubstream.StreamData,
121 FileInfoSubstream.StreamData))
124 if (auto EC = initializeSectionContributionData())
126 if (auto EC = initializeSectionHeadersData())
128 if (auto EC = initializeSectionMapData())
130 if (auto EC = initializeFpoRecords())
133 if (Reader.bytesRemaining() > 0)
134 return make_error<RawError>(raw_error_code::corrupt_file,
135 "Found unexpected bytes in DBI Stream.");
137 if (!ECSubstream.empty()) {
138 BinaryStreamReader ECReader(ECSubstream.StreamData);
139 if (auto EC = ECNames.reload(ECReader))
143 return Error::success();
146 PdbRaw_DbiVer DbiStream::getDbiVersion() const {
147 uint32_t Value = Header->VersionHeader;
148 return static_cast<PdbRaw_DbiVer>(Value);
151 uint32_t DbiStream::getAge() const { return Header->Age; }
153 uint16_t DbiStream::getPublicSymbolStreamIndex() const {
154 return Header->PublicSymbolStreamIndex;
157 uint16_t DbiStream::getGlobalSymbolStreamIndex() const {
158 return Header->GlobalSymbolStreamIndex;
161 uint16_t DbiStream::getFlags() const { return Header->Flags; }
163 bool DbiStream::isIncrementallyLinked() const {
164 return (Header->Flags & DbiFlags::FlagIncrementalMask) != 0;
167 bool DbiStream::hasCTypes() const {
168 return (Header->Flags & DbiFlags::FlagHasCTypesMask) != 0;
171 bool DbiStream::isStripped() const {
172 return (Header->Flags & DbiFlags::FlagStrippedMask) != 0;
175 uint16_t DbiStream::getBuildNumber() const { return Header->BuildNumber; }
177 uint16_t DbiStream::getBuildMajorVersion() const {
178 return (Header->BuildNumber & DbiBuildNo::BuildMajorMask) >>
179 DbiBuildNo::BuildMajorShift;
182 uint16_t DbiStream::getBuildMinorVersion() const {
183 return (Header->BuildNumber & DbiBuildNo::BuildMinorMask) >>
184 DbiBuildNo::BuildMinorShift;
187 uint16_t DbiStream::getPdbDllRbld() const { return Header->PdbDllRbld; }
189 uint32_t DbiStream::getPdbDllVersion() const { return Header->PdbDllVersion; }
191 uint32_t DbiStream::getSymRecordStreamIndex() const {
192 return Header->SymRecordStreamIndex;
195 PDB_Machine DbiStream::getMachineType() const {
196 uint16_t Machine = Header->MachineType;
197 return static_cast<PDB_Machine>(Machine);
200 FixedStreamArray<object::coff_section> DbiStream::getSectionHeaders() {
201 return SectionHeaders;
204 FixedStreamArray<object::FpoData> DbiStream::getFpoRecords() {
208 const DbiModuleList &DbiStream::modules() const { return Modules; }
210 FixedStreamArray<SecMapEntry> DbiStream::getSectionMap() const {
214 void DbiStream::visitSectionContributions(
215 ISectionContribVisitor &Visitor) const {
216 if (!SectionContribs.empty()) {
217 assert(SectionContribVersion == DbiSecContribVer60);
218 for (auto &SC : SectionContribs)
220 } else if (!SectionContribs2.empty()) {
221 assert(SectionContribVersion == DbiSecContribV2);
222 for (auto &SC : SectionContribs2)
227 Expected<StringRef> DbiStream::getECName(uint32_t NI) const {
228 return ECNames.getStringForID(NI);
231 Error DbiStream::initializeSectionContributionData() {
232 if (SecContrSubstream.empty())
233 return Error::success();
235 BinaryStreamReader SCReader(SecContrSubstream.StreamData);
236 if (auto EC = SCReader.readEnum(SectionContribVersion))
239 if (SectionContribVersion == DbiSecContribVer60)
240 return loadSectionContribs<SectionContrib>(SectionContribs, SCReader);
241 if (SectionContribVersion == DbiSecContribV2)
242 return loadSectionContribs<SectionContrib2>(SectionContribs2, SCReader);
244 return make_error<RawError>(raw_error_code::feature_unsupported,
245 "Unsupported DBI Section Contribution version");
248 // Initializes this->SectionHeaders.
249 Error DbiStream::initializeSectionHeadersData() {
250 if (DbgStreams.size() == 0)
251 return Error::success();
253 uint32_t StreamNum = getDebugStreamIndex(DbgHeaderType::SectionHdr);
254 if (StreamNum == kInvalidStreamIndex)
255 return Error::success();
257 if (StreamNum >= Pdb.getNumStreams())
258 return make_error<RawError>(raw_error_code::no_stream);
260 auto SHS = MappedBlockStream::createIndexedStream(
261 Pdb.getMsfLayout(), Pdb.getMsfBuffer(), StreamNum, Pdb.getAllocator());
263 size_t StreamLen = SHS->getLength();
264 if (StreamLen % sizeof(object::coff_section))
265 return make_error<RawError>(raw_error_code::corrupt_file,
266 "Corrupted section header stream.");
268 size_t NumSections = StreamLen / sizeof(object::coff_section);
269 BinaryStreamReader Reader(*SHS);
270 if (auto EC = Reader.readArray(SectionHeaders, NumSections))
271 return make_error<RawError>(raw_error_code::corrupt_file,
272 "Could not read a bitmap.");
274 SectionHeaderStream = std::move(SHS);
275 return Error::success();
278 // Initializes this->Fpos.
279 Error DbiStream::initializeFpoRecords() {
280 if (DbgStreams.size() == 0)
281 return Error::success();
283 uint32_t StreamNum = getDebugStreamIndex(DbgHeaderType::NewFPO);
285 // This means there is no FPO data.
286 if (StreamNum == kInvalidStreamIndex)
287 return Error::success();
289 if (StreamNum >= Pdb.getNumStreams())
290 return make_error<RawError>(raw_error_code::no_stream);
292 auto FS = MappedBlockStream::createIndexedStream(
293 Pdb.getMsfLayout(), Pdb.getMsfBuffer(), StreamNum, Pdb.getAllocator());
295 size_t StreamLen = FS->getLength();
296 if (StreamLen % sizeof(object::FpoData))
297 return make_error<RawError>(raw_error_code::corrupt_file,
298 "Corrupted New FPO stream.");
300 size_t NumRecords = StreamLen / sizeof(object::FpoData);
301 BinaryStreamReader Reader(*FS);
302 if (auto EC = Reader.readArray(FpoRecords, NumRecords))
303 return make_error<RawError>(raw_error_code::corrupt_file,
304 "Corrupted New FPO stream.");
305 FpoStream = std::move(FS);
306 return Error::success();
309 BinarySubstreamRef DbiStream::getSectionContributionData() const {
310 return SecContrSubstream;
313 BinarySubstreamRef DbiStream::getSecMapSubstreamData() const {
314 return SecMapSubstream;
317 BinarySubstreamRef DbiStream::getModiSubstreamData() const {
318 return ModiSubstream;
321 BinarySubstreamRef DbiStream::getFileInfoSubstreamData() const {
322 return FileInfoSubstream;
325 BinarySubstreamRef DbiStream::getTypeServerMapSubstreamData() const {
326 return TypeServerMapSubstream;
329 BinarySubstreamRef DbiStream::getECSubstreamData() const { return ECSubstream; }
331 Error DbiStream::initializeSectionMapData() {
332 if (SecMapSubstream.empty())
333 return Error::success();
335 BinaryStreamReader SMReader(SecMapSubstream.StreamData);
336 const SecMapHeader *Header;
337 if (auto EC = SMReader.readObject(Header))
339 if (auto EC = SMReader.readArray(SectionMap, Header->SecCount))
341 return Error::success();
344 uint32_t DbiStream::getDebugStreamIndex(DbgHeaderType Type) const {
345 uint16_t T = static_cast<uint16_t>(Type);
346 if (T >= DbgStreams.size())
347 return kInvalidStreamIndex;
348 return DbgStreams[T];