1 //===- DbiStream.cpp - PDB Dbi Stream (Stream 3) Access -------------------===// 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 10 #include "llvm/DebugInfo/PDB/Native/DbiStream.h" 11 #include "llvm/ADT/StringRef.h" 12 #include "llvm/DebugInfo/MSF/MappedBlockStream.h" 13 #include "llvm/DebugInfo/PDB/Native/DbiModuleDescriptor.h" 14 #include "llvm/DebugInfo/PDB/Native/ISectionContribVisitor.h" 15 #include "llvm/DebugInfo/PDB/Native/InfoStream.h" 16 #include "llvm/DebugInfo/PDB/Native/PDBFile.h" 17 #include "llvm/DebugInfo/PDB/Native/RawConstants.h" 18 #include "llvm/DebugInfo/PDB/Native/RawError.h" 19 #include "llvm/DebugInfo/PDB/Native/RawTypes.h" 20 #include "llvm/DebugInfo/PDB/PDBTypes.h" 21 #include "llvm/Object/COFF.h" 22 #include "llvm/Support/BinaryStreamArray.h" 23 #include "llvm/Support/BinaryStreamReader.h" 24 #include "llvm/Support/Error.h" 25 #include <algorithm> 26 #include <cstddef> 27 #include <cstdint> 28 29 using namespace llvm; 30 using namespace llvm::codeview; 31 using namespace llvm::msf; 32 using namespace llvm::pdb; 33 using namespace llvm::support; 34 35 template <typename ContribType> 36 static Error loadSectionContribs(FixedStreamArray<ContribType> &Output, 37 BinaryStreamReader &Reader) { 38 if (Reader.bytesRemaining() % sizeof(ContribType) != 0) 39 return make_error<RawError>( 40 raw_error_code::corrupt_file, 41 "Invalid number of bytes of section contributions"); 42 43 uint32_t Count = Reader.bytesRemaining() / sizeof(ContribType); 44 if (auto EC = Reader.readArray(Output, Count)) 45 return EC; 46 return Error::success(); 47 } 48 49 DbiStream::DbiStream(PDBFile &File, std::unique_ptr<MappedBlockStream> Stream) 50 : Pdb(File), Stream(std::move(Stream)), Header(nullptr) {} 51 52 DbiStream::~DbiStream() = default; 53 54 Error DbiStream::reload() { 55 BinaryStreamReader Reader(*Stream); 56 57 if (Stream->getLength() < sizeof(DbiStreamHeader)) 58 return make_error<RawError>(raw_error_code::corrupt_file, 59 "DBI Stream does not contain a header."); 60 if (auto EC = Reader.readObject(Header)) 61 return make_error<RawError>(raw_error_code::corrupt_file, 62 "DBI Stream does not contain a header."); 63 64 if (Header->VersionSignature != -1) 65 return make_error<RawError>(raw_error_code::corrupt_file, 66 "Invalid DBI version signature."); 67 68 // Require at least version 7, which should be present in all PDBs 69 // produced in the last decade and allows us to avoid having to 70 // special case all kinds of complicated arcane formats. 71 if (Header->VersionHeader < PdbDbiV70) 72 return make_error<RawError>(raw_error_code::feature_unsupported, 73 "Unsupported DBI version."); 74 75 if (Stream->getLength() != 76 sizeof(DbiStreamHeader) + Header->ModiSubstreamSize + 77 Header->SecContrSubstreamSize + Header->SectionMapSize + 78 Header->FileInfoSize + Header->TypeServerSize + 79 Header->OptionalDbgHdrSize + Header->ECSubstreamSize) 80 return make_error<RawError>(raw_error_code::corrupt_file, 81 "DBI Length does not equal sum of substreams."); 82 83 // Only certain substreams are guaranteed to be aligned. Validate 84 // them here. 85 if (Header->ModiSubstreamSize % sizeof(uint32_t) != 0) 86 return make_error<RawError>(raw_error_code::corrupt_file, 87 "DBI MODI substream not aligned."); 88 if (Header->SecContrSubstreamSize % sizeof(uint32_t) != 0) 89 return make_error<RawError>( 90 raw_error_code::corrupt_file, 91 "DBI section contribution substream not aligned."); 92 if (Header->SectionMapSize % sizeof(uint32_t) != 0) 93 return make_error<RawError>(raw_error_code::corrupt_file, 94 "DBI section map substream not aligned."); 95 if (Header->FileInfoSize % sizeof(uint32_t) != 0) 96 return make_error<RawError>(raw_error_code::corrupt_file, 97 "DBI file info substream not aligned."); 98 if (Header->TypeServerSize % sizeof(uint32_t) != 0) 99 return make_error<RawError>(raw_error_code::corrupt_file, 100 "DBI type server substream not aligned."); 101 102 BinaryStreamRef ModInfoSubstream; 103 BinaryStreamRef FileInfoSubstream; 104 if (auto EC = 105 Reader.readStreamRef(ModInfoSubstream, Header->ModiSubstreamSize)) 106 return EC; 107 108 if (auto EC = Reader.readStreamRef(SecContrSubstream, 109 Header->SecContrSubstreamSize)) 110 return EC; 111 if (auto EC = Reader.readStreamRef(SecMapSubstream, Header->SectionMapSize)) 112 return EC; 113 if (auto EC = Reader.readStreamRef(FileInfoSubstream, Header->FileInfoSize)) 114 return EC; 115 if (auto EC = 116 Reader.readStreamRef(TypeServerMapSubstream, Header->TypeServerSize)) 117 return EC; 118 if (auto EC = Reader.readStreamRef(ECSubstream, Header->ECSubstreamSize)) 119 return EC; 120 if (auto EC = Reader.readArray( 121 DbgStreams, Header->OptionalDbgHdrSize / sizeof(ulittle16_t))) 122 return EC; 123 124 if (auto EC = Modules.initialize(ModInfoSubstream, FileInfoSubstream)) 125 return EC; 126 127 if (auto EC = initializeSectionContributionData()) 128 return EC; 129 if (auto EC = initializeSectionHeadersData()) 130 return EC; 131 if (auto EC = initializeSectionMapData()) 132 return EC; 133 if (auto EC = initializeFpoRecords()) 134 return EC; 135 136 if (Reader.bytesRemaining() > 0) 137 return make_error<RawError>(raw_error_code::corrupt_file, 138 "Found unexpected bytes in DBI Stream."); 139 140 if (ECSubstream.getLength() > 0) { 141 BinaryStreamReader ECReader(ECSubstream); 142 if (auto EC = ECNames.reload(ECReader)) 143 return EC; 144 } 145 146 return Error::success(); 147 } 148 149 PdbRaw_DbiVer DbiStream::getDbiVersion() const { 150 uint32_t Value = Header->VersionHeader; 151 return static_cast<PdbRaw_DbiVer>(Value); 152 } 153 154 uint32_t DbiStream::getAge() const { return Header->Age; } 155 156 uint16_t DbiStream::getPublicSymbolStreamIndex() const { 157 return Header->PublicSymbolStreamIndex; 158 } 159 160 uint16_t DbiStream::getGlobalSymbolStreamIndex() const { 161 return Header->GlobalSymbolStreamIndex; 162 } 163 164 uint16_t DbiStream::getFlags() const { return Header->Flags; } 165 166 bool DbiStream::isIncrementallyLinked() const { 167 return (Header->Flags & DbiFlags::FlagIncrementalMask) != 0; 168 } 169 170 bool DbiStream::hasCTypes() const { 171 return (Header->Flags & DbiFlags::FlagHasCTypesMask) != 0; 172 } 173 174 bool DbiStream::isStripped() const { 175 return (Header->Flags & DbiFlags::FlagStrippedMask) != 0; 176 } 177 178 uint16_t DbiStream::getBuildNumber() const { return Header->BuildNumber; } 179 180 uint16_t DbiStream::getBuildMajorVersion() const { 181 return (Header->BuildNumber & DbiBuildNo::BuildMajorMask) >> 182 DbiBuildNo::BuildMajorShift; 183 } 184 185 uint16_t DbiStream::getBuildMinorVersion() const { 186 return (Header->BuildNumber & DbiBuildNo::BuildMinorMask) >> 187 DbiBuildNo::BuildMinorShift; 188 } 189 190 uint16_t DbiStream::getPdbDllRbld() const { return Header->PdbDllRbld; } 191 192 uint32_t DbiStream::getPdbDllVersion() const { return Header->PdbDllVersion; } 193 194 uint32_t DbiStream::getSymRecordStreamIndex() const { 195 return Header->SymRecordStreamIndex; 196 } 197 198 PDB_Machine DbiStream::getMachineType() const { 199 uint16_t Machine = Header->MachineType; 200 return static_cast<PDB_Machine>(Machine); 201 } 202 203 FixedStreamArray<object::coff_section> DbiStream::getSectionHeaders() { 204 return SectionHeaders; 205 } 206 207 FixedStreamArray<object::FpoData> DbiStream::getFpoRecords() { 208 return FpoRecords; 209 } 210 211 const DbiModuleList &DbiStream::modules() const { return Modules; } 212 213 FixedStreamArray<SecMapEntry> DbiStream::getSectionMap() const { 214 return SectionMap; 215 } 216 217 void DbiStream::visitSectionContributions( 218 ISectionContribVisitor &Visitor) const { 219 if (!SectionContribs.empty()) { 220 assert(SectionContribVersion == DbiSecContribVer60); 221 for (auto &SC : SectionContribs) 222 Visitor.visit(SC); 223 } else if (!SectionContribs2.empty()) { 224 assert(SectionContribVersion == DbiSecContribV2); 225 for (auto &SC : SectionContribs2) 226 Visitor.visit(SC); 227 } 228 } 229 230 Error DbiStream::initializeSectionContributionData() { 231 if (SecContrSubstream.getLength() == 0) 232 return Error::success(); 233 234 BinaryStreamReader SCReader(SecContrSubstream); 235 if (auto EC = SCReader.readEnum(SectionContribVersion)) 236 return EC; 237 238 if (SectionContribVersion == DbiSecContribVer60) 239 return loadSectionContribs<SectionContrib>(SectionContribs, SCReader); 240 if (SectionContribVersion == DbiSecContribV2) 241 return loadSectionContribs<SectionContrib2>(SectionContribs2, SCReader); 242 243 return make_error<RawError>(raw_error_code::feature_unsupported, 244 "Unsupported DBI Section Contribution version"); 245 } 246 247 // Initializes this->SectionHeaders. 248 Error DbiStream::initializeSectionHeadersData() { 249 if (DbgStreams.size() == 0) 250 return Error::success(); 251 252 uint32_t StreamNum = getDebugStreamIndex(DbgHeaderType::SectionHdr); 253 if (StreamNum >= Pdb.getNumStreams()) 254 return make_error<RawError>(raw_error_code::no_stream); 255 256 auto SHS = MappedBlockStream::createIndexedStream( 257 Pdb.getMsfLayout(), Pdb.getMsfBuffer(), StreamNum, Pdb.getAllocator()); 258 259 size_t StreamLen = SHS->getLength(); 260 if (StreamLen % sizeof(object::coff_section)) 261 return make_error<RawError>(raw_error_code::corrupt_file, 262 "Corrupted section header stream."); 263 264 size_t NumSections = StreamLen / sizeof(object::coff_section); 265 BinaryStreamReader Reader(*SHS); 266 if (auto EC = Reader.readArray(SectionHeaders, NumSections)) 267 return make_error<RawError>(raw_error_code::corrupt_file, 268 "Could not read a bitmap."); 269 270 SectionHeaderStream = std::move(SHS); 271 return Error::success(); 272 } 273 274 // Initializes this->Fpos. 275 Error DbiStream::initializeFpoRecords() { 276 if (DbgStreams.size() == 0) 277 return Error::success(); 278 279 uint32_t StreamNum = getDebugStreamIndex(DbgHeaderType::NewFPO); 280 281 // This means there is no FPO data. 282 if (StreamNum == kInvalidStreamIndex) 283 return Error::success(); 284 285 if (StreamNum >= Pdb.getNumStreams()) 286 return make_error<RawError>(raw_error_code::no_stream); 287 288 auto FS = MappedBlockStream::createIndexedStream( 289 Pdb.getMsfLayout(), Pdb.getMsfBuffer(), StreamNum, Pdb.getAllocator()); 290 291 size_t StreamLen = FS->getLength(); 292 if (StreamLen % sizeof(object::FpoData)) 293 return make_error<RawError>(raw_error_code::corrupt_file, 294 "Corrupted New FPO stream."); 295 296 size_t NumRecords = StreamLen / sizeof(object::FpoData); 297 BinaryStreamReader Reader(*FS); 298 if (auto EC = Reader.readArray(FpoRecords, NumRecords)) 299 return make_error<RawError>(raw_error_code::corrupt_file, 300 "Corrupted New FPO stream."); 301 FpoStream = std::move(FS); 302 return Error::success(); 303 } 304 305 Error DbiStream::initializeSectionMapData() { 306 if (SecMapSubstream.getLength() == 0) 307 return Error::success(); 308 309 BinaryStreamReader SMReader(SecMapSubstream); 310 const SecMapHeader *Header; 311 if (auto EC = SMReader.readObject(Header)) 312 return EC; 313 if (auto EC = SMReader.readArray(SectionMap, Header->SecCount)) 314 return EC; 315 return Error::success(); 316 } 317 318 uint32_t DbiStream::getDebugStreamIndex(DbgHeaderType Type) const { 319 uint16_t T = static_cast<uint16_t>(Type); 320 if (T >= DbgStreams.size()) 321 return kInvalidStreamIndex; 322 return DbgStreams[T]; 323 } 324