LCOV - code coverage report
Current view: top level - lib/DebugInfo/PDB/Native - DbiModuleDescriptorBuilder.cpp (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 82 94 87.2 %
Date: 2017-09-14 15:23:50 Functions: 13 14 92.9 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : //===- DbiModuleDescriptorBuilder.cpp - PDB Mod Info 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/DbiModuleDescriptorBuilder.h"
      11             : 
      12             : #include "llvm/ADT/ArrayRef.h"
      13             : #include "llvm/BinaryFormat/COFF.h"
      14             : #include "llvm/DebugInfo/CodeView/DebugSubsectionRecord.h"
      15             : #include "llvm/DebugInfo/MSF/MSFBuilder.h"
      16             : #include "llvm/DebugInfo/MSF/MSFCommon.h"
      17             : #include "llvm/DebugInfo/MSF/MappedBlockStream.h"
      18             : #include "llvm/DebugInfo/PDB/Native/DbiModuleDescriptor.h"
      19             : #include "llvm/DebugInfo/PDB/Native/GSIStreamBuilder.h"
      20             : #include "llvm/DebugInfo/PDB/Native/RawConstants.h"
      21             : #include "llvm/DebugInfo/PDB/Native/RawError.h"
      22             : #include "llvm/Support/BinaryItemStream.h"
      23             : #include "llvm/Support/BinaryStreamWriter.h"
      24             : 
      25             : using namespace llvm;
      26             : using namespace llvm::codeview;
      27             : using namespace llvm::msf;
      28             : using namespace llvm::pdb;
      29             : 
      30             : static uint32_t calculateDiSymbolStreamSize(uint32_t SymbolByteSize,
      31             :                                             uint32_t C13Size) {
      32         108 :   uint32_t Size = sizeof(uint32_t);   // Signature
      33         216 :   Size += alignTo(SymbolByteSize, 4); // Symbol Data
      34         108 :   Size += 0;                          // TODO: Layout.C11Bytes
      35         108 :   Size += C13Size;                    // C13 Debug Info Size
      36         108 :   Size += sizeof(uint32_t);           // GlobalRefs substream size (always 0)
      37         108 :   Size += 0;                          // GlobalRefs substream bytes
      38             :   return Size;
      39             : }
      40             : 
      41         108 : DbiModuleDescriptorBuilder::DbiModuleDescriptorBuilder(StringRef ModuleName,
      42             :                                                        uint32_t ModIndex,
      43         108 :                                                        msf::MSFBuilder &Msf)
      44         648 :     : MSF(Msf), ModuleName(ModuleName) {
      45         108 :   ::memset(&Layout, 0, sizeof(Layout));
      46         216 :   Layout.Mod = ModIndex;
      47         108 : }
      48             : 
      49         432 : DbiModuleDescriptorBuilder::~DbiModuleDescriptorBuilder() {}
      50             : 
      51           0 : uint16_t DbiModuleDescriptorBuilder::getStreamIndex() const {
      52           0 :   return Layout.ModDiStream;
      53             : }
      54             : 
      55          68 : void DbiModuleDescriptorBuilder::setObjFileName(StringRef Name) {
      56         136 :   ObjFileName = Name;
      57          68 : }
      58             : 
      59          40 : void DbiModuleDescriptorBuilder::setPdbFilePathNI(uint32_t NI) {
      60          40 :   PdbFilePathNI = NI;
      61          40 : }
      62             : 
      63         498 : void DbiModuleDescriptorBuilder::addSymbol(CVSymbol Symbol) {
      64         498 :   Symbols.push_back(Symbol);
      65             :   // Symbols written to a PDB file are required to be 4 byte aligned.  The same
      66             :   // is not true of object files.
      67             :   assert(Symbol.length() % alignOf(CodeViewContainer::Pdb) == 0 &&
      68             :          "Invalid Symbol alignment!");
      69         498 :   SymbolByteSize += Symbol.length();
      70         498 : }
      71             : 
      72          39 : void DbiModuleDescriptorBuilder::addSourceFile(StringRef Path) {
      73         156 :   SourceFiles.push_back(Path);
      74          39 : }
      75             : 
      76         216 : uint32_t DbiModuleDescriptorBuilder::calculateC13DebugInfoSize() const {
      77         216 :   uint32_t Result = 0;
      78        1016 :   for (const auto &Builder : C13Builders) {
      79             :     assert(Builder && "Empty C13 Fragment Builder!");
      80         152 :     Result += Builder->calculateSerializedLength();
      81             :   }
      82         216 :   return Result;
      83             : }
      84             : 
      85         216 : uint32_t DbiModuleDescriptorBuilder::calculateSerializedLength() const {
      86         216 :   uint32_t L = sizeof(Layout);
      87         216 :   uint32_t M = ModuleName.size() + 1;
      88         216 :   uint32_t O = ObjFileName.size() + 1;
      89         432 :   return alignTo(L + M + O, sizeof(uint32_t));
      90             : }
      91             : 
      92         108 : void DbiModuleDescriptorBuilder::finalize() {
      93         324 :   Layout.SC.ModuleIndex = Layout.Mod;
      94         216 :   Layout.FileNameOffs = 0; // TODO: Fix this
      95         216 :   Layout.Flags = 0;        // TODO: Fix this
      96         216 :   Layout.C11Bytes = 0;
      97         216 :   Layout.C13Bytes = calculateC13DebugInfoSize();
      98             :   (void)Layout.Mod;         // Set in constructor
      99             :   (void)Layout.ModDiStream; // Set in finalizeMsfLayout
     100         324 :   Layout.NumFiles = SourceFiles.size();
     101         216 :   Layout.PdbFilePathNI = PdbFilePathNI;
     102         216 :   Layout.SrcFileNameNI = 0;
     103             : 
     104             :   // This value includes both the signature field as well as the record bytes
     105             :   // from the symbol stream.
     106         216 :   Layout.SymBytes = SymbolByteSize + sizeof(uint32_t);
     107         108 : }
     108             : 
     109         108 : Error DbiModuleDescriptorBuilder::finalizeMsfLayout() {
     110         216 :   this->Layout.ModDiStream = kInvalidStreamIndex;
     111         108 :   uint32_t C13Size = calculateC13DebugInfoSize();
     112             :   auto ExpectedSN =
     113         324 :       MSF.addStream(calculateDiSymbolStreamSize(SymbolByteSize, C13Size));
     114         108 :   if (!ExpectedSN)
     115             :     return ExpectedSN.takeError();
     116         216 :   Layout.ModDiStream = *ExpectedSN;
     117         324 :   return Error::success();
     118             : }
     119             : 
     120         108 : Error DbiModuleDescriptorBuilder::commit(BinaryStreamWriter &ModiWriter,
     121             :                                          const msf::MSFLayout &MsfLayout,
     122             :                                          WritableBinaryStreamRef MsfBuffer) {
     123             :   // We write the Modi record to the `ModiWriter`, but we additionally write its
     124             :   // symbol stream to a brand new stream.
     125         432 :   if (auto EC = ModiWriter.writeObject(Layout))
     126           0 :     return EC;
     127         432 :   if (auto EC = ModiWriter.writeCString(ModuleName))
     128           0 :     return EC;
     129         432 :   if (auto EC = ModiWriter.writeCString(ObjFileName))
     130           0 :     return EC;
     131         324 :   if (auto EC = ModiWriter.padToAlignment(sizeof(uint32_t)))
     132           0 :     return EC;
     133             : 
     134         216 :   if (Layout.ModDiStream != kInvalidStreamIndex) {
     135             :     auto NS = WritableMappedBlockStream::createIndexedStream(
     136         540 :         MsfLayout, MsfBuffer, Layout.ModDiStream, MSF.getAllocator());
     137         216 :     WritableBinaryStreamRef Ref(*NS);
     138         324 :     BinaryStreamWriter SymbolWriter(Ref);
     139             :     // Write the symbols.
     140         108 :     if (auto EC =
     141         324 :             SymbolWriter.writeInteger<uint32_t>(COFF::DEBUG_SECTION_MAGIC))
     142           0 :       return EC;
     143         216 :     BinaryItemStream<CVSymbol> Records(llvm::support::endianness::little);
     144         324 :     Records.setItems(Symbols);
     145         216 :     BinaryStreamRef RecordsRef(Records);
     146         432 :     if (auto EC = SymbolWriter.writeStreamRef(RecordsRef))
     147           0 :       return EC;
     148         324 :     if (auto EC = SymbolWriter.padToAlignment(4))
     149           0 :       return EC;
     150             :     // TODO: Write C11 Line data
     151             :     assert(SymbolWriter.getOffset() % alignOf(CodeViewContainer::Pdb) == 0 &&
     152             :            "Invalid debug section alignment!");
     153         508 :     for (const auto &Builder : C13Builders) {
     154             :       assert(Builder && "Empty C13 Fragment Builder!");
     155         228 :       if (auto EC = Builder->commit(SymbolWriter))
     156           0 :         return EC;
     157             :     }
     158             : 
     159             :     // TODO: Figure out what GlobalRefs substream actually is and populate it.
     160         324 :     if (auto EC = SymbolWriter.writeInteger<uint32_t>(0))
     161           0 :       return EC;
     162         108 :     if (SymbolWriter.bytesRemaining() > 0)
     163           0 :       return make_error<RawError>(raw_error_code::stream_too_long);
     164             :   }
     165         324 :   return Error::success();
     166             : }
     167             : 
     168          39 : void DbiModuleDescriptorBuilder::addDebugSubsection(
     169             :     std::shared_ptr<DebugSubsection> Subsection) {
     170             :   assert(Subsection);
     171         117 :   C13Builders.push_back(llvm::make_unique<DebugSubsectionRecordBuilder>(
     172          39 :       std::move(Subsection), CodeViewContainer::Pdb));
     173          39 : }
     174             : 
     175          37 : void DbiModuleDescriptorBuilder::addDebugSubsection(
     176             :     const DebugSubsectionRecord &SubsectionContents) {
     177         111 :   C13Builders.push_back(llvm::make_unique<DebugSubsectionRecordBuilder>(
     178             :       SubsectionContents, CodeViewContainer::Pdb));
     179          37 : }

Generated by: LCOV version 1.13