DbiStreamBuilder.h 4.4 KB

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