10b57cec5SDimitry Andric //===- DbiStreamBuilder.h - PDB Dbi Stream Creation -------------*- C++ -*-===// 20b57cec5SDimitry Andric // 30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 60b57cec5SDimitry Andric // 70b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 80b57cec5SDimitry Andric 9fe6060f1SDimitry Andric #ifndef LLVM_DEBUGINFO_PDB_NATIVE_DBISTREAMBUILDER_H 10fe6060f1SDimitry Andric #define LLVM_DEBUGINFO_PDB_NATIVE_DBISTREAMBUILDER_H 110b57cec5SDimitry Andric 1281ad6265SDimitry Andric #include "llvm/ADT/StringMap.h" 1381ad6265SDimitry Andric #include "llvm/ADT/StringRef.h" 140b57cec5SDimitry Andric #include "llvm/BinaryFormat/COFF.h" 1581ad6265SDimitry Andric #include "llvm/Object/COFF.h" 1681ad6265SDimitry Andric #include "llvm/Support/Allocator.h" 170b57cec5SDimitry Andric #include "llvm/Support/Error.h" 180b57cec5SDimitry Andric 190b57cec5SDimitry Andric #include "llvm/DebugInfo/CodeView/DebugFrameDataSubsection.h" 200b57cec5SDimitry Andric #include "llvm/DebugInfo/PDB/Native/PDBStringTableBuilder.h" 210b57cec5SDimitry Andric #include "llvm/DebugInfo/PDB/Native/RawConstants.h" 2281ad6265SDimitry Andric #include "llvm/DebugInfo/PDB/Native/RawTypes.h" 230b57cec5SDimitry Andric #include "llvm/DebugInfo/PDB/PDBTypes.h" 240b57cec5SDimitry Andric #include "llvm/Support/BinaryByteStream.h" 2581ad6265SDimitry Andric #include "llvm/Support/BinaryStreamRef.h" 260b57cec5SDimitry Andric 270b57cec5SDimitry Andric namespace llvm { 2881ad6265SDimitry Andric 2981ad6265SDimitry Andric class BinaryStreamWriter; 300b57cec5SDimitry Andric namespace codeview { 310b57cec5SDimitry Andric struct FrameData; 320b57cec5SDimitry Andric } 330b57cec5SDimitry Andric namespace msf { 340b57cec5SDimitry Andric class MSFBuilder; 3581ad6265SDimitry Andric struct MSFLayout; 360b57cec5SDimitry Andric } 370b57cec5SDimitry Andric namespace pdb { 380b57cec5SDimitry Andric class DbiModuleDescriptorBuilder; 390b57cec5SDimitry Andric 400b57cec5SDimitry Andric class DbiStreamBuilder { 410b57cec5SDimitry Andric public: 420b57cec5SDimitry Andric DbiStreamBuilder(msf::MSFBuilder &Msf); 430b57cec5SDimitry Andric ~DbiStreamBuilder(); 440b57cec5SDimitry Andric 450b57cec5SDimitry Andric DbiStreamBuilder(const DbiStreamBuilder &) = delete; 460b57cec5SDimitry Andric DbiStreamBuilder &operator=(const DbiStreamBuilder &) = delete; 470b57cec5SDimitry Andric 480b57cec5SDimitry Andric void setVersionHeader(PdbRaw_DbiVer V); 490b57cec5SDimitry Andric void setAge(uint32_t A); 500b57cec5SDimitry Andric void setBuildNumber(uint16_t B); 510b57cec5SDimitry Andric void setBuildNumber(uint8_t Major, uint8_t Minor); 520b57cec5SDimitry Andric void setPdbDllVersion(uint16_t V); 530b57cec5SDimitry Andric void setPdbDllRbld(uint16_t R); 540b57cec5SDimitry Andric void setFlags(uint16_t F); 550b57cec5SDimitry Andric void setMachineType(PDB_Machine M); 560b57cec5SDimitry Andric void setMachineType(COFF::MachineTypes M); 570b57cec5SDimitry Andric 580b57cec5SDimitry Andric // Add given bytes as a new stream. 590b57cec5SDimitry Andric Error addDbgStream(pdb::DbgHeaderType Type, ArrayRef<uint8_t> Data); 600b57cec5SDimitry Andric 610b57cec5SDimitry Andric uint32_t addECName(StringRef Name); 620b57cec5SDimitry Andric 630b57cec5SDimitry Andric uint32_t calculateSerializedLength() const; 640b57cec5SDimitry Andric 650b57cec5SDimitry Andric void setGlobalsStreamIndex(uint32_t Index); 660b57cec5SDimitry Andric void setPublicsStreamIndex(uint32_t Index); 670b57cec5SDimitry Andric void setSymbolRecordStreamIndex(uint32_t Index); 680b57cec5SDimitry Andric void addNewFpoData(const codeview::FrameData &FD); 690b57cec5SDimitry Andric void addOldFpoData(const object::FpoData &Fpo); 700b57cec5SDimitry Andric 710b57cec5SDimitry Andric Expected<DbiModuleDescriptorBuilder &> addModuleInfo(StringRef ModuleName); 720b57cec5SDimitry Andric Error addModuleSourceFile(DbiModuleDescriptorBuilder &Module, StringRef File); 730b57cec5SDimitry Andric Expected<uint32_t> getSourceFileNameIndex(StringRef FileName); 740b57cec5SDimitry Andric 750b57cec5SDimitry Andric Error finalizeMsfLayout(); 760b57cec5SDimitry Andric 770b57cec5SDimitry Andric Error commit(const msf::MSFLayout &Layout, WritableBinaryStreamRef MsfBuffer); 780b57cec5SDimitry Andric addSectionContrib(const SectionContrib & SC)790b57cec5SDimitry Andric void addSectionContrib(const SectionContrib &SC) { 800b57cec5SDimitry Andric SectionContribs.emplace_back(SC); 810b57cec5SDimitry Andric } 820b57cec5SDimitry Andric 835ffd83dbSDimitry Andric // Populate the Section Map from COFF section headers. 845ffd83dbSDimitry Andric void createSectionMap(ArrayRef<llvm::object::coff_section> SecHdrs); 850b57cec5SDimitry Andric 860b57cec5SDimitry Andric private: 870b57cec5SDimitry Andric struct DebugStream { 880b57cec5SDimitry Andric std::function<Error(BinaryStreamWriter &)> WriteFn; 890b57cec5SDimitry Andric uint32_t Size = 0; 900b57cec5SDimitry Andric uint16_t StreamNumber = kInvalidStreamIndex; 910b57cec5SDimitry Andric }; 920b57cec5SDimitry Andric 930b57cec5SDimitry Andric Error finalize(); 940b57cec5SDimitry Andric uint32_t calculateModiSubstreamSize() const; 950b57cec5SDimitry Andric uint32_t calculateNamesOffset() const; 960b57cec5SDimitry Andric uint32_t calculateSectionContribsStreamSize() const; 970b57cec5SDimitry Andric uint32_t calculateSectionMapStreamSize() const; 980b57cec5SDimitry Andric uint32_t calculateFileInfoSubstreamSize() const; 990b57cec5SDimitry Andric uint32_t calculateNamesBufferSize() const; 1000b57cec5SDimitry Andric uint32_t calculateDbgStreamsSize() const; 1010b57cec5SDimitry Andric 1020b57cec5SDimitry Andric Error generateFileInfoSubstream(); 1030b57cec5SDimitry Andric 1040b57cec5SDimitry Andric msf::MSFBuilder &Msf; 1050b57cec5SDimitry Andric BumpPtrAllocator &Allocator; 1060b57cec5SDimitry Andric 107*bdd1243dSDimitry Andric std::optional<PdbRaw_DbiVer> VerHeader; 1080b57cec5SDimitry Andric uint32_t Age; 1090b57cec5SDimitry Andric uint16_t BuildNumber; 1100b57cec5SDimitry Andric uint16_t PdbDllVersion; 1110b57cec5SDimitry Andric uint16_t PdbDllRbld; 1120b57cec5SDimitry Andric uint16_t Flags; 1130b57cec5SDimitry Andric PDB_Machine MachineType; 1140b57cec5SDimitry Andric uint32_t GlobalsStreamIndex = kInvalidStreamIndex; 1150b57cec5SDimitry Andric uint32_t PublicsStreamIndex = kInvalidStreamIndex; 1160b57cec5SDimitry Andric uint32_t SymRecordStreamIndex = kInvalidStreamIndex; 1170b57cec5SDimitry Andric 1180b57cec5SDimitry Andric const DbiStreamHeader *Header; 1190b57cec5SDimitry Andric 1200b57cec5SDimitry Andric std::vector<std::unique_ptr<DbiModuleDescriptorBuilder>> ModiList; 1210b57cec5SDimitry Andric 122*bdd1243dSDimitry Andric std::optional<codeview::DebugFrameDataSubsection> NewFpoData; 1230b57cec5SDimitry Andric std::vector<object::FpoData> OldFpoData; 1240b57cec5SDimitry Andric 1250b57cec5SDimitry Andric StringMap<uint32_t> SourceFileNames; 1260b57cec5SDimitry Andric 1270b57cec5SDimitry Andric PDBStringTableBuilder ECNamesBuilder; 1280b57cec5SDimitry Andric WritableBinaryStreamRef NamesBuffer; 1290b57cec5SDimitry Andric MutableBinaryByteStream FileInfoBuffer; 1300b57cec5SDimitry Andric std::vector<SectionContrib> SectionContribs; 1315ffd83dbSDimitry Andric std::vector<SecMapEntry> SectionMap; 132*bdd1243dSDimitry Andric std::array<std::optional<DebugStream>, (int)DbgHeaderType::Max> DbgStreams; 1330b57cec5SDimitry Andric }; 13481ad6265SDimitry Andric } // namespace pdb 1350b57cec5SDimitry Andric } 1360b57cec5SDimitry Andric 1370b57cec5SDimitry Andric #endif 138