LCOV - code coverage report
Current view: top level - lib/DebugInfo/PDB/Native - DbiStream.cpp (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 128 139 92.1 %
Date: 2018-02-23 15:42:53 Functions: 33 36 91.7 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.13