blob: 1827ab078df21bfafc0873462bcf88d211512645 [file] [log] [blame]
Zachary Turner2f09b502016-04-29 17:28:47 +00001//===- DbiStream.cpp - PDB Dbi Stream (Stream 3) Access -------------------===//
Zachary Turner53a65ba2016-04-26 18:42:34 +00002//
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//===----------------------------------------------------------------------===//
Zachary Turner2f09b502016-04-29 17:28:47 +00009#include "llvm/DebugInfo/PDB/Raw/DbiStream.h"
Zachary Turner819e77d2016-05-06 20:51:57 +000010
Zachary Turner8dbe3622016-05-27 01:54:44 +000011#include "llvm/DebugInfo/CodeView/StreamArray.h"
Zachary Turnerd5d37dc2016-05-25 20:37:03 +000012#include "llvm/DebugInfo/CodeView/StreamReader.h"
Zachary Turner2f09b502016-04-29 17:28:47 +000013#include "llvm/DebugInfo/PDB/Raw/InfoStream.h"
Zachary Turner1822af542016-04-27 23:41:42 +000014#include "llvm/DebugInfo/PDB/Raw/ModInfo.h"
Zachary Turner0eace0b2016-05-02 18:09:14 +000015#include "llvm/DebugInfo/PDB/Raw/NameHashTable.h"
Zachary Turner53a65ba2016-04-26 18:42:34 +000016#include "llvm/DebugInfo/PDB/Raw/PDBFile.h"
Zachary Turner2f09b502016-04-29 17:28:47 +000017#include "llvm/DebugInfo/PDB/Raw/RawConstants.h"
Zachary Turner819e77d2016-05-06 20:51:57 +000018#include "llvm/DebugInfo/PDB/Raw/RawError.h"
Zachary Turner53a65ba2016-04-26 18:42:34 +000019
20using namespace llvm;
Zachary Turner2f09b502016-04-29 17:28:47 +000021using namespace llvm::pdb;
Zachary Turner53a65ba2016-04-26 18:42:34 +000022using namespace llvm::support;
23
24namespace {
25// Some of the values are stored in bitfields. Since this needs to be portable
26// across compilers and architectures (big / little endian in particular) we
27// can't use the actual structures below, but must instead do the shifting
28// and masking ourselves. The struct definitions are provided for reference.
29
30// struct DbiFlags {
31// uint16_t IncrementalLinking : 1; // True if linked incrementally
32// uint16_t IsStripped : 1; // True if private symbols were stripped.
33// uint16_t HasCTypes : 1; // True if linked with /debug:ctypes.
34// uint16_t Reserved : 13;
35//};
36const uint16_t FlagIncrementalMask = 0x0001;
37const uint16_t FlagStrippedMask = 0x0002;
38const uint16_t FlagHasCTypesMask = 0x0004;
39
40// struct DbiBuildNo {
41// uint16_t MinorVersion : 8;
42// uint16_t MajorVersion : 7;
43// uint16_t NewVersionFormat : 1;
44//};
45const uint16_t BuildMinorMask = 0x00FF;
46const uint16_t BuildMinorShift = 0;
47
48const uint16_t BuildMajorMask = 0x7F00;
49const uint16_t BuildMajorShift = 8;
Zachary Turner53a65ba2016-04-26 18:42:34 +000050}
51
Zachary Turner2f09b502016-04-29 17:28:47 +000052struct DbiStream::HeaderInfo {
Zachary Turnerff788aa2016-04-26 19:24:10 +000053 little32_t VersionSignature;
Zachary Turner53a65ba2016-04-26 18:42:34 +000054 ulittle32_t VersionHeader;
Rui Ueyama1f6b6e22016-05-13 21:21:53 +000055 ulittle32_t Age; // Should match InfoStream.
Rui Ueyama0376b1a2016-05-19 18:05:58 +000056 ulittle16_t GlobalSymbolStreamIndex; // Global symbol stream #
Rui Ueyama1f6b6e22016-05-13 21:21:53 +000057 ulittle16_t BuildNumber; // See DbiBuildNo structure.
Rui Ueyama0376b1a2016-05-19 18:05:58 +000058 ulittle16_t PublicSymbolStreamIndex; // Public symbols stream #
Rui Ueyama1f6b6e22016-05-13 21:21:53 +000059 ulittle16_t PdbDllVersion; // version of mspdbNNN.dll
Rui Ueyama0376b1a2016-05-19 18:05:58 +000060 ulittle16_t SymRecordStreamIndex; // Symbol records stream #
Rui Ueyama1f6b6e22016-05-13 21:21:53 +000061 ulittle16_t PdbDllRbld; // rbld number of mspdbNNN.dll
62 little32_t ModiSubstreamSize; // Size of module info stream
63 little32_t SecContrSubstreamSize; // Size of sec. contribution stream
64 little32_t SectionMapSize; // Size of sec. map substream
65 little32_t FileInfoSize; // Size of file info substream
Zachary Turner2f09b502016-04-29 17:28:47 +000066 little32_t TypeServerSize; // Size of type server map
67 ulittle32_t MFCTypeServerIndex; // Index of MFC Type Server
68 little32_t OptionalDbgHdrSize; // Size of DbgHeader info
69 little32_t ECSubstreamSize; // Size of EC stream (what is EC?)
70 ulittle16_t Flags; // See DbiFlags enum.
71 ulittle16_t MachineType; // See PDB_MachineType enum.
Zachary Turner53a65ba2016-04-26 18:42:34 +000072
73 ulittle32_t Reserved; // Pad to 64 bytes
74};
75
Zachary Turner8dbe3622016-05-27 01:54:44 +000076DbiStream::DbiStream(PDBFile &File)
77 : Pdb(File), Stream(StreamDBI, File), Header(nullptr) {
Zachary Turner53a65ba2016-04-26 18:42:34 +000078 static_assert(sizeof(HeaderInfo) == 64, "Invalid HeaderInfo size!");
79}
80
Zachary Turner2f09b502016-04-29 17:28:47 +000081DbiStream::~DbiStream() {}
Zachary Turner53a65ba2016-04-26 18:42:34 +000082
Zachary Turner819e77d2016-05-06 20:51:57 +000083Error DbiStream::reload() {
Zachary Turnerd5d37dc2016-05-25 20:37:03 +000084 codeview::StreamReader Reader(Stream);
Zachary Turner6ba65de2016-04-29 17:22:58 +000085
Zachary Turner53a65ba2016-04-26 18:42:34 +000086 if (Stream.getLength() < sizeof(HeaderInfo))
Zachary Turner819e77d2016-05-06 20:51:57 +000087 return make_error<RawError>(raw_error_code::corrupt_file,
88 "DBI Stream does not contain a header.");
Zachary Turner8dbe3622016-05-27 01:54:44 +000089 if (auto EC = Reader.readObject(Header))
Zachary Turner819e77d2016-05-06 20:51:57 +000090 return make_error<RawError>(raw_error_code::corrupt_file,
91 "DBI Stream does not contain a header.");
Zachary Turner53a65ba2016-04-26 18:42:34 +000092
93 if (Header->VersionSignature != -1)
Zachary Turner819e77d2016-05-06 20:51:57 +000094 return make_error<RawError>(raw_error_code::corrupt_file,
95 "Invalid DBI version signature.");
Zachary Turner53a65ba2016-04-26 18:42:34 +000096
Zachary Turner1822af542016-04-27 23:41:42 +000097 // Require at least version 7, which should be present in all PDBs
98 // produced in the last decade and allows us to avoid having to
99 // special case all kinds of complicated arcane formats.
100 if (Header->VersionHeader < PdbDbiV70)
Zachary Turner819e77d2016-05-06 20:51:57 +0000101 return make_error<RawError>(raw_error_code::corrupt_file,
102 "Unsupported DBI version.");
Zachary Turner53a65ba2016-04-26 18:42:34 +0000103
Zachary Turner819e77d2016-05-06 20:51:57 +0000104 auto InfoStream = Pdb.getPDBInfoStream();
105 if (auto EC = InfoStream.takeError())
106 return EC;
107
108 if (Header->Age != InfoStream.get().getAge())
109 return make_error<RawError>(raw_error_code::corrupt_file,
110 "DBI Age does not match PDB Age.");
Zachary Turner53a65ba2016-04-26 18:42:34 +0000111
112 if (Stream.getLength() !=
113 sizeof(HeaderInfo) + Header->ModiSubstreamSize +
114 Header->SecContrSubstreamSize + Header->SectionMapSize +
115 Header->FileInfoSize + Header->TypeServerSize +
116 Header->OptionalDbgHdrSize + Header->ECSubstreamSize)
Zachary Turner819e77d2016-05-06 20:51:57 +0000117 return make_error<RawError>(raw_error_code::corrupt_file,
118 "DBI Length does not equal sum of substreams.");
Zachary Turner53a65ba2016-04-26 18:42:34 +0000119
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000120 // Only certain substreams are guaranteed to be aligned. Validate
121 // them here.
Zachary Turner1822af542016-04-27 23:41:42 +0000122 if (Header->ModiSubstreamSize % sizeof(uint32_t) != 0)
Zachary Turner819e77d2016-05-06 20:51:57 +0000123 return make_error<RawError>(raw_error_code::corrupt_file,
124 "DBI MODI substream not aligned.");
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000125 if (Header->SecContrSubstreamSize % sizeof(uint32_t) != 0)
Zachary Turner819e77d2016-05-06 20:51:57 +0000126 return make_error<RawError>(
127 raw_error_code::corrupt_file,
128 "DBI section contribution substream not aligned.");
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000129 if (Header->SectionMapSize % sizeof(uint32_t) != 0)
Zachary Turner819e77d2016-05-06 20:51:57 +0000130 return make_error<RawError>(raw_error_code::corrupt_file,
131 "DBI section map substream not aligned.");
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000132 if (Header->FileInfoSize % sizeof(uint32_t) != 0)
Zachary Turner819e77d2016-05-06 20:51:57 +0000133 return make_error<RawError>(raw_error_code::corrupt_file,
134 "DBI file info substream not aligned.");
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000135 if (Header->TypeServerSize % sizeof(uint32_t) != 0)
Zachary Turner819e77d2016-05-06 20:51:57 +0000136 return make_error<RawError>(raw_error_code::corrupt_file,
137 "DBI type server substream not aligned.");
Zachary Turner1822af542016-04-27 23:41:42 +0000138
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000139 // Since each ModInfo in the stream is a variable length, we have to iterate
140 // them to know how many there actually are.
Zachary Turner1de49c92016-05-27 18:47:20 +0000141 codeview::VarStreamArray<ModInfo> ModInfoArray;
142 if (auto EC = Reader.readArray(ModInfoArray, Header->ModiSubstreamSize))
143 return EC;
144 for (auto &Info : ModInfoArray) {
Zachary Turner8dbe3622016-05-27 01:54:44 +0000145 ModuleInfos.emplace_back(Info);
146 }
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000147
Zachary Turner8dbe3622016-05-27 01:54:44 +0000148 if (auto EC = Reader.readStreamRef(SecContrSubstream,
149 Header->SecContrSubstreamSize))
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000150 return EC;
Zachary Turner8dbe3622016-05-27 01:54:44 +0000151 if (auto EC = Reader.readStreamRef(SecMapSubstream, Header->SectionMapSize))
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000152 return EC;
Zachary Turner8dbe3622016-05-27 01:54:44 +0000153 if (auto EC = Reader.readStreamRef(FileInfoSubstream, Header->FileInfoSize))
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000154 return EC;
Zachary Turner819e77d2016-05-06 20:51:57 +0000155 if (auto EC =
Zachary Turner8dbe3622016-05-27 01:54:44 +0000156 Reader.readStreamRef(TypeServerMapSubstream, Header->TypeServerSize))
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000157 return EC;
Zachary Turner8dbe3622016-05-27 01:54:44 +0000158 if (auto EC = Reader.readStreamRef(ECSubstream, Header->ECSubstreamSize))
Zachary Turner6ba65de2016-04-29 17:22:58 +0000159 return EC;
Zachary Turner8dbe3622016-05-27 01:54:44 +0000160 if (auto EC = Reader.readArray(DbgStreams, Header->OptionalDbgHdrSize /
161 sizeof(ulittle16_t)))
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000162 return EC;
163
Zachary Turner819e77d2016-05-06 20:51:57 +0000164 if (auto EC = initializeFileInfo())
Zachary Turner1822af542016-04-27 23:41:42 +0000165 return EC;
166
Zachary Turner6ba65de2016-04-29 17:22:58 +0000167 if (Reader.bytesRemaining() > 0)
Zachary Turner819e77d2016-05-06 20:51:57 +0000168 return make_error<RawError>(raw_error_code::corrupt_file,
169 "Found unexpected bytes in DBI Stream.");
Zachary Turner6ba65de2016-04-29 17:22:58 +0000170
Zachary Turnerd5d37dc2016-05-25 20:37:03 +0000171 codeview::StreamReader ECReader(ECSubstream);
Zachary Turner819e77d2016-05-06 20:51:57 +0000172 if (auto EC = ECNames.load(ECReader))
173 return EC;
Zachary Turner0eace0b2016-05-02 18:09:14 +0000174
Zachary Turner819e77d2016-05-06 20:51:57 +0000175 return Error::success();
Zachary Turner53a65ba2016-04-26 18:42:34 +0000176}
177
Zachary Turner2f09b502016-04-29 17:28:47 +0000178PdbRaw_DbiVer DbiStream::getDbiVersion() const {
Zachary Turner53a65ba2016-04-26 18:42:34 +0000179 uint32_t Value = Header->VersionHeader;
180 return static_cast<PdbRaw_DbiVer>(Value);
181}
182
Zachary Turner2f09b502016-04-29 17:28:47 +0000183uint32_t DbiStream::getAge() const { return Header->Age; }
Zachary Turner53a65ba2016-04-26 18:42:34 +0000184
Rui Ueyama1f6b6e22016-05-13 21:21:53 +0000185uint16_t DbiStream::getPublicSymbolStreamIndex() const {
186 return Header->PublicSymbolStreamIndex;
187}
188
Zachary Turner96e60f72016-05-24 20:31:48 +0000189uint16_t DbiStream::getGlobalSymbolStreamIndex() const {
190 return Header->GlobalSymbolStreamIndex;
191}
192
Zachary Turner2f09b502016-04-29 17:28:47 +0000193bool DbiStream::isIncrementallyLinked() const {
Zachary Turner53a65ba2016-04-26 18:42:34 +0000194 return (Header->Flags & FlagIncrementalMask) != 0;
195}
196
Zachary Turner2f09b502016-04-29 17:28:47 +0000197bool DbiStream::hasCTypes() const {
Zachary Turner53a65ba2016-04-26 18:42:34 +0000198 return (Header->Flags & FlagHasCTypesMask) != 0;
199}
200
Zachary Turner2f09b502016-04-29 17:28:47 +0000201bool DbiStream::isStripped() const {
Zachary Turner53a65ba2016-04-26 18:42:34 +0000202 return (Header->Flags & FlagStrippedMask) != 0;
203}
204
Zachary Turner2f09b502016-04-29 17:28:47 +0000205uint16_t DbiStream::getBuildMajorVersion() const {
Zachary Turner53a65ba2016-04-26 18:42:34 +0000206 return (Header->BuildNumber & BuildMajorMask) >> BuildMajorShift;
207}
208
Zachary Turner2f09b502016-04-29 17:28:47 +0000209uint16_t DbiStream::getBuildMinorVersion() const {
Zachary Turner53a65ba2016-04-26 18:42:34 +0000210 return (Header->BuildNumber & BuildMinorMask) >> BuildMinorShift;
211}
212
Zachary Turner2f09b502016-04-29 17:28:47 +0000213uint32_t DbiStream::getPdbDllVersion() const { return Header->PdbDllVersion; }
Zachary Turner53a65ba2016-04-26 18:42:34 +0000214
Rui Ueyama0376b1a2016-05-19 18:05:58 +0000215uint32_t DbiStream::getSymRecordStreamIndex() const {
216 return Header->SymRecordStreamIndex;
217}
Zachary Turner53a65ba2016-04-26 18:42:34 +0000218
Zachary Turner2f09b502016-04-29 17:28:47 +0000219PDB_Machine DbiStream::getMachineType() const {
Zachary Turner53a65ba2016-04-26 18:42:34 +0000220 uint16_t Machine = Header->MachineType;
221 return static_cast<PDB_Machine>(Machine);
222}
Zachary Turner1822af542016-04-27 23:41:42 +0000223
Zachary Turner2f09b502016-04-29 17:28:47 +0000224ArrayRef<ModuleInfoEx> DbiStream::modules() const { return ModuleInfos; }
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000225
Zachary Turner819e77d2016-05-06 20:51:57 +0000226Error DbiStream::initializeFileInfo() {
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000227 struct FileInfoSubstreamHeader {
228 ulittle16_t NumModules; // Total # of modules, should match number of
229 // records in the ModuleInfo substream.
230 ulittle16_t NumSourceFiles; // Total # of source files. This value is not
231 // accurate because PDB actually supports more
232 // than 64k source files, so we ignore it and
233 // compute the value from other stream fields.
234 };
235
236 // The layout of the FileInfoSubstream is like this:
237 // struct {
238 // ulittle16_t NumModules;
239 // ulittle16_t NumSourceFiles;
240 // ulittle16_t ModIndices[NumModules];
241 // ulittle16_t ModFileCounts[NumModules];
242 // ulittle32_t FileNameOffsets[NumSourceFiles];
243 // char Names[][NumSourceFiles];
244 // };
245 // with the caveat that `NumSourceFiles` cannot be trusted, so
246 // it is computed by summing `ModFileCounts`.
247 //
Zachary Turner8dbe3622016-05-27 01:54:44 +0000248 const FileInfoSubstreamHeader *FH;
249 codeview::StreamReader FISR(FileInfoSubstream);
250 if (auto EC = FISR.readObject(FH))
251 return EC;
252
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000253 // The number of modules in the stream should be the same as reported by
254 // the FileInfoSubstreamHeader.
Zachary Turner8dbe3622016-05-27 01:54:44 +0000255 if (FH->NumModules != ModuleInfos.size())
Zachary Turner819e77d2016-05-06 20:51:57 +0000256 return make_error<RawError>(raw_error_code::corrupt_file,
257 "FileInfo substream count doesn't match DBI.");
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000258
Zachary Turner8dbe3622016-05-27 01:54:44 +0000259 codeview::FixedStreamArray<ulittle16_t> ModIndexArray;
260 codeview::FixedStreamArray<ulittle16_t> ModFileCountArray;
261 codeview::FixedStreamArray<little32_t> FileNameOffsets;
262
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000263 // First is an array of `NumModules` module indices. This is not used for the
264 // same reason that `NumSourceFiles` is not used. It's an array of uint16's,
265 // but it's possible there are more than 64k source files, which would imply
266 // more than 64k modules (e.g. object files) as well. So we ignore this
267 // field.
Zachary Turner8dbe3622016-05-27 01:54:44 +0000268 if (auto EC = FISR.readArray(ModIndexArray, ModuleInfos.size()))
269 return EC;
270 if (auto EC = FISR.readArray(ModFileCountArray, ModuleInfos.size()))
271 return EC;
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000272
273 // Compute the real number of source files.
274 uint32_t NumSourceFiles = 0;
275 for (auto Count : ModFileCountArray)
276 NumSourceFiles += Count;
277
278 // This is the array that in the reference implementation corresponds to
279 // `ModInfo::FileLayout::FileNameOffs`, which is commented there as being a
280 // pointer. Due to the mentioned problems of pointers causing difficulty
281 // when reading from the file on 64-bit systems, we continue to ignore that
282 // field in `ModInfo`, and instead build a vector of StringRefs and stores
283 // them in `ModuleInfoEx`. The value written to and read from the file is
284 // not used anyway, it is only there as a way to store the offsets for the
285 // purposes of later accessing the names at runtime.
Zachary Turner8dbe3622016-05-27 01:54:44 +0000286 if (auto EC = FISR.readArray(FileNameOffsets, NumSourceFiles))
287 return EC;
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000288
Zachary Turner8dbe3622016-05-27 01:54:44 +0000289 codeview::StreamRef NamesBufferRef;
290 if (auto EC = FISR.readStreamRef(NamesBufferRef))
291 return EC;
292 codeview::StreamReader Names(NamesBufferRef);
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000293
294 // We go through each ModuleInfo, determine the number N of source files for
295 // that module, and then get the next N offsets from the Offsets array, using
296 // them to get the corresponding N names from the Names buffer and associating
297 // each one with the corresponding module.
298 uint32_t NextFileIndex = 0;
299 for (size_t I = 0; I < ModuleInfos.size(); ++I) {
300 uint32_t NumFiles = ModFileCountArray[I];
301 ModuleInfos[I].SourceFiles.resize(NumFiles);
302 for (size_t J = 0; J < NumFiles; ++J, ++NextFileIndex) {
Zachary Turner8dbe3622016-05-27 01:54:44 +0000303 uint32_t FileOffset = FileNameOffsets[NextFileIndex];
304 Names.setOffset(FileOffset);
305 if (auto EC = Names.readZeroString(ModuleInfos[I].SourceFiles[J]))
306 return EC;
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000307 }
308 }
309
Zachary Turner819e77d2016-05-06 20:51:57 +0000310 return Error::success();
Zachary Turner1822af542016-04-27 23:41:42 +0000311}
Zachary Turnerd3076ab2016-05-25 05:49:48 +0000312
313uint32_t DbiStream::getDebugStreamIndex(DbgHeaderType Type) const {
Zachary Turner8dbe3622016-05-27 01:54:44 +0000314 return DbgStreams[static_cast<uint16_t>(Type)];
Zachary Turnerd3076ab2016-05-25 05:49:48 +0000315}