DbiModuleDescriptorBuilder.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. //===- DbiModuleDescriptorBuilder.cpp - PDB Mod Info 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. #include "llvm/DebugInfo/PDB/Native/DbiModuleDescriptorBuilder.h"
  9. #include "llvm/ADT/ArrayRef.h"
  10. #include "llvm/BinaryFormat/COFF.h"
  11. #include "llvm/DebugInfo/CodeView/CodeView.h"
  12. #include "llvm/DebugInfo/CodeView/DebugSubsectionRecord.h"
  13. #include "llvm/DebugInfo/MSF/MSFBuilder.h"
  14. #include "llvm/DebugInfo/MSF/MappedBlockStream.h"
  15. #include "llvm/DebugInfo/PDB/Native/RawConstants.h"
  16. #include "llvm/DebugInfo/PDB/Native/RawError.h"
  17. #include "llvm/Support/BinaryStreamWriter.h"
  18. using namespace llvm;
  19. using namespace llvm::codeview;
  20. using namespace llvm::msf;
  21. using namespace llvm::pdb;
  22. namespace llvm {
  23. namespace codeview {
  24. class DebugSubsection;
  25. }
  26. } // namespace llvm
  27. static uint32_t calculateDiSymbolStreamSize(uint32_t SymbolByteSize,
  28. uint32_t C13Size) {
  29. uint32_t Size = sizeof(uint32_t); // Signature
  30. Size += alignTo(SymbolByteSize, 4); // Symbol Data
  31. Size += 0; // TODO: Layout.C11Bytes
  32. Size += C13Size; // C13 Debug Info Size
  33. Size += sizeof(uint32_t); // GlobalRefs substream size (always 0)
  34. Size += 0; // GlobalRefs substream bytes
  35. return Size;
  36. }
  37. DbiModuleDescriptorBuilder::DbiModuleDescriptorBuilder(StringRef ModuleName,
  38. uint32_t ModIndex,
  39. msf::MSFBuilder &Msf)
  40. : MSF(Msf), ModuleName(std::string(ModuleName)) {
  41. ::memset(&Layout, 0, sizeof(Layout));
  42. Layout.Mod = ModIndex;
  43. }
  44. DbiModuleDescriptorBuilder::~DbiModuleDescriptorBuilder() = default;
  45. uint16_t DbiModuleDescriptorBuilder::getStreamIndex() const {
  46. return Layout.ModDiStream;
  47. }
  48. void DbiModuleDescriptorBuilder::setObjFileName(StringRef Name) {
  49. ObjFileName = std::string(Name);
  50. }
  51. void DbiModuleDescriptorBuilder::setPdbFilePathNI(uint32_t NI) {
  52. PdbFilePathNI = NI;
  53. }
  54. void DbiModuleDescriptorBuilder::setFirstSectionContrib(
  55. const SectionContrib &SC) {
  56. Layout.SC = SC;
  57. }
  58. void DbiModuleDescriptorBuilder::addSymbol(CVSymbol Symbol) {
  59. // Defer to the bulk API. It does the same thing.
  60. addSymbolsInBulk(Symbol.data());
  61. }
  62. void DbiModuleDescriptorBuilder::addSymbolsInBulk(
  63. ArrayRef<uint8_t> BulkSymbols) {
  64. // Do nothing for empty runs of symbols.
  65. if (BulkSymbols.empty())
  66. return;
  67. Symbols.push_back(SymbolListWrapper(BulkSymbols));
  68. // Symbols written to a PDB file are required to be 4 byte aligned. The same
  69. // is not true of object files.
  70. assert(BulkSymbols.size() % alignOf(CodeViewContainer::Pdb) == 0 &&
  71. "Invalid Symbol alignment!");
  72. SymbolByteSize += BulkSymbols.size();
  73. }
  74. void DbiModuleDescriptorBuilder::addUnmergedSymbols(void *SymSrc,
  75. uint32_t SymLength) {
  76. assert(SymLength > 0);
  77. Symbols.push_back(SymbolListWrapper(SymSrc, SymLength));
  78. // Symbols written to a PDB file are required to be 4 byte aligned. The same
  79. // is not true of object files.
  80. assert(SymLength % alignOf(CodeViewContainer::Pdb) == 0 &&
  81. "Invalid Symbol alignment!");
  82. SymbolByteSize += SymLength;
  83. }
  84. void DbiModuleDescriptorBuilder::addSourceFile(StringRef Path) {
  85. SourceFiles.push_back(std::string(Path));
  86. }
  87. uint32_t DbiModuleDescriptorBuilder::calculateC13DebugInfoSize() const {
  88. uint32_t Result = 0;
  89. for (const auto &Builder : C13Builders) {
  90. Result += Builder.calculateSerializedLength();
  91. }
  92. return Result;
  93. }
  94. uint32_t DbiModuleDescriptorBuilder::calculateSerializedLength() const {
  95. uint32_t L = sizeof(Layout);
  96. uint32_t M = ModuleName.size() + 1;
  97. uint32_t O = ObjFileName.size() + 1;
  98. return alignTo(L + M + O, sizeof(uint32_t));
  99. }
  100. void DbiModuleDescriptorBuilder::finalize() {
  101. Layout.FileNameOffs = 0; // TODO: Fix this
  102. Layout.Flags = 0; // TODO: Fix this
  103. Layout.C11Bytes = 0;
  104. Layout.C13Bytes = calculateC13DebugInfoSize();
  105. (void)Layout.Mod; // Set in constructor
  106. (void)Layout.ModDiStream; // Set in finalizeMsfLayout
  107. Layout.NumFiles = SourceFiles.size();
  108. Layout.PdbFilePathNI = PdbFilePathNI;
  109. Layout.SrcFileNameNI = 0;
  110. // This value includes both the signature field as well as the record bytes
  111. // from the symbol stream.
  112. Layout.SymBytes =
  113. Layout.ModDiStream == kInvalidStreamIndex ? 0 : getNextSymbolOffset();
  114. }
  115. Error DbiModuleDescriptorBuilder::finalizeMsfLayout() {
  116. this->Layout.ModDiStream = kInvalidStreamIndex;
  117. uint32_t C13Size = calculateC13DebugInfoSize();
  118. if (!C13Size && !SymbolByteSize)
  119. return Error::success();
  120. auto ExpectedSN =
  121. MSF.addStream(calculateDiSymbolStreamSize(SymbolByteSize, C13Size));
  122. if (!ExpectedSN)
  123. return ExpectedSN.takeError();
  124. Layout.ModDiStream = *ExpectedSN;
  125. return Error::success();
  126. }
  127. Error DbiModuleDescriptorBuilder::commit(BinaryStreamWriter &ModiWriter) {
  128. // We write the Modi record to the `ModiWriter`, but we additionally write its
  129. // symbol stream to a brand new stream.
  130. if (auto EC = ModiWriter.writeObject(Layout))
  131. return EC;
  132. if (auto EC = ModiWriter.writeCString(ModuleName))
  133. return EC;
  134. if (auto EC = ModiWriter.writeCString(ObjFileName))
  135. return EC;
  136. if (auto EC = ModiWriter.padToAlignment(sizeof(uint32_t)))
  137. return EC;
  138. return Error::success();
  139. }
  140. Error DbiModuleDescriptorBuilder::commitSymbolStream(
  141. const msf::MSFLayout &MsfLayout, WritableBinaryStreamRef MsfBuffer) {
  142. if (Layout.ModDiStream == kInvalidStreamIndex)
  143. return Error::success();
  144. auto NS = WritableMappedBlockStream::createIndexedStream(
  145. MsfLayout, MsfBuffer, Layout.ModDiStream, MSF.getAllocator());
  146. WritableBinaryStreamRef Ref(*NS);
  147. BinaryStreamWriter SymbolWriter(Ref);
  148. // Write the symbols.
  149. if (auto EC = SymbolWriter.writeInteger<uint32_t>(COFF::DEBUG_SECTION_MAGIC))
  150. return EC;
  151. for (const SymbolListWrapper &Sym : Symbols) {
  152. if (Sym.NeedsToBeMerged) {
  153. assert(MergeSymsCallback);
  154. if (auto EC = MergeSymsCallback(MergeSymsCtx, Sym.SymPtr, SymbolWriter))
  155. return EC;
  156. } else {
  157. if (auto EC = SymbolWriter.writeBytes(Sym.asArray()))
  158. return EC;
  159. }
  160. }
  161. // Apply the string table fixups.
  162. auto SavedOffset = SymbolWriter.getOffset();
  163. for (const StringTableFixup &Fixup : StringTableFixups) {
  164. SymbolWriter.setOffset(Fixup.SymOffsetOfReference);
  165. if (auto E = SymbolWriter.writeInteger<uint32_t>(Fixup.StrTabOffset))
  166. return E;
  167. }
  168. SymbolWriter.setOffset(SavedOffset);
  169. assert(SymbolWriter.getOffset() % alignOf(CodeViewContainer::Pdb) == 0 &&
  170. "Invalid debug section alignment!");
  171. // TODO: Write C11 Line data
  172. for (const auto &Builder : C13Builders) {
  173. if (auto EC = Builder.commit(SymbolWriter, CodeViewContainer::Pdb))
  174. return EC;
  175. }
  176. // TODO: Figure out what GlobalRefs substream actually is and populate it.
  177. if (auto EC = SymbolWriter.writeInteger<uint32_t>(0))
  178. return EC;
  179. if (SymbolWriter.bytesRemaining() > 0)
  180. return make_error<RawError>(raw_error_code::stream_too_long);
  181. return Error::success();
  182. }
  183. void DbiModuleDescriptorBuilder::addDebugSubsection(
  184. std::shared_ptr<DebugSubsection> Subsection) {
  185. assert(Subsection);
  186. C13Builders.push_back(DebugSubsectionRecordBuilder(std::move(Subsection)));
  187. }
  188. void DbiModuleDescriptorBuilder::addDebugSubsection(
  189. const DebugSubsectionRecord &SubsectionContents) {
  190. C13Builders.push_back(DebugSubsectionRecordBuilder(SubsectionContents));
  191. }