DbiStreamBuilder.h 4.4 KB

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