LCOV - code coverage report
Current view: top level - lib/DebugInfo/PDB/Native - DbiStreamBuilder.cpp (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 196 199 98.5 %
Date: 2018-06-17 00:07:59 Functions: 34 35 97.1 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : //===- DbiStreamBuilder.cpp - PDB Dbi Stream Creation -----------*- C++ -*-===//
       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/DbiStreamBuilder.h"
      11             : 
      12             : #include "llvm/ADT/ArrayRef.h"
      13             : #include "llvm/BinaryFormat/COFF.h"
      14             : #include "llvm/DebugInfo/MSF/MSFBuilder.h"
      15             : #include "llvm/DebugInfo/MSF/MappedBlockStream.h"
      16             : #include "llvm/DebugInfo/PDB/Native/DbiModuleDescriptorBuilder.h"
      17             : #include "llvm/DebugInfo/PDB/Native/DbiStream.h"
      18             : #include "llvm/DebugInfo/PDB/Native/RawError.h"
      19             : #include "llvm/Object/COFF.h"
      20             : #include "llvm/Support/BinaryStreamWriter.h"
      21             : 
      22             : using namespace llvm;
      23             : using namespace llvm::codeview;
      24             : using namespace llvm::msf;
      25             : using namespace llvm::pdb;
      26             : 
      27          87 : DbiStreamBuilder::DbiStreamBuilder(msf::MSFBuilder &Msf)
      28          87 :     : Msf(Msf), Allocator(Msf.getAllocator()), Age(1), BuildNumber(0),
      29             :       PdbDllVersion(0), PdbDllRbld(0), Flags(0), MachineType(PDB_Machine::x86),
      30         348 :       Header(nullptr) {}
      31             : 
      32         174 : DbiStreamBuilder::~DbiStreamBuilder() {}
      33             : 
      34         174 : void DbiStreamBuilder::setVersionHeader(PdbRaw_DbiVer V) { VerHeader = V; }
      35             : 
      36          87 : void DbiStreamBuilder::setAge(uint32_t A) { Age = A; }
      37             : 
      38          18 : void DbiStreamBuilder::setBuildNumber(uint16_t B) { BuildNumber = B; }
      39             : 
      40          69 : void DbiStreamBuilder::setBuildNumber(uint8_t Major, uint8_t Minor) {
      41          69 :   BuildNumber = (uint16_t(Major) << DbiBuildNo::BuildMajorShift) &
      42             :                 DbiBuildNo::BuildMajorMask;
      43          69 :   BuildNumber |= (uint16_t(Minor) << DbiBuildNo::BuildMinorShift) &
      44             :                  DbiBuildNo::BuildMinorMask;
      45          69 :   BuildNumber |= DbiBuildNo::NewVersionFormatMask;
      46          69 : }
      47             : 
      48          18 : void DbiStreamBuilder::setPdbDllVersion(uint16_t V) { PdbDllVersion = V; }
      49             : 
      50          18 : void DbiStreamBuilder::setPdbDllRbld(uint16_t R) { PdbDllRbld = R; }
      51             : 
      52          18 : void DbiStreamBuilder::setFlags(uint16_t F) { Flags = F; }
      53             : 
      54          18 : void DbiStreamBuilder::setMachineType(PDB_Machine M) { MachineType = M; }
      55             : 
      56          69 : void DbiStreamBuilder::setMachineType(COFF::MachineTypes M) {
      57             :   // These enums are mirrors of each other, so we can just cast the value.
      58          69 :   MachineType = static_cast<pdb::PDB_Machine>(static_cast<unsigned>(M));
      59          69 : }
      60             : 
      61          69 : void DbiStreamBuilder::setSectionMap(ArrayRef<SecMapEntry> SecMap) {
      62          69 :   SectionMap = SecMap;
      63          69 : }
      64             : 
      65          69 : void DbiStreamBuilder::setGlobalsStreamIndex(uint32_t Index) {
      66          69 :   GlobalsStreamIndex = Index;
      67          69 : }
      68             : 
      69          69 : void DbiStreamBuilder::setSymbolRecordStreamIndex(uint32_t Index) {
      70          69 :   SymRecordStreamIndex = Index;
      71          69 : }
      72             : 
      73          69 : void DbiStreamBuilder::setPublicsStreamIndex(uint32_t Index) {
      74          69 :   PublicsStreamIndex = Index;
      75          69 : }
      76             : 
      77          69 : Error DbiStreamBuilder::addDbgStream(pdb::DbgHeaderType Type,
      78             :                                      ArrayRef<uint8_t> Data) {
      79          69 :   DbgStreams[(int)Type].emplace();
      80          69 :   DbgStreams[(int)Type]->Data = Data;
      81          69 :   return Error::success();
      82             : }
      83             : 
      84          69 : uint32_t DbiStreamBuilder::addECName(StringRef Name) {
      85          69 :   return ECNamesBuilder.insert(Name);
      86             : }
      87             : 
      88          87 : uint32_t DbiStreamBuilder::calculateSerializedLength() const {
      89             :   // For now we only support serializing the header.
      90         174 :   return sizeof(DbiStreamHeader) + calculateFileInfoSubstreamSize() +
      91         261 :          calculateModiSubstreamSize() + calculateSectionContribsStreamSize() +
      92         261 :          calculateSectionMapStreamSize() + calculateDbgStreamsSize() +
      93         174 :          ECNamesBuilder.calculateSerializedSize();
      94             : }
      95             : 
      96             : Expected<DbiModuleDescriptorBuilder &>
      97         175 : DbiStreamBuilder::addModuleInfo(StringRef ModuleName) {
      98         350 :   uint32_t Index = ModiList.size();
      99         175 :   ModiList.push_back(
     100         350 :       llvm::make_unique<DbiModuleDescriptorBuilder>(ModuleName, Index, Msf));
     101         175 :   return *ModiList.back();
     102             : }
     103             : 
     104          56 : Error DbiStreamBuilder::addModuleSourceFile(DbiModuleDescriptorBuilder &Module,
     105             :                                             StringRef File) {
     106          56 :   uint32_t Index = SourceFileNames.size();
     107          56 :   SourceFileNames.insert(std::make_pair(File, Index));
     108          56 :   Module.addSourceFile(File);
     109          56 :   return Error::success();
     110             : }
     111             : 
     112           0 : Expected<uint32_t> DbiStreamBuilder::getSourceFileNameIndex(StringRef File) {
     113           0 :   auto NameIter = SourceFileNames.find(File);
     114           0 :   if (NameIter == SourceFileNames.end())
     115             :     return make_error<RawError>(raw_error_code::no_entry,
     116             :                                 "The specified source file was not found");
     117             :   return NameIter->getValue();
     118             : }
     119             : 
     120         174 : uint32_t DbiStreamBuilder::calculateModiSubstreamSize() const {
     121             :   uint32_t Size = 0;
     122         174 :   for (const auto &M : ModiList)
     123         350 :     Size += M->calculateSerializedLength();
     124         174 :   return Size;
     125             : }
     126             : 
     127         174 : uint32_t DbiStreamBuilder::calculateSectionContribsStreamSize() const {
     128         174 :   if (SectionContribs.empty())
     129             :     return 0;
     130         138 :   return sizeof(enum PdbRaw_DbiSecContribVer) +
     131         138 :          sizeof(SectionContribs[0]) * SectionContribs.size();
     132             : }
     133             : 
     134         174 : uint32_t DbiStreamBuilder::calculateSectionMapStreamSize() const {
     135         174 :   if (SectionMap.empty())
     136             :     return 0;
     137         138 :   return sizeof(SecMapHeader) + sizeof(SecMapEntry) * SectionMap.size();
     138             : }
     139             : 
     140         261 : uint32_t DbiStreamBuilder::calculateNamesOffset() const {
     141             :   uint32_t Offset = 0;
     142             :   Offset += sizeof(ulittle16_t);                         // NumModules
     143             :   Offset += sizeof(ulittle16_t);                         // NumSourceFiles
     144         522 :   Offset += ModiList.size() * sizeof(ulittle16_t);       // ModIndices
     145         261 :   Offset += ModiList.size() * sizeof(ulittle16_t);       // ModFileCounts
     146             :   uint32_t NumFileInfos = 0;
     147         786 :   for (const auto &M : ModiList)
     148         525 :     NumFileInfos += M->source_files().size();
     149         261 :   Offset += NumFileInfos * sizeof(ulittle32_t); // FileNameOffsets
     150         261 :   return Offset;
     151             : }
     152             : 
     153         174 : uint32_t DbiStreamBuilder::calculateFileInfoSubstreamSize() const {
     154         174 :   uint32_t Size = calculateNamesOffset();
     155         174 :   Size += calculateNamesBufferSize();
     156         348 :   return alignTo(Size, sizeof(uint32_t));
     157             : }
     158             : 
     159         174 : uint32_t DbiStreamBuilder::calculateNamesBufferSize() const {
     160             :   uint32_t Size = 0;
     161         456 :   for (const auto &F : SourceFileNames) {
     162         108 :     Size += F.getKeyLength() + 1; // Names[I];
     163             :   }
     164         174 :   return Size;
     165             : }
     166             : 
     167          87 : uint32_t DbiStreamBuilder::calculateDbgStreamsSize() const {
     168          87 :   return DbgStreams.size() * sizeof(uint16_t);
     169             : }
     170             : 
     171          87 : Error DbiStreamBuilder::generateFileInfoSubstream() {
     172          87 :   uint32_t Size = calculateFileInfoSubstreamSize();
     173          87 :   auto Data = Allocator.Allocate<uint8_t>(Size);
     174          87 :   uint32_t NamesOffset = calculateNamesOffset();
     175             : 
     176             :   FileInfoBuffer = MutableBinaryByteStream(MutableArrayRef<uint8_t>(Data, Size),
     177             :                                            llvm::support::little);
     178             : 
     179             :   WritableBinaryStreamRef MetadataBuffer =
     180         174 :       WritableBinaryStreamRef(FileInfoBuffer).keep_front(NamesOffset);
     181         174 :   BinaryStreamWriter MetadataWriter(MetadataBuffer);
     182             : 
     183         261 :   uint16_t ModiCount = std::min<uint32_t>(UINT16_MAX, ModiList.size());
     184         174 :   uint16_t FileCount = std::min<uint32_t>(UINT16_MAX, SourceFileNames.size());
     185         174 :   if (auto EC = MetadataWriter.writeInteger(ModiCount)) // NumModules
     186             :     return EC;
     187         174 :   if (auto EC = MetadataWriter.writeInteger(FileCount)) // NumSourceFiles
     188             :     return EC;
     189         437 :   for (uint16_t I = 0; I < ModiCount; ++I) {
     190         350 :     if (auto EC = MetadataWriter.writeInteger(I)) // Mod Indices
     191             :       return EC;
     192             :   }
     193         262 :   for (const auto &MI : ModiList) {
     194         175 :     FileCount = static_cast<uint16_t>(MI->source_files().size());
     195         350 :     if (auto EC = MetadataWriter.writeInteger(FileCount)) // Mod File Counts
     196             :       return EC;
     197             :   }
     198             : 
     199             :   // Before writing the FileNameOffsets array, write the NamesBuffer array.
     200             :   // A side effect of this is that this will actually compute the various
     201             :   // file name offsets, so we can then go back and write the FileNameOffsets
     202             :   // array to the other substream.
     203         261 :   NamesBuffer = WritableBinaryStreamRef(FileInfoBuffer).drop_front(NamesOffset);
     204         174 :   BinaryStreamWriter NameBufferWriter(NamesBuffer);
     205         228 :   for (auto &Name : SourceFileNames) {
     206          54 :     Name.second = NameBufferWriter.getOffset();
     207         108 :     if (auto EC = NameBufferWriter.writeCString(Name.getKey()))
     208             :       return EC;
     209             :   }
     210             : 
     211         262 :   for (const auto &MI : ModiList) {
     212         287 :     for (StringRef Name : MI->source_files()) {
     213          56 :       auto Result = SourceFileNames.find(Name);
     214         112 :       if (Result == SourceFileNames.end())
     215             :         return make_error<RawError>(raw_error_code::no_entry,
     216             :                                     "The source file was not found.");
     217         112 :       if (auto EC = MetadataWriter.writeInteger(Result->second))
     218             :         return EC;
     219             :     }
     220             :   }
     221             : 
     222         174 :   if (auto EC = NameBufferWriter.padToAlignment(sizeof(uint32_t)))
     223             :     return EC;
     224             : 
     225          87 :   if (NameBufferWriter.bytesRemaining() > 0)
     226             :     return make_error<RawError>(raw_error_code::invalid_format,
     227             :                                 "The names buffer contained unexpected data.");
     228             : 
     229          87 :   if (MetadataWriter.bytesRemaining() > sizeof(uint32_t))
     230             :     return make_error<RawError>(
     231             :         raw_error_code::invalid_format,
     232             :         "The metadata buffer contained unexpected data.");
     233             : 
     234             :   return Error::success();
     235             : }
     236             : 
     237          87 : Error DbiStreamBuilder::finalize() {
     238          87 :   if (Header)
     239             :     return Error::success();
     240             : 
     241         262 :   for (auto &MI : ModiList)
     242         175 :     MI->finalize();
     243             : 
     244         174 :   if (auto EC = generateFileInfoSubstream())
     245             :     return EC;
     246             : 
     247          87 :   DbiStreamHeader *H = Allocator.Allocate<DbiStreamHeader>();
     248          87 :   ::memset(H, 0, sizeof(DbiStreamHeader));
     249          87 :   H->VersionHeader = *VerHeader;
     250             :   H->VersionSignature = -1;
     251          87 :   H->Age = Age;
     252          87 :   H->BuildNumber = BuildNumber;
     253          87 :   H->Flags = Flags;
     254          87 :   H->PdbDllRbld = PdbDllRbld;
     255          87 :   H->PdbDllVersion = PdbDllVersion;
     256          87 :   H->MachineType = static_cast<uint16_t>(MachineType);
     257             : 
     258          87 :   H->ECSubstreamSize = ECNamesBuilder.calculateSerializedSize();
     259             :   H->FileInfoSize = FileInfoBuffer.getLength();
     260          87 :   H->ModiSubstreamSize = calculateModiSubstreamSize();
     261             :   H->OptionalDbgHdrSize = DbgStreams.size() * sizeof(uint16_t);
     262          87 :   H->SecContrSubstreamSize = calculateSectionContribsStreamSize();
     263          87 :   H->SectionMapSize = calculateSectionMapStreamSize();
     264             :   H->TypeServerSize = 0;
     265          87 :   H->SymRecordStreamIndex = SymRecordStreamIndex;
     266          87 :   H->PublicSymbolStreamIndex = PublicsStreamIndex;
     267             :   H->MFCTypeServerIndex = 0; // Not sure what this is, but link.exe writes 0.
     268          87 :   H->GlobalSymbolStreamIndex = GlobalsStreamIndex;
     269             : 
     270          87 :   Header = H;
     271             :   return Error::success();
     272             : }
     273             : 
     274          87 : Error DbiStreamBuilder::finalizeMsfLayout() {
     275        2001 :   for (auto &S : DbgStreams) {
     276         957 :     if (!S.hasValue())
     277         888 :       continue;
     278          69 :     auto ExpectedIndex = Msf.addStream(S->Data.size());
     279          69 :     if (!ExpectedIndex)
     280             :       return ExpectedIndex.takeError();
     281          69 :     S->StreamNumber = *ExpectedIndex;
     282             :   }
     283             : 
     284         262 :   for (auto &MI : ModiList) {
     285         350 :     if (auto EC = MI->finalizeMsfLayout())
     286             :       return EC;
     287             :   }
     288             : 
     289          87 :   uint32_t Length = calculateSerializedLength();
     290         174 :   if (auto EC = Msf.setStreamSize(StreamDBI, Length))
     291             :     return EC;
     292             :   return Error::success();
     293             : }
     294             : 
     295         181 : static uint16_t toSecMapFlags(uint32_t Flags) {
     296             :   uint16_t Ret = 0;
     297         181 :   if (Flags & COFF::IMAGE_SCN_MEM_READ)
     298             :     Ret |= static_cast<uint16_t>(OMFSegDescFlags::Read);
     299         181 :   if (Flags & COFF::IMAGE_SCN_MEM_WRITE)
     300          12 :     Ret |= static_cast<uint16_t>(OMFSegDescFlags::Write);
     301         181 :   if (Flags & COFF::IMAGE_SCN_MEM_EXECUTE)
     302          67 :     Ret |= static_cast<uint16_t>(OMFSegDescFlags::Execute);
     303         181 :   if (Flags & COFF::IMAGE_SCN_MEM_EXECUTE)
     304          67 :     Ret |= static_cast<uint16_t>(OMFSegDescFlags::Execute);
     305         181 :   if (!(Flags & COFF::IMAGE_SCN_MEM_16BIT))
     306         181 :     Ret |= static_cast<uint16_t>(OMFSegDescFlags::AddressIs32Bit);
     307             : 
     308             :   // This seems always 1.
     309         181 :   Ret |= static_cast<uint16_t>(OMFSegDescFlags::IsSelector);
     310             : 
     311         181 :   return Ret;
     312             : }
     313             : 
     314             : // A utility function to create a Section Map for a given list of COFF sections.
     315             : //
     316             : // A Section Map seem to be a copy of a COFF section list in other format.
     317             : // I don't know why a PDB file contains both a COFF section header and
     318             : // a Section Map, but it seems it must be present in a PDB.
     319          69 : std::vector<SecMapEntry> DbiStreamBuilder::createSectionMap(
     320             :     ArrayRef<llvm::object::coff_section> SecHdrs) {
     321             :   std::vector<SecMapEntry> Ret;
     322          69 :   int Idx = 0;
     323             : 
     324         250 :   auto Add = [&]() -> SecMapEntry & {
     325         250 :     Ret.emplace_back();
     326             :     auto &Entry = Ret.back();
     327         250 :     memset(&Entry, 0, sizeof(Entry));
     328             : 
     329         250 :     Entry.Frame = Idx + 1;
     330             : 
     331             :     // We don't know the meaning of these fields yet.
     332             :     Entry.SecName = UINT16_MAX;
     333             :     Entry.ClassName = UINT16_MAX;
     334             : 
     335         250 :     return Entry;
     336          69 :   };
     337             : 
     338         431 :   for (auto &Hdr : SecHdrs) {
     339         181 :     auto &Entry = Add();
     340         181 :     Entry.Flags = toSecMapFlags(Hdr.Characteristics);
     341         181 :     Entry.SecByteLength = Hdr.VirtualSize;
     342         181 :     ++Idx;
     343             :   }
     344             : 
     345             :   // The last entry is for absolute symbols.
     346          69 :   auto &Entry = Add();
     347             :   Entry.Flags = static_cast<uint16_t>(OMFSegDescFlags::AddressIs32Bit) |
     348             :                 static_cast<uint16_t>(OMFSegDescFlags::IsAbsoluteAddress);
     349             :   Entry.SecByteLength = UINT32_MAX;
     350             : 
     351          69 :   return Ret;
     352             : }
     353             : 
     354          87 : Error DbiStreamBuilder::commit(const msf::MSFLayout &Layout,
     355             :                                WritableBinaryStreamRef MsfBuffer) {
     356         174 :   if (auto EC = finalize())
     357             :     return EC;
     358             : 
     359             :   auto DbiS = WritableMappedBlockStream::createIndexedStream(
     360         261 :       Layout, MsfBuffer, StreamDBI, Allocator);
     361             : 
     362          87 :   BinaryStreamWriter Writer(*DbiS);
     363         174 :   if (auto EC = Writer.writeObject(*Header))
     364             :     return EC;
     365             : 
     366         262 :   for (auto &M : ModiList) {
     367         525 :     if (auto EC = M->commit(Writer, Layout, MsfBuffer))
     368             :       return EC;
     369             :   }
     370             : 
     371          87 :   if (!SectionContribs.empty()) {
     372          69 :     if (auto EC = Writer.writeEnum(DbiSecContribVer60))
     373             :       return EC;
     374         138 :     if (auto EC = Writer.writeArray(makeArrayRef(SectionContribs)))
     375             :       return EC;
     376             :   }
     377             : 
     378          87 :   if (!SectionMap.empty()) {
     379          69 :     ulittle16_t Size = static_cast<ulittle16_t>(SectionMap.size());
     380          69 :     SecMapHeader SMHeader = {Size, Size};
     381          69 :     if (auto EC = Writer.writeObject(SMHeader))
     382             :       return EC;
     383         138 :     if (auto EC = Writer.writeArray(SectionMap))
     384             :       return EC;
     385             :   }
     386             : 
     387         261 :   if (auto EC = Writer.writeStreamRef(FileInfoBuffer))
     388             :     return EC;
     389             : 
     390         174 :   if (auto EC = ECNamesBuilder.commit(Writer))
     391             :     return EC;
     392             : 
     393        2001 :   for (auto &Stream : DbgStreams) {
     394             :     uint16_t StreamNumber = kInvalidStreamIndex;
     395         957 :     if (Stream.hasValue())
     396          69 :       StreamNumber = Stream->StreamNumber;
     397        1914 :     if (auto EC = Writer.writeInteger(StreamNumber))
     398             :       return EC;
     399             :   }
     400             : 
     401        2001 :   for (auto &Stream : DbgStreams) {
     402         957 :     if (!Stream.hasValue())
     403         888 :       continue;
     404             :     assert(Stream->StreamNumber != kInvalidStreamIndex);
     405             : 
     406             :     auto WritableStream = WritableMappedBlockStream::createIndexedStream(
     407         207 :         Layout, MsfBuffer, Stream->StreamNumber, Allocator);
     408          69 :     BinaryStreamWriter DbgStreamWriter(*WritableStream);
     409         138 :     if (auto EC = DbgStreamWriter.writeArray(Stream->Data))
     410             :       return EC;
     411             :   }
     412             : 
     413          87 :   if (Writer.bytesRemaining() > 0)
     414             :     return make_error<RawError>(raw_error_code::invalid_format,
     415             :                                 "Unexpected bytes found in DBI Stream");
     416             :   return Error::success();
     417             : }

Generated by: LCOV version 1.13