blob: 57f7644d034785e05028f838bd4b1867c14aab1a [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"
11#include "llvm/DebugInfo/PDB/Raw/InfoStream.h"
Zachary Turner1822af542016-04-27 23:41:42 +000012#include "llvm/DebugInfo/PDB/Raw/ModInfo.h"
Zachary Turner0eace0b2016-05-02 18:09:14 +000013#include "llvm/DebugInfo/PDB/Raw/NameHashTable.h"
Zachary Turner53a65ba2016-04-26 18:42:34 +000014#include "llvm/DebugInfo/PDB/Raw/PDBFile.h"
Zachary Turner2f09b502016-04-29 17:28:47 +000015#include "llvm/DebugInfo/PDB/Raw/RawConstants.h"
Zachary Turner6ba65de2016-04-29 17:22:58 +000016#include "llvm/DebugInfo/PDB/Raw/StreamReader.h"
Zachary Turner53a65ba2016-04-26 18:42:34 +000017
18using namespace llvm;
Zachary Turner2f09b502016-04-29 17:28:47 +000019using namespace llvm::pdb;
Zachary Turner53a65ba2016-04-26 18:42:34 +000020using namespace llvm::support;
21
22namespace {
23// Some of the values are stored in bitfields. Since this needs to be portable
24// across compilers and architectures (big / little endian in particular) we
25// can't use the actual structures below, but must instead do the shifting
26// and masking ourselves. The struct definitions are provided for reference.
27
28// struct DbiFlags {
29// uint16_t IncrementalLinking : 1; // True if linked incrementally
30// uint16_t IsStripped : 1; // True if private symbols were stripped.
31// uint16_t HasCTypes : 1; // True if linked with /debug:ctypes.
32// uint16_t Reserved : 13;
33//};
34const uint16_t FlagIncrementalMask = 0x0001;
35const uint16_t FlagStrippedMask = 0x0002;
36const uint16_t FlagHasCTypesMask = 0x0004;
37
38// struct DbiBuildNo {
39// uint16_t MinorVersion : 8;
40// uint16_t MajorVersion : 7;
41// uint16_t NewVersionFormat : 1;
42//};
43const uint16_t BuildMinorMask = 0x00FF;
44const uint16_t BuildMinorShift = 0;
45
46const uint16_t BuildMajorMask = 0x7F00;
47const uint16_t BuildMajorShift = 8;
Zachary Turner53a65ba2016-04-26 18:42:34 +000048}
49
Zachary Turner2f09b502016-04-29 17:28:47 +000050struct DbiStream::HeaderInfo {
Zachary Turnerff788aa2016-04-26 19:24:10 +000051 little32_t VersionSignature;
Zachary Turner53a65ba2016-04-26 18:42:34 +000052 ulittle32_t VersionHeader;
Zachary Turner2f09b502016-04-29 17:28:47 +000053 ulittle32_t Age; // Should match InfoStream.
Zachary Turner84c3a8b2016-04-28 20:05:18 +000054 ulittle16_t GSSyms; // Number of global symbols
55 ulittle16_t BuildNumber; // See DbiBuildNo structure.
56 ulittle16_t PSSyms; // Number of public symbols
Zachary Turner53a65ba2016-04-26 18:42:34 +000057 ulittle16_t PdbDllVersion; // version of mspdbNNN.dll
58 ulittle16_t SymRecords; // Number of symbols
59 ulittle16_t PdbDllRbld; // rbld number of mspdbNNN.dll
60 little32_t ModiSubstreamSize; // Size of module info stream
61 little32_t SecContrSubstreamSize; // Size of sec. contribution stream
Zachary Turner84c3a8b2016-04-28 20:05:18 +000062 little32_t SectionMapSize; // Size of sec. map substream
63 little32_t FileInfoSize; // Size of file info substream
Zachary Turner2f09b502016-04-29 17:28:47 +000064 little32_t TypeServerSize; // Size of type server map
65 ulittle32_t MFCTypeServerIndex; // Index of MFC Type Server
66 little32_t OptionalDbgHdrSize; // Size of DbgHeader info
67 little32_t ECSubstreamSize; // Size of EC stream (what is EC?)
68 ulittle16_t Flags; // See DbiFlags enum.
69 ulittle16_t MachineType; // See PDB_MachineType enum.
Zachary Turner53a65ba2016-04-26 18:42:34 +000070
71 ulittle32_t Reserved; // Pad to 64 bytes
72};
73
Zachary Turner2f09b502016-04-29 17:28:47 +000074DbiStream::DbiStream(PDBFile &File) : Pdb(File), Stream(3, File) {
Zachary Turner53a65ba2016-04-26 18:42:34 +000075 static_assert(sizeof(HeaderInfo) == 64, "Invalid HeaderInfo size!");
76}
77
Zachary Turner2f09b502016-04-29 17:28:47 +000078DbiStream::~DbiStream() {}
Zachary Turner53a65ba2016-04-26 18:42:34 +000079
Zachary Turner2f09b502016-04-29 17:28:47 +000080std::error_code DbiStream::reload() {
Zachary Turner6ba65de2016-04-29 17:22:58 +000081 StreamReader Reader(Stream);
82
Zachary Turner53a65ba2016-04-26 18:42:34 +000083 Header.reset(new HeaderInfo());
84
85 if (Stream.getLength() < sizeof(HeaderInfo))
86 return std::make_error_code(std::errc::illegal_byte_sequence);
Zachary Turner6ba65de2016-04-29 17:22:58 +000087 Reader.readObject(Header.get());
Zachary Turner53a65ba2016-04-26 18:42:34 +000088
89 if (Header->VersionSignature != -1)
90 return std::make_error_code(std::errc::illegal_byte_sequence);
91
Zachary Turner1822af542016-04-27 23:41:42 +000092 // Require at least version 7, which should be present in all PDBs
93 // produced in the last decade and allows us to avoid having to
94 // special case all kinds of complicated arcane formats.
95 if (Header->VersionHeader < PdbDbiV70)
Zachary Turner53a65ba2016-04-26 18:42:34 +000096 return std::make_error_code(std::errc::not_supported);
97
98 if (Header->Age != Pdb.getPDBInfoStream().getAge())
99 return std::make_error_code(std::errc::illegal_byte_sequence);
100
101 if (Stream.getLength() !=
102 sizeof(HeaderInfo) + Header->ModiSubstreamSize +
103 Header->SecContrSubstreamSize + Header->SectionMapSize +
104 Header->FileInfoSize + Header->TypeServerSize +
105 Header->OptionalDbgHdrSize + Header->ECSubstreamSize)
106 return std::make_error_code(std::errc::illegal_byte_sequence);
107
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000108 // Only certain substreams are guaranteed to be aligned. Validate
109 // them here.
Zachary Turner1822af542016-04-27 23:41:42 +0000110 if (Header->ModiSubstreamSize % sizeof(uint32_t) != 0)
111 return std::make_error_code(std::errc::illegal_byte_sequence);
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000112 if (Header->SecContrSubstreamSize % sizeof(uint32_t) != 0)
113 return std::make_error_code(std::errc::illegal_byte_sequence);
114 if (Header->SectionMapSize % sizeof(uint32_t) != 0)
115 return std::make_error_code(std::errc::illegal_byte_sequence);
116 if (Header->FileInfoSize % sizeof(uint32_t) != 0)
117 return std::make_error_code(std::errc::illegal_byte_sequence);
118 if (Header->TypeServerSize % sizeof(uint32_t) != 0)
119 return std::make_error_code(std::errc::illegal_byte_sequence);
Zachary Turner1822af542016-04-27 23:41:42 +0000120
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000121 std::error_code EC;
Zachary Turner6ba65de2016-04-29 17:22:58 +0000122 ModInfoSubstream.initialize(Reader, Header->ModiSubstreamSize);
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000123
124 // Since each ModInfo in the stream is a variable length, we have to iterate
125 // them to know how many there actually are.
Zachary Turner6ba65de2016-04-29 17:22:58 +0000126 auto Range =
127 llvm::make_range(ModInfoIterator(&ModInfoSubstream.data().front()),
128 ModInfoIterator(&ModInfoSubstream.data().back() + 1));
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000129 for (auto Info : Range)
130 ModuleInfos.push_back(ModuleInfoEx(Info));
131
Zachary Turner2f09b502016-04-29 17:28:47 +0000132 if ((EC =
133 SecContrSubstream.initialize(Reader, Header->SecContrSubstreamSize)))
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000134 return EC;
Zachary Turner6ba65de2016-04-29 17:22:58 +0000135 if ((EC = SecMapSubstream.initialize(Reader, Header->SectionMapSize)))
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000136 return EC;
Zachary Turner6ba65de2016-04-29 17:22:58 +0000137 if ((EC = FileInfoSubstream.initialize(Reader, Header->FileInfoSize)))
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000138 return EC;
Zachary Turner6ba65de2016-04-29 17:22:58 +0000139 if ((EC = TypeServerMapSubstream.initialize(Reader, Header->TypeServerSize)))
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000140 return EC;
Zachary Turner6ba65de2016-04-29 17:22:58 +0000141 if ((EC = ECSubstream.initialize(Reader, Header->ECSubstreamSize)))
142 return EC;
143 if ((EC = DbgHeader.initialize(Reader, Header->OptionalDbgHdrSize)))
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000144 return EC;
145
Zachary Turner897067e2016-04-28 20:26:30 +0000146 if ((EC = initializeFileInfo()))
Zachary Turner1822af542016-04-27 23:41:42 +0000147 return EC;
148
Zachary Turner6ba65de2016-04-29 17:22:58 +0000149 if (Reader.bytesRemaining() > 0)
150 return std::make_error_code(std::errc::illegal_byte_sequence);
151
Zachary Turner0eace0b2016-05-02 18:09:14 +0000152 StreamReader ECReader(ECSubstream);
153 ECNames.load(ECReader);
154
Zachary Turner53a65ba2016-04-26 18:42:34 +0000155 return std::error_code();
156}
157
Zachary Turner2f09b502016-04-29 17:28:47 +0000158PdbRaw_DbiVer DbiStream::getDbiVersion() const {
Zachary Turner53a65ba2016-04-26 18:42:34 +0000159 uint32_t Value = Header->VersionHeader;
160 return static_cast<PdbRaw_DbiVer>(Value);
161}
162
Zachary Turner2f09b502016-04-29 17:28:47 +0000163uint32_t DbiStream::getAge() const { return Header->Age; }
Zachary Turner53a65ba2016-04-26 18:42:34 +0000164
Zachary Turner2f09b502016-04-29 17:28:47 +0000165bool DbiStream::isIncrementallyLinked() const {
Zachary Turner53a65ba2016-04-26 18:42:34 +0000166 return (Header->Flags & FlagIncrementalMask) != 0;
167}
168
Zachary Turner2f09b502016-04-29 17:28:47 +0000169bool DbiStream::hasCTypes() const {
Zachary Turner53a65ba2016-04-26 18:42:34 +0000170 return (Header->Flags & FlagHasCTypesMask) != 0;
171}
172
Zachary Turner2f09b502016-04-29 17:28:47 +0000173bool DbiStream::isStripped() const {
Zachary Turner53a65ba2016-04-26 18:42:34 +0000174 return (Header->Flags & FlagStrippedMask) != 0;
175}
176
Zachary Turner2f09b502016-04-29 17:28:47 +0000177uint16_t DbiStream::getBuildMajorVersion() const {
Zachary Turner53a65ba2016-04-26 18:42:34 +0000178 return (Header->BuildNumber & BuildMajorMask) >> BuildMajorShift;
179}
180
Zachary Turner2f09b502016-04-29 17:28:47 +0000181uint16_t DbiStream::getBuildMinorVersion() const {
Zachary Turner53a65ba2016-04-26 18:42:34 +0000182 return (Header->BuildNumber & BuildMinorMask) >> BuildMinorShift;
183}
184
Zachary Turner2f09b502016-04-29 17:28:47 +0000185uint32_t DbiStream::getPdbDllVersion() const { return Header->PdbDllVersion; }
Zachary Turner53a65ba2016-04-26 18:42:34 +0000186
Zachary Turner2f09b502016-04-29 17:28:47 +0000187uint32_t DbiStream::getNumberOfSymbols() const { return Header->SymRecords; }
Zachary Turner53a65ba2016-04-26 18:42:34 +0000188
Zachary Turner2f09b502016-04-29 17:28:47 +0000189PDB_Machine DbiStream::getMachineType() const {
Zachary Turner53a65ba2016-04-26 18:42:34 +0000190 uint16_t Machine = Header->MachineType;
191 return static_cast<PDB_Machine>(Machine);
192}
Zachary Turner1822af542016-04-27 23:41:42 +0000193
Zachary Turner2f09b502016-04-29 17:28:47 +0000194ArrayRef<ModuleInfoEx> DbiStream::modules() const { return ModuleInfos; }
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000195
Zachary Turner2f09b502016-04-29 17:28:47 +0000196std::error_code DbiStream::initializeFileInfo() {
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000197 struct FileInfoSubstreamHeader {
198 ulittle16_t NumModules; // Total # of modules, should match number of
199 // records in the ModuleInfo substream.
200 ulittle16_t NumSourceFiles; // Total # of source files. This value is not
201 // accurate because PDB actually supports more
202 // than 64k source files, so we ignore it and
203 // compute the value from other stream fields.
204 };
205
206 // The layout of the FileInfoSubstream is like this:
207 // struct {
208 // ulittle16_t NumModules;
209 // ulittle16_t NumSourceFiles;
210 // ulittle16_t ModIndices[NumModules];
211 // ulittle16_t ModFileCounts[NumModules];
212 // ulittle32_t FileNameOffsets[NumSourceFiles];
213 // char Names[][NumSourceFiles];
214 // };
215 // with the caveat that `NumSourceFiles` cannot be trusted, so
216 // it is computed by summing `ModFileCounts`.
217 //
Zachary Turner6ba65de2016-04-29 17:22:58 +0000218 const uint8_t *Buf = &FileInfoSubstream.data().front();
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000219 auto FI = reinterpret_cast<const FileInfoSubstreamHeader *>(Buf);
220 Buf += sizeof(FileInfoSubstreamHeader);
221 // The number of modules in the stream should be the same as reported by
222 // the FileInfoSubstreamHeader.
223 if (FI->NumModules != ModuleInfos.size())
224 return std::make_error_code(std::errc::illegal_byte_sequence);
225
226 // First is an array of `NumModules` module indices. This is not used for the
227 // same reason that `NumSourceFiles` is not used. It's an array of uint16's,
228 // but it's possible there are more than 64k source files, which would imply
229 // more than 64k modules (e.g. object files) as well. So we ignore this
230 // field.
231 llvm::ArrayRef<ulittle16_t> ModIndexArray(
232 reinterpret_cast<const ulittle16_t *>(Buf), ModuleInfos.size());
233
234 llvm::ArrayRef<ulittle16_t> ModFileCountArray(ModIndexArray.end(),
235 ModuleInfos.size());
236
237 // Compute the real number of source files.
238 uint32_t NumSourceFiles = 0;
239 for (auto Count : ModFileCountArray)
240 NumSourceFiles += Count;
241
242 // This is the array that in the reference implementation corresponds to
243 // `ModInfo::FileLayout::FileNameOffs`, which is commented there as being a
244 // pointer. Due to the mentioned problems of pointers causing difficulty
245 // when reading from the file on 64-bit systems, we continue to ignore that
246 // field in `ModInfo`, and instead build a vector of StringRefs and stores
247 // them in `ModuleInfoEx`. The value written to and read from the file is
248 // not used anyway, it is only there as a way to store the offsets for the
249 // purposes of later accessing the names at runtime.
250 llvm::ArrayRef<little32_t> FileNameOffsets(
251 reinterpret_cast<const little32_t *>(ModFileCountArray.end()),
252 NumSourceFiles);
253
254 const char *Names = reinterpret_cast<const char *>(FileNameOffsets.end());
255
256 // We go through each ModuleInfo, determine the number N of source files for
257 // that module, and then get the next N offsets from the Offsets array, using
258 // them to get the corresponding N names from the Names buffer and associating
259 // each one with the corresponding module.
260 uint32_t NextFileIndex = 0;
261 for (size_t I = 0; I < ModuleInfos.size(); ++I) {
262 uint32_t NumFiles = ModFileCountArray[I];
263 ModuleInfos[I].SourceFiles.resize(NumFiles);
264 for (size_t J = 0; J < NumFiles; ++J, ++NextFileIndex) {
265 uint32_t FileIndex = FileNameOffsets[NextFileIndex];
266 ModuleInfos[I].SourceFiles[J] = StringRef(Names + FileIndex);
267 }
268 }
269
270 return std::error_code();
Zachary Turner1822af542016-04-27 23:41:42 +0000271}