Home | History | Annotate | Line # | Download | only in Native
      1 //===- DbiStreamBuilder.h - PDB Dbi Stream Creation -------------*- C++ -*-===//
      2 //
      3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
      4 // See https://llvm.org/LICENSE.txt for license information.
      5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
      6 //
      7 //===----------------------------------------------------------------------===//
      8 
      9 #ifndef LLVM_DEBUGINFO_PDB_NATIVE_DBISTREAMBUILDER_H
     10 #define LLVM_DEBUGINFO_PDB_NATIVE_DBISTREAMBUILDER_H
     11 
     12 #include "llvm/ADT/Optional.h"
     13 #include "llvm/ADT/StringSet.h"
     14 #include "llvm/BinaryFormat/COFF.h"
     15 #include "llvm/Support/Error.h"
     16 
     17 #include "llvm/DebugInfo/CodeView/DebugFrameDataSubsection.h"
     18 #include "llvm/DebugInfo/PDB/Native/PDBFile.h"
     19 #include "llvm/DebugInfo/PDB/Native/PDBStringTableBuilder.h"
     20 #include "llvm/DebugInfo/PDB/Native/RawConstants.h"
     21 #include "llvm/DebugInfo/PDB/PDBTypes.h"
     22 #include "llvm/Support/BinaryByteStream.h"
     23 #include "llvm/Support/BinaryStreamReader.h"
     24 #include "llvm/Support/Endian.h"
     25 
     26 namespace llvm {
     27 namespace codeview {
     28 struct FrameData;
     29 }
     30 namespace msf {
     31 class MSFBuilder;
     32 }
     33 namespace object {
     34 struct coff_section;
     35 struct FpoData;
     36 }
     37 namespace pdb {
     38 class DbiStream;
     39 struct DbiStreamHeader;
     40 class DbiModuleDescriptorBuilder;
     41 class PDBFile;
     42 
     43 class DbiStreamBuilder {
     44 public:
     45   DbiStreamBuilder(msf::MSFBuilder &Msf);
     46   ~DbiStreamBuilder();
     47 
     48   DbiStreamBuilder(const DbiStreamBuilder &) = delete;
     49   DbiStreamBuilder &operator=(const DbiStreamBuilder &) = delete;
     50 
     51   void setVersionHeader(PdbRaw_DbiVer V);
     52   void setAge(uint32_t A);
     53   void setBuildNumber(uint16_t B);
     54   void setBuildNumber(uint8_t Major, uint8_t Minor);
     55   void setPdbDllVersion(uint16_t V);
     56   void setPdbDllRbld(uint16_t R);
     57   void setFlags(uint16_t F);
     58   void setMachineType(PDB_Machine M);
     59   void setMachineType(COFF::MachineTypes M);
     60 
     61   // Add given bytes as a new stream.
     62   Error addDbgStream(pdb::DbgHeaderType Type, ArrayRef<uint8_t> Data);
     63 
     64   uint32_t addECName(StringRef Name);
     65 
     66   uint32_t calculateSerializedLength() const;
     67 
     68   void setGlobalsStreamIndex(uint32_t Index);
     69   void setPublicsStreamIndex(uint32_t Index);
     70   void setSymbolRecordStreamIndex(uint32_t Index);
     71   void addNewFpoData(const codeview::FrameData &FD);
     72   void addOldFpoData(const object::FpoData &Fpo);
     73 
     74   Expected<DbiModuleDescriptorBuilder &> addModuleInfo(StringRef ModuleName);
     75   Error addModuleSourceFile(DbiModuleDescriptorBuilder &Module, StringRef File);
     76   Expected<uint32_t> getSourceFileNameIndex(StringRef FileName);
     77 
     78   Error finalizeMsfLayout();
     79 
     80   Error commit(const msf::MSFLayout &Layout, WritableBinaryStreamRef MsfBuffer);
     81 
     82   void addSectionContrib(const SectionContrib &SC) {
     83     SectionContribs.emplace_back(SC);
     84   }
     85 
     86   // Populate the Section Map from COFF section headers.
     87   void createSectionMap(ArrayRef<llvm::object::coff_section> SecHdrs);
     88 
     89 private:
     90   struct DebugStream {
     91     std::function<Error(BinaryStreamWriter &)> WriteFn;
     92     uint32_t Size = 0;
     93     uint16_t StreamNumber = kInvalidStreamIndex;
     94   };
     95 
     96   Error finalize();
     97   uint32_t calculateModiSubstreamSize() const;
     98   uint32_t calculateNamesOffset() const;
     99   uint32_t calculateSectionContribsStreamSize() const;
    100   uint32_t calculateSectionMapStreamSize() const;
    101   uint32_t calculateFileInfoSubstreamSize() const;
    102   uint32_t calculateNamesBufferSize() const;
    103   uint32_t calculateDbgStreamsSize() const;
    104 
    105   Error generateFileInfoSubstream();
    106 
    107   msf::MSFBuilder &Msf;
    108   BumpPtrAllocator &Allocator;
    109 
    110   Optional<PdbRaw_DbiVer> VerHeader;
    111   uint32_t Age;
    112   uint16_t BuildNumber;
    113   uint16_t PdbDllVersion;
    114   uint16_t PdbDllRbld;
    115   uint16_t Flags;
    116   PDB_Machine MachineType;
    117   uint32_t GlobalsStreamIndex = kInvalidStreamIndex;
    118   uint32_t PublicsStreamIndex = kInvalidStreamIndex;
    119   uint32_t SymRecordStreamIndex = kInvalidStreamIndex;
    120 
    121   const DbiStreamHeader *Header;
    122 
    123   std::vector<std::unique_ptr<DbiModuleDescriptorBuilder>> ModiList;
    124 
    125   Optional<codeview::DebugFrameDataSubsection> NewFpoData;
    126   std::vector<object::FpoData> OldFpoData;
    127 
    128   StringMap<uint32_t> SourceFileNames;
    129 
    130   PDBStringTableBuilder ECNamesBuilder;
    131   WritableBinaryStreamRef NamesBuffer;
    132   MutableBinaryByteStream FileInfoBuffer;
    133   std::vector<SectionContrib> SectionContribs;
    134   std::vector<SecMapEntry> SectionMap;
    135   std::array<Optional<DebugStream>, (int)DbgHeaderType::Max> DbgStreams;
    136 };
    137 }
    138 }
    139 
    140 #endif
    141