DbiModuleDescriptorBuilder.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- DbiModuleDescriptorBuilder.h - PDB module information ----*- 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_DBIMODULEDESCRIPTORBUILDER_H
  14. #define LLVM_DEBUGINFO_PDB_NATIVE_DBIMODULEDESCRIPTORBUILDER_H
  15. #include "llvm/ADT/StringRef.h"
  16. #include "llvm/DebugInfo/CodeView/DebugChecksumsSubsection.h"
  17. #include "llvm/DebugInfo/CodeView/DebugInlineeLinesSubsection.h"
  18. #include "llvm/DebugInfo/CodeView/DebugLinesSubsection.h"
  19. #include "llvm/DebugInfo/CodeView/DebugSubsectionRecord.h"
  20. #include "llvm/DebugInfo/CodeView/SymbolRecord.h"
  21. #include "llvm/DebugInfo/PDB/Native/RawTypes.h"
  22. #include "llvm/Support/Error.h"
  23. #include <cstdint>
  24. #include <string>
  25. #include <vector>
  26. namespace llvm {
  27. class BinaryStreamWriter;
  28. namespace codeview {
  29. class DebugSubsectionRecordBuilder;
  30. }
  31. namespace msf {
  32. class MSFBuilder;
  33. struct MSFLayout;
  34. }
  35. namespace pdb {
  36. // Represents merged or unmerged symbols. Merged symbols can be written to the
  37. // output file as is, but unmerged symbols must be rewritten first. In either
  38. // case, the size must be known up front.
  39. struct SymbolListWrapper {
  40. explicit SymbolListWrapper(ArrayRef<uint8_t> Syms)
  41. : SymPtr(const_cast<uint8_t *>(Syms.data())), SymSize(Syms.size()),
  42. NeedsToBeMerged(false) {}
  43. explicit SymbolListWrapper(void *SymSrc, uint32_t Length)
  44. : SymPtr(SymSrc), SymSize(Length), NeedsToBeMerged(true) {}
  45. ArrayRef<uint8_t> asArray() const {
  46. return ArrayRef<uint8_t>(static_cast<const uint8_t *>(SymPtr), SymSize);
  47. }
  48. uint32_t size() const { return SymSize; }
  49. void *SymPtr = nullptr;
  50. uint32_t SymSize = 0;
  51. bool NeedsToBeMerged = false;
  52. };
  53. /// Represents a string table reference at some offset in the module symbol
  54. /// stream.
  55. struct StringTableFixup {
  56. uint32_t StrTabOffset = 0;
  57. uint32_t SymOffsetOfReference = 0;
  58. };
  59. class DbiModuleDescriptorBuilder {
  60. friend class DbiStreamBuilder;
  61. public:
  62. DbiModuleDescriptorBuilder(StringRef ModuleName, uint32_t ModIndex,
  63. msf::MSFBuilder &Msf);
  64. ~DbiModuleDescriptorBuilder();
  65. DbiModuleDescriptorBuilder(const DbiModuleDescriptorBuilder &) = delete;
  66. DbiModuleDescriptorBuilder &
  67. operator=(const DbiModuleDescriptorBuilder &) = delete;
  68. void setPdbFilePathNI(uint32_t NI);
  69. void setObjFileName(StringRef Name);
  70. // Callback to merge one source of unmerged symbols.
  71. using MergeSymbolsCallback = Error (*)(void *Ctx, void *Symbols,
  72. BinaryStreamWriter &Writer);
  73. void setMergeSymbolsCallback(void *Ctx, MergeSymbolsCallback Callback) {
  74. MergeSymsCtx = Ctx;
  75. MergeSymsCallback = Callback;
  76. }
  77. void setStringTableFixups(std::vector<StringTableFixup> &&Fixups) {
  78. StringTableFixups = std::move(Fixups);
  79. }
  80. void setFirstSectionContrib(const SectionContrib &SC);
  81. void addSymbol(codeview::CVSymbol Symbol);
  82. void addSymbolsInBulk(ArrayRef<uint8_t> BulkSymbols);
  83. // Add symbols of known size which will be merged (rewritten) when committing
  84. // the PDB to disk.
  85. void addUnmergedSymbols(void *SymSrc, uint32_t SymLength);
  86. void
  87. addDebugSubsection(std::shared_ptr<codeview::DebugSubsection> Subsection);
  88. void
  89. addDebugSubsection(const codeview::DebugSubsectionRecord &SubsectionContents);
  90. uint16_t getStreamIndex() const;
  91. StringRef getModuleName() const { return ModuleName; }
  92. StringRef getObjFileName() const { return ObjFileName; }
  93. unsigned getModuleIndex() const { return Layout.Mod; }
  94. ArrayRef<std::string> source_files() const {
  95. return makeArrayRef(SourceFiles);
  96. }
  97. uint32_t calculateSerializedLength() const;
  98. /// Return the offset within the module symbol stream of the next symbol
  99. /// record passed to addSymbol. Add four to account for the signature.
  100. uint32_t getNextSymbolOffset() const { return SymbolByteSize + 4; }
  101. void finalize();
  102. Error finalizeMsfLayout();
  103. /// Commit the DBI descriptor to the DBI stream.
  104. Error commit(BinaryStreamWriter &ModiWriter);
  105. /// Commit the accumulated symbols to the module symbol stream. Safe to call
  106. /// in parallel on different DbiModuleDescriptorBuilder objects. Only modifies
  107. /// the pre-allocated stream in question.
  108. Error commitSymbolStream(const msf::MSFLayout &MsfLayout,
  109. WritableBinaryStreamRef MsfBuffer);
  110. private:
  111. uint32_t calculateC13DebugInfoSize() const;
  112. void addSourceFile(StringRef Path);
  113. msf::MSFBuilder &MSF;
  114. uint32_t SymbolByteSize = 0;
  115. uint32_t PdbFilePathNI = 0;
  116. std::string ModuleName;
  117. std::string ObjFileName;
  118. std::vector<std::string> SourceFiles;
  119. std::vector<SymbolListWrapper> Symbols;
  120. void *MergeSymsCtx = nullptr;
  121. MergeSymbolsCallback MergeSymsCallback = nullptr;
  122. std::vector<StringTableFixup> StringTableFixups;
  123. std::vector<codeview::DebugSubsectionRecordBuilder> C13Builders;
  124. ModuleInfoHeader Layout;
  125. };
  126. } // end namespace pdb
  127. } // end namespace llvm
  128. #endif // LLVM_DEBUGINFO_PDB_NATIVE_DBIMODULEDESCRIPTORBUILDER_H
  129. #ifdef __GNUC__
  130. #pragma GCC diagnostic pop
  131. #endif