blob: 6b5cd212ed95423a44c7dda13b442988daf7ac1d [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//===----------------------------------------------------------------------===//
9
Zachary Turner2f09b502016-04-29 17:28:47 +000010#include "llvm/DebugInfo/PDB/Raw/DbiStream.h"
Zachary Turner819e77d2016-05-06 20:51:57 +000011
Zachary Turner2f09b502016-04-29 17:28:47 +000012#include "llvm/DebugInfo/PDB/Raw/InfoStream.h"
Zachary Turner1822af542016-04-27 23:41:42 +000013#include "llvm/DebugInfo/PDB/Raw/ModInfo.h"
Zachary Turner0eace0b2016-05-02 18:09:14 +000014#include "llvm/DebugInfo/PDB/Raw/NameHashTable.h"
Zachary Turner53a65ba2016-04-26 18:42:34 +000015#include "llvm/DebugInfo/PDB/Raw/PDBFile.h"
Zachary Turner2f09b502016-04-29 17:28:47 +000016#include "llvm/DebugInfo/PDB/Raw/RawConstants.h"
Zachary Turner819e77d2016-05-06 20:51:57 +000017#include "llvm/DebugInfo/PDB/Raw/RawError.h"
Zachary Turner6ba65de2016-04-29 17:22:58 +000018#include "llvm/DebugInfo/PDB/Raw/StreamReader.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 Turnerb56d9042016-05-02 18:09:21 +000076DbiStream::DbiStream(PDBFile &File) : Pdb(File), Stream(StreamDBI, File) {
Zachary Turner53a65ba2016-04-26 18:42:34 +000077 static_assert(sizeof(HeaderInfo) == 64, "Invalid HeaderInfo size!");
78}
79
Zachary Turner2f09b502016-04-29 17:28:47 +000080DbiStream::~DbiStream() {}
Zachary Turner53a65ba2016-04-26 18:42:34 +000081
Zachary Turner819e77d2016-05-06 20:51:57 +000082Error DbiStream::reload() {
Zachary Turner6ba65de2016-04-29 17:22:58 +000083 StreamReader Reader(Stream);
84
Zachary Turner53a65ba2016-04-26 18:42:34 +000085 Header.reset(new HeaderInfo());
86
87 if (Stream.getLength() < sizeof(HeaderInfo))
Zachary Turner819e77d2016-05-06 20:51:57 +000088 return make_error<RawError>(raw_error_code::corrupt_file,
89 "DBI Stream does not contain a header.");
90 if (auto EC = Reader.readObject(Header.get()))
91 return make_error<RawError>(raw_error_code::corrupt_file,
92 "DBI Stream does not contain a header.");
Zachary Turner53a65ba2016-04-26 18:42:34 +000093
94 if (Header->VersionSignature != -1)
Zachary Turner819e77d2016-05-06 20:51:57 +000095 return make_error<RawError>(raw_error_code::corrupt_file,
96 "Invalid DBI version signature.");
Zachary Turner53a65ba2016-04-26 18:42:34 +000097
Zachary Turner1822af542016-04-27 23:41:42 +000098 // Require at least version 7, which should be present in all PDBs
99 // produced in the last decade and allows us to avoid having to
100 // special case all kinds of complicated arcane formats.
101 if (Header->VersionHeader < PdbDbiV70)
Zachary Turner819e77d2016-05-06 20:51:57 +0000102 return make_error<RawError>(raw_error_code::corrupt_file,
103 "Unsupported DBI version.");
Zachary Turner53a65ba2016-04-26 18:42:34 +0000104
Zachary Turner819e77d2016-05-06 20:51:57 +0000105 auto InfoStream = Pdb.getPDBInfoStream();
106 if (auto EC = InfoStream.takeError())
107 return EC;
108
109 if (Header->Age != InfoStream.get().getAge())
110 return make_error<RawError>(raw_error_code::corrupt_file,
111 "DBI Age does not match PDB Age.");
Zachary Turner53a65ba2016-04-26 18:42:34 +0000112
113 if (Stream.getLength() !=
114 sizeof(HeaderInfo) + Header->ModiSubstreamSize +
115 Header->SecContrSubstreamSize + Header->SectionMapSize +
116 Header->FileInfoSize + Header->TypeServerSize +
117 Header->OptionalDbgHdrSize + Header->ECSubstreamSize)
Zachary Turner819e77d2016-05-06 20:51:57 +0000118 return make_error<RawError>(raw_error_code::corrupt_file,
119 "DBI Length does not equal sum of substreams.");
Zachary Turner53a65ba2016-04-26 18:42:34 +0000120
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000121 // Only certain substreams are guaranteed to be aligned. Validate
122 // them here.
Zachary Turner1822af542016-04-27 23:41:42 +0000123 if (Header->ModiSubstreamSize % sizeof(uint32_t) != 0)
Zachary Turner819e77d2016-05-06 20:51:57 +0000124 return make_error<RawError>(raw_error_code::corrupt_file,
125 "DBI MODI substream not aligned.");
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000126 if (Header->SecContrSubstreamSize % sizeof(uint32_t) != 0)
Zachary Turner819e77d2016-05-06 20:51:57 +0000127 return make_error<RawError>(
128 raw_error_code::corrupt_file,
129 "DBI section contribution substream not aligned.");
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000130 if (Header->SectionMapSize % sizeof(uint32_t) != 0)
Zachary Turner819e77d2016-05-06 20:51:57 +0000131 return make_error<RawError>(raw_error_code::corrupt_file,
132 "DBI section map substream not aligned.");
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000133 if (Header->FileInfoSize % sizeof(uint32_t) != 0)
Zachary Turner819e77d2016-05-06 20:51:57 +0000134 return make_error<RawError>(raw_error_code::corrupt_file,
135 "DBI file info substream not aligned.");
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000136 if (Header->TypeServerSize % sizeof(uint32_t) != 0)
Zachary Turner819e77d2016-05-06 20:51:57 +0000137 return make_error<RawError>(raw_error_code::corrupt_file,
138 "DBI type server substream not aligned.");
Zachary Turner1822af542016-04-27 23:41:42 +0000139
Zachary Turner819e77d2016-05-06 20:51:57 +0000140 if (auto EC = ModInfoSubstream.initialize(Reader, Header->ModiSubstreamSize))
141 return EC;
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000142
143 // Since each ModInfo in the stream is a variable length, we have to iterate
144 // them to know how many there actually are.
Zachary Turner6ba65de2016-04-29 17:22:58 +0000145 auto Range =
146 llvm::make_range(ModInfoIterator(&ModInfoSubstream.data().front()),
147 ModInfoIterator(&ModInfoSubstream.data().back() + 1));
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000148 for (auto Info : Range)
149 ModuleInfos.push_back(ModuleInfoEx(Info));
150
Zachary Turner819e77d2016-05-06 20:51:57 +0000151 if (auto EC =
152 SecContrSubstream.initialize(Reader, Header->SecContrSubstreamSize))
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000153 return EC;
Zachary Turner819e77d2016-05-06 20:51:57 +0000154 if (auto EC = SecMapSubstream.initialize(Reader, Header->SectionMapSize))
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000155 return EC;
Zachary Turner819e77d2016-05-06 20:51:57 +0000156 if (auto EC = FileInfoSubstream.initialize(Reader, Header->FileInfoSize))
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000157 return EC;
Zachary Turner819e77d2016-05-06 20:51:57 +0000158 if (auto EC =
159 TypeServerMapSubstream.initialize(Reader, Header->TypeServerSize))
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000160 return EC;
Zachary Turner819e77d2016-05-06 20:51:57 +0000161 if (auto EC = ECSubstream.initialize(Reader, Header->ECSubstreamSize))
Zachary Turner6ba65de2016-04-29 17:22:58 +0000162 return EC;
Zachary Turner819e77d2016-05-06 20:51:57 +0000163 if (auto EC = DbgHeader.initialize(Reader, Header->OptionalDbgHdrSize))
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000164 return EC;
165
Zachary Turner819e77d2016-05-06 20:51:57 +0000166 if (auto EC = initializeFileInfo())
Zachary Turner1822af542016-04-27 23:41:42 +0000167 return EC;
168
Zachary Turner6ba65de2016-04-29 17:22:58 +0000169 if (Reader.bytesRemaining() > 0)
Zachary Turner819e77d2016-05-06 20:51:57 +0000170 return make_error<RawError>(raw_error_code::corrupt_file,
171 "Found unexpected bytes in DBI Stream.");
Zachary Turner6ba65de2016-04-29 17:22:58 +0000172
Zachary Turner0eace0b2016-05-02 18:09:14 +0000173 StreamReader ECReader(ECSubstream);
Zachary Turner819e77d2016-05-06 20:51:57 +0000174 if (auto EC = ECNames.load(ECReader))
175 return EC;
Zachary Turner0eace0b2016-05-02 18:09:14 +0000176
Zachary Turner819e77d2016-05-06 20:51:57 +0000177 return Error::success();
Zachary Turner53a65ba2016-04-26 18:42:34 +0000178}
179
Zachary Turner2f09b502016-04-29 17:28:47 +0000180PdbRaw_DbiVer DbiStream::getDbiVersion() const {
Zachary Turner53a65ba2016-04-26 18:42:34 +0000181 uint32_t Value = Header->VersionHeader;
182 return static_cast<PdbRaw_DbiVer>(Value);
183}
184
Zachary Turner2f09b502016-04-29 17:28:47 +0000185uint32_t DbiStream::getAge() const { return Header->Age; }
Zachary Turner53a65ba2016-04-26 18:42:34 +0000186
Rui Ueyama1f6b6e22016-05-13 21:21:53 +0000187uint16_t DbiStream::getPublicSymbolStreamIndex() const {
188 return Header->PublicSymbolStreamIndex;
189}
190
Zachary Turner96e60f72016-05-24 20:31:48 +0000191uint16_t DbiStream::getGlobalSymbolStreamIndex() const {
192 return Header->GlobalSymbolStreamIndex;
193}
194
Zachary Turner2f09b502016-04-29 17:28:47 +0000195bool DbiStream::isIncrementallyLinked() const {
Zachary Turner53a65ba2016-04-26 18:42:34 +0000196 return (Header->Flags & FlagIncrementalMask) != 0;
197}
198
Zachary Turner2f09b502016-04-29 17:28:47 +0000199bool DbiStream::hasCTypes() const {
Zachary Turner53a65ba2016-04-26 18:42:34 +0000200 return (Header->Flags & FlagHasCTypesMask) != 0;
201}
202
Zachary Turner2f09b502016-04-29 17:28:47 +0000203bool DbiStream::isStripped() const {
Zachary Turner53a65ba2016-04-26 18:42:34 +0000204 return (Header->Flags & FlagStrippedMask) != 0;
205}
206
Zachary Turner2f09b502016-04-29 17:28:47 +0000207uint16_t DbiStream::getBuildMajorVersion() const {
Zachary Turner53a65ba2016-04-26 18:42:34 +0000208 return (Header->BuildNumber & BuildMajorMask) >> BuildMajorShift;
209}
210
Zachary Turner2f09b502016-04-29 17:28:47 +0000211uint16_t DbiStream::getBuildMinorVersion() const {
Zachary Turner53a65ba2016-04-26 18:42:34 +0000212 return (Header->BuildNumber & BuildMinorMask) >> BuildMinorShift;
213}
214
Zachary Turner2f09b502016-04-29 17:28:47 +0000215uint32_t DbiStream::getPdbDllVersion() const { return Header->PdbDllVersion; }
Zachary Turner53a65ba2016-04-26 18:42:34 +0000216
Rui Ueyama0376b1a2016-05-19 18:05:58 +0000217uint32_t DbiStream::getSymRecordStreamIndex() const {
218 return Header->SymRecordStreamIndex;
219}
Zachary Turner53a65ba2016-04-26 18:42:34 +0000220
Zachary Turner2f09b502016-04-29 17:28:47 +0000221PDB_Machine DbiStream::getMachineType() const {
Zachary Turner53a65ba2016-04-26 18:42:34 +0000222 uint16_t Machine = Header->MachineType;
223 return static_cast<PDB_Machine>(Machine);
224}
Zachary Turner1822af542016-04-27 23:41:42 +0000225
Zachary Turner2f09b502016-04-29 17:28:47 +0000226ArrayRef<ModuleInfoEx> DbiStream::modules() const { return ModuleInfos; }
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000227
Zachary Turner819e77d2016-05-06 20:51:57 +0000228Error DbiStream::initializeFileInfo() {
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000229 struct FileInfoSubstreamHeader {
230 ulittle16_t NumModules; // Total # of modules, should match number of
231 // records in the ModuleInfo substream.
232 ulittle16_t NumSourceFiles; // Total # of source files. This value is not
233 // accurate because PDB actually supports more
234 // than 64k source files, so we ignore it and
235 // compute the value from other stream fields.
236 };
237
238 // The layout of the FileInfoSubstream is like this:
239 // struct {
240 // ulittle16_t NumModules;
241 // ulittle16_t NumSourceFiles;
242 // ulittle16_t ModIndices[NumModules];
243 // ulittle16_t ModFileCounts[NumModules];
244 // ulittle32_t FileNameOffsets[NumSourceFiles];
245 // char Names[][NumSourceFiles];
246 // };
247 // with the caveat that `NumSourceFiles` cannot be trusted, so
248 // it is computed by summing `ModFileCounts`.
249 //
Zachary Turner6ba65de2016-04-29 17:22:58 +0000250 const uint8_t *Buf = &FileInfoSubstream.data().front();
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000251 auto FI = reinterpret_cast<const FileInfoSubstreamHeader *>(Buf);
252 Buf += sizeof(FileInfoSubstreamHeader);
253 // The number of modules in the stream should be the same as reported by
254 // the FileInfoSubstreamHeader.
255 if (FI->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
259 // First is an array of `NumModules` module indices. This is not used for the
260 // same reason that `NumSourceFiles` is not used. It's an array of uint16's,
261 // but it's possible there are more than 64k source files, which would imply
262 // more than 64k modules (e.g. object files) as well. So we ignore this
263 // field.
264 llvm::ArrayRef<ulittle16_t> ModIndexArray(
265 reinterpret_cast<const ulittle16_t *>(Buf), ModuleInfos.size());
266
267 llvm::ArrayRef<ulittle16_t> ModFileCountArray(ModIndexArray.end(),
268 ModuleInfos.size());
269
270 // Compute the real number of source files.
271 uint32_t NumSourceFiles = 0;
272 for (auto Count : ModFileCountArray)
273 NumSourceFiles += Count;
274
275 // This is the array that in the reference implementation corresponds to
276 // `ModInfo::FileLayout::FileNameOffs`, which is commented there as being a
277 // pointer. Due to the mentioned problems of pointers causing difficulty
278 // when reading from the file on 64-bit systems, we continue to ignore that
279 // field in `ModInfo`, and instead build a vector of StringRefs and stores
280 // them in `ModuleInfoEx`. The value written to and read from the file is
281 // not used anyway, it is only there as a way to store the offsets for the
282 // purposes of later accessing the names at runtime.
283 llvm::ArrayRef<little32_t> FileNameOffsets(
284 reinterpret_cast<const little32_t *>(ModFileCountArray.end()),
285 NumSourceFiles);
286
287 const char *Names = reinterpret_cast<const char *>(FileNameOffsets.end());
288
289 // We go through each ModuleInfo, determine the number N of source files for
290 // that module, and then get the next N offsets from the Offsets array, using
291 // them to get the corresponding N names from the Names buffer and associating
292 // each one with the corresponding module.
293 uint32_t NextFileIndex = 0;
294 for (size_t I = 0; I < ModuleInfos.size(); ++I) {
295 uint32_t NumFiles = ModFileCountArray[I];
296 ModuleInfos[I].SourceFiles.resize(NumFiles);
297 for (size_t J = 0; J < NumFiles; ++J, ++NextFileIndex) {
298 uint32_t FileIndex = FileNameOffsets[NextFileIndex];
299 ModuleInfos[I].SourceFiles[J] = StringRef(Names + FileIndex);
300 }
301 }
302
Zachary Turner819e77d2016-05-06 20:51:57 +0000303 return Error::success();
Zachary Turner1822af542016-04-27 23:41:42 +0000304}
Zachary Turnerd3076ab2016-05-25 05:49:48 +0000305
306uint32_t DbiStream::getDebugStreamIndex(DbgHeaderType Type) const {
307 ArrayRef<uint8_t> DbgData;
308 if (auto EC = DbgHeader.getArrayRef(0, DbgData, DbgHeader.getLength())) {
309 consumeError(std::move(EC));
310 return uint32_t(-1);
311 }
312 ArrayRef<ulittle16_t> DebugStreams(
313 reinterpret_cast<const ulittle16_t *>(DbgData.data()),
314 DbgData.size() / sizeof(ulittle16_t));
315 return DebugStreams[static_cast<uint16_t>(Type)];
316}