blob: 7762fa34cebcf6f2f47d18ec9c490f65b8e5a3b5 [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 Turner53a65ba2016-04-26 18:42:34 +000013#include "llvm/DebugInfo/PDB/Raw/PDBFile.h"
Zachary Turner2f09b502016-04-29 17:28:47 +000014#include "llvm/DebugInfo/PDB/Raw/RawConstants.h"
Zachary Turner6ba65de2016-04-29 17:22:58 +000015#include "llvm/DebugInfo/PDB/Raw/StreamReader.h"
Zachary Turner53a65ba2016-04-26 18:42:34 +000016
17using namespace llvm;
Zachary Turner2f09b502016-04-29 17:28:47 +000018using namespace llvm::pdb;
Zachary Turner53a65ba2016-04-26 18:42:34 +000019using namespace llvm::support;
20
21namespace {
22// Some of the values are stored in bitfields. Since this needs to be portable
23// across compilers and architectures (big / little endian in particular) we
24// can't use the actual structures below, but must instead do the shifting
25// and masking ourselves. The struct definitions are provided for reference.
26
27// struct DbiFlags {
28// uint16_t IncrementalLinking : 1; // True if linked incrementally
29// uint16_t IsStripped : 1; // True if private symbols were stripped.
30// uint16_t HasCTypes : 1; // True if linked with /debug:ctypes.
31// uint16_t Reserved : 13;
32//};
33const uint16_t FlagIncrementalMask = 0x0001;
34const uint16_t FlagStrippedMask = 0x0002;
35const uint16_t FlagHasCTypesMask = 0x0004;
36
37// struct DbiBuildNo {
38// uint16_t MinorVersion : 8;
39// uint16_t MajorVersion : 7;
40// uint16_t NewVersionFormat : 1;
41//};
42const uint16_t BuildMinorMask = 0x00FF;
43const uint16_t BuildMinorShift = 0;
44
45const uint16_t BuildMajorMask = 0x7F00;
46const uint16_t BuildMajorShift = 8;
Zachary Turner53a65ba2016-04-26 18:42:34 +000047}
48
Zachary Turner2f09b502016-04-29 17:28:47 +000049struct DbiStream::HeaderInfo {
Zachary Turnerff788aa2016-04-26 19:24:10 +000050 little32_t VersionSignature;
Zachary Turner53a65ba2016-04-26 18:42:34 +000051 ulittle32_t VersionHeader;
Zachary Turner2f09b502016-04-29 17:28:47 +000052 ulittle32_t Age; // Should match InfoStream.
Zachary Turner84c3a8b2016-04-28 20:05:18 +000053 ulittle16_t GSSyms; // Number of global symbols
54 ulittle16_t BuildNumber; // See DbiBuildNo structure.
55 ulittle16_t PSSyms; // Number of public symbols
Zachary Turner53a65ba2016-04-26 18:42:34 +000056 ulittle16_t PdbDllVersion; // version of mspdbNNN.dll
57 ulittle16_t SymRecords; // Number of symbols
58 ulittle16_t PdbDllRbld; // rbld number of mspdbNNN.dll
59 little32_t ModiSubstreamSize; // Size of module info stream
60 little32_t SecContrSubstreamSize; // Size of sec. contribution stream
Zachary Turner84c3a8b2016-04-28 20:05:18 +000061 little32_t SectionMapSize; // Size of sec. map substream
62 little32_t FileInfoSize; // Size of file info substream
Zachary Turner2f09b502016-04-29 17:28:47 +000063 little32_t TypeServerSize; // Size of type server map
64 ulittle32_t MFCTypeServerIndex; // Index of MFC Type Server
65 little32_t OptionalDbgHdrSize; // Size of DbgHeader info
66 little32_t ECSubstreamSize; // Size of EC stream (what is EC?)
67 ulittle16_t Flags; // See DbiFlags enum.
68 ulittle16_t MachineType; // See PDB_MachineType enum.
Zachary Turner53a65ba2016-04-26 18:42:34 +000069
70 ulittle32_t Reserved; // Pad to 64 bytes
71};
72
Zachary Turner2f09b502016-04-29 17:28:47 +000073DbiStream::DbiStream(PDBFile &File) : Pdb(File), Stream(3, File) {
Zachary Turner53a65ba2016-04-26 18:42:34 +000074 static_assert(sizeof(HeaderInfo) == 64, "Invalid HeaderInfo size!");
75}
76
Zachary Turner2f09b502016-04-29 17:28:47 +000077DbiStream::~DbiStream() {}
Zachary Turner53a65ba2016-04-26 18:42:34 +000078
Zachary Turner2f09b502016-04-29 17:28:47 +000079std::error_code DbiStream::reload() {
Zachary Turner6ba65de2016-04-29 17:22:58 +000080 StreamReader Reader(Stream);
81
Zachary Turner53a65ba2016-04-26 18:42:34 +000082 Header.reset(new HeaderInfo());
83
84 if (Stream.getLength() < sizeof(HeaderInfo))
85 return std::make_error_code(std::errc::illegal_byte_sequence);
Zachary Turner6ba65de2016-04-29 17:22:58 +000086 Reader.readObject(Header.get());
Zachary Turner53a65ba2016-04-26 18:42:34 +000087
88 if (Header->VersionSignature != -1)
89 return std::make_error_code(std::errc::illegal_byte_sequence);
90
Zachary Turner1822af542016-04-27 23:41:42 +000091 // Require at least version 7, which should be present in all PDBs
92 // produced in the last decade and allows us to avoid having to
93 // special case all kinds of complicated arcane formats.
94 if (Header->VersionHeader < PdbDbiV70)
Zachary Turner53a65ba2016-04-26 18:42:34 +000095 return std::make_error_code(std::errc::not_supported);
96
97 if (Header->Age != Pdb.getPDBInfoStream().getAge())
98 return std::make_error_code(std::errc::illegal_byte_sequence);
99
100 if (Stream.getLength() !=
101 sizeof(HeaderInfo) + Header->ModiSubstreamSize +
102 Header->SecContrSubstreamSize + Header->SectionMapSize +
103 Header->FileInfoSize + Header->TypeServerSize +
104 Header->OptionalDbgHdrSize + Header->ECSubstreamSize)
105 return std::make_error_code(std::errc::illegal_byte_sequence);
106
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000107 // Only certain substreams are guaranteed to be aligned. Validate
108 // them here.
Zachary Turner1822af542016-04-27 23:41:42 +0000109 if (Header->ModiSubstreamSize % sizeof(uint32_t) != 0)
110 return std::make_error_code(std::errc::illegal_byte_sequence);
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000111 if (Header->SecContrSubstreamSize % sizeof(uint32_t) != 0)
112 return std::make_error_code(std::errc::illegal_byte_sequence);
113 if (Header->SectionMapSize % sizeof(uint32_t) != 0)
114 return std::make_error_code(std::errc::illegal_byte_sequence);
115 if (Header->FileInfoSize % sizeof(uint32_t) != 0)
116 return std::make_error_code(std::errc::illegal_byte_sequence);
117 if (Header->TypeServerSize % sizeof(uint32_t) != 0)
118 return std::make_error_code(std::errc::illegal_byte_sequence);
Zachary Turner1822af542016-04-27 23:41:42 +0000119
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000120 std::error_code EC;
Zachary Turner6ba65de2016-04-29 17:22:58 +0000121 ModInfoSubstream.initialize(Reader, Header->ModiSubstreamSize);
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000122
123 // Since each ModInfo in the stream is a variable length, we have to iterate
124 // them to know how many there actually are.
Zachary Turner6ba65de2016-04-29 17:22:58 +0000125 auto Range =
126 llvm::make_range(ModInfoIterator(&ModInfoSubstream.data().front()),
127 ModInfoIterator(&ModInfoSubstream.data().back() + 1));
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000128 for (auto Info : Range)
129 ModuleInfos.push_back(ModuleInfoEx(Info));
130
Zachary Turner2f09b502016-04-29 17:28:47 +0000131 if ((EC =
132 SecContrSubstream.initialize(Reader, Header->SecContrSubstreamSize)))
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000133 return EC;
Zachary Turner6ba65de2016-04-29 17:22:58 +0000134 if ((EC = SecMapSubstream.initialize(Reader, Header->SectionMapSize)))
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000135 return EC;
Zachary Turner6ba65de2016-04-29 17:22:58 +0000136 if ((EC = FileInfoSubstream.initialize(Reader, Header->FileInfoSize)))
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000137 return EC;
Zachary Turner6ba65de2016-04-29 17:22:58 +0000138 if ((EC = TypeServerMapSubstream.initialize(Reader, Header->TypeServerSize)))
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000139 return EC;
Zachary Turner6ba65de2016-04-29 17:22:58 +0000140 if ((EC = ECSubstream.initialize(Reader, Header->ECSubstreamSize)))
141 return EC;
142 if ((EC = DbgHeader.initialize(Reader, Header->OptionalDbgHdrSize)))
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000143 return EC;
144
Zachary Turner897067e2016-04-28 20:26:30 +0000145 if ((EC = initializeFileInfo()))
Zachary Turner1822af542016-04-27 23:41:42 +0000146 return EC;
147
Zachary Turner6ba65de2016-04-29 17:22:58 +0000148 if (Reader.bytesRemaining() > 0)
149 return std::make_error_code(std::errc::illegal_byte_sequence);
150
Zachary Turner53a65ba2016-04-26 18:42:34 +0000151 return std::error_code();
152}
153
Zachary Turner2f09b502016-04-29 17:28:47 +0000154PdbRaw_DbiVer DbiStream::getDbiVersion() const {
Zachary Turner53a65ba2016-04-26 18:42:34 +0000155 uint32_t Value = Header->VersionHeader;
156 return static_cast<PdbRaw_DbiVer>(Value);
157}
158
Zachary Turner2f09b502016-04-29 17:28:47 +0000159uint32_t DbiStream::getAge() const { return Header->Age; }
Zachary Turner53a65ba2016-04-26 18:42:34 +0000160
Zachary Turner2f09b502016-04-29 17:28:47 +0000161bool DbiStream::isIncrementallyLinked() const {
Zachary Turner53a65ba2016-04-26 18:42:34 +0000162 return (Header->Flags & FlagIncrementalMask) != 0;
163}
164
Zachary Turner2f09b502016-04-29 17:28:47 +0000165bool DbiStream::hasCTypes() const {
Zachary Turner53a65ba2016-04-26 18:42:34 +0000166 return (Header->Flags & FlagHasCTypesMask) != 0;
167}
168
Zachary Turner2f09b502016-04-29 17:28:47 +0000169bool DbiStream::isStripped() const {
Zachary Turner53a65ba2016-04-26 18:42:34 +0000170 return (Header->Flags & FlagStrippedMask) != 0;
171}
172
Zachary Turner2f09b502016-04-29 17:28:47 +0000173uint16_t DbiStream::getBuildMajorVersion() const {
Zachary Turner53a65ba2016-04-26 18:42:34 +0000174 return (Header->BuildNumber & BuildMajorMask) >> BuildMajorShift;
175}
176
Zachary Turner2f09b502016-04-29 17:28:47 +0000177uint16_t DbiStream::getBuildMinorVersion() const {
Zachary Turner53a65ba2016-04-26 18:42:34 +0000178 return (Header->BuildNumber & BuildMinorMask) >> BuildMinorShift;
179}
180
Zachary Turner2f09b502016-04-29 17:28:47 +0000181uint32_t DbiStream::getPdbDllVersion() const { return Header->PdbDllVersion; }
Zachary Turner53a65ba2016-04-26 18:42:34 +0000182
Zachary Turner2f09b502016-04-29 17:28:47 +0000183uint32_t DbiStream::getNumberOfSymbols() const { return Header->SymRecords; }
Zachary Turner53a65ba2016-04-26 18:42:34 +0000184
Zachary Turner2f09b502016-04-29 17:28:47 +0000185PDB_Machine DbiStream::getMachineType() const {
Zachary Turner53a65ba2016-04-26 18:42:34 +0000186 uint16_t Machine = Header->MachineType;
187 return static_cast<PDB_Machine>(Machine);
188}
Zachary Turner1822af542016-04-27 23:41:42 +0000189
Zachary Turner2f09b502016-04-29 17:28:47 +0000190ArrayRef<ModuleInfoEx> DbiStream::modules() const { return ModuleInfos; }
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000191
Zachary Turner2f09b502016-04-29 17:28:47 +0000192std::error_code DbiStream::initializeFileInfo() {
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000193 struct FileInfoSubstreamHeader {
194 ulittle16_t NumModules; // Total # of modules, should match number of
195 // records in the ModuleInfo substream.
196 ulittle16_t NumSourceFiles; // Total # of source files. This value is not
197 // accurate because PDB actually supports more
198 // than 64k source files, so we ignore it and
199 // compute the value from other stream fields.
200 };
201
202 // The layout of the FileInfoSubstream is like this:
203 // struct {
204 // ulittle16_t NumModules;
205 // ulittle16_t NumSourceFiles;
206 // ulittle16_t ModIndices[NumModules];
207 // ulittle16_t ModFileCounts[NumModules];
208 // ulittle32_t FileNameOffsets[NumSourceFiles];
209 // char Names[][NumSourceFiles];
210 // };
211 // with the caveat that `NumSourceFiles` cannot be trusted, so
212 // it is computed by summing `ModFileCounts`.
213 //
Zachary Turner6ba65de2016-04-29 17:22:58 +0000214 const uint8_t *Buf = &FileInfoSubstream.data().front();
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000215 auto FI = reinterpret_cast<const FileInfoSubstreamHeader *>(Buf);
216 Buf += sizeof(FileInfoSubstreamHeader);
217 // The number of modules in the stream should be the same as reported by
218 // the FileInfoSubstreamHeader.
219 if (FI->NumModules != ModuleInfos.size())
220 return std::make_error_code(std::errc::illegal_byte_sequence);
221
222 // First is an array of `NumModules` module indices. This is not used for the
223 // same reason that `NumSourceFiles` is not used. It's an array of uint16's,
224 // but it's possible there are more than 64k source files, which would imply
225 // more than 64k modules (e.g. object files) as well. So we ignore this
226 // field.
227 llvm::ArrayRef<ulittle16_t> ModIndexArray(
228 reinterpret_cast<const ulittle16_t *>(Buf), ModuleInfos.size());
229
230 llvm::ArrayRef<ulittle16_t> ModFileCountArray(ModIndexArray.end(),
231 ModuleInfos.size());
232
233 // Compute the real number of source files.
234 uint32_t NumSourceFiles = 0;
235 for (auto Count : ModFileCountArray)
236 NumSourceFiles += Count;
237
238 // This is the array that in the reference implementation corresponds to
239 // `ModInfo::FileLayout::FileNameOffs`, which is commented there as being a
240 // pointer. Due to the mentioned problems of pointers causing difficulty
241 // when reading from the file on 64-bit systems, we continue to ignore that
242 // field in `ModInfo`, and instead build a vector of StringRefs and stores
243 // them in `ModuleInfoEx`. The value written to and read from the file is
244 // not used anyway, it is only there as a way to store the offsets for the
245 // purposes of later accessing the names at runtime.
246 llvm::ArrayRef<little32_t> FileNameOffsets(
247 reinterpret_cast<const little32_t *>(ModFileCountArray.end()),
248 NumSourceFiles);
249
250 const char *Names = reinterpret_cast<const char *>(FileNameOffsets.end());
251
252 // We go through each ModuleInfo, determine the number N of source files for
253 // that module, and then get the next N offsets from the Offsets array, using
254 // them to get the corresponding N names from the Names buffer and associating
255 // each one with the corresponding module.
256 uint32_t NextFileIndex = 0;
257 for (size_t I = 0; I < ModuleInfos.size(); ++I) {
258 uint32_t NumFiles = ModFileCountArray[I];
259 ModuleInfos[I].SourceFiles.resize(NumFiles);
260 for (size_t J = 0; J < NumFiles; ++J, ++NextFileIndex) {
261 uint32_t FileIndex = FileNameOffsets[NextFileIndex];
262 ModuleInfos[I].SourceFiles[J] = StringRef(Names + FileIndex);
263 }
264 }
265
266 return std::error_code();
Zachary Turner1822af542016-04-27 23:41:42 +0000267}