Zachary Turner | 2f09b50 | 2016-04-29 17:28:47 +0000 | [diff] [blame] | 1 | //===- DbiStream.cpp - PDB Dbi Stream (Stream 3) Access -------------------===// |
Zachary Turner | 53a65ba | 2016-04-26 18:42:34 +0000 | [diff] [blame] | 2 | // |
| 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 Turner | 2f09b50 | 2016-04-29 17:28:47 +0000 | [diff] [blame] | 10 | #include "llvm/DebugInfo/PDB/Raw/DbiStream.h" |
| 11 | #include "llvm/DebugInfo/PDB/Raw/InfoStream.h" |
Zachary Turner | 1822af54 | 2016-04-27 23:41:42 +0000 | [diff] [blame] | 12 | #include "llvm/DebugInfo/PDB/Raw/ModInfo.h" |
Zachary Turner | 0eace0b | 2016-05-02 18:09:14 +0000 | [diff] [blame^] | 13 | #include "llvm/DebugInfo/PDB/Raw/NameHashTable.h" |
Zachary Turner | 53a65ba | 2016-04-26 18:42:34 +0000 | [diff] [blame] | 14 | #include "llvm/DebugInfo/PDB/Raw/PDBFile.h" |
Zachary Turner | 2f09b50 | 2016-04-29 17:28:47 +0000 | [diff] [blame] | 15 | #include "llvm/DebugInfo/PDB/Raw/RawConstants.h" |
Zachary Turner | 6ba65de | 2016-04-29 17:22:58 +0000 | [diff] [blame] | 16 | #include "llvm/DebugInfo/PDB/Raw/StreamReader.h" |
Zachary Turner | 53a65ba | 2016-04-26 18:42:34 +0000 | [diff] [blame] | 17 | |
| 18 | using namespace llvm; |
Zachary Turner | 2f09b50 | 2016-04-29 17:28:47 +0000 | [diff] [blame] | 19 | using namespace llvm::pdb; |
Zachary Turner | 53a65ba | 2016-04-26 18:42:34 +0000 | [diff] [blame] | 20 | using namespace llvm::support; |
| 21 | |
| 22 | namespace { |
| 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 | //}; |
| 34 | const uint16_t FlagIncrementalMask = 0x0001; |
| 35 | const uint16_t FlagStrippedMask = 0x0002; |
| 36 | const uint16_t FlagHasCTypesMask = 0x0004; |
| 37 | |
| 38 | // struct DbiBuildNo { |
| 39 | // uint16_t MinorVersion : 8; |
| 40 | // uint16_t MajorVersion : 7; |
| 41 | // uint16_t NewVersionFormat : 1; |
| 42 | //}; |
| 43 | const uint16_t BuildMinorMask = 0x00FF; |
| 44 | const uint16_t BuildMinorShift = 0; |
| 45 | |
| 46 | const uint16_t BuildMajorMask = 0x7F00; |
| 47 | const uint16_t BuildMajorShift = 8; |
Zachary Turner | 53a65ba | 2016-04-26 18:42:34 +0000 | [diff] [blame] | 48 | } |
| 49 | |
Zachary Turner | 2f09b50 | 2016-04-29 17:28:47 +0000 | [diff] [blame] | 50 | struct DbiStream::HeaderInfo { |
Zachary Turner | ff788aa | 2016-04-26 19:24:10 +0000 | [diff] [blame] | 51 | little32_t VersionSignature; |
Zachary Turner | 53a65ba | 2016-04-26 18:42:34 +0000 | [diff] [blame] | 52 | ulittle32_t VersionHeader; |
Zachary Turner | 2f09b50 | 2016-04-29 17:28:47 +0000 | [diff] [blame] | 53 | ulittle32_t Age; // Should match InfoStream. |
Zachary Turner | 84c3a8b | 2016-04-28 20:05:18 +0000 | [diff] [blame] | 54 | ulittle16_t GSSyms; // Number of global symbols |
| 55 | ulittle16_t BuildNumber; // See DbiBuildNo structure. |
| 56 | ulittle16_t PSSyms; // Number of public symbols |
Zachary Turner | 53a65ba | 2016-04-26 18:42:34 +0000 | [diff] [blame] | 57 | 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 Turner | 84c3a8b | 2016-04-28 20:05:18 +0000 | [diff] [blame] | 62 | little32_t SectionMapSize; // Size of sec. map substream |
| 63 | little32_t FileInfoSize; // Size of file info substream |
Zachary Turner | 2f09b50 | 2016-04-29 17:28:47 +0000 | [diff] [blame] | 64 | 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 Turner | 53a65ba | 2016-04-26 18:42:34 +0000 | [diff] [blame] | 70 | |
| 71 | ulittle32_t Reserved; // Pad to 64 bytes |
| 72 | }; |
| 73 | |
Zachary Turner | 2f09b50 | 2016-04-29 17:28:47 +0000 | [diff] [blame] | 74 | DbiStream::DbiStream(PDBFile &File) : Pdb(File), Stream(3, File) { |
Zachary Turner | 53a65ba | 2016-04-26 18:42:34 +0000 | [diff] [blame] | 75 | static_assert(sizeof(HeaderInfo) == 64, "Invalid HeaderInfo size!"); |
| 76 | } |
| 77 | |
Zachary Turner | 2f09b50 | 2016-04-29 17:28:47 +0000 | [diff] [blame] | 78 | DbiStream::~DbiStream() {} |
Zachary Turner | 53a65ba | 2016-04-26 18:42:34 +0000 | [diff] [blame] | 79 | |
Zachary Turner | 2f09b50 | 2016-04-29 17:28:47 +0000 | [diff] [blame] | 80 | std::error_code DbiStream::reload() { |
Zachary Turner | 6ba65de | 2016-04-29 17:22:58 +0000 | [diff] [blame] | 81 | StreamReader Reader(Stream); |
| 82 | |
Zachary Turner | 53a65ba | 2016-04-26 18:42:34 +0000 | [diff] [blame] | 83 | Header.reset(new HeaderInfo()); |
| 84 | |
| 85 | if (Stream.getLength() < sizeof(HeaderInfo)) |
| 86 | return std::make_error_code(std::errc::illegal_byte_sequence); |
Zachary Turner | 6ba65de | 2016-04-29 17:22:58 +0000 | [diff] [blame] | 87 | Reader.readObject(Header.get()); |
Zachary Turner | 53a65ba | 2016-04-26 18:42:34 +0000 | [diff] [blame] | 88 | |
| 89 | if (Header->VersionSignature != -1) |
| 90 | return std::make_error_code(std::errc::illegal_byte_sequence); |
| 91 | |
Zachary Turner | 1822af54 | 2016-04-27 23:41:42 +0000 | [diff] [blame] | 92 | // 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 Turner | 53a65ba | 2016-04-26 18:42:34 +0000 | [diff] [blame] | 96 | 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 Turner | 84c3a8b | 2016-04-28 20:05:18 +0000 | [diff] [blame] | 108 | // Only certain substreams are guaranteed to be aligned. Validate |
| 109 | // them here. |
Zachary Turner | 1822af54 | 2016-04-27 23:41:42 +0000 | [diff] [blame] | 110 | if (Header->ModiSubstreamSize % sizeof(uint32_t) != 0) |
| 111 | return std::make_error_code(std::errc::illegal_byte_sequence); |
Zachary Turner | 84c3a8b | 2016-04-28 20:05:18 +0000 | [diff] [blame] | 112 | 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 Turner | 1822af54 | 2016-04-27 23:41:42 +0000 | [diff] [blame] | 120 | |
Zachary Turner | 84c3a8b | 2016-04-28 20:05:18 +0000 | [diff] [blame] | 121 | std::error_code EC; |
Zachary Turner | 6ba65de | 2016-04-29 17:22:58 +0000 | [diff] [blame] | 122 | ModInfoSubstream.initialize(Reader, Header->ModiSubstreamSize); |
Zachary Turner | 84c3a8b | 2016-04-28 20:05:18 +0000 | [diff] [blame] | 123 | |
| 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 Turner | 6ba65de | 2016-04-29 17:22:58 +0000 | [diff] [blame] | 126 | auto Range = |
| 127 | llvm::make_range(ModInfoIterator(&ModInfoSubstream.data().front()), |
| 128 | ModInfoIterator(&ModInfoSubstream.data().back() + 1)); |
Zachary Turner | 84c3a8b | 2016-04-28 20:05:18 +0000 | [diff] [blame] | 129 | for (auto Info : Range) |
| 130 | ModuleInfos.push_back(ModuleInfoEx(Info)); |
| 131 | |
Zachary Turner | 2f09b50 | 2016-04-29 17:28:47 +0000 | [diff] [blame] | 132 | if ((EC = |
| 133 | SecContrSubstream.initialize(Reader, Header->SecContrSubstreamSize))) |
Zachary Turner | 84c3a8b | 2016-04-28 20:05:18 +0000 | [diff] [blame] | 134 | return EC; |
Zachary Turner | 6ba65de | 2016-04-29 17:22:58 +0000 | [diff] [blame] | 135 | if ((EC = SecMapSubstream.initialize(Reader, Header->SectionMapSize))) |
Zachary Turner | 84c3a8b | 2016-04-28 20:05:18 +0000 | [diff] [blame] | 136 | return EC; |
Zachary Turner | 6ba65de | 2016-04-29 17:22:58 +0000 | [diff] [blame] | 137 | if ((EC = FileInfoSubstream.initialize(Reader, Header->FileInfoSize))) |
Zachary Turner | 84c3a8b | 2016-04-28 20:05:18 +0000 | [diff] [blame] | 138 | return EC; |
Zachary Turner | 6ba65de | 2016-04-29 17:22:58 +0000 | [diff] [blame] | 139 | if ((EC = TypeServerMapSubstream.initialize(Reader, Header->TypeServerSize))) |
Zachary Turner | 84c3a8b | 2016-04-28 20:05:18 +0000 | [diff] [blame] | 140 | return EC; |
Zachary Turner | 6ba65de | 2016-04-29 17:22:58 +0000 | [diff] [blame] | 141 | if ((EC = ECSubstream.initialize(Reader, Header->ECSubstreamSize))) |
| 142 | return EC; |
| 143 | if ((EC = DbgHeader.initialize(Reader, Header->OptionalDbgHdrSize))) |
Zachary Turner | 84c3a8b | 2016-04-28 20:05:18 +0000 | [diff] [blame] | 144 | return EC; |
| 145 | |
Zachary Turner | 897067e | 2016-04-28 20:26:30 +0000 | [diff] [blame] | 146 | if ((EC = initializeFileInfo())) |
Zachary Turner | 1822af54 | 2016-04-27 23:41:42 +0000 | [diff] [blame] | 147 | return EC; |
| 148 | |
Zachary Turner | 6ba65de | 2016-04-29 17:22:58 +0000 | [diff] [blame] | 149 | if (Reader.bytesRemaining() > 0) |
| 150 | return std::make_error_code(std::errc::illegal_byte_sequence); |
| 151 | |
Zachary Turner | 0eace0b | 2016-05-02 18:09:14 +0000 | [diff] [blame^] | 152 | StreamReader ECReader(ECSubstream); |
| 153 | ECNames.load(ECReader); |
| 154 | |
Zachary Turner | 53a65ba | 2016-04-26 18:42:34 +0000 | [diff] [blame] | 155 | return std::error_code(); |
| 156 | } |
| 157 | |
Zachary Turner | 2f09b50 | 2016-04-29 17:28:47 +0000 | [diff] [blame] | 158 | PdbRaw_DbiVer DbiStream::getDbiVersion() const { |
Zachary Turner | 53a65ba | 2016-04-26 18:42:34 +0000 | [diff] [blame] | 159 | uint32_t Value = Header->VersionHeader; |
| 160 | return static_cast<PdbRaw_DbiVer>(Value); |
| 161 | } |
| 162 | |
Zachary Turner | 2f09b50 | 2016-04-29 17:28:47 +0000 | [diff] [blame] | 163 | uint32_t DbiStream::getAge() const { return Header->Age; } |
Zachary Turner | 53a65ba | 2016-04-26 18:42:34 +0000 | [diff] [blame] | 164 | |
Zachary Turner | 2f09b50 | 2016-04-29 17:28:47 +0000 | [diff] [blame] | 165 | bool DbiStream::isIncrementallyLinked() const { |
Zachary Turner | 53a65ba | 2016-04-26 18:42:34 +0000 | [diff] [blame] | 166 | return (Header->Flags & FlagIncrementalMask) != 0; |
| 167 | } |
| 168 | |
Zachary Turner | 2f09b50 | 2016-04-29 17:28:47 +0000 | [diff] [blame] | 169 | bool DbiStream::hasCTypes() const { |
Zachary Turner | 53a65ba | 2016-04-26 18:42:34 +0000 | [diff] [blame] | 170 | return (Header->Flags & FlagHasCTypesMask) != 0; |
| 171 | } |
| 172 | |
Zachary Turner | 2f09b50 | 2016-04-29 17:28:47 +0000 | [diff] [blame] | 173 | bool DbiStream::isStripped() const { |
Zachary Turner | 53a65ba | 2016-04-26 18:42:34 +0000 | [diff] [blame] | 174 | return (Header->Flags & FlagStrippedMask) != 0; |
| 175 | } |
| 176 | |
Zachary Turner | 2f09b50 | 2016-04-29 17:28:47 +0000 | [diff] [blame] | 177 | uint16_t DbiStream::getBuildMajorVersion() const { |
Zachary Turner | 53a65ba | 2016-04-26 18:42:34 +0000 | [diff] [blame] | 178 | return (Header->BuildNumber & BuildMajorMask) >> BuildMajorShift; |
| 179 | } |
| 180 | |
Zachary Turner | 2f09b50 | 2016-04-29 17:28:47 +0000 | [diff] [blame] | 181 | uint16_t DbiStream::getBuildMinorVersion() const { |
Zachary Turner | 53a65ba | 2016-04-26 18:42:34 +0000 | [diff] [blame] | 182 | return (Header->BuildNumber & BuildMinorMask) >> BuildMinorShift; |
| 183 | } |
| 184 | |
Zachary Turner | 2f09b50 | 2016-04-29 17:28:47 +0000 | [diff] [blame] | 185 | uint32_t DbiStream::getPdbDllVersion() const { return Header->PdbDllVersion; } |
Zachary Turner | 53a65ba | 2016-04-26 18:42:34 +0000 | [diff] [blame] | 186 | |
Zachary Turner | 2f09b50 | 2016-04-29 17:28:47 +0000 | [diff] [blame] | 187 | uint32_t DbiStream::getNumberOfSymbols() const { return Header->SymRecords; } |
Zachary Turner | 53a65ba | 2016-04-26 18:42:34 +0000 | [diff] [blame] | 188 | |
Zachary Turner | 2f09b50 | 2016-04-29 17:28:47 +0000 | [diff] [blame] | 189 | PDB_Machine DbiStream::getMachineType() const { |
Zachary Turner | 53a65ba | 2016-04-26 18:42:34 +0000 | [diff] [blame] | 190 | uint16_t Machine = Header->MachineType; |
| 191 | return static_cast<PDB_Machine>(Machine); |
| 192 | } |
Zachary Turner | 1822af54 | 2016-04-27 23:41:42 +0000 | [diff] [blame] | 193 | |
Zachary Turner | 2f09b50 | 2016-04-29 17:28:47 +0000 | [diff] [blame] | 194 | ArrayRef<ModuleInfoEx> DbiStream::modules() const { return ModuleInfos; } |
Zachary Turner | 84c3a8b | 2016-04-28 20:05:18 +0000 | [diff] [blame] | 195 | |
Zachary Turner | 2f09b50 | 2016-04-29 17:28:47 +0000 | [diff] [blame] | 196 | std::error_code DbiStream::initializeFileInfo() { |
Zachary Turner | 84c3a8b | 2016-04-28 20:05:18 +0000 | [diff] [blame] | 197 | 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 Turner | 6ba65de | 2016-04-29 17:22:58 +0000 | [diff] [blame] | 218 | const uint8_t *Buf = &FileInfoSubstream.data().front(); |
Zachary Turner | 84c3a8b | 2016-04-28 20:05:18 +0000 | [diff] [blame] | 219 | 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 Turner | 1822af54 | 2016-04-27 23:41:42 +0000 | [diff] [blame] | 271 | } |