blob: 35937574645de5c4b032404043307899f7adc18b [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 Turner8dbe3622016-05-27 01:54:44 +0000139 if (auto EC =
140 Reader.readStreamRef(ModInfoSubstream, Header->ModiSubstreamSize))
Zachary Turner819e77d2016-05-06 20:51:57 +0000141 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 Turner8dbe3622016-05-27 01:54:44 +0000145 codeview::VarStreamArray ModInfoArray(ModInfoSubstream, ModInfoRecordLength);
146 for (auto Info : ModInfoArray) {
147 ModuleInfos.emplace_back(Info);
148 }
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000149
Zachary Turner8dbe3622016-05-27 01:54:44 +0000150 if (auto EC = Reader.readStreamRef(SecContrSubstream,
151 Header->SecContrSubstreamSize))
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000152 return EC;
Zachary Turner8dbe3622016-05-27 01:54:44 +0000153 if (auto EC = Reader.readStreamRef(SecMapSubstream, Header->SectionMapSize))
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000154 return EC;
Zachary Turner8dbe3622016-05-27 01:54:44 +0000155 if (auto EC = Reader.readStreamRef(FileInfoSubstream, Header->FileInfoSize))
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000156 return EC;
Zachary Turner819e77d2016-05-06 20:51:57 +0000157 if (auto EC =
Zachary Turner8dbe3622016-05-27 01:54:44 +0000158 Reader.readStreamRef(TypeServerMapSubstream, Header->TypeServerSize))
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000159 return EC;
Zachary Turner8dbe3622016-05-27 01:54:44 +0000160 if (auto EC = Reader.readStreamRef(ECSubstream, Header->ECSubstreamSize))
Zachary Turner6ba65de2016-04-29 17:22:58 +0000161 return EC;
Zachary Turner8dbe3622016-05-27 01:54:44 +0000162 if (auto EC = Reader.readArray(DbgStreams, Header->OptionalDbgHdrSize /
163 sizeof(ulittle16_t)))
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 Turnerd5d37dc2016-05-25 20:37:03 +0000173 codeview::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 Turner8dbe3622016-05-27 01:54:44 +0000250 const FileInfoSubstreamHeader *FH;
251 codeview::StreamReader FISR(FileInfoSubstream);
252 if (auto EC = FISR.readObject(FH))
253 return EC;
254
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000255 // The number of modules in the stream should be the same as reported by
256 // the FileInfoSubstreamHeader.
Zachary Turner8dbe3622016-05-27 01:54:44 +0000257 if (FH->NumModules != ModuleInfos.size())
Zachary Turner819e77d2016-05-06 20:51:57 +0000258 return make_error<RawError>(raw_error_code::corrupt_file,
259 "FileInfo substream count doesn't match DBI.");
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000260
Zachary Turner8dbe3622016-05-27 01:54:44 +0000261 codeview::FixedStreamArray<ulittle16_t> ModIndexArray;
262 codeview::FixedStreamArray<ulittle16_t> ModFileCountArray;
263 codeview::FixedStreamArray<little32_t> FileNameOffsets;
264
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000265 // First is an array of `NumModules` module indices. This is not used for the
266 // same reason that `NumSourceFiles` is not used. It's an array of uint16's,
267 // but it's possible there are more than 64k source files, which would imply
268 // more than 64k modules (e.g. object files) as well. So we ignore this
269 // field.
Zachary Turner8dbe3622016-05-27 01:54:44 +0000270 if (auto EC = FISR.readArray(ModIndexArray, ModuleInfos.size()))
271 return EC;
272 if (auto EC = FISR.readArray(ModFileCountArray, ModuleInfos.size()))
273 return EC;
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000274
275 // Compute the real number of source files.
276 uint32_t NumSourceFiles = 0;
277 for (auto Count : ModFileCountArray)
278 NumSourceFiles += Count;
279
280 // This is the array that in the reference implementation corresponds to
281 // `ModInfo::FileLayout::FileNameOffs`, which is commented there as being a
282 // pointer. Due to the mentioned problems of pointers causing difficulty
283 // when reading from the file on 64-bit systems, we continue to ignore that
284 // field in `ModInfo`, and instead build a vector of StringRefs and stores
285 // them in `ModuleInfoEx`. The value written to and read from the file is
286 // not used anyway, it is only there as a way to store the offsets for the
287 // purposes of later accessing the names at runtime.
Zachary Turner8dbe3622016-05-27 01:54:44 +0000288 if (auto EC = FISR.readArray(FileNameOffsets, NumSourceFiles))
289 return EC;
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000290
Zachary Turner8dbe3622016-05-27 01:54:44 +0000291 codeview::StreamRef NamesBufferRef;
292 if (auto EC = FISR.readStreamRef(NamesBufferRef))
293 return EC;
294 codeview::StreamReader Names(NamesBufferRef);
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000295
296 // We go through each ModuleInfo, determine the number N of source files for
297 // that module, and then get the next N offsets from the Offsets array, using
298 // them to get the corresponding N names from the Names buffer and associating
299 // each one with the corresponding module.
300 uint32_t NextFileIndex = 0;
301 for (size_t I = 0; I < ModuleInfos.size(); ++I) {
302 uint32_t NumFiles = ModFileCountArray[I];
303 ModuleInfos[I].SourceFiles.resize(NumFiles);
304 for (size_t J = 0; J < NumFiles; ++J, ++NextFileIndex) {
Zachary Turner8dbe3622016-05-27 01:54:44 +0000305 uint32_t FileOffset = FileNameOffsets[NextFileIndex];
306 Names.setOffset(FileOffset);
307 if (auto EC = Names.readZeroString(ModuleInfos[I].SourceFiles[J]))
308 return EC;
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000309 }
310 }
311
Zachary Turner819e77d2016-05-06 20:51:57 +0000312 return Error::success();
Zachary Turner1822af542016-04-27 23:41:42 +0000313}
Zachary Turnerd3076ab2016-05-25 05:49:48 +0000314
315uint32_t DbiStream::getDebugStreamIndex(DbgHeaderType Type) const {
Zachary Turner8dbe3622016-05-27 01:54:44 +0000316 return DbgStreams[static_cast<uint16_t>(Type)];
Zachary Turnerd3076ab2016-05-25 05:49:48 +0000317}