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